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>
45 #include <linux/poll.h>
47 #include <linux/init.h>
49 #include <linux/file.h>
52 #include <linux/kmod.h>
55 #include <asm/unaligned.h>
57 #define MAJOR_NR MD_MAJOR
60 /* 63 partitions with the alternate major number (mdp) */
61 #define MdpMinorShift 6
64 #define dprintk(x...) ((void)(DEBUG && printk(x)))
68 static void autostart_arrays (int part);
71 static LIST_HEAD(pers_list);
72 static DEFINE_SPINLOCK(pers_lock);
75 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
76 * is 1000 KB/sec, so the extra system load does not show up that much.
77 * Increase it if you want to have more _guaranteed_ speed. Note that
78 * the RAID driver will use the maximum available bandwidth if the IO
79 * subsystem is idle. There is also an 'absolute maximum' reconstruction
80 * speed limit - in case reconstruction slows down your system despite
83 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
84 * or /sys/block/mdX/md/sync_speed_{min,max}
87 static int sysctl_speed_limit_min = 1000;
88 static int sysctl_speed_limit_max = 200000;
89 static inline int speed_min(mddev_t *mddev)
91 return mddev->sync_speed_min ?
92 mddev->sync_speed_min : sysctl_speed_limit_min;
95 static inline int speed_max(mddev_t *mddev)
97 return mddev->sync_speed_max ?
98 mddev->sync_speed_max : sysctl_speed_limit_max;
101 static struct ctl_table_header *raid_table_header;
103 static ctl_table raid_table[] = {
105 .ctl_name = DEV_RAID_SPEED_LIMIT_MIN,
106 .procname = "speed_limit_min",
107 .data = &sysctl_speed_limit_min,
108 .maxlen = sizeof(int),
110 .proc_handler = &proc_dointvec,
113 .ctl_name = DEV_RAID_SPEED_LIMIT_MAX,
114 .procname = "speed_limit_max",
115 .data = &sysctl_speed_limit_max,
116 .maxlen = sizeof(int),
118 .proc_handler = &proc_dointvec,
123 static ctl_table raid_dir_table[] = {
125 .ctl_name = DEV_RAID,
134 static ctl_table raid_root_table[] = {
140 .child = raid_dir_table,
145 static struct block_device_operations md_fops;
147 static int start_readonly;
150 * We have a system wide 'event count' that is incremented
151 * on any 'interesting' event, and readers of /proc/mdstat
152 * can use 'poll' or 'select' to find out when the event
156 * start array, stop array, error, add device, remove device,
157 * start build, activate spare
159 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
160 static atomic_t md_event_count;
161 static void md_new_event(mddev_t *mddev)
163 atomic_inc(&md_event_count);
164 wake_up(&md_event_waiters);
168 * Enables to iterate over all existing md arrays
169 * all_mddevs_lock protects this list.
171 static LIST_HEAD(all_mddevs);
172 static DEFINE_SPINLOCK(all_mddevs_lock);
176 * iterates through all used mddevs in the system.
177 * We take care to grab the all_mddevs_lock whenever navigating
178 * the list, and to always hold a refcount when unlocked.
179 * Any code which breaks out of this loop while own
180 * a reference to the current mddev and must mddev_put it.
182 #define ITERATE_MDDEV(mddev,tmp) \
184 for (({ spin_lock(&all_mddevs_lock); \
185 tmp = all_mddevs.next; \
187 ({ if (tmp != &all_mddevs) \
188 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
189 spin_unlock(&all_mddevs_lock); \
190 if (mddev) mddev_put(mddev); \
191 mddev = list_entry(tmp, mddev_t, all_mddevs); \
192 tmp != &all_mddevs;}); \
193 ({ spin_lock(&all_mddevs_lock); \
198 static int md_fail_request (request_queue_t *q, struct bio *bio)
200 bio_io_error(bio, bio->bi_size);
204 static inline mddev_t *mddev_get(mddev_t *mddev)
206 atomic_inc(&mddev->active);
210 static void mddev_put(mddev_t *mddev)
212 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
214 if (!mddev->raid_disks && list_empty(&mddev->disks)) {
215 list_del(&mddev->all_mddevs);
217 blk_cleanup_queue(mddev->queue);
218 /* that also blocks */
219 kobject_unregister(&mddev->kobj);
220 /* result blows... */
222 spin_unlock(&all_mddevs_lock);
225 static mddev_t * mddev_find(dev_t unit)
227 mddev_t *mddev, *new = NULL;
230 spin_lock(&all_mddevs_lock);
231 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
232 if (mddev->unit == unit) {
234 spin_unlock(&all_mddevs_lock);
240 list_add(&new->all_mddevs, &all_mddevs);
241 spin_unlock(&all_mddevs_lock);
244 spin_unlock(&all_mddevs_lock);
246 new = kzalloc(sizeof(*new), GFP_KERNEL);
251 if (MAJOR(unit) == MD_MAJOR)
252 new->md_minor = MINOR(unit);
254 new->md_minor = MINOR(unit) >> MdpMinorShift;
256 init_MUTEX(&new->reconfig_sem);
257 INIT_LIST_HEAD(&new->disks);
258 INIT_LIST_HEAD(&new->all_mddevs);
259 init_timer(&new->safemode_timer);
260 atomic_set(&new->active, 1);
261 spin_lock_init(&new->write_lock);
262 init_waitqueue_head(&new->sb_wait);
264 new->queue = blk_alloc_queue(GFP_KERNEL);
270 blk_queue_make_request(new->queue, md_fail_request);
275 static inline int mddev_lock(mddev_t * mddev)
277 return down_interruptible(&mddev->reconfig_sem);
280 static inline void mddev_lock_uninterruptible(mddev_t * mddev)
282 down(&mddev->reconfig_sem);
285 static inline int mddev_trylock(mddev_t * mddev)
287 return down_trylock(&mddev->reconfig_sem);
290 static inline void mddev_unlock(mddev_t * mddev)
292 up(&mddev->reconfig_sem);
294 md_wakeup_thread(mddev->thread);
297 static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
300 struct list_head *tmp;
302 ITERATE_RDEV(mddev,rdev,tmp) {
303 if (rdev->desc_nr == nr)
309 static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
311 struct list_head *tmp;
314 ITERATE_RDEV(mddev,rdev,tmp) {
315 if (rdev->bdev->bd_dev == dev)
321 static struct mdk_personality *find_pers(int level, char *clevel)
323 struct mdk_personality *pers;
324 list_for_each_entry(pers, &pers_list, list) {
325 if (level != LEVEL_NONE && pers->level == level)
327 if (strcmp(pers->name, clevel)==0)
333 static inline sector_t calc_dev_sboffset(struct block_device *bdev)
335 sector_t size = bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
336 return MD_NEW_SIZE_BLOCKS(size);
339 static sector_t calc_dev_size(mdk_rdev_t *rdev, unsigned chunk_size)
343 size = rdev->sb_offset;
346 size &= ~((sector_t)chunk_size/1024 - 1);
350 static int alloc_disk_sb(mdk_rdev_t * rdev)
355 rdev->sb_page = alloc_page(GFP_KERNEL);
356 if (!rdev->sb_page) {
357 printk(KERN_ALERT "md: out of memory.\n");
364 static void free_disk_sb(mdk_rdev_t * rdev)
367 put_page(rdev->sb_page);
369 rdev->sb_page = NULL;
376 static int super_written(struct bio *bio, unsigned int bytes_done, int error)
378 mdk_rdev_t *rdev = bio->bi_private;
379 mddev_t *mddev = rdev->mddev;
383 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags))
384 md_error(mddev, rdev);
386 if (atomic_dec_and_test(&mddev->pending_writes))
387 wake_up(&mddev->sb_wait);
392 static int super_written_barrier(struct bio *bio, unsigned int bytes_done, int error)
394 struct bio *bio2 = bio->bi_private;
395 mdk_rdev_t *rdev = bio2->bi_private;
396 mddev_t *mddev = rdev->mddev;
400 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
401 error == -EOPNOTSUPP) {
403 /* barriers don't appear to be supported :-( */
404 set_bit(BarriersNotsupp, &rdev->flags);
405 mddev->barriers_work = 0;
406 spin_lock_irqsave(&mddev->write_lock, flags);
407 bio2->bi_next = mddev->biolist;
408 mddev->biolist = bio2;
409 spin_unlock_irqrestore(&mddev->write_lock, flags);
410 wake_up(&mddev->sb_wait);
415 bio->bi_private = rdev;
416 return super_written(bio, bytes_done, error);
419 void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
420 sector_t sector, int size, struct page *page)
422 /* write first size bytes of page to sector of rdev
423 * Increment mddev->pending_writes before returning
424 * and decrement it on completion, waking up sb_wait
425 * if zero is reached.
426 * If an error occurred, call md_error
428 * As we might need to resubmit the request if BIO_RW_BARRIER
429 * causes ENOTSUPP, we allocate a spare bio...
431 struct bio *bio = bio_alloc(GFP_NOIO, 1);
432 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNC);
434 bio->bi_bdev = rdev->bdev;
435 bio->bi_sector = sector;
436 bio_add_page(bio, page, size, 0);
437 bio->bi_private = rdev;
438 bio->bi_end_io = super_written;
441 atomic_inc(&mddev->pending_writes);
442 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
444 rw |= (1<<BIO_RW_BARRIER);
445 rbio = bio_clone(bio, GFP_NOIO);
446 rbio->bi_private = bio;
447 rbio->bi_end_io = super_written_barrier;
448 submit_bio(rw, rbio);
453 void md_super_wait(mddev_t *mddev)
455 /* wait for all superblock writes that were scheduled to complete.
456 * if any had to be retried (due to BARRIER problems), retry them
460 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
461 if (atomic_read(&mddev->pending_writes)==0)
463 while (mddev->biolist) {
465 spin_lock_irq(&mddev->write_lock);
466 bio = mddev->biolist;
467 mddev->biolist = bio->bi_next ;
469 spin_unlock_irq(&mddev->write_lock);
470 submit_bio(bio->bi_rw, bio);
474 finish_wait(&mddev->sb_wait, &wq);
477 static int bi_complete(struct bio *bio, unsigned int bytes_done, int error)
482 complete((struct completion*)bio->bi_private);
486 int sync_page_io(struct block_device *bdev, sector_t sector, int size,
487 struct page *page, int rw)
489 struct bio *bio = bio_alloc(GFP_NOIO, 1);
490 struct completion event;
493 rw |= (1 << BIO_RW_SYNC);
496 bio->bi_sector = sector;
497 bio_add_page(bio, page, size, 0);
498 init_completion(&event);
499 bio->bi_private = &event;
500 bio->bi_end_io = bi_complete;
502 wait_for_completion(&event);
504 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
508 EXPORT_SYMBOL_GPL(sync_page_io);
510 static int read_disk_sb(mdk_rdev_t * rdev, int size)
512 char b[BDEVNAME_SIZE];
513 if (!rdev->sb_page) {
521 if (!sync_page_io(rdev->bdev, rdev->sb_offset<<1, size, rdev->sb_page, READ))
527 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
528 bdevname(rdev->bdev,b));
532 static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
534 if ( (sb1->set_uuid0 == sb2->set_uuid0) &&
535 (sb1->set_uuid1 == sb2->set_uuid1) &&
536 (sb1->set_uuid2 == sb2->set_uuid2) &&
537 (sb1->set_uuid3 == sb2->set_uuid3))
545 static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
548 mdp_super_t *tmp1, *tmp2;
550 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
551 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
553 if (!tmp1 || !tmp2) {
555 printk(KERN_INFO "md.c: sb1 is not equal to sb2!\n");
563 * nr_disks is not constant
568 if (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4))
579 static unsigned int calc_sb_csum(mdp_super_t * sb)
581 unsigned int disk_csum, csum;
583 disk_csum = sb->sb_csum;
585 csum = csum_partial((void *)sb, MD_SB_BYTES, 0);
586 sb->sb_csum = disk_csum;
592 * Handle superblock details.
593 * We want to be able to handle multiple superblock formats
594 * so we have a common interface to them all, and an array of
595 * different handlers.
596 * We rely on user-space to write the initial superblock, and support
597 * reading and updating of superblocks.
598 * Interface methods are:
599 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
600 * loads and validates a superblock on dev.
601 * if refdev != NULL, compare superblocks on both devices
603 * 0 - dev has a superblock that is compatible with refdev
604 * 1 - dev has a superblock that is compatible and newer than refdev
605 * so dev should be used as the refdev in future
606 * -EINVAL superblock incompatible or invalid
607 * -othererror e.g. -EIO
609 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
610 * Verify that dev is acceptable into mddev.
611 * The first time, mddev->raid_disks will be 0, and data from
612 * dev should be merged in. Subsequent calls check that dev
613 * is new enough. Return 0 or -EINVAL
615 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
616 * Update the superblock for rdev with data in mddev
617 * This does not write to disc.
623 struct module *owner;
624 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version);
625 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
626 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
630 * load_super for 0.90.0
632 static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
634 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
640 * Calculate the position of the superblock,
641 * it's at the end of the disk.
643 * It also happens to be a multiple of 4Kb.
645 sb_offset = calc_dev_sboffset(rdev->bdev);
646 rdev->sb_offset = sb_offset;
648 ret = read_disk_sb(rdev, MD_SB_BYTES);
653 bdevname(rdev->bdev, b);
654 sb = (mdp_super_t*)page_address(rdev->sb_page);
656 if (sb->md_magic != MD_SB_MAGIC) {
657 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
662 if (sb->major_version != 0 ||
663 sb->minor_version != 90) {
664 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
665 sb->major_version, sb->minor_version,
670 if (sb->raid_disks <= 0)
673 if (csum_fold(calc_sb_csum(sb)) != csum_fold(sb->sb_csum)) {
674 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
679 rdev->preferred_minor = sb->md_minor;
680 rdev->data_offset = 0;
681 rdev->sb_size = MD_SB_BYTES;
683 if (sb->level == LEVEL_MULTIPATH)
686 rdev->desc_nr = sb->this_disk.number;
692 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
693 if (!uuid_equal(refsb, sb)) {
694 printk(KERN_WARNING "md: %s has different UUID to %s\n",
695 b, bdevname(refdev->bdev,b2));
698 if (!sb_equal(refsb, sb)) {
699 printk(KERN_WARNING "md: %s has same UUID"
700 " but different superblock to %s\n",
701 b, bdevname(refdev->bdev, b2));
705 ev2 = md_event(refsb);
711 rdev->size = calc_dev_size(rdev, sb->chunk_size);
713 if (rdev->size < sb->size && sb->level > 1)
714 /* "this cannot possibly happen" ... */
722 * validate_super for 0.90.0
724 static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
727 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
729 rdev->raid_disk = -1;
731 if (mddev->raid_disks == 0) {
732 mddev->major_version = 0;
733 mddev->minor_version = sb->minor_version;
734 mddev->patch_version = sb->patch_version;
735 mddev->persistent = ! sb->not_persistent;
736 mddev->chunk_size = sb->chunk_size;
737 mddev->ctime = sb->ctime;
738 mddev->utime = sb->utime;
739 mddev->level = sb->level;
740 mddev->clevel[0] = 0;
741 mddev->layout = sb->layout;
742 mddev->raid_disks = sb->raid_disks;
743 mddev->size = sb->size;
744 mddev->events = md_event(sb);
745 mddev->bitmap_offset = 0;
746 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
748 if (sb->state & (1<<MD_SB_CLEAN))
749 mddev->recovery_cp = MaxSector;
751 if (sb->events_hi == sb->cp_events_hi &&
752 sb->events_lo == sb->cp_events_lo) {
753 mddev->recovery_cp = sb->recovery_cp;
755 mddev->recovery_cp = 0;
758 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
759 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
760 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
761 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
763 mddev->max_disks = MD_SB_DISKS;
765 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
766 mddev->bitmap_file == NULL) {
767 if (mddev->level != 1 && mddev->level != 5 && mddev->level != 6
768 && mddev->level != 10) {
769 /* FIXME use a better test */
770 printk(KERN_WARNING "md: bitmaps not supported for this level.\n");
773 mddev->bitmap_offset = mddev->default_bitmap_offset;
776 } else if (mddev->pers == NULL) {
777 /* Insist on good event counter while assembling */
778 __u64 ev1 = md_event(sb);
780 if (ev1 < mddev->events)
782 } else if (mddev->bitmap) {
783 /* if adding to array with a bitmap, then we can accept an
784 * older device ... but not too old.
786 __u64 ev1 = md_event(sb);
787 if (ev1 < mddev->bitmap->events_cleared)
789 } else /* just a hot-add of a new device, leave raid_disk at -1 */
792 if (mddev->level != LEVEL_MULTIPATH) {
793 desc = sb->disks + rdev->desc_nr;
795 if (desc->state & (1<<MD_DISK_FAULTY))
796 set_bit(Faulty, &rdev->flags);
797 else if (desc->state & (1<<MD_DISK_SYNC) &&
798 desc->raid_disk < mddev->raid_disks) {
799 set_bit(In_sync, &rdev->flags);
800 rdev->raid_disk = desc->raid_disk;
802 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
803 set_bit(WriteMostly, &rdev->flags);
804 } else /* MULTIPATH are always insync */
805 set_bit(In_sync, &rdev->flags);
810 * sync_super for 0.90.0
812 static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
815 struct list_head *tmp;
817 int next_spare = mddev->raid_disks;
820 /* make rdev->sb match mddev data..
823 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
824 * 3/ any empty disks < next_spare become removed
826 * disks[0] gets initialised to REMOVED because
827 * we cannot be sure from other fields if it has
828 * been initialised or not.
831 int active=0, working=0,failed=0,spare=0,nr_disks=0;
833 rdev->sb_size = MD_SB_BYTES;
835 sb = (mdp_super_t*)page_address(rdev->sb_page);
837 memset(sb, 0, sizeof(*sb));
839 sb->md_magic = MD_SB_MAGIC;
840 sb->major_version = mddev->major_version;
841 sb->minor_version = mddev->minor_version;
842 sb->patch_version = mddev->patch_version;
843 sb->gvalid_words = 0; /* ignored */
844 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
845 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
846 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
847 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
849 sb->ctime = mddev->ctime;
850 sb->level = mddev->level;
851 sb->size = mddev->size;
852 sb->raid_disks = mddev->raid_disks;
853 sb->md_minor = mddev->md_minor;
854 sb->not_persistent = !mddev->persistent;
855 sb->utime = mddev->utime;
857 sb->events_hi = (mddev->events>>32);
858 sb->events_lo = (u32)mddev->events;
862 sb->recovery_cp = mddev->recovery_cp;
863 sb->cp_events_hi = (mddev->events>>32);
864 sb->cp_events_lo = (u32)mddev->events;
865 if (mddev->recovery_cp == MaxSector)
866 sb->state = (1<< MD_SB_CLEAN);
870 sb->layout = mddev->layout;
871 sb->chunk_size = mddev->chunk_size;
873 if (mddev->bitmap && mddev->bitmap_file == NULL)
874 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
876 sb->disks[0].state = (1<<MD_DISK_REMOVED);
877 ITERATE_RDEV(mddev,rdev2,tmp) {
880 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
881 && !test_bit(Faulty, &rdev2->flags))
882 desc_nr = rdev2->raid_disk;
884 desc_nr = next_spare++;
885 rdev2->desc_nr = desc_nr;
886 d = &sb->disks[rdev2->desc_nr];
888 d->number = rdev2->desc_nr;
889 d->major = MAJOR(rdev2->bdev->bd_dev);
890 d->minor = MINOR(rdev2->bdev->bd_dev);
891 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
892 && !test_bit(Faulty, &rdev2->flags))
893 d->raid_disk = rdev2->raid_disk;
895 d->raid_disk = rdev2->desc_nr; /* compatibility */
896 if (test_bit(Faulty, &rdev2->flags)) {
897 d->state = (1<<MD_DISK_FAULTY);
899 } else if (test_bit(In_sync, &rdev2->flags)) {
900 d->state = (1<<MD_DISK_ACTIVE);
901 d->state |= (1<<MD_DISK_SYNC);
909 if (test_bit(WriteMostly, &rdev2->flags))
910 d->state |= (1<<MD_DISK_WRITEMOSTLY);
912 /* now set the "removed" and "faulty" bits on any missing devices */
913 for (i=0 ; i < mddev->raid_disks ; i++) {
914 mdp_disk_t *d = &sb->disks[i];
915 if (d->state == 0 && d->number == 0) {
918 d->state = (1<<MD_DISK_REMOVED);
919 d->state |= (1<<MD_DISK_FAULTY);
923 sb->nr_disks = nr_disks;
924 sb->active_disks = active;
925 sb->working_disks = working;
926 sb->failed_disks = failed;
927 sb->spare_disks = spare;
929 sb->this_disk = sb->disks[rdev->desc_nr];
930 sb->sb_csum = calc_sb_csum(sb);
934 * version 1 superblock
937 static unsigned int calc_sb_1_csum(struct mdp_superblock_1 * sb)
939 unsigned int disk_csum, csum;
940 unsigned long long newcsum;
941 int size = 256 + le32_to_cpu(sb->max_dev)*2;
942 unsigned int *isuper = (unsigned int*)sb;
945 disk_csum = sb->sb_csum;
948 for (i=0; size>=4; size -= 4 )
949 newcsum += le32_to_cpu(*isuper++);
952 newcsum += le16_to_cpu(*(unsigned short*) isuper);
954 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
955 sb->sb_csum = disk_csum;
956 return cpu_to_le32(csum);
959 static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
961 struct mdp_superblock_1 *sb;
964 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
968 * Calculate the position of the superblock.
969 * It is always aligned to a 4K boundary and
970 * depeding on minor_version, it can be:
971 * 0: At least 8K, but less than 12K, from end of device
972 * 1: At start of device
973 * 2: 4K from start of device.
975 switch(minor_version) {
977 sb_offset = rdev->bdev->bd_inode->i_size >> 9;
979 sb_offset &= ~(sector_t)(4*2-1);
980 /* convert from sectors to K */
992 rdev->sb_offset = sb_offset;
994 /* superblock is rarely larger than 1K, but it can be larger,
995 * and it is safe to read 4k, so we do that
997 ret = read_disk_sb(rdev, 4096);
1001 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1003 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1004 sb->major_version != cpu_to_le32(1) ||
1005 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1006 le64_to_cpu(sb->super_offset) != (rdev->sb_offset<<1) ||
1007 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
1010 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1011 printk("md: invalid superblock checksum on %s\n",
1012 bdevname(rdev->bdev,b));
1015 if (le64_to_cpu(sb->data_size) < 10) {
1016 printk("md: data_size too small on %s\n",
1017 bdevname(rdev->bdev,b));
1020 rdev->preferred_minor = 0xffff;
1021 rdev->data_offset = le64_to_cpu(sb->data_offset);
1022 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
1024 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
1025 bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
1026 if (rdev->sb_size & bmask)
1027 rdev-> sb_size = (rdev->sb_size | bmask)+1;
1033 struct mdp_superblock_1 *refsb =
1034 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1036 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1037 sb->level != refsb->level ||
1038 sb->layout != refsb->layout ||
1039 sb->chunksize != refsb->chunksize) {
1040 printk(KERN_WARNING "md: %s has strangely different"
1041 " superblock to %s\n",
1042 bdevname(rdev->bdev,b),
1043 bdevname(refdev->bdev,b2));
1046 ev1 = le64_to_cpu(sb->events);
1047 ev2 = le64_to_cpu(refsb->events);
1055 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
1057 rdev->size = rdev->sb_offset;
1058 if (rdev->size < le64_to_cpu(sb->data_size)/2)
1060 rdev->size = le64_to_cpu(sb->data_size)/2;
1061 if (le32_to_cpu(sb->chunksize))
1062 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
1064 if (le32_to_cpu(sb->size) > rdev->size*2)
1069 static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1071 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1073 rdev->raid_disk = -1;
1075 if (mddev->raid_disks == 0) {
1076 mddev->major_version = 1;
1077 mddev->patch_version = 0;
1078 mddev->persistent = 1;
1079 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
1080 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1081 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1082 mddev->level = le32_to_cpu(sb->level);
1083 mddev->clevel[0] = 0;
1084 mddev->layout = le32_to_cpu(sb->layout);
1085 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1086 mddev->size = le64_to_cpu(sb->size)/2;
1087 mddev->events = le64_to_cpu(sb->events);
1088 mddev->bitmap_offset = 0;
1089 mddev->default_bitmap_offset = 1024 >> 9;
1091 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1092 memcpy(mddev->uuid, sb->set_uuid, 16);
1094 mddev->max_disks = (4096-256)/2;
1096 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
1097 mddev->bitmap_file == NULL ) {
1098 if (mddev->level != 1 && mddev->level != 5 && mddev->level != 6
1099 && mddev->level != 10) {
1100 printk(KERN_WARNING "md: bitmaps not supported for this level.\n");
1103 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
1105 } else if (mddev->pers == NULL) {
1106 /* Insist of good event counter while assembling */
1107 __u64 ev1 = le64_to_cpu(sb->events);
1109 if (ev1 < mddev->events)
1111 } else if (mddev->bitmap) {
1112 /* If adding to array with a bitmap, then we can accept an
1113 * older device, but not too old.
1115 __u64 ev1 = le64_to_cpu(sb->events);
1116 if (ev1 < mddev->bitmap->events_cleared)
1118 } else /* just a hot-add of a new device, leave raid_disk at -1 */
1121 if (mddev->level != LEVEL_MULTIPATH) {
1123 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1124 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1126 case 0xffff: /* spare */
1128 case 0xfffe: /* faulty */
1129 set_bit(Faulty, &rdev->flags);
1132 set_bit(In_sync, &rdev->flags);
1133 rdev->raid_disk = role;
1136 if (sb->devflags & WriteMostly1)
1137 set_bit(WriteMostly, &rdev->flags);
1138 } else /* MULTIPATH are always insync */
1139 set_bit(In_sync, &rdev->flags);
1144 static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1146 struct mdp_superblock_1 *sb;
1147 struct list_head *tmp;
1150 /* make rdev->sb match mddev and rdev data. */
1152 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1154 sb->feature_map = 0;
1156 memset(sb->pad1, 0, sizeof(sb->pad1));
1157 memset(sb->pad2, 0, sizeof(sb->pad2));
1158 memset(sb->pad3, 0, sizeof(sb->pad3));
1160 sb->utime = cpu_to_le64((__u64)mddev->utime);
1161 sb->events = cpu_to_le64(mddev->events);
1163 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1165 sb->resync_offset = cpu_to_le64(0);
1167 sb->cnt_corrected_read = atomic_read(&rdev->corrected_errors);
1169 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
1170 sb->size = cpu_to_le64(mddev->size<<1);
1172 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1173 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
1174 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
1178 ITERATE_RDEV(mddev,rdev2,tmp)
1179 if (rdev2->desc_nr+1 > max_dev)
1180 max_dev = rdev2->desc_nr+1;
1182 sb->max_dev = cpu_to_le32(max_dev);
1183 for (i=0; i<max_dev;i++)
1184 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1186 ITERATE_RDEV(mddev,rdev2,tmp) {
1188 if (test_bit(Faulty, &rdev2->flags))
1189 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1190 else if (test_bit(In_sync, &rdev2->flags))
1191 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1193 sb->dev_roles[i] = cpu_to_le16(0xffff);
1196 sb->recovery_offset = cpu_to_le64(0); /* not supported yet */
1197 sb->sb_csum = calc_sb_1_csum(sb);
1201 static struct super_type super_types[] = {
1204 .owner = THIS_MODULE,
1205 .load_super = super_90_load,
1206 .validate_super = super_90_validate,
1207 .sync_super = super_90_sync,
1211 .owner = THIS_MODULE,
1212 .load_super = super_1_load,
1213 .validate_super = super_1_validate,
1214 .sync_super = super_1_sync,
1218 static mdk_rdev_t * match_dev_unit(mddev_t *mddev, mdk_rdev_t *dev)
1220 struct list_head *tmp;
1223 ITERATE_RDEV(mddev,rdev,tmp)
1224 if (rdev->bdev->bd_contains == dev->bdev->bd_contains)
1230 static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1232 struct list_head *tmp;
1235 ITERATE_RDEV(mddev1,rdev,tmp)
1236 if (match_dev_unit(mddev2, rdev))
1242 static LIST_HEAD(pending_raid_disks);
1244 static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1246 mdk_rdev_t *same_pdev;
1247 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1255 /* make sure rdev->size exceeds mddev->size */
1256 if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) {
1258 /* Cannot change size, so fail */
1261 mddev->size = rdev->size;
1263 same_pdev = match_dev_unit(mddev, rdev);
1266 "%s: WARNING: %s appears to be on the same physical"
1267 " disk as %s. True\n protection against single-disk"
1268 " failure might be compromised.\n",
1269 mdname(mddev), bdevname(rdev->bdev,b),
1270 bdevname(same_pdev->bdev,b2));
1272 /* Verify rdev->desc_nr is unique.
1273 * If it is -1, assign a free number, else
1274 * check number is not in use
1276 if (rdev->desc_nr < 0) {
1278 if (mddev->pers) choice = mddev->raid_disks;
1279 while (find_rdev_nr(mddev, choice))
1281 rdev->desc_nr = choice;
1283 if (find_rdev_nr(mddev, rdev->desc_nr))
1286 bdevname(rdev->bdev,b);
1287 if (kobject_set_name(&rdev->kobj, "dev-%s", b) < 0)
1289 while ( (s=strchr(rdev->kobj.k_name, '/')) != NULL)
1292 list_add(&rdev->same_set, &mddev->disks);
1293 rdev->mddev = mddev;
1294 printk(KERN_INFO "md: bind<%s>\n", b);
1296 rdev->kobj.parent = &mddev->kobj;
1297 kobject_add(&rdev->kobj);
1299 if (rdev->bdev->bd_part)
1300 ko = &rdev->bdev->bd_part->kobj;
1302 ko = &rdev->bdev->bd_disk->kobj;
1303 sysfs_create_link(&rdev->kobj, ko, "block");
1307 static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1309 char b[BDEVNAME_SIZE];
1314 list_del_init(&rdev->same_set);
1315 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1317 sysfs_remove_link(&rdev->kobj, "block");
1318 kobject_del(&rdev->kobj);
1322 * prevent the device from being mounted, repartitioned or
1323 * otherwise reused by a RAID array (or any other kernel
1324 * subsystem), by bd_claiming the device.
1326 static int lock_rdev(mdk_rdev_t *rdev, dev_t dev)
1329 struct block_device *bdev;
1330 char b[BDEVNAME_SIZE];
1332 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
1334 printk(KERN_ERR "md: could not open %s.\n",
1335 __bdevname(dev, b));
1336 return PTR_ERR(bdev);
1338 err = bd_claim(bdev, rdev);
1340 printk(KERN_ERR "md: could not bd_claim %s.\n",
1349 static void unlock_rdev(mdk_rdev_t *rdev)
1351 struct block_device *bdev = rdev->bdev;
1359 void md_autodetect_dev(dev_t dev);
1361 static void export_rdev(mdk_rdev_t * rdev)
1363 char b[BDEVNAME_SIZE];
1364 printk(KERN_INFO "md: export_rdev(%s)\n",
1365 bdevname(rdev->bdev,b));
1369 list_del_init(&rdev->same_set);
1371 md_autodetect_dev(rdev->bdev->bd_dev);
1374 kobject_put(&rdev->kobj);
1377 static void kick_rdev_from_array(mdk_rdev_t * rdev)
1379 unbind_rdev_from_array(rdev);
1383 static void export_array(mddev_t *mddev)
1385 struct list_head *tmp;
1388 ITERATE_RDEV(mddev,rdev,tmp) {
1393 kick_rdev_from_array(rdev);
1395 if (!list_empty(&mddev->disks))
1397 mddev->raid_disks = 0;
1398 mddev->major_version = 0;
1401 static void print_desc(mdp_disk_t *desc)
1403 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1404 desc->major,desc->minor,desc->raid_disk,desc->state);
1407 static void print_sb(mdp_super_t *sb)
1412 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1413 sb->major_version, sb->minor_version, sb->patch_version,
1414 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1416 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1417 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1418 sb->md_minor, sb->layout, sb->chunk_size);
1419 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1420 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1421 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1422 sb->failed_disks, sb->spare_disks,
1423 sb->sb_csum, (unsigned long)sb->events_lo);
1426 for (i = 0; i < MD_SB_DISKS; i++) {
1429 desc = sb->disks + i;
1430 if (desc->number || desc->major || desc->minor ||
1431 desc->raid_disk || (desc->state && (desc->state != 4))) {
1432 printk(" D %2d: ", i);
1436 printk(KERN_INFO "md: THIS: ");
1437 print_desc(&sb->this_disk);
1441 static void print_rdev(mdk_rdev_t *rdev)
1443 char b[BDEVNAME_SIZE];
1444 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1445 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
1446 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1448 if (rdev->sb_loaded) {
1449 printk(KERN_INFO "md: rdev superblock:\n");
1450 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1452 printk(KERN_INFO "md: no rdev superblock!\n");
1455 void md_print_devices(void)
1457 struct list_head *tmp, *tmp2;
1460 char b[BDEVNAME_SIZE];
1463 printk("md: **********************************\n");
1464 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1465 printk("md: **********************************\n");
1466 ITERATE_MDDEV(mddev,tmp) {
1469 bitmap_print_sb(mddev->bitmap);
1471 printk("%s: ", mdname(mddev));
1472 ITERATE_RDEV(mddev,rdev,tmp2)
1473 printk("<%s>", bdevname(rdev->bdev,b));
1476 ITERATE_RDEV(mddev,rdev,tmp2)
1479 printk("md: **********************************\n");
1484 static void sync_sbs(mddev_t * mddev)
1487 struct list_head *tmp;
1489 ITERATE_RDEV(mddev,rdev,tmp) {
1490 super_types[mddev->major_version].
1491 sync_super(mddev, rdev);
1492 rdev->sb_loaded = 1;
1496 static void md_update_sb(mddev_t * mddev)
1499 struct list_head *tmp;
1504 spin_lock_irq(&mddev->write_lock);
1505 sync_req = mddev->in_sync;
1506 mddev->utime = get_seconds();
1509 if (!mddev->events) {
1511 * oops, this 64-bit counter should never wrap.
1512 * Either we are in around ~1 trillion A.C., assuming
1513 * 1 reboot per second, or we have a bug:
1518 mddev->sb_dirty = 2;
1522 * do not write anything to disk if using
1523 * nonpersistent superblocks
1525 if (!mddev->persistent) {
1526 mddev->sb_dirty = 0;
1527 spin_unlock_irq(&mddev->write_lock);
1528 wake_up(&mddev->sb_wait);
1531 spin_unlock_irq(&mddev->write_lock);
1534 "md: updating %s RAID superblock on device (in sync %d)\n",
1535 mdname(mddev),mddev->in_sync);
1537 err = bitmap_update_sb(mddev->bitmap);
1538 ITERATE_RDEV(mddev,rdev,tmp) {
1539 char b[BDEVNAME_SIZE];
1540 dprintk(KERN_INFO "md: ");
1541 if (test_bit(Faulty, &rdev->flags))
1542 dprintk("(skipping faulty ");
1544 dprintk("%s ", bdevname(rdev->bdev,b));
1545 if (!test_bit(Faulty, &rdev->flags)) {
1546 md_super_write(mddev,rdev,
1547 rdev->sb_offset<<1, rdev->sb_size,
1549 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1550 bdevname(rdev->bdev,b),
1551 (unsigned long long)rdev->sb_offset);
1555 if (mddev->level == LEVEL_MULTIPATH)
1556 /* only need to write one superblock... */
1559 md_super_wait(mddev);
1560 /* if there was a failure, sb_dirty was set to 1, and we re-write super */
1562 spin_lock_irq(&mddev->write_lock);
1563 if (mddev->in_sync != sync_req|| mddev->sb_dirty == 1) {
1564 /* have to write it out again */
1565 spin_unlock_irq(&mddev->write_lock);
1568 mddev->sb_dirty = 0;
1569 spin_unlock_irq(&mddev->write_lock);
1570 wake_up(&mddev->sb_wait);
1574 /* words written to sysfs files may, or my not, be \n terminated.
1575 * We want to accept with case. For this we use cmd_match.
1577 static int cmd_match(const char *cmd, const char *str)
1579 /* See if cmd, written into a sysfs file, matches
1580 * str. They must either be the same, or cmd can
1581 * have a trailing newline
1583 while (*cmd && *str && *cmd == *str) {
1594 struct rdev_sysfs_entry {
1595 struct attribute attr;
1596 ssize_t (*show)(mdk_rdev_t *, char *);
1597 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1601 state_show(mdk_rdev_t *rdev, char *page)
1606 if (test_bit(Faulty, &rdev->flags)) {
1607 len+= sprintf(page+len, "%sfaulty",sep);
1610 if (test_bit(In_sync, &rdev->flags)) {
1611 len += sprintf(page+len, "%sin_sync",sep);
1614 if (!test_bit(Faulty, &rdev->flags) &&
1615 !test_bit(In_sync, &rdev->flags)) {
1616 len += sprintf(page+len, "%sspare", sep);
1619 return len+sprintf(page+len, "\n");
1622 static struct rdev_sysfs_entry
1623 rdev_state = __ATTR_RO(state);
1626 super_show(mdk_rdev_t *rdev, char *page)
1628 if (rdev->sb_loaded && rdev->sb_size) {
1629 memcpy(page, page_address(rdev->sb_page), rdev->sb_size);
1630 return rdev->sb_size;
1634 static struct rdev_sysfs_entry rdev_super = __ATTR_RO(super);
1637 errors_show(mdk_rdev_t *rdev, char *page)
1639 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
1643 errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1646 unsigned long n = simple_strtoul(buf, &e, 10);
1647 if (*buf && (*e == 0 || *e == '\n')) {
1648 atomic_set(&rdev->corrected_errors, n);
1653 static struct rdev_sysfs_entry rdev_errors =
1654 __ATTR(errors, 0644, errors_show, errors_store);
1657 slot_show(mdk_rdev_t *rdev, char *page)
1659 if (rdev->raid_disk < 0)
1660 return sprintf(page, "none\n");
1662 return sprintf(page, "%d\n", rdev->raid_disk);
1666 slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1669 int slot = simple_strtoul(buf, &e, 10);
1670 if (strncmp(buf, "none", 4)==0)
1672 else if (e==buf || (*e && *e!= '\n'))
1674 if (rdev->mddev->pers)
1675 /* Cannot set slot in active array (yet) */
1677 if (slot >= rdev->mddev->raid_disks)
1679 rdev->raid_disk = slot;
1680 /* assume it is working */
1682 set_bit(In_sync, &rdev->flags);
1687 static struct rdev_sysfs_entry rdev_slot =
1688 __ATTR(slot, 0644, slot_show, slot_store);
1691 offset_show(mdk_rdev_t *rdev, char *page)
1693 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
1697 offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1700 unsigned long long offset = simple_strtoull(buf, &e, 10);
1701 if (e==buf || (*e && *e != '\n'))
1703 if (rdev->mddev->pers)
1705 rdev->data_offset = offset;
1709 static struct rdev_sysfs_entry rdev_offset =
1710 __ATTR(offset, 0644, offset_show, offset_store);
1713 rdev_size_show(mdk_rdev_t *rdev, char *page)
1715 return sprintf(page, "%llu\n", (unsigned long long)rdev->size);
1719 rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1722 unsigned long long size = simple_strtoull(buf, &e, 10);
1723 if (e==buf || (*e && *e != '\n'))
1725 if (rdev->mddev->pers)
1728 if (size < rdev->mddev->size || rdev->mddev->size == 0)
1729 rdev->mddev->size = size;
1733 static struct rdev_sysfs_entry rdev_size =
1734 __ATTR(size, 0644, rdev_size_show, rdev_size_store);
1736 static struct attribute *rdev_default_attrs[] = {
1746 rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
1748 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1749 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1753 return entry->show(rdev, page);
1757 rdev_attr_store(struct kobject *kobj, struct attribute *attr,
1758 const char *page, size_t length)
1760 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1761 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1765 return entry->store(rdev, page, length);
1768 static void rdev_free(struct kobject *ko)
1770 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
1773 static struct sysfs_ops rdev_sysfs_ops = {
1774 .show = rdev_attr_show,
1775 .store = rdev_attr_store,
1777 static struct kobj_type rdev_ktype = {
1778 .release = rdev_free,
1779 .sysfs_ops = &rdev_sysfs_ops,
1780 .default_attrs = rdev_default_attrs,
1784 * Import a device. If 'super_format' >= 0, then sanity check the superblock
1786 * mark the device faulty if:
1788 * - the device is nonexistent (zero size)
1789 * - the device has no valid superblock
1791 * a faulty rdev _never_ has rdev->sb set.
1793 static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
1795 char b[BDEVNAME_SIZE];
1800 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
1802 printk(KERN_ERR "md: could not alloc mem for new device!\n");
1803 return ERR_PTR(-ENOMEM);
1806 if ((err = alloc_disk_sb(rdev)))
1809 err = lock_rdev(rdev, newdev);
1813 rdev->kobj.parent = NULL;
1814 rdev->kobj.ktype = &rdev_ktype;
1815 kobject_init(&rdev->kobj);
1819 rdev->data_offset = 0;
1820 atomic_set(&rdev->nr_pending, 0);
1821 atomic_set(&rdev->read_errors, 0);
1822 atomic_set(&rdev->corrected_errors, 0);
1824 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
1827 "md: %s has zero or unknown size, marking faulty!\n",
1828 bdevname(rdev->bdev,b));
1833 if (super_format >= 0) {
1834 err = super_types[super_format].
1835 load_super(rdev, NULL, super_minor);
1836 if (err == -EINVAL) {
1838 "md: %s has invalid sb, not importing!\n",
1839 bdevname(rdev->bdev,b));
1844 "md: could not read %s's sb, not importing!\n",
1845 bdevname(rdev->bdev,b));
1849 INIT_LIST_HEAD(&rdev->same_set);
1854 if (rdev->sb_page) {
1860 return ERR_PTR(err);
1864 * Check a full RAID array for plausibility
1868 static void analyze_sbs(mddev_t * mddev)
1871 struct list_head *tmp;
1872 mdk_rdev_t *rdev, *freshest;
1873 char b[BDEVNAME_SIZE];
1876 ITERATE_RDEV(mddev,rdev,tmp)
1877 switch (super_types[mddev->major_version].
1878 load_super(rdev, freshest, mddev->minor_version)) {
1886 "md: fatal superblock inconsistency in %s"
1887 " -- removing from array\n",
1888 bdevname(rdev->bdev,b));
1889 kick_rdev_from_array(rdev);
1893 super_types[mddev->major_version].
1894 validate_super(mddev, freshest);
1897 ITERATE_RDEV(mddev,rdev,tmp) {
1898 if (rdev != freshest)
1899 if (super_types[mddev->major_version].
1900 validate_super(mddev, rdev)) {
1901 printk(KERN_WARNING "md: kicking non-fresh %s"
1903 bdevname(rdev->bdev,b));
1904 kick_rdev_from_array(rdev);
1907 if (mddev->level == LEVEL_MULTIPATH) {
1908 rdev->desc_nr = i++;
1909 rdev->raid_disk = rdev->desc_nr;
1910 set_bit(In_sync, &rdev->flags);
1916 if (mddev->recovery_cp != MaxSector &&
1918 printk(KERN_ERR "md: %s: raid array is not clean"
1919 " -- starting background reconstruction\n",
1925 level_show(mddev_t *mddev, char *page)
1927 struct mdk_personality *p = mddev->pers;
1929 return sprintf(page, "%s\n", p->name);
1930 else if (mddev->clevel[0])
1931 return sprintf(page, "%s\n", mddev->clevel);
1932 else if (mddev->level != LEVEL_NONE)
1933 return sprintf(page, "%d\n", mddev->level);
1939 level_store(mddev_t *mddev, const char *buf, size_t len)
1946 if (len >= sizeof(mddev->clevel))
1948 strncpy(mddev->clevel, buf, len);
1949 if (mddev->clevel[len-1] == '\n')
1951 mddev->clevel[len] = 0;
1952 mddev->level = LEVEL_NONE;
1956 static struct md_sysfs_entry md_level =
1957 __ATTR(level, 0644, level_show, level_store);
1960 raid_disks_show(mddev_t *mddev, char *page)
1962 if (mddev->raid_disks == 0)
1964 return sprintf(page, "%d\n", mddev->raid_disks);
1967 static int update_raid_disks(mddev_t *mddev, int raid_disks);
1970 raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
1972 /* can only set raid_disks if array is not yet active */
1975 unsigned long n = simple_strtoul(buf, &e, 10);
1977 if (!*buf || (*e && *e != '\n'))
1981 rv = update_raid_disks(mddev, n);
1983 mddev->raid_disks = n;
1984 return rv ? rv : len;
1986 static struct md_sysfs_entry md_raid_disks =
1987 __ATTR(raid_disks, 0644, raid_disks_show, raid_disks_store);
1990 chunk_size_show(mddev_t *mddev, char *page)
1992 return sprintf(page, "%d\n", mddev->chunk_size);
1996 chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
1998 /* can only set chunk_size if array is not yet active */
2000 unsigned long n = simple_strtoul(buf, &e, 10);
2004 if (!*buf || (*e && *e != '\n'))
2007 mddev->chunk_size = n;
2010 static struct md_sysfs_entry md_chunk_size =
2011 __ATTR(chunk_size, 0644, chunk_size_show, chunk_size_store);
2014 null_show(mddev_t *mddev, char *page)
2020 new_dev_store(mddev_t *mddev, const char *buf, size_t len)
2022 /* buf must be %d:%d\n? giving major and minor numbers */
2023 /* The new device is added to the array.
2024 * If the array has a persistent superblock, we read the
2025 * superblock to initialise info and check validity.
2026 * Otherwise, only checking done is that in bind_rdev_to_array,
2027 * which mainly checks size.
2030 int major = simple_strtoul(buf, &e, 10);
2036 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
2038 minor = simple_strtoul(e+1, &e, 10);
2039 if (*e && *e != '\n')
2041 dev = MKDEV(major, minor);
2042 if (major != MAJOR(dev) ||
2043 minor != MINOR(dev))
2047 if (mddev->persistent) {
2048 rdev = md_import_device(dev, mddev->major_version,
2049 mddev->minor_version);
2050 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
2051 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2052 mdk_rdev_t, same_set);
2053 err = super_types[mddev->major_version]
2054 .load_super(rdev, rdev0, mddev->minor_version);
2059 rdev = md_import_device(dev, -1, -1);
2062 return PTR_ERR(rdev);
2063 err = bind_rdev_to_array(rdev, mddev);
2067 return err ? err : len;
2070 static struct md_sysfs_entry md_new_device =
2071 __ATTR(new_dev, 0200, null_show, new_dev_store);
2074 size_show(mddev_t *mddev, char *page)
2076 return sprintf(page, "%llu\n", (unsigned long long)mddev->size);
2079 static int update_size(mddev_t *mddev, unsigned long size);
2082 size_store(mddev_t *mddev, const char *buf, size_t len)
2084 /* If array is inactive, we can reduce the component size, but
2085 * not increase it (except from 0).
2086 * If array is active, we can try an on-line resize
2090 unsigned long long size = simple_strtoull(buf, &e, 10);
2091 if (!*buf || *buf == '\n' ||
2096 err = update_size(mddev, size);
2097 md_update_sb(mddev);
2099 if (mddev->size == 0 ||
2105 return err ? err : len;
2108 static struct md_sysfs_entry md_size =
2109 __ATTR(component_size, 0644, size_show, size_store);
2113 * This is either 'none' for arrays with externally managed metadata,
2114 * or N.M for internally known formats
2117 metadata_show(mddev_t *mddev, char *page)
2119 if (mddev->persistent)
2120 return sprintf(page, "%d.%d\n",
2121 mddev->major_version, mddev->minor_version);
2123 return sprintf(page, "none\n");
2127 metadata_store(mddev_t *mddev, const char *buf, size_t len)
2131 if (!list_empty(&mddev->disks))
2134 if (cmd_match(buf, "none")) {
2135 mddev->persistent = 0;
2136 mddev->major_version = 0;
2137 mddev->minor_version = 90;
2140 major = simple_strtoul(buf, &e, 10);
2141 if (e==buf || *e != '.')
2144 minor = simple_strtoul(buf, &e, 10);
2145 if (e==buf || *e != '\n')
2147 if (major >= sizeof(super_types)/sizeof(super_types[0]) ||
2148 super_types[major].name == NULL)
2150 mddev->major_version = major;
2151 mddev->minor_version = minor;
2152 mddev->persistent = 1;
2156 static struct md_sysfs_entry md_metadata =
2157 __ATTR(metadata_version, 0644, metadata_show, metadata_store);
2160 action_show(mddev_t *mddev, char *page)
2162 char *type = "idle";
2163 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2164 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) {
2165 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2166 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2168 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2175 return sprintf(page, "%s\n", type);
2179 action_store(mddev_t *mddev, const char *page, size_t len)
2181 if (!mddev->pers || !mddev->pers->sync_request)
2184 if (cmd_match(page, "idle")) {
2185 if (mddev->sync_thread) {
2186 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2187 md_unregister_thread(mddev->sync_thread);
2188 mddev->sync_thread = NULL;
2189 mddev->recovery = 0;
2191 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2192 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
2194 else if (cmd_match(page, "resync") || cmd_match(page, "recover"))
2195 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2197 if (cmd_match(page, "check"))
2198 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
2199 else if (cmd_match(page, "repair"))
2201 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
2202 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
2204 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2205 md_wakeup_thread(mddev->thread);
2210 mismatch_cnt_show(mddev_t *mddev, char *page)
2212 return sprintf(page, "%llu\n",
2213 (unsigned long long) mddev->resync_mismatches);
2216 static struct md_sysfs_entry
2217 md_scan_mode = __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
2220 static struct md_sysfs_entry
2221 md_mismatches = __ATTR_RO(mismatch_cnt);
2224 sync_min_show(mddev_t *mddev, char *page)
2226 return sprintf(page, "%d (%s)\n", speed_min(mddev),
2227 mddev->sync_speed_min ? "local": "system");
2231 sync_min_store(mddev_t *mddev, const char *buf, size_t len)
2235 if (strncmp(buf, "system", 6)==0) {
2236 mddev->sync_speed_min = 0;
2239 min = simple_strtoul(buf, &e, 10);
2240 if (buf == e || (*e && *e != '\n') || min <= 0)
2242 mddev->sync_speed_min = min;
2246 static struct md_sysfs_entry md_sync_min =
2247 __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
2250 sync_max_show(mddev_t *mddev, char *page)
2252 return sprintf(page, "%d (%s)\n", speed_max(mddev),
2253 mddev->sync_speed_max ? "local": "system");
2257 sync_max_store(mddev_t *mddev, const char *buf, size_t len)
2261 if (strncmp(buf, "system", 6)==0) {
2262 mddev->sync_speed_max = 0;
2265 max = simple_strtoul(buf, &e, 10);
2266 if (buf == e || (*e && *e != '\n') || max <= 0)
2268 mddev->sync_speed_max = max;
2272 static struct md_sysfs_entry md_sync_max =
2273 __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
2277 sync_speed_show(mddev_t *mddev, char *page)
2279 unsigned long resync, dt, db;
2280 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
2281 dt = ((jiffies - mddev->resync_mark) / HZ);
2283 db = resync - (mddev->resync_mark_cnt);
2284 return sprintf(page, "%ld\n", db/dt/2); /* K/sec */
2287 static struct md_sysfs_entry
2288 md_sync_speed = __ATTR_RO(sync_speed);
2291 sync_completed_show(mddev_t *mddev, char *page)
2293 unsigned long max_blocks, resync;
2295 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2296 max_blocks = mddev->resync_max_sectors;
2298 max_blocks = mddev->size << 1;
2300 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
2301 return sprintf(page, "%lu / %lu\n", resync, max_blocks);
2304 static struct md_sysfs_entry
2305 md_sync_completed = __ATTR_RO(sync_completed);
2307 static struct attribute *md_default_attrs[] = {
2309 &md_raid_disks.attr,
2310 &md_chunk_size.attr,
2313 &md_new_device.attr,
2317 static struct attribute *md_redundancy_attrs[] = {
2319 &md_mismatches.attr,
2322 &md_sync_speed.attr,
2323 &md_sync_completed.attr,
2326 static struct attribute_group md_redundancy_group = {
2328 .attrs = md_redundancy_attrs,
2333 md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2335 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
2336 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
2342 rv = entry->show(mddev, page);
2343 mddev_unlock(mddev);
2348 md_attr_store(struct kobject *kobj, struct attribute *attr,
2349 const char *page, size_t length)
2351 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
2352 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
2358 rv = entry->store(mddev, page, length);
2359 mddev_unlock(mddev);
2363 static void md_free(struct kobject *ko)
2365 mddev_t *mddev = container_of(ko, mddev_t, kobj);
2369 static struct sysfs_ops md_sysfs_ops = {
2370 .show = md_attr_show,
2371 .store = md_attr_store,
2373 static struct kobj_type md_ktype = {
2375 .sysfs_ops = &md_sysfs_ops,
2376 .default_attrs = md_default_attrs,
2381 static struct kobject *md_probe(dev_t dev, int *part, void *data)
2383 static DECLARE_MUTEX(disks_sem);
2384 mddev_t *mddev = mddev_find(dev);
2385 struct gendisk *disk;
2386 int partitioned = (MAJOR(dev) != MD_MAJOR);
2387 int shift = partitioned ? MdpMinorShift : 0;
2388 int unit = MINOR(dev) >> shift;
2394 if (mddev->gendisk) {
2399 disk = alloc_disk(1 << shift);
2405 disk->major = MAJOR(dev);
2406 disk->first_minor = unit << shift;
2408 sprintf(disk->disk_name, "md_d%d", unit);
2409 sprintf(disk->devfs_name, "md/d%d", unit);
2411 sprintf(disk->disk_name, "md%d", unit);
2412 sprintf(disk->devfs_name, "md/%d", unit);
2414 disk->fops = &md_fops;
2415 disk->private_data = mddev;
2416 disk->queue = mddev->queue;
2418 mddev->gendisk = disk;
2420 mddev->kobj.parent = &disk->kobj;
2421 mddev->kobj.k_name = NULL;
2422 snprintf(mddev->kobj.name, KOBJ_NAME_LEN, "%s", "md");
2423 mddev->kobj.ktype = &md_ktype;
2424 kobject_register(&mddev->kobj);
2428 void md_wakeup_thread(mdk_thread_t *thread);
2430 static void md_safemode_timeout(unsigned long data)
2432 mddev_t *mddev = (mddev_t *) data;
2434 mddev->safemode = 1;
2435 md_wakeup_thread(mddev->thread);
2438 static int start_dirty_degraded;
2440 static int do_md_run(mddev_t * mddev)
2444 struct list_head *tmp;
2446 struct gendisk *disk;
2447 struct mdk_personality *pers;
2448 char b[BDEVNAME_SIZE];
2450 if (list_empty(&mddev->disks))
2451 /* cannot run an array with no devices.. */
2458 * Analyze all RAID superblock(s)
2460 if (!mddev->raid_disks)
2463 chunk_size = mddev->chunk_size;
2466 if (chunk_size > MAX_CHUNK_SIZE) {
2467 printk(KERN_ERR "too big chunk_size: %d > %d\n",
2468 chunk_size, MAX_CHUNK_SIZE);
2472 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
2474 if ( (1 << ffz(~chunk_size)) != chunk_size) {
2475 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
2478 if (chunk_size < PAGE_SIZE) {
2479 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
2480 chunk_size, PAGE_SIZE);
2484 /* devices must have minimum size of one chunk */
2485 ITERATE_RDEV(mddev,rdev,tmp) {
2486 if (test_bit(Faulty, &rdev->flags))
2488 if (rdev->size < chunk_size / 1024) {
2490 "md: Dev %s smaller than chunk_size:"
2492 bdevname(rdev->bdev,b),
2493 (unsigned long long)rdev->size,
2501 if (mddev->level != LEVEL_NONE)
2502 request_module("md-level-%d", mddev->level);
2503 else if (mddev->clevel[0])
2504 request_module("md-%s", mddev->clevel);
2508 * Drop all container device buffers, from now on
2509 * the only valid external interface is through the md
2511 * Also find largest hardsector size
2513 ITERATE_RDEV(mddev,rdev,tmp) {
2514 if (test_bit(Faulty, &rdev->flags))
2516 sync_blockdev(rdev->bdev);
2517 invalidate_bdev(rdev->bdev, 0);
2520 md_probe(mddev->unit, NULL, NULL);
2521 disk = mddev->gendisk;
2525 spin_lock(&pers_lock);
2526 pers = find_pers(mddev->level, mddev->clevel);
2527 if (!pers || !try_module_get(pers->owner)) {
2528 spin_unlock(&pers_lock);
2529 if (mddev->level != LEVEL_NONE)
2530 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
2533 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
2538 spin_unlock(&pers_lock);
2539 mddev->level = pers->level;
2540 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
2542 mddev->recovery = 0;
2543 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
2544 mddev->barriers_work = 1;
2545 mddev->ok_start_degraded = start_dirty_degraded;
2548 mddev->ro = 2; /* read-only, but switch on first write */
2550 err = mddev->pers->run(mddev);
2551 if (!err && mddev->pers->sync_request) {
2552 err = bitmap_create(mddev);
2554 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
2555 mdname(mddev), err);
2556 mddev->pers->stop(mddev);
2560 printk(KERN_ERR "md: pers->run() failed ...\n");
2561 module_put(mddev->pers->owner);
2563 bitmap_destroy(mddev);
2566 if (mddev->pers->sync_request)
2567 sysfs_create_group(&mddev->kobj, &md_redundancy_group);
2568 else if (mddev->ro == 2) /* auto-readonly not meaningful */
2571 atomic_set(&mddev->writes_pending,0);
2572 mddev->safemode = 0;
2573 mddev->safemode_timer.function = md_safemode_timeout;
2574 mddev->safemode_timer.data = (unsigned long) mddev;
2575 mddev->safemode_delay = (20 * HZ)/1000 +1; /* 20 msec delay */
2578 ITERATE_RDEV(mddev,rdev,tmp)
2579 if (rdev->raid_disk >= 0) {
2581 sprintf(nm, "rd%d", rdev->raid_disk);
2582 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
2585 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2586 md_wakeup_thread(mddev->thread);
2588 if (mddev->sb_dirty)
2589 md_update_sb(mddev);
2591 set_capacity(disk, mddev->array_size<<1);
2593 /* If we call blk_queue_make_request here, it will
2594 * re-initialise max_sectors etc which may have been
2595 * refined inside -> run. So just set the bits we need to set.
2596 * Most initialisation happended when we called
2597 * blk_queue_make_request(..., md_fail_request)
2600 mddev->queue->queuedata = mddev;
2601 mddev->queue->make_request_fn = mddev->pers->make_request;
2604 md_new_event(mddev);
2608 static int restart_array(mddev_t *mddev)
2610 struct gendisk *disk = mddev->gendisk;
2614 * Complain if it has no devices
2617 if (list_empty(&mddev->disks))
2625 mddev->safemode = 0;
2627 set_disk_ro(disk, 0);
2629 printk(KERN_INFO "md: %s switched to read-write mode.\n",
2632 * Kick recovery or resync if necessary
2634 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2635 md_wakeup_thread(mddev->thread);
2638 printk(KERN_ERR "md: %s has no personality assigned.\n",
2647 static int do_md_stop(mddev_t * mddev, int ro)
2650 struct gendisk *disk = mddev->gendisk;
2653 if (atomic_read(&mddev->active)>2) {
2654 printk("md: %s still in use.\n",mdname(mddev));
2658 if (mddev->sync_thread) {
2659 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2660 md_unregister_thread(mddev->sync_thread);
2661 mddev->sync_thread = NULL;
2664 del_timer_sync(&mddev->safemode_timer);
2666 invalidate_partition(disk, 0);
2674 bitmap_flush(mddev);
2675 md_super_wait(mddev);
2677 set_disk_ro(disk, 0);
2678 blk_queue_make_request(mddev->queue, md_fail_request);
2679 mddev->pers->stop(mddev);
2680 if (mddev->pers->sync_request)
2681 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
2683 module_put(mddev->pers->owner);
2688 if (!mddev->in_sync) {
2689 /* mark array as shutdown cleanly */
2691 md_update_sb(mddev);
2694 set_disk_ro(disk, 1);
2698 * Free resources if final stop
2702 struct list_head *tmp;
2703 struct gendisk *disk;
2704 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
2706 bitmap_destroy(mddev);
2707 if (mddev->bitmap_file) {
2708 atomic_set(&mddev->bitmap_file->f_dentry->d_inode->i_writecount, 1);
2709 fput(mddev->bitmap_file);
2710 mddev->bitmap_file = NULL;
2712 mddev->bitmap_offset = 0;
2714 ITERATE_RDEV(mddev,rdev,tmp)
2715 if (rdev->raid_disk >= 0) {
2717 sprintf(nm, "rd%d", rdev->raid_disk);
2718 sysfs_remove_link(&mddev->kobj, nm);
2721 export_array(mddev);
2723 mddev->array_size = 0;
2724 disk = mddev->gendisk;
2726 set_capacity(disk, 0);
2729 printk(KERN_INFO "md: %s switched to read-only mode.\n",
2732 md_new_event(mddev);
2737 static void autorun_array(mddev_t *mddev)
2740 struct list_head *tmp;
2743 if (list_empty(&mddev->disks))
2746 printk(KERN_INFO "md: running: ");
2748 ITERATE_RDEV(mddev,rdev,tmp) {
2749 char b[BDEVNAME_SIZE];
2750 printk("<%s>", bdevname(rdev->bdev,b));
2754 err = do_md_run (mddev);
2756 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
2757 do_md_stop (mddev, 0);
2762 * lets try to run arrays based on all disks that have arrived
2763 * until now. (those are in pending_raid_disks)
2765 * the method: pick the first pending disk, collect all disks with
2766 * the same UUID, remove all from the pending list and put them into
2767 * the 'same_array' list. Then order this list based on superblock
2768 * update time (freshest comes first), kick out 'old' disks and
2769 * compare superblocks. If everything's fine then run it.
2771 * If "unit" is allocated, then bump its reference count
2773 static void autorun_devices(int part)
2775 struct list_head candidates;
2776 struct list_head *tmp;
2777 mdk_rdev_t *rdev0, *rdev;
2779 char b[BDEVNAME_SIZE];
2781 printk(KERN_INFO "md: autorun ...\n");
2782 while (!list_empty(&pending_raid_disks)) {
2784 rdev0 = list_entry(pending_raid_disks.next,
2785 mdk_rdev_t, same_set);
2787 printk(KERN_INFO "md: considering %s ...\n",
2788 bdevname(rdev0->bdev,b));
2789 INIT_LIST_HEAD(&candidates);
2790 ITERATE_RDEV_PENDING(rdev,tmp)
2791 if (super_90_load(rdev, rdev0, 0) >= 0) {
2792 printk(KERN_INFO "md: adding %s ...\n",
2793 bdevname(rdev->bdev,b));
2794 list_move(&rdev->same_set, &candidates);
2797 * now we have a set of devices, with all of them having
2798 * mostly sane superblocks. It's time to allocate the
2801 if (rdev0->preferred_minor < 0 || rdev0->preferred_minor >= MAX_MD_DEVS) {
2802 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
2803 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
2807 dev = MKDEV(mdp_major,
2808 rdev0->preferred_minor << MdpMinorShift);
2810 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
2812 md_probe(dev, NULL, NULL);
2813 mddev = mddev_find(dev);
2816 "md: cannot allocate memory for md drive.\n");
2819 if (mddev_lock(mddev))
2820 printk(KERN_WARNING "md: %s locked, cannot run\n",
2822 else if (mddev->raid_disks || mddev->major_version
2823 || !list_empty(&mddev->disks)) {
2825 "md: %s already running, cannot run %s\n",
2826 mdname(mddev), bdevname(rdev0->bdev,b));
2827 mddev_unlock(mddev);
2829 printk(KERN_INFO "md: created %s\n", mdname(mddev));
2830 ITERATE_RDEV_GENERIC(candidates,rdev,tmp) {
2831 list_del_init(&rdev->same_set);
2832 if (bind_rdev_to_array(rdev, mddev))
2835 autorun_array(mddev);
2836 mddev_unlock(mddev);
2838 /* on success, candidates will be empty, on error
2841 ITERATE_RDEV_GENERIC(candidates,rdev,tmp)
2845 printk(KERN_INFO "md: ... autorun DONE.\n");
2849 * import RAID devices based on one partition
2850 * if possible, the array gets run as well.
2853 static int autostart_array(dev_t startdev)
2855 char b[BDEVNAME_SIZE];
2856 int err = -EINVAL, i;
2857 mdp_super_t *sb = NULL;
2858 mdk_rdev_t *start_rdev = NULL, *rdev;
2860 start_rdev = md_import_device(startdev, 0, 0);
2861 if (IS_ERR(start_rdev))
2865 /* NOTE: this can only work for 0.90.0 superblocks */
2866 sb = (mdp_super_t*)page_address(start_rdev->sb_page);
2867 if (sb->major_version != 0 ||
2868 sb->minor_version != 90 ) {
2869 printk(KERN_WARNING "md: can only autostart 0.90.0 arrays\n");
2870 export_rdev(start_rdev);
2874 if (test_bit(Faulty, &start_rdev->flags)) {
2876 "md: can not autostart based on faulty %s!\n",
2877 bdevname(start_rdev->bdev,b));
2878 export_rdev(start_rdev);
2881 list_add(&start_rdev->same_set, &pending_raid_disks);
2883 for (i = 0; i < MD_SB_DISKS; i++) {
2884 mdp_disk_t *desc = sb->disks + i;
2885 dev_t dev = MKDEV(desc->major, desc->minor);
2889 if (dev == startdev)
2891 if (MAJOR(dev) != desc->major || MINOR(dev) != desc->minor)
2893 rdev = md_import_device(dev, 0, 0);
2897 list_add(&rdev->same_set, &pending_raid_disks);
2901 * possibly return codes
2909 static int get_version(void __user * arg)
2913 ver.major = MD_MAJOR_VERSION;
2914 ver.minor = MD_MINOR_VERSION;
2915 ver.patchlevel = MD_PATCHLEVEL_VERSION;
2917 if (copy_to_user(arg, &ver, sizeof(ver)))
2923 static int get_array_info(mddev_t * mddev, void __user * arg)
2925 mdu_array_info_t info;
2926 int nr,working,active,failed,spare;
2928 struct list_head *tmp;
2930 nr=working=active=failed=spare=0;
2931 ITERATE_RDEV(mddev,rdev,tmp) {
2933 if (test_bit(Faulty, &rdev->flags))
2937 if (test_bit(In_sync, &rdev->flags))
2944 info.major_version = mddev->major_version;
2945 info.minor_version = mddev->minor_version;
2946 info.patch_version = MD_PATCHLEVEL_VERSION;
2947 info.ctime = mddev->ctime;
2948 info.level = mddev->level;
2949 info.size = mddev->size;
2950 if (info.size != mddev->size) /* overflow */
2953 info.raid_disks = mddev->raid_disks;
2954 info.md_minor = mddev->md_minor;
2955 info.not_persistent= !mddev->persistent;
2957 info.utime = mddev->utime;
2960 info.state = (1<<MD_SB_CLEAN);
2961 if (mddev->bitmap && mddev->bitmap_offset)
2962 info.state = (1<<MD_SB_BITMAP_PRESENT);
2963 info.active_disks = active;
2964 info.working_disks = working;
2965 info.failed_disks = failed;
2966 info.spare_disks = spare;
2968 info.layout = mddev->layout;
2969 info.chunk_size = mddev->chunk_size;
2971 if (copy_to_user(arg, &info, sizeof(info)))
2977 static int get_bitmap_file(mddev_t * mddev, void __user * arg)
2979 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
2980 char *ptr, *buf = NULL;
2983 file = kmalloc(sizeof(*file), GFP_KERNEL);
2987 /* bitmap disabled, zero the first byte and copy out */
2988 if (!mddev->bitmap || !mddev->bitmap->file) {
2989 file->pathname[0] = '\0';
2993 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
2997 ptr = file_path(mddev->bitmap->file, buf, sizeof(file->pathname));
3001 strcpy(file->pathname, ptr);
3005 if (copy_to_user(arg, file, sizeof(*file)))
3013 static int get_disk_info(mddev_t * mddev, void __user * arg)
3015 mdu_disk_info_t info;
3019 if (copy_from_user(&info, arg, sizeof(info)))
3024 rdev = find_rdev_nr(mddev, nr);
3026 info.major = MAJOR(rdev->bdev->bd_dev);
3027 info.minor = MINOR(rdev->bdev->bd_dev);
3028 info.raid_disk = rdev->raid_disk;
3030 if (test_bit(Faulty, &rdev->flags))
3031 info.state |= (1<<MD_DISK_FAULTY);
3032 else if (test_bit(In_sync, &rdev->flags)) {
3033 info.state |= (1<<MD_DISK_ACTIVE);
3034 info.state |= (1<<MD_DISK_SYNC);
3036 if (test_bit(WriteMostly, &rdev->flags))
3037 info.state |= (1<<MD_DISK_WRITEMOSTLY);
3039 info.major = info.minor = 0;
3040 info.raid_disk = -1;
3041 info.state = (1<<MD_DISK_REMOVED);
3044 if (copy_to_user(arg, &info, sizeof(info)))
3050 static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
3052 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3054 dev_t dev = MKDEV(info->major,info->minor);
3056 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
3059 if (!mddev->raid_disks) {
3061 /* expecting a device which has a superblock */
3062 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
3065 "md: md_import_device returned %ld\n",
3067 return PTR_ERR(rdev);
3069 if (!list_empty(&mddev->disks)) {
3070 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3071 mdk_rdev_t, same_set);
3072 int err = super_types[mddev->major_version]
3073 .load_super(rdev, rdev0, mddev->minor_version);
3076 "md: %s has different UUID to %s\n",
3077 bdevname(rdev->bdev,b),
3078 bdevname(rdev0->bdev,b2));
3083 err = bind_rdev_to_array(rdev, mddev);
3090 * add_new_disk can be used once the array is assembled
3091 * to add "hot spares". They must already have a superblock
3096 if (!mddev->pers->hot_add_disk) {
3098 "%s: personality does not support diskops!\n",
3102 if (mddev->persistent)
3103 rdev = md_import_device(dev, mddev->major_version,
3104 mddev->minor_version);
3106 rdev = md_import_device(dev, -1, -1);
3109 "md: md_import_device returned %ld\n",
3111 return PTR_ERR(rdev);
3113 /* set save_raid_disk if appropriate */
3114 if (!mddev->persistent) {
3115 if (info->state & (1<<MD_DISK_SYNC) &&
3116 info->raid_disk < mddev->raid_disks)
3117 rdev->raid_disk = info->raid_disk;
3119 rdev->raid_disk = -1;
3121 super_types[mddev->major_version].
3122 validate_super(mddev, rdev);
3123 rdev->saved_raid_disk = rdev->raid_disk;
3125 clear_bit(In_sync, &rdev->flags); /* just to be sure */
3126 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
3127 set_bit(WriteMostly, &rdev->flags);
3129 rdev->raid_disk = -1;
3130 err = bind_rdev_to_array(rdev, mddev);
3134 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3135 md_wakeup_thread(mddev->thread);
3139 /* otherwise, add_new_disk is only allowed
3140 * for major_version==0 superblocks
3142 if (mddev->major_version != 0) {
3143 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
3148 if (!(info->state & (1<<MD_DISK_FAULTY))) {
3150 rdev = md_import_device (dev, -1, 0);
3153 "md: error, md_import_device() returned %ld\n",
3155 return PTR_ERR(rdev);
3157 rdev->desc_nr = info->number;
3158 if (info->raid_disk < mddev->raid_disks)
3159 rdev->raid_disk = info->raid_disk;
3161 rdev->raid_disk = -1;
3165 if (rdev->raid_disk < mddev->raid_disks)
3166 if (info->state & (1<<MD_DISK_SYNC))
3167 set_bit(In_sync, &rdev->flags);
3169 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
3170 set_bit(WriteMostly, &rdev->flags);
3172 if (!mddev->persistent) {
3173 printk(KERN_INFO "md: nonpersistent superblock ...\n");
3174 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
3176 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
3177 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
3179 err = bind_rdev_to_array(rdev, mddev);
3189 static int hot_remove_disk(mddev_t * mddev, dev_t dev)
3191 char b[BDEVNAME_SIZE];
3197 rdev = find_rdev(mddev, dev);
3201 if (rdev->raid_disk >= 0)
3204 kick_rdev_from_array(rdev);
3205 md_update_sb(mddev);
3206 md_new_event(mddev);
3210 printk(KERN_WARNING "md: cannot remove active disk %s from %s ... \n",
3211 bdevname(rdev->bdev,b), mdname(mddev));
3215 static int hot_add_disk(mddev_t * mddev, dev_t dev)
3217 char b[BDEVNAME_SIZE];
3225 if (mddev->major_version != 0) {
3226 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
3227 " version-0 superblocks.\n",
3231 if (!mddev->pers->hot_add_disk) {
3233 "%s: personality does not support diskops!\n",
3238 rdev = md_import_device (dev, -1, 0);
3241 "md: error, md_import_device() returned %ld\n",
3246 if (mddev->persistent)
3247 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
3250 rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
3252 size = calc_dev_size(rdev, mddev->chunk_size);
3255 if (test_bit(Faulty, &rdev->flags)) {
3257 "md: can not hot-add faulty %s disk to %s!\n",
3258 bdevname(rdev->bdev,b), mdname(mddev));
3262 clear_bit(In_sync, &rdev->flags);
3264 err = bind_rdev_to_array(rdev, mddev);
3269 * The rest should better be atomic, we can have disk failures
3270 * noticed in interrupt contexts ...
3273 if (rdev->desc_nr == mddev->max_disks) {
3274 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
3277 goto abort_unbind_export;
3280 rdev->raid_disk = -1;
3282 md_update_sb(mddev);
3285 * Kick recovery, maybe this spare has to be added to the
3286 * array immediately.
3288 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3289 md_wakeup_thread(mddev->thread);
3290 md_new_event(mddev);
3293 abort_unbind_export:
3294 unbind_rdev_from_array(rdev);
3301 /* similar to deny_write_access, but accounts for our holding a reference
3302 * to the file ourselves */
3303 static int deny_bitmap_write_access(struct file * file)
3305 struct inode *inode = file->f_mapping->host;
3307 spin_lock(&inode->i_lock);
3308 if (atomic_read(&inode->i_writecount) > 1) {
3309 spin_unlock(&inode->i_lock);
3312 atomic_set(&inode->i_writecount, -1);
3313 spin_unlock(&inode->i_lock);
3318 static int set_bitmap_file(mddev_t *mddev, int fd)
3323 if (!mddev->pers->quiesce)
3325 if (mddev->recovery || mddev->sync_thread)
3327 /* we should be able to change the bitmap.. */
3333 return -EEXIST; /* cannot add when bitmap is present */
3334 mddev->bitmap_file = fget(fd);
3336 if (mddev->bitmap_file == NULL) {
3337 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
3342 err = deny_bitmap_write_access(mddev->bitmap_file);
3344 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
3346 fput(mddev->bitmap_file);
3347 mddev->bitmap_file = NULL;
3350 mddev->bitmap_offset = 0; /* file overrides offset */
3351 } else if (mddev->bitmap == NULL)
3352 return -ENOENT; /* cannot remove what isn't there */
3355 mddev->pers->quiesce(mddev, 1);
3357 err = bitmap_create(mddev);
3359 bitmap_destroy(mddev);
3360 mddev->pers->quiesce(mddev, 0);
3361 } else if (fd < 0) {
3362 if (mddev->bitmap_file)
3363 fput(mddev->bitmap_file);
3364 mddev->bitmap_file = NULL;
3371 * set_array_info is used two different ways
3372 * The original usage is when creating a new array.
3373 * In this usage, raid_disks is > 0 and it together with
3374 * level, size, not_persistent,layout,chunksize determine the
3375 * shape of the array.
3376 * This will always create an array with a type-0.90.0 superblock.
3377 * The newer usage is when assembling an array.
3378 * In this case raid_disks will be 0, and the major_version field is
3379 * use to determine which style super-blocks are to be found on the devices.
3380 * The minor and patch _version numbers are also kept incase the
3381 * super_block handler wishes to interpret them.
3383 static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
3386 if (info->raid_disks == 0) {
3387 /* just setting version number for superblock loading */
3388 if (info->major_version < 0 ||
3389 info->major_version >= sizeof(super_types)/sizeof(super_types[0]) ||
3390 super_types[info->major_version].name == NULL) {
3391 /* maybe try to auto-load a module? */
3393 "md: superblock version %d not known\n",
3394 info->major_version);
3397 mddev->major_version = info->major_version;
3398 mddev->minor_version = info->minor_version;
3399 mddev->patch_version = info->patch_version;
3402 mddev->major_version = MD_MAJOR_VERSION;
3403 mddev->minor_version = MD_MINOR_VERSION;
3404 mddev->patch_version = MD_PATCHLEVEL_VERSION;
3405 mddev->ctime = get_seconds();
3407 mddev->level = info->level;
3408 mddev->clevel[0] = 0;
3409 mddev->size = info->size;
3410 mddev->raid_disks = info->raid_disks;
3411 /* don't set md_minor, it is determined by which /dev/md* was
3414 if (info->state & (1<<MD_SB_CLEAN))
3415 mddev->recovery_cp = MaxSector;
3417 mddev->recovery_cp = 0;
3418 mddev->persistent = ! info->not_persistent;
3420 mddev->layout = info->layout;
3421 mddev->chunk_size = info->chunk_size;
3423 mddev->max_disks = MD_SB_DISKS;
3425 mddev->sb_dirty = 1;
3427 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
3428 mddev->bitmap_offset = 0;
3431 * Generate a 128 bit UUID
3433 get_random_bytes(mddev->uuid, 16);
3438 static int update_size(mddev_t *mddev, unsigned long size)
3442 struct list_head *tmp;
3444 if (mddev->pers->resize == NULL)
3446 /* The "size" is the amount of each device that is used.
3447 * This can only make sense for arrays with redundancy.
3448 * linear and raid0 always use whatever space is available
3449 * We can only consider changing the size if no resync
3450 * or reconstruction is happening, and if the new size
3451 * is acceptable. It must fit before the sb_offset or,
3452 * if that is <data_offset, it must fit before the
3453 * size of each device.
3454 * If size is zero, we find the largest size that fits.
3456 if (mddev->sync_thread)
3458 ITERATE_RDEV(mddev,rdev,tmp) {
3460 int fit = (size == 0);
3461 if (rdev->sb_offset > rdev->data_offset)
3462 avail = (rdev->sb_offset*2) - rdev->data_offset;
3464 avail = get_capacity(rdev->bdev->bd_disk)
3465 - rdev->data_offset;
3466 if (fit && (size == 0 || size > avail/2))
3468 if (avail < ((sector_t)size << 1))
3471 rv = mddev->pers->resize(mddev, (sector_t)size *2);
3473 struct block_device *bdev;
3475 bdev = bdget_disk(mddev->gendisk, 0);
3477 mutex_lock(&bdev->bd_inode->i_mutex);
3478 i_size_write(bdev->bd_inode, (loff_t)mddev->array_size << 10);
3479 mutex_unlock(&bdev->bd_inode->i_mutex);
3486 static int update_raid_disks(mddev_t *mddev, int raid_disks)
3489 /* change the number of raid disks */
3490 if (mddev->pers->reshape == NULL)
3492 if (raid_disks <= 0 ||
3493 raid_disks >= mddev->max_disks)
3495 if (mddev->sync_thread)
3497 rv = mddev->pers->reshape(mddev, raid_disks);
3503 * update_array_info is used to change the configuration of an
3505 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
3506 * fields in the info are checked against the array.
3507 * Any differences that cannot be handled will cause an error.
3508 * Normally, only one change can be managed at a time.
3510 static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
3516 /* calculate expected state,ignoring low bits */
3517 if (mddev->bitmap && mddev->bitmap_offset)
3518 state |= (1 << MD_SB_BITMAP_PRESENT);
3520 if (mddev->major_version != info->major_version ||
3521 mddev->minor_version != info->minor_version ||
3522 /* mddev->patch_version != info->patch_version || */
3523 mddev->ctime != info->ctime ||
3524 mddev->level != info->level ||
3525 /* mddev->layout != info->layout || */
3526 !mddev->persistent != info->not_persistent||
3527 mddev->chunk_size != info->chunk_size ||
3528 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
3529 ((state^info->state) & 0xfffffe00)
3532 /* Check there is only one change */
3533 if (info->size >= 0 && mddev->size != info->size) cnt++;
3534 if (mddev->raid_disks != info->raid_disks) cnt++;
3535 if (mddev->layout != info->layout) cnt++;
3536 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
3537 if (cnt == 0) return 0;
3538 if (cnt > 1) return -EINVAL;
3540 if (mddev->layout != info->layout) {
3542 * we don't need to do anything at the md level, the
3543 * personality will take care of it all.
3545 if (mddev->pers->reconfig == NULL)
3548 return mddev->pers->reconfig(mddev, info->layout, -1);
3550 if (info->size >= 0 && mddev->size != info->size)
3551 rv = update_size(mddev, info->size);
3553 if (mddev->raid_disks != info->raid_disks)
3554 rv = update_raid_disks(mddev, info->raid_disks);
3556 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
3557 if (mddev->pers->quiesce == NULL)
3559 if (mddev->recovery || mddev->sync_thread)
3561 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
3562 /* add the bitmap */
3565 if (mddev->default_bitmap_offset == 0)
3567 mddev->bitmap_offset = mddev->default_bitmap_offset;
3568 mddev->pers->quiesce(mddev, 1);
3569 rv = bitmap_create(mddev);
3571 bitmap_destroy(mddev);
3572 mddev->pers->quiesce(mddev, 0);
3574 /* remove the bitmap */
3577 if (mddev->bitmap->file)
3579 mddev->pers->quiesce(mddev, 1);
3580 bitmap_destroy(mddev);
3581 mddev->pers->quiesce(mddev, 0);
3582 mddev->bitmap_offset = 0;
3585 md_update_sb(mddev);
3589 static int set_disk_faulty(mddev_t *mddev, dev_t dev)
3593 if (mddev->pers == NULL)
3596 rdev = find_rdev(mddev, dev);
3600 md_error(mddev, rdev);
3604 static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
3606 mddev_t *mddev = bdev->bd_disk->private_data;
3610 geo->cylinders = get_capacity(mddev->gendisk) / 8;
3614 static int md_ioctl(struct inode *inode, struct file *file,
3615 unsigned int cmd, unsigned long arg)
3618 void __user *argp = (void __user *)arg;
3619 mddev_t *mddev = NULL;
3621 if (!capable(CAP_SYS_ADMIN))
3625 * Commands dealing with the RAID driver but not any
3631 err = get_version(argp);
3634 case PRINT_RAID_DEBUG:
3642 autostart_arrays(arg);
3649 * Commands creating/starting a new array:
3652 mddev = inode->i_bdev->bd_disk->private_data;
3660 if (cmd == START_ARRAY) {
3661 /* START_ARRAY doesn't need to lock the array as autostart_array
3662 * does the locking, and it could even be a different array
3667 "md: %s(pid %d) used deprecated START_ARRAY ioctl. "
3668 "This will not be supported beyond July 2006\n",
3669 current->comm, current->pid);
3672 err = autostart_array(new_decode_dev(arg));
3674 printk(KERN_WARNING "md: autostart failed!\n");
3680 err = mddev_lock(mddev);
3683 "md: ioctl lock interrupted, reason %d, cmd %d\n",
3690 case SET_ARRAY_INFO:
3692 mdu_array_info_t info;
3694 memset(&info, 0, sizeof(info));
3695 else if (copy_from_user(&info, argp, sizeof(info))) {
3700 err = update_array_info(mddev, &info);
3702 printk(KERN_WARNING "md: couldn't update"
3703 " array info. %d\n", err);
3708 if (!list_empty(&mddev->disks)) {
3710 "md: array %s already has disks!\n",
3715 if (mddev->raid_disks) {
3717 "md: array %s already initialised!\n",
3722 err = set_array_info(mddev, &info);
3724 printk(KERN_WARNING "md: couldn't set"
3725 " array info. %d\n", err);
3735 * Commands querying/configuring an existing array:
3737 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
3738 * RUN_ARRAY, and SET_BITMAP_FILE are allowed */
3739 if (!mddev->raid_disks && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
3740 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE) {
3746 * Commands even a read-only array can execute:
3750 case GET_ARRAY_INFO:
3751 err = get_array_info(mddev, argp);
3754 case GET_BITMAP_FILE:
3755 err = get_bitmap_file(mddev, argp);
3759 err = get_disk_info(mddev, argp);
3762 case RESTART_ARRAY_RW:
3763 err = restart_array(mddev);
3767 err = do_md_stop (mddev, 0);
3771 err = do_md_stop (mddev, 1);
3775 * We have a problem here : there is no easy way to give a CHS
3776 * virtual geometry. We currently pretend that we have a 2 heads
3777 * 4 sectors (with a BIG number of cylinders...). This drives
3778 * dosfs just mad... ;-)
3783 * The remaining ioctls are changing the state of the
3784 * superblock, so we do not allow them on read-only arrays.
3785 * However non-MD ioctls (e.g. get-size) will still come through
3786 * here and hit the 'default' below, so only disallow
3787 * 'md' ioctls, and switch to rw mode if started auto-readonly.
3789 if (_IOC_TYPE(cmd) == MD_MAJOR &&
3790 mddev->ro && mddev->pers) {
3791 if (mddev->ro == 2) {
3793 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3794 md_wakeup_thread(mddev->thread);
3806 mdu_disk_info_t info;
3807 if (copy_from_user(&info, argp, sizeof(info)))
3810 err = add_new_disk(mddev, &info);
3814 case HOT_REMOVE_DISK:
3815 err = hot_remove_disk(mddev, new_decode_dev(arg));
3819 err = hot_add_disk(mddev, new_decode_dev(arg));
3822 case SET_DISK_FAULTY:
3823 err = set_disk_faulty(mddev, new_decode_dev(arg));
3827 err = do_md_run (mddev);
3830 case SET_BITMAP_FILE:
3831 err = set_bitmap_file(mddev, (int)arg);
3835 if (_IOC_TYPE(cmd) == MD_MAJOR)
3836 printk(KERN_WARNING "md: %s(pid %d) used"
3837 " obsolete MD ioctl, upgrade your"
3838 " software to use new ictls.\n",
3839 current->comm, current->pid);
3846 mddev_unlock(mddev);
3856 static int md_open(struct inode *inode, struct file *file)
3859 * Succeed if we can lock the mddev, which confirms that
3860 * it isn't being stopped right now.
3862 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
3865 if ((err = mddev_lock(mddev)))
3870 mddev_unlock(mddev);
3872 check_disk_change(inode->i_bdev);
3877 static int md_release(struct inode *inode, struct file * file)
3879 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
3888 static int md_media_changed(struct gendisk *disk)
3890 mddev_t *mddev = disk->private_data;
3892 return mddev->changed;
3895 static int md_revalidate(struct gendisk *disk)
3897 mddev_t *mddev = disk->private_data;
3902 static struct block_device_operations md_fops =
3904 .owner = THIS_MODULE,
3906 .release = md_release,
3908 .getgeo = md_getgeo,
3909 .media_changed = md_media_changed,
3910 .revalidate_disk= md_revalidate,
3913 static int md_thread(void * arg)
3915 mdk_thread_t *thread = arg;
3918 * md_thread is a 'system-thread', it's priority should be very
3919 * high. We avoid resource deadlocks individually in each
3920 * raid personality. (RAID5 does preallocation) We also use RR and
3921 * the very same RT priority as kswapd, thus we will never get
3922 * into a priority inversion deadlock.
3924 * we definitely have to have equal or higher priority than
3925 * bdflush, otherwise bdflush will deadlock if there are too
3926 * many dirty RAID5 blocks.
3929 allow_signal(SIGKILL);
3930 while (!kthread_should_stop()) {
3932 /* We need to wait INTERRUPTIBLE so that
3933 * we don't add to the load-average.
3934 * That means we need to be sure no signals are
3937 if (signal_pending(current))
3938 flush_signals(current);
3940 wait_event_interruptible_timeout
3942 test_bit(THREAD_WAKEUP, &thread->flags)
3943 || kthread_should_stop(),
3947 clear_bit(THREAD_WAKEUP, &thread->flags);
3949 thread->run(thread->mddev);
3955 void md_wakeup_thread(mdk_thread_t *thread)
3958 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
3959 set_bit(THREAD_WAKEUP, &thread->flags);
3960 wake_up(&thread->wqueue);
3964 mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
3967 mdk_thread_t *thread;
3969 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
3973 init_waitqueue_head(&thread->wqueue);
3976 thread->mddev = mddev;
3977 thread->timeout = MAX_SCHEDULE_TIMEOUT;
3978 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
3979 if (IS_ERR(thread->tsk)) {
3986 void md_unregister_thread(mdk_thread_t *thread)
3988 dprintk("interrupting MD-thread pid %d\n", thread->tsk->pid);
3990 kthread_stop(thread->tsk);
3994 void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
4001 if (!rdev || test_bit(Faulty, &rdev->flags))
4004 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
4006 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
4007 __builtin_return_address(0),__builtin_return_address(1),
4008 __builtin_return_address(2),__builtin_return_address(3));
4010 if (!mddev->pers->error_handler)
4012 mddev->pers->error_handler(mddev,rdev);
4013 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4014 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4015 md_wakeup_thread(mddev->thread);
4016 md_new_event(mddev);
4019 /* seq_file implementation /proc/mdstat */
4021 static void status_unused(struct seq_file *seq)
4025 struct list_head *tmp;
4027 seq_printf(seq, "unused devices: ");
4029 ITERATE_RDEV_PENDING(rdev,tmp) {
4030 char b[BDEVNAME_SIZE];
4032 seq_printf(seq, "%s ",
4033 bdevname(rdev->bdev,b));
4036 seq_printf(seq, "<none>");
4038 seq_printf(seq, "\n");
4042 static void status_resync(struct seq_file *seq, mddev_t * mddev)
4044 unsigned long max_blocks, resync, res, dt, db, rt;
4046 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
4048 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
4049 max_blocks = mddev->resync_max_sectors >> 1;
4051 max_blocks = mddev->size;
4054 * Should not happen.
4060 res = (resync/1024)*1000/(max_blocks/1024 + 1);
4062 int i, x = res/50, y = 20-x;
4063 seq_printf(seq, "[");
4064 for (i = 0; i < x; i++)
4065 seq_printf(seq, "=");
4066 seq_printf(seq, ">");
4067 for (i = 0; i < y; i++)
4068 seq_printf(seq, ".");
4069 seq_printf(seq, "] ");
4071 seq_printf(seq, " %s =%3lu.%lu%% (%lu/%lu)",
4072 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
4073 "resync" : "recovery"),
4074 res/10, res % 10, resync, max_blocks);
4077 * We do not want to overflow, so the order of operands and
4078 * the * 100 / 100 trick are important. We do a +1 to be
4079 * safe against division by zero. We only estimate anyway.
4081 * dt: time from mark until now
4082 * db: blocks written from mark until now
4083 * rt: remaining time
4085 dt = ((jiffies - mddev->resync_mark) / HZ);
4087 db = resync - (mddev->resync_mark_cnt/2);
4088 rt = (dt * ((max_blocks-resync) / (db/100+1)))/100;
4090 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
4092 seq_printf(seq, " speed=%ldK/sec", db/dt);
4095 static void *md_seq_start(struct seq_file *seq, loff_t *pos)
4097 struct list_head *tmp;
4107 spin_lock(&all_mddevs_lock);
4108 list_for_each(tmp,&all_mddevs)
4110 mddev = list_entry(tmp, mddev_t, all_mddevs);
4112 spin_unlock(&all_mddevs_lock);
4115 spin_unlock(&all_mddevs_lock);
4117 return (void*)2;/* tail */
4121 static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4123 struct list_head *tmp;
4124 mddev_t *next_mddev, *mddev = v;
4130 spin_lock(&all_mddevs_lock);
4132 tmp = all_mddevs.next;
4134 tmp = mddev->all_mddevs.next;
4135 if (tmp != &all_mddevs)
4136 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
4138 next_mddev = (void*)2;
4141 spin_unlock(&all_mddevs_lock);
4149 static void md_seq_stop(struct seq_file *seq, void *v)
4153 if (mddev && v != (void*)1 && v != (void*)2)
4157 struct mdstat_info {
4161 static int md_seq_show(struct seq_file *seq, void *v)
4165 struct list_head *tmp2;
4167 struct mdstat_info *mi = seq->private;
4168 struct bitmap *bitmap;
4170 if (v == (void*)1) {
4171 struct mdk_personality *pers;
4172 seq_printf(seq, "Personalities : ");
4173 spin_lock(&pers_lock);
4174 list_for_each_entry(pers, &pers_list, list)
4175 seq_printf(seq, "[%s] ", pers->name);
4177 spin_unlock(&pers_lock);
4178 seq_printf(seq, "\n");
4179 mi->event = atomic_read(&md_event_count);
4182 if (v == (void*)2) {
4187 if (mddev_lock(mddev)!=0)
4189 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
4190 seq_printf(seq, "%s : %sactive", mdname(mddev),
4191 mddev->pers ? "" : "in");
4194 seq_printf(seq, " (read-only)");
4196 seq_printf(seq, "(auto-read-only)");
4197 seq_printf(seq, " %s", mddev->pers->name);
4201 ITERATE_RDEV(mddev,rdev,tmp2) {
4202 char b[BDEVNAME_SIZE];
4203 seq_printf(seq, " %s[%d]",
4204 bdevname(rdev->bdev,b), rdev->desc_nr);
4205 if (test_bit(WriteMostly, &rdev->flags))
4206 seq_printf(seq, "(W)");
4207 if (test_bit(Faulty, &rdev->flags)) {
4208 seq_printf(seq, "(F)");
4210 } else if (rdev->raid_disk < 0)
4211 seq_printf(seq, "(S)"); /* spare */
4215 if (!list_empty(&mddev->disks)) {
4217 seq_printf(seq, "\n %llu blocks",
4218 (unsigned long long)mddev->array_size);
4220 seq_printf(seq, "\n %llu blocks",
4221 (unsigned long long)size);
4223 if (mddev->persistent) {
4224 if (mddev->major_version != 0 ||
4225 mddev->minor_version != 90) {
4226 seq_printf(seq," super %d.%d",
4227 mddev->major_version,
4228 mddev->minor_version);
4231 seq_printf(seq, " super non-persistent");
4234 mddev->pers->status (seq, mddev);
4235 seq_printf(seq, "\n ");
4236 if (mddev->pers->sync_request) {
4237 if (mddev->curr_resync > 2) {
4238 status_resync (seq, mddev);
4239 seq_printf(seq, "\n ");
4240 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
4241 seq_printf(seq, "\tresync=DELAYED\n ");
4242 else if (mddev->recovery_cp < MaxSector)
4243 seq_printf(seq, "\tresync=PENDING\n ");
4246 seq_printf(seq, "\n ");
4248 if ((bitmap = mddev->bitmap)) {
4249 unsigned long chunk_kb;
4250 unsigned long flags;
4251 spin_lock_irqsave(&bitmap->lock, flags);
4252 chunk_kb = bitmap->chunksize >> 10;
4253 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
4255 bitmap->pages - bitmap->missing_pages,
4257 (bitmap->pages - bitmap->missing_pages)
4258 << (PAGE_SHIFT - 10),
4259 chunk_kb ? chunk_kb : bitmap->chunksize,
4260 chunk_kb ? "KB" : "B");
4262 seq_printf(seq, ", file: ");
4263 seq_path(seq, bitmap->file->f_vfsmnt,
4264 bitmap->file->f_dentry," \t\n");
4267 seq_printf(seq, "\n");
4268 spin_unlock_irqrestore(&bitmap->lock, flags);
4271 seq_printf(seq, "\n");
4273 mddev_unlock(mddev);
4278 static struct seq_operations md_seq_ops = {
4279 .start = md_seq_start,
4280 .next = md_seq_next,
4281 .stop = md_seq_stop,
4282 .show = md_seq_show,
4285 static int md_seq_open(struct inode *inode, struct file *file)
4288 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
4292 error = seq_open(file, &md_seq_ops);
4296 struct seq_file *p = file->private_data;
4298 mi->event = atomic_read(&md_event_count);
4303 static int md_seq_release(struct inode *inode, struct file *file)
4305 struct seq_file *m = file->private_data;
4306 struct mdstat_info *mi = m->private;
4309 return seq_release(inode, file);
4312 static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
4314 struct seq_file *m = filp->private_data;
4315 struct mdstat_info *mi = m->private;
4318 poll_wait(filp, &md_event_waiters, wait);
4320 /* always allow read */
4321 mask = POLLIN | POLLRDNORM;
4323 if (mi->event != atomic_read(&md_event_count))
4324 mask |= POLLERR | POLLPRI;
4328 static struct file_operations md_seq_fops = {
4329 .open = md_seq_open,
4331 .llseek = seq_lseek,
4332 .release = md_seq_release,
4333 .poll = mdstat_poll,
4336 int register_md_personality(struct mdk_personality *p)
4338 spin_lock(&pers_lock);
4339 list_add_tail(&p->list, &pers_list);
4340 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
4341 spin_unlock(&pers_lock);
4345 int unregister_md_personality(struct mdk_personality *p)
4347 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
4348 spin_lock(&pers_lock);
4349 list_del_init(&p->list);
4350 spin_unlock(&pers_lock);
4354 static int is_mddev_idle(mddev_t *mddev)
4357 struct list_head *tmp;
4359 unsigned long curr_events;
4362 ITERATE_RDEV(mddev,rdev,tmp) {
4363 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
4364 curr_events = disk_stat_read(disk, sectors[0]) +
4365 disk_stat_read(disk, sectors[1]) -
4366 atomic_read(&disk->sync_io);
4367 /* The difference between curr_events and last_events
4368 * will be affected by any new non-sync IO (making
4369 * curr_events bigger) and any difference in the amount of
4370 * in-flight syncio (making current_events bigger or smaller)
4371 * The amount in-flight is currently limited to
4372 * 32*64K in raid1/10 and 256*PAGE_SIZE in raid5/6
4373 * which is at most 4096 sectors.
4374 * These numbers are fairly fragile and should be made
4375 * more robust, probably by enforcing the
4376 * 'window size' that md_do_sync sort-of uses.
4378 * Note: the following is an unsigned comparison.
4380 if ((curr_events - rdev->last_events + 4096) > 8192) {
4381 rdev->last_events = curr_events;
4388 void md_done_sync(mddev_t *mddev, int blocks, int ok)
4390 /* another "blocks" (512byte) blocks have been synced */
4391 atomic_sub(blocks, &mddev->recovery_active);
4392 wake_up(&mddev->recovery_wait);
4394 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
4395 md_wakeup_thread(mddev->thread);
4396 // stop recovery, signal do_sync ....
4401 /* md_write_start(mddev, bi)
4402 * If we need to update some array metadata (e.g. 'active' flag
4403 * in superblock) before writing, schedule a superblock update
4404 * and wait for it to complete.
4406 void md_write_start(mddev_t *mddev, struct bio *bi)
4408 if (bio_data_dir(bi) != WRITE)
4411 BUG_ON(mddev->ro == 1);
4412 if (mddev->ro == 2) {
4413 /* need to switch to read/write */
4415 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4416 md_wakeup_thread(mddev->thread);
4418 atomic_inc(&mddev->writes_pending);
4419 if (mddev->in_sync) {
4420 spin_lock_irq(&mddev->write_lock);
4421 if (mddev->in_sync) {
4423 mddev->sb_dirty = 1;
4424 md_wakeup_thread(mddev->thread);
4426 spin_unlock_irq(&mddev->write_lock);
4428 wait_event(mddev->sb_wait, mddev->sb_dirty==0);
4431 void md_write_end(mddev_t *mddev)
4433 if (atomic_dec_and_test(&mddev->writes_pending)) {
4434 if (mddev->safemode == 2)
4435 md_wakeup_thread(mddev->thread);
4437 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
4441 static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
4443 #define SYNC_MARKS 10
4444 #define SYNC_MARK_STEP (3*HZ)
4445 static void md_do_sync(mddev_t *mddev)
4448 unsigned int currspeed = 0,
4450 sector_t max_sectors,j, io_sectors;
4451 unsigned long mark[SYNC_MARKS];
4452 sector_t mark_cnt[SYNC_MARKS];
4454 struct list_head *tmp;
4455 sector_t last_check;
4458 /* just incase thread restarts... */
4459 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
4462 /* we overload curr_resync somewhat here.
4463 * 0 == not engaged in resync at all
4464 * 2 == checking that there is no conflict with another sync
4465 * 1 == like 2, but have yielded to allow conflicting resync to
4467 * other == active in resync - this many blocks
4469 * Before starting a resync we must have set curr_resync to
4470 * 2, and then checked that every "conflicting" array has curr_resync
4471 * less than ours. When we find one that is the same or higher
4472 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
4473 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
4474 * This will mean we have to start checking from the beginning again.
4479 mddev->curr_resync = 2;
4482 if (kthread_should_stop()) {
4483 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4486 ITERATE_MDDEV(mddev2,tmp) {
4487 if (mddev2 == mddev)
4489 if (mddev2->curr_resync &&
4490 match_mddev_units(mddev,mddev2)) {
4492 if (mddev < mddev2 && mddev->curr_resync == 2) {
4493 /* arbitrarily yield */
4494 mddev->curr_resync = 1;
4495 wake_up(&resync_wait);
4497 if (mddev > mddev2 && mddev->curr_resync == 1)
4498 /* no need to wait here, we can wait the next
4499 * time 'round when curr_resync == 2
4502 prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE);
4503 if (!kthread_should_stop() &&
4504 mddev2->curr_resync >= mddev->curr_resync) {
4505 printk(KERN_INFO "md: delaying resync of %s"
4506 " until %s has finished resync (they"
4507 " share one or more physical units)\n",
4508 mdname(mddev), mdname(mddev2));
4511 finish_wait(&resync_wait, &wq);
4514 finish_wait(&resync_wait, &wq);
4517 } while (mddev->curr_resync < 2);
4519 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
4520 /* resync follows the size requested by the personality,
4521 * which defaults to physical size, but can be virtual size
4523 max_sectors = mddev->resync_max_sectors;
4524 mddev->resync_mismatches = 0;
4526 /* recovery follows the physical size of devices */
4527 max_sectors = mddev->size << 1;
4529 printk(KERN_INFO "md: syncing RAID array %s\n", mdname(mddev));
4530 printk(KERN_INFO "md: minimum _guaranteed_ reconstruction speed:"
4531 " %d KB/sec/disc.\n", speed_min(mddev));
4532 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
4533 "(but not more than %d KB/sec) for reconstruction.\n",
4536 is_mddev_idle(mddev); /* this also initializes IO event counters */
4537 /* we don't use the checkpoint if there's a bitmap */
4538 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && !mddev->bitmap
4539 && ! test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
4540 j = mddev->recovery_cp;
4544 for (m = 0; m < SYNC_MARKS; m++) {
4546 mark_cnt[m] = io_sectors;
4549 mddev->resync_mark = mark[last_mark];
4550 mddev->resync_mark_cnt = mark_cnt[last_mark];
4553 * Tune reconstruction:
4555 window = 32*(PAGE_SIZE/512);
4556 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
4557 window/2,(unsigned long long) max_sectors/2);
4559 atomic_set(&mddev->recovery_active, 0);
4560 init_waitqueue_head(&mddev->recovery_wait);
4565 "md: resuming recovery of %s from checkpoint.\n",
4567 mddev->curr_resync = j;
4570 while (j < max_sectors) {
4574 sectors = mddev->pers->sync_request(mddev, j, &skipped,
4575 currspeed < speed_min(mddev));
4577 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
4581 if (!skipped) { /* actual IO requested */
4582 io_sectors += sectors;
4583 atomic_add(sectors, &mddev->recovery_active);
4587 if (j>1) mddev->curr_resync = j;
4588 if (last_check == 0)
4589 /* this is the earliers that rebuilt will be
4590 * visible in /proc/mdstat
4592 md_new_event(mddev);
4594 if (last_check + window > io_sectors || j == max_sectors)
4597 last_check = io_sectors;
4599 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery) ||
4600 test_bit(MD_RECOVERY_ERR, &mddev->recovery))
4604 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
4606 int next = (last_mark+1) % SYNC_MARKS;
4608 mddev->resync_mark = mark[next];
4609 mddev->resync_mark_cnt = mark_cnt[next];
4610 mark[next] = jiffies;
4611 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
4616 if (kthread_should_stop()) {
4618 * got a signal, exit.
4621 "md: md_do_sync() got signal ... exiting\n");
4622 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4627 * this loop exits only if either when we are slower than
4628 * the 'hard' speed limit, or the system was IO-idle for
4630 * the system might be non-idle CPU-wise, but we only care
4631 * about not overloading the IO subsystem. (things like an
4632 * e2fsck being done on the RAID array should execute fast)
4634 mddev->queue->unplug_fn(mddev->queue);
4637 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
4638 /((jiffies-mddev->resync_mark)/HZ +1) +1;
4640 if (currspeed > speed_min(mddev)) {
4641 if ((currspeed > speed_max(mddev)) ||
4642 !is_mddev_idle(mddev)) {
4648 printk(KERN_INFO "md: %s: sync done.\n",mdname(mddev));
4650 * this also signals 'finished resyncing' to md_stop
4653 mddev->queue->unplug_fn(mddev->queue);
4655 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
4657 /* tell personality that we are finished */
4658 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
4660 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
4661 mddev->curr_resync > 2 &&
4662 mddev->curr_resync >= mddev->recovery_cp) {
4663 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4665 "md: checkpointing recovery of %s.\n",
4667 mddev->recovery_cp = mddev->curr_resync;
4669 mddev->recovery_cp = MaxSector;
4673 mddev->curr_resync = 0;
4674 wake_up(&resync_wait);
4675 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
4676 md_wakeup_thread(mddev->thread);
4681 * This routine is regularly called by all per-raid-array threads to
4682 * deal with generic issues like resync and super-block update.
4683 * Raid personalities that don't have a thread (linear/raid0) do not
4684 * need this as they never do any recovery or update the superblock.
4686 * It does not do any resync itself, but rather "forks" off other threads
4687 * to do that as needed.
4688 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
4689 * "->recovery" and create a thread at ->sync_thread.
4690 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
4691 * and wakeups up this thread which will reap the thread and finish up.
4692 * This thread also removes any faulty devices (with nr_pending == 0).
4694 * The overall approach is:
4695 * 1/ if the superblock needs updating, update it.
4696 * 2/ If a recovery thread is running, don't do anything else.
4697 * 3/ If recovery has finished, clean up, possibly marking spares active.
4698 * 4/ If there are any faulty devices, remove them.
4699 * 5/ If array is degraded, try to add spares devices
4700 * 6/ If array has spares or is not in-sync, start a resync thread.
4702 void md_check_recovery(mddev_t *mddev)
4705 struct list_head *rtmp;
4709 bitmap_daemon_work(mddev->bitmap);
4714 if (signal_pending(current)) {
4715 if (mddev->pers->sync_request) {
4716 printk(KERN_INFO "md: %s in immediate safe mode\n",
4718 mddev->safemode = 2;
4720 flush_signals(current);
4725 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
4726 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
4727 (mddev->safemode == 1) ||
4728 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
4729 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
4733 if (mddev_trylock(mddev)==0) {
4736 spin_lock_irq(&mddev->write_lock);
4737 if (mddev->safemode && !atomic_read(&mddev->writes_pending) &&
4738 !mddev->in_sync && mddev->recovery_cp == MaxSector) {
4740 mddev->sb_dirty = 1;
4742 if (mddev->safemode == 1)
4743 mddev->safemode = 0;
4744 spin_unlock_irq(&mddev->write_lock);
4746 if (mddev->sb_dirty)
4747 md_update_sb(mddev);
4750 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4751 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
4752 /* resync/recovery still happening */
4753 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4756 if (mddev->sync_thread) {
4757 /* resync has finished, collect result */
4758 md_unregister_thread(mddev->sync_thread);
4759 mddev->sync_thread = NULL;
4760 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
4761 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4763 /* activate any spares */
4764 mddev->pers->spare_active(mddev);
4766 md_update_sb(mddev);
4768 /* if array is no-longer degraded, then any saved_raid_disk
4769 * information must be scrapped
4771 if (!mddev->degraded)
4772 ITERATE_RDEV(mddev,rdev,rtmp)
4773 rdev->saved_raid_disk = -1;
4775 mddev->recovery = 0;
4776 /* flag recovery needed just to double check */
4777 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4778 md_new_event(mddev);
4781 /* Clear some bits that don't mean anything, but
4784 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4785 clear_bit(MD_RECOVERY_ERR, &mddev->recovery);
4786 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
4787 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
4789 /* no recovery is running.
4790 * remove any failed drives, then
4791 * add spares if possible.
4792 * Spare are also removed and re-added, to allow
4793 * the personality to fail the re-add.
4795 ITERATE_RDEV(mddev,rdev,rtmp)
4796 if (rdev->raid_disk >= 0 &&
4797 (test_bit(Faulty, &rdev->flags) || ! test_bit(In_sync, &rdev->flags)) &&
4798 atomic_read(&rdev->nr_pending)==0) {
4799 if (mddev->pers->hot_remove_disk(mddev, rdev->raid_disk)==0) {
4801 sprintf(nm,"rd%d", rdev->raid_disk);
4802 sysfs_remove_link(&mddev->kobj, nm);
4803 rdev->raid_disk = -1;
4807 if (mddev->degraded) {
4808 ITERATE_RDEV(mddev,rdev,rtmp)
4809 if (rdev->raid_disk < 0
4810 && !test_bit(Faulty, &rdev->flags)) {
4811 if (mddev->pers->hot_add_disk(mddev,rdev)) {
4813 sprintf(nm, "rd%d", rdev->raid_disk);
4814 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
4816 md_new_event(mddev);
4823 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4824 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4825 } else if (mddev->recovery_cp < MaxSector) {
4826 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4827 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
4828 /* nothing to be done ... */
4831 if (mddev->pers->sync_request) {
4832 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4833 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
4834 /* We are adding a device or devices to an array
4835 * which has the bitmap stored on all devices.
4836 * So make sure all bitmap pages get written
4838 bitmap_write_all(mddev->bitmap);
4840 mddev->sync_thread = md_register_thread(md_do_sync,
4843 if (!mddev->sync_thread) {
4844 printk(KERN_ERR "%s: could not start resync"
4847 /* leave the spares where they are, it shouldn't hurt */
4848 mddev->recovery = 0;
4850 md_wakeup_thread(mddev->sync_thread);
4851 md_new_event(mddev);
4854 mddev_unlock(mddev);
4858 static int md_notify_reboot(struct notifier_block *this,
4859 unsigned long code, void *x)
4861 struct list_head *tmp;
4864 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
4866 printk(KERN_INFO "md: stopping all md devices.\n");
4868 ITERATE_MDDEV(mddev,tmp)
4869 if (mddev_trylock(mddev)==0)
4870 do_md_stop (mddev, 1);
4872 * certain more exotic SCSI devices are known to be
4873 * volatile wrt too early system reboots. While the
4874 * right place to handle this issue is the given
4875 * driver, we do want to have a safe RAID driver ...
4882 static struct notifier_block md_notifier = {
4883 .notifier_call = md_notify_reboot,
4885 .priority = INT_MAX, /* before any real devices */
4888 static void md_geninit(void)
4890 struct proc_dir_entry *p;
4892 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
4894 p = create_proc_entry("mdstat", S_IRUGO, NULL);
4896 p->proc_fops = &md_seq_fops;
4899 static int __init md_init(void)
4903 printk(KERN_INFO "md: md driver %d.%d.%d MAX_MD_DEVS=%d,"
4904 " MD_SB_DISKS=%d\n",
4905 MD_MAJOR_VERSION, MD_MINOR_VERSION,
4906 MD_PATCHLEVEL_VERSION, MAX_MD_DEVS, MD_SB_DISKS);
4907 printk(KERN_INFO "md: bitmap version %d.%d\n", BITMAP_MAJOR_HI,
4910 if (register_blkdev(MAJOR_NR, "md"))
4912 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
4913 unregister_blkdev(MAJOR_NR, "md");
4917 blk_register_region(MKDEV(MAJOR_NR, 0), MAX_MD_DEVS, THIS_MODULE,
4918 md_probe, NULL, NULL);
4919 blk_register_region(MKDEV(mdp_major, 0), MAX_MD_DEVS<<MdpMinorShift, THIS_MODULE,
4920 md_probe, NULL, NULL);
4922 for (minor=0; minor < MAX_MD_DEVS; ++minor)
4923 devfs_mk_bdev(MKDEV(MAJOR_NR, minor),
4924 S_IFBLK|S_IRUSR|S_IWUSR,
4927 for (minor=0; minor < MAX_MD_DEVS; ++minor)
4928 devfs_mk_bdev(MKDEV(mdp_major, minor<<MdpMinorShift),
4929 S_IFBLK|S_IRUSR|S_IWUSR,
4933 register_reboot_notifier(&md_notifier);
4934 raid_table_header = register_sysctl_table(raid_root_table, 1);
4944 * Searches all registered partitions for autorun RAID arrays
4947 static dev_t detected_devices[128];
4950 void md_autodetect_dev(dev_t dev)
4952 if (dev_cnt >= 0 && dev_cnt < 127)
4953 detected_devices[dev_cnt++] = dev;
4957 static void autostart_arrays(int part)
4962 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
4964 for (i = 0; i < dev_cnt; i++) {
4965 dev_t dev = detected_devices[i];
4967 rdev = md_import_device(dev,0, 0);
4971 if (test_bit(Faulty, &rdev->flags)) {
4975 list_add(&rdev->same_set, &pending_raid_disks);
4979 autorun_devices(part);
4984 static __exit void md_exit(void)
4987 struct list_head *tmp;
4989 blk_unregister_region(MKDEV(MAJOR_NR,0), MAX_MD_DEVS);
4990 blk_unregister_region(MKDEV(mdp_major,0), MAX_MD_DEVS << MdpMinorShift);
4991 for (i=0; i < MAX_MD_DEVS; i++)
4992 devfs_remove("md/%d", i);
4993 for (i=0; i < MAX_MD_DEVS; i++)
4994 devfs_remove("md/d%d", i);
4998 unregister_blkdev(MAJOR_NR,"md");
4999 unregister_blkdev(mdp_major, "mdp");
5000 unregister_reboot_notifier(&md_notifier);
5001 unregister_sysctl_table(raid_table_header);
5002 remove_proc_entry("mdstat", NULL);
5003 ITERATE_MDDEV(mddev,tmp) {
5004 struct gendisk *disk = mddev->gendisk;
5007 export_array(mddev);
5010 mddev->gendisk = NULL;
5015 module_init(md_init)
5016 module_exit(md_exit)
5018 static int get_ro(char *buffer, struct kernel_param *kp)
5020 return sprintf(buffer, "%d", start_readonly);
5022 static int set_ro(const char *val, struct kernel_param *kp)
5025 int num = simple_strtoul(val, &e, 10);
5026 if (*val && (*e == '\0' || *e == '\n')) {
5027 start_readonly = num;
5033 module_param_call(start_ro, set_ro, get_ro, NULL, 0600);
5034 module_param(start_dirty_degraded, int, 0644);
5037 EXPORT_SYMBOL(register_md_personality);
5038 EXPORT_SYMBOL(unregister_md_personality);
5039 EXPORT_SYMBOL(md_error);
5040 EXPORT_SYMBOL(md_done_sync);
5041 EXPORT_SYMBOL(md_write_start);
5042 EXPORT_SYMBOL(md_write_end);
5043 EXPORT_SYMBOL(md_register_thread);
5044 EXPORT_SYMBOL(md_unregister_thread);
5045 EXPORT_SYMBOL(md_wakeup_thread);
5046 EXPORT_SYMBOL(md_print_devices);
5047 EXPORT_SYMBOL(md_check_recovery);
5048 MODULE_LICENSE("GPL");
5050 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);