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/kernel.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/buffer_head.h> /* for invalidate_bdev */
43 #include <linux/poll.h>
44 #include <linux/mutex.h>
45 #include <linux/ctype.h>
46 #include <linux/freezer.h>
48 #include <linux/init.h>
50 #include <linux/file.h>
53 #include <linux/kmod.h>
56 #include <asm/unaligned.h>
58 #define MAJOR_NR MD_MAJOR
61 /* 63 partitions with the alternate major number (mdp) */
62 #define MdpMinorShift 6
65 #define dprintk(x...) ((void)(DEBUG && printk(x)))
69 static void autostart_arrays (int part);
72 static LIST_HEAD(pers_list);
73 static DEFINE_SPINLOCK(pers_lock);
75 static void md_print_devices(void);
77 static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
79 #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
82 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
83 * is 1000 KB/sec, so the extra system load does not show up that much.
84 * Increase it if you want to have more _guaranteed_ speed. Note that
85 * the RAID driver will use the maximum available bandwidth if the IO
86 * subsystem is idle. There is also an 'absolute maximum' reconstruction
87 * speed limit - in case reconstruction slows down your system despite
90 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
91 * or /sys/block/mdX/md/sync_speed_{min,max}
94 static int sysctl_speed_limit_min = 1000;
95 static int sysctl_speed_limit_max = 200000;
96 static inline int speed_min(mddev_t *mddev)
98 return mddev->sync_speed_min ?
99 mddev->sync_speed_min : sysctl_speed_limit_min;
102 static inline int speed_max(mddev_t *mddev)
104 return mddev->sync_speed_max ?
105 mddev->sync_speed_max : sysctl_speed_limit_max;
108 static struct ctl_table_header *raid_table_header;
110 static ctl_table raid_table[] = {
112 .ctl_name = DEV_RAID_SPEED_LIMIT_MIN,
113 .procname = "speed_limit_min",
114 .data = &sysctl_speed_limit_min,
115 .maxlen = sizeof(int),
116 .mode = S_IRUGO|S_IWUSR,
117 .proc_handler = &proc_dointvec,
120 .ctl_name = DEV_RAID_SPEED_LIMIT_MAX,
121 .procname = "speed_limit_max",
122 .data = &sysctl_speed_limit_max,
123 .maxlen = sizeof(int),
124 .mode = S_IRUGO|S_IWUSR,
125 .proc_handler = &proc_dointvec,
130 static ctl_table raid_dir_table[] = {
132 .ctl_name = DEV_RAID,
135 .mode = S_IRUGO|S_IXUGO,
141 static ctl_table raid_root_table[] = {
147 .child = raid_dir_table,
152 static struct block_device_operations md_fops;
154 static int start_readonly;
157 * We have a system wide 'event count' that is incremented
158 * on any 'interesting' event, and readers of /proc/mdstat
159 * can use 'poll' or 'select' to find out when the event
163 * start array, stop array, error, add device, remove device,
164 * start build, activate spare
166 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
167 static atomic_t md_event_count;
168 void md_new_event(mddev_t *mddev)
170 atomic_inc(&md_event_count);
171 wake_up(&md_event_waiters);
173 EXPORT_SYMBOL_GPL(md_new_event);
175 /* Alternate version that can be called from interrupts
176 * when calling sysfs_notify isn't needed.
178 static void md_new_event_inintr(mddev_t *mddev)
180 atomic_inc(&md_event_count);
181 wake_up(&md_event_waiters);
185 * Enables to iterate over all existing md arrays
186 * all_mddevs_lock protects this list.
188 static LIST_HEAD(all_mddevs);
189 static DEFINE_SPINLOCK(all_mddevs_lock);
193 * iterates through all used mddevs in the system.
194 * We take care to grab the all_mddevs_lock whenever navigating
195 * the list, and to always hold a refcount when unlocked.
196 * Any code which breaks out of this loop while own
197 * a reference to the current mddev and must mddev_put it.
199 #define for_each_mddev(mddev,tmp) \
201 for (({ spin_lock(&all_mddevs_lock); \
202 tmp = all_mddevs.next; \
204 ({ if (tmp != &all_mddevs) \
205 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
206 spin_unlock(&all_mddevs_lock); \
207 if (mddev) mddev_put(mddev); \
208 mddev = list_entry(tmp, mddev_t, all_mddevs); \
209 tmp != &all_mddevs;}); \
210 ({ spin_lock(&all_mddevs_lock); \
215 static int md_fail_request (struct request_queue *q, struct bio *bio)
221 static inline mddev_t *mddev_get(mddev_t *mddev)
223 atomic_inc(&mddev->active);
227 static void mddev_put(mddev_t *mddev)
229 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
231 if (!mddev->raid_disks && list_empty(&mddev->disks)) {
232 list_del(&mddev->all_mddevs);
233 spin_unlock(&all_mddevs_lock);
234 blk_cleanup_queue(mddev->queue);
235 kobject_put(&mddev->kobj);
237 spin_unlock(&all_mddevs_lock);
240 static mddev_t * mddev_find(dev_t unit)
242 mddev_t *mddev, *new = NULL;
245 spin_lock(&all_mddevs_lock);
246 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
247 if (mddev->unit == unit) {
249 spin_unlock(&all_mddevs_lock);
255 list_add(&new->all_mddevs, &all_mddevs);
256 spin_unlock(&all_mddevs_lock);
259 spin_unlock(&all_mddevs_lock);
261 new = kzalloc(sizeof(*new), GFP_KERNEL);
266 if (MAJOR(unit) == MD_MAJOR)
267 new->md_minor = MINOR(unit);
269 new->md_minor = MINOR(unit) >> MdpMinorShift;
271 mutex_init(&new->reconfig_mutex);
272 INIT_LIST_HEAD(&new->disks);
273 INIT_LIST_HEAD(&new->all_mddevs);
274 init_timer(&new->safemode_timer);
275 atomic_set(&new->active, 1);
276 spin_lock_init(&new->write_lock);
277 init_waitqueue_head(&new->sb_wait);
278 init_waitqueue_head(&new->recovery_wait);
279 new->reshape_position = MaxSector;
281 new->resync_max = MaxSector;
282 new->level = LEVEL_NONE;
284 new->queue = blk_alloc_queue(GFP_KERNEL);
289 /* Can be unlocked because the queue is new: no concurrency */
290 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, new->queue);
292 blk_queue_make_request(new->queue, md_fail_request);
297 static inline int mddev_lock(mddev_t * mddev)
299 return mutex_lock_interruptible(&mddev->reconfig_mutex);
302 static inline int mddev_trylock(mddev_t * mddev)
304 return mutex_trylock(&mddev->reconfig_mutex);
307 static inline void mddev_unlock(mddev_t * mddev)
309 mutex_unlock(&mddev->reconfig_mutex);
311 md_wakeup_thread(mddev->thread);
314 static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
317 struct list_head *tmp;
319 rdev_for_each(rdev, tmp, mddev) {
320 if (rdev->desc_nr == nr)
326 static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
328 struct list_head *tmp;
331 rdev_for_each(rdev, tmp, mddev) {
332 if (rdev->bdev->bd_dev == dev)
338 static struct mdk_personality *find_pers(int level, char *clevel)
340 struct mdk_personality *pers;
341 list_for_each_entry(pers, &pers_list, list) {
342 if (level != LEVEL_NONE && pers->level == level)
344 if (strcmp(pers->name, clevel)==0)
350 static inline sector_t calc_dev_sboffset(struct block_device *bdev)
352 sector_t size = bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
353 return MD_NEW_SIZE_BLOCKS(size);
356 static sector_t calc_dev_size(mdk_rdev_t *rdev, unsigned chunk_size)
360 size = rdev->sb_offset;
363 size &= ~((sector_t)chunk_size/1024 - 1);
367 static int alloc_disk_sb(mdk_rdev_t * rdev)
372 rdev->sb_page = alloc_page(GFP_KERNEL);
373 if (!rdev->sb_page) {
374 printk(KERN_ALERT "md: out of memory.\n");
381 static void free_disk_sb(mdk_rdev_t * rdev)
384 put_page(rdev->sb_page);
386 rdev->sb_page = NULL;
393 static void super_written(struct bio *bio, int error)
395 mdk_rdev_t *rdev = bio->bi_private;
396 mddev_t *mddev = rdev->mddev;
398 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
399 printk("md: super_written gets error=%d, uptodate=%d\n",
400 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
401 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
402 md_error(mddev, rdev);
405 if (atomic_dec_and_test(&mddev->pending_writes))
406 wake_up(&mddev->sb_wait);
410 static void super_written_barrier(struct bio *bio, int error)
412 struct bio *bio2 = bio->bi_private;
413 mdk_rdev_t *rdev = bio2->bi_private;
414 mddev_t *mddev = rdev->mddev;
416 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
417 error == -EOPNOTSUPP) {
419 /* barriers don't appear to be supported :-( */
420 set_bit(BarriersNotsupp, &rdev->flags);
421 mddev->barriers_work = 0;
422 spin_lock_irqsave(&mddev->write_lock, flags);
423 bio2->bi_next = mddev->biolist;
424 mddev->biolist = bio2;
425 spin_unlock_irqrestore(&mddev->write_lock, flags);
426 wake_up(&mddev->sb_wait);
430 bio->bi_private = rdev;
431 super_written(bio, error);
435 void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
436 sector_t sector, int size, struct page *page)
438 /* write first size bytes of page to sector of rdev
439 * Increment mddev->pending_writes before returning
440 * and decrement it on completion, waking up sb_wait
441 * if zero is reached.
442 * If an error occurred, call md_error
444 * As we might need to resubmit the request if BIO_RW_BARRIER
445 * causes ENOTSUPP, we allocate a spare bio...
447 struct bio *bio = bio_alloc(GFP_NOIO, 1);
448 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNC);
450 bio->bi_bdev = rdev->bdev;
451 bio->bi_sector = sector;
452 bio_add_page(bio, page, size, 0);
453 bio->bi_private = rdev;
454 bio->bi_end_io = super_written;
457 atomic_inc(&mddev->pending_writes);
458 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
460 rw |= (1<<BIO_RW_BARRIER);
461 rbio = bio_clone(bio, GFP_NOIO);
462 rbio->bi_private = bio;
463 rbio->bi_end_io = super_written_barrier;
464 submit_bio(rw, rbio);
469 void md_super_wait(mddev_t *mddev)
471 /* wait for all superblock writes that were scheduled to complete.
472 * if any had to be retried (due to BARRIER problems), retry them
476 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
477 if (atomic_read(&mddev->pending_writes)==0)
479 while (mddev->biolist) {
481 spin_lock_irq(&mddev->write_lock);
482 bio = mddev->biolist;
483 mddev->biolist = bio->bi_next ;
485 spin_unlock_irq(&mddev->write_lock);
486 submit_bio(bio->bi_rw, bio);
490 finish_wait(&mddev->sb_wait, &wq);
493 static void bi_complete(struct bio *bio, int error)
495 complete((struct completion*)bio->bi_private);
498 int sync_page_io(struct block_device *bdev, sector_t sector, int size,
499 struct page *page, int rw)
501 struct bio *bio = bio_alloc(GFP_NOIO, 1);
502 struct completion event;
505 rw |= (1 << BIO_RW_SYNC);
508 bio->bi_sector = sector;
509 bio_add_page(bio, page, size, 0);
510 init_completion(&event);
511 bio->bi_private = &event;
512 bio->bi_end_io = bi_complete;
514 wait_for_completion(&event);
516 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
520 EXPORT_SYMBOL_GPL(sync_page_io);
522 static int read_disk_sb(mdk_rdev_t * rdev, int size)
524 char b[BDEVNAME_SIZE];
525 if (!rdev->sb_page) {
533 if (!sync_page_io(rdev->bdev, rdev->sb_offset<<1, size, rdev->sb_page, READ))
539 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
540 bdevname(rdev->bdev,b));
544 static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
546 if ( (sb1->set_uuid0 == sb2->set_uuid0) &&
547 (sb1->set_uuid1 == sb2->set_uuid1) &&
548 (sb1->set_uuid2 == sb2->set_uuid2) &&
549 (sb1->set_uuid3 == sb2->set_uuid3))
557 static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
560 mdp_super_t *tmp1, *tmp2;
562 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
563 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
565 if (!tmp1 || !tmp2) {
567 printk(KERN_INFO "md.c: sb1 is not equal to sb2!\n");
575 * nr_disks is not constant
580 if (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4))
592 static u32 md_csum_fold(u32 csum)
594 csum = (csum & 0xffff) + (csum >> 16);
595 return (csum & 0xffff) + (csum >> 16);
598 static unsigned int calc_sb_csum(mdp_super_t * sb)
601 u32 *sb32 = (u32*)sb;
603 unsigned int disk_csum, csum;
605 disk_csum = sb->sb_csum;
608 for (i = 0; i < MD_SB_BYTES/4 ; i++)
610 csum = (newcsum & 0xffffffff) + (newcsum>>32);
614 /* This used to use csum_partial, which was wrong for several
615 * reasons including that different results are returned on
616 * different architectures. It isn't critical that we get exactly
617 * the same return value as before (we always csum_fold before
618 * testing, and that removes any differences). However as we
619 * know that csum_partial always returned a 16bit value on
620 * alphas, do a fold to maximise conformity to previous behaviour.
622 sb->sb_csum = md_csum_fold(disk_csum);
624 sb->sb_csum = disk_csum;
631 * Handle superblock details.
632 * We want to be able to handle multiple superblock formats
633 * so we have a common interface to them all, and an array of
634 * different handlers.
635 * We rely on user-space to write the initial superblock, and support
636 * reading and updating of superblocks.
637 * Interface methods are:
638 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
639 * loads and validates a superblock on dev.
640 * if refdev != NULL, compare superblocks on both devices
642 * 0 - dev has a superblock that is compatible with refdev
643 * 1 - dev has a superblock that is compatible and newer than refdev
644 * so dev should be used as the refdev in future
645 * -EINVAL superblock incompatible or invalid
646 * -othererror e.g. -EIO
648 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
649 * Verify that dev is acceptable into mddev.
650 * The first time, mddev->raid_disks will be 0, and data from
651 * dev should be merged in. Subsequent calls check that dev
652 * is new enough. Return 0 or -EINVAL
654 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
655 * Update the superblock for rdev with data in mddev
656 * This does not write to disc.
662 struct module *owner;
663 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
665 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
666 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
667 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
668 unsigned long long size);
672 * load_super for 0.90.0
674 static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
676 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
682 * Calculate the position of the superblock,
683 * it's at the end of the disk.
685 * It also happens to be a multiple of 4Kb.
687 sb_offset = calc_dev_sboffset(rdev->bdev);
688 rdev->sb_offset = sb_offset;
690 ret = read_disk_sb(rdev, MD_SB_BYTES);
695 bdevname(rdev->bdev, b);
696 sb = (mdp_super_t*)page_address(rdev->sb_page);
698 if (sb->md_magic != MD_SB_MAGIC) {
699 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
704 if (sb->major_version != 0 ||
705 sb->minor_version < 90 ||
706 sb->minor_version > 91) {
707 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
708 sb->major_version, sb->minor_version,
713 if (sb->raid_disks <= 0)
716 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
717 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
722 rdev->preferred_minor = sb->md_minor;
723 rdev->data_offset = 0;
724 rdev->sb_size = MD_SB_BYTES;
726 if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) {
727 if (sb->level != 1 && sb->level != 4
728 && sb->level != 5 && sb->level != 6
729 && sb->level != 10) {
730 /* FIXME use a better test */
732 "md: bitmaps not supported for this level.\n");
737 if (sb->level == LEVEL_MULTIPATH)
740 rdev->desc_nr = sb->this_disk.number;
746 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
747 if (!uuid_equal(refsb, sb)) {
748 printk(KERN_WARNING "md: %s has different UUID to %s\n",
749 b, bdevname(refdev->bdev,b2));
752 if (!sb_equal(refsb, sb)) {
753 printk(KERN_WARNING "md: %s has same UUID"
754 " but different superblock to %s\n",
755 b, bdevname(refdev->bdev, b2));
759 ev2 = md_event(refsb);
765 rdev->size = calc_dev_size(rdev, sb->chunk_size);
767 if (rdev->size < sb->size && sb->level > 1)
768 /* "this cannot possibly happen" ... */
776 * validate_super for 0.90.0
778 static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
781 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
782 __u64 ev1 = md_event(sb);
784 rdev->raid_disk = -1;
785 clear_bit(Faulty, &rdev->flags);
786 clear_bit(In_sync, &rdev->flags);
787 clear_bit(WriteMostly, &rdev->flags);
788 clear_bit(BarriersNotsupp, &rdev->flags);
790 if (mddev->raid_disks == 0) {
791 mddev->major_version = 0;
792 mddev->minor_version = sb->minor_version;
793 mddev->patch_version = sb->patch_version;
795 mddev->chunk_size = sb->chunk_size;
796 mddev->ctime = sb->ctime;
797 mddev->utime = sb->utime;
798 mddev->level = sb->level;
799 mddev->clevel[0] = 0;
800 mddev->layout = sb->layout;
801 mddev->raid_disks = sb->raid_disks;
802 mddev->size = sb->size;
804 mddev->bitmap_offset = 0;
805 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
807 if (mddev->minor_version >= 91) {
808 mddev->reshape_position = sb->reshape_position;
809 mddev->delta_disks = sb->delta_disks;
810 mddev->new_level = sb->new_level;
811 mddev->new_layout = sb->new_layout;
812 mddev->new_chunk = sb->new_chunk;
814 mddev->reshape_position = MaxSector;
815 mddev->delta_disks = 0;
816 mddev->new_level = mddev->level;
817 mddev->new_layout = mddev->layout;
818 mddev->new_chunk = mddev->chunk_size;
821 if (sb->state & (1<<MD_SB_CLEAN))
822 mddev->recovery_cp = MaxSector;
824 if (sb->events_hi == sb->cp_events_hi &&
825 sb->events_lo == sb->cp_events_lo) {
826 mddev->recovery_cp = sb->recovery_cp;
828 mddev->recovery_cp = 0;
831 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
832 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
833 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
834 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
836 mddev->max_disks = MD_SB_DISKS;
838 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
839 mddev->bitmap_file == NULL)
840 mddev->bitmap_offset = mddev->default_bitmap_offset;
842 } else if (mddev->pers == NULL) {
843 /* Insist on good event counter while assembling */
845 if (ev1 < mddev->events)
847 } else if (mddev->bitmap) {
848 /* if adding to array with a bitmap, then we can accept an
849 * older device ... but not too old.
851 if (ev1 < mddev->bitmap->events_cleared)
854 if (ev1 < mddev->events)
855 /* just a hot-add of a new device, leave raid_disk at -1 */
859 if (mddev->level != LEVEL_MULTIPATH) {
860 desc = sb->disks + rdev->desc_nr;
862 if (desc->state & (1<<MD_DISK_FAULTY))
863 set_bit(Faulty, &rdev->flags);
864 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
865 desc->raid_disk < mddev->raid_disks */) {
866 set_bit(In_sync, &rdev->flags);
867 rdev->raid_disk = desc->raid_disk;
869 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
870 set_bit(WriteMostly, &rdev->flags);
871 } else /* MULTIPATH are always insync */
872 set_bit(In_sync, &rdev->flags);
877 * sync_super for 0.90.0
879 static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
882 struct list_head *tmp;
884 int next_spare = mddev->raid_disks;
887 /* make rdev->sb match mddev data..
890 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
891 * 3/ any empty disks < next_spare become removed
893 * disks[0] gets initialised to REMOVED because
894 * we cannot be sure from other fields if it has
895 * been initialised or not.
898 int active=0, working=0,failed=0,spare=0,nr_disks=0;
900 rdev->sb_size = MD_SB_BYTES;
902 sb = (mdp_super_t*)page_address(rdev->sb_page);
904 memset(sb, 0, sizeof(*sb));
906 sb->md_magic = MD_SB_MAGIC;
907 sb->major_version = mddev->major_version;
908 sb->patch_version = mddev->patch_version;
909 sb->gvalid_words = 0; /* ignored */
910 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
911 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
912 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
913 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
915 sb->ctime = mddev->ctime;
916 sb->level = mddev->level;
917 sb->size = mddev->size;
918 sb->raid_disks = mddev->raid_disks;
919 sb->md_minor = mddev->md_minor;
920 sb->not_persistent = 0;
921 sb->utime = mddev->utime;
923 sb->events_hi = (mddev->events>>32);
924 sb->events_lo = (u32)mddev->events;
926 if (mddev->reshape_position == MaxSector)
927 sb->minor_version = 90;
929 sb->minor_version = 91;
930 sb->reshape_position = mddev->reshape_position;
931 sb->new_level = mddev->new_level;
932 sb->delta_disks = mddev->delta_disks;
933 sb->new_layout = mddev->new_layout;
934 sb->new_chunk = mddev->new_chunk;
936 mddev->minor_version = sb->minor_version;
939 sb->recovery_cp = mddev->recovery_cp;
940 sb->cp_events_hi = (mddev->events>>32);
941 sb->cp_events_lo = (u32)mddev->events;
942 if (mddev->recovery_cp == MaxSector)
943 sb->state = (1<< MD_SB_CLEAN);
947 sb->layout = mddev->layout;
948 sb->chunk_size = mddev->chunk_size;
950 if (mddev->bitmap && mddev->bitmap_file == NULL)
951 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
953 sb->disks[0].state = (1<<MD_DISK_REMOVED);
954 rdev_for_each(rdev2, tmp, mddev) {
957 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
958 && !test_bit(Faulty, &rdev2->flags))
959 desc_nr = rdev2->raid_disk;
961 desc_nr = next_spare++;
962 rdev2->desc_nr = desc_nr;
963 d = &sb->disks[rdev2->desc_nr];
965 d->number = rdev2->desc_nr;
966 d->major = MAJOR(rdev2->bdev->bd_dev);
967 d->minor = MINOR(rdev2->bdev->bd_dev);
968 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
969 && !test_bit(Faulty, &rdev2->flags))
970 d->raid_disk = rdev2->raid_disk;
972 d->raid_disk = rdev2->desc_nr; /* compatibility */
973 if (test_bit(Faulty, &rdev2->flags))
974 d->state = (1<<MD_DISK_FAULTY);
975 else if (test_bit(In_sync, &rdev2->flags)) {
976 d->state = (1<<MD_DISK_ACTIVE);
977 d->state |= (1<<MD_DISK_SYNC);
985 if (test_bit(WriteMostly, &rdev2->flags))
986 d->state |= (1<<MD_DISK_WRITEMOSTLY);
988 /* now set the "removed" and "faulty" bits on any missing devices */
989 for (i=0 ; i < mddev->raid_disks ; i++) {
990 mdp_disk_t *d = &sb->disks[i];
991 if (d->state == 0 && d->number == 0) {
994 d->state = (1<<MD_DISK_REMOVED);
995 d->state |= (1<<MD_DISK_FAULTY);
999 sb->nr_disks = nr_disks;
1000 sb->active_disks = active;
1001 sb->working_disks = working;
1002 sb->failed_disks = failed;
1003 sb->spare_disks = spare;
1005 sb->this_disk = sb->disks[rdev->desc_nr];
1006 sb->sb_csum = calc_sb_csum(sb);
1010 * rdev_size_change for 0.90.0
1012 static unsigned long long
1013 super_90_rdev_size_change(mdk_rdev_t *rdev, unsigned long long size)
1015 if (size && size < rdev->mddev->size)
1016 return 0; /* component must fit device */
1017 size *= 2; /* convert to sectors */
1018 if (rdev->mddev->bitmap_offset)
1019 return 0; /* can't move bitmap */
1020 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
1021 if (!size || size > rdev->sb_offset*2)
1022 size = rdev->sb_offset*2;
1023 md_super_write(rdev->mddev, rdev, rdev->sb_offset << 1, rdev->sb_size,
1025 md_super_wait(rdev->mddev);
1026 return size/2; /* kB for sysfs */
1031 * version 1 superblock
1034 static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
1038 unsigned long long newcsum;
1039 int size = 256 + le32_to_cpu(sb->max_dev)*2;
1040 __le32 *isuper = (__le32*)sb;
1043 disk_csum = sb->sb_csum;
1046 for (i=0; size>=4; size -= 4 )
1047 newcsum += le32_to_cpu(*isuper++);
1050 newcsum += le16_to_cpu(*(__le16*) isuper);
1052 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1053 sb->sb_csum = disk_csum;
1054 return cpu_to_le32(csum);
1057 static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1059 struct mdp_superblock_1 *sb;
1062 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1066 * Calculate the position of the superblock.
1067 * It is always aligned to a 4K boundary and
1068 * depeding on minor_version, it can be:
1069 * 0: At least 8K, but less than 12K, from end of device
1070 * 1: At start of device
1071 * 2: 4K from start of device.
1073 switch(minor_version) {
1075 sb_offset = rdev->bdev->bd_inode->i_size >> 9;
1077 sb_offset &= ~(sector_t)(4*2-1);
1078 /* convert from sectors to K */
1090 rdev->sb_offset = sb_offset;
1092 /* superblock is rarely larger than 1K, but it can be larger,
1093 * and it is safe to read 4k, so we do that
1095 ret = read_disk_sb(rdev, 4096);
1096 if (ret) return ret;
1099 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1101 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1102 sb->major_version != cpu_to_le32(1) ||
1103 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1104 le64_to_cpu(sb->super_offset) != (rdev->sb_offset<<1) ||
1105 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
1108 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1109 printk("md: invalid superblock checksum on %s\n",
1110 bdevname(rdev->bdev,b));
1113 if (le64_to_cpu(sb->data_size) < 10) {
1114 printk("md: data_size too small on %s\n",
1115 bdevname(rdev->bdev,b));
1118 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET)) {
1119 if (sb->level != cpu_to_le32(1) &&
1120 sb->level != cpu_to_le32(4) &&
1121 sb->level != cpu_to_le32(5) &&
1122 sb->level != cpu_to_le32(6) &&
1123 sb->level != cpu_to_le32(10)) {
1125 "md: bitmaps not supported for this level.\n");
1130 rdev->preferred_minor = 0xffff;
1131 rdev->data_offset = le64_to_cpu(sb->data_offset);
1132 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
1134 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
1135 bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
1136 if (rdev->sb_size & bmask)
1137 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1140 && rdev->data_offset < sb_offset + (rdev->sb_size/512))
1143 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1146 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1152 struct mdp_superblock_1 *refsb =
1153 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1155 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1156 sb->level != refsb->level ||
1157 sb->layout != refsb->layout ||
1158 sb->chunksize != refsb->chunksize) {
1159 printk(KERN_WARNING "md: %s has strangely different"
1160 " superblock to %s\n",
1161 bdevname(rdev->bdev,b),
1162 bdevname(refdev->bdev,b2));
1165 ev1 = le64_to_cpu(sb->events);
1166 ev2 = le64_to_cpu(refsb->events);
1174 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
1176 rdev->size = rdev->sb_offset;
1177 if (rdev->size < le64_to_cpu(sb->data_size)/2)
1179 rdev->size = le64_to_cpu(sb->data_size)/2;
1180 if (le32_to_cpu(sb->chunksize))
1181 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
1183 if (le64_to_cpu(sb->size) > rdev->size*2)
1188 static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1190 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1191 __u64 ev1 = le64_to_cpu(sb->events);
1193 rdev->raid_disk = -1;
1194 clear_bit(Faulty, &rdev->flags);
1195 clear_bit(In_sync, &rdev->flags);
1196 clear_bit(WriteMostly, &rdev->flags);
1197 clear_bit(BarriersNotsupp, &rdev->flags);
1199 if (mddev->raid_disks == 0) {
1200 mddev->major_version = 1;
1201 mddev->patch_version = 0;
1202 mddev->external = 0;
1203 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
1204 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1205 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1206 mddev->level = le32_to_cpu(sb->level);
1207 mddev->clevel[0] = 0;
1208 mddev->layout = le32_to_cpu(sb->layout);
1209 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1210 mddev->size = le64_to_cpu(sb->size)/2;
1211 mddev->events = ev1;
1212 mddev->bitmap_offset = 0;
1213 mddev->default_bitmap_offset = 1024 >> 9;
1215 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1216 memcpy(mddev->uuid, sb->set_uuid, 16);
1218 mddev->max_disks = (4096-256)/2;
1220 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
1221 mddev->bitmap_file == NULL )
1222 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
1224 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1225 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1226 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1227 mddev->new_level = le32_to_cpu(sb->new_level);
1228 mddev->new_layout = le32_to_cpu(sb->new_layout);
1229 mddev->new_chunk = le32_to_cpu(sb->new_chunk)<<9;
1231 mddev->reshape_position = MaxSector;
1232 mddev->delta_disks = 0;
1233 mddev->new_level = mddev->level;
1234 mddev->new_layout = mddev->layout;
1235 mddev->new_chunk = mddev->chunk_size;
1238 } else if (mddev->pers == NULL) {
1239 /* Insist of good event counter while assembling */
1241 if (ev1 < mddev->events)
1243 } else if (mddev->bitmap) {
1244 /* If adding to array with a bitmap, then we can accept an
1245 * older device, but not too old.
1247 if (ev1 < mddev->bitmap->events_cleared)
1250 if (ev1 < mddev->events)
1251 /* just a hot-add of a new device, leave raid_disk at -1 */
1254 if (mddev->level != LEVEL_MULTIPATH) {
1256 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1258 case 0xffff: /* spare */
1260 case 0xfffe: /* faulty */
1261 set_bit(Faulty, &rdev->flags);
1264 if ((le32_to_cpu(sb->feature_map) &
1265 MD_FEATURE_RECOVERY_OFFSET))
1266 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1268 set_bit(In_sync, &rdev->flags);
1269 rdev->raid_disk = role;
1272 if (sb->devflags & WriteMostly1)
1273 set_bit(WriteMostly, &rdev->flags);
1274 } else /* MULTIPATH are always insync */
1275 set_bit(In_sync, &rdev->flags);
1280 static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1282 struct mdp_superblock_1 *sb;
1283 struct list_head *tmp;
1286 /* make rdev->sb match mddev and rdev data. */
1288 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1290 sb->feature_map = 0;
1292 sb->recovery_offset = cpu_to_le64(0);
1293 memset(sb->pad1, 0, sizeof(sb->pad1));
1294 memset(sb->pad2, 0, sizeof(sb->pad2));
1295 memset(sb->pad3, 0, sizeof(sb->pad3));
1297 sb->utime = cpu_to_le64((__u64)mddev->utime);
1298 sb->events = cpu_to_le64(mddev->events);
1300 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1302 sb->resync_offset = cpu_to_le64(0);
1304 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
1306 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
1307 sb->size = cpu_to_le64(mddev->size<<1);
1309 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1310 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
1311 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
1314 if (rdev->raid_disk >= 0 &&
1315 !test_bit(In_sync, &rdev->flags) &&
1316 rdev->recovery_offset > 0) {
1317 sb->feature_map |= cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1318 sb->recovery_offset = cpu_to_le64(rdev->recovery_offset);
1321 if (mddev->reshape_position != MaxSector) {
1322 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1323 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1324 sb->new_layout = cpu_to_le32(mddev->new_layout);
1325 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1326 sb->new_level = cpu_to_le32(mddev->new_level);
1327 sb->new_chunk = cpu_to_le32(mddev->new_chunk>>9);
1331 rdev_for_each(rdev2, tmp, mddev)
1332 if (rdev2->desc_nr+1 > max_dev)
1333 max_dev = rdev2->desc_nr+1;
1335 if (max_dev > le32_to_cpu(sb->max_dev))
1336 sb->max_dev = cpu_to_le32(max_dev);
1337 for (i=0; i<max_dev;i++)
1338 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1340 rdev_for_each(rdev2, tmp, mddev) {
1342 if (test_bit(Faulty, &rdev2->flags))
1343 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1344 else if (test_bit(In_sync, &rdev2->flags))
1345 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1346 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1347 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1349 sb->dev_roles[i] = cpu_to_le16(0xffff);
1352 sb->sb_csum = calc_sb_1_csum(sb);
1355 static unsigned long long
1356 super_1_rdev_size_change(mdk_rdev_t *rdev, unsigned long long size)
1358 struct mdp_superblock_1 *sb;
1359 unsigned long long max_size;
1360 if (size && size < rdev->mddev->size)
1361 return 0; /* component must fit device */
1362 size *= 2; /* convert to sectors */
1363 if (rdev->sb_offset < rdev->data_offset/2) {
1364 /* minor versions 1 and 2; superblock before data */
1365 max_size = (rdev->bdev->bd_inode->i_size >> 9);
1366 max_size -= rdev->data_offset;
1367 if (!size || size > max_size)
1369 } else if (rdev->mddev->bitmap_offset) {
1370 /* minor version 0 with bitmap we can't move */
1373 /* minor version 0; superblock after data */
1375 sb_offset = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1376 sb_offset &= ~(sector_t)(4*2 - 1);
1377 max_size = rdev->size*2 + sb_offset - rdev->sb_offset*2;
1378 if (!size || size > max_size)
1380 rdev->sb_offset = sb_offset/2;
1382 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
1383 sb->data_size = cpu_to_le64(size);
1384 sb->super_offset = rdev->sb_offset*2;
1385 sb->sb_csum = calc_sb_1_csum(sb);
1386 md_super_write(rdev->mddev, rdev, rdev->sb_offset << 1, rdev->sb_size,
1388 md_super_wait(rdev->mddev);
1389 return size/2; /* kB for sysfs */
1392 static struct super_type super_types[] = {
1395 .owner = THIS_MODULE,
1396 .load_super = super_90_load,
1397 .validate_super = super_90_validate,
1398 .sync_super = super_90_sync,
1399 .rdev_size_change = super_90_rdev_size_change,
1403 .owner = THIS_MODULE,
1404 .load_super = super_1_load,
1405 .validate_super = super_1_validate,
1406 .sync_super = super_1_sync,
1407 .rdev_size_change = super_1_rdev_size_change,
1411 static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1413 struct list_head *tmp, *tmp2;
1414 mdk_rdev_t *rdev, *rdev2;
1416 rdev_for_each(rdev, tmp, mddev1)
1417 rdev_for_each(rdev2, tmp2, mddev2)
1418 if (rdev->bdev->bd_contains ==
1419 rdev2->bdev->bd_contains)
1425 static LIST_HEAD(pending_raid_disks);
1427 static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1429 char b[BDEVNAME_SIZE];
1439 /* prevent duplicates */
1440 if (find_rdev(mddev, rdev->bdev->bd_dev))
1443 /* make sure rdev->size exceeds mddev->size */
1444 if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) {
1446 /* Cannot change size, so fail
1447 * If mddev->level <= 0, then we don't care
1448 * about aligning sizes (e.g. linear)
1450 if (mddev->level > 0)
1453 mddev->size = rdev->size;
1456 /* Verify rdev->desc_nr is unique.
1457 * If it is -1, assign a free number, else
1458 * check number is not in use
1460 if (rdev->desc_nr < 0) {
1462 if (mddev->pers) choice = mddev->raid_disks;
1463 while (find_rdev_nr(mddev, choice))
1465 rdev->desc_nr = choice;
1467 if (find_rdev_nr(mddev, rdev->desc_nr))
1470 bdevname(rdev->bdev,b);
1471 while ( (s=strchr(b, '/')) != NULL)
1474 rdev->mddev = mddev;
1475 printk(KERN_INFO "md: bind<%s>\n", b);
1477 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
1480 if (rdev->bdev->bd_part)
1481 ko = &rdev->bdev->bd_part->dev.kobj;
1483 ko = &rdev->bdev->bd_disk->dev.kobj;
1484 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1485 kobject_del(&rdev->kobj);
1488 list_add(&rdev->same_set, &mddev->disks);
1489 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
1493 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1498 static void md_delayed_delete(struct work_struct *ws)
1500 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1501 kobject_del(&rdev->kobj);
1502 kobject_put(&rdev->kobj);
1505 static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1507 char b[BDEVNAME_SIZE];
1512 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
1513 list_del_init(&rdev->same_set);
1514 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1516 sysfs_remove_link(&rdev->kobj, "block");
1518 /* We need to delay this, otherwise we can deadlock when
1519 * writing to 'remove' to "dev/state"
1521 INIT_WORK(&rdev->del_work, md_delayed_delete);
1522 kobject_get(&rdev->kobj);
1523 schedule_work(&rdev->del_work);
1527 * prevent the device from being mounted, repartitioned or
1528 * otherwise reused by a RAID array (or any other kernel
1529 * subsystem), by bd_claiming the device.
1531 static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
1534 struct block_device *bdev;
1535 char b[BDEVNAME_SIZE];
1537 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
1539 printk(KERN_ERR "md: could not open %s.\n",
1540 __bdevname(dev, b));
1541 return PTR_ERR(bdev);
1543 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
1545 printk(KERN_ERR "md: could not bd_claim %s.\n",
1551 set_bit(AllReserved, &rdev->flags);
1556 static void unlock_rdev(mdk_rdev_t *rdev)
1558 struct block_device *bdev = rdev->bdev;
1566 void md_autodetect_dev(dev_t dev);
1568 static void export_rdev(mdk_rdev_t * rdev)
1570 char b[BDEVNAME_SIZE];
1571 printk(KERN_INFO "md: export_rdev(%s)\n",
1572 bdevname(rdev->bdev,b));
1576 list_del_init(&rdev->same_set);
1578 if (test_bit(AutoDetected, &rdev->flags))
1579 md_autodetect_dev(rdev->bdev->bd_dev);
1582 kobject_put(&rdev->kobj);
1585 static void kick_rdev_from_array(mdk_rdev_t * rdev)
1587 unbind_rdev_from_array(rdev);
1591 static void export_array(mddev_t *mddev)
1593 struct list_head *tmp;
1596 rdev_for_each(rdev, tmp, mddev) {
1601 kick_rdev_from_array(rdev);
1603 if (!list_empty(&mddev->disks))
1605 mddev->raid_disks = 0;
1606 mddev->major_version = 0;
1609 static void print_desc(mdp_disk_t *desc)
1611 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1612 desc->major,desc->minor,desc->raid_disk,desc->state);
1615 static void print_sb(mdp_super_t *sb)
1620 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1621 sb->major_version, sb->minor_version, sb->patch_version,
1622 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1624 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1625 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1626 sb->md_minor, sb->layout, sb->chunk_size);
1627 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1628 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1629 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1630 sb->failed_disks, sb->spare_disks,
1631 sb->sb_csum, (unsigned long)sb->events_lo);
1634 for (i = 0; i < MD_SB_DISKS; i++) {
1637 desc = sb->disks + i;
1638 if (desc->number || desc->major || desc->minor ||
1639 desc->raid_disk || (desc->state && (desc->state != 4))) {
1640 printk(" D %2d: ", i);
1644 printk(KERN_INFO "md: THIS: ");
1645 print_desc(&sb->this_disk);
1649 static void print_rdev(mdk_rdev_t *rdev)
1651 char b[BDEVNAME_SIZE];
1652 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1653 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
1654 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1656 if (rdev->sb_loaded) {
1657 printk(KERN_INFO "md: rdev superblock:\n");
1658 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1660 printk(KERN_INFO "md: no rdev superblock!\n");
1663 static void md_print_devices(void)
1665 struct list_head *tmp, *tmp2;
1668 char b[BDEVNAME_SIZE];
1671 printk("md: **********************************\n");
1672 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1673 printk("md: **********************************\n");
1674 for_each_mddev(mddev, tmp) {
1677 bitmap_print_sb(mddev->bitmap);
1679 printk("%s: ", mdname(mddev));
1680 rdev_for_each(rdev, tmp2, mddev)
1681 printk("<%s>", bdevname(rdev->bdev,b));
1684 rdev_for_each(rdev, tmp2, mddev)
1687 printk("md: **********************************\n");
1692 static void sync_sbs(mddev_t * mddev, int nospares)
1694 /* Update each superblock (in-memory image), but
1695 * if we are allowed to, skip spares which already
1696 * have the right event counter, or have one earlier
1697 * (which would mean they aren't being marked as dirty
1698 * with the rest of the array)
1701 struct list_head *tmp;
1703 rdev_for_each(rdev, tmp, mddev) {
1704 if (rdev->sb_events == mddev->events ||
1706 rdev->raid_disk < 0 &&
1707 (rdev->sb_events&1)==0 &&
1708 rdev->sb_events+1 == mddev->events)) {
1709 /* Don't update this superblock */
1710 rdev->sb_loaded = 2;
1712 super_types[mddev->major_version].
1713 sync_super(mddev, rdev);
1714 rdev->sb_loaded = 1;
1719 static void md_update_sb(mddev_t * mddev, int force_change)
1721 struct list_head *tmp;
1726 if (mddev->external)
1729 spin_lock_irq(&mddev->write_lock);
1731 set_bit(MD_CHANGE_PENDING, &mddev->flags);
1732 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
1734 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
1735 /* just a clean<-> dirty transition, possibly leave spares alone,
1736 * though if events isn't the right even/odd, we will have to do
1742 if (mddev->degraded)
1743 /* If the array is degraded, then skipping spares is both
1744 * dangerous and fairly pointless.
1745 * Dangerous because a device that was removed from the array
1746 * might have a event_count that still looks up-to-date,
1747 * so it can be re-added without a resync.
1748 * Pointless because if there are any spares to skip,
1749 * then a recovery will happen and soon that array won't
1750 * be degraded any more and the spare can go back to sleep then.
1754 sync_req = mddev->in_sync;
1755 mddev->utime = get_seconds();
1757 /* If this is just a dirty<->clean transition, and the array is clean
1758 * and 'events' is odd, we can roll back to the previous clean state */
1760 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
1761 && (mddev->events & 1)
1762 && mddev->events != 1)
1765 /* otherwise we have to go forward and ... */
1767 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
1768 /* .. if the array isn't clean, insist on an odd 'events' */
1769 if ((mddev->events&1)==0) {
1774 /* otherwise insist on an even 'events' (for clean states) */
1775 if ((mddev->events&1)) {
1782 if (!mddev->events) {
1784 * oops, this 64-bit counter should never wrap.
1785 * Either we are in around ~1 trillion A.C., assuming
1786 * 1 reboot per second, or we have a bug:
1793 * do not write anything to disk if using
1794 * nonpersistent superblocks
1796 if (!mddev->persistent) {
1797 if (!mddev->external)
1798 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
1800 spin_unlock_irq(&mddev->write_lock);
1801 wake_up(&mddev->sb_wait);
1804 sync_sbs(mddev, nospares);
1805 spin_unlock_irq(&mddev->write_lock);
1808 "md: updating %s RAID superblock on device (in sync %d)\n",
1809 mdname(mddev),mddev->in_sync);
1811 bitmap_update_sb(mddev->bitmap);
1812 rdev_for_each(rdev, tmp, mddev) {
1813 char b[BDEVNAME_SIZE];
1814 dprintk(KERN_INFO "md: ");
1815 if (rdev->sb_loaded != 1)
1816 continue; /* no noise on spare devices */
1817 if (test_bit(Faulty, &rdev->flags))
1818 dprintk("(skipping faulty ");
1820 dprintk("%s ", bdevname(rdev->bdev,b));
1821 if (!test_bit(Faulty, &rdev->flags)) {
1822 md_super_write(mddev,rdev,
1823 rdev->sb_offset<<1, rdev->sb_size,
1825 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1826 bdevname(rdev->bdev,b),
1827 (unsigned long long)rdev->sb_offset);
1828 rdev->sb_events = mddev->events;
1832 if (mddev->level == LEVEL_MULTIPATH)
1833 /* only need to write one superblock... */
1836 md_super_wait(mddev);
1837 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
1839 spin_lock_irq(&mddev->write_lock);
1840 if (mddev->in_sync != sync_req ||
1841 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
1842 /* have to write it out again */
1843 spin_unlock_irq(&mddev->write_lock);
1846 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
1847 spin_unlock_irq(&mddev->write_lock);
1848 wake_up(&mddev->sb_wait);
1852 /* words written to sysfs files may, or my not, be \n terminated.
1853 * We want to accept with case. For this we use cmd_match.
1855 static int cmd_match(const char *cmd, const char *str)
1857 /* See if cmd, written into a sysfs file, matches
1858 * str. They must either be the same, or cmd can
1859 * have a trailing newline
1861 while (*cmd && *str && *cmd == *str) {
1872 struct rdev_sysfs_entry {
1873 struct attribute attr;
1874 ssize_t (*show)(mdk_rdev_t *, char *);
1875 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1879 state_show(mdk_rdev_t *rdev, char *page)
1884 if (test_bit(Faulty, &rdev->flags)) {
1885 len+= sprintf(page+len, "%sfaulty",sep);
1888 if (test_bit(In_sync, &rdev->flags)) {
1889 len += sprintf(page+len, "%sin_sync",sep);
1892 if (test_bit(WriteMostly, &rdev->flags)) {
1893 len += sprintf(page+len, "%swrite_mostly",sep);
1896 if (test_bit(Blocked, &rdev->flags)) {
1897 len += sprintf(page+len, "%sblocked", sep);
1900 if (!test_bit(Faulty, &rdev->flags) &&
1901 !test_bit(In_sync, &rdev->flags)) {
1902 len += sprintf(page+len, "%sspare", sep);
1905 return len+sprintf(page+len, "\n");
1909 state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1912 * faulty - simulates and error
1913 * remove - disconnects the device
1914 * writemostly - sets write_mostly
1915 * -writemostly - clears write_mostly
1916 * blocked - sets the Blocked flag
1917 * -blocked - clears the Blocked flag
1920 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
1921 md_error(rdev->mddev, rdev);
1923 } else if (cmd_match(buf, "remove")) {
1924 if (rdev->raid_disk >= 0)
1927 mddev_t *mddev = rdev->mddev;
1928 kick_rdev_from_array(rdev);
1930 md_update_sb(mddev, 1);
1931 md_new_event(mddev);
1934 } else if (cmd_match(buf, "writemostly")) {
1935 set_bit(WriteMostly, &rdev->flags);
1937 } else if (cmd_match(buf, "-writemostly")) {
1938 clear_bit(WriteMostly, &rdev->flags);
1940 } else if (cmd_match(buf, "blocked")) {
1941 set_bit(Blocked, &rdev->flags);
1943 } else if (cmd_match(buf, "-blocked")) {
1944 clear_bit(Blocked, &rdev->flags);
1945 wake_up(&rdev->blocked_wait);
1946 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
1947 md_wakeup_thread(rdev->mddev->thread);
1952 sysfs_notify(&rdev->kobj, NULL, "state");
1953 return err ? err : len;
1955 static struct rdev_sysfs_entry rdev_state =
1956 __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
1959 errors_show(mdk_rdev_t *rdev, char *page)
1961 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
1965 errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1968 unsigned long n = simple_strtoul(buf, &e, 10);
1969 if (*buf && (*e == 0 || *e == '\n')) {
1970 atomic_set(&rdev->corrected_errors, n);
1975 static struct rdev_sysfs_entry rdev_errors =
1976 __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
1979 slot_show(mdk_rdev_t *rdev, char *page)
1981 if (rdev->raid_disk < 0)
1982 return sprintf(page, "none\n");
1984 return sprintf(page, "%d\n", rdev->raid_disk);
1988 slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1993 int slot = simple_strtoul(buf, &e, 10);
1994 if (strncmp(buf, "none", 4)==0)
1996 else if (e==buf || (*e && *e!= '\n'))
1998 if (rdev->mddev->pers && slot == -1) {
1999 /* Setting 'slot' on an active array requires also
2000 * updating the 'rd%d' link, and communicating
2001 * with the personality with ->hot_*_disk.
2002 * For now we only support removing
2003 * failed/spare devices. This normally happens automatically,
2004 * but not when the metadata is externally managed.
2006 if (rdev->raid_disk == -1)
2008 /* personality does all needed checks */
2009 if (rdev->mddev->pers->hot_add_disk == NULL)
2011 err = rdev->mddev->pers->
2012 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2015 sprintf(nm, "rd%d", rdev->raid_disk);
2016 sysfs_remove_link(&rdev->mddev->kobj, nm);
2017 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2018 md_wakeup_thread(rdev->mddev->thread);
2019 } else if (rdev->mddev->pers) {
2021 struct list_head *tmp;
2022 /* Activating a spare .. or possibly reactivating
2023 * if we every get bitmaps working here.
2026 if (rdev->raid_disk != -1)
2029 if (rdev->mddev->pers->hot_add_disk == NULL)
2032 rdev_for_each(rdev2, tmp, rdev->mddev)
2033 if (rdev2->raid_disk == slot)
2036 rdev->raid_disk = slot;
2037 if (test_bit(In_sync, &rdev->flags))
2038 rdev->saved_raid_disk = slot;
2040 rdev->saved_raid_disk = -1;
2041 err = rdev->mddev->pers->
2042 hot_add_disk(rdev->mddev, rdev);
2044 rdev->raid_disk = -1;
2047 sysfs_notify(&rdev->kobj, NULL, "state");
2048 sprintf(nm, "rd%d", rdev->raid_disk);
2049 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2051 "md: cannot register "
2053 nm, mdname(rdev->mddev));
2055 /* don't wakeup anyone, leave that to userspace. */
2057 if (slot >= rdev->mddev->raid_disks)
2059 rdev->raid_disk = slot;
2060 /* assume it is working */
2061 clear_bit(Faulty, &rdev->flags);
2062 clear_bit(WriteMostly, &rdev->flags);
2063 set_bit(In_sync, &rdev->flags);
2064 sysfs_notify(&rdev->kobj, NULL, "state");
2070 static struct rdev_sysfs_entry rdev_slot =
2071 __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
2074 offset_show(mdk_rdev_t *rdev, char *page)
2076 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
2080 offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2083 unsigned long long offset = simple_strtoull(buf, &e, 10);
2084 if (e==buf || (*e && *e != '\n'))
2086 if (rdev->mddev->pers && rdev->raid_disk >= 0)
2088 if (rdev->size && rdev->mddev->external)
2089 /* Must set offset before size, so overlap checks
2092 rdev->data_offset = offset;
2096 static struct rdev_sysfs_entry rdev_offset =
2097 __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
2100 rdev_size_show(mdk_rdev_t *rdev, char *page)
2102 return sprintf(page, "%llu\n", (unsigned long long)rdev->size);
2105 static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2107 /* check if two start/length pairs overlap */
2116 rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2119 unsigned long long size = simple_strtoull(buf, &e, 10);
2120 unsigned long long oldsize = rdev->size;
2121 mddev_t *my_mddev = rdev->mddev;
2123 if (e==buf || (*e && *e != '\n'))
2125 if (my_mddev->pers && rdev->raid_disk >= 0) {
2126 if (rdev->mddev->persistent) {
2127 size = super_types[rdev->mddev->major_version].
2128 rdev_size_change(rdev, size);
2132 size = (rdev->bdev->bd_inode->i_size >> 10);
2133 size -= rdev->data_offset/2;
2135 if (size < rdev->mddev->size)
2136 return -EINVAL; /* component must fit device */
2140 if (size > oldsize && rdev->mddev->external) {
2141 /* need to check that all other rdevs with the same ->bdev
2142 * do not overlap. We need to unlock the mddev to avoid
2143 * a deadlock. We have already changed rdev->size, and if
2144 * we have to change it back, we will have the lock again.
2148 struct list_head *tmp, *tmp2;
2150 mddev_unlock(my_mddev);
2151 for_each_mddev(mddev, tmp) {
2155 rdev_for_each(rdev2, tmp2, mddev)
2156 if (test_bit(AllReserved, &rdev2->flags) ||
2157 (rdev->bdev == rdev2->bdev &&
2159 overlaps(rdev->data_offset, rdev->size,
2160 rdev2->data_offset, rdev2->size))) {
2164 mddev_unlock(mddev);
2170 mddev_lock(my_mddev);
2172 /* Someone else could have slipped in a size
2173 * change here, but doing so is just silly.
2174 * We put oldsize back because we *know* it is
2175 * safe, and trust userspace not to race with
2178 rdev->size = oldsize;
2182 if (size < my_mddev->size || my_mddev->size == 0)
2183 my_mddev->size = size;
2187 static struct rdev_sysfs_entry rdev_size =
2188 __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
2190 static struct attribute *rdev_default_attrs[] = {
2199 rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2201 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2202 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
2203 mddev_t *mddev = rdev->mddev;
2209 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2211 if (rdev->mddev == NULL)
2214 rv = entry->show(rdev, page);
2215 mddev_unlock(mddev);
2221 rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2222 const char *page, size_t length)
2224 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2225 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
2227 mddev_t *mddev = rdev->mddev;
2231 if (!capable(CAP_SYS_ADMIN))
2233 rv = mddev ? mddev_lock(mddev): -EBUSY;
2235 if (rdev->mddev == NULL)
2238 rv = entry->store(rdev, page, length);
2239 mddev_unlock(mddev);
2244 static void rdev_free(struct kobject *ko)
2246 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2249 static struct sysfs_ops rdev_sysfs_ops = {
2250 .show = rdev_attr_show,
2251 .store = rdev_attr_store,
2253 static struct kobj_type rdev_ktype = {
2254 .release = rdev_free,
2255 .sysfs_ops = &rdev_sysfs_ops,
2256 .default_attrs = rdev_default_attrs,
2260 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2262 * mark the device faulty if:
2264 * - the device is nonexistent (zero size)
2265 * - the device has no valid superblock
2267 * a faulty rdev _never_ has rdev->sb set.
2269 static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2271 char b[BDEVNAME_SIZE];
2276 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
2278 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2279 return ERR_PTR(-ENOMEM);
2282 if ((err = alloc_disk_sb(rdev)))
2285 err = lock_rdev(rdev, newdev, super_format == -2);
2289 kobject_init(&rdev->kobj, &rdev_ktype);
2292 rdev->saved_raid_disk = -1;
2293 rdev->raid_disk = -1;
2295 rdev->data_offset = 0;
2296 rdev->sb_events = 0;
2297 atomic_set(&rdev->nr_pending, 0);
2298 atomic_set(&rdev->read_errors, 0);
2299 atomic_set(&rdev->corrected_errors, 0);
2301 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2304 "md: %s has zero or unknown size, marking faulty!\n",
2305 bdevname(rdev->bdev,b));
2310 if (super_format >= 0) {
2311 err = super_types[super_format].
2312 load_super(rdev, NULL, super_minor);
2313 if (err == -EINVAL) {
2315 "md: %s does not have a valid v%d.%d "
2316 "superblock, not importing!\n",
2317 bdevname(rdev->bdev,b),
2318 super_format, super_minor);
2323 "md: could not read %s's sb, not importing!\n",
2324 bdevname(rdev->bdev,b));
2329 INIT_LIST_HEAD(&rdev->same_set);
2330 init_waitqueue_head(&rdev->blocked_wait);
2335 if (rdev->sb_page) {
2341 return ERR_PTR(err);
2345 * Check a full RAID array for plausibility
2349 static void analyze_sbs(mddev_t * mddev)
2352 struct list_head *tmp;
2353 mdk_rdev_t *rdev, *freshest;
2354 char b[BDEVNAME_SIZE];
2357 rdev_for_each(rdev, tmp, mddev)
2358 switch (super_types[mddev->major_version].
2359 load_super(rdev, freshest, mddev->minor_version)) {
2367 "md: fatal superblock inconsistency in %s"
2368 " -- removing from array\n",
2369 bdevname(rdev->bdev,b));
2370 kick_rdev_from_array(rdev);
2374 super_types[mddev->major_version].
2375 validate_super(mddev, freshest);
2378 rdev_for_each(rdev, tmp, mddev) {
2379 if (rdev != freshest)
2380 if (super_types[mddev->major_version].
2381 validate_super(mddev, rdev)) {
2382 printk(KERN_WARNING "md: kicking non-fresh %s"
2384 bdevname(rdev->bdev,b));
2385 kick_rdev_from_array(rdev);
2388 if (mddev->level == LEVEL_MULTIPATH) {
2389 rdev->desc_nr = i++;
2390 rdev->raid_disk = rdev->desc_nr;
2391 set_bit(In_sync, &rdev->flags);
2392 } else if (rdev->raid_disk >= mddev->raid_disks) {
2393 rdev->raid_disk = -1;
2394 clear_bit(In_sync, &rdev->flags);
2400 if (mddev->recovery_cp != MaxSector &&
2402 printk(KERN_ERR "md: %s: raid array is not clean"
2403 " -- starting background reconstruction\n",
2409 safe_delay_show(mddev_t *mddev, char *page)
2411 int msec = (mddev->safemode_delay*1000)/HZ;
2412 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2415 safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2423 /* remove a period, and count digits after it */
2424 if (len >= sizeof(buf))
2426 strlcpy(buf, cbuf, len);
2428 for (i=0; i<len; i++) {
2430 if (isdigit(buf[i])) {
2435 } else if (buf[i] == '.') {
2440 msec = simple_strtoul(buf, &e, 10);
2441 if (e == buf || (*e && *e != '\n'))
2443 msec = (msec * 1000) / scale;
2445 mddev->safemode_delay = 0;
2447 mddev->safemode_delay = (msec*HZ)/1000;
2448 if (mddev->safemode_delay == 0)
2449 mddev->safemode_delay = 1;
2453 static struct md_sysfs_entry md_safe_delay =
2454 __ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
2457 level_show(mddev_t *mddev, char *page)
2459 struct mdk_personality *p = mddev->pers;
2461 return sprintf(page, "%s\n", p->name);
2462 else if (mddev->clevel[0])
2463 return sprintf(page, "%s\n", mddev->clevel);
2464 else if (mddev->level != LEVEL_NONE)
2465 return sprintf(page, "%d\n", mddev->level);
2471 level_store(mddev_t *mddev, const char *buf, size_t len)
2478 if (len >= sizeof(mddev->clevel))
2480 strncpy(mddev->clevel, buf, len);
2481 if (mddev->clevel[len-1] == '\n')
2483 mddev->clevel[len] = 0;
2484 mddev->level = LEVEL_NONE;
2488 static struct md_sysfs_entry md_level =
2489 __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
2493 layout_show(mddev_t *mddev, char *page)
2495 /* just a number, not meaningful for all levels */
2496 if (mddev->reshape_position != MaxSector &&
2497 mddev->layout != mddev->new_layout)
2498 return sprintf(page, "%d (%d)\n",
2499 mddev->new_layout, mddev->layout);
2500 return sprintf(page, "%d\n", mddev->layout);
2504 layout_store(mddev_t *mddev, const char *buf, size_t len)
2507 unsigned long n = simple_strtoul(buf, &e, 10);
2509 if (!*buf || (*e && *e != '\n'))
2514 if (mddev->reshape_position != MaxSector)
2515 mddev->new_layout = n;
2520 static struct md_sysfs_entry md_layout =
2521 __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
2525 raid_disks_show(mddev_t *mddev, char *page)
2527 if (mddev->raid_disks == 0)
2529 if (mddev->reshape_position != MaxSector &&
2530 mddev->delta_disks != 0)
2531 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
2532 mddev->raid_disks - mddev->delta_disks);
2533 return sprintf(page, "%d\n", mddev->raid_disks);
2536 static int update_raid_disks(mddev_t *mddev, int raid_disks);
2539 raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2543 unsigned long n = simple_strtoul(buf, &e, 10);
2545 if (!*buf || (*e && *e != '\n'))
2549 rv = update_raid_disks(mddev, n);
2550 else if (mddev->reshape_position != MaxSector) {
2551 int olddisks = mddev->raid_disks - mddev->delta_disks;
2552 mddev->delta_disks = n - olddisks;
2553 mddev->raid_disks = n;
2555 mddev->raid_disks = n;
2556 return rv ? rv : len;
2558 static struct md_sysfs_entry md_raid_disks =
2559 __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
2562 chunk_size_show(mddev_t *mddev, char *page)
2564 if (mddev->reshape_position != MaxSector &&
2565 mddev->chunk_size != mddev->new_chunk)
2566 return sprintf(page, "%d (%d)\n", mddev->new_chunk,
2568 return sprintf(page, "%d\n", mddev->chunk_size);
2572 chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2574 /* can only set chunk_size if array is not yet active */
2576 unsigned long n = simple_strtoul(buf, &e, 10);
2578 if (!*buf || (*e && *e != '\n'))
2583 else if (mddev->reshape_position != MaxSector)
2584 mddev->new_chunk = n;
2586 mddev->chunk_size = n;
2589 static struct md_sysfs_entry md_chunk_size =
2590 __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
2593 resync_start_show(mddev_t *mddev, char *page)
2595 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2599 resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2602 unsigned long long n = simple_strtoull(buf, &e, 10);
2606 if (!*buf || (*e && *e != '\n'))
2609 mddev->recovery_cp = n;
2612 static struct md_sysfs_entry md_resync_start =
2613 __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
2616 * The array state can be:
2619 * No devices, no size, no level
2620 * Equivalent to STOP_ARRAY ioctl
2622 * May have some settings, but array is not active
2623 * all IO results in error
2624 * When written, doesn't tear down array, but just stops it
2625 * suspended (not supported yet)
2626 * All IO requests will block. The array can be reconfigured.
2627 * Writing this, if accepted, will block until array is quiessent
2629 * no resync can happen. no superblocks get written.
2630 * write requests fail
2632 * like readonly, but behaves like 'clean' on a write request.
2634 * clean - no pending writes, but otherwise active.
2635 * When written to inactive array, starts without resync
2636 * If a write request arrives then
2637 * if metadata is known, mark 'dirty' and switch to 'active'.
2638 * if not known, block and switch to write-pending
2639 * If written to an active array that has pending writes, then fails.
2641 * fully active: IO and resync can be happening.
2642 * When written to inactive array, starts with resync
2645 * clean, but writes are blocked waiting for 'active' to be written.
2648 * like active, but no writes have been seen for a while (100msec).
2651 enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
2652 write_pending, active_idle, bad_word};
2653 static char *array_states[] = {
2654 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2655 "write-pending", "active-idle", NULL };
2657 static int match_word(const char *word, char **list)
2660 for (n=0; list[n]; n++)
2661 if (cmd_match(word, list[n]))
2667 array_state_show(mddev_t *mddev, char *page)
2669 enum array_state st = inactive;
2682 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
2684 else if (mddev->safemode)
2690 if (list_empty(&mddev->disks) &&
2691 mddev->raid_disks == 0 &&
2697 return sprintf(page, "%s\n", array_states[st]);
2700 static int do_md_stop(mddev_t * mddev, int ro);
2701 static int do_md_run(mddev_t * mddev);
2702 static int restart_array(mddev_t *mddev);
2705 array_state_store(mddev_t *mddev, const char *buf, size_t len)
2708 enum array_state st = match_word(buf, array_states);
2713 /* stopping an active array */
2714 if (atomic_read(&mddev->active) > 1)
2716 err = do_md_stop(mddev, 0);
2719 /* stopping an active array */
2721 if (atomic_read(&mddev->active) > 1)
2723 err = do_md_stop(mddev, 2);
2725 err = 0; /* already inactive */
2728 break; /* not supported yet */
2731 err = do_md_stop(mddev, 1);
2734 set_disk_ro(mddev->gendisk, 1);
2735 err = do_md_run(mddev);
2741 err = do_md_stop(mddev, 1);
2743 err = restart_array(mddev);
2746 set_disk_ro(mddev->gendisk, 0);
2750 err = do_md_run(mddev);
2755 restart_array(mddev);
2756 spin_lock_irq(&mddev->write_lock);
2757 if (atomic_read(&mddev->writes_pending) == 0) {
2758 if (mddev->in_sync == 0) {
2760 if (mddev->safemode == 1)
2761 mddev->safemode = 0;
2762 if (mddev->persistent)
2763 set_bit(MD_CHANGE_CLEAN,
2769 spin_unlock_irq(&mddev->write_lock);
2772 mddev->recovery_cp = MaxSector;
2773 err = do_md_run(mddev);
2778 restart_array(mddev);
2779 if (mddev->external)
2780 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
2781 wake_up(&mddev->sb_wait);
2785 set_disk_ro(mddev->gendisk, 0);
2786 err = do_md_run(mddev);
2791 /* these cannot be set */
2797 sysfs_notify(&mddev->kobj, NULL, "array_state");
2801 static struct md_sysfs_entry md_array_state =
2802 __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
2805 null_show(mddev_t *mddev, char *page)
2811 new_dev_store(mddev_t *mddev, const char *buf, size_t len)
2813 /* buf must be %d:%d\n? giving major and minor numbers */
2814 /* The new device is added to the array.
2815 * If the array has a persistent superblock, we read the
2816 * superblock to initialise info and check validity.
2817 * Otherwise, only checking done is that in bind_rdev_to_array,
2818 * which mainly checks size.
2821 int major = simple_strtoul(buf, &e, 10);
2827 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
2829 minor = simple_strtoul(e+1, &e, 10);
2830 if (*e && *e != '\n')
2832 dev = MKDEV(major, minor);
2833 if (major != MAJOR(dev) ||
2834 minor != MINOR(dev))
2838 if (mddev->persistent) {
2839 rdev = md_import_device(dev, mddev->major_version,
2840 mddev->minor_version);
2841 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
2842 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2843 mdk_rdev_t, same_set);
2844 err = super_types[mddev->major_version]
2845 .load_super(rdev, rdev0, mddev->minor_version);
2849 } else if (mddev->external)
2850 rdev = md_import_device(dev, -2, -1);
2852 rdev = md_import_device(dev, -1, -1);
2855 return PTR_ERR(rdev);
2856 err = bind_rdev_to_array(rdev, mddev);
2860 return err ? err : len;
2863 static struct md_sysfs_entry md_new_device =
2864 __ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
2867 bitmap_store(mddev_t *mddev, const char *buf, size_t len)
2870 unsigned long chunk, end_chunk;
2874 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
2876 chunk = end_chunk = simple_strtoul(buf, &end, 0);
2877 if (buf == end) break;
2878 if (*end == '-') { /* range */
2880 end_chunk = simple_strtoul(buf, &end, 0);
2881 if (buf == end) break;
2883 if (*end && !isspace(*end)) break;
2884 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
2886 while (isspace(*buf)) buf++;
2888 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
2893 static struct md_sysfs_entry md_bitmap =
2894 __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
2897 size_show(mddev_t *mddev, char *page)
2899 return sprintf(page, "%llu\n", (unsigned long long)mddev->size);
2902 static int update_size(mddev_t *mddev, unsigned long size);
2905 size_store(mddev_t *mddev, const char *buf, size_t len)
2907 /* If array is inactive, we can reduce the component size, but
2908 * not increase it (except from 0).
2909 * If array is active, we can try an on-line resize
2913 unsigned long long size = simple_strtoull(buf, &e, 10);
2914 if (!*buf || *buf == '\n' ||
2919 err = update_size(mddev, size);
2920 md_update_sb(mddev, 1);
2922 if (mddev->size == 0 ||
2928 return err ? err : len;
2931 static struct md_sysfs_entry md_size =
2932 __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
2937 * 'none' for arrays with no metadata (good luck...)
2938 * 'external' for arrays with externally managed metadata,
2939 * or N.M for internally known formats
2942 metadata_show(mddev_t *mddev, char *page)
2944 if (mddev->persistent)
2945 return sprintf(page, "%d.%d\n",
2946 mddev->major_version, mddev->minor_version);
2947 else if (mddev->external)
2948 return sprintf(page, "external:%s\n", mddev->metadata_type);
2950 return sprintf(page, "none\n");
2954 metadata_store(mddev_t *mddev, const char *buf, size_t len)
2958 if (!list_empty(&mddev->disks))
2961 if (cmd_match(buf, "none")) {
2962 mddev->persistent = 0;
2963 mddev->external = 0;
2964 mddev->major_version = 0;
2965 mddev->minor_version = 90;
2968 if (strncmp(buf, "external:", 9) == 0) {
2969 size_t namelen = len-9;
2970 if (namelen >= sizeof(mddev->metadata_type))
2971 namelen = sizeof(mddev->metadata_type)-1;
2972 strncpy(mddev->metadata_type, buf+9, namelen);
2973 mddev->metadata_type[namelen] = 0;
2974 if (namelen && mddev->metadata_type[namelen-1] == '\n')
2975 mddev->metadata_type[--namelen] = 0;
2976 mddev->persistent = 0;
2977 mddev->external = 1;
2978 mddev->major_version = 0;
2979 mddev->minor_version = 90;
2982 major = simple_strtoul(buf, &e, 10);
2983 if (e==buf || *e != '.')
2986 minor = simple_strtoul(buf, &e, 10);
2987 if (e==buf || (*e && *e != '\n') )
2989 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
2991 mddev->major_version = major;
2992 mddev->minor_version = minor;
2993 mddev->persistent = 1;
2994 mddev->external = 0;
2998 static struct md_sysfs_entry md_metadata =
2999 __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
3002 action_show(mddev_t *mddev, char *page)
3004 char *type = "idle";
3005 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3006 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
3007 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3009 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3010 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3012 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3016 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
3019 return sprintf(page, "%s\n", type);
3023 action_store(mddev_t *mddev, const char *page, size_t len)
3025 if (!mddev->pers || !mddev->pers->sync_request)
3028 if (cmd_match(page, "idle")) {
3029 if (mddev->sync_thread) {
3030 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3031 md_unregister_thread(mddev->sync_thread);
3032 mddev->sync_thread = NULL;
3033 mddev->recovery = 0;
3035 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3036 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
3038 else if (cmd_match(page, "resync"))
3039 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3040 else if (cmd_match(page, "recover")) {
3041 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3042 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3043 } else if (cmd_match(page, "reshape")) {
3045 if (mddev->pers->start_reshape == NULL)
3047 err = mddev->pers->start_reshape(mddev);
3050 sysfs_notify(&mddev->kobj, NULL, "degraded");
3052 if (cmd_match(page, "check"))
3053 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3054 else if (!cmd_match(page, "repair"))
3056 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3057 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3059 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3060 md_wakeup_thread(mddev->thread);
3061 sysfs_notify(&mddev->kobj, NULL, "sync_action");
3066 mismatch_cnt_show(mddev_t *mddev, char *page)
3068 return sprintf(page, "%llu\n",
3069 (unsigned long long) mddev->resync_mismatches);
3072 static struct md_sysfs_entry md_scan_mode =
3073 __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
3076 static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
3079 sync_min_show(mddev_t *mddev, char *page)
3081 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3082 mddev->sync_speed_min ? "local": "system");
3086 sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3090 if (strncmp(buf, "system", 6)==0) {
3091 mddev->sync_speed_min = 0;
3094 min = simple_strtoul(buf, &e, 10);
3095 if (buf == e || (*e && *e != '\n') || min <= 0)
3097 mddev->sync_speed_min = min;
3101 static struct md_sysfs_entry md_sync_min =
3102 __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3105 sync_max_show(mddev_t *mddev, char *page)
3107 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3108 mddev->sync_speed_max ? "local": "system");
3112 sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3116 if (strncmp(buf, "system", 6)==0) {
3117 mddev->sync_speed_max = 0;
3120 max = simple_strtoul(buf, &e, 10);
3121 if (buf == e || (*e && *e != '\n') || max <= 0)
3123 mddev->sync_speed_max = max;
3127 static struct md_sysfs_entry md_sync_max =
3128 __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3131 degraded_show(mddev_t *mddev, char *page)
3133 return sprintf(page, "%d\n", mddev->degraded);
3135 static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
3138 sync_force_parallel_show(mddev_t *mddev, char *page)
3140 return sprintf(page, "%d\n", mddev->parallel_resync);
3144 sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3148 if (strict_strtol(buf, 10, &n))
3151 if (n != 0 && n != 1)
3154 mddev->parallel_resync = n;
3156 if (mddev->sync_thread)
3157 wake_up(&resync_wait);
3162 /* force parallel resync, even with shared block devices */
3163 static struct md_sysfs_entry md_sync_force_parallel =
3164 __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3165 sync_force_parallel_show, sync_force_parallel_store);
3168 sync_speed_show(mddev_t *mddev, char *page)
3170 unsigned long resync, dt, db;
3171 resync = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active));
3172 dt = ((jiffies - mddev->resync_mark) / HZ);
3174 db = resync - (mddev->resync_mark_cnt);
3175 return sprintf(page, "%ld\n", db/dt/2); /* K/sec */
3178 static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
3181 sync_completed_show(mddev_t *mddev, char *page)
3183 unsigned long max_blocks, resync;
3185 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3186 max_blocks = mddev->resync_max_sectors;
3188 max_blocks = mddev->size << 1;
3190 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
3191 return sprintf(page, "%lu / %lu\n", resync, max_blocks);
3194 static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
3197 min_sync_show(mddev_t *mddev, char *page)
3199 return sprintf(page, "%llu\n",
3200 (unsigned long long)mddev->resync_min);
3203 min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3205 unsigned long long min;
3206 if (strict_strtoull(buf, 10, &min))
3208 if (min > mddev->resync_max)
3210 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3213 /* Must be a multiple of chunk_size */
3214 if (mddev->chunk_size) {
3215 if (min & (sector_t)((mddev->chunk_size>>9)-1))
3218 mddev->resync_min = min;
3223 static struct md_sysfs_entry md_min_sync =
3224 __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3227 max_sync_show(mddev_t *mddev, char *page)
3229 if (mddev->resync_max == MaxSector)
3230 return sprintf(page, "max\n");
3232 return sprintf(page, "%llu\n",
3233 (unsigned long long)mddev->resync_max);
3236 max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3238 if (strncmp(buf, "max", 3) == 0)
3239 mddev->resync_max = MaxSector;
3241 unsigned long long max;
3242 if (strict_strtoull(buf, 10, &max))
3244 if (max < mddev->resync_min)
3246 if (max < mddev->resync_max &&
3247 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3250 /* Must be a multiple of chunk_size */
3251 if (mddev->chunk_size) {
3252 if (max & (sector_t)((mddev->chunk_size>>9)-1))
3255 mddev->resync_max = max;
3257 wake_up(&mddev->recovery_wait);
3261 static struct md_sysfs_entry md_max_sync =
3262 __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3265 suspend_lo_show(mddev_t *mddev, char *page)
3267 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3271 suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3274 unsigned long long new = simple_strtoull(buf, &e, 10);
3276 if (mddev->pers->quiesce == NULL)
3278 if (buf == e || (*e && *e != '\n'))
3280 if (new >= mddev->suspend_hi ||
3281 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3282 mddev->suspend_lo = new;
3283 mddev->pers->quiesce(mddev, 2);
3288 static struct md_sysfs_entry md_suspend_lo =
3289 __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3293 suspend_hi_show(mddev_t *mddev, char *page)
3295 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3299 suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3302 unsigned long long new = simple_strtoull(buf, &e, 10);
3304 if (mddev->pers->quiesce == NULL)
3306 if (buf == e || (*e && *e != '\n'))
3308 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3309 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3310 mddev->suspend_hi = new;
3311 mddev->pers->quiesce(mddev, 1);
3312 mddev->pers->quiesce(mddev, 0);
3317 static struct md_sysfs_entry md_suspend_hi =
3318 __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3321 reshape_position_show(mddev_t *mddev, char *page)
3323 if (mddev->reshape_position != MaxSector)
3324 return sprintf(page, "%llu\n",
3325 (unsigned long long)mddev->reshape_position);
3326 strcpy(page, "none\n");
3331 reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3334 unsigned long long new = simple_strtoull(buf, &e, 10);
3337 if (buf == e || (*e && *e != '\n'))
3339 mddev->reshape_position = new;
3340 mddev->delta_disks = 0;
3341 mddev->new_level = mddev->level;
3342 mddev->new_layout = mddev->layout;
3343 mddev->new_chunk = mddev->chunk_size;
3347 static struct md_sysfs_entry md_reshape_position =
3348 __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3349 reshape_position_store);
3352 static struct attribute *md_default_attrs[] = {
3355 &md_raid_disks.attr,
3356 &md_chunk_size.attr,
3358 &md_resync_start.attr,
3360 &md_new_device.attr,
3361 &md_safe_delay.attr,
3362 &md_array_state.attr,
3363 &md_reshape_position.attr,
3367 static struct attribute *md_redundancy_attrs[] = {
3369 &md_mismatches.attr,
3372 &md_sync_speed.attr,
3373 &md_sync_force_parallel.attr,
3374 &md_sync_completed.attr,
3377 &md_suspend_lo.attr,
3378 &md_suspend_hi.attr,
3383 static struct attribute_group md_redundancy_group = {
3385 .attrs = md_redundancy_attrs,
3390 md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3392 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3393 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
3398 rv = mddev_lock(mddev);
3400 rv = entry->show(mddev, page);
3401 mddev_unlock(mddev);
3407 md_attr_store(struct kobject *kobj, struct attribute *attr,
3408 const char *page, size_t length)
3410 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3411 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
3416 if (!capable(CAP_SYS_ADMIN))
3418 rv = mddev_lock(mddev);
3420 rv = entry->store(mddev, page, length);
3421 mddev_unlock(mddev);
3426 static void md_free(struct kobject *ko)
3428 mddev_t *mddev = container_of(ko, mddev_t, kobj);
3432 static struct sysfs_ops md_sysfs_ops = {
3433 .show = md_attr_show,
3434 .store = md_attr_store,
3436 static struct kobj_type md_ktype = {
3438 .sysfs_ops = &md_sysfs_ops,
3439 .default_attrs = md_default_attrs,
3444 static struct kobject *md_probe(dev_t dev, int *part, void *data)
3446 static DEFINE_MUTEX(disks_mutex);
3447 mddev_t *mddev = mddev_find(dev);
3448 struct gendisk *disk;
3449 int partitioned = (MAJOR(dev) != MD_MAJOR);
3450 int shift = partitioned ? MdpMinorShift : 0;
3451 int unit = MINOR(dev) >> shift;
3457 mutex_lock(&disks_mutex);
3458 if (mddev->gendisk) {
3459 mutex_unlock(&disks_mutex);
3463 disk = alloc_disk(1 << shift);
3465 mutex_unlock(&disks_mutex);
3469 disk->major = MAJOR(dev);
3470 disk->first_minor = unit << shift;
3472 sprintf(disk->disk_name, "md_d%d", unit);
3474 sprintf(disk->disk_name, "md%d", unit);
3475 disk->fops = &md_fops;
3476 disk->private_data = mddev;
3477 disk->queue = mddev->queue;
3479 mddev->gendisk = disk;
3480 error = kobject_init_and_add(&mddev->kobj, &md_ktype, &disk->dev.kobj,
3482 mutex_unlock(&disks_mutex);
3484 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
3487 kobject_uevent(&mddev->kobj, KOBJ_ADD);
3491 static void md_safemode_timeout(unsigned long data)
3493 mddev_t *mddev = (mddev_t *) data;
3495 if (!atomic_read(&mddev->writes_pending)) {
3496 mddev->safemode = 1;
3497 if (mddev->external)
3498 sysfs_notify(&mddev->kobj, NULL, "array_state");
3500 md_wakeup_thread(mddev->thread);
3503 static int start_dirty_degraded;
3505 static int do_md_run(mddev_t * mddev)
3509 struct list_head *tmp;
3511 struct gendisk *disk;
3512 struct mdk_personality *pers;
3513 char b[BDEVNAME_SIZE];
3515 if (list_empty(&mddev->disks))
3516 /* cannot run an array with no devices.. */
3523 * Analyze all RAID superblock(s)
3525 if (!mddev->raid_disks) {
3526 if (!mddev->persistent)
3531 chunk_size = mddev->chunk_size;
3534 if (chunk_size > MAX_CHUNK_SIZE) {
3535 printk(KERN_ERR "too big chunk_size: %d > %d\n",
3536 chunk_size, MAX_CHUNK_SIZE);
3540 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
3542 if ( (1 << ffz(~chunk_size)) != chunk_size) {
3543 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
3546 if (chunk_size < PAGE_SIZE) {
3547 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
3548 chunk_size, PAGE_SIZE);
3552 /* devices must have minimum size of one chunk */
3553 rdev_for_each(rdev, tmp, mddev) {
3554 if (test_bit(Faulty, &rdev->flags))
3556 if (rdev->size < chunk_size / 1024) {
3558 "md: Dev %s smaller than chunk_size:"
3560 bdevname(rdev->bdev,b),
3561 (unsigned long long)rdev->size,
3569 if (mddev->level != LEVEL_NONE)
3570 request_module("md-level-%d", mddev->level);
3571 else if (mddev->clevel[0])
3572 request_module("md-%s", mddev->clevel);
3576 * Drop all container device buffers, from now on
3577 * the only valid external interface is through the md
3580 rdev_for_each(rdev, tmp, mddev) {
3581 if (test_bit(Faulty, &rdev->flags))
3583 sync_blockdev(rdev->bdev);
3584 invalidate_bdev(rdev->bdev);
3586 /* perform some consistency tests on the device.
3587 * We don't want the data to overlap the metadata,
3588 * Internal Bitmap issues has handled elsewhere.
3590 if (rdev->data_offset < rdev->sb_offset) {
3592 rdev->data_offset + mddev->size*2
3593 > rdev->sb_offset*2) {
3594 printk("md: %s: data overlaps metadata\n",
3599 if (rdev->sb_offset*2 + rdev->sb_size/512
3600 > rdev->data_offset) {
3601 printk("md: %s: metadata overlaps data\n",
3606 sysfs_notify(&rdev->kobj, NULL, "state");
3609 md_probe(mddev->unit, NULL, NULL);
3610 disk = mddev->gendisk;
3614 spin_lock(&pers_lock);
3615 pers = find_pers(mddev->level, mddev->clevel);
3616 if (!pers || !try_module_get(pers->owner)) {
3617 spin_unlock(&pers_lock);
3618 if (mddev->level != LEVEL_NONE)
3619 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
3622 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
3627 spin_unlock(&pers_lock);
3628 mddev->level = pers->level;
3629 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3631 if (mddev->reshape_position != MaxSector &&
3632 pers->start_reshape == NULL) {
3633 /* This personality cannot handle reshaping... */
3635 module_put(pers->owner);
3639 if (pers->sync_request) {
3640 /* Warn if this is a potentially silly
3643 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3645 struct list_head *tmp2;
3647 rdev_for_each(rdev, tmp, mddev) {
3648 rdev_for_each(rdev2, tmp2, mddev) {
3650 rdev->bdev->bd_contains ==
3651 rdev2->bdev->bd_contains) {
3653 "%s: WARNING: %s appears to be"
3654 " on the same physical disk as"
3657 bdevname(rdev->bdev,b),
3658 bdevname(rdev2->bdev,b2));
3665 "True protection against single-disk"
3666 " failure might be compromised.\n");
3669 mddev->recovery = 0;
3670 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
3671 mddev->barriers_work = 1;
3672 mddev->ok_start_degraded = start_dirty_degraded;
3675 mddev->ro = 2; /* read-only, but switch on first write */
3677 err = mddev->pers->run(mddev);
3678 if (!err && mddev->pers->sync_request) {
3679 err = bitmap_create(mddev);
3681 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
3682 mdname(mddev), err);
3683 mddev->pers->stop(mddev);
3687 printk(KERN_ERR "md: pers->run() failed ...\n");
3688 module_put(mddev->pers->owner);
3690 bitmap_destroy(mddev);
3693 if (mddev->pers->sync_request) {
3694 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3696 "md: cannot register extra attributes for %s\n",
3698 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
3701 atomic_set(&mddev->writes_pending,0);
3702 mddev->safemode = 0;
3703 mddev->safemode_timer.function = md_safemode_timeout;
3704 mddev->safemode_timer.data = (unsigned long) mddev;
3705 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
3708 rdev_for_each(rdev, tmp, mddev)
3709 if (rdev->raid_disk >= 0) {
3711 sprintf(nm, "rd%d", rdev->raid_disk);
3712 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
3713 printk("md: cannot register %s for %s\n",
3717 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3720 md_update_sb(mddev, 0);
3722 set_capacity(disk, mddev->array_size<<1);
3724 /* If we call blk_queue_make_request here, it will
3725 * re-initialise max_sectors etc which may have been
3726 * refined inside -> run. So just set the bits we need to set.
3727 * Most initialisation happended when we called
3728 * blk_queue_make_request(..., md_fail_request)
3731 mddev->queue->queuedata = mddev;
3732 mddev->queue->make_request_fn = mddev->pers->make_request;
3734 /* If there is a partially-recovered drive we need to
3735 * start recovery here. If we leave it to md_check_recovery,
3736 * it will remove the drives and not do the right thing
3738 if (mddev->degraded && !mddev->sync_thread) {
3739 struct list_head *rtmp;
3741 rdev_for_each(rdev, rtmp, mddev)
3742 if (rdev->raid_disk >= 0 &&
3743 !test_bit(In_sync, &rdev->flags) &&
3744 !test_bit(Faulty, &rdev->flags))
3745 /* complete an interrupted recovery */
3747 if (spares && mddev->pers->sync_request) {
3748 mddev->recovery = 0;
3749 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3750 mddev->sync_thread = md_register_thread(md_do_sync,
3753 if (!mddev->sync_thread) {
3754 printk(KERN_ERR "%s: could not start resync"
3757 /* leave the spares where they are, it shouldn't hurt */
3758 mddev->recovery = 0;
3762 md_wakeup_thread(mddev->thread);
3763 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
3766 md_new_event(mddev);
3767 sysfs_notify(&mddev->kobj, NULL, "array_state");
3768 sysfs_notify(&mddev->kobj, NULL, "sync_action");
3769 sysfs_notify(&mddev->kobj, NULL, "degraded");
3770 kobject_uevent(&mddev->gendisk->dev.kobj, KOBJ_CHANGE);
3774 static int restart_array(mddev_t *mddev)
3776 struct gendisk *disk = mddev->gendisk;
3780 * Complain if it has no devices
3783 if (list_empty(&mddev->disks))
3791 mddev->safemode = 0;
3793 set_disk_ro(disk, 0);
3795 printk(KERN_INFO "md: %s switched to read-write mode.\n",
3798 * Kick recovery or resync if necessary
3800 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3801 md_wakeup_thread(mddev->thread);
3802 md_wakeup_thread(mddev->sync_thread);
3804 sysfs_notify(&mddev->kobj, NULL, "array_state");
3813 /* similar to deny_write_access, but accounts for our holding a reference
3814 * to the file ourselves */
3815 static int deny_bitmap_write_access(struct file * file)
3817 struct inode *inode = file->f_mapping->host;
3819 spin_lock(&inode->i_lock);
3820 if (atomic_read(&inode->i_writecount) > 1) {
3821 spin_unlock(&inode->i_lock);
3824 atomic_set(&inode->i_writecount, -1);
3825 spin_unlock(&inode->i_lock);
3830 static void restore_bitmap_write_access(struct file *file)
3832 struct inode *inode = file->f_mapping->host;
3834 spin_lock(&inode->i_lock);
3835 atomic_set(&inode->i_writecount, 1);
3836 spin_unlock(&inode->i_lock);
3840 * 0 - completely stop and dis-assemble array
3841 * 1 - switch to readonly
3842 * 2 - stop but do not disassemble array
3844 static int do_md_stop(mddev_t * mddev, int mode)
3847 struct gendisk *disk = mddev->gendisk;
3850 if (atomic_read(&mddev->active)>2) {
3851 printk("md: %s still in use.\n",mdname(mddev));
3855 if (mddev->sync_thread) {
3856 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3857 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3858 md_unregister_thread(mddev->sync_thread);
3859 mddev->sync_thread = NULL;
3862 del_timer_sync(&mddev->safemode_timer);
3864 invalidate_partition(disk, 0);
3867 case 1: /* readonly */
3873 case 0: /* disassemble */
3875 bitmap_flush(mddev);
3876 md_super_wait(mddev);
3878 set_disk_ro(disk, 0);
3879 blk_queue_make_request(mddev->queue, md_fail_request);
3880 mddev->pers->stop(mddev);
3881 mddev->queue->merge_bvec_fn = NULL;
3882 mddev->queue->unplug_fn = NULL;
3883 mddev->queue->backing_dev_info.congested_fn = NULL;
3884 if (mddev->pers->sync_request)
3885 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
3887 module_put(mddev->pers->owner);
3889 /* tell userspace to handle 'inactive' */
3890 sysfs_notify(&mddev->kobj, NULL, "array_state");
3892 set_capacity(disk, 0);
3898 if (!mddev->in_sync || mddev->flags) {
3899 /* mark array as shutdown cleanly */
3901 md_update_sb(mddev, 1);
3904 set_disk_ro(disk, 1);
3905 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3909 * Free resources if final stop
3913 struct list_head *tmp;
3915 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
3917 bitmap_destroy(mddev);
3918 if (mddev->bitmap_file) {
3919 restore_bitmap_write_access(mddev->bitmap_file);
3920 fput(mddev->bitmap_file);
3921 mddev->bitmap_file = NULL;
3923 mddev->bitmap_offset = 0;
3925 rdev_for_each(rdev, tmp, mddev)
3926 if (rdev->raid_disk >= 0) {
3928 sprintf(nm, "rd%d", rdev->raid_disk);
3929 sysfs_remove_link(&mddev->kobj, nm);
3932 /* make sure all md_delayed_delete calls have finished */
3933 flush_scheduled_work();
3935 export_array(mddev);
3937 mddev->array_size = 0;
3939 mddev->raid_disks = 0;
3940 mddev->recovery_cp = 0;
3941 mddev->resync_min = 0;
3942 mddev->resync_max = MaxSector;
3943 mddev->reshape_position = MaxSector;
3944 mddev->external = 0;
3945 mddev->persistent = 0;
3946 mddev->level = LEVEL_NONE;
3947 mddev->clevel[0] = 0;
3950 mddev->metadata_type[0] = 0;
3951 mddev->chunk_size = 0;
3952 mddev->ctime = mddev->utime = 0;
3954 mddev->max_disks = 0;
3956 mddev->delta_disks = 0;
3957 mddev->new_level = LEVEL_NONE;
3958 mddev->new_layout = 0;
3959 mddev->new_chunk = 0;
3960 mddev->curr_resync = 0;
3961 mddev->resync_mismatches = 0;
3962 mddev->suspend_lo = mddev->suspend_hi = 0;
3963 mddev->sync_speed_min = mddev->sync_speed_max = 0;
3964 mddev->recovery = 0;
3967 mddev->degraded = 0;
3968 mddev->barriers_work = 0;
3969 mddev->safemode = 0;
3971 } else if (mddev->pers)
3972 printk(KERN_INFO "md: %s switched to read-only mode.\n",
3975 md_new_event(mddev);
3976 sysfs_notify(&mddev->kobj, NULL, "array_state");
3982 static void autorun_array(mddev_t *mddev)
3985 struct list_head *tmp;
3988 if (list_empty(&mddev->disks))
3991 printk(KERN_INFO "md: running: ");
3993 rdev_for_each(rdev, tmp, mddev) {
3994 char b[BDEVNAME_SIZE];
3995 printk("<%s>", bdevname(rdev->bdev,b));
3999 err = do_md_run (mddev);
4001 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
4002 do_md_stop (mddev, 0);
4007 * lets try to run arrays based on all disks that have arrived
4008 * until now. (those are in pending_raid_disks)
4010 * the method: pick the first pending disk, collect all disks with
4011 * the same UUID, remove all from the pending list and put them into
4012 * the 'same_array' list. Then order this list based on superblock
4013 * update time (freshest comes first), kick out 'old' disks and
4014 * compare superblocks. If everything's fine then run it.
4016 * If "unit" is allocated, then bump its reference count
4018 static void autorun_devices(int part)
4020 struct list_head *tmp;
4021 mdk_rdev_t *rdev0, *rdev;
4023 char b[BDEVNAME_SIZE];
4025 printk(KERN_INFO "md: autorun ...\n");
4026 while (!list_empty(&pending_raid_disks)) {
4029 LIST_HEAD(candidates);
4030 rdev0 = list_entry(pending_raid_disks.next,
4031 mdk_rdev_t, same_set);
4033 printk(KERN_INFO "md: considering %s ...\n",
4034 bdevname(rdev0->bdev,b));
4035 INIT_LIST_HEAD(&candidates);
4036 rdev_for_each_list(rdev, tmp, pending_raid_disks)
4037 if (super_90_load(rdev, rdev0, 0) >= 0) {
4038 printk(KERN_INFO "md: adding %s ...\n",
4039 bdevname(rdev->bdev,b));
4040 list_move(&rdev->same_set, &candidates);
4043 * now we have a set of devices, with all of them having
4044 * mostly sane superblocks. It's time to allocate the
4048 dev = MKDEV(mdp_major,
4049 rdev0->preferred_minor << MdpMinorShift);
4050 unit = MINOR(dev) >> MdpMinorShift;
4052 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4055 if (rdev0->preferred_minor != unit) {
4056 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4057 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4061 md_probe(dev, NULL, NULL);
4062 mddev = mddev_find(dev);
4063 if (!mddev || !mddev->gendisk) {
4067 "md: cannot allocate memory for md drive.\n");
4070 if (mddev_lock(mddev))
4071 printk(KERN_WARNING "md: %s locked, cannot run\n",
4073 else if (mddev->raid_disks || mddev->major_version
4074 || !list_empty(&mddev->disks)) {
4076 "md: %s already running, cannot run %s\n",
4077 mdname(mddev), bdevname(rdev0->bdev,b));
4078 mddev_unlock(mddev);
4080 printk(KERN_INFO "md: created %s\n", mdname(mddev));
4081 mddev->persistent = 1;
4082 rdev_for_each_list(rdev, tmp, candidates) {
4083 list_del_init(&rdev->same_set);
4084 if (bind_rdev_to_array(rdev, mddev))
4087 autorun_array(mddev);
4088 mddev_unlock(mddev);
4090 /* on success, candidates will be empty, on error
4093 rdev_for_each_list(rdev, tmp, candidates)
4097 printk(KERN_INFO "md: ... autorun DONE.\n");
4099 #endif /* !MODULE */
4101 static int get_version(void __user * arg)
4105 ver.major = MD_MAJOR_VERSION;
4106 ver.minor = MD_MINOR_VERSION;
4107 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4109 if (copy_to_user(arg, &ver, sizeof(ver)))
4115 static int get_array_info(mddev_t * mddev, void __user * arg)
4117 mdu_array_info_t info;
4118 int nr,working,active,failed,spare;
4120 struct list_head *tmp;
4122 nr=working=active=failed=spare=0;
4123 rdev_for_each(rdev, tmp, mddev) {
4125 if (test_bit(Faulty, &rdev->flags))
4129 if (test_bit(In_sync, &rdev->flags))
4136 info.major_version = mddev->major_version;
4137 info.minor_version = mddev->minor_version;
4138 info.patch_version = MD_PATCHLEVEL_VERSION;
4139 info.ctime = mddev->ctime;
4140 info.level = mddev->level;
4141 info.size = mddev->size;
4142 if (info.size != mddev->size) /* overflow */
4145 info.raid_disks = mddev->raid_disks;
4146 info.md_minor = mddev->md_minor;
4147 info.not_persistent= !mddev->persistent;
4149 info.utime = mddev->utime;
4152 info.state = (1<<MD_SB_CLEAN);
4153 if (mddev->bitmap && mddev->bitmap_offset)
4154 info.state = (1<<MD_SB_BITMAP_PRESENT);
4155 info.active_disks = active;
4156 info.working_disks = working;
4157 info.failed_disks = failed;
4158 info.spare_disks = spare;
4160 info.layout = mddev->layout;
4161 info.chunk_size = mddev->chunk_size;
4163 if (copy_to_user(arg, &info, sizeof(info)))
4169 static int get_bitmap_file(mddev_t * mddev, void __user * arg)
4171 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4172 char *ptr, *buf = NULL;
4175 if (md_allow_write(mddev))
4176 file = kmalloc(sizeof(*file), GFP_NOIO);
4178 file = kmalloc(sizeof(*file), GFP_KERNEL);
4183 /* bitmap disabled, zero the first byte and copy out */
4184 if (!mddev->bitmap || !mddev->bitmap->file) {
4185 file->pathname[0] = '\0';
4189 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4193 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4197 strcpy(file->pathname, ptr);
4201 if (copy_to_user(arg, file, sizeof(*file)))
4209 static int get_disk_info(mddev_t * mddev, void __user * arg)
4211 mdu_disk_info_t info;
4215 if (copy_from_user(&info, arg, sizeof(info)))
4220 rdev = find_rdev_nr(mddev, nr);
4222 info.major = MAJOR(rdev->bdev->bd_dev);
4223 info.minor = MINOR(rdev->bdev->bd_dev);
4224 info.raid_disk = rdev->raid_disk;
4226 if (test_bit(Faulty, &rdev->flags))
4227 info.state |= (1<<MD_DISK_FAULTY);
4228 else if (test_bit(In_sync, &rdev->flags)) {
4229 info.state |= (1<<MD_DISK_ACTIVE);
4230 info.state |= (1<<MD_DISK_SYNC);
4232 if (test_bit(WriteMostly, &rdev->flags))
4233 info.state |= (1<<MD_DISK_WRITEMOSTLY);
4235 info.major = info.minor = 0;
4236 info.raid_disk = -1;
4237 info.state = (1<<MD_DISK_REMOVED);
4240 if (copy_to_user(arg, &info, sizeof(info)))
4246 static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4248 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4250 dev_t dev = MKDEV(info->major,info->minor);
4252 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4255 if (!mddev->raid_disks) {
4257 /* expecting a device which has a superblock */
4258 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4261 "md: md_import_device returned %ld\n",
4263 return PTR_ERR(rdev);
4265 if (!list_empty(&mddev->disks)) {
4266 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4267 mdk_rdev_t, same_set);
4268 int err = super_types[mddev->major_version]
4269 .load_super(rdev, rdev0, mddev->minor_version);
4272 "md: %s has different UUID to %s\n",
4273 bdevname(rdev->bdev,b),
4274 bdevname(rdev0->bdev,b2));
4279 err = bind_rdev_to_array(rdev, mddev);
4286 * add_new_disk can be used once the array is assembled
4287 * to add "hot spares". They must already have a superblock
4292 if (!mddev->pers->hot_add_disk) {
4294 "%s: personality does not support diskops!\n",
4298 if (mddev->persistent)
4299 rdev = md_import_device(dev, mddev->major_version,
4300 mddev->minor_version);
4302 rdev = md_import_device(dev, -1, -1);
4305 "md: md_import_device returned %ld\n",
4307 return PTR_ERR(rdev);
4309 /* set save_raid_disk if appropriate */
4310 if (!mddev->persistent) {
4311 if (info->state & (1<<MD_DISK_SYNC) &&
4312 info->raid_disk < mddev->raid_disks)
4313 rdev->raid_disk = info->raid_disk;
4315 rdev->raid_disk = -1;
4317 super_types[mddev->major_version].
4318 validate_super(mddev, rdev);
4319 rdev->saved_raid_disk = rdev->raid_disk;
4321 clear_bit(In_sync, &rdev->flags); /* just to be sure */
4322 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4323 set_bit(WriteMostly, &rdev->flags);
4325 rdev->raid_disk = -1;
4326 err = bind_rdev_to_array(rdev, mddev);
4327 if (!err && !mddev->pers->hot_remove_disk) {
4328 /* If there is hot_add_disk but no hot_remove_disk
4329 * then added disks for geometry changes,
4330 * and should be added immediately.
4332 super_types[mddev->major_version].
4333 validate_super(mddev, rdev);
4334 err = mddev->pers->hot_add_disk(mddev, rdev);
4336 unbind_rdev_from_array(rdev);
4341 sysfs_notify(&rdev->kobj, NULL, "state");
4343 md_update_sb(mddev, 1);
4344 if (mddev->degraded)
4345 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4346 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4347 md_wakeup_thread(mddev->thread);
4351 /* otherwise, add_new_disk is only allowed
4352 * for major_version==0 superblocks
4354 if (mddev->major_version != 0) {
4355 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
4360 if (!(info->state & (1<<MD_DISK_FAULTY))) {
4362 rdev = md_import_device (dev, -1, 0);
4365 "md: error, md_import_device() returned %ld\n",
4367 return PTR_ERR(rdev);
4369 rdev->desc_nr = info->number;
4370 if (info->raid_disk < mddev->raid_disks)
4371 rdev->raid_disk = info->raid_disk;
4373 rdev->raid_disk = -1;
4375 if (rdev->raid_disk < mddev->raid_disks)
4376 if (info->state & (1<<MD_DISK_SYNC))
4377 set_bit(In_sync, &rdev->flags);
4379 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4380 set_bit(WriteMostly, &rdev->flags);
4382 if (!mddev->persistent) {
4383 printk(KERN_INFO "md: nonpersistent superblock ...\n");
4384 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
4386 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
4387 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
4389 err = bind_rdev_to_array(rdev, mddev);
4399 static int hot_remove_disk(mddev_t * mddev, dev_t dev)
4401 char b[BDEVNAME_SIZE];
4404 rdev = find_rdev(mddev, dev);
4408 if (rdev->raid_disk >= 0)
4411 kick_rdev_from_array(rdev);
4412 md_update_sb(mddev, 1);
4413 md_new_event(mddev);
4417 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
4418 bdevname(rdev->bdev,b), mdname(mddev));
4422 static int hot_add_disk(mddev_t * mddev, dev_t dev)
4424 char b[BDEVNAME_SIZE];
4432 if (mddev->major_version != 0) {
4433 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
4434 " version-0 superblocks.\n",
4438 if (!mddev->pers->hot_add_disk) {
4440 "%s: personality does not support diskops!\n",
4445 rdev = md_import_device (dev, -1, 0);
4448 "md: error, md_import_device() returned %ld\n",
4453 if (mddev->persistent)
4454 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
4457 rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
4459 size = calc_dev_size(rdev, mddev->chunk_size);
4462 if (test_bit(Faulty, &rdev->flags)) {
4464 "md: can not hot-add faulty %s disk to %s!\n",
4465 bdevname(rdev->bdev,b), mdname(mddev));
4469 clear_bit(In_sync, &rdev->flags);
4471 rdev->saved_raid_disk = -1;
4472 err = bind_rdev_to_array(rdev, mddev);
4477 * The rest should better be atomic, we can have disk failures
4478 * noticed in interrupt contexts ...
4481 if (rdev->desc_nr == mddev->max_disks) {
4482 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
4485 goto abort_unbind_export;
4488 rdev->raid_disk = -1;
4490 md_update_sb(mddev, 1);
4493 * Kick recovery, maybe this spare has to be added to the
4494 * array immediately.
4496 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4497 md_wakeup_thread(mddev->thread);
4498 md_new_event(mddev);
4501 abort_unbind_export:
4502 unbind_rdev_from_array(rdev);
4509 static int set_bitmap_file(mddev_t *mddev, int fd)
4514 if (!mddev->pers->quiesce)
4516 if (mddev->recovery || mddev->sync_thread)
4518 /* we should be able to change the bitmap.. */
4524 return -EEXIST; /* cannot add when bitmap is present */
4525 mddev->bitmap_file = fget(fd);
4527 if (mddev->bitmap_file == NULL) {
4528 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
4533 err = deny_bitmap_write_access(mddev->bitmap_file);
4535 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
4537 fput(mddev->bitmap_file);
4538 mddev->bitmap_file = NULL;
4541 mddev->bitmap_offset = 0; /* file overrides offset */
4542 } else if (mddev->bitmap == NULL)
4543 return -ENOENT; /* cannot remove what isn't there */
4546 mddev->pers->quiesce(mddev, 1);
4548 err = bitmap_create(mddev);
4549 if (fd < 0 || err) {
4550 bitmap_destroy(mddev);
4551 fd = -1; /* make sure to put the file */
4553 mddev->pers->quiesce(mddev, 0);
4556 if (mddev->bitmap_file) {
4557 restore_bitmap_write_access(mddev->bitmap_file);
4558 fput(mddev->bitmap_file);
4560 mddev->bitmap_file = NULL;
4567 * set_array_info is used two different ways
4568 * The original usage is when creating a new array.
4569 * In this usage, raid_disks is > 0 and it together with
4570 * level, size, not_persistent,layout,chunksize determine the
4571 * shape of the array.
4572 * This will always create an array with a type-0.90.0 superblock.
4573 * The newer usage is when assembling an array.
4574 * In this case raid_disks will be 0, and the major_version field is
4575 * use to determine which style super-blocks are to be found on the devices.
4576 * The minor and patch _version numbers are also kept incase the
4577 * super_block handler wishes to interpret them.
4579 static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
4582 if (info->raid_disks == 0) {
4583 /* just setting version number for superblock loading */
4584 if (info->major_version < 0 ||
4585 info->major_version >= ARRAY_SIZE(super_types) ||
4586 super_types[info->major_version].name == NULL) {
4587 /* maybe try to auto-load a module? */
4589 "md: superblock version %d not known\n",
4590 info->major_version);
4593 mddev->major_version = info->major_version;
4594 mddev->minor_version = info->minor_version;
4595 mddev->patch_version = info->patch_version;
4596 mddev->persistent = !info->not_persistent;
4599 mddev->major_version = MD_MAJOR_VERSION;
4600 mddev->minor_version = MD_MINOR_VERSION;
4601 mddev->patch_version = MD_PATCHLEVEL_VERSION;
4602 mddev->ctime = get_seconds();
4604 mddev->level = info->level;
4605 mddev->clevel[0] = 0;
4606 mddev->size = info->size;
4607 mddev->raid_disks = info->raid_disks;
4608 /* don't set md_minor, it is determined by which /dev/md* was
4611 if (info->state & (1<<MD_SB_CLEAN))
4612 mddev->recovery_cp = MaxSector;
4614 mddev->recovery_cp = 0;
4615 mddev->persistent = ! info->not_persistent;
4616 mddev->external = 0;
4618 mddev->layout = info->layout;
4619 mddev->chunk_size = info->chunk_size;
4621 mddev->max_disks = MD_SB_DISKS;
4623 if (mddev->persistent)
4625 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4627 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
4628 mddev->bitmap_offset = 0;
4630 mddev->reshape_position = MaxSector;
4633 * Generate a 128 bit UUID
4635 get_random_bytes(mddev->uuid, 16);
4637 mddev->new_level = mddev->level;
4638 mddev->new_chunk = mddev->chunk_size;
4639 mddev->new_layout = mddev->layout;
4640 mddev->delta_disks = 0;
4645 static int update_size(mddev_t *mddev, unsigned long size)
4649 struct list_head *tmp;
4650 int fit = (size == 0);
4652 if (mddev->pers->resize == NULL)
4654 /* The "size" is the amount of each device that is used.
4655 * This can only make sense for arrays with redundancy.
4656 * linear and raid0 always use whatever space is available
4657 * We can only consider changing the size if no resync
4658 * or reconstruction is happening, and if the new size
4659 * is acceptable. It must fit before the sb_offset or,
4660 * if that is <data_offset, it must fit before the
4661 * size of each device.
4662 * If size is zero, we find the largest size that fits.
4664 if (mddev->sync_thread)
4666 rdev_for_each(rdev, tmp, mddev) {
4668 avail = rdev->size * 2;
4670 if (fit && (size == 0 || size > avail/2))
4672 if (avail < ((sector_t)size << 1))
4675 rv = mddev->pers->resize(mddev, (sector_t)size *2);
4677 struct block_device *bdev;
4679 bdev = bdget_disk(mddev->gendisk, 0);
4681 mutex_lock(&bdev->bd_inode->i_mutex);
4682 i_size_write(bdev->bd_inode, (loff_t)mddev->array_size << 10);
4683 mutex_unlock(&bdev->bd_inode->i_mutex);
4690 static int update_raid_disks(mddev_t *mddev, int raid_disks)
4693 /* change the number of raid disks */
4694 if (mddev->pers->check_reshape == NULL)
4696 if (raid_disks <= 0 ||
4697 raid_disks >= mddev->max_disks)
4699 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
4701 mddev->delta_disks = raid_disks - mddev->raid_disks;
4703 rv = mddev->pers->check_reshape(mddev);
4709 * update_array_info is used to change the configuration of an
4711 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
4712 * fields in the info are checked against the array.
4713 * Any differences that cannot be handled will cause an error.
4714 * Normally, only one change can be managed at a time.
4716 static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
4722 /* calculate expected state,ignoring low bits */
4723 if (mddev->bitmap && mddev->bitmap_offset)
4724 state |= (1 << MD_SB_BITMAP_PRESENT);
4726 if (mddev->major_version != info->major_version ||
4727 mddev->minor_version != info->minor_version ||
4728 /* mddev->patch_version != info->patch_version || */
4729 mddev->ctime != info->ctime ||
4730 mddev->level != info->level ||
4731 /* mddev->layout != info->layout || */
4732 !mddev->persistent != info->not_persistent||
4733 mddev->chunk_size != info->chunk_size ||
4734 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
4735 ((state^info->state) & 0xfffffe00)
4738 /* Check there is only one change */
4739 if (info->size >= 0 && mddev->size != info->size) cnt++;
4740 if (mddev->raid_disks != info->raid_disks) cnt++;
4741 if (mddev->layout != info->layout) cnt++;
4742 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
4743 if (cnt == 0) return 0;
4744 if (cnt > 1) return -EINVAL;
4746 if (mddev->layout != info->layout) {
4748 * we don't need to do anything at the md level, the
4749 * personality will take care of it all.
4751 if (mddev->pers->reconfig == NULL)
4754 return mddev->pers->reconfig(mddev, info->layout, -1);
4756 if (info->size >= 0 && mddev->size != info->size)
4757 rv = update_size(mddev, info->size);
4759 if (mddev->raid_disks != info->raid_disks)
4760 rv = update_raid_disks(mddev, info->raid_disks);
4762 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
4763 if (mddev->pers->quiesce == NULL)
4765 if (mddev->recovery || mddev->sync_thread)
4767 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
4768 /* add the bitmap */
4771 if (mddev->default_bitmap_offset == 0)
4773 mddev->bitmap_offset = mddev->default_bitmap_offset;
4774 mddev->pers->quiesce(mddev, 1);
4775 rv = bitmap_create(mddev);
4777 bitmap_destroy(mddev);
4778 mddev->pers->quiesce(mddev, 0);
4780 /* remove the bitmap */
4783 if (mddev->bitmap->file)
4785 mddev->pers->quiesce(mddev, 1);
4786 bitmap_destroy(mddev);
4787 mddev->pers->quiesce(mddev, 0);
4788 mddev->bitmap_offset = 0;
4791 md_update_sb(mddev, 1);
4795 static int set_disk_faulty(mddev_t *mddev, dev_t dev)
4799 if (mddev->pers == NULL)
4802 rdev = find_rdev(mddev, dev);
4806 md_error(mddev, rdev);
4810 static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4812 mddev_t *mddev = bdev->bd_disk->private_data;
4816 geo->cylinders = get_capacity(mddev->gendisk) / 8;
4820 static int md_ioctl(struct inode *inode, struct file *file,
4821 unsigned int cmd, unsigned long arg)
4824 void __user *argp = (void __user *)arg;
4825 mddev_t *mddev = NULL;
4827 if (!capable(CAP_SYS_ADMIN))
4831 * Commands dealing with the RAID driver but not any
4837 err = get_version(argp);
4840 case PRINT_RAID_DEBUG:
4848 autostart_arrays(arg);
4855 * Commands creating/starting a new array:
4858 mddev = inode->i_bdev->bd_disk->private_data;
4865 err = mddev_lock(mddev);
4868 "md: ioctl lock interrupted, reason %d, cmd %d\n",
4875 case SET_ARRAY_INFO:
4877 mdu_array_info_t info;
4879 memset(&info, 0, sizeof(info));
4880 else if (copy_from_user(&info, argp, sizeof(info))) {
4885 err = update_array_info(mddev, &info);
4887 printk(KERN_WARNING "md: couldn't update"
4888 " array info. %d\n", err);
4893 if (!list_empty(&mddev->disks)) {
4895 "md: array %s already has disks!\n",
4900 if (mddev->raid_disks) {
4902 "md: array %s already initialised!\n",
4907 err = set_array_info(mddev, &info);
4909 printk(KERN_WARNING "md: couldn't set"
4910 " array info. %d\n", err);
4920 * Commands querying/configuring an existing array:
4922 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
4923 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
4924 if ((!mddev->raid_disks && !mddev->external)
4925 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
4926 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
4927 && cmd != GET_BITMAP_FILE) {
4933 * Commands even a read-only array can execute:
4937 case GET_ARRAY_INFO:
4938 err = get_array_info(mddev, argp);
4941 case GET_BITMAP_FILE:
4942 err = get_bitmap_file(mddev, argp);
4946 err = get_disk_info(mddev, argp);
4949 case RESTART_ARRAY_RW:
4950 err = restart_array(mddev);
4954 err = do_md_stop (mddev, 0);
4958 err = do_md_stop (mddev, 1);
4962 * We have a problem here : there is no easy way to give a CHS
4963 * virtual geometry. We currently pretend that we have a 2 heads
4964 * 4 sectors (with a BIG number of cylinders...). This drives
4965 * dosfs just mad... ;-)
4970 * The remaining ioctls are changing the state of the
4971 * superblock, so we do not allow them on read-only arrays.
4972 * However non-MD ioctls (e.g. get-size) will still come through
4973 * here and hit the 'default' below, so only disallow
4974 * 'md' ioctls, and switch to rw mode if started auto-readonly.
4976 if (_IOC_TYPE(cmd) == MD_MAJOR &&
4977 mddev->ro && mddev->pers) {
4978 if (mddev->ro == 2) {
4980 sysfs_notify(&mddev->kobj, NULL, "array_state");
4981 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4982 md_wakeup_thread(mddev->thread);
4994 mdu_disk_info_t info;
4995 if (copy_from_user(&info, argp, sizeof(info)))
4998 err = add_new_disk(mddev, &info);
5002 case HOT_REMOVE_DISK:
5003 err = hot_remove_disk(mddev, new_decode_dev(arg));
5007 err = hot_add_disk(mddev, new_decode_dev(arg));
5010 case SET_DISK_FAULTY:
5011 err = set_disk_faulty(mddev, new_decode_dev(arg));
5015 err = do_md_run (mddev);
5018 case SET_BITMAP_FILE:
5019 err = set_bitmap_file(mddev, (int)arg);
5029 mddev_unlock(mddev);
5039 static int md_open(struct inode *inode, struct file *file)
5042 * Succeed if we can lock the mddev, which confirms that
5043 * it isn't being stopped right now.
5045 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
5048 if ((err = mutex_lock_interruptible_nested(&mddev->reconfig_mutex, 1)))
5053 mddev_unlock(mddev);
5055 check_disk_change(inode->i_bdev);
5060 static int md_release(struct inode *inode, struct file * file)
5062 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
5070 static int md_media_changed(struct gendisk *disk)
5072 mddev_t *mddev = disk->private_data;
5074 return mddev->changed;
5077 static int md_revalidate(struct gendisk *disk)
5079 mddev_t *mddev = disk->private_data;
5084 static struct block_device_operations md_fops =
5086 .owner = THIS_MODULE,
5088 .release = md_release,
5090 .getgeo = md_getgeo,
5091 .media_changed = md_media_changed,
5092 .revalidate_disk= md_revalidate,
5095 static int md_thread(void * arg)
5097 mdk_thread_t *thread = arg;
5100 * md_thread is a 'system-thread', it's priority should be very
5101 * high. We avoid resource deadlocks individually in each
5102 * raid personality. (RAID5 does preallocation) We also use RR and
5103 * the very same RT priority as kswapd, thus we will never get
5104 * into a priority inversion deadlock.
5106 * we definitely have to have equal or higher priority than
5107 * bdflush, otherwise bdflush will deadlock if there are too
5108 * many dirty RAID5 blocks.
5111 allow_signal(SIGKILL);
5112 while (!kthread_should_stop()) {
5114 /* We need to wait INTERRUPTIBLE so that
5115 * we don't add to the load-average.
5116 * That means we need to be sure no signals are
5119 if (signal_pending(current))
5120 flush_signals(current);
5122 wait_event_interruptible_timeout
5124 test_bit(THREAD_WAKEUP, &thread->flags)
5125 || kthread_should_stop(),
5128 clear_bit(THREAD_WAKEUP, &thread->flags);
5130 thread->run(thread->mddev);
5136 void md_wakeup_thread(mdk_thread_t *thread)
5139 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5140 set_bit(THREAD_WAKEUP, &thread->flags);
5141 wake_up(&thread->wqueue);
5145 mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5148 mdk_thread_t *thread;
5150 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
5154 init_waitqueue_head(&thread->wqueue);
5157 thread->mddev = mddev;
5158 thread->timeout = MAX_SCHEDULE_TIMEOUT;
5159 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
5160 if (IS_ERR(thread->tsk)) {
5167 void md_unregister_thread(mdk_thread_t *thread)
5169 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
5171 kthread_stop(thread->tsk);
5175 void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
5182 if (!rdev || test_bit(Faulty, &rdev->flags))
5185 if (mddev->external)
5186 set_bit(Blocked, &rdev->flags);
5188 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5190 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5191 __builtin_return_address(0),__builtin_return_address(1),
5192 __builtin_return_address(2),__builtin_return_address(3));
5196 if (!mddev->pers->error_handler)
5198 mddev->pers->error_handler(mddev,rdev);
5199 if (mddev->degraded)
5200 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
5201 set_bit(StateChanged, &rdev->flags);
5202 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5203 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5204 md_wakeup_thread(mddev->thread);
5205 md_new_event_inintr(mddev);
5208 /* seq_file implementation /proc/mdstat */
5210 static void status_unused(struct seq_file *seq)
5214 struct list_head *tmp;
5216 seq_printf(seq, "unused devices: ");
5218 rdev_for_each_list(rdev, tmp, pending_raid_disks) {
5219 char b[BDEVNAME_SIZE];
5221 seq_printf(seq, "%s ",
5222 bdevname(rdev->bdev,b));
5225 seq_printf(seq, "<none>");
5227 seq_printf(seq, "\n");
5231 static void status_resync(struct seq_file *seq, mddev_t * mddev)
5233 sector_t max_blocks, resync, res;
5234 unsigned long dt, db, rt;
5236 unsigned int per_milli;
5238 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
5240 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
5241 max_blocks = mddev->resync_max_sectors >> 1;
5243 max_blocks = mddev->size;
5246 * Should not happen.
5252 /* Pick 'scale' such that (resync>>scale)*1000 will fit
5253 * in a sector_t, and (max_blocks>>scale) will fit in a
5254 * u32, as those are the requirements for sector_div.
5255 * Thus 'scale' must be at least 10
5258 if (sizeof(sector_t) > sizeof(unsigned long)) {
5259 while ( max_blocks/2 > (1ULL<<(scale+32)))
5262 res = (resync>>scale)*1000;
5263 sector_div(res, (u32)((max_blocks>>scale)+1));
5267 int i, x = per_milli/50, y = 20-x;
5268 seq_printf(seq, "[");
5269 for (i = 0; i < x; i++)
5270 seq_printf(seq, "=");
5271 seq_printf(seq, ">");
5272 for (i = 0; i < y; i++)
5273 seq_printf(seq, ".");
5274 seq_printf(seq, "] ");
5276 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
5277 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
5279 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
5281 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
5282 "resync" : "recovery"))),
5283 per_milli/10, per_milli % 10,
5284 (unsigned long long) resync,
5285 (unsigned long long) max_blocks);
5288 * We do not want to overflow, so the order of operands and
5289 * the * 100 / 100 trick are important. We do a +1 to be
5290 * safe against division by zero. We only estimate anyway.
5292 * dt: time from mark until now
5293 * db: blocks written from mark until now
5294 * rt: remaining time
5296 dt = ((jiffies - mddev->resync_mark) / HZ);
5298 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
5299 - mddev->resync_mark_cnt;
5300 rt = (dt * ((unsigned long)(max_blocks-resync) / (db/2/100+1)))/100;
5302 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
5304 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
5307 static void *md_seq_start(struct seq_file *seq, loff_t *pos)
5309 struct list_head *tmp;
5319 spin_lock(&all_mddevs_lock);
5320 list_for_each(tmp,&all_mddevs)
5322 mddev = list_entry(tmp, mddev_t, all_mddevs);
5324 spin_unlock(&all_mddevs_lock);
5327 spin_unlock(&all_mddevs_lock);
5329 return (void*)2;/* tail */
5333 static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
5335 struct list_head *tmp;
5336 mddev_t *next_mddev, *mddev = v;
5342 spin_lock(&all_mddevs_lock);
5344 tmp = all_mddevs.next;
5346 tmp = mddev->all_mddevs.next;
5347 if (tmp != &all_mddevs)
5348 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
5350 next_mddev = (void*)2;
5353 spin_unlock(&all_mddevs_lock);
5361 static void md_seq_stop(struct seq_file *seq, void *v)
5365 if (mddev && v != (void*)1 && v != (void*)2)
5369 struct mdstat_info {
5373 static int md_seq_show(struct seq_file *seq, void *v)
5377 struct list_head *tmp2;
5379 struct mdstat_info *mi = seq->private;
5380 struct bitmap *bitmap;
5382 if (v == (void*)1) {
5383 struct mdk_personality *pers;
5384 seq_printf(seq, "Personalities : ");
5385 spin_lock(&pers_lock);
5386 list_for_each_entry(pers, &pers_list, list)
5387 seq_printf(seq, "[%s] ", pers->name);
5389 spin_unlock(&pers_lock);
5390 seq_printf(seq, "\n");
5391 mi->event = atomic_read(&md_event_count);
5394 if (v == (void*)2) {
5399 if (mddev_lock(mddev) < 0)
5402 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
5403 seq_printf(seq, "%s : %sactive", mdname(mddev),
5404 mddev->pers ? "" : "in");
5407 seq_printf(seq, " (read-only)");
5409 seq_printf(seq, " (auto-read-only)");
5410 seq_printf(seq, " %s", mddev->pers->name);
5414 rdev_for_each(rdev, tmp2, mddev) {
5415 char b[BDEVNAME_SIZE];
5416 seq_printf(seq, " %s[%d]",
5417 bdevname(rdev->bdev,b), rdev->desc_nr);
5418 if (test_bit(WriteMostly, &rdev->flags))
5419 seq_printf(seq, "(W)");
5420 if (test_bit(Faulty, &rdev->flags)) {
5421 seq_printf(seq, "(F)");
5423 } else if (rdev->raid_disk < 0)
5424 seq_printf(seq, "(S)"); /* spare */
5428 if (!list_empty(&mddev->disks)) {
5430 seq_printf(seq, "\n %llu blocks",
5431 (unsigned long long)mddev->array_size);
5433 seq_printf(seq, "\n %llu blocks",
5434 (unsigned long long)size);
5436 if (mddev->persistent) {
5437 if (mddev->major_version != 0 ||
5438 mddev->minor_version != 90) {
5439 seq_printf(seq," super %d.%d",
5440 mddev->major_version,
5441 mddev->minor_version);
5443 } else if (mddev->external)
5444 seq_printf(seq, " super external:%s",
5445 mddev->metadata_type);
5447 seq_printf(seq, " super non-persistent");
5450 mddev->pers->status (seq, mddev);
5451 seq_printf(seq, "\n ");
5452 if (mddev->pers->sync_request) {
5453 if (mddev->curr_resync > 2) {
5454 status_resync (seq, mddev);
5455 seq_printf(seq, "\n ");
5456 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
5457 seq_printf(seq, "\tresync=DELAYED\n ");
5458 else if (mddev->recovery_cp < MaxSector)
5459 seq_printf(seq, "\tresync=PENDING\n ");
5462 seq_printf(seq, "\n ");
5464 if ((bitmap = mddev->bitmap)) {
5465 unsigned long chunk_kb;
5466 unsigned long flags;
5467 spin_lock_irqsave(&bitmap->lock, flags);
5468 chunk_kb = bitmap->chunksize >> 10;
5469 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
5471 bitmap->pages - bitmap->missing_pages,
5473 (bitmap->pages - bitmap->missing_pages)
5474 << (PAGE_SHIFT - 10),
5475 chunk_kb ? chunk_kb : bitmap->chunksize,
5476 chunk_kb ? "KB" : "B");
5478 seq_printf(seq, ", file: ");
5479 seq_path(seq, &bitmap->file->f_path, " \t\n");
5482 seq_printf(seq, "\n");
5483 spin_unlock_irqrestore(&bitmap->lock, flags);
5486 seq_printf(seq, "\n");
5488 mddev_unlock(mddev);
5493 static struct seq_operations md_seq_ops = {
5494 .start = md_seq_start,
5495 .next = md_seq_next,
5496 .stop = md_seq_stop,
5497 .show = md_seq_show,
5500 static int md_seq_open(struct inode *inode, struct file *file)
5503 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
5507 error = seq_open(file, &md_seq_ops);
5511 struct seq_file *p = file->private_data;
5513 mi->event = atomic_read(&md_event_count);
5518 static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
5520 struct seq_file *m = filp->private_data;
5521 struct mdstat_info *mi = m->private;
5524 poll_wait(filp, &md_event_waiters, wait);
5526 /* always allow read */
5527 mask = POLLIN | POLLRDNORM;
5529 if (mi->event != atomic_read(&md_event_count))
5530 mask |= POLLERR | POLLPRI;
5534 static const struct file_operations md_seq_fops = {
5535 .owner = THIS_MODULE,
5536 .open = md_seq_open,
5538 .llseek = seq_lseek,
5539 .release = seq_release_private,
5540 .poll = mdstat_poll,
5543 int register_md_personality(struct mdk_personality *p)
5545 spin_lock(&pers_lock);
5546 list_add_tail(&p->list, &pers_list);
5547 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
5548 spin_unlock(&pers_lock);
5552 int unregister_md_personality(struct mdk_personality *p)
5554 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
5555 spin_lock(&pers_lock);
5556 list_del_init(&p->list);
5557 spin_unlock(&pers_lock);
5561 static int is_mddev_idle(mddev_t *mddev)
5564 struct list_head *tmp;
5569 rdev_for_each(rdev, tmp, mddev) {
5570 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
5571 curr_events = disk_stat_read(disk, sectors[0]) +
5572 disk_stat_read(disk, sectors[1]) -
5573 atomic_read(&disk->sync_io);
5574 /* sync IO will cause sync_io to increase before the disk_stats
5575 * as sync_io is counted when a request starts, and
5576 * disk_stats is counted when it completes.
5577 * So resync activity will cause curr_events to be smaller than
5578 * when there was no such activity.
5579 * non-sync IO will cause disk_stat to increase without
5580 * increasing sync_io so curr_events will (eventually)
5581 * be larger than it was before. Once it becomes
5582 * substantially larger, the test below will cause
5583 * the array to appear non-idle, and resync will slow
5585 * If there is a lot of outstanding resync activity when
5586 * we set last_event to curr_events, then all that activity
5587 * completing might cause the array to appear non-idle
5588 * and resync will be slowed down even though there might
5589 * not have been non-resync activity. This will only
5590 * happen once though. 'last_events' will soon reflect
5591 * the state where there is little or no outstanding
5592 * resync requests, and further resync activity will
5593 * always make curr_events less than last_events.
5596 if (curr_events - rdev->last_events > 4096) {
5597 rdev->last_events = curr_events;
5604 void md_done_sync(mddev_t *mddev, int blocks, int ok)
5606 /* another "blocks" (512byte) blocks have been synced */
5607 atomic_sub(blocks, &mddev->recovery_active);
5608 wake_up(&mddev->recovery_wait);
5610 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5611 md_wakeup_thread(mddev->thread);
5612 // stop recovery, signal do_sync ....
5617 /* md_write_start(mddev, bi)
5618 * If we need to update some array metadata (e.g. 'active' flag
5619 * in superblock) before writing, schedule a superblock update
5620 * and wait for it to complete.
5622 void md_write_start(mddev_t *mddev, struct bio *bi)
5625 if (bio_data_dir(bi) != WRITE)
5628 BUG_ON(mddev->ro == 1);
5629 if (mddev->ro == 2) {
5630 /* need to switch to read/write */
5632 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5633 md_wakeup_thread(mddev->thread);
5634 md_wakeup_thread(mddev->sync_thread);
5637 atomic_inc(&mddev->writes_pending);
5638 if (mddev->safemode == 1)
5639 mddev->safemode = 0;
5640 if (mddev->in_sync) {
5641 spin_lock_irq(&mddev->write_lock);
5642 if (mddev->in_sync) {
5644 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
5645 md_wakeup_thread(mddev->thread);
5648 spin_unlock_irq(&mddev->write_lock);
5651 sysfs_notify(&mddev->kobj, NULL, "array_state");
5652 wait_event(mddev->sb_wait,
5653 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
5654 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
5657 void md_write_end(mddev_t *mddev)
5659 if (atomic_dec_and_test(&mddev->writes_pending)) {
5660 if (mddev->safemode == 2)
5661 md_wakeup_thread(mddev->thread);
5662 else if (mddev->safemode_delay)
5663 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
5667 /* md_allow_write(mddev)
5668 * Calling this ensures that the array is marked 'active' so that writes
5669 * may proceed without blocking. It is important to call this before
5670 * attempting a GFP_KERNEL allocation while holding the mddev lock.
5671 * Must be called with mddev_lock held.
5673 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
5674 * is dropped, so return -EAGAIN after notifying userspace.
5676 int md_allow_write(mddev_t *mddev)
5682 if (!mddev->pers->sync_request)
5685 spin_lock_irq(&mddev->write_lock);
5686 if (mddev->in_sync) {
5688 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
5689 if (mddev->safemode_delay &&
5690 mddev->safemode == 0)
5691 mddev->safemode = 1;
5692 spin_unlock_irq(&mddev->write_lock);
5693 md_update_sb(mddev, 0);
5694 sysfs_notify(&mddev->kobj, NULL, "array_state");
5696 spin_unlock_irq(&mddev->write_lock);
5698 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
5703 EXPORT_SYMBOL_GPL(md_allow_write);
5705 #define SYNC_MARKS 10
5706 #define SYNC_MARK_STEP (3*HZ)
5707 void md_do_sync(mddev_t *mddev)
5710 unsigned int currspeed = 0,
5712 sector_t max_sectors,j, io_sectors;
5713 unsigned long mark[SYNC_MARKS];
5714 sector_t mark_cnt[SYNC_MARKS];
5716 struct list_head *tmp;
5717 sector_t last_check;
5719 struct list_head *rtmp;
5723 /* just incase thread restarts... */
5724 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
5726 if (mddev->ro) /* never try to sync a read-only array */
5729 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5730 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
5731 desc = "data-check";
5732 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5733 desc = "requested-resync";
5736 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5741 /* we overload curr_resync somewhat here.
5742 * 0 == not engaged in resync at all
5743 * 2 == checking that there is no conflict with another sync
5744 * 1 == like 2, but have yielded to allow conflicting resync to
5746 * other == active in resync - this many blocks
5748 * Before starting a resync we must have set curr_resync to
5749 * 2, and then checked that every "conflicting" array has curr_resync
5750 * less than ours. When we find one that is the same or higher
5751 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
5752 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
5753 * This will mean we have to start checking from the beginning again.
5758 mddev->curr_resync = 2;
5761 if (kthread_should_stop()) {
5762 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5765 for_each_mddev(mddev2, tmp) {
5766 if (mddev2 == mddev)
5768 if (!mddev->parallel_resync
5769 && mddev2->curr_resync
5770 && match_mddev_units(mddev, mddev2)) {
5772 if (mddev < mddev2 && mddev->curr_resync == 2) {
5773 /* arbitrarily yield */
5774 mddev->curr_resync = 1;
5775 wake_up(&resync_wait);
5777 if (mddev > mddev2 && mddev->curr_resync == 1)
5778 /* no need to wait here, we can wait the next
5779 * time 'round when curr_resync == 2
5782 prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE);
5783 if (!kthread_should_stop() &&
5784 mddev2->curr_resync >= mddev->curr_resync) {
5785 printk(KERN_INFO "md: delaying %s of %s"
5786 " until %s has finished (they"
5787 " share one or more physical units)\n",
5788 desc, mdname(mddev), mdname(mddev2));
5791 finish_wait(&resync_wait, &wq);
5794 finish_wait(&resync_wait, &wq);
5797 } while (mddev->curr_resync < 2);
5800 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5801 /* resync follows the size requested by the personality,
5802 * which defaults to physical size, but can be virtual size
5804 max_sectors = mddev->resync_max_sectors;
5805 mddev->resync_mismatches = 0;
5806 /* we don't use the checkpoint if there's a bitmap */
5807 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5808 j = mddev->resync_min;
5809 else if (!mddev->bitmap)
5810 j = mddev->recovery_cp;
5812 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5813 max_sectors = mddev->size << 1;
5815 /* recovery follows the physical size of devices */
5816 max_sectors = mddev->size << 1;
5818 rdev_for_each(rdev, rtmp, mddev)
5819 if (rdev->raid_disk >= 0 &&
5820 !test_bit(Faulty, &rdev->flags) &&
5821 !test_bit(In_sync, &rdev->flags) &&
5822 rdev->recovery_offset < j)
5823 j = rdev->recovery_offset;
5826 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
5827 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
5828 " %d KB/sec/disk.\n", speed_min(mddev));
5829 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
5830 "(but not more than %d KB/sec) for %s.\n",
5831 speed_max(mddev), desc);
5833 is_mddev_idle(mddev); /* this also initializes IO event counters */
5836 for (m = 0; m < SYNC_MARKS; m++) {
5838 mark_cnt[m] = io_sectors;
5841 mddev->resync_mark = mark[last_mark];
5842 mddev->resync_mark_cnt = mark_cnt[last_mark];
5845 * Tune reconstruction:
5847 window = 32*(PAGE_SIZE/512);
5848 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
5849 window/2,(unsigned long long) max_sectors/2);
5851 atomic_set(&mddev->recovery_active, 0);
5856 "md: resuming %s of %s from checkpoint.\n",
5857 desc, mdname(mddev));
5858 mddev->curr_resync = j;
5861 while (j < max_sectors) {
5865 if (j >= mddev->resync_max) {
5866 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
5867 wait_event(mddev->recovery_wait,
5868 mddev->resync_max > j
5869 || kthread_should_stop());
5871 if (kthread_should_stop())
5873 sectors = mddev->pers->sync_request(mddev, j, &skipped,
5874 currspeed < speed_min(mddev));
5876 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5880 if (!skipped) { /* actual IO requested */
5881 io_sectors += sectors;
5882 atomic_add(sectors, &mddev->recovery_active);
5886 if (j>1) mddev->curr_resync = j;
5887 mddev->curr_mark_cnt = io_sectors;
5888 if (last_check == 0)
5889 /* this is the earliers that rebuilt will be
5890 * visible in /proc/mdstat
5892 md_new_event(mddev);
5894 if (last_check + window > io_sectors || j == max_sectors)
5897 last_check = io_sectors;
5899 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5903 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
5905 int next = (last_mark+1) % SYNC_MARKS;
5907 mddev->resync_mark = mark[next];
5908 mddev->resync_mark_cnt = mark_cnt[next];
5909 mark[next] = jiffies;
5910 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
5915 if (kthread_should_stop())
5920 * this loop exits only if either when we are slower than
5921 * the 'hard' speed limit, or the system was IO-idle for
5923 * the system might be non-idle CPU-wise, but we only care
5924 * about not overloading the IO subsystem. (things like an
5925 * e2fsck being done on the RAID array should execute fast)
5927 blk_unplug(mddev->queue);
5930 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
5931 /((jiffies-mddev->resync_mark)/HZ +1) +1;
5933 if (currspeed > speed_min(mddev)) {
5934 if ((currspeed > speed_max(mddev)) ||
5935 !is_mddev_idle(mddev)) {
5941 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
5943 * this also signals 'finished resyncing' to md_stop
5946 blk_unplug(mddev->queue);
5948 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
5950 /* tell personality that we are finished */
5951 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
5953 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
5954 mddev->curr_resync > 2) {
5955 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5956 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5957 if (mddev->curr_resync >= mddev->recovery_cp) {
5959 "md: checkpointing %s of %s.\n",
5960 desc, mdname(mddev));
5961 mddev->recovery_cp = mddev->curr_resync;
5964 mddev->recovery_cp = MaxSector;
5966 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5967 mddev->curr_resync = MaxSector;
5968 rdev_for_each(rdev, rtmp, mddev)
5969 if (rdev->raid_disk >= 0 &&
5970 !test_bit(Faulty, &rdev->flags) &&
5971 !test_bit(In_sync, &rdev->flags) &&
5972 rdev->recovery_offset < mddev->curr_resync)
5973 rdev->recovery_offset = mddev->curr_resync;
5976 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5979 mddev->curr_resync = 0;
5980 mddev->resync_min = 0;
5981 mddev->resync_max = MaxSector;
5982 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
5983 wake_up(&resync_wait);
5984 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
5985 md_wakeup_thread(mddev->thread);
5990 * got a signal, exit.
5993 "md: md_do_sync() got signal ... exiting\n");
5994 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5998 EXPORT_SYMBOL_GPL(md_do_sync);
6001 static int remove_and_add_spares(mddev_t *mddev)
6004 struct list_head *rtmp;
6007 rdev_for_each(rdev, rtmp, mddev)
6008 if (rdev->raid_disk >= 0 &&
6009 !test_bit(Blocked, &rdev->flags) &&
6010 (test_bit(Faulty, &rdev->flags) ||
6011 ! test_bit(In_sync, &rdev->flags)) &&
6012 atomic_read(&rdev->nr_pending)==0) {
6013 if (mddev->pers->hot_remove_disk(
6014 mddev, rdev->raid_disk)==0) {
6016 sprintf(nm,"rd%d", rdev->raid_disk);
6017 sysfs_remove_link(&mddev->kobj, nm);
6018 rdev->raid_disk = -1;
6022 if (mddev->degraded) {
6023 rdev_for_each(rdev, rtmp, mddev) {
6024 if (rdev->raid_disk >= 0 &&
6025 !test_bit(In_sync, &rdev->flags))
6027 if (rdev->raid_disk < 0
6028 && !test_bit(Faulty, &rdev->flags)) {
6029 rdev->recovery_offset = 0;
6031 hot_add_disk(mddev, rdev) == 0) {
6033 sprintf(nm, "rd%d", rdev->raid_disk);
6034 if (sysfs_create_link(&mddev->kobj,
6037 "md: cannot register "
6041 md_new_event(mddev);
6050 * This routine is regularly called by all per-raid-array threads to
6051 * deal with generic issues like resync and super-block update.
6052 * Raid personalities that don't have a thread (linear/raid0) do not
6053 * need this as they never do any recovery or update the superblock.
6055 * It does not do any resync itself, but rather "forks" off other threads
6056 * to do that as needed.
6057 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6058 * "->recovery" and create a thread at ->sync_thread.
6059 * When the thread finishes it sets MD_RECOVERY_DONE
6060 * and wakeups up this thread which will reap the thread and finish up.
6061 * This thread also removes any faulty devices (with nr_pending == 0).
6063 * The overall approach is:
6064 * 1/ if the superblock needs updating, update it.
6065 * 2/ If a recovery thread is running, don't do anything else.
6066 * 3/ If recovery has finished, clean up, possibly marking spares active.
6067 * 4/ If there are any faulty devices, remove them.
6068 * 5/ If array is degraded, try to add spares devices
6069 * 6/ If array has spares or is not in-sync, start a resync thread.
6071 void md_check_recovery(mddev_t *mddev)
6074 struct list_head *rtmp;
6078 bitmap_daemon_work(mddev->bitmap);
6083 if (signal_pending(current)) {
6084 if (mddev->pers->sync_request && !mddev->external) {
6085 printk(KERN_INFO "md: %s in immediate safe mode\n",
6087 mddev->safemode = 2;
6089 flush_signals(current);
6093 (mddev->flags && !mddev->external) ||
6094 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
6095 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
6096 (mddev->external == 0 && mddev->safemode == 1) ||
6097 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6098 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
6102 if (mddev_trylock(mddev)) {
6105 if (!mddev->external) {
6107 spin_lock_irq(&mddev->write_lock);
6108 if (mddev->safemode &&
6109 !atomic_read(&mddev->writes_pending) &&
6111 mddev->recovery_cp == MaxSector) {
6114 if (mddev->persistent)
6115 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6117 if (mddev->safemode == 1)
6118 mddev->safemode = 0;
6119 spin_unlock_irq(&mddev->write_lock);
6121 sysfs_notify(&mddev->kobj, NULL, "array_state");
6125 md_update_sb(mddev, 0);
6127 rdev_for_each(rdev, rtmp, mddev)
6128 if (test_and_clear_bit(StateChanged, &rdev->flags))
6129 sysfs_notify(&rdev->kobj, NULL, "state");
6132 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
6133 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
6134 /* resync/recovery still happening */
6135 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6138 if (mddev->sync_thread) {
6139 /* resync has finished, collect result */
6140 md_unregister_thread(mddev->sync_thread);
6141 mddev->sync_thread = NULL;
6142 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6144 /* activate any spares */
6145 if (mddev->pers->spare_active(mddev))
6146 sysfs_notify(&mddev->kobj, NULL,
6149 md_update_sb(mddev, 1);
6151 /* if array is no-longer degraded, then any saved_raid_disk
6152 * information must be scrapped
6154 if (!mddev->degraded)
6155 rdev_for_each(rdev, rtmp, mddev)
6156 rdev->saved_raid_disk = -1;
6158 mddev->recovery = 0;
6159 /* flag recovery needed just to double check */
6160 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6161 sysfs_notify(&mddev->kobj, NULL, "sync_action");
6162 md_new_event(mddev);
6165 /* Set RUNNING before clearing NEEDED to avoid
6166 * any transients in the value of "sync_action".
6168 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6169 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6170 /* Clear some bits that don't mean anything, but
6173 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
6174 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
6176 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
6178 /* no recovery is running.
6179 * remove any failed drives, then
6180 * add spares if possible.
6181 * Spare are also removed and re-added, to allow
6182 * the personality to fail the re-add.
6185 if (mddev->reshape_position != MaxSector) {
6186 if (mddev->pers->check_reshape(mddev) != 0)
6187 /* Cannot proceed */
6189 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
6190 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
6191 } else if ((spares = remove_and_add_spares(mddev))) {
6192 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6193 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
6194 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
6195 } else if (mddev->recovery_cp < MaxSector) {
6196 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6197 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
6198 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
6199 /* nothing to be done ... */
6202 if (mddev->pers->sync_request) {
6203 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
6204 /* We are adding a device or devices to an array
6205 * which has the bitmap stored on all devices.
6206 * So make sure all bitmap pages get written
6208 bitmap_write_all(mddev->bitmap);
6210 mddev->sync_thread = md_register_thread(md_do_sync,
6213 if (!mddev->sync_thread) {
6214 printk(KERN_ERR "%s: could not start resync"
6217 /* leave the spares where they are, it shouldn't hurt */
6218 mddev->recovery = 0;
6220 md_wakeup_thread(mddev->sync_thread);
6221 sysfs_notify(&mddev->kobj, NULL, "sync_action");
6222 md_new_event(mddev);
6225 if (!mddev->sync_thread) {
6226 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6227 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
6229 sysfs_notify(&mddev->kobj, NULL, "sync_action");
6231 mddev_unlock(mddev);
6235 void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
6237 sysfs_notify(&rdev->kobj, NULL, "state");
6238 wait_event_timeout(rdev->blocked_wait,
6239 !test_bit(Blocked, &rdev->flags),
6240 msecs_to_jiffies(5000));
6241 rdev_dec_pending(rdev, mddev);
6243 EXPORT_SYMBOL(md_wait_for_blocked_rdev);
6245 static int md_notify_reboot(struct notifier_block *this,
6246 unsigned long code, void *x)
6248 struct list_head *tmp;
6251 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
6253 printk(KERN_INFO "md: stopping all md devices.\n");
6255 for_each_mddev(mddev, tmp)
6256 if (mddev_trylock(mddev)) {
6257 do_md_stop (mddev, 1);
6258 mddev_unlock(mddev);
6261 * certain more exotic SCSI devices are known to be
6262 * volatile wrt too early system reboots. While the
6263 * right place to handle this issue is the given
6264 * driver, we do want to have a safe RAID driver ...
6271 static struct notifier_block md_notifier = {
6272 .notifier_call = md_notify_reboot,
6274 .priority = INT_MAX, /* before any real devices */
6277 static void md_geninit(void)
6279 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
6281 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
6284 static int __init md_init(void)
6286 if (register_blkdev(MAJOR_NR, "md"))
6288 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
6289 unregister_blkdev(MAJOR_NR, "md");
6292 blk_register_region(MKDEV(MAJOR_NR, 0), 1UL<<MINORBITS, THIS_MODULE,
6293 md_probe, NULL, NULL);
6294 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
6295 md_probe, NULL, NULL);
6297 register_reboot_notifier(&md_notifier);
6298 raid_table_header = register_sysctl_table(raid_root_table);
6308 * Searches all registered partitions for autorun RAID arrays
6312 static LIST_HEAD(all_detected_devices);
6313 struct detected_devices_node {
6314 struct list_head list;
6318 void md_autodetect_dev(dev_t dev)
6320 struct detected_devices_node *node_detected_dev;
6322 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
6323 if (node_detected_dev) {
6324 node_detected_dev->dev = dev;
6325 list_add_tail(&node_detected_dev->list, &all_detected_devices);
6327 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
6328 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
6333 static void autostart_arrays(int part)
6336 struct detected_devices_node *node_detected_dev;
6338 int i_scanned, i_passed;
6343 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
6345 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
6347 node_detected_dev = list_entry(all_detected_devices.next,
6348 struct detected_devices_node, list);
6349 list_del(&node_detected_dev->list);
6350 dev = node_detected_dev->dev;
6351 kfree(node_detected_dev);
6352 rdev = md_import_device(dev,0, 90);
6356 if (test_bit(Faulty, &rdev->flags)) {
6360 set_bit(AutoDetected, &rdev->flags);
6361 list_add(&rdev->same_set, &pending_raid_disks);
6365 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
6366 i_scanned, i_passed);
6368 autorun_devices(part);
6371 #endif /* !MODULE */
6373 static __exit void md_exit(void)
6376 struct list_head *tmp;
6378 blk_unregister_region(MKDEV(MAJOR_NR,0), 1U << MINORBITS);
6379 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
6381 unregister_blkdev(MAJOR_NR,"md");
6382 unregister_blkdev(mdp_major, "mdp");
6383 unregister_reboot_notifier(&md_notifier);
6384 unregister_sysctl_table(raid_table_header);
6385 remove_proc_entry("mdstat", NULL);
6386 for_each_mddev(mddev, tmp) {
6387 struct gendisk *disk = mddev->gendisk;
6390 export_array(mddev);
6393 mddev->gendisk = NULL;
6398 subsys_initcall(md_init);
6399 module_exit(md_exit)
6401 static int get_ro(char *buffer, struct kernel_param *kp)
6403 return sprintf(buffer, "%d", start_readonly);
6405 static int set_ro(const char *val, struct kernel_param *kp)
6408 int num = simple_strtoul(val, &e, 10);
6409 if (*val && (*e == '\0' || *e == '\n')) {
6410 start_readonly = num;
6416 module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
6417 module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
6420 EXPORT_SYMBOL(register_md_personality);
6421 EXPORT_SYMBOL(unregister_md_personality);
6422 EXPORT_SYMBOL(md_error);
6423 EXPORT_SYMBOL(md_done_sync);
6424 EXPORT_SYMBOL(md_write_start);
6425 EXPORT_SYMBOL(md_write_end);
6426 EXPORT_SYMBOL(md_register_thread);
6427 EXPORT_SYMBOL(md_unregister_thread);
6428 EXPORT_SYMBOL(md_wakeup_thread);
6429 EXPORT_SYMBOL(md_check_recovery);
6430 MODULE_LICENSE("GPL");
6432 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);