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 && *bio != IO_BLOCKED)
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);
271 static void raid1_end_read_request(struct bio *bio, int error)
273 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
274 r1bio_t *r1_bio = bio->bi_private;
276 conf_t *conf = r1_bio->mddev->private;
278 mirror = r1_bio->read_disk;
280 * this branch is our 'one mirror IO has finished' event handler:
282 update_head_pos(mirror, r1_bio);
285 set_bit(R1BIO_Uptodate, &r1_bio->state);
287 /* If all other devices have failed, we want to return
288 * the error upwards rather than fail the last device.
289 * Here we redefine "uptodate" to mean "Don't want to retry"
292 spin_lock_irqsave(&conf->device_lock, flags);
293 if (r1_bio->mddev->degraded == conf->raid_disks ||
294 (r1_bio->mddev->degraded == conf->raid_disks-1 &&
295 !test_bit(Faulty, &conf->mirrors[mirror].rdev->flags)))
297 spin_unlock_irqrestore(&conf->device_lock, flags);
301 raid_end_bio_io(r1_bio);
306 char b[BDEVNAME_SIZE];
308 KERN_ERR "md/raid1:%s: %s: "
309 "rescheduling sector %llu\n",
311 bdevname(conf->mirrors[mirror].rdev->bdev,
313 (unsigned long long)r1_bio->sector);
314 set_bit(R1BIO_ReadError, &r1_bio->state);
315 reschedule_retry(r1_bio);
318 rdev_dec_pending(conf->mirrors[mirror].rdev, conf->mddev);
321 static void r1_bio_write_done(r1bio_t *r1_bio)
323 if (atomic_dec_and_test(&r1_bio->remaining))
325 /* it really is the end of this request */
326 if (test_bit(R1BIO_BehindIO, &r1_bio->state)) {
327 /* free extra copy of the data pages */
328 int i = r1_bio->behind_page_count;
330 safe_put_page(r1_bio->behind_pages[i]);
331 kfree(r1_bio->behind_pages);
332 r1_bio->behind_pages = NULL;
334 /* clear the bitmap if all writes complete successfully */
335 bitmap_endwrite(r1_bio->mddev->bitmap, r1_bio->sector,
337 !test_bit(R1BIO_Degraded, &r1_bio->state),
338 test_bit(R1BIO_BehindIO, &r1_bio->state));
339 md_write_end(r1_bio->mddev);
340 raid_end_bio_io(r1_bio);
344 static void raid1_end_write_request(struct bio *bio, int error)
346 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
347 r1bio_t *r1_bio = bio->bi_private;
348 int mirror, behind = test_bit(R1BIO_BehindIO, &r1_bio->state);
349 conf_t *conf = r1_bio->mddev->private;
350 struct bio *to_put = NULL;
353 for (mirror = 0; mirror < conf->raid_disks; mirror++)
354 if (r1_bio->bios[mirror] == bio)
358 * 'one mirror IO has finished' event handler:
360 r1_bio->bios[mirror] = NULL;
363 md_error(r1_bio->mddev, conf->mirrors[mirror].rdev);
364 /* an I/O failed, we can't clear the bitmap */
365 set_bit(R1BIO_Degraded, &r1_bio->state);
368 * Set R1BIO_Uptodate in our master bio, so that we
369 * will return a good error code for to the higher
370 * levels even if IO on some other mirrored buffer
373 * The 'master' represents the composite IO operation
374 * to user-side. So if something waits for IO, then it
375 * will wait for the 'master' bio.
377 set_bit(R1BIO_Uptodate, &r1_bio->state);
379 update_head_pos(mirror, r1_bio);
382 if (test_bit(WriteMostly, &conf->mirrors[mirror].rdev->flags))
383 atomic_dec(&r1_bio->behind_remaining);
386 * In behind mode, we ACK the master bio once the I/O
387 * has safely reached all non-writemostly
388 * disks. Setting the Returned bit ensures that this
389 * gets done only once -- we don't ever want to return
390 * -EIO here, instead we'll wait
392 if (atomic_read(&r1_bio->behind_remaining) >= (atomic_read(&r1_bio->remaining)-1) &&
393 test_bit(R1BIO_Uptodate, &r1_bio->state)) {
394 /* Maybe we can return now */
395 if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
396 struct bio *mbio = r1_bio->master_bio;
397 PRINTK(KERN_DEBUG "raid1: behind end write sectors %llu-%llu\n",
398 (unsigned long long) mbio->bi_sector,
399 (unsigned long long) mbio->bi_sector +
400 (mbio->bi_size >> 9) - 1);
401 call_bio_endio(r1_bio);
405 rdev_dec_pending(conf->mirrors[mirror].rdev, conf->mddev);
408 * Let's see if all mirrored write operations have finished
411 r1_bio_write_done(r1_bio);
419 * This routine returns the disk from which the requested read should
420 * be done. There is a per-array 'next expected sequential IO' sector
421 * number - if this matches on the next IO then we use the last disk.
422 * There is also a per-disk 'last know head position' sector that is
423 * maintained from IRQ contexts, both the normal and the resync IO
424 * completion handlers update this position correctly. If there is no
425 * perfect sequential match then we pick the disk whose head is closest.
427 * If there are 2 mirrors in the same 2 devices, performance degrades
428 * because position is mirror, not device based.
430 * The rdev for the device selected will have nr_pending incremented.
432 static int read_balance(conf_t *conf, r1bio_t *r1_bio, int *max_sectors)
434 const sector_t this_sector = r1_bio->sector;
436 int best_good_sectors;
446 * Check if we can balance. We can balance on the whole
447 * device if no resync is going on, or below the resync window.
448 * We take the first readable disk when above the resync window.
451 sectors = r1_bio->sectors;
453 best_dist = MaxSector;
454 best_good_sectors = 0;
456 if (conf->mddev->recovery_cp < MaxSector &&
457 (this_sector + sectors >= conf->next_resync)) {
462 start_disk = conf->last_used;
465 for (i = 0 ; i < conf->raid_disks ; i++) {
470 int disk = start_disk + i;
471 if (disk >= conf->raid_disks)
472 disk -= conf->raid_disks;
474 rdev = rcu_dereference(conf->mirrors[disk].rdev);
475 if (r1_bio->bios[disk] == IO_BLOCKED
477 || test_bit(Faulty, &rdev->flags))
479 if (!test_bit(In_sync, &rdev->flags) &&
480 rdev->recovery_offset < this_sector + sectors)
482 if (test_bit(WriteMostly, &rdev->flags)) {
483 /* Don't balance among write-mostly, just
484 * use the first as a last resort */
489 /* This is a reasonable device to use. It might
492 if (is_badblock(rdev, this_sector, sectors,
493 &first_bad, &bad_sectors)) {
494 if (best_dist < MaxSector)
495 /* already have a better device */
497 if (first_bad <= this_sector) {
498 /* cannot read here. If this is the 'primary'
499 * device, then we must not read beyond
500 * bad_sectors from another device..
502 bad_sectors -= (this_sector - first_bad);
503 if (choose_first && sectors > bad_sectors)
504 sectors = bad_sectors;
505 if (best_good_sectors > sectors)
506 best_good_sectors = sectors;
509 sector_t good_sectors = first_bad - this_sector;
510 if (good_sectors > best_good_sectors) {
511 best_good_sectors = good_sectors;
519 best_good_sectors = sectors;
521 dist = abs(this_sector - conf->mirrors[disk].head_position);
523 /* Don't change to another disk for sequential reads */
524 || conf->next_seq_sect == this_sector
526 /* If device is idle, use it */
527 || atomic_read(&rdev->nr_pending) == 0) {
531 if (dist < best_dist) {
537 if (best_disk >= 0) {
538 rdev = rcu_dereference(conf->mirrors[best_disk].rdev);
541 atomic_inc(&rdev->nr_pending);
542 if (test_bit(Faulty, &rdev->flags)) {
543 /* cannot risk returning a device that failed
544 * before we inc'ed nr_pending
546 rdev_dec_pending(rdev, conf->mddev);
549 sectors = best_good_sectors;
550 conf->next_seq_sect = this_sector + sectors;
551 conf->last_used = best_disk;
554 *max_sectors = sectors;
559 int md_raid1_congested(mddev_t *mddev, int bits)
561 conf_t *conf = mddev->private;
565 for (i = 0; i < mddev->raid_disks; i++) {
566 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
567 if (rdev && !test_bit(Faulty, &rdev->flags)) {
568 struct request_queue *q = bdev_get_queue(rdev->bdev);
572 /* Note the '|| 1' - when read_balance prefers
573 * non-congested targets, it can be removed
575 if ((bits & (1<<BDI_async_congested)) || 1)
576 ret |= bdi_congested(&q->backing_dev_info, bits);
578 ret &= bdi_congested(&q->backing_dev_info, bits);
584 EXPORT_SYMBOL_GPL(md_raid1_congested);
586 static int raid1_congested(void *data, int bits)
588 mddev_t *mddev = data;
590 return mddev_congested(mddev, bits) ||
591 md_raid1_congested(mddev, bits);
594 static void flush_pending_writes(conf_t *conf)
596 /* Any writes that have been queued but are awaiting
597 * bitmap updates get flushed here.
599 spin_lock_irq(&conf->device_lock);
601 if (conf->pending_bio_list.head) {
603 bio = bio_list_get(&conf->pending_bio_list);
604 spin_unlock_irq(&conf->device_lock);
605 /* flush any pending bitmap writes to
606 * disk before proceeding w/ I/O */
607 bitmap_unplug(conf->mddev->bitmap);
609 while (bio) { /* submit pending writes */
610 struct bio *next = bio->bi_next;
612 generic_make_request(bio);
616 spin_unlock_irq(&conf->device_lock);
620 * Sometimes we need to suspend IO while we do something else,
621 * either some resync/recovery, or reconfigure the array.
622 * To do this we raise a 'barrier'.
623 * The 'barrier' is a counter that can be raised multiple times
624 * to count how many activities are happening which preclude
626 * We can only raise the barrier if there is no pending IO.
627 * i.e. if nr_pending == 0.
628 * We choose only to raise the barrier if no-one is waiting for the
629 * barrier to go down. This means that as soon as an IO request
630 * is ready, no other operations which require a barrier will start
631 * until the IO request has had a chance.
633 * So: regular IO calls 'wait_barrier'. When that returns there
634 * is no backgroup IO happening, It must arrange to call
635 * allow_barrier when it has finished its IO.
636 * backgroup IO calls must call raise_barrier. Once that returns
637 * there is no normal IO happeing. It must arrange to call
638 * lower_barrier when the particular background IO completes.
640 #define RESYNC_DEPTH 32
642 static void raise_barrier(conf_t *conf)
644 spin_lock_irq(&conf->resync_lock);
646 /* Wait until no block IO is waiting */
647 wait_event_lock_irq(conf->wait_barrier, !conf->nr_waiting,
648 conf->resync_lock, );
650 /* block any new IO from starting */
653 /* Now wait for all pending IO to complete */
654 wait_event_lock_irq(conf->wait_barrier,
655 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
656 conf->resync_lock, );
658 spin_unlock_irq(&conf->resync_lock);
661 static void lower_barrier(conf_t *conf)
664 BUG_ON(conf->barrier <= 0);
665 spin_lock_irqsave(&conf->resync_lock, flags);
667 spin_unlock_irqrestore(&conf->resync_lock, flags);
668 wake_up(&conf->wait_barrier);
671 static void wait_barrier(conf_t *conf)
673 spin_lock_irq(&conf->resync_lock);
676 wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
682 spin_unlock_irq(&conf->resync_lock);
685 static void allow_barrier(conf_t *conf)
688 spin_lock_irqsave(&conf->resync_lock, flags);
690 spin_unlock_irqrestore(&conf->resync_lock, flags);
691 wake_up(&conf->wait_barrier);
694 static void freeze_array(conf_t *conf)
696 /* stop syncio and normal IO and wait for everything to
698 * We increment barrier and nr_waiting, and then
699 * wait until nr_pending match nr_queued+1
700 * This is called in the context of one normal IO request
701 * that has failed. Thus any sync request that might be pending
702 * will be blocked by nr_pending, and we need to wait for
703 * pending IO requests to complete or be queued for re-try.
704 * Thus the number queued (nr_queued) plus this request (1)
705 * must match the number of pending IOs (nr_pending) before
708 spin_lock_irq(&conf->resync_lock);
711 wait_event_lock_irq(conf->wait_barrier,
712 conf->nr_pending == conf->nr_queued+1,
714 flush_pending_writes(conf));
715 spin_unlock_irq(&conf->resync_lock);
717 static void unfreeze_array(conf_t *conf)
719 /* reverse the effect of the freeze */
720 spin_lock_irq(&conf->resync_lock);
723 wake_up(&conf->wait_barrier);
724 spin_unlock_irq(&conf->resync_lock);
728 /* duplicate the data pages for behind I/O
730 static void alloc_behind_pages(struct bio *bio, r1bio_t *r1_bio)
733 struct bio_vec *bvec;
734 struct page **pages = kzalloc(bio->bi_vcnt * sizeof(struct page*),
736 if (unlikely(!pages))
739 bio_for_each_segment(bvec, bio, i) {
740 pages[i] = alloc_page(GFP_NOIO);
741 if (unlikely(!pages[i]))
743 memcpy(kmap(pages[i]) + bvec->bv_offset,
744 kmap(bvec->bv_page) + bvec->bv_offset, bvec->bv_len);
746 kunmap(bvec->bv_page);
748 r1_bio->behind_pages = pages;
749 r1_bio->behind_page_count = bio->bi_vcnt;
750 set_bit(R1BIO_BehindIO, &r1_bio->state);
754 for (i = 0; i < bio->bi_vcnt; i++)
758 PRINTK("%dB behind alloc failed, doing sync I/O\n", bio->bi_size);
761 static int make_request(mddev_t *mddev, struct bio * bio)
763 conf_t *conf = mddev->private;
764 mirror_info_t *mirror;
766 struct bio *read_bio;
767 int i, targets = 0, disks;
768 struct bitmap *bitmap;
770 const int rw = bio_data_dir(bio);
771 const unsigned long do_sync = (bio->bi_rw & REQ_SYNC);
772 const unsigned long do_flush_fua = (bio->bi_rw & (REQ_FLUSH | REQ_FUA));
773 mdk_rdev_t *blocked_rdev;
777 * Register the new request and wait if the reconstruction
778 * thread has put up a bar for new requests.
779 * Continue immediately if no resync is active currently.
782 md_write_start(mddev, bio); /* wait on superblock update early */
784 if (bio_data_dir(bio) == WRITE &&
785 bio->bi_sector + bio->bi_size/512 > mddev->suspend_lo &&
786 bio->bi_sector < mddev->suspend_hi) {
787 /* As the suspend_* range is controlled by
788 * userspace, we want an interruptible
793 flush_signals(current);
794 prepare_to_wait(&conf->wait_barrier,
795 &w, TASK_INTERRUPTIBLE);
796 if (bio->bi_sector + bio->bi_size/512 <= mddev->suspend_lo ||
797 bio->bi_sector >= mddev->suspend_hi)
801 finish_wait(&conf->wait_barrier, &w);
806 bitmap = mddev->bitmap;
809 * make_request() can abort the operation when READA is being
810 * used and no empty request is available.
813 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
815 r1_bio->master_bio = bio;
816 r1_bio->sectors = bio->bi_size >> 9;
818 r1_bio->mddev = mddev;
819 r1_bio->sector = bio->bi_sector;
821 /* We might need to issue multiple reads to different
822 * devices if there are bad blocks around, so we keep
823 * track of the number of reads in bio->bi_phys_segments.
824 * If this is 0, there is only one r1_bio and no locking
825 * will be needed when requests complete. If it is
826 * non-zero, then it is the number of not-completed requests.
828 bio->bi_phys_segments = 0;
829 clear_bit(BIO_SEG_VALID, &bio->bi_flags);
833 * read balancing logic:
839 rdisk = read_balance(conf, r1_bio, &max_sectors);
842 /* couldn't find anywhere to read from */
843 raid_end_bio_io(r1_bio);
846 mirror = conf->mirrors + rdisk;
848 if (test_bit(WriteMostly, &mirror->rdev->flags) &&
850 /* Reading from a write-mostly device must
851 * take care not to over-take any writes
854 wait_event(bitmap->behind_wait,
855 atomic_read(&bitmap->behind_writes) == 0);
857 r1_bio->read_disk = rdisk;
859 read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
860 md_trim_bio(read_bio, r1_bio->sector - bio->bi_sector,
863 r1_bio->bios[rdisk] = read_bio;
865 read_bio->bi_sector = r1_bio->sector + mirror->rdev->data_offset;
866 read_bio->bi_bdev = mirror->rdev->bdev;
867 read_bio->bi_end_io = raid1_end_read_request;
868 read_bio->bi_rw = READ | do_sync;
869 read_bio->bi_private = r1_bio;
871 if (max_sectors < r1_bio->sectors) {
872 /* could not read all from this device, so we will
873 * need another r1_bio.
877 sectors_handled = (r1_bio->sector + max_sectors
879 r1_bio->sectors = max_sectors;
880 spin_lock_irq(&conf->device_lock);
881 if (bio->bi_phys_segments == 0)
882 bio->bi_phys_segments = 2;
884 bio->bi_phys_segments++;
885 spin_unlock_irq(&conf->device_lock);
886 /* Cannot call generic_make_request directly
887 * as that will be queued in __make_request
888 * and subsequent mempool_alloc might block waiting
889 * for it. So hand bio over to raid1d.
891 reschedule_retry(r1_bio);
893 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
895 r1_bio->master_bio = bio;
896 r1_bio->sectors = (bio->bi_size >> 9) - sectors_handled;
898 r1_bio->mddev = mddev;
899 r1_bio->sector = bio->bi_sector + sectors_handled;
902 generic_make_request(read_bio);
909 /* first select target devices under spinlock and
910 * inc refcount on their rdev. Record them by setting
913 plugged = mddev_check_plugged(mddev);
915 disks = conf->raid_disks;
919 for (i = 0; i < disks; i++) {
920 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
921 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
922 atomic_inc(&rdev->nr_pending);
926 if (rdev && !test_bit(Faulty, &rdev->flags)) {
927 atomic_inc(&rdev->nr_pending);
928 if (test_bit(Faulty, &rdev->flags)) {
929 rdev_dec_pending(rdev, mddev);
930 r1_bio->bios[i] = NULL;
932 r1_bio->bios[i] = bio;
936 r1_bio->bios[i] = NULL;
940 if (unlikely(blocked_rdev)) {
941 /* Wait for this device to become unblocked */
944 for (j = 0; j < i; j++)
946 rdev_dec_pending(conf->mirrors[j].rdev, mddev);
949 md_wait_for_blocked_rdev(blocked_rdev, mddev);
954 if (targets < conf->raid_disks) {
955 /* array is degraded, we will not clear the bitmap
956 * on I/O completion (see raid1_end_write_request) */
957 set_bit(R1BIO_Degraded, &r1_bio->state);
961 * Not if there are too many, or cannot allocate memory,
962 * or a reader on WriteMostly is waiting for behind writes
965 (atomic_read(&bitmap->behind_writes)
966 < mddev->bitmap_info.max_write_behind) &&
967 !waitqueue_active(&bitmap->behind_wait))
968 alloc_behind_pages(bio, r1_bio);
970 atomic_set(&r1_bio->remaining, 1);
971 atomic_set(&r1_bio->behind_remaining, 0);
973 bitmap_startwrite(bitmap, bio->bi_sector, r1_bio->sectors,
974 test_bit(R1BIO_BehindIO, &r1_bio->state));
975 for (i = 0; i < disks; i++) {
977 if (!r1_bio->bios[i])
980 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
981 r1_bio->bios[i] = mbio;
983 mbio->bi_sector = r1_bio->sector + conf->mirrors[i].rdev->data_offset;
984 mbio->bi_bdev = conf->mirrors[i].rdev->bdev;
985 mbio->bi_end_io = raid1_end_write_request;
986 mbio->bi_rw = WRITE | do_flush_fua | do_sync;
987 mbio->bi_private = r1_bio;
989 if (r1_bio->behind_pages) {
990 struct bio_vec *bvec;
993 /* Yes, I really want the '__' version so that
994 * we clear any unused pointer in the io_vec, rather
995 * than leave them unchanged. This is important
996 * because when we come to free the pages, we won't
997 * know the original bi_idx, so we just free
1000 __bio_for_each_segment(bvec, mbio, j, 0)
1001 bvec->bv_page = r1_bio->behind_pages[j];
1002 if (test_bit(WriteMostly, &conf->mirrors[i].rdev->flags))
1003 atomic_inc(&r1_bio->behind_remaining);
1006 atomic_inc(&r1_bio->remaining);
1007 spin_lock_irqsave(&conf->device_lock, flags);
1008 bio_list_add(&conf->pending_bio_list, mbio);
1009 spin_unlock_irqrestore(&conf->device_lock, flags);
1011 r1_bio_write_done(r1_bio);
1013 /* In case raid1d snuck in to freeze_array */
1014 wake_up(&conf->wait_barrier);
1016 if (do_sync || !bitmap || !plugged)
1017 md_wakeup_thread(mddev->thread);
1022 static void status(struct seq_file *seq, mddev_t *mddev)
1024 conf_t *conf = mddev->private;
1027 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
1028 conf->raid_disks - mddev->degraded);
1030 for (i = 0; i < conf->raid_disks; i++) {
1031 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
1032 seq_printf(seq, "%s",
1033 rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
1036 seq_printf(seq, "]");
1040 static void error(mddev_t *mddev, mdk_rdev_t *rdev)
1042 char b[BDEVNAME_SIZE];
1043 conf_t *conf = mddev->private;
1046 * If it is not operational, then we have already marked it as dead
1047 * else if it is the last working disks, ignore the error, let the
1048 * next level up know.
1049 * else mark the drive as failed
1051 if (test_bit(In_sync, &rdev->flags)
1052 && (conf->raid_disks - mddev->degraded) == 1) {
1054 * Don't fail the drive, act as though we were just a
1055 * normal single drive.
1056 * However don't try a recovery from this drive as
1057 * it is very likely to fail.
1059 conf->recovery_disabled = mddev->recovery_disabled;
1062 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1063 unsigned long flags;
1064 spin_lock_irqsave(&conf->device_lock, flags);
1066 set_bit(Faulty, &rdev->flags);
1067 spin_unlock_irqrestore(&conf->device_lock, flags);
1069 * if recovery is running, make sure it aborts.
1071 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1073 set_bit(Faulty, &rdev->flags);
1074 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1076 "md/raid1:%s: Disk failure on %s, disabling device.\n"
1077 "md/raid1:%s: Operation continuing on %d devices.\n",
1078 mdname(mddev), bdevname(rdev->bdev, b),
1079 mdname(mddev), conf->raid_disks - mddev->degraded);
1082 static void print_conf(conf_t *conf)
1086 printk(KERN_DEBUG "RAID1 conf printout:\n");
1088 printk(KERN_DEBUG "(!conf)\n");
1091 printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
1095 for (i = 0; i < conf->raid_disks; i++) {
1096 char b[BDEVNAME_SIZE];
1097 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
1099 printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
1100 i, !test_bit(In_sync, &rdev->flags),
1101 !test_bit(Faulty, &rdev->flags),
1102 bdevname(rdev->bdev,b));
1107 static void close_sync(conf_t *conf)
1110 allow_barrier(conf);
1112 mempool_destroy(conf->r1buf_pool);
1113 conf->r1buf_pool = NULL;
1116 static int raid1_spare_active(mddev_t *mddev)
1119 conf_t *conf = mddev->private;
1121 unsigned long flags;
1124 * Find all failed disks within the RAID1 configuration
1125 * and mark them readable.
1126 * Called under mddev lock, so rcu protection not needed.
1128 for (i = 0; i < conf->raid_disks; i++) {
1129 mdk_rdev_t *rdev = conf->mirrors[i].rdev;
1131 && !test_bit(Faulty, &rdev->flags)
1132 && !test_and_set_bit(In_sync, &rdev->flags)) {
1134 sysfs_notify_dirent_safe(rdev->sysfs_state);
1137 spin_lock_irqsave(&conf->device_lock, flags);
1138 mddev->degraded -= count;
1139 spin_unlock_irqrestore(&conf->device_lock, flags);
1146 static int raid1_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
1148 conf_t *conf = mddev->private;
1153 int last = mddev->raid_disks - 1;
1155 if (mddev->recovery_disabled == conf->recovery_disabled)
1158 if (rdev->badblocks.count)
1161 if (rdev->raid_disk >= 0)
1162 first = last = rdev->raid_disk;
1164 for (mirror = first; mirror <= last; mirror++)
1165 if ( !(p=conf->mirrors+mirror)->rdev) {
1167 disk_stack_limits(mddev->gendisk, rdev->bdev,
1168 rdev->data_offset << 9);
1169 /* as we don't honour merge_bvec_fn, we must
1170 * never risk violating it, so limit
1171 * ->max_segments to one lying with a single
1172 * page, as a one page request is never in
1175 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
1176 blk_queue_max_segments(mddev->queue, 1);
1177 blk_queue_segment_boundary(mddev->queue,
1178 PAGE_CACHE_SIZE - 1);
1181 p->head_position = 0;
1182 rdev->raid_disk = mirror;
1184 /* As all devices are equivalent, we don't need a full recovery
1185 * if this was recently any drive of the array
1187 if (rdev->saved_raid_disk < 0)
1189 rcu_assign_pointer(p->rdev, rdev);
1192 md_integrity_add_rdev(rdev, mddev);
1197 static int raid1_remove_disk(mddev_t *mddev, int number)
1199 conf_t *conf = mddev->private;
1202 mirror_info_t *p = conf->mirrors+ number;
1207 if (test_bit(In_sync, &rdev->flags) ||
1208 atomic_read(&rdev->nr_pending)) {
1212 /* Only remove non-faulty devices if recovery
1215 if (!test_bit(Faulty, &rdev->flags) &&
1216 mddev->recovery_disabled != conf->recovery_disabled &&
1217 mddev->degraded < conf->raid_disks) {
1223 if (atomic_read(&rdev->nr_pending)) {
1224 /* lost the race, try later */
1229 err = md_integrity_register(mddev);
1238 static void end_sync_read(struct bio *bio, int error)
1240 r1bio_t *r1_bio = bio->bi_private;
1243 for (i=r1_bio->mddev->raid_disks; i--; )
1244 if (r1_bio->bios[i] == bio)
1247 update_head_pos(i, r1_bio);
1249 * we have read a block, now it needs to be re-written,
1250 * or re-read if the read failed.
1251 * We don't do much here, just schedule handling by raid1d
1253 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
1254 set_bit(R1BIO_Uptodate, &r1_bio->state);
1256 if (atomic_dec_and_test(&r1_bio->remaining))
1257 reschedule_retry(r1_bio);
1260 static void end_sync_write(struct bio *bio, int error)
1262 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1263 r1bio_t *r1_bio = bio->bi_private;
1264 mddev_t *mddev = r1_bio->mddev;
1265 conf_t *conf = mddev->private;
1269 for (i = 0; i < conf->raid_disks; i++)
1270 if (r1_bio->bios[i] == bio) {
1275 sector_t sync_blocks = 0;
1276 sector_t s = r1_bio->sector;
1277 long sectors_to_go = r1_bio->sectors;
1278 /* make sure these bits doesn't get cleared. */
1280 bitmap_end_sync(mddev->bitmap, s,
1283 sectors_to_go -= sync_blocks;
1284 } while (sectors_to_go > 0);
1285 md_error(mddev, conf->mirrors[mirror].rdev);
1288 update_head_pos(mirror, r1_bio);
1290 if (atomic_dec_and_test(&r1_bio->remaining)) {
1291 sector_t s = r1_bio->sectors;
1293 md_done_sync(mddev, s, uptodate);
1297 static int fix_sync_read_error(r1bio_t *r1_bio)
1299 /* Try some synchronous reads of other devices to get
1300 * good data, much like with normal read errors. Only
1301 * read into the pages we already have so we don't
1302 * need to re-issue the read request.
1303 * We don't need to freeze the array, because being in an
1304 * active sync request, there is no normal IO, and
1305 * no overlapping syncs.
1307 mddev_t *mddev = r1_bio->mddev;
1308 conf_t *conf = mddev->private;
1309 struct bio *bio = r1_bio->bios[r1_bio->read_disk];
1310 sector_t sect = r1_bio->sector;
1311 int sectors = r1_bio->sectors;
1316 int d = r1_bio->read_disk;
1321 if (s > (PAGE_SIZE>>9))
1324 if (r1_bio->bios[d]->bi_end_io == end_sync_read) {
1325 /* No rcu protection needed here devices
1326 * can only be removed when no resync is
1327 * active, and resync is currently active
1329 rdev = conf->mirrors[d].rdev;
1330 if (sync_page_io(rdev, sect, s<<9,
1331 bio->bi_io_vec[idx].bv_page,
1338 if (d == conf->raid_disks)
1340 } while (!success && d != r1_bio->read_disk);
1343 char b[BDEVNAME_SIZE];
1344 /* Cannot read from anywhere, array is toast */
1345 md_error(mddev, conf->mirrors[r1_bio->read_disk].rdev);
1346 printk(KERN_ALERT "md/raid1:%s: %s: unrecoverable I/O read error"
1347 " for block %llu\n",
1349 bdevname(bio->bi_bdev, b),
1350 (unsigned long long)r1_bio->sector);
1351 md_done_sync(mddev, r1_bio->sectors, 0);
1357 /* write it back and re-read */
1358 while (d != r1_bio->read_disk) {
1360 d = conf->raid_disks;
1362 if (r1_bio->bios[d]->bi_end_io != end_sync_read)
1364 rdev = conf->mirrors[d].rdev;
1365 if (sync_page_io(rdev, sect, s<<9,
1366 bio->bi_io_vec[idx].bv_page,
1367 WRITE, false) == 0) {
1368 r1_bio->bios[d]->bi_end_io = NULL;
1369 rdev_dec_pending(rdev, mddev);
1370 md_error(mddev, rdev);
1374 while (d != r1_bio->read_disk) {
1376 d = conf->raid_disks;
1378 if (r1_bio->bios[d]->bi_end_io != end_sync_read)
1380 rdev = conf->mirrors[d].rdev;
1381 if (sync_page_io(rdev, sect, s<<9,
1382 bio->bi_io_vec[idx].bv_page,
1384 md_error(mddev, rdev);
1386 atomic_add(s, &rdev->corrected_errors);
1392 set_bit(R1BIO_Uptodate, &r1_bio->state);
1393 set_bit(BIO_UPTODATE, &bio->bi_flags);
1397 static int process_checks(r1bio_t *r1_bio)
1399 /* We have read all readable devices. If we haven't
1400 * got the block, then there is no hope left.
1401 * If we have, then we want to do a comparison
1402 * and skip the write if everything is the same.
1403 * If any blocks failed to read, then we need to
1404 * attempt an over-write
1406 mddev_t *mddev = r1_bio->mddev;
1407 conf_t *conf = mddev->private;
1411 for (primary = 0; primary < conf->raid_disks; primary++)
1412 if (r1_bio->bios[primary]->bi_end_io == end_sync_read &&
1413 test_bit(BIO_UPTODATE, &r1_bio->bios[primary]->bi_flags)) {
1414 r1_bio->bios[primary]->bi_end_io = NULL;
1415 rdev_dec_pending(conf->mirrors[primary].rdev, mddev);
1418 r1_bio->read_disk = primary;
1419 for (i = 0; i < conf->raid_disks; i++) {
1421 int vcnt = r1_bio->sectors >> (PAGE_SHIFT- 9);
1422 struct bio *pbio = r1_bio->bios[primary];
1423 struct bio *sbio = r1_bio->bios[i];
1426 if (r1_bio->bios[i]->bi_end_io != end_sync_read)
1429 if (test_bit(BIO_UPTODATE, &sbio->bi_flags)) {
1430 for (j = vcnt; j-- ; ) {
1432 p = pbio->bi_io_vec[j].bv_page;
1433 s = sbio->bi_io_vec[j].bv_page;
1434 if (memcmp(page_address(p),
1442 mddev->resync_mismatches += r1_bio->sectors;
1443 if (j < 0 || (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)
1444 && test_bit(BIO_UPTODATE, &sbio->bi_flags))) {
1445 /* No need to write to this device. */
1446 sbio->bi_end_io = NULL;
1447 rdev_dec_pending(conf->mirrors[i].rdev, mddev);
1450 /* fixup the bio for reuse */
1451 sbio->bi_vcnt = vcnt;
1452 sbio->bi_size = r1_bio->sectors << 9;
1454 sbio->bi_phys_segments = 0;
1455 sbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1456 sbio->bi_flags |= 1 << BIO_UPTODATE;
1457 sbio->bi_next = NULL;
1458 sbio->bi_sector = r1_bio->sector +
1459 conf->mirrors[i].rdev->data_offset;
1460 sbio->bi_bdev = conf->mirrors[i].rdev->bdev;
1461 size = sbio->bi_size;
1462 for (j = 0; j < vcnt ; j++) {
1464 bi = &sbio->bi_io_vec[j];
1466 if (size > PAGE_SIZE)
1467 bi->bv_len = PAGE_SIZE;
1471 memcpy(page_address(bi->bv_page),
1472 page_address(pbio->bi_io_vec[j].bv_page),
1479 static void sync_request_write(mddev_t *mddev, r1bio_t *r1_bio)
1481 conf_t *conf = mddev->private;
1483 int disks = conf->raid_disks;
1484 struct bio *bio, *wbio;
1486 bio = r1_bio->bios[r1_bio->read_disk];
1488 if (!test_bit(R1BIO_Uptodate, &r1_bio->state))
1489 /* ouch - failed to read all of that. */
1490 if (!fix_sync_read_error(r1_bio))
1493 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
1494 if (process_checks(r1_bio) < 0)
1499 atomic_set(&r1_bio->remaining, 1);
1500 for (i = 0; i < disks ; i++) {
1501 wbio = r1_bio->bios[i];
1502 if (wbio->bi_end_io == NULL ||
1503 (wbio->bi_end_io == end_sync_read &&
1504 (i == r1_bio->read_disk ||
1505 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery))))
1508 wbio->bi_rw = WRITE;
1509 wbio->bi_end_io = end_sync_write;
1510 atomic_inc(&r1_bio->remaining);
1511 md_sync_acct(conf->mirrors[i].rdev->bdev, wbio->bi_size >> 9);
1513 generic_make_request(wbio);
1516 if (atomic_dec_and_test(&r1_bio->remaining)) {
1517 /* if we're here, all write(s) have completed, so clean up */
1518 md_done_sync(mddev, r1_bio->sectors, 1);
1524 * This is a kernel thread which:
1526 * 1. Retries failed read operations on working mirrors.
1527 * 2. Updates the raid superblock when problems encounter.
1528 * 3. Performs writes following reads for array synchronising.
1531 static void fix_read_error(conf_t *conf, int read_disk,
1532 sector_t sect, int sectors)
1534 mddev_t *mddev = conf->mddev;
1542 if (s > (PAGE_SIZE>>9))
1546 /* Note: no rcu protection needed here
1547 * as this is synchronous in the raid1d thread
1548 * which is the thread that might remove
1549 * a device. If raid1d ever becomes multi-threaded....
1554 rdev = conf->mirrors[d].rdev;
1556 test_bit(In_sync, &rdev->flags) &&
1557 is_badblock(rdev, sect, s,
1558 &first_bad, &bad_sectors) == 0 &&
1559 sync_page_io(rdev, sect, s<<9,
1560 conf->tmppage, READ, false))
1564 if (d == conf->raid_disks)
1567 } while (!success && d != read_disk);
1570 /* Cannot read from anywhere -- bye bye array */
1571 md_error(mddev, conf->mirrors[read_disk].rdev);
1574 /* write it back and re-read */
1576 while (d != read_disk) {
1578 d = conf->raid_disks;
1580 rdev = conf->mirrors[d].rdev;
1582 test_bit(In_sync, &rdev->flags)) {
1583 if (sync_page_io(rdev, sect, s<<9,
1584 conf->tmppage, WRITE, false)
1586 /* Well, this device is dead */
1587 md_error(mddev, rdev);
1591 while (d != read_disk) {
1592 char b[BDEVNAME_SIZE];
1594 d = conf->raid_disks;
1596 rdev = conf->mirrors[d].rdev;
1598 test_bit(In_sync, &rdev->flags)) {
1599 if (sync_page_io(rdev, sect, s<<9,
1600 conf->tmppage, READ, false)
1602 /* Well, this device is dead */
1603 md_error(mddev, rdev);
1605 atomic_add(s, &rdev->corrected_errors);
1607 "md/raid1:%s: read error corrected "
1608 "(%d sectors at %llu on %s)\n",
1610 (unsigned long long)(sect +
1612 bdevname(rdev->bdev, b));
1621 static void raid1d(mddev_t *mddev)
1625 unsigned long flags;
1626 conf_t *conf = mddev->private;
1627 struct list_head *head = &conf->retry_list;
1629 struct blk_plug plug;
1631 md_check_recovery(mddev);
1633 blk_start_plug(&plug);
1635 char b[BDEVNAME_SIZE];
1637 if (atomic_read(&mddev->plug_cnt) == 0)
1638 flush_pending_writes(conf);
1640 spin_lock_irqsave(&conf->device_lock, flags);
1641 if (list_empty(head)) {
1642 spin_unlock_irqrestore(&conf->device_lock, flags);
1645 r1_bio = list_entry(head->prev, r1bio_t, retry_list);
1646 list_del(head->prev);
1648 spin_unlock_irqrestore(&conf->device_lock, flags);
1650 mddev = r1_bio->mddev;
1651 conf = mddev->private;
1652 if (test_bit(R1BIO_IsSync, &r1_bio->state))
1653 sync_request_write(mddev, r1_bio);
1654 else if (test_bit(R1BIO_ReadError, &r1_bio->state)) {
1658 clear_bit(R1BIO_ReadError, &r1_bio->state);
1659 /* we got a read error. Maybe the drive is bad. Maybe just
1660 * the block and we can fix it.
1661 * We freeze all other IO, and try reading the block from
1662 * other devices. When we find one, we re-write
1663 * and check it that fixes the read error.
1664 * This is all done synchronously while the array is
1667 if (mddev->ro == 0) {
1669 fix_read_error(conf, r1_bio->read_disk,
1672 unfreeze_array(conf);
1675 conf->mirrors[r1_bio->read_disk].rdev);
1677 bio = r1_bio->bios[r1_bio->read_disk];
1678 bdevname(bio->bi_bdev, b);
1680 disk = read_balance(conf, r1_bio, &max_sectors);
1682 printk(KERN_ALERT "md/raid1:%s: %s: unrecoverable I/O"
1683 " read error for block %llu\n",
1685 (unsigned long long)r1_bio->sector);
1686 raid_end_bio_io(r1_bio);
1688 const unsigned long do_sync = r1_bio->master_bio->bi_rw & REQ_SYNC;
1690 r1_bio->bios[r1_bio->read_disk] =
1691 mddev->ro ? IO_BLOCKED : NULL;
1694 r1_bio->read_disk = disk;
1695 bio = bio_clone_mddev(r1_bio->master_bio,
1698 r1_bio->sector - bio->bi_sector,
1700 r1_bio->bios[r1_bio->read_disk] = bio;
1701 rdev = conf->mirrors[disk].rdev;
1704 "md/raid1:%s: redirecting sector %llu"
1705 " to other mirror: %s\n",
1707 (unsigned long long)r1_bio->sector,
1708 bdevname(rdev->bdev, b));
1709 bio->bi_sector = r1_bio->sector + rdev->data_offset;
1710 bio->bi_bdev = rdev->bdev;
1711 bio->bi_end_io = raid1_end_read_request;
1712 bio->bi_rw = READ | do_sync;
1713 bio->bi_private = r1_bio;
1714 if (max_sectors < r1_bio->sectors) {
1715 /* Drat - have to split this up more */
1716 struct bio *mbio = r1_bio->master_bio;
1717 int sectors_handled =
1718 r1_bio->sector + max_sectors
1720 r1_bio->sectors = max_sectors;
1721 spin_lock_irq(&conf->device_lock);
1722 if (mbio->bi_phys_segments == 0)
1723 mbio->bi_phys_segments = 2;
1725 mbio->bi_phys_segments++;
1726 spin_unlock_irq(&conf->device_lock);
1727 generic_make_request(bio);
1730 r1_bio = mempool_alloc(conf->r1bio_pool,
1733 r1_bio->master_bio = mbio;
1734 r1_bio->sectors = (mbio->bi_size >> 9)
1737 set_bit(R1BIO_ReadError,
1739 r1_bio->mddev = mddev;
1740 r1_bio->sector = mbio->bi_sector
1745 generic_make_request(bio);
1748 /* just a partial read to be scheduled from separate
1751 generic_make_request(r1_bio->bios[r1_bio->read_disk]);
1755 blk_finish_plug(&plug);
1759 static int init_resync(conf_t *conf)
1763 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
1764 BUG_ON(conf->r1buf_pool);
1765 conf->r1buf_pool = mempool_create(buffs, r1buf_pool_alloc, r1buf_pool_free,
1767 if (!conf->r1buf_pool)
1769 conf->next_resync = 0;
1774 * perform a "sync" on one "block"
1776 * We need to make sure that no normal I/O request - particularly write
1777 * requests - conflict with active sync requests.
1779 * This is achieved by tracking pending requests and a 'barrier' concept
1780 * that can be installed to exclude normal IO requests.
1783 static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
1785 conf_t *conf = mddev->private;
1788 sector_t max_sector, nr_sectors;
1792 int write_targets = 0, read_targets = 0;
1793 sector_t sync_blocks;
1794 int still_degraded = 0;
1796 if (!conf->r1buf_pool)
1797 if (init_resync(conf))
1800 max_sector = mddev->dev_sectors;
1801 if (sector_nr >= max_sector) {
1802 /* If we aborted, we need to abort the
1803 * sync on the 'current' bitmap chunk (there will
1804 * only be one in raid1 resync.
1805 * We can find the current addess in mddev->curr_resync
1807 if (mddev->curr_resync < max_sector) /* aborted */
1808 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
1810 else /* completed sync */
1813 bitmap_close_sync(mddev->bitmap);
1818 if (mddev->bitmap == NULL &&
1819 mddev->recovery_cp == MaxSector &&
1820 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
1821 conf->fullsync == 0) {
1823 return max_sector - sector_nr;
1825 /* before building a request, check if we can skip these blocks..
1826 * This call the bitmap_start_sync doesn't actually record anything
1828 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
1829 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
1830 /* We can skip this block, and probably several more */
1835 * If there is non-resync activity waiting for a turn,
1836 * and resync is going fast enough,
1837 * then let it though before starting on this new sync request.
1839 if (!go_faster && conf->nr_waiting)
1840 msleep_interruptible(1000);
1842 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
1843 r1_bio = mempool_alloc(conf->r1buf_pool, GFP_NOIO);
1844 raise_barrier(conf);
1846 conf->next_resync = sector_nr;
1850 * If we get a correctably read error during resync or recovery,
1851 * we might want to read from a different device. So we
1852 * flag all drives that could conceivably be read from for READ,
1853 * and any others (which will be non-In_sync devices) for WRITE.
1854 * If a read fails, we try reading from something else for which READ
1858 r1_bio->mddev = mddev;
1859 r1_bio->sector = sector_nr;
1861 set_bit(R1BIO_IsSync, &r1_bio->state);
1863 for (i=0; i < conf->raid_disks; i++) {
1865 bio = r1_bio->bios[i];
1867 /* take from bio_init */
1868 bio->bi_next = NULL;
1869 bio->bi_flags &= ~(BIO_POOL_MASK-1);
1870 bio->bi_flags |= 1 << BIO_UPTODATE;
1871 bio->bi_comp_cpu = -1;
1875 bio->bi_phys_segments = 0;
1877 bio->bi_end_io = NULL;
1878 bio->bi_private = NULL;
1880 rdev = rcu_dereference(conf->mirrors[i].rdev);
1882 test_bit(Faulty, &rdev->flags)) {
1885 } else if (!test_bit(In_sync, &rdev->flags)) {
1887 bio->bi_end_io = end_sync_write;
1890 /* may need to read from here */
1892 bio->bi_end_io = end_sync_read;
1893 if (test_bit(WriteMostly, &rdev->flags)) {
1902 atomic_inc(&rdev->nr_pending);
1903 bio->bi_sector = sector_nr + rdev->data_offset;
1904 bio->bi_bdev = rdev->bdev;
1905 bio->bi_private = r1_bio;
1910 r1_bio->read_disk = disk;
1912 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && read_targets > 0)
1913 /* extra read targets are also write targets */
1914 write_targets += read_targets-1;
1916 if (write_targets == 0 || read_targets == 0) {
1917 /* There is nowhere to write, so all non-sync
1918 * drives must be failed - so we are finished
1920 sector_t rv = max_sector - sector_nr;
1926 if (max_sector > mddev->resync_max)
1927 max_sector = mddev->resync_max; /* Don't do IO beyond here */
1932 int len = PAGE_SIZE;
1933 if (sector_nr + (len>>9) > max_sector)
1934 len = (max_sector - sector_nr) << 9;
1937 if (sync_blocks == 0) {
1938 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
1939 &sync_blocks, still_degraded) &&
1941 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
1943 BUG_ON(sync_blocks < (PAGE_SIZE>>9));
1944 if ((len >> 9) > sync_blocks)
1945 len = sync_blocks<<9;
1948 for (i=0 ; i < conf->raid_disks; i++) {
1949 bio = r1_bio->bios[i];
1950 if (bio->bi_end_io) {
1951 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
1952 if (bio_add_page(bio, page, len, 0) == 0) {
1954 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
1957 bio = r1_bio->bios[i];
1958 if (bio->bi_end_io==NULL)
1960 /* remove last page from this bio */
1962 bio->bi_size -= len;
1963 bio->bi_flags &= ~(1<< BIO_SEG_VALID);
1969 nr_sectors += len>>9;
1970 sector_nr += len>>9;
1971 sync_blocks -= (len>>9);
1972 } while (r1_bio->bios[disk]->bi_vcnt < RESYNC_PAGES);
1974 r1_bio->sectors = nr_sectors;
1976 /* For a user-requested sync, we read all readable devices and do a
1979 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
1980 atomic_set(&r1_bio->remaining, read_targets);
1981 for (i=0; i<conf->raid_disks; i++) {
1982 bio = r1_bio->bios[i];
1983 if (bio->bi_end_io == end_sync_read) {
1984 md_sync_acct(bio->bi_bdev, nr_sectors);
1985 generic_make_request(bio);
1989 atomic_set(&r1_bio->remaining, 1);
1990 bio = r1_bio->bios[r1_bio->read_disk];
1991 md_sync_acct(bio->bi_bdev, nr_sectors);
1992 generic_make_request(bio);
1998 static sector_t raid1_size(mddev_t *mddev, sector_t sectors, int raid_disks)
2003 return mddev->dev_sectors;
2006 static conf_t *setup_conf(mddev_t *mddev)
2010 mirror_info_t *disk;
2014 conf = kzalloc(sizeof(conf_t), GFP_KERNEL);
2018 conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
2023 conf->tmppage = alloc_page(GFP_KERNEL);
2027 conf->poolinfo = kzalloc(sizeof(*conf->poolinfo), GFP_KERNEL);
2028 if (!conf->poolinfo)
2030 conf->poolinfo->raid_disks = mddev->raid_disks;
2031 conf->r1bio_pool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
2034 if (!conf->r1bio_pool)
2037 conf->poolinfo->mddev = mddev;
2039 spin_lock_init(&conf->device_lock);
2040 list_for_each_entry(rdev, &mddev->disks, same_set) {
2041 int disk_idx = rdev->raid_disk;
2042 if (disk_idx >= mddev->raid_disks
2045 disk = conf->mirrors + disk_idx;
2049 disk->head_position = 0;
2051 conf->raid_disks = mddev->raid_disks;
2052 conf->mddev = mddev;
2053 INIT_LIST_HEAD(&conf->retry_list);
2055 spin_lock_init(&conf->resync_lock);
2056 init_waitqueue_head(&conf->wait_barrier);
2058 bio_list_init(&conf->pending_bio_list);
2060 conf->last_used = -1;
2061 for (i = 0; i < conf->raid_disks; i++) {
2063 disk = conf->mirrors + i;
2066 !test_bit(In_sync, &disk->rdev->flags)) {
2067 disk->head_position = 0;
2070 } else if (conf->last_used < 0)
2072 * The first working device is used as a
2073 * starting point to read balancing.
2075 conf->last_used = i;
2079 if (conf->last_used < 0) {
2080 printk(KERN_ERR "md/raid1:%s: no operational mirrors\n",
2085 conf->thread = md_register_thread(raid1d, mddev, NULL);
2086 if (!conf->thread) {
2088 "md/raid1:%s: couldn't allocate thread\n",
2097 if (conf->r1bio_pool)
2098 mempool_destroy(conf->r1bio_pool);
2099 kfree(conf->mirrors);
2100 safe_put_page(conf->tmppage);
2101 kfree(conf->poolinfo);
2104 return ERR_PTR(err);
2107 static int run(mddev_t *mddev)
2113 if (mddev->level != 1) {
2114 printk(KERN_ERR "md/raid1:%s: raid level not set to mirroring (%d)\n",
2115 mdname(mddev), mddev->level);
2118 if (mddev->reshape_position != MaxSector) {
2119 printk(KERN_ERR "md/raid1:%s: reshape_position set but not supported\n",
2124 * copy the already verified devices into our private RAID1
2125 * bookkeeping area. [whatever we allocate in run(),
2126 * should be freed in stop()]
2128 if (mddev->private == NULL)
2129 conf = setup_conf(mddev);
2131 conf = mddev->private;
2134 return PTR_ERR(conf);
2136 list_for_each_entry(rdev, &mddev->disks, same_set) {
2137 if (!mddev->gendisk)
2139 disk_stack_limits(mddev->gendisk, rdev->bdev,
2140 rdev->data_offset << 9);
2141 /* as we don't honour merge_bvec_fn, we must never risk
2142 * violating it, so limit ->max_segments to 1 lying within
2143 * a single page, as a one page request is never in violation.
2145 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
2146 blk_queue_max_segments(mddev->queue, 1);
2147 blk_queue_segment_boundary(mddev->queue,
2148 PAGE_CACHE_SIZE - 1);
2150 if (rdev->badblocks.count) {
2151 printk(KERN_ERR "md/raid1: Cannot handle bad blocks yet\n");
2156 mddev->degraded = 0;
2157 for (i=0; i < conf->raid_disks; i++)
2158 if (conf->mirrors[i].rdev == NULL ||
2159 !test_bit(In_sync, &conf->mirrors[i].rdev->flags) ||
2160 test_bit(Faulty, &conf->mirrors[i].rdev->flags))
2163 if (conf->raid_disks - mddev->degraded == 1)
2164 mddev->recovery_cp = MaxSector;
2166 if (mddev->recovery_cp != MaxSector)
2167 printk(KERN_NOTICE "md/raid1:%s: not clean"
2168 " -- starting background reconstruction\n",
2171 "md/raid1:%s: active with %d out of %d mirrors\n",
2172 mdname(mddev), mddev->raid_disks - mddev->degraded,
2176 * Ok, everything is just fine now
2178 mddev->thread = conf->thread;
2179 conf->thread = NULL;
2180 mddev->private = conf;
2182 md_set_array_sectors(mddev, raid1_size(mddev, 0, 0));
2185 mddev->queue->backing_dev_info.congested_fn = raid1_congested;
2186 mddev->queue->backing_dev_info.congested_data = mddev;
2188 return md_integrity_register(mddev);
2191 static int stop(mddev_t *mddev)
2193 conf_t *conf = mddev->private;
2194 struct bitmap *bitmap = mddev->bitmap;
2196 /* wait for behind writes to complete */
2197 if (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
2198 printk(KERN_INFO "md/raid1:%s: behind writes in progress - waiting to stop.\n",
2200 /* need to kick something here to make sure I/O goes? */
2201 wait_event(bitmap->behind_wait,
2202 atomic_read(&bitmap->behind_writes) == 0);
2205 raise_barrier(conf);
2206 lower_barrier(conf);
2208 md_unregister_thread(mddev->thread);
2209 mddev->thread = NULL;
2210 if (conf->r1bio_pool)
2211 mempool_destroy(conf->r1bio_pool);
2212 kfree(conf->mirrors);
2213 kfree(conf->poolinfo);
2215 mddev->private = NULL;
2219 static int raid1_resize(mddev_t *mddev, sector_t sectors)
2221 /* no resync is happening, and there is enough space
2222 * on all devices, so we can resize.
2223 * We need to make sure resync covers any new space.
2224 * If the array is shrinking we should possibly wait until
2225 * any io in the removed space completes, but it hardly seems
2228 md_set_array_sectors(mddev, raid1_size(mddev, sectors, 0));
2229 if (mddev->array_sectors > raid1_size(mddev, sectors, 0))
2231 set_capacity(mddev->gendisk, mddev->array_sectors);
2232 revalidate_disk(mddev->gendisk);
2233 if (sectors > mddev->dev_sectors &&
2234 mddev->recovery_cp > mddev->dev_sectors) {
2235 mddev->recovery_cp = mddev->dev_sectors;
2236 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2238 mddev->dev_sectors = sectors;
2239 mddev->resync_max_sectors = sectors;
2243 static int raid1_reshape(mddev_t *mddev)
2246 * 1/ resize the r1bio_pool
2247 * 2/ resize conf->mirrors
2249 * We allocate a new r1bio_pool if we can.
2250 * Then raise a device barrier and wait until all IO stops.
2251 * Then resize conf->mirrors and swap in the new r1bio pool.
2253 * At the same time, we "pack" the devices so that all the missing
2254 * devices have the higher raid_disk numbers.
2256 mempool_t *newpool, *oldpool;
2257 struct pool_info *newpoolinfo;
2258 mirror_info_t *newmirrors;
2259 conf_t *conf = mddev->private;
2260 int cnt, raid_disks;
2261 unsigned long flags;
2264 /* Cannot change chunk_size, layout, or level */
2265 if (mddev->chunk_sectors != mddev->new_chunk_sectors ||
2266 mddev->layout != mddev->new_layout ||
2267 mddev->level != mddev->new_level) {
2268 mddev->new_chunk_sectors = mddev->chunk_sectors;
2269 mddev->new_layout = mddev->layout;
2270 mddev->new_level = mddev->level;
2274 err = md_allow_write(mddev);
2278 raid_disks = mddev->raid_disks + mddev->delta_disks;
2280 if (raid_disks < conf->raid_disks) {
2282 for (d= 0; d < conf->raid_disks; d++)
2283 if (conf->mirrors[d].rdev)
2285 if (cnt > raid_disks)
2289 newpoolinfo = kmalloc(sizeof(*newpoolinfo), GFP_KERNEL);
2292 newpoolinfo->mddev = mddev;
2293 newpoolinfo->raid_disks = raid_disks;
2295 newpool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
2296 r1bio_pool_free, newpoolinfo);
2301 newmirrors = kzalloc(sizeof(struct mirror_info) * raid_disks, GFP_KERNEL);
2304 mempool_destroy(newpool);
2308 raise_barrier(conf);
2310 /* ok, everything is stopped */
2311 oldpool = conf->r1bio_pool;
2312 conf->r1bio_pool = newpool;
2314 for (d = d2 = 0; d < conf->raid_disks; d++) {
2315 mdk_rdev_t *rdev = conf->mirrors[d].rdev;
2316 if (rdev && rdev->raid_disk != d2) {
2317 sysfs_unlink_rdev(mddev, rdev);
2318 rdev->raid_disk = d2;
2319 sysfs_unlink_rdev(mddev, rdev);
2320 if (sysfs_link_rdev(mddev, rdev))
2322 "md/raid1:%s: cannot register rd%d\n",
2323 mdname(mddev), rdev->raid_disk);
2326 newmirrors[d2++].rdev = rdev;
2328 kfree(conf->mirrors);
2329 conf->mirrors = newmirrors;
2330 kfree(conf->poolinfo);
2331 conf->poolinfo = newpoolinfo;
2333 spin_lock_irqsave(&conf->device_lock, flags);
2334 mddev->degraded += (raid_disks - conf->raid_disks);
2335 spin_unlock_irqrestore(&conf->device_lock, flags);
2336 conf->raid_disks = mddev->raid_disks = raid_disks;
2337 mddev->delta_disks = 0;
2339 conf->last_used = 0; /* just make sure it is in-range */
2340 lower_barrier(conf);
2342 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2343 md_wakeup_thread(mddev->thread);
2345 mempool_destroy(oldpool);
2349 static void raid1_quiesce(mddev_t *mddev, int state)
2351 conf_t *conf = mddev->private;
2354 case 2: /* wake for suspend */
2355 wake_up(&conf->wait_barrier);
2358 raise_barrier(conf);
2361 lower_barrier(conf);
2366 static void *raid1_takeover(mddev_t *mddev)
2368 /* raid1 can take over:
2369 * raid5 with 2 devices, any layout or chunk size
2371 if (mddev->level == 5 && mddev->raid_disks == 2) {
2373 mddev->new_level = 1;
2374 mddev->new_layout = 0;
2375 mddev->new_chunk_sectors = 0;
2376 conf = setup_conf(mddev);
2381 return ERR_PTR(-EINVAL);
2384 static struct mdk_personality raid1_personality =
2388 .owner = THIS_MODULE,
2389 .make_request = make_request,
2393 .error_handler = error,
2394 .hot_add_disk = raid1_add_disk,
2395 .hot_remove_disk= raid1_remove_disk,
2396 .spare_active = raid1_spare_active,
2397 .sync_request = sync_request,
2398 .resize = raid1_resize,
2400 .check_reshape = raid1_reshape,
2401 .quiesce = raid1_quiesce,
2402 .takeover = raid1_takeover,
2405 static int __init raid_init(void)
2407 return register_md_personality(&raid1_personality);
2410 static void raid_exit(void)
2412 unregister_md_personality(&raid1_personality);
2415 module_init(raid_init);
2416 module_exit(raid_exit);
2417 MODULE_LICENSE("GPL");
2418 MODULE_DESCRIPTION("RAID1 (mirroring) personality for MD");
2419 MODULE_ALIAS("md-personality-3"); /* RAID1 */
2420 MODULE_ALIAS("md-raid1");
2421 MODULE_ALIAS("md-level-1");