2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
5 completely rewritten, based on the MD driver code from Marc Zyngier
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
20 Neil Brown <neilb@cse.unsw.edu.au>.
22 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
25 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 #include <linux/module.h>
36 #include <linux/config.h>
37 #include <linux/kthread.h>
38 #include <linux/linkage.h>
39 #include <linux/raid/md.h>
40 #include <linux/raid/bitmap.h>
41 #include <linux/sysctl.h>
42 #include <linux/devfs_fs_kernel.h>
43 #include <linux/buffer_head.h> /* for invalidate_bdev */
44 #include <linux/suspend.h>
46 #include <linux/init.h>
48 #include <linux/file.h>
51 #include <linux/kmod.h>
54 #include <asm/unaligned.h>
56 #define MAJOR_NR MD_MAJOR
59 /* 63 partitions with the alternate major number (mdp) */
60 #define MdpMinorShift 6
63 #define dprintk(x...) ((void)(DEBUG && printk(x)))
67 static void autostart_arrays (int part);
70 static mdk_personality_t *pers[MAX_PERSONALITY];
71 static DEFINE_SPINLOCK(pers_lock);
74 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
75 * is 1000 KB/sec, so the extra system load does not show up that much.
76 * Increase it if you want to have more _guaranteed_ speed. Note that
77 * the RAID driver will use the maximum available bandwith if the IO
78 * subsystem is idle. There is also an 'absolute maximum' reconstruction
79 * speed limit - in case reconstruction slows down your system despite
82 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
85 static int sysctl_speed_limit_min = 1000;
86 static int sysctl_speed_limit_max = 200000;
88 static struct ctl_table_header *raid_table_header;
90 static ctl_table raid_table[] = {
92 .ctl_name = DEV_RAID_SPEED_LIMIT_MIN,
93 .procname = "speed_limit_min",
94 .data = &sysctl_speed_limit_min,
95 .maxlen = sizeof(int),
97 .proc_handler = &proc_dointvec,
100 .ctl_name = DEV_RAID_SPEED_LIMIT_MAX,
101 .procname = "speed_limit_max",
102 .data = &sysctl_speed_limit_max,
103 .maxlen = sizeof(int),
105 .proc_handler = &proc_dointvec,
110 static ctl_table raid_dir_table[] = {
112 .ctl_name = DEV_RAID,
121 static ctl_table raid_root_table[] = {
127 .child = raid_dir_table,
132 static struct block_device_operations md_fops;
135 * Enables to iterate over all existing md arrays
136 * all_mddevs_lock protects this list.
138 static LIST_HEAD(all_mddevs);
139 static DEFINE_SPINLOCK(all_mddevs_lock);
143 * iterates through all used mddevs in the system.
144 * We take care to grab the all_mddevs_lock whenever navigating
145 * the list, and to always hold a refcount when unlocked.
146 * Any code which breaks out of this loop while own
147 * a reference to the current mddev and must mddev_put it.
149 #define ITERATE_MDDEV(mddev,tmp) \
151 for (({ spin_lock(&all_mddevs_lock); \
152 tmp = all_mddevs.next; \
154 ({ if (tmp != &all_mddevs) \
155 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
156 spin_unlock(&all_mddevs_lock); \
157 if (mddev) mddev_put(mddev); \
158 mddev = list_entry(tmp, mddev_t, all_mddevs); \
159 tmp != &all_mddevs;}); \
160 ({ spin_lock(&all_mddevs_lock); \
165 static int md_fail_request (request_queue_t *q, struct bio *bio)
167 bio_io_error(bio, bio->bi_size);
171 static inline mddev_t *mddev_get(mddev_t *mddev)
173 atomic_inc(&mddev->active);
177 static void mddev_put(mddev_t *mddev)
179 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
181 if (!mddev->raid_disks && list_empty(&mddev->disks)) {
182 list_del(&mddev->all_mddevs);
183 blk_put_queue(mddev->queue);
186 spin_unlock(&all_mddevs_lock);
189 static mddev_t * mddev_find(dev_t unit)
191 mddev_t *mddev, *new = NULL;
194 spin_lock(&all_mddevs_lock);
195 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
196 if (mddev->unit == unit) {
198 spin_unlock(&all_mddevs_lock);
204 list_add(&new->all_mddevs, &all_mddevs);
205 spin_unlock(&all_mddevs_lock);
208 spin_unlock(&all_mddevs_lock);
210 new = (mddev_t *) kmalloc(sizeof(*new), GFP_KERNEL);
214 memset(new, 0, sizeof(*new));
217 if (MAJOR(unit) == MD_MAJOR)
218 new->md_minor = MINOR(unit);
220 new->md_minor = MINOR(unit) >> MdpMinorShift;
222 init_MUTEX(&new->reconfig_sem);
223 INIT_LIST_HEAD(&new->disks);
224 INIT_LIST_HEAD(&new->all_mddevs);
225 init_timer(&new->safemode_timer);
226 atomic_set(&new->active, 1);
227 spin_lock_init(&new->write_lock);
228 init_waitqueue_head(&new->sb_wait);
230 new->queue = blk_alloc_queue(GFP_KERNEL);
236 blk_queue_make_request(new->queue, md_fail_request);
241 static inline int mddev_lock(mddev_t * mddev)
243 return down_interruptible(&mddev->reconfig_sem);
246 static inline void mddev_lock_uninterruptible(mddev_t * mddev)
248 down(&mddev->reconfig_sem);
251 static inline int mddev_trylock(mddev_t * mddev)
253 return down_trylock(&mddev->reconfig_sem);
256 static inline void mddev_unlock(mddev_t * mddev)
258 up(&mddev->reconfig_sem);
260 md_wakeup_thread(mddev->thread);
263 mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
266 struct list_head *tmp;
268 ITERATE_RDEV(mddev,rdev,tmp) {
269 if (rdev->desc_nr == nr)
275 static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
277 struct list_head *tmp;
280 ITERATE_RDEV(mddev,rdev,tmp) {
281 if (rdev->bdev->bd_dev == dev)
287 static inline sector_t calc_dev_sboffset(struct block_device *bdev)
289 sector_t size = bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
290 return MD_NEW_SIZE_BLOCKS(size);
293 static sector_t calc_dev_size(mdk_rdev_t *rdev, unsigned chunk_size)
297 size = rdev->sb_offset;
300 size &= ~((sector_t)chunk_size/1024 - 1);
304 static int alloc_disk_sb(mdk_rdev_t * rdev)
309 rdev->sb_page = alloc_page(GFP_KERNEL);
310 if (!rdev->sb_page) {
311 printk(KERN_ALERT "md: out of memory.\n");
318 static void free_disk_sb(mdk_rdev_t * rdev)
321 page_cache_release(rdev->sb_page);
323 rdev->sb_page = NULL;
330 static int super_written(struct bio *bio, unsigned int bytes_done, int error)
332 mdk_rdev_t *rdev = bio->bi_private;
336 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags))
337 md_error(rdev->mddev, rdev);
339 if (atomic_dec_and_test(&rdev->mddev->pending_writes))
340 wake_up(&rdev->mddev->sb_wait);
345 void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
346 sector_t sector, int size, struct page *page)
348 /* write first size bytes of page to sector of rdev
349 * Increment mddev->pending_writes before returning
350 * and decrement it on completion, waking up sb_wait
351 * if zero is reached.
352 * If an error occurred, call md_error
354 struct bio *bio = bio_alloc(GFP_NOIO, 1);
356 bio->bi_bdev = rdev->bdev;
357 bio->bi_sector = sector;
358 bio_add_page(bio, page, size, 0);
359 bio->bi_private = rdev;
360 bio->bi_end_io = super_written;
361 atomic_inc(&mddev->pending_writes);
362 submit_bio((1<<BIO_RW)|(1<<BIO_RW_SYNC), bio);
365 static int bi_complete(struct bio *bio, unsigned int bytes_done, int error)
370 complete((struct completion*)bio->bi_private);
374 int sync_page_io(struct block_device *bdev, sector_t sector, int size,
375 struct page *page, int rw)
377 struct bio *bio = bio_alloc(GFP_NOIO, 1);
378 struct completion event;
381 rw |= (1 << BIO_RW_SYNC);
384 bio->bi_sector = sector;
385 bio_add_page(bio, page, size, 0);
386 init_completion(&event);
387 bio->bi_private = &event;
388 bio->bi_end_io = bi_complete;
390 wait_for_completion(&event);
392 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
397 static int read_disk_sb(mdk_rdev_t * rdev, int size)
399 char b[BDEVNAME_SIZE];
400 if (!rdev->sb_page) {
408 if (!sync_page_io(rdev->bdev, rdev->sb_offset<<1, size, rdev->sb_page, READ))
414 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
415 bdevname(rdev->bdev,b));
419 static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
421 if ( (sb1->set_uuid0 == sb2->set_uuid0) &&
422 (sb1->set_uuid1 == sb2->set_uuid1) &&
423 (sb1->set_uuid2 == sb2->set_uuid2) &&
424 (sb1->set_uuid3 == sb2->set_uuid3))
432 static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
435 mdp_super_t *tmp1, *tmp2;
437 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
438 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
440 if (!tmp1 || !tmp2) {
442 printk(KERN_INFO "md.c: sb1 is not equal to sb2!\n");
450 * nr_disks is not constant
455 if (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4))
466 static unsigned int calc_sb_csum(mdp_super_t * sb)
468 unsigned int disk_csum, csum;
470 disk_csum = sb->sb_csum;
472 csum = csum_partial((void *)sb, MD_SB_BYTES, 0);
473 sb->sb_csum = disk_csum;
479 * Handle superblock details.
480 * We want to be able to handle multiple superblock formats
481 * so we have a common interface to them all, and an array of
482 * different handlers.
483 * We rely on user-space to write the initial superblock, and support
484 * reading and updating of superblocks.
485 * Interface methods are:
486 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
487 * loads and validates a superblock on dev.
488 * if refdev != NULL, compare superblocks on both devices
490 * 0 - dev has a superblock that is compatible with refdev
491 * 1 - dev has a superblock that is compatible and newer than refdev
492 * so dev should be used as the refdev in future
493 * -EINVAL superblock incompatible or invalid
494 * -othererror e.g. -EIO
496 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
497 * Verify that dev is acceptable into mddev.
498 * The first time, mddev->raid_disks will be 0, and data from
499 * dev should be merged in. Subsequent calls check that dev
500 * is new enough. Return 0 or -EINVAL
502 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
503 * Update the superblock for rdev with data in mddev
504 * This does not write to disc.
510 struct module *owner;
511 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version);
512 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
513 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
517 * load_super for 0.90.0
519 static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
521 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
527 * Calculate the position of the superblock,
528 * it's at the end of the disk.
530 * It also happens to be a multiple of 4Kb.
532 sb_offset = calc_dev_sboffset(rdev->bdev);
533 rdev->sb_offset = sb_offset;
535 ret = read_disk_sb(rdev, MD_SB_BYTES);
540 bdevname(rdev->bdev, b);
541 sb = (mdp_super_t*)page_address(rdev->sb_page);
543 if (sb->md_magic != MD_SB_MAGIC) {
544 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
549 if (sb->major_version != 0 ||
550 sb->minor_version != 90) {
551 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
552 sb->major_version, sb->minor_version,
557 if (sb->raid_disks <= 0)
560 if (csum_fold(calc_sb_csum(sb)) != csum_fold(sb->sb_csum)) {
561 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
566 rdev->preferred_minor = sb->md_minor;
567 rdev->data_offset = 0;
568 rdev->sb_size = MD_SB_BYTES;
570 if (sb->level == LEVEL_MULTIPATH)
573 rdev->desc_nr = sb->this_disk.number;
579 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
580 if (!uuid_equal(refsb, sb)) {
581 printk(KERN_WARNING "md: %s has different UUID to %s\n",
582 b, bdevname(refdev->bdev,b2));
585 if (!sb_equal(refsb, sb)) {
586 printk(KERN_WARNING "md: %s has same UUID"
587 " but different superblock to %s\n",
588 b, bdevname(refdev->bdev, b2));
592 ev2 = md_event(refsb);
598 rdev->size = calc_dev_size(rdev, sb->chunk_size);
605 * validate_super for 0.90.0
607 static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
610 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
612 rdev->raid_disk = -1;
614 if (mddev->raid_disks == 0) {
615 mddev->major_version = 0;
616 mddev->minor_version = sb->minor_version;
617 mddev->patch_version = sb->patch_version;
618 mddev->persistent = ! sb->not_persistent;
619 mddev->chunk_size = sb->chunk_size;
620 mddev->ctime = sb->ctime;
621 mddev->utime = sb->utime;
622 mddev->level = sb->level;
623 mddev->layout = sb->layout;
624 mddev->raid_disks = sb->raid_disks;
625 mddev->size = sb->size;
626 mddev->events = md_event(sb);
627 mddev->bitmap_offset = 0;
628 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
630 if (sb->state & (1<<MD_SB_CLEAN))
631 mddev->recovery_cp = MaxSector;
633 if (sb->events_hi == sb->cp_events_hi &&
634 sb->events_lo == sb->cp_events_lo) {
635 mddev->recovery_cp = sb->recovery_cp;
637 mddev->recovery_cp = 0;
640 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
641 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
642 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
643 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
645 mddev->max_disks = MD_SB_DISKS;
647 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
648 mddev->bitmap_file == NULL) {
649 if (mddev->level != 1 && mddev->level != 5 && mddev->level != 6) {
650 /* FIXME use a better test */
651 printk(KERN_WARNING "md: bitmaps only support for raid1\n");
654 mddev->bitmap_offset = mddev->default_bitmap_offset;
657 } else if (mddev->pers == NULL) {
658 /* Insist on good event counter while assembling */
659 __u64 ev1 = md_event(sb);
661 if (ev1 < mddev->events)
663 } else if (mddev->bitmap) {
664 /* if adding to array with a bitmap, then we can accept an
665 * older device ... but not too old.
667 __u64 ev1 = md_event(sb);
668 if (ev1 < mddev->bitmap->events_cleared)
670 } else /* just a hot-add of a new device, leave raid_disk at -1 */
673 if (mddev->level != LEVEL_MULTIPATH) {
676 desc = sb->disks + rdev->desc_nr;
678 if (desc->state & (1<<MD_DISK_FAULTY))
680 else if (desc->state & (1<<MD_DISK_SYNC) &&
681 desc->raid_disk < mddev->raid_disks) {
683 rdev->raid_disk = desc->raid_disk;
685 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
686 set_bit(WriteMostly, &rdev->flags);
687 } else /* MULTIPATH are always insync */
693 * sync_super for 0.90.0
695 static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
698 struct list_head *tmp;
700 int next_spare = mddev->raid_disks;
702 /* make rdev->sb match mddev data..
705 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
706 * 3/ any empty disks < next_spare become removed
708 * disks[0] gets initialised to REMOVED because
709 * we cannot be sure from other fields if it has
710 * been initialised or not.
713 int active=0, working=0,failed=0,spare=0,nr_disks=0;
715 sb = (mdp_super_t*)page_address(rdev->sb_page);
717 memset(sb, 0, sizeof(*sb));
719 sb->md_magic = MD_SB_MAGIC;
720 sb->major_version = mddev->major_version;
721 sb->minor_version = mddev->minor_version;
722 sb->patch_version = mddev->patch_version;
723 sb->gvalid_words = 0; /* ignored */
724 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
725 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
726 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
727 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
729 sb->ctime = mddev->ctime;
730 sb->level = mddev->level;
731 sb->size = mddev->size;
732 sb->raid_disks = mddev->raid_disks;
733 sb->md_minor = mddev->md_minor;
734 sb->not_persistent = !mddev->persistent;
735 sb->utime = mddev->utime;
737 sb->events_hi = (mddev->events>>32);
738 sb->events_lo = (u32)mddev->events;
742 sb->recovery_cp = mddev->recovery_cp;
743 sb->cp_events_hi = (mddev->events>>32);
744 sb->cp_events_lo = (u32)mddev->events;
745 if (mddev->recovery_cp == MaxSector)
746 sb->state = (1<< MD_SB_CLEAN);
750 sb->layout = mddev->layout;
751 sb->chunk_size = mddev->chunk_size;
753 if (mddev->bitmap && mddev->bitmap_file == NULL)
754 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
756 sb->disks[0].state = (1<<MD_DISK_REMOVED);
757 ITERATE_RDEV(mddev,rdev2,tmp) {
759 if (rdev2->raid_disk >= 0 && rdev2->in_sync && !rdev2->faulty)
760 rdev2->desc_nr = rdev2->raid_disk;
762 rdev2->desc_nr = next_spare++;
763 d = &sb->disks[rdev2->desc_nr];
765 d->number = rdev2->desc_nr;
766 d->major = MAJOR(rdev2->bdev->bd_dev);
767 d->minor = MINOR(rdev2->bdev->bd_dev);
768 if (rdev2->raid_disk >= 0 && rdev->in_sync && !rdev2->faulty)
769 d->raid_disk = rdev2->raid_disk;
771 d->raid_disk = rdev2->desc_nr; /* compatibility */
773 d->state = (1<<MD_DISK_FAULTY);
775 } else if (rdev2->in_sync) {
776 d->state = (1<<MD_DISK_ACTIVE);
777 d->state |= (1<<MD_DISK_SYNC);
785 if (test_bit(WriteMostly, &rdev2->flags))
786 d->state |= (1<<MD_DISK_WRITEMOSTLY);
789 /* now set the "removed" and "faulty" bits on any missing devices */
790 for (i=0 ; i < mddev->raid_disks ; i++) {
791 mdp_disk_t *d = &sb->disks[i];
792 if (d->state == 0 && d->number == 0) {
795 d->state = (1<<MD_DISK_REMOVED);
796 d->state |= (1<<MD_DISK_FAULTY);
800 sb->nr_disks = nr_disks;
801 sb->active_disks = active;
802 sb->working_disks = working;
803 sb->failed_disks = failed;
804 sb->spare_disks = spare;
806 sb->this_disk = sb->disks[rdev->desc_nr];
807 sb->sb_csum = calc_sb_csum(sb);
811 * version 1 superblock
814 static unsigned int calc_sb_1_csum(struct mdp_superblock_1 * sb)
816 unsigned int disk_csum, csum;
817 unsigned long long newcsum;
818 int size = 256 + le32_to_cpu(sb->max_dev)*2;
819 unsigned int *isuper = (unsigned int*)sb;
822 disk_csum = sb->sb_csum;
825 for (i=0; size>=4; size -= 4 )
826 newcsum += le32_to_cpu(*isuper++);
829 newcsum += le16_to_cpu(*(unsigned short*) isuper);
831 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
832 sb->sb_csum = disk_csum;
833 return cpu_to_le32(csum);
836 static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
838 struct mdp_superblock_1 *sb;
841 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
845 * Calculate the position of the superblock.
846 * It is always aligned to a 4K boundary and
847 * depeding on minor_version, it can be:
848 * 0: At least 8K, but less than 12K, from end of device
849 * 1: At start of device
850 * 2: 4K from start of device.
852 switch(minor_version) {
854 sb_offset = rdev->bdev->bd_inode->i_size >> 9;
856 sb_offset &= ~(sector_t)(4*2-1);
857 /* convert from sectors to K */
869 rdev->sb_offset = sb_offset;
871 /* superblock is rarely larger than 1K, but it can be larger,
872 * and it is safe to read 4k, so we do that
874 ret = read_disk_sb(rdev, 4096);
878 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
880 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
881 sb->major_version != cpu_to_le32(1) ||
882 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
883 le64_to_cpu(sb->super_offset) != (rdev->sb_offset<<1) ||
884 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
887 if (calc_sb_1_csum(sb) != sb->sb_csum) {
888 printk("md: invalid superblock checksum on %s\n",
889 bdevname(rdev->bdev,b));
892 if (le64_to_cpu(sb->data_size) < 10) {
893 printk("md: data_size too small on %s\n",
894 bdevname(rdev->bdev,b));
897 rdev->preferred_minor = 0xffff;
898 rdev->data_offset = le64_to_cpu(sb->data_offset);
900 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
901 bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
902 if (rdev->sb_size & bmask)
903 rdev-> sb_size = (rdev->sb_size | bmask)+1;
909 struct mdp_superblock_1 *refsb =
910 (struct mdp_superblock_1*)page_address(refdev->sb_page);
912 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
913 sb->level != refsb->level ||
914 sb->layout != refsb->layout ||
915 sb->chunksize != refsb->chunksize) {
916 printk(KERN_WARNING "md: %s has strangely different"
917 " superblock to %s\n",
918 bdevname(rdev->bdev,b),
919 bdevname(refdev->bdev,b2));
922 ev1 = le64_to_cpu(sb->events);
923 ev2 = le64_to_cpu(refsb->events);
929 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
931 rdev->size = rdev->sb_offset;
932 if (rdev->size < le64_to_cpu(sb->data_size)/2)
934 rdev->size = le64_to_cpu(sb->data_size)/2;
935 if (le32_to_cpu(sb->chunksize))
936 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
940 static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
942 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
944 rdev->raid_disk = -1;
946 if (mddev->raid_disks == 0) {
947 mddev->major_version = 1;
948 mddev->patch_version = 0;
949 mddev->persistent = 1;
950 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
951 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
952 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
953 mddev->level = le32_to_cpu(sb->level);
954 mddev->layout = le32_to_cpu(sb->layout);
955 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
956 mddev->size = le64_to_cpu(sb->size)/2;
957 mddev->events = le64_to_cpu(sb->events);
958 mddev->bitmap_offset = 0;
959 mddev->default_bitmap_offset = 0;
960 mddev->default_bitmap_offset = 1024;
962 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
963 memcpy(mddev->uuid, sb->set_uuid, 16);
965 mddev->max_disks = (4096-256)/2;
967 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
968 mddev->bitmap_file == NULL ) {
969 if (mddev->level != 1) {
970 printk(KERN_WARNING "md: bitmaps only supported for raid1\n");
973 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
975 } else if (mddev->pers == NULL) {
976 /* Insist of good event counter while assembling */
977 __u64 ev1 = le64_to_cpu(sb->events);
979 if (ev1 < mddev->events)
981 } else if (mddev->bitmap) {
982 /* If adding to array with a bitmap, then we can accept an
983 * older device, but not too old.
985 __u64 ev1 = le64_to_cpu(sb->events);
986 if (ev1 < mddev->bitmap->events_cleared)
988 } else /* just a hot-add of a new device, leave raid_disk at -1 */
991 if (mddev->level != LEVEL_MULTIPATH) {
993 rdev->desc_nr = le32_to_cpu(sb->dev_number);
994 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
996 case 0xffff: /* spare */
999 case 0xfffe: /* faulty */
1005 rdev->raid_disk = role;
1009 if (sb->devflags & WriteMostly1)
1010 set_bit(WriteMostly, &rdev->flags);
1011 } else /* MULTIPATH are always insync */
1017 static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1019 struct mdp_superblock_1 *sb;
1020 struct list_head *tmp;
1023 /* make rdev->sb match mddev and rdev data. */
1025 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1027 sb->feature_map = 0;
1029 memset(sb->pad1, 0, sizeof(sb->pad1));
1030 memset(sb->pad2, 0, sizeof(sb->pad2));
1031 memset(sb->pad3, 0, sizeof(sb->pad3));
1033 sb->utime = cpu_to_le64((__u64)mddev->utime);
1034 sb->events = cpu_to_le64(mddev->events);
1036 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1038 sb->resync_offset = cpu_to_le64(0);
1040 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1041 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
1042 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
1046 ITERATE_RDEV(mddev,rdev2,tmp)
1047 if (rdev2->desc_nr+1 > max_dev)
1048 max_dev = rdev2->desc_nr+1;
1050 sb->max_dev = cpu_to_le32(max_dev);
1051 for (i=0; i<max_dev;i++)
1052 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1054 ITERATE_RDEV(mddev,rdev2,tmp) {
1057 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1058 else if (rdev2->in_sync)
1059 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1061 sb->dev_roles[i] = cpu_to_le16(0xffff);
1064 sb->recovery_offset = cpu_to_le64(0); /* not supported yet */
1065 sb->sb_csum = calc_sb_1_csum(sb);
1069 static struct super_type super_types[] = {
1072 .owner = THIS_MODULE,
1073 .load_super = super_90_load,
1074 .validate_super = super_90_validate,
1075 .sync_super = super_90_sync,
1079 .owner = THIS_MODULE,
1080 .load_super = super_1_load,
1081 .validate_super = super_1_validate,
1082 .sync_super = super_1_sync,
1086 static mdk_rdev_t * match_dev_unit(mddev_t *mddev, mdk_rdev_t *dev)
1088 struct list_head *tmp;
1091 ITERATE_RDEV(mddev,rdev,tmp)
1092 if (rdev->bdev->bd_contains == dev->bdev->bd_contains)
1098 static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1100 struct list_head *tmp;
1103 ITERATE_RDEV(mddev1,rdev,tmp)
1104 if (match_dev_unit(mddev2, rdev))
1110 static LIST_HEAD(pending_raid_disks);
1112 static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1114 mdk_rdev_t *same_pdev;
1115 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1121 same_pdev = match_dev_unit(mddev, rdev);
1124 "%s: WARNING: %s appears to be on the same physical"
1125 " disk as %s. True\n protection against single-disk"
1126 " failure might be compromised.\n",
1127 mdname(mddev), bdevname(rdev->bdev,b),
1128 bdevname(same_pdev->bdev,b2));
1130 /* Verify rdev->desc_nr is unique.
1131 * If it is -1, assign a free number, else
1132 * check number is not in use
1134 if (rdev->desc_nr < 0) {
1136 if (mddev->pers) choice = mddev->raid_disks;
1137 while (find_rdev_nr(mddev, choice))
1139 rdev->desc_nr = choice;
1141 if (find_rdev_nr(mddev, rdev->desc_nr))
1145 list_add(&rdev->same_set, &mddev->disks);
1146 rdev->mddev = mddev;
1147 printk(KERN_INFO "md: bind<%s>\n", bdevname(rdev->bdev,b));
1151 static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1153 char b[BDEVNAME_SIZE];
1158 list_del_init(&rdev->same_set);
1159 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1164 * prevent the device from being mounted, repartitioned or
1165 * otherwise reused by a RAID array (or any other kernel
1166 * subsystem), by bd_claiming the device.
1168 static int lock_rdev(mdk_rdev_t *rdev, dev_t dev)
1171 struct block_device *bdev;
1172 char b[BDEVNAME_SIZE];
1174 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
1176 printk(KERN_ERR "md: could not open %s.\n",
1177 __bdevname(dev, b));
1178 return PTR_ERR(bdev);
1180 err = bd_claim(bdev, rdev);
1182 printk(KERN_ERR "md: could not bd_claim %s.\n",
1191 static void unlock_rdev(mdk_rdev_t *rdev)
1193 struct block_device *bdev = rdev->bdev;
1201 void md_autodetect_dev(dev_t dev);
1203 static void export_rdev(mdk_rdev_t * rdev)
1205 char b[BDEVNAME_SIZE];
1206 printk(KERN_INFO "md: export_rdev(%s)\n",
1207 bdevname(rdev->bdev,b));
1211 list_del_init(&rdev->same_set);
1213 md_autodetect_dev(rdev->bdev->bd_dev);
1219 static void kick_rdev_from_array(mdk_rdev_t * rdev)
1221 unbind_rdev_from_array(rdev);
1225 static void export_array(mddev_t *mddev)
1227 struct list_head *tmp;
1230 ITERATE_RDEV(mddev,rdev,tmp) {
1235 kick_rdev_from_array(rdev);
1237 if (!list_empty(&mddev->disks))
1239 mddev->raid_disks = 0;
1240 mddev->major_version = 0;
1243 static void print_desc(mdp_disk_t *desc)
1245 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1246 desc->major,desc->minor,desc->raid_disk,desc->state);
1249 static void print_sb(mdp_super_t *sb)
1254 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1255 sb->major_version, sb->minor_version, sb->patch_version,
1256 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1258 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1259 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1260 sb->md_minor, sb->layout, sb->chunk_size);
1261 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1262 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1263 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1264 sb->failed_disks, sb->spare_disks,
1265 sb->sb_csum, (unsigned long)sb->events_lo);
1268 for (i = 0; i < MD_SB_DISKS; i++) {
1271 desc = sb->disks + i;
1272 if (desc->number || desc->major || desc->minor ||
1273 desc->raid_disk || (desc->state && (desc->state != 4))) {
1274 printk(" D %2d: ", i);
1278 printk(KERN_INFO "md: THIS: ");
1279 print_desc(&sb->this_disk);
1283 static void print_rdev(mdk_rdev_t *rdev)
1285 char b[BDEVNAME_SIZE];
1286 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1287 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
1288 rdev->faulty, rdev->in_sync, rdev->desc_nr);
1289 if (rdev->sb_loaded) {
1290 printk(KERN_INFO "md: rdev superblock:\n");
1291 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1293 printk(KERN_INFO "md: no rdev superblock!\n");
1296 void md_print_devices(void)
1298 struct list_head *tmp, *tmp2;
1301 char b[BDEVNAME_SIZE];
1304 printk("md: **********************************\n");
1305 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1306 printk("md: **********************************\n");
1307 ITERATE_MDDEV(mddev,tmp) {
1310 bitmap_print_sb(mddev->bitmap);
1312 printk("%s: ", mdname(mddev));
1313 ITERATE_RDEV(mddev,rdev,tmp2)
1314 printk("<%s>", bdevname(rdev->bdev,b));
1317 ITERATE_RDEV(mddev,rdev,tmp2)
1320 printk("md: **********************************\n");
1325 static void sync_sbs(mddev_t * mddev)
1328 struct list_head *tmp;
1330 ITERATE_RDEV(mddev,rdev,tmp) {
1331 super_types[mddev->major_version].
1332 sync_super(mddev, rdev);
1333 rdev->sb_loaded = 1;
1337 static void md_update_sb(mddev_t * mddev)
1340 struct list_head *tmp;
1345 spin_lock(&mddev->write_lock);
1346 sync_req = mddev->in_sync;
1347 mddev->utime = get_seconds();
1350 if (!mddev->events) {
1352 * oops, this 64-bit counter should never wrap.
1353 * Either we are in around ~1 trillion A.C., assuming
1354 * 1 reboot per second, or we have a bug:
1359 mddev->sb_dirty = 2;
1363 * do not write anything to disk if using
1364 * nonpersistent superblocks
1366 if (!mddev->persistent) {
1367 mddev->sb_dirty = 0;
1368 spin_unlock(&mddev->write_lock);
1369 wake_up(&mddev->sb_wait);
1372 spin_unlock(&mddev->write_lock);
1375 "md: updating %s RAID superblock on device (in sync %d)\n",
1376 mdname(mddev),mddev->in_sync);
1378 err = bitmap_update_sb(mddev->bitmap);
1379 ITERATE_RDEV(mddev,rdev,tmp) {
1380 char b[BDEVNAME_SIZE];
1381 dprintk(KERN_INFO "md: ");
1383 dprintk("(skipping faulty ");
1385 dprintk("%s ", bdevname(rdev->bdev,b));
1386 if (!rdev->faulty) {
1387 md_super_write(mddev,rdev,
1388 rdev->sb_offset<<1, rdev->sb_size,
1390 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1391 bdevname(rdev->bdev,b),
1392 (unsigned long long)rdev->sb_offset);
1396 if (mddev->level == LEVEL_MULTIPATH)
1397 /* only need to write one superblock... */
1400 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
1401 /* if there was a failure, sb_dirty was set to 1, and we re-write super */
1403 spin_lock(&mddev->write_lock);
1404 if (mddev->in_sync != sync_req|| mddev->sb_dirty == 1) {
1405 /* have to write it out again */
1406 spin_unlock(&mddev->write_lock);
1409 mddev->sb_dirty = 0;
1410 spin_unlock(&mddev->write_lock);
1411 wake_up(&mddev->sb_wait);
1416 * Import a device. If 'super_format' >= 0, then sanity check the superblock
1418 * mark the device faulty if:
1420 * - the device is nonexistent (zero size)
1421 * - the device has no valid superblock
1423 * a faulty rdev _never_ has rdev->sb set.
1425 static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
1427 char b[BDEVNAME_SIZE];
1432 rdev = (mdk_rdev_t *) kmalloc(sizeof(*rdev), GFP_KERNEL);
1434 printk(KERN_ERR "md: could not alloc mem for new device!\n");
1435 return ERR_PTR(-ENOMEM);
1437 memset(rdev, 0, sizeof(*rdev));
1439 if ((err = alloc_disk_sb(rdev)))
1442 err = lock_rdev(rdev, newdev);
1449 rdev->data_offset = 0;
1450 atomic_set(&rdev->nr_pending, 0);
1452 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
1455 "md: %s has zero or unknown size, marking faulty!\n",
1456 bdevname(rdev->bdev,b));
1461 if (super_format >= 0) {
1462 err = super_types[super_format].
1463 load_super(rdev, NULL, super_minor);
1464 if (err == -EINVAL) {
1466 "md: %s has invalid sb, not importing!\n",
1467 bdevname(rdev->bdev,b));
1472 "md: could not read %s's sb, not importing!\n",
1473 bdevname(rdev->bdev,b));
1477 INIT_LIST_HEAD(&rdev->same_set);
1482 if (rdev->sb_page) {
1488 return ERR_PTR(err);
1492 * Check a full RAID array for plausibility
1496 static void analyze_sbs(mddev_t * mddev)
1499 struct list_head *tmp;
1500 mdk_rdev_t *rdev, *freshest;
1501 char b[BDEVNAME_SIZE];
1504 ITERATE_RDEV(mddev,rdev,tmp)
1505 switch (super_types[mddev->major_version].
1506 load_super(rdev, freshest, mddev->minor_version)) {
1514 "md: fatal superblock inconsistency in %s"
1515 " -- removing from array\n",
1516 bdevname(rdev->bdev,b));
1517 kick_rdev_from_array(rdev);
1521 super_types[mddev->major_version].
1522 validate_super(mddev, freshest);
1525 ITERATE_RDEV(mddev,rdev,tmp) {
1526 if (rdev != freshest)
1527 if (super_types[mddev->major_version].
1528 validate_super(mddev, rdev)) {
1529 printk(KERN_WARNING "md: kicking non-fresh %s"
1531 bdevname(rdev->bdev,b));
1532 kick_rdev_from_array(rdev);
1535 if (mddev->level == LEVEL_MULTIPATH) {
1536 rdev->desc_nr = i++;
1537 rdev->raid_disk = rdev->desc_nr;
1544 if (mddev->recovery_cp != MaxSector &&
1546 printk(KERN_ERR "md: %s: raid array is not clean"
1547 " -- starting background reconstruction\n",
1554 static struct kobject *md_probe(dev_t dev, int *part, void *data)
1556 static DECLARE_MUTEX(disks_sem);
1557 mddev_t *mddev = mddev_find(dev);
1558 struct gendisk *disk;
1559 int partitioned = (MAJOR(dev) != MD_MAJOR);
1560 int shift = partitioned ? MdpMinorShift : 0;
1561 int unit = MINOR(dev) >> shift;
1567 if (mddev->gendisk) {
1572 disk = alloc_disk(1 << shift);
1578 disk->major = MAJOR(dev);
1579 disk->first_minor = unit << shift;
1581 sprintf(disk->disk_name, "md_d%d", unit);
1582 sprintf(disk->devfs_name, "md/d%d", unit);
1584 sprintf(disk->disk_name, "md%d", unit);
1585 sprintf(disk->devfs_name, "md/%d", unit);
1587 disk->fops = &md_fops;
1588 disk->private_data = mddev;
1589 disk->queue = mddev->queue;
1591 mddev->gendisk = disk;
1596 void md_wakeup_thread(mdk_thread_t *thread);
1598 static void md_safemode_timeout(unsigned long data)
1600 mddev_t *mddev = (mddev_t *) data;
1602 mddev->safemode = 1;
1603 md_wakeup_thread(mddev->thread);
1607 static int do_md_run(mddev_t * mddev)
1611 struct list_head *tmp;
1613 struct gendisk *disk;
1614 char b[BDEVNAME_SIZE];
1616 if (list_empty(&mddev->disks))
1617 /* cannot run an array with no devices.. */
1624 * Analyze all RAID superblock(s)
1626 if (!mddev->raid_disks)
1629 chunk_size = mddev->chunk_size;
1630 pnum = level_to_pers(mddev->level);
1632 if ((pnum != MULTIPATH) && (pnum != RAID1)) {
1635 * 'default chunksize' in the old md code used to
1636 * be PAGE_SIZE, baaad.
1637 * we abort here to be on the safe side. We don't
1638 * want to continue the bad practice.
1641 "no chunksize specified, see 'man raidtab'\n");
1644 if (chunk_size > MAX_CHUNK_SIZE) {
1645 printk(KERN_ERR "too big chunk_size: %d > %d\n",
1646 chunk_size, MAX_CHUNK_SIZE);
1650 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
1652 if ( (1 << ffz(~chunk_size)) != chunk_size) {
1653 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
1656 if (chunk_size < PAGE_SIZE) {
1657 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
1658 chunk_size, PAGE_SIZE);
1662 /* devices must have minimum size of one chunk */
1663 ITERATE_RDEV(mddev,rdev,tmp) {
1666 if (rdev->size < chunk_size / 1024) {
1668 "md: Dev %s smaller than chunk_size:"
1670 bdevname(rdev->bdev,b),
1671 (unsigned long long)rdev->size,
1681 request_module("md-personality-%d", pnum);
1686 * Drop all container device buffers, from now on
1687 * the only valid external interface is through the md
1689 * Also find largest hardsector size
1691 ITERATE_RDEV(mddev,rdev,tmp) {
1694 sync_blockdev(rdev->bdev);
1695 invalidate_bdev(rdev->bdev, 0);
1698 md_probe(mddev->unit, NULL, NULL);
1699 disk = mddev->gendisk;
1703 spin_lock(&pers_lock);
1704 if (!pers[pnum] || !try_module_get(pers[pnum]->owner)) {
1705 spin_unlock(&pers_lock);
1706 printk(KERN_WARNING "md: personality %d is not loaded!\n",
1711 mddev->pers = pers[pnum];
1712 spin_unlock(&pers_lock);
1714 mddev->recovery = 0;
1715 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
1717 /* before we start the array running, initialise the bitmap */
1718 err = bitmap_create(mddev);
1720 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
1721 mdname(mddev), err);
1723 err = mddev->pers->run(mddev);
1725 printk(KERN_ERR "md: pers->run() failed ...\n");
1726 module_put(mddev->pers->owner);
1728 bitmap_destroy(mddev);
1731 atomic_set(&mddev->writes_pending,0);
1732 mddev->safemode = 0;
1733 mddev->safemode_timer.function = md_safemode_timeout;
1734 mddev->safemode_timer.data = (unsigned long) mddev;
1735 mddev->safemode_delay = (20 * HZ)/1000 +1; /* 20 msec delay */
1738 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1739 md_wakeup_thread(mddev->thread);
1741 if (mddev->sb_dirty)
1742 md_update_sb(mddev);
1744 set_capacity(disk, mddev->array_size<<1);
1746 /* If we call blk_queue_make_request here, it will
1747 * re-initialise max_sectors etc which may have been
1748 * refined inside -> run. So just set the bits we need to set.
1749 * Most initialisation happended when we called
1750 * blk_queue_make_request(..., md_fail_request)
1753 mddev->queue->queuedata = mddev;
1754 mddev->queue->make_request_fn = mddev->pers->make_request;
1760 static int restart_array(mddev_t *mddev)
1762 struct gendisk *disk = mddev->gendisk;
1766 * Complain if it has no devices
1769 if (list_empty(&mddev->disks))
1777 mddev->safemode = 0;
1779 set_disk_ro(disk, 0);
1781 printk(KERN_INFO "md: %s switched to read-write mode.\n",
1784 * Kick recovery or resync if necessary
1786 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1787 md_wakeup_thread(mddev->thread);
1790 printk(KERN_ERR "md: %s has no personality assigned.\n",
1799 static int do_md_stop(mddev_t * mddev, int ro)
1802 struct gendisk *disk = mddev->gendisk;
1805 if (atomic_read(&mddev->active)>2) {
1806 printk("md: %s still in use.\n",mdname(mddev));
1810 if (mddev->sync_thread) {
1811 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1812 md_unregister_thread(mddev->sync_thread);
1813 mddev->sync_thread = NULL;
1816 del_timer_sync(&mddev->safemode_timer);
1818 invalidate_partition(disk, 0);
1826 bitmap_flush(mddev);
1827 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
1829 set_disk_ro(disk, 0);
1830 blk_queue_make_request(mddev->queue, md_fail_request);
1831 mddev->pers->stop(mddev);
1832 module_put(mddev->pers->owner);
1837 if (!mddev->in_sync) {
1838 /* mark array as shutdown cleanly */
1840 md_update_sb(mddev);
1843 set_disk_ro(disk, 1);
1846 bitmap_destroy(mddev);
1847 if (mddev->bitmap_file) {
1848 atomic_set(&mddev->bitmap_file->f_dentry->d_inode->i_writecount, 1);
1849 fput(mddev->bitmap_file);
1850 mddev->bitmap_file = NULL;
1852 mddev->bitmap_offset = 0;
1855 * Free resources if final stop
1858 struct gendisk *disk;
1859 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
1861 export_array(mddev);
1863 mddev->array_size = 0;
1864 disk = mddev->gendisk;
1866 set_capacity(disk, 0);
1869 printk(KERN_INFO "md: %s switched to read-only mode.\n",
1876 static void autorun_array(mddev_t *mddev)
1879 struct list_head *tmp;
1882 if (list_empty(&mddev->disks))
1885 printk(KERN_INFO "md: running: ");
1887 ITERATE_RDEV(mddev,rdev,tmp) {
1888 char b[BDEVNAME_SIZE];
1889 printk("<%s>", bdevname(rdev->bdev,b));
1893 err = do_md_run (mddev);
1895 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
1896 do_md_stop (mddev, 0);
1901 * lets try to run arrays based on all disks that have arrived
1902 * until now. (those are in pending_raid_disks)
1904 * the method: pick the first pending disk, collect all disks with
1905 * the same UUID, remove all from the pending list and put them into
1906 * the 'same_array' list. Then order this list based on superblock
1907 * update time (freshest comes first), kick out 'old' disks and
1908 * compare superblocks. If everything's fine then run it.
1910 * If "unit" is allocated, then bump its reference count
1912 static void autorun_devices(int part)
1914 struct list_head candidates;
1915 struct list_head *tmp;
1916 mdk_rdev_t *rdev0, *rdev;
1918 char b[BDEVNAME_SIZE];
1920 printk(KERN_INFO "md: autorun ...\n");
1921 while (!list_empty(&pending_raid_disks)) {
1923 rdev0 = list_entry(pending_raid_disks.next,
1924 mdk_rdev_t, same_set);
1926 printk(KERN_INFO "md: considering %s ...\n",
1927 bdevname(rdev0->bdev,b));
1928 INIT_LIST_HEAD(&candidates);
1929 ITERATE_RDEV_PENDING(rdev,tmp)
1930 if (super_90_load(rdev, rdev0, 0) >= 0) {
1931 printk(KERN_INFO "md: adding %s ...\n",
1932 bdevname(rdev->bdev,b));
1933 list_move(&rdev->same_set, &candidates);
1936 * now we have a set of devices, with all of them having
1937 * mostly sane superblocks. It's time to allocate the
1940 if (rdev0->preferred_minor < 0 || rdev0->preferred_minor >= MAX_MD_DEVS) {
1941 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
1942 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
1946 dev = MKDEV(mdp_major,
1947 rdev0->preferred_minor << MdpMinorShift);
1949 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
1951 md_probe(dev, NULL, NULL);
1952 mddev = mddev_find(dev);
1955 "md: cannot allocate memory for md drive.\n");
1958 if (mddev_lock(mddev))
1959 printk(KERN_WARNING "md: %s locked, cannot run\n",
1961 else if (mddev->raid_disks || mddev->major_version
1962 || !list_empty(&mddev->disks)) {
1964 "md: %s already running, cannot run %s\n",
1965 mdname(mddev), bdevname(rdev0->bdev,b));
1966 mddev_unlock(mddev);
1968 printk(KERN_INFO "md: created %s\n", mdname(mddev));
1969 ITERATE_RDEV_GENERIC(candidates,rdev,tmp) {
1970 list_del_init(&rdev->same_set);
1971 if (bind_rdev_to_array(rdev, mddev))
1974 autorun_array(mddev);
1975 mddev_unlock(mddev);
1977 /* on success, candidates will be empty, on error
1980 ITERATE_RDEV_GENERIC(candidates,rdev,tmp)
1984 printk(KERN_INFO "md: ... autorun DONE.\n");
1988 * import RAID devices based on one partition
1989 * if possible, the array gets run as well.
1992 static int autostart_array(dev_t startdev)
1994 char b[BDEVNAME_SIZE];
1995 int err = -EINVAL, i;
1996 mdp_super_t *sb = NULL;
1997 mdk_rdev_t *start_rdev = NULL, *rdev;
1999 start_rdev = md_import_device(startdev, 0, 0);
2000 if (IS_ERR(start_rdev))
2004 /* NOTE: this can only work for 0.90.0 superblocks */
2005 sb = (mdp_super_t*)page_address(start_rdev->sb_page);
2006 if (sb->major_version != 0 ||
2007 sb->minor_version != 90 ) {
2008 printk(KERN_WARNING "md: can only autostart 0.90.0 arrays\n");
2009 export_rdev(start_rdev);
2013 if (start_rdev->faulty) {
2015 "md: can not autostart based on faulty %s!\n",
2016 bdevname(start_rdev->bdev,b));
2017 export_rdev(start_rdev);
2020 list_add(&start_rdev->same_set, &pending_raid_disks);
2022 for (i = 0; i < MD_SB_DISKS; i++) {
2023 mdp_disk_t *desc = sb->disks + i;
2024 dev_t dev = MKDEV(desc->major, desc->minor);
2028 if (dev == startdev)
2030 if (MAJOR(dev) != desc->major || MINOR(dev) != desc->minor)
2032 rdev = md_import_device(dev, 0, 0);
2036 list_add(&rdev->same_set, &pending_raid_disks);
2040 * possibly return codes
2048 static int get_version(void __user * arg)
2052 ver.major = MD_MAJOR_VERSION;
2053 ver.minor = MD_MINOR_VERSION;
2054 ver.patchlevel = MD_PATCHLEVEL_VERSION;
2056 if (copy_to_user(arg, &ver, sizeof(ver)))
2062 static int get_array_info(mddev_t * mddev, void __user * arg)
2064 mdu_array_info_t info;
2065 int nr,working,active,failed,spare;
2067 struct list_head *tmp;
2069 nr=working=active=failed=spare=0;
2070 ITERATE_RDEV(mddev,rdev,tmp) {
2083 info.major_version = mddev->major_version;
2084 info.minor_version = mddev->minor_version;
2085 info.patch_version = MD_PATCHLEVEL_VERSION;
2086 info.ctime = mddev->ctime;
2087 info.level = mddev->level;
2088 info.size = mddev->size;
2090 info.raid_disks = mddev->raid_disks;
2091 info.md_minor = mddev->md_minor;
2092 info.not_persistent= !mddev->persistent;
2094 info.utime = mddev->utime;
2097 info.state = (1<<MD_SB_CLEAN);
2098 if (mddev->bitmap && mddev->bitmap_offset)
2099 info.state = (1<<MD_SB_BITMAP_PRESENT);
2100 info.active_disks = active;
2101 info.working_disks = working;
2102 info.failed_disks = failed;
2103 info.spare_disks = spare;
2105 info.layout = mddev->layout;
2106 info.chunk_size = mddev->chunk_size;
2108 if (copy_to_user(arg, &info, sizeof(info)))
2114 static int get_bitmap_file(mddev_t * mddev, void __user * arg)
2116 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
2117 char *ptr, *buf = NULL;
2120 file = kmalloc(sizeof(*file), GFP_KERNEL);
2124 /* bitmap disabled, zero the first byte and copy out */
2125 if (!mddev->bitmap || !mddev->bitmap->file) {
2126 file->pathname[0] = '\0';
2130 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
2134 ptr = file_path(mddev->bitmap->file, buf, sizeof(file->pathname));
2138 strcpy(file->pathname, ptr);
2142 if (copy_to_user(arg, file, sizeof(*file)))
2150 static int get_disk_info(mddev_t * mddev, void __user * arg)
2152 mdu_disk_info_t info;
2156 if (copy_from_user(&info, arg, sizeof(info)))
2161 rdev = find_rdev_nr(mddev, nr);
2163 info.major = MAJOR(rdev->bdev->bd_dev);
2164 info.minor = MINOR(rdev->bdev->bd_dev);
2165 info.raid_disk = rdev->raid_disk;
2168 info.state |= (1<<MD_DISK_FAULTY);
2169 else if (rdev->in_sync) {
2170 info.state |= (1<<MD_DISK_ACTIVE);
2171 info.state |= (1<<MD_DISK_SYNC);
2173 if (test_bit(WriteMostly, &rdev->flags))
2174 info.state |= (1<<MD_DISK_WRITEMOSTLY);
2176 info.major = info.minor = 0;
2177 info.raid_disk = -1;
2178 info.state = (1<<MD_DISK_REMOVED);
2181 if (copy_to_user(arg, &info, sizeof(info)))
2187 static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
2189 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
2191 dev_t dev = MKDEV(info->major,info->minor);
2193 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
2196 if (!mddev->raid_disks) {
2198 /* expecting a device which has a superblock */
2199 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
2202 "md: md_import_device returned %ld\n",
2204 return PTR_ERR(rdev);
2206 if (!list_empty(&mddev->disks)) {
2207 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2208 mdk_rdev_t, same_set);
2209 int err = super_types[mddev->major_version]
2210 .load_super(rdev, rdev0, mddev->minor_version);
2213 "md: %s has different UUID to %s\n",
2214 bdevname(rdev->bdev,b),
2215 bdevname(rdev0->bdev,b2));
2220 err = bind_rdev_to_array(rdev, mddev);
2227 * add_new_disk can be used once the array is assembled
2228 * to add "hot spares". They must already have a superblock
2233 if (!mddev->pers->hot_add_disk) {
2235 "%s: personality does not support diskops!\n",
2239 if (mddev->persistent)
2240 rdev = md_import_device(dev, mddev->major_version,
2241 mddev->minor_version);
2243 rdev = md_import_device(dev, -1, -1);
2246 "md: md_import_device returned %ld\n",
2248 return PTR_ERR(rdev);
2250 /* set save_raid_disk if appropriate */
2251 if (!mddev->persistent) {
2252 if (info->state & (1<<MD_DISK_SYNC) &&
2253 info->raid_disk < mddev->raid_disks)
2254 rdev->raid_disk = info->raid_disk;
2256 rdev->raid_disk = -1;
2258 super_types[mddev->major_version].
2259 validate_super(mddev, rdev);
2260 rdev->saved_raid_disk = rdev->raid_disk;
2262 rdev->in_sync = 0; /* just to be sure */
2263 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
2264 set_bit(WriteMostly, &rdev->flags);
2266 rdev->raid_disk = -1;
2267 err = bind_rdev_to_array(rdev, mddev);
2271 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2272 md_wakeup_thread(mddev->thread);
2276 /* otherwise, add_new_disk is only allowed
2277 * for major_version==0 superblocks
2279 if (mddev->major_version != 0) {
2280 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
2285 if (!(info->state & (1<<MD_DISK_FAULTY))) {
2287 rdev = md_import_device (dev, -1, 0);
2290 "md: error, md_import_device() returned %ld\n",
2292 return PTR_ERR(rdev);
2294 rdev->desc_nr = info->number;
2295 if (info->raid_disk < mddev->raid_disks)
2296 rdev->raid_disk = info->raid_disk;
2298 rdev->raid_disk = -1;
2301 if (rdev->raid_disk < mddev->raid_disks)
2302 rdev->in_sync = (info->state & (1<<MD_DISK_SYNC));
2306 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
2307 set_bit(WriteMostly, &rdev->flags);
2309 err = bind_rdev_to_array(rdev, mddev);
2315 if (!mddev->persistent) {
2316 printk(KERN_INFO "md: nonpersistent superblock ...\n");
2317 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2319 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
2320 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
2322 if (!mddev->size || (mddev->size > rdev->size))
2323 mddev->size = rdev->size;
2329 static int hot_remove_disk(mddev_t * mddev, dev_t dev)
2331 char b[BDEVNAME_SIZE];
2337 rdev = find_rdev(mddev, dev);
2341 if (rdev->raid_disk >= 0)
2344 kick_rdev_from_array(rdev);
2345 md_update_sb(mddev);
2349 printk(KERN_WARNING "md: cannot remove active disk %s from %s ... \n",
2350 bdevname(rdev->bdev,b), mdname(mddev));
2354 static int hot_add_disk(mddev_t * mddev, dev_t dev)
2356 char b[BDEVNAME_SIZE];
2364 if (mddev->major_version != 0) {
2365 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
2366 " version-0 superblocks.\n",
2370 if (!mddev->pers->hot_add_disk) {
2372 "%s: personality does not support diskops!\n",
2377 rdev = md_import_device (dev, -1, 0);
2380 "md: error, md_import_device() returned %ld\n",
2385 if (mddev->persistent)
2386 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
2389 rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2391 size = calc_dev_size(rdev, mddev->chunk_size);
2394 if (size < mddev->size) {
2396 "%s: disk size %llu blocks < array size %llu\n",
2397 mdname(mddev), (unsigned long long)size,
2398 (unsigned long long)mddev->size);
2405 "md: can not hot-add faulty %s disk to %s!\n",
2406 bdevname(rdev->bdev,b), mdname(mddev));
2412 bind_rdev_to_array(rdev, mddev);
2415 * The rest should better be atomic, we can have disk failures
2416 * noticed in interrupt contexts ...
2419 if (rdev->desc_nr == mddev->max_disks) {
2420 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
2423 goto abort_unbind_export;
2426 rdev->raid_disk = -1;
2428 md_update_sb(mddev);
2431 * Kick recovery, maybe this spare has to be added to the
2432 * array immediately.
2434 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2435 md_wakeup_thread(mddev->thread);
2439 abort_unbind_export:
2440 unbind_rdev_from_array(rdev);
2447 /* similar to deny_write_access, but accounts for our holding a reference
2448 * to the file ourselves */
2449 static int deny_bitmap_write_access(struct file * file)
2451 struct inode *inode = file->f_mapping->host;
2453 spin_lock(&inode->i_lock);
2454 if (atomic_read(&inode->i_writecount) > 1) {
2455 spin_unlock(&inode->i_lock);
2458 atomic_set(&inode->i_writecount, -1);
2459 spin_unlock(&inode->i_lock);
2464 static int set_bitmap_file(mddev_t *mddev, int fd)
2469 if (!mddev->pers->quiesce)
2471 if (mddev->recovery || mddev->sync_thread)
2473 /* we should be able to change the bitmap.. */
2479 return -EEXIST; /* cannot add when bitmap is present */
2480 mddev->bitmap_file = fget(fd);
2482 if (mddev->bitmap_file == NULL) {
2483 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
2488 err = deny_bitmap_write_access(mddev->bitmap_file);
2490 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
2492 fput(mddev->bitmap_file);
2493 mddev->bitmap_file = NULL;
2496 mddev->bitmap_offset = 0; /* file overrides offset */
2497 } else if (mddev->bitmap == NULL)
2498 return -ENOENT; /* cannot remove what isn't there */
2501 mddev->pers->quiesce(mddev, 1);
2503 err = bitmap_create(mddev);
2505 bitmap_destroy(mddev);
2506 mddev->pers->quiesce(mddev, 0);
2507 } else if (fd < 0) {
2508 if (mddev->bitmap_file)
2509 fput(mddev->bitmap_file);
2510 mddev->bitmap_file = NULL;
2517 * set_array_info is used two different ways
2518 * The original usage is when creating a new array.
2519 * In this usage, raid_disks is > 0 and it together with
2520 * level, size, not_persistent,layout,chunksize determine the
2521 * shape of the array.
2522 * This will always create an array with a type-0.90.0 superblock.
2523 * The newer usage is when assembling an array.
2524 * In this case raid_disks will be 0, and the major_version field is
2525 * use to determine which style super-blocks are to be found on the devices.
2526 * The minor and patch _version numbers are also kept incase the
2527 * super_block handler wishes to interpret them.
2529 static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
2532 if (info->raid_disks == 0) {
2533 /* just setting version number for superblock loading */
2534 if (info->major_version < 0 ||
2535 info->major_version >= sizeof(super_types)/sizeof(super_types[0]) ||
2536 super_types[info->major_version].name == NULL) {
2537 /* maybe try to auto-load a module? */
2539 "md: superblock version %d not known\n",
2540 info->major_version);
2543 mddev->major_version = info->major_version;
2544 mddev->minor_version = info->minor_version;
2545 mddev->patch_version = info->patch_version;
2548 mddev->major_version = MD_MAJOR_VERSION;
2549 mddev->minor_version = MD_MINOR_VERSION;
2550 mddev->patch_version = MD_PATCHLEVEL_VERSION;
2551 mddev->ctime = get_seconds();
2553 mddev->level = info->level;
2554 mddev->size = info->size;
2555 mddev->raid_disks = info->raid_disks;
2556 /* don't set md_minor, it is determined by which /dev/md* was
2559 if (info->state & (1<<MD_SB_CLEAN))
2560 mddev->recovery_cp = MaxSector;
2562 mddev->recovery_cp = 0;
2563 mddev->persistent = ! info->not_persistent;
2565 mddev->layout = info->layout;
2566 mddev->chunk_size = info->chunk_size;
2568 mddev->max_disks = MD_SB_DISKS;
2570 mddev->sb_dirty = 1;
2573 * Generate a 128 bit UUID
2575 get_random_bytes(mddev->uuid, 16);
2581 * update_array_info is used to change the configuration of an
2583 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
2584 * fields in the info are checked against the array.
2585 * Any differences that cannot be handled will cause an error.
2586 * Normally, only one change can be managed at a time.
2588 static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
2594 /* calculate expected state,ignoring low bits */
2595 if (mddev->bitmap && mddev->bitmap_offset)
2596 state |= (1 << MD_SB_BITMAP_PRESENT);
2598 if (mddev->major_version != info->major_version ||
2599 mddev->minor_version != info->minor_version ||
2600 /* mddev->patch_version != info->patch_version || */
2601 mddev->ctime != info->ctime ||
2602 mddev->level != info->level ||
2603 /* mddev->layout != info->layout || */
2604 !mddev->persistent != info->not_persistent||
2605 mddev->chunk_size != info->chunk_size ||
2606 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
2607 ((state^info->state) & 0xfffffe00)
2610 /* Check there is only one change */
2611 if (mddev->size != info->size) cnt++;
2612 if (mddev->raid_disks != info->raid_disks) cnt++;
2613 if (mddev->layout != info->layout) cnt++;
2614 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
2615 if (cnt == 0) return 0;
2616 if (cnt > 1) return -EINVAL;
2618 if (mddev->layout != info->layout) {
2620 * we don't need to do anything at the md level, the
2621 * personality will take care of it all.
2623 if (mddev->pers->reconfig == NULL)
2626 return mddev->pers->reconfig(mddev, info->layout, -1);
2628 if (mddev->size != info->size) {
2630 struct list_head *tmp;
2631 if (mddev->pers->resize == NULL)
2633 /* The "size" is the amount of each device that is used.
2634 * This can only make sense for arrays with redundancy.
2635 * linear and raid0 always use whatever space is available
2636 * We can only consider changing the size if no resync
2637 * or reconstruction is happening, and if the new size
2638 * is acceptable. It must fit before the sb_offset or,
2639 * if that is <data_offset, it must fit before the
2640 * size of each device.
2641 * If size is zero, we find the largest size that fits.
2643 if (mddev->sync_thread)
2645 ITERATE_RDEV(mddev,rdev,tmp) {
2647 int fit = (info->size == 0);
2648 if (rdev->sb_offset > rdev->data_offset)
2649 avail = (rdev->sb_offset*2) - rdev->data_offset;
2651 avail = get_capacity(rdev->bdev->bd_disk)
2652 - rdev->data_offset;
2653 if (fit && (info->size == 0 || info->size > avail/2))
2654 info->size = avail/2;
2655 if (avail < ((sector_t)info->size << 1))
2658 rv = mddev->pers->resize(mddev, (sector_t)info->size *2);
2660 struct block_device *bdev;
2662 bdev = bdget_disk(mddev->gendisk, 0);
2664 down(&bdev->bd_inode->i_sem);
2665 i_size_write(bdev->bd_inode, mddev->array_size << 10);
2666 up(&bdev->bd_inode->i_sem);
2671 if (mddev->raid_disks != info->raid_disks) {
2672 /* change the number of raid disks */
2673 if (mddev->pers->reshape == NULL)
2675 if (info->raid_disks <= 0 ||
2676 info->raid_disks >= mddev->max_disks)
2678 if (mddev->sync_thread)
2680 rv = mddev->pers->reshape(mddev, info->raid_disks);
2682 struct block_device *bdev;
2684 bdev = bdget_disk(mddev->gendisk, 0);
2686 down(&bdev->bd_inode->i_sem);
2687 i_size_write(bdev->bd_inode, mddev->array_size << 10);
2688 up(&bdev->bd_inode->i_sem);
2693 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
2694 if (mddev->pers->quiesce == NULL)
2696 if (mddev->recovery || mddev->sync_thread)
2698 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
2699 /* add the bitmap */
2702 if (mddev->default_bitmap_offset == 0)
2704 mddev->bitmap_offset = mddev->default_bitmap_offset;
2705 mddev->pers->quiesce(mddev, 1);
2706 rv = bitmap_create(mddev);
2708 bitmap_destroy(mddev);
2709 mddev->pers->quiesce(mddev, 0);
2711 /* remove the bitmap */
2714 if (mddev->bitmap->file)
2716 mddev->pers->quiesce(mddev, 1);
2717 bitmap_destroy(mddev);
2718 mddev->pers->quiesce(mddev, 0);
2719 mddev->bitmap_offset = 0;
2722 md_update_sb(mddev);
2726 static int set_disk_faulty(mddev_t *mddev, dev_t dev)
2730 if (mddev->pers == NULL)
2733 rdev = find_rdev(mddev, dev);
2737 md_error(mddev, rdev);
2741 static int md_ioctl(struct inode *inode, struct file *file,
2742 unsigned int cmd, unsigned long arg)
2745 void __user *argp = (void __user *)arg;
2746 struct hd_geometry __user *loc = argp;
2747 mddev_t *mddev = NULL;
2749 if (!capable(CAP_SYS_ADMIN))
2753 * Commands dealing with the RAID driver but not any
2759 err = get_version(argp);
2762 case PRINT_RAID_DEBUG:
2770 autostart_arrays(arg);
2777 * Commands creating/starting a new array:
2780 mddev = inode->i_bdev->bd_disk->private_data;
2788 if (cmd == START_ARRAY) {
2789 /* START_ARRAY doesn't need to lock the array as autostart_array
2790 * does the locking, and it could even be a different array
2795 "md: %s(pid %d) used deprecated START_ARRAY ioctl. "
2796 "This will not be supported beyond 2.6\n",
2797 current->comm, current->pid);
2800 err = autostart_array(new_decode_dev(arg));
2802 printk(KERN_WARNING "md: autostart failed!\n");
2808 err = mddev_lock(mddev);
2811 "md: ioctl lock interrupted, reason %d, cmd %d\n",
2818 case SET_ARRAY_INFO:
2820 mdu_array_info_t info;
2822 memset(&info, 0, sizeof(info));
2823 else if (copy_from_user(&info, argp, sizeof(info))) {
2828 err = update_array_info(mddev, &info);
2830 printk(KERN_WARNING "md: couldn't update"
2831 " array info. %d\n", err);
2836 if (!list_empty(&mddev->disks)) {
2838 "md: array %s already has disks!\n",
2843 if (mddev->raid_disks) {
2845 "md: array %s already initialised!\n",
2850 err = set_array_info(mddev, &info);
2852 printk(KERN_WARNING "md: couldn't set"
2853 " array info. %d\n", err);
2863 * Commands querying/configuring an existing array:
2865 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
2866 * RUN_ARRAY, and SET_BITMAP_FILE are allowed */
2867 if (!mddev->raid_disks && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
2868 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE) {
2874 * Commands even a read-only array can execute:
2878 case GET_ARRAY_INFO:
2879 err = get_array_info(mddev, argp);
2882 case GET_BITMAP_FILE:
2883 err = get_bitmap_file(mddev, argp);
2887 err = get_disk_info(mddev, argp);
2890 case RESTART_ARRAY_RW:
2891 err = restart_array(mddev);
2895 err = do_md_stop (mddev, 0);
2899 err = do_md_stop (mddev, 1);
2903 * We have a problem here : there is no easy way to give a CHS
2904 * virtual geometry. We currently pretend that we have a 2 heads
2905 * 4 sectors (with a BIG number of cylinders...). This drives
2906 * dosfs just mad... ;-)
2913 err = put_user (2, (char __user *) &loc->heads);
2916 err = put_user (4, (char __user *) &loc->sectors);
2919 err = put_user(get_capacity(mddev->gendisk)/8,
2920 (short __user *) &loc->cylinders);
2923 err = put_user (get_start_sect(inode->i_bdev),
2924 (long __user *) &loc->start);
2929 * The remaining ioctls are changing the state of the
2930 * superblock, so we do not allow read-only arrays
2942 mdu_disk_info_t info;
2943 if (copy_from_user(&info, argp, sizeof(info)))
2946 err = add_new_disk(mddev, &info);
2950 case HOT_REMOVE_DISK:
2951 err = hot_remove_disk(mddev, new_decode_dev(arg));
2955 err = hot_add_disk(mddev, new_decode_dev(arg));
2958 case SET_DISK_FAULTY:
2959 err = set_disk_faulty(mddev, new_decode_dev(arg));
2963 err = do_md_run (mddev);
2966 case SET_BITMAP_FILE:
2967 err = set_bitmap_file(mddev, (int)arg);
2971 if (_IOC_TYPE(cmd) == MD_MAJOR)
2972 printk(KERN_WARNING "md: %s(pid %d) used"
2973 " obsolete MD ioctl, upgrade your"
2974 " software to use new ictls.\n",
2975 current->comm, current->pid);
2982 mddev_unlock(mddev);
2992 static int md_open(struct inode *inode, struct file *file)
2995 * Succeed if we can lock the mddev, which confirms that
2996 * it isn't being stopped right now.
2998 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
3001 if ((err = mddev_lock(mddev)))
3006 mddev_unlock(mddev);
3008 check_disk_change(inode->i_bdev);
3013 static int md_release(struct inode *inode, struct file * file)
3015 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
3024 static int md_media_changed(struct gendisk *disk)
3026 mddev_t *mddev = disk->private_data;
3028 return mddev->changed;
3031 static int md_revalidate(struct gendisk *disk)
3033 mddev_t *mddev = disk->private_data;
3038 static struct block_device_operations md_fops =
3040 .owner = THIS_MODULE,
3042 .release = md_release,
3044 .media_changed = md_media_changed,
3045 .revalidate_disk= md_revalidate,
3048 static int md_thread(void * arg)
3050 mdk_thread_t *thread = arg;
3053 * md_thread is a 'system-thread', it's priority should be very
3054 * high. We avoid resource deadlocks individually in each
3055 * raid personality. (RAID5 does preallocation) We also use RR and
3056 * the very same RT priority as kswapd, thus we will never get
3057 * into a priority inversion deadlock.
3059 * we definitely have to have equal or higher priority than
3060 * bdflush, otherwise bdflush will deadlock if there are too
3061 * many dirty RAID5 blocks.
3064 complete(thread->event);
3065 while (!kthread_should_stop()) {
3066 void (*run)(mddev_t *);
3068 wait_event_interruptible_timeout(thread->wqueue,
3069 test_bit(THREAD_WAKEUP, &thread->flags)
3070 || kthread_should_stop(),
3074 clear_bit(THREAD_WAKEUP, &thread->flags);
3084 void md_wakeup_thread(mdk_thread_t *thread)
3087 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
3088 set_bit(THREAD_WAKEUP, &thread->flags);
3089 wake_up(&thread->wqueue);
3093 mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
3096 mdk_thread_t *thread;
3097 struct completion event;
3099 thread = kmalloc(sizeof(mdk_thread_t), GFP_KERNEL);
3103 memset(thread, 0, sizeof(mdk_thread_t));
3104 init_waitqueue_head(&thread->wqueue);
3106 init_completion(&event);
3107 thread->event = &event;
3109 thread->mddev = mddev;
3110 thread->name = name;
3111 thread->timeout = MAX_SCHEDULE_TIMEOUT;
3112 thread->tsk = kthread_run(md_thread, thread, mdname(thread->mddev));
3113 if (IS_ERR(thread->tsk)) {
3117 wait_for_completion(&event);
3121 void md_unregister_thread(mdk_thread_t *thread)
3123 dprintk("interrupting MD-thread pid %d\n", thread->tsk->pid);
3125 kthread_stop(thread->tsk);
3129 void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
3136 if (!rdev || rdev->faulty)
3139 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
3141 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
3142 __builtin_return_address(0),__builtin_return_address(1),
3143 __builtin_return_address(2),__builtin_return_address(3));
3145 if (!mddev->pers->error_handler)
3147 mddev->pers->error_handler(mddev,rdev);
3148 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3149 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3150 md_wakeup_thread(mddev->thread);
3153 /* seq_file implementation /proc/mdstat */
3155 static void status_unused(struct seq_file *seq)
3159 struct list_head *tmp;
3161 seq_printf(seq, "unused devices: ");
3163 ITERATE_RDEV_PENDING(rdev,tmp) {
3164 char b[BDEVNAME_SIZE];
3166 seq_printf(seq, "%s ",
3167 bdevname(rdev->bdev,b));
3170 seq_printf(seq, "<none>");
3172 seq_printf(seq, "\n");
3176 static void status_resync(struct seq_file *seq, mddev_t * mddev)
3178 unsigned long max_blocks, resync, res, dt, db, rt;
3180 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
3182 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3183 max_blocks = mddev->resync_max_sectors >> 1;
3185 max_blocks = mddev->size;
3188 * Should not happen.
3194 res = (resync/1024)*1000/(max_blocks/1024 + 1);
3196 int i, x = res/50, y = 20-x;
3197 seq_printf(seq, "[");
3198 for (i = 0; i < x; i++)
3199 seq_printf(seq, "=");
3200 seq_printf(seq, ">");
3201 for (i = 0; i < y; i++)
3202 seq_printf(seq, ".");
3203 seq_printf(seq, "] ");
3205 seq_printf(seq, " %s =%3lu.%lu%% (%lu/%lu)",
3206 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
3207 "resync" : "recovery"),
3208 res/10, res % 10, resync, max_blocks);
3211 * We do not want to overflow, so the order of operands and
3212 * the * 100 / 100 trick are important. We do a +1 to be
3213 * safe against division by zero. We only estimate anyway.
3215 * dt: time from mark until now
3216 * db: blocks written from mark until now
3217 * rt: remaining time
3219 dt = ((jiffies - mddev->resync_mark) / HZ);
3221 db = resync - (mddev->resync_mark_cnt/2);
3222 rt = (dt * ((max_blocks-resync) / (db/100+1)))/100;
3224 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
3226 seq_printf(seq, " speed=%ldK/sec", db/dt);
3229 static void *md_seq_start(struct seq_file *seq, loff_t *pos)
3231 struct list_head *tmp;
3241 spin_lock(&all_mddevs_lock);
3242 list_for_each(tmp,&all_mddevs)
3244 mddev = list_entry(tmp, mddev_t, all_mddevs);
3246 spin_unlock(&all_mddevs_lock);
3249 spin_unlock(&all_mddevs_lock);
3251 return (void*)2;/* tail */
3255 static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3257 struct list_head *tmp;
3258 mddev_t *next_mddev, *mddev = v;
3264 spin_lock(&all_mddevs_lock);
3266 tmp = all_mddevs.next;
3268 tmp = mddev->all_mddevs.next;
3269 if (tmp != &all_mddevs)
3270 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
3272 next_mddev = (void*)2;
3275 spin_unlock(&all_mddevs_lock);
3283 static void md_seq_stop(struct seq_file *seq, void *v)
3287 if (mddev && v != (void*)1 && v != (void*)2)
3291 static int md_seq_show(struct seq_file *seq, void *v)
3295 struct list_head *tmp2;
3298 struct bitmap *bitmap;
3300 if (v == (void*)1) {
3301 seq_printf(seq, "Personalities : ");
3302 spin_lock(&pers_lock);
3303 for (i = 0; i < MAX_PERSONALITY; i++)
3305 seq_printf(seq, "[%s] ", pers[i]->name);
3307 spin_unlock(&pers_lock);
3308 seq_printf(seq, "\n");
3311 if (v == (void*)2) {
3316 if (mddev_lock(mddev)!=0)
3318 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
3319 seq_printf(seq, "%s : %sactive", mdname(mddev),
3320 mddev->pers ? "" : "in");
3323 seq_printf(seq, " (read-only)");
3324 seq_printf(seq, " %s", mddev->pers->name);
3328 ITERATE_RDEV(mddev,rdev,tmp2) {
3329 char b[BDEVNAME_SIZE];
3330 seq_printf(seq, " %s[%d]",
3331 bdevname(rdev->bdev,b), rdev->desc_nr);
3332 if (test_bit(WriteMostly, &rdev->flags))
3333 seq_printf(seq, "(W)");
3335 seq_printf(seq, "(F)");
3341 if (!list_empty(&mddev->disks)) {
3343 seq_printf(seq, "\n %llu blocks",
3344 (unsigned long long)mddev->array_size);
3346 seq_printf(seq, "\n %llu blocks",
3347 (unsigned long long)size);
3349 if (mddev->persistent) {
3350 if (mddev->major_version != 0 ||
3351 mddev->minor_version != 90) {
3352 seq_printf(seq," super %d.%d",
3353 mddev->major_version,
3354 mddev->minor_version);
3357 seq_printf(seq, " super non-persistent");
3360 mddev->pers->status (seq, mddev);
3361 seq_printf(seq, "\n ");
3362 if (mddev->curr_resync > 2) {
3363 status_resync (seq, mddev);
3364 seq_printf(seq, "\n ");
3365 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
3366 seq_printf(seq, " resync=DELAYED\n ");
3368 seq_printf(seq, "\n ");
3370 if ((bitmap = mddev->bitmap)) {
3371 unsigned long chunk_kb;
3372 unsigned long flags;
3373 spin_lock_irqsave(&bitmap->lock, flags);
3374 chunk_kb = bitmap->chunksize >> 10;
3375 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
3377 bitmap->pages - bitmap->missing_pages,
3379 (bitmap->pages - bitmap->missing_pages)
3380 << (PAGE_SHIFT - 10),
3381 chunk_kb ? chunk_kb : bitmap->chunksize,
3382 chunk_kb ? "KB" : "B");
3384 seq_printf(seq, ", file: ");
3385 seq_path(seq, bitmap->file->f_vfsmnt,
3386 bitmap->file->f_dentry," \t\n");
3389 seq_printf(seq, "\n");
3390 spin_unlock_irqrestore(&bitmap->lock, flags);
3393 seq_printf(seq, "\n");
3395 mddev_unlock(mddev);
3400 static struct seq_operations md_seq_ops = {
3401 .start = md_seq_start,
3402 .next = md_seq_next,
3403 .stop = md_seq_stop,
3404 .show = md_seq_show,
3407 static int md_seq_open(struct inode *inode, struct file *file)
3411 error = seq_open(file, &md_seq_ops);
3415 static struct file_operations md_seq_fops = {
3416 .open = md_seq_open,
3418 .llseek = seq_lseek,
3419 .release = seq_release,
3422 int register_md_personality(int pnum, mdk_personality_t *p)
3424 if (pnum >= MAX_PERSONALITY) {
3426 "md: tried to install personality %s as nr %d, but max is %lu\n",
3427 p->name, pnum, MAX_PERSONALITY-1);
3431 spin_lock(&pers_lock);
3433 spin_unlock(&pers_lock);
3438 printk(KERN_INFO "md: %s personality registered as nr %d\n", p->name, pnum);
3439 spin_unlock(&pers_lock);
3443 int unregister_md_personality(int pnum)
3445 if (pnum >= MAX_PERSONALITY)
3448 printk(KERN_INFO "md: %s personality unregistered\n", pers[pnum]->name);
3449 spin_lock(&pers_lock);
3451 spin_unlock(&pers_lock);
3455 static int is_mddev_idle(mddev_t *mddev)
3458 struct list_head *tmp;
3460 unsigned long curr_events;
3463 ITERATE_RDEV(mddev,rdev,tmp) {
3464 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
3465 curr_events = disk_stat_read(disk, read_sectors) +
3466 disk_stat_read(disk, write_sectors) -
3467 atomic_read(&disk->sync_io);
3468 /* Allow some slack between valud of curr_events and last_events,
3469 * as there are some uninteresting races.
3470 * Note: the following is an unsigned comparison.
3472 if ((curr_events - rdev->last_events + 32) > 64) {
3473 rdev->last_events = curr_events;
3480 void md_done_sync(mddev_t *mddev, int blocks, int ok)
3482 /* another "blocks" (512byte) blocks have been synced */
3483 atomic_sub(blocks, &mddev->recovery_active);
3484 wake_up(&mddev->recovery_wait);
3486 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
3487 md_wakeup_thread(mddev->thread);
3488 // stop recovery, signal do_sync ....
3493 /* md_write_start(mddev, bi)
3494 * If we need to update some array metadata (e.g. 'active' flag
3495 * in superblock) before writing, schedule a superblock update
3496 * and wait for it to complete.
3498 void md_write_start(mddev_t *mddev, struct bio *bi)
3500 if (bio_data_dir(bi) != WRITE)
3503 atomic_inc(&mddev->writes_pending);
3504 if (mddev->in_sync) {
3505 spin_lock(&mddev->write_lock);
3506 if (mddev->in_sync) {
3508 mddev->sb_dirty = 1;
3509 md_wakeup_thread(mddev->thread);
3511 spin_unlock(&mddev->write_lock);
3513 wait_event(mddev->sb_wait, mddev->sb_dirty==0);
3516 void md_write_end(mddev_t *mddev)
3518 if (atomic_dec_and_test(&mddev->writes_pending)) {
3519 if (mddev->safemode == 2)
3520 md_wakeup_thread(mddev->thread);
3522 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
3526 static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
3528 #define SYNC_MARKS 10
3529 #define SYNC_MARK_STEP (3*HZ)
3530 static void md_do_sync(mddev_t *mddev)
3533 unsigned int currspeed = 0,
3535 sector_t max_sectors,j, io_sectors;
3536 unsigned long mark[SYNC_MARKS];
3537 sector_t mark_cnt[SYNC_MARKS];
3539 struct list_head *tmp;
3540 sector_t last_check;
3543 /* just incase thread restarts... */
3544 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
3547 /* we overload curr_resync somewhat here.
3548 * 0 == not engaged in resync at all
3549 * 2 == checking that there is no conflict with another sync
3550 * 1 == like 2, but have yielded to allow conflicting resync to
3552 * other == active in resync - this many blocks
3554 * Before starting a resync we must have set curr_resync to
3555 * 2, and then checked that every "conflicting" array has curr_resync
3556 * less than ours. When we find one that is the same or higher
3557 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
3558 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
3559 * This will mean we have to start checking from the beginning again.
3564 mddev->curr_resync = 2;
3567 if (signal_pending(current)) {
3568 flush_signals(current);
3571 ITERATE_MDDEV(mddev2,tmp) {
3572 if (mddev2 == mddev)
3574 if (mddev2->curr_resync &&
3575 match_mddev_units(mddev,mddev2)) {
3577 if (mddev < mddev2 && mddev->curr_resync == 2) {
3578 /* arbitrarily yield */
3579 mddev->curr_resync = 1;
3580 wake_up(&resync_wait);
3582 if (mddev > mddev2 && mddev->curr_resync == 1)
3583 /* no need to wait here, we can wait the next
3584 * time 'round when curr_resync == 2
3587 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
3588 if (!signal_pending(current)
3589 && mddev2->curr_resync >= mddev->curr_resync) {
3590 printk(KERN_INFO "md: delaying resync of %s"
3591 " until %s has finished resync (they"
3592 " share one or more physical units)\n",
3593 mdname(mddev), mdname(mddev2));
3596 finish_wait(&resync_wait, &wq);
3599 finish_wait(&resync_wait, &wq);
3602 } while (mddev->curr_resync < 2);
3604 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3605 /* resync follows the size requested by the personality,
3606 * which defaults to physical size, but can be virtual size
3608 max_sectors = mddev->resync_max_sectors;
3610 /* recovery follows the physical size of devices */
3611 max_sectors = mddev->size << 1;
3613 printk(KERN_INFO "md: syncing RAID array %s\n", mdname(mddev));
3614 printk(KERN_INFO "md: minimum _guaranteed_ reconstruction speed:"
3615 " %d KB/sec/disc.\n", sysctl_speed_limit_min);
3616 printk(KERN_INFO "md: using maximum available idle IO bandwith "
3617 "(but not more than %d KB/sec) for reconstruction.\n",
3618 sysctl_speed_limit_max);
3620 is_mddev_idle(mddev); /* this also initializes IO event counters */
3621 /* we don't use the checkpoint if there's a bitmap */
3622 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && !mddev->bitmap)
3623 j = mddev->recovery_cp;
3627 for (m = 0; m < SYNC_MARKS; m++) {
3629 mark_cnt[m] = io_sectors;
3632 mddev->resync_mark = mark[last_mark];
3633 mddev->resync_mark_cnt = mark_cnt[last_mark];
3636 * Tune reconstruction:
3638 window = 32*(PAGE_SIZE/512);
3639 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
3640 window/2,(unsigned long long) max_sectors/2);
3642 atomic_set(&mddev->recovery_active, 0);
3643 init_waitqueue_head(&mddev->recovery_wait);
3648 "md: resuming recovery of %s from checkpoint.\n",
3650 mddev->curr_resync = j;
3653 while (j < max_sectors) {
3657 sectors = mddev->pers->sync_request(mddev, j, &skipped,
3658 currspeed < sysctl_speed_limit_min);
3660 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
3664 if (!skipped) { /* actual IO requested */
3665 io_sectors += sectors;
3666 atomic_add(sectors, &mddev->recovery_active);
3670 if (j>1) mddev->curr_resync = j;
3673 if (last_check + window > io_sectors || j == max_sectors)
3676 last_check = io_sectors;
3678 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery) ||
3679 test_bit(MD_RECOVERY_ERR, &mddev->recovery))
3683 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
3685 int next = (last_mark+1) % SYNC_MARKS;
3687 mddev->resync_mark = mark[next];
3688 mddev->resync_mark_cnt = mark_cnt[next];
3689 mark[next] = jiffies;
3690 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
3695 if (signal_pending(current)) {
3697 * got a signal, exit.
3700 "md: md_do_sync() got signal ... exiting\n");
3701 flush_signals(current);
3702 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3707 * this loop exits only if either when we are slower than
3708 * the 'hard' speed limit, or the system was IO-idle for
3710 * the system might be non-idle CPU-wise, but we only care
3711 * about not overloading the IO subsystem. (things like an
3712 * e2fsck being done on the RAID array should execute fast)
3714 mddev->queue->unplug_fn(mddev->queue);
3717 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
3718 /((jiffies-mddev->resync_mark)/HZ +1) +1;
3720 if (currspeed > sysctl_speed_limit_min) {
3721 if ((currspeed > sysctl_speed_limit_max) ||
3722 !is_mddev_idle(mddev)) {
3723 msleep_interruptible(250);
3728 printk(KERN_INFO "md: %s: sync done.\n",mdname(mddev));
3730 * this also signals 'finished resyncing' to md_stop
3733 mddev->queue->unplug_fn(mddev->queue);
3735 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
3737 /* tell personality that we are finished */
3738 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
3740 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
3741 mddev->curr_resync > 2 &&
3742 mddev->curr_resync >= mddev->recovery_cp) {
3743 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
3745 "md: checkpointing recovery of %s.\n",
3747 mddev->recovery_cp = mddev->curr_resync;
3749 mddev->recovery_cp = MaxSector;
3753 mddev->curr_resync = 0;
3754 wake_up(&resync_wait);
3755 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
3756 md_wakeup_thread(mddev->thread);
3761 * This routine is regularly called by all per-raid-array threads to
3762 * deal with generic issues like resync and super-block update.
3763 * Raid personalities that don't have a thread (linear/raid0) do not
3764 * need this as they never do any recovery or update the superblock.
3766 * It does not do any resync itself, but rather "forks" off other threads
3767 * to do that as needed.
3768 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
3769 * "->recovery" and create a thread at ->sync_thread.
3770 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
3771 * and wakeups up this thread which will reap the thread and finish up.
3772 * This thread also removes any faulty devices (with nr_pending == 0).
3774 * The overall approach is:
3775 * 1/ if the superblock needs updating, update it.
3776 * 2/ If a recovery thread is running, don't do anything else.
3777 * 3/ If recovery has finished, clean up, possibly marking spares active.
3778 * 4/ If there are any faulty devices, remove them.
3779 * 5/ If array is degraded, try to add spares devices
3780 * 6/ If array has spares or is not in-sync, start a resync thread.
3782 void md_check_recovery(mddev_t *mddev)
3785 struct list_head *rtmp;
3789 bitmap_daemon_work(mddev->bitmap);
3794 if (signal_pending(current)) {
3795 if (mddev->pers->sync_request) {
3796 printk(KERN_INFO "md: %s in immediate safe mode\n",
3798 mddev->safemode = 2;
3800 flush_signals(current);
3805 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
3806 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
3807 (mddev->safemode == 1) ||
3808 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
3809 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
3813 if (mddev_trylock(mddev)==0) {
3816 spin_lock(&mddev->write_lock);
3817 if (mddev->safemode && !atomic_read(&mddev->writes_pending) &&
3818 !mddev->in_sync && mddev->recovery_cp == MaxSector) {
3820 mddev->sb_dirty = 1;
3822 if (mddev->safemode == 1)
3823 mddev->safemode = 0;
3824 spin_unlock(&mddev->write_lock);
3826 if (mddev->sb_dirty)
3827 md_update_sb(mddev);
3830 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
3831 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
3832 /* resync/recovery still happening */
3833 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3836 if (mddev->sync_thread) {
3837 /* resync has finished, collect result */
3838 md_unregister_thread(mddev->sync_thread);
3839 mddev->sync_thread = NULL;
3840 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
3841 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
3843 /* activate any spares */
3844 mddev->pers->spare_active(mddev);
3846 md_update_sb(mddev);
3848 /* if array is no-longer degraded, then any saved_raid_disk
3849 * information must be scrapped
3851 if (!mddev->degraded)
3852 ITERATE_RDEV(mddev,rdev,rtmp)
3853 rdev->saved_raid_disk = -1;
3855 mddev->recovery = 0;
3856 /* flag recovery needed just to double check */
3857 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3860 if (mddev->recovery)
3861 /* probably just the RECOVERY_NEEDED flag */
3862 mddev->recovery = 0;
3864 /* no recovery is running.
3865 * remove any failed drives, then
3866 * add spares if possible.
3867 * Spare are also removed and re-added, to allow
3868 * the personality to fail the re-add.
3870 ITERATE_RDEV(mddev,rdev,rtmp)
3871 if (rdev->raid_disk >= 0 &&
3872 (rdev->faulty || ! rdev->in_sync) &&
3873 atomic_read(&rdev->nr_pending)==0) {
3874 if (mddev->pers->hot_remove_disk(mddev, rdev->raid_disk)==0)
3875 rdev->raid_disk = -1;
3878 if (mddev->degraded) {
3879 ITERATE_RDEV(mddev,rdev,rtmp)
3880 if (rdev->raid_disk < 0
3882 if (mddev->pers->hot_add_disk(mddev,rdev))
3889 if (!spares && (mddev->recovery_cp == MaxSector )) {
3890 /* nothing we can do ... */
3893 if (mddev->pers->sync_request) {
3894 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3896 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3897 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
3898 /* We are adding a device or devices to an array
3899 * which has the bitmap stored on all devices.
3900 * So make sure all bitmap pages get written
3902 bitmap_write_all(mddev->bitmap);
3904 mddev->sync_thread = md_register_thread(md_do_sync,
3907 if (!mddev->sync_thread) {
3908 printk(KERN_ERR "%s: could not start resync"
3911 /* leave the spares where they are, it shouldn't hurt */
3912 mddev->recovery = 0;
3914 md_wakeup_thread(mddev->sync_thread);
3918 mddev_unlock(mddev);
3922 static int md_notify_reboot(struct notifier_block *this,
3923 unsigned long code, void *x)
3925 struct list_head *tmp;
3928 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
3930 printk(KERN_INFO "md: stopping all md devices.\n");
3932 ITERATE_MDDEV(mddev,tmp)
3933 if (mddev_trylock(mddev)==0)
3934 do_md_stop (mddev, 1);
3936 * certain more exotic SCSI devices are known to be
3937 * volatile wrt too early system reboots. While the
3938 * right place to handle this issue is the given
3939 * driver, we do want to have a safe RAID driver ...
3946 static struct notifier_block md_notifier = {
3947 .notifier_call = md_notify_reboot,
3949 .priority = INT_MAX, /* before any real devices */
3952 static void md_geninit(void)
3954 struct proc_dir_entry *p;
3956 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
3958 p = create_proc_entry("mdstat", S_IRUGO, NULL);
3960 p->proc_fops = &md_seq_fops;
3963 static int __init md_init(void)
3967 printk(KERN_INFO "md: md driver %d.%d.%d MAX_MD_DEVS=%d,"
3968 " MD_SB_DISKS=%d\n",
3969 MD_MAJOR_VERSION, MD_MINOR_VERSION,
3970 MD_PATCHLEVEL_VERSION, MAX_MD_DEVS, MD_SB_DISKS);
3971 printk(KERN_INFO "md: bitmap version %d.%d\n", BITMAP_MAJOR,
3974 if (register_blkdev(MAJOR_NR, "md"))
3976 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
3977 unregister_blkdev(MAJOR_NR, "md");
3981 blk_register_region(MKDEV(MAJOR_NR, 0), MAX_MD_DEVS, THIS_MODULE,
3982 md_probe, NULL, NULL);
3983 blk_register_region(MKDEV(mdp_major, 0), MAX_MD_DEVS<<MdpMinorShift, THIS_MODULE,
3984 md_probe, NULL, NULL);
3986 for (minor=0; minor < MAX_MD_DEVS; ++minor)
3987 devfs_mk_bdev(MKDEV(MAJOR_NR, minor),
3988 S_IFBLK|S_IRUSR|S_IWUSR,
3991 for (minor=0; minor < MAX_MD_DEVS; ++minor)
3992 devfs_mk_bdev(MKDEV(mdp_major, minor<<MdpMinorShift),
3993 S_IFBLK|S_IRUSR|S_IWUSR,
3997 register_reboot_notifier(&md_notifier);
3998 raid_table_header = register_sysctl_table(raid_root_table, 1);
4008 * Searches all registered partitions for autorun RAID arrays
4011 static dev_t detected_devices[128];
4014 void md_autodetect_dev(dev_t dev)
4016 if (dev_cnt >= 0 && dev_cnt < 127)
4017 detected_devices[dev_cnt++] = dev;
4021 static void autostart_arrays(int part)
4026 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
4028 for (i = 0; i < dev_cnt; i++) {
4029 dev_t dev = detected_devices[i];
4031 rdev = md_import_device(dev,0, 0);
4039 list_add(&rdev->same_set, &pending_raid_disks);
4043 autorun_devices(part);
4048 static __exit void md_exit(void)
4051 struct list_head *tmp;
4053 blk_unregister_region(MKDEV(MAJOR_NR,0), MAX_MD_DEVS);
4054 blk_unregister_region(MKDEV(mdp_major,0), MAX_MD_DEVS << MdpMinorShift);
4055 for (i=0; i < MAX_MD_DEVS; i++)
4056 devfs_remove("md/%d", i);
4057 for (i=0; i < MAX_MD_DEVS; i++)
4058 devfs_remove("md/d%d", i);
4062 unregister_blkdev(MAJOR_NR,"md");
4063 unregister_blkdev(mdp_major, "mdp");
4064 unregister_reboot_notifier(&md_notifier);
4065 unregister_sysctl_table(raid_table_header);
4066 remove_proc_entry("mdstat", NULL);
4067 ITERATE_MDDEV(mddev,tmp) {
4068 struct gendisk *disk = mddev->gendisk;
4071 export_array(mddev);
4074 mddev->gendisk = NULL;
4079 module_init(md_init)
4080 module_exit(md_exit)
4082 EXPORT_SYMBOL(register_md_personality);
4083 EXPORT_SYMBOL(unregister_md_personality);
4084 EXPORT_SYMBOL(md_error);
4085 EXPORT_SYMBOL(md_done_sync);
4086 EXPORT_SYMBOL(md_write_start);
4087 EXPORT_SYMBOL(md_write_end);
4088 EXPORT_SYMBOL(md_register_thread);
4089 EXPORT_SYMBOL(md_unregister_thread);
4090 EXPORT_SYMBOL(md_wakeup_thread);
4091 EXPORT_SYMBOL(md_print_devices);
4092 EXPORT_SYMBOL(md_check_recovery);
4093 MODULE_LICENSE("GPL");
4095 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);