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/kthread.h>
36 #include <linux/raid/md.h>
37 #include <linux/raid/bitmap.h>
38 #include <linux/sysctl.h>
39 #include <linux/buffer_head.h> /* for invalidate_bdev */
40 #include <linux/poll.h>
41 #include <linux/ctype.h>
42 #include <linux/hdreg.h>
43 #include <linux/proc_fs.h>
44 #include <linux/random.h>
45 #include <linux/reboot.h>
46 #include <linux/file.h>
47 #include <linux/delay.h>
49 #define MAJOR_NR MD_MAJOR
51 /* 63 partitions with the alternate major number (mdp) */
52 #define MdpMinorShift 6
55 #define dprintk(x...) ((void)(DEBUG && printk(x)))
59 static void autostart_arrays(int part);
62 static LIST_HEAD(pers_list);
63 static DEFINE_SPINLOCK(pers_lock);
65 static void md_print_devices(void);
67 static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
69 #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
72 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
73 * is 1000 KB/sec, so the extra system load does not show up that much.
74 * Increase it if you want to have more _guaranteed_ speed. Note that
75 * the RAID driver will use the maximum available bandwidth if the IO
76 * subsystem is idle. There is also an 'absolute maximum' reconstruction
77 * speed limit - in case reconstruction slows down your system despite
80 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
81 * or /sys/block/mdX/md/sync_speed_{min,max}
84 static int sysctl_speed_limit_min = 1000;
85 static int sysctl_speed_limit_max = 200000;
86 static inline int speed_min(mddev_t *mddev)
88 return mddev->sync_speed_min ?
89 mddev->sync_speed_min : sysctl_speed_limit_min;
92 static inline int speed_max(mddev_t *mddev)
94 return mddev->sync_speed_max ?
95 mddev->sync_speed_max : sysctl_speed_limit_max;
98 static struct ctl_table_header *raid_table_header;
100 static ctl_table raid_table[] = {
102 .ctl_name = DEV_RAID_SPEED_LIMIT_MIN,
103 .procname = "speed_limit_min",
104 .data = &sysctl_speed_limit_min,
105 .maxlen = sizeof(int),
106 .mode = S_IRUGO|S_IWUSR,
107 .proc_handler = &proc_dointvec,
110 .ctl_name = DEV_RAID_SPEED_LIMIT_MAX,
111 .procname = "speed_limit_max",
112 .data = &sysctl_speed_limit_max,
113 .maxlen = sizeof(int),
114 .mode = S_IRUGO|S_IWUSR,
115 .proc_handler = &proc_dointvec,
120 static ctl_table raid_dir_table[] = {
122 .ctl_name = DEV_RAID,
125 .mode = S_IRUGO|S_IXUGO,
131 static ctl_table raid_root_table[] = {
137 .child = raid_dir_table,
142 static struct block_device_operations md_fops;
144 static int start_readonly;
147 * We have a system wide 'event count' that is incremented
148 * on any 'interesting' event, and readers of /proc/mdstat
149 * can use 'poll' or 'select' to find out when the event
153 * start array, stop array, error, add device, remove device,
154 * start build, activate spare
156 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
157 static atomic_t md_event_count;
158 void md_new_event(mddev_t *mddev)
160 atomic_inc(&md_event_count);
161 wake_up(&md_event_waiters);
163 EXPORT_SYMBOL_GPL(md_new_event);
165 /* Alternate version that can be called from interrupts
166 * when calling sysfs_notify isn't needed.
168 static void md_new_event_inintr(mddev_t *mddev)
170 atomic_inc(&md_event_count);
171 wake_up(&md_event_waiters);
175 * Enables to iterate over all existing md arrays
176 * all_mddevs_lock protects this list.
178 static LIST_HEAD(all_mddevs);
179 static DEFINE_SPINLOCK(all_mddevs_lock);
183 * iterates through all used mddevs in the system.
184 * We take care to grab the all_mddevs_lock whenever navigating
185 * the list, and to always hold a refcount when unlocked.
186 * Any code which breaks out of this loop while own
187 * a reference to the current mddev and must mddev_put it.
189 #define for_each_mddev(mddev,tmp) \
191 for (({ spin_lock(&all_mddevs_lock); \
192 tmp = all_mddevs.next; \
194 ({ if (tmp != &all_mddevs) \
195 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
196 spin_unlock(&all_mddevs_lock); \
197 if (mddev) mddev_put(mddev); \
198 mddev = list_entry(tmp, mddev_t, all_mddevs); \
199 tmp != &all_mddevs;}); \
200 ({ spin_lock(&all_mddevs_lock); \
205 static int md_fail_request(struct request_queue *q, struct bio *bio)
211 static inline mddev_t *mddev_get(mddev_t *mddev)
213 atomic_inc(&mddev->active);
217 static void mddev_put(mddev_t *mddev)
219 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
221 if (!mddev->raid_disks && list_empty(&mddev->disks)) {
222 list_del(&mddev->all_mddevs);
223 spin_unlock(&all_mddevs_lock);
224 blk_cleanup_queue(mddev->queue);
225 kobject_put(&mddev->kobj);
227 spin_unlock(&all_mddevs_lock);
230 static mddev_t * mddev_find(dev_t unit)
232 mddev_t *mddev, *new = NULL;
235 spin_lock(&all_mddevs_lock);
236 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
237 if (mddev->unit == unit) {
239 spin_unlock(&all_mddevs_lock);
245 list_add(&new->all_mddevs, &all_mddevs);
246 spin_unlock(&all_mddevs_lock);
249 spin_unlock(&all_mddevs_lock);
251 new = kzalloc(sizeof(*new), GFP_KERNEL);
256 if (MAJOR(unit) == MD_MAJOR)
257 new->md_minor = MINOR(unit);
259 new->md_minor = MINOR(unit) >> MdpMinorShift;
261 mutex_init(&new->reconfig_mutex);
262 INIT_LIST_HEAD(&new->disks);
263 INIT_LIST_HEAD(&new->all_mddevs);
264 init_timer(&new->safemode_timer);
265 atomic_set(&new->active, 1);
266 atomic_set(&new->openers, 0);
267 spin_lock_init(&new->write_lock);
268 init_waitqueue_head(&new->sb_wait);
269 init_waitqueue_head(&new->recovery_wait);
270 new->reshape_position = MaxSector;
272 new->resync_max = MaxSector;
273 new->level = LEVEL_NONE;
275 new->queue = blk_alloc_queue(GFP_KERNEL);
280 /* Can be unlocked because the queue is new: no concurrency */
281 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, new->queue);
283 blk_queue_make_request(new->queue, md_fail_request);
288 static inline int mddev_lock(mddev_t * mddev)
290 return mutex_lock_interruptible(&mddev->reconfig_mutex);
293 static inline int mddev_trylock(mddev_t * mddev)
295 return mutex_trylock(&mddev->reconfig_mutex);
298 static inline void mddev_unlock(mddev_t * mddev)
300 mutex_unlock(&mddev->reconfig_mutex);
302 md_wakeup_thread(mddev->thread);
305 static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
308 struct list_head *tmp;
310 rdev_for_each(rdev, tmp, mddev) {
311 if (rdev->desc_nr == nr)
317 static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
319 struct list_head *tmp;
322 rdev_for_each(rdev, tmp, mddev) {
323 if (rdev->bdev->bd_dev == dev)
329 static struct mdk_personality *find_pers(int level, char *clevel)
331 struct mdk_personality *pers;
332 list_for_each_entry(pers, &pers_list, list) {
333 if (level != LEVEL_NONE && pers->level == level)
335 if (strcmp(pers->name, clevel)==0)
341 /* return the offset of the super block in 512byte sectors */
342 static inline sector_t calc_dev_sboffset(struct block_device *bdev)
344 sector_t num_sectors = bdev->bd_inode->i_size / 512;
345 return MD_NEW_SIZE_SECTORS(num_sectors);
348 static sector_t calc_num_sectors(mdk_rdev_t *rdev, unsigned chunk_size)
350 sector_t num_sectors = rdev->sb_start;
353 num_sectors &= ~((sector_t)chunk_size/512 - 1);
357 static int alloc_disk_sb(mdk_rdev_t * rdev)
362 rdev->sb_page = alloc_page(GFP_KERNEL);
363 if (!rdev->sb_page) {
364 printk(KERN_ALERT "md: out of memory.\n");
371 static void free_disk_sb(mdk_rdev_t * rdev)
374 put_page(rdev->sb_page);
376 rdev->sb_page = NULL;
383 static void super_written(struct bio *bio, int error)
385 mdk_rdev_t *rdev = bio->bi_private;
386 mddev_t *mddev = rdev->mddev;
388 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
389 printk("md: super_written gets error=%d, uptodate=%d\n",
390 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
391 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
392 md_error(mddev, rdev);
395 if (atomic_dec_and_test(&mddev->pending_writes))
396 wake_up(&mddev->sb_wait);
400 static void super_written_barrier(struct bio *bio, int error)
402 struct bio *bio2 = bio->bi_private;
403 mdk_rdev_t *rdev = bio2->bi_private;
404 mddev_t *mddev = rdev->mddev;
406 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
407 error == -EOPNOTSUPP) {
409 /* barriers don't appear to be supported :-( */
410 set_bit(BarriersNotsupp, &rdev->flags);
411 mddev->barriers_work = 0;
412 spin_lock_irqsave(&mddev->write_lock, flags);
413 bio2->bi_next = mddev->biolist;
414 mddev->biolist = bio2;
415 spin_unlock_irqrestore(&mddev->write_lock, flags);
416 wake_up(&mddev->sb_wait);
420 bio->bi_private = rdev;
421 super_written(bio, error);
425 void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
426 sector_t sector, int size, struct page *page)
428 /* write first size bytes of page to sector of rdev
429 * Increment mddev->pending_writes before returning
430 * and decrement it on completion, waking up sb_wait
431 * if zero is reached.
432 * If an error occurred, call md_error
434 * As we might need to resubmit the request if BIO_RW_BARRIER
435 * causes ENOTSUPP, we allocate a spare bio...
437 struct bio *bio = bio_alloc(GFP_NOIO, 1);
438 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNC);
440 bio->bi_bdev = rdev->bdev;
441 bio->bi_sector = sector;
442 bio_add_page(bio, page, size, 0);
443 bio->bi_private = rdev;
444 bio->bi_end_io = super_written;
447 atomic_inc(&mddev->pending_writes);
448 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
450 rw |= (1<<BIO_RW_BARRIER);
451 rbio = bio_clone(bio, GFP_NOIO);
452 rbio->bi_private = bio;
453 rbio->bi_end_io = super_written_barrier;
454 submit_bio(rw, rbio);
459 void md_super_wait(mddev_t *mddev)
461 /* wait for all superblock writes that were scheduled to complete.
462 * if any had to be retried (due to BARRIER problems), retry them
466 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
467 if (atomic_read(&mddev->pending_writes)==0)
469 while (mddev->biolist) {
471 spin_lock_irq(&mddev->write_lock);
472 bio = mddev->biolist;
473 mddev->biolist = bio->bi_next ;
475 spin_unlock_irq(&mddev->write_lock);
476 submit_bio(bio->bi_rw, bio);
480 finish_wait(&mddev->sb_wait, &wq);
483 static void bi_complete(struct bio *bio, int error)
485 complete((struct completion*)bio->bi_private);
488 int sync_page_io(struct block_device *bdev, sector_t sector, int size,
489 struct page *page, int rw)
491 struct bio *bio = bio_alloc(GFP_NOIO, 1);
492 struct completion event;
495 rw |= (1 << BIO_RW_SYNC);
498 bio->bi_sector = sector;
499 bio_add_page(bio, page, size, 0);
500 init_completion(&event);
501 bio->bi_private = &event;
502 bio->bi_end_io = bi_complete;
504 wait_for_completion(&event);
506 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
510 EXPORT_SYMBOL_GPL(sync_page_io);
512 static int read_disk_sb(mdk_rdev_t * rdev, int size)
514 char b[BDEVNAME_SIZE];
515 if (!rdev->sb_page) {
523 if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ))
529 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
530 bdevname(rdev->bdev,b));
534 static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
536 return sb1->set_uuid0 == sb2->set_uuid0 &&
537 sb1->set_uuid1 == sb2->set_uuid1 &&
538 sb1->set_uuid2 == sb2->set_uuid2 &&
539 sb1->set_uuid3 == sb2->set_uuid3;
542 static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
545 mdp_super_t *tmp1, *tmp2;
547 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
548 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
550 if (!tmp1 || !tmp2) {
552 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
560 * nr_disks is not constant
565 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
573 static u32 md_csum_fold(u32 csum)
575 csum = (csum & 0xffff) + (csum >> 16);
576 return (csum & 0xffff) + (csum >> 16);
579 static unsigned int calc_sb_csum(mdp_super_t * sb)
582 u32 *sb32 = (u32*)sb;
584 unsigned int disk_csum, csum;
586 disk_csum = sb->sb_csum;
589 for (i = 0; i < MD_SB_BYTES/4 ; i++)
591 csum = (newcsum & 0xffffffff) + (newcsum>>32);
595 /* This used to use csum_partial, which was wrong for several
596 * reasons including that different results are returned on
597 * different architectures. It isn't critical that we get exactly
598 * the same return value as before (we always csum_fold before
599 * testing, and that removes any differences). However as we
600 * know that csum_partial always returned a 16bit value on
601 * alphas, do a fold to maximise conformity to previous behaviour.
603 sb->sb_csum = md_csum_fold(disk_csum);
605 sb->sb_csum = disk_csum;
612 * Handle superblock details.
613 * We want to be able to handle multiple superblock formats
614 * so we have a common interface to them all, and an array of
615 * different handlers.
616 * We rely on user-space to write the initial superblock, and support
617 * reading and updating of superblocks.
618 * Interface methods are:
619 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
620 * loads and validates a superblock on dev.
621 * if refdev != NULL, compare superblocks on both devices
623 * 0 - dev has a superblock that is compatible with refdev
624 * 1 - dev has a superblock that is compatible and newer than refdev
625 * so dev should be used as the refdev in future
626 * -EINVAL superblock incompatible or invalid
627 * -othererror e.g. -EIO
629 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
630 * Verify that dev is acceptable into mddev.
631 * The first time, mddev->raid_disks will be 0, and data from
632 * dev should be merged in. Subsequent calls check that dev
633 * is new enough. Return 0 or -EINVAL
635 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
636 * Update the superblock for rdev with data in mddev
637 * This does not write to disc.
643 struct module *owner;
644 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
646 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
647 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
648 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
649 sector_t num_sectors);
653 * load_super for 0.90.0
655 static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
657 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
662 * Calculate the position of the superblock (512byte sectors),
663 * it's at the end of the disk.
665 * It also happens to be a multiple of 4Kb.
667 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
669 ret = read_disk_sb(rdev, MD_SB_BYTES);
674 bdevname(rdev->bdev, b);
675 sb = (mdp_super_t*)page_address(rdev->sb_page);
677 if (sb->md_magic != MD_SB_MAGIC) {
678 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
683 if (sb->major_version != 0 ||
684 sb->minor_version < 90 ||
685 sb->minor_version > 91) {
686 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
687 sb->major_version, sb->minor_version,
692 if (sb->raid_disks <= 0)
695 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
696 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
701 rdev->preferred_minor = sb->md_minor;
702 rdev->data_offset = 0;
703 rdev->sb_size = MD_SB_BYTES;
705 if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) {
706 if (sb->level != 1 && sb->level != 4
707 && sb->level != 5 && sb->level != 6
708 && sb->level != 10) {
709 /* FIXME use a better test */
711 "md: bitmaps not supported for this level.\n");
716 if (sb->level == LEVEL_MULTIPATH)
719 rdev->desc_nr = sb->this_disk.number;
725 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
726 if (!uuid_equal(refsb, sb)) {
727 printk(KERN_WARNING "md: %s has different UUID to %s\n",
728 b, bdevname(refdev->bdev,b2));
731 if (!sb_equal(refsb, sb)) {
732 printk(KERN_WARNING "md: %s has same UUID"
733 " but different superblock to %s\n",
734 b, bdevname(refdev->bdev, b2));
738 ev2 = md_event(refsb);
744 rdev->size = calc_num_sectors(rdev, sb->chunk_size) / 2;
746 if (rdev->size < sb->size && sb->level > 1)
747 /* "this cannot possibly happen" ... */
755 * validate_super for 0.90.0
757 static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
760 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
761 __u64 ev1 = md_event(sb);
763 rdev->raid_disk = -1;
764 clear_bit(Faulty, &rdev->flags);
765 clear_bit(In_sync, &rdev->flags);
766 clear_bit(WriteMostly, &rdev->flags);
767 clear_bit(BarriersNotsupp, &rdev->flags);
769 if (mddev->raid_disks == 0) {
770 mddev->major_version = 0;
771 mddev->minor_version = sb->minor_version;
772 mddev->patch_version = sb->patch_version;
774 mddev->chunk_size = sb->chunk_size;
775 mddev->ctime = sb->ctime;
776 mddev->utime = sb->utime;
777 mddev->level = sb->level;
778 mddev->clevel[0] = 0;
779 mddev->layout = sb->layout;
780 mddev->raid_disks = sb->raid_disks;
781 mddev->size = sb->size;
783 mddev->bitmap_offset = 0;
784 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
786 if (mddev->minor_version >= 91) {
787 mddev->reshape_position = sb->reshape_position;
788 mddev->delta_disks = sb->delta_disks;
789 mddev->new_level = sb->new_level;
790 mddev->new_layout = sb->new_layout;
791 mddev->new_chunk = sb->new_chunk;
793 mddev->reshape_position = MaxSector;
794 mddev->delta_disks = 0;
795 mddev->new_level = mddev->level;
796 mddev->new_layout = mddev->layout;
797 mddev->new_chunk = mddev->chunk_size;
800 if (sb->state & (1<<MD_SB_CLEAN))
801 mddev->recovery_cp = MaxSector;
803 if (sb->events_hi == sb->cp_events_hi &&
804 sb->events_lo == sb->cp_events_lo) {
805 mddev->recovery_cp = sb->recovery_cp;
807 mddev->recovery_cp = 0;
810 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
811 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
812 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
813 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
815 mddev->max_disks = MD_SB_DISKS;
817 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
818 mddev->bitmap_file == NULL)
819 mddev->bitmap_offset = mddev->default_bitmap_offset;
821 } else if (mddev->pers == NULL) {
822 /* Insist on good event counter while assembling */
824 if (ev1 < mddev->events)
826 } else if (mddev->bitmap) {
827 /* if adding to array with a bitmap, then we can accept an
828 * older device ... but not too old.
830 if (ev1 < mddev->bitmap->events_cleared)
833 if (ev1 < mddev->events)
834 /* just a hot-add of a new device, leave raid_disk at -1 */
838 if (mddev->level != LEVEL_MULTIPATH) {
839 desc = sb->disks + rdev->desc_nr;
841 if (desc->state & (1<<MD_DISK_FAULTY))
842 set_bit(Faulty, &rdev->flags);
843 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
844 desc->raid_disk < mddev->raid_disks */) {
845 set_bit(In_sync, &rdev->flags);
846 rdev->raid_disk = desc->raid_disk;
848 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
849 set_bit(WriteMostly, &rdev->flags);
850 } else /* MULTIPATH are always insync */
851 set_bit(In_sync, &rdev->flags);
856 * sync_super for 0.90.0
858 static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
861 struct list_head *tmp;
863 int next_spare = mddev->raid_disks;
866 /* make rdev->sb match mddev data..
869 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
870 * 3/ any empty disks < next_spare become removed
872 * disks[0] gets initialised to REMOVED because
873 * we cannot be sure from other fields if it has
874 * been initialised or not.
877 int active=0, working=0,failed=0,spare=0,nr_disks=0;
879 rdev->sb_size = MD_SB_BYTES;
881 sb = (mdp_super_t*)page_address(rdev->sb_page);
883 memset(sb, 0, sizeof(*sb));
885 sb->md_magic = MD_SB_MAGIC;
886 sb->major_version = mddev->major_version;
887 sb->patch_version = mddev->patch_version;
888 sb->gvalid_words = 0; /* ignored */
889 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
890 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
891 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
892 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
894 sb->ctime = mddev->ctime;
895 sb->level = mddev->level;
896 sb->size = mddev->size;
897 sb->raid_disks = mddev->raid_disks;
898 sb->md_minor = mddev->md_minor;
899 sb->not_persistent = 0;
900 sb->utime = mddev->utime;
902 sb->events_hi = (mddev->events>>32);
903 sb->events_lo = (u32)mddev->events;
905 if (mddev->reshape_position == MaxSector)
906 sb->minor_version = 90;
908 sb->minor_version = 91;
909 sb->reshape_position = mddev->reshape_position;
910 sb->new_level = mddev->new_level;
911 sb->delta_disks = mddev->delta_disks;
912 sb->new_layout = mddev->new_layout;
913 sb->new_chunk = mddev->new_chunk;
915 mddev->minor_version = sb->minor_version;
918 sb->recovery_cp = mddev->recovery_cp;
919 sb->cp_events_hi = (mddev->events>>32);
920 sb->cp_events_lo = (u32)mddev->events;
921 if (mddev->recovery_cp == MaxSector)
922 sb->state = (1<< MD_SB_CLEAN);
926 sb->layout = mddev->layout;
927 sb->chunk_size = mddev->chunk_size;
929 if (mddev->bitmap && mddev->bitmap_file == NULL)
930 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
932 sb->disks[0].state = (1<<MD_DISK_REMOVED);
933 rdev_for_each(rdev2, tmp, mddev) {
936 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
937 && !test_bit(Faulty, &rdev2->flags))
938 desc_nr = rdev2->raid_disk;
940 desc_nr = next_spare++;
941 rdev2->desc_nr = desc_nr;
942 d = &sb->disks[rdev2->desc_nr];
944 d->number = rdev2->desc_nr;
945 d->major = MAJOR(rdev2->bdev->bd_dev);
946 d->minor = MINOR(rdev2->bdev->bd_dev);
947 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
948 && !test_bit(Faulty, &rdev2->flags))
949 d->raid_disk = rdev2->raid_disk;
951 d->raid_disk = rdev2->desc_nr; /* compatibility */
952 if (test_bit(Faulty, &rdev2->flags))
953 d->state = (1<<MD_DISK_FAULTY);
954 else if (test_bit(In_sync, &rdev2->flags)) {
955 d->state = (1<<MD_DISK_ACTIVE);
956 d->state |= (1<<MD_DISK_SYNC);
964 if (test_bit(WriteMostly, &rdev2->flags))
965 d->state |= (1<<MD_DISK_WRITEMOSTLY);
967 /* now set the "removed" and "faulty" bits on any missing devices */
968 for (i=0 ; i < mddev->raid_disks ; i++) {
969 mdp_disk_t *d = &sb->disks[i];
970 if (d->state == 0 && d->number == 0) {
973 d->state = (1<<MD_DISK_REMOVED);
974 d->state |= (1<<MD_DISK_FAULTY);
978 sb->nr_disks = nr_disks;
979 sb->active_disks = active;
980 sb->working_disks = working;
981 sb->failed_disks = failed;
982 sb->spare_disks = spare;
984 sb->this_disk = sb->disks[rdev->desc_nr];
985 sb->sb_csum = calc_sb_csum(sb);
989 * rdev_size_change for 0.90.0
991 static unsigned long long
992 super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
994 if (num_sectors && num_sectors < rdev->mddev->size * 2)
995 return 0; /* component must fit device */
996 if (rdev->mddev->bitmap_offset)
997 return 0; /* can't move bitmap */
998 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
999 if (!num_sectors || num_sectors > rdev->sb_start)
1000 num_sectors = rdev->sb_start;
1001 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1003 md_super_wait(rdev->mddev);
1004 return num_sectors / 2; /* kB for sysfs */
1009 * version 1 superblock
1012 static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
1016 unsigned long long newcsum;
1017 int size = 256 + le32_to_cpu(sb->max_dev)*2;
1018 __le32 *isuper = (__le32*)sb;
1021 disk_csum = sb->sb_csum;
1024 for (i=0; size>=4; size -= 4 )
1025 newcsum += le32_to_cpu(*isuper++);
1028 newcsum += le16_to_cpu(*(__le16*) isuper);
1030 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1031 sb->sb_csum = disk_csum;
1032 return cpu_to_le32(csum);
1035 static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1037 struct mdp_superblock_1 *sb;
1040 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1044 * Calculate the position of the superblock in 512byte sectors.
1045 * It is always aligned to a 4K boundary and
1046 * depeding on minor_version, it can be:
1047 * 0: At least 8K, but less than 12K, from end of device
1048 * 1: At start of device
1049 * 2: 4K from start of device.
1051 switch(minor_version) {
1053 sb_start = rdev->bdev->bd_inode->i_size >> 9;
1055 sb_start &= ~(sector_t)(4*2-1);
1066 rdev->sb_start = sb_start;
1068 /* superblock is rarely larger than 1K, but it can be larger,
1069 * and it is safe to read 4k, so we do that
1071 ret = read_disk_sb(rdev, 4096);
1072 if (ret) return ret;
1075 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1077 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1078 sb->major_version != cpu_to_le32(1) ||
1079 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1080 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
1081 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
1084 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1085 printk("md: invalid superblock checksum on %s\n",
1086 bdevname(rdev->bdev,b));
1089 if (le64_to_cpu(sb->data_size) < 10) {
1090 printk("md: data_size too small on %s\n",
1091 bdevname(rdev->bdev,b));
1094 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET)) {
1095 if (sb->level != cpu_to_le32(1) &&
1096 sb->level != cpu_to_le32(4) &&
1097 sb->level != cpu_to_le32(5) &&
1098 sb->level != cpu_to_le32(6) &&
1099 sb->level != cpu_to_le32(10)) {
1101 "md: bitmaps not supported for this level.\n");
1106 rdev->preferred_minor = 0xffff;
1107 rdev->data_offset = le64_to_cpu(sb->data_offset);
1108 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
1110 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
1111 bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
1112 if (rdev->sb_size & bmask)
1113 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1116 && rdev->data_offset < sb_start + (rdev->sb_size/512))
1119 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1122 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1128 struct mdp_superblock_1 *refsb =
1129 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1131 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1132 sb->level != refsb->level ||
1133 sb->layout != refsb->layout ||
1134 sb->chunksize != refsb->chunksize) {
1135 printk(KERN_WARNING "md: %s has strangely different"
1136 " superblock to %s\n",
1137 bdevname(rdev->bdev,b),
1138 bdevname(refdev->bdev,b2));
1141 ev1 = le64_to_cpu(sb->events);
1142 ev2 = le64_to_cpu(refsb->events);
1150 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
1152 rdev->size = rdev->sb_start / 2;
1153 if (rdev->size < le64_to_cpu(sb->data_size)/2)
1155 rdev->size = le64_to_cpu(sb->data_size)/2;
1156 if (le32_to_cpu(sb->chunksize))
1157 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
1159 if (le64_to_cpu(sb->size) > rdev->size*2)
1164 static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1166 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1167 __u64 ev1 = le64_to_cpu(sb->events);
1169 rdev->raid_disk = -1;
1170 clear_bit(Faulty, &rdev->flags);
1171 clear_bit(In_sync, &rdev->flags);
1172 clear_bit(WriteMostly, &rdev->flags);
1173 clear_bit(BarriersNotsupp, &rdev->flags);
1175 if (mddev->raid_disks == 0) {
1176 mddev->major_version = 1;
1177 mddev->patch_version = 0;
1178 mddev->external = 0;
1179 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
1180 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1181 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1182 mddev->level = le32_to_cpu(sb->level);
1183 mddev->clevel[0] = 0;
1184 mddev->layout = le32_to_cpu(sb->layout);
1185 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1186 mddev->size = le64_to_cpu(sb->size)/2;
1187 mddev->events = ev1;
1188 mddev->bitmap_offset = 0;
1189 mddev->default_bitmap_offset = 1024 >> 9;
1191 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1192 memcpy(mddev->uuid, sb->set_uuid, 16);
1194 mddev->max_disks = (4096-256)/2;
1196 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
1197 mddev->bitmap_file == NULL )
1198 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
1200 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1201 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1202 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1203 mddev->new_level = le32_to_cpu(sb->new_level);
1204 mddev->new_layout = le32_to_cpu(sb->new_layout);
1205 mddev->new_chunk = le32_to_cpu(sb->new_chunk)<<9;
1207 mddev->reshape_position = MaxSector;
1208 mddev->delta_disks = 0;
1209 mddev->new_level = mddev->level;
1210 mddev->new_layout = mddev->layout;
1211 mddev->new_chunk = mddev->chunk_size;
1214 } else if (mddev->pers == NULL) {
1215 /* Insist of good event counter while assembling */
1217 if (ev1 < mddev->events)
1219 } else if (mddev->bitmap) {
1220 /* If adding to array with a bitmap, then we can accept an
1221 * older device, but not too old.
1223 if (ev1 < mddev->bitmap->events_cleared)
1226 if (ev1 < mddev->events)
1227 /* just a hot-add of a new device, leave raid_disk at -1 */
1230 if (mddev->level != LEVEL_MULTIPATH) {
1232 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1234 case 0xffff: /* spare */
1236 case 0xfffe: /* faulty */
1237 set_bit(Faulty, &rdev->flags);
1240 if ((le32_to_cpu(sb->feature_map) &
1241 MD_FEATURE_RECOVERY_OFFSET))
1242 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1244 set_bit(In_sync, &rdev->flags);
1245 rdev->raid_disk = role;
1248 if (sb->devflags & WriteMostly1)
1249 set_bit(WriteMostly, &rdev->flags);
1250 } else /* MULTIPATH are always insync */
1251 set_bit(In_sync, &rdev->flags);
1256 static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1258 struct mdp_superblock_1 *sb;
1259 struct list_head *tmp;
1262 /* make rdev->sb match mddev and rdev data. */
1264 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1266 sb->feature_map = 0;
1268 sb->recovery_offset = cpu_to_le64(0);
1269 memset(sb->pad1, 0, sizeof(sb->pad1));
1270 memset(sb->pad2, 0, sizeof(sb->pad2));
1271 memset(sb->pad3, 0, sizeof(sb->pad3));
1273 sb->utime = cpu_to_le64((__u64)mddev->utime);
1274 sb->events = cpu_to_le64(mddev->events);
1276 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1278 sb->resync_offset = cpu_to_le64(0);
1280 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
1282 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
1283 sb->size = cpu_to_le64(mddev->size<<1);
1285 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1286 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
1287 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
1290 if (rdev->raid_disk >= 0 &&
1291 !test_bit(In_sync, &rdev->flags) &&
1292 rdev->recovery_offset > 0) {
1293 sb->feature_map |= cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1294 sb->recovery_offset = cpu_to_le64(rdev->recovery_offset);
1297 if (mddev->reshape_position != MaxSector) {
1298 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1299 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1300 sb->new_layout = cpu_to_le32(mddev->new_layout);
1301 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1302 sb->new_level = cpu_to_le32(mddev->new_level);
1303 sb->new_chunk = cpu_to_le32(mddev->new_chunk>>9);
1307 rdev_for_each(rdev2, tmp, mddev)
1308 if (rdev2->desc_nr+1 > max_dev)
1309 max_dev = rdev2->desc_nr+1;
1311 if (max_dev > le32_to_cpu(sb->max_dev))
1312 sb->max_dev = cpu_to_le32(max_dev);
1313 for (i=0; i<max_dev;i++)
1314 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1316 rdev_for_each(rdev2, tmp, mddev) {
1318 if (test_bit(Faulty, &rdev2->flags))
1319 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1320 else if (test_bit(In_sync, &rdev2->flags))
1321 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1322 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1323 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1325 sb->dev_roles[i] = cpu_to_le16(0xffff);
1328 sb->sb_csum = calc_sb_1_csum(sb);
1331 static unsigned long long
1332 super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
1334 struct mdp_superblock_1 *sb;
1335 sector_t max_sectors;
1336 if (num_sectors && num_sectors < rdev->mddev->size * 2)
1337 return 0; /* component must fit device */
1338 if (rdev->sb_start < rdev->data_offset) {
1339 /* minor versions 1 and 2; superblock before data */
1340 max_sectors = rdev->bdev->bd_inode->i_size >> 9;
1341 max_sectors -= rdev->data_offset;
1342 if (!num_sectors || num_sectors > max_sectors)
1343 num_sectors = max_sectors;
1344 } else if (rdev->mddev->bitmap_offset) {
1345 /* minor version 0 with bitmap we can't move */
1348 /* minor version 0; superblock after data */
1350 sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1351 sb_start &= ~(sector_t)(4*2 - 1);
1352 max_sectors = rdev->size * 2 + sb_start - rdev->sb_start;
1353 if (!num_sectors || num_sectors > max_sectors)
1354 num_sectors = max_sectors;
1355 rdev->sb_start = sb_start;
1357 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
1358 sb->data_size = cpu_to_le64(num_sectors);
1359 sb->super_offset = rdev->sb_start;
1360 sb->sb_csum = calc_sb_1_csum(sb);
1361 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1363 md_super_wait(rdev->mddev);
1364 return num_sectors / 2; /* kB for sysfs */
1367 static struct super_type super_types[] = {
1370 .owner = THIS_MODULE,
1371 .load_super = super_90_load,
1372 .validate_super = super_90_validate,
1373 .sync_super = super_90_sync,
1374 .rdev_size_change = super_90_rdev_size_change,
1378 .owner = THIS_MODULE,
1379 .load_super = super_1_load,
1380 .validate_super = super_1_validate,
1381 .sync_super = super_1_sync,
1382 .rdev_size_change = super_1_rdev_size_change,
1386 static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1388 mdk_rdev_t *rdev, *rdev2;
1391 rdev_for_each_rcu(rdev, mddev1)
1392 rdev_for_each_rcu(rdev2, mddev2)
1393 if (rdev->bdev->bd_contains ==
1394 rdev2->bdev->bd_contains) {
1402 static LIST_HEAD(pending_raid_disks);
1404 static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1406 char b[BDEVNAME_SIZE];
1416 /* prevent duplicates */
1417 if (find_rdev(mddev, rdev->bdev->bd_dev))
1420 /* make sure rdev->size exceeds mddev->size */
1421 if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) {
1423 /* Cannot change size, so fail
1424 * If mddev->level <= 0, then we don't care
1425 * about aligning sizes (e.g. linear)
1427 if (mddev->level > 0)
1430 mddev->size = rdev->size;
1433 /* Verify rdev->desc_nr is unique.
1434 * If it is -1, assign a free number, else
1435 * check number is not in use
1437 if (rdev->desc_nr < 0) {
1439 if (mddev->pers) choice = mddev->raid_disks;
1440 while (find_rdev_nr(mddev, choice))
1442 rdev->desc_nr = choice;
1444 if (find_rdev_nr(mddev, rdev->desc_nr))
1447 bdevname(rdev->bdev,b);
1448 while ( (s=strchr(b, '/')) != NULL)
1451 rdev->mddev = mddev;
1452 printk(KERN_INFO "md: bind<%s>\n", b);
1454 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
1457 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
1458 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1459 kobject_del(&rdev->kobj);
1462 list_add_rcu(&rdev->same_set, &mddev->disks);
1463 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
1467 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1472 static void md_delayed_delete(struct work_struct *ws)
1474 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1475 kobject_del(&rdev->kobj);
1476 kobject_put(&rdev->kobj);
1479 static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1481 char b[BDEVNAME_SIZE];
1486 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
1487 list_del_rcu(&rdev->same_set);
1488 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1490 sysfs_remove_link(&rdev->kobj, "block");
1492 /* We need to delay this, otherwise we can deadlock when
1493 * writing to 'remove' to "dev/state". We also need
1494 * to delay it due to rcu usage.
1497 INIT_WORK(&rdev->del_work, md_delayed_delete);
1498 kobject_get(&rdev->kobj);
1499 schedule_work(&rdev->del_work);
1503 * prevent the device from being mounted, repartitioned or
1504 * otherwise reused by a RAID array (or any other kernel
1505 * subsystem), by bd_claiming the device.
1507 static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
1510 struct block_device *bdev;
1511 char b[BDEVNAME_SIZE];
1513 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
1515 printk(KERN_ERR "md: could not open %s.\n",
1516 __bdevname(dev, b));
1517 return PTR_ERR(bdev);
1519 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
1521 printk(KERN_ERR "md: could not bd_claim %s.\n",
1527 set_bit(AllReserved, &rdev->flags);
1532 static void unlock_rdev(mdk_rdev_t *rdev)
1534 struct block_device *bdev = rdev->bdev;
1542 void md_autodetect_dev(dev_t dev);
1544 static void export_rdev(mdk_rdev_t * rdev)
1546 char b[BDEVNAME_SIZE];
1547 printk(KERN_INFO "md: export_rdev(%s)\n",
1548 bdevname(rdev->bdev,b));
1553 if (test_bit(AutoDetected, &rdev->flags))
1554 md_autodetect_dev(rdev->bdev->bd_dev);
1557 kobject_put(&rdev->kobj);
1560 static void kick_rdev_from_array(mdk_rdev_t * rdev)
1562 unbind_rdev_from_array(rdev);
1566 static void export_array(mddev_t *mddev)
1568 struct list_head *tmp;
1571 rdev_for_each(rdev, tmp, mddev) {
1576 kick_rdev_from_array(rdev);
1578 if (!list_empty(&mddev->disks))
1580 mddev->raid_disks = 0;
1581 mddev->major_version = 0;
1584 static void print_desc(mdp_disk_t *desc)
1586 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1587 desc->major,desc->minor,desc->raid_disk,desc->state);
1590 static void print_sb(mdp_super_t *sb)
1595 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1596 sb->major_version, sb->minor_version, sb->patch_version,
1597 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1599 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1600 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1601 sb->md_minor, sb->layout, sb->chunk_size);
1602 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1603 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1604 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1605 sb->failed_disks, sb->spare_disks,
1606 sb->sb_csum, (unsigned long)sb->events_lo);
1609 for (i = 0; i < MD_SB_DISKS; i++) {
1612 desc = sb->disks + i;
1613 if (desc->number || desc->major || desc->minor ||
1614 desc->raid_disk || (desc->state && (desc->state != 4))) {
1615 printk(" D %2d: ", i);
1619 printk(KERN_INFO "md: THIS: ");
1620 print_desc(&sb->this_disk);
1624 static void print_rdev(mdk_rdev_t *rdev)
1626 char b[BDEVNAME_SIZE];
1627 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1628 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
1629 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1631 if (rdev->sb_loaded) {
1632 printk(KERN_INFO "md: rdev superblock:\n");
1633 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1635 printk(KERN_INFO "md: no rdev superblock!\n");
1638 static void md_print_devices(void)
1640 struct list_head *tmp, *tmp2;
1643 char b[BDEVNAME_SIZE];
1646 printk("md: **********************************\n");
1647 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1648 printk("md: **********************************\n");
1649 for_each_mddev(mddev, tmp) {
1652 bitmap_print_sb(mddev->bitmap);
1654 printk("%s: ", mdname(mddev));
1655 rdev_for_each(rdev, tmp2, mddev)
1656 printk("<%s>", bdevname(rdev->bdev,b));
1659 rdev_for_each(rdev, tmp2, mddev)
1662 printk("md: **********************************\n");
1667 static void sync_sbs(mddev_t * mddev, int nospares)
1669 /* Update each superblock (in-memory image), but
1670 * if we are allowed to, skip spares which already
1671 * have the right event counter, or have one earlier
1672 * (which would mean they aren't being marked as dirty
1673 * with the rest of the array)
1676 struct list_head *tmp;
1678 rdev_for_each(rdev, tmp, mddev) {
1679 if (rdev->sb_events == mddev->events ||
1681 rdev->raid_disk < 0 &&
1682 (rdev->sb_events&1)==0 &&
1683 rdev->sb_events+1 == mddev->events)) {
1684 /* Don't update this superblock */
1685 rdev->sb_loaded = 2;
1687 super_types[mddev->major_version].
1688 sync_super(mddev, rdev);
1689 rdev->sb_loaded = 1;
1694 static void md_update_sb(mddev_t * mddev, int force_change)
1696 struct list_head *tmp;
1701 if (mddev->external)
1704 spin_lock_irq(&mddev->write_lock);
1706 set_bit(MD_CHANGE_PENDING, &mddev->flags);
1707 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
1709 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
1710 /* just a clean<-> dirty transition, possibly leave spares alone,
1711 * though if events isn't the right even/odd, we will have to do
1717 if (mddev->degraded)
1718 /* If the array is degraded, then skipping spares is both
1719 * dangerous and fairly pointless.
1720 * Dangerous because a device that was removed from the array
1721 * might have a event_count that still looks up-to-date,
1722 * so it can be re-added without a resync.
1723 * Pointless because if there are any spares to skip,
1724 * then a recovery will happen and soon that array won't
1725 * be degraded any more and the spare can go back to sleep then.
1729 sync_req = mddev->in_sync;
1730 mddev->utime = get_seconds();
1732 /* If this is just a dirty<->clean transition, and the array is clean
1733 * and 'events' is odd, we can roll back to the previous clean state */
1735 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
1736 && (mddev->events & 1)
1737 && mddev->events != 1)
1740 /* otherwise we have to go forward and ... */
1742 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
1743 /* .. if the array isn't clean, insist on an odd 'events' */
1744 if ((mddev->events&1)==0) {
1749 /* otherwise insist on an even 'events' (for clean states) */
1750 if ((mddev->events&1)) {
1757 if (!mddev->events) {
1759 * oops, this 64-bit counter should never wrap.
1760 * Either we are in around ~1 trillion A.C., assuming
1761 * 1 reboot per second, or we have a bug:
1768 * do not write anything to disk if using
1769 * nonpersistent superblocks
1771 if (!mddev->persistent) {
1772 if (!mddev->external)
1773 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
1775 spin_unlock_irq(&mddev->write_lock);
1776 wake_up(&mddev->sb_wait);
1779 sync_sbs(mddev, nospares);
1780 spin_unlock_irq(&mddev->write_lock);
1783 "md: updating %s RAID superblock on device (in sync %d)\n",
1784 mdname(mddev),mddev->in_sync);
1786 bitmap_update_sb(mddev->bitmap);
1787 rdev_for_each(rdev, tmp, mddev) {
1788 char b[BDEVNAME_SIZE];
1789 dprintk(KERN_INFO "md: ");
1790 if (rdev->sb_loaded != 1)
1791 continue; /* no noise on spare devices */
1792 if (test_bit(Faulty, &rdev->flags))
1793 dprintk("(skipping faulty ");
1795 dprintk("%s ", bdevname(rdev->bdev,b));
1796 if (!test_bit(Faulty, &rdev->flags)) {
1797 md_super_write(mddev,rdev,
1798 rdev->sb_start, rdev->sb_size,
1800 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1801 bdevname(rdev->bdev,b),
1802 (unsigned long long)rdev->sb_start);
1803 rdev->sb_events = mddev->events;
1807 if (mddev->level == LEVEL_MULTIPATH)
1808 /* only need to write one superblock... */
1811 md_super_wait(mddev);
1812 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
1814 spin_lock_irq(&mddev->write_lock);
1815 if (mddev->in_sync != sync_req ||
1816 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
1817 /* have to write it out again */
1818 spin_unlock_irq(&mddev->write_lock);
1821 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
1822 spin_unlock_irq(&mddev->write_lock);
1823 wake_up(&mddev->sb_wait);
1827 /* words written to sysfs files may, or may not, be \n terminated.
1828 * We want to accept with case. For this we use cmd_match.
1830 static int cmd_match(const char *cmd, const char *str)
1832 /* See if cmd, written into a sysfs file, matches
1833 * str. They must either be the same, or cmd can
1834 * have a trailing newline
1836 while (*cmd && *str && *cmd == *str) {
1847 struct rdev_sysfs_entry {
1848 struct attribute attr;
1849 ssize_t (*show)(mdk_rdev_t *, char *);
1850 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1854 state_show(mdk_rdev_t *rdev, char *page)
1859 if (test_bit(Faulty, &rdev->flags)) {
1860 len+= sprintf(page+len, "%sfaulty",sep);
1863 if (test_bit(In_sync, &rdev->flags)) {
1864 len += sprintf(page+len, "%sin_sync",sep);
1867 if (test_bit(WriteMostly, &rdev->flags)) {
1868 len += sprintf(page+len, "%swrite_mostly",sep);
1871 if (test_bit(Blocked, &rdev->flags)) {
1872 len += sprintf(page+len, "%sblocked", sep);
1875 if (!test_bit(Faulty, &rdev->flags) &&
1876 !test_bit(In_sync, &rdev->flags)) {
1877 len += sprintf(page+len, "%sspare", sep);
1880 return len+sprintf(page+len, "\n");
1884 state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1887 * faulty - simulates and error
1888 * remove - disconnects the device
1889 * writemostly - sets write_mostly
1890 * -writemostly - clears write_mostly
1891 * blocked - sets the Blocked flag
1892 * -blocked - clears the Blocked flag
1895 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
1896 md_error(rdev->mddev, rdev);
1898 } else if (cmd_match(buf, "remove")) {
1899 if (rdev->raid_disk >= 0)
1902 mddev_t *mddev = rdev->mddev;
1903 kick_rdev_from_array(rdev);
1905 md_update_sb(mddev, 1);
1906 md_new_event(mddev);
1909 } else if (cmd_match(buf, "writemostly")) {
1910 set_bit(WriteMostly, &rdev->flags);
1912 } else if (cmd_match(buf, "-writemostly")) {
1913 clear_bit(WriteMostly, &rdev->flags);
1915 } else if (cmd_match(buf, "blocked")) {
1916 set_bit(Blocked, &rdev->flags);
1918 } else if (cmd_match(buf, "-blocked")) {
1919 clear_bit(Blocked, &rdev->flags);
1920 wake_up(&rdev->blocked_wait);
1921 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
1922 md_wakeup_thread(rdev->mddev->thread);
1927 sysfs_notify(&rdev->kobj, NULL, "state");
1928 return err ? err : len;
1930 static struct rdev_sysfs_entry rdev_state =
1931 __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
1934 errors_show(mdk_rdev_t *rdev, char *page)
1936 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
1940 errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1943 unsigned long n = simple_strtoul(buf, &e, 10);
1944 if (*buf && (*e == 0 || *e == '\n')) {
1945 atomic_set(&rdev->corrected_errors, n);
1950 static struct rdev_sysfs_entry rdev_errors =
1951 __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
1954 slot_show(mdk_rdev_t *rdev, char *page)
1956 if (rdev->raid_disk < 0)
1957 return sprintf(page, "none\n");
1959 return sprintf(page, "%d\n", rdev->raid_disk);
1963 slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1968 int slot = simple_strtoul(buf, &e, 10);
1969 if (strncmp(buf, "none", 4)==0)
1971 else if (e==buf || (*e && *e!= '\n'))
1973 if (rdev->mddev->pers && slot == -1) {
1974 /* Setting 'slot' on an active array requires also
1975 * updating the 'rd%d' link, and communicating
1976 * with the personality with ->hot_*_disk.
1977 * For now we only support removing
1978 * failed/spare devices. This normally happens automatically,
1979 * but not when the metadata is externally managed.
1981 if (rdev->raid_disk == -1)
1983 /* personality does all needed checks */
1984 if (rdev->mddev->pers->hot_add_disk == NULL)
1986 err = rdev->mddev->pers->
1987 hot_remove_disk(rdev->mddev, rdev->raid_disk);
1990 sprintf(nm, "rd%d", rdev->raid_disk);
1991 sysfs_remove_link(&rdev->mddev->kobj, nm);
1992 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
1993 md_wakeup_thread(rdev->mddev->thread);
1994 } else if (rdev->mddev->pers) {
1996 struct list_head *tmp;
1997 /* Activating a spare .. or possibly reactivating
1998 * if we every get bitmaps working here.
2001 if (rdev->raid_disk != -1)
2004 if (rdev->mddev->pers->hot_add_disk == NULL)
2007 rdev_for_each(rdev2, tmp, rdev->mddev)
2008 if (rdev2->raid_disk == slot)
2011 rdev->raid_disk = slot;
2012 if (test_bit(In_sync, &rdev->flags))
2013 rdev->saved_raid_disk = slot;
2015 rdev->saved_raid_disk = -1;
2016 err = rdev->mddev->pers->
2017 hot_add_disk(rdev->mddev, rdev);
2019 rdev->raid_disk = -1;
2022 sysfs_notify(&rdev->kobj, NULL, "state");
2023 sprintf(nm, "rd%d", rdev->raid_disk);
2024 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2026 "md: cannot register "
2028 nm, mdname(rdev->mddev));
2030 /* don't wakeup anyone, leave that to userspace. */
2032 if (slot >= rdev->mddev->raid_disks)
2034 rdev->raid_disk = slot;
2035 /* assume it is working */
2036 clear_bit(Faulty, &rdev->flags);
2037 clear_bit(WriteMostly, &rdev->flags);
2038 set_bit(In_sync, &rdev->flags);
2039 sysfs_notify(&rdev->kobj, NULL, "state");
2045 static struct rdev_sysfs_entry rdev_slot =
2046 __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
2049 offset_show(mdk_rdev_t *rdev, char *page)
2051 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
2055 offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2058 unsigned long long offset = simple_strtoull(buf, &e, 10);
2059 if (e==buf || (*e && *e != '\n'))
2061 if (rdev->mddev->pers && rdev->raid_disk >= 0)
2063 if (rdev->size && rdev->mddev->external)
2064 /* Must set offset before size, so overlap checks
2067 rdev->data_offset = offset;
2071 static struct rdev_sysfs_entry rdev_offset =
2072 __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
2075 rdev_size_show(mdk_rdev_t *rdev, char *page)
2077 return sprintf(page, "%llu\n", (unsigned long long)rdev->size);
2080 static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2082 /* check if two start/length pairs overlap */
2091 rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2093 unsigned long long size;
2094 unsigned long long oldsize = rdev->size;
2095 mddev_t *my_mddev = rdev->mddev;
2097 if (strict_strtoull(buf, 10, &size) < 0)
2099 if (my_mddev->pers && rdev->raid_disk >= 0) {
2100 if (my_mddev->persistent) {
2101 size = super_types[my_mddev->major_version].
2102 rdev_size_change(rdev, size * 2);
2106 size = (rdev->bdev->bd_inode->i_size >> 10);
2107 size -= rdev->data_offset/2;
2110 if (size < my_mddev->size)
2111 return -EINVAL; /* component must fit device */
2114 if (size > oldsize && my_mddev->external) {
2115 /* need to check that all other rdevs with the same ->bdev
2116 * do not overlap. We need to unlock the mddev to avoid
2117 * a deadlock. We have already changed rdev->size, and if
2118 * we have to change it back, we will have the lock again.
2122 struct list_head *tmp, *tmp2;
2124 mddev_unlock(my_mddev);
2125 for_each_mddev(mddev, tmp) {
2129 rdev_for_each(rdev2, tmp2, mddev)
2130 if (test_bit(AllReserved, &rdev2->flags) ||
2131 (rdev->bdev == rdev2->bdev &&
2133 overlaps(rdev->data_offset, rdev->size * 2,
2135 rdev2->size * 2))) {
2139 mddev_unlock(mddev);
2145 mddev_lock(my_mddev);
2147 /* Someone else could have slipped in a size
2148 * change here, but doing so is just silly.
2149 * We put oldsize back because we *know* it is
2150 * safe, and trust userspace not to race with
2153 rdev->size = oldsize;
2160 static struct rdev_sysfs_entry rdev_size =
2161 __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
2163 static struct attribute *rdev_default_attrs[] = {
2172 rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2174 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2175 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
2176 mddev_t *mddev = rdev->mddev;
2182 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2184 if (rdev->mddev == NULL)
2187 rv = entry->show(rdev, page);
2188 mddev_unlock(mddev);
2194 rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2195 const char *page, size_t length)
2197 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2198 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
2200 mddev_t *mddev = rdev->mddev;
2204 if (!capable(CAP_SYS_ADMIN))
2206 rv = mddev ? mddev_lock(mddev): -EBUSY;
2208 if (rdev->mddev == NULL)
2211 rv = entry->store(rdev, page, length);
2212 mddev_unlock(mddev);
2217 static void rdev_free(struct kobject *ko)
2219 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2222 static struct sysfs_ops rdev_sysfs_ops = {
2223 .show = rdev_attr_show,
2224 .store = rdev_attr_store,
2226 static struct kobj_type rdev_ktype = {
2227 .release = rdev_free,
2228 .sysfs_ops = &rdev_sysfs_ops,
2229 .default_attrs = rdev_default_attrs,
2233 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2235 * mark the device faulty if:
2237 * - the device is nonexistent (zero size)
2238 * - the device has no valid superblock
2240 * a faulty rdev _never_ has rdev->sb set.
2242 static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2244 char b[BDEVNAME_SIZE];
2249 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
2251 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2252 return ERR_PTR(-ENOMEM);
2255 if ((err = alloc_disk_sb(rdev)))
2258 err = lock_rdev(rdev, newdev, super_format == -2);
2262 kobject_init(&rdev->kobj, &rdev_ktype);
2265 rdev->saved_raid_disk = -1;
2266 rdev->raid_disk = -1;
2268 rdev->data_offset = 0;
2269 rdev->sb_events = 0;
2270 atomic_set(&rdev->nr_pending, 0);
2271 atomic_set(&rdev->read_errors, 0);
2272 atomic_set(&rdev->corrected_errors, 0);
2274 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2277 "md: %s has zero or unknown size, marking faulty!\n",
2278 bdevname(rdev->bdev,b));
2283 if (super_format >= 0) {
2284 err = super_types[super_format].
2285 load_super(rdev, NULL, super_minor);
2286 if (err == -EINVAL) {
2288 "md: %s does not have a valid v%d.%d "
2289 "superblock, not importing!\n",
2290 bdevname(rdev->bdev,b),
2291 super_format, super_minor);
2296 "md: could not read %s's sb, not importing!\n",
2297 bdevname(rdev->bdev,b));
2302 INIT_LIST_HEAD(&rdev->same_set);
2303 init_waitqueue_head(&rdev->blocked_wait);
2308 if (rdev->sb_page) {
2314 return ERR_PTR(err);
2318 * Check a full RAID array for plausibility
2322 static void analyze_sbs(mddev_t * mddev)
2325 struct list_head *tmp;
2326 mdk_rdev_t *rdev, *freshest;
2327 char b[BDEVNAME_SIZE];
2330 rdev_for_each(rdev, tmp, mddev)
2331 switch (super_types[mddev->major_version].
2332 load_super(rdev, freshest, mddev->minor_version)) {
2340 "md: fatal superblock inconsistency in %s"
2341 " -- removing from array\n",
2342 bdevname(rdev->bdev,b));
2343 kick_rdev_from_array(rdev);
2347 super_types[mddev->major_version].
2348 validate_super(mddev, freshest);
2351 rdev_for_each(rdev, tmp, mddev) {
2352 if (rdev != freshest)
2353 if (super_types[mddev->major_version].
2354 validate_super(mddev, rdev)) {
2355 printk(KERN_WARNING "md: kicking non-fresh %s"
2357 bdevname(rdev->bdev,b));
2358 kick_rdev_from_array(rdev);
2361 if (mddev->level == LEVEL_MULTIPATH) {
2362 rdev->desc_nr = i++;
2363 rdev->raid_disk = rdev->desc_nr;
2364 set_bit(In_sync, &rdev->flags);
2365 } else if (rdev->raid_disk >= mddev->raid_disks) {
2366 rdev->raid_disk = -1;
2367 clear_bit(In_sync, &rdev->flags);
2373 if (mddev->recovery_cp != MaxSector &&
2375 printk(KERN_ERR "md: %s: raid array is not clean"
2376 " -- starting background reconstruction\n",
2381 static void md_safemode_timeout(unsigned long data);
2384 safe_delay_show(mddev_t *mddev, char *page)
2386 int msec = (mddev->safemode_delay*1000)/HZ;
2387 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2390 safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2398 /* remove a period, and count digits after it */
2399 if (len >= sizeof(buf))
2401 strlcpy(buf, cbuf, sizeof(buf));
2402 for (i=0; i<len; i++) {
2404 if (isdigit(buf[i])) {
2409 } else if (buf[i] == '.') {
2414 if (strict_strtoul(buf, 10, &msec) < 0)
2416 msec = (msec * 1000) / scale;
2418 mddev->safemode_delay = 0;
2420 unsigned long old_delay = mddev->safemode_delay;
2421 mddev->safemode_delay = (msec*HZ)/1000;
2422 if (mddev->safemode_delay == 0)
2423 mddev->safemode_delay = 1;
2424 if (mddev->safemode_delay < old_delay)
2425 md_safemode_timeout((unsigned long)mddev);
2429 static struct md_sysfs_entry md_safe_delay =
2430 __ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
2433 level_show(mddev_t *mddev, char *page)
2435 struct mdk_personality *p = mddev->pers;
2437 return sprintf(page, "%s\n", p->name);
2438 else if (mddev->clevel[0])
2439 return sprintf(page, "%s\n", mddev->clevel);
2440 else if (mddev->level != LEVEL_NONE)
2441 return sprintf(page, "%d\n", mddev->level);
2447 level_store(mddev_t *mddev, const char *buf, size_t len)
2454 if (len >= sizeof(mddev->clevel))
2456 strncpy(mddev->clevel, buf, len);
2457 if (mddev->clevel[len-1] == '\n')
2459 mddev->clevel[len] = 0;
2460 mddev->level = LEVEL_NONE;
2464 static struct md_sysfs_entry md_level =
2465 __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
2469 layout_show(mddev_t *mddev, char *page)
2471 /* just a number, not meaningful for all levels */
2472 if (mddev->reshape_position != MaxSector &&
2473 mddev->layout != mddev->new_layout)
2474 return sprintf(page, "%d (%d)\n",
2475 mddev->new_layout, mddev->layout);
2476 return sprintf(page, "%d\n", mddev->layout);
2480 layout_store(mddev_t *mddev, const char *buf, size_t len)
2483 unsigned long n = simple_strtoul(buf, &e, 10);
2485 if (!*buf || (*e && *e != '\n'))
2490 if (mddev->reshape_position != MaxSector)
2491 mddev->new_layout = n;
2496 static struct md_sysfs_entry md_layout =
2497 __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
2501 raid_disks_show(mddev_t *mddev, char *page)
2503 if (mddev->raid_disks == 0)
2505 if (mddev->reshape_position != MaxSector &&
2506 mddev->delta_disks != 0)
2507 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
2508 mddev->raid_disks - mddev->delta_disks);
2509 return sprintf(page, "%d\n", mddev->raid_disks);
2512 static int update_raid_disks(mddev_t *mddev, int raid_disks);
2515 raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2519 unsigned long n = simple_strtoul(buf, &e, 10);
2521 if (!*buf || (*e && *e != '\n'))
2525 rv = update_raid_disks(mddev, n);
2526 else if (mddev->reshape_position != MaxSector) {
2527 int olddisks = mddev->raid_disks - mddev->delta_disks;
2528 mddev->delta_disks = n - olddisks;
2529 mddev->raid_disks = n;
2531 mddev->raid_disks = n;
2532 return rv ? rv : len;
2534 static struct md_sysfs_entry md_raid_disks =
2535 __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
2538 chunk_size_show(mddev_t *mddev, char *page)
2540 if (mddev->reshape_position != MaxSector &&
2541 mddev->chunk_size != mddev->new_chunk)
2542 return sprintf(page, "%d (%d)\n", mddev->new_chunk,
2544 return sprintf(page, "%d\n", mddev->chunk_size);
2548 chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2550 /* can only set chunk_size if array is not yet active */
2552 unsigned long n = simple_strtoul(buf, &e, 10);
2554 if (!*buf || (*e && *e != '\n'))
2559 else if (mddev->reshape_position != MaxSector)
2560 mddev->new_chunk = n;
2562 mddev->chunk_size = n;
2565 static struct md_sysfs_entry md_chunk_size =
2566 __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
2569 resync_start_show(mddev_t *mddev, char *page)
2571 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2575 resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2578 unsigned long long n = simple_strtoull(buf, &e, 10);
2582 if (!*buf || (*e && *e != '\n'))
2585 mddev->recovery_cp = n;
2588 static struct md_sysfs_entry md_resync_start =
2589 __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
2592 * The array state can be:
2595 * No devices, no size, no level
2596 * Equivalent to STOP_ARRAY ioctl
2598 * May have some settings, but array is not active
2599 * all IO results in error
2600 * When written, doesn't tear down array, but just stops it
2601 * suspended (not supported yet)
2602 * All IO requests will block. The array can be reconfigured.
2603 * Writing this, if accepted, will block until array is quiescent
2605 * no resync can happen. no superblocks get written.
2606 * write requests fail
2608 * like readonly, but behaves like 'clean' on a write request.
2610 * clean - no pending writes, but otherwise active.
2611 * When written to inactive array, starts without resync
2612 * If a write request arrives then
2613 * if metadata is known, mark 'dirty' and switch to 'active'.
2614 * if not known, block and switch to write-pending
2615 * If written to an active array that has pending writes, then fails.
2617 * fully active: IO and resync can be happening.
2618 * When written to inactive array, starts with resync
2621 * clean, but writes are blocked waiting for 'active' to be written.
2624 * like active, but no writes have been seen for a while (100msec).
2627 enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
2628 write_pending, active_idle, bad_word};
2629 static char *array_states[] = {
2630 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2631 "write-pending", "active-idle", NULL };
2633 static int match_word(const char *word, char **list)
2636 for (n=0; list[n]; n++)
2637 if (cmd_match(word, list[n]))
2643 array_state_show(mddev_t *mddev, char *page)
2645 enum array_state st = inactive;
2658 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
2660 else if (mddev->safemode)
2666 if (list_empty(&mddev->disks) &&
2667 mddev->raid_disks == 0 &&
2673 return sprintf(page, "%s\n", array_states[st]);
2676 static int do_md_stop(mddev_t * mddev, int ro, int is_open);
2677 static int do_md_run(mddev_t * mddev);
2678 static int restart_array(mddev_t *mddev);
2681 array_state_store(mddev_t *mddev, const char *buf, size_t len)
2684 enum array_state st = match_word(buf, array_states);
2689 /* stopping an active array */
2690 if (atomic_read(&mddev->openers) > 0)
2692 err = do_md_stop(mddev, 0, 0);
2695 /* stopping an active array */
2697 if (atomic_read(&mddev->openers) > 0)
2699 err = do_md_stop(mddev, 2, 0);
2701 err = 0; /* already inactive */
2704 break; /* not supported yet */
2707 err = do_md_stop(mddev, 1, 0);
2710 set_disk_ro(mddev->gendisk, 1);
2711 err = do_md_run(mddev);
2717 err = do_md_stop(mddev, 1, 0);
2718 else if (mddev->ro == 1)
2719 err = restart_array(mddev);
2722 set_disk_ro(mddev->gendisk, 0);
2726 err = do_md_run(mddev);
2731 restart_array(mddev);
2732 spin_lock_irq(&mddev->write_lock);
2733 if (atomic_read(&mddev->writes_pending) == 0) {
2734 if (mddev->in_sync == 0) {
2736 if (mddev->safemode == 1)
2737 mddev->safemode = 0;
2738 if (mddev->persistent)
2739 set_bit(MD_CHANGE_CLEAN,
2745 spin_unlock_irq(&mddev->write_lock);
2748 mddev->recovery_cp = MaxSector;
2749 err = do_md_run(mddev);
2754 restart_array(mddev);
2755 if (mddev->external)
2756 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
2757 wake_up(&mddev->sb_wait);
2761 set_disk_ro(mddev->gendisk, 0);
2762 err = do_md_run(mddev);
2767 /* these cannot be set */
2773 sysfs_notify(&mddev->kobj, NULL, "array_state");
2777 static struct md_sysfs_entry md_array_state =
2778 __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
2781 null_show(mddev_t *mddev, char *page)
2787 new_dev_store(mddev_t *mddev, const char *buf, size_t len)
2789 /* buf must be %d:%d\n? giving major and minor numbers */
2790 /* The new device is added to the array.
2791 * If the array has a persistent superblock, we read the
2792 * superblock to initialise info and check validity.
2793 * Otherwise, only checking done is that in bind_rdev_to_array,
2794 * which mainly checks size.
2797 int major = simple_strtoul(buf, &e, 10);
2803 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
2805 minor = simple_strtoul(e+1, &e, 10);
2806 if (*e && *e != '\n')
2808 dev = MKDEV(major, minor);
2809 if (major != MAJOR(dev) ||
2810 minor != MINOR(dev))
2814 if (mddev->persistent) {
2815 rdev = md_import_device(dev, mddev->major_version,
2816 mddev->minor_version);
2817 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
2818 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2819 mdk_rdev_t, same_set);
2820 err = super_types[mddev->major_version]
2821 .load_super(rdev, rdev0, mddev->minor_version);
2825 } else if (mddev->external)
2826 rdev = md_import_device(dev, -2, -1);
2828 rdev = md_import_device(dev, -1, -1);
2831 return PTR_ERR(rdev);
2832 err = bind_rdev_to_array(rdev, mddev);
2836 return err ? err : len;
2839 static struct md_sysfs_entry md_new_device =
2840 __ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
2843 bitmap_store(mddev_t *mddev, const char *buf, size_t len)
2846 unsigned long chunk, end_chunk;
2850 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
2852 chunk = end_chunk = simple_strtoul(buf, &end, 0);
2853 if (buf == end) break;
2854 if (*end == '-') { /* range */
2856 end_chunk = simple_strtoul(buf, &end, 0);
2857 if (buf == end) break;
2859 if (*end && !isspace(*end)) break;
2860 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
2862 while (isspace(*buf)) buf++;
2864 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
2869 static struct md_sysfs_entry md_bitmap =
2870 __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
2873 size_show(mddev_t *mddev, char *page)
2875 return sprintf(page, "%llu\n", (unsigned long long)mddev->size);
2878 static int update_size(mddev_t *mddev, sector_t num_sectors);
2881 size_store(mddev_t *mddev, const char *buf, size_t len)
2883 /* If array is inactive, we can reduce the component size, but
2884 * not increase it (except from 0).
2885 * If array is active, we can try an on-line resize
2889 unsigned long long size = simple_strtoull(buf, &e, 10);
2890 if (!*buf || *buf == '\n' ||
2895 err = update_size(mddev, size * 2);
2896 md_update_sb(mddev, 1);
2898 if (mddev->size == 0 ||
2904 return err ? err : len;
2907 static struct md_sysfs_entry md_size =
2908 __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
2913 * 'none' for arrays with no metadata (good luck...)
2914 * 'external' for arrays with externally managed metadata,
2915 * or N.M for internally known formats
2918 metadata_show(mddev_t *mddev, char *page)
2920 if (mddev->persistent)
2921 return sprintf(page, "%d.%d\n",
2922 mddev->major_version, mddev->minor_version);
2923 else if (mddev->external)
2924 return sprintf(page, "external:%s\n", mddev->metadata_type);
2926 return sprintf(page, "none\n");
2930 metadata_store(mddev_t *mddev, const char *buf, size_t len)
2934 /* Changing the details of 'external' metadata is
2935 * always permitted. Otherwise there must be
2936 * no devices attached to the array.
2938 if (mddev->external && strncmp(buf, "external:", 9) == 0)
2940 else if (!list_empty(&mddev->disks))
2943 if (cmd_match(buf, "none")) {
2944 mddev->persistent = 0;
2945 mddev->external = 0;
2946 mddev->major_version = 0;
2947 mddev->minor_version = 90;
2950 if (strncmp(buf, "external:", 9) == 0) {
2951 size_t namelen = len-9;
2952 if (namelen >= sizeof(mddev->metadata_type))
2953 namelen = sizeof(mddev->metadata_type)-1;
2954 strncpy(mddev->metadata_type, buf+9, namelen);
2955 mddev->metadata_type[namelen] = 0;
2956 if (namelen && mddev->metadata_type[namelen-1] == '\n')
2957 mddev->metadata_type[--namelen] = 0;
2958 mddev->persistent = 0;
2959 mddev->external = 1;
2960 mddev->major_version = 0;
2961 mddev->minor_version = 90;
2964 major = simple_strtoul(buf, &e, 10);
2965 if (e==buf || *e != '.')
2968 minor = simple_strtoul(buf, &e, 10);
2969 if (e==buf || (*e && *e != '\n') )
2971 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
2973 mddev->major_version = major;
2974 mddev->minor_version = minor;
2975 mddev->persistent = 1;
2976 mddev->external = 0;
2980 static struct md_sysfs_entry md_metadata =
2981 __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
2984 action_show(mddev_t *mddev, char *page)
2986 char *type = "idle";
2987 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2988 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
2989 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2991 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2992 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2994 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2998 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
3001 return sprintf(page, "%s\n", type);
3005 action_store(mddev_t *mddev, const char *page, size_t len)
3007 if (!mddev->pers || !mddev->pers->sync_request)
3010 if (cmd_match(page, "idle")) {
3011 if (mddev->sync_thread) {
3012 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3013 md_unregister_thread(mddev->sync_thread);
3014 mddev->sync_thread = NULL;
3015 mddev->recovery = 0;
3017 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3018 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
3020 else if (cmd_match(page, "resync"))
3021 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3022 else if (cmd_match(page, "recover")) {
3023 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3024 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3025 } else if (cmd_match(page, "reshape")) {
3027 if (mddev->pers->start_reshape == NULL)
3029 err = mddev->pers->start_reshape(mddev);
3032 sysfs_notify(&mddev->kobj, NULL, "degraded");
3034 if (cmd_match(page, "check"))
3035 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3036 else if (!cmd_match(page, "repair"))
3038 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3039 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3041 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3042 md_wakeup_thread(mddev->thread);
3043 sysfs_notify(&mddev->kobj, NULL, "sync_action");
3048 mismatch_cnt_show(mddev_t *mddev, char *page)
3050 return sprintf(page, "%llu\n",
3051 (unsigned long long) mddev->resync_mismatches);
3054 static struct md_sysfs_entry md_scan_mode =
3055 __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
3058 static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
3061 sync_min_show(mddev_t *mddev, char *page)
3063 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3064 mddev->sync_speed_min ? "local": "system");
3068 sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3072 if (strncmp(buf, "system", 6)==0) {
3073 mddev->sync_speed_min = 0;
3076 min = simple_strtoul(buf, &e, 10);
3077 if (buf == e || (*e && *e != '\n') || min <= 0)
3079 mddev->sync_speed_min = min;
3083 static struct md_sysfs_entry md_sync_min =
3084 __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3087 sync_max_show(mddev_t *mddev, char *page)
3089 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3090 mddev->sync_speed_max ? "local": "system");
3094 sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3098 if (strncmp(buf, "system", 6)==0) {
3099 mddev->sync_speed_max = 0;
3102 max = simple_strtoul(buf, &e, 10);
3103 if (buf == e || (*e && *e != '\n') || max <= 0)
3105 mddev->sync_speed_max = max;
3109 static struct md_sysfs_entry md_sync_max =
3110 __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3113 degraded_show(mddev_t *mddev, char *page)
3115 return sprintf(page, "%d\n", mddev->degraded);
3117 static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
3120 sync_force_parallel_show(mddev_t *mddev, char *page)
3122 return sprintf(page, "%d\n", mddev->parallel_resync);
3126 sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3130 if (strict_strtol(buf, 10, &n))
3133 if (n != 0 && n != 1)
3136 mddev->parallel_resync = n;
3138 if (mddev->sync_thread)
3139 wake_up(&resync_wait);
3144 /* force parallel resync, even with shared block devices */
3145 static struct md_sysfs_entry md_sync_force_parallel =
3146 __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3147 sync_force_parallel_show, sync_force_parallel_store);
3150 sync_speed_show(mddev_t *mddev, char *page)
3152 unsigned long resync, dt, db;
3153 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3154 dt = (jiffies - mddev->resync_mark) / HZ;
3156 db = resync - mddev->resync_mark_cnt;
3157 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
3160 static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
3163 sync_completed_show(mddev_t *mddev, char *page)
3165 unsigned long max_blocks, resync;
3167 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3168 max_blocks = mddev->resync_max_sectors;
3170 max_blocks = mddev->size << 1;
3172 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
3173 return sprintf(page, "%lu / %lu\n", resync, max_blocks);
3176 static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
3179 min_sync_show(mddev_t *mddev, char *page)
3181 return sprintf(page, "%llu\n",
3182 (unsigned long long)mddev->resync_min);
3185 min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3187 unsigned long long min;
3188 if (strict_strtoull(buf, 10, &min))
3190 if (min > mddev->resync_max)
3192 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3195 /* Must be a multiple of chunk_size */
3196 if (mddev->chunk_size) {
3197 if (min & (sector_t)((mddev->chunk_size>>9)-1))
3200 mddev->resync_min = min;
3205 static struct md_sysfs_entry md_min_sync =
3206 __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3209 max_sync_show(mddev_t *mddev, char *page)
3211 if (mddev->resync_max == MaxSector)
3212 return sprintf(page, "max\n");
3214 return sprintf(page, "%llu\n",
3215 (unsigned long long)mddev->resync_max);
3218 max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3220 if (strncmp(buf, "max", 3) == 0)
3221 mddev->resync_max = MaxSector;
3223 unsigned long long max;
3224 if (strict_strtoull(buf, 10, &max))
3226 if (max < mddev->resync_min)
3228 if (max < mddev->resync_max &&
3229 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3232 /* Must be a multiple of chunk_size */
3233 if (mddev->chunk_size) {
3234 if (max & (sector_t)((mddev->chunk_size>>9)-1))
3237 mddev->resync_max = max;
3239 wake_up(&mddev->recovery_wait);
3243 static struct md_sysfs_entry md_max_sync =
3244 __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3247 suspend_lo_show(mddev_t *mddev, char *page)
3249 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3253 suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3256 unsigned long long new = simple_strtoull(buf, &e, 10);
3258 if (mddev->pers->quiesce == NULL)
3260 if (buf == e || (*e && *e != '\n'))
3262 if (new >= mddev->suspend_hi ||
3263 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3264 mddev->suspend_lo = new;
3265 mddev->pers->quiesce(mddev, 2);
3270 static struct md_sysfs_entry md_suspend_lo =
3271 __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3275 suspend_hi_show(mddev_t *mddev, char *page)
3277 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3281 suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3284 unsigned long long new = simple_strtoull(buf, &e, 10);
3286 if (mddev->pers->quiesce == NULL)
3288 if (buf == e || (*e && *e != '\n'))
3290 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3291 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3292 mddev->suspend_hi = new;
3293 mddev->pers->quiesce(mddev, 1);
3294 mddev->pers->quiesce(mddev, 0);
3299 static struct md_sysfs_entry md_suspend_hi =
3300 __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3303 reshape_position_show(mddev_t *mddev, char *page)
3305 if (mddev->reshape_position != MaxSector)
3306 return sprintf(page, "%llu\n",
3307 (unsigned long long)mddev->reshape_position);
3308 strcpy(page, "none\n");
3313 reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3316 unsigned long long new = simple_strtoull(buf, &e, 10);
3319 if (buf == e || (*e && *e != '\n'))
3321 mddev->reshape_position = new;
3322 mddev->delta_disks = 0;
3323 mddev->new_level = mddev->level;
3324 mddev->new_layout = mddev->layout;
3325 mddev->new_chunk = mddev->chunk_size;
3329 static struct md_sysfs_entry md_reshape_position =
3330 __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3331 reshape_position_store);
3334 static struct attribute *md_default_attrs[] = {
3337 &md_raid_disks.attr,
3338 &md_chunk_size.attr,
3340 &md_resync_start.attr,
3342 &md_new_device.attr,
3343 &md_safe_delay.attr,
3344 &md_array_state.attr,
3345 &md_reshape_position.attr,
3349 static struct attribute *md_redundancy_attrs[] = {
3351 &md_mismatches.attr,
3354 &md_sync_speed.attr,
3355 &md_sync_force_parallel.attr,
3356 &md_sync_completed.attr,
3359 &md_suspend_lo.attr,
3360 &md_suspend_hi.attr,
3365 static struct attribute_group md_redundancy_group = {
3367 .attrs = md_redundancy_attrs,
3372 md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3374 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3375 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
3380 rv = mddev_lock(mddev);
3382 rv = entry->show(mddev, page);
3383 mddev_unlock(mddev);
3389 md_attr_store(struct kobject *kobj, struct attribute *attr,
3390 const char *page, size_t length)
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 if (!capable(CAP_SYS_ADMIN))
3400 rv = mddev_lock(mddev);
3402 rv = entry->store(mddev, page, length);
3403 mddev_unlock(mddev);
3408 static void md_free(struct kobject *ko)
3410 mddev_t *mddev = container_of(ko, mddev_t, kobj);
3414 static struct sysfs_ops md_sysfs_ops = {
3415 .show = md_attr_show,
3416 .store = md_attr_store,
3418 static struct kobj_type md_ktype = {
3420 .sysfs_ops = &md_sysfs_ops,
3421 .default_attrs = md_default_attrs,
3426 static struct kobject *md_probe(dev_t dev, int *part, void *data)
3428 static DEFINE_MUTEX(disks_mutex);
3429 mddev_t *mddev = mddev_find(dev);
3430 struct gendisk *disk;
3431 int partitioned = (MAJOR(dev) != MD_MAJOR);
3432 int shift = partitioned ? MdpMinorShift : 0;
3433 int unit = MINOR(dev) >> shift;
3439 mutex_lock(&disks_mutex);
3440 if (mddev->gendisk) {
3441 mutex_unlock(&disks_mutex);
3445 disk = alloc_disk(1 << shift);
3447 mutex_unlock(&disks_mutex);
3451 disk->major = MAJOR(dev);
3452 disk->first_minor = unit << shift;
3454 sprintf(disk->disk_name, "md_d%d", unit);
3456 sprintf(disk->disk_name, "md%d", unit);
3457 disk->fops = &md_fops;
3458 disk->private_data = mddev;
3459 disk->queue = mddev->queue;
3461 mddev->gendisk = disk;
3462 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
3463 &disk_to_dev(disk)->kobj, "%s", "md");
3464 mutex_unlock(&disks_mutex);
3466 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
3469 kobject_uevent(&mddev->kobj, KOBJ_ADD);
3473 static void md_safemode_timeout(unsigned long data)
3475 mddev_t *mddev = (mddev_t *) data;
3477 if (!atomic_read(&mddev->writes_pending)) {
3478 mddev->safemode = 1;
3479 if (mddev->external)
3480 set_bit(MD_NOTIFY_ARRAY_STATE, &mddev->flags);
3482 md_wakeup_thread(mddev->thread);
3485 static int start_dirty_degraded;
3487 static int do_md_run(mddev_t * mddev)
3491 struct list_head *tmp;
3493 struct gendisk *disk;
3494 struct mdk_personality *pers;
3495 char b[BDEVNAME_SIZE];
3497 if (list_empty(&mddev->disks))
3498 /* cannot run an array with no devices.. */
3505 * Analyze all RAID superblock(s)
3507 if (!mddev->raid_disks) {
3508 if (!mddev->persistent)
3513 chunk_size = mddev->chunk_size;
3516 if (chunk_size > MAX_CHUNK_SIZE) {
3517 printk(KERN_ERR "too big chunk_size: %d > %d\n",
3518 chunk_size, MAX_CHUNK_SIZE);
3522 * chunk-size has to be a power of 2
3524 if ( (1 << ffz(~chunk_size)) != chunk_size) {
3525 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
3529 /* devices must have minimum size of one chunk */
3530 rdev_for_each(rdev, tmp, mddev) {
3531 if (test_bit(Faulty, &rdev->flags))
3533 if (rdev->size < chunk_size / 1024) {
3535 "md: Dev %s smaller than chunk_size:"
3537 bdevname(rdev->bdev,b),
3538 (unsigned long long)rdev->size,
3545 if (mddev->level != LEVEL_NONE)
3546 request_module("md-level-%d", mddev->level);
3547 else if (mddev->clevel[0])
3548 request_module("md-%s", mddev->clevel);
3551 * Drop all container device buffers, from now on
3552 * the only valid external interface is through the md
3555 rdev_for_each(rdev, tmp, mddev) {
3556 if (test_bit(Faulty, &rdev->flags))
3558 sync_blockdev(rdev->bdev);
3559 invalidate_bdev(rdev->bdev);
3561 /* perform some consistency tests on the device.
3562 * We don't want the data to overlap the metadata,
3563 * Internal Bitmap issues has handled elsewhere.
3565 if (rdev->data_offset < rdev->sb_start) {
3567 rdev->data_offset + mddev->size*2
3569 printk("md: %s: data overlaps metadata\n",
3574 if (rdev->sb_start + rdev->sb_size/512
3575 > rdev->data_offset) {
3576 printk("md: %s: metadata overlaps data\n",
3581 sysfs_notify(&rdev->kobj, NULL, "state");
3584 md_probe(mddev->unit, NULL, NULL);
3585 disk = mddev->gendisk;
3589 spin_lock(&pers_lock);
3590 pers = find_pers(mddev->level, mddev->clevel);
3591 if (!pers || !try_module_get(pers->owner)) {
3592 spin_unlock(&pers_lock);
3593 if (mddev->level != LEVEL_NONE)
3594 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
3597 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
3602 spin_unlock(&pers_lock);
3603 mddev->level = pers->level;
3604 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3606 if (mddev->reshape_position != MaxSector &&
3607 pers->start_reshape == NULL) {
3608 /* This personality cannot handle reshaping... */
3610 module_put(pers->owner);
3614 if (pers->sync_request) {
3615 /* Warn if this is a potentially silly
3618 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3620 struct list_head *tmp2;
3622 rdev_for_each(rdev, tmp, mddev) {
3623 rdev_for_each(rdev2, tmp2, mddev) {
3625 rdev->bdev->bd_contains ==
3626 rdev2->bdev->bd_contains) {
3628 "%s: WARNING: %s appears to be"
3629 " on the same physical disk as"
3632 bdevname(rdev->bdev,b),
3633 bdevname(rdev2->bdev,b2));
3640 "True protection against single-disk"
3641 " failure might be compromised.\n");
3644 mddev->recovery = 0;
3645 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
3646 mddev->barriers_work = 1;
3647 mddev->ok_start_degraded = start_dirty_degraded;
3650 mddev->ro = 2; /* read-only, but switch on first write */
3652 err = mddev->pers->run(mddev);
3654 printk(KERN_ERR "md: pers->run() failed ...\n");
3655 else if (mddev->pers->sync_request) {
3656 err = bitmap_create(mddev);
3658 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
3659 mdname(mddev), err);
3660 mddev->pers->stop(mddev);
3664 module_put(mddev->pers->owner);
3666 bitmap_destroy(mddev);
3669 if (mddev->pers->sync_request) {
3670 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3672 "md: cannot register extra attributes for %s\n",
3674 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
3677 atomic_set(&mddev->writes_pending,0);
3678 mddev->safemode = 0;
3679 mddev->safemode_timer.function = md_safemode_timeout;
3680 mddev->safemode_timer.data = (unsigned long) mddev;
3681 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
3684 rdev_for_each(rdev, tmp, mddev)
3685 if (rdev->raid_disk >= 0) {
3687 sprintf(nm, "rd%d", rdev->raid_disk);
3688 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
3689 printk("md: cannot register %s for %s\n",
3693 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3696 md_update_sb(mddev, 0);
3698 set_capacity(disk, mddev->array_sectors);
3700 /* If we call blk_queue_make_request here, it will
3701 * re-initialise max_sectors etc which may have been
3702 * refined inside -> run. So just set the bits we need to set.
3703 * Most initialisation happended when we called
3704 * blk_queue_make_request(..., md_fail_request)
3707 mddev->queue->queuedata = mddev;
3708 mddev->queue->make_request_fn = mddev->pers->make_request;
3710 /* If there is a partially-recovered drive we need to
3711 * start recovery here. If we leave it to md_check_recovery,
3712 * it will remove the drives and not do the right thing
3714 if (mddev->degraded && !mddev->sync_thread) {
3715 struct list_head *rtmp;
3717 rdev_for_each(rdev, rtmp, mddev)
3718 if (rdev->raid_disk >= 0 &&
3719 !test_bit(In_sync, &rdev->flags) &&
3720 !test_bit(Faulty, &rdev->flags))
3721 /* complete an interrupted recovery */
3723 if (spares && mddev->pers->sync_request) {
3724 mddev->recovery = 0;
3725 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3726 mddev->sync_thread = md_register_thread(md_do_sync,
3729 if (!mddev->sync_thread) {
3730 printk(KERN_ERR "%s: could not start resync"
3733 /* leave the spares where they are, it shouldn't hurt */
3734 mddev->recovery = 0;
3738 md_wakeup_thread(mddev->thread);
3739 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
3742 md_new_event(mddev);
3743 sysfs_notify(&mddev->kobj, NULL, "array_state");
3744 sysfs_notify(&mddev->kobj, NULL, "sync_action");
3745 sysfs_notify(&mddev->kobj, NULL, "degraded");
3746 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
3750 static int restart_array(mddev_t *mddev)
3752 struct gendisk *disk = mddev->gendisk;
3754 /* Complain if it has no devices */
3755 if (list_empty(&mddev->disks))
3761 mddev->safemode = 0;
3763 set_disk_ro(disk, 0);
3764 printk(KERN_INFO "md: %s switched to read-write mode.\n",
3766 /* Kick recovery or resync if necessary */
3767 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3768 md_wakeup_thread(mddev->thread);
3769 md_wakeup_thread(mddev->sync_thread);
3770 sysfs_notify(&mddev->kobj, NULL, "array_state");
3774 /* similar to deny_write_access, but accounts for our holding a reference
3775 * to the file ourselves */
3776 static int deny_bitmap_write_access(struct file * file)
3778 struct inode *inode = file->f_mapping->host;
3780 spin_lock(&inode->i_lock);
3781 if (atomic_read(&inode->i_writecount) > 1) {
3782 spin_unlock(&inode->i_lock);
3785 atomic_set(&inode->i_writecount, -1);
3786 spin_unlock(&inode->i_lock);
3791 static void restore_bitmap_write_access(struct file *file)
3793 struct inode *inode = file->f_mapping->host;
3795 spin_lock(&inode->i_lock);
3796 atomic_set(&inode->i_writecount, 1);
3797 spin_unlock(&inode->i_lock);
3801 * 0 - completely stop and dis-assemble array
3802 * 1 - switch to readonly
3803 * 2 - stop but do not disassemble array
3805 static int do_md_stop(mddev_t * mddev, int mode, int is_open)
3808 struct gendisk *disk = mddev->gendisk;
3810 if (atomic_read(&mddev->openers) > is_open) {
3811 printk("md: %s still in use.\n",mdname(mddev));
3817 if (mddev->sync_thread) {
3818 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3819 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3820 md_unregister_thread(mddev->sync_thread);
3821 mddev->sync_thread = NULL;
3824 del_timer_sync(&mddev->safemode_timer);
3827 case 1: /* readonly */
3833 case 0: /* disassemble */
3835 bitmap_flush(mddev);
3836 md_super_wait(mddev);
3838 set_disk_ro(disk, 0);
3839 blk_queue_make_request(mddev->queue, md_fail_request);
3840 mddev->pers->stop(mddev);
3841 mddev->queue->merge_bvec_fn = NULL;
3842 mddev->queue->unplug_fn = NULL;
3843 mddev->queue->backing_dev_info.congested_fn = NULL;
3844 if (mddev->pers->sync_request)
3845 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
3847 module_put(mddev->pers->owner);
3849 /* tell userspace to handle 'inactive' */
3850 sysfs_notify(&mddev->kobj, NULL, "array_state");
3852 set_capacity(disk, 0);
3858 if (!mddev->in_sync || mddev->flags) {
3859 /* mark array as shutdown cleanly */
3861 md_update_sb(mddev, 1);
3864 set_disk_ro(disk, 1);
3865 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3869 * Free resources if final stop
3873 struct list_head *tmp;
3875 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
3877 bitmap_destroy(mddev);
3878 if (mddev->bitmap_file) {
3879 restore_bitmap_write_access(mddev->bitmap_file);
3880 fput(mddev->bitmap_file);
3881 mddev->bitmap_file = NULL;
3883 mddev->bitmap_offset = 0;
3885 rdev_for_each(rdev, tmp, mddev)
3886 if (rdev->raid_disk >= 0) {
3888 sprintf(nm, "rd%d", rdev->raid_disk);
3889 sysfs_remove_link(&mddev->kobj, nm);
3892 /* make sure all md_delayed_delete calls have finished */
3893 flush_scheduled_work();
3895 export_array(mddev);
3897 mddev->array_sectors = 0;
3899 mddev->raid_disks = 0;
3900 mddev->recovery_cp = 0;
3901 mddev->resync_min = 0;
3902 mddev->resync_max = MaxSector;
3903 mddev->reshape_position = MaxSector;
3904 mddev->external = 0;
3905 mddev->persistent = 0;
3906 mddev->level = LEVEL_NONE;
3907 mddev->clevel[0] = 0;
3910 mddev->metadata_type[0] = 0;
3911 mddev->chunk_size = 0;
3912 mddev->ctime = mddev->utime = 0;
3914 mddev->max_disks = 0;
3916 mddev->delta_disks = 0;
3917 mddev->new_level = LEVEL_NONE;
3918 mddev->new_layout = 0;
3919 mddev->new_chunk = 0;
3920 mddev->curr_resync = 0;
3921 mddev->resync_mismatches = 0;
3922 mddev->suspend_lo = mddev->suspend_hi = 0;
3923 mddev->sync_speed_min = mddev->sync_speed_max = 0;
3924 mddev->recovery = 0;
3927 mddev->degraded = 0;
3928 mddev->barriers_work = 0;
3929 mddev->safemode = 0;
3931 } else if (mddev->pers)
3932 printk(KERN_INFO "md: %s switched to read-only mode.\n",
3935 md_new_event(mddev);
3936 sysfs_notify(&mddev->kobj, NULL, "array_state");
3942 static void autorun_array(mddev_t *mddev)
3945 struct list_head *tmp;
3948 if (list_empty(&mddev->disks))
3951 printk(KERN_INFO "md: running: ");
3953 rdev_for_each(rdev, tmp, mddev) {
3954 char b[BDEVNAME_SIZE];
3955 printk("<%s>", bdevname(rdev->bdev,b));
3959 err = do_md_run(mddev);
3961 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
3962 do_md_stop(mddev, 0, 0);
3967 * lets try to run arrays based on all disks that have arrived
3968 * until now. (those are in pending_raid_disks)
3970 * the method: pick the first pending disk, collect all disks with
3971 * the same UUID, remove all from the pending list and put them into
3972 * the 'same_array' list. Then order this list based on superblock
3973 * update time (freshest comes first), kick out 'old' disks and
3974 * compare superblocks. If everything's fine then run it.
3976 * If "unit" is allocated, then bump its reference count
3978 static void autorun_devices(int part)
3980 struct list_head *tmp;
3981 mdk_rdev_t *rdev0, *rdev;
3983 char b[BDEVNAME_SIZE];
3985 printk(KERN_INFO "md: autorun ...\n");
3986 while (!list_empty(&pending_raid_disks)) {
3989 LIST_HEAD(candidates);
3990 rdev0 = list_entry(pending_raid_disks.next,
3991 mdk_rdev_t, same_set);
3993 printk(KERN_INFO "md: considering %s ...\n",
3994 bdevname(rdev0->bdev,b));
3995 INIT_LIST_HEAD(&candidates);
3996 rdev_for_each_list(rdev, tmp, pending_raid_disks)
3997 if (super_90_load(rdev, rdev0, 0) >= 0) {
3998 printk(KERN_INFO "md: adding %s ...\n",
3999 bdevname(rdev->bdev,b));
4000 list_move(&rdev->same_set, &candidates);
4003 * now we have a set of devices, with all of them having
4004 * mostly sane superblocks. It's time to allocate the
4008 dev = MKDEV(mdp_major,
4009 rdev0->preferred_minor << MdpMinorShift);
4010 unit = MINOR(dev) >> MdpMinorShift;
4012 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4015 if (rdev0->preferred_minor != unit) {
4016 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4017 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4021 md_probe(dev, NULL, NULL);
4022 mddev = mddev_find(dev);
4023 if (!mddev || !mddev->gendisk) {
4027 "md: cannot allocate memory for md drive.\n");
4030 if (mddev_lock(mddev))
4031 printk(KERN_WARNING "md: %s locked, cannot run\n",
4033 else if (mddev->raid_disks || mddev->major_version
4034 || !list_empty(&mddev->disks)) {
4036 "md: %s already running, cannot run %s\n",
4037 mdname(mddev), bdevname(rdev0->bdev,b));
4038 mddev_unlock(mddev);
4040 printk(KERN_INFO "md: created %s\n", mdname(mddev));
4041 mddev->persistent = 1;
4042 rdev_for_each_list(rdev, tmp, candidates) {
4043 list_del_init(&rdev->same_set);
4044 if (bind_rdev_to_array(rdev, mddev))
4047 autorun_array(mddev);
4048 mddev_unlock(mddev);
4050 /* on success, candidates will be empty, on error
4053 rdev_for_each_list(rdev, tmp, candidates) {
4054 list_del_init(&rdev->same_set);
4059 printk(KERN_INFO "md: ... autorun DONE.\n");
4061 #endif /* !MODULE */
4063 static int get_version(void __user * arg)
4067 ver.major = MD_MAJOR_VERSION;
4068 ver.minor = MD_MINOR_VERSION;
4069 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4071 if (copy_to_user(arg, &ver, sizeof(ver)))
4077 static int get_array_info(mddev_t * mddev, void __user * arg)
4079 mdu_array_info_t info;
4080 int nr,working,active,failed,spare;
4082 struct list_head *tmp;
4084 nr=working=active=failed=spare=0;
4085 rdev_for_each(rdev, tmp, mddev) {
4087 if (test_bit(Faulty, &rdev->flags))
4091 if (test_bit(In_sync, &rdev->flags))
4098 info.major_version = mddev->major_version;
4099 info.minor_version = mddev->minor_version;
4100 info.patch_version = MD_PATCHLEVEL_VERSION;
4101 info.ctime = mddev->ctime;
4102 info.level = mddev->level;
4103 info.size = mddev->size;
4104 if (info.size != mddev->size) /* overflow */
4107 info.raid_disks = mddev->raid_disks;
4108 info.md_minor = mddev->md_minor;
4109 info.not_persistent= !mddev->persistent;
4111 info.utime = mddev->utime;
4114 info.state = (1<<MD_SB_CLEAN);
4115 if (mddev->bitmap && mddev->bitmap_offset)
4116 info.state = (1<<MD_SB_BITMAP_PRESENT);
4117 info.active_disks = active;
4118 info.working_disks = working;
4119 info.failed_disks = failed;
4120 info.spare_disks = spare;
4122 info.layout = mddev->layout;
4123 info.chunk_size = mddev->chunk_size;
4125 if (copy_to_user(arg, &info, sizeof(info)))
4131 static int get_bitmap_file(mddev_t * mddev, void __user * arg)
4133 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4134 char *ptr, *buf = NULL;
4137 if (md_allow_write(mddev))
4138 file = kmalloc(sizeof(*file), GFP_NOIO);
4140 file = kmalloc(sizeof(*file), GFP_KERNEL);
4145 /* bitmap disabled, zero the first byte and copy out */
4146 if (!mddev->bitmap || !mddev->bitmap->file) {
4147 file->pathname[0] = '\0';
4151 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4155 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4159 strcpy(file->pathname, ptr);
4163 if (copy_to_user(arg, file, sizeof(*file)))
4171 static int get_disk_info(mddev_t * mddev, void __user * arg)
4173 mdu_disk_info_t info;
4176 if (copy_from_user(&info, arg, sizeof(info)))
4179 rdev = find_rdev_nr(mddev, info.number);
4181 info.major = MAJOR(rdev->bdev->bd_dev);
4182 info.minor = MINOR(rdev->bdev->bd_dev);
4183 info.raid_disk = rdev->raid_disk;
4185 if (test_bit(Faulty, &rdev->flags))
4186 info.state |= (1<<MD_DISK_FAULTY);
4187 else if (test_bit(In_sync, &rdev->flags)) {
4188 info.state |= (1<<MD_DISK_ACTIVE);
4189 info.state |= (1<<MD_DISK_SYNC);
4191 if (test_bit(WriteMostly, &rdev->flags))
4192 info.state |= (1<<MD_DISK_WRITEMOSTLY);
4194 info.major = info.minor = 0;
4195 info.raid_disk = -1;
4196 info.state = (1<<MD_DISK_REMOVED);
4199 if (copy_to_user(arg, &info, sizeof(info)))
4205 static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4207 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4209 dev_t dev = MKDEV(info->major,info->minor);
4211 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4214 if (!mddev->raid_disks) {
4216 /* expecting a device which has a superblock */
4217 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4220 "md: md_import_device returned %ld\n",
4222 return PTR_ERR(rdev);
4224 if (!list_empty(&mddev->disks)) {
4225 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4226 mdk_rdev_t, same_set);
4227 int err = super_types[mddev->major_version]
4228 .load_super(rdev, rdev0, mddev->minor_version);
4231 "md: %s has different UUID to %s\n",
4232 bdevname(rdev->bdev,b),
4233 bdevname(rdev0->bdev,b2));
4238 err = bind_rdev_to_array(rdev, mddev);
4245 * add_new_disk can be used once the array is assembled
4246 * to add "hot spares". They must already have a superblock
4251 if (!mddev->pers->hot_add_disk) {
4253 "%s: personality does not support diskops!\n",
4257 if (mddev->persistent)
4258 rdev = md_import_device(dev, mddev->major_version,
4259 mddev->minor_version);
4261 rdev = md_import_device(dev, -1, -1);
4264 "md: md_import_device returned %ld\n",
4266 return PTR_ERR(rdev);
4268 /* set save_raid_disk if appropriate */
4269 if (!mddev->persistent) {
4270 if (info->state & (1<<MD_DISK_SYNC) &&
4271 info->raid_disk < mddev->raid_disks)
4272 rdev->raid_disk = info->raid_disk;
4274 rdev->raid_disk = -1;
4276 super_types[mddev->major_version].
4277 validate_super(mddev, rdev);
4278 rdev->saved_raid_disk = rdev->raid_disk;
4280 clear_bit(In_sync, &rdev->flags); /* just to be sure */
4281 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4282 set_bit(WriteMostly, &rdev->flags);
4284 rdev->raid_disk = -1;
4285 err = bind_rdev_to_array(rdev, mddev);
4286 if (!err && !mddev->pers->hot_remove_disk) {
4287 /* If there is hot_add_disk but no hot_remove_disk
4288 * then added disks for geometry changes,
4289 * and should be added immediately.
4291 super_types[mddev->major_version].
4292 validate_super(mddev, rdev);
4293 err = mddev->pers->hot_add_disk(mddev, rdev);
4295 unbind_rdev_from_array(rdev);
4300 sysfs_notify(&rdev->kobj, NULL, "state");
4302 md_update_sb(mddev, 1);
4303 if (mddev->degraded)
4304 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4305 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4306 md_wakeup_thread(mddev->thread);
4310 /* otherwise, add_new_disk is only allowed
4311 * for major_version==0 superblocks
4313 if (mddev->major_version != 0) {
4314 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
4319 if (!(info->state & (1<<MD_DISK_FAULTY))) {
4321 rdev = md_import_device(dev, -1, 0);
4324 "md: error, md_import_device() returned %ld\n",
4326 return PTR_ERR(rdev);
4328 rdev->desc_nr = info->number;
4329 if (info->raid_disk < mddev->raid_disks)
4330 rdev->raid_disk = info->raid_disk;
4332 rdev->raid_disk = -1;
4334 if (rdev->raid_disk < mddev->raid_disks)
4335 if (info->state & (1<<MD_DISK_SYNC))
4336 set_bit(In_sync, &rdev->flags);
4338 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4339 set_bit(WriteMostly, &rdev->flags);
4341 if (!mddev->persistent) {
4342 printk(KERN_INFO "md: nonpersistent superblock ...\n");
4343 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
4345 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
4346 rdev->size = calc_num_sectors(rdev, mddev->chunk_size) / 2;
4348 err = bind_rdev_to_array(rdev, mddev);
4358 static int hot_remove_disk(mddev_t * mddev, dev_t dev)
4360 char b[BDEVNAME_SIZE];
4363 rdev = find_rdev(mddev, dev);
4367 if (rdev->raid_disk >= 0)
4370 kick_rdev_from_array(rdev);
4371 md_update_sb(mddev, 1);
4372 md_new_event(mddev);
4376 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
4377 bdevname(rdev->bdev,b), mdname(mddev));
4381 static int hot_add_disk(mddev_t * mddev, dev_t dev)
4383 char b[BDEVNAME_SIZE];
4390 if (mddev->major_version != 0) {
4391 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
4392 " version-0 superblocks.\n",
4396 if (!mddev->pers->hot_add_disk) {
4398 "%s: personality does not support diskops!\n",
4403 rdev = md_import_device(dev, -1, 0);
4406 "md: error, md_import_device() returned %ld\n",
4411 if (mddev->persistent)
4412 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
4414 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
4416 rdev->size = calc_num_sectors(rdev, mddev->chunk_size) / 2;
4418 if (test_bit(Faulty, &rdev->flags)) {
4420 "md: can not hot-add faulty %s disk to %s!\n",
4421 bdevname(rdev->bdev,b), mdname(mddev));
4425 clear_bit(In_sync, &rdev->flags);
4427 rdev->saved_raid_disk = -1;
4428 err = bind_rdev_to_array(rdev, mddev);
4433 * The rest should better be atomic, we can have disk failures
4434 * noticed in interrupt contexts ...
4437 if (rdev->desc_nr == mddev->max_disks) {
4438 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
4441 goto abort_unbind_export;
4444 rdev->raid_disk = -1;
4446 md_update_sb(mddev, 1);
4449 * Kick recovery, maybe this spare has to be added to the
4450 * array immediately.
4452 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4453 md_wakeup_thread(mddev->thread);
4454 md_new_event(mddev);
4457 abort_unbind_export:
4458 unbind_rdev_from_array(rdev);
4465 static int set_bitmap_file(mddev_t *mddev, int fd)
4470 if (!mddev->pers->quiesce)
4472 if (mddev->recovery || mddev->sync_thread)
4474 /* we should be able to change the bitmap.. */
4480 return -EEXIST; /* cannot add when bitmap is present */
4481 mddev->bitmap_file = fget(fd);
4483 if (mddev->bitmap_file == NULL) {
4484 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
4489 err = deny_bitmap_write_access(mddev->bitmap_file);
4491 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
4493 fput(mddev->bitmap_file);
4494 mddev->bitmap_file = NULL;
4497 mddev->bitmap_offset = 0; /* file overrides offset */
4498 } else if (mddev->bitmap == NULL)
4499 return -ENOENT; /* cannot remove what isn't there */
4502 mddev->pers->quiesce(mddev, 1);
4504 err = bitmap_create(mddev);
4505 if (fd < 0 || err) {
4506 bitmap_destroy(mddev);
4507 fd = -1; /* make sure to put the file */
4509 mddev->pers->quiesce(mddev, 0);
4512 if (mddev->bitmap_file) {
4513 restore_bitmap_write_access(mddev->bitmap_file);
4514 fput(mddev->bitmap_file);
4516 mddev->bitmap_file = NULL;
4523 * set_array_info is used two different ways
4524 * The original usage is when creating a new array.
4525 * In this usage, raid_disks is > 0 and it together with
4526 * level, size, not_persistent,layout,chunksize determine the
4527 * shape of the array.
4528 * This will always create an array with a type-0.90.0 superblock.
4529 * The newer usage is when assembling an array.
4530 * In this case raid_disks will be 0, and the major_version field is
4531 * use to determine which style super-blocks are to be found on the devices.
4532 * The minor and patch _version numbers are also kept incase the
4533 * super_block handler wishes to interpret them.
4535 static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
4538 if (info->raid_disks == 0) {
4539 /* just setting version number for superblock loading */
4540 if (info->major_version < 0 ||
4541 info->major_version >= ARRAY_SIZE(super_types) ||
4542 super_types[info->major_version].name == NULL) {
4543 /* maybe try to auto-load a module? */
4545 "md: superblock version %d not known\n",
4546 info->major_version);
4549 mddev->major_version = info->major_version;
4550 mddev->minor_version = info->minor_version;
4551 mddev->patch_version = info->patch_version;
4552 mddev->persistent = !info->not_persistent;
4555 mddev->major_version = MD_MAJOR_VERSION;
4556 mddev->minor_version = MD_MINOR_VERSION;
4557 mddev->patch_version = MD_PATCHLEVEL_VERSION;
4558 mddev->ctime = get_seconds();
4560 mddev->level = info->level;
4561 mddev->clevel[0] = 0;
4562 mddev->size = info->size;
4563 mddev->raid_disks = info->raid_disks;
4564 /* don't set md_minor, it is determined by which /dev/md* was
4567 if (info->state & (1<<MD_SB_CLEAN))
4568 mddev->recovery_cp = MaxSector;
4570 mddev->recovery_cp = 0;
4571 mddev->persistent = ! info->not_persistent;
4572 mddev->external = 0;
4574 mddev->layout = info->layout;
4575 mddev->chunk_size = info->chunk_size;
4577 mddev->max_disks = MD_SB_DISKS;
4579 if (mddev->persistent)
4581 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4583 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
4584 mddev->bitmap_offset = 0;
4586 mddev->reshape_position = MaxSector;
4589 * Generate a 128 bit UUID
4591 get_random_bytes(mddev->uuid, 16);
4593 mddev->new_level = mddev->level;
4594 mddev->new_chunk = mddev->chunk_size;
4595 mddev->new_layout = mddev->layout;
4596 mddev->delta_disks = 0;
4601 static int update_size(mddev_t *mddev, sector_t num_sectors)
4605 struct list_head *tmp;
4606 int fit = (num_sectors == 0);
4608 if (mddev->pers->resize == NULL)
4610 /* The "num_sectors" is the number of sectors of each device that
4611 * is used. This can only make sense for arrays with redundancy.
4612 * linear and raid0 always use whatever space is available. We can only
4613 * consider changing this number if no resync or reconstruction is
4614 * happening, and if the new size is acceptable. It must fit before the
4615 * sb_start or, if that is <data_offset, it must fit before the size
4616 * of each device. If num_sectors is zero, we find the largest size
4620 if (mddev->sync_thread)
4623 /* Sorry, cannot grow a bitmap yet, just remove it,
4627 rdev_for_each(rdev, tmp, mddev) {
4629 avail = rdev->size * 2;
4631 if (fit && (num_sectors == 0 || num_sectors > avail))
4632 num_sectors = avail;
4633 if (avail < num_sectors)
4636 rv = mddev->pers->resize(mddev, num_sectors);
4638 struct block_device *bdev;
4640 bdev = bdget_disk(mddev->gendisk, 0);
4642 mutex_lock(&bdev->bd_inode->i_mutex);
4643 i_size_write(bdev->bd_inode,
4644 (loff_t)mddev->array_sectors << 9);
4645 mutex_unlock(&bdev->bd_inode->i_mutex);
4652 static int update_raid_disks(mddev_t *mddev, int raid_disks)
4655 /* change the number of raid disks */
4656 if (mddev->pers->check_reshape == NULL)
4658 if (raid_disks <= 0 ||
4659 raid_disks >= mddev->max_disks)
4661 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
4663 mddev->delta_disks = raid_disks - mddev->raid_disks;
4665 rv = mddev->pers->check_reshape(mddev);
4671 * update_array_info is used to change the configuration of an
4673 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
4674 * fields in the info are checked against the array.
4675 * Any differences that cannot be handled will cause an error.
4676 * Normally, only one change can be managed at a time.
4678 static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
4684 /* calculate expected state,ignoring low bits */
4685 if (mddev->bitmap && mddev->bitmap_offset)
4686 state |= (1 << MD_SB_BITMAP_PRESENT);
4688 if (mddev->major_version != info->major_version ||
4689 mddev->minor_version != info->minor_version ||
4690 /* mddev->patch_version != info->patch_version || */
4691 mddev->ctime != info->ctime ||
4692 mddev->level != info->level ||
4693 /* mddev->layout != info->layout || */
4694 !mddev->persistent != info->not_persistent||
4695 mddev->chunk_size != info->chunk_size ||
4696 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
4697 ((state^info->state) & 0xfffffe00)
4700 /* Check there is only one change */
4701 if (info->size >= 0 && mddev->size != info->size) cnt++;
4702 if (mddev->raid_disks != info->raid_disks) cnt++;
4703 if (mddev->layout != info->layout) cnt++;
4704 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
4705 if (cnt == 0) return 0;
4706 if (cnt > 1) return -EINVAL;
4708 if (mddev->layout != info->layout) {
4710 * we don't need to do anything at the md level, the
4711 * personality will take care of it all.
4713 if (mddev->pers->reconfig == NULL)
4716 return mddev->pers->reconfig(mddev, info->layout, -1);
4718 if (info->size >= 0 && mddev->size != info->size)
4719 rv = update_size(mddev, (sector_t)info->size * 2);
4721 if (mddev->raid_disks != info->raid_disks)
4722 rv = update_raid_disks(mddev, info->raid_disks);
4724 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
4725 if (mddev->pers->quiesce == NULL)
4727 if (mddev->recovery || mddev->sync_thread)
4729 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
4730 /* add the bitmap */
4733 if (mddev->default_bitmap_offset == 0)
4735 mddev->bitmap_offset = mddev->default_bitmap_offset;
4736 mddev->pers->quiesce(mddev, 1);
4737 rv = bitmap_create(mddev);
4739 bitmap_destroy(mddev);
4740 mddev->pers->quiesce(mddev, 0);
4742 /* remove the bitmap */
4745 if (mddev->bitmap->file)
4747 mddev->pers->quiesce(mddev, 1);
4748 bitmap_destroy(mddev);
4749 mddev->pers->quiesce(mddev, 0);
4750 mddev->bitmap_offset = 0;
4753 md_update_sb(mddev, 1);
4757 static int set_disk_faulty(mddev_t *mddev, dev_t dev)
4761 if (mddev->pers == NULL)
4764 rdev = find_rdev(mddev, dev);
4768 md_error(mddev, rdev);
4773 * We have a problem here : there is no easy way to give a CHS
4774 * virtual geometry. We currently pretend that we have a 2 heads
4775 * 4 sectors (with a BIG number of cylinders...). This drives
4776 * dosfs just mad... ;-)
4778 static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4780 mddev_t *mddev = bdev->bd_disk->private_data;
4784 geo->cylinders = get_capacity(mddev->gendisk) / 8;
4788 static int md_ioctl(struct inode *inode, struct file *file,
4789 unsigned int cmd, unsigned long arg)
4792 void __user *argp = (void __user *)arg;
4793 mddev_t *mddev = NULL;
4795 if (!capable(CAP_SYS_ADMIN))
4799 * Commands dealing with the RAID driver but not any
4805 err = get_version(argp);
4808 case PRINT_RAID_DEBUG:
4816 autostart_arrays(arg);
4823 * Commands creating/starting a new array:
4826 mddev = inode->i_bdev->bd_disk->private_data;
4833 err = mddev_lock(mddev);
4836 "md: ioctl lock interrupted, reason %d, cmd %d\n",
4843 case SET_ARRAY_INFO:
4845 mdu_array_info_t info;
4847 memset(&info, 0, sizeof(info));
4848 else if (copy_from_user(&info, argp, sizeof(info))) {
4853 err = update_array_info(mddev, &info);
4855 printk(KERN_WARNING "md: couldn't update"
4856 " array info. %d\n", err);
4861 if (!list_empty(&mddev->disks)) {
4863 "md: array %s already has disks!\n",
4868 if (mddev->raid_disks) {
4870 "md: array %s already initialised!\n",
4875 err = set_array_info(mddev, &info);
4877 printk(KERN_WARNING "md: couldn't set"
4878 " array info. %d\n", err);
4888 * Commands querying/configuring an existing array:
4890 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
4891 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
4892 if ((!mddev->raid_disks && !mddev->external)
4893 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
4894 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
4895 && cmd != GET_BITMAP_FILE) {
4901 * Commands even a read-only array can execute:
4905 case GET_ARRAY_INFO:
4906 err = get_array_info(mddev, argp);
4909 case GET_BITMAP_FILE:
4910 err = get_bitmap_file(mddev, argp);
4914 err = get_disk_info(mddev, argp);
4917 case RESTART_ARRAY_RW:
4918 err = restart_array(mddev);
4922 err = do_md_stop(mddev, 0, 1);
4926 err = do_md_stop(mddev, 1, 1);
4932 * The remaining ioctls are changing the state of the
4933 * superblock, so we do not allow them on read-only arrays.
4934 * However non-MD ioctls (e.g. get-size) will still come through
4935 * here and hit the 'default' below, so only disallow
4936 * 'md' ioctls, and switch to rw mode if started auto-readonly.
4938 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
4939 if (mddev->ro == 2) {
4941 sysfs_notify(&mddev->kobj, NULL, "array_state");
4942 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4943 md_wakeup_thread(mddev->thread);
4954 mdu_disk_info_t info;
4955 if (copy_from_user(&info, argp, sizeof(info)))
4958 err = add_new_disk(mddev, &info);
4962 case HOT_REMOVE_DISK:
4963 err = hot_remove_disk(mddev, new_decode_dev(arg));
4967 err = hot_add_disk(mddev, new_decode_dev(arg));
4970 case SET_DISK_FAULTY:
4971 err = set_disk_faulty(mddev, new_decode_dev(arg));
4975 err = do_md_run(mddev);
4978 case SET_BITMAP_FILE:
4979 err = set_bitmap_file(mddev, (int)arg);
4989 mddev_unlock(mddev);
4999 static int md_open(struct inode *inode, struct file *file)
5002 * Succeed if we can lock the mddev, which confirms that
5003 * it isn't being stopped right now.
5005 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
5008 if ((err = mutex_lock_interruptible_nested(&mddev->reconfig_mutex, 1)))
5013 atomic_inc(&mddev->openers);
5014 mddev_unlock(mddev);
5016 check_disk_change(inode->i_bdev);
5021 static int md_release(struct inode *inode, struct file * file)
5023 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
5026 atomic_dec(&mddev->openers);
5032 static int md_media_changed(struct gendisk *disk)
5034 mddev_t *mddev = disk->private_data;
5036 return mddev->changed;
5039 static int md_revalidate(struct gendisk *disk)
5041 mddev_t *mddev = disk->private_data;
5046 static struct block_device_operations md_fops =
5048 .owner = THIS_MODULE,
5050 .release = md_release,
5052 .getgeo = md_getgeo,
5053 .media_changed = md_media_changed,
5054 .revalidate_disk= md_revalidate,
5057 static int md_thread(void * arg)
5059 mdk_thread_t *thread = arg;
5062 * md_thread is a 'system-thread', it's priority should be very
5063 * high. We avoid resource deadlocks individually in each
5064 * raid personality. (RAID5 does preallocation) We also use RR and
5065 * the very same RT priority as kswapd, thus we will never get
5066 * into a priority inversion deadlock.
5068 * we definitely have to have equal or higher priority than
5069 * bdflush, otherwise bdflush will deadlock if there are too
5070 * many dirty RAID5 blocks.
5073 allow_signal(SIGKILL);
5074 while (!kthread_should_stop()) {
5076 /* We need to wait INTERRUPTIBLE so that
5077 * we don't add to the load-average.
5078 * That means we need to be sure no signals are
5081 if (signal_pending(current))
5082 flush_signals(current);
5084 wait_event_interruptible_timeout
5086 test_bit(THREAD_WAKEUP, &thread->flags)
5087 || kthread_should_stop(),
5090 clear_bit(THREAD_WAKEUP, &thread->flags);
5092 thread->run(thread->mddev);
5098 void md_wakeup_thread(mdk_thread_t *thread)
5101 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5102 set_bit(THREAD_WAKEUP, &thread->flags);
5103 wake_up(&thread->wqueue);
5107 mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5110 mdk_thread_t *thread;
5112 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
5116 init_waitqueue_head(&thread->wqueue);
5119 thread->mddev = mddev;
5120 thread->timeout = MAX_SCHEDULE_TIMEOUT;
5121 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
5122 if (IS_ERR(thread->tsk)) {
5129 void md_unregister_thread(mdk_thread_t *thread)
5131 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
5133 kthread_stop(thread->tsk);
5137 void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
5144 if (!rdev || test_bit(Faulty, &rdev->flags))
5147 if (mddev->external)
5148 set_bit(Blocked, &rdev->flags);
5150 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5152 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5153 __builtin_return_address(0),__builtin_return_address(1),
5154 __builtin_return_address(2),__builtin_return_address(3));
5158 if (!mddev->pers->error_handler)
5160 mddev->pers->error_handler(mddev,rdev);
5161 if (mddev->degraded)
5162 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
5163 set_bit(StateChanged, &rdev->flags);
5164 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5165 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5166 md_wakeup_thread(mddev->thread);
5167 md_new_event_inintr(mddev);
5170 /* seq_file implementation /proc/mdstat */
5172 static void status_unused(struct seq_file *seq)
5176 struct list_head *tmp;
5178 seq_printf(seq, "unused devices: ");
5180 rdev_for_each_list(rdev, tmp, pending_raid_disks) {
5181 char b[BDEVNAME_SIZE];
5183 seq_printf(seq, "%s ",
5184 bdevname(rdev->bdev,b));
5187 seq_printf(seq, "<none>");
5189 seq_printf(seq, "\n");
5193 static void status_resync(struct seq_file *seq, mddev_t * mddev)
5195 sector_t max_blocks, resync, res;
5196 unsigned long dt, db, rt;
5198 unsigned int per_milli;
5200 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
5202 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
5203 max_blocks = mddev->resync_max_sectors >> 1;
5205 max_blocks = mddev->size;
5208 * Should not happen.
5214 /* Pick 'scale' such that (resync>>scale)*1000 will fit
5215 * in a sector_t, and (max_blocks>>scale) will fit in a
5216 * u32, as those are the requirements for sector_div.
5217 * Thus 'scale' must be at least 10
5220 if (sizeof(sector_t) > sizeof(unsigned long)) {
5221 while ( max_blocks/2 > (1ULL<<(scale+32)))
5224 res = (resync>>scale)*1000;
5225 sector_div(res, (u32)((max_blocks>>scale)+1));
5229 int i, x = per_milli/50, y = 20-x;
5230 seq_printf(seq, "[");
5231 for (i = 0; i < x; i++)
5232 seq_printf(seq, "=");
5233 seq_printf(seq, ">");
5234 for (i = 0; i < y; i++)
5235 seq_printf(seq, ".");
5236 seq_printf(seq, "] ");
5238 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
5239 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
5241 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
5243 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
5244 "resync" : "recovery"))),
5245 per_milli/10, per_milli % 10,
5246 (unsigned long long) resync,
5247 (unsigned long long) max_blocks);
5250 * We do not want to overflow, so the order of operands and
5251 * the * 100 / 100 trick are important. We do a +1 to be
5252 * safe against division by zero. We only estimate anyway.
5254 * dt: time from mark until now
5255 * db: blocks written from mark until now
5256 * rt: remaining time
5258 dt = ((jiffies - mddev->resync_mark) / HZ);
5260 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
5261 - mddev->resync_mark_cnt;
5262 rt = (dt * ((unsigned long)(max_blocks-resync) / (db/2/100+1)))/100;
5264 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
5266 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
5269 static void *md_seq_start(struct seq_file *seq, loff_t *pos)
5271 struct list_head *tmp;
5281 spin_lock(&all_mddevs_lock);
5282 list_for_each(tmp,&all_mddevs)
5284 mddev = list_entry(tmp, mddev_t, all_mddevs);
5286 spin_unlock(&all_mddevs_lock);
5289 spin_unlock(&all_mddevs_lock);
5291 return (void*)2;/* tail */
5295 static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
5297 struct list_head *tmp;
5298 mddev_t *next_mddev, *mddev = v;
5304 spin_lock(&all_mddevs_lock);
5306 tmp = all_mddevs.next;
5308 tmp = mddev->all_mddevs.next;
5309 if (tmp != &all_mddevs)
5310 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
5312 next_mddev = (void*)2;
5315 spin_unlock(&all_mddevs_lock);
5323 static void md_seq_stop(struct seq_file *seq, void *v)
5327 if (mddev && v != (void*)1 && v != (void*)2)
5331 struct mdstat_info {
5335 static int md_seq_show(struct seq_file *seq, void *v)
5339 struct list_head *tmp2;
5341 struct mdstat_info *mi = seq->private;
5342 struct bitmap *bitmap;
5344 if (v == (void*)1) {
5345 struct mdk_personality *pers;
5346 seq_printf(seq, "Personalities : ");
5347 spin_lock(&pers_lock);
5348 list_for_each_entry(pers, &pers_list, list)
5349 seq_printf(seq, "[%s] ", pers->name);
5351 spin_unlock(&pers_lock);
5352 seq_printf(seq, "\n");
5353 mi->event = atomic_read(&md_event_count);
5356 if (v == (void*)2) {
5361 if (mddev_lock(mddev) < 0)
5364 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
5365 seq_printf(seq, "%s : %sactive", mdname(mddev),
5366 mddev->pers ? "" : "in");
5369 seq_printf(seq, " (read-only)");
5371 seq_printf(seq, " (auto-read-only)");
5372 seq_printf(seq, " %s", mddev->pers->name);
5376 rdev_for_each(rdev, tmp2, mddev) {
5377 char b[BDEVNAME_SIZE];
5378 seq_printf(seq, " %s[%d]",
5379 bdevname(rdev->bdev,b), rdev->desc_nr);
5380 if (test_bit(WriteMostly, &rdev->flags))
5381 seq_printf(seq, "(W)");
5382 if (test_bit(Faulty, &rdev->flags)) {
5383 seq_printf(seq, "(F)");
5385 } else if (rdev->raid_disk < 0)
5386 seq_printf(seq, "(S)"); /* spare */
5390 if (!list_empty(&mddev->disks)) {
5392 seq_printf(seq, "\n %llu blocks",
5393 (unsigned long long)
5394 mddev->array_sectors / 2);
5396 seq_printf(seq, "\n %llu blocks",
5397 (unsigned long long)size);
5399 if (mddev->persistent) {
5400 if (mddev->major_version != 0 ||
5401 mddev->minor_version != 90) {
5402 seq_printf(seq," super %d.%d",
5403 mddev->major_version,
5404 mddev->minor_version);
5406 } else if (mddev->external)
5407 seq_printf(seq, " super external:%s",
5408 mddev->metadata_type);
5410 seq_printf(seq, " super non-persistent");
5413 mddev->pers->status(seq, mddev);
5414 seq_printf(seq, "\n ");
5415 if (mddev->pers->sync_request) {
5416 if (mddev->curr_resync > 2) {
5417 status_resync(seq, mddev);
5418 seq_printf(seq, "\n ");
5419 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
5420 seq_printf(seq, "\tresync=DELAYED\n ");
5421 else if (mddev->recovery_cp < MaxSector)
5422 seq_printf(seq, "\tresync=PENDING\n ");
5425 seq_printf(seq, "\n ");
5427 if ((bitmap = mddev->bitmap)) {
5428 unsigned long chunk_kb;
5429 unsigned long flags;
5430 spin_lock_irqsave(&bitmap->lock, flags);
5431 chunk_kb = bitmap->chunksize >> 10;
5432 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
5434 bitmap->pages - bitmap->missing_pages,
5436 (bitmap->pages - bitmap->missing_pages)
5437 << (PAGE_SHIFT - 10),
5438 chunk_kb ? chunk_kb : bitmap->chunksize,
5439 chunk_kb ? "KB" : "B");
5441 seq_printf(seq, ", file: ");
5442 seq_path(seq, &bitmap->file->f_path, " \t\n");
5445 seq_printf(seq, "\n");
5446 spin_unlock_irqrestore(&bitmap->lock, flags);
5449 seq_printf(seq, "\n");
5451 mddev_unlock(mddev);
5456 static struct seq_operations md_seq_ops = {
5457 .start = md_seq_start,
5458 .next = md_seq_next,
5459 .stop = md_seq_stop,
5460 .show = md_seq_show,
5463 static int md_seq_open(struct inode *inode, struct file *file)
5466 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
5470 error = seq_open(file, &md_seq_ops);
5474 struct seq_file *p = file->private_data;
5476 mi->event = atomic_read(&md_event_count);
5481 static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
5483 struct seq_file *m = filp->private_data;
5484 struct mdstat_info *mi = m->private;
5487 poll_wait(filp, &md_event_waiters, wait);
5489 /* always allow read */
5490 mask = POLLIN | POLLRDNORM;
5492 if (mi->event != atomic_read(&md_event_count))
5493 mask |= POLLERR | POLLPRI;
5497 static const struct file_operations md_seq_fops = {
5498 .owner = THIS_MODULE,
5499 .open = md_seq_open,
5501 .llseek = seq_lseek,
5502 .release = seq_release_private,
5503 .poll = mdstat_poll,
5506 int register_md_personality(struct mdk_personality *p)
5508 spin_lock(&pers_lock);
5509 list_add_tail(&p->list, &pers_list);
5510 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
5511 spin_unlock(&pers_lock);
5515 int unregister_md_personality(struct mdk_personality *p)
5517 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
5518 spin_lock(&pers_lock);
5519 list_del_init(&p->list);
5520 spin_unlock(&pers_lock);
5524 static int is_mddev_idle(mddev_t *mddev)
5532 rdev_for_each_rcu(rdev, mddev) {
5533 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
5534 curr_events = part_stat_read(&disk->part0, sectors[0]) +
5535 part_stat_read(&disk->part0, sectors[1]) -
5536 atomic_read(&disk->sync_io);
5537 /* sync IO will cause sync_io to increase before the disk_stats
5538 * as sync_io is counted when a request starts, and
5539 * disk_stats is counted when it completes.
5540 * So resync activity will cause curr_events to be smaller than
5541 * when there was no such activity.
5542 * non-sync IO will cause disk_stat to increase without
5543 * increasing sync_io so curr_events will (eventually)
5544 * be larger than it was before. Once it becomes
5545 * substantially larger, the test below will cause
5546 * the array to appear non-idle, and resync will slow
5548 * If there is a lot of outstanding resync activity when
5549 * we set last_event to curr_events, then all that activity
5550 * completing might cause the array to appear non-idle
5551 * and resync will be slowed down even though there might
5552 * not have been non-resync activity. This will only
5553 * happen once though. 'last_events' will soon reflect
5554 * the state where there is little or no outstanding
5555 * resync requests, and further resync activity will
5556 * always make curr_events less than last_events.
5559 if (curr_events - rdev->last_events > 4096) {
5560 rdev->last_events = curr_events;
5568 void md_done_sync(mddev_t *mddev, int blocks, int ok)
5570 /* another "blocks" (512byte) blocks have been synced */
5571 atomic_sub(blocks, &mddev->recovery_active);
5572 wake_up(&mddev->recovery_wait);
5574 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5575 md_wakeup_thread(mddev->thread);
5576 // stop recovery, signal do_sync ....
5581 /* md_write_start(mddev, bi)
5582 * If we need to update some array metadata (e.g. 'active' flag
5583 * in superblock) before writing, schedule a superblock update
5584 * and wait for it to complete.
5586 void md_write_start(mddev_t *mddev, struct bio *bi)
5589 if (bio_data_dir(bi) != WRITE)
5592 BUG_ON(mddev->ro == 1);
5593 if (mddev->ro == 2) {
5594 /* need to switch to read/write */
5596 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5597 md_wakeup_thread(mddev->thread);
5598 md_wakeup_thread(mddev->sync_thread);
5601 atomic_inc(&mddev->writes_pending);
5602 if (mddev->safemode == 1)
5603 mddev->safemode = 0;
5604 if (mddev->in_sync) {
5605 spin_lock_irq(&mddev->write_lock);
5606 if (mddev->in_sync) {
5608 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
5609 md_wakeup_thread(mddev->thread);
5612 spin_unlock_irq(&mddev->write_lock);
5615 sysfs_notify(&mddev->kobj, NULL, "array_state");
5616 wait_event(mddev->sb_wait,
5617 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
5618 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
5621 void md_write_end(mddev_t *mddev)
5623 if (atomic_dec_and_test(&mddev->writes_pending)) {
5624 if (mddev->safemode == 2)
5625 md_wakeup_thread(mddev->thread);
5626 else if (mddev->safemode_delay)
5627 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
5631 /* md_allow_write(mddev)
5632 * Calling this ensures that the array is marked 'active' so that writes
5633 * may proceed without blocking. It is important to call this before
5634 * attempting a GFP_KERNEL allocation while holding the mddev lock.
5635 * Must be called with mddev_lock held.
5637 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
5638 * is dropped, so return -EAGAIN after notifying userspace.
5640 int md_allow_write(mddev_t *mddev)
5646 if (!mddev->pers->sync_request)
5649 spin_lock_irq(&mddev->write_lock);
5650 if (mddev->in_sync) {
5652 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
5653 if (mddev->safemode_delay &&
5654 mddev->safemode == 0)
5655 mddev->safemode = 1;
5656 spin_unlock_irq(&mddev->write_lock);
5657 md_update_sb(mddev, 0);
5658 sysfs_notify(&mddev->kobj, NULL, "array_state");
5660 spin_unlock_irq(&mddev->write_lock);
5662 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
5667 EXPORT_SYMBOL_GPL(md_allow_write);
5669 #define SYNC_MARKS 10
5670 #define SYNC_MARK_STEP (3*HZ)
5671 void md_do_sync(mddev_t *mddev)
5674 unsigned int currspeed = 0,
5676 sector_t max_sectors,j, io_sectors;
5677 unsigned long mark[SYNC_MARKS];
5678 sector_t mark_cnt[SYNC_MARKS];
5680 struct list_head *tmp;
5681 sector_t last_check;
5683 struct list_head *rtmp;
5687 /* just incase thread restarts... */
5688 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
5690 if (mddev->ro) /* never try to sync a read-only array */
5693 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5694 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
5695 desc = "data-check";
5696 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5697 desc = "requested-resync";
5700 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5705 /* we overload curr_resync somewhat here.
5706 * 0 == not engaged in resync at all
5707 * 2 == checking that there is no conflict with another sync
5708 * 1 == like 2, but have yielded to allow conflicting resync to
5710 * other == active in resync - this many blocks
5712 * Before starting a resync we must have set curr_resync to
5713 * 2, and then checked that every "conflicting" array has curr_resync
5714 * less than ours. When we find one that is the same or higher
5715 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
5716 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
5717 * This will mean we have to start checking from the beginning again.
5722 mddev->curr_resync = 2;
5725 if (kthread_should_stop()) {
5726 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5729 for_each_mddev(mddev2, tmp) {
5730 if (mddev2 == mddev)
5732 if (!mddev->parallel_resync
5733 && mddev2->curr_resync
5734 && match_mddev_units(mddev, mddev2)) {
5736 if (mddev < mddev2 && mddev->curr_resync == 2) {
5737 /* arbitrarily yield */
5738 mddev->curr_resync = 1;
5739 wake_up(&resync_wait);
5741 if (mddev > mddev2 && mddev->curr_resync == 1)
5742 /* no need to wait here, we can wait the next
5743 * time 'round when curr_resync == 2
5746 /* We need to wait 'interruptible' so as not to
5747 * contribute to the load average, and not to
5748 * be caught by 'softlockup'
5750 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
5751 if (!kthread_should_stop() &&
5752 mddev2->curr_resync >= mddev->curr_resync) {
5753 printk(KERN_INFO "md: delaying %s of %s"
5754 " until %s has finished (they"
5755 " share one or more physical units)\n",
5756 desc, mdname(mddev), mdname(mddev2));
5758 if (signal_pending(current))
5759 flush_signals(current);
5761 finish_wait(&resync_wait, &wq);
5764 finish_wait(&resync_wait, &wq);
5767 } while (mddev->curr_resync < 2);
5770 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5771 /* resync follows the size requested by the personality,
5772 * which defaults to physical size, but can be virtual size
5774 max_sectors = mddev->resync_max_sectors;
5775 mddev->resync_mismatches = 0;
5776 /* we don't use the checkpoint if there's a bitmap */
5777 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5778 j = mddev->resync_min;
5779 else if (!mddev->bitmap)
5780 j = mddev->recovery_cp;
5782 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5783 max_sectors = mddev->size << 1;
5785 /* recovery follows the physical size of devices */
5786 max_sectors = mddev->size << 1;
5788 rdev_for_each(rdev, rtmp, mddev)
5789 if (rdev->raid_disk >= 0 &&
5790 !test_bit(Faulty, &rdev->flags) &&
5791 !test_bit(In_sync, &rdev->flags) &&
5792 rdev->recovery_offset < j)
5793 j = rdev->recovery_offset;
5796 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
5797 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
5798 " %d KB/sec/disk.\n", speed_min(mddev));
5799 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
5800 "(but not more than %d KB/sec) for %s.\n",
5801 speed_max(mddev), desc);
5803 is_mddev_idle(mddev); /* this also initializes IO event counters */
5806 for (m = 0; m < SYNC_MARKS; m++) {
5808 mark_cnt[m] = io_sectors;
5811 mddev->resync_mark = mark[last_mark];
5812 mddev->resync_mark_cnt = mark_cnt[last_mark];
5815 * Tune reconstruction:
5817 window = 32*(PAGE_SIZE/512);
5818 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
5819 window/2,(unsigned long long) max_sectors/2);
5821 atomic_set(&mddev->recovery_active, 0);
5826 "md: resuming %s of %s from checkpoint.\n",
5827 desc, mdname(mddev));
5828 mddev->curr_resync = j;
5831 while (j < max_sectors) {
5835 if (j >= mddev->resync_max) {
5836 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
5837 wait_event(mddev->recovery_wait,
5838 mddev->resync_max > j
5839 || kthread_should_stop());
5841 if (kthread_should_stop())
5843 sectors = mddev->pers->sync_request(mddev, j, &skipped,
5844 currspeed < speed_min(mddev));
5846 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5850 if (!skipped) { /* actual IO requested */
5851 io_sectors += sectors;
5852 atomic_add(sectors, &mddev->recovery_active);
5856 if (j>1) mddev->curr_resync = j;
5857 mddev->curr_mark_cnt = io_sectors;
5858 if (last_check == 0)
5859 /* this is the earliers that rebuilt will be
5860 * visible in /proc/mdstat
5862 md_new_event(mddev);
5864 if (last_check + window > io_sectors || j == max_sectors)
5867 last_check = io_sectors;
5869 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5873 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
5875 int next = (last_mark+1) % SYNC_MARKS;
5877 mddev->resync_mark = mark[next];
5878 mddev->resync_mark_cnt = mark_cnt[next];
5879 mark[next] = jiffies;
5880 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
5885 if (kthread_should_stop())
5890 * this loop exits only if either when we are slower than
5891 * the 'hard' speed limit, or the system was IO-idle for
5893 * the system might be non-idle CPU-wise, but we only care
5894 * about not overloading the IO subsystem. (things like an
5895 * e2fsck being done on the RAID array should execute fast)
5897 blk_unplug(mddev->queue);
5900 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
5901 /((jiffies-mddev->resync_mark)/HZ +1) +1;
5903 if (currspeed > speed_min(mddev)) {
5904 if ((currspeed > speed_max(mddev)) ||
5905 !is_mddev_idle(mddev)) {
5911 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
5913 * this also signals 'finished resyncing' to md_stop
5916 blk_unplug(mddev->queue);
5918 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
5920 /* tell personality that we are finished */
5921 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
5923 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
5924 mddev->curr_resync > 2) {
5925 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5926 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5927 if (mddev->curr_resync >= mddev->recovery_cp) {
5929 "md: checkpointing %s of %s.\n",
5930 desc, mdname(mddev));
5931 mddev->recovery_cp = mddev->curr_resync;
5934 mddev->recovery_cp = MaxSector;
5936 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5937 mddev->curr_resync = MaxSector;
5938 rdev_for_each(rdev, rtmp, mddev)
5939 if (rdev->raid_disk >= 0 &&
5940 !test_bit(Faulty, &rdev->flags) &&
5941 !test_bit(In_sync, &rdev->flags) &&
5942 rdev->recovery_offset < mddev->curr_resync)
5943 rdev->recovery_offset = mddev->curr_resync;
5946 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5949 mddev->curr_resync = 0;
5950 mddev->resync_min = 0;
5951 mddev->resync_max = MaxSector;
5952 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
5953 wake_up(&resync_wait);
5954 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
5955 md_wakeup_thread(mddev->thread);
5960 * got a signal, exit.
5963 "md: md_do_sync() got signal ... exiting\n");
5964 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5968 EXPORT_SYMBOL_GPL(md_do_sync);
5971 static int remove_and_add_spares(mddev_t *mddev)
5974 struct list_head *rtmp;
5977 rdev_for_each(rdev, rtmp, mddev)
5978 if (rdev->raid_disk >= 0 &&
5979 !test_bit(Blocked, &rdev->flags) &&
5980 (test_bit(Faulty, &rdev->flags) ||
5981 ! test_bit(In_sync, &rdev->flags)) &&
5982 atomic_read(&rdev->nr_pending)==0) {
5983 if (mddev->pers->hot_remove_disk(
5984 mddev, rdev->raid_disk)==0) {
5986 sprintf(nm,"rd%d", rdev->raid_disk);
5987 sysfs_remove_link(&mddev->kobj, nm);
5988 rdev->raid_disk = -1;
5992 if (mddev->degraded && ! mddev->ro) {
5993 rdev_for_each(rdev, rtmp, mddev) {
5994 if (rdev->raid_disk >= 0 &&
5995 !test_bit(In_sync, &rdev->flags) &&
5996 !test_bit(Blocked, &rdev->flags))
5998 if (rdev->raid_disk < 0
5999 && !test_bit(Faulty, &rdev->flags)) {
6000 rdev->recovery_offset = 0;
6002 hot_add_disk(mddev, rdev) == 0) {
6004 sprintf(nm, "rd%d", rdev->raid_disk);
6005 if (sysfs_create_link(&mddev->kobj,
6008 "md: cannot register "
6012 md_new_event(mddev);
6021 * This routine is regularly called by all per-raid-array threads to
6022 * deal with generic issues like resync and super-block update.
6023 * Raid personalities that don't have a thread (linear/raid0) do not
6024 * need this as they never do any recovery or update the superblock.
6026 * It does not do any resync itself, but rather "forks" off other threads
6027 * to do that as needed.
6028 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6029 * "->recovery" and create a thread at ->sync_thread.
6030 * When the thread finishes it sets MD_RECOVERY_DONE
6031 * and wakeups up this thread which will reap the thread and finish up.
6032 * This thread also removes any faulty devices (with nr_pending == 0).
6034 * The overall approach is:
6035 * 1/ if the superblock needs updating, update it.
6036 * 2/ If a recovery thread is running, don't do anything else.
6037 * 3/ If recovery has finished, clean up, possibly marking spares active.
6038 * 4/ If there are any faulty devices, remove them.
6039 * 5/ If array is degraded, try to add spares devices
6040 * 6/ If array has spares or is not in-sync, start a resync thread.
6042 void md_check_recovery(mddev_t *mddev)
6045 struct list_head *rtmp;
6049 bitmap_daemon_work(mddev->bitmap);
6051 if (test_and_clear_bit(MD_NOTIFY_ARRAY_STATE, &mddev->flags))
6052 sysfs_notify(&mddev->kobj, NULL, "array_state");
6057 if (signal_pending(current)) {
6058 if (mddev->pers->sync_request && !mddev->external) {
6059 printk(KERN_INFO "md: %s in immediate safe mode\n",
6061 mddev->safemode = 2;
6063 flush_signals(current);
6066 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
6069 (mddev->flags && !mddev->external) ||
6070 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
6071 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
6072 (mddev->external == 0 && mddev->safemode == 1) ||
6073 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6074 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
6078 if (mddev_trylock(mddev)) {
6082 /* Only thing we do on a ro array is remove
6085 remove_and_add_spares(mddev);
6086 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6090 if (!mddev->external) {
6092 spin_lock_irq(&mddev->write_lock);
6093 if (mddev->safemode &&
6094 !atomic_read(&mddev->writes_pending) &&
6096 mddev->recovery_cp == MaxSector) {
6099 if (mddev->persistent)
6100 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6102 if (mddev->safemode == 1)
6103 mddev->safemode = 0;
6104 spin_unlock_irq(&mddev->write_lock);
6106 sysfs_notify(&mddev->kobj, NULL, "array_state");
6110 md_update_sb(mddev, 0);
6112 rdev_for_each(rdev, rtmp, mddev)
6113 if (test_and_clear_bit(StateChanged, &rdev->flags))
6114 sysfs_notify(&rdev->kobj, NULL, "state");
6117 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
6118 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
6119 /* resync/recovery still happening */
6120 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6123 if (mddev->sync_thread) {
6124 /* resync has finished, collect result */
6125 md_unregister_thread(mddev->sync_thread);
6126 mddev->sync_thread = NULL;
6127 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
6128 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
6130 /* activate any spares */
6131 if (mddev->pers->spare_active(mddev))
6132 sysfs_notify(&mddev->kobj, NULL,
6135 md_update_sb(mddev, 1);
6137 /* if array is no-longer degraded, then any saved_raid_disk
6138 * information must be scrapped
6140 if (!mddev->degraded)
6141 rdev_for_each(rdev, rtmp, mddev)
6142 rdev->saved_raid_disk = -1;
6144 mddev->recovery = 0;
6145 /* flag recovery needed just to double check */
6146 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6147 sysfs_notify(&mddev->kobj, NULL, "sync_action");
6148 md_new_event(mddev);
6151 /* Set RUNNING before clearing NEEDED to avoid
6152 * any transients in the value of "sync_action".
6154 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6155 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6156 /* Clear some bits that don't mean anything, but
6159 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
6160 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
6162 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
6164 /* no recovery is running.
6165 * remove any failed drives, then
6166 * add spares if possible.
6167 * Spare are also removed and re-added, to allow
6168 * the personality to fail the re-add.
6171 if (mddev->reshape_position != MaxSector) {
6172 if (mddev->pers->check_reshape(mddev) != 0)
6173 /* Cannot proceed */
6175 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
6176 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
6177 } else if ((spares = remove_and_add_spares(mddev))) {
6178 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6179 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
6180 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
6181 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
6182 } else if (mddev->recovery_cp < MaxSector) {
6183 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6184 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
6185 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
6186 /* nothing to be done ... */
6189 if (mddev->pers->sync_request) {
6190 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
6191 /* We are adding a device or devices to an array
6192 * which has the bitmap stored on all devices.
6193 * So make sure all bitmap pages get written
6195 bitmap_write_all(mddev->bitmap);
6197 mddev->sync_thread = md_register_thread(md_do_sync,
6200 if (!mddev->sync_thread) {
6201 printk(KERN_ERR "%s: could not start resync"
6204 /* leave the spares where they are, it shouldn't hurt */
6205 mddev->recovery = 0;
6207 md_wakeup_thread(mddev->sync_thread);
6208 sysfs_notify(&mddev->kobj, NULL, "sync_action");
6209 md_new_event(mddev);
6212 if (!mddev->sync_thread) {
6213 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6214 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
6216 sysfs_notify(&mddev->kobj, NULL, "sync_action");
6218 mddev_unlock(mddev);
6222 void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
6224 sysfs_notify(&rdev->kobj, NULL, "state");
6225 wait_event_timeout(rdev->blocked_wait,
6226 !test_bit(Blocked, &rdev->flags),
6227 msecs_to_jiffies(5000));
6228 rdev_dec_pending(rdev, mddev);
6230 EXPORT_SYMBOL(md_wait_for_blocked_rdev);
6232 static int md_notify_reboot(struct notifier_block *this,
6233 unsigned long code, void *x)
6235 struct list_head *tmp;
6238 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
6240 printk(KERN_INFO "md: stopping all md devices.\n");
6242 for_each_mddev(mddev, tmp)
6243 if (mddev_trylock(mddev)) {
6244 /* Force a switch to readonly even array
6245 * appears to still be in use. Hence
6248 do_md_stop(mddev, 1, 100);
6249 mddev_unlock(mddev);
6252 * certain more exotic SCSI devices are known to be
6253 * volatile wrt too early system reboots. While the
6254 * right place to handle this issue is the given
6255 * driver, we do want to have a safe RAID driver ...
6262 static struct notifier_block md_notifier = {
6263 .notifier_call = md_notify_reboot,
6265 .priority = INT_MAX, /* before any real devices */
6268 static void md_geninit(void)
6270 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
6272 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
6275 static int __init md_init(void)
6277 if (register_blkdev(MAJOR_NR, "md"))
6279 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
6280 unregister_blkdev(MAJOR_NR, "md");
6283 blk_register_region(MKDEV(MAJOR_NR, 0), 1UL<<MINORBITS, THIS_MODULE,
6284 md_probe, NULL, NULL);
6285 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
6286 md_probe, NULL, NULL);
6288 register_reboot_notifier(&md_notifier);
6289 raid_table_header = register_sysctl_table(raid_root_table);
6299 * Searches all registered partitions for autorun RAID arrays
6303 static LIST_HEAD(all_detected_devices);
6304 struct detected_devices_node {
6305 struct list_head list;
6309 void md_autodetect_dev(dev_t dev)
6311 struct detected_devices_node *node_detected_dev;
6313 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
6314 if (node_detected_dev) {
6315 node_detected_dev->dev = dev;
6316 list_add_tail(&node_detected_dev->list, &all_detected_devices);
6318 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
6319 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
6324 static void autostart_arrays(int part)
6327 struct detected_devices_node *node_detected_dev;
6329 int i_scanned, i_passed;
6334 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
6336 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
6338 node_detected_dev = list_entry(all_detected_devices.next,
6339 struct detected_devices_node, list);
6340 list_del(&node_detected_dev->list);
6341 dev = node_detected_dev->dev;
6342 kfree(node_detected_dev);
6343 rdev = md_import_device(dev,0, 90);
6347 if (test_bit(Faulty, &rdev->flags)) {
6351 set_bit(AutoDetected, &rdev->flags);
6352 list_add(&rdev->same_set, &pending_raid_disks);
6356 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
6357 i_scanned, i_passed);
6359 autorun_devices(part);
6362 #endif /* !MODULE */
6364 static __exit void md_exit(void)
6367 struct list_head *tmp;
6369 blk_unregister_region(MKDEV(MAJOR_NR,0), 1U << MINORBITS);
6370 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
6372 unregister_blkdev(MAJOR_NR,"md");
6373 unregister_blkdev(mdp_major, "mdp");
6374 unregister_reboot_notifier(&md_notifier);
6375 unregister_sysctl_table(raid_table_header);
6376 remove_proc_entry("mdstat", NULL);
6377 for_each_mddev(mddev, tmp) {
6378 struct gendisk *disk = mddev->gendisk;
6381 export_array(mddev);
6384 mddev->gendisk = NULL;
6389 subsys_initcall(md_init);
6390 module_exit(md_exit)
6392 static int get_ro(char *buffer, struct kernel_param *kp)
6394 return sprintf(buffer, "%d", start_readonly);
6396 static int set_ro(const char *val, struct kernel_param *kp)
6399 int num = simple_strtoul(val, &e, 10);
6400 if (*val && (*e == '\0' || *e == '\n')) {
6401 start_readonly = num;
6407 module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
6408 module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
6411 EXPORT_SYMBOL(register_md_personality);
6412 EXPORT_SYMBOL(unregister_md_personality);
6413 EXPORT_SYMBOL(md_error);
6414 EXPORT_SYMBOL(md_done_sync);
6415 EXPORT_SYMBOL(md_write_start);
6416 EXPORT_SYMBOL(md_write_end);
6417 EXPORT_SYMBOL(md_register_thread);
6418 EXPORT_SYMBOL(md_unregister_thread);
6419 EXPORT_SYMBOL(md_wakeup_thread);
6420 EXPORT_SYMBOL(md_check_recovery);
6421 MODULE_LICENSE("GPL");
6423 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);