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 "dm-bio-list.h"
35 #include <linux/raid/raid1.h>
36 #include <linux/raid/bitmap.h>
40 #define PRINTK(x...) printk(x)
46 * Number of guaranteed r1bios in case of extreme VM load:
48 #define NR_RAID1_BIOS 256
50 static mdk_personality_t raid1_personality;
52 static void unplug_slaves(mddev_t *mddev);
54 static void allow_barrier(conf_t *conf);
55 static void lower_barrier(conf_t *conf);
57 static void * r1bio_pool_alloc(gfp_t gfp_flags, void *data)
59 struct pool_info *pi = data;
61 int size = offsetof(r1bio_t, bios[pi->raid_disks]);
63 /* allocate a r1bio with room for raid_disks entries in the bios array */
64 r1_bio = kmalloc(size, gfp_flags);
66 memset(r1_bio, 0, size);
68 unplug_slaves(pi->mddev);
73 static void r1bio_pool_free(void *r1_bio, void *data)
78 #define RESYNC_BLOCK_SIZE (64*1024)
79 //#define RESYNC_BLOCK_SIZE PAGE_SIZE
80 #define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
81 #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
82 #define RESYNC_WINDOW (2048*1024)
84 static void * r1buf_pool_alloc(gfp_t gfp_flags, void *data)
86 struct pool_info *pi = data;
92 r1_bio = r1bio_pool_alloc(gfp_flags, pi);
94 unplug_slaves(pi->mddev);
99 * Allocate bios : 1 for reading, n-1 for writing
101 for (j = pi->raid_disks ; j-- ; ) {
102 bio = bio_alloc(gfp_flags, RESYNC_PAGES);
105 r1_bio->bios[j] = bio;
108 * Allocate RESYNC_PAGES data pages and attach them to
111 bio = r1_bio->bios[0];
112 for (i = 0; i < RESYNC_PAGES; i++) {
113 page = alloc_page(gfp_flags);
117 bio->bi_io_vec[i].bv_page = page;
120 r1_bio->master_bio = NULL;
126 __free_page(bio->bi_io_vec[i-1].bv_page);
128 while ( ++j < pi->raid_disks )
129 bio_put(r1_bio->bios[j]);
130 r1bio_pool_free(r1_bio, data);
134 static void r1buf_pool_free(void *__r1_bio, void *data)
136 struct pool_info *pi = data;
138 r1bio_t *r1bio = __r1_bio;
139 struct bio *bio = r1bio->bios[0];
141 for (i = 0; i < RESYNC_PAGES; i++) {
142 __free_page(bio->bi_io_vec[i].bv_page);
143 bio->bi_io_vec[i].bv_page = NULL;
145 for (i=0 ; i < pi->raid_disks; i++)
146 bio_put(r1bio->bios[i]);
148 r1bio_pool_free(r1bio, data);
151 static void put_all_bios(conf_t *conf, r1bio_t *r1_bio)
155 for (i = 0; i < conf->raid_disks; i++) {
156 struct bio **bio = r1_bio->bios + i;
163 static inline void free_r1bio(r1bio_t *r1_bio)
165 conf_t *conf = mddev_to_conf(r1_bio->mddev);
168 * Wake up any possible resync thread that waits for the device
173 put_all_bios(conf, r1_bio);
174 mempool_free(r1_bio, conf->r1bio_pool);
177 static inline void put_buf(r1bio_t *r1_bio)
179 conf_t *conf = mddev_to_conf(r1_bio->mddev);
181 mempool_free(r1_bio, conf->r1buf_pool);
186 static void reschedule_retry(r1bio_t *r1_bio)
189 mddev_t *mddev = r1_bio->mddev;
190 conf_t *conf = mddev_to_conf(mddev);
192 spin_lock_irqsave(&conf->device_lock, flags);
193 list_add(&r1_bio->retry_list, &conf->retry_list);
194 spin_unlock_irqrestore(&conf->device_lock, flags);
196 wake_up(&conf->wait_barrier);
197 md_wakeup_thread(mddev->thread);
201 * raid_end_bio_io() is called when we have finished servicing a mirrored
202 * operation and are ready to return a success/failure code to the buffer
205 static void raid_end_bio_io(r1bio_t *r1_bio)
207 struct bio *bio = r1_bio->master_bio;
209 /* if nobody has done the final endio yet, do it now */
210 if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
211 PRINTK(KERN_DEBUG "raid1: sync end %s on sectors %llu-%llu\n",
212 (bio_data_dir(bio) == WRITE) ? "write" : "read",
213 (unsigned long long) bio->bi_sector,
214 (unsigned long long) bio->bi_sector +
215 (bio->bi_size >> 9) - 1);
217 bio_endio(bio, bio->bi_size,
218 test_bit(R1BIO_Uptodate, &r1_bio->state) ? 0 : -EIO);
224 * Update disk head position estimator based on IRQ completion info.
226 static inline void update_head_pos(int disk, r1bio_t *r1_bio)
228 conf_t *conf = mddev_to_conf(r1_bio->mddev);
230 conf->mirrors[disk].head_position =
231 r1_bio->sector + (r1_bio->sectors);
234 static int raid1_end_read_request(struct bio *bio, unsigned int bytes_done, int error)
236 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
237 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
239 conf_t *conf = mddev_to_conf(r1_bio->mddev);
244 mirror = r1_bio->read_disk;
246 * this branch is our 'one mirror IO has finished' event handler:
249 md_error(r1_bio->mddev, conf->mirrors[mirror].rdev);
252 * Set R1BIO_Uptodate in our master bio, so that
253 * we will return a good error code for to the higher
254 * levels even if IO on some other mirrored buffer fails.
256 * The 'master' represents the composite IO operation to
257 * user-side. So if something waits for IO, then it will
258 * wait for the 'master' bio.
260 set_bit(R1BIO_Uptodate, &r1_bio->state);
262 update_head_pos(mirror, r1_bio);
265 * we have only one bio on the read side
268 raid_end_bio_io(r1_bio);
273 char b[BDEVNAME_SIZE];
274 if (printk_ratelimit())
275 printk(KERN_ERR "raid1: %s: rescheduling sector %llu\n",
276 bdevname(conf->mirrors[mirror].rdev->bdev,b), (unsigned long long)r1_bio->sector);
277 reschedule_retry(r1_bio);
280 rdev_dec_pending(conf->mirrors[mirror].rdev, conf->mddev);
284 static int raid1_end_write_request(struct bio *bio, unsigned int bytes_done, int error)
286 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
287 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
288 int mirror, behind = test_bit(R1BIO_BehindIO, &r1_bio->state);
289 conf_t *conf = mddev_to_conf(r1_bio->mddev);
294 for (mirror = 0; mirror < conf->raid_disks; mirror++)
295 if (r1_bio->bios[mirror] == bio)
298 if (error == -ENOTSUPP && test_bit(R1BIO_Barrier, &r1_bio->state)) {
299 set_bit(BarriersNotsupp, &conf->mirrors[mirror].rdev->flags);
300 set_bit(R1BIO_BarrierRetry, &r1_bio->state);
301 r1_bio->mddev->barriers_work = 0;
304 * this branch is our 'one mirror IO has finished' event handler:
306 r1_bio->bios[mirror] = NULL;
308 md_error(r1_bio->mddev, conf->mirrors[mirror].rdev);
309 /* an I/O failed, we can't clear the bitmap */
310 set_bit(R1BIO_Degraded, &r1_bio->state);
313 * Set R1BIO_Uptodate in our master bio, so that
314 * we will return a good error code for to the higher
315 * levels even if IO on some other mirrored buffer fails.
317 * The 'master' represents the composite IO operation to
318 * user-side. So if something waits for IO, then it will
319 * wait for the 'master' bio.
321 set_bit(R1BIO_Uptodate, &r1_bio->state);
323 update_head_pos(mirror, r1_bio);
326 if (test_bit(WriteMostly, &conf->mirrors[mirror].rdev->flags))
327 atomic_dec(&r1_bio->behind_remaining);
329 /* In behind mode, we ACK the master bio once the I/O has safely
330 * reached all non-writemostly disks. Setting the Returned bit
331 * ensures that this gets done only once -- we don't ever want to
332 * return -EIO here, instead we'll wait */
334 if (atomic_read(&r1_bio->behind_remaining) >= (atomic_read(&r1_bio->remaining)-1) &&
335 test_bit(R1BIO_Uptodate, &r1_bio->state)) {
336 /* Maybe we can return now */
337 if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
338 struct bio *mbio = r1_bio->master_bio;
339 PRINTK(KERN_DEBUG "raid1: behind end write sectors %llu-%llu\n",
340 (unsigned long long) mbio->bi_sector,
341 (unsigned long long) mbio->bi_sector +
342 (mbio->bi_size >> 9) - 1);
343 bio_endio(mbio, mbio->bi_size, 0);
350 * Let's see if all mirrored write operations have finished
353 if (atomic_dec_and_test(&r1_bio->remaining)) {
354 if (test_bit(R1BIO_BarrierRetry, &r1_bio->state)) {
355 reschedule_retry(r1_bio);
356 /* Don't dec_pending yet, we want to hold
357 * the reference over the retry
361 if (test_bit(R1BIO_BehindIO, &r1_bio->state)) {
362 /* free extra copy of the data pages */
363 int i = bio->bi_vcnt;
365 __free_page(bio->bi_io_vec[i].bv_page);
367 /* clear the bitmap if all writes complete successfully */
368 bitmap_endwrite(r1_bio->mddev->bitmap, r1_bio->sector,
370 !test_bit(R1BIO_Degraded, &r1_bio->state),
372 md_write_end(r1_bio->mddev);
373 raid_end_bio_io(r1_bio);
376 if (r1_bio->bios[mirror]==NULL)
379 rdev_dec_pending(conf->mirrors[mirror].rdev, conf->mddev);
385 * This routine returns the disk from which the requested read should
386 * be done. There is a per-array 'next expected sequential IO' sector
387 * number - if this matches on the next IO then we use the last disk.
388 * There is also a per-disk 'last know head position' sector that is
389 * maintained from IRQ contexts, both the normal and the resync IO
390 * completion handlers update this position correctly. If there is no
391 * perfect sequential match then we pick the disk whose head is closest.
393 * If there are 2 mirrors in the same 2 devices, performance degrades
394 * because position is mirror, not device based.
396 * The rdev for the device selected will have nr_pending incremented.
398 static int read_balance(conf_t *conf, r1bio_t *r1_bio)
400 const unsigned long this_sector = r1_bio->sector;
401 int new_disk = conf->last_used, disk = new_disk;
403 const int sectors = r1_bio->sectors;
404 sector_t new_distance, current_distance;
409 * Check if we can balance. We can balance on the whole
410 * device if no resync is going on, or below the resync window.
411 * We take the first readable disk when above the resync window.
414 if (conf->mddev->recovery_cp < MaxSector &&
415 (this_sector + sectors >= conf->next_resync)) {
416 /* Choose the first operation device, for consistancy */
419 for (rdev = rcu_dereference(conf->mirrors[new_disk].rdev);
420 !rdev || !test_bit(In_sync, &rdev->flags)
421 || test_bit(WriteMostly, &rdev->flags);
422 rdev = rcu_dereference(conf->mirrors[++new_disk].rdev)) {
424 if (rdev && test_bit(In_sync, &rdev->flags))
425 wonly_disk = new_disk;
427 if (new_disk == conf->raid_disks - 1) {
428 new_disk = wonly_disk;
436 /* make sure the disk is operational */
437 for (rdev = rcu_dereference(conf->mirrors[new_disk].rdev);
438 !rdev || !test_bit(In_sync, &rdev->flags) ||
439 test_bit(WriteMostly, &rdev->flags);
440 rdev = rcu_dereference(conf->mirrors[new_disk].rdev)) {
442 if (rdev && test_bit(In_sync, &rdev->flags))
443 wonly_disk = new_disk;
446 new_disk = conf->raid_disks;
448 if (new_disk == disk) {
449 new_disk = wonly_disk;
458 /* now disk == new_disk == starting point for search */
461 * Don't change to another disk for sequential reads:
463 if (conf->next_seq_sect == this_sector)
465 if (this_sector == conf->mirrors[new_disk].head_position)
468 current_distance = abs(this_sector - conf->mirrors[disk].head_position);
470 /* Find the disk whose head is closest */
474 disk = conf->raid_disks;
477 rdev = rcu_dereference(conf->mirrors[disk].rdev);
480 !test_bit(In_sync, &rdev->flags) ||
481 test_bit(WriteMostly, &rdev->flags))
484 if (!atomic_read(&rdev->nr_pending)) {
488 new_distance = abs(this_sector - conf->mirrors[disk].head_position);
489 if (new_distance < current_distance) {
490 current_distance = new_distance;
493 } while (disk != conf->last_used);
499 rdev = rcu_dereference(conf->mirrors[new_disk].rdev);
502 atomic_inc(&rdev->nr_pending);
503 if (!test_bit(In_sync, &rdev->flags)) {
504 /* cannot risk returning a device that failed
505 * before we inc'ed nr_pending
507 atomic_dec(&rdev->nr_pending);
510 conf->next_seq_sect = this_sector + sectors;
511 conf->last_used = new_disk;
518 static void unplug_slaves(mddev_t *mddev)
520 conf_t *conf = mddev_to_conf(mddev);
524 for (i=0; i<mddev->raid_disks; i++) {
525 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
526 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
527 request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
529 atomic_inc(&rdev->nr_pending);
532 if (r_queue->unplug_fn)
533 r_queue->unplug_fn(r_queue);
535 rdev_dec_pending(rdev, mddev);
542 static void raid1_unplug(request_queue_t *q)
544 mddev_t *mddev = q->queuedata;
546 unplug_slaves(mddev);
547 md_wakeup_thread(mddev->thread);
550 static int raid1_issue_flush(request_queue_t *q, struct gendisk *disk,
551 sector_t *error_sector)
553 mddev_t *mddev = q->queuedata;
554 conf_t *conf = mddev_to_conf(mddev);
558 for (i=0; i<mddev->raid_disks && ret == 0; i++) {
559 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
560 if (rdev && !test_bit(Faulty, &rdev->flags)) {
561 struct block_device *bdev = rdev->bdev;
562 request_queue_t *r_queue = bdev_get_queue(bdev);
564 if (!r_queue->issue_flush_fn)
567 atomic_inc(&rdev->nr_pending);
569 ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
571 rdev_dec_pending(rdev, mddev);
581 * Sometimes we need to suspend IO while we do something else,
582 * either some resync/recovery, or reconfigure the array.
583 * To do this we raise a 'barrier'.
584 * The 'barrier' is a counter that can be raised multiple times
585 * to count how many activities are happening which preclude
587 * We can only raise the barrier if there is no pending IO.
588 * i.e. if nr_pending == 0.
589 * We choose only to raise the barrier if no-one is waiting for the
590 * barrier to go down. This means that as soon as an IO request
591 * is ready, no other operations which require a barrier will start
592 * until the IO request has had a chance.
594 * So: regular IO calls 'wait_barrier'. When that returns there
595 * is no backgroup IO happening, It must arrange to call
596 * allow_barrier when it has finished its IO.
597 * backgroup IO calls must call raise_barrier. Once that returns
598 * there is no normal IO happeing. It must arrange to call
599 * lower_barrier when the particular background IO completes.
601 #define RESYNC_DEPTH 32
603 static void raise_barrier(conf_t *conf)
605 spin_lock_irq(&conf->resync_lock);
607 /* Wait until no block IO is waiting */
608 wait_event_lock_irq(conf->wait_barrier, !conf->nr_waiting,
610 raid1_unplug(conf->mddev->queue));
612 /* block any new IO from starting */
615 /* No wait for all pending IO to complete */
616 wait_event_lock_irq(conf->wait_barrier,
617 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
619 raid1_unplug(conf->mddev->queue));
621 spin_unlock_irq(&conf->resync_lock);
624 static void lower_barrier(conf_t *conf)
627 spin_lock_irqsave(&conf->resync_lock, flags);
629 spin_unlock_irqrestore(&conf->resync_lock, flags);
630 wake_up(&conf->wait_barrier);
633 static void wait_barrier(conf_t *conf)
635 spin_lock_irq(&conf->resync_lock);
638 wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
640 raid1_unplug(conf->mddev->queue));
644 spin_unlock_irq(&conf->resync_lock);
647 static void allow_barrier(conf_t *conf)
650 spin_lock_irqsave(&conf->resync_lock, flags);
652 spin_unlock_irqrestore(&conf->resync_lock, flags);
653 wake_up(&conf->wait_barrier);
657 /* duplicate the data pages for behind I/O */
658 static struct page **alloc_behind_pages(struct bio *bio)
661 struct bio_vec *bvec;
662 struct page **pages = kmalloc(bio->bi_vcnt * sizeof(struct page *),
664 if (unlikely(!pages))
667 memset(pages, 0, bio->bi_vcnt * sizeof(struct page *));
669 bio_for_each_segment(bvec, bio, i) {
670 pages[i] = alloc_page(GFP_NOIO);
671 if (unlikely(!pages[i]))
673 memcpy(kmap(pages[i]) + bvec->bv_offset,
674 kmap(bvec->bv_page) + bvec->bv_offset, bvec->bv_len);
676 kunmap(bvec->bv_page);
683 for (i = 0; i < bio->bi_vcnt && pages[i]; i++)
684 __free_page(pages[i]);
686 PRINTK("%dB behind alloc failed, doing sync I/O\n", bio->bi_size);
690 static int make_request(request_queue_t *q, struct bio * bio)
692 mddev_t *mddev = q->queuedata;
693 conf_t *conf = mddev_to_conf(mddev);
694 mirror_info_t *mirror;
696 struct bio *read_bio;
697 int i, targets = 0, disks;
699 struct bitmap *bitmap = mddev->bitmap;
702 struct page **behind_pages = NULL;
703 const int rw = bio_data_dir(bio);
706 if (unlikely(!mddev->barriers_work && bio_barrier(bio))) {
707 bio_endio(bio, bio->bi_size, -EOPNOTSUPP);
712 * Register the new request and wait if the reconstruction
713 * thread has put up a bar for new requests.
714 * Continue immediately if no resync is active currently.
716 md_write_start(mddev, bio); /* wait on superblock update early */
720 disk_stat_inc(mddev->gendisk, ios[rw]);
721 disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bio));
724 * make_request() can abort the operation when READA is being
725 * used and no empty request is available.
728 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
730 r1_bio->master_bio = bio;
731 r1_bio->sectors = bio->bi_size >> 9;
733 r1_bio->mddev = mddev;
734 r1_bio->sector = bio->bi_sector;
738 * read balancing logic:
740 int rdisk = read_balance(conf, r1_bio);
743 /* couldn't find anywhere to read from */
744 raid_end_bio_io(r1_bio);
747 mirror = conf->mirrors + rdisk;
749 r1_bio->read_disk = rdisk;
751 read_bio = bio_clone(bio, GFP_NOIO);
753 r1_bio->bios[rdisk] = read_bio;
755 read_bio->bi_sector = r1_bio->sector + mirror->rdev->data_offset;
756 read_bio->bi_bdev = mirror->rdev->bdev;
757 read_bio->bi_end_io = raid1_end_read_request;
758 read_bio->bi_rw = READ;
759 read_bio->bi_private = r1_bio;
761 generic_make_request(read_bio);
768 /* first select target devices under spinlock and
769 * inc refcount on their rdev. Record them by setting
772 disks = conf->raid_disks;
774 { static int first=1;
775 if (first) printk("First Write sector %llu disks %d\n",
776 (unsigned long long)r1_bio->sector, disks);
781 for (i = 0; i < disks; i++) {
782 if ((rdev=rcu_dereference(conf->mirrors[i].rdev)) != NULL &&
783 !test_bit(Faulty, &rdev->flags)) {
784 atomic_inc(&rdev->nr_pending);
785 if (test_bit(Faulty, &rdev->flags)) {
786 atomic_dec(&rdev->nr_pending);
787 r1_bio->bios[i] = NULL;
789 r1_bio->bios[i] = bio;
792 r1_bio->bios[i] = NULL;
796 BUG_ON(targets == 0); /* we never fail the last device */
798 if (targets < conf->raid_disks) {
799 /* array is degraded, we will not clear the bitmap
800 * on I/O completion (see raid1_end_write_request) */
801 set_bit(R1BIO_Degraded, &r1_bio->state);
804 /* do behind I/O ? */
806 atomic_read(&bitmap->behind_writes) < bitmap->max_write_behind &&
807 (behind_pages = alloc_behind_pages(bio)) != NULL)
808 set_bit(R1BIO_BehindIO, &r1_bio->state);
810 atomic_set(&r1_bio->remaining, 0);
811 atomic_set(&r1_bio->behind_remaining, 0);
813 do_barriers = bio->bi_rw & BIO_RW_BARRIER;
815 set_bit(R1BIO_Barrier, &r1_bio->state);
818 for (i = 0; i < disks; i++) {
820 if (!r1_bio->bios[i])
823 mbio = bio_clone(bio, GFP_NOIO);
824 r1_bio->bios[i] = mbio;
826 mbio->bi_sector = r1_bio->sector + conf->mirrors[i].rdev->data_offset;
827 mbio->bi_bdev = conf->mirrors[i].rdev->bdev;
828 mbio->bi_end_io = raid1_end_write_request;
829 mbio->bi_rw = WRITE | do_barriers;
830 mbio->bi_private = r1_bio;
833 struct bio_vec *bvec;
836 /* Yes, I really want the '__' version so that
837 * we clear any unused pointer in the io_vec, rather
838 * than leave them unchanged. This is important
839 * because when we come to free the pages, we won't
840 * know the originial bi_idx, so we just free
843 __bio_for_each_segment(bvec, mbio, j, 0)
844 bvec->bv_page = behind_pages[j];
845 if (test_bit(WriteMostly, &conf->mirrors[i].rdev->flags))
846 atomic_inc(&r1_bio->behind_remaining);
849 atomic_inc(&r1_bio->remaining);
851 bio_list_add(&bl, mbio);
853 kfree(behind_pages); /* the behind pages are attached to the bios now */
855 bitmap_startwrite(bitmap, bio->bi_sector, r1_bio->sectors,
856 test_bit(R1BIO_BehindIO, &r1_bio->state));
857 spin_lock_irqsave(&conf->device_lock, flags);
858 bio_list_merge(&conf->pending_bio_list, &bl);
861 blk_plug_device(mddev->queue);
862 spin_unlock_irqrestore(&conf->device_lock, flags);
865 while ((bio = bio_list_pop(&bl)) != NULL)
866 generic_make_request(bio);
872 static void status(struct seq_file *seq, mddev_t *mddev)
874 conf_t *conf = mddev_to_conf(mddev);
877 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
878 conf->working_disks);
879 for (i = 0; i < conf->raid_disks; i++)
880 seq_printf(seq, "%s",
881 conf->mirrors[i].rdev &&
882 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
883 seq_printf(seq, "]");
887 static void error(mddev_t *mddev, mdk_rdev_t *rdev)
889 char b[BDEVNAME_SIZE];
890 conf_t *conf = mddev_to_conf(mddev);
893 * If it is not operational, then we have already marked it as dead
894 * else if it is the last working disks, ignore the error, let the
895 * next level up know.
896 * else mark the drive as failed
898 if (test_bit(In_sync, &rdev->flags)
899 && conf->working_disks == 1)
901 * Don't fail the drive, act as though we were just a
902 * normal single drive
905 if (test_bit(In_sync, &rdev->flags)) {
907 conf->working_disks--;
909 * if recovery is running, make sure it aborts.
911 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
913 clear_bit(In_sync, &rdev->flags);
914 set_bit(Faulty, &rdev->flags);
916 printk(KERN_ALERT "raid1: Disk failure on %s, disabling device. \n"
917 " Operation continuing on %d devices\n",
918 bdevname(rdev->bdev,b), conf->working_disks);
921 static void print_conf(conf_t *conf)
926 printk("RAID1 conf printout:\n");
931 printk(" --- wd:%d rd:%d\n", conf->working_disks,
934 for (i = 0; i < conf->raid_disks; i++) {
935 char b[BDEVNAME_SIZE];
936 tmp = conf->mirrors + i;
938 printk(" disk %d, wo:%d, o:%d, dev:%s\n",
939 i, !test_bit(In_sync, &tmp->rdev->flags), !test_bit(Faulty, &tmp->rdev->flags),
940 bdevname(tmp->rdev->bdev,b));
944 static void close_sync(conf_t *conf)
949 mempool_destroy(conf->r1buf_pool);
950 conf->r1buf_pool = NULL;
953 static int raid1_spare_active(mddev_t *mddev)
956 conf_t *conf = mddev->private;
960 * Find all failed disks within the RAID1 configuration
961 * and mark them readable
963 for (i = 0; i < conf->raid_disks; i++) {
964 tmp = conf->mirrors + i;
966 && !test_bit(Faulty, &tmp->rdev->flags)
967 && !test_bit(In_sync, &tmp->rdev->flags)) {
968 conf->working_disks++;
970 set_bit(In_sync, &tmp->rdev->flags);
979 static int raid1_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
981 conf_t *conf = mddev->private;
986 for (mirror=0; mirror < mddev->raid_disks; mirror++)
987 if ( !(p=conf->mirrors+mirror)->rdev) {
989 blk_queue_stack_limits(mddev->queue,
990 rdev->bdev->bd_disk->queue);
991 /* as we don't honour merge_bvec_fn, we must never risk
992 * violating it, so limit ->max_sector to one PAGE, as
993 * a one page request is never in violation.
995 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
996 mddev->queue->max_sectors > (PAGE_SIZE>>9))
997 blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
999 p->head_position = 0;
1000 rdev->raid_disk = mirror;
1002 /* As all devices are equivalent, we don't need a full recovery
1003 * if this was recently any drive of the array
1005 if (rdev->saved_raid_disk < 0)
1007 rcu_assign_pointer(p->rdev, rdev);
1015 static int raid1_remove_disk(mddev_t *mddev, int number)
1017 conf_t *conf = mddev->private;
1020 mirror_info_t *p = conf->mirrors+ number;
1025 if (test_bit(In_sync, &rdev->flags) ||
1026 atomic_read(&rdev->nr_pending)) {
1032 if (atomic_read(&rdev->nr_pending)) {
1033 /* lost the race, try later */
1045 static int end_sync_read(struct bio *bio, unsigned int bytes_done, int error)
1047 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1048 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
1049 conf_t *conf = mddev_to_conf(r1_bio->mddev);
1054 if (r1_bio->bios[r1_bio->read_disk] != bio)
1056 update_head_pos(r1_bio->read_disk, r1_bio);
1058 * we have read a block, now it needs to be re-written,
1059 * or re-read if the read failed.
1060 * We don't do much here, just schedule handling by raid1d
1063 md_error(r1_bio->mddev,
1064 conf->mirrors[r1_bio->read_disk].rdev);
1066 set_bit(R1BIO_Uptodate, &r1_bio->state);
1067 rdev_dec_pending(conf->mirrors[r1_bio->read_disk].rdev, conf->mddev);
1068 reschedule_retry(r1_bio);
1072 static int end_sync_write(struct bio *bio, unsigned int bytes_done, int error)
1074 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1075 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
1076 mddev_t *mddev = r1_bio->mddev;
1077 conf_t *conf = mddev_to_conf(mddev);
1084 for (i = 0; i < conf->raid_disks; i++)
1085 if (r1_bio->bios[i] == bio) {
1090 md_error(mddev, conf->mirrors[mirror].rdev);
1092 update_head_pos(mirror, r1_bio);
1094 if (atomic_dec_and_test(&r1_bio->remaining)) {
1095 md_done_sync(mddev, r1_bio->sectors, uptodate);
1098 rdev_dec_pending(conf->mirrors[mirror].rdev, mddev);
1102 static void sync_request_write(mddev_t *mddev, r1bio_t *r1_bio)
1104 conf_t *conf = mddev_to_conf(mddev);
1106 int disks = conf->raid_disks;
1107 struct bio *bio, *wbio;
1109 bio = r1_bio->bios[r1_bio->read_disk];
1112 if (r1_bio->sector == 0) printk("First sync write startss\n");
1117 if (!test_bit(R1BIO_Uptodate, &r1_bio->state)) {
1119 * There is no point trying a read-for-reconstruct as
1120 * reconstruct is about to be aborted
1122 char b[BDEVNAME_SIZE];
1123 printk(KERN_ALERT "raid1: %s: unrecoverable I/O read error"
1124 " for block %llu\n",
1125 bdevname(bio->bi_bdev,b),
1126 (unsigned long long)r1_bio->sector);
1127 md_done_sync(mddev, r1_bio->sectors, 0);
1132 atomic_set(&r1_bio->remaining, 1);
1133 for (i = 0; i < disks ; i++) {
1134 wbio = r1_bio->bios[i];
1135 if (wbio->bi_end_io != end_sync_write)
1138 atomic_inc(&conf->mirrors[i].rdev->nr_pending);
1139 atomic_inc(&r1_bio->remaining);
1140 md_sync_acct(conf->mirrors[i].rdev->bdev, wbio->bi_size >> 9);
1142 generic_make_request(wbio);
1145 if (atomic_dec_and_test(&r1_bio->remaining)) {
1146 /* if we're here, all write(s) have completed, so clean up */
1147 md_done_sync(mddev, r1_bio->sectors, 1);
1153 * This is a kernel thread which:
1155 * 1. Retries failed read operations on working mirrors.
1156 * 2. Updates the raid superblock when problems encounter.
1157 * 3. Performs writes following reads for array syncronising.
1160 static void raid1d(mddev_t *mddev)
1164 unsigned long flags;
1165 conf_t *conf = mddev_to_conf(mddev);
1166 struct list_head *head = &conf->retry_list;
1170 md_check_recovery(mddev);
1173 char b[BDEVNAME_SIZE];
1174 spin_lock_irqsave(&conf->device_lock, flags);
1176 if (conf->pending_bio_list.head) {
1177 bio = bio_list_get(&conf->pending_bio_list);
1178 blk_remove_plug(mddev->queue);
1179 spin_unlock_irqrestore(&conf->device_lock, flags);
1180 /* flush any pending bitmap writes to disk before proceeding w/ I/O */
1181 if (bitmap_unplug(mddev->bitmap) != 0)
1182 printk("%s: bitmap file write failed!\n", mdname(mddev));
1184 while (bio) { /* submit pending writes */
1185 struct bio *next = bio->bi_next;
1186 bio->bi_next = NULL;
1187 generic_make_request(bio);
1195 if (list_empty(head))
1197 r1_bio = list_entry(head->prev, r1bio_t, retry_list);
1198 list_del(head->prev);
1199 spin_unlock_irqrestore(&conf->device_lock, flags);
1201 mddev = r1_bio->mddev;
1202 conf = mddev_to_conf(mddev);
1203 if (test_bit(R1BIO_IsSync, &r1_bio->state)) {
1204 sync_request_write(mddev, r1_bio);
1206 } else if (test_bit(R1BIO_BarrierRetry, &r1_bio->state)) {
1207 /* some requests in the r1bio were BIO_RW_BARRIER
1208 * requests which failed with -ENOTSUPP. Hohumm..
1209 * Better resubmit without the barrier.
1210 * We know which devices to resubmit for, because
1211 * all others have had their bios[] entry cleared.
1214 clear_bit(R1BIO_BarrierRetry, &r1_bio->state);
1215 clear_bit(R1BIO_Barrier, &r1_bio->state);
1216 for (i=0; i < conf->raid_disks; i++)
1217 if (r1_bio->bios[i]) {
1218 struct bio_vec *bvec;
1221 bio = bio_clone(r1_bio->master_bio, GFP_NOIO);
1222 /* copy pages from the failed bio, as
1223 * this might be a write-behind device */
1224 __bio_for_each_segment(bvec, bio, j, 0)
1225 bvec->bv_page = bio_iovec_idx(r1_bio->bios[i], j)->bv_page;
1226 bio_put(r1_bio->bios[i]);
1227 bio->bi_sector = r1_bio->sector +
1228 conf->mirrors[i].rdev->data_offset;
1229 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
1230 bio->bi_end_io = raid1_end_write_request;
1232 bio->bi_private = r1_bio;
1233 r1_bio->bios[i] = bio;
1234 generic_make_request(bio);
1238 bio = r1_bio->bios[r1_bio->read_disk];
1239 if ((disk=read_balance(conf, r1_bio)) == -1) {
1240 printk(KERN_ALERT "raid1: %s: unrecoverable I/O"
1241 " read error for block %llu\n",
1242 bdevname(bio->bi_bdev,b),
1243 (unsigned long long)r1_bio->sector);
1244 raid_end_bio_io(r1_bio);
1246 r1_bio->bios[r1_bio->read_disk] = NULL;
1247 r1_bio->read_disk = disk;
1249 bio = bio_clone(r1_bio->master_bio, GFP_NOIO);
1250 r1_bio->bios[r1_bio->read_disk] = bio;
1251 rdev = conf->mirrors[disk].rdev;
1252 if (printk_ratelimit())
1253 printk(KERN_ERR "raid1: %s: redirecting sector %llu to"
1254 " another mirror\n",
1255 bdevname(rdev->bdev,b),
1256 (unsigned long long)r1_bio->sector);
1257 bio->bi_sector = r1_bio->sector + rdev->data_offset;
1258 bio->bi_bdev = rdev->bdev;
1259 bio->bi_end_io = raid1_end_read_request;
1261 bio->bi_private = r1_bio;
1263 generic_make_request(bio);
1267 spin_unlock_irqrestore(&conf->device_lock, flags);
1269 unplug_slaves(mddev);
1273 static int init_resync(conf_t *conf)
1277 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
1278 if (conf->r1buf_pool)
1280 conf->r1buf_pool = mempool_create(buffs, r1buf_pool_alloc, r1buf_pool_free,
1282 if (!conf->r1buf_pool)
1284 conf->next_resync = 0;
1289 * perform a "sync" on one "block"
1291 * We need to make sure that no normal I/O request - particularly write
1292 * requests - conflict with active sync requests.
1294 * This is achieved by tracking pending requests and a 'barrier' concept
1295 * that can be installed to exclude normal IO requests.
1298 static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
1300 conf_t *conf = mddev_to_conf(mddev);
1301 mirror_info_t *mirror;
1304 sector_t max_sector, nr_sectors;
1308 int write_targets = 0;
1310 int still_degraded = 0;
1312 if (!conf->r1buf_pool)
1315 printk("sync start - bitmap %p\n", mddev->bitmap);
1317 if (init_resync(conf))
1321 max_sector = mddev->size << 1;
1322 if (sector_nr >= max_sector) {
1323 /* If we aborted, we need to abort the
1324 * sync on the 'current' bitmap chunk (there will
1325 * only be one in raid1 resync.
1326 * We can find the current addess in mddev->curr_resync
1328 if (mddev->curr_resync < max_sector) /* aborted */
1329 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
1331 else /* completed sync */
1334 bitmap_close_sync(mddev->bitmap);
1339 /* before building a request, check if we can skip these blocks..
1340 * This call the bitmap_start_sync doesn't actually record anything
1342 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
1343 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
1344 /* We can skip this block, and probably several more */
1349 * If there is non-resync activity waiting for a turn,
1350 * and resync is going fast enough,
1351 * then let it though before starting on this new sync request.
1353 if (!go_faster && conf->nr_waiting)
1354 msleep_interruptible(1000);
1356 raise_barrier(conf);
1358 conf->next_resync = sector_nr;
1361 * If reconstructing, and >1 working disc,
1362 * could dedicate one to rebuild and others to
1363 * service read requests ..
1365 disk = conf->last_used;
1366 /* make sure disk is operational */
1368 while (conf->mirrors[disk].rdev == NULL ||
1369 !test_bit(In_sync, &conf->mirrors[disk].rdev->flags) ||
1370 test_bit(WriteMostly, &conf->mirrors[disk].rdev->flags)
1372 if (conf->mirrors[disk].rdev &&
1373 test_bit(In_sync, &conf->mirrors[disk].rdev->flags))
1376 disk = conf->raid_disks;
1378 if (disk == conf->last_used) {
1383 conf->last_used = disk;
1384 atomic_inc(&conf->mirrors[disk].rdev->nr_pending);
1387 mirror = conf->mirrors + disk;
1389 r1_bio = mempool_alloc(conf->r1buf_pool, GFP_NOIO);
1391 r1_bio->mddev = mddev;
1392 r1_bio->sector = sector_nr;
1394 set_bit(R1BIO_IsSync, &r1_bio->state);
1395 r1_bio->read_disk = disk;
1397 for (i=0; i < conf->raid_disks; i++) {
1398 bio = r1_bio->bios[i];
1400 /* take from bio_init */
1401 bio->bi_next = NULL;
1402 bio->bi_flags |= 1 << BIO_UPTODATE;
1406 bio->bi_phys_segments = 0;
1407 bio->bi_hw_segments = 0;
1409 bio->bi_end_io = NULL;
1410 bio->bi_private = NULL;
1414 bio->bi_end_io = end_sync_read;
1415 } else if (conf->mirrors[i].rdev == NULL ||
1416 test_bit(Faulty, &conf->mirrors[i].rdev->flags)) {
1419 } else if (!test_bit(In_sync, &conf->mirrors[i].rdev->flags) ||
1420 sector_nr + RESYNC_SECTORS > mddev->recovery_cp ||
1421 test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
1423 bio->bi_end_io = end_sync_write;
1426 /* no need to read or write here */
1428 bio->bi_sector = sector_nr + conf->mirrors[i].rdev->data_offset;
1429 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
1430 bio->bi_private = r1_bio;
1433 if (write_targets == 0) {
1434 /* There is nowhere to write, so all non-sync
1435 * drives must be failed - so we are finished
1437 sector_t rv = max_sector - sector_nr;
1440 rdev_dec_pending(conf->mirrors[disk].rdev, mddev);
1448 int len = PAGE_SIZE;
1449 if (sector_nr + (len>>9) > max_sector)
1450 len = (max_sector - sector_nr) << 9;
1453 if (sync_blocks == 0) {
1454 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
1455 &sync_blocks, still_degraded) &&
1457 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
1459 if (sync_blocks < (PAGE_SIZE>>9))
1461 if (len > (sync_blocks<<9))
1462 len = sync_blocks<<9;
1465 for (i=0 ; i < conf->raid_disks; i++) {
1466 bio = r1_bio->bios[i];
1467 if (bio->bi_end_io) {
1468 page = r1_bio->bios[0]->bi_io_vec[bio->bi_vcnt].bv_page;
1469 if (bio_add_page(bio, page, len, 0) == 0) {
1471 r1_bio->bios[0]->bi_io_vec[bio->bi_vcnt].bv_page = page;
1474 bio = r1_bio->bios[i];
1475 if (bio->bi_end_io==NULL)
1477 /* remove last page from this bio */
1479 bio->bi_size -= len;
1480 bio->bi_flags &= ~(1<< BIO_SEG_VALID);
1486 nr_sectors += len>>9;
1487 sector_nr += len>>9;
1488 sync_blocks -= (len>>9);
1489 } while (r1_bio->bios[disk]->bi_vcnt < RESYNC_PAGES);
1491 bio = r1_bio->bios[disk];
1492 r1_bio->sectors = nr_sectors;
1494 md_sync_acct(mirror->rdev->bdev, nr_sectors);
1496 generic_make_request(bio);
1501 static int run(mddev_t *mddev)
1505 mirror_info_t *disk;
1507 struct list_head *tmp;
1509 if (mddev->level != 1) {
1510 printk("raid1: %s: raid level not set to mirroring (%d)\n",
1511 mdname(mddev), mddev->level);
1515 * copy the already verified devices into our private RAID1
1516 * bookkeeping area. [whatever we allocate in run(),
1517 * should be freed in stop()]
1519 conf = kmalloc(sizeof(conf_t), GFP_KERNEL);
1520 mddev->private = conf;
1524 memset(conf, 0, sizeof(*conf));
1525 conf->mirrors = kmalloc(sizeof(struct mirror_info)*mddev->raid_disks,
1530 memset(conf->mirrors, 0, sizeof(struct mirror_info)*mddev->raid_disks);
1532 conf->poolinfo = kmalloc(sizeof(*conf->poolinfo), GFP_KERNEL);
1533 if (!conf->poolinfo)
1535 conf->poolinfo->mddev = mddev;
1536 conf->poolinfo->raid_disks = mddev->raid_disks;
1537 conf->r1bio_pool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
1540 if (!conf->r1bio_pool)
1543 ITERATE_RDEV(mddev, rdev, tmp) {
1544 disk_idx = rdev->raid_disk;
1545 if (disk_idx >= mddev->raid_disks
1548 disk = conf->mirrors + disk_idx;
1552 blk_queue_stack_limits(mddev->queue,
1553 rdev->bdev->bd_disk->queue);
1554 /* as we don't honour merge_bvec_fn, we must never risk
1555 * violating it, so limit ->max_sector to one PAGE, as
1556 * a one page request is never in violation.
1558 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
1559 mddev->queue->max_sectors > (PAGE_SIZE>>9))
1560 blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
1562 disk->head_position = 0;
1563 if (!test_bit(Faulty, &rdev->flags) && test_bit(In_sync, &rdev->flags))
1564 conf->working_disks++;
1566 conf->raid_disks = mddev->raid_disks;
1567 conf->mddev = mddev;
1568 spin_lock_init(&conf->device_lock);
1569 INIT_LIST_HEAD(&conf->retry_list);
1570 if (conf->working_disks == 1)
1571 mddev->recovery_cp = MaxSector;
1573 spin_lock_init(&conf->resync_lock);
1574 init_waitqueue_head(&conf->wait_barrier);
1576 bio_list_init(&conf->pending_bio_list);
1577 bio_list_init(&conf->flushing_bio_list);
1579 if (!conf->working_disks) {
1580 printk(KERN_ERR "raid1: no operational mirrors for %s\n",
1585 mddev->degraded = 0;
1586 for (i = 0; i < conf->raid_disks; i++) {
1588 disk = conf->mirrors + i;
1591 disk->head_position = 0;
1597 * find the first working one and use it as a starting point
1598 * to read balancing.
1600 for (j = 0; j < conf->raid_disks &&
1601 (!conf->mirrors[j].rdev ||
1602 !test_bit(In_sync, &conf->mirrors[j].rdev->flags)) ; j++)
1604 conf->last_used = j;
1607 mddev->thread = md_register_thread(raid1d, mddev, "%s_raid1");
1608 if (!mddev->thread) {
1610 "raid1: couldn't allocate thread for %s\n",
1614 if (mddev->bitmap) mddev->thread->timeout = mddev->bitmap->daemon_sleep * HZ;
1617 "raid1: raid set %s active with %d out of %d mirrors\n",
1618 mdname(mddev), mddev->raid_disks - mddev->degraded,
1621 * Ok, everything is just fine now
1623 mddev->array_size = mddev->size;
1625 mddev->queue->unplug_fn = raid1_unplug;
1626 mddev->queue->issue_flush_fn = raid1_issue_flush;
1631 printk(KERN_ERR "raid1: couldn't allocate memory for %s\n",
1636 if (conf->r1bio_pool)
1637 mempool_destroy(conf->r1bio_pool);
1638 kfree(conf->mirrors);
1639 kfree(conf->poolinfo);
1641 mddev->private = NULL;
1647 static int stop(mddev_t *mddev)
1649 conf_t *conf = mddev_to_conf(mddev);
1650 struct bitmap *bitmap = mddev->bitmap;
1651 int behind_wait = 0;
1653 /* wait for behind writes to complete */
1654 while (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
1656 printk(KERN_INFO "raid1: behind writes in progress on device %s, waiting to stop (%d)\n", mdname(mddev), behind_wait);
1657 set_current_state(TASK_UNINTERRUPTIBLE);
1658 schedule_timeout(HZ); /* wait a second */
1659 /* need to kick something here to make sure I/O goes? */
1662 md_unregister_thread(mddev->thread);
1663 mddev->thread = NULL;
1664 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
1665 if (conf->r1bio_pool)
1666 mempool_destroy(conf->r1bio_pool);
1667 kfree(conf->mirrors);
1668 kfree(conf->poolinfo);
1670 mddev->private = NULL;
1674 static int raid1_resize(mddev_t *mddev, sector_t sectors)
1676 /* no resync is happening, and there is enough space
1677 * on all devices, so we can resize.
1678 * We need to make sure resync covers any new space.
1679 * If the array is shrinking we should possibly wait until
1680 * any io in the removed space completes, but it hardly seems
1683 mddev->array_size = sectors>>1;
1684 set_capacity(mddev->gendisk, mddev->array_size << 1);
1686 if (mddev->array_size > mddev->size && mddev->recovery_cp == MaxSector) {
1687 mddev->recovery_cp = mddev->size << 1;
1688 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1690 mddev->size = mddev->array_size;
1691 mddev->resync_max_sectors = sectors;
1695 static int raid1_reshape(mddev_t *mddev, int raid_disks)
1698 * 1/ resize the r1bio_pool
1699 * 2/ resize conf->mirrors
1701 * We allocate a new r1bio_pool if we can.
1702 * Then raise a device barrier and wait until all IO stops.
1703 * Then resize conf->mirrors and swap in the new r1bio pool.
1705 * At the same time, we "pack" the devices so that all the missing
1706 * devices have the higher raid_disk numbers.
1708 mempool_t *newpool, *oldpool;
1709 struct pool_info *newpoolinfo;
1710 mirror_info_t *newmirrors;
1711 conf_t *conf = mddev_to_conf(mddev);
1716 if (raid_disks < conf->raid_disks) {
1718 for (d= 0; d < conf->raid_disks; d++)
1719 if (conf->mirrors[d].rdev)
1721 if (cnt > raid_disks)
1725 newpoolinfo = kmalloc(sizeof(*newpoolinfo), GFP_KERNEL);
1728 newpoolinfo->mddev = mddev;
1729 newpoolinfo->raid_disks = raid_disks;
1731 newpool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
1732 r1bio_pool_free, newpoolinfo);
1737 newmirrors = kmalloc(sizeof(struct mirror_info) * raid_disks, GFP_KERNEL);
1740 mempool_destroy(newpool);
1743 memset(newmirrors, 0, sizeof(struct mirror_info)*raid_disks);
1745 raise_barrier(conf);
1747 /* ok, everything is stopped */
1748 oldpool = conf->r1bio_pool;
1749 conf->r1bio_pool = newpool;
1751 for (d=d2=0; d < conf->raid_disks; d++)
1752 if (conf->mirrors[d].rdev) {
1753 conf->mirrors[d].rdev->raid_disk = d2;
1754 newmirrors[d2++].rdev = conf->mirrors[d].rdev;
1756 kfree(conf->mirrors);
1757 conf->mirrors = newmirrors;
1758 kfree(conf->poolinfo);
1759 conf->poolinfo = newpoolinfo;
1761 mddev->degraded += (raid_disks - conf->raid_disks);
1762 conf->raid_disks = mddev->raid_disks = raid_disks;
1764 conf->last_used = 0; /* just make sure it is in-range */
1765 lower_barrier(conf);
1767 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1768 md_wakeup_thread(mddev->thread);
1770 mempool_destroy(oldpool);
1774 static void raid1_quiesce(mddev_t *mddev, int state)
1776 conf_t *conf = mddev_to_conf(mddev);
1780 raise_barrier(conf);
1783 lower_barrier(conf);
1786 if (mddev->thread) {
1788 mddev->thread->timeout = mddev->bitmap->daemon_sleep * HZ;
1790 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
1791 md_wakeup_thread(mddev->thread);
1796 static mdk_personality_t raid1_personality =
1799 .owner = THIS_MODULE,
1800 .make_request = make_request,
1804 .error_handler = error,
1805 .hot_add_disk = raid1_add_disk,
1806 .hot_remove_disk= raid1_remove_disk,
1807 .spare_active = raid1_spare_active,
1808 .sync_request = sync_request,
1809 .resize = raid1_resize,
1810 .reshape = raid1_reshape,
1811 .quiesce = raid1_quiesce,
1814 static int __init raid_init(void)
1816 return register_md_personality(RAID1, &raid1_personality);
1819 static void raid_exit(void)
1821 unregister_md_personality(RAID1);
1824 module_init(raid_init);
1825 module_exit(raid_exit);
1826 MODULE_LICENSE("GPL");
1827 MODULE_ALIAS("md-personality-3"); /* RAID1 */