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);
195 spin_unlock_irqrestore(&conf->device_lock, flags);
197 wake_up(&conf->wait_barrier);
198 md_wakeup_thread(mddev->thread);
202 * raid_end_bio_io() is called when we have finished servicing a mirrored
203 * operation and are ready to return a success/failure code to the buffer
206 static void raid_end_bio_io(r1bio_t *r1_bio)
208 struct bio *bio = r1_bio->master_bio;
210 /* if nobody has done the final endio yet, do it now */
211 if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
212 PRINTK(KERN_DEBUG "raid1: sync end %s on sectors %llu-%llu\n",
213 (bio_data_dir(bio) == WRITE) ? "write" : "read",
214 (unsigned long long) bio->bi_sector,
215 (unsigned long long) bio->bi_sector +
216 (bio->bi_size >> 9) - 1);
218 bio_endio(bio, bio->bi_size,
219 test_bit(R1BIO_Uptodate, &r1_bio->state) ? 0 : -EIO);
225 * Update disk head position estimator based on IRQ completion info.
227 static inline void update_head_pos(int disk, r1bio_t *r1_bio)
229 conf_t *conf = mddev_to_conf(r1_bio->mddev);
231 conf->mirrors[disk].head_position =
232 r1_bio->sector + (r1_bio->sectors);
235 static int raid1_end_read_request(struct bio *bio, unsigned int bytes_done, int error)
237 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
238 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
240 conf_t *conf = mddev_to_conf(r1_bio->mddev);
245 mirror = r1_bio->read_disk;
247 * this branch is our 'one mirror IO has finished' event handler:
249 update_head_pos(mirror, r1_bio);
251 if (uptodate || conf->working_disks <= 1) {
253 * Set R1BIO_Uptodate in our master bio, so that
254 * we will return a good error code for to the higher
255 * levels even if IO on some other mirrored buffer fails.
257 * The 'master' represents the composite IO operation to
258 * user-side. So if something waits for IO, then it will
259 * wait for the 'master' bio.
261 set_bit(R1BIO_Uptodate, &r1_bio->state);
263 raid_end_bio_io(r1_bio);
268 char b[BDEVNAME_SIZE];
269 if (printk_ratelimit())
270 printk(KERN_ERR "raid1: %s: rescheduling sector %llu\n",
271 bdevname(conf->mirrors[mirror].rdev->bdev,b), (unsigned long long)r1_bio->sector);
272 reschedule_retry(r1_bio);
275 rdev_dec_pending(conf->mirrors[mirror].rdev, conf->mddev);
279 static int raid1_end_write_request(struct bio *bio, unsigned int bytes_done, int error)
281 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
282 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
283 int mirror, behind = test_bit(R1BIO_BehindIO, &r1_bio->state);
284 conf_t *conf = mddev_to_conf(r1_bio->mddev);
289 for (mirror = 0; mirror < conf->raid_disks; mirror++)
290 if (r1_bio->bios[mirror] == bio)
293 if (error == -ENOTSUPP && test_bit(R1BIO_Barrier, &r1_bio->state)) {
294 set_bit(BarriersNotsupp, &conf->mirrors[mirror].rdev->flags);
295 set_bit(R1BIO_BarrierRetry, &r1_bio->state);
296 r1_bio->mddev->barriers_work = 0;
299 * this branch is our 'one mirror IO has finished' event handler:
301 r1_bio->bios[mirror] = NULL;
303 md_error(r1_bio->mddev, conf->mirrors[mirror].rdev);
304 /* an I/O failed, we can't clear the bitmap */
305 set_bit(R1BIO_Degraded, &r1_bio->state);
308 * Set R1BIO_Uptodate in our master bio, so that
309 * we will return a good error code for to the higher
310 * levels even if IO on some other mirrored buffer fails.
312 * The 'master' represents the composite IO operation to
313 * user-side. So if something waits for IO, then it will
314 * wait for the 'master' bio.
316 set_bit(R1BIO_Uptodate, &r1_bio->state);
318 update_head_pos(mirror, r1_bio);
321 if (test_bit(WriteMostly, &conf->mirrors[mirror].rdev->flags))
322 atomic_dec(&r1_bio->behind_remaining);
324 /* In behind mode, we ACK the master bio once the I/O has safely
325 * reached all non-writemostly disks. Setting the Returned bit
326 * ensures that this gets done only once -- we don't ever want to
327 * return -EIO here, instead we'll wait */
329 if (atomic_read(&r1_bio->behind_remaining) >= (atomic_read(&r1_bio->remaining)-1) &&
330 test_bit(R1BIO_Uptodate, &r1_bio->state)) {
331 /* Maybe we can return now */
332 if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
333 struct bio *mbio = r1_bio->master_bio;
334 PRINTK(KERN_DEBUG "raid1: behind end write sectors %llu-%llu\n",
335 (unsigned long long) mbio->bi_sector,
336 (unsigned long long) mbio->bi_sector +
337 (mbio->bi_size >> 9) - 1);
338 bio_endio(mbio, mbio->bi_size, 0);
345 * Let's see if all mirrored write operations have finished
348 if (atomic_dec_and_test(&r1_bio->remaining)) {
349 if (test_bit(R1BIO_BarrierRetry, &r1_bio->state)) {
350 reschedule_retry(r1_bio);
351 /* Don't dec_pending yet, we want to hold
352 * the reference over the retry
356 if (test_bit(R1BIO_BehindIO, &r1_bio->state)) {
357 /* free extra copy of the data pages */
358 int i = bio->bi_vcnt;
360 __free_page(bio->bi_io_vec[i].bv_page);
362 /* clear the bitmap if all writes complete successfully */
363 bitmap_endwrite(r1_bio->mddev->bitmap, r1_bio->sector,
365 !test_bit(R1BIO_Degraded, &r1_bio->state),
367 md_write_end(r1_bio->mddev);
368 raid_end_bio_io(r1_bio);
371 if (r1_bio->bios[mirror]==NULL)
374 rdev_dec_pending(conf->mirrors[mirror].rdev, conf->mddev);
380 * This routine returns the disk from which the requested read should
381 * be done. There is a per-array 'next expected sequential IO' sector
382 * number - if this matches on the next IO then we use the last disk.
383 * There is also a per-disk 'last know head position' sector that is
384 * maintained from IRQ contexts, both the normal and the resync IO
385 * completion handlers update this position correctly. If there is no
386 * perfect sequential match then we pick the disk whose head is closest.
388 * If there are 2 mirrors in the same 2 devices, performance degrades
389 * because position is mirror, not device based.
391 * The rdev for the device selected will have nr_pending incremented.
393 static int read_balance(conf_t *conf, r1bio_t *r1_bio)
395 const unsigned long this_sector = r1_bio->sector;
396 int new_disk = conf->last_used, disk = new_disk;
398 const int sectors = r1_bio->sectors;
399 sector_t new_distance, current_distance;
404 * Check if we can balance. We can balance on the whole
405 * device if no resync is going on, or below the resync window.
406 * We take the first readable disk when above the resync window.
409 if (conf->mddev->recovery_cp < MaxSector &&
410 (this_sector + sectors >= conf->next_resync)) {
411 /* Choose the first operation device, for consistancy */
414 for (rdev = rcu_dereference(conf->mirrors[new_disk].rdev);
415 !rdev || !test_bit(In_sync, &rdev->flags)
416 || test_bit(WriteMostly, &rdev->flags);
417 rdev = rcu_dereference(conf->mirrors[++new_disk].rdev)) {
419 if (rdev && test_bit(In_sync, &rdev->flags))
420 wonly_disk = new_disk;
422 if (new_disk == conf->raid_disks - 1) {
423 new_disk = wonly_disk;
431 /* make sure the disk is operational */
432 for (rdev = rcu_dereference(conf->mirrors[new_disk].rdev);
433 !rdev || !test_bit(In_sync, &rdev->flags) ||
434 test_bit(WriteMostly, &rdev->flags);
435 rdev = rcu_dereference(conf->mirrors[new_disk].rdev)) {
437 if (rdev && test_bit(In_sync, &rdev->flags))
438 wonly_disk = new_disk;
441 new_disk = conf->raid_disks;
443 if (new_disk == disk) {
444 new_disk = wonly_disk;
453 /* now disk == new_disk == starting point for search */
456 * Don't change to another disk for sequential reads:
458 if (conf->next_seq_sect == this_sector)
460 if (this_sector == conf->mirrors[new_disk].head_position)
463 current_distance = abs(this_sector - conf->mirrors[disk].head_position);
465 /* Find the disk whose head is closest */
469 disk = conf->raid_disks;
472 rdev = rcu_dereference(conf->mirrors[disk].rdev);
475 !test_bit(In_sync, &rdev->flags) ||
476 test_bit(WriteMostly, &rdev->flags))
479 if (!atomic_read(&rdev->nr_pending)) {
483 new_distance = abs(this_sector - conf->mirrors[disk].head_position);
484 if (new_distance < current_distance) {
485 current_distance = new_distance;
488 } while (disk != conf->last_used);
494 rdev = rcu_dereference(conf->mirrors[new_disk].rdev);
497 atomic_inc(&rdev->nr_pending);
498 if (!test_bit(In_sync, &rdev->flags)) {
499 /* cannot risk returning a device that failed
500 * before we inc'ed nr_pending
502 atomic_dec(&rdev->nr_pending);
505 conf->next_seq_sect = this_sector + sectors;
506 conf->last_used = new_disk;
513 static void unplug_slaves(mddev_t *mddev)
515 conf_t *conf = mddev_to_conf(mddev);
519 for (i=0; i<mddev->raid_disks; i++) {
520 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
521 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
522 request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
524 atomic_inc(&rdev->nr_pending);
527 if (r_queue->unplug_fn)
528 r_queue->unplug_fn(r_queue);
530 rdev_dec_pending(rdev, mddev);
537 static void raid1_unplug(request_queue_t *q)
539 mddev_t *mddev = q->queuedata;
541 unplug_slaves(mddev);
542 md_wakeup_thread(mddev->thread);
545 static int raid1_issue_flush(request_queue_t *q, struct gendisk *disk,
546 sector_t *error_sector)
548 mddev_t *mddev = q->queuedata;
549 conf_t *conf = mddev_to_conf(mddev);
553 for (i=0; i<mddev->raid_disks && ret == 0; i++) {
554 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
555 if (rdev && !test_bit(Faulty, &rdev->flags)) {
556 struct block_device *bdev = rdev->bdev;
557 request_queue_t *r_queue = bdev_get_queue(bdev);
559 if (!r_queue->issue_flush_fn)
562 atomic_inc(&rdev->nr_pending);
564 ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
566 rdev_dec_pending(rdev, mddev);
576 * Sometimes we need to suspend IO while we do something else,
577 * either some resync/recovery, or reconfigure the array.
578 * To do this we raise a 'barrier'.
579 * The 'barrier' is a counter that can be raised multiple times
580 * to count how many activities are happening which preclude
582 * We can only raise the barrier if there is no pending IO.
583 * i.e. if nr_pending == 0.
584 * We choose only to raise the barrier if no-one is waiting for the
585 * barrier to go down. This means that as soon as an IO request
586 * is ready, no other operations which require a barrier will start
587 * until the IO request has had a chance.
589 * So: regular IO calls 'wait_barrier'. When that returns there
590 * is no backgroup IO happening, It must arrange to call
591 * allow_barrier when it has finished its IO.
592 * backgroup IO calls must call raise_barrier. Once that returns
593 * there is no normal IO happeing. It must arrange to call
594 * lower_barrier when the particular background IO completes.
596 #define RESYNC_DEPTH 32
598 static void raise_barrier(conf_t *conf)
600 spin_lock_irq(&conf->resync_lock);
602 /* Wait until no block IO is waiting */
603 wait_event_lock_irq(conf->wait_barrier, !conf->nr_waiting,
605 raid1_unplug(conf->mddev->queue));
607 /* block any new IO from starting */
610 /* No wait for all pending IO to complete */
611 wait_event_lock_irq(conf->wait_barrier,
612 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
614 raid1_unplug(conf->mddev->queue));
616 spin_unlock_irq(&conf->resync_lock);
619 static void lower_barrier(conf_t *conf)
622 spin_lock_irqsave(&conf->resync_lock, flags);
624 spin_unlock_irqrestore(&conf->resync_lock, flags);
625 wake_up(&conf->wait_barrier);
628 static void wait_barrier(conf_t *conf)
630 spin_lock_irq(&conf->resync_lock);
633 wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
635 raid1_unplug(conf->mddev->queue));
639 spin_unlock_irq(&conf->resync_lock);
642 static void allow_barrier(conf_t *conf)
645 spin_lock_irqsave(&conf->resync_lock, flags);
647 spin_unlock_irqrestore(&conf->resync_lock, flags);
648 wake_up(&conf->wait_barrier);
651 static void freeze_array(conf_t *conf)
653 /* stop syncio and normal IO and wait for everything to
655 * We increment barrier and nr_waiting, and then
656 * wait until barrier+nr_pending match nr_queued+2
658 spin_lock_irq(&conf->resync_lock);
661 wait_event_lock_irq(conf->wait_barrier,
662 conf->barrier+conf->nr_pending == conf->nr_queued+2,
664 raid1_unplug(conf->mddev->queue));
665 spin_unlock_irq(&conf->resync_lock);
667 static void unfreeze_array(conf_t *conf)
669 /* reverse the effect of the freeze */
670 spin_lock_irq(&conf->resync_lock);
673 wake_up(&conf->wait_barrier);
674 spin_unlock_irq(&conf->resync_lock);
678 /* duplicate the data pages for behind I/O */
679 static struct page **alloc_behind_pages(struct bio *bio)
682 struct bio_vec *bvec;
683 struct page **pages = kmalloc(bio->bi_vcnt * sizeof(struct page *),
685 if (unlikely(!pages))
688 memset(pages, 0, bio->bi_vcnt * sizeof(struct page *));
690 bio_for_each_segment(bvec, bio, i) {
691 pages[i] = alloc_page(GFP_NOIO);
692 if (unlikely(!pages[i]))
694 memcpy(kmap(pages[i]) + bvec->bv_offset,
695 kmap(bvec->bv_page) + bvec->bv_offset, bvec->bv_len);
697 kunmap(bvec->bv_page);
704 for (i = 0; i < bio->bi_vcnt && pages[i]; i++)
705 __free_page(pages[i]);
707 PRINTK("%dB behind alloc failed, doing sync I/O\n", bio->bi_size);
711 static int make_request(request_queue_t *q, struct bio * bio)
713 mddev_t *mddev = q->queuedata;
714 conf_t *conf = mddev_to_conf(mddev);
715 mirror_info_t *mirror;
717 struct bio *read_bio;
718 int i, targets = 0, disks;
720 struct bitmap *bitmap = mddev->bitmap;
723 struct page **behind_pages = NULL;
724 const int rw = bio_data_dir(bio);
727 if (unlikely(!mddev->barriers_work && bio_barrier(bio))) {
728 bio_endio(bio, bio->bi_size, -EOPNOTSUPP);
733 * Register the new request and wait if the reconstruction
734 * thread has put up a bar for new requests.
735 * Continue immediately if no resync is active currently.
737 md_write_start(mddev, bio); /* wait on superblock update early */
741 disk_stat_inc(mddev->gendisk, ios[rw]);
742 disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bio));
745 * make_request() can abort the operation when READA is being
746 * used and no empty request is available.
749 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
751 r1_bio->master_bio = bio;
752 r1_bio->sectors = bio->bi_size >> 9;
754 r1_bio->mddev = mddev;
755 r1_bio->sector = bio->bi_sector;
759 * read balancing logic:
761 int rdisk = read_balance(conf, r1_bio);
764 /* couldn't find anywhere to read from */
765 raid_end_bio_io(r1_bio);
768 mirror = conf->mirrors + rdisk;
770 r1_bio->read_disk = rdisk;
772 read_bio = bio_clone(bio, GFP_NOIO);
774 r1_bio->bios[rdisk] = read_bio;
776 read_bio->bi_sector = r1_bio->sector + mirror->rdev->data_offset;
777 read_bio->bi_bdev = mirror->rdev->bdev;
778 read_bio->bi_end_io = raid1_end_read_request;
779 read_bio->bi_rw = READ;
780 read_bio->bi_private = r1_bio;
782 generic_make_request(read_bio);
789 /* first select target devices under spinlock and
790 * inc refcount on their rdev. Record them by setting
793 disks = conf->raid_disks;
795 { static int first=1;
796 if (first) printk("First Write sector %llu disks %d\n",
797 (unsigned long long)r1_bio->sector, disks);
802 for (i = 0; i < disks; i++) {
803 if ((rdev=rcu_dereference(conf->mirrors[i].rdev)) != NULL &&
804 !test_bit(Faulty, &rdev->flags)) {
805 atomic_inc(&rdev->nr_pending);
806 if (test_bit(Faulty, &rdev->flags)) {
807 atomic_dec(&rdev->nr_pending);
808 r1_bio->bios[i] = NULL;
810 r1_bio->bios[i] = bio;
813 r1_bio->bios[i] = NULL;
817 BUG_ON(targets == 0); /* we never fail the last device */
819 if (targets < conf->raid_disks) {
820 /* array is degraded, we will not clear the bitmap
821 * on I/O completion (see raid1_end_write_request) */
822 set_bit(R1BIO_Degraded, &r1_bio->state);
825 /* do behind I/O ? */
827 atomic_read(&bitmap->behind_writes) < bitmap->max_write_behind &&
828 (behind_pages = alloc_behind_pages(bio)) != NULL)
829 set_bit(R1BIO_BehindIO, &r1_bio->state);
831 atomic_set(&r1_bio->remaining, 0);
832 atomic_set(&r1_bio->behind_remaining, 0);
834 do_barriers = bio->bi_rw & BIO_RW_BARRIER;
836 set_bit(R1BIO_Barrier, &r1_bio->state);
839 for (i = 0; i < disks; i++) {
841 if (!r1_bio->bios[i])
844 mbio = bio_clone(bio, GFP_NOIO);
845 r1_bio->bios[i] = mbio;
847 mbio->bi_sector = r1_bio->sector + conf->mirrors[i].rdev->data_offset;
848 mbio->bi_bdev = conf->mirrors[i].rdev->bdev;
849 mbio->bi_end_io = raid1_end_write_request;
850 mbio->bi_rw = WRITE | do_barriers;
851 mbio->bi_private = r1_bio;
854 struct bio_vec *bvec;
857 /* Yes, I really want the '__' version so that
858 * we clear any unused pointer in the io_vec, rather
859 * than leave them unchanged. This is important
860 * because when we come to free the pages, we won't
861 * know the originial bi_idx, so we just free
864 __bio_for_each_segment(bvec, mbio, j, 0)
865 bvec->bv_page = behind_pages[j];
866 if (test_bit(WriteMostly, &conf->mirrors[i].rdev->flags))
867 atomic_inc(&r1_bio->behind_remaining);
870 atomic_inc(&r1_bio->remaining);
872 bio_list_add(&bl, mbio);
874 kfree(behind_pages); /* the behind pages are attached to the bios now */
876 bitmap_startwrite(bitmap, bio->bi_sector, r1_bio->sectors,
877 test_bit(R1BIO_BehindIO, &r1_bio->state));
878 spin_lock_irqsave(&conf->device_lock, flags);
879 bio_list_merge(&conf->pending_bio_list, &bl);
882 blk_plug_device(mddev->queue);
883 spin_unlock_irqrestore(&conf->device_lock, flags);
886 while ((bio = bio_list_pop(&bl)) != NULL)
887 generic_make_request(bio);
893 static void status(struct seq_file *seq, mddev_t *mddev)
895 conf_t *conf = mddev_to_conf(mddev);
898 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
899 conf->working_disks);
900 for (i = 0; i < conf->raid_disks; i++)
901 seq_printf(seq, "%s",
902 conf->mirrors[i].rdev &&
903 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
904 seq_printf(seq, "]");
908 static void error(mddev_t *mddev, mdk_rdev_t *rdev)
910 char b[BDEVNAME_SIZE];
911 conf_t *conf = mddev_to_conf(mddev);
914 * If it is not operational, then we have already marked it as dead
915 * else if it is the last working disks, ignore the error, let the
916 * next level up know.
917 * else mark the drive as failed
919 if (test_bit(In_sync, &rdev->flags)
920 && conf->working_disks == 1)
922 * Don't fail the drive, act as though we were just a
923 * normal single drive
926 if (test_bit(In_sync, &rdev->flags)) {
928 conf->working_disks--;
930 * if recovery is running, make sure it aborts.
932 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
934 clear_bit(In_sync, &rdev->flags);
935 set_bit(Faulty, &rdev->flags);
937 printk(KERN_ALERT "raid1: Disk failure on %s, disabling device. \n"
938 " Operation continuing on %d devices\n",
939 bdevname(rdev->bdev,b), conf->working_disks);
942 static void print_conf(conf_t *conf)
947 printk("RAID1 conf printout:\n");
952 printk(" --- wd:%d rd:%d\n", conf->working_disks,
955 for (i = 0; i < conf->raid_disks; i++) {
956 char b[BDEVNAME_SIZE];
957 tmp = conf->mirrors + i;
959 printk(" disk %d, wo:%d, o:%d, dev:%s\n",
960 i, !test_bit(In_sync, &tmp->rdev->flags), !test_bit(Faulty, &tmp->rdev->flags),
961 bdevname(tmp->rdev->bdev,b));
965 static void close_sync(conf_t *conf)
970 mempool_destroy(conf->r1buf_pool);
971 conf->r1buf_pool = NULL;
974 static int raid1_spare_active(mddev_t *mddev)
977 conf_t *conf = mddev->private;
981 * Find all failed disks within the RAID1 configuration
982 * and mark them readable
984 for (i = 0; i < conf->raid_disks; i++) {
985 tmp = conf->mirrors + i;
987 && !test_bit(Faulty, &tmp->rdev->flags)
988 && !test_bit(In_sync, &tmp->rdev->flags)) {
989 conf->working_disks++;
991 set_bit(In_sync, &tmp->rdev->flags);
1000 static int raid1_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
1002 conf_t *conf = mddev->private;
1007 for (mirror=0; mirror < mddev->raid_disks; mirror++)
1008 if ( !(p=conf->mirrors+mirror)->rdev) {
1010 blk_queue_stack_limits(mddev->queue,
1011 rdev->bdev->bd_disk->queue);
1012 /* as we don't honour merge_bvec_fn, we must never risk
1013 * violating it, so limit ->max_sector to one PAGE, as
1014 * a one page request is never in violation.
1016 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
1017 mddev->queue->max_sectors > (PAGE_SIZE>>9))
1018 blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
1020 p->head_position = 0;
1021 rdev->raid_disk = mirror;
1023 /* As all devices are equivalent, we don't need a full recovery
1024 * if this was recently any drive of the array
1026 if (rdev->saved_raid_disk < 0)
1028 rcu_assign_pointer(p->rdev, rdev);
1036 static int raid1_remove_disk(mddev_t *mddev, int number)
1038 conf_t *conf = mddev->private;
1041 mirror_info_t *p = conf->mirrors+ number;
1046 if (test_bit(In_sync, &rdev->flags) ||
1047 atomic_read(&rdev->nr_pending)) {
1053 if (atomic_read(&rdev->nr_pending)) {
1054 /* lost the race, try later */
1066 static int end_sync_read(struct bio *bio, unsigned int bytes_done, int error)
1068 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1069 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
1070 conf_t *conf = mddev_to_conf(r1_bio->mddev);
1075 if (r1_bio->bios[r1_bio->read_disk] != bio)
1077 update_head_pos(r1_bio->read_disk, r1_bio);
1079 * we have read a block, now it needs to be re-written,
1080 * or re-read if the read failed.
1081 * We don't do much here, just schedule handling by raid1d
1084 md_error(r1_bio->mddev,
1085 conf->mirrors[r1_bio->read_disk].rdev);
1087 set_bit(R1BIO_Uptodate, &r1_bio->state);
1088 rdev_dec_pending(conf->mirrors[r1_bio->read_disk].rdev, conf->mddev);
1089 reschedule_retry(r1_bio);
1093 static int end_sync_write(struct bio *bio, unsigned int bytes_done, int error)
1095 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1096 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
1097 mddev_t *mddev = r1_bio->mddev;
1098 conf_t *conf = mddev_to_conf(mddev);
1105 for (i = 0; i < conf->raid_disks; i++)
1106 if (r1_bio->bios[i] == bio) {
1111 md_error(mddev, conf->mirrors[mirror].rdev);
1113 update_head_pos(mirror, r1_bio);
1115 if (atomic_dec_and_test(&r1_bio->remaining)) {
1116 md_done_sync(mddev, r1_bio->sectors, uptodate);
1119 rdev_dec_pending(conf->mirrors[mirror].rdev, mddev);
1123 static void sync_request_write(mddev_t *mddev, r1bio_t *r1_bio)
1125 conf_t *conf = mddev_to_conf(mddev);
1127 int disks = conf->raid_disks;
1128 struct bio *bio, *wbio;
1130 bio = r1_bio->bios[r1_bio->read_disk];
1133 if (r1_bio->sector == 0) printk("First sync write startss\n");
1138 if (!test_bit(R1BIO_Uptodate, &r1_bio->state)) {
1140 * There is no point trying a read-for-reconstruct as
1141 * reconstruct is about to be aborted
1143 char b[BDEVNAME_SIZE];
1144 printk(KERN_ALERT "raid1: %s: unrecoverable I/O read error"
1145 " for block %llu\n",
1146 bdevname(bio->bi_bdev,b),
1147 (unsigned long long)r1_bio->sector);
1148 md_done_sync(mddev, r1_bio->sectors, 0);
1153 atomic_set(&r1_bio->remaining, 1);
1154 for (i = 0; i < disks ; i++) {
1155 wbio = r1_bio->bios[i];
1156 if (wbio->bi_end_io != end_sync_write)
1159 atomic_inc(&conf->mirrors[i].rdev->nr_pending);
1160 atomic_inc(&r1_bio->remaining);
1161 md_sync_acct(conf->mirrors[i].rdev->bdev, wbio->bi_size >> 9);
1163 generic_make_request(wbio);
1166 if (atomic_dec_and_test(&r1_bio->remaining)) {
1167 /* if we're here, all write(s) have completed, so clean up */
1168 md_done_sync(mddev, r1_bio->sectors, 1);
1174 * This is a kernel thread which:
1176 * 1. Retries failed read operations on working mirrors.
1177 * 2. Updates the raid superblock when problems encounter.
1178 * 3. Performs writes following reads for array syncronising.
1181 static void raid1d(mddev_t *mddev)
1185 unsigned long flags;
1186 conf_t *conf = mddev_to_conf(mddev);
1187 struct list_head *head = &conf->retry_list;
1191 md_check_recovery(mddev);
1194 char b[BDEVNAME_SIZE];
1195 spin_lock_irqsave(&conf->device_lock, flags);
1197 if (conf->pending_bio_list.head) {
1198 bio = bio_list_get(&conf->pending_bio_list);
1199 blk_remove_plug(mddev->queue);
1200 spin_unlock_irqrestore(&conf->device_lock, flags);
1201 /* flush any pending bitmap writes to disk before proceeding w/ I/O */
1202 if (bitmap_unplug(mddev->bitmap) != 0)
1203 printk("%s: bitmap file write failed!\n", mdname(mddev));
1205 while (bio) { /* submit pending writes */
1206 struct bio *next = bio->bi_next;
1207 bio->bi_next = NULL;
1208 generic_make_request(bio);
1216 if (list_empty(head))
1218 r1_bio = list_entry(head->prev, r1bio_t, retry_list);
1219 list_del(head->prev);
1221 spin_unlock_irqrestore(&conf->device_lock, flags);
1223 mddev = r1_bio->mddev;
1224 conf = mddev_to_conf(mddev);
1225 if (test_bit(R1BIO_IsSync, &r1_bio->state)) {
1226 sync_request_write(mddev, r1_bio);
1228 } else if (test_bit(R1BIO_BarrierRetry, &r1_bio->state)) {
1229 /* some requests in the r1bio were BIO_RW_BARRIER
1230 * requests which failed with -ENOTSUPP. Hohumm..
1231 * Better resubmit without the barrier.
1232 * We know which devices to resubmit for, because
1233 * all others have had their bios[] entry cleared.
1236 clear_bit(R1BIO_BarrierRetry, &r1_bio->state);
1237 clear_bit(R1BIO_Barrier, &r1_bio->state);
1238 for (i=0; i < conf->raid_disks; i++)
1239 if (r1_bio->bios[i]) {
1240 struct bio_vec *bvec;
1243 bio = bio_clone(r1_bio->master_bio, GFP_NOIO);
1244 /* copy pages from the failed bio, as
1245 * this might be a write-behind device */
1246 __bio_for_each_segment(bvec, bio, j, 0)
1247 bvec->bv_page = bio_iovec_idx(r1_bio->bios[i], j)->bv_page;
1248 bio_put(r1_bio->bios[i]);
1249 bio->bi_sector = r1_bio->sector +
1250 conf->mirrors[i].rdev->data_offset;
1251 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
1252 bio->bi_end_io = raid1_end_write_request;
1254 bio->bi_private = r1_bio;
1255 r1_bio->bios[i] = bio;
1256 generic_make_request(bio);
1261 /* we got a read error. Maybe the drive is bad. Maybe just
1262 * the block and we can fix it.
1263 * We freeze all other IO, and try reading the block from
1264 * other devices. When we find one, we re-write
1265 * and check it that fixes the read error.
1266 * This is all done synchronously while the array is
1269 sector_t sect = r1_bio->sector;
1270 int sectors = r1_bio->sectors;
1274 int d = r1_bio->read_disk;
1277 if (s > (PAGE_SIZE>>9))
1281 rdev = conf->mirrors[d].rdev;
1283 test_bit(In_sync, &rdev->flags) &&
1284 sync_page_io(rdev->bdev,
1285 sect + rdev->data_offset,
1287 conf->tmppage, READ))
1291 if (d == conf->raid_disks)
1294 } while (!success && d != r1_bio->read_disk);
1297 /* write it back and re-read */
1298 while (d != r1_bio->read_disk) {
1300 d = conf->raid_disks;
1302 rdev = conf->mirrors[d].rdev;
1304 test_bit(In_sync, &rdev->flags)) {
1305 if (sync_page_io(rdev->bdev,
1306 sect + rdev->data_offset,
1307 s<<9, conf->tmppage, WRITE) == 0 ||
1308 sync_page_io(rdev->bdev,
1309 sect + rdev->data_offset,
1310 s<<9, conf->tmppage, READ) == 0) {
1311 /* Well, this device is dead */
1312 md_error(mddev, rdev);
1317 /* Cannot read from anywhere -- bye bye array */
1318 md_error(mddev, conf->mirrors[r1_bio->read_disk].rdev);
1326 unfreeze_array(conf);
1328 bio = r1_bio->bios[r1_bio->read_disk];
1329 if ((disk=read_balance(conf, r1_bio)) == -1) {
1330 printk(KERN_ALERT "raid1: %s: unrecoverable I/O"
1331 " read error for block %llu\n",
1332 bdevname(bio->bi_bdev,b),
1333 (unsigned long long)r1_bio->sector);
1334 raid_end_bio_io(r1_bio);
1336 r1_bio->bios[r1_bio->read_disk] = NULL;
1337 r1_bio->read_disk = disk;
1339 bio = bio_clone(r1_bio->master_bio, GFP_NOIO);
1340 r1_bio->bios[r1_bio->read_disk] = bio;
1341 rdev = conf->mirrors[disk].rdev;
1342 if (printk_ratelimit())
1343 printk(KERN_ERR "raid1: %s: redirecting sector %llu to"
1344 " another mirror\n",
1345 bdevname(rdev->bdev,b),
1346 (unsigned long long)r1_bio->sector);
1347 bio->bi_sector = r1_bio->sector + rdev->data_offset;
1348 bio->bi_bdev = rdev->bdev;
1349 bio->bi_end_io = raid1_end_read_request;
1351 bio->bi_private = r1_bio;
1353 generic_make_request(bio);
1357 spin_unlock_irqrestore(&conf->device_lock, flags);
1359 unplug_slaves(mddev);
1363 static int init_resync(conf_t *conf)
1367 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
1368 if (conf->r1buf_pool)
1370 conf->r1buf_pool = mempool_create(buffs, r1buf_pool_alloc, r1buf_pool_free,
1372 if (!conf->r1buf_pool)
1374 conf->next_resync = 0;
1379 * perform a "sync" on one "block"
1381 * We need to make sure that no normal I/O request - particularly write
1382 * requests - conflict with active sync requests.
1384 * This is achieved by tracking pending requests and a 'barrier' concept
1385 * that can be installed to exclude normal IO requests.
1388 static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
1390 conf_t *conf = mddev_to_conf(mddev);
1391 mirror_info_t *mirror;
1394 sector_t max_sector, nr_sectors;
1398 int write_targets = 0;
1400 int still_degraded = 0;
1402 if (!conf->r1buf_pool)
1405 printk("sync start - bitmap %p\n", mddev->bitmap);
1407 if (init_resync(conf))
1411 max_sector = mddev->size << 1;
1412 if (sector_nr >= max_sector) {
1413 /* If we aborted, we need to abort the
1414 * sync on the 'current' bitmap chunk (there will
1415 * only be one in raid1 resync.
1416 * We can find the current addess in mddev->curr_resync
1418 if (mddev->curr_resync < max_sector) /* aborted */
1419 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
1421 else /* completed sync */
1424 bitmap_close_sync(mddev->bitmap);
1429 /* before building a request, check if we can skip these blocks..
1430 * This call the bitmap_start_sync doesn't actually record anything
1432 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
1433 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
1434 /* We can skip this block, and probably several more */
1439 * If there is non-resync activity waiting for a turn,
1440 * and resync is going fast enough,
1441 * then let it though before starting on this new sync request.
1443 if (!go_faster && conf->nr_waiting)
1444 msleep_interruptible(1000);
1446 raise_barrier(conf);
1448 conf->next_resync = sector_nr;
1451 * If reconstructing, and >1 working disc,
1452 * could dedicate one to rebuild and others to
1453 * service read requests ..
1455 disk = conf->last_used;
1456 /* make sure disk is operational */
1458 while (conf->mirrors[disk].rdev == NULL ||
1459 !test_bit(In_sync, &conf->mirrors[disk].rdev->flags) ||
1460 test_bit(WriteMostly, &conf->mirrors[disk].rdev->flags)
1462 if (conf->mirrors[disk].rdev &&
1463 test_bit(In_sync, &conf->mirrors[disk].rdev->flags))
1466 disk = conf->raid_disks;
1468 if (disk == conf->last_used) {
1473 conf->last_used = disk;
1474 atomic_inc(&conf->mirrors[disk].rdev->nr_pending);
1477 mirror = conf->mirrors + disk;
1479 r1_bio = mempool_alloc(conf->r1buf_pool, GFP_NOIO);
1481 r1_bio->mddev = mddev;
1482 r1_bio->sector = sector_nr;
1484 set_bit(R1BIO_IsSync, &r1_bio->state);
1485 r1_bio->read_disk = disk;
1487 for (i=0; i < conf->raid_disks; i++) {
1488 bio = r1_bio->bios[i];
1490 /* take from bio_init */
1491 bio->bi_next = NULL;
1492 bio->bi_flags |= 1 << BIO_UPTODATE;
1496 bio->bi_phys_segments = 0;
1497 bio->bi_hw_segments = 0;
1499 bio->bi_end_io = NULL;
1500 bio->bi_private = NULL;
1504 bio->bi_end_io = end_sync_read;
1505 } else if (conf->mirrors[i].rdev == NULL ||
1506 test_bit(Faulty, &conf->mirrors[i].rdev->flags)) {
1509 } else if (!test_bit(In_sync, &conf->mirrors[i].rdev->flags) ||
1510 sector_nr + RESYNC_SECTORS > mddev->recovery_cp ||
1511 test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
1513 bio->bi_end_io = end_sync_write;
1516 /* no need to read or write here */
1518 bio->bi_sector = sector_nr + conf->mirrors[i].rdev->data_offset;
1519 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
1520 bio->bi_private = r1_bio;
1523 if (write_targets == 0) {
1524 /* There is nowhere to write, so all non-sync
1525 * drives must be failed - so we are finished
1527 sector_t rv = max_sector - sector_nr;
1530 rdev_dec_pending(conf->mirrors[disk].rdev, mddev);
1538 int len = PAGE_SIZE;
1539 if (sector_nr + (len>>9) > max_sector)
1540 len = (max_sector - sector_nr) << 9;
1543 if (sync_blocks == 0) {
1544 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
1545 &sync_blocks, still_degraded) &&
1547 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
1549 if (sync_blocks < (PAGE_SIZE>>9))
1551 if (len > (sync_blocks<<9))
1552 len = sync_blocks<<9;
1555 for (i=0 ; i < conf->raid_disks; i++) {
1556 bio = r1_bio->bios[i];
1557 if (bio->bi_end_io) {
1558 page = r1_bio->bios[0]->bi_io_vec[bio->bi_vcnt].bv_page;
1559 if (bio_add_page(bio, page, len, 0) == 0) {
1561 r1_bio->bios[0]->bi_io_vec[bio->bi_vcnt].bv_page = page;
1564 bio = r1_bio->bios[i];
1565 if (bio->bi_end_io==NULL)
1567 /* remove last page from this bio */
1569 bio->bi_size -= len;
1570 bio->bi_flags &= ~(1<< BIO_SEG_VALID);
1576 nr_sectors += len>>9;
1577 sector_nr += len>>9;
1578 sync_blocks -= (len>>9);
1579 } while (r1_bio->bios[disk]->bi_vcnt < RESYNC_PAGES);
1581 bio = r1_bio->bios[disk];
1582 r1_bio->sectors = nr_sectors;
1584 md_sync_acct(mirror->rdev->bdev, nr_sectors);
1586 generic_make_request(bio);
1591 static int run(mddev_t *mddev)
1595 mirror_info_t *disk;
1597 struct list_head *tmp;
1599 if (mddev->level != 1) {
1600 printk("raid1: %s: raid level not set to mirroring (%d)\n",
1601 mdname(mddev), mddev->level);
1605 * copy the already verified devices into our private RAID1
1606 * bookkeeping area. [whatever we allocate in run(),
1607 * should be freed in stop()]
1609 conf = kmalloc(sizeof(conf_t), GFP_KERNEL);
1610 mddev->private = conf;
1614 memset(conf, 0, sizeof(*conf));
1615 conf->mirrors = kmalloc(sizeof(struct mirror_info)*mddev->raid_disks,
1620 memset(conf->mirrors, 0, sizeof(struct mirror_info)*mddev->raid_disks);
1622 conf->tmppage = alloc_page(GFP_KERNEL);
1626 conf->poolinfo = kmalloc(sizeof(*conf->poolinfo), GFP_KERNEL);
1627 if (!conf->poolinfo)
1629 conf->poolinfo->mddev = mddev;
1630 conf->poolinfo->raid_disks = mddev->raid_disks;
1631 conf->r1bio_pool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
1634 if (!conf->r1bio_pool)
1637 ITERATE_RDEV(mddev, rdev, tmp) {
1638 disk_idx = rdev->raid_disk;
1639 if (disk_idx >= mddev->raid_disks
1642 disk = conf->mirrors + disk_idx;
1646 blk_queue_stack_limits(mddev->queue,
1647 rdev->bdev->bd_disk->queue);
1648 /* as we don't honour merge_bvec_fn, we must never risk
1649 * violating it, so limit ->max_sector to one PAGE, as
1650 * a one page request is never in violation.
1652 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
1653 mddev->queue->max_sectors > (PAGE_SIZE>>9))
1654 blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
1656 disk->head_position = 0;
1657 if (!test_bit(Faulty, &rdev->flags) && test_bit(In_sync, &rdev->flags))
1658 conf->working_disks++;
1660 conf->raid_disks = mddev->raid_disks;
1661 conf->mddev = mddev;
1662 spin_lock_init(&conf->device_lock);
1663 INIT_LIST_HEAD(&conf->retry_list);
1664 if (conf->working_disks == 1)
1665 mddev->recovery_cp = MaxSector;
1667 spin_lock_init(&conf->resync_lock);
1668 init_waitqueue_head(&conf->wait_barrier);
1670 bio_list_init(&conf->pending_bio_list);
1671 bio_list_init(&conf->flushing_bio_list);
1673 if (!conf->working_disks) {
1674 printk(KERN_ERR "raid1: no operational mirrors for %s\n",
1679 mddev->degraded = 0;
1680 for (i = 0; i < conf->raid_disks; i++) {
1682 disk = conf->mirrors + i;
1685 disk->head_position = 0;
1691 * find the first working one and use it as a starting point
1692 * to read balancing.
1694 for (j = 0; j < conf->raid_disks &&
1695 (!conf->mirrors[j].rdev ||
1696 !test_bit(In_sync, &conf->mirrors[j].rdev->flags)) ; j++)
1698 conf->last_used = j;
1701 mddev->thread = md_register_thread(raid1d, mddev, "%s_raid1");
1702 if (!mddev->thread) {
1704 "raid1: couldn't allocate thread for %s\n",
1710 "raid1: raid set %s active with %d out of %d mirrors\n",
1711 mdname(mddev), mddev->raid_disks - mddev->degraded,
1714 * Ok, everything is just fine now
1716 mddev->array_size = mddev->size;
1718 mddev->queue->unplug_fn = raid1_unplug;
1719 mddev->queue->issue_flush_fn = raid1_issue_flush;
1724 printk(KERN_ERR "raid1: couldn't allocate memory for %s\n",
1729 if (conf->r1bio_pool)
1730 mempool_destroy(conf->r1bio_pool);
1731 kfree(conf->mirrors);
1732 __free_page(conf->tmppage);
1733 kfree(conf->poolinfo);
1735 mddev->private = NULL;
1741 static int stop(mddev_t *mddev)
1743 conf_t *conf = mddev_to_conf(mddev);
1744 struct bitmap *bitmap = mddev->bitmap;
1745 int behind_wait = 0;
1747 /* wait for behind writes to complete */
1748 while (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
1750 printk(KERN_INFO "raid1: behind writes in progress on device %s, waiting to stop (%d)\n", mdname(mddev), behind_wait);
1751 set_current_state(TASK_UNINTERRUPTIBLE);
1752 schedule_timeout(HZ); /* wait a second */
1753 /* need to kick something here to make sure I/O goes? */
1756 md_unregister_thread(mddev->thread);
1757 mddev->thread = NULL;
1758 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
1759 if (conf->r1bio_pool)
1760 mempool_destroy(conf->r1bio_pool);
1761 kfree(conf->mirrors);
1762 kfree(conf->poolinfo);
1764 mddev->private = NULL;
1768 static int raid1_resize(mddev_t *mddev, sector_t sectors)
1770 /* no resync is happening, and there is enough space
1771 * on all devices, so we can resize.
1772 * We need to make sure resync covers any new space.
1773 * If the array is shrinking we should possibly wait until
1774 * any io in the removed space completes, but it hardly seems
1777 mddev->array_size = sectors>>1;
1778 set_capacity(mddev->gendisk, mddev->array_size << 1);
1780 if (mddev->array_size > mddev->size && mddev->recovery_cp == MaxSector) {
1781 mddev->recovery_cp = mddev->size << 1;
1782 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1784 mddev->size = mddev->array_size;
1785 mddev->resync_max_sectors = sectors;
1789 static int raid1_reshape(mddev_t *mddev, int raid_disks)
1792 * 1/ resize the r1bio_pool
1793 * 2/ resize conf->mirrors
1795 * We allocate a new r1bio_pool if we can.
1796 * Then raise a device barrier and wait until all IO stops.
1797 * Then resize conf->mirrors and swap in the new r1bio pool.
1799 * At the same time, we "pack" the devices so that all the missing
1800 * devices have the higher raid_disk numbers.
1802 mempool_t *newpool, *oldpool;
1803 struct pool_info *newpoolinfo;
1804 mirror_info_t *newmirrors;
1805 conf_t *conf = mddev_to_conf(mddev);
1810 if (raid_disks < conf->raid_disks) {
1812 for (d= 0; d < conf->raid_disks; d++)
1813 if (conf->mirrors[d].rdev)
1815 if (cnt > raid_disks)
1819 newpoolinfo = kmalloc(sizeof(*newpoolinfo), GFP_KERNEL);
1822 newpoolinfo->mddev = mddev;
1823 newpoolinfo->raid_disks = raid_disks;
1825 newpool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
1826 r1bio_pool_free, newpoolinfo);
1831 newmirrors = kmalloc(sizeof(struct mirror_info) * raid_disks, GFP_KERNEL);
1834 mempool_destroy(newpool);
1837 memset(newmirrors, 0, sizeof(struct mirror_info)*raid_disks);
1839 raise_barrier(conf);
1841 /* ok, everything is stopped */
1842 oldpool = conf->r1bio_pool;
1843 conf->r1bio_pool = newpool;
1845 for (d=d2=0; d < conf->raid_disks; d++)
1846 if (conf->mirrors[d].rdev) {
1847 conf->mirrors[d].rdev->raid_disk = d2;
1848 newmirrors[d2++].rdev = conf->mirrors[d].rdev;
1850 kfree(conf->mirrors);
1851 conf->mirrors = newmirrors;
1852 kfree(conf->poolinfo);
1853 conf->poolinfo = newpoolinfo;
1855 mddev->degraded += (raid_disks - conf->raid_disks);
1856 conf->raid_disks = mddev->raid_disks = raid_disks;
1858 conf->last_used = 0; /* just make sure it is in-range */
1859 lower_barrier(conf);
1861 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1862 md_wakeup_thread(mddev->thread);
1864 mempool_destroy(oldpool);
1868 static void raid1_quiesce(mddev_t *mddev, int state)
1870 conf_t *conf = mddev_to_conf(mddev);
1874 raise_barrier(conf);
1877 lower_barrier(conf);
1883 static mdk_personality_t raid1_personality =
1886 .owner = THIS_MODULE,
1887 .make_request = make_request,
1891 .error_handler = error,
1892 .hot_add_disk = raid1_add_disk,
1893 .hot_remove_disk= raid1_remove_disk,
1894 .spare_active = raid1_spare_active,
1895 .sync_request = sync_request,
1896 .resize = raid1_resize,
1897 .reshape = raid1_reshape,
1898 .quiesce = raid1_quiesce,
1901 static int __init raid_init(void)
1903 return register_md_personality(RAID1, &raid1_personality);
1906 static void raid_exit(void)
1908 unregister_md_personality(RAID1);
1911 module_init(raid_init);
1912 module_exit(raid_exit);
1913 MODULE_LICENSE("GPL");
1914 MODULE_ALIAS("md-personality-3"); /* RAID1 */