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/blkdev.h>
37 #include <linux/sysctl.h>
38 #include <linux/seq_file.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>
48 #include <linux/raid/md_p.h>
49 #include <linux/raid/md_u.h>
54 #define dprintk(x...) ((void)(DEBUG && printk(x)))
58 static void autostart_arrays(int part);
61 static LIST_HEAD(pers_list);
62 static DEFINE_SPINLOCK(pers_lock);
64 static void md_print_devices(void);
66 static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
68 #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
71 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
72 * is 1000 KB/sec, so the extra system load does not show up that much.
73 * Increase it if you want to have more _guaranteed_ speed. Note that
74 * the RAID driver will use the maximum available bandwidth if the IO
75 * subsystem is idle. There is also an 'absolute maximum' reconstruction
76 * speed limit - in case reconstruction slows down your system despite
79 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
80 * or /sys/block/mdX/md/sync_speed_{min,max}
83 static int sysctl_speed_limit_min = 1000;
84 static int sysctl_speed_limit_max = 200000;
85 static inline int speed_min(mddev_t *mddev)
87 return mddev->sync_speed_min ?
88 mddev->sync_speed_min : sysctl_speed_limit_min;
91 static inline int speed_max(mddev_t *mddev)
93 return mddev->sync_speed_max ?
94 mddev->sync_speed_max : sysctl_speed_limit_max;
97 static struct ctl_table_header *raid_table_header;
99 static ctl_table raid_table[] = {
101 .ctl_name = DEV_RAID_SPEED_LIMIT_MIN,
102 .procname = "speed_limit_min",
103 .data = &sysctl_speed_limit_min,
104 .maxlen = sizeof(int),
105 .mode = S_IRUGO|S_IWUSR,
106 .proc_handler = &proc_dointvec,
109 .ctl_name = DEV_RAID_SPEED_LIMIT_MAX,
110 .procname = "speed_limit_max",
111 .data = &sysctl_speed_limit_max,
112 .maxlen = sizeof(int),
113 .mode = S_IRUGO|S_IWUSR,
114 .proc_handler = &proc_dointvec,
119 static ctl_table raid_dir_table[] = {
121 .ctl_name = DEV_RAID,
124 .mode = S_IRUGO|S_IXUGO,
130 static ctl_table raid_root_table[] = {
136 .child = raid_dir_table,
141 static struct block_device_operations md_fops;
143 static int start_readonly;
146 * We have a system wide 'event count' that is incremented
147 * on any 'interesting' event, and readers of /proc/mdstat
148 * can use 'poll' or 'select' to find out when the event
152 * start array, stop array, error, add device, remove device,
153 * start build, activate spare
155 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
156 static atomic_t md_event_count;
157 void md_new_event(mddev_t *mddev)
159 atomic_inc(&md_event_count);
160 wake_up(&md_event_waiters);
162 EXPORT_SYMBOL_GPL(md_new_event);
164 /* Alternate version that can be called from interrupts
165 * when calling sysfs_notify isn't needed.
167 static void md_new_event_inintr(mddev_t *mddev)
169 atomic_inc(&md_event_count);
170 wake_up(&md_event_waiters);
174 * Enables to iterate over all existing md arrays
175 * all_mddevs_lock protects this list.
177 static LIST_HEAD(all_mddevs);
178 static DEFINE_SPINLOCK(all_mddevs_lock);
182 * iterates through all used mddevs in the system.
183 * We take care to grab the all_mddevs_lock whenever navigating
184 * the list, and to always hold a refcount when unlocked.
185 * Any code which breaks out of this loop while own
186 * a reference to the current mddev and must mddev_put it.
188 #define for_each_mddev(mddev,tmp) \
190 for (({ spin_lock(&all_mddevs_lock); \
191 tmp = all_mddevs.next; \
193 ({ if (tmp != &all_mddevs) \
194 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
195 spin_unlock(&all_mddevs_lock); \
196 if (mddev) mddev_put(mddev); \
197 mddev = list_entry(tmp, mddev_t, all_mddevs); \
198 tmp != &all_mddevs;}); \
199 ({ spin_lock(&all_mddevs_lock); \
204 /* Rather than calling directly into the personality make_request function,
205 * IO requests come here first so that we can check if the device is
206 * being suspended pending a reconfiguration.
207 * We hold a refcount over the call to ->make_request. By the time that
208 * call has finished, the bio has been linked into some internal structure
209 * and so is visible to ->quiesce(), so we don't need the refcount any more.
211 static int md_make_request(struct request_queue *q, struct bio *bio)
213 mddev_t *mddev = q->queuedata;
215 if (mddev == NULL || mddev->pers == NULL) {
220 if (mddev->suspended) {
223 prepare_to_wait(&mddev->sb_wait, &__wait,
224 TASK_UNINTERRUPTIBLE);
225 if (!mddev->suspended)
231 finish_wait(&mddev->sb_wait, &__wait);
233 atomic_inc(&mddev->active_io);
235 rv = mddev->pers->make_request(q, bio);
236 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
237 wake_up(&mddev->sb_wait);
242 static void mddev_suspend(mddev_t *mddev)
244 BUG_ON(mddev->suspended);
245 mddev->suspended = 1;
247 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
248 mddev->pers->quiesce(mddev, 1);
249 md_unregister_thread(mddev->thread);
250 mddev->thread = NULL;
251 /* we now know that no code is executing in the personality module,
252 * except possibly the tail end of a ->bi_end_io function, but that
253 * is certain to complete before the module has a chance to get
258 static void mddev_resume(mddev_t *mddev)
260 mddev->suspended = 0;
261 wake_up(&mddev->sb_wait);
262 mddev->pers->quiesce(mddev, 0);
266 static inline mddev_t *mddev_get(mddev_t *mddev)
268 atomic_inc(&mddev->active);
272 static void mddev_delayed_delete(struct work_struct *ws);
274 static void mddev_put(mddev_t *mddev)
276 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
278 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
279 !mddev->hold_active) {
280 list_del(&mddev->all_mddevs);
281 if (mddev->gendisk) {
282 /* we did a probe so need to clean up.
283 * Call schedule_work inside the spinlock
284 * so that flush_scheduled_work() after
285 * mddev_find will succeed in waiting for the
288 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
289 schedule_work(&mddev->del_work);
293 spin_unlock(&all_mddevs_lock);
296 static mddev_t * mddev_find(dev_t unit)
298 mddev_t *mddev, *new = NULL;
301 spin_lock(&all_mddevs_lock);
304 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
305 if (mddev->unit == unit) {
307 spin_unlock(&all_mddevs_lock);
313 list_add(&new->all_mddevs, &all_mddevs);
314 spin_unlock(&all_mddevs_lock);
315 new->hold_active = UNTIL_IOCTL;
319 /* find an unused unit number */
320 static int next_minor = 512;
321 int start = next_minor;
325 dev = MKDEV(MD_MAJOR, next_minor);
327 if (next_minor > MINORMASK)
329 if (next_minor == start) {
330 /* Oh dear, all in use. */
331 spin_unlock(&all_mddevs_lock);
337 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
338 if (mddev->unit == dev) {
344 new->md_minor = MINOR(dev);
345 new->hold_active = UNTIL_STOP;
346 list_add(&new->all_mddevs, &all_mddevs);
347 spin_unlock(&all_mddevs_lock);
350 spin_unlock(&all_mddevs_lock);
352 new = kzalloc(sizeof(*new), GFP_KERNEL);
357 if (MAJOR(unit) == MD_MAJOR)
358 new->md_minor = MINOR(unit);
360 new->md_minor = MINOR(unit) >> MdpMinorShift;
362 mutex_init(&new->reconfig_mutex);
363 INIT_LIST_HEAD(&new->disks);
364 INIT_LIST_HEAD(&new->all_mddevs);
365 init_timer(&new->safemode_timer);
366 atomic_set(&new->active, 1);
367 atomic_set(&new->openers, 0);
368 atomic_set(&new->active_io, 0);
369 spin_lock_init(&new->write_lock);
370 init_waitqueue_head(&new->sb_wait);
371 init_waitqueue_head(&new->recovery_wait);
372 new->reshape_position = MaxSector;
374 new->resync_max = MaxSector;
375 new->level = LEVEL_NONE;
380 static inline int mddev_lock(mddev_t * mddev)
382 return mutex_lock_interruptible(&mddev->reconfig_mutex);
385 static inline int mddev_is_locked(mddev_t *mddev)
387 return mutex_is_locked(&mddev->reconfig_mutex);
390 static inline int mddev_trylock(mddev_t * mddev)
392 return mutex_trylock(&mddev->reconfig_mutex);
395 static inline void mddev_unlock(mddev_t * mddev)
397 mutex_unlock(&mddev->reconfig_mutex);
399 md_wakeup_thread(mddev->thread);
402 static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
406 list_for_each_entry(rdev, &mddev->disks, same_set)
407 if (rdev->desc_nr == nr)
413 static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
417 list_for_each_entry(rdev, &mddev->disks, same_set)
418 if (rdev->bdev->bd_dev == dev)
424 static struct mdk_personality *find_pers(int level, char *clevel)
426 struct mdk_personality *pers;
427 list_for_each_entry(pers, &pers_list, list) {
428 if (level != LEVEL_NONE && pers->level == level)
430 if (strcmp(pers->name, clevel)==0)
436 /* return the offset of the super block in 512byte sectors */
437 static inline sector_t calc_dev_sboffset(struct block_device *bdev)
439 sector_t num_sectors = bdev->bd_inode->i_size / 512;
440 return MD_NEW_SIZE_SECTORS(num_sectors);
443 static int alloc_disk_sb(mdk_rdev_t * rdev)
448 rdev->sb_page = alloc_page(GFP_KERNEL);
449 if (!rdev->sb_page) {
450 printk(KERN_ALERT "md: out of memory.\n");
457 static void free_disk_sb(mdk_rdev_t * rdev)
460 put_page(rdev->sb_page);
462 rdev->sb_page = NULL;
469 static void super_written(struct bio *bio, int error)
471 mdk_rdev_t *rdev = bio->bi_private;
472 mddev_t *mddev = rdev->mddev;
474 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
475 printk("md: super_written gets error=%d, uptodate=%d\n",
476 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
477 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
478 md_error(mddev, rdev);
481 if (atomic_dec_and_test(&mddev->pending_writes))
482 wake_up(&mddev->sb_wait);
486 static void super_written_barrier(struct bio *bio, int error)
488 struct bio *bio2 = bio->bi_private;
489 mdk_rdev_t *rdev = bio2->bi_private;
490 mddev_t *mddev = rdev->mddev;
492 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
493 error == -EOPNOTSUPP) {
495 /* barriers don't appear to be supported :-( */
496 set_bit(BarriersNotsupp, &rdev->flags);
497 mddev->barriers_work = 0;
498 spin_lock_irqsave(&mddev->write_lock, flags);
499 bio2->bi_next = mddev->biolist;
500 mddev->biolist = bio2;
501 spin_unlock_irqrestore(&mddev->write_lock, flags);
502 wake_up(&mddev->sb_wait);
506 bio->bi_private = rdev;
507 super_written(bio, error);
511 void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
512 sector_t sector, int size, struct page *page)
514 /* write first size bytes of page to sector of rdev
515 * Increment mddev->pending_writes before returning
516 * and decrement it on completion, waking up sb_wait
517 * if zero is reached.
518 * If an error occurred, call md_error
520 * As we might need to resubmit the request if BIO_RW_BARRIER
521 * causes ENOTSUPP, we allocate a spare bio...
523 struct bio *bio = bio_alloc(GFP_NOIO, 1);
524 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNCIO) | (1<<BIO_RW_UNPLUG);
526 bio->bi_bdev = rdev->bdev;
527 bio->bi_sector = sector;
528 bio_add_page(bio, page, size, 0);
529 bio->bi_private = rdev;
530 bio->bi_end_io = super_written;
533 atomic_inc(&mddev->pending_writes);
534 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
536 rw |= (1<<BIO_RW_BARRIER);
537 rbio = bio_clone(bio, GFP_NOIO);
538 rbio->bi_private = bio;
539 rbio->bi_end_io = super_written_barrier;
540 submit_bio(rw, rbio);
545 void md_super_wait(mddev_t *mddev)
547 /* wait for all superblock writes that were scheduled to complete.
548 * if any had to be retried (due to BARRIER problems), retry them
552 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
553 if (atomic_read(&mddev->pending_writes)==0)
555 while (mddev->biolist) {
557 spin_lock_irq(&mddev->write_lock);
558 bio = mddev->biolist;
559 mddev->biolist = bio->bi_next ;
561 spin_unlock_irq(&mddev->write_lock);
562 submit_bio(bio->bi_rw, bio);
566 finish_wait(&mddev->sb_wait, &wq);
569 static void bi_complete(struct bio *bio, int error)
571 complete((struct completion*)bio->bi_private);
574 int sync_page_io(struct block_device *bdev, sector_t sector, int size,
575 struct page *page, int rw)
577 struct bio *bio = bio_alloc(GFP_NOIO, 1);
578 struct completion event;
581 rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG);
584 bio->bi_sector = sector;
585 bio_add_page(bio, page, size, 0);
586 init_completion(&event);
587 bio->bi_private = &event;
588 bio->bi_end_io = bi_complete;
590 wait_for_completion(&event);
592 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
596 EXPORT_SYMBOL_GPL(sync_page_io);
598 static int read_disk_sb(mdk_rdev_t * rdev, int size)
600 char b[BDEVNAME_SIZE];
601 if (!rdev->sb_page) {
609 if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ))
615 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
616 bdevname(rdev->bdev,b));
620 static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
622 return sb1->set_uuid0 == sb2->set_uuid0 &&
623 sb1->set_uuid1 == sb2->set_uuid1 &&
624 sb1->set_uuid2 == sb2->set_uuid2 &&
625 sb1->set_uuid3 == sb2->set_uuid3;
628 static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
631 mdp_super_t *tmp1, *tmp2;
633 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
634 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
636 if (!tmp1 || !tmp2) {
638 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
646 * nr_disks is not constant
651 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
659 static u32 md_csum_fold(u32 csum)
661 csum = (csum & 0xffff) + (csum >> 16);
662 return (csum & 0xffff) + (csum >> 16);
665 static unsigned int calc_sb_csum(mdp_super_t * sb)
668 u32 *sb32 = (u32*)sb;
670 unsigned int disk_csum, csum;
672 disk_csum = sb->sb_csum;
675 for (i = 0; i < MD_SB_BYTES/4 ; i++)
677 csum = (newcsum & 0xffffffff) + (newcsum>>32);
681 /* This used to use csum_partial, which was wrong for several
682 * reasons including that different results are returned on
683 * different architectures. It isn't critical that we get exactly
684 * the same return value as before (we always csum_fold before
685 * testing, and that removes any differences). However as we
686 * know that csum_partial always returned a 16bit value on
687 * alphas, do a fold to maximise conformity to previous behaviour.
689 sb->sb_csum = md_csum_fold(disk_csum);
691 sb->sb_csum = disk_csum;
698 * Handle superblock details.
699 * We want to be able to handle multiple superblock formats
700 * so we have a common interface to them all, and an array of
701 * different handlers.
702 * We rely on user-space to write the initial superblock, and support
703 * reading and updating of superblocks.
704 * Interface methods are:
705 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
706 * loads and validates a superblock on dev.
707 * if refdev != NULL, compare superblocks on both devices
709 * 0 - dev has a superblock that is compatible with refdev
710 * 1 - dev has a superblock that is compatible and newer than refdev
711 * so dev should be used as the refdev in future
712 * -EINVAL superblock incompatible or invalid
713 * -othererror e.g. -EIO
715 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
716 * Verify that dev is acceptable into mddev.
717 * The first time, mddev->raid_disks will be 0, and data from
718 * dev should be merged in. Subsequent calls check that dev
719 * is new enough. Return 0 or -EINVAL
721 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
722 * Update the superblock for rdev with data in mddev
723 * This does not write to disc.
729 struct module *owner;
730 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
732 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
733 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
734 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
735 sector_t num_sectors);
739 * Check that the given mddev has no bitmap.
741 * This function is called from the run method of all personalities that do not
742 * support bitmaps. It prints an error message and returns non-zero if mddev
743 * has a bitmap. Otherwise, it returns 0.
746 int md_check_no_bitmap(mddev_t *mddev)
748 if (!mddev->bitmap_file && !mddev->bitmap_offset)
750 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
751 mdname(mddev), mddev->pers->name);
754 EXPORT_SYMBOL(md_check_no_bitmap);
757 * load_super for 0.90.0
759 static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
761 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
766 * Calculate the position of the superblock (512byte sectors),
767 * it's at the end of the disk.
769 * It also happens to be a multiple of 4Kb.
771 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
773 ret = read_disk_sb(rdev, MD_SB_BYTES);
778 bdevname(rdev->bdev, b);
779 sb = (mdp_super_t*)page_address(rdev->sb_page);
781 if (sb->md_magic != MD_SB_MAGIC) {
782 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
787 if (sb->major_version != 0 ||
788 sb->minor_version < 90 ||
789 sb->minor_version > 91) {
790 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
791 sb->major_version, sb->minor_version,
796 if (sb->raid_disks <= 0)
799 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
800 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
805 rdev->preferred_minor = sb->md_minor;
806 rdev->data_offset = 0;
807 rdev->sb_size = MD_SB_BYTES;
809 if (sb->level == LEVEL_MULTIPATH)
812 rdev->desc_nr = sb->this_disk.number;
818 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
819 if (!uuid_equal(refsb, sb)) {
820 printk(KERN_WARNING "md: %s has different UUID to %s\n",
821 b, bdevname(refdev->bdev,b2));
824 if (!sb_equal(refsb, sb)) {
825 printk(KERN_WARNING "md: %s has same UUID"
826 " but different superblock to %s\n",
827 b, bdevname(refdev->bdev, b2));
831 ev2 = md_event(refsb);
837 rdev->sectors = rdev->sb_start;
839 if (rdev->sectors < sb->size * 2 && sb->level > 1)
840 /* "this cannot possibly happen" ... */
848 * validate_super for 0.90.0
850 static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
853 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
854 __u64 ev1 = md_event(sb);
856 rdev->raid_disk = -1;
857 clear_bit(Faulty, &rdev->flags);
858 clear_bit(In_sync, &rdev->flags);
859 clear_bit(WriteMostly, &rdev->flags);
860 clear_bit(BarriersNotsupp, &rdev->flags);
862 if (mddev->raid_disks == 0) {
863 mddev->major_version = 0;
864 mddev->minor_version = sb->minor_version;
865 mddev->patch_version = sb->patch_version;
867 mddev->chunk_sectors = sb->chunk_size >> 9;
868 mddev->ctime = sb->ctime;
869 mddev->utime = sb->utime;
870 mddev->level = sb->level;
871 mddev->clevel[0] = 0;
872 mddev->layout = sb->layout;
873 mddev->raid_disks = sb->raid_disks;
874 mddev->dev_sectors = sb->size * 2;
876 mddev->bitmap_offset = 0;
877 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
879 if (mddev->minor_version >= 91) {
880 mddev->reshape_position = sb->reshape_position;
881 mddev->delta_disks = sb->delta_disks;
882 mddev->new_level = sb->new_level;
883 mddev->new_layout = sb->new_layout;
884 mddev->new_chunk_sectors = sb->new_chunk >> 9;
886 mddev->reshape_position = MaxSector;
887 mddev->delta_disks = 0;
888 mddev->new_level = mddev->level;
889 mddev->new_layout = mddev->layout;
890 mddev->new_chunk_sectors = mddev->chunk_sectors;
893 if (sb->state & (1<<MD_SB_CLEAN))
894 mddev->recovery_cp = MaxSector;
896 if (sb->events_hi == sb->cp_events_hi &&
897 sb->events_lo == sb->cp_events_lo) {
898 mddev->recovery_cp = sb->recovery_cp;
900 mddev->recovery_cp = 0;
903 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
904 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
905 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
906 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
908 mddev->max_disks = MD_SB_DISKS;
910 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
911 mddev->bitmap_file == NULL)
912 mddev->bitmap_offset = mddev->default_bitmap_offset;
914 } else if (mddev->pers == NULL) {
915 /* Insist on good event counter while assembling */
917 if (ev1 < mddev->events)
919 } else if (mddev->bitmap) {
920 /* if adding to array with a bitmap, then we can accept an
921 * older device ... but not too old.
923 if (ev1 < mddev->bitmap->events_cleared)
926 if (ev1 < mddev->events)
927 /* just a hot-add of a new device, leave raid_disk at -1 */
931 if (mddev->level != LEVEL_MULTIPATH) {
932 desc = sb->disks + rdev->desc_nr;
934 if (desc->state & (1<<MD_DISK_FAULTY))
935 set_bit(Faulty, &rdev->flags);
936 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
937 desc->raid_disk < mddev->raid_disks */) {
938 set_bit(In_sync, &rdev->flags);
939 rdev->raid_disk = desc->raid_disk;
941 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
942 set_bit(WriteMostly, &rdev->flags);
943 } else /* MULTIPATH are always insync */
944 set_bit(In_sync, &rdev->flags);
949 * sync_super for 0.90.0
951 static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
955 int next_spare = mddev->raid_disks;
958 /* make rdev->sb match mddev data..
961 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
962 * 3/ any empty disks < next_spare become removed
964 * disks[0] gets initialised to REMOVED because
965 * we cannot be sure from other fields if it has
966 * been initialised or not.
969 int active=0, working=0,failed=0,spare=0,nr_disks=0;
971 rdev->sb_size = MD_SB_BYTES;
973 sb = (mdp_super_t*)page_address(rdev->sb_page);
975 memset(sb, 0, sizeof(*sb));
977 sb->md_magic = MD_SB_MAGIC;
978 sb->major_version = mddev->major_version;
979 sb->patch_version = mddev->patch_version;
980 sb->gvalid_words = 0; /* ignored */
981 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
982 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
983 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
984 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
986 sb->ctime = mddev->ctime;
987 sb->level = mddev->level;
988 sb->size = mddev->dev_sectors / 2;
989 sb->raid_disks = mddev->raid_disks;
990 sb->md_minor = mddev->md_minor;
991 sb->not_persistent = 0;
992 sb->utime = mddev->utime;
994 sb->events_hi = (mddev->events>>32);
995 sb->events_lo = (u32)mddev->events;
997 if (mddev->reshape_position == MaxSector)
998 sb->minor_version = 90;
1000 sb->minor_version = 91;
1001 sb->reshape_position = mddev->reshape_position;
1002 sb->new_level = mddev->new_level;
1003 sb->delta_disks = mddev->delta_disks;
1004 sb->new_layout = mddev->new_layout;
1005 sb->new_chunk = mddev->new_chunk_sectors << 9;
1007 mddev->minor_version = sb->minor_version;
1010 sb->recovery_cp = mddev->recovery_cp;
1011 sb->cp_events_hi = (mddev->events>>32);
1012 sb->cp_events_lo = (u32)mddev->events;
1013 if (mddev->recovery_cp == MaxSector)
1014 sb->state = (1<< MD_SB_CLEAN);
1016 sb->recovery_cp = 0;
1018 sb->layout = mddev->layout;
1019 sb->chunk_size = mddev->chunk_sectors << 9;
1021 if (mddev->bitmap && mddev->bitmap_file == NULL)
1022 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1024 sb->disks[0].state = (1<<MD_DISK_REMOVED);
1025 list_for_each_entry(rdev2, &mddev->disks, same_set) {
1028 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
1029 && !test_bit(Faulty, &rdev2->flags))
1030 desc_nr = rdev2->raid_disk;
1032 desc_nr = next_spare++;
1033 rdev2->desc_nr = desc_nr;
1034 d = &sb->disks[rdev2->desc_nr];
1036 d->number = rdev2->desc_nr;
1037 d->major = MAJOR(rdev2->bdev->bd_dev);
1038 d->minor = MINOR(rdev2->bdev->bd_dev);
1039 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
1040 && !test_bit(Faulty, &rdev2->flags))
1041 d->raid_disk = rdev2->raid_disk;
1043 d->raid_disk = rdev2->desc_nr; /* compatibility */
1044 if (test_bit(Faulty, &rdev2->flags))
1045 d->state = (1<<MD_DISK_FAULTY);
1046 else if (test_bit(In_sync, &rdev2->flags)) {
1047 d->state = (1<<MD_DISK_ACTIVE);
1048 d->state |= (1<<MD_DISK_SYNC);
1056 if (test_bit(WriteMostly, &rdev2->flags))
1057 d->state |= (1<<MD_DISK_WRITEMOSTLY);
1059 /* now set the "removed" and "faulty" bits on any missing devices */
1060 for (i=0 ; i < mddev->raid_disks ; i++) {
1061 mdp_disk_t *d = &sb->disks[i];
1062 if (d->state == 0 && d->number == 0) {
1065 d->state = (1<<MD_DISK_REMOVED);
1066 d->state |= (1<<MD_DISK_FAULTY);
1070 sb->nr_disks = nr_disks;
1071 sb->active_disks = active;
1072 sb->working_disks = working;
1073 sb->failed_disks = failed;
1074 sb->spare_disks = spare;
1076 sb->this_disk = sb->disks[rdev->desc_nr];
1077 sb->sb_csum = calc_sb_csum(sb);
1081 * rdev_size_change for 0.90.0
1083 static unsigned long long
1084 super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
1086 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1087 return 0; /* component must fit device */
1088 if (rdev->mddev->bitmap_offset)
1089 return 0; /* can't move bitmap */
1090 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
1091 if (!num_sectors || num_sectors > rdev->sb_start)
1092 num_sectors = rdev->sb_start;
1093 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1095 md_super_wait(rdev->mddev);
1096 return num_sectors / 2; /* kB for sysfs */
1101 * version 1 superblock
1104 static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
1108 unsigned long long newcsum;
1109 int size = 256 + le32_to_cpu(sb->max_dev)*2;
1110 __le32 *isuper = (__le32*)sb;
1113 disk_csum = sb->sb_csum;
1116 for (i=0; size>=4; size -= 4 )
1117 newcsum += le32_to_cpu(*isuper++);
1120 newcsum += le16_to_cpu(*(__le16*) isuper);
1122 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1123 sb->sb_csum = disk_csum;
1124 return cpu_to_le32(csum);
1127 static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1129 struct mdp_superblock_1 *sb;
1132 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1136 * Calculate the position of the superblock in 512byte sectors.
1137 * It is always aligned to a 4K boundary and
1138 * depeding on minor_version, it can be:
1139 * 0: At least 8K, but less than 12K, from end of device
1140 * 1: At start of device
1141 * 2: 4K from start of device.
1143 switch(minor_version) {
1145 sb_start = rdev->bdev->bd_inode->i_size >> 9;
1147 sb_start &= ~(sector_t)(4*2-1);
1158 rdev->sb_start = sb_start;
1160 /* superblock is rarely larger than 1K, but it can be larger,
1161 * and it is safe to read 4k, so we do that
1163 ret = read_disk_sb(rdev, 4096);
1164 if (ret) return ret;
1167 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1169 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1170 sb->major_version != cpu_to_le32(1) ||
1171 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1172 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
1173 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
1176 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1177 printk("md: invalid superblock checksum on %s\n",
1178 bdevname(rdev->bdev,b));
1181 if (le64_to_cpu(sb->data_size) < 10) {
1182 printk("md: data_size too small on %s\n",
1183 bdevname(rdev->bdev,b));
1187 rdev->preferred_minor = 0xffff;
1188 rdev->data_offset = le64_to_cpu(sb->data_offset);
1189 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
1191 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
1192 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1193 if (rdev->sb_size & bmask)
1194 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1197 && rdev->data_offset < sb_start + (rdev->sb_size/512))
1200 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1203 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1209 struct mdp_superblock_1 *refsb =
1210 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1212 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1213 sb->level != refsb->level ||
1214 sb->layout != refsb->layout ||
1215 sb->chunksize != refsb->chunksize) {
1216 printk(KERN_WARNING "md: %s has strangely different"
1217 " superblock to %s\n",
1218 bdevname(rdev->bdev,b),
1219 bdevname(refdev->bdev,b2));
1222 ev1 = le64_to_cpu(sb->events);
1223 ev2 = le64_to_cpu(refsb->events);
1231 rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) -
1232 le64_to_cpu(sb->data_offset);
1234 rdev->sectors = rdev->sb_start;
1235 if (rdev->sectors < le64_to_cpu(sb->data_size))
1237 rdev->sectors = le64_to_cpu(sb->data_size);
1238 if (le64_to_cpu(sb->size) > rdev->sectors)
1243 static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1245 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1246 __u64 ev1 = le64_to_cpu(sb->events);
1248 rdev->raid_disk = -1;
1249 clear_bit(Faulty, &rdev->flags);
1250 clear_bit(In_sync, &rdev->flags);
1251 clear_bit(WriteMostly, &rdev->flags);
1252 clear_bit(BarriersNotsupp, &rdev->flags);
1254 if (mddev->raid_disks == 0) {
1255 mddev->major_version = 1;
1256 mddev->patch_version = 0;
1257 mddev->external = 0;
1258 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
1259 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1260 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1261 mddev->level = le32_to_cpu(sb->level);
1262 mddev->clevel[0] = 0;
1263 mddev->layout = le32_to_cpu(sb->layout);
1264 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1265 mddev->dev_sectors = le64_to_cpu(sb->size);
1266 mddev->events = ev1;
1267 mddev->bitmap_offset = 0;
1268 mddev->default_bitmap_offset = 1024 >> 9;
1270 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1271 memcpy(mddev->uuid, sb->set_uuid, 16);
1273 mddev->max_disks = (4096-256)/2;
1275 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
1276 mddev->bitmap_file == NULL )
1277 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
1279 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1280 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1281 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1282 mddev->new_level = le32_to_cpu(sb->new_level);
1283 mddev->new_layout = le32_to_cpu(sb->new_layout);
1284 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
1286 mddev->reshape_position = MaxSector;
1287 mddev->delta_disks = 0;
1288 mddev->new_level = mddev->level;
1289 mddev->new_layout = mddev->layout;
1290 mddev->new_chunk_sectors = mddev->chunk_sectors;
1293 } else if (mddev->pers == NULL) {
1294 /* Insist of good event counter while assembling */
1296 if (ev1 < mddev->events)
1298 } else if (mddev->bitmap) {
1299 /* If adding to array with a bitmap, then we can accept an
1300 * older device, but not too old.
1302 if (ev1 < mddev->bitmap->events_cleared)
1305 if (ev1 < mddev->events)
1306 /* just a hot-add of a new device, leave raid_disk at -1 */
1309 if (mddev->level != LEVEL_MULTIPATH) {
1311 if (rdev->desc_nr < 0 ||
1312 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1316 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1318 case 0xffff: /* spare */
1320 case 0xfffe: /* faulty */
1321 set_bit(Faulty, &rdev->flags);
1324 if ((le32_to_cpu(sb->feature_map) &
1325 MD_FEATURE_RECOVERY_OFFSET))
1326 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1328 set_bit(In_sync, &rdev->flags);
1329 rdev->raid_disk = role;
1332 if (sb->devflags & WriteMostly1)
1333 set_bit(WriteMostly, &rdev->flags);
1334 } else /* MULTIPATH are always insync */
1335 set_bit(In_sync, &rdev->flags);
1340 static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1342 struct mdp_superblock_1 *sb;
1345 /* make rdev->sb match mddev and rdev data. */
1347 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1349 sb->feature_map = 0;
1351 sb->recovery_offset = cpu_to_le64(0);
1352 memset(sb->pad1, 0, sizeof(sb->pad1));
1353 memset(sb->pad2, 0, sizeof(sb->pad2));
1354 memset(sb->pad3, 0, sizeof(sb->pad3));
1356 sb->utime = cpu_to_le64((__u64)mddev->utime);
1357 sb->events = cpu_to_le64(mddev->events);
1359 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1361 sb->resync_offset = cpu_to_le64(0);
1363 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
1365 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
1366 sb->size = cpu_to_le64(mddev->dev_sectors);
1367 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
1368 sb->level = cpu_to_le32(mddev->level);
1369 sb->layout = cpu_to_le32(mddev->layout);
1371 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1372 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
1373 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
1376 if (rdev->raid_disk >= 0 &&
1377 !test_bit(In_sync, &rdev->flags)) {
1378 if (mddev->curr_resync_completed > rdev->recovery_offset)
1379 rdev->recovery_offset = mddev->curr_resync_completed;
1380 if (rdev->recovery_offset > 0) {
1382 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1383 sb->recovery_offset =
1384 cpu_to_le64(rdev->recovery_offset);
1388 if (mddev->reshape_position != MaxSector) {
1389 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1390 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1391 sb->new_layout = cpu_to_le32(mddev->new_layout);
1392 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1393 sb->new_level = cpu_to_le32(mddev->new_level);
1394 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
1398 list_for_each_entry(rdev2, &mddev->disks, same_set)
1399 if (rdev2->desc_nr+1 > max_dev)
1400 max_dev = rdev2->desc_nr+1;
1402 if (max_dev > le32_to_cpu(sb->max_dev)) {
1404 sb->max_dev = cpu_to_le32(max_dev);
1405 rdev->sb_size = max_dev * 2 + 256;
1406 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1407 if (rdev->sb_size & bmask)
1408 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1410 for (i=0; i<max_dev;i++)
1411 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1413 list_for_each_entry(rdev2, &mddev->disks, same_set) {
1415 if (test_bit(Faulty, &rdev2->flags))
1416 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1417 else if (test_bit(In_sync, &rdev2->flags))
1418 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1419 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1420 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1422 sb->dev_roles[i] = cpu_to_le16(0xffff);
1425 sb->sb_csum = calc_sb_1_csum(sb);
1428 static unsigned long long
1429 super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
1431 struct mdp_superblock_1 *sb;
1432 sector_t max_sectors;
1433 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1434 return 0; /* component must fit device */
1435 if (rdev->sb_start < rdev->data_offset) {
1436 /* minor versions 1 and 2; superblock before data */
1437 max_sectors = rdev->bdev->bd_inode->i_size >> 9;
1438 max_sectors -= rdev->data_offset;
1439 if (!num_sectors || num_sectors > max_sectors)
1440 num_sectors = max_sectors;
1441 } else if (rdev->mddev->bitmap_offset) {
1442 /* minor version 0 with bitmap we can't move */
1445 /* minor version 0; superblock after data */
1447 sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1448 sb_start &= ~(sector_t)(4*2 - 1);
1449 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
1450 if (!num_sectors || num_sectors > max_sectors)
1451 num_sectors = max_sectors;
1452 rdev->sb_start = sb_start;
1454 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
1455 sb->data_size = cpu_to_le64(num_sectors);
1456 sb->super_offset = rdev->sb_start;
1457 sb->sb_csum = calc_sb_1_csum(sb);
1458 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1460 md_super_wait(rdev->mddev);
1461 return num_sectors / 2; /* kB for sysfs */
1464 static struct super_type super_types[] = {
1467 .owner = THIS_MODULE,
1468 .load_super = super_90_load,
1469 .validate_super = super_90_validate,
1470 .sync_super = super_90_sync,
1471 .rdev_size_change = super_90_rdev_size_change,
1475 .owner = THIS_MODULE,
1476 .load_super = super_1_load,
1477 .validate_super = super_1_validate,
1478 .sync_super = super_1_sync,
1479 .rdev_size_change = super_1_rdev_size_change,
1483 static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1485 mdk_rdev_t *rdev, *rdev2;
1488 rdev_for_each_rcu(rdev, mddev1)
1489 rdev_for_each_rcu(rdev2, mddev2)
1490 if (rdev->bdev->bd_contains ==
1491 rdev2->bdev->bd_contains) {
1499 static LIST_HEAD(pending_raid_disks);
1502 * Try to register data integrity profile for an mddev
1504 * This is called when an array is started and after a disk has been kicked
1505 * from the array. It only succeeds if all working and active component devices
1506 * are integrity capable with matching profiles.
1508 int md_integrity_register(mddev_t *mddev)
1510 mdk_rdev_t *rdev, *reference = NULL;
1512 if (list_empty(&mddev->disks))
1513 return 0; /* nothing to do */
1514 if (blk_get_integrity(mddev->gendisk))
1515 return 0; /* already registered */
1516 list_for_each_entry(rdev, &mddev->disks, same_set) {
1517 /* skip spares and non-functional disks */
1518 if (test_bit(Faulty, &rdev->flags))
1520 if (rdev->raid_disk < 0)
1523 * If at least one rdev is not integrity capable, we can not
1524 * enable data integrity for the md device.
1526 if (!bdev_get_integrity(rdev->bdev))
1529 /* Use the first rdev as the reference */
1533 /* does this rdev's profile match the reference profile? */
1534 if (blk_integrity_compare(reference->bdev->bd_disk,
1535 rdev->bdev->bd_disk) < 0)
1539 * All component devices are integrity capable and have matching
1540 * profiles, register the common profile for the md device.
1542 if (blk_integrity_register(mddev->gendisk,
1543 bdev_get_integrity(reference->bdev)) != 0) {
1544 printk(KERN_ERR "md: failed to register integrity for %s\n",
1548 printk(KERN_NOTICE "md: data integrity on %s enabled\n",
1552 EXPORT_SYMBOL(md_integrity_register);
1554 /* Disable data integrity if non-capable/non-matching disk is being added */
1555 void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
1557 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1558 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1560 if (!bi_mddev) /* nothing to do */
1562 if (rdev->raid_disk < 0) /* skip spares */
1564 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1565 rdev->bdev->bd_disk) >= 0)
1567 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1568 blk_integrity_unregister(mddev->gendisk);
1570 EXPORT_SYMBOL(md_integrity_add_rdev);
1572 static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1574 char b[BDEVNAME_SIZE];
1584 /* prevent duplicates */
1585 if (find_rdev(mddev, rdev->bdev->bd_dev))
1588 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1589 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1590 rdev->sectors < mddev->dev_sectors)) {
1592 /* Cannot change size, so fail
1593 * If mddev->level <= 0, then we don't care
1594 * about aligning sizes (e.g. linear)
1596 if (mddev->level > 0)
1599 mddev->dev_sectors = rdev->sectors;
1602 /* Verify rdev->desc_nr is unique.
1603 * If it is -1, assign a free number, else
1604 * check number is not in use
1606 if (rdev->desc_nr < 0) {
1608 if (mddev->pers) choice = mddev->raid_disks;
1609 while (find_rdev_nr(mddev, choice))
1611 rdev->desc_nr = choice;
1613 if (find_rdev_nr(mddev, rdev->desc_nr))
1616 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1617 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1618 mdname(mddev), mddev->max_disks);
1621 bdevname(rdev->bdev,b);
1622 while ( (s=strchr(b, '/')) != NULL)
1625 rdev->mddev = mddev;
1626 printk(KERN_INFO "md: bind<%s>\n", b);
1628 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
1631 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
1632 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1633 kobject_del(&rdev->kobj);
1636 rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state");
1638 list_add_rcu(&rdev->same_set, &mddev->disks);
1639 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
1641 /* May as well allow recovery to be retried once */
1642 mddev->recovery_disabled = 0;
1647 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1652 static void md_delayed_delete(struct work_struct *ws)
1654 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1655 kobject_del(&rdev->kobj);
1656 kobject_put(&rdev->kobj);
1659 static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1661 char b[BDEVNAME_SIZE];
1666 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
1667 list_del_rcu(&rdev->same_set);
1668 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1670 sysfs_remove_link(&rdev->kobj, "block");
1671 sysfs_put(rdev->sysfs_state);
1672 rdev->sysfs_state = NULL;
1673 /* We need to delay this, otherwise we can deadlock when
1674 * writing to 'remove' to "dev/state". We also need
1675 * to delay it due to rcu usage.
1678 INIT_WORK(&rdev->del_work, md_delayed_delete);
1679 kobject_get(&rdev->kobj);
1680 schedule_work(&rdev->del_work);
1684 * prevent the device from being mounted, repartitioned or
1685 * otherwise reused by a RAID array (or any other kernel
1686 * subsystem), by bd_claiming the device.
1688 static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
1691 struct block_device *bdev;
1692 char b[BDEVNAME_SIZE];
1694 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
1696 printk(KERN_ERR "md: could not open %s.\n",
1697 __bdevname(dev, b));
1698 return PTR_ERR(bdev);
1700 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
1702 printk(KERN_ERR "md: could not bd_claim %s.\n",
1704 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
1708 set_bit(AllReserved, &rdev->flags);
1713 static void unlock_rdev(mdk_rdev_t *rdev)
1715 struct block_device *bdev = rdev->bdev;
1720 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
1723 void md_autodetect_dev(dev_t dev);
1725 static void export_rdev(mdk_rdev_t * rdev)
1727 char b[BDEVNAME_SIZE];
1728 printk(KERN_INFO "md: export_rdev(%s)\n",
1729 bdevname(rdev->bdev,b));
1734 if (test_bit(AutoDetected, &rdev->flags))
1735 md_autodetect_dev(rdev->bdev->bd_dev);
1738 kobject_put(&rdev->kobj);
1741 static void kick_rdev_from_array(mdk_rdev_t * rdev)
1743 unbind_rdev_from_array(rdev);
1747 static void export_array(mddev_t *mddev)
1749 mdk_rdev_t *rdev, *tmp;
1751 rdev_for_each(rdev, tmp, mddev) {
1756 kick_rdev_from_array(rdev);
1758 if (!list_empty(&mddev->disks))
1760 mddev->raid_disks = 0;
1761 mddev->major_version = 0;
1764 static void print_desc(mdp_disk_t *desc)
1766 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1767 desc->major,desc->minor,desc->raid_disk,desc->state);
1770 static void print_sb_90(mdp_super_t *sb)
1775 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1776 sb->major_version, sb->minor_version, sb->patch_version,
1777 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1779 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1780 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1781 sb->md_minor, sb->layout, sb->chunk_size);
1782 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1783 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1784 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1785 sb->failed_disks, sb->spare_disks,
1786 sb->sb_csum, (unsigned long)sb->events_lo);
1789 for (i = 0; i < MD_SB_DISKS; i++) {
1792 desc = sb->disks + i;
1793 if (desc->number || desc->major || desc->minor ||
1794 desc->raid_disk || (desc->state && (desc->state != 4))) {
1795 printk(" D %2d: ", i);
1799 printk(KERN_INFO "md: THIS: ");
1800 print_desc(&sb->this_disk);
1803 static void print_sb_1(struct mdp_superblock_1 *sb)
1807 uuid = sb->set_uuid;
1809 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%02x%02x%02x%02x"
1810 ":%02x%02x:%02x%02x:%02x%02x:%02x%02x%02x%02x%02x%02x>\n"
1811 "md: Name: \"%s\" CT:%llu\n",
1812 le32_to_cpu(sb->major_version),
1813 le32_to_cpu(sb->feature_map),
1814 uuid[0], uuid[1], uuid[2], uuid[3],
1815 uuid[4], uuid[5], uuid[6], uuid[7],
1816 uuid[8], uuid[9], uuid[10], uuid[11],
1817 uuid[12], uuid[13], uuid[14], uuid[15],
1819 (unsigned long long)le64_to_cpu(sb->ctime)
1820 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
1822 uuid = sb->device_uuid;
1824 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
1826 "md: Dev:%08x UUID: %02x%02x%02x%02x:%02x%02x:%02x%02x:%02x%02x"
1827 ":%02x%02x%02x%02x%02x%02x\n"
1828 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
1829 "md: (MaxDev:%u) \n",
1830 le32_to_cpu(sb->level),
1831 (unsigned long long)le64_to_cpu(sb->size),
1832 le32_to_cpu(sb->raid_disks),
1833 le32_to_cpu(sb->layout),
1834 le32_to_cpu(sb->chunksize),
1835 (unsigned long long)le64_to_cpu(sb->data_offset),
1836 (unsigned long long)le64_to_cpu(sb->data_size),
1837 (unsigned long long)le64_to_cpu(sb->super_offset),
1838 (unsigned long long)le64_to_cpu(sb->recovery_offset),
1839 le32_to_cpu(sb->dev_number),
1840 uuid[0], uuid[1], uuid[2], uuid[3],
1841 uuid[4], uuid[5], uuid[6], uuid[7],
1842 uuid[8], uuid[9], uuid[10], uuid[11],
1843 uuid[12], uuid[13], uuid[14], uuid[15],
1845 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
1846 (unsigned long long)le64_to_cpu(sb->events),
1847 (unsigned long long)le64_to_cpu(sb->resync_offset),
1848 le32_to_cpu(sb->sb_csum),
1849 le32_to_cpu(sb->max_dev)
1853 static void print_rdev(mdk_rdev_t *rdev, int major_version)
1855 char b[BDEVNAME_SIZE];
1856 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
1857 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
1858 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1860 if (rdev->sb_loaded) {
1861 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
1862 switch (major_version) {
1864 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
1867 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
1871 printk(KERN_INFO "md: no rdev superblock!\n");
1874 static void md_print_devices(void)
1876 struct list_head *tmp;
1879 char b[BDEVNAME_SIZE];
1882 printk("md: **********************************\n");
1883 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1884 printk("md: **********************************\n");
1885 for_each_mddev(mddev, tmp) {
1888 bitmap_print_sb(mddev->bitmap);
1890 printk("%s: ", mdname(mddev));
1891 list_for_each_entry(rdev, &mddev->disks, same_set)
1892 printk("<%s>", bdevname(rdev->bdev,b));
1895 list_for_each_entry(rdev, &mddev->disks, same_set)
1896 print_rdev(rdev, mddev->major_version);
1898 printk("md: **********************************\n");
1903 static void sync_sbs(mddev_t * mddev, int nospares)
1905 /* Update each superblock (in-memory image), but
1906 * if we are allowed to, skip spares which already
1907 * have the right event counter, or have one earlier
1908 * (which would mean they aren't being marked as dirty
1909 * with the rest of the array)
1913 list_for_each_entry(rdev, &mddev->disks, same_set) {
1914 if (rdev->sb_events == mddev->events ||
1916 rdev->raid_disk < 0 &&
1917 (rdev->sb_events&1)==0 &&
1918 rdev->sb_events+1 == mddev->events)) {
1919 /* Don't update this superblock */
1920 rdev->sb_loaded = 2;
1922 super_types[mddev->major_version].
1923 sync_super(mddev, rdev);
1924 rdev->sb_loaded = 1;
1929 static void md_update_sb(mddev_t * mddev, int force_change)
1935 mddev->utime = get_seconds();
1936 if (mddev->external)
1939 spin_lock_irq(&mddev->write_lock);
1941 set_bit(MD_CHANGE_PENDING, &mddev->flags);
1942 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
1944 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
1945 /* just a clean<-> dirty transition, possibly leave spares alone,
1946 * though if events isn't the right even/odd, we will have to do
1952 if (mddev->degraded)
1953 /* If the array is degraded, then skipping spares is both
1954 * dangerous and fairly pointless.
1955 * Dangerous because a device that was removed from the array
1956 * might have a event_count that still looks up-to-date,
1957 * so it can be re-added without a resync.
1958 * Pointless because if there are any spares to skip,
1959 * then a recovery will happen and soon that array won't
1960 * be degraded any more and the spare can go back to sleep then.
1964 sync_req = mddev->in_sync;
1966 /* If this is just a dirty<->clean transition, and the array is clean
1967 * and 'events' is odd, we can roll back to the previous clean state */
1969 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
1970 && (mddev->events & 1)
1971 && mddev->events != 1)
1974 /* otherwise we have to go forward and ... */
1976 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
1977 /* .. if the array isn't clean, insist on an odd 'events' */
1978 if ((mddev->events&1)==0) {
1983 /* otherwise insist on an even 'events' (for clean states) */
1984 if ((mddev->events&1)) {
1991 if (!mddev->events) {
1993 * oops, this 64-bit counter should never wrap.
1994 * Either we are in around ~1 trillion A.C., assuming
1995 * 1 reboot per second, or we have a bug:
2002 * do not write anything to disk if using
2003 * nonpersistent superblocks
2005 if (!mddev->persistent) {
2006 if (!mddev->external)
2007 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2009 spin_unlock_irq(&mddev->write_lock);
2010 wake_up(&mddev->sb_wait);
2013 sync_sbs(mddev, nospares);
2014 spin_unlock_irq(&mddev->write_lock);
2017 "md: updating %s RAID superblock on device (in sync %d)\n",
2018 mdname(mddev),mddev->in_sync);
2020 bitmap_update_sb(mddev->bitmap);
2021 list_for_each_entry(rdev, &mddev->disks, same_set) {
2022 char b[BDEVNAME_SIZE];
2023 dprintk(KERN_INFO "md: ");
2024 if (rdev->sb_loaded != 1)
2025 continue; /* no noise on spare devices */
2026 if (test_bit(Faulty, &rdev->flags))
2027 dprintk("(skipping faulty ");
2029 dprintk("%s ", bdevname(rdev->bdev,b));
2030 if (!test_bit(Faulty, &rdev->flags)) {
2031 md_super_write(mddev,rdev,
2032 rdev->sb_start, rdev->sb_size,
2034 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
2035 bdevname(rdev->bdev,b),
2036 (unsigned long long)rdev->sb_start);
2037 rdev->sb_events = mddev->events;
2041 if (mddev->level == LEVEL_MULTIPATH)
2042 /* only need to write one superblock... */
2045 md_super_wait(mddev);
2046 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
2048 spin_lock_irq(&mddev->write_lock);
2049 if (mddev->in_sync != sync_req ||
2050 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
2051 /* have to write it out again */
2052 spin_unlock_irq(&mddev->write_lock);
2055 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2056 spin_unlock_irq(&mddev->write_lock);
2057 wake_up(&mddev->sb_wait);
2058 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2059 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
2063 /* words written to sysfs files may, or may not, be \n terminated.
2064 * We want to accept with case. For this we use cmd_match.
2066 static int cmd_match(const char *cmd, const char *str)
2068 /* See if cmd, written into a sysfs file, matches
2069 * str. They must either be the same, or cmd can
2070 * have a trailing newline
2072 while (*cmd && *str && *cmd == *str) {
2083 struct rdev_sysfs_entry {
2084 struct attribute attr;
2085 ssize_t (*show)(mdk_rdev_t *, char *);
2086 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2090 state_show(mdk_rdev_t *rdev, char *page)
2095 if (test_bit(Faulty, &rdev->flags)) {
2096 len+= sprintf(page+len, "%sfaulty",sep);
2099 if (test_bit(In_sync, &rdev->flags)) {
2100 len += sprintf(page+len, "%sin_sync",sep);
2103 if (test_bit(WriteMostly, &rdev->flags)) {
2104 len += sprintf(page+len, "%swrite_mostly",sep);
2107 if (test_bit(Blocked, &rdev->flags)) {
2108 len += sprintf(page+len, "%sblocked", sep);
2111 if (!test_bit(Faulty, &rdev->flags) &&
2112 !test_bit(In_sync, &rdev->flags)) {
2113 len += sprintf(page+len, "%sspare", sep);
2116 return len+sprintf(page+len, "\n");
2120 state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2123 * faulty - simulates and error
2124 * remove - disconnects the device
2125 * writemostly - sets write_mostly
2126 * -writemostly - clears write_mostly
2127 * blocked - sets the Blocked flag
2128 * -blocked - clears the Blocked flag
2129 * insync - sets Insync providing device isn't active
2132 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2133 md_error(rdev->mddev, rdev);
2135 } else if (cmd_match(buf, "remove")) {
2136 if (rdev->raid_disk >= 0)
2139 mddev_t *mddev = rdev->mddev;
2140 kick_rdev_from_array(rdev);
2142 md_update_sb(mddev, 1);
2143 md_new_event(mddev);
2146 } else if (cmd_match(buf, "writemostly")) {
2147 set_bit(WriteMostly, &rdev->flags);
2149 } else if (cmd_match(buf, "-writemostly")) {
2150 clear_bit(WriteMostly, &rdev->flags);
2152 } else if (cmd_match(buf, "blocked")) {
2153 set_bit(Blocked, &rdev->flags);
2155 } else if (cmd_match(buf, "-blocked")) {
2156 clear_bit(Blocked, &rdev->flags);
2157 wake_up(&rdev->blocked_wait);
2158 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2159 md_wakeup_thread(rdev->mddev->thread);
2162 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2163 set_bit(In_sync, &rdev->flags);
2166 if (!err && rdev->sysfs_state)
2167 sysfs_notify_dirent(rdev->sysfs_state);
2168 return err ? err : len;
2170 static struct rdev_sysfs_entry rdev_state =
2171 __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
2174 errors_show(mdk_rdev_t *rdev, char *page)
2176 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2180 errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2183 unsigned long n = simple_strtoul(buf, &e, 10);
2184 if (*buf && (*e == 0 || *e == '\n')) {
2185 atomic_set(&rdev->corrected_errors, n);
2190 static struct rdev_sysfs_entry rdev_errors =
2191 __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
2194 slot_show(mdk_rdev_t *rdev, char *page)
2196 if (rdev->raid_disk < 0)
2197 return sprintf(page, "none\n");
2199 return sprintf(page, "%d\n", rdev->raid_disk);
2203 slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2208 int slot = simple_strtoul(buf, &e, 10);
2209 if (strncmp(buf, "none", 4)==0)
2211 else if (e==buf || (*e && *e!= '\n'))
2213 if (rdev->mddev->pers && slot == -1) {
2214 /* Setting 'slot' on an active array requires also
2215 * updating the 'rd%d' link, and communicating
2216 * with the personality with ->hot_*_disk.
2217 * For now we only support removing
2218 * failed/spare devices. This normally happens automatically,
2219 * but not when the metadata is externally managed.
2221 if (rdev->raid_disk == -1)
2223 /* personality does all needed checks */
2224 if (rdev->mddev->pers->hot_add_disk == NULL)
2226 err = rdev->mddev->pers->
2227 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2230 sprintf(nm, "rd%d", rdev->raid_disk);
2231 sysfs_remove_link(&rdev->mddev->kobj, nm);
2232 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2233 md_wakeup_thread(rdev->mddev->thread);
2234 } else if (rdev->mddev->pers) {
2236 /* Activating a spare .. or possibly reactivating
2237 * if we ever get bitmaps working here.
2240 if (rdev->raid_disk != -1)
2243 if (rdev->mddev->pers->hot_add_disk == NULL)
2246 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
2247 if (rdev2->raid_disk == slot)
2250 rdev->raid_disk = slot;
2251 if (test_bit(In_sync, &rdev->flags))
2252 rdev->saved_raid_disk = slot;
2254 rdev->saved_raid_disk = -1;
2255 err = rdev->mddev->pers->
2256 hot_add_disk(rdev->mddev, rdev);
2258 rdev->raid_disk = -1;
2261 sysfs_notify_dirent(rdev->sysfs_state);
2262 sprintf(nm, "rd%d", rdev->raid_disk);
2263 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2265 "md: cannot register "
2267 nm, mdname(rdev->mddev));
2269 /* don't wakeup anyone, leave that to userspace. */
2271 if (slot >= rdev->mddev->raid_disks)
2273 rdev->raid_disk = slot;
2274 /* assume it is working */
2275 clear_bit(Faulty, &rdev->flags);
2276 clear_bit(WriteMostly, &rdev->flags);
2277 set_bit(In_sync, &rdev->flags);
2278 sysfs_notify_dirent(rdev->sysfs_state);
2284 static struct rdev_sysfs_entry rdev_slot =
2285 __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
2288 offset_show(mdk_rdev_t *rdev, char *page)
2290 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
2294 offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2297 unsigned long long offset = simple_strtoull(buf, &e, 10);
2298 if (e==buf || (*e && *e != '\n'))
2300 if (rdev->mddev->pers && rdev->raid_disk >= 0)
2302 if (rdev->sectors && rdev->mddev->external)
2303 /* Must set offset before size, so overlap checks
2306 rdev->data_offset = offset;
2310 static struct rdev_sysfs_entry rdev_offset =
2311 __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
2314 rdev_size_show(mdk_rdev_t *rdev, char *page)
2316 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
2319 static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2321 /* check if two start/length pairs overlap */
2329 static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2331 unsigned long long blocks;
2334 if (strict_strtoull(buf, 10, &blocks) < 0)
2337 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2338 return -EINVAL; /* sector conversion overflow */
2341 if (new != blocks * 2)
2342 return -EINVAL; /* unsigned long long to sector_t overflow */
2349 rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2351 mddev_t *my_mddev = rdev->mddev;
2352 sector_t oldsectors = rdev->sectors;
2355 if (strict_blocks_to_sectors(buf, §ors) < 0)
2357 if (my_mddev->pers && rdev->raid_disk >= 0) {
2358 if (my_mddev->persistent) {
2359 sectors = super_types[my_mddev->major_version].
2360 rdev_size_change(rdev, sectors);
2363 } else if (!sectors)
2364 sectors = (rdev->bdev->bd_inode->i_size >> 9) -
2367 if (sectors < my_mddev->dev_sectors)
2368 return -EINVAL; /* component must fit device */
2370 rdev->sectors = sectors;
2371 if (sectors > oldsectors && my_mddev->external) {
2372 /* need to check that all other rdevs with the same ->bdev
2373 * do not overlap. We need to unlock the mddev to avoid
2374 * a deadlock. We have already changed rdev->sectors, and if
2375 * we have to change it back, we will have the lock again.
2379 struct list_head *tmp;
2381 mddev_unlock(my_mddev);
2382 for_each_mddev(mddev, tmp) {
2386 list_for_each_entry(rdev2, &mddev->disks, same_set)
2387 if (test_bit(AllReserved, &rdev2->flags) ||
2388 (rdev->bdev == rdev2->bdev &&
2390 overlaps(rdev->data_offset, rdev->sectors,
2396 mddev_unlock(mddev);
2402 mddev_lock(my_mddev);
2404 /* Someone else could have slipped in a size
2405 * change here, but doing so is just silly.
2406 * We put oldsectors back because we *know* it is
2407 * safe, and trust userspace not to race with
2410 rdev->sectors = oldsectors;
2417 static struct rdev_sysfs_entry rdev_size =
2418 __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
2420 static struct attribute *rdev_default_attrs[] = {
2429 rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2431 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2432 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
2433 mddev_t *mddev = rdev->mddev;
2439 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2441 if (rdev->mddev == NULL)
2444 rv = entry->show(rdev, page);
2445 mddev_unlock(mddev);
2451 rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2452 const char *page, size_t length)
2454 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2455 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
2457 mddev_t *mddev = rdev->mddev;
2461 if (!capable(CAP_SYS_ADMIN))
2463 rv = mddev ? mddev_lock(mddev): -EBUSY;
2465 if (rdev->mddev == NULL)
2468 rv = entry->store(rdev, page, length);
2469 mddev_unlock(mddev);
2474 static void rdev_free(struct kobject *ko)
2476 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2479 static struct sysfs_ops rdev_sysfs_ops = {
2480 .show = rdev_attr_show,
2481 .store = rdev_attr_store,
2483 static struct kobj_type rdev_ktype = {
2484 .release = rdev_free,
2485 .sysfs_ops = &rdev_sysfs_ops,
2486 .default_attrs = rdev_default_attrs,
2490 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2492 * mark the device faulty if:
2494 * - the device is nonexistent (zero size)
2495 * - the device has no valid superblock
2497 * a faulty rdev _never_ has rdev->sb set.
2499 static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2501 char b[BDEVNAME_SIZE];
2506 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
2508 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2509 return ERR_PTR(-ENOMEM);
2512 if ((err = alloc_disk_sb(rdev)))
2515 err = lock_rdev(rdev, newdev, super_format == -2);
2519 kobject_init(&rdev->kobj, &rdev_ktype);
2522 rdev->saved_raid_disk = -1;
2523 rdev->raid_disk = -1;
2525 rdev->data_offset = 0;
2526 rdev->sb_events = 0;
2527 atomic_set(&rdev->nr_pending, 0);
2528 atomic_set(&rdev->read_errors, 0);
2529 atomic_set(&rdev->corrected_errors, 0);
2531 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2534 "md: %s has zero or unknown size, marking faulty!\n",
2535 bdevname(rdev->bdev,b));
2540 if (super_format >= 0) {
2541 err = super_types[super_format].
2542 load_super(rdev, NULL, super_minor);
2543 if (err == -EINVAL) {
2545 "md: %s does not have a valid v%d.%d "
2546 "superblock, not importing!\n",
2547 bdevname(rdev->bdev,b),
2548 super_format, super_minor);
2553 "md: could not read %s's sb, not importing!\n",
2554 bdevname(rdev->bdev,b));
2559 INIT_LIST_HEAD(&rdev->same_set);
2560 init_waitqueue_head(&rdev->blocked_wait);
2565 if (rdev->sb_page) {
2571 return ERR_PTR(err);
2575 * Check a full RAID array for plausibility
2579 static void analyze_sbs(mddev_t * mddev)
2582 mdk_rdev_t *rdev, *freshest, *tmp;
2583 char b[BDEVNAME_SIZE];
2586 rdev_for_each(rdev, tmp, mddev)
2587 switch (super_types[mddev->major_version].
2588 load_super(rdev, freshest, mddev->minor_version)) {
2596 "md: fatal superblock inconsistency in %s"
2597 " -- removing from array\n",
2598 bdevname(rdev->bdev,b));
2599 kick_rdev_from_array(rdev);
2603 super_types[mddev->major_version].
2604 validate_super(mddev, freshest);
2607 rdev_for_each(rdev, tmp, mddev) {
2608 if (rdev->desc_nr >= mddev->max_disks ||
2609 i > mddev->max_disks) {
2611 "md: %s: %s: only %d devices permitted\n",
2612 mdname(mddev), bdevname(rdev->bdev, b),
2614 kick_rdev_from_array(rdev);
2617 if (rdev != freshest)
2618 if (super_types[mddev->major_version].
2619 validate_super(mddev, rdev)) {
2620 printk(KERN_WARNING "md: kicking non-fresh %s"
2622 bdevname(rdev->bdev,b));
2623 kick_rdev_from_array(rdev);
2626 if (mddev->level == LEVEL_MULTIPATH) {
2627 rdev->desc_nr = i++;
2628 rdev->raid_disk = rdev->desc_nr;
2629 set_bit(In_sync, &rdev->flags);
2630 } else if (rdev->raid_disk >= mddev->raid_disks) {
2631 rdev->raid_disk = -1;
2632 clear_bit(In_sync, &rdev->flags);
2637 static void md_safemode_timeout(unsigned long data);
2640 safe_delay_show(mddev_t *mddev, char *page)
2642 int msec = (mddev->safemode_delay*1000)/HZ;
2643 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2646 safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2654 /* remove a period, and count digits after it */
2655 if (len >= sizeof(buf))
2657 strlcpy(buf, cbuf, sizeof(buf));
2658 for (i=0; i<len; i++) {
2660 if (isdigit(buf[i])) {
2665 } else if (buf[i] == '.') {
2670 if (strict_strtoul(buf, 10, &msec) < 0)
2672 msec = (msec * 1000) / scale;
2674 mddev->safemode_delay = 0;
2676 unsigned long old_delay = mddev->safemode_delay;
2677 mddev->safemode_delay = (msec*HZ)/1000;
2678 if (mddev->safemode_delay == 0)
2679 mddev->safemode_delay = 1;
2680 if (mddev->safemode_delay < old_delay)
2681 md_safemode_timeout((unsigned long)mddev);
2685 static struct md_sysfs_entry md_safe_delay =
2686 __ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
2689 level_show(mddev_t *mddev, char *page)
2691 struct mdk_personality *p = mddev->pers;
2693 return sprintf(page, "%s\n", p->name);
2694 else if (mddev->clevel[0])
2695 return sprintf(page, "%s\n", mddev->clevel);
2696 else if (mddev->level != LEVEL_NONE)
2697 return sprintf(page, "%d\n", mddev->level);
2703 level_store(mddev_t *mddev, const char *buf, size_t len)
2707 struct mdk_personality *pers;
2711 if (mddev->pers == NULL) {
2714 if (len >= sizeof(mddev->clevel))
2716 strncpy(mddev->clevel, buf, len);
2717 if (mddev->clevel[len-1] == '\n')
2719 mddev->clevel[len] = 0;
2720 mddev->level = LEVEL_NONE;
2724 /* request to change the personality. Need to ensure:
2725 * - array is not engaged in resync/recovery/reshape
2726 * - old personality can be suspended
2727 * - new personality will access other array.
2730 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
2733 if (!mddev->pers->quiesce) {
2734 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
2735 mdname(mddev), mddev->pers->name);
2739 /* Now find the new personality */
2740 if (len == 0 || len >= sizeof(level))
2742 strncpy(level, buf, len);
2743 if (level[len-1] == '\n')
2747 request_module("md-%s", level);
2748 spin_lock(&pers_lock);
2749 pers = find_pers(LEVEL_NONE, level);
2750 if (!pers || !try_module_get(pers->owner)) {
2751 spin_unlock(&pers_lock);
2752 printk(KERN_WARNING "md: personality %s not loaded\n", level);
2755 spin_unlock(&pers_lock);
2757 if (pers == mddev->pers) {
2758 /* Nothing to do! */
2759 module_put(pers->owner);
2762 if (!pers->takeover) {
2763 module_put(pers->owner);
2764 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
2765 mdname(mddev), level);
2769 /* ->takeover must set new_* and/or delta_disks
2770 * if it succeeds, and may set them when it fails.
2772 priv = pers->takeover(mddev);
2774 mddev->new_level = mddev->level;
2775 mddev->new_layout = mddev->layout;
2776 mddev->new_chunk_sectors = mddev->chunk_sectors;
2777 mddev->raid_disks -= mddev->delta_disks;
2778 mddev->delta_disks = 0;
2779 module_put(pers->owner);
2780 printk(KERN_WARNING "md: %s: %s would not accept array\n",
2781 mdname(mddev), level);
2782 return PTR_ERR(priv);
2785 /* Looks like we have a winner */
2786 mddev_suspend(mddev);
2787 mddev->pers->stop(mddev);
2788 module_put(mddev->pers->owner);
2789 /* Invalidate devices that are now superfluous */
2790 list_for_each_entry(rdev, &mddev->disks, same_set)
2791 if (rdev->raid_disk >= mddev->raid_disks) {
2792 rdev->raid_disk = -1;
2793 clear_bit(In_sync, &rdev->flags);
2796 mddev->private = priv;
2797 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
2798 mddev->level = mddev->new_level;
2799 mddev->layout = mddev->new_layout;
2800 mddev->chunk_sectors = mddev->new_chunk_sectors;
2801 mddev->delta_disks = 0;
2803 mddev_resume(mddev);
2804 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2805 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2806 md_wakeup_thread(mddev->thread);
2810 static struct md_sysfs_entry md_level =
2811 __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
2815 layout_show(mddev_t *mddev, char *page)
2817 /* just a number, not meaningful for all levels */
2818 if (mddev->reshape_position != MaxSector &&
2819 mddev->layout != mddev->new_layout)
2820 return sprintf(page, "%d (%d)\n",
2821 mddev->new_layout, mddev->layout);
2822 return sprintf(page, "%d\n", mddev->layout);
2826 layout_store(mddev_t *mddev, const char *buf, size_t len)
2829 unsigned long n = simple_strtoul(buf, &e, 10);
2831 if (!*buf || (*e && *e != '\n'))
2836 if (mddev->pers->check_reshape == NULL)
2838 mddev->new_layout = n;
2839 err = mddev->pers->check_reshape(mddev);
2841 mddev->new_layout = mddev->layout;
2845 mddev->new_layout = n;
2846 if (mddev->reshape_position == MaxSector)
2851 static struct md_sysfs_entry md_layout =
2852 __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
2856 raid_disks_show(mddev_t *mddev, char *page)
2858 if (mddev->raid_disks == 0)
2860 if (mddev->reshape_position != MaxSector &&
2861 mddev->delta_disks != 0)
2862 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
2863 mddev->raid_disks - mddev->delta_disks);
2864 return sprintf(page, "%d\n", mddev->raid_disks);
2867 static int update_raid_disks(mddev_t *mddev, int raid_disks);
2870 raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2874 unsigned long n = simple_strtoul(buf, &e, 10);
2876 if (!*buf || (*e && *e != '\n'))
2880 rv = update_raid_disks(mddev, n);
2881 else if (mddev->reshape_position != MaxSector) {
2882 int olddisks = mddev->raid_disks - mddev->delta_disks;
2883 mddev->delta_disks = n - olddisks;
2884 mddev->raid_disks = n;
2886 mddev->raid_disks = n;
2887 return rv ? rv : len;
2889 static struct md_sysfs_entry md_raid_disks =
2890 __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
2893 chunk_size_show(mddev_t *mddev, char *page)
2895 if (mddev->reshape_position != MaxSector &&
2896 mddev->chunk_sectors != mddev->new_chunk_sectors)
2897 return sprintf(page, "%d (%d)\n",
2898 mddev->new_chunk_sectors << 9,
2899 mddev->chunk_sectors << 9);
2900 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
2904 chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2907 unsigned long n = simple_strtoul(buf, &e, 10);
2909 if (!*buf || (*e && *e != '\n'))
2914 if (mddev->pers->check_reshape == NULL)
2916 mddev->new_chunk_sectors = n >> 9;
2917 err = mddev->pers->check_reshape(mddev);
2919 mddev->new_chunk_sectors = mddev->chunk_sectors;
2923 mddev->new_chunk_sectors = n >> 9;
2924 if (mddev->reshape_position == MaxSector)
2925 mddev->chunk_sectors = n >> 9;
2929 static struct md_sysfs_entry md_chunk_size =
2930 __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
2933 resync_start_show(mddev_t *mddev, char *page)
2935 if (mddev->recovery_cp == MaxSector)
2936 return sprintf(page, "none\n");
2937 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2941 resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2944 unsigned long long n = simple_strtoull(buf, &e, 10);
2948 if (!*buf || (*e && *e != '\n'))
2951 mddev->recovery_cp = n;
2954 static struct md_sysfs_entry md_resync_start =
2955 __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
2958 * The array state can be:
2961 * No devices, no size, no level
2962 * Equivalent to STOP_ARRAY ioctl
2964 * May have some settings, but array is not active
2965 * all IO results in error
2966 * When written, doesn't tear down array, but just stops it
2967 * suspended (not supported yet)
2968 * All IO requests will block. The array can be reconfigured.
2969 * Writing this, if accepted, will block until array is quiescent
2971 * no resync can happen. no superblocks get written.
2972 * write requests fail
2974 * like readonly, but behaves like 'clean' on a write request.
2976 * clean - no pending writes, but otherwise active.
2977 * When written to inactive array, starts without resync
2978 * If a write request arrives then
2979 * if metadata is known, mark 'dirty' and switch to 'active'.
2980 * if not known, block and switch to write-pending
2981 * If written to an active array that has pending writes, then fails.
2983 * fully active: IO and resync can be happening.
2984 * When written to inactive array, starts with resync
2987 * clean, but writes are blocked waiting for 'active' to be written.
2990 * like active, but no writes have been seen for a while (100msec).
2993 enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
2994 write_pending, active_idle, bad_word};
2995 static char *array_states[] = {
2996 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2997 "write-pending", "active-idle", NULL };
2999 static int match_word(const char *word, char **list)
3002 for (n=0; list[n]; n++)
3003 if (cmd_match(word, list[n]))
3009 array_state_show(mddev_t *mddev, char *page)
3011 enum array_state st = inactive;
3024 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
3026 else if (mddev->safemode)
3032 if (list_empty(&mddev->disks) &&
3033 mddev->raid_disks == 0 &&
3034 mddev->dev_sectors == 0)
3039 return sprintf(page, "%s\n", array_states[st]);
3042 static int do_md_stop(mddev_t * mddev, int ro, int is_open);
3043 static int do_md_run(mddev_t * mddev);
3044 static int restart_array(mddev_t *mddev);
3047 array_state_store(mddev_t *mddev, const char *buf, size_t len)
3050 enum array_state st = match_word(buf, array_states);
3055 /* stopping an active array */
3056 if (atomic_read(&mddev->openers) > 0)
3058 err = do_md_stop(mddev, 0, 0);
3061 /* stopping an active array */
3063 if (atomic_read(&mddev->openers) > 0)
3065 err = do_md_stop(mddev, 2, 0);
3067 err = 0; /* already inactive */
3070 break; /* not supported yet */
3073 err = do_md_stop(mddev, 1, 0);
3076 set_disk_ro(mddev->gendisk, 1);
3077 err = do_md_run(mddev);
3083 err = do_md_stop(mddev, 1, 0);
3084 else if (mddev->ro == 1)
3085 err = restart_array(mddev);
3088 set_disk_ro(mddev->gendisk, 0);
3092 err = do_md_run(mddev);
3097 restart_array(mddev);
3098 spin_lock_irq(&mddev->write_lock);
3099 if (atomic_read(&mddev->writes_pending) == 0) {
3100 if (mddev->in_sync == 0) {
3102 if (mddev->safemode == 1)
3103 mddev->safemode = 0;
3104 if (mddev->persistent)
3105 set_bit(MD_CHANGE_CLEAN,
3111 spin_unlock_irq(&mddev->write_lock);
3117 restart_array(mddev);
3118 if (mddev->external)
3119 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
3120 wake_up(&mddev->sb_wait);
3124 set_disk_ro(mddev->gendisk, 0);
3125 err = do_md_run(mddev);
3130 /* these cannot be set */
3136 sysfs_notify_dirent(mddev->sysfs_state);
3140 static struct md_sysfs_entry md_array_state =
3141 __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
3144 null_show(mddev_t *mddev, char *page)
3150 new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3152 /* buf must be %d:%d\n? giving major and minor numbers */
3153 /* The new device is added to the array.
3154 * If the array has a persistent superblock, we read the
3155 * superblock to initialise info and check validity.
3156 * Otherwise, only checking done is that in bind_rdev_to_array,
3157 * which mainly checks size.
3160 int major = simple_strtoul(buf, &e, 10);
3166 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3168 minor = simple_strtoul(e+1, &e, 10);
3169 if (*e && *e != '\n')
3171 dev = MKDEV(major, minor);
3172 if (major != MAJOR(dev) ||
3173 minor != MINOR(dev))
3177 if (mddev->persistent) {
3178 rdev = md_import_device(dev, mddev->major_version,
3179 mddev->minor_version);
3180 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3181 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3182 mdk_rdev_t, same_set);
3183 err = super_types[mddev->major_version]
3184 .load_super(rdev, rdev0, mddev->minor_version);
3188 } else if (mddev->external)
3189 rdev = md_import_device(dev, -2, -1);
3191 rdev = md_import_device(dev, -1, -1);
3194 return PTR_ERR(rdev);
3195 err = bind_rdev_to_array(rdev, mddev);
3199 return err ? err : len;
3202 static struct md_sysfs_entry md_new_device =
3203 __ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
3206 bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3209 unsigned long chunk, end_chunk;
3213 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3215 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3216 if (buf == end) break;
3217 if (*end == '-') { /* range */
3219 end_chunk = simple_strtoul(buf, &end, 0);
3220 if (buf == end) break;
3222 if (*end && !isspace(*end)) break;
3223 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
3225 while (isspace(*buf)) buf++;
3227 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3232 static struct md_sysfs_entry md_bitmap =
3233 __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3236 size_show(mddev_t *mddev, char *page)
3238 return sprintf(page, "%llu\n",
3239 (unsigned long long)mddev->dev_sectors / 2);
3242 static int update_size(mddev_t *mddev, sector_t num_sectors);
3245 size_store(mddev_t *mddev, const char *buf, size_t len)
3247 /* If array is inactive, we can reduce the component size, but
3248 * not increase it (except from 0).
3249 * If array is active, we can try an on-line resize
3252 int err = strict_blocks_to_sectors(buf, §ors);
3257 err = update_size(mddev, sectors);
3258 md_update_sb(mddev, 1);
3260 if (mddev->dev_sectors == 0 ||
3261 mddev->dev_sectors > sectors)
3262 mddev->dev_sectors = sectors;
3266 return err ? err : len;
3269 static struct md_sysfs_entry md_size =
3270 __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
3275 * 'none' for arrays with no metadata (good luck...)
3276 * 'external' for arrays with externally managed metadata,
3277 * or N.M for internally known formats
3280 metadata_show(mddev_t *mddev, char *page)
3282 if (mddev->persistent)
3283 return sprintf(page, "%d.%d\n",
3284 mddev->major_version, mddev->minor_version);
3285 else if (mddev->external)
3286 return sprintf(page, "external:%s\n", mddev->metadata_type);
3288 return sprintf(page, "none\n");
3292 metadata_store(mddev_t *mddev, const char *buf, size_t len)
3296 /* Changing the details of 'external' metadata is
3297 * always permitted. Otherwise there must be
3298 * no devices attached to the array.
3300 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3302 else if (!list_empty(&mddev->disks))
3305 if (cmd_match(buf, "none")) {
3306 mddev->persistent = 0;
3307 mddev->external = 0;
3308 mddev->major_version = 0;
3309 mddev->minor_version = 90;
3312 if (strncmp(buf, "external:", 9) == 0) {
3313 size_t namelen = len-9;
3314 if (namelen >= sizeof(mddev->metadata_type))
3315 namelen = sizeof(mddev->metadata_type)-1;
3316 strncpy(mddev->metadata_type, buf+9, namelen);
3317 mddev->metadata_type[namelen] = 0;
3318 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3319 mddev->metadata_type[--namelen] = 0;
3320 mddev->persistent = 0;
3321 mddev->external = 1;
3322 mddev->major_version = 0;
3323 mddev->minor_version = 90;
3326 major = simple_strtoul(buf, &e, 10);
3327 if (e==buf || *e != '.')
3330 minor = simple_strtoul(buf, &e, 10);
3331 if (e==buf || (*e && *e != '\n') )
3333 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
3335 mddev->major_version = major;
3336 mddev->minor_version = minor;
3337 mddev->persistent = 1;
3338 mddev->external = 0;
3342 static struct md_sysfs_entry md_metadata =
3343 __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
3346 action_show(mddev_t *mddev, char *page)
3348 char *type = "idle";
3349 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3351 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3352 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
3353 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3355 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3356 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3358 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3362 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
3365 return sprintf(page, "%s\n", type);
3369 action_store(mddev_t *mddev, const char *page, size_t len)
3371 if (!mddev->pers || !mddev->pers->sync_request)
3374 if (cmd_match(page, "frozen"))
3375 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3377 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3379 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
3380 if (mddev->sync_thread) {
3381 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3382 md_unregister_thread(mddev->sync_thread);
3383 mddev->sync_thread = NULL;
3384 mddev->recovery = 0;
3386 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3387 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
3389 else if (cmd_match(page, "resync"))
3390 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3391 else if (cmd_match(page, "recover")) {
3392 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3393 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3394 } else if (cmd_match(page, "reshape")) {
3396 if (mddev->pers->start_reshape == NULL)
3398 err = mddev->pers->start_reshape(mddev);
3401 sysfs_notify(&mddev->kobj, NULL, "degraded");
3403 if (cmd_match(page, "check"))
3404 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3405 else if (!cmd_match(page, "repair"))
3407 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3408 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3410 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3411 md_wakeup_thread(mddev->thread);
3412 sysfs_notify_dirent(mddev->sysfs_action);
3417 mismatch_cnt_show(mddev_t *mddev, char *page)
3419 return sprintf(page, "%llu\n",
3420 (unsigned long long) mddev->resync_mismatches);
3423 static struct md_sysfs_entry md_scan_mode =
3424 __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
3427 static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
3430 sync_min_show(mddev_t *mddev, char *page)
3432 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3433 mddev->sync_speed_min ? "local": "system");
3437 sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3441 if (strncmp(buf, "system", 6)==0) {
3442 mddev->sync_speed_min = 0;
3445 min = simple_strtoul(buf, &e, 10);
3446 if (buf == e || (*e && *e != '\n') || min <= 0)
3448 mddev->sync_speed_min = min;
3452 static struct md_sysfs_entry md_sync_min =
3453 __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3456 sync_max_show(mddev_t *mddev, char *page)
3458 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3459 mddev->sync_speed_max ? "local": "system");
3463 sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3467 if (strncmp(buf, "system", 6)==0) {
3468 mddev->sync_speed_max = 0;
3471 max = simple_strtoul(buf, &e, 10);
3472 if (buf == e || (*e && *e != '\n') || max <= 0)
3474 mddev->sync_speed_max = max;
3478 static struct md_sysfs_entry md_sync_max =
3479 __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3482 degraded_show(mddev_t *mddev, char *page)
3484 return sprintf(page, "%d\n", mddev->degraded);
3486 static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
3489 sync_force_parallel_show(mddev_t *mddev, char *page)
3491 return sprintf(page, "%d\n", mddev->parallel_resync);
3495 sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3499 if (strict_strtol(buf, 10, &n))
3502 if (n != 0 && n != 1)
3505 mddev->parallel_resync = n;
3507 if (mddev->sync_thread)
3508 wake_up(&resync_wait);
3513 /* force parallel resync, even with shared block devices */
3514 static struct md_sysfs_entry md_sync_force_parallel =
3515 __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3516 sync_force_parallel_show, sync_force_parallel_store);
3519 sync_speed_show(mddev_t *mddev, char *page)
3521 unsigned long resync, dt, db;
3522 if (mddev->curr_resync == 0)
3523 return sprintf(page, "none\n");
3524 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3525 dt = (jiffies - mddev->resync_mark) / HZ;
3527 db = resync - mddev->resync_mark_cnt;
3528 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
3531 static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
3534 sync_completed_show(mddev_t *mddev, char *page)
3536 unsigned long max_sectors, resync;
3538 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3539 return sprintf(page, "none\n");
3541 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3542 max_sectors = mddev->resync_max_sectors;
3544 max_sectors = mddev->dev_sectors;
3546 resync = mddev->curr_resync_completed;
3547 return sprintf(page, "%lu / %lu\n", resync, max_sectors);
3550 static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
3553 min_sync_show(mddev_t *mddev, char *page)
3555 return sprintf(page, "%llu\n",
3556 (unsigned long long)mddev->resync_min);
3559 min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3561 unsigned long long min;
3562 if (strict_strtoull(buf, 10, &min))
3564 if (min > mddev->resync_max)
3566 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3569 /* Must be a multiple of chunk_size */
3570 if (mddev->chunk_sectors) {
3571 sector_t temp = min;
3572 if (sector_div(temp, mddev->chunk_sectors))
3575 mddev->resync_min = min;
3580 static struct md_sysfs_entry md_min_sync =
3581 __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3584 max_sync_show(mddev_t *mddev, char *page)
3586 if (mddev->resync_max == MaxSector)
3587 return sprintf(page, "max\n");
3589 return sprintf(page, "%llu\n",
3590 (unsigned long long)mddev->resync_max);
3593 max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3595 if (strncmp(buf, "max", 3) == 0)
3596 mddev->resync_max = MaxSector;
3598 unsigned long long max;
3599 if (strict_strtoull(buf, 10, &max))
3601 if (max < mddev->resync_min)
3603 if (max < mddev->resync_max &&
3604 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3607 /* Must be a multiple of chunk_size */
3608 if (mddev->chunk_sectors) {
3609 sector_t temp = max;
3610 if (sector_div(temp, mddev->chunk_sectors))
3613 mddev->resync_max = max;
3615 wake_up(&mddev->recovery_wait);
3619 static struct md_sysfs_entry md_max_sync =
3620 __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3623 suspend_lo_show(mddev_t *mddev, char *page)
3625 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3629 suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3632 unsigned long long new = simple_strtoull(buf, &e, 10);
3634 if (mddev->pers == NULL ||
3635 mddev->pers->quiesce == NULL)
3637 if (buf == e || (*e && *e != '\n'))
3639 if (new >= mddev->suspend_hi ||
3640 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3641 mddev->suspend_lo = new;
3642 mddev->pers->quiesce(mddev, 2);
3647 static struct md_sysfs_entry md_suspend_lo =
3648 __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3652 suspend_hi_show(mddev_t *mddev, char *page)
3654 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3658 suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3661 unsigned long long new = simple_strtoull(buf, &e, 10);
3663 if (mddev->pers == NULL ||
3664 mddev->pers->quiesce == NULL)
3666 if (buf == e || (*e && *e != '\n'))
3668 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3669 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3670 mddev->suspend_hi = new;
3671 mddev->pers->quiesce(mddev, 1);
3672 mddev->pers->quiesce(mddev, 0);
3677 static struct md_sysfs_entry md_suspend_hi =
3678 __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3681 reshape_position_show(mddev_t *mddev, char *page)
3683 if (mddev->reshape_position != MaxSector)
3684 return sprintf(page, "%llu\n",
3685 (unsigned long long)mddev->reshape_position);
3686 strcpy(page, "none\n");
3691 reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3694 unsigned long long new = simple_strtoull(buf, &e, 10);
3697 if (buf == e || (*e && *e != '\n'))
3699 mddev->reshape_position = new;
3700 mddev->delta_disks = 0;
3701 mddev->new_level = mddev->level;
3702 mddev->new_layout = mddev->layout;
3703 mddev->new_chunk_sectors = mddev->chunk_sectors;
3707 static struct md_sysfs_entry md_reshape_position =
3708 __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3709 reshape_position_store);
3712 array_size_show(mddev_t *mddev, char *page)
3714 if (mddev->external_size)
3715 return sprintf(page, "%llu\n",
3716 (unsigned long long)mddev->array_sectors/2);
3718 return sprintf(page, "default\n");
3722 array_size_store(mddev_t *mddev, const char *buf, size_t len)
3726 if (strncmp(buf, "default", 7) == 0) {
3728 sectors = mddev->pers->size(mddev, 0, 0);
3730 sectors = mddev->array_sectors;
3732 mddev->external_size = 0;
3734 if (strict_blocks_to_sectors(buf, §ors) < 0)
3736 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
3739 mddev->external_size = 1;
3742 mddev->array_sectors = sectors;
3743 set_capacity(mddev->gendisk, mddev->array_sectors);
3745 revalidate_disk(mddev->gendisk);
3750 static struct md_sysfs_entry md_array_size =
3751 __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
3754 static struct attribute *md_default_attrs[] = {
3757 &md_raid_disks.attr,
3758 &md_chunk_size.attr,
3760 &md_resync_start.attr,
3762 &md_new_device.attr,
3763 &md_safe_delay.attr,
3764 &md_array_state.attr,
3765 &md_reshape_position.attr,
3766 &md_array_size.attr,
3770 static struct attribute *md_redundancy_attrs[] = {
3772 &md_mismatches.attr,
3775 &md_sync_speed.attr,
3776 &md_sync_force_parallel.attr,
3777 &md_sync_completed.attr,
3780 &md_suspend_lo.attr,
3781 &md_suspend_hi.attr,
3786 static struct attribute_group md_redundancy_group = {
3788 .attrs = md_redundancy_attrs,
3793 md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3795 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3796 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
3801 rv = mddev_lock(mddev);
3803 rv = entry->show(mddev, page);
3804 mddev_unlock(mddev);
3810 md_attr_store(struct kobject *kobj, struct attribute *attr,
3811 const char *page, size_t length)
3813 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3814 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
3819 if (!capable(CAP_SYS_ADMIN))
3821 rv = mddev_lock(mddev);
3822 if (mddev->hold_active == UNTIL_IOCTL)
3823 mddev->hold_active = 0;
3825 rv = entry->store(mddev, page, length);
3826 mddev_unlock(mddev);
3831 static void md_free(struct kobject *ko)
3833 mddev_t *mddev = container_of(ko, mddev_t, kobj);
3835 if (mddev->sysfs_state)
3836 sysfs_put(mddev->sysfs_state);
3838 if (mddev->gendisk) {
3839 del_gendisk(mddev->gendisk);
3840 put_disk(mddev->gendisk);
3843 blk_cleanup_queue(mddev->queue);
3848 static struct sysfs_ops md_sysfs_ops = {
3849 .show = md_attr_show,
3850 .store = md_attr_store,
3852 static struct kobj_type md_ktype = {
3854 .sysfs_ops = &md_sysfs_ops,
3855 .default_attrs = md_default_attrs,
3860 static void mddev_delayed_delete(struct work_struct *ws)
3862 mddev_t *mddev = container_of(ws, mddev_t, del_work);
3864 if (mddev->private == &md_redundancy_group) {
3865 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
3866 if (mddev->sysfs_action)
3867 sysfs_put(mddev->sysfs_action);
3868 mddev->sysfs_action = NULL;
3869 mddev->private = NULL;
3871 kobject_del(&mddev->kobj);
3872 kobject_put(&mddev->kobj);
3875 static int md_alloc(dev_t dev, char *name)
3877 static DEFINE_MUTEX(disks_mutex);
3878 mddev_t *mddev = mddev_find(dev);
3879 struct gendisk *disk;
3888 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
3889 shift = partitioned ? MdpMinorShift : 0;
3890 unit = MINOR(mddev->unit) >> shift;
3892 /* wait for any previous instance if this device
3893 * to be completed removed (mddev_delayed_delete).
3895 flush_scheduled_work();
3897 mutex_lock(&disks_mutex);
3903 /* Need to ensure that 'name' is not a duplicate.
3906 spin_lock(&all_mddevs_lock);
3908 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
3909 if (mddev2->gendisk &&
3910 strcmp(mddev2->gendisk->disk_name, name) == 0) {
3911 spin_unlock(&all_mddevs_lock);
3914 spin_unlock(&all_mddevs_lock);
3918 mddev->queue = blk_alloc_queue(GFP_KERNEL);
3921 mddev->queue->queuedata = mddev;
3923 /* Can be unlocked because the queue is new: no concurrency */
3924 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue);
3926 blk_queue_make_request(mddev->queue, md_make_request);
3928 disk = alloc_disk(1 << shift);
3930 blk_cleanup_queue(mddev->queue);
3931 mddev->queue = NULL;
3934 disk->major = MAJOR(mddev->unit);
3935 disk->first_minor = unit << shift;
3937 strcpy(disk->disk_name, name);
3938 else if (partitioned)
3939 sprintf(disk->disk_name, "md_d%d", unit);
3941 sprintf(disk->disk_name, "md%d", unit);
3942 disk->fops = &md_fops;
3943 disk->private_data = mddev;
3944 disk->queue = mddev->queue;
3945 /* Allow extended partitions. This makes the
3946 * 'mdp' device redundant, but we can't really
3949 disk->flags |= GENHD_FL_EXT_DEVT;
3951 mddev->gendisk = disk;
3952 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
3953 &disk_to_dev(disk)->kobj, "%s", "md");
3955 /* This isn't possible, but as kobject_init_and_add is marked
3956 * __must_check, we must do something with the result
3958 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
3963 mutex_unlock(&disks_mutex);
3965 kobject_uevent(&mddev->kobj, KOBJ_ADD);
3966 mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state");
3972 static struct kobject *md_probe(dev_t dev, int *part, void *data)
3974 md_alloc(dev, NULL);
3978 static int add_named_array(const char *val, struct kernel_param *kp)
3980 /* val must be "md_*" where * is not all digits.
3981 * We allocate an array with a large free minor number, and
3982 * set the name to val. val must not already be an active name.
3984 int len = strlen(val);
3985 char buf[DISK_NAME_LEN];
3987 while (len && val[len-1] == '\n')
3989 if (len >= DISK_NAME_LEN)
3991 strlcpy(buf, val, len+1);
3992 if (strncmp(buf, "md_", 3) != 0)
3994 return md_alloc(0, buf);
3997 static void md_safemode_timeout(unsigned long data)
3999 mddev_t *mddev = (mddev_t *) data;
4001 if (!atomic_read(&mddev->writes_pending)) {
4002 mddev->safemode = 1;
4003 if (mddev->external)
4004 sysfs_notify_dirent(mddev->sysfs_state);
4006 md_wakeup_thread(mddev->thread);
4009 static int start_dirty_degraded;
4011 static int do_md_run(mddev_t * mddev)
4015 struct gendisk *disk;
4016 struct mdk_personality *pers;
4018 if (list_empty(&mddev->disks))
4019 /* cannot run an array with no devices.. */
4026 * Analyze all RAID superblock(s)
4028 if (!mddev->raid_disks) {
4029 if (!mddev->persistent)
4034 if (mddev->level != LEVEL_NONE)
4035 request_module("md-level-%d", mddev->level);
4036 else if (mddev->clevel[0])
4037 request_module("md-%s", mddev->clevel);
4040 * Drop all container device buffers, from now on
4041 * the only valid external interface is through the md
4044 list_for_each_entry(rdev, &mddev->disks, same_set) {
4045 if (test_bit(Faulty, &rdev->flags))
4047 sync_blockdev(rdev->bdev);
4048 invalidate_bdev(rdev->bdev);
4050 /* perform some consistency tests on the device.
4051 * We don't want the data to overlap the metadata,
4052 * Internal Bitmap issues have been handled elsewhere.
4054 if (rdev->data_offset < rdev->sb_start) {
4055 if (mddev->dev_sectors &&
4056 rdev->data_offset + mddev->dev_sectors
4058 printk("md: %s: data overlaps metadata\n",
4063 if (rdev->sb_start + rdev->sb_size/512
4064 > rdev->data_offset) {
4065 printk("md: %s: metadata overlaps data\n",
4070 sysfs_notify_dirent(rdev->sysfs_state);
4073 md_probe(mddev->unit, NULL, NULL);
4074 disk = mddev->gendisk;
4078 spin_lock(&pers_lock);
4079 pers = find_pers(mddev->level, mddev->clevel);
4080 if (!pers || !try_module_get(pers->owner)) {
4081 spin_unlock(&pers_lock);
4082 if (mddev->level != LEVEL_NONE)
4083 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4086 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4091 spin_unlock(&pers_lock);
4092 if (mddev->level != pers->level) {
4093 mddev->level = pers->level;
4094 mddev->new_level = pers->level;
4096 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
4098 if (mddev->reshape_position != MaxSector &&
4099 pers->start_reshape == NULL) {
4100 /* This personality cannot handle reshaping... */
4102 module_put(pers->owner);
4106 if (pers->sync_request) {
4107 /* Warn if this is a potentially silly
4110 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4114 list_for_each_entry(rdev, &mddev->disks, same_set)
4115 list_for_each_entry(rdev2, &mddev->disks, same_set) {
4117 rdev->bdev->bd_contains ==
4118 rdev2->bdev->bd_contains) {
4120 "%s: WARNING: %s appears to be"
4121 " on the same physical disk as"
4124 bdevname(rdev->bdev,b),
4125 bdevname(rdev2->bdev,b2));
4132 "True protection against single-disk"
4133 " failure might be compromised.\n");
4136 mddev->recovery = 0;
4137 /* may be over-ridden by personality */
4138 mddev->resync_max_sectors = mddev->dev_sectors;
4140 mddev->barriers_work = 1;
4141 mddev->ok_start_degraded = start_dirty_degraded;
4144 mddev->ro = 2; /* read-only, but switch on first write */
4146 err = mddev->pers->run(mddev);
4148 printk(KERN_ERR "md: pers->run() failed ...\n");
4149 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4150 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4151 " but 'external_size' not in effect?\n", __func__);
4153 "md: invalid array_size %llu > default size %llu\n",
4154 (unsigned long long)mddev->array_sectors / 2,
4155 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4157 mddev->pers->stop(mddev);
4159 if (err == 0 && mddev->pers->sync_request) {
4160 err = bitmap_create(mddev);
4162 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4163 mdname(mddev), err);
4164 mddev->pers->stop(mddev);
4168 module_put(mddev->pers->owner);
4170 bitmap_destroy(mddev);
4173 if (mddev->pers->sync_request) {
4174 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
4176 "md: cannot register extra attributes for %s\n",
4178 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
4179 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
4182 atomic_set(&mddev->writes_pending,0);
4183 mddev->safemode = 0;
4184 mddev->safemode_timer.function = md_safemode_timeout;
4185 mddev->safemode_timer.data = (unsigned long) mddev;
4186 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
4189 list_for_each_entry(rdev, &mddev->disks, same_set)
4190 if (rdev->raid_disk >= 0) {
4192 sprintf(nm, "rd%d", rdev->raid_disk);
4193 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
4194 printk("md: cannot register %s for %s\n",
4198 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4201 md_update_sb(mddev, 0);
4203 set_capacity(disk, mddev->array_sectors);
4205 /* If there is a partially-recovered drive we need to
4206 * start recovery here. If we leave it to md_check_recovery,
4207 * it will remove the drives and not do the right thing
4209 if (mddev->degraded && !mddev->sync_thread) {
4211 list_for_each_entry(rdev, &mddev->disks, same_set)
4212 if (rdev->raid_disk >= 0 &&
4213 !test_bit(In_sync, &rdev->flags) &&
4214 !test_bit(Faulty, &rdev->flags))
4215 /* complete an interrupted recovery */
4217 if (spares && mddev->pers->sync_request) {
4218 mddev->recovery = 0;
4219 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4220 mddev->sync_thread = md_register_thread(md_do_sync,
4223 if (!mddev->sync_thread) {
4224 printk(KERN_ERR "%s: could not start resync"
4227 /* leave the spares where they are, it shouldn't hurt */
4228 mddev->recovery = 0;
4232 md_wakeup_thread(mddev->thread);
4233 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
4235 revalidate_disk(mddev->gendisk);
4237 md_new_event(mddev);
4238 sysfs_notify_dirent(mddev->sysfs_state);
4239 if (mddev->sysfs_action)
4240 sysfs_notify_dirent(mddev->sysfs_action);
4241 sysfs_notify(&mddev->kobj, NULL, "degraded");
4242 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4246 static int restart_array(mddev_t *mddev)
4248 struct gendisk *disk = mddev->gendisk;
4250 /* Complain if it has no devices */
4251 if (list_empty(&mddev->disks))
4257 mddev->safemode = 0;
4259 set_disk_ro(disk, 0);
4260 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4262 /* Kick recovery or resync if necessary */
4263 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4264 md_wakeup_thread(mddev->thread);
4265 md_wakeup_thread(mddev->sync_thread);
4266 sysfs_notify_dirent(mddev->sysfs_state);
4270 /* similar to deny_write_access, but accounts for our holding a reference
4271 * to the file ourselves */
4272 static int deny_bitmap_write_access(struct file * file)
4274 struct inode *inode = file->f_mapping->host;
4276 spin_lock(&inode->i_lock);
4277 if (atomic_read(&inode->i_writecount) > 1) {
4278 spin_unlock(&inode->i_lock);
4281 atomic_set(&inode->i_writecount, -1);
4282 spin_unlock(&inode->i_lock);
4287 static void restore_bitmap_write_access(struct file *file)
4289 struct inode *inode = file->f_mapping->host;
4291 spin_lock(&inode->i_lock);
4292 atomic_set(&inode->i_writecount, 1);
4293 spin_unlock(&inode->i_lock);
4297 * 0 - completely stop and dis-assemble array
4298 * 1 - switch to readonly
4299 * 2 - stop but do not disassemble array
4301 static int do_md_stop(mddev_t * mddev, int mode, int is_open)
4304 struct gendisk *disk = mddev->gendisk;
4307 if (atomic_read(&mddev->openers) > is_open) {
4308 printk("md: %s still in use.\n",mdname(mddev));
4314 if (mddev->sync_thread) {
4315 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4316 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4317 md_unregister_thread(mddev->sync_thread);
4318 mddev->sync_thread = NULL;
4321 del_timer_sync(&mddev->safemode_timer);
4324 case 1: /* readonly */
4330 case 0: /* disassemble */
4332 bitmap_flush(mddev);
4333 md_super_wait(mddev);
4335 set_disk_ro(disk, 0);
4337 mddev->pers->stop(mddev);
4338 mddev->queue->merge_bvec_fn = NULL;
4339 mddev->queue->unplug_fn = NULL;
4340 mddev->queue->backing_dev_info.congested_fn = NULL;
4341 module_put(mddev->pers->owner);
4342 if (mddev->pers->sync_request)
4343 mddev->private = &md_redundancy_group;
4345 /* tell userspace to handle 'inactive' */
4346 sysfs_notify_dirent(mddev->sysfs_state);
4348 list_for_each_entry(rdev, &mddev->disks, same_set)
4349 if (rdev->raid_disk >= 0) {
4351 sprintf(nm, "rd%d", rdev->raid_disk);
4352 sysfs_remove_link(&mddev->kobj, nm);
4355 set_capacity(disk, 0);
4361 if (!mddev->in_sync || mddev->flags) {
4362 /* mark array as shutdown cleanly */
4364 md_update_sb(mddev, 1);
4367 set_disk_ro(disk, 1);
4368 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4372 * Free resources if final stop
4376 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4378 bitmap_destroy(mddev);
4379 if (mddev->bitmap_file) {
4380 restore_bitmap_write_access(mddev->bitmap_file);
4381 fput(mddev->bitmap_file);
4382 mddev->bitmap_file = NULL;
4384 mddev->bitmap_offset = 0;
4386 /* make sure all md_delayed_delete calls have finished */
4387 flush_scheduled_work();
4389 export_array(mddev);
4391 mddev->array_sectors = 0;
4392 mddev->external_size = 0;
4393 mddev->dev_sectors = 0;
4394 mddev->raid_disks = 0;
4395 mddev->recovery_cp = 0;
4396 mddev->resync_min = 0;
4397 mddev->resync_max = MaxSector;
4398 mddev->reshape_position = MaxSector;
4399 mddev->external = 0;
4400 mddev->persistent = 0;
4401 mddev->level = LEVEL_NONE;
4402 mddev->clevel[0] = 0;
4405 mddev->metadata_type[0] = 0;
4406 mddev->chunk_sectors = 0;
4407 mddev->ctime = mddev->utime = 0;
4409 mddev->max_disks = 0;
4411 mddev->delta_disks = 0;
4412 mddev->new_level = LEVEL_NONE;
4413 mddev->new_layout = 0;
4414 mddev->new_chunk_sectors = 0;
4415 mddev->curr_resync = 0;
4416 mddev->resync_mismatches = 0;
4417 mddev->suspend_lo = mddev->suspend_hi = 0;
4418 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4419 mddev->recovery = 0;
4422 mddev->degraded = 0;
4423 mddev->barriers_work = 0;
4424 mddev->safemode = 0;
4425 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4426 if (mddev->hold_active == UNTIL_STOP)
4427 mddev->hold_active = 0;
4429 } else if (mddev->pers)
4430 printk(KERN_INFO "md: %s switched to read-only mode.\n",
4433 blk_integrity_unregister(disk);
4434 md_new_event(mddev);
4435 sysfs_notify_dirent(mddev->sysfs_state);
4441 static void autorun_array(mddev_t *mddev)
4446 if (list_empty(&mddev->disks))
4449 printk(KERN_INFO "md: running: ");
4451 list_for_each_entry(rdev, &mddev->disks, same_set) {
4452 char b[BDEVNAME_SIZE];
4453 printk("<%s>", bdevname(rdev->bdev,b));
4457 err = do_md_run(mddev);
4459 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
4460 do_md_stop(mddev, 0, 0);
4465 * lets try to run arrays based on all disks that have arrived
4466 * until now. (those are in pending_raid_disks)
4468 * the method: pick the first pending disk, collect all disks with
4469 * the same UUID, remove all from the pending list and put them into
4470 * the 'same_array' list. Then order this list based on superblock
4471 * update time (freshest comes first), kick out 'old' disks and
4472 * compare superblocks. If everything's fine then run it.
4474 * If "unit" is allocated, then bump its reference count
4476 static void autorun_devices(int part)
4478 mdk_rdev_t *rdev0, *rdev, *tmp;
4480 char b[BDEVNAME_SIZE];
4482 printk(KERN_INFO "md: autorun ...\n");
4483 while (!list_empty(&pending_raid_disks)) {
4486 LIST_HEAD(candidates);
4487 rdev0 = list_entry(pending_raid_disks.next,
4488 mdk_rdev_t, same_set);
4490 printk(KERN_INFO "md: considering %s ...\n",
4491 bdevname(rdev0->bdev,b));
4492 INIT_LIST_HEAD(&candidates);
4493 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
4494 if (super_90_load(rdev, rdev0, 0) >= 0) {
4495 printk(KERN_INFO "md: adding %s ...\n",
4496 bdevname(rdev->bdev,b));
4497 list_move(&rdev->same_set, &candidates);
4500 * now we have a set of devices, with all of them having
4501 * mostly sane superblocks. It's time to allocate the
4505 dev = MKDEV(mdp_major,
4506 rdev0->preferred_minor << MdpMinorShift);
4507 unit = MINOR(dev) >> MdpMinorShift;
4509 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4512 if (rdev0->preferred_minor != unit) {
4513 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4514 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4518 md_probe(dev, NULL, NULL);
4519 mddev = mddev_find(dev);
4520 if (!mddev || !mddev->gendisk) {
4524 "md: cannot allocate memory for md drive.\n");
4527 if (mddev_lock(mddev))
4528 printk(KERN_WARNING "md: %s locked, cannot run\n",
4530 else if (mddev->raid_disks || mddev->major_version
4531 || !list_empty(&mddev->disks)) {
4533 "md: %s already running, cannot run %s\n",
4534 mdname(mddev), bdevname(rdev0->bdev,b));
4535 mddev_unlock(mddev);
4537 printk(KERN_INFO "md: created %s\n", mdname(mddev));
4538 mddev->persistent = 1;
4539 rdev_for_each_list(rdev, tmp, &candidates) {
4540 list_del_init(&rdev->same_set);
4541 if (bind_rdev_to_array(rdev, mddev))
4544 autorun_array(mddev);
4545 mddev_unlock(mddev);
4547 /* on success, candidates will be empty, on error
4550 rdev_for_each_list(rdev, tmp, &candidates) {
4551 list_del_init(&rdev->same_set);
4556 printk(KERN_INFO "md: ... autorun DONE.\n");
4558 #endif /* !MODULE */
4560 static int get_version(void __user * arg)
4564 ver.major = MD_MAJOR_VERSION;
4565 ver.minor = MD_MINOR_VERSION;
4566 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4568 if (copy_to_user(arg, &ver, sizeof(ver)))
4574 static int get_array_info(mddev_t * mddev, void __user * arg)
4576 mdu_array_info_t info;
4577 int nr,working,active,failed,spare;
4580 nr=working=active=failed=spare=0;
4581 list_for_each_entry(rdev, &mddev->disks, same_set) {
4583 if (test_bit(Faulty, &rdev->flags))
4587 if (test_bit(In_sync, &rdev->flags))
4594 info.major_version = mddev->major_version;
4595 info.minor_version = mddev->minor_version;
4596 info.patch_version = MD_PATCHLEVEL_VERSION;
4597 info.ctime = mddev->ctime;
4598 info.level = mddev->level;
4599 info.size = mddev->dev_sectors / 2;
4600 if (info.size != mddev->dev_sectors / 2) /* overflow */
4603 info.raid_disks = mddev->raid_disks;
4604 info.md_minor = mddev->md_minor;
4605 info.not_persistent= !mddev->persistent;
4607 info.utime = mddev->utime;
4610 info.state = (1<<MD_SB_CLEAN);
4611 if (mddev->bitmap && mddev->bitmap_offset)
4612 info.state = (1<<MD_SB_BITMAP_PRESENT);
4613 info.active_disks = active;
4614 info.working_disks = working;
4615 info.failed_disks = failed;
4616 info.spare_disks = spare;
4618 info.layout = mddev->layout;
4619 info.chunk_size = mddev->chunk_sectors << 9;
4621 if (copy_to_user(arg, &info, sizeof(info)))
4627 static int get_bitmap_file(mddev_t * mddev, void __user * arg)
4629 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4630 char *ptr, *buf = NULL;
4633 if (md_allow_write(mddev))
4634 file = kmalloc(sizeof(*file), GFP_NOIO);
4636 file = kmalloc(sizeof(*file), GFP_KERNEL);
4641 /* bitmap disabled, zero the first byte and copy out */
4642 if (!mddev->bitmap || !mddev->bitmap->file) {
4643 file->pathname[0] = '\0';
4647 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4651 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4655 strcpy(file->pathname, ptr);
4659 if (copy_to_user(arg, file, sizeof(*file)))
4667 static int get_disk_info(mddev_t * mddev, void __user * arg)
4669 mdu_disk_info_t info;
4672 if (copy_from_user(&info, arg, sizeof(info)))
4675 rdev = find_rdev_nr(mddev, info.number);
4677 info.major = MAJOR(rdev->bdev->bd_dev);
4678 info.minor = MINOR(rdev->bdev->bd_dev);
4679 info.raid_disk = rdev->raid_disk;
4681 if (test_bit(Faulty, &rdev->flags))
4682 info.state |= (1<<MD_DISK_FAULTY);
4683 else if (test_bit(In_sync, &rdev->flags)) {
4684 info.state |= (1<<MD_DISK_ACTIVE);
4685 info.state |= (1<<MD_DISK_SYNC);
4687 if (test_bit(WriteMostly, &rdev->flags))
4688 info.state |= (1<<MD_DISK_WRITEMOSTLY);
4690 info.major = info.minor = 0;
4691 info.raid_disk = -1;
4692 info.state = (1<<MD_DISK_REMOVED);
4695 if (copy_to_user(arg, &info, sizeof(info)))
4701 static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4703 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4705 dev_t dev = MKDEV(info->major,info->minor);
4707 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4710 if (!mddev->raid_disks) {
4712 /* expecting a device which has a superblock */
4713 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4716 "md: md_import_device returned %ld\n",
4718 return PTR_ERR(rdev);
4720 if (!list_empty(&mddev->disks)) {
4721 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4722 mdk_rdev_t, same_set);
4723 int err = super_types[mddev->major_version]
4724 .load_super(rdev, rdev0, mddev->minor_version);
4727 "md: %s has different UUID to %s\n",
4728 bdevname(rdev->bdev,b),
4729 bdevname(rdev0->bdev,b2));
4734 err = bind_rdev_to_array(rdev, mddev);
4741 * add_new_disk can be used once the array is assembled
4742 * to add "hot spares". They must already have a superblock
4747 if (!mddev->pers->hot_add_disk) {
4749 "%s: personality does not support diskops!\n",
4753 if (mddev->persistent)
4754 rdev = md_import_device(dev, mddev->major_version,
4755 mddev->minor_version);
4757 rdev = md_import_device(dev, -1, -1);
4760 "md: md_import_device returned %ld\n",
4762 return PTR_ERR(rdev);
4764 /* set save_raid_disk if appropriate */
4765 if (!mddev->persistent) {
4766 if (info->state & (1<<MD_DISK_SYNC) &&
4767 info->raid_disk < mddev->raid_disks)
4768 rdev->raid_disk = info->raid_disk;
4770 rdev->raid_disk = -1;
4772 super_types[mddev->major_version].
4773 validate_super(mddev, rdev);
4774 rdev->saved_raid_disk = rdev->raid_disk;
4776 clear_bit(In_sync, &rdev->flags); /* just to be sure */
4777 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4778 set_bit(WriteMostly, &rdev->flags);
4780 clear_bit(WriteMostly, &rdev->flags);
4782 rdev->raid_disk = -1;
4783 err = bind_rdev_to_array(rdev, mddev);
4784 if (!err && !mddev->pers->hot_remove_disk) {
4785 /* If there is hot_add_disk but no hot_remove_disk
4786 * then added disks for geometry changes,
4787 * and should be added immediately.
4789 super_types[mddev->major_version].
4790 validate_super(mddev, rdev);
4791 err = mddev->pers->hot_add_disk(mddev, rdev);
4793 unbind_rdev_from_array(rdev);
4798 sysfs_notify_dirent(rdev->sysfs_state);
4800 md_update_sb(mddev, 1);
4801 if (mddev->degraded)
4802 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4803 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4804 md_wakeup_thread(mddev->thread);
4808 /* otherwise, add_new_disk is only allowed
4809 * for major_version==0 superblocks
4811 if (mddev->major_version != 0) {
4812 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
4817 if (!(info->state & (1<<MD_DISK_FAULTY))) {
4819 rdev = md_import_device(dev, -1, 0);
4822 "md: error, md_import_device() returned %ld\n",
4824 return PTR_ERR(rdev);
4826 rdev->desc_nr = info->number;
4827 if (info->raid_disk < mddev->raid_disks)
4828 rdev->raid_disk = info->raid_disk;
4830 rdev->raid_disk = -1;
4832 if (rdev->raid_disk < mddev->raid_disks)
4833 if (info->state & (1<<MD_DISK_SYNC))
4834 set_bit(In_sync, &rdev->flags);
4836 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4837 set_bit(WriteMostly, &rdev->flags);
4839 if (!mddev->persistent) {
4840 printk(KERN_INFO "md: nonpersistent superblock ...\n");
4841 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
4843 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
4844 rdev->sectors = rdev->sb_start;
4846 err = bind_rdev_to_array(rdev, mddev);
4856 static int hot_remove_disk(mddev_t * mddev, dev_t dev)
4858 char b[BDEVNAME_SIZE];
4861 rdev = find_rdev(mddev, dev);
4865 if (rdev->raid_disk >= 0)
4868 kick_rdev_from_array(rdev);
4869 md_update_sb(mddev, 1);
4870 md_new_event(mddev);
4874 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
4875 bdevname(rdev->bdev,b), mdname(mddev));
4879 static int hot_add_disk(mddev_t * mddev, dev_t dev)
4881 char b[BDEVNAME_SIZE];
4888 if (mddev->major_version != 0) {
4889 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
4890 " version-0 superblocks.\n",
4894 if (!mddev->pers->hot_add_disk) {
4896 "%s: personality does not support diskops!\n",
4901 rdev = md_import_device(dev, -1, 0);
4904 "md: error, md_import_device() returned %ld\n",
4909 if (mddev->persistent)
4910 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
4912 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
4914 rdev->sectors = rdev->sb_start;
4916 if (test_bit(Faulty, &rdev->flags)) {
4918 "md: can not hot-add faulty %s disk to %s!\n",
4919 bdevname(rdev->bdev,b), mdname(mddev));
4923 clear_bit(In_sync, &rdev->flags);
4925 rdev->saved_raid_disk = -1;
4926 err = bind_rdev_to_array(rdev, mddev);
4931 * The rest should better be atomic, we can have disk failures
4932 * noticed in interrupt contexts ...
4935 rdev->raid_disk = -1;
4937 md_update_sb(mddev, 1);
4940 * Kick recovery, maybe this spare has to be added to the
4941 * array immediately.
4943 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4944 md_wakeup_thread(mddev->thread);
4945 md_new_event(mddev);
4953 static int set_bitmap_file(mddev_t *mddev, int fd)
4958 if (!mddev->pers->quiesce)
4960 if (mddev->recovery || mddev->sync_thread)
4962 /* we should be able to change the bitmap.. */
4968 return -EEXIST; /* cannot add when bitmap is present */
4969 mddev->bitmap_file = fget(fd);
4971 if (mddev->bitmap_file == NULL) {
4972 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
4977 err = deny_bitmap_write_access(mddev->bitmap_file);
4979 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
4981 fput(mddev->bitmap_file);
4982 mddev->bitmap_file = NULL;
4985 mddev->bitmap_offset = 0; /* file overrides offset */
4986 } else if (mddev->bitmap == NULL)
4987 return -ENOENT; /* cannot remove what isn't there */
4990 mddev->pers->quiesce(mddev, 1);
4992 err = bitmap_create(mddev);
4993 if (fd < 0 || err) {
4994 bitmap_destroy(mddev);
4995 fd = -1; /* make sure to put the file */
4997 mddev->pers->quiesce(mddev, 0);
5000 if (mddev->bitmap_file) {
5001 restore_bitmap_write_access(mddev->bitmap_file);
5002 fput(mddev->bitmap_file);
5004 mddev->bitmap_file = NULL;
5011 * set_array_info is used two different ways
5012 * The original usage is when creating a new array.
5013 * In this usage, raid_disks is > 0 and it together with
5014 * level, size, not_persistent,layout,chunksize determine the
5015 * shape of the array.
5016 * This will always create an array with a type-0.90.0 superblock.
5017 * The newer usage is when assembling an array.
5018 * In this case raid_disks will be 0, and the major_version field is
5019 * use to determine which style super-blocks are to be found on the devices.
5020 * The minor and patch _version numbers are also kept incase the
5021 * super_block handler wishes to interpret them.
5023 static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5026 if (info->raid_disks == 0) {
5027 /* just setting version number for superblock loading */
5028 if (info->major_version < 0 ||
5029 info->major_version >= ARRAY_SIZE(super_types) ||
5030 super_types[info->major_version].name == NULL) {
5031 /* maybe try to auto-load a module? */
5033 "md: superblock version %d not known\n",
5034 info->major_version);
5037 mddev->major_version = info->major_version;
5038 mddev->minor_version = info->minor_version;
5039 mddev->patch_version = info->patch_version;
5040 mddev->persistent = !info->not_persistent;
5043 mddev->major_version = MD_MAJOR_VERSION;
5044 mddev->minor_version = MD_MINOR_VERSION;
5045 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5046 mddev->ctime = get_seconds();
5048 mddev->level = info->level;
5049 mddev->clevel[0] = 0;
5050 mddev->dev_sectors = 2 * (sector_t)info->size;
5051 mddev->raid_disks = info->raid_disks;
5052 /* don't set md_minor, it is determined by which /dev/md* was
5055 if (info->state & (1<<MD_SB_CLEAN))
5056 mddev->recovery_cp = MaxSector;
5058 mddev->recovery_cp = 0;
5059 mddev->persistent = ! info->not_persistent;
5060 mddev->external = 0;
5062 mddev->layout = info->layout;
5063 mddev->chunk_sectors = info->chunk_size >> 9;
5065 mddev->max_disks = MD_SB_DISKS;
5067 if (mddev->persistent)
5069 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5071 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
5072 mddev->bitmap_offset = 0;
5074 mddev->reshape_position = MaxSector;
5077 * Generate a 128 bit UUID
5079 get_random_bytes(mddev->uuid, 16);
5081 mddev->new_level = mddev->level;
5082 mddev->new_chunk_sectors = mddev->chunk_sectors;
5083 mddev->new_layout = mddev->layout;
5084 mddev->delta_disks = 0;
5089 void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5091 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5093 if (mddev->external_size)
5096 mddev->array_sectors = array_sectors;
5098 EXPORT_SYMBOL(md_set_array_sectors);
5100 static int update_size(mddev_t *mddev, sector_t num_sectors)
5104 int fit = (num_sectors == 0);
5106 if (mddev->pers->resize == NULL)
5108 /* The "num_sectors" is the number of sectors of each device that
5109 * is used. This can only make sense for arrays with redundancy.
5110 * linear and raid0 always use whatever space is available. We can only
5111 * consider changing this number if no resync or reconstruction is
5112 * happening, and if the new size is acceptable. It must fit before the
5113 * sb_start or, if that is <data_offset, it must fit before the size
5114 * of each device. If num_sectors is zero, we find the largest size
5118 if (mddev->sync_thread)
5121 /* Sorry, cannot grow a bitmap yet, just remove it,
5125 list_for_each_entry(rdev, &mddev->disks, same_set) {
5126 sector_t avail = rdev->sectors;
5128 if (fit && (num_sectors == 0 || num_sectors > avail))
5129 num_sectors = avail;
5130 if (avail < num_sectors)
5133 rv = mddev->pers->resize(mddev, num_sectors);
5135 revalidate_disk(mddev->gendisk);
5139 static int update_raid_disks(mddev_t *mddev, int raid_disks)
5142 /* change the number of raid disks */
5143 if (mddev->pers->check_reshape == NULL)
5145 if (raid_disks <= 0 ||
5146 raid_disks >= mddev->max_disks)
5148 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
5150 mddev->delta_disks = raid_disks - mddev->raid_disks;
5152 rv = mddev->pers->check_reshape(mddev);
5158 * update_array_info is used to change the configuration of an
5160 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5161 * fields in the info are checked against the array.
5162 * Any differences that cannot be handled will cause an error.
5163 * Normally, only one change can be managed at a time.
5165 static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5171 /* calculate expected state,ignoring low bits */
5172 if (mddev->bitmap && mddev->bitmap_offset)
5173 state |= (1 << MD_SB_BITMAP_PRESENT);
5175 if (mddev->major_version != info->major_version ||
5176 mddev->minor_version != info->minor_version ||
5177 /* mddev->patch_version != info->patch_version || */
5178 mddev->ctime != info->ctime ||
5179 mddev->level != info->level ||
5180 /* mddev->layout != info->layout || */
5181 !mddev->persistent != info->not_persistent||
5182 mddev->chunk_sectors != info->chunk_size >> 9 ||
5183 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5184 ((state^info->state) & 0xfffffe00)
5187 /* Check there is only one change */
5188 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5190 if (mddev->raid_disks != info->raid_disks)
5192 if (mddev->layout != info->layout)
5194 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5201 if (mddev->layout != info->layout) {
5203 * we don't need to do anything at the md level, the
5204 * personality will take care of it all.
5206 if (mddev->pers->check_reshape == NULL)
5209 mddev->new_layout = info->layout;
5210 rv = mddev->pers->check_reshape(mddev);
5212 mddev->new_layout = mddev->layout;
5216 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5217 rv = update_size(mddev, (sector_t)info->size * 2);
5219 if (mddev->raid_disks != info->raid_disks)
5220 rv = update_raid_disks(mddev, info->raid_disks);
5222 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5223 if (mddev->pers->quiesce == NULL)
5225 if (mddev->recovery || mddev->sync_thread)
5227 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5228 /* add the bitmap */
5231 if (mddev->default_bitmap_offset == 0)
5233 mddev->bitmap_offset = mddev->default_bitmap_offset;
5234 mddev->pers->quiesce(mddev, 1);
5235 rv = bitmap_create(mddev);
5237 bitmap_destroy(mddev);
5238 mddev->pers->quiesce(mddev, 0);
5240 /* remove the bitmap */
5243 if (mddev->bitmap->file)
5245 mddev->pers->quiesce(mddev, 1);
5246 bitmap_destroy(mddev);
5247 mddev->pers->quiesce(mddev, 0);
5248 mddev->bitmap_offset = 0;
5251 md_update_sb(mddev, 1);
5255 static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5259 if (mddev->pers == NULL)
5262 rdev = find_rdev(mddev, dev);
5266 md_error(mddev, rdev);
5271 * We have a problem here : there is no easy way to give a CHS
5272 * virtual geometry. We currently pretend that we have a 2 heads
5273 * 4 sectors (with a BIG number of cylinders...). This drives
5274 * dosfs just mad... ;-)
5276 static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5278 mddev_t *mddev = bdev->bd_disk->private_data;
5282 geo->cylinders = get_capacity(mddev->gendisk) / 8;
5286 static int md_ioctl(struct block_device *bdev, fmode_t mode,
5287 unsigned int cmd, unsigned long arg)
5290 void __user *argp = (void __user *)arg;
5291 mddev_t *mddev = NULL;
5293 if (!capable(CAP_SYS_ADMIN))
5297 * Commands dealing with the RAID driver but not any
5303 err = get_version(argp);
5306 case PRINT_RAID_DEBUG:
5314 autostart_arrays(arg);
5321 * Commands creating/starting a new array:
5324 mddev = bdev->bd_disk->private_data;
5331 err = mddev_lock(mddev);
5334 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5341 case SET_ARRAY_INFO:
5343 mdu_array_info_t info;
5345 memset(&info, 0, sizeof(info));
5346 else if (copy_from_user(&info, argp, sizeof(info))) {
5351 err = update_array_info(mddev, &info);
5353 printk(KERN_WARNING "md: couldn't update"
5354 " array info. %d\n", err);
5359 if (!list_empty(&mddev->disks)) {
5361 "md: array %s already has disks!\n",
5366 if (mddev->raid_disks) {
5368 "md: array %s already initialised!\n",
5373 err = set_array_info(mddev, &info);
5375 printk(KERN_WARNING "md: couldn't set"
5376 " array info. %d\n", err);
5386 * Commands querying/configuring an existing array:
5388 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
5389 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
5390 if ((!mddev->raid_disks && !mddev->external)
5391 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5392 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5393 && cmd != GET_BITMAP_FILE) {
5399 * Commands even a read-only array can execute:
5403 case GET_ARRAY_INFO:
5404 err = get_array_info(mddev, argp);
5407 case GET_BITMAP_FILE:
5408 err = get_bitmap_file(mddev, argp);
5412 err = get_disk_info(mddev, argp);
5415 case RESTART_ARRAY_RW:
5416 err = restart_array(mddev);
5420 err = do_md_stop(mddev, 0, 1);
5424 err = do_md_stop(mddev, 1, 1);
5430 * The remaining ioctls are changing the state of the
5431 * superblock, so we do not allow them on read-only arrays.
5432 * However non-MD ioctls (e.g. get-size) will still come through
5433 * here and hit the 'default' below, so only disallow
5434 * 'md' ioctls, and switch to rw mode if started auto-readonly.
5436 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
5437 if (mddev->ro == 2) {
5439 sysfs_notify_dirent(mddev->sysfs_state);
5440 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5441 md_wakeup_thread(mddev->thread);
5452 mdu_disk_info_t info;
5453 if (copy_from_user(&info, argp, sizeof(info)))
5456 err = add_new_disk(mddev, &info);
5460 case HOT_REMOVE_DISK:
5461 err = hot_remove_disk(mddev, new_decode_dev(arg));
5465 err = hot_add_disk(mddev, new_decode_dev(arg));
5468 case SET_DISK_FAULTY:
5469 err = set_disk_faulty(mddev, new_decode_dev(arg));
5473 err = do_md_run(mddev);
5476 case SET_BITMAP_FILE:
5477 err = set_bitmap_file(mddev, (int)arg);
5487 if (mddev->hold_active == UNTIL_IOCTL &&
5489 mddev->hold_active = 0;
5490 mddev_unlock(mddev);
5500 static int md_open(struct block_device *bdev, fmode_t mode)
5503 * Succeed if we can lock the mddev, which confirms that
5504 * it isn't being stopped right now.
5506 mddev_t *mddev = mddev_find(bdev->bd_dev);
5509 if (mddev->gendisk != bdev->bd_disk) {
5510 /* we are racing with mddev_put which is discarding this
5514 /* Wait until bdev->bd_disk is definitely gone */
5515 flush_scheduled_work();
5516 /* Then retry the open from the top */
5517 return -ERESTARTSYS;
5519 BUG_ON(mddev != bdev->bd_disk->private_data);
5521 if ((err = mutex_lock_interruptible_nested(&mddev->reconfig_mutex, 1)))
5525 atomic_inc(&mddev->openers);
5526 mddev_unlock(mddev);
5528 check_disk_change(bdev);
5533 static int md_release(struct gendisk *disk, fmode_t mode)
5535 mddev_t *mddev = disk->private_data;
5538 atomic_dec(&mddev->openers);
5544 static int md_media_changed(struct gendisk *disk)
5546 mddev_t *mddev = disk->private_data;
5548 return mddev->changed;
5551 static int md_revalidate(struct gendisk *disk)
5553 mddev_t *mddev = disk->private_data;
5558 static struct block_device_operations md_fops =
5560 .owner = THIS_MODULE,
5562 .release = md_release,
5564 .getgeo = md_getgeo,
5565 .media_changed = md_media_changed,
5566 .revalidate_disk= md_revalidate,
5569 static int md_thread(void * arg)
5571 mdk_thread_t *thread = arg;
5574 * md_thread is a 'system-thread', it's priority should be very
5575 * high. We avoid resource deadlocks individually in each
5576 * raid personality. (RAID5 does preallocation) We also use RR and
5577 * the very same RT priority as kswapd, thus we will never get
5578 * into a priority inversion deadlock.
5580 * we definitely have to have equal or higher priority than
5581 * bdflush, otherwise bdflush will deadlock if there are too
5582 * many dirty RAID5 blocks.
5585 allow_signal(SIGKILL);
5586 while (!kthread_should_stop()) {
5588 /* We need to wait INTERRUPTIBLE so that
5589 * we don't add to the load-average.
5590 * That means we need to be sure no signals are
5593 if (signal_pending(current))
5594 flush_signals(current);
5596 wait_event_interruptible_timeout
5598 test_bit(THREAD_WAKEUP, &thread->flags)
5599 || kthread_should_stop(),
5602 clear_bit(THREAD_WAKEUP, &thread->flags);
5604 thread->run(thread->mddev);
5610 void md_wakeup_thread(mdk_thread_t *thread)
5613 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5614 set_bit(THREAD_WAKEUP, &thread->flags);
5615 wake_up(&thread->wqueue);
5619 mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5622 mdk_thread_t *thread;
5624 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
5628 init_waitqueue_head(&thread->wqueue);
5631 thread->mddev = mddev;
5632 thread->timeout = MAX_SCHEDULE_TIMEOUT;
5633 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
5634 if (IS_ERR(thread->tsk)) {
5641 void md_unregister_thread(mdk_thread_t *thread)
5645 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
5647 kthread_stop(thread->tsk);
5651 void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
5658 if (!rdev || test_bit(Faulty, &rdev->flags))
5661 if (mddev->external)
5662 set_bit(Blocked, &rdev->flags);
5664 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5666 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5667 __builtin_return_address(0),__builtin_return_address(1),
5668 __builtin_return_address(2),__builtin_return_address(3));
5672 if (!mddev->pers->error_handler)
5674 mddev->pers->error_handler(mddev,rdev);
5675 if (mddev->degraded)
5676 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
5677 set_bit(StateChanged, &rdev->flags);
5678 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5679 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5680 md_wakeup_thread(mddev->thread);
5681 md_new_event_inintr(mddev);
5684 /* seq_file implementation /proc/mdstat */
5686 static void status_unused(struct seq_file *seq)
5691 seq_printf(seq, "unused devices: ");
5693 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
5694 char b[BDEVNAME_SIZE];
5696 seq_printf(seq, "%s ",
5697 bdevname(rdev->bdev,b));
5700 seq_printf(seq, "<none>");
5702 seq_printf(seq, "\n");
5706 static void status_resync(struct seq_file *seq, mddev_t * mddev)
5708 sector_t max_sectors, resync, res;
5709 unsigned long dt, db;
5712 unsigned int per_milli;
5714 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
5716 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
5717 max_sectors = mddev->resync_max_sectors;
5719 max_sectors = mddev->dev_sectors;
5722 * Should not happen.
5728 /* Pick 'scale' such that (resync>>scale)*1000 will fit
5729 * in a sector_t, and (max_sectors>>scale) will fit in a
5730 * u32, as those are the requirements for sector_div.
5731 * Thus 'scale' must be at least 10
5734 if (sizeof(sector_t) > sizeof(unsigned long)) {
5735 while ( max_sectors/2 > (1ULL<<(scale+32)))
5738 res = (resync>>scale)*1000;
5739 sector_div(res, (u32)((max_sectors>>scale)+1));
5743 int i, x = per_milli/50, y = 20-x;
5744 seq_printf(seq, "[");
5745 for (i = 0; i < x; i++)
5746 seq_printf(seq, "=");
5747 seq_printf(seq, ">");
5748 for (i = 0; i < y; i++)
5749 seq_printf(seq, ".");
5750 seq_printf(seq, "] ");
5752 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
5753 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
5755 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
5757 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
5758 "resync" : "recovery"))),
5759 per_milli/10, per_milli % 10,
5760 (unsigned long long) resync/2,
5761 (unsigned long long) max_sectors/2);
5764 * dt: time from mark until now
5765 * db: blocks written from mark until now
5766 * rt: remaining time
5768 * rt is a sector_t, so could be 32bit or 64bit.
5769 * So we divide before multiply in case it is 32bit and close
5771 * We scale the divisor (db) by 32 to avoid loosing precision
5772 * near the end of resync when the number of remaining sectors
5774 * We then divide rt by 32 after multiplying by db to compensate.
5775 * The '+1' avoids division by zero if db is very small.
5777 dt = ((jiffies - mddev->resync_mark) / HZ);
5779 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
5780 - mddev->resync_mark_cnt;
5782 rt = max_sectors - resync; /* number of remaining sectors */
5783 sector_div(rt, db/32+1);
5787 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
5788 ((unsigned long)rt % 60)/6);
5790 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
5793 static void *md_seq_start(struct seq_file *seq, loff_t *pos)
5795 struct list_head *tmp;
5805 spin_lock(&all_mddevs_lock);
5806 list_for_each(tmp,&all_mddevs)
5808 mddev = list_entry(tmp, mddev_t, all_mddevs);
5810 spin_unlock(&all_mddevs_lock);
5813 spin_unlock(&all_mddevs_lock);
5815 return (void*)2;/* tail */
5819 static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
5821 struct list_head *tmp;
5822 mddev_t *next_mddev, *mddev = v;
5828 spin_lock(&all_mddevs_lock);
5830 tmp = all_mddevs.next;
5832 tmp = mddev->all_mddevs.next;
5833 if (tmp != &all_mddevs)
5834 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
5836 next_mddev = (void*)2;
5839 spin_unlock(&all_mddevs_lock);
5847 static void md_seq_stop(struct seq_file *seq, void *v)
5851 if (mddev && v != (void*)1 && v != (void*)2)
5855 struct mdstat_info {
5859 static int md_seq_show(struct seq_file *seq, void *v)
5864 struct mdstat_info *mi = seq->private;
5865 struct bitmap *bitmap;
5867 if (v == (void*)1) {
5868 struct mdk_personality *pers;
5869 seq_printf(seq, "Personalities : ");
5870 spin_lock(&pers_lock);
5871 list_for_each_entry(pers, &pers_list, list)
5872 seq_printf(seq, "[%s] ", pers->name);
5874 spin_unlock(&pers_lock);
5875 seq_printf(seq, "\n");
5876 mi->event = atomic_read(&md_event_count);
5879 if (v == (void*)2) {
5884 if (mddev_lock(mddev) < 0)
5887 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
5888 seq_printf(seq, "%s : %sactive", mdname(mddev),
5889 mddev->pers ? "" : "in");
5892 seq_printf(seq, " (read-only)");
5894 seq_printf(seq, " (auto-read-only)");
5895 seq_printf(seq, " %s", mddev->pers->name);
5899 list_for_each_entry(rdev, &mddev->disks, same_set) {
5900 char b[BDEVNAME_SIZE];
5901 seq_printf(seq, " %s[%d]",
5902 bdevname(rdev->bdev,b), rdev->desc_nr);
5903 if (test_bit(WriteMostly, &rdev->flags))
5904 seq_printf(seq, "(W)");
5905 if (test_bit(Faulty, &rdev->flags)) {
5906 seq_printf(seq, "(F)");
5908 } else if (rdev->raid_disk < 0)
5909 seq_printf(seq, "(S)"); /* spare */
5910 sectors += rdev->sectors;
5913 if (!list_empty(&mddev->disks)) {
5915 seq_printf(seq, "\n %llu blocks",
5916 (unsigned long long)
5917 mddev->array_sectors / 2);
5919 seq_printf(seq, "\n %llu blocks",
5920 (unsigned long long)sectors / 2);
5922 if (mddev->persistent) {
5923 if (mddev->major_version != 0 ||
5924 mddev->minor_version != 90) {
5925 seq_printf(seq," super %d.%d",
5926 mddev->major_version,
5927 mddev->minor_version);
5929 } else if (mddev->external)
5930 seq_printf(seq, " super external:%s",
5931 mddev->metadata_type);
5933 seq_printf(seq, " super non-persistent");
5936 mddev->pers->status(seq, mddev);
5937 seq_printf(seq, "\n ");
5938 if (mddev->pers->sync_request) {
5939 if (mddev->curr_resync > 2) {
5940 status_resync(seq, mddev);
5941 seq_printf(seq, "\n ");
5942 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
5943 seq_printf(seq, "\tresync=DELAYED\n ");
5944 else if (mddev->recovery_cp < MaxSector)
5945 seq_printf(seq, "\tresync=PENDING\n ");
5948 seq_printf(seq, "\n ");
5950 if ((bitmap = mddev->bitmap)) {
5951 unsigned long chunk_kb;
5952 unsigned long flags;
5953 spin_lock_irqsave(&bitmap->lock, flags);
5954 chunk_kb = bitmap->chunksize >> 10;
5955 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
5957 bitmap->pages - bitmap->missing_pages,
5959 (bitmap->pages - bitmap->missing_pages)
5960 << (PAGE_SHIFT - 10),
5961 chunk_kb ? chunk_kb : bitmap->chunksize,
5962 chunk_kb ? "KB" : "B");
5964 seq_printf(seq, ", file: ");
5965 seq_path(seq, &bitmap->file->f_path, " \t\n");
5968 seq_printf(seq, "\n");
5969 spin_unlock_irqrestore(&bitmap->lock, flags);
5972 seq_printf(seq, "\n");
5974 mddev_unlock(mddev);
5979 static const struct seq_operations md_seq_ops = {
5980 .start = md_seq_start,
5981 .next = md_seq_next,
5982 .stop = md_seq_stop,
5983 .show = md_seq_show,
5986 static int md_seq_open(struct inode *inode, struct file *file)
5989 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
5993 error = seq_open(file, &md_seq_ops);
5997 struct seq_file *p = file->private_data;
5999 mi->event = atomic_read(&md_event_count);
6004 static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6006 struct seq_file *m = filp->private_data;
6007 struct mdstat_info *mi = m->private;
6010 poll_wait(filp, &md_event_waiters, wait);
6012 /* always allow read */
6013 mask = POLLIN | POLLRDNORM;
6015 if (mi->event != atomic_read(&md_event_count))
6016 mask |= POLLERR | POLLPRI;
6020 static const struct file_operations md_seq_fops = {
6021 .owner = THIS_MODULE,
6022 .open = md_seq_open,
6024 .llseek = seq_lseek,
6025 .release = seq_release_private,
6026 .poll = mdstat_poll,
6029 int register_md_personality(struct mdk_personality *p)
6031 spin_lock(&pers_lock);
6032 list_add_tail(&p->list, &pers_list);
6033 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
6034 spin_unlock(&pers_lock);
6038 int unregister_md_personality(struct mdk_personality *p)
6040 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
6041 spin_lock(&pers_lock);
6042 list_del_init(&p->list);
6043 spin_unlock(&pers_lock);
6047 static int is_mddev_idle(mddev_t *mddev, int init)
6055 rdev_for_each_rcu(rdev, mddev) {
6056 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
6057 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6058 (int)part_stat_read(&disk->part0, sectors[1]) -
6059 atomic_read(&disk->sync_io);
6060 /* sync IO will cause sync_io to increase before the disk_stats
6061 * as sync_io is counted when a request starts, and
6062 * disk_stats is counted when it completes.
6063 * So resync activity will cause curr_events to be smaller than
6064 * when there was no such activity.
6065 * non-sync IO will cause disk_stat to increase without
6066 * increasing sync_io so curr_events will (eventually)
6067 * be larger than it was before. Once it becomes
6068 * substantially larger, the test below will cause
6069 * the array to appear non-idle, and resync will slow
6071 * If there is a lot of outstanding resync activity when
6072 * we set last_event to curr_events, then all that activity
6073 * completing might cause the array to appear non-idle
6074 * and resync will be slowed down even though there might
6075 * not have been non-resync activity. This will only
6076 * happen once though. 'last_events' will soon reflect
6077 * the state where there is little or no outstanding
6078 * resync requests, and further resync activity will
6079 * always make curr_events less than last_events.
6082 if (init || curr_events - rdev->last_events > 64) {
6083 rdev->last_events = curr_events;
6091 void md_done_sync(mddev_t *mddev, int blocks, int ok)
6093 /* another "blocks" (512byte) blocks have been synced */
6094 atomic_sub(blocks, &mddev->recovery_active);
6095 wake_up(&mddev->recovery_wait);
6097 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6098 md_wakeup_thread(mddev->thread);
6099 // stop recovery, signal do_sync ....
6104 /* md_write_start(mddev, bi)
6105 * If we need to update some array metadata (e.g. 'active' flag
6106 * in superblock) before writing, schedule a superblock update
6107 * and wait for it to complete.
6109 void md_write_start(mddev_t *mddev, struct bio *bi)
6112 if (bio_data_dir(bi) != WRITE)
6115 BUG_ON(mddev->ro == 1);
6116 if (mddev->ro == 2) {
6117 /* need to switch to read/write */
6119 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6120 md_wakeup_thread(mddev->thread);
6121 md_wakeup_thread(mddev->sync_thread);
6124 atomic_inc(&mddev->writes_pending);
6125 if (mddev->safemode == 1)
6126 mddev->safemode = 0;
6127 if (mddev->in_sync) {
6128 spin_lock_irq(&mddev->write_lock);
6129 if (mddev->in_sync) {
6131 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6132 md_wakeup_thread(mddev->thread);
6135 spin_unlock_irq(&mddev->write_lock);
6138 sysfs_notify_dirent(mddev->sysfs_state);
6139 wait_event(mddev->sb_wait,
6140 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
6141 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
6144 void md_write_end(mddev_t *mddev)
6146 if (atomic_dec_and_test(&mddev->writes_pending)) {
6147 if (mddev->safemode == 2)
6148 md_wakeup_thread(mddev->thread);
6149 else if (mddev->safemode_delay)
6150 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6154 /* md_allow_write(mddev)
6155 * Calling this ensures that the array is marked 'active' so that writes
6156 * may proceed without blocking. It is important to call this before
6157 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6158 * Must be called with mddev_lock held.
6160 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6161 * is dropped, so return -EAGAIN after notifying userspace.
6163 int md_allow_write(mddev_t *mddev)
6169 if (!mddev->pers->sync_request)
6172 spin_lock_irq(&mddev->write_lock);
6173 if (mddev->in_sync) {
6175 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6176 if (mddev->safemode_delay &&
6177 mddev->safemode == 0)
6178 mddev->safemode = 1;
6179 spin_unlock_irq(&mddev->write_lock);
6180 md_update_sb(mddev, 0);
6181 sysfs_notify_dirent(mddev->sysfs_state);
6183 spin_unlock_irq(&mddev->write_lock);
6185 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
6190 EXPORT_SYMBOL_GPL(md_allow_write);
6192 #define SYNC_MARKS 10
6193 #define SYNC_MARK_STEP (3*HZ)
6194 void md_do_sync(mddev_t *mddev)
6197 unsigned int currspeed = 0,
6199 sector_t max_sectors,j, io_sectors;
6200 unsigned long mark[SYNC_MARKS];
6201 sector_t mark_cnt[SYNC_MARKS];
6203 struct list_head *tmp;
6204 sector_t last_check;
6209 /* just incase thread restarts... */
6210 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6212 if (mddev->ro) /* never try to sync a read-only array */
6215 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6216 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6217 desc = "data-check";
6218 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6219 desc = "requested-resync";
6222 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6227 /* we overload curr_resync somewhat here.
6228 * 0 == not engaged in resync at all
6229 * 2 == checking that there is no conflict with another sync
6230 * 1 == like 2, but have yielded to allow conflicting resync to
6232 * other == active in resync - this many blocks
6234 * Before starting a resync we must have set curr_resync to
6235 * 2, and then checked that every "conflicting" array has curr_resync
6236 * less than ours. When we find one that is the same or higher
6237 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6238 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6239 * This will mean we have to start checking from the beginning again.
6244 mddev->curr_resync = 2;
6247 if (kthread_should_stop()) {
6248 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6251 for_each_mddev(mddev2, tmp) {
6252 if (mddev2 == mddev)
6254 if (!mddev->parallel_resync
6255 && mddev2->curr_resync
6256 && match_mddev_units(mddev, mddev2)) {
6258 if (mddev < mddev2 && mddev->curr_resync == 2) {
6259 /* arbitrarily yield */
6260 mddev->curr_resync = 1;
6261 wake_up(&resync_wait);
6263 if (mddev > mddev2 && mddev->curr_resync == 1)
6264 /* no need to wait here, we can wait the next
6265 * time 'round when curr_resync == 2
6268 /* We need to wait 'interruptible' so as not to
6269 * contribute to the load average, and not to
6270 * be caught by 'softlockup'
6272 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
6273 if (!kthread_should_stop() &&
6274 mddev2->curr_resync >= mddev->curr_resync) {
6275 printk(KERN_INFO "md: delaying %s of %s"
6276 " until %s has finished (they"
6277 " share one or more physical units)\n",
6278 desc, mdname(mddev), mdname(mddev2));
6280 if (signal_pending(current))
6281 flush_signals(current);
6283 finish_wait(&resync_wait, &wq);
6286 finish_wait(&resync_wait, &wq);
6289 } while (mddev->curr_resync < 2);
6292 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6293 /* resync follows the size requested by the personality,
6294 * which defaults to physical size, but can be virtual size
6296 max_sectors = mddev->resync_max_sectors;
6297 mddev->resync_mismatches = 0;
6298 /* we don't use the checkpoint if there's a bitmap */
6299 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6300 j = mddev->resync_min;
6301 else if (!mddev->bitmap)
6302 j = mddev->recovery_cp;
6304 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6305 max_sectors = mddev->dev_sectors;
6307 /* recovery follows the physical size of devices */
6308 max_sectors = mddev->dev_sectors;
6310 list_for_each_entry(rdev, &mddev->disks, same_set)
6311 if (rdev->raid_disk >= 0 &&
6312 !test_bit(Faulty, &rdev->flags) &&
6313 !test_bit(In_sync, &rdev->flags) &&
6314 rdev->recovery_offset < j)
6315 j = rdev->recovery_offset;
6318 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6319 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6320 " %d KB/sec/disk.\n", speed_min(mddev));
6321 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
6322 "(but not more than %d KB/sec) for %s.\n",
6323 speed_max(mddev), desc);
6325 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
6328 for (m = 0; m < SYNC_MARKS; m++) {
6330 mark_cnt[m] = io_sectors;
6333 mddev->resync_mark = mark[last_mark];
6334 mddev->resync_mark_cnt = mark_cnt[last_mark];
6337 * Tune reconstruction:
6339 window = 32*(PAGE_SIZE/512);
6340 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6341 window/2,(unsigned long long) max_sectors/2);
6343 atomic_set(&mddev->recovery_active, 0);
6348 "md: resuming %s of %s from checkpoint.\n",
6349 desc, mdname(mddev));
6350 mddev->curr_resync = j;
6353 while (j < max_sectors) {
6358 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6359 ((mddev->curr_resync > mddev->curr_resync_completed &&
6360 (mddev->curr_resync - mddev->curr_resync_completed)
6361 > (max_sectors >> 4)) ||
6362 (j - mddev->curr_resync_completed)*2
6363 >= mddev->resync_max - mddev->curr_resync_completed
6365 /* time to update curr_resync_completed */
6366 blk_unplug(mddev->queue);
6367 wait_event(mddev->recovery_wait,
6368 atomic_read(&mddev->recovery_active) == 0);
6369 mddev->curr_resync_completed =
6371 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6372 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
6375 while (j >= mddev->resync_max && !kthread_should_stop()) {
6376 /* As this condition is controlled by user-space,
6377 * we can block indefinitely, so use '_interruptible'
6378 * to avoid triggering warnings.
6380 flush_signals(current); /* just in case */
6381 wait_event_interruptible(mddev->recovery_wait,
6382 mddev->resync_max > j
6383 || kthread_should_stop());
6386 if (kthread_should_stop())
6389 sectors = mddev->pers->sync_request(mddev, j, &skipped,
6390 currspeed < speed_min(mddev));
6392 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6396 if (!skipped) { /* actual IO requested */
6397 io_sectors += sectors;
6398 atomic_add(sectors, &mddev->recovery_active);
6402 if (j>1) mddev->curr_resync = j;
6403 mddev->curr_mark_cnt = io_sectors;
6404 if (last_check == 0)
6405 /* this is the earliers that rebuilt will be
6406 * visible in /proc/mdstat
6408 md_new_event(mddev);
6410 if (last_check + window > io_sectors || j == max_sectors)
6413 last_check = io_sectors;
6415 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6419 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6421 int next = (last_mark+1) % SYNC_MARKS;
6423 mddev->resync_mark = mark[next];
6424 mddev->resync_mark_cnt = mark_cnt[next];
6425 mark[next] = jiffies;
6426 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
6431 if (kthread_should_stop())
6436 * this loop exits only if either when we are slower than
6437 * the 'hard' speed limit, or the system was IO-idle for
6439 * the system might be non-idle CPU-wise, but we only care
6440 * about not overloading the IO subsystem. (things like an
6441 * e2fsck being done on the RAID array should execute fast)
6443 blk_unplug(mddev->queue);
6446 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6447 /((jiffies-mddev->resync_mark)/HZ +1) +1;
6449 if (currspeed > speed_min(mddev)) {
6450 if ((currspeed > speed_max(mddev)) ||
6451 !is_mddev_idle(mddev, 0)) {
6457 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
6459 * this also signals 'finished resyncing' to md_stop
6462 blk_unplug(mddev->queue);
6464 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6466 /* tell personality that we are finished */
6467 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
6469 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
6470 mddev->curr_resync > 2) {
6471 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6472 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6473 if (mddev->curr_resync >= mddev->recovery_cp) {
6475 "md: checkpointing %s of %s.\n",
6476 desc, mdname(mddev));
6477 mddev->recovery_cp = mddev->curr_resync;
6480 mddev->recovery_cp = MaxSector;
6482 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6483 mddev->curr_resync = MaxSector;
6484 list_for_each_entry(rdev, &mddev->disks, same_set)
6485 if (rdev->raid_disk >= 0 &&
6486 !test_bit(Faulty, &rdev->flags) &&
6487 !test_bit(In_sync, &rdev->flags) &&
6488 rdev->recovery_offset < mddev->curr_resync)
6489 rdev->recovery_offset = mddev->curr_resync;
6492 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6495 mddev->curr_resync = 0;
6496 mddev->curr_resync_completed = 0;
6497 mddev->resync_min = 0;
6498 mddev->resync_max = MaxSector;
6499 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
6500 wake_up(&resync_wait);
6501 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6502 md_wakeup_thread(mddev->thread);
6507 * got a signal, exit.
6510 "md: md_do_sync() got signal ... exiting\n");
6511 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6515 EXPORT_SYMBOL_GPL(md_do_sync);
6518 static int remove_and_add_spares(mddev_t *mddev)
6523 mddev->curr_resync_completed = 0;
6525 list_for_each_entry(rdev, &mddev->disks, same_set)
6526 if (rdev->raid_disk >= 0 &&
6527 !test_bit(Blocked, &rdev->flags) &&
6528 (test_bit(Faulty, &rdev->flags) ||
6529 ! test_bit(In_sync, &rdev->flags)) &&
6530 atomic_read(&rdev->nr_pending)==0) {
6531 if (mddev->pers->hot_remove_disk(
6532 mddev, rdev->raid_disk)==0) {
6534 sprintf(nm,"rd%d", rdev->raid_disk);
6535 sysfs_remove_link(&mddev->kobj, nm);
6536 rdev->raid_disk = -1;
6540 if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) {
6541 list_for_each_entry(rdev, &mddev->disks, same_set) {
6542 if (rdev->raid_disk >= 0 &&
6543 !test_bit(In_sync, &rdev->flags) &&
6544 !test_bit(Blocked, &rdev->flags))
6546 if (rdev->raid_disk < 0
6547 && !test_bit(Faulty, &rdev->flags)) {
6548 rdev->recovery_offset = 0;
6550 hot_add_disk(mddev, rdev) == 0) {
6552 sprintf(nm, "rd%d", rdev->raid_disk);
6553 if (sysfs_create_link(&mddev->kobj,
6556 "md: cannot register "
6560 md_new_event(mddev);
6569 * This routine is regularly called by all per-raid-array threads to
6570 * deal with generic issues like resync and super-block update.
6571 * Raid personalities that don't have a thread (linear/raid0) do not
6572 * need this as they never do any recovery or update the superblock.
6574 * It does not do any resync itself, but rather "forks" off other threads
6575 * to do that as needed.
6576 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6577 * "->recovery" and create a thread at ->sync_thread.
6578 * When the thread finishes it sets MD_RECOVERY_DONE
6579 * and wakeups up this thread which will reap the thread and finish up.
6580 * This thread also removes any faulty devices (with nr_pending == 0).
6582 * The overall approach is:
6583 * 1/ if the superblock needs updating, update it.
6584 * 2/ If a recovery thread is running, don't do anything else.
6585 * 3/ If recovery has finished, clean up, possibly marking spares active.
6586 * 4/ If there are any faulty devices, remove them.
6587 * 5/ If array is degraded, try to add spares devices
6588 * 6/ If array has spares or is not in-sync, start a resync thread.
6590 void md_check_recovery(mddev_t *mddev)
6596 bitmap_daemon_work(mddev->bitmap);
6601 if (signal_pending(current)) {
6602 if (mddev->pers->sync_request && !mddev->external) {
6603 printk(KERN_INFO "md: %s in immediate safe mode\n",
6605 mddev->safemode = 2;
6607 flush_signals(current);
6610 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
6613 (mddev->flags && !mddev->external) ||
6614 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
6615 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
6616 (mddev->external == 0 && mddev->safemode == 1) ||
6617 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6618 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
6622 if (mddev_trylock(mddev)) {
6626 /* Only thing we do on a ro array is remove
6629 remove_and_add_spares(mddev);
6630 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6634 if (!mddev->external) {
6636 spin_lock_irq(&mddev->write_lock);
6637 if (mddev->safemode &&
6638 !atomic_read(&mddev->writes_pending) &&
6640 mddev->recovery_cp == MaxSector) {
6643 if (mddev->persistent)
6644 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6646 if (mddev->safemode == 1)
6647 mddev->safemode = 0;
6648 spin_unlock_irq(&mddev->write_lock);
6650 sysfs_notify_dirent(mddev->sysfs_state);
6654 md_update_sb(mddev, 0);
6656 list_for_each_entry(rdev, &mddev->disks, same_set)
6657 if (test_and_clear_bit(StateChanged, &rdev->flags))
6658 sysfs_notify_dirent(rdev->sysfs_state);
6661 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
6662 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
6663 /* resync/recovery still happening */
6664 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6667 if (mddev->sync_thread) {
6668 /* resync has finished, collect result */
6669 md_unregister_thread(mddev->sync_thread);
6670 mddev->sync_thread = NULL;
6671 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
6672 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
6674 /* activate any spares */
6675 if (mddev->pers->spare_active(mddev))
6676 sysfs_notify(&mddev->kobj, NULL,
6679 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6680 mddev->pers->finish_reshape)
6681 mddev->pers->finish_reshape(mddev);
6682 md_update_sb(mddev, 1);
6684 /* if array is no-longer degraded, then any saved_raid_disk
6685 * information must be scrapped
6687 if (!mddev->degraded)
6688 list_for_each_entry(rdev, &mddev->disks, same_set)
6689 rdev->saved_raid_disk = -1;
6691 mddev->recovery = 0;
6692 /* flag recovery needed just to double check */
6693 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6694 sysfs_notify_dirent(mddev->sysfs_action);
6695 md_new_event(mddev);
6698 /* Set RUNNING before clearing NEEDED to avoid
6699 * any transients in the value of "sync_action".
6701 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6702 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6703 /* Clear some bits that don't mean anything, but
6706 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
6707 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
6709 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
6711 /* no recovery is running.
6712 * remove any failed drives, then
6713 * add spares if possible.
6714 * Spare are also removed and re-added, to allow
6715 * the personality to fail the re-add.
6718 if (mddev->reshape_position != MaxSector) {
6719 if (mddev->pers->check_reshape == NULL ||
6720 mddev->pers->check_reshape(mddev) != 0)
6721 /* Cannot proceed */
6723 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
6724 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
6725 } else if ((spares = remove_and_add_spares(mddev))) {
6726 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6727 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
6728 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
6729 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
6730 } else if (mddev->recovery_cp < MaxSector) {
6731 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6732 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
6733 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
6734 /* nothing to be done ... */
6737 if (mddev->pers->sync_request) {
6738 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
6739 /* We are adding a device or devices to an array
6740 * which has the bitmap stored on all devices.
6741 * So make sure all bitmap pages get written
6743 bitmap_write_all(mddev->bitmap);
6745 mddev->sync_thread = md_register_thread(md_do_sync,
6748 if (!mddev->sync_thread) {
6749 printk(KERN_ERR "%s: could not start resync"
6752 /* leave the spares where they are, it shouldn't hurt */
6753 mddev->recovery = 0;
6755 md_wakeup_thread(mddev->sync_thread);
6756 sysfs_notify_dirent(mddev->sysfs_action);
6757 md_new_event(mddev);
6760 if (!mddev->sync_thread) {
6761 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6762 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
6764 if (mddev->sysfs_action)
6765 sysfs_notify_dirent(mddev->sysfs_action);
6767 mddev_unlock(mddev);
6771 void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
6773 sysfs_notify_dirent(rdev->sysfs_state);
6774 wait_event_timeout(rdev->blocked_wait,
6775 !test_bit(Blocked, &rdev->flags),
6776 msecs_to_jiffies(5000));
6777 rdev_dec_pending(rdev, mddev);
6779 EXPORT_SYMBOL(md_wait_for_blocked_rdev);
6781 static int md_notify_reboot(struct notifier_block *this,
6782 unsigned long code, void *x)
6784 struct list_head *tmp;
6787 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
6789 printk(KERN_INFO "md: stopping all md devices.\n");
6791 for_each_mddev(mddev, tmp)
6792 if (mddev_trylock(mddev)) {
6793 /* Force a switch to readonly even array
6794 * appears to still be in use. Hence
6797 do_md_stop(mddev, 1, 100);
6798 mddev_unlock(mddev);
6801 * certain more exotic SCSI devices are known to be
6802 * volatile wrt too early system reboots. While the
6803 * right place to handle this issue is the given
6804 * driver, we do want to have a safe RAID driver ...
6811 static struct notifier_block md_notifier = {
6812 .notifier_call = md_notify_reboot,
6814 .priority = INT_MAX, /* before any real devices */
6817 static void md_geninit(void)
6819 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
6821 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
6824 static int __init md_init(void)
6826 if (register_blkdev(MD_MAJOR, "md"))
6828 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
6829 unregister_blkdev(MD_MAJOR, "md");
6832 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
6833 md_probe, NULL, NULL);
6834 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
6835 md_probe, NULL, NULL);
6837 register_reboot_notifier(&md_notifier);
6838 raid_table_header = register_sysctl_table(raid_root_table);
6848 * Searches all registered partitions for autorun RAID arrays
6852 static LIST_HEAD(all_detected_devices);
6853 struct detected_devices_node {
6854 struct list_head list;
6858 void md_autodetect_dev(dev_t dev)
6860 struct detected_devices_node *node_detected_dev;
6862 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
6863 if (node_detected_dev) {
6864 node_detected_dev->dev = dev;
6865 list_add_tail(&node_detected_dev->list, &all_detected_devices);
6867 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
6868 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
6873 static void autostart_arrays(int part)
6876 struct detected_devices_node *node_detected_dev;
6878 int i_scanned, i_passed;
6883 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
6885 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
6887 node_detected_dev = list_entry(all_detected_devices.next,
6888 struct detected_devices_node, list);
6889 list_del(&node_detected_dev->list);
6890 dev = node_detected_dev->dev;
6891 kfree(node_detected_dev);
6892 rdev = md_import_device(dev,0, 90);
6896 if (test_bit(Faulty, &rdev->flags)) {
6900 set_bit(AutoDetected, &rdev->flags);
6901 list_add(&rdev->same_set, &pending_raid_disks);
6905 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
6906 i_scanned, i_passed);
6908 autorun_devices(part);
6911 #endif /* !MODULE */
6913 static __exit void md_exit(void)
6916 struct list_head *tmp;
6918 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
6919 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
6921 unregister_blkdev(MD_MAJOR,"md");
6922 unregister_blkdev(mdp_major, "mdp");
6923 unregister_reboot_notifier(&md_notifier);
6924 unregister_sysctl_table(raid_table_header);
6925 remove_proc_entry("mdstat", NULL);
6926 for_each_mddev(mddev, tmp) {
6927 export_array(mddev);
6928 mddev->hold_active = 0;
6932 subsys_initcall(md_init);
6933 module_exit(md_exit)
6935 static int get_ro(char *buffer, struct kernel_param *kp)
6937 return sprintf(buffer, "%d", start_readonly);
6939 static int set_ro(const char *val, struct kernel_param *kp)
6942 int num = simple_strtoul(val, &e, 10);
6943 if (*val && (*e == '\0' || *e == '\n')) {
6944 start_readonly = num;
6950 module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
6951 module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
6953 module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
6955 EXPORT_SYMBOL(register_md_personality);
6956 EXPORT_SYMBOL(unregister_md_personality);
6957 EXPORT_SYMBOL(md_error);
6958 EXPORT_SYMBOL(md_done_sync);
6959 EXPORT_SYMBOL(md_write_start);
6960 EXPORT_SYMBOL(md_write_end);
6961 EXPORT_SYMBOL(md_register_thread);
6962 EXPORT_SYMBOL(md_unregister_thread);
6963 EXPORT_SYMBOL(md_wakeup_thread);
6964 EXPORT_SYMBOL(md_check_recovery);
6965 MODULE_LICENSE("GPL");
6967 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);