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 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
11 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
12 - kmod support by: Cyrus Durgin
13 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
14 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
16 - lots of fixes and improvements to the RAID1/RAID5 and generic
17 RAID code (such as request based resynchronization):
19 Neil Brown <neilb@cse.unsw.edu.au>.
21 This program is free software; you can redistribute it and/or modify
22 it under the terms of the GNU General Public License as published by
23 the Free Software Foundation; either version 2, or (at your option)
26 You should have received a copy of the GNU General Public License
27 (for example /usr/src/linux/COPYING); if not, write to the Free
28 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 #include <linux/module.h>
32 #include <linux/config.h>
33 #include <linux/linkage.h>
34 #include <linux/raid/md.h>
35 #include <linux/sysctl.h>
36 #include <linux/bio.h>
37 #include <linux/devfs_fs_kernel.h>
38 #include <linux/buffer_head.h> /* for invalidate_bdev */
39 #include <linux/suspend.h>
41 #include <linux/init.h>
44 #include <linux/kmod.h>
47 #define __KERNEL_SYSCALLS__
48 #include <linux/unistd.h>
50 #include <asm/unaligned.h>
52 #define MAJOR_NR MD_MAJOR
54 #define DEVICE_NR(device) (minor(device))
56 #include <linux/blk.h>
59 #define dprintk(x...) ((void)(DEBUG && printk(x)))
63 static void autostart_arrays (void);
66 static mdk_personality_t *pers[MAX_PERSONALITY];
67 static spinlock_t pers_lock = SPIN_LOCK_UNLOCKED;
70 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
71 * is 1000 KB/sec, so the extra system load does not show up that much.
72 * Increase it if you want to have more _guaranteed_ speed. Note that
73 * the RAID driver will use the maximum available bandwith if the IO
74 * subsystem is idle. There is also an 'absolute maximum' reconstruction
75 * speed limit - in case reconstruction slows down your system despite
78 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
81 static int sysctl_speed_limit_min = 1000;
82 static int sysctl_speed_limit_max = 200000;
84 static struct ctl_table_header *raid_table_header;
86 static ctl_table raid_table[] = {
88 .ctl_name = DEV_RAID_SPEED_LIMIT_MIN,
89 .procname = "speed_limit_min",
90 .data = &sysctl_speed_limit_min,
91 .maxlen = sizeof(int),
93 .proc_handler = &proc_dointvec,
96 .ctl_name = DEV_RAID_SPEED_LIMIT_MAX,
97 .procname = "speed_limit_max",
98 .data = &sysctl_speed_limit_max,
99 .maxlen = sizeof(int),
101 .proc_handler = &proc_dointvec,
106 static ctl_table raid_dir_table[] = {
108 .ctl_name = DEV_RAID,
117 static ctl_table raid_root_table[] = {
123 .child = raid_dir_table,
128 static struct block_device_operations md_fops;
130 static struct gendisk *disks[MAX_MD_DEVS];
133 * Enables to iterate over all existing md arrays
134 * all_mddevs_lock protects this list as well as mddev_map.
136 static LIST_HEAD(all_mddevs);
137 static spinlock_t all_mddevs_lock = SPIN_LOCK_UNLOCKED;
141 * iterates through all used mddevs in the system.
142 * We take care to grab the all_mddevs_lock whenever navigating
143 * the list, and to always hold a refcount when unlocked.
144 * Any code which breaks out of this loop while own
145 * a reference to the current mddev and must mddev_put it.
147 #define ITERATE_MDDEV(mddev,tmp) \
149 for (({ spin_lock(&all_mddevs_lock); \
150 tmp = all_mddevs.next; \
152 ({ if (tmp != &all_mddevs) \
153 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
154 spin_unlock(&all_mddevs_lock); \
155 if (mddev) mddev_put(mddev); \
156 mddev = list_entry(tmp, mddev_t, all_mddevs); \
157 tmp != &all_mddevs;}); \
158 ({ spin_lock(&all_mddevs_lock); \
162 static mddev_t *mddev_map[MAX_MD_DEVS];
164 static int md_fail_request (request_queue_t *q, struct bio *bio)
166 bio_io_error(bio, bio->bi_size);
170 static inline mddev_t *mddev_get(mddev_t *mddev)
172 atomic_inc(&mddev->active);
176 static void mddev_put(mddev_t *mddev)
178 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
180 if (!mddev->raid_disks && list_empty(&mddev->disks)) {
181 list_del(&mddev->all_mddevs);
182 mddev_map[mdidx(mddev)] = NULL;
186 spin_unlock(&all_mddevs_lock);
189 static mddev_t * mddev_find(int unit)
191 mddev_t *mddev, *new = NULL;
194 spin_lock(&all_mddevs_lock);
195 if (mddev_map[unit]) {
196 mddev = mddev_get(mddev_map[unit]);
197 spin_unlock(&all_mddevs_lock);
203 mddev_map[unit] = new;
204 list_add(&new->all_mddevs, &all_mddevs);
205 spin_unlock(&all_mddevs_lock);
209 spin_unlock(&all_mddevs_lock);
211 new = (mddev_t *) kmalloc(sizeof(*new), GFP_KERNEL);
215 memset(new, 0, sizeof(*new));
218 init_MUTEX(&new->reconfig_sem);
219 INIT_LIST_HEAD(&new->disks);
220 INIT_LIST_HEAD(&new->all_mddevs);
221 init_timer(&new->safemode_timer);
222 atomic_set(&new->active, 1);
223 blk_queue_make_request(&new->queue, md_fail_request);
228 static inline int mddev_lock(mddev_t * mddev)
230 return down_interruptible(&mddev->reconfig_sem);
233 static inline void mddev_lock_uninterruptible(mddev_t * mddev)
235 down(&mddev->reconfig_sem);
238 static inline int mddev_trylock(mddev_t * mddev)
240 return down_trylock(&mddev->reconfig_sem);
243 static inline void mddev_unlock(mddev_t * mddev)
245 up(&mddev->reconfig_sem);
248 mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
251 struct list_head *tmp;
253 ITERATE_RDEV(mddev,rdev,tmp) {
254 if (rdev->desc_nr == nr)
260 static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
262 struct list_head *tmp;
265 ITERATE_RDEV(mddev,rdev,tmp) {
266 if (rdev->bdev->bd_dev == dev)
272 inline static sector_t calc_dev_sboffset(struct block_device *bdev)
274 sector_t size = bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
275 return MD_NEW_SIZE_BLOCKS(size);
278 static sector_t calc_dev_size(mdk_rdev_t *rdev, unsigned chunk_size)
282 size = rdev->sb_offset;
285 size &= ~((sector_t)chunk_size/1024 - 1);
289 static int alloc_disk_sb(mdk_rdev_t * rdev)
294 rdev->sb_page = alloc_page(GFP_KERNEL);
295 if (!rdev->sb_page) {
296 printk(KERN_ALERT "md: out of memory.\n");
303 static void free_disk_sb(mdk_rdev_t * rdev)
306 page_cache_release(rdev->sb_page);
308 rdev->sb_page = NULL;
315 static int bi_complete(struct bio *bio, unsigned int bytes_done, int error)
320 complete((struct completion*)bio->bi_private);
324 static int sync_page_io(struct block_device *bdev, sector_t sector, int size,
325 struct page *page, int rw)
329 struct completion event;
332 bio.bi_io_vec = &vec;
340 bio.bi_sector = sector;
341 init_completion(&event);
342 bio.bi_private = &event;
343 bio.bi_end_io = bi_complete;
344 submit_bio(rw, &bio);
346 wait_for_completion(&event);
348 return test_bit(BIO_UPTODATE, &bio.bi_flags);
351 static int read_disk_sb(mdk_rdev_t * rdev)
354 if (!rdev->sb_page) {
362 if (!sync_page_io(rdev->bdev, rdev->sb_offset<<1, MD_SB_BYTES, rdev->sb_page, READ))
368 printk(KERN_ERR "md: disabled device %s, could not read superblock.\n",
369 bdev_partition_name(rdev->bdev));
373 static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
375 if ( (sb1->set_uuid0 == sb2->set_uuid0) &&
376 (sb1->set_uuid1 == sb2->set_uuid1) &&
377 (sb1->set_uuid2 == sb2->set_uuid2) &&
378 (sb1->set_uuid3 == sb2->set_uuid3))
386 static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
389 mdp_super_t *tmp1, *tmp2;
391 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
392 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
394 if (!tmp1 || !tmp2) {
396 printk(KERN_INFO "md.c: sb1 is not equal to sb2!\n");
404 * nr_disks is not constant
409 if (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4))
423 static unsigned int calc_sb_csum(mdp_super_t * sb)
425 unsigned int disk_csum, csum;
427 disk_csum = sb->sb_csum;
429 csum = csum_partial((void *)sb, MD_SB_BYTES, 0);
430 sb->sb_csum = disk_csum;
435 * Handle superblock details.
436 * We want to be able to handle multiple superblock formats
437 * so we have a common interface to them all, and an array of
438 * different handlers.
439 * We rely on user-space to write the initial superblock, and support
440 * reading and updating of superblocks.
441 * Interface methods are:
442 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
443 * loads and validates a superblock on dev.
444 * if refdev != NULL, compare superblocks on both devices
446 * 0 - dev has a superblock that is compatible with refdev
447 * 1 - dev has a superblock that is compatible and newer than refdev
448 * so dev should be used as the refdev in future
449 * -EINVAL superblock incompatible or invalid
450 * -othererror e.g. -EIO
452 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
453 * Verify that dev is acceptable into mddev.
454 * The first time, mddev->raid_disks will be 0, and data from
455 * dev should be merged in. Subsequent calls check that dev
456 * is new enough. Return 0 or -EINVAL
458 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
459 * Update the superblock for rdev with data in mddev
460 * This does not write to disc.
466 struct module *owner;
467 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version);
468 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
469 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
473 * load_super for 0.90.0
475 static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
482 * Calculate the position of the superblock,
483 * it's at the end of the disk.
485 * It also happens to be a multiple of 4Kb.
487 sb_offset = calc_dev_sboffset(rdev->bdev);
488 rdev->sb_offset = sb_offset;
490 ret = read_disk_sb(rdev);
495 sb = (mdp_super_t*)page_address(rdev->sb_page);
497 if (sb->md_magic != MD_SB_MAGIC) {
498 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
499 bdev_partition_name(rdev->bdev));
503 if (sb->major_version != 0 ||
504 sb->minor_version != 90) {
505 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
506 sb->major_version, sb->minor_version,
507 bdev_partition_name(rdev->bdev));
511 if (sb->md_minor >= MAX_MD_DEVS) {
512 printk(KERN_ERR "md: %s: invalid raid minor (%x)\n",
513 bdev_partition_name(rdev->bdev), sb->md_minor);
516 if (sb->raid_disks <= 0)
519 if (calc_sb_csum(sb) != sb->sb_csum) {
520 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
521 bdev_partition_name(rdev->bdev));
525 rdev->preferred_minor = sb->md_minor;
526 rdev->data_offset = 0;
528 if (sb->level == MULTIPATH)
531 rdev->desc_nr = sb->this_disk.number;
537 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
538 if (!uuid_equal(refsb, sb)) {
539 printk(KERN_WARNING "md: %s has different UUID to %s\n",
540 bdev_partition_name(rdev->bdev),
541 bdev_partition_name(refdev->bdev));
544 if (!sb_equal(refsb, sb)) {
545 printk(KERN_WARNING "md: %s has same UUID"
546 " but different superblock to %s\n",
547 bdev_partition_name(rdev->bdev),
548 bdev_partition_name(refdev->bdev));
552 ev2 = md_event(refsb);
558 rdev->size = calc_dev_size(rdev, sb->chunk_size);
565 * validate_super for 0.90.0
567 static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
570 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
572 if (mddev->raid_disks == 0) {
573 mddev->major_version = 0;
574 mddev->minor_version = sb->minor_version;
575 mddev->patch_version = sb->patch_version;
576 mddev->persistent = ! sb->not_persistent;
577 mddev->chunk_size = sb->chunk_size;
578 mddev->ctime = sb->ctime;
579 mddev->utime = sb->utime;
580 mddev->level = sb->level;
581 mddev->layout = sb->layout;
582 mddev->raid_disks = sb->raid_disks;
583 mddev->size = sb->size;
584 mddev->events = md_event(sb);
586 if (sb->state & (1<<MD_SB_CLEAN))
587 mddev->recovery_cp = MaxSector;
589 if (sb->events_hi == sb->cp_events_hi &&
590 sb->events_lo == sb->cp_events_lo) {
591 mddev->recovery_cp = sb->recovery_cp;
593 mddev->recovery_cp = 0;
596 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
597 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
598 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
599 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
601 mddev->max_disks = MD_SB_DISKS;
606 if (ev1 < mddev->events)
609 if (mddev->level != LEVEL_MULTIPATH) {
610 rdev->raid_disk = -1;
611 rdev->in_sync = rdev->faulty = 0;
612 desc = sb->disks + rdev->desc_nr;
614 if (desc->state & (1<<MD_DISK_FAULTY))
616 else if (desc->state & (1<<MD_DISK_SYNC) &&
617 desc->raid_disk < mddev->raid_disks) {
619 rdev->raid_disk = desc->raid_disk;
626 * sync_super for 0.90.0
628 static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
631 struct list_head *tmp;
633 int next_spare = mddev->raid_disks;
635 /* make rdev->sb match mddev data..
638 * 2/ Add info for each disk, keeping track of highest desc_nr
639 * 3/ any empty disks < highest become removed
641 * disks[0] gets initialised to REMOVED because
642 * we cannot be sure from other fields if it has
643 * been initialised or not.
647 int active=0, working=0,failed=0,spare=0,nr_disks=0;
649 sb = (mdp_super_t*)page_address(rdev->sb_page);
651 memset(sb, 0, sizeof(*sb));
653 sb->md_magic = MD_SB_MAGIC;
654 sb->major_version = mddev->major_version;
655 sb->minor_version = mddev->minor_version;
656 sb->patch_version = mddev->patch_version;
657 sb->gvalid_words = 0; /* ignored */
658 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
659 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
660 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
661 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
663 sb->ctime = mddev->ctime;
664 sb->level = mddev->level;
665 sb->size = mddev->size;
666 sb->raid_disks = mddev->raid_disks;
667 sb->md_minor = mddev->__minor;
668 sb->not_persistent = !mddev->persistent;
669 sb->utime = mddev->utime;
671 sb->events_hi = (mddev->events>>32);
672 sb->events_lo = (u32)mddev->events;
676 sb->recovery_cp = mddev->recovery_cp;
677 sb->cp_events_hi = (mddev->events>>32);
678 sb->cp_events_lo = (u32)mddev->events;
679 if (mddev->recovery_cp == MaxSector)
680 sb->state = (1<< MD_SB_CLEAN);
684 sb->layout = mddev->layout;
685 sb->chunk_size = mddev->chunk_size;
687 sb->disks[0].state = (1<<MD_DISK_REMOVED);
688 ITERATE_RDEV(mddev,rdev2,tmp) {
690 if (rdev2->raid_disk >= 0 && rdev2->in_sync && !rdev2->faulty)
691 rdev2->desc_nr = rdev2->raid_disk;
693 rdev2->desc_nr = next_spare++;
694 d = &sb->disks[rdev2->desc_nr];
696 d->number = rdev2->desc_nr;
697 d->major = MAJOR(rdev2->bdev->bd_dev);
698 d->minor = MINOR(rdev2->bdev->bd_dev);
699 if (rdev2->raid_disk >= 0 && rdev->in_sync && !rdev2->faulty)
700 d->raid_disk = rdev2->raid_disk;
702 d->raid_disk = rdev2->desc_nr; /* compatibility */
704 d->state = (1<<MD_DISK_FAULTY);
706 } else if (rdev2->in_sync) {
707 d->state = (1<<MD_DISK_ACTIVE);
708 d->state |= (1<<MD_DISK_SYNC);
716 if (rdev2->desc_nr > highest)
717 highest = rdev2->desc_nr;
720 /* now set the "removed" bit on any non-trailing holes */
721 for (i=0; i<highest; i++) {
722 mdp_disk_t *d = &sb->disks[i];
723 if (d->state == 0 && d->number == 0) {
726 d->state = (1<<MD_DISK_REMOVED);
729 sb->nr_disks = nr_disks;
730 sb->active_disks = active;
731 sb->working_disks = working;
732 sb->failed_disks = failed;
733 sb->spare_disks = spare;
735 sb->this_disk = sb->disks[rdev->desc_nr];
736 sb->sb_csum = calc_sb_csum(sb);
740 * version 1 superblock
743 static unsigned int calc_sb_1_csum(struct mdp_superblock_1 * sb)
745 unsigned int disk_csum, csum;
746 int size = 256 + sb->max_dev*2;
748 disk_csum = sb->sb_csum;
750 csum = csum_partial((void *)sb, size, 0);
751 sb->sb_csum = disk_csum;
755 static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
757 struct mdp_superblock_1 *sb;
762 * Calculate the position of the superblock.
763 * It is always aligned to a 4K boundary and
764 * depeding on minor_version, it can be:
765 * 0: At least 8K, but less than 12K, from end of device
766 * 1: At start of device
767 * 2: 4K from start of device.
769 switch(minor_version) {
771 sb_offset = rdev->bdev->bd_inode->i_size >> 9;
774 /* convert from sectors to K */
786 rdev->sb_offset = sb_offset;
788 ret = read_disk_sb(rdev);
792 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
794 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
795 sb->major_version != cpu_to_le32(1) ||
796 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
797 le64_to_cpu(sb->super_offset) != (rdev->sb_offset<<1) ||
798 sb->feature_map != 0)
801 if (calc_sb_1_csum(sb) != sb->sb_csum) {
802 printk("md: invalid superblock checksum on %s\n",
803 bdev_partition_name(rdev->bdev));
806 rdev->preferred_minor = 0xffff;
807 rdev->data_offset = le64_to_cpu(sb->data_offset);
813 struct mdp_superblock_1 *refsb =
814 (struct mdp_superblock_1*)page_address(refdev->sb_page);
816 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
817 sb->level != refsb->level ||
818 sb->layout != refsb->layout ||
819 sb->chunksize != refsb->chunksize) {
820 printk(KERN_WARNING "md: %s has strangely different"
821 " superblock to %s\n",
822 bdev_partition_name(rdev->bdev),
823 bdev_partition_name(refdev->bdev));
826 ev1 = le64_to_cpu(sb->events);
827 ev2 = le64_to_cpu(refsb->events);
833 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
835 rdev->size = rdev->sb_offset;
836 if (rdev->size < le64_to_cpu(sb->data_size)/2)
838 rdev->size = le64_to_cpu(sb->data_size)/2;
839 if (le32_to_cpu(sb->chunksize))
840 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
844 static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
846 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
848 if (mddev->raid_disks == 0) {
849 mddev->major_version = 1;
850 mddev->minor_version = 0;
851 mddev->patch_version = 0;
852 mddev->persistent = 1;
853 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
854 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
855 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
856 mddev->level = le32_to_cpu(sb->level);
857 mddev->layout = le32_to_cpu(sb->layout);
858 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
859 mddev->size = (u32)le64_to_cpu(sb->size);
860 mddev->events = le64_to_cpu(sb->events);
862 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
863 memcpy(mddev->uuid, sb->set_uuid, 16);
865 mddev->max_disks = (4096-256)/2;
868 ev1 = le64_to_cpu(sb->events);
870 if (ev1 < mddev->events)
874 if (mddev->level != LEVEL_MULTIPATH) {
876 rdev->desc_nr = le32_to_cpu(sb->dev_number);
877 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
879 case 0xffff: /* spare */
882 rdev->raid_disk = -1;
884 case 0xfffe: /* faulty */
887 rdev->raid_disk = -1;
892 rdev->raid_disk = role;
899 static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
901 struct mdp_superblock_1 *sb;
902 struct list_head *tmp;
905 /* make rdev->sb match mddev and rdev data. */
907 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
911 memset(sb->pad1, 0, sizeof(sb->pad1));
912 memset(sb->pad2, 0, sizeof(sb->pad2));
913 memset(sb->pad3, 0, sizeof(sb->pad3));
915 sb->utime = cpu_to_le64((__u64)mddev->utime);
916 sb->events = cpu_to_le64(mddev->events);
918 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
920 sb->resync_offset = cpu_to_le64(0);
923 ITERATE_RDEV(mddev,rdev2,tmp)
924 if (rdev2->desc_nr > max_dev)
925 max_dev = rdev2->desc_nr;
927 sb->max_dev = max_dev;
928 for (i=0; i<max_dev;i++)
929 sb->dev_roles[max_dev] = cpu_to_le16(0xfffe);
931 ITERATE_RDEV(mddev,rdev2,tmp) {
934 sb->dev_roles[i] = cpu_to_le16(0xfffe);
935 else if (rdev2->in_sync)
936 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
938 sb->dev_roles[i] = cpu_to_le16(0xffff);
941 sb->recovery_offset = cpu_to_le64(0); /* not supported yet */
945 struct super_type super_types[] = {
948 .owner = THIS_MODULE,
949 .load_super = super_90_load,
950 .validate_super = super_90_validate,
951 .sync_super = super_90_sync,
955 .owner = THIS_MODULE,
956 .load_super = super_1_load,
957 .validate_super = super_1_validate,
958 .sync_super = super_1_sync,
962 static mdk_rdev_t * match_dev_unit(mddev_t *mddev, mdk_rdev_t *dev)
964 struct list_head *tmp;
967 ITERATE_RDEV(mddev,rdev,tmp)
968 if (rdev->bdev->bd_contains == dev->bdev->bd_contains)
974 static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
976 struct list_head *tmp;
979 ITERATE_RDEV(mddev1,rdev,tmp)
980 if (match_dev_unit(mddev2, rdev))
986 static LIST_HEAD(pending_raid_disks);
988 static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
990 mdk_rdev_t *same_pdev;
996 same_pdev = match_dev_unit(mddev, rdev);
999 "md%d: WARNING: %s appears to be on the same physical"
1000 " disk as %s. True\n protection against single-disk"
1001 " failure might be compromised.\n",
1002 mdidx(mddev), bdev_partition_name(rdev->bdev),
1003 bdev_partition_name(same_pdev->bdev));
1005 /* Verify rdev->desc_nr is unique.
1006 * If it is -1, assign a free number, else
1007 * check number is not in use
1009 if (rdev->desc_nr < 0) {
1011 if (mddev->pers) choice = mddev->raid_disks;
1012 while (find_rdev_nr(mddev, choice))
1014 rdev->desc_nr = choice;
1016 if (find_rdev_nr(mddev, rdev->desc_nr))
1020 list_add(&rdev->same_set, &mddev->disks);
1021 rdev->mddev = mddev;
1022 printk(KERN_INFO "md: bind<%s>\n", bdev_partition_name(rdev->bdev));
1026 static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1032 list_del_init(&rdev->same_set);
1033 printk(KERN_INFO "md: unbind<%s>\n", bdev_partition_name(rdev->bdev));
1038 * prevent the device from being mounted, repartitioned or
1039 * otherwise reused by a RAID array (or any other kernel
1040 * subsystem), by opening the device. [simply getting an
1041 * inode is not enough, the SCSI module usage code needs
1042 * an explicit open() on the device]
1044 static int lock_rdev(mdk_rdev_t *rdev, dev_t dev)
1047 struct block_device *bdev;
1052 err = blkdev_get(bdev, FMODE_READ|FMODE_WRITE, 0, BDEV_RAW);
1055 err = bd_claim(bdev, rdev);
1057 blkdev_put(bdev, BDEV_RAW);
1064 static void unlock_rdev(mdk_rdev_t *rdev)
1066 struct block_device *bdev = rdev->bdev;
1071 blkdev_put(bdev, BDEV_RAW);
1074 void md_autodetect_dev(dev_t dev);
1076 static void export_rdev(mdk_rdev_t * rdev)
1078 printk(KERN_INFO "md: export_rdev(%s)\n",
1079 bdev_partition_name(rdev->bdev));
1083 list_del_init(&rdev->same_set);
1085 md_autodetect_dev(rdev->bdev->bd_dev);
1091 static void kick_rdev_from_array(mdk_rdev_t * rdev)
1093 unbind_rdev_from_array(rdev);
1097 static void export_array(mddev_t *mddev)
1099 struct list_head *tmp;
1102 ITERATE_RDEV(mddev,rdev,tmp) {
1107 kick_rdev_from_array(rdev);
1109 if (!list_empty(&mddev->disks))
1111 mddev->raid_disks = 0;
1112 mddev->major_version = 0;
1115 static void print_desc(mdp_disk_t *desc)
1117 printk(" DISK<N:%d,%s(%d,%d),R:%d,S:%d>\n", desc->number,
1118 partition_name(MKDEV(desc->major,desc->minor)),
1119 desc->major,desc->minor,desc->raid_disk,desc->state);
1122 static void print_sb(mdp_super_t *sb)
1127 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1128 sb->major_version, sb->minor_version, sb->patch_version,
1129 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1131 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1132 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1133 sb->md_minor, sb->layout, sb->chunk_size);
1134 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1135 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1136 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1137 sb->failed_disks, sb->spare_disks,
1138 sb->sb_csum, (unsigned long)sb->events_lo);
1141 for (i = 0; i < MD_SB_DISKS; i++) {
1144 desc = sb->disks + i;
1145 if (desc->number || desc->major || desc->minor ||
1146 desc->raid_disk || (desc->state && (desc->state != 4))) {
1147 printk(" D %2d: ", i);
1151 printk(KERN_INFO "md: THIS: ");
1152 print_desc(&sb->this_disk);
1156 static void print_rdev(mdk_rdev_t *rdev)
1158 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%d ",
1159 bdev_partition_name(rdev->bdev), (unsigned long long)rdev->size,
1160 rdev->faulty, rdev->in_sync, rdev->desc_nr);
1161 if (rdev->sb_loaded) {
1162 printk(KERN_INFO "md: rdev superblock:\n");
1163 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1165 printk(KERN_INFO "md: no rdev superblock!\n");
1168 void md_print_devices(void)
1170 struct list_head *tmp, *tmp2;
1175 printk("md: **********************************\n");
1176 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1177 printk("md: **********************************\n");
1178 ITERATE_MDDEV(mddev,tmp) {
1179 printk("md%d: ", mdidx(mddev));
1181 ITERATE_RDEV(mddev,rdev,tmp2)
1182 printk("<%s>", bdev_partition_name(rdev->bdev));
1184 ITERATE_RDEV(mddev,rdev,tmp2)
1187 printk("md: **********************************\n");
1192 static int write_disk_sb(mdk_rdev_t * rdev)
1195 if (!rdev->sb_loaded) {
1204 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1205 bdev_partition_name(rdev->bdev),
1206 (unsigned long long)rdev->sb_offset);
1208 if (sync_page_io(rdev->bdev, rdev->sb_offset<<1, MD_SB_BYTES, rdev->sb_page, WRITE))
1211 printk("md: write_disk_sb failed for device %s\n",
1212 bdev_partition_name(rdev->bdev));
1216 static void sync_sbs(mddev_t * mddev)
1219 struct list_head *tmp;
1221 ITERATE_RDEV(mddev,rdev,tmp) {
1222 super_types[mddev->major_version].
1223 sync_super(mddev, rdev);
1224 rdev->sb_loaded = 1;
1228 static void md_update_sb(mddev_t * mddev)
1230 int err, count = 100;
1231 struct list_head *tmp;
1234 mddev->sb_dirty = 0;
1236 mddev->utime = get_seconds();
1239 if (!mddev->events) {
1241 * oops, this 64-bit counter should never wrap.
1242 * Either we are in around ~1 trillion A.C., assuming
1243 * 1 reboot per second, or we have a bug:
1251 * do not write anything to disk if using
1252 * nonpersistent superblocks
1254 if (!mddev->persistent)
1258 "md: updating md%d RAID superblock on device (in sync %d)\n",
1259 mdidx(mddev),mddev->in_sync);
1262 ITERATE_RDEV(mddev,rdev,tmp) {
1263 dprintk(KERN_INFO "md: ");
1265 dprintk("(skipping faulty ");
1267 dprintk("%s ", bdev_partition_name(rdev->bdev));
1268 if (!rdev->faulty) {
1269 err += write_disk_sb(rdev);
1272 if (!err && mddev->level == LEVEL_MULTIPATH)
1273 /* only need to write one superblock... */
1278 printk(KERN_ERR "md: errors occurred during superblock"
1279 " update, repeating\n");
1283 "md: excessive errors occurred during superblock update, exiting\n");
1288 * Import a device. If 'super_format' >= 0, then sanity check the superblock
1290 * mark the device faulty if:
1292 * - the device is nonexistent (zero size)
1293 * - the device has no valid superblock
1295 * a faulty rdev _never_ has rdev->sb set.
1297 static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
1303 rdev = (mdk_rdev_t *) kmalloc(sizeof(*rdev), GFP_KERNEL);
1305 printk(KERN_ERR "md: could not alloc mem for %s!\n",
1306 partition_name(newdev));
1307 return ERR_PTR(-ENOMEM);
1309 memset(rdev, 0, sizeof(*rdev));
1311 if ((err = alloc_disk_sb(rdev)))
1314 err = lock_rdev(rdev, newdev);
1316 printk(KERN_ERR "md: could not lock %s.\n",
1317 partition_name(newdev));
1323 rdev->data_offset = 0;
1324 atomic_set(&rdev->nr_pending, 0);
1326 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
1329 "md: %s has zero or unknown size, marking faulty!\n",
1330 bdev_partition_name(rdev->bdev));
1335 if (super_format >= 0) {
1336 err = super_types[super_format].
1337 load_super(rdev, NULL, super_minor);
1338 if (err == -EINVAL) {
1340 "md: %s has invalid sb, not importing!\n",
1341 bdev_partition_name(rdev->bdev));
1346 "md: could not read %s's sb, not importing!\n",
1347 bdev_partition_name(rdev->bdev));
1351 INIT_LIST_HEAD(&rdev->same_set);
1356 if (rdev->sb_page) {
1362 return ERR_PTR(err);
1366 * Check a full RAID array for plausibility
1370 static int analyze_sbs(mddev_t * mddev)
1373 struct list_head *tmp;
1374 mdk_rdev_t *rdev, *freshest;
1377 ITERATE_RDEV(mddev,rdev,tmp)
1378 switch (super_types[mddev->major_version].
1379 load_super(rdev, freshest, mddev->minor_version)) {
1387 "md: fatal superblock inconsistency in %s"
1388 " -- removing from array\n",
1389 bdev_partition_name(rdev->bdev));
1390 kick_rdev_from_array(rdev);
1394 super_types[mddev->major_version].
1395 validate_super(mddev, freshest);
1398 ITERATE_RDEV(mddev,rdev,tmp) {
1399 if (rdev != freshest)
1400 if (super_types[mddev->major_version].
1401 validate_super(mddev, rdev)) {
1402 printk(KERN_WARNING "md: kicking non-fresh %s"
1404 bdev_partition_name(rdev->bdev));
1405 kick_rdev_from_array(rdev);
1408 if (mddev->level == LEVEL_MULTIPATH) {
1409 rdev->desc_nr = i++;
1410 rdev->raid_disk = rdev->desc_nr;
1417 * Check if we can support this RAID array
1419 if (mddev->major_version != MD_MAJOR_VERSION ||
1420 mddev->minor_version > MD_MINOR_VERSION) {
1422 "md: md%d: unsupported raid array version %d.%d.%d\n",
1423 mdidx(mddev), mddev->major_version,
1424 mddev->minor_version, mddev->patch_version);
1428 if ((mddev->recovery_cp != MaxSector) && ((mddev->level == 1) ||
1429 (mddev->level == 4) || (mddev->level == 5)))
1430 printk(KERN_ERR "md: md%d: raid array is not clean"
1431 " -- starting background reconstruction\n",
1439 static struct gendisk *md_probe(dev_t dev, int *part, void *data)
1441 static DECLARE_MUTEX(disks_sem);
1442 int unit = MINOR(dev);
1443 mddev_t *mddev = mddev_find(unit);
1444 struct gendisk *disk;
1455 disk = alloc_disk(1);
1461 disk->major = MD_MAJOR;
1462 disk->first_minor = mdidx(mddev);
1463 sprintf(disk->disk_name, "md%d", mdidx(mddev));
1464 disk->fops = &md_fops;
1465 disk->private_data = mddev;
1466 disk->queue = &mddev->queue;
1468 disks[mdidx(mddev)] = disk;
1473 void md_wakeup_thread(mdk_thread_t *thread);
1475 static void md_safemode_timeout(unsigned long data)
1477 mddev_t *mddev = (mddev_t *) data;
1479 mddev->safemode = 1;
1480 md_wakeup_thread(mddev->thread);
1484 static int do_md_run(mddev_t * mddev)
1488 struct list_head *tmp;
1490 struct gendisk *disk;
1492 if (list_empty(&mddev->disks)) {
1501 * Analyze all RAID superblock(s)
1503 if (!mddev->raid_disks && analyze_sbs(mddev)) {
1508 chunk_size = mddev->chunk_size;
1509 pnum = level_to_pers(mddev->level);
1511 if ((pnum != MULTIPATH) && (pnum != RAID1)) {
1514 * 'default chunksize' in the old md code used to
1515 * be PAGE_SIZE, baaad.
1516 * we abort here to be on the safe side. We don't
1517 * want to continue the bad practice.
1520 "no chunksize specified, see 'man raidtab'\n");
1523 if (chunk_size > MAX_CHUNK_SIZE) {
1524 printk(KERN_ERR "too big chunk_size: %d > %d\n",
1525 chunk_size, MAX_CHUNK_SIZE);
1529 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
1531 if ( (1 << ffz(~chunk_size)) != chunk_size) {
1535 if (chunk_size < PAGE_SIZE) {
1536 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
1537 chunk_size, PAGE_SIZE);
1541 /* devices must have minimum size of one chunk */
1542 ITERATE_RDEV(mddev,rdev,tmp) {
1545 if (rdev->size < chunk_size / 1024) {
1547 "md: Dev %s smaller than chunk_size:"
1549 bdev_partition_name(rdev->bdev),
1550 (unsigned long long)rdev->size,
1556 if (pnum >= MAX_PERSONALITY) {
1564 char module_name[80];
1565 sprintf (module_name, "md-personality-%d", pnum);
1566 request_module (module_name);
1571 * Drop all container device buffers, from now on
1572 * the only valid external interface is through the md
1574 * Also find largest hardsector size
1576 ITERATE_RDEV(mddev,rdev,tmp) {
1579 sync_blockdev(rdev->bdev);
1580 invalidate_bdev(rdev->bdev, 0);
1583 md_probe(mdidx(mddev), NULL, NULL);
1584 disk = disks[mdidx(mddev)];
1588 spin_lock(&pers_lock);
1589 if (!pers[pnum] || !try_module_get(pers[pnum]->owner)) {
1590 spin_unlock(&pers_lock);
1591 printk(KERN_ERR "md: personality %d is not loaded!\n",
1596 mddev->pers = pers[pnum];
1597 spin_unlock(&pers_lock);
1599 blk_queue_make_request(&mddev->queue, mddev->pers->make_request);
1600 printk("%s: setting max_sectors to %d, segment boundary to %d\n",
1604 blk_queue_max_sectors(&mddev->queue, chunk_size >> 9);
1605 blk_queue_segment_boundary(&mddev->queue, (chunk_size>>1) - 1);
1606 mddev->queue.queuedata = mddev;
1608 err = mddev->pers->run(mddev);
1610 printk(KERN_ERR "md: pers->run() failed ...\n");
1611 module_put(mddev->pers->owner);
1615 atomic_set(&mddev->writes_pending,0);
1616 mddev->safemode = 0;
1617 mddev->safemode_timer.function = md_safemode_timeout;
1618 mddev->safemode_timer.data = (unsigned long) mddev;
1619 mddev->safemode_delay = (20 * HZ)/1000 +1; /* 20 msec delay */
1622 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1623 md_wakeup_thread(mddev->thread);
1624 set_capacity(disk, mddev->array_size<<1);
1628 static int restart_array(mddev_t *mddev)
1630 struct gendisk *disk = disks[mdidx(mddev)];
1634 * Complain if it has no devices
1637 if (list_empty(&mddev->disks))
1645 mddev->safemode = 0;
1647 set_disk_ro(disk, 0);
1649 printk(KERN_INFO "md: md%d switched to read-write mode.\n",
1652 * Kick recovery or resync if necessary
1654 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1655 md_wakeup_thread(mddev->thread);
1658 printk(KERN_ERR "md: md%d has no personality assigned.\n",
1667 static int do_md_stop(mddev_t * mddev, int ro)
1670 struct gendisk *disk = disks[mdidx(mddev)];
1672 if (atomic_read(&mddev->active)>2) {
1673 printk("md: md%d still in use.\n",mdidx(mddev));
1679 if (mddev->sync_thread) {
1680 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1681 md_unregister_thread(mddev->sync_thread);
1682 mddev->sync_thread = NULL;
1685 del_timer_sync(&mddev->safemode_timer);
1687 invalidate_device(mk_kdev(disk->major, disk->first_minor), 1);
1696 set_disk_ro(disk, 0);
1697 if (mddev->pers->stop(mddev)) {
1700 set_disk_ro(disk, 1);
1703 module_put(mddev->pers->owner);
1708 if (mddev->raid_disks) {
1709 /* mark array as shutdown cleanly */
1711 md_update_sb(mddev);
1714 set_disk_ro(disk, 1);
1717 * Free resources if final stop
1720 struct gendisk *disk;
1721 printk(KERN_INFO "md: md%d stopped.\n", mdidx(mddev));
1723 export_array(mddev);
1725 mddev->array_size = 0;
1726 disk = disks[mdidx(mddev)];
1728 set_capacity(disk, 0);
1730 printk(KERN_INFO "md: md%d switched to read-only mode.\n",
1737 static void autorun_array(mddev_t *mddev)
1740 struct list_head *tmp;
1743 if (list_empty(&mddev->disks)) {
1748 printk(KERN_INFO "md: running: ");
1750 ITERATE_RDEV(mddev,rdev,tmp) {
1751 printk("<%s>", bdev_partition_name(rdev->bdev));
1755 err = do_md_run (mddev);
1757 printk(KERN_WARNING "md :do_md_run() returned %d\n", err);
1758 do_md_stop (mddev, 0);
1763 * lets try to run arrays based on all disks that have arrived
1764 * until now. (those are in pending_raid_disks)
1766 * the method: pick the first pending disk, collect all disks with
1767 * the same UUID, remove all from the pending list and put them into
1768 * the 'same_array' list. Then order this list based on superblock
1769 * update time (freshest comes first), kick out 'old' disks and
1770 * compare superblocks. If everything's fine then run it.
1772 * If "unit" is allocated, then bump its reference count
1774 static void autorun_devices(void)
1776 struct list_head candidates;
1777 struct list_head *tmp;
1778 mdk_rdev_t *rdev0, *rdev;
1781 printk(KERN_INFO "md: autorun ...\n");
1782 while (!list_empty(&pending_raid_disks)) {
1783 rdev0 = list_entry(pending_raid_disks.next,
1784 mdk_rdev_t, same_set);
1786 printk(KERN_INFO "md: considering %s ...\n",
1787 bdev_partition_name(rdev0->bdev));
1788 INIT_LIST_HEAD(&candidates);
1789 ITERATE_RDEV_PENDING(rdev,tmp)
1790 if (super_90_load(rdev, rdev0, 0) >= 0) {
1791 printk(KERN_INFO "md: adding %s ...\n",
1792 bdev_partition_name(rdev->bdev));
1793 list_move(&rdev->same_set, &candidates);
1796 * now we have a set of devices, with all of them having
1797 * mostly sane superblocks. It's time to allocate the
1801 mddev = mddev_find(rdev0->preferred_minor);
1804 "md: cannot allocate memory for md drive.\n");
1807 if (mddev_lock(mddev))
1808 printk(KERN_WARNING "md: md%d locked, cannot run\n",
1810 else if (mddev->raid_disks || mddev->major_version
1811 || !list_empty(&mddev->disks)) {
1813 "md: md%d already running, cannot run %s\n",
1814 mdidx(mddev), bdev_partition_name(rdev0->bdev));
1815 mddev_unlock(mddev);
1817 printk(KERN_INFO "md: created md%d\n", mdidx(mddev));
1818 ITERATE_RDEV_GENERIC(candidates,rdev,tmp) {
1819 list_del_init(&rdev->same_set);
1820 if (bind_rdev_to_array(rdev, mddev))
1823 autorun_array(mddev);
1824 mddev_unlock(mddev);
1826 /* on success, candidates will be empty, on error
1829 ITERATE_RDEV_GENERIC(candidates,rdev,tmp)
1833 printk(KERN_INFO "md: ... autorun DONE.\n");
1837 * import RAID devices based on one partition
1838 * if possible, the array gets run as well.
1841 static int autostart_array(dev_t startdev)
1843 int err = -EINVAL, i;
1844 mdp_super_t *sb = NULL;
1845 mdk_rdev_t *start_rdev = NULL, *rdev;
1847 start_rdev = md_import_device(startdev, 0, 0);
1848 if (IS_ERR(start_rdev)) {
1849 printk(KERN_WARNING "md: could not import %s!\n",
1850 partition_name(startdev));
1854 /* NOTE: this can only work for 0.90.0 superblocks */
1855 sb = (mdp_super_t*)page_address(start_rdev->sb_page);
1856 if (sb->major_version != 0 ||
1857 sb->minor_version != 90 ) {
1858 printk(KERN_WARNING "md: can only autostart 0.90.0 arrays\n");
1859 export_rdev(start_rdev);
1863 if (start_rdev->faulty) {
1865 "md: can not autostart based on faulty %s!\n",
1866 bdev_partition_name(start_rdev->bdev));
1867 export_rdev(start_rdev);
1870 list_add(&start_rdev->same_set, &pending_raid_disks);
1872 for (i = 0; i < MD_SB_DISKS; i++) {
1876 desc = sb->disks + i;
1877 dev = MKDEV(desc->major, desc->minor);
1881 if (dev == startdev)
1883 rdev = md_import_device(dev, 0, 0);
1885 printk(KERN_WARNING "md: could not import %s,"
1886 " trying to run array nevertheless.\n",
1887 partition_name(dev));
1890 list_add(&rdev->same_set, &pending_raid_disks);
1894 * possibly return codes
1902 static int get_version(void * arg)
1906 ver.major = MD_MAJOR_VERSION;
1907 ver.minor = MD_MINOR_VERSION;
1908 ver.patchlevel = MD_PATCHLEVEL_VERSION;
1910 if (copy_to_user(arg, &ver, sizeof(ver)))
1916 static int get_array_info(mddev_t * mddev, void * arg)
1918 mdu_array_info_t info;
1919 int nr,working,active,failed,spare;
1921 struct list_head *tmp;
1923 nr=working=active=failed=spare=0;
1924 ITERATE_RDEV(mddev,rdev,tmp) {
1937 info.major_version = mddev->major_version;
1938 info.minor_version = mddev->minor_version;
1939 info.patch_version = 1;
1940 info.ctime = mddev->ctime;
1941 info.level = mddev->level;
1942 info.size = mddev->size;
1944 info.raid_disks = mddev->raid_disks;
1945 info.md_minor = mddev->__minor;
1946 info.not_persistent= !mddev->persistent;
1948 info.utime = mddev->utime;
1951 info.state = (1<<MD_SB_CLEAN);
1952 info.active_disks = active;
1953 info.working_disks = working;
1954 info.failed_disks = failed;
1955 info.spare_disks = spare;
1957 info.layout = mddev->layout;
1958 info.chunk_size = mddev->chunk_size;
1960 if (copy_to_user(arg, &info, sizeof(info)))
1966 static int get_disk_info(mddev_t * mddev, void * arg)
1968 mdu_disk_info_t info;
1972 if (copy_from_user(&info, arg, sizeof(info)))
1977 rdev = find_rdev_nr(mddev, nr);
1979 info.major = MAJOR(rdev->bdev->bd_dev);
1980 info.minor = MINOR(rdev->bdev->bd_dev);
1981 info.raid_disk = rdev->raid_disk;
1984 info.state |= (1<<MD_DISK_FAULTY);
1985 else if (rdev->in_sync) {
1986 info.state |= (1<<MD_DISK_ACTIVE);
1987 info.state |= (1<<MD_DISK_SYNC);
1990 info.major = info.minor = 0;
1991 info.raid_disk = -1;
1992 info.state = (1<<MD_DISK_REMOVED);
1995 if (copy_to_user(arg, &info, sizeof(info)))
2001 static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
2005 dev = MKDEV(info->major,info->minor);
2006 if (!mddev->raid_disks) {
2008 /* expecting a device which has a superblock */
2009 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
2012 "md: md_import_device returned %ld\n",
2014 return PTR_ERR(rdev);
2016 if (!list_empty(&mddev->disks)) {
2017 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2018 mdk_rdev_t, same_set);
2019 int err = super_types[mddev->major_version]
2020 .load_super(rdev, rdev0, mddev->minor_version);
2023 "md: %s has different UUID to %s\n",
2024 bdev_partition_name(rdev->bdev),
2025 bdev_partition_name(rdev0->bdev));
2030 err = bind_rdev_to_array(rdev, mddev);
2037 * add_new_disk can be used once the array is assembled
2038 * to add "hot spares". They must already have a superblock
2043 if (!mddev->pers->hot_add_disk) {
2045 "md%d: personality does not support diskops!\n",
2049 rdev = md_import_device(dev, mddev->major_version,
2050 mddev->minor_version);
2053 "md: md_import_device returned %ld\n",
2055 return PTR_ERR(rdev);
2057 rdev->in_sync = 0; /* just to be sure */
2058 rdev->raid_disk = -1;
2059 err = bind_rdev_to_array(rdev, mddev);
2063 md_wakeup_thread(mddev->thread);
2067 /* otherwise, add_new_disk is only allowed
2068 * for major_version==0 superblocks
2070 if (mddev->major_version != 0) {
2071 printk(KERN_WARNING "md%d: ADD_NEW_DISK not supported\n",
2076 if (!(info->state & (1<<MD_DISK_FAULTY))) {
2078 rdev = md_import_device (dev, -1, 0);
2081 "md: error, md_import_device() returned %ld\n",
2083 return PTR_ERR(rdev);
2085 rdev->desc_nr = info->number;
2086 if (info->raid_disk < mddev->raid_disks)
2087 rdev->raid_disk = info->raid_disk;
2089 rdev->raid_disk = -1;
2092 if (rdev->raid_disk < mddev->raid_disks)
2093 rdev->in_sync = (info->state & (1<<MD_DISK_SYNC));
2097 err = bind_rdev_to_array(rdev, mddev);
2103 if (!mddev->persistent) {
2104 printk(KERN_INFO "md: nonpersistent superblock ...\n");
2105 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2107 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
2108 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
2110 if (!mddev->size || (mddev->size > rdev->size))
2111 mddev->size = rdev->size;
2117 static int hot_generate_error(mddev_t * mddev, dev_t dev)
2119 struct request_queue *q;
2125 printk(KERN_INFO "md: trying to generate %s error in md%d ... \n",
2126 partition_name(dev), mdidx(mddev));
2128 rdev = find_rdev(mddev, dev);
2134 if (rdev->desc_nr == -1) {
2141 q = bdev_get_queue(rdev->bdev);
2146 printk(KERN_INFO "md: okay, generating error!\n");
2147 // q->oneshot_error = 1; // disabled for now
2152 static int hot_remove_disk(mddev_t * mddev, dev_t dev)
2159 printk(KERN_INFO "md: trying to remove %s from md%d ... \n",
2160 partition_name(dev), mdidx(mddev));
2162 rdev = find_rdev(mddev, dev);
2166 if (rdev->raid_disk >= 0)
2169 kick_rdev_from_array(rdev);
2170 md_update_sb(mddev);
2174 printk(KERN_WARNING "md: cannot remove active disk %s from md%d ... \n",
2175 bdev_partition_name(rdev->bdev), mdidx(mddev));
2179 static int hot_add_disk(mddev_t * mddev, dev_t dev)
2188 printk(KERN_INFO "md: trying to hot-add %s to md%d ... \n",
2189 partition_name(dev), mdidx(mddev));
2191 if (mddev->major_version != 0) {
2192 printk(KERN_WARNING "md%d: HOT_ADD may only be used with"
2193 " version-0 superblocks.\n",
2197 if (!mddev->pers->hot_add_disk) {
2199 "md%d: personality does not support diskops!\n",
2204 rdev = md_import_device (dev, -1, 0);
2207 "md: error, md_import_device() returned %ld\n",
2212 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
2213 size = calc_dev_size(rdev, mddev->chunk_size);
2216 if (size < mddev->size) {
2218 "md%d: disk size %llu blocks < array size %llu\n",
2219 mdidx(mddev), (unsigned long long)size,
2220 (unsigned long long)mddev->size);
2227 "md: can not hot-add faulty %s disk to md%d!\n",
2228 bdev_partition_name(rdev->bdev), mdidx(mddev));
2234 bind_rdev_to_array(rdev, mddev);
2237 * The rest should better be atomic, we can have disk failures
2238 * noticed in interrupt contexts ...
2241 if (rdev->desc_nr == mddev->max_disks) {
2242 printk(KERN_WARNING "md%d: can not hot-add to full array!\n",
2245 goto abort_unbind_export;
2248 rdev->raid_disk = -1;
2250 md_update_sb(mddev);
2253 * Kick recovery, maybe this spare has to be added to the
2254 * array immediately.
2256 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2257 md_wakeup_thread(mddev->thread);
2261 abort_unbind_export:
2262 unbind_rdev_from_array(rdev);
2270 * set_array_info is used two different ways
2271 * The original usage is when creating a new array.
2272 * In this usage, raid_disks is > = and it together with
2273 * level, size, not_persistent,layout,chunksize determine the
2274 * shape of the array.
2275 * This will always create an array with a type-0.90.0 superblock.
2276 * The newer usage is when assembling an array.
2277 * In this case raid_disks will be 0, and the major_version field is
2278 * use to determine which style super-blocks are to be found on the devices.
2279 * The minor and patch _version numbers are also kept incase the
2280 * super_block handler wishes to interpret them.
2282 static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
2285 if (info->raid_disks == 0) {
2286 /* just setting version number for superblock loading */
2287 if (info->major_version < 0 ||
2288 info->major_version >= sizeof(super_types)/sizeof(super_types[0]) ||
2289 super_types[info->major_version].name == NULL) {
2290 /* maybe try to auto-load a module? */
2292 "md: superblock version %d not known\n",
2293 info->major_version);
2296 mddev->major_version = info->major_version;
2297 mddev->minor_version = info->minor_version;
2298 mddev->patch_version = info->patch_version;
2301 mddev->major_version = MD_MAJOR_VERSION;
2302 mddev->minor_version = MD_MINOR_VERSION;
2303 mddev->patch_version = MD_PATCHLEVEL_VERSION;
2304 mddev->ctime = get_seconds();
2306 mddev->level = info->level;
2307 mddev->size = info->size;
2308 mddev->raid_disks = info->raid_disks;
2309 /* don't set __minor, it is determined by which /dev/md* was
2312 if (info->state & (1<<MD_SB_CLEAN))
2313 mddev->recovery_cp = MaxSector;
2315 mddev->recovery_cp = 0;
2316 mddev->persistent = ! info->not_persistent;
2318 mddev->layout = info->layout;
2319 mddev->chunk_size = info->chunk_size;
2321 mddev->max_disks = MD_SB_DISKS;
2325 * Generate a 128 bit UUID
2327 get_random_bytes(mddev->uuid, 16);
2332 static int set_disk_faulty(mddev_t *mddev, dev_t dev)
2336 rdev = find_rdev(mddev, dev);
2340 md_error(mddev, rdev);
2344 static int md_ioctl(struct inode *inode, struct file *file,
2345 unsigned int cmd, unsigned long arg)
2349 struct hd_geometry *loc = (struct hd_geometry *) arg;
2350 mddev_t *mddev = NULL;
2353 if (!capable(CAP_SYS_ADMIN))
2356 dev = inode->i_rdev;
2358 if (minor >= MAX_MD_DEVS) {
2364 * Commands dealing with the RAID driver but not any
2370 err = get_version((void *)arg);
2373 case PRINT_RAID_DEBUG:
2388 * Commands creating/starting a new array:
2391 mddev = inode->i_bdev->bd_inode->u.generic_ip;
2399 if (cmd == START_ARRAY) {
2400 /* START_ARRAY doesn't need to lock the array as autostart_array
2401 * does the locking, and it could even be a different array
2403 err = autostart_array(arg);
2405 printk(KERN_WARNING "md: autostart %s failed!\n",
2406 partition_name(arg));
2412 err = mddev_lock(mddev);
2415 "md: ioctl lock interrupted, reason %d, cmd %d\n",
2422 case SET_ARRAY_INFO:
2424 if (!list_empty(&mddev->disks)) {
2426 "md: array md%d already has disks!\n",
2431 if (mddev->raid_disks) {
2433 "md: array md%d already initialised!\n",
2439 mdu_array_info_t info;
2441 memset(&info, 0, sizeof(info));
2442 else if (copy_from_user(&info, (void*)arg, sizeof(info))) {
2446 err = set_array_info(mddev, &info);
2448 printk(KERN_WARNING "md: couldn't set"
2449 " array info. %d\n", err);
2459 * Commands querying/configuring an existing array:
2461 /* if we are initialised yet, only ADD_NEW_DISK or STOP_ARRAY is allowed */
2462 if (!mddev->raid_disks && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY && cmd != RUN_ARRAY) {
2468 * Commands even a read-only array can execute:
2472 case GET_ARRAY_INFO:
2473 err = get_array_info(mddev, (void *)arg);
2477 err = get_disk_info(mddev, (void *)arg);
2480 case RESTART_ARRAY_RW:
2481 err = restart_array(mddev);
2485 err = do_md_stop (mddev, 0);
2489 err = do_md_stop (mddev, 1);
2493 * We have a problem here : there is no easy way to give a CHS
2494 * virtual geometry. We currently pretend that we have a 2 heads
2495 * 4 sectors (with a BIG number of cylinders...). This drives
2496 * dosfs just mad... ;-)
2503 err = put_user (2, (char *) &loc->heads);
2506 err = put_user (4, (char *) &loc->sectors);
2509 err = put_user(get_capacity(disks[mdidx(mddev)])/8,
2510 (short *) &loc->cylinders);
2513 err = put_user (get_start_sect(inode->i_bdev),
2514 (long *) &loc->start);
2519 * The remaining ioctls are changing the state of the
2520 * superblock, so we do not allow read-only arrays
2532 mdu_disk_info_t info;
2533 if (copy_from_user(&info, (void*)arg, sizeof(info)))
2536 err = add_new_disk(mddev, &info);
2539 case HOT_GENERATE_ERROR:
2540 err = hot_generate_error(mddev, arg);
2542 case HOT_REMOVE_DISK:
2543 err = hot_remove_disk(mddev, arg);
2547 err = hot_add_disk(mddev, arg);
2550 case SET_DISK_FAULTY:
2551 err = set_disk_faulty(mddev, arg);
2556 err = do_md_run (mddev);
2558 * we have to clean up the mess if
2559 * the array cannot be run for some
2561 * ->pers will not be set, to superblock will
2565 do_md_stop (mddev, 0);
2570 if (_IOC_TYPE(cmd) == MD_MAJOR)
2571 printk(KERN_WARNING "md: %s(pid %d) used"
2572 " obsolete MD ioctl, upgrade your"
2573 " software to use new ictls.\n",
2574 current->comm, current->pid);
2581 mddev_unlock(mddev);
2591 static int md_open(struct inode *inode, struct file *file)
2594 * Succeed if we can find or allocate a mddev structure.
2596 mddev_t *mddev = mddev_find(minor(inode->i_rdev));
2602 if ((err = mddev_lock(mddev)))
2606 mddev_unlock(mddev);
2607 inode->i_bdev->bd_inode->u.generic_ip = mddev_get(mddev);
2614 static int md_release(struct inode *inode, struct file * file)
2616 mddev_t *mddev = inode->i_bdev->bd_inode->u.generic_ip;
2625 static struct block_device_operations md_fops =
2627 .owner = THIS_MODULE,
2629 .release = md_release,
2633 int md_thread(void * arg)
2635 mdk_thread_t *thread = arg;
2643 daemonize(thread->name, mdidx(thread->mddev));
2645 current->exit_signal = SIGCHLD;
2646 allow_signal(SIGKILL);
2647 thread->tsk = current;
2650 * md_thread is a 'system-thread', it's priority should be very
2651 * high. We avoid resource deadlocks individually in each
2652 * raid personality. (RAID5 does preallocation) We also use RR and
2653 * the very same RT priority as kswapd, thus we will never get
2654 * into a priority inversion deadlock.
2656 * we definitely have to have equal or higher priority than
2657 * bdflush, otherwise bdflush will deadlock if there are too
2658 * many dirty RAID5 blocks.
2662 complete(thread->event);
2663 while (thread->run) {
2664 void (*run)(mddev_t *);
2666 wait_event_interruptible(thread->wqueue,
2667 test_bit(THREAD_WAKEUP, &thread->flags));
2668 if (current->flags & PF_FREEZE)
2669 refrigerator(PF_IOTHREAD);
2671 clear_bit(THREAD_WAKEUP, &thread->flags);
2678 if (signal_pending(current))
2679 flush_signals(current);
2681 complete(thread->event);
2685 void md_wakeup_thread(mdk_thread_t *thread)
2688 dprintk("md: waking up MD thread %p.\n", thread);
2689 set_bit(THREAD_WAKEUP, &thread->flags);
2690 wake_up(&thread->wqueue);
2694 mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
2697 mdk_thread_t *thread;
2699 struct completion event;
2701 thread = (mdk_thread_t *) kmalloc
2702 (sizeof(mdk_thread_t), GFP_KERNEL);
2706 memset(thread, 0, sizeof(mdk_thread_t));
2707 init_waitqueue_head(&thread->wqueue);
2709 init_completion(&event);
2710 thread->event = &event;
2712 thread->mddev = mddev;
2713 thread->name = name;
2714 ret = kernel_thread(md_thread, thread, 0);
2719 wait_for_completion(&event);
2723 void md_interrupt_thread(mdk_thread_t *thread)
2729 dprintk("interrupting MD-thread pid %d\n", thread->tsk->pid);
2730 send_sig(SIGKILL, thread->tsk, 1);
2733 void md_unregister_thread(mdk_thread_t *thread)
2735 struct completion event;
2737 init_completion(&event);
2739 thread->event = &event;
2741 thread->name = NULL;
2742 md_interrupt_thread(thread);
2743 wait_for_completion(&event);
2747 void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
2749 dprintk("md_error dev:(%d:%d), rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
2750 MD_MAJOR,mdidx(mddev),
2751 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
2752 __builtin_return_address(0),__builtin_return_address(1),
2753 __builtin_return_address(2),__builtin_return_address(3));
2760 if (!rdev || rdev->faulty)
2762 if (!mddev->pers->error_handler)
2764 mddev->pers->error_handler(mddev,rdev);
2765 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2766 md_wakeup_thread(mddev->thread);
2769 /* seq_file implementation /proc/mdstat */
2771 static void status_unused(struct seq_file *seq)
2775 struct list_head *tmp;
2777 seq_printf(seq, "unused devices: ");
2779 ITERATE_RDEV_PENDING(rdev,tmp) {
2781 seq_printf(seq, "%s ",
2782 bdev_partition_name(rdev->bdev));
2785 seq_printf(seq, "<none>");
2787 seq_printf(seq, "\n");
2791 static void status_resync(struct seq_file *seq, mddev_t * mddev)
2793 unsigned long max_blocks, resync, res, dt, db, rt;
2795 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
2796 max_blocks = mddev->size;
2799 * Should not happen.
2805 res = (resync/1024)*1000/(max_blocks/1024 + 1);
2807 int i, x = res/50, y = 20-x;
2808 seq_printf(seq, "[");
2809 for (i = 0; i < x; i++)
2810 seq_printf(seq, "=");
2811 seq_printf(seq, ">");
2812 for (i = 0; i < y; i++)
2813 seq_printf(seq, ".");
2814 seq_printf(seq, "] ");
2816 seq_printf(seq, " %s =%3lu.%lu%% (%lu/%lu)",
2817 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
2818 "resync" : "recovery"),
2819 res/10, res % 10, resync, max_blocks);
2822 * We do not want to overflow, so the order of operands and
2823 * the * 100 / 100 trick are important. We do a +1 to be
2824 * safe against division by zero. We only estimate anyway.
2826 * dt: time from mark until now
2827 * db: blocks written from mark until now
2828 * rt: remaining time
2830 dt = ((jiffies - mddev->resync_mark) / HZ);
2832 db = resync - (mddev->resync_mark_cnt/2);
2833 rt = (dt * ((max_blocks-resync) / (db/100+1)))/100;
2835 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
2837 seq_printf(seq, " speed=%ldK/sec", db/dt);
2840 static void *md_seq_start(struct seq_file *seq, loff_t *pos)
2842 struct list_head *tmp;
2852 spin_lock(&all_mddevs_lock);
2853 list_for_each(tmp,&all_mddevs)
2855 mddev = list_entry(tmp, mddev_t, all_mddevs);
2857 spin_unlock(&all_mddevs_lock);
2860 spin_unlock(&all_mddevs_lock);
2861 return (void*)2;/* tail */
2864 static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2866 struct list_head *tmp;
2867 mddev_t *next_mddev, *mddev = v;
2873 spin_lock(&all_mddevs_lock);
2875 tmp = all_mddevs.next;
2877 tmp = mddev->all_mddevs.next;
2878 if (tmp != &all_mddevs)
2879 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
2881 next_mddev = (void*)2;
2884 spin_unlock(&all_mddevs_lock);
2892 static void md_seq_stop(struct seq_file *seq, void *v)
2896 if (mddev && v != (void*)1 && v != (void*)2)
2900 static int md_seq_show(struct seq_file *seq, void *v)
2904 struct list_head *tmp2;
2908 if (v == (void*)1) {
2909 seq_printf(seq, "Personalities : ");
2910 spin_lock(&pers_lock);
2911 for (i = 0; i < MAX_PERSONALITY; i++)
2913 seq_printf(seq, "[%s] ", pers[i]->name);
2915 spin_unlock(&pers_lock);
2916 seq_printf(seq, "\n");
2919 if (v == (void*)2) {
2924 if (mddev_lock(mddev)!=0)
2926 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
2927 seq_printf(seq, "md%d : %sactive", mdidx(mddev),
2928 mddev->pers ? "" : "in");
2931 seq_printf(seq, " (read-only)");
2932 seq_printf(seq, " %s", mddev->pers->name);
2936 ITERATE_RDEV(mddev,rdev,tmp2) {
2937 seq_printf(seq, " %s[%d]",
2938 bdev_partition_name(rdev->bdev), rdev->desc_nr);
2940 seq_printf(seq, "(F)");
2946 if (!list_empty(&mddev->disks)) {
2948 seq_printf(seq, "\n %llu blocks",
2949 (unsigned long long)mddev->array_size);
2951 seq_printf(seq, "\n %llu blocks",
2952 (unsigned long long)size);
2956 mddev->pers->status (seq, mddev);
2957 seq_printf(seq, "\n ");
2958 if (mddev->curr_resync > 2)
2959 status_resync (seq, mddev);
2960 else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
2961 seq_printf(seq, " resync=DELAYED");
2964 seq_printf(seq, "\n");
2966 mddev_unlock(mddev);
2971 static struct seq_operations md_seq_ops = {
2972 .start = md_seq_start,
2973 .next = md_seq_next,
2974 .stop = md_seq_stop,
2975 .show = md_seq_show,
2978 static int md_seq_open(struct inode *inode, struct file *file)
2982 error = seq_open(file, &md_seq_ops);
2986 static struct file_operations md_seq_fops = {
2987 .open = md_seq_open,
2989 .llseek = seq_lseek,
2990 .release = seq_release,
2993 int register_md_personality(int pnum, mdk_personality_t *p)
2995 if (pnum >= MAX_PERSONALITY) {
3000 spin_lock(&pers_lock);
3002 spin_unlock(&pers_lock);
3008 printk(KERN_INFO "md: %s personality registered as nr %d\n", p->name, pnum);
3009 spin_unlock(&pers_lock);
3013 int unregister_md_personality(int pnum)
3015 if (pnum >= MAX_PERSONALITY) {
3020 printk(KERN_INFO "md: %s personality unregistered\n", pers[pnum]->name);
3021 spin_lock(&pers_lock);
3023 spin_unlock(&pers_lock);
3027 void md_sync_acct(mdk_rdev_t *rdev, unsigned long nr_sectors)
3029 rdev->bdev->bd_contains->bd_disk->sync_io += nr_sectors;
3032 static int is_mddev_idle(mddev_t *mddev)
3035 struct list_head *tmp;
3037 unsigned long curr_events;
3040 ITERATE_RDEV(mddev,rdev,tmp) {
3041 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
3042 curr_events = disk_stat_read(disk, read_sectors) +
3043 disk_stat_read(disk, write_sectors) -
3045 if ((curr_events - rdev->last_events) > 32) {
3046 rdev->last_events = curr_events;
3053 void md_done_sync(mddev_t *mddev, int blocks, int ok)
3055 /* another "blocks" (512byte) blocks have been synced */
3056 atomic_sub(blocks, &mddev->recovery_active);
3057 wake_up(&mddev->recovery_wait);
3059 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
3060 md_wakeup_thread(mddev->thread);
3061 // stop recovery, signal do_sync ....
3066 void md_write_start(mddev_t *mddev)
3068 if (!atomic_read(&mddev->writes_pending)) {
3069 mddev_lock_uninterruptible(mddev);
3070 if (mddev->in_sync) {
3072 del_timer(&mddev->safemode_timer);
3073 md_update_sb(mddev);
3075 atomic_inc(&mddev->writes_pending);
3076 mddev_unlock(mddev);
3078 atomic_inc(&mddev->writes_pending);
3081 void md_write_end(mddev_t *mddev)
3083 if (atomic_dec_and_test(&mddev->writes_pending)) {
3084 if (mddev->safemode == 2)
3085 md_wakeup_thread(mddev->thread);
3087 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
3091 static inline void md_enter_safemode(mddev_t *mddev)
3093 mddev_lock_uninterruptible(mddev);
3094 if (mddev->safemode && !atomic_read(&mddev->writes_pending) &&
3095 !mddev->in_sync && mddev->recovery_cp == MaxSector) {
3097 md_update_sb(mddev);
3099 mddev_unlock(mddev);
3101 if (mddev->safemode == 1)
3102 mddev->safemode = 0;
3105 void md_handle_safemode(mddev_t *mddev)
3107 if (signal_pending(current)) {
3108 printk(KERN_INFO "md: md%d in immediate safe mode\n",
3110 mddev->safemode = 2;
3111 flush_signals(current);
3113 if (mddev->safemode)
3114 md_enter_safemode(mddev);
3118 DECLARE_WAIT_QUEUE_HEAD(resync_wait);
3120 #define SYNC_MARKS 10
3121 #define SYNC_MARK_STEP (3*HZ)
3122 static void md_do_sync(mddev_t *mddev)
3125 unsigned int max_sectors, currspeed = 0,
3127 unsigned long mark[SYNC_MARKS];
3128 unsigned long mark_cnt[SYNC_MARKS];
3130 struct list_head *tmp;
3131 unsigned long last_check;
3133 /* just incase thread restarts... */
3134 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
3137 /* we overload curr_resync somewhat here.
3138 * 0 == not engaged in resync at all
3139 * 2 == checking that there is no conflict with another sync
3140 * 1 == like 2, but have yielded to allow conflicting resync to
3142 * other == active in resync - this many blocks
3145 mddev->curr_resync = 2;
3147 ITERATE_MDDEV(mddev2,tmp) {
3148 if (mddev2 == mddev)
3150 if (mddev2->curr_resync &&
3151 match_mddev_units(mddev,mddev2)) {
3152 printk(KERN_INFO "md: delaying resync of md%d"
3153 " until md%d has finished resync (they"
3154 " share one or more physical units)\n",
3155 mdidx(mddev), mdidx(mddev2));
3156 if (mddev < mddev2) {/* arbitrarily yield */
3157 mddev->curr_resync = 1;
3158 wake_up(&resync_wait);
3160 if (wait_event_interruptible(resync_wait,
3161 mddev2->curr_resync < mddev->curr_resync)) {
3162 flush_signals(current);
3167 if (mddev->curr_resync == 1) {
3172 } while (mddev->curr_resync < 2);
3174 max_sectors = mddev->size << 1;
3176 printk(KERN_INFO "md: syncing RAID array md%d\n", mdidx(mddev));
3177 printk(KERN_INFO "md: minimum _guaranteed_ reconstruction speed:"
3178 " %d KB/sec/disc.\n", sysctl_speed_limit_min);
3179 printk(KERN_INFO "md: using maximum available idle IO bandwith "
3180 "(but not more than %d KB/sec) for reconstruction.\n",
3181 sysctl_speed_limit_max);
3183 is_mddev_idle(mddev); /* this also initializes IO event counters */
3184 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3185 j = mddev->recovery_cp;
3188 for (m = 0; m < SYNC_MARKS; m++) {
3193 mddev->resync_mark = mark[last_mark];
3194 mddev->resync_mark_cnt = mark_cnt[last_mark];
3197 * Tune reconstruction:
3199 window = 32*(PAGE_SIZE/512);
3200 printk(KERN_INFO "md: using %dk window, over a total of %d blocks.\n",
3201 window/2,max_sectors/2);
3203 atomic_set(&mddev->recovery_active, 0);
3204 init_waitqueue_head(&mddev->recovery_wait);
3209 "md: resuming recovery of md%d from checkpoint.\n",
3212 while (j < max_sectors) {
3215 sectors = mddev->pers->sync_request(mddev, j, currspeed < sysctl_speed_limit_min);
3217 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
3220 atomic_add(sectors, &mddev->recovery_active);
3222 if (j>1) mddev->curr_resync = j;
3224 if (last_check + window > j)
3229 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery) ||
3230 test_bit(MD_RECOVERY_ERR, &mddev->recovery))
3236 if (jiffies >= mark[last_mark] + SYNC_MARK_STEP ) {
3238 int next = (last_mark+1) % SYNC_MARKS;
3240 mddev->resync_mark = mark[next];
3241 mddev->resync_mark_cnt = mark_cnt[next];
3242 mark[next] = jiffies;
3243 mark_cnt[next] = j - atomic_read(&mddev->recovery_active);
3248 if (signal_pending(current)) {
3250 * got a signal, exit.
3253 "md: md_do_sync() got signal ... exiting\n");
3254 flush_signals(current);
3255 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3260 * this loop exits only if either when we are slower than
3261 * the 'hard' speed limit, or the system was IO-idle for
3263 * the system might be non-idle CPU-wise, but we only care
3264 * about not overloading the IO subsystem. (things like an
3265 * e2fsck being done on the RAID array should execute fast)
3269 currspeed = (j-mddev->resync_mark_cnt)/2/((jiffies-mddev->resync_mark)/HZ +1) +1;
3271 if (currspeed > sysctl_speed_limit_min) {
3272 if ((currspeed > sysctl_speed_limit_max) ||
3273 !is_mddev_idle(mddev)) {
3274 current->state = TASK_INTERRUPTIBLE;
3275 schedule_timeout(HZ/4);
3280 printk(KERN_INFO "md: md%d: sync done.\n",mdidx(mddev));
3282 * this also signals 'finished resyncing' to md_stop
3285 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
3287 /* tell personality that we are finished */
3288 mddev->pers->sync_request(mddev, max_sectors, 1);
3290 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
3291 mddev->curr_resync > 2 &&
3292 mddev->curr_resync > mddev->recovery_cp) {
3293 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
3295 "md: checkpointing recovery of md%d.\n",
3297 mddev->recovery_cp = mddev->curr_resync;
3299 mddev->recovery_cp = MaxSector;
3302 if (mddev->safemode)
3303 md_enter_safemode(mddev);
3305 mddev->curr_resync = 0;
3306 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
3307 md_wakeup_thread(mddev->thread);
3312 * This routine is regularly called by all per-raid-array threads to
3313 * deal with generic issues like resync and super-block update.
3314 * Raid personalities that don't have a thread (linear/raid0) do not
3315 * need this as they never do any recovery or update the superblock.
3317 * It does not do any resync itself, but rather "forks" off other threads
3318 * to do that as needed.
3319 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
3320 * "->recovery" and create a thread at ->sync_thread.
3321 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
3322 * and wakeups up this thread which will reap the thread and finish up.
3323 * This thread also removes any faulty devices (with nr_pending == 0).
3325 * The overall approach is:
3326 * 1/ if the superblock needs updating, update it.
3327 * 2/ If a recovery thread is running, don't do anything else.
3328 * 3/ If recovery has finished, clean up, possibly marking spares active.
3329 * 4/ If there are any faulty devices, remove them.
3330 * 5/ If array is degraded, try to add spares devices
3331 * 6/ If array has spares or is not in-sync, start a resync thread.
3333 void md_check_recovery(mddev_t *mddev)
3336 struct list_head *rtmp;
3339 dprintk(KERN_INFO "md: recovery thread got woken up ...\n");
3345 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
3346 test_bit(MD_RECOVERY_DONE, &mddev->recovery)
3349 if (mddev_trylock(mddev)==0) {
3351 if (mddev->sb_dirty)
3352 md_update_sb(mddev);
3353 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
3354 !test_bit(MD_RECOVERY_DONE, &mddev->recovery))
3355 /* resync/recovery still happening */
3357 if (mddev->sync_thread) {
3358 /* resync has finished, collect result */
3359 md_unregister_thread(mddev->sync_thread);
3360 mddev->sync_thread = NULL;
3361 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery)) {
3363 /* activate any spares */
3364 mddev->pers->spare_active(mddev);
3366 md_update_sb(mddev);
3367 mddev->recovery = 0;
3368 wake_up(&resync_wait);
3371 if (mddev->recovery) {
3373 mddev->recovery = 0;
3374 wake_up(&resync_wait);
3377 /* no recovery is running.
3378 * remove any failed drives, then
3379 * add spares if possible
3381 ITERATE_RDEV(mddev,rdev,rtmp) {
3382 if (rdev->raid_disk >= 0 &&
3384 atomic_read(&rdev->nr_pending)==0) {
3385 mddev->pers->hot_remove_disk(mddev, rdev->raid_disk);
3386 rdev->raid_disk = -1;
3388 if (!rdev->faulty && rdev->raid_disk >= 0 && !rdev->in_sync)
3391 if (mddev->degraded) {
3392 ITERATE_RDEV(mddev,rdev,rtmp)
3393 if (rdev->raid_disk < 0
3395 if (mddev->pers->hot_add_disk(mddev,rdev))
3402 if (!spares && (mddev->recovery_cp == MaxSector )) {
3403 /* nothing we can do ... */
3406 if (mddev->pers->sync_request) {
3407 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3409 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3410 mddev->sync_thread = md_register_thread(md_do_sync,
3413 if (!mddev->sync_thread) {
3414 printk(KERN_ERR "md%d: could not start resync"
3417 /* leave the spares where they are, it shouldn't hurt */
3418 mddev->recovery = 0;
3420 md_wakeup_thread(mddev->sync_thread);
3424 mddev_unlock(mddev);
3428 int md_notify_reboot(struct notifier_block *this,
3429 unsigned long code, void *x)
3431 struct list_head *tmp;
3434 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
3436 printk(KERN_INFO "md: stopping all md devices.\n");
3438 ITERATE_MDDEV(mddev,tmp)
3439 if (mddev_trylock(mddev)==0)
3440 do_md_stop (mddev, 1);
3442 * certain more exotic SCSI devices are known to be
3443 * volatile wrt too early system reboots. While the
3444 * right place to handle this issue is the given
3445 * driver, we do want to have a safe RAID driver ...
3452 struct notifier_block md_notifier = {
3453 .notifier_call = md_notify_reboot,
3455 .priority = INT_MAX, /* before any real devices */
3458 static void md_geninit(void)
3460 struct proc_dir_entry *p;
3462 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
3464 #ifdef CONFIG_PROC_FS
3465 p = create_proc_entry("mdstat", S_IRUGO, NULL);
3467 p->proc_fops = &md_seq_fops;
3471 int __init md_init(void)
3475 printk(KERN_INFO "md: md driver %d.%d.%d MAX_MD_DEVS=%d,"
3476 " MD_SB_DISKS=%d\n",
3477 MD_MAJOR_VERSION, MD_MINOR_VERSION,
3478 MD_PATCHLEVEL_VERSION, MAX_MD_DEVS, MD_SB_DISKS);
3480 if (register_blkdev(MAJOR_NR, "md"))
3484 blk_register_region(MKDEV(MAJOR_NR, 0), MAX_MD_DEVS, THIS_MODULE,
3485 md_probe, NULL, NULL);
3486 for (minor=0; minor < MAX_MD_DEVS; ++minor) {
3488 sprintf(name, "md/%d", minor);
3489 devfs_register(NULL, name, DEVFS_FL_DEFAULT, MAJOR_NR, minor,
3490 S_IFBLK | S_IRUSR | S_IWUSR, &md_fops, NULL);
3493 register_reboot_notifier(&md_notifier);
3494 raid_table_header = register_sysctl_table(raid_root_table, 1);
3504 * Searches all registered partitions for autorun RAID arrays
3507 static dev_t detected_devices[128];
3510 void md_autodetect_dev(dev_t dev)
3512 if (dev_cnt >= 0 && dev_cnt < 127)
3513 detected_devices[dev_cnt++] = dev;
3517 static void autostart_arrays(void)
3522 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
3524 for (i = 0; i < dev_cnt; i++) {
3525 dev_t dev = detected_devices[i];
3527 rdev = md_import_device(dev,0, 0);
3529 printk(KERN_ALERT "md: could not import %s!\n",
3530 partition_name(dev));
3537 list_add(&rdev->same_set, &pending_raid_disks);
3546 static __exit void md_exit(void)
3549 blk_unregister_region(MKDEV(MAJOR_NR,0), MAX_MD_DEVS);
3550 for (i=0; i < MAX_MD_DEVS; i++)
3551 devfs_remove("md/%d", i);
3554 unregister_blkdev(MAJOR_NR,"md");
3555 unregister_reboot_notifier(&md_notifier);
3556 unregister_sysctl_table(raid_table_header);
3557 #ifdef CONFIG_PROC_FS
3558 remove_proc_entry("mdstat", NULL);
3560 for (i = 0; i < MAX_MD_DEVS; i++) {
3561 struct gendisk *disk = disks[i];
3565 mddev = disk->private_data;
3572 module_init(md_init)
3573 module_exit(md_exit)
3575 EXPORT_SYMBOL(register_md_personality);
3576 EXPORT_SYMBOL(unregister_md_personality);
3577 EXPORT_SYMBOL(md_error);
3578 EXPORT_SYMBOL(md_sync_acct);
3579 EXPORT_SYMBOL(md_done_sync);
3580 EXPORT_SYMBOL(md_write_start);
3581 EXPORT_SYMBOL(md_write_end);
3582 EXPORT_SYMBOL(md_handle_safemode);
3583 EXPORT_SYMBOL(md_register_thread);
3584 EXPORT_SYMBOL(md_unregister_thread);
3585 EXPORT_SYMBOL(md_wakeup_thread);
3586 EXPORT_SYMBOL(md_print_devices);
3587 EXPORT_SYMBOL(md_interrupt_thread);
3588 EXPORT_SYMBOL(md_check_recovery);
3589 MODULE_LICENSE("GPL");