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 bandwidth 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;
134 static int start_readonly;
137 * Enables to iterate over all existing md arrays
138 * all_mddevs_lock protects this list.
140 static LIST_HEAD(all_mddevs);
141 static DEFINE_SPINLOCK(all_mddevs_lock);
145 * iterates through all used mddevs in the system.
146 * We take care to grab the all_mddevs_lock whenever navigating
147 * the list, and to always hold a refcount when unlocked.
148 * Any code which breaks out of this loop while own
149 * a reference to the current mddev and must mddev_put it.
151 #define ITERATE_MDDEV(mddev,tmp) \
153 for (({ spin_lock(&all_mddevs_lock); \
154 tmp = all_mddevs.next; \
156 ({ if (tmp != &all_mddevs) \
157 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
158 spin_unlock(&all_mddevs_lock); \
159 if (mddev) mddev_put(mddev); \
160 mddev = list_entry(tmp, mddev_t, all_mddevs); \
161 tmp != &all_mddevs;}); \
162 ({ spin_lock(&all_mddevs_lock); \
167 static int md_fail_request (request_queue_t *q, struct bio *bio)
169 bio_io_error(bio, bio->bi_size);
173 static inline mddev_t *mddev_get(mddev_t *mddev)
175 atomic_inc(&mddev->active);
179 static void mddev_put(mddev_t *mddev)
181 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
183 if (!mddev->raid_disks && list_empty(&mddev->disks)) {
184 list_del(&mddev->all_mddevs);
185 blk_put_queue(mddev->queue);
186 kobject_unregister(&mddev->kobj);
188 spin_unlock(&all_mddevs_lock);
191 static mddev_t * mddev_find(dev_t unit)
193 mddev_t *mddev, *new = NULL;
196 spin_lock(&all_mddevs_lock);
197 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
198 if (mddev->unit == unit) {
200 spin_unlock(&all_mddevs_lock);
206 list_add(&new->all_mddevs, &all_mddevs);
207 spin_unlock(&all_mddevs_lock);
210 spin_unlock(&all_mddevs_lock);
212 new = (mddev_t *) kmalloc(sizeof(*new), GFP_KERNEL);
216 memset(new, 0, sizeof(*new));
219 if (MAJOR(unit) == MD_MAJOR)
220 new->md_minor = MINOR(unit);
222 new->md_minor = MINOR(unit) >> MdpMinorShift;
224 init_MUTEX(&new->reconfig_sem);
225 INIT_LIST_HEAD(&new->disks);
226 INIT_LIST_HEAD(&new->all_mddevs);
227 init_timer(&new->safemode_timer);
228 atomic_set(&new->active, 1);
229 spin_lock_init(&new->write_lock);
230 init_waitqueue_head(&new->sb_wait);
232 new->queue = blk_alloc_queue(GFP_KERNEL);
238 blk_queue_make_request(new->queue, md_fail_request);
243 static inline int mddev_lock(mddev_t * mddev)
245 return down_interruptible(&mddev->reconfig_sem);
248 static inline void mddev_lock_uninterruptible(mddev_t * mddev)
250 down(&mddev->reconfig_sem);
253 static inline int mddev_trylock(mddev_t * mddev)
255 return down_trylock(&mddev->reconfig_sem);
258 static inline void mddev_unlock(mddev_t * mddev)
260 up(&mddev->reconfig_sem);
262 md_wakeup_thread(mddev->thread);
265 mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
268 struct list_head *tmp;
270 ITERATE_RDEV(mddev,rdev,tmp) {
271 if (rdev->desc_nr == nr)
277 static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
279 struct list_head *tmp;
282 ITERATE_RDEV(mddev,rdev,tmp) {
283 if (rdev->bdev->bd_dev == dev)
289 static inline sector_t calc_dev_sboffset(struct block_device *bdev)
291 sector_t size = bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
292 return MD_NEW_SIZE_BLOCKS(size);
295 static sector_t calc_dev_size(mdk_rdev_t *rdev, unsigned chunk_size)
299 size = rdev->sb_offset;
302 size &= ~((sector_t)chunk_size/1024 - 1);
306 static int alloc_disk_sb(mdk_rdev_t * rdev)
311 rdev->sb_page = alloc_page(GFP_KERNEL);
312 if (!rdev->sb_page) {
313 printk(KERN_ALERT "md: out of memory.\n");
320 static void free_disk_sb(mdk_rdev_t * rdev)
323 page_cache_release(rdev->sb_page);
325 rdev->sb_page = NULL;
332 static int super_written(struct bio *bio, unsigned int bytes_done, int error)
334 mdk_rdev_t *rdev = bio->bi_private;
335 mddev_t *mddev = rdev->mddev;
339 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags))
340 md_error(mddev, rdev);
342 if (atomic_dec_and_test(&mddev->pending_writes))
343 wake_up(&mddev->sb_wait);
348 static int super_written_barrier(struct bio *bio, unsigned int bytes_done, int error)
350 struct bio *bio2 = bio->bi_private;
351 mdk_rdev_t *rdev = bio2->bi_private;
352 mddev_t *mddev = rdev->mddev;
356 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
357 error == -EOPNOTSUPP) {
359 /* barriers don't appear to be supported :-( */
360 set_bit(BarriersNotsupp, &rdev->flags);
361 mddev->barriers_work = 0;
362 spin_lock_irqsave(&mddev->write_lock, flags);
363 bio2->bi_next = mddev->biolist;
364 mddev->biolist = bio2;
365 spin_unlock_irqrestore(&mddev->write_lock, flags);
366 wake_up(&mddev->sb_wait);
371 bio->bi_private = rdev;
372 return super_written(bio, bytes_done, error);
375 void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
376 sector_t sector, int size, struct page *page)
378 /* write first size bytes of page to sector of rdev
379 * Increment mddev->pending_writes before returning
380 * and decrement it on completion, waking up sb_wait
381 * if zero is reached.
382 * If an error occurred, call md_error
384 * As we might need to resubmit the request if BIO_RW_BARRIER
385 * causes ENOTSUPP, we allocate a spare bio...
387 struct bio *bio = bio_alloc(GFP_NOIO, 1);
388 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNC);
390 bio->bi_bdev = rdev->bdev;
391 bio->bi_sector = sector;
392 bio_add_page(bio, page, size, 0);
393 bio->bi_private = rdev;
394 bio->bi_end_io = super_written;
397 atomic_inc(&mddev->pending_writes);
398 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
400 rw |= (1<<BIO_RW_BARRIER);
401 rbio = bio_clone(bio, GFP_NOIO);
402 rbio->bi_private = bio;
403 rbio->bi_end_io = super_written_barrier;
404 submit_bio(rw, rbio);
409 void md_super_wait(mddev_t *mddev)
411 /* wait for all superblock writes that were scheduled to complete.
412 * if any had to be retried (due to BARRIER problems), retry them
416 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
417 if (atomic_read(&mddev->pending_writes)==0)
419 while (mddev->biolist) {
421 spin_lock_irq(&mddev->write_lock);
422 bio = mddev->biolist;
423 mddev->biolist = bio->bi_next ;
425 spin_unlock_irq(&mddev->write_lock);
426 submit_bio(bio->bi_rw, bio);
430 finish_wait(&mddev->sb_wait, &wq);
433 static int bi_complete(struct bio *bio, unsigned int bytes_done, int error)
438 complete((struct completion*)bio->bi_private);
442 int sync_page_io(struct block_device *bdev, sector_t sector, int size,
443 struct page *page, int rw)
445 struct bio *bio = bio_alloc(GFP_NOIO, 1);
446 struct completion event;
449 rw |= (1 << BIO_RW_SYNC);
452 bio->bi_sector = sector;
453 bio_add_page(bio, page, size, 0);
454 init_completion(&event);
455 bio->bi_private = &event;
456 bio->bi_end_io = bi_complete;
458 wait_for_completion(&event);
460 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
465 static int read_disk_sb(mdk_rdev_t * rdev, int size)
467 char b[BDEVNAME_SIZE];
468 if (!rdev->sb_page) {
476 if (!sync_page_io(rdev->bdev, rdev->sb_offset<<1, size, rdev->sb_page, READ))
482 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
483 bdevname(rdev->bdev,b));
487 static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
489 if ( (sb1->set_uuid0 == sb2->set_uuid0) &&
490 (sb1->set_uuid1 == sb2->set_uuid1) &&
491 (sb1->set_uuid2 == sb2->set_uuid2) &&
492 (sb1->set_uuid3 == sb2->set_uuid3))
500 static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
503 mdp_super_t *tmp1, *tmp2;
505 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
506 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
508 if (!tmp1 || !tmp2) {
510 printk(KERN_INFO "md.c: sb1 is not equal to sb2!\n");
518 * nr_disks is not constant
523 if (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4))
534 static unsigned int calc_sb_csum(mdp_super_t * sb)
536 unsigned int disk_csum, csum;
538 disk_csum = sb->sb_csum;
540 csum = csum_partial((void *)sb, MD_SB_BYTES, 0);
541 sb->sb_csum = disk_csum;
547 * Handle superblock details.
548 * We want to be able to handle multiple superblock formats
549 * so we have a common interface to them all, and an array of
550 * different handlers.
551 * We rely on user-space to write the initial superblock, and support
552 * reading and updating of superblocks.
553 * Interface methods are:
554 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
555 * loads and validates a superblock on dev.
556 * if refdev != NULL, compare superblocks on both devices
558 * 0 - dev has a superblock that is compatible with refdev
559 * 1 - dev has a superblock that is compatible and newer than refdev
560 * so dev should be used as the refdev in future
561 * -EINVAL superblock incompatible or invalid
562 * -othererror e.g. -EIO
564 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
565 * Verify that dev is acceptable into mddev.
566 * The first time, mddev->raid_disks will be 0, and data from
567 * dev should be merged in. Subsequent calls check that dev
568 * is new enough. Return 0 or -EINVAL
570 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
571 * Update the superblock for rdev with data in mddev
572 * This does not write to disc.
578 struct module *owner;
579 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version);
580 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
581 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
585 * load_super for 0.90.0
587 static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
589 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
595 * Calculate the position of the superblock,
596 * it's at the end of the disk.
598 * It also happens to be a multiple of 4Kb.
600 sb_offset = calc_dev_sboffset(rdev->bdev);
601 rdev->sb_offset = sb_offset;
603 ret = read_disk_sb(rdev, MD_SB_BYTES);
608 bdevname(rdev->bdev, b);
609 sb = (mdp_super_t*)page_address(rdev->sb_page);
611 if (sb->md_magic != MD_SB_MAGIC) {
612 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
617 if (sb->major_version != 0 ||
618 sb->minor_version != 90) {
619 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
620 sb->major_version, sb->minor_version,
625 if (sb->raid_disks <= 0)
628 if (csum_fold(calc_sb_csum(sb)) != csum_fold(sb->sb_csum)) {
629 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
634 rdev->preferred_minor = sb->md_minor;
635 rdev->data_offset = 0;
636 rdev->sb_size = MD_SB_BYTES;
638 if (sb->level == LEVEL_MULTIPATH)
641 rdev->desc_nr = sb->this_disk.number;
647 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
648 if (!uuid_equal(refsb, sb)) {
649 printk(KERN_WARNING "md: %s has different UUID to %s\n",
650 b, bdevname(refdev->bdev,b2));
653 if (!sb_equal(refsb, sb)) {
654 printk(KERN_WARNING "md: %s has same UUID"
655 " but different superblock to %s\n",
656 b, bdevname(refdev->bdev, b2));
660 ev2 = md_event(refsb);
666 rdev->size = calc_dev_size(rdev, sb->chunk_size);
673 * validate_super for 0.90.0
675 static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
678 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
680 rdev->raid_disk = -1;
682 if (mddev->raid_disks == 0) {
683 mddev->major_version = 0;
684 mddev->minor_version = sb->minor_version;
685 mddev->patch_version = sb->patch_version;
686 mddev->persistent = ! sb->not_persistent;
687 mddev->chunk_size = sb->chunk_size;
688 mddev->ctime = sb->ctime;
689 mddev->utime = sb->utime;
690 mddev->level = sb->level;
691 mddev->layout = sb->layout;
692 mddev->raid_disks = sb->raid_disks;
693 mddev->size = sb->size;
694 mddev->events = md_event(sb);
695 mddev->bitmap_offset = 0;
696 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
698 if (sb->state & (1<<MD_SB_CLEAN))
699 mddev->recovery_cp = MaxSector;
701 if (sb->events_hi == sb->cp_events_hi &&
702 sb->events_lo == sb->cp_events_lo) {
703 mddev->recovery_cp = sb->recovery_cp;
705 mddev->recovery_cp = 0;
708 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
709 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
710 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
711 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
713 mddev->max_disks = MD_SB_DISKS;
715 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
716 mddev->bitmap_file == NULL) {
717 if (mddev->level != 1 && mddev->level != 5 && mddev->level != 6) {
718 /* FIXME use a better test */
719 printk(KERN_WARNING "md: bitmaps only support for raid1\n");
722 mddev->bitmap_offset = mddev->default_bitmap_offset;
725 } else if (mddev->pers == NULL) {
726 /* Insist on good event counter while assembling */
727 __u64 ev1 = md_event(sb);
729 if (ev1 < mddev->events)
731 } else if (mddev->bitmap) {
732 /* if adding to array with a bitmap, then we can accept an
733 * older device ... but not too old.
735 __u64 ev1 = md_event(sb);
736 if (ev1 < mddev->bitmap->events_cleared)
738 } else /* just a hot-add of a new device, leave raid_disk at -1 */
741 if (mddev->level != LEVEL_MULTIPATH) {
742 desc = sb->disks + rdev->desc_nr;
744 if (desc->state & (1<<MD_DISK_FAULTY))
745 set_bit(Faulty, &rdev->flags);
746 else if (desc->state & (1<<MD_DISK_SYNC) &&
747 desc->raid_disk < mddev->raid_disks) {
748 set_bit(In_sync, &rdev->flags);
749 rdev->raid_disk = desc->raid_disk;
751 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
752 set_bit(WriteMostly, &rdev->flags);
753 } else /* MULTIPATH are always insync */
754 set_bit(In_sync, &rdev->flags);
759 * sync_super for 0.90.0
761 static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
764 struct list_head *tmp;
766 int next_spare = mddev->raid_disks;
769 /* make rdev->sb match mddev data..
772 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
773 * 3/ any empty disks < next_spare become removed
775 * disks[0] gets initialised to REMOVED because
776 * we cannot be sure from other fields if it has
777 * been initialised or not.
780 int active=0, working=0,failed=0,spare=0,nr_disks=0;
782 rdev->sb_size = MD_SB_BYTES;
784 sb = (mdp_super_t*)page_address(rdev->sb_page);
786 memset(sb, 0, sizeof(*sb));
788 sb->md_magic = MD_SB_MAGIC;
789 sb->major_version = mddev->major_version;
790 sb->minor_version = mddev->minor_version;
791 sb->patch_version = mddev->patch_version;
792 sb->gvalid_words = 0; /* ignored */
793 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
794 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
795 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
796 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
798 sb->ctime = mddev->ctime;
799 sb->level = mddev->level;
800 sb->size = mddev->size;
801 sb->raid_disks = mddev->raid_disks;
802 sb->md_minor = mddev->md_minor;
803 sb->not_persistent = !mddev->persistent;
804 sb->utime = mddev->utime;
806 sb->events_hi = (mddev->events>>32);
807 sb->events_lo = (u32)mddev->events;
811 sb->recovery_cp = mddev->recovery_cp;
812 sb->cp_events_hi = (mddev->events>>32);
813 sb->cp_events_lo = (u32)mddev->events;
814 if (mddev->recovery_cp == MaxSector)
815 sb->state = (1<< MD_SB_CLEAN);
819 sb->layout = mddev->layout;
820 sb->chunk_size = mddev->chunk_size;
822 if (mddev->bitmap && mddev->bitmap_file == NULL)
823 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
825 sb->disks[0].state = (1<<MD_DISK_REMOVED);
826 ITERATE_RDEV(mddev,rdev2,tmp) {
829 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
830 && !test_bit(Faulty, &rdev2->flags))
831 desc_nr = rdev2->raid_disk;
833 desc_nr = next_spare++;
834 rdev2->desc_nr = desc_nr;
835 d = &sb->disks[rdev2->desc_nr];
837 d->number = rdev2->desc_nr;
838 d->major = MAJOR(rdev2->bdev->bd_dev);
839 d->minor = MINOR(rdev2->bdev->bd_dev);
840 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
841 && !test_bit(Faulty, &rdev2->flags))
842 d->raid_disk = rdev2->raid_disk;
844 d->raid_disk = rdev2->desc_nr; /* compatibility */
845 if (test_bit(Faulty, &rdev2->flags)) {
846 d->state = (1<<MD_DISK_FAULTY);
848 } else if (test_bit(In_sync, &rdev2->flags)) {
849 d->state = (1<<MD_DISK_ACTIVE);
850 d->state |= (1<<MD_DISK_SYNC);
858 if (test_bit(WriteMostly, &rdev2->flags))
859 d->state |= (1<<MD_DISK_WRITEMOSTLY);
861 /* now set the "removed" and "faulty" bits on any missing devices */
862 for (i=0 ; i < mddev->raid_disks ; i++) {
863 mdp_disk_t *d = &sb->disks[i];
864 if (d->state == 0 && d->number == 0) {
867 d->state = (1<<MD_DISK_REMOVED);
868 d->state |= (1<<MD_DISK_FAULTY);
872 sb->nr_disks = nr_disks;
873 sb->active_disks = active;
874 sb->working_disks = working;
875 sb->failed_disks = failed;
876 sb->spare_disks = spare;
878 sb->this_disk = sb->disks[rdev->desc_nr];
879 sb->sb_csum = calc_sb_csum(sb);
883 * version 1 superblock
886 static unsigned int calc_sb_1_csum(struct mdp_superblock_1 * sb)
888 unsigned int disk_csum, csum;
889 unsigned long long newcsum;
890 int size = 256 + le32_to_cpu(sb->max_dev)*2;
891 unsigned int *isuper = (unsigned int*)sb;
894 disk_csum = sb->sb_csum;
897 for (i=0; size>=4; size -= 4 )
898 newcsum += le32_to_cpu(*isuper++);
901 newcsum += le16_to_cpu(*(unsigned short*) isuper);
903 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
904 sb->sb_csum = disk_csum;
905 return cpu_to_le32(csum);
908 static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
910 struct mdp_superblock_1 *sb;
913 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
917 * Calculate the position of the superblock.
918 * It is always aligned to a 4K boundary and
919 * depeding on minor_version, it can be:
920 * 0: At least 8K, but less than 12K, from end of device
921 * 1: At start of device
922 * 2: 4K from start of device.
924 switch(minor_version) {
926 sb_offset = rdev->bdev->bd_inode->i_size >> 9;
928 sb_offset &= ~(sector_t)(4*2-1);
929 /* convert from sectors to K */
941 rdev->sb_offset = sb_offset;
943 /* superblock is rarely larger than 1K, but it can be larger,
944 * and it is safe to read 4k, so we do that
946 ret = read_disk_sb(rdev, 4096);
950 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
952 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
953 sb->major_version != cpu_to_le32(1) ||
954 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
955 le64_to_cpu(sb->super_offset) != (rdev->sb_offset<<1) ||
956 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
959 if (calc_sb_1_csum(sb) != sb->sb_csum) {
960 printk("md: invalid superblock checksum on %s\n",
961 bdevname(rdev->bdev,b));
964 if (le64_to_cpu(sb->data_size) < 10) {
965 printk("md: data_size too small on %s\n",
966 bdevname(rdev->bdev,b));
969 rdev->preferred_minor = 0xffff;
970 rdev->data_offset = le64_to_cpu(sb->data_offset);
972 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
973 bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
974 if (rdev->sb_size & bmask)
975 rdev-> sb_size = (rdev->sb_size | bmask)+1;
981 struct mdp_superblock_1 *refsb =
982 (struct mdp_superblock_1*)page_address(refdev->sb_page);
984 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
985 sb->level != refsb->level ||
986 sb->layout != refsb->layout ||
987 sb->chunksize != refsb->chunksize) {
988 printk(KERN_WARNING "md: %s has strangely different"
989 " superblock to %s\n",
990 bdevname(rdev->bdev,b),
991 bdevname(refdev->bdev,b2));
994 ev1 = le64_to_cpu(sb->events);
995 ev2 = le64_to_cpu(refsb->events);
1001 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
1003 rdev->size = rdev->sb_offset;
1004 if (rdev->size < le64_to_cpu(sb->data_size)/2)
1006 rdev->size = le64_to_cpu(sb->data_size)/2;
1007 if (le32_to_cpu(sb->chunksize))
1008 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
1012 static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1014 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1016 rdev->raid_disk = -1;
1018 if (mddev->raid_disks == 0) {
1019 mddev->major_version = 1;
1020 mddev->patch_version = 0;
1021 mddev->persistent = 1;
1022 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
1023 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1024 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1025 mddev->level = le32_to_cpu(sb->level);
1026 mddev->layout = le32_to_cpu(sb->layout);
1027 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1028 mddev->size = le64_to_cpu(sb->size)/2;
1029 mddev->events = le64_to_cpu(sb->events);
1030 mddev->bitmap_offset = 0;
1031 mddev->default_bitmap_offset = 0;
1032 mddev->default_bitmap_offset = 1024;
1034 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1035 memcpy(mddev->uuid, sb->set_uuid, 16);
1037 mddev->max_disks = (4096-256)/2;
1039 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
1040 mddev->bitmap_file == NULL ) {
1041 if (mddev->level != 1) {
1042 printk(KERN_WARNING "md: bitmaps only supported for raid1\n");
1045 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
1047 } else if (mddev->pers == NULL) {
1048 /* Insist of good event counter while assembling */
1049 __u64 ev1 = le64_to_cpu(sb->events);
1051 if (ev1 < mddev->events)
1053 } else if (mddev->bitmap) {
1054 /* If adding to array with a bitmap, then we can accept an
1055 * older device, but not too old.
1057 __u64 ev1 = le64_to_cpu(sb->events);
1058 if (ev1 < mddev->bitmap->events_cleared)
1060 } else /* just a hot-add of a new device, leave raid_disk at -1 */
1063 if (mddev->level != LEVEL_MULTIPATH) {
1065 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1066 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1068 case 0xffff: /* spare */
1070 case 0xfffe: /* faulty */
1071 set_bit(Faulty, &rdev->flags);
1074 set_bit(In_sync, &rdev->flags);
1075 rdev->raid_disk = role;
1078 if (sb->devflags & WriteMostly1)
1079 set_bit(WriteMostly, &rdev->flags);
1080 } else /* MULTIPATH are always insync */
1081 set_bit(In_sync, &rdev->flags);
1086 static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1088 struct mdp_superblock_1 *sb;
1089 struct list_head *tmp;
1092 /* make rdev->sb match mddev and rdev data. */
1094 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1096 sb->feature_map = 0;
1098 memset(sb->pad1, 0, sizeof(sb->pad1));
1099 memset(sb->pad2, 0, sizeof(sb->pad2));
1100 memset(sb->pad3, 0, sizeof(sb->pad3));
1102 sb->utime = cpu_to_le64((__u64)mddev->utime);
1103 sb->events = cpu_to_le64(mddev->events);
1105 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1107 sb->resync_offset = cpu_to_le64(0);
1109 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1110 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
1111 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
1115 ITERATE_RDEV(mddev,rdev2,tmp)
1116 if (rdev2->desc_nr+1 > max_dev)
1117 max_dev = rdev2->desc_nr+1;
1119 sb->max_dev = cpu_to_le32(max_dev);
1120 for (i=0; i<max_dev;i++)
1121 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1123 ITERATE_RDEV(mddev,rdev2,tmp) {
1125 if (test_bit(Faulty, &rdev2->flags))
1126 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1127 else if (test_bit(In_sync, &rdev2->flags))
1128 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1130 sb->dev_roles[i] = cpu_to_le16(0xffff);
1133 sb->recovery_offset = cpu_to_le64(0); /* not supported yet */
1134 sb->sb_csum = calc_sb_1_csum(sb);
1138 static struct super_type super_types[] = {
1141 .owner = THIS_MODULE,
1142 .load_super = super_90_load,
1143 .validate_super = super_90_validate,
1144 .sync_super = super_90_sync,
1148 .owner = THIS_MODULE,
1149 .load_super = super_1_load,
1150 .validate_super = super_1_validate,
1151 .sync_super = super_1_sync,
1155 static mdk_rdev_t * match_dev_unit(mddev_t *mddev, mdk_rdev_t *dev)
1157 struct list_head *tmp;
1160 ITERATE_RDEV(mddev,rdev,tmp)
1161 if (rdev->bdev->bd_contains == dev->bdev->bd_contains)
1167 static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1169 struct list_head *tmp;
1172 ITERATE_RDEV(mddev1,rdev,tmp)
1173 if (match_dev_unit(mddev2, rdev))
1179 static LIST_HEAD(pending_raid_disks);
1181 static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1183 mdk_rdev_t *same_pdev;
1184 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1191 same_pdev = match_dev_unit(mddev, rdev);
1194 "%s: WARNING: %s appears to be on the same physical"
1195 " disk as %s. True\n protection against single-disk"
1196 " failure might be compromised.\n",
1197 mdname(mddev), bdevname(rdev->bdev,b),
1198 bdevname(same_pdev->bdev,b2));
1200 /* Verify rdev->desc_nr is unique.
1201 * If it is -1, assign a free number, else
1202 * check number is not in use
1204 if (rdev->desc_nr < 0) {
1206 if (mddev->pers) choice = mddev->raid_disks;
1207 while (find_rdev_nr(mddev, choice))
1209 rdev->desc_nr = choice;
1211 if (find_rdev_nr(mddev, rdev->desc_nr))
1214 bdevname(rdev->bdev,b);
1215 if (kobject_set_name(&rdev->kobj, "dev-%s", b) < 0)
1218 list_add(&rdev->same_set, &mddev->disks);
1219 rdev->mddev = mddev;
1220 printk(KERN_INFO "md: bind<%s>\n", b);
1222 rdev->kobj.parent = &mddev->kobj;
1223 kobject_add(&rdev->kobj);
1225 if (rdev->bdev->bd_part)
1226 ko = &rdev->bdev->bd_part->kobj;
1228 ko = &rdev->bdev->bd_disk->kobj;
1229 sysfs_create_link(&rdev->kobj, ko, "block");
1233 static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1235 char b[BDEVNAME_SIZE];
1240 list_del_init(&rdev->same_set);
1241 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1243 sysfs_remove_link(&rdev->kobj, "block");
1244 kobject_del(&rdev->kobj);
1248 * prevent the device from being mounted, repartitioned or
1249 * otherwise reused by a RAID array (or any other kernel
1250 * subsystem), by bd_claiming the device.
1252 static int lock_rdev(mdk_rdev_t *rdev, dev_t dev)
1255 struct block_device *bdev;
1256 char b[BDEVNAME_SIZE];
1258 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
1260 printk(KERN_ERR "md: could not open %s.\n",
1261 __bdevname(dev, b));
1262 return PTR_ERR(bdev);
1264 err = bd_claim(bdev, rdev);
1266 printk(KERN_ERR "md: could not bd_claim %s.\n",
1275 static void unlock_rdev(mdk_rdev_t *rdev)
1277 struct block_device *bdev = rdev->bdev;
1285 void md_autodetect_dev(dev_t dev);
1287 static void export_rdev(mdk_rdev_t * rdev)
1289 char b[BDEVNAME_SIZE];
1290 printk(KERN_INFO "md: export_rdev(%s)\n",
1291 bdevname(rdev->bdev,b));
1295 list_del_init(&rdev->same_set);
1297 md_autodetect_dev(rdev->bdev->bd_dev);
1300 kobject_put(&rdev->kobj);
1303 static void kick_rdev_from_array(mdk_rdev_t * rdev)
1305 unbind_rdev_from_array(rdev);
1309 static void export_array(mddev_t *mddev)
1311 struct list_head *tmp;
1314 ITERATE_RDEV(mddev,rdev,tmp) {
1319 kick_rdev_from_array(rdev);
1321 if (!list_empty(&mddev->disks))
1323 mddev->raid_disks = 0;
1324 mddev->major_version = 0;
1327 static void print_desc(mdp_disk_t *desc)
1329 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1330 desc->major,desc->minor,desc->raid_disk,desc->state);
1333 static void print_sb(mdp_super_t *sb)
1338 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1339 sb->major_version, sb->minor_version, sb->patch_version,
1340 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1342 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1343 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1344 sb->md_minor, sb->layout, sb->chunk_size);
1345 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1346 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1347 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1348 sb->failed_disks, sb->spare_disks,
1349 sb->sb_csum, (unsigned long)sb->events_lo);
1352 for (i = 0; i < MD_SB_DISKS; i++) {
1355 desc = sb->disks + i;
1356 if (desc->number || desc->major || desc->minor ||
1357 desc->raid_disk || (desc->state && (desc->state != 4))) {
1358 printk(" D %2d: ", i);
1362 printk(KERN_INFO "md: THIS: ");
1363 print_desc(&sb->this_disk);
1367 static void print_rdev(mdk_rdev_t *rdev)
1369 char b[BDEVNAME_SIZE];
1370 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1371 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
1372 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1374 if (rdev->sb_loaded) {
1375 printk(KERN_INFO "md: rdev superblock:\n");
1376 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1378 printk(KERN_INFO "md: no rdev superblock!\n");
1381 void md_print_devices(void)
1383 struct list_head *tmp, *tmp2;
1386 char b[BDEVNAME_SIZE];
1389 printk("md: **********************************\n");
1390 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1391 printk("md: **********************************\n");
1392 ITERATE_MDDEV(mddev,tmp) {
1395 bitmap_print_sb(mddev->bitmap);
1397 printk("%s: ", mdname(mddev));
1398 ITERATE_RDEV(mddev,rdev,tmp2)
1399 printk("<%s>", bdevname(rdev->bdev,b));
1402 ITERATE_RDEV(mddev,rdev,tmp2)
1405 printk("md: **********************************\n");
1410 static void sync_sbs(mddev_t * mddev)
1413 struct list_head *tmp;
1415 ITERATE_RDEV(mddev,rdev,tmp) {
1416 super_types[mddev->major_version].
1417 sync_super(mddev, rdev);
1418 rdev->sb_loaded = 1;
1422 static void md_update_sb(mddev_t * mddev)
1425 struct list_head *tmp;
1430 spin_lock_irq(&mddev->write_lock);
1431 sync_req = mddev->in_sync;
1432 mddev->utime = get_seconds();
1435 if (!mddev->events) {
1437 * oops, this 64-bit counter should never wrap.
1438 * Either we are in around ~1 trillion A.C., assuming
1439 * 1 reboot per second, or we have a bug:
1444 mddev->sb_dirty = 2;
1448 * do not write anything to disk if using
1449 * nonpersistent superblocks
1451 if (!mddev->persistent) {
1452 mddev->sb_dirty = 0;
1453 spin_unlock_irq(&mddev->write_lock);
1454 wake_up(&mddev->sb_wait);
1457 spin_unlock_irq(&mddev->write_lock);
1460 "md: updating %s RAID superblock on device (in sync %d)\n",
1461 mdname(mddev),mddev->in_sync);
1463 err = bitmap_update_sb(mddev->bitmap);
1464 ITERATE_RDEV(mddev,rdev,tmp) {
1465 char b[BDEVNAME_SIZE];
1466 dprintk(KERN_INFO "md: ");
1467 if (test_bit(Faulty, &rdev->flags))
1468 dprintk("(skipping faulty ");
1470 dprintk("%s ", bdevname(rdev->bdev,b));
1471 if (!test_bit(Faulty, &rdev->flags)) {
1472 md_super_write(mddev,rdev,
1473 rdev->sb_offset<<1, rdev->sb_size,
1475 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1476 bdevname(rdev->bdev,b),
1477 (unsigned long long)rdev->sb_offset);
1481 if (mddev->level == LEVEL_MULTIPATH)
1482 /* only need to write one superblock... */
1485 md_super_wait(mddev);
1486 /* if there was a failure, sb_dirty was set to 1, and we re-write super */
1488 spin_lock_irq(&mddev->write_lock);
1489 if (mddev->in_sync != sync_req|| mddev->sb_dirty == 1) {
1490 /* have to write it out again */
1491 spin_unlock_irq(&mddev->write_lock);
1494 mddev->sb_dirty = 0;
1495 spin_unlock_irq(&mddev->write_lock);
1496 wake_up(&mddev->sb_wait);
1500 struct rdev_sysfs_entry {
1501 struct attribute attr;
1502 ssize_t (*show)(mdk_rdev_t *, char *);
1503 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1507 state_show(mdk_rdev_t *rdev, char *page)
1512 if (test_bit(Faulty, &rdev->flags)) {
1513 len+= sprintf(page+len, "%sfaulty",sep);
1516 if (test_bit(In_sync, &rdev->flags)) {
1517 len += sprintf(page+len, "%sin_sync",sep);
1520 if (!test_bit(Faulty, &rdev->flags) &&
1521 !test_bit(In_sync, &rdev->flags)) {
1522 len += sprintf(page+len, "%sspare", sep);
1525 return len+sprintf(page+len, "\n");
1528 static struct rdev_sysfs_entry
1529 rdev_state = __ATTR_RO(state);
1532 super_show(mdk_rdev_t *rdev, char *page)
1534 if (rdev->sb_loaded && rdev->sb_size) {
1535 memcpy(page, page_address(rdev->sb_page), rdev->sb_size);
1536 return rdev->sb_size;
1540 static struct rdev_sysfs_entry rdev_super = __ATTR_RO(super);
1542 static struct attribute *rdev_default_attrs[] = {
1548 rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
1550 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1551 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1555 return entry->show(rdev, page);
1559 rdev_attr_store(struct kobject *kobj, struct attribute *attr,
1560 const char *page, size_t length)
1562 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1563 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1567 return entry->store(rdev, page, length);
1570 static void rdev_free(struct kobject *ko)
1572 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
1575 static struct sysfs_ops rdev_sysfs_ops = {
1576 .show = rdev_attr_show,
1577 .store = rdev_attr_store,
1579 static struct kobj_type rdev_ktype = {
1580 .release = rdev_free,
1581 .sysfs_ops = &rdev_sysfs_ops,
1582 .default_attrs = rdev_default_attrs,
1586 * Import a device. If 'super_format' >= 0, then sanity check the superblock
1588 * mark the device faulty if:
1590 * - the device is nonexistent (zero size)
1591 * - the device has no valid superblock
1593 * a faulty rdev _never_ has rdev->sb set.
1595 static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
1597 char b[BDEVNAME_SIZE];
1602 rdev = (mdk_rdev_t *) kmalloc(sizeof(*rdev), GFP_KERNEL);
1604 printk(KERN_ERR "md: could not alloc mem for new device!\n");
1605 return ERR_PTR(-ENOMEM);
1607 memset(rdev, 0, sizeof(*rdev));
1609 if ((err = alloc_disk_sb(rdev)))
1612 err = lock_rdev(rdev, newdev);
1616 rdev->kobj.parent = NULL;
1617 rdev->kobj.ktype = &rdev_ktype;
1618 kobject_init(&rdev->kobj);
1622 rdev->data_offset = 0;
1623 atomic_set(&rdev->nr_pending, 0);
1624 atomic_set(&rdev->read_errors, 0);
1626 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
1629 "md: %s has zero or unknown size, marking faulty!\n",
1630 bdevname(rdev->bdev,b));
1635 if (super_format >= 0) {
1636 err = super_types[super_format].
1637 load_super(rdev, NULL, super_minor);
1638 if (err == -EINVAL) {
1640 "md: %s has invalid sb, not importing!\n",
1641 bdevname(rdev->bdev,b));
1646 "md: could not read %s's sb, not importing!\n",
1647 bdevname(rdev->bdev,b));
1651 INIT_LIST_HEAD(&rdev->same_set);
1656 if (rdev->sb_page) {
1662 return ERR_PTR(err);
1666 * Check a full RAID array for plausibility
1670 static void analyze_sbs(mddev_t * mddev)
1673 struct list_head *tmp;
1674 mdk_rdev_t *rdev, *freshest;
1675 char b[BDEVNAME_SIZE];
1678 ITERATE_RDEV(mddev,rdev,tmp)
1679 switch (super_types[mddev->major_version].
1680 load_super(rdev, freshest, mddev->minor_version)) {
1688 "md: fatal superblock inconsistency in %s"
1689 " -- removing from array\n",
1690 bdevname(rdev->bdev,b));
1691 kick_rdev_from_array(rdev);
1695 super_types[mddev->major_version].
1696 validate_super(mddev, freshest);
1699 ITERATE_RDEV(mddev,rdev,tmp) {
1700 if (rdev != freshest)
1701 if (super_types[mddev->major_version].
1702 validate_super(mddev, rdev)) {
1703 printk(KERN_WARNING "md: kicking non-fresh %s"
1705 bdevname(rdev->bdev,b));
1706 kick_rdev_from_array(rdev);
1709 if (mddev->level == LEVEL_MULTIPATH) {
1710 rdev->desc_nr = i++;
1711 rdev->raid_disk = rdev->desc_nr;
1712 set_bit(In_sync, &rdev->flags);
1718 if (mddev->recovery_cp != MaxSector &&
1720 printk(KERN_ERR "md: %s: raid array is not clean"
1721 " -- starting background reconstruction\n",
1727 level_show(mddev_t *mddev, char *page)
1729 mdk_personality_t *p = mddev->pers;
1732 if (mddev->level >= 0)
1733 return sprintf(page, "RAID-%d\n", mddev->level);
1735 return sprintf(page, "%s\n", p->name);
1738 static struct md_sysfs_entry md_level = __ATTR_RO(level);
1741 raid_disks_show(mddev_t *mddev, char *page)
1743 return sprintf(page, "%d\n", mddev->raid_disks);
1746 static struct md_sysfs_entry md_raid_disks = __ATTR_RO(raid_disks);
1749 md_show_scan(mddev_t *mddev, char *page)
1751 char *type = "none";
1752 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
1753 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) {
1754 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
1755 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
1757 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1764 return sprintf(page, "%s\n", type);
1768 md_store_scan(mddev_t *mddev, const char *page, size_t len)
1772 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
1773 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
1776 if (mddev->pers && mddev->pers->sync_request)
1782 if (strcmp(page, "check")==0 || strcmp(page, "check\n")==0)
1783 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
1784 else if (strcmp(page, "repair")!=0 && strcmp(page, "repair\n")!=0)
1786 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
1787 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
1788 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1789 md_wakeup_thread(mddev->thread);
1794 mismatch_cnt_show(mddev_t *mddev, char *page)
1796 return sprintf(page, "%llu\n",
1797 (unsigned long long) mddev->resync_mismatches);
1800 static struct md_sysfs_entry
1801 md_scan_mode = __ATTR(scan_mode, S_IRUGO|S_IWUSR, md_show_scan, md_store_scan);
1804 static struct md_sysfs_entry
1805 md_mismatches = __ATTR_RO(mismatch_cnt);
1807 static struct attribute *md_default_attrs[] = {
1809 &md_raid_disks.attr,
1813 static struct attribute *md_redundancy_attrs[] = {
1815 &md_mismatches.attr,
1818 static struct attribute_group md_redundancy_group = {
1820 .attrs = md_redundancy_attrs,
1825 md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
1827 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
1828 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
1834 rv = entry->show(mddev, page);
1835 mddev_unlock(mddev);
1840 md_attr_store(struct kobject *kobj, struct attribute *attr,
1841 const char *page, size_t length)
1843 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
1844 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
1850 rv = entry->store(mddev, page, length);
1851 mddev_unlock(mddev);
1855 static void md_free(struct kobject *ko)
1857 mddev_t *mddev = container_of(ko, mddev_t, kobj);
1861 static struct sysfs_ops md_sysfs_ops = {
1862 .show = md_attr_show,
1863 .store = md_attr_store,
1865 static struct kobj_type md_ktype = {
1867 .sysfs_ops = &md_sysfs_ops,
1868 .default_attrs = md_default_attrs,
1873 static struct kobject *md_probe(dev_t dev, int *part, void *data)
1875 static DECLARE_MUTEX(disks_sem);
1876 mddev_t *mddev = mddev_find(dev);
1877 struct gendisk *disk;
1878 int partitioned = (MAJOR(dev) != MD_MAJOR);
1879 int shift = partitioned ? MdpMinorShift : 0;
1880 int unit = MINOR(dev) >> shift;
1886 if (mddev->gendisk) {
1891 disk = alloc_disk(1 << shift);
1897 disk->major = MAJOR(dev);
1898 disk->first_minor = unit << shift;
1900 sprintf(disk->disk_name, "md_d%d", unit);
1901 sprintf(disk->devfs_name, "md/d%d", unit);
1903 sprintf(disk->disk_name, "md%d", unit);
1904 sprintf(disk->devfs_name, "md/%d", unit);
1906 disk->fops = &md_fops;
1907 disk->private_data = mddev;
1908 disk->queue = mddev->queue;
1910 mddev->gendisk = disk;
1912 mddev->kobj.parent = &disk->kobj;
1913 mddev->kobj.k_name = NULL;
1914 snprintf(mddev->kobj.name, KOBJ_NAME_LEN, "%s", "md");
1915 mddev->kobj.ktype = &md_ktype;
1916 kobject_register(&mddev->kobj);
1920 void md_wakeup_thread(mdk_thread_t *thread);
1922 static void md_safemode_timeout(unsigned long data)
1924 mddev_t *mddev = (mddev_t *) data;
1926 mddev->safemode = 1;
1927 md_wakeup_thread(mddev->thread);
1931 static int do_md_run(mddev_t * mddev)
1935 struct list_head *tmp;
1937 struct gendisk *disk;
1938 char b[BDEVNAME_SIZE];
1940 if (list_empty(&mddev->disks))
1941 /* cannot run an array with no devices.. */
1948 * Analyze all RAID superblock(s)
1950 if (!mddev->raid_disks)
1953 chunk_size = mddev->chunk_size;
1954 pnum = level_to_pers(mddev->level);
1956 if ((pnum != MULTIPATH) && (pnum != RAID1)) {
1959 * 'default chunksize' in the old md code used to
1960 * be PAGE_SIZE, baaad.
1961 * we abort here to be on the safe side. We don't
1962 * want to continue the bad practice.
1965 "no chunksize specified, see 'man raidtab'\n");
1968 if (chunk_size > MAX_CHUNK_SIZE) {
1969 printk(KERN_ERR "too big chunk_size: %d > %d\n",
1970 chunk_size, MAX_CHUNK_SIZE);
1974 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
1976 if ( (1 << ffz(~chunk_size)) != chunk_size) {
1977 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
1980 if (chunk_size < PAGE_SIZE) {
1981 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
1982 chunk_size, PAGE_SIZE);
1986 /* devices must have minimum size of one chunk */
1987 ITERATE_RDEV(mddev,rdev,tmp) {
1988 if (test_bit(Faulty, &rdev->flags))
1990 if (rdev->size < chunk_size / 1024) {
1992 "md: Dev %s smaller than chunk_size:"
1994 bdevname(rdev->bdev,b),
1995 (unsigned long long)rdev->size,
2005 request_module("md-personality-%d", pnum);
2010 * Drop all container device buffers, from now on
2011 * the only valid external interface is through the md
2013 * Also find largest hardsector size
2015 ITERATE_RDEV(mddev,rdev,tmp) {
2016 if (test_bit(Faulty, &rdev->flags))
2018 sync_blockdev(rdev->bdev);
2019 invalidate_bdev(rdev->bdev, 0);
2022 md_probe(mddev->unit, NULL, NULL);
2023 disk = mddev->gendisk;
2027 spin_lock(&pers_lock);
2028 if (!pers[pnum] || !try_module_get(pers[pnum]->owner)) {
2029 spin_unlock(&pers_lock);
2030 printk(KERN_WARNING "md: personality %d is not loaded!\n",
2035 mddev->pers = pers[pnum];
2036 spin_unlock(&pers_lock);
2038 mddev->recovery = 0;
2039 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
2040 mddev->barriers_work = 1;
2043 mddev->ro = 2; /* read-only, but switch on first write */
2045 /* before we start the array running, initialise the bitmap */
2046 err = bitmap_create(mddev);
2048 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
2049 mdname(mddev), err);
2051 err = mddev->pers->run(mddev);
2053 printk(KERN_ERR "md: pers->run() failed ...\n");
2054 module_put(mddev->pers->owner);
2056 bitmap_destroy(mddev);
2059 if (mddev->pers->sync_request)
2060 sysfs_create_group(&mddev->kobj, &md_redundancy_group);
2061 atomic_set(&mddev->writes_pending,0);
2062 mddev->safemode = 0;
2063 mddev->safemode_timer.function = md_safemode_timeout;
2064 mddev->safemode_timer.data = (unsigned long) mddev;
2065 mddev->safemode_delay = (20 * HZ)/1000 +1; /* 20 msec delay */
2068 ITERATE_RDEV(mddev,rdev,tmp)
2069 if (rdev->raid_disk >= 0) {
2071 sprintf(nm, "rd%d", rdev->raid_disk);
2072 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
2075 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2076 md_wakeup_thread(mddev->thread);
2078 if (mddev->sb_dirty)
2079 md_update_sb(mddev);
2081 set_capacity(disk, mddev->array_size<<1);
2083 /* If we call blk_queue_make_request here, it will
2084 * re-initialise max_sectors etc which may have been
2085 * refined inside -> run. So just set the bits we need to set.
2086 * Most initialisation happended when we called
2087 * blk_queue_make_request(..., md_fail_request)
2090 mddev->queue->queuedata = mddev;
2091 mddev->queue->make_request_fn = mddev->pers->make_request;
2097 static int restart_array(mddev_t *mddev)
2099 struct gendisk *disk = mddev->gendisk;
2103 * Complain if it has no devices
2106 if (list_empty(&mddev->disks))
2114 mddev->safemode = 0;
2116 set_disk_ro(disk, 0);
2118 printk(KERN_INFO "md: %s switched to read-write mode.\n",
2121 * Kick recovery or resync if necessary
2123 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2124 md_wakeup_thread(mddev->thread);
2127 printk(KERN_ERR "md: %s has no personality assigned.\n",
2136 static int do_md_stop(mddev_t * mddev, int ro)
2139 struct gendisk *disk = mddev->gendisk;
2142 if (atomic_read(&mddev->active)>2) {
2143 printk("md: %s still in use.\n",mdname(mddev));
2147 if (mddev->sync_thread) {
2148 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2149 md_unregister_thread(mddev->sync_thread);
2150 mddev->sync_thread = NULL;
2153 del_timer_sync(&mddev->safemode_timer);
2155 invalidate_partition(disk, 0);
2163 bitmap_flush(mddev);
2164 md_super_wait(mddev);
2166 set_disk_ro(disk, 0);
2167 blk_queue_make_request(mddev->queue, md_fail_request);
2168 mddev->pers->stop(mddev);
2169 if (mddev->pers->sync_request)
2170 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
2172 module_put(mddev->pers->owner);
2177 if (!mddev->in_sync) {
2178 /* mark array as shutdown cleanly */
2180 md_update_sb(mddev);
2183 set_disk_ro(disk, 1);
2186 bitmap_destroy(mddev);
2187 if (mddev->bitmap_file) {
2188 atomic_set(&mddev->bitmap_file->f_dentry->d_inode->i_writecount, 1);
2189 fput(mddev->bitmap_file);
2190 mddev->bitmap_file = NULL;
2192 mddev->bitmap_offset = 0;
2195 * Free resources if final stop
2199 struct list_head *tmp;
2200 struct gendisk *disk;
2201 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
2203 ITERATE_RDEV(mddev,rdev,tmp)
2204 if (rdev->raid_disk >= 0) {
2206 sprintf(nm, "rd%d", rdev->raid_disk);
2207 sysfs_remove_link(&mddev->kobj, nm);
2210 export_array(mddev);
2212 mddev->array_size = 0;
2213 disk = mddev->gendisk;
2215 set_capacity(disk, 0);
2218 printk(KERN_INFO "md: %s switched to read-only mode.\n",
2225 static void autorun_array(mddev_t *mddev)
2228 struct list_head *tmp;
2231 if (list_empty(&mddev->disks))
2234 printk(KERN_INFO "md: running: ");
2236 ITERATE_RDEV(mddev,rdev,tmp) {
2237 char b[BDEVNAME_SIZE];
2238 printk("<%s>", bdevname(rdev->bdev,b));
2242 err = do_md_run (mddev);
2244 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
2245 do_md_stop (mddev, 0);
2250 * lets try to run arrays based on all disks that have arrived
2251 * until now. (those are in pending_raid_disks)
2253 * the method: pick the first pending disk, collect all disks with
2254 * the same UUID, remove all from the pending list and put them into
2255 * the 'same_array' list. Then order this list based on superblock
2256 * update time (freshest comes first), kick out 'old' disks and
2257 * compare superblocks. If everything's fine then run it.
2259 * If "unit" is allocated, then bump its reference count
2261 static void autorun_devices(int part)
2263 struct list_head candidates;
2264 struct list_head *tmp;
2265 mdk_rdev_t *rdev0, *rdev;
2267 char b[BDEVNAME_SIZE];
2269 printk(KERN_INFO "md: autorun ...\n");
2270 while (!list_empty(&pending_raid_disks)) {
2272 rdev0 = list_entry(pending_raid_disks.next,
2273 mdk_rdev_t, same_set);
2275 printk(KERN_INFO "md: considering %s ...\n",
2276 bdevname(rdev0->bdev,b));
2277 INIT_LIST_HEAD(&candidates);
2278 ITERATE_RDEV_PENDING(rdev,tmp)
2279 if (super_90_load(rdev, rdev0, 0) >= 0) {
2280 printk(KERN_INFO "md: adding %s ...\n",
2281 bdevname(rdev->bdev,b));
2282 list_move(&rdev->same_set, &candidates);
2285 * now we have a set of devices, with all of them having
2286 * mostly sane superblocks. It's time to allocate the
2289 if (rdev0->preferred_minor < 0 || rdev0->preferred_minor >= MAX_MD_DEVS) {
2290 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
2291 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
2295 dev = MKDEV(mdp_major,
2296 rdev0->preferred_minor << MdpMinorShift);
2298 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
2300 md_probe(dev, NULL, NULL);
2301 mddev = mddev_find(dev);
2304 "md: cannot allocate memory for md drive.\n");
2307 if (mddev_lock(mddev))
2308 printk(KERN_WARNING "md: %s locked, cannot run\n",
2310 else if (mddev->raid_disks || mddev->major_version
2311 || !list_empty(&mddev->disks)) {
2313 "md: %s already running, cannot run %s\n",
2314 mdname(mddev), bdevname(rdev0->bdev,b));
2315 mddev_unlock(mddev);
2317 printk(KERN_INFO "md: created %s\n", mdname(mddev));
2318 ITERATE_RDEV_GENERIC(candidates,rdev,tmp) {
2319 list_del_init(&rdev->same_set);
2320 if (bind_rdev_to_array(rdev, mddev))
2323 autorun_array(mddev);
2324 mddev_unlock(mddev);
2326 /* on success, candidates will be empty, on error
2329 ITERATE_RDEV_GENERIC(candidates,rdev,tmp)
2333 printk(KERN_INFO "md: ... autorun DONE.\n");
2337 * import RAID devices based on one partition
2338 * if possible, the array gets run as well.
2341 static int autostart_array(dev_t startdev)
2343 char b[BDEVNAME_SIZE];
2344 int err = -EINVAL, i;
2345 mdp_super_t *sb = NULL;
2346 mdk_rdev_t *start_rdev = NULL, *rdev;
2348 start_rdev = md_import_device(startdev, 0, 0);
2349 if (IS_ERR(start_rdev))
2353 /* NOTE: this can only work for 0.90.0 superblocks */
2354 sb = (mdp_super_t*)page_address(start_rdev->sb_page);
2355 if (sb->major_version != 0 ||
2356 sb->minor_version != 90 ) {
2357 printk(KERN_WARNING "md: can only autostart 0.90.0 arrays\n");
2358 export_rdev(start_rdev);
2362 if (test_bit(Faulty, &start_rdev->flags)) {
2364 "md: can not autostart based on faulty %s!\n",
2365 bdevname(start_rdev->bdev,b));
2366 export_rdev(start_rdev);
2369 list_add(&start_rdev->same_set, &pending_raid_disks);
2371 for (i = 0; i < MD_SB_DISKS; i++) {
2372 mdp_disk_t *desc = sb->disks + i;
2373 dev_t dev = MKDEV(desc->major, desc->minor);
2377 if (dev == startdev)
2379 if (MAJOR(dev) != desc->major || MINOR(dev) != desc->minor)
2381 rdev = md_import_device(dev, 0, 0);
2385 list_add(&rdev->same_set, &pending_raid_disks);
2389 * possibly return codes
2397 static int get_version(void __user * arg)
2401 ver.major = MD_MAJOR_VERSION;
2402 ver.minor = MD_MINOR_VERSION;
2403 ver.patchlevel = MD_PATCHLEVEL_VERSION;
2405 if (copy_to_user(arg, &ver, sizeof(ver)))
2411 static int get_array_info(mddev_t * mddev, void __user * arg)
2413 mdu_array_info_t info;
2414 int nr,working,active,failed,spare;
2416 struct list_head *tmp;
2418 nr=working=active=failed=spare=0;
2419 ITERATE_RDEV(mddev,rdev,tmp) {
2421 if (test_bit(Faulty, &rdev->flags))
2425 if (test_bit(In_sync, &rdev->flags))
2432 info.major_version = mddev->major_version;
2433 info.minor_version = mddev->minor_version;
2434 info.patch_version = MD_PATCHLEVEL_VERSION;
2435 info.ctime = mddev->ctime;
2436 info.level = mddev->level;
2437 info.size = mddev->size;
2439 info.raid_disks = mddev->raid_disks;
2440 info.md_minor = mddev->md_minor;
2441 info.not_persistent= !mddev->persistent;
2443 info.utime = mddev->utime;
2446 info.state = (1<<MD_SB_CLEAN);
2447 if (mddev->bitmap && mddev->bitmap_offset)
2448 info.state = (1<<MD_SB_BITMAP_PRESENT);
2449 info.active_disks = active;
2450 info.working_disks = working;
2451 info.failed_disks = failed;
2452 info.spare_disks = spare;
2454 info.layout = mddev->layout;
2455 info.chunk_size = mddev->chunk_size;
2457 if (copy_to_user(arg, &info, sizeof(info)))
2463 static int get_bitmap_file(mddev_t * mddev, void __user * arg)
2465 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
2466 char *ptr, *buf = NULL;
2469 file = kmalloc(sizeof(*file), GFP_KERNEL);
2473 /* bitmap disabled, zero the first byte and copy out */
2474 if (!mddev->bitmap || !mddev->bitmap->file) {
2475 file->pathname[0] = '\0';
2479 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
2483 ptr = file_path(mddev->bitmap->file, buf, sizeof(file->pathname));
2487 strcpy(file->pathname, ptr);
2491 if (copy_to_user(arg, file, sizeof(*file)))
2499 static int get_disk_info(mddev_t * mddev, void __user * arg)
2501 mdu_disk_info_t info;
2505 if (copy_from_user(&info, arg, sizeof(info)))
2510 rdev = find_rdev_nr(mddev, nr);
2512 info.major = MAJOR(rdev->bdev->bd_dev);
2513 info.minor = MINOR(rdev->bdev->bd_dev);
2514 info.raid_disk = rdev->raid_disk;
2516 if (test_bit(Faulty, &rdev->flags))
2517 info.state |= (1<<MD_DISK_FAULTY);
2518 else if (test_bit(In_sync, &rdev->flags)) {
2519 info.state |= (1<<MD_DISK_ACTIVE);
2520 info.state |= (1<<MD_DISK_SYNC);
2522 if (test_bit(WriteMostly, &rdev->flags))
2523 info.state |= (1<<MD_DISK_WRITEMOSTLY);
2525 info.major = info.minor = 0;
2526 info.raid_disk = -1;
2527 info.state = (1<<MD_DISK_REMOVED);
2530 if (copy_to_user(arg, &info, sizeof(info)))
2536 static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
2538 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
2540 dev_t dev = MKDEV(info->major,info->minor);
2542 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
2545 if (!mddev->raid_disks) {
2547 /* expecting a device which has a superblock */
2548 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
2551 "md: md_import_device returned %ld\n",
2553 return PTR_ERR(rdev);
2555 if (!list_empty(&mddev->disks)) {
2556 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2557 mdk_rdev_t, same_set);
2558 int err = super_types[mddev->major_version]
2559 .load_super(rdev, rdev0, mddev->minor_version);
2562 "md: %s has different UUID to %s\n",
2563 bdevname(rdev->bdev,b),
2564 bdevname(rdev0->bdev,b2));
2569 err = bind_rdev_to_array(rdev, mddev);
2576 * add_new_disk can be used once the array is assembled
2577 * to add "hot spares". They must already have a superblock
2582 if (!mddev->pers->hot_add_disk) {
2584 "%s: personality does not support diskops!\n",
2588 if (mddev->persistent)
2589 rdev = md_import_device(dev, mddev->major_version,
2590 mddev->minor_version);
2592 rdev = md_import_device(dev, -1, -1);
2595 "md: md_import_device returned %ld\n",
2597 return PTR_ERR(rdev);
2599 /* set save_raid_disk if appropriate */
2600 if (!mddev->persistent) {
2601 if (info->state & (1<<MD_DISK_SYNC) &&
2602 info->raid_disk < mddev->raid_disks)
2603 rdev->raid_disk = info->raid_disk;
2605 rdev->raid_disk = -1;
2607 super_types[mddev->major_version].
2608 validate_super(mddev, rdev);
2609 rdev->saved_raid_disk = rdev->raid_disk;
2611 clear_bit(In_sync, &rdev->flags); /* just to be sure */
2612 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
2613 set_bit(WriteMostly, &rdev->flags);
2615 rdev->raid_disk = -1;
2616 err = bind_rdev_to_array(rdev, mddev);
2620 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2621 md_wakeup_thread(mddev->thread);
2625 /* otherwise, add_new_disk is only allowed
2626 * for major_version==0 superblocks
2628 if (mddev->major_version != 0) {
2629 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
2634 if (!(info->state & (1<<MD_DISK_FAULTY))) {
2636 rdev = md_import_device (dev, -1, 0);
2639 "md: error, md_import_device() returned %ld\n",
2641 return PTR_ERR(rdev);
2643 rdev->desc_nr = info->number;
2644 if (info->raid_disk < mddev->raid_disks)
2645 rdev->raid_disk = info->raid_disk;
2647 rdev->raid_disk = -1;
2651 if (rdev->raid_disk < mddev->raid_disks)
2652 if (info->state & (1<<MD_DISK_SYNC))
2653 set_bit(In_sync, &rdev->flags);
2655 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
2656 set_bit(WriteMostly, &rdev->flags);
2658 err = bind_rdev_to_array(rdev, mddev);
2664 if (!mddev->persistent) {
2665 printk(KERN_INFO "md: nonpersistent superblock ...\n");
2666 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2668 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
2669 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
2671 if (!mddev->size || (mddev->size > rdev->size))
2672 mddev->size = rdev->size;
2678 static int hot_remove_disk(mddev_t * mddev, dev_t dev)
2680 char b[BDEVNAME_SIZE];
2686 rdev = find_rdev(mddev, dev);
2690 if (rdev->raid_disk >= 0)
2693 kick_rdev_from_array(rdev);
2694 md_update_sb(mddev);
2698 printk(KERN_WARNING "md: cannot remove active disk %s from %s ... \n",
2699 bdevname(rdev->bdev,b), mdname(mddev));
2703 static int hot_add_disk(mddev_t * mddev, dev_t dev)
2705 char b[BDEVNAME_SIZE];
2713 if (mddev->major_version != 0) {
2714 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
2715 " version-0 superblocks.\n",
2719 if (!mddev->pers->hot_add_disk) {
2721 "%s: personality does not support diskops!\n",
2726 rdev = md_import_device (dev, -1, 0);
2729 "md: error, md_import_device() returned %ld\n",
2734 if (mddev->persistent)
2735 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
2738 rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2740 size = calc_dev_size(rdev, mddev->chunk_size);
2743 if (size < mddev->size) {
2745 "%s: disk size %llu blocks < array size %llu\n",
2746 mdname(mddev), (unsigned long long)size,
2747 (unsigned long long)mddev->size);
2752 if (test_bit(Faulty, &rdev->flags)) {
2754 "md: can not hot-add faulty %s disk to %s!\n",
2755 bdevname(rdev->bdev,b), mdname(mddev));
2759 clear_bit(In_sync, &rdev->flags);
2761 bind_rdev_to_array(rdev, mddev);
2764 * The rest should better be atomic, we can have disk failures
2765 * noticed in interrupt contexts ...
2768 if (rdev->desc_nr == mddev->max_disks) {
2769 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
2772 goto abort_unbind_export;
2775 rdev->raid_disk = -1;
2777 md_update_sb(mddev);
2780 * Kick recovery, maybe this spare has to be added to the
2781 * array immediately.
2783 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2784 md_wakeup_thread(mddev->thread);
2788 abort_unbind_export:
2789 unbind_rdev_from_array(rdev);
2796 /* similar to deny_write_access, but accounts for our holding a reference
2797 * to the file ourselves */
2798 static int deny_bitmap_write_access(struct file * file)
2800 struct inode *inode = file->f_mapping->host;
2802 spin_lock(&inode->i_lock);
2803 if (atomic_read(&inode->i_writecount) > 1) {
2804 spin_unlock(&inode->i_lock);
2807 atomic_set(&inode->i_writecount, -1);
2808 spin_unlock(&inode->i_lock);
2813 static int set_bitmap_file(mddev_t *mddev, int fd)
2818 if (!mddev->pers->quiesce)
2820 if (mddev->recovery || mddev->sync_thread)
2822 /* we should be able to change the bitmap.. */
2828 return -EEXIST; /* cannot add when bitmap is present */
2829 mddev->bitmap_file = fget(fd);
2831 if (mddev->bitmap_file == NULL) {
2832 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
2837 err = deny_bitmap_write_access(mddev->bitmap_file);
2839 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
2841 fput(mddev->bitmap_file);
2842 mddev->bitmap_file = NULL;
2845 mddev->bitmap_offset = 0; /* file overrides offset */
2846 } else if (mddev->bitmap == NULL)
2847 return -ENOENT; /* cannot remove what isn't there */
2850 mddev->pers->quiesce(mddev, 1);
2852 err = bitmap_create(mddev);
2854 bitmap_destroy(mddev);
2855 mddev->pers->quiesce(mddev, 0);
2856 } else if (fd < 0) {
2857 if (mddev->bitmap_file)
2858 fput(mddev->bitmap_file);
2859 mddev->bitmap_file = NULL;
2866 * set_array_info is used two different ways
2867 * The original usage is when creating a new array.
2868 * In this usage, raid_disks is > 0 and it together with
2869 * level, size, not_persistent,layout,chunksize determine the
2870 * shape of the array.
2871 * This will always create an array with a type-0.90.0 superblock.
2872 * The newer usage is when assembling an array.
2873 * In this case raid_disks will be 0, and the major_version field is
2874 * use to determine which style super-blocks are to be found on the devices.
2875 * The minor and patch _version numbers are also kept incase the
2876 * super_block handler wishes to interpret them.
2878 static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
2881 if (info->raid_disks == 0) {
2882 /* just setting version number for superblock loading */
2883 if (info->major_version < 0 ||
2884 info->major_version >= sizeof(super_types)/sizeof(super_types[0]) ||
2885 super_types[info->major_version].name == NULL) {
2886 /* maybe try to auto-load a module? */
2888 "md: superblock version %d not known\n",
2889 info->major_version);
2892 mddev->major_version = info->major_version;
2893 mddev->minor_version = info->minor_version;
2894 mddev->patch_version = info->patch_version;
2897 mddev->major_version = MD_MAJOR_VERSION;
2898 mddev->minor_version = MD_MINOR_VERSION;
2899 mddev->patch_version = MD_PATCHLEVEL_VERSION;
2900 mddev->ctime = get_seconds();
2902 mddev->level = info->level;
2903 mddev->size = info->size;
2904 mddev->raid_disks = info->raid_disks;
2905 /* don't set md_minor, it is determined by which /dev/md* was
2908 if (info->state & (1<<MD_SB_CLEAN))
2909 mddev->recovery_cp = MaxSector;
2911 mddev->recovery_cp = 0;
2912 mddev->persistent = ! info->not_persistent;
2914 mddev->layout = info->layout;
2915 mddev->chunk_size = info->chunk_size;
2917 mddev->max_disks = MD_SB_DISKS;
2919 mddev->sb_dirty = 1;
2922 * Generate a 128 bit UUID
2924 get_random_bytes(mddev->uuid, 16);
2930 * update_array_info is used to change the configuration of an
2932 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
2933 * fields in the info are checked against the array.
2934 * Any differences that cannot be handled will cause an error.
2935 * Normally, only one change can be managed at a time.
2937 static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
2943 /* calculate expected state,ignoring low bits */
2944 if (mddev->bitmap && mddev->bitmap_offset)
2945 state |= (1 << MD_SB_BITMAP_PRESENT);
2947 if (mddev->major_version != info->major_version ||
2948 mddev->minor_version != info->minor_version ||
2949 /* mddev->patch_version != info->patch_version || */
2950 mddev->ctime != info->ctime ||
2951 mddev->level != info->level ||
2952 /* mddev->layout != info->layout || */
2953 !mddev->persistent != info->not_persistent||
2954 mddev->chunk_size != info->chunk_size ||
2955 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
2956 ((state^info->state) & 0xfffffe00)
2959 /* Check there is only one change */
2960 if (mddev->size != info->size) cnt++;
2961 if (mddev->raid_disks != info->raid_disks) cnt++;
2962 if (mddev->layout != info->layout) cnt++;
2963 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
2964 if (cnt == 0) return 0;
2965 if (cnt > 1) return -EINVAL;
2967 if (mddev->layout != info->layout) {
2969 * we don't need to do anything at the md level, the
2970 * personality will take care of it all.
2972 if (mddev->pers->reconfig == NULL)
2975 return mddev->pers->reconfig(mddev, info->layout, -1);
2977 if (mddev->size != info->size) {
2979 struct list_head *tmp;
2980 if (mddev->pers->resize == NULL)
2982 /* The "size" is the amount of each device that is used.
2983 * This can only make sense for arrays with redundancy.
2984 * linear and raid0 always use whatever space is available
2985 * We can only consider changing the size if no resync
2986 * or reconstruction is happening, and if the new size
2987 * is acceptable. It must fit before the sb_offset or,
2988 * if that is <data_offset, it must fit before the
2989 * size of each device.
2990 * If size is zero, we find the largest size that fits.
2992 if (mddev->sync_thread)
2994 ITERATE_RDEV(mddev,rdev,tmp) {
2996 int fit = (info->size == 0);
2997 if (rdev->sb_offset > rdev->data_offset)
2998 avail = (rdev->sb_offset*2) - rdev->data_offset;
3000 avail = get_capacity(rdev->bdev->bd_disk)
3001 - rdev->data_offset;
3002 if (fit && (info->size == 0 || info->size > avail/2))
3003 info->size = avail/2;
3004 if (avail < ((sector_t)info->size << 1))
3007 rv = mddev->pers->resize(mddev, (sector_t)info->size *2);
3009 struct block_device *bdev;
3011 bdev = bdget_disk(mddev->gendisk, 0);
3013 down(&bdev->bd_inode->i_sem);
3014 i_size_write(bdev->bd_inode, mddev->array_size << 10);
3015 up(&bdev->bd_inode->i_sem);
3020 if (mddev->raid_disks != info->raid_disks) {
3021 /* change the number of raid disks */
3022 if (mddev->pers->reshape == NULL)
3024 if (info->raid_disks <= 0 ||
3025 info->raid_disks >= mddev->max_disks)
3027 if (mddev->sync_thread)
3029 rv = mddev->pers->reshape(mddev, info->raid_disks);
3031 struct block_device *bdev;
3033 bdev = bdget_disk(mddev->gendisk, 0);
3035 down(&bdev->bd_inode->i_sem);
3036 i_size_write(bdev->bd_inode, mddev->array_size << 10);
3037 up(&bdev->bd_inode->i_sem);
3042 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
3043 if (mddev->pers->quiesce == NULL)
3045 if (mddev->recovery || mddev->sync_thread)
3047 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
3048 /* add the bitmap */
3051 if (mddev->default_bitmap_offset == 0)
3053 mddev->bitmap_offset = mddev->default_bitmap_offset;
3054 mddev->pers->quiesce(mddev, 1);
3055 rv = bitmap_create(mddev);
3057 bitmap_destroy(mddev);
3058 mddev->pers->quiesce(mddev, 0);
3060 /* remove the bitmap */
3063 if (mddev->bitmap->file)
3065 mddev->pers->quiesce(mddev, 1);
3066 bitmap_destroy(mddev);
3067 mddev->pers->quiesce(mddev, 0);
3068 mddev->bitmap_offset = 0;
3071 md_update_sb(mddev);
3075 static int set_disk_faulty(mddev_t *mddev, dev_t dev)
3079 if (mddev->pers == NULL)
3082 rdev = find_rdev(mddev, dev);
3086 md_error(mddev, rdev);
3090 static int md_ioctl(struct inode *inode, struct file *file,
3091 unsigned int cmd, unsigned long arg)
3094 void __user *argp = (void __user *)arg;
3095 struct hd_geometry __user *loc = argp;
3096 mddev_t *mddev = NULL;
3098 if (!capable(CAP_SYS_ADMIN))
3102 * Commands dealing with the RAID driver but not any
3108 err = get_version(argp);
3111 case PRINT_RAID_DEBUG:
3119 autostart_arrays(arg);
3126 * Commands creating/starting a new array:
3129 mddev = inode->i_bdev->bd_disk->private_data;
3137 if (cmd == START_ARRAY) {
3138 /* START_ARRAY doesn't need to lock the array as autostart_array
3139 * does the locking, and it could even be a different array
3144 "md: %s(pid %d) used deprecated START_ARRAY ioctl. "
3145 "This will not be supported beyond 2.6\n",
3146 current->comm, current->pid);
3149 err = autostart_array(new_decode_dev(arg));
3151 printk(KERN_WARNING "md: autostart failed!\n");
3157 err = mddev_lock(mddev);
3160 "md: ioctl lock interrupted, reason %d, cmd %d\n",
3167 case SET_ARRAY_INFO:
3169 mdu_array_info_t info;
3171 memset(&info, 0, sizeof(info));
3172 else if (copy_from_user(&info, argp, sizeof(info))) {
3177 err = update_array_info(mddev, &info);
3179 printk(KERN_WARNING "md: couldn't update"
3180 " array info. %d\n", err);
3185 if (!list_empty(&mddev->disks)) {
3187 "md: array %s already has disks!\n",
3192 if (mddev->raid_disks) {
3194 "md: array %s already initialised!\n",
3199 err = set_array_info(mddev, &info);
3201 printk(KERN_WARNING "md: couldn't set"
3202 " array info. %d\n", err);
3212 * Commands querying/configuring an existing array:
3214 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
3215 * RUN_ARRAY, and SET_BITMAP_FILE are allowed */
3216 if (!mddev->raid_disks && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
3217 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE) {
3223 * Commands even a read-only array can execute:
3227 case GET_ARRAY_INFO:
3228 err = get_array_info(mddev, argp);
3231 case GET_BITMAP_FILE:
3232 err = get_bitmap_file(mddev, argp);
3236 err = get_disk_info(mddev, argp);
3239 case RESTART_ARRAY_RW:
3240 err = restart_array(mddev);
3244 err = do_md_stop (mddev, 0);
3248 err = do_md_stop (mddev, 1);
3252 * We have a problem here : there is no easy way to give a CHS
3253 * virtual geometry. We currently pretend that we have a 2 heads
3254 * 4 sectors (with a BIG number of cylinders...). This drives
3255 * dosfs just mad... ;-)
3262 err = put_user (2, (char __user *) &loc->heads);
3265 err = put_user (4, (char __user *) &loc->sectors);
3268 err = put_user(get_capacity(mddev->gendisk)/8,
3269 (short __user *) &loc->cylinders);
3272 err = put_user (get_start_sect(inode->i_bdev),
3273 (long __user *) &loc->start);
3278 * The remaining ioctls are changing the state of the
3279 * superblock, so we do not allow them on read-only arrays.
3280 * However non-MD ioctls (e.g. get-size) will still come through
3281 * here and hit the 'default' below, so only disallow
3282 * 'md' ioctls, and switch to rw mode if started auto-readonly.
3284 if (_IOC_TYPE(cmd) == MD_MAJOR &&
3285 mddev->ro && mddev->pers) {
3286 if (mddev->ro == 2) {
3288 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3289 md_wakeup_thread(mddev->thread);
3301 mdu_disk_info_t info;
3302 if (copy_from_user(&info, argp, sizeof(info)))
3305 err = add_new_disk(mddev, &info);
3309 case HOT_REMOVE_DISK:
3310 err = hot_remove_disk(mddev, new_decode_dev(arg));
3314 err = hot_add_disk(mddev, new_decode_dev(arg));
3317 case SET_DISK_FAULTY:
3318 err = set_disk_faulty(mddev, new_decode_dev(arg));
3322 err = do_md_run (mddev);
3325 case SET_BITMAP_FILE:
3326 err = set_bitmap_file(mddev, (int)arg);
3330 if (_IOC_TYPE(cmd) == MD_MAJOR)
3331 printk(KERN_WARNING "md: %s(pid %d) used"
3332 " obsolete MD ioctl, upgrade your"
3333 " software to use new ictls.\n",
3334 current->comm, current->pid);
3341 mddev_unlock(mddev);
3351 static int md_open(struct inode *inode, struct file *file)
3354 * Succeed if we can lock the mddev, which confirms that
3355 * it isn't being stopped right now.
3357 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
3360 if ((err = mddev_lock(mddev)))
3365 mddev_unlock(mddev);
3367 check_disk_change(inode->i_bdev);
3372 static int md_release(struct inode *inode, struct file * file)
3374 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
3383 static int md_media_changed(struct gendisk *disk)
3385 mddev_t *mddev = disk->private_data;
3387 return mddev->changed;
3390 static int md_revalidate(struct gendisk *disk)
3392 mddev_t *mddev = disk->private_data;
3397 static struct block_device_operations md_fops =
3399 .owner = THIS_MODULE,
3401 .release = md_release,
3403 .media_changed = md_media_changed,
3404 .revalidate_disk= md_revalidate,
3407 static int md_thread(void * arg)
3409 mdk_thread_t *thread = arg;
3412 * md_thread is a 'system-thread', it's priority should be very
3413 * high. We avoid resource deadlocks individually in each
3414 * raid personality. (RAID5 does preallocation) We also use RR and
3415 * the very same RT priority as kswapd, thus we will never get
3416 * into a priority inversion deadlock.
3418 * we definitely have to have equal or higher priority than
3419 * bdflush, otherwise bdflush will deadlock if there are too
3420 * many dirty RAID5 blocks.
3423 allow_signal(SIGKILL);
3424 complete(thread->event);
3425 while (!kthread_should_stop()) {
3426 void (*run)(mddev_t *);
3428 wait_event_interruptible_timeout(thread->wqueue,
3429 test_bit(THREAD_WAKEUP, &thread->flags)
3430 || kthread_should_stop(),
3434 clear_bit(THREAD_WAKEUP, &thread->flags);
3444 void md_wakeup_thread(mdk_thread_t *thread)
3447 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
3448 set_bit(THREAD_WAKEUP, &thread->flags);
3449 wake_up(&thread->wqueue);
3453 mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
3456 mdk_thread_t *thread;
3457 struct completion event;
3459 thread = kmalloc(sizeof(mdk_thread_t), GFP_KERNEL);
3463 memset(thread, 0, sizeof(mdk_thread_t));
3464 init_waitqueue_head(&thread->wqueue);
3466 init_completion(&event);
3467 thread->event = &event;
3469 thread->mddev = mddev;
3470 thread->name = name;
3471 thread->timeout = MAX_SCHEDULE_TIMEOUT;
3472 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
3473 if (IS_ERR(thread->tsk)) {
3477 wait_for_completion(&event);
3481 void md_unregister_thread(mdk_thread_t *thread)
3483 dprintk("interrupting MD-thread pid %d\n", thread->tsk->pid);
3485 kthread_stop(thread->tsk);
3489 void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
3496 if (!rdev || test_bit(Faulty, &rdev->flags))
3499 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
3501 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
3502 __builtin_return_address(0),__builtin_return_address(1),
3503 __builtin_return_address(2),__builtin_return_address(3));
3505 if (!mddev->pers->error_handler)
3507 mddev->pers->error_handler(mddev,rdev);
3508 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3509 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3510 md_wakeup_thread(mddev->thread);
3513 /* seq_file implementation /proc/mdstat */
3515 static void status_unused(struct seq_file *seq)
3519 struct list_head *tmp;
3521 seq_printf(seq, "unused devices: ");
3523 ITERATE_RDEV_PENDING(rdev,tmp) {
3524 char b[BDEVNAME_SIZE];
3526 seq_printf(seq, "%s ",
3527 bdevname(rdev->bdev,b));
3530 seq_printf(seq, "<none>");
3532 seq_printf(seq, "\n");
3536 static void status_resync(struct seq_file *seq, mddev_t * mddev)
3538 unsigned long max_blocks, resync, res, dt, db, rt;
3540 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
3542 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3543 max_blocks = mddev->resync_max_sectors >> 1;
3545 max_blocks = mddev->size;
3548 * Should not happen.
3554 res = (resync/1024)*1000/(max_blocks/1024 + 1);
3556 int i, x = res/50, y = 20-x;
3557 seq_printf(seq, "[");
3558 for (i = 0; i < x; i++)
3559 seq_printf(seq, "=");
3560 seq_printf(seq, ">");
3561 for (i = 0; i < y; i++)
3562 seq_printf(seq, ".");
3563 seq_printf(seq, "] ");
3565 seq_printf(seq, " %s =%3lu.%lu%% (%lu/%lu)",
3566 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
3567 "resync" : "recovery"),
3568 res/10, res % 10, resync, max_blocks);
3571 * We do not want to overflow, so the order of operands and
3572 * the * 100 / 100 trick are important. We do a +1 to be
3573 * safe against division by zero. We only estimate anyway.
3575 * dt: time from mark until now
3576 * db: blocks written from mark until now
3577 * rt: remaining time
3579 dt = ((jiffies - mddev->resync_mark) / HZ);
3581 db = resync - (mddev->resync_mark_cnt/2);
3582 rt = (dt * ((max_blocks-resync) / (db/100+1)))/100;
3584 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
3586 seq_printf(seq, " speed=%ldK/sec", db/dt);
3589 static void *md_seq_start(struct seq_file *seq, loff_t *pos)
3591 struct list_head *tmp;
3601 spin_lock(&all_mddevs_lock);
3602 list_for_each(tmp,&all_mddevs)
3604 mddev = list_entry(tmp, mddev_t, all_mddevs);
3606 spin_unlock(&all_mddevs_lock);
3609 spin_unlock(&all_mddevs_lock);
3611 return (void*)2;/* tail */
3615 static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3617 struct list_head *tmp;
3618 mddev_t *next_mddev, *mddev = v;
3624 spin_lock(&all_mddevs_lock);
3626 tmp = all_mddevs.next;
3628 tmp = mddev->all_mddevs.next;
3629 if (tmp != &all_mddevs)
3630 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
3632 next_mddev = (void*)2;
3635 spin_unlock(&all_mddevs_lock);
3643 static void md_seq_stop(struct seq_file *seq, void *v)
3647 if (mddev && v != (void*)1 && v != (void*)2)
3651 static int md_seq_show(struct seq_file *seq, void *v)
3655 struct list_head *tmp2;
3658 struct bitmap *bitmap;
3660 if (v == (void*)1) {
3661 seq_printf(seq, "Personalities : ");
3662 spin_lock(&pers_lock);
3663 for (i = 0; i < MAX_PERSONALITY; i++)
3665 seq_printf(seq, "[%s] ", pers[i]->name);
3667 spin_unlock(&pers_lock);
3668 seq_printf(seq, "\n");
3671 if (v == (void*)2) {
3676 if (mddev_lock(mddev)!=0)
3678 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
3679 seq_printf(seq, "%s : %sactive", mdname(mddev),
3680 mddev->pers ? "" : "in");
3683 seq_printf(seq, " (read-only)");
3685 seq_printf(seq, "(auto-read-only)");
3686 seq_printf(seq, " %s", mddev->pers->name);
3690 ITERATE_RDEV(mddev,rdev,tmp2) {
3691 char b[BDEVNAME_SIZE];
3692 seq_printf(seq, " %s[%d]",
3693 bdevname(rdev->bdev,b), rdev->desc_nr);
3694 if (test_bit(WriteMostly, &rdev->flags))
3695 seq_printf(seq, "(W)");
3696 if (test_bit(Faulty, &rdev->flags)) {
3697 seq_printf(seq, "(F)");
3699 } else if (rdev->raid_disk < 0)
3700 seq_printf(seq, "(S)"); /* spare */
3704 if (!list_empty(&mddev->disks)) {
3706 seq_printf(seq, "\n %llu blocks",
3707 (unsigned long long)mddev->array_size);
3709 seq_printf(seq, "\n %llu blocks",
3710 (unsigned long long)size);
3712 if (mddev->persistent) {
3713 if (mddev->major_version != 0 ||
3714 mddev->minor_version != 90) {
3715 seq_printf(seq," super %d.%d",
3716 mddev->major_version,
3717 mddev->minor_version);
3720 seq_printf(seq, " super non-persistent");
3723 mddev->pers->status (seq, mddev);
3724 seq_printf(seq, "\n ");
3725 if (mddev->curr_resync > 2) {
3726 status_resync (seq, mddev);
3727 seq_printf(seq, "\n ");
3728 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
3729 seq_printf(seq, "\tresync=DELAYED\n ");
3730 else if (mddev->recovery_cp < MaxSector)
3731 seq_printf(seq, "\tresync=PENDING\n ");
3733 seq_printf(seq, "\n ");
3735 if ((bitmap = mddev->bitmap)) {
3736 unsigned long chunk_kb;
3737 unsigned long flags;
3738 spin_lock_irqsave(&bitmap->lock, flags);
3739 chunk_kb = bitmap->chunksize >> 10;
3740 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
3742 bitmap->pages - bitmap->missing_pages,
3744 (bitmap->pages - bitmap->missing_pages)
3745 << (PAGE_SHIFT - 10),
3746 chunk_kb ? chunk_kb : bitmap->chunksize,
3747 chunk_kb ? "KB" : "B");
3749 seq_printf(seq, ", file: ");
3750 seq_path(seq, bitmap->file->f_vfsmnt,
3751 bitmap->file->f_dentry," \t\n");
3754 seq_printf(seq, "\n");
3755 spin_unlock_irqrestore(&bitmap->lock, flags);
3758 seq_printf(seq, "\n");
3760 mddev_unlock(mddev);
3765 static struct seq_operations md_seq_ops = {
3766 .start = md_seq_start,
3767 .next = md_seq_next,
3768 .stop = md_seq_stop,
3769 .show = md_seq_show,
3772 static int md_seq_open(struct inode *inode, struct file *file)
3776 error = seq_open(file, &md_seq_ops);
3780 static struct file_operations md_seq_fops = {
3781 .open = md_seq_open,
3783 .llseek = seq_lseek,
3784 .release = seq_release,
3787 int register_md_personality(int pnum, mdk_personality_t *p)
3789 if (pnum >= MAX_PERSONALITY) {
3791 "md: tried to install personality %s as nr %d, but max is %lu\n",
3792 p->name, pnum, MAX_PERSONALITY-1);
3796 spin_lock(&pers_lock);
3798 spin_unlock(&pers_lock);
3803 printk(KERN_INFO "md: %s personality registered as nr %d\n", p->name, pnum);
3804 spin_unlock(&pers_lock);
3808 int unregister_md_personality(int pnum)
3810 if (pnum >= MAX_PERSONALITY)
3813 printk(KERN_INFO "md: %s personality unregistered\n", pers[pnum]->name);
3814 spin_lock(&pers_lock);
3816 spin_unlock(&pers_lock);
3820 static int is_mddev_idle(mddev_t *mddev)
3823 struct list_head *tmp;
3825 unsigned long curr_events;
3828 ITERATE_RDEV(mddev,rdev,tmp) {
3829 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
3830 curr_events = disk_stat_read(disk, sectors[0]) +
3831 disk_stat_read(disk, sectors[1]) -
3832 atomic_read(&disk->sync_io);
3833 /* Allow some slack between valud of curr_events and last_events,
3834 * as there are some uninteresting races.
3835 * Note: the following is an unsigned comparison.
3837 if ((curr_events - rdev->last_events + 32) > 64) {
3838 rdev->last_events = curr_events;
3845 void md_done_sync(mddev_t *mddev, int blocks, int ok)
3847 /* another "blocks" (512byte) blocks have been synced */
3848 atomic_sub(blocks, &mddev->recovery_active);
3849 wake_up(&mddev->recovery_wait);
3851 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
3852 md_wakeup_thread(mddev->thread);
3853 // stop recovery, signal do_sync ....
3858 /* md_write_start(mddev, bi)
3859 * If we need to update some array metadata (e.g. 'active' flag
3860 * in superblock) before writing, schedule a superblock update
3861 * and wait for it to complete.
3863 void md_write_start(mddev_t *mddev, struct bio *bi)
3865 if (bio_data_dir(bi) != WRITE)
3868 BUG_ON(mddev->ro == 1);
3869 if (mddev->ro == 2) {
3870 /* need to switch to read/write */
3872 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3873 md_wakeup_thread(mddev->thread);
3875 atomic_inc(&mddev->writes_pending);
3876 if (mddev->in_sync) {
3877 spin_lock_irq(&mddev->write_lock);
3878 if (mddev->in_sync) {
3880 mddev->sb_dirty = 1;
3881 md_wakeup_thread(mddev->thread);
3883 spin_unlock_irq(&mddev->write_lock);
3885 wait_event(mddev->sb_wait, mddev->sb_dirty==0);
3888 void md_write_end(mddev_t *mddev)
3890 if (atomic_dec_and_test(&mddev->writes_pending)) {
3891 if (mddev->safemode == 2)
3892 md_wakeup_thread(mddev->thread);
3894 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
3898 static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
3900 #define SYNC_MARKS 10
3901 #define SYNC_MARK_STEP (3*HZ)
3902 static void md_do_sync(mddev_t *mddev)
3905 unsigned int currspeed = 0,
3907 sector_t max_sectors,j, io_sectors;
3908 unsigned long mark[SYNC_MARKS];
3909 sector_t mark_cnt[SYNC_MARKS];
3911 struct list_head *tmp;
3912 sector_t last_check;
3915 /* just incase thread restarts... */
3916 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
3919 /* we overload curr_resync somewhat here.
3920 * 0 == not engaged in resync at all
3921 * 2 == checking that there is no conflict with another sync
3922 * 1 == like 2, but have yielded to allow conflicting resync to
3924 * other == active in resync - this many blocks
3926 * Before starting a resync we must have set curr_resync to
3927 * 2, and then checked that every "conflicting" array has curr_resync
3928 * less than ours. When we find one that is the same or higher
3929 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
3930 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
3931 * This will mean we have to start checking from the beginning again.
3936 mddev->curr_resync = 2;
3939 if (signal_pending(current) ||
3940 kthread_should_stop()) {
3941 flush_signals(current);
3942 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3945 ITERATE_MDDEV(mddev2,tmp) {
3946 if (mddev2 == mddev)
3948 if (mddev2->curr_resync &&
3949 match_mddev_units(mddev,mddev2)) {
3951 if (mddev < mddev2 && mddev->curr_resync == 2) {
3952 /* arbitrarily yield */
3953 mddev->curr_resync = 1;
3954 wake_up(&resync_wait);
3956 if (mddev > mddev2 && mddev->curr_resync == 1)
3957 /* no need to wait here, we can wait the next
3958 * time 'round when curr_resync == 2
3961 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
3962 if (!signal_pending(current) &&
3963 !kthread_should_stop() &&
3964 mddev2->curr_resync >= mddev->curr_resync) {
3965 printk(KERN_INFO "md: delaying resync of %s"
3966 " until %s has finished resync (they"
3967 " share one or more physical units)\n",
3968 mdname(mddev), mdname(mddev2));
3971 finish_wait(&resync_wait, &wq);
3974 finish_wait(&resync_wait, &wq);
3977 } while (mddev->curr_resync < 2);
3979 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3980 /* resync follows the size requested by the personality,
3981 * which defaults to physical size, but can be virtual size
3983 max_sectors = mddev->resync_max_sectors;
3984 mddev->resync_mismatches = 0;
3986 /* recovery follows the physical size of devices */
3987 max_sectors = mddev->size << 1;
3989 printk(KERN_INFO "md: syncing RAID array %s\n", mdname(mddev));
3990 printk(KERN_INFO "md: minimum _guaranteed_ reconstruction speed:"
3991 " %d KB/sec/disc.\n", sysctl_speed_limit_min);
3992 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
3993 "(but not more than %d KB/sec) for reconstruction.\n",
3994 sysctl_speed_limit_max);
3996 is_mddev_idle(mddev); /* this also initializes IO event counters */
3997 /* we don't use the checkpoint if there's a bitmap */
3998 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && !mddev->bitmap
3999 && ! test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
4000 j = mddev->recovery_cp;
4004 for (m = 0; m < SYNC_MARKS; m++) {
4006 mark_cnt[m] = io_sectors;
4009 mddev->resync_mark = mark[last_mark];
4010 mddev->resync_mark_cnt = mark_cnt[last_mark];
4013 * Tune reconstruction:
4015 window = 32*(PAGE_SIZE/512);
4016 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
4017 window/2,(unsigned long long) max_sectors/2);
4019 atomic_set(&mddev->recovery_active, 0);
4020 init_waitqueue_head(&mddev->recovery_wait);
4025 "md: resuming recovery of %s from checkpoint.\n",
4027 mddev->curr_resync = j;
4030 while (j < max_sectors) {
4034 sectors = mddev->pers->sync_request(mddev, j, &skipped,
4035 currspeed < sysctl_speed_limit_min);
4037 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
4041 if (!skipped) { /* actual IO requested */
4042 io_sectors += sectors;
4043 atomic_add(sectors, &mddev->recovery_active);
4047 if (j>1) mddev->curr_resync = j;
4050 if (last_check + window > io_sectors || j == max_sectors)
4053 last_check = io_sectors;
4055 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery) ||
4056 test_bit(MD_RECOVERY_ERR, &mddev->recovery))
4060 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
4062 int next = (last_mark+1) % SYNC_MARKS;
4064 mddev->resync_mark = mark[next];
4065 mddev->resync_mark_cnt = mark_cnt[next];
4066 mark[next] = jiffies;
4067 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
4072 if (signal_pending(current) || kthread_should_stop()) {
4074 * got a signal, exit.
4077 "md: md_do_sync() got signal ... exiting\n");
4078 flush_signals(current);
4079 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4084 * this loop exits only if either when we are slower than
4085 * the 'hard' speed limit, or the system was IO-idle for
4087 * the system might be non-idle CPU-wise, but we only care
4088 * about not overloading the IO subsystem. (things like an
4089 * e2fsck being done on the RAID array should execute fast)
4091 mddev->queue->unplug_fn(mddev->queue);
4094 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
4095 /((jiffies-mddev->resync_mark)/HZ +1) +1;
4097 if (currspeed > sysctl_speed_limit_min) {
4098 if ((currspeed > sysctl_speed_limit_max) ||
4099 !is_mddev_idle(mddev)) {
4100 msleep_interruptible(250);
4105 printk(KERN_INFO "md: %s: sync done.\n",mdname(mddev));
4107 * this also signals 'finished resyncing' to md_stop
4110 mddev->queue->unplug_fn(mddev->queue);
4112 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
4114 /* tell personality that we are finished */
4115 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
4117 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
4118 mddev->curr_resync > 2 &&
4119 mddev->curr_resync >= mddev->recovery_cp) {
4120 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4122 "md: checkpointing recovery of %s.\n",
4124 mddev->recovery_cp = mddev->curr_resync;
4126 mddev->recovery_cp = MaxSector;
4130 mddev->curr_resync = 0;
4131 wake_up(&resync_wait);
4132 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
4133 md_wakeup_thread(mddev->thread);
4138 * This routine is regularly called by all per-raid-array threads to
4139 * deal with generic issues like resync and super-block update.
4140 * Raid personalities that don't have a thread (linear/raid0) do not
4141 * need this as they never do any recovery or update the superblock.
4143 * It does not do any resync itself, but rather "forks" off other threads
4144 * to do that as needed.
4145 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
4146 * "->recovery" and create a thread at ->sync_thread.
4147 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
4148 * and wakeups up this thread which will reap the thread and finish up.
4149 * This thread also removes any faulty devices (with nr_pending == 0).
4151 * The overall approach is:
4152 * 1/ if the superblock needs updating, update it.
4153 * 2/ If a recovery thread is running, don't do anything else.
4154 * 3/ If recovery has finished, clean up, possibly marking spares active.
4155 * 4/ If there are any faulty devices, remove them.
4156 * 5/ If array is degraded, try to add spares devices
4157 * 6/ If array has spares or is not in-sync, start a resync thread.
4159 void md_check_recovery(mddev_t *mddev)
4162 struct list_head *rtmp;
4166 bitmap_daemon_work(mddev->bitmap);
4171 if (signal_pending(current)) {
4172 if (mddev->pers->sync_request) {
4173 printk(KERN_INFO "md: %s in immediate safe mode\n",
4175 mddev->safemode = 2;
4177 flush_signals(current);
4182 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
4183 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
4184 (mddev->safemode == 1) ||
4185 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
4186 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
4190 if (mddev_trylock(mddev)==0) {
4193 spin_lock_irq(&mddev->write_lock);
4194 if (mddev->safemode && !atomic_read(&mddev->writes_pending) &&
4195 !mddev->in_sync && mddev->recovery_cp == MaxSector) {
4197 mddev->sb_dirty = 1;
4199 if (mddev->safemode == 1)
4200 mddev->safemode = 0;
4201 spin_unlock_irq(&mddev->write_lock);
4203 if (mddev->sb_dirty)
4204 md_update_sb(mddev);
4207 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4208 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
4209 /* resync/recovery still happening */
4210 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4213 if (mddev->sync_thread) {
4214 /* resync has finished, collect result */
4215 md_unregister_thread(mddev->sync_thread);
4216 mddev->sync_thread = NULL;
4217 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
4218 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4220 /* activate any spares */
4221 mddev->pers->spare_active(mddev);
4223 md_update_sb(mddev);
4225 /* if array is no-longer degraded, then any saved_raid_disk
4226 * information must be scrapped
4228 if (!mddev->degraded)
4229 ITERATE_RDEV(mddev,rdev,rtmp)
4230 rdev->saved_raid_disk = -1;
4232 mddev->recovery = 0;
4233 /* flag recovery needed just to double check */
4234 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4237 /* Clear some bits that don't mean anything, but
4240 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4241 clear_bit(MD_RECOVERY_ERR, &mddev->recovery);
4242 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
4243 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
4245 /* no recovery is running.
4246 * remove any failed drives, then
4247 * add spares if possible.
4248 * Spare are also removed and re-added, to allow
4249 * the personality to fail the re-add.
4251 ITERATE_RDEV(mddev,rdev,rtmp)
4252 if (rdev->raid_disk >= 0 &&
4253 (test_bit(Faulty, &rdev->flags) || ! test_bit(In_sync, &rdev->flags)) &&
4254 atomic_read(&rdev->nr_pending)==0) {
4255 if (mddev->pers->hot_remove_disk(mddev, rdev->raid_disk)==0) {
4257 sprintf(nm,"rd%d", rdev->raid_disk);
4258 sysfs_remove_link(&mddev->kobj, nm);
4259 rdev->raid_disk = -1;
4263 if (mddev->degraded) {
4264 ITERATE_RDEV(mddev,rdev,rtmp)
4265 if (rdev->raid_disk < 0
4266 && !test_bit(Faulty, &rdev->flags)) {
4267 if (mddev->pers->hot_add_disk(mddev,rdev)) {
4269 sprintf(nm, "rd%d", rdev->raid_disk);
4270 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
4278 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4279 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4280 } else if (mddev->recovery_cp < MaxSector) {
4281 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4282 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
4283 /* nothing to be done ... */
4286 if (mddev->pers->sync_request) {
4287 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4288 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
4289 /* We are adding a device or devices to an array
4290 * which has the bitmap stored on all devices.
4291 * So make sure all bitmap pages get written
4293 bitmap_write_all(mddev->bitmap);
4295 mddev->sync_thread = md_register_thread(md_do_sync,
4298 if (!mddev->sync_thread) {
4299 printk(KERN_ERR "%s: could not start resync"
4302 /* leave the spares where they are, it shouldn't hurt */
4303 mddev->recovery = 0;
4305 md_wakeup_thread(mddev->sync_thread);
4309 mddev_unlock(mddev);
4313 static int md_notify_reboot(struct notifier_block *this,
4314 unsigned long code, void *x)
4316 struct list_head *tmp;
4319 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
4321 printk(KERN_INFO "md: stopping all md devices.\n");
4323 ITERATE_MDDEV(mddev,tmp)
4324 if (mddev_trylock(mddev)==0)
4325 do_md_stop (mddev, 1);
4327 * certain more exotic SCSI devices are known to be
4328 * volatile wrt too early system reboots. While the
4329 * right place to handle this issue is the given
4330 * driver, we do want to have a safe RAID driver ...
4337 static struct notifier_block md_notifier = {
4338 .notifier_call = md_notify_reboot,
4340 .priority = INT_MAX, /* before any real devices */
4343 static void md_geninit(void)
4345 struct proc_dir_entry *p;
4347 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
4349 p = create_proc_entry("mdstat", S_IRUGO, NULL);
4351 p->proc_fops = &md_seq_fops;
4354 static int __init md_init(void)
4358 printk(KERN_INFO "md: md driver %d.%d.%d MAX_MD_DEVS=%d,"
4359 " MD_SB_DISKS=%d\n",
4360 MD_MAJOR_VERSION, MD_MINOR_VERSION,
4361 MD_PATCHLEVEL_VERSION, MAX_MD_DEVS, MD_SB_DISKS);
4362 printk(KERN_INFO "md: bitmap version %d.%d\n", BITMAP_MAJOR_HI,
4365 if (register_blkdev(MAJOR_NR, "md"))
4367 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
4368 unregister_blkdev(MAJOR_NR, "md");
4372 blk_register_region(MKDEV(MAJOR_NR, 0), MAX_MD_DEVS, THIS_MODULE,
4373 md_probe, NULL, NULL);
4374 blk_register_region(MKDEV(mdp_major, 0), MAX_MD_DEVS<<MdpMinorShift, THIS_MODULE,
4375 md_probe, NULL, NULL);
4377 for (minor=0; minor < MAX_MD_DEVS; ++minor)
4378 devfs_mk_bdev(MKDEV(MAJOR_NR, minor),
4379 S_IFBLK|S_IRUSR|S_IWUSR,
4382 for (minor=0; minor < MAX_MD_DEVS; ++minor)
4383 devfs_mk_bdev(MKDEV(mdp_major, minor<<MdpMinorShift),
4384 S_IFBLK|S_IRUSR|S_IWUSR,
4388 register_reboot_notifier(&md_notifier);
4389 raid_table_header = register_sysctl_table(raid_root_table, 1);
4399 * Searches all registered partitions for autorun RAID arrays
4402 static dev_t detected_devices[128];
4405 void md_autodetect_dev(dev_t dev)
4407 if (dev_cnt >= 0 && dev_cnt < 127)
4408 detected_devices[dev_cnt++] = dev;
4412 static void autostart_arrays(int part)
4417 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
4419 for (i = 0; i < dev_cnt; i++) {
4420 dev_t dev = detected_devices[i];
4422 rdev = md_import_device(dev,0, 0);
4426 if (test_bit(Faulty, &rdev->flags)) {
4430 list_add(&rdev->same_set, &pending_raid_disks);
4434 autorun_devices(part);
4439 static __exit void md_exit(void)
4442 struct list_head *tmp;
4444 blk_unregister_region(MKDEV(MAJOR_NR,0), MAX_MD_DEVS);
4445 blk_unregister_region(MKDEV(mdp_major,0), MAX_MD_DEVS << MdpMinorShift);
4446 for (i=0; i < MAX_MD_DEVS; i++)
4447 devfs_remove("md/%d", i);
4448 for (i=0; i < MAX_MD_DEVS; i++)
4449 devfs_remove("md/d%d", i);
4453 unregister_blkdev(MAJOR_NR,"md");
4454 unregister_blkdev(mdp_major, "mdp");
4455 unregister_reboot_notifier(&md_notifier);
4456 unregister_sysctl_table(raid_table_header);
4457 remove_proc_entry("mdstat", NULL);
4458 ITERATE_MDDEV(mddev,tmp) {
4459 struct gendisk *disk = mddev->gendisk;
4462 export_array(mddev);
4465 mddev->gendisk = NULL;
4470 module_init(md_init)
4471 module_exit(md_exit)
4473 static int get_ro(char *buffer, struct kernel_param *kp)
4475 return sprintf(buffer, "%d", start_readonly);
4477 static int set_ro(const char *val, struct kernel_param *kp)
4480 int num = simple_strtoul(val, &e, 10);
4481 if (*val && (*e == '\0' || *e == '\n')) {
4482 start_readonly = num;
4488 module_param_call(start_ro, set_ro, get_ro, NULL, 0600);
4490 EXPORT_SYMBOL(register_md_personality);
4491 EXPORT_SYMBOL(unregister_md_personality);
4492 EXPORT_SYMBOL(md_error);
4493 EXPORT_SYMBOL(md_done_sync);
4494 EXPORT_SYMBOL(md_write_start);
4495 EXPORT_SYMBOL(md_write_end);
4496 EXPORT_SYMBOL(md_register_thread);
4497 EXPORT_SYMBOL(md_unregister_thread);
4498 EXPORT_SYMBOL(md_wakeup_thread);
4499 EXPORT_SYMBOL(md_print_devices);
4500 EXPORT_SYMBOL(md_check_recovery);
4501 MODULE_LICENSE("GPL");
4503 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);