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 sector_t calc_num_sectors(mdk_rdev_t *rdev, unsigned chunk_size)
445 sector_t num_sectors = rdev->sb_start;
448 num_sectors &= ~((sector_t)chunk_size/512 - 1);
452 static int alloc_disk_sb(mdk_rdev_t * rdev)
457 rdev->sb_page = alloc_page(GFP_KERNEL);
458 if (!rdev->sb_page) {
459 printk(KERN_ALERT "md: out of memory.\n");
466 static void free_disk_sb(mdk_rdev_t * rdev)
469 put_page(rdev->sb_page);
471 rdev->sb_page = NULL;
478 static void super_written(struct bio *bio, int error)
480 mdk_rdev_t *rdev = bio->bi_private;
481 mddev_t *mddev = rdev->mddev;
483 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
484 printk("md: super_written gets error=%d, uptodate=%d\n",
485 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
486 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
487 md_error(mddev, rdev);
490 if (atomic_dec_and_test(&mddev->pending_writes))
491 wake_up(&mddev->sb_wait);
495 static void super_written_barrier(struct bio *bio, int error)
497 struct bio *bio2 = bio->bi_private;
498 mdk_rdev_t *rdev = bio2->bi_private;
499 mddev_t *mddev = rdev->mddev;
501 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
502 error == -EOPNOTSUPP) {
504 /* barriers don't appear to be supported :-( */
505 set_bit(BarriersNotsupp, &rdev->flags);
506 mddev->barriers_work = 0;
507 spin_lock_irqsave(&mddev->write_lock, flags);
508 bio2->bi_next = mddev->biolist;
509 mddev->biolist = bio2;
510 spin_unlock_irqrestore(&mddev->write_lock, flags);
511 wake_up(&mddev->sb_wait);
515 bio->bi_private = rdev;
516 super_written(bio, error);
520 void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
521 sector_t sector, int size, struct page *page)
523 /* write first size bytes of page to sector of rdev
524 * Increment mddev->pending_writes before returning
525 * and decrement it on completion, waking up sb_wait
526 * if zero is reached.
527 * If an error occurred, call md_error
529 * As we might need to resubmit the request if BIO_RW_BARRIER
530 * causes ENOTSUPP, we allocate a spare bio...
532 struct bio *bio = bio_alloc(GFP_NOIO, 1);
533 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNCIO) | (1<<BIO_RW_UNPLUG);
535 bio->bi_bdev = rdev->bdev;
536 bio->bi_sector = sector;
537 bio_add_page(bio, page, size, 0);
538 bio->bi_private = rdev;
539 bio->bi_end_io = super_written;
542 atomic_inc(&mddev->pending_writes);
543 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
545 rw |= (1<<BIO_RW_BARRIER);
546 rbio = bio_clone(bio, GFP_NOIO);
547 rbio->bi_private = bio;
548 rbio->bi_end_io = super_written_barrier;
549 submit_bio(rw, rbio);
554 void md_super_wait(mddev_t *mddev)
556 /* wait for all superblock writes that were scheduled to complete.
557 * if any had to be retried (due to BARRIER problems), retry them
561 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
562 if (atomic_read(&mddev->pending_writes)==0)
564 while (mddev->biolist) {
566 spin_lock_irq(&mddev->write_lock);
567 bio = mddev->biolist;
568 mddev->biolist = bio->bi_next ;
570 spin_unlock_irq(&mddev->write_lock);
571 submit_bio(bio->bi_rw, bio);
575 finish_wait(&mddev->sb_wait, &wq);
578 static void bi_complete(struct bio *bio, int error)
580 complete((struct completion*)bio->bi_private);
583 int sync_page_io(struct block_device *bdev, sector_t sector, int size,
584 struct page *page, int rw)
586 struct bio *bio = bio_alloc(GFP_NOIO, 1);
587 struct completion event;
590 rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG);
593 bio->bi_sector = sector;
594 bio_add_page(bio, page, size, 0);
595 init_completion(&event);
596 bio->bi_private = &event;
597 bio->bi_end_io = bi_complete;
599 wait_for_completion(&event);
601 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
605 EXPORT_SYMBOL_GPL(sync_page_io);
607 static int read_disk_sb(mdk_rdev_t * rdev, int size)
609 char b[BDEVNAME_SIZE];
610 if (!rdev->sb_page) {
618 if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ))
624 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
625 bdevname(rdev->bdev,b));
629 static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
631 return sb1->set_uuid0 == sb2->set_uuid0 &&
632 sb1->set_uuid1 == sb2->set_uuid1 &&
633 sb1->set_uuid2 == sb2->set_uuid2 &&
634 sb1->set_uuid3 == sb2->set_uuid3;
637 static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
640 mdp_super_t *tmp1, *tmp2;
642 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
643 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
645 if (!tmp1 || !tmp2) {
647 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
655 * nr_disks is not constant
660 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
668 static u32 md_csum_fold(u32 csum)
670 csum = (csum & 0xffff) + (csum >> 16);
671 return (csum & 0xffff) + (csum >> 16);
674 static unsigned int calc_sb_csum(mdp_super_t * sb)
677 u32 *sb32 = (u32*)sb;
679 unsigned int disk_csum, csum;
681 disk_csum = sb->sb_csum;
684 for (i = 0; i < MD_SB_BYTES/4 ; i++)
686 csum = (newcsum & 0xffffffff) + (newcsum>>32);
690 /* This used to use csum_partial, which was wrong for several
691 * reasons including that different results are returned on
692 * different architectures. It isn't critical that we get exactly
693 * the same return value as before (we always csum_fold before
694 * testing, and that removes any differences). However as we
695 * know that csum_partial always returned a 16bit value on
696 * alphas, do a fold to maximise conformity to previous behaviour.
698 sb->sb_csum = md_csum_fold(disk_csum);
700 sb->sb_csum = disk_csum;
707 * Handle superblock details.
708 * We want to be able to handle multiple superblock formats
709 * so we have a common interface to them all, and an array of
710 * different handlers.
711 * We rely on user-space to write the initial superblock, and support
712 * reading and updating of superblocks.
713 * Interface methods are:
714 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
715 * loads and validates a superblock on dev.
716 * if refdev != NULL, compare superblocks on both devices
718 * 0 - dev has a superblock that is compatible with refdev
719 * 1 - dev has a superblock that is compatible and newer than refdev
720 * so dev should be used as the refdev in future
721 * -EINVAL superblock incompatible or invalid
722 * -othererror e.g. -EIO
724 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
725 * Verify that dev is acceptable into mddev.
726 * The first time, mddev->raid_disks will be 0, and data from
727 * dev should be merged in. Subsequent calls check that dev
728 * is new enough. Return 0 or -EINVAL
730 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
731 * Update the superblock for rdev with data in mddev
732 * This does not write to disc.
738 struct module *owner;
739 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
741 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
742 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
743 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
744 sector_t num_sectors);
748 * load_super for 0.90.0
750 static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
752 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
757 * Calculate the position of the superblock (512byte sectors),
758 * it's at the end of the disk.
760 * It also happens to be a multiple of 4Kb.
762 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
764 ret = read_disk_sb(rdev, MD_SB_BYTES);
769 bdevname(rdev->bdev, b);
770 sb = (mdp_super_t*)page_address(rdev->sb_page);
772 if (sb->md_magic != MD_SB_MAGIC) {
773 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
778 if (sb->major_version != 0 ||
779 sb->minor_version < 90 ||
780 sb->minor_version > 91) {
781 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
782 sb->major_version, sb->minor_version,
787 if (sb->raid_disks <= 0)
790 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
791 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
796 rdev->preferred_minor = sb->md_minor;
797 rdev->data_offset = 0;
798 rdev->sb_size = MD_SB_BYTES;
800 if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) {
801 if (sb->level != 1 && sb->level != 4
802 && sb->level != 5 && sb->level != 6
803 && sb->level != 10) {
804 /* FIXME use a better test */
806 "md: bitmaps not supported for this level.\n");
811 if (sb->level == LEVEL_MULTIPATH)
814 rdev->desc_nr = sb->this_disk.number;
820 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
821 if (!uuid_equal(refsb, sb)) {
822 printk(KERN_WARNING "md: %s has different UUID to %s\n",
823 b, bdevname(refdev->bdev,b2));
826 if (!sb_equal(refsb, sb)) {
827 printk(KERN_WARNING "md: %s has same UUID"
828 " but different superblock to %s\n",
829 b, bdevname(refdev->bdev, b2));
833 ev2 = md_event(refsb);
839 rdev->sectors = calc_num_sectors(rdev, sb->chunk_size);
841 if (rdev->sectors < sb->size * 2 && sb->level > 1)
842 /* "this cannot possibly happen" ... */
850 * validate_super for 0.90.0
852 static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
855 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
856 __u64 ev1 = md_event(sb);
858 rdev->raid_disk = -1;
859 clear_bit(Faulty, &rdev->flags);
860 clear_bit(In_sync, &rdev->flags);
861 clear_bit(WriteMostly, &rdev->flags);
862 clear_bit(BarriersNotsupp, &rdev->flags);
864 if (mddev->raid_disks == 0) {
865 mddev->major_version = 0;
866 mddev->minor_version = sb->minor_version;
867 mddev->patch_version = sb->patch_version;
869 mddev->chunk_size = sb->chunk_size;
870 mddev->ctime = sb->ctime;
871 mddev->utime = sb->utime;
872 mddev->level = sb->level;
873 mddev->clevel[0] = 0;
874 mddev->layout = sb->layout;
875 mddev->raid_disks = sb->raid_disks;
876 mddev->dev_sectors = sb->size * 2;
878 mddev->bitmap_offset = 0;
879 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
881 if (mddev->minor_version >= 91) {
882 mddev->reshape_position = sb->reshape_position;
883 mddev->delta_disks = sb->delta_disks;
884 mddev->new_level = sb->new_level;
885 mddev->new_layout = sb->new_layout;
886 mddev->new_chunk = sb->new_chunk;
888 mddev->reshape_position = MaxSector;
889 mddev->delta_disks = 0;
890 mddev->new_level = mddev->level;
891 mddev->new_layout = mddev->layout;
892 mddev->new_chunk = mddev->chunk_size;
895 if (sb->state & (1<<MD_SB_CLEAN))
896 mddev->recovery_cp = MaxSector;
898 if (sb->events_hi == sb->cp_events_hi &&
899 sb->events_lo == sb->cp_events_lo) {
900 mddev->recovery_cp = sb->recovery_cp;
902 mddev->recovery_cp = 0;
905 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
906 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
907 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
908 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
910 mddev->max_disks = MD_SB_DISKS;
912 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
913 mddev->bitmap_file == NULL)
914 mddev->bitmap_offset = mddev->default_bitmap_offset;
916 } else if (mddev->pers == NULL) {
917 /* Insist on good event counter while assembling */
919 if (ev1 < mddev->events)
921 } else if (mddev->bitmap) {
922 /* if adding to array with a bitmap, then we can accept an
923 * older device ... but not too old.
925 if (ev1 < mddev->bitmap->events_cleared)
928 if (ev1 < mddev->events)
929 /* just a hot-add of a new device, leave raid_disk at -1 */
933 if (mddev->level != LEVEL_MULTIPATH) {
934 desc = sb->disks + rdev->desc_nr;
936 if (desc->state & (1<<MD_DISK_FAULTY))
937 set_bit(Faulty, &rdev->flags);
938 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
939 desc->raid_disk < mddev->raid_disks */) {
940 set_bit(In_sync, &rdev->flags);
941 rdev->raid_disk = desc->raid_disk;
943 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
944 set_bit(WriteMostly, &rdev->flags);
945 } else /* MULTIPATH are always insync */
946 set_bit(In_sync, &rdev->flags);
951 * sync_super for 0.90.0
953 static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
957 int next_spare = mddev->raid_disks;
960 /* make rdev->sb match mddev data..
963 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
964 * 3/ any empty disks < next_spare become removed
966 * disks[0] gets initialised to REMOVED because
967 * we cannot be sure from other fields if it has
968 * been initialised or not.
971 int active=0, working=0,failed=0,spare=0,nr_disks=0;
973 rdev->sb_size = MD_SB_BYTES;
975 sb = (mdp_super_t*)page_address(rdev->sb_page);
977 memset(sb, 0, sizeof(*sb));
979 sb->md_magic = MD_SB_MAGIC;
980 sb->major_version = mddev->major_version;
981 sb->patch_version = mddev->patch_version;
982 sb->gvalid_words = 0; /* ignored */
983 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
984 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
985 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
986 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
988 sb->ctime = mddev->ctime;
989 sb->level = mddev->level;
990 sb->size = mddev->dev_sectors / 2;
991 sb->raid_disks = mddev->raid_disks;
992 sb->md_minor = mddev->md_minor;
993 sb->not_persistent = 0;
994 sb->utime = mddev->utime;
996 sb->events_hi = (mddev->events>>32);
997 sb->events_lo = (u32)mddev->events;
999 if (mddev->reshape_position == MaxSector)
1000 sb->minor_version = 90;
1002 sb->minor_version = 91;
1003 sb->reshape_position = mddev->reshape_position;
1004 sb->new_level = mddev->new_level;
1005 sb->delta_disks = mddev->delta_disks;
1006 sb->new_layout = mddev->new_layout;
1007 sb->new_chunk = mddev->new_chunk;
1009 mddev->minor_version = sb->minor_version;
1012 sb->recovery_cp = mddev->recovery_cp;
1013 sb->cp_events_hi = (mddev->events>>32);
1014 sb->cp_events_lo = (u32)mddev->events;
1015 if (mddev->recovery_cp == MaxSector)
1016 sb->state = (1<< MD_SB_CLEAN);
1018 sb->recovery_cp = 0;
1020 sb->layout = mddev->layout;
1021 sb->chunk_size = mddev->chunk_size;
1023 if (mddev->bitmap && mddev->bitmap_file == NULL)
1024 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1026 sb->disks[0].state = (1<<MD_DISK_REMOVED);
1027 list_for_each_entry(rdev2, &mddev->disks, same_set) {
1030 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
1031 && !test_bit(Faulty, &rdev2->flags))
1032 desc_nr = rdev2->raid_disk;
1034 desc_nr = next_spare++;
1035 rdev2->desc_nr = desc_nr;
1036 d = &sb->disks[rdev2->desc_nr];
1038 d->number = rdev2->desc_nr;
1039 d->major = MAJOR(rdev2->bdev->bd_dev);
1040 d->minor = MINOR(rdev2->bdev->bd_dev);
1041 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
1042 && !test_bit(Faulty, &rdev2->flags))
1043 d->raid_disk = rdev2->raid_disk;
1045 d->raid_disk = rdev2->desc_nr; /* compatibility */
1046 if (test_bit(Faulty, &rdev2->flags))
1047 d->state = (1<<MD_DISK_FAULTY);
1048 else if (test_bit(In_sync, &rdev2->flags)) {
1049 d->state = (1<<MD_DISK_ACTIVE);
1050 d->state |= (1<<MD_DISK_SYNC);
1058 if (test_bit(WriteMostly, &rdev2->flags))
1059 d->state |= (1<<MD_DISK_WRITEMOSTLY);
1061 /* now set the "removed" and "faulty" bits on any missing devices */
1062 for (i=0 ; i < mddev->raid_disks ; i++) {
1063 mdp_disk_t *d = &sb->disks[i];
1064 if (d->state == 0 && d->number == 0) {
1067 d->state = (1<<MD_DISK_REMOVED);
1068 d->state |= (1<<MD_DISK_FAULTY);
1072 sb->nr_disks = nr_disks;
1073 sb->active_disks = active;
1074 sb->working_disks = working;
1075 sb->failed_disks = failed;
1076 sb->spare_disks = spare;
1078 sb->this_disk = sb->disks[rdev->desc_nr];
1079 sb->sb_csum = calc_sb_csum(sb);
1083 * rdev_size_change for 0.90.0
1085 static unsigned long long
1086 super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
1088 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1089 return 0; /* component must fit device */
1090 if (rdev->mddev->bitmap_offset)
1091 return 0; /* can't move bitmap */
1092 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
1093 if (!num_sectors || num_sectors > rdev->sb_start)
1094 num_sectors = rdev->sb_start;
1095 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1097 md_super_wait(rdev->mddev);
1098 return num_sectors / 2; /* kB for sysfs */
1103 * version 1 superblock
1106 static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
1110 unsigned long long newcsum;
1111 int size = 256 + le32_to_cpu(sb->max_dev)*2;
1112 __le32 *isuper = (__le32*)sb;
1115 disk_csum = sb->sb_csum;
1118 for (i=0; size>=4; size -= 4 )
1119 newcsum += le32_to_cpu(*isuper++);
1122 newcsum += le16_to_cpu(*(__le16*) isuper);
1124 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1125 sb->sb_csum = disk_csum;
1126 return cpu_to_le32(csum);
1129 static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1131 struct mdp_superblock_1 *sb;
1134 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1138 * Calculate the position of the superblock in 512byte sectors.
1139 * It is always aligned to a 4K boundary and
1140 * depeding on minor_version, it can be:
1141 * 0: At least 8K, but less than 12K, from end of device
1142 * 1: At start of device
1143 * 2: 4K from start of device.
1145 switch(minor_version) {
1147 sb_start = rdev->bdev->bd_inode->i_size >> 9;
1149 sb_start &= ~(sector_t)(4*2-1);
1160 rdev->sb_start = sb_start;
1162 /* superblock is rarely larger than 1K, but it can be larger,
1163 * and it is safe to read 4k, so we do that
1165 ret = read_disk_sb(rdev, 4096);
1166 if (ret) return ret;
1169 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1171 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1172 sb->major_version != cpu_to_le32(1) ||
1173 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1174 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
1175 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
1178 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1179 printk("md: invalid superblock checksum on %s\n",
1180 bdevname(rdev->bdev,b));
1183 if (le64_to_cpu(sb->data_size) < 10) {
1184 printk("md: data_size too small on %s\n",
1185 bdevname(rdev->bdev,b));
1188 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET)) {
1189 if (sb->level != cpu_to_le32(1) &&
1190 sb->level != cpu_to_le32(4) &&
1191 sb->level != cpu_to_le32(5) &&
1192 sb->level != cpu_to_le32(6) &&
1193 sb->level != cpu_to_le32(10)) {
1195 "md: bitmaps not supported for this level.\n");
1200 rdev->preferred_minor = 0xffff;
1201 rdev->data_offset = le64_to_cpu(sb->data_offset);
1202 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
1204 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
1205 bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
1206 if (rdev->sb_size & bmask)
1207 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1210 && rdev->data_offset < sb_start + (rdev->sb_size/512))
1213 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1216 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1222 struct mdp_superblock_1 *refsb =
1223 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1225 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1226 sb->level != refsb->level ||
1227 sb->layout != refsb->layout ||
1228 sb->chunksize != refsb->chunksize) {
1229 printk(KERN_WARNING "md: %s has strangely different"
1230 " superblock to %s\n",
1231 bdevname(rdev->bdev,b),
1232 bdevname(refdev->bdev,b2));
1235 ev1 = le64_to_cpu(sb->events);
1236 ev2 = le64_to_cpu(refsb->events);
1244 rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) -
1245 le64_to_cpu(sb->data_offset);
1247 rdev->sectors = rdev->sb_start;
1248 if (rdev->sectors < le64_to_cpu(sb->data_size))
1250 rdev->sectors = le64_to_cpu(sb->data_size);
1251 if (le32_to_cpu(sb->chunksize))
1252 rdev->sectors &= ~((sector_t)le32_to_cpu(sb->chunksize) - 1);
1254 if (le64_to_cpu(sb->size) > rdev->sectors)
1259 static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1261 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1262 __u64 ev1 = le64_to_cpu(sb->events);
1264 rdev->raid_disk = -1;
1265 clear_bit(Faulty, &rdev->flags);
1266 clear_bit(In_sync, &rdev->flags);
1267 clear_bit(WriteMostly, &rdev->flags);
1268 clear_bit(BarriersNotsupp, &rdev->flags);
1270 if (mddev->raid_disks == 0) {
1271 mddev->major_version = 1;
1272 mddev->patch_version = 0;
1273 mddev->external = 0;
1274 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
1275 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1276 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1277 mddev->level = le32_to_cpu(sb->level);
1278 mddev->clevel[0] = 0;
1279 mddev->layout = le32_to_cpu(sb->layout);
1280 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1281 mddev->dev_sectors = le64_to_cpu(sb->size);
1282 mddev->events = ev1;
1283 mddev->bitmap_offset = 0;
1284 mddev->default_bitmap_offset = 1024 >> 9;
1286 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1287 memcpy(mddev->uuid, sb->set_uuid, 16);
1289 mddev->max_disks = (4096-256)/2;
1291 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
1292 mddev->bitmap_file == NULL )
1293 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
1295 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1296 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1297 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1298 mddev->new_level = le32_to_cpu(sb->new_level);
1299 mddev->new_layout = le32_to_cpu(sb->new_layout);
1300 mddev->new_chunk = le32_to_cpu(sb->new_chunk)<<9;
1302 mddev->reshape_position = MaxSector;
1303 mddev->delta_disks = 0;
1304 mddev->new_level = mddev->level;
1305 mddev->new_layout = mddev->layout;
1306 mddev->new_chunk = mddev->chunk_size;
1309 } else if (mddev->pers == NULL) {
1310 /* Insist of good event counter while assembling */
1312 if (ev1 < mddev->events)
1314 } else if (mddev->bitmap) {
1315 /* If adding to array with a bitmap, then we can accept an
1316 * older device, but not too old.
1318 if (ev1 < mddev->bitmap->events_cleared)
1321 if (ev1 < mddev->events)
1322 /* just a hot-add of a new device, leave raid_disk at -1 */
1325 if (mddev->level != LEVEL_MULTIPATH) {
1327 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1329 case 0xffff: /* spare */
1331 case 0xfffe: /* faulty */
1332 set_bit(Faulty, &rdev->flags);
1335 if ((le32_to_cpu(sb->feature_map) &
1336 MD_FEATURE_RECOVERY_OFFSET))
1337 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1339 set_bit(In_sync, &rdev->flags);
1340 rdev->raid_disk = role;
1343 if (sb->devflags & WriteMostly1)
1344 set_bit(WriteMostly, &rdev->flags);
1345 } else /* MULTIPATH are always insync */
1346 set_bit(In_sync, &rdev->flags);
1351 static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1353 struct mdp_superblock_1 *sb;
1356 /* make rdev->sb match mddev and rdev data. */
1358 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1360 sb->feature_map = 0;
1362 sb->recovery_offset = cpu_to_le64(0);
1363 memset(sb->pad1, 0, sizeof(sb->pad1));
1364 memset(sb->pad2, 0, sizeof(sb->pad2));
1365 memset(sb->pad3, 0, sizeof(sb->pad3));
1367 sb->utime = cpu_to_le64((__u64)mddev->utime);
1368 sb->events = cpu_to_le64(mddev->events);
1370 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1372 sb->resync_offset = cpu_to_le64(0);
1374 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
1376 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
1377 sb->size = cpu_to_le64(mddev->dev_sectors);
1379 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1380 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
1381 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
1384 if (rdev->raid_disk >= 0 &&
1385 !test_bit(In_sync, &rdev->flags)) {
1386 if (mddev->curr_resync_completed > rdev->recovery_offset)
1387 rdev->recovery_offset = mddev->curr_resync_completed;
1388 if (rdev->recovery_offset > 0) {
1390 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1391 sb->recovery_offset =
1392 cpu_to_le64(rdev->recovery_offset);
1396 if (mddev->reshape_position != MaxSector) {
1397 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1398 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1399 sb->new_layout = cpu_to_le32(mddev->new_layout);
1400 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1401 sb->new_level = cpu_to_le32(mddev->new_level);
1402 sb->new_chunk = cpu_to_le32(mddev->new_chunk>>9);
1406 list_for_each_entry(rdev2, &mddev->disks, same_set)
1407 if (rdev2->desc_nr+1 > max_dev)
1408 max_dev = rdev2->desc_nr+1;
1410 if (max_dev > le32_to_cpu(sb->max_dev))
1411 sb->max_dev = cpu_to_le32(max_dev);
1412 for (i=0; i<max_dev;i++)
1413 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1415 list_for_each_entry(rdev2, &mddev->disks, same_set) {
1417 if (test_bit(Faulty, &rdev2->flags))
1418 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1419 else if (test_bit(In_sync, &rdev2->flags))
1420 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1421 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1422 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1424 sb->dev_roles[i] = cpu_to_le16(0xffff);
1427 sb->sb_csum = calc_sb_1_csum(sb);
1430 static unsigned long long
1431 super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
1433 struct mdp_superblock_1 *sb;
1434 sector_t max_sectors;
1435 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1436 return 0; /* component must fit device */
1437 if (rdev->sb_start < rdev->data_offset) {
1438 /* minor versions 1 and 2; superblock before data */
1439 max_sectors = rdev->bdev->bd_inode->i_size >> 9;
1440 max_sectors -= rdev->data_offset;
1441 if (!num_sectors || num_sectors > max_sectors)
1442 num_sectors = max_sectors;
1443 } else if (rdev->mddev->bitmap_offset) {
1444 /* minor version 0 with bitmap we can't move */
1447 /* minor version 0; superblock after data */
1449 sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1450 sb_start &= ~(sector_t)(4*2 - 1);
1451 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
1452 if (!num_sectors || num_sectors > max_sectors)
1453 num_sectors = max_sectors;
1454 rdev->sb_start = sb_start;
1456 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
1457 sb->data_size = cpu_to_le64(num_sectors);
1458 sb->super_offset = rdev->sb_start;
1459 sb->sb_csum = calc_sb_1_csum(sb);
1460 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1462 md_super_wait(rdev->mddev);
1463 return num_sectors / 2; /* kB for sysfs */
1466 static struct super_type super_types[] = {
1469 .owner = THIS_MODULE,
1470 .load_super = super_90_load,
1471 .validate_super = super_90_validate,
1472 .sync_super = super_90_sync,
1473 .rdev_size_change = super_90_rdev_size_change,
1477 .owner = THIS_MODULE,
1478 .load_super = super_1_load,
1479 .validate_super = super_1_validate,
1480 .sync_super = super_1_sync,
1481 .rdev_size_change = super_1_rdev_size_change,
1485 static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1487 mdk_rdev_t *rdev, *rdev2;
1490 rdev_for_each_rcu(rdev, mddev1)
1491 rdev_for_each_rcu(rdev2, mddev2)
1492 if (rdev->bdev->bd_contains ==
1493 rdev2->bdev->bd_contains) {
1501 static LIST_HEAD(pending_raid_disks);
1503 static void md_integrity_check(mdk_rdev_t *rdev, mddev_t *mddev)
1505 struct mdk_personality *pers = mddev->pers;
1506 struct gendisk *disk = mddev->gendisk;
1507 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1508 struct blk_integrity *bi_mddev = blk_get_integrity(disk);
1510 /* Data integrity passthrough not supported on RAID 4, 5 and 6 */
1511 if (pers && pers->level >= 4 && pers->level <= 6)
1514 /* If rdev is integrity capable, register profile for mddev */
1515 if (!bi_mddev && bi_rdev) {
1516 if (blk_integrity_register(disk, bi_rdev))
1517 printk(KERN_ERR "%s: %s Could not register integrity!\n",
1518 __func__, disk->disk_name);
1520 printk(KERN_NOTICE "Enabling data integrity on %s\n",
1525 /* Check that mddev and rdev have matching profiles */
1526 if (blk_integrity_compare(disk, rdev->bdev->bd_disk) < 0) {
1527 printk(KERN_ERR "%s: %s/%s integrity mismatch!\n", __func__,
1528 disk->disk_name, rdev->bdev->bd_disk->disk_name);
1529 printk(KERN_NOTICE "Disabling data integrity on %s\n",
1531 blk_integrity_unregister(disk);
1535 static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1537 char b[BDEVNAME_SIZE];
1547 /* prevent duplicates */
1548 if (find_rdev(mddev, rdev->bdev->bd_dev))
1551 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1552 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1553 rdev->sectors < mddev->dev_sectors)) {
1555 /* Cannot change size, so fail
1556 * If mddev->level <= 0, then we don't care
1557 * about aligning sizes (e.g. linear)
1559 if (mddev->level > 0)
1562 mddev->dev_sectors = rdev->sectors;
1565 /* Verify rdev->desc_nr is unique.
1566 * If it is -1, assign a free number, else
1567 * check number is not in use
1569 if (rdev->desc_nr < 0) {
1571 if (mddev->pers) choice = mddev->raid_disks;
1572 while (find_rdev_nr(mddev, choice))
1574 rdev->desc_nr = choice;
1576 if (find_rdev_nr(mddev, rdev->desc_nr))
1579 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1580 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1581 mdname(mddev), mddev->max_disks);
1584 bdevname(rdev->bdev,b);
1585 while ( (s=strchr(b, '/')) != NULL)
1588 rdev->mddev = mddev;
1589 printk(KERN_INFO "md: bind<%s>\n", b);
1591 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
1594 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
1595 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1596 kobject_del(&rdev->kobj);
1599 rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state");
1601 list_add_rcu(&rdev->same_set, &mddev->disks);
1602 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
1604 /* May as well allow recovery to be retried once */
1605 mddev->recovery_disabled = 0;
1607 md_integrity_check(rdev, mddev);
1611 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1616 static void md_delayed_delete(struct work_struct *ws)
1618 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1619 kobject_del(&rdev->kobj);
1620 kobject_put(&rdev->kobj);
1623 static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1625 char b[BDEVNAME_SIZE];
1630 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
1631 list_del_rcu(&rdev->same_set);
1632 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1634 sysfs_remove_link(&rdev->kobj, "block");
1635 sysfs_put(rdev->sysfs_state);
1636 rdev->sysfs_state = NULL;
1637 /* We need to delay this, otherwise we can deadlock when
1638 * writing to 'remove' to "dev/state". We also need
1639 * to delay it due to rcu usage.
1642 INIT_WORK(&rdev->del_work, md_delayed_delete);
1643 kobject_get(&rdev->kobj);
1644 schedule_work(&rdev->del_work);
1648 * prevent the device from being mounted, repartitioned or
1649 * otherwise reused by a RAID array (or any other kernel
1650 * subsystem), by bd_claiming the device.
1652 static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
1655 struct block_device *bdev;
1656 char b[BDEVNAME_SIZE];
1658 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
1660 printk(KERN_ERR "md: could not open %s.\n",
1661 __bdevname(dev, b));
1662 return PTR_ERR(bdev);
1664 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
1666 printk(KERN_ERR "md: could not bd_claim %s.\n",
1668 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
1672 set_bit(AllReserved, &rdev->flags);
1677 static void unlock_rdev(mdk_rdev_t *rdev)
1679 struct block_device *bdev = rdev->bdev;
1684 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
1687 void md_autodetect_dev(dev_t dev);
1689 static void export_rdev(mdk_rdev_t * rdev)
1691 char b[BDEVNAME_SIZE];
1692 printk(KERN_INFO "md: export_rdev(%s)\n",
1693 bdevname(rdev->bdev,b));
1698 if (test_bit(AutoDetected, &rdev->flags))
1699 md_autodetect_dev(rdev->bdev->bd_dev);
1702 kobject_put(&rdev->kobj);
1705 static void kick_rdev_from_array(mdk_rdev_t * rdev)
1707 unbind_rdev_from_array(rdev);
1711 static void export_array(mddev_t *mddev)
1713 mdk_rdev_t *rdev, *tmp;
1715 rdev_for_each(rdev, tmp, mddev) {
1720 kick_rdev_from_array(rdev);
1722 if (!list_empty(&mddev->disks))
1724 mddev->raid_disks = 0;
1725 mddev->major_version = 0;
1728 static void print_desc(mdp_disk_t *desc)
1730 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1731 desc->major,desc->minor,desc->raid_disk,desc->state);
1734 static void print_sb_90(mdp_super_t *sb)
1739 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1740 sb->major_version, sb->minor_version, sb->patch_version,
1741 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1743 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1744 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1745 sb->md_minor, sb->layout, sb->chunk_size);
1746 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1747 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1748 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1749 sb->failed_disks, sb->spare_disks,
1750 sb->sb_csum, (unsigned long)sb->events_lo);
1753 for (i = 0; i < MD_SB_DISKS; i++) {
1756 desc = sb->disks + i;
1757 if (desc->number || desc->major || desc->minor ||
1758 desc->raid_disk || (desc->state && (desc->state != 4))) {
1759 printk(" D %2d: ", i);
1763 printk(KERN_INFO "md: THIS: ");
1764 print_desc(&sb->this_disk);
1767 static void print_sb_1(struct mdp_superblock_1 *sb)
1771 uuid = sb->set_uuid;
1772 printk(KERN_INFO "md: SB: (V:%u) (F:0x%08x) Array-ID:<%02x%02x%02x%02x"
1773 ":%02x%02x:%02x%02x:%02x%02x:%02x%02x%02x%02x%02x%02x>\n"
1774 KERN_INFO "md: Name: \"%s\" CT:%llu\n",
1775 le32_to_cpu(sb->major_version),
1776 le32_to_cpu(sb->feature_map),
1777 uuid[0], uuid[1], uuid[2], uuid[3],
1778 uuid[4], uuid[5], uuid[6], uuid[7],
1779 uuid[8], uuid[9], uuid[10], uuid[11],
1780 uuid[12], uuid[13], uuid[14], uuid[15],
1782 (unsigned long long)le64_to_cpu(sb->ctime)
1783 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
1785 uuid = sb->device_uuid;
1786 printk(KERN_INFO "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
1788 KERN_INFO "md: Dev:%08x UUID: %02x%02x%02x%02x:%02x%02x:%02x%02x:%02x%02x"
1789 ":%02x%02x%02x%02x%02x%02x\n"
1790 KERN_INFO "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
1791 KERN_INFO "md: (MaxDev:%u) \n",
1792 le32_to_cpu(sb->level),
1793 (unsigned long long)le64_to_cpu(sb->size),
1794 le32_to_cpu(sb->raid_disks),
1795 le32_to_cpu(sb->layout),
1796 le32_to_cpu(sb->chunksize),
1797 (unsigned long long)le64_to_cpu(sb->data_offset),
1798 (unsigned long long)le64_to_cpu(sb->data_size),
1799 (unsigned long long)le64_to_cpu(sb->super_offset),
1800 (unsigned long long)le64_to_cpu(sb->recovery_offset),
1801 le32_to_cpu(sb->dev_number),
1802 uuid[0], uuid[1], uuid[2], uuid[3],
1803 uuid[4], uuid[5], uuid[6], uuid[7],
1804 uuid[8], uuid[9], uuid[10], uuid[11],
1805 uuid[12], uuid[13], uuid[14], uuid[15],
1807 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
1808 (unsigned long long)le64_to_cpu(sb->events),
1809 (unsigned long long)le64_to_cpu(sb->resync_offset),
1810 le32_to_cpu(sb->sb_csum),
1811 le32_to_cpu(sb->max_dev)
1815 static void print_rdev(mdk_rdev_t *rdev, int major_version)
1817 char b[BDEVNAME_SIZE];
1818 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
1819 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
1820 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1822 if (rdev->sb_loaded) {
1823 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
1824 switch (major_version) {
1826 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
1829 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
1833 printk(KERN_INFO "md: no rdev superblock!\n");
1836 static void md_print_devices(void)
1838 struct list_head *tmp;
1841 char b[BDEVNAME_SIZE];
1844 printk("md: **********************************\n");
1845 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1846 printk("md: **********************************\n");
1847 for_each_mddev(mddev, tmp) {
1850 bitmap_print_sb(mddev->bitmap);
1852 printk("%s: ", mdname(mddev));
1853 list_for_each_entry(rdev, &mddev->disks, same_set)
1854 printk("<%s>", bdevname(rdev->bdev,b));
1857 list_for_each_entry(rdev, &mddev->disks, same_set)
1858 print_rdev(rdev, mddev->major_version);
1860 printk("md: **********************************\n");
1865 static void sync_sbs(mddev_t * mddev, int nospares)
1867 /* Update each superblock (in-memory image), but
1868 * if we are allowed to, skip spares which already
1869 * have the right event counter, or have one earlier
1870 * (which would mean they aren't being marked as dirty
1871 * with the rest of the array)
1875 list_for_each_entry(rdev, &mddev->disks, same_set) {
1876 if (rdev->sb_events == mddev->events ||
1878 rdev->raid_disk < 0 &&
1879 (rdev->sb_events&1)==0 &&
1880 rdev->sb_events+1 == mddev->events)) {
1881 /* Don't update this superblock */
1882 rdev->sb_loaded = 2;
1884 super_types[mddev->major_version].
1885 sync_super(mddev, rdev);
1886 rdev->sb_loaded = 1;
1891 static void md_update_sb(mddev_t * mddev, int force_change)
1897 if (mddev->external)
1900 spin_lock_irq(&mddev->write_lock);
1902 set_bit(MD_CHANGE_PENDING, &mddev->flags);
1903 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
1905 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
1906 /* just a clean<-> dirty transition, possibly leave spares alone,
1907 * though if events isn't the right even/odd, we will have to do
1913 if (mddev->degraded)
1914 /* If the array is degraded, then skipping spares is both
1915 * dangerous and fairly pointless.
1916 * Dangerous because a device that was removed from the array
1917 * might have a event_count that still looks up-to-date,
1918 * so it can be re-added without a resync.
1919 * Pointless because if there are any spares to skip,
1920 * then a recovery will happen and soon that array won't
1921 * be degraded any more and the spare can go back to sleep then.
1925 sync_req = mddev->in_sync;
1926 mddev->utime = get_seconds();
1928 /* If this is just a dirty<->clean transition, and the array is clean
1929 * and 'events' is odd, we can roll back to the previous clean state */
1931 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
1932 && (mddev->events & 1)
1933 && mddev->events != 1)
1936 /* otherwise we have to go forward and ... */
1938 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
1939 /* .. if the array isn't clean, insist on an odd 'events' */
1940 if ((mddev->events&1)==0) {
1945 /* otherwise insist on an even 'events' (for clean states) */
1946 if ((mddev->events&1)) {
1953 if (!mddev->events) {
1955 * oops, this 64-bit counter should never wrap.
1956 * Either we are in around ~1 trillion A.C., assuming
1957 * 1 reboot per second, or we have a bug:
1964 * do not write anything to disk if using
1965 * nonpersistent superblocks
1967 if (!mddev->persistent) {
1968 if (!mddev->external)
1969 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
1971 spin_unlock_irq(&mddev->write_lock);
1972 wake_up(&mddev->sb_wait);
1975 sync_sbs(mddev, nospares);
1976 spin_unlock_irq(&mddev->write_lock);
1979 "md: updating %s RAID superblock on device (in sync %d)\n",
1980 mdname(mddev),mddev->in_sync);
1982 bitmap_update_sb(mddev->bitmap);
1983 list_for_each_entry(rdev, &mddev->disks, same_set) {
1984 char b[BDEVNAME_SIZE];
1985 dprintk(KERN_INFO "md: ");
1986 if (rdev->sb_loaded != 1)
1987 continue; /* no noise on spare devices */
1988 if (test_bit(Faulty, &rdev->flags))
1989 dprintk("(skipping faulty ");
1991 dprintk("%s ", bdevname(rdev->bdev,b));
1992 if (!test_bit(Faulty, &rdev->flags)) {
1993 md_super_write(mddev,rdev,
1994 rdev->sb_start, rdev->sb_size,
1996 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1997 bdevname(rdev->bdev,b),
1998 (unsigned long long)rdev->sb_start);
1999 rdev->sb_events = mddev->events;
2003 if (mddev->level == LEVEL_MULTIPATH)
2004 /* only need to write one superblock... */
2007 md_super_wait(mddev);
2008 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
2010 spin_lock_irq(&mddev->write_lock);
2011 if (mddev->in_sync != sync_req ||
2012 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
2013 /* have to write it out again */
2014 spin_unlock_irq(&mddev->write_lock);
2017 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2018 spin_unlock_irq(&mddev->write_lock);
2019 wake_up(&mddev->sb_wait);
2020 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2021 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
2025 /* words written to sysfs files may, or may not, be \n terminated.
2026 * We want to accept with case. For this we use cmd_match.
2028 static int cmd_match(const char *cmd, const char *str)
2030 /* See if cmd, written into a sysfs file, matches
2031 * str. They must either be the same, or cmd can
2032 * have a trailing newline
2034 while (*cmd && *str && *cmd == *str) {
2045 struct rdev_sysfs_entry {
2046 struct attribute attr;
2047 ssize_t (*show)(mdk_rdev_t *, char *);
2048 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2052 state_show(mdk_rdev_t *rdev, char *page)
2057 if (test_bit(Faulty, &rdev->flags)) {
2058 len+= sprintf(page+len, "%sfaulty",sep);
2061 if (test_bit(In_sync, &rdev->flags)) {
2062 len += sprintf(page+len, "%sin_sync",sep);
2065 if (test_bit(WriteMostly, &rdev->flags)) {
2066 len += sprintf(page+len, "%swrite_mostly",sep);
2069 if (test_bit(Blocked, &rdev->flags)) {
2070 len += sprintf(page+len, "%sblocked", sep);
2073 if (!test_bit(Faulty, &rdev->flags) &&
2074 !test_bit(In_sync, &rdev->flags)) {
2075 len += sprintf(page+len, "%sspare", sep);
2078 return len+sprintf(page+len, "\n");
2082 state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2085 * faulty - simulates and error
2086 * remove - disconnects the device
2087 * writemostly - sets write_mostly
2088 * -writemostly - clears write_mostly
2089 * blocked - sets the Blocked flag
2090 * -blocked - clears the Blocked flag
2091 * insync - sets Insync providing device isn't active
2094 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2095 md_error(rdev->mddev, rdev);
2097 } else if (cmd_match(buf, "remove")) {
2098 if (rdev->raid_disk >= 0)
2101 mddev_t *mddev = rdev->mddev;
2102 kick_rdev_from_array(rdev);
2104 md_update_sb(mddev, 1);
2105 md_new_event(mddev);
2108 } else if (cmd_match(buf, "writemostly")) {
2109 set_bit(WriteMostly, &rdev->flags);
2111 } else if (cmd_match(buf, "-writemostly")) {
2112 clear_bit(WriteMostly, &rdev->flags);
2114 } else if (cmd_match(buf, "blocked")) {
2115 set_bit(Blocked, &rdev->flags);
2117 } else if (cmd_match(buf, "-blocked")) {
2118 clear_bit(Blocked, &rdev->flags);
2119 wake_up(&rdev->blocked_wait);
2120 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2121 md_wakeup_thread(rdev->mddev->thread);
2124 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2125 set_bit(In_sync, &rdev->flags);
2128 if (!err && rdev->sysfs_state)
2129 sysfs_notify_dirent(rdev->sysfs_state);
2130 return err ? err : len;
2132 static struct rdev_sysfs_entry rdev_state =
2133 __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
2136 errors_show(mdk_rdev_t *rdev, char *page)
2138 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2142 errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2145 unsigned long n = simple_strtoul(buf, &e, 10);
2146 if (*buf && (*e == 0 || *e == '\n')) {
2147 atomic_set(&rdev->corrected_errors, n);
2152 static struct rdev_sysfs_entry rdev_errors =
2153 __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
2156 slot_show(mdk_rdev_t *rdev, char *page)
2158 if (rdev->raid_disk < 0)
2159 return sprintf(page, "none\n");
2161 return sprintf(page, "%d\n", rdev->raid_disk);
2165 slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2170 int slot = simple_strtoul(buf, &e, 10);
2171 if (strncmp(buf, "none", 4)==0)
2173 else if (e==buf || (*e && *e!= '\n'))
2175 if (rdev->mddev->pers && slot == -1) {
2176 /* Setting 'slot' on an active array requires also
2177 * updating the 'rd%d' link, and communicating
2178 * with the personality with ->hot_*_disk.
2179 * For now we only support removing
2180 * failed/spare devices. This normally happens automatically,
2181 * but not when the metadata is externally managed.
2183 if (rdev->raid_disk == -1)
2185 /* personality does all needed checks */
2186 if (rdev->mddev->pers->hot_add_disk == NULL)
2188 err = rdev->mddev->pers->
2189 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2192 sprintf(nm, "rd%d", rdev->raid_disk);
2193 sysfs_remove_link(&rdev->mddev->kobj, nm);
2194 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2195 md_wakeup_thread(rdev->mddev->thread);
2196 } else if (rdev->mddev->pers) {
2198 /* Activating a spare .. or possibly reactivating
2199 * if we ever get bitmaps working here.
2202 if (rdev->raid_disk != -1)
2205 if (rdev->mddev->pers->hot_add_disk == NULL)
2208 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
2209 if (rdev2->raid_disk == slot)
2212 rdev->raid_disk = slot;
2213 if (test_bit(In_sync, &rdev->flags))
2214 rdev->saved_raid_disk = slot;
2216 rdev->saved_raid_disk = -1;
2217 err = rdev->mddev->pers->
2218 hot_add_disk(rdev->mddev, rdev);
2220 rdev->raid_disk = -1;
2223 sysfs_notify_dirent(rdev->sysfs_state);
2224 sprintf(nm, "rd%d", rdev->raid_disk);
2225 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2227 "md: cannot register "
2229 nm, mdname(rdev->mddev));
2231 /* don't wakeup anyone, leave that to userspace. */
2233 if (slot >= rdev->mddev->raid_disks)
2235 rdev->raid_disk = slot;
2236 /* assume it is working */
2237 clear_bit(Faulty, &rdev->flags);
2238 clear_bit(WriteMostly, &rdev->flags);
2239 set_bit(In_sync, &rdev->flags);
2240 sysfs_notify_dirent(rdev->sysfs_state);
2246 static struct rdev_sysfs_entry rdev_slot =
2247 __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
2250 offset_show(mdk_rdev_t *rdev, char *page)
2252 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
2256 offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2259 unsigned long long offset = simple_strtoull(buf, &e, 10);
2260 if (e==buf || (*e && *e != '\n'))
2262 if (rdev->mddev->pers && rdev->raid_disk >= 0)
2264 if (rdev->sectors && rdev->mddev->external)
2265 /* Must set offset before size, so overlap checks
2268 rdev->data_offset = offset;
2272 static struct rdev_sysfs_entry rdev_offset =
2273 __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
2276 rdev_size_show(mdk_rdev_t *rdev, char *page)
2278 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
2281 static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2283 /* check if two start/length pairs overlap */
2291 static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2293 unsigned long long blocks;
2296 if (strict_strtoull(buf, 10, &blocks) < 0)
2299 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2300 return -EINVAL; /* sector conversion overflow */
2303 if (new != blocks * 2)
2304 return -EINVAL; /* unsigned long long to sector_t overflow */
2311 rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2313 mddev_t *my_mddev = rdev->mddev;
2314 sector_t oldsectors = rdev->sectors;
2317 if (strict_blocks_to_sectors(buf, §ors) < 0)
2319 if (my_mddev->pers && rdev->raid_disk >= 0) {
2320 if (my_mddev->persistent) {
2321 sectors = super_types[my_mddev->major_version].
2322 rdev_size_change(rdev, sectors);
2325 } else if (!sectors)
2326 sectors = (rdev->bdev->bd_inode->i_size >> 9) -
2329 if (sectors < my_mddev->dev_sectors)
2330 return -EINVAL; /* component must fit device */
2332 rdev->sectors = sectors;
2333 if (sectors > oldsectors && my_mddev->external) {
2334 /* need to check that all other rdevs with the same ->bdev
2335 * do not overlap. We need to unlock the mddev to avoid
2336 * a deadlock. We have already changed rdev->sectors, and if
2337 * we have to change it back, we will have the lock again.
2341 struct list_head *tmp;
2343 mddev_unlock(my_mddev);
2344 for_each_mddev(mddev, tmp) {
2348 list_for_each_entry(rdev2, &mddev->disks, same_set)
2349 if (test_bit(AllReserved, &rdev2->flags) ||
2350 (rdev->bdev == rdev2->bdev &&
2352 overlaps(rdev->data_offset, rdev->sectors,
2358 mddev_unlock(mddev);
2364 mddev_lock(my_mddev);
2366 /* Someone else could have slipped in a size
2367 * change here, but doing so is just silly.
2368 * We put oldsectors back because we *know* it is
2369 * safe, and trust userspace not to race with
2372 rdev->sectors = oldsectors;
2379 static struct rdev_sysfs_entry rdev_size =
2380 __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
2382 static struct attribute *rdev_default_attrs[] = {
2391 rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2393 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2394 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
2395 mddev_t *mddev = rdev->mddev;
2401 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2403 if (rdev->mddev == NULL)
2406 rv = entry->show(rdev, page);
2407 mddev_unlock(mddev);
2413 rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2414 const char *page, size_t length)
2416 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2417 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
2419 mddev_t *mddev = rdev->mddev;
2423 if (!capable(CAP_SYS_ADMIN))
2425 rv = mddev ? mddev_lock(mddev): -EBUSY;
2427 if (rdev->mddev == NULL)
2430 rv = entry->store(rdev, page, length);
2431 mddev_unlock(mddev);
2436 static void rdev_free(struct kobject *ko)
2438 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2441 static struct sysfs_ops rdev_sysfs_ops = {
2442 .show = rdev_attr_show,
2443 .store = rdev_attr_store,
2445 static struct kobj_type rdev_ktype = {
2446 .release = rdev_free,
2447 .sysfs_ops = &rdev_sysfs_ops,
2448 .default_attrs = rdev_default_attrs,
2452 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2454 * mark the device faulty if:
2456 * - the device is nonexistent (zero size)
2457 * - the device has no valid superblock
2459 * a faulty rdev _never_ has rdev->sb set.
2461 static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2463 char b[BDEVNAME_SIZE];
2468 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
2470 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2471 return ERR_PTR(-ENOMEM);
2474 if ((err = alloc_disk_sb(rdev)))
2477 err = lock_rdev(rdev, newdev, super_format == -2);
2481 kobject_init(&rdev->kobj, &rdev_ktype);
2484 rdev->saved_raid_disk = -1;
2485 rdev->raid_disk = -1;
2487 rdev->data_offset = 0;
2488 rdev->sb_events = 0;
2489 atomic_set(&rdev->nr_pending, 0);
2490 atomic_set(&rdev->read_errors, 0);
2491 atomic_set(&rdev->corrected_errors, 0);
2493 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2496 "md: %s has zero or unknown size, marking faulty!\n",
2497 bdevname(rdev->bdev,b));
2502 if (super_format >= 0) {
2503 err = super_types[super_format].
2504 load_super(rdev, NULL, super_minor);
2505 if (err == -EINVAL) {
2507 "md: %s does not have a valid v%d.%d "
2508 "superblock, not importing!\n",
2509 bdevname(rdev->bdev,b),
2510 super_format, super_minor);
2515 "md: could not read %s's sb, not importing!\n",
2516 bdevname(rdev->bdev,b));
2521 INIT_LIST_HEAD(&rdev->same_set);
2522 init_waitqueue_head(&rdev->blocked_wait);
2527 if (rdev->sb_page) {
2533 return ERR_PTR(err);
2537 * Check a full RAID array for plausibility
2541 static void analyze_sbs(mddev_t * mddev)
2544 mdk_rdev_t *rdev, *freshest, *tmp;
2545 char b[BDEVNAME_SIZE];
2548 rdev_for_each(rdev, tmp, mddev)
2549 switch (super_types[mddev->major_version].
2550 load_super(rdev, freshest, mddev->minor_version)) {
2558 "md: fatal superblock inconsistency in %s"
2559 " -- removing from array\n",
2560 bdevname(rdev->bdev,b));
2561 kick_rdev_from_array(rdev);
2565 super_types[mddev->major_version].
2566 validate_super(mddev, freshest);
2569 rdev_for_each(rdev, tmp, mddev) {
2570 if (rdev->desc_nr >= mddev->max_disks ||
2571 i > mddev->max_disks) {
2573 "md: %s: %s: only %d devices permitted\n",
2574 mdname(mddev), bdevname(rdev->bdev, b),
2576 kick_rdev_from_array(rdev);
2579 if (rdev != freshest)
2580 if (super_types[mddev->major_version].
2581 validate_super(mddev, rdev)) {
2582 printk(KERN_WARNING "md: kicking non-fresh %s"
2584 bdevname(rdev->bdev,b));
2585 kick_rdev_from_array(rdev);
2588 if (mddev->level == LEVEL_MULTIPATH) {
2589 rdev->desc_nr = i++;
2590 rdev->raid_disk = rdev->desc_nr;
2591 set_bit(In_sync, &rdev->flags);
2592 } else if (rdev->raid_disk >= mddev->raid_disks) {
2593 rdev->raid_disk = -1;
2594 clear_bit(In_sync, &rdev->flags);
2600 if (mddev->recovery_cp != MaxSector &&
2602 printk(KERN_ERR "md: %s: raid array is not clean"
2603 " -- starting background reconstruction\n",
2608 static void md_safemode_timeout(unsigned long data);
2611 safe_delay_show(mddev_t *mddev, char *page)
2613 int msec = (mddev->safemode_delay*1000)/HZ;
2614 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2617 safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2625 /* remove a period, and count digits after it */
2626 if (len >= sizeof(buf))
2628 strlcpy(buf, cbuf, sizeof(buf));
2629 for (i=0; i<len; i++) {
2631 if (isdigit(buf[i])) {
2636 } else if (buf[i] == '.') {
2641 if (strict_strtoul(buf, 10, &msec) < 0)
2643 msec = (msec * 1000) / scale;
2645 mddev->safemode_delay = 0;
2647 unsigned long old_delay = mddev->safemode_delay;
2648 mddev->safemode_delay = (msec*HZ)/1000;
2649 if (mddev->safemode_delay == 0)
2650 mddev->safemode_delay = 1;
2651 if (mddev->safemode_delay < old_delay)
2652 md_safemode_timeout((unsigned long)mddev);
2656 static struct md_sysfs_entry md_safe_delay =
2657 __ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
2660 level_show(mddev_t *mddev, char *page)
2662 struct mdk_personality *p = mddev->pers;
2664 return sprintf(page, "%s\n", p->name);
2665 else if (mddev->clevel[0])
2666 return sprintf(page, "%s\n", mddev->clevel);
2667 else if (mddev->level != LEVEL_NONE)
2668 return sprintf(page, "%d\n", mddev->level);
2674 level_store(mddev_t *mddev, const char *buf, size_t len)
2678 struct mdk_personality *pers;
2681 if (mddev->pers == NULL) {
2684 if (len >= sizeof(mddev->clevel))
2686 strncpy(mddev->clevel, buf, len);
2687 if (mddev->clevel[len-1] == '\n')
2689 mddev->clevel[len] = 0;
2690 mddev->level = LEVEL_NONE;
2694 /* request to change the personality. Need to ensure:
2695 * - array is not engaged in resync/recovery/reshape
2696 * - old personality can be suspended
2697 * - new personality will access other array.
2700 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
2703 if (!mddev->pers->quiesce) {
2704 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
2705 mdname(mddev), mddev->pers->name);
2709 /* Now find the new personality */
2710 if (len == 0 || len >= sizeof(level))
2712 strncpy(level, buf, len);
2713 if (level[len-1] == '\n')
2717 request_module("md-%s", level);
2718 spin_lock(&pers_lock);
2719 pers = find_pers(LEVEL_NONE, level);
2720 if (!pers || !try_module_get(pers->owner)) {
2721 spin_unlock(&pers_lock);
2722 printk(KERN_WARNING "md: personality %s not loaded\n", level);
2725 spin_unlock(&pers_lock);
2727 if (pers == mddev->pers) {
2728 /* Nothing to do! */
2729 module_put(pers->owner);
2732 if (!pers->takeover) {
2733 module_put(pers->owner);
2734 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
2735 mdname(mddev), level);
2739 /* ->takeover must set new_* and/or delta_disks
2740 * if it succeeds, and may set them when it fails.
2742 priv = pers->takeover(mddev);
2744 mddev->new_level = mddev->level;
2745 mddev->new_layout = mddev->layout;
2746 mddev->new_chunk = mddev->chunk_size;
2747 mddev->raid_disks -= mddev->delta_disks;
2748 mddev->delta_disks = 0;
2749 module_put(pers->owner);
2750 printk(KERN_WARNING "md: %s: %s would not accept array\n",
2751 mdname(mddev), level);
2752 return PTR_ERR(priv);
2755 /* Looks like we have a winner */
2756 mddev_suspend(mddev);
2757 mddev->pers->stop(mddev);
2758 module_put(mddev->pers->owner);
2760 mddev->private = priv;
2761 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
2762 mddev->level = mddev->new_level;
2763 mddev->layout = mddev->new_layout;
2764 mddev->chunk_size = mddev->new_chunk;
2765 mddev->delta_disks = 0;
2767 mddev_resume(mddev);
2768 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2769 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2770 md_wakeup_thread(mddev->thread);
2774 static struct md_sysfs_entry md_level =
2775 __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
2779 layout_show(mddev_t *mddev, char *page)
2781 /* just a number, not meaningful for all levels */
2782 if (mddev->reshape_position != MaxSector &&
2783 mddev->layout != mddev->new_layout)
2784 return sprintf(page, "%d (%d)\n",
2785 mddev->new_layout, mddev->layout);
2786 return sprintf(page, "%d\n", mddev->layout);
2790 layout_store(mddev_t *mddev, const char *buf, size_t len)
2793 unsigned long n = simple_strtoul(buf, &e, 10);
2795 if (!*buf || (*e && *e != '\n'))
2800 if (mddev->pers->reconfig == NULL)
2802 err = mddev->pers->reconfig(mddev, n, -1);
2806 mddev->new_layout = n;
2807 if (mddev->reshape_position == MaxSector)
2812 static struct md_sysfs_entry md_layout =
2813 __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
2817 raid_disks_show(mddev_t *mddev, char *page)
2819 if (mddev->raid_disks == 0)
2821 if (mddev->reshape_position != MaxSector &&
2822 mddev->delta_disks != 0)
2823 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
2824 mddev->raid_disks - mddev->delta_disks);
2825 return sprintf(page, "%d\n", mddev->raid_disks);
2828 static int update_raid_disks(mddev_t *mddev, int raid_disks);
2831 raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2835 unsigned long n = simple_strtoul(buf, &e, 10);
2837 if (!*buf || (*e && *e != '\n'))
2841 rv = update_raid_disks(mddev, n);
2842 else if (mddev->reshape_position != MaxSector) {
2843 int olddisks = mddev->raid_disks - mddev->delta_disks;
2844 mddev->delta_disks = n - olddisks;
2845 mddev->raid_disks = n;
2847 mddev->raid_disks = n;
2848 return rv ? rv : len;
2850 static struct md_sysfs_entry md_raid_disks =
2851 __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
2854 chunk_size_show(mddev_t *mddev, char *page)
2856 if (mddev->reshape_position != MaxSector &&
2857 mddev->chunk_size != mddev->new_chunk)
2858 return sprintf(page, "%d (%d)\n", mddev->new_chunk,
2860 return sprintf(page, "%d\n", mddev->chunk_size);
2864 chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2867 unsigned long n = simple_strtoul(buf, &e, 10);
2869 if (!*buf || (*e && *e != '\n'))
2874 if (mddev->pers->reconfig == NULL)
2876 err = mddev->pers->reconfig(mddev, -1, n);
2880 mddev->new_chunk = n;
2881 if (mddev->reshape_position == MaxSector)
2882 mddev->chunk_size = n;
2886 static struct md_sysfs_entry md_chunk_size =
2887 __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
2890 resync_start_show(mddev_t *mddev, char *page)
2892 if (mddev->recovery_cp == MaxSector)
2893 return sprintf(page, "none\n");
2894 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2898 resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2901 unsigned long long n = simple_strtoull(buf, &e, 10);
2905 if (!*buf || (*e && *e != '\n'))
2908 mddev->recovery_cp = n;
2911 static struct md_sysfs_entry md_resync_start =
2912 __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
2915 * The array state can be:
2918 * No devices, no size, no level
2919 * Equivalent to STOP_ARRAY ioctl
2921 * May have some settings, but array is not active
2922 * all IO results in error
2923 * When written, doesn't tear down array, but just stops it
2924 * suspended (not supported yet)
2925 * All IO requests will block. The array can be reconfigured.
2926 * Writing this, if accepted, will block until array is quiescent
2928 * no resync can happen. no superblocks get written.
2929 * write requests fail
2931 * like readonly, but behaves like 'clean' on a write request.
2933 * clean - no pending writes, but otherwise active.
2934 * When written to inactive array, starts without resync
2935 * If a write request arrives then
2936 * if metadata is known, mark 'dirty' and switch to 'active'.
2937 * if not known, block and switch to write-pending
2938 * If written to an active array that has pending writes, then fails.
2940 * fully active: IO and resync can be happening.
2941 * When written to inactive array, starts with resync
2944 * clean, but writes are blocked waiting for 'active' to be written.
2947 * like active, but no writes have been seen for a while (100msec).
2950 enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
2951 write_pending, active_idle, bad_word};
2952 static char *array_states[] = {
2953 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2954 "write-pending", "active-idle", NULL };
2956 static int match_word(const char *word, char **list)
2959 for (n=0; list[n]; n++)
2960 if (cmd_match(word, list[n]))
2966 array_state_show(mddev_t *mddev, char *page)
2968 enum array_state st = inactive;
2981 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
2983 else if (mddev->safemode)
2989 if (list_empty(&mddev->disks) &&
2990 mddev->raid_disks == 0 &&
2991 mddev->dev_sectors == 0)
2996 return sprintf(page, "%s\n", array_states[st]);
2999 static int do_md_stop(mddev_t * mddev, int ro, int is_open);
3000 static int do_md_run(mddev_t * mddev);
3001 static int restart_array(mddev_t *mddev);
3004 array_state_store(mddev_t *mddev, const char *buf, size_t len)
3007 enum array_state st = match_word(buf, array_states);
3012 /* stopping an active array */
3013 if (atomic_read(&mddev->openers) > 0)
3015 err = do_md_stop(mddev, 0, 0);
3018 /* stopping an active array */
3020 if (atomic_read(&mddev->openers) > 0)
3022 err = do_md_stop(mddev, 2, 0);
3024 err = 0; /* already inactive */
3027 break; /* not supported yet */
3030 err = do_md_stop(mddev, 1, 0);
3033 set_disk_ro(mddev->gendisk, 1);
3034 err = do_md_run(mddev);
3040 err = do_md_stop(mddev, 1, 0);
3041 else if (mddev->ro == 1)
3042 err = restart_array(mddev);
3045 set_disk_ro(mddev->gendisk, 0);
3049 err = do_md_run(mddev);
3054 restart_array(mddev);
3055 spin_lock_irq(&mddev->write_lock);
3056 if (atomic_read(&mddev->writes_pending) == 0) {
3057 if (mddev->in_sync == 0) {
3059 if (mddev->safemode == 1)
3060 mddev->safemode = 0;
3061 if (mddev->persistent)
3062 set_bit(MD_CHANGE_CLEAN,
3068 spin_unlock_irq(&mddev->write_lock);
3074 restart_array(mddev);
3075 if (mddev->external)
3076 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
3077 wake_up(&mddev->sb_wait);
3081 set_disk_ro(mddev->gendisk, 0);
3082 err = do_md_run(mddev);
3087 /* these cannot be set */
3093 sysfs_notify_dirent(mddev->sysfs_state);
3097 static struct md_sysfs_entry md_array_state =
3098 __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
3101 null_show(mddev_t *mddev, char *page)
3107 new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3109 /* buf must be %d:%d\n? giving major and minor numbers */
3110 /* The new device is added to the array.
3111 * If the array has a persistent superblock, we read the
3112 * superblock to initialise info and check validity.
3113 * Otherwise, only checking done is that in bind_rdev_to_array,
3114 * which mainly checks size.
3117 int major = simple_strtoul(buf, &e, 10);
3123 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3125 minor = simple_strtoul(e+1, &e, 10);
3126 if (*e && *e != '\n')
3128 dev = MKDEV(major, minor);
3129 if (major != MAJOR(dev) ||
3130 minor != MINOR(dev))
3134 if (mddev->persistent) {
3135 rdev = md_import_device(dev, mddev->major_version,
3136 mddev->minor_version);
3137 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3138 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3139 mdk_rdev_t, same_set);
3140 err = super_types[mddev->major_version]
3141 .load_super(rdev, rdev0, mddev->minor_version);
3145 } else if (mddev->external)
3146 rdev = md_import_device(dev, -2, -1);
3148 rdev = md_import_device(dev, -1, -1);
3151 return PTR_ERR(rdev);
3152 err = bind_rdev_to_array(rdev, mddev);
3156 return err ? err : len;
3159 static struct md_sysfs_entry md_new_device =
3160 __ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
3163 bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3166 unsigned long chunk, end_chunk;
3170 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3172 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3173 if (buf == end) break;
3174 if (*end == '-') { /* range */
3176 end_chunk = simple_strtoul(buf, &end, 0);
3177 if (buf == end) break;
3179 if (*end && !isspace(*end)) break;
3180 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
3182 while (isspace(*buf)) buf++;
3184 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3189 static struct md_sysfs_entry md_bitmap =
3190 __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3193 size_show(mddev_t *mddev, char *page)
3195 return sprintf(page, "%llu\n",
3196 (unsigned long long)mddev->dev_sectors / 2);
3199 static int update_size(mddev_t *mddev, sector_t num_sectors);
3202 size_store(mddev_t *mddev, const char *buf, size_t len)
3204 /* If array is inactive, we can reduce the component size, but
3205 * not increase it (except from 0).
3206 * If array is active, we can try an on-line resize
3209 int err = strict_blocks_to_sectors(buf, §ors);
3214 err = update_size(mddev, sectors);
3215 md_update_sb(mddev, 1);
3217 if (mddev->dev_sectors == 0 ||
3218 mddev->dev_sectors > sectors)
3219 mddev->dev_sectors = sectors;
3223 return err ? err : len;
3226 static struct md_sysfs_entry md_size =
3227 __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
3232 * 'none' for arrays with no metadata (good luck...)
3233 * 'external' for arrays with externally managed metadata,
3234 * or N.M for internally known formats
3237 metadata_show(mddev_t *mddev, char *page)
3239 if (mddev->persistent)
3240 return sprintf(page, "%d.%d\n",
3241 mddev->major_version, mddev->minor_version);
3242 else if (mddev->external)
3243 return sprintf(page, "external:%s\n", mddev->metadata_type);
3245 return sprintf(page, "none\n");
3249 metadata_store(mddev_t *mddev, const char *buf, size_t len)
3253 /* Changing the details of 'external' metadata is
3254 * always permitted. Otherwise there must be
3255 * no devices attached to the array.
3257 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3259 else if (!list_empty(&mddev->disks))
3262 if (cmd_match(buf, "none")) {
3263 mddev->persistent = 0;
3264 mddev->external = 0;
3265 mddev->major_version = 0;
3266 mddev->minor_version = 90;
3269 if (strncmp(buf, "external:", 9) == 0) {
3270 size_t namelen = len-9;
3271 if (namelen >= sizeof(mddev->metadata_type))
3272 namelen = sizeof(mddev->metadata_type)-1;
3273 strncpy(mddev->metadata_type, buf+9, namelen);
3274 mddev->metadata_type[namelen] = 0;
3275 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3276 mddev->metadata_type[--namelen] = 0;
3277 mddev->persistent = 0;
3278 mddev->external = 1;
3279 mddev->major_version = 0;
3280 mddev->minor_version = 90;
3283 major = simple_strtoul(buf, &e, 10);
3284 if (e==buf || *e != '.')
3287 minor = simple_strtoul(buf, &e, 10);
3288 if (e==buf || (*e && *e != '\n') )
3290 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
3292 mddev->major_version = major;
3293 mddev->minor_version = minor;
3294 mddev->persistent = 1;
3295 mddev->external = 0;
3299 static struct md_sysfs_entry md_metadata =
3300 __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
3303 action_show(mddev_t *mddev, char *page)
3305 char *type = "idle";
3306 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3307 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
3308 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3310 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3311 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3313 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3317 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
3320 return sprintf(page, "%s\n", type);
3324 action_store(mddev_t *mddev, const char *page, size_t len)
3326 if (!mddev->pers || !mddev->pers->sync_request)
3329 if (cmd_match(page, "idle")) {
3330 if (mddev->sync_thread) {
3331 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3332 md_unregister_thread(mddev->sync_thread);
3333 mddev->sync_thread = NULL;
3334 mddev->recovery = 0;
3336 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3337 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
3339 else if (cmd_match(page, "resync"))
3340 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3341 else if (cmd_match(page, "recover")) {
3342 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3343 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3344 } else if (cmd_match(page, "reshape")) {
3346 if (mddev->pers->start_reshape == NULL)
3348 err = mddev->pers->start_reshape(mddev);
3351 sysfs_notify(&mddev->kobj, NULL, "degraded");
3353 if (cmd_match(page, "check"))
3354 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3355 else if (!cmd_match(page, "repair"))
3357 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3358 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3360 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3361 md_wakeup_thread(mddev->thread);
3362 sysfs_notify_dirent(mddev->sysfs_action);
3367 mismatch_cnt_show(mddev_t *mddev, char *page)
3369 return sprintf(page, "%llu\n",
3370 (unsigned long long) mddev->resync_mismatches);
3373 static struct md_sysfs_entry md_scan_mode =
3374 __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
3377 static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
3380 sync_min_show(mddev_t *mddev, char *page)
3382 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3383 mddev->sync_speed_min ? "local": "system");
3387 sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3391 if (strncmp(buf, "system", 6)==0) {
3392 mddev->sync_speed_min = 0;
3395 min = simple_strtoul(buf, &e, 10);
3396 if (buf == e || (*e && *e != '\n') || min <= 0)
3398 mddev->sync_speed_min = min;
3402 static struct md_sysfs_entry md_sync_min =
3403 __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3406 sync_max_show(mddev_t *mddev, char *page)
3408 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3409 mddev->sync_speed_max ? "local": "system");
3413 sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3417 if (strncmp(buf, "system", 6)==0) {
3418 mddev->sync_speed_max = 0;
3421 max = simple_strtoul(buf, &e, 10);
3422 if (buf == e || (*e && *e != '\n') || max <= 0)
3424 mddev->sync_speed_max = max;
3428 static struct md_sysfs_entry md_sync_max =
3429 __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3432 degraded_show(mddev_t *mddev, char *page)
3434 return sprintf(page, "%d\n", mddev->degraded);
3436 static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
3439 sync_force_parallel_show(mddev_t *mddev, char *page)
3441 return sprintf(page, "%d\n", mddev->parallel_resync);
3445 sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3449 if (strict_strtol(buf, 10, &n))
3452 if (n != 0 && n != 1)
3455 mddev->parallel_resync = n;
3457 if (mddev->sync_thread)
3458 wake_up(&resync_wait);
3463 /* force parallel resync, even with shared block devices */
3464 static struct md_sysfs_entry md_sync_force_parallel =
3465 __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3466 sync_force_parallel_show, sync_force_parallel_store);
3469 sync_speed_show(mddev_t *mddev, char *page)
3471 unsigned long resync, dt, db;
3472 if (mddev->curr_resync == 0)
3473 return sprintf(page, "none\n");
3474 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3475 dt = (jiffies - mddev->resync_mark) / HZ;
3477 db = resync - mddev->resync_mark_cnt;
3478 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
3481 static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
3484 sync_completed_show(mddev_t *mddev, char *page)
3486 unsigned long max_sectors, resync;
3488 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3489 return sprintf(page, "none\n");
3491 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3492 max_sectors = mddev->resync_max_sectors;
3494 max_sectors = mddev->dev_sectors;
3496 resync = mddev->curr_resync_completed;
3497 return sprintf(page, "%lu / %lu\n", resync, max_sectors);
3500 static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
3503 min_sync_show(mddev_t *mddev, char *page)
3505 return sprintf(page, "%llu\n",
3506 (unsigned long long)mddev->resync_min);
3509 min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3511 unsigned long long min;
3512 if (strict_strtoull(buf, 10, &min))
3514 if (min > mddev->resync_max)
3516 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3519 /* Must be a multiple of chunk_size */
3520 if (mddev->chunk_size) {
3521 if (min & (sector_t)((mddev->chunk_size>>9)-1))
3524 mddev->resync_min = min;
3529 static struct md_sysfs_entry md_min_sync =
3530 __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3533 max_sync_show(mddev_t *mddev, char *page)
3535 if (mddev->resync_max == MaxSector)
3536 return sprintf(page, "max\n");
3538 return sprintf(page, "%llu\n",
3539 (unsigned long long)mddev->resync_max);
3542 max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3544 if (strncmp(buf, "max", 3) == 0)
3545 mddev->resync_max = MaxSector;
3547 unsigned long long max;
3548 if (strict_strtoull(buf, 10, &max))
3550 if (max < mddev->resync_min)
3552 if (max < mddev->resync_max &&
3553 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3556 /* Must be a multiple of chunk_size */
3557 if (mddev->chunk_size) {
3558 if (max & (sector_t)((mddev->chunk_size>>9)-1))
3561 mddev->resync_max = max;
3563 wake_up(&mddev->recovery_wait);
3567 static struct md_sysfs_entry md_max_sync =
3568 __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3571 suspend_lo_show(mddev_t *mddev, char *page)
3573 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3577 suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3580 unsigned long long new = simple_strtoull(buf, &e, 10);
3582 if (mddev->pers->quiesce == NULL)
3584 if (buf == e || (*e && *e != '\n'))
3586 if (new >= mddev->suspend_hi ||
3587 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3588 mddev->suspend_lo = new;
3589 mddev->pers->quiesce(mddev, 2);
3594 static struct md_sysfs_entry md_suspend_lo =
3595 __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3599 suspend_hi_show(mddev_t *mddev, char *page)
3601 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3605 suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3608 unsigned long long new = simple_strtoull(buf, &e, 10);
3610 if (mddev->pers->quiesce == NULL)
3612 if (buf == e || (*e && *e != '\n'))
3614 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3615 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3616 mddev->suspend_hi = new;
3617 mddev->pers->quiesce(mddev, 1);
3618 mddev->pers->quiesce(mddev, 0);
3623 static struct md_sysfs_entry md_suspend_hi =
3624 __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3627 reshape_position_show(mddev_t *mddev, char *page)
3629 if (mddev->reshape_position != MaxSector)
3630 return sprintf(page, "%llu\n",
3631 (unsigned long long)mddev->reshape_position);
3632 strcpy(page, "none\n");
3637 reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3640 unsigned long long new = simple_strtoull(buf, &e, 10);
3643 if (buf == e || (*e && *e != '\n'))
3645 mddev->reshape_position = new;
3646 mddev->delta_disks = 0;
3647 mddev->new_level = mddev->level;
3648 mddev->new_layout = mddev->layout;
3649 mddev->new_chunk = mddev->chunk_size;
3653 static struct md_sysfs_entry md_reshape_position =
3654 __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3655 reshape_position_store);
3658 array_size_show(mddev_t *mddev, char *page)
3660 if (mddev->external_size)
3661 return sprintf(page, "%llu\n",
3662 (unsigned long long)mddev->array_sectors/2);
3664 return sprintf(page, "default\n");
3668 array_size_store(mddev_t *mddev, const char *buf, size_t len)
3672 if (strncmp(buf, "default", 7) == 0) {
3674 sectors = mddev->pers->size(mddev, 0, 0);
3676 sectors = mddev->array_sectors;
3678 mddev->external_size = 0;
3680 if (strict_blocks_to_sectors(buf, §ors) < 0)
3682 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
3685 mddev->external_size = 1;
3688 mddev->array_sectors = sectors;
3689 set_capacity(mddev->gendisk, mddev->array_sectors);
3691 struct block_device *bdev = bdget_disk(mddev->gendisk, 0);
3694 mutex_lock(&bdev->bd_inode->i_mutex);
3695 i_size_write(bdev->bd_inode,
3696 (loff_t)mddev->array_sectors << 9);
3697 mutex_unlock(&bdev->bd_inode->i_mutex);
3705 static struct md_sysfs_entry md_array_size =
3706 __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
3709 static struct attribute *md_default_attrs[] = {
3712 &md_raid_disks.attr,
3713 &md_chunk_size.attr,
3715 &md_resync_start.attr,
3717 &md_new_device.attr,
3718 &md_safe_delay.attr,
3719 &md_array_state.attr,
3720 &md_reshape_position.attr,
3721 &md_array_size.attr,
3725 static struct attribute *md_redundancy_attrs[] = {
3727 &md_mismatches.attr,
3730 &md_sync_speed.attr,
3731 &md_sync_force_parallel.attr,
3732 &md_sync_completed.attr,
3735 &md_suspend_lo.attr,
3736 &md_suspend_hi.attr,
3741 static struct attribute_group md_redundancy_group = {
3743 .attrs = md_redundancy_attrs,
3748 md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3750 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3751 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
3756 rv = mddev_lock(mddev);
3758 rv = entry->show(mddev, page);
3759 mddev_unlock(mddev);
3765 md_attr_store(struct kobject *kobj, struct attribute *attr,
3766 const char *page, size_t length)
3768 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3769 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
3774 if (!capable(CAP_SYS_ADMIN))
3776 rv = mddev_lock(mddev);
3777 if (mddev->hold_active == UNTIL_IOCTL)
3778 mddev->hold_active = 0;
3780 rv = entry->store(mddev, page, length);
3781 mddev_unlock(mddev);
3786 static void md_free(struct kobject *ko)
3788 mddev_t *mddev = container_of(ko, mddev_t, kobj);
3790 if (mddev->sysfs_state)
3791 sysfs_put(mddev->sysfs_state);
3793 if (mddev->gendisk) {
3794 del_gendisk(mddev->gendisk);
3795 put_disk(mddev->gendisk);
3798 blk_cleanup_queue(mddev->queue);
3803 static struct sysfs_ops md_sysfs_ops = {
3804 .show = md_attr_show,
3805 .store = md_attr_store,
3807 static struct kobj_type md_ktype = {
3809 .sysfs_ops = &md_sysfs_ops,
3810 .default_attrs = md_default_attrs,
3815 static void mddev_delayed_delete(struct work_struct *ws)
3817 mddev_t *mddev = container_of(ws, mddev_t, del_work);
3819 if (mddev->private == &md_redundancy_group) {
3820 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
3821 if (mddev->sysfs_action)
3822 sysfs_put(mddev->sysfs_action);
3823 mddev->sysfs_action = NULL;
3824 mddev->private = NULL;
3826 kobject_del(&mddev->kobj);
3827 kobject_put(&mddev->kobj);
3830 static int md_alloc(dev_t dev, char *name)
3832 static DEFINE_MUTEX(disks_mutex);
3833 mddev_t *mddev = mddev_find(dev);
3834 struct gendisk *disk;
3843 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
3844 shift = partitioned ? MdpMinorShift : 0;
3845 unit = MINOR(mddev->unit) >> shift;
3847 /* wait for any previous instance if this device
3848 * to be completed removed (mddev_delayed_delete).
3850 flush_scheduled_work();
3852 mutex_lock(&disks_mutex);
3853 if (mddev->gendisk) {
3854 mutex_unlock(&disks_mutex);
3860 /* Need to ensure that 'name' is not a duplicate.
3863 spin_lock(&all_mddevs_lock);
3865 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
3866 if (mddev2->gendisk &&
3867 strcmp(mddev2->gendisk->disk_name, name) == 0) {
3868 spin_unlock(&all_mddevs_lock);
3871 spin_unlock(&all_mddevs_lock);
3874 mddev->queue = blk_alloc_queue(GFP_KERNEL);
3875 if (!mddev->queue) {
3876 mutex_unlock(&disks_mutex);
3880 mddev->queue->queuedata = mddev;
3882 /* Can be unlocked because the queue is new: no concurrency */
3883 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue);
3885 blk_queue_make_request(mddev->queue, md_make_request);
3887 disk = alloc_disk(1 << shift);
3889 mutex_unlock(&disks_mutex);
3890 blk_cleanup_queue(mddev->queue);
3891 mddev->queue = NULL;
3895 disk->major = MAJOR(mddev->unit);
3896 disk->first_minor = unit << shift;
3898 strcpy(disk->disk_name, name);
3899 else if (partitioned)
3900 sprintf(disk->disk_name, "md_d%d", unit);
3902 sprintf(disk->disk_name, "md%d", unit);
3903 disk->fops = &md_fops;
3904 disk->private_data = mddev;
3905 disk->queue = mddev->queue;
3906 /* Allow extended partitions. This makes the
3907 * 'mdp' device redundant, but we can't really
3910 disk->flags |= GENHD_FL_EXT_DEVT;
3912 mddev->gendisk = disk;
3913 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
3914 &disk_to_dev(disk)->kobj, "%s", "md");
3915 mutex_unlock(&disks_mutex);
3917 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
3920 kobject_uevent(&mddev->kobj, KOBJ_ADD);
3921 mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state");
3927 static struct kobject *md_probe(dev_t dev, int *part, void *data)
3929 md_alloc(dev, NULL);
3933 static int add_named_array(const char *val, struct kernel_param *kp)
3935 /* val must be "md_*" where * is not all digits.
3936 * We allocate an array with a large free minor number, and
3937 * set the name to val. val must not already be an active name.
3939 int len = strlen(val);
3940 char buf[DISK_NAME_LEN];
3942 while (len && val[len-1] == '\n')
3944 if (len >= DISK_NAME_LEN)
3946 strlcpy(buf, val, len+1);
3947 if (strncmp(buf, "md_", 3) != 0)
3949 return md_alloc(0, buf);
3952 static void md_safemode_timeout(unsigned long data)
3954 mddev_t *mddev = (mddev_t *) data;
3956 if (!atomic_read(&mddev->writes_pending)) {
3957 mddev->safemode = 1;
3958 if (mddev->external)
3959 sysfs_notify_dirent(mddev->sysfs_state);
3961 md_wakeup_thread(mddev->thread);
3964 static int start_dirty_degraded;
3966 static int do_md_run(mddev_t * mddev)
3971 struct gendisk *disk;
3972 struct mdk_personality *pers;
3973 char b[BDEVNAME_SIZE];
3975 if (list_empty(&mddev->disks))
3976 /* cannot run an array with no devices.. */
3983 * Analyze all RAID superblock(s)
3985 if (!mddev->raid_disks) {
3986 if (!mddev->persistent)
3991 chunk_size = mddev->chunk_size;
3994 if (chunk_size > MAX_CHUNK_SIZE) {
3995 printk(KERN_ERR "too big chunk_size: %d > %d\n",
3996 chunk_size, MAX_CHUNK_SIZE);
4000 * chunk-size has to be a power of 2
4002 if ( (1 << ffz(~chunk_size)) != chunk_size) {
4003 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
4007 /* devices must have minimum size of one chunk */
4008 list_for_each_entry(rdev, &mddev->disks, same_set) {
4009 if (test_bit(Faulty, &rdev->flags))
4011 if (rdev->sectors < chunk_size / 512) {
4013 "md: Dev %s smaller than chunk_size:"
4015 bdevname(rdev->bdev,b),
4016 (unsigned long long)rdev->sectors,
4023 if (mddev->level != LEVEL_NONE)
4024 request_module("md-level-%d", mddev->level);
4025 else if (mddev->clevel[0])
4026 request_module("md-%s", mddev->clevel);
4029 * Drop all container device buffers, from now on
4030 * the only valid external interface is through the md
4033 list_for_each_entry(rdev, &mddev->disks, same_set) {
4034 if (test_bit(Faulty, &rdev->flags))
4036 sync_blockdev(rdev->bdev);
4037 invalidate_bdev(rdev->bdev);
4039 /* perform some consistency tests on the device.
4040 * We don't want the data to overlap the metadata,
4041 * Internal Bitmap issues have been handled elsewhere.
4043 if (rdev->data_offset < rdev->sb_start) {
4044 if (mddev->dev_sectors &&
4045 rdev->data_offset + mddev->dev_sectors
4047 printk("md: %s: data overlaps metadata\n",
4052 if (rdev->sb_start + rdev->sb_size/512
4053 > rdev->data_offset) {
4054 printk("md: %s: metadata overlaps data\n",
4059 sysfs_notify_dirent(rdev->sysfs_state);
4062 md_probe(mddev->unit, NULL, NULL);
4063 disk = mddev->gendisk;
4067 spin_lock(&pers_lock);
4068 pers = find_pers(mddev->level, mddev->clevel);
4069 if (!pers || !try_module_get(pers->owner)) {
4070 spin_unlock(&pers_lock);
4071 if (mddev->level != LEVEL_NONE)
4072 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4075 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4080 spin_unlock(&pers_lock);
4081 if (mddev->level != pers->level) {
4082 mddev->level = pers->level;
4083 mddev->new_level = pers->level;
4085 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
4087 if (pers->level >= 4 && pers->level <= 6)
4088 /* Cannot support integrity (yet) */
4089 blk_integrity_unregister(mddev->gendisk);
4091 if (mddev->reshape_position != MaxSector &&
4092 pers->start_reshape == NULL) {
4093 /* This personality cannot handle reshaping... */
4095 module_put(pers->owner);
4099 if (pers->sync_request) {
4100 /* Warn if this is a potentially silly
4103 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4107 list_for_each_entry(rdev, &mddev->disks, same_set)
4108 list_for_each_entry(rdev2, &mddev->disks, same_set) {
4110 rdev->bdev->bd_contains ==
4111 rdev2->bdev->bd_contains) {
4113 "%s: WARNING: %s appears to be"
4114 " on the same physical disk as"
4117 bdevname(rdev->bdev,b),
4118 bdevname(rdev2->bdev,b2));
4125 "True protection against single-disk"
4126 " failure might be compromised.\n");
4129 mddev->recovery = 0;
4130 /* may be over-ridden by personality */
4131 mddev->resync_max_sectors = mddev->dev_sectors;
4133 mddev->barriers_work = 1;
4134 mddev->ok_start_degraded = start_dirty_degraded;
4137 mddev->ro = 2; /* read-only, but switch on first write */
4139 err = mddev->pers->run(mddev);
4141 printk(KERN_ERR "md: pers->run() failed ...\n");
4142 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4143 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4144 " but 'external_size' not in effect?\n", __func__);
4146 "md: invalid array_size %llu > default size %llu\n",
4147 (unsigned long long)mddev->array_sectors / 2,
4148 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4150 mddev->pers->stop(mddev);
4152 if (err == 0 && mddev->pers->sync_request) {
4153 err = bitmap_create(mddev);
4155 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4156 mdname(mddev), err);
4157 mddev->pers->stop(mddev);
4161 module_put(mddev->pers->owner);
4163 bitmap_destroy(mddev);
4166 if (mddev->pers->sync_request) {
4167 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
4169 "md: cannot register extra attributes for %s\n",
4171 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
4172 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
4175 atomic_set(&mddev->writes_pending,0);
4176 mddev->safemode = 0;
4177 mddev->safemode_timer.function = md_safemode_timeout;
4178 mddev->safemode_timer.data = (unsigned long) mddev;
4179 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
4182 list_for_each_entry(rdev, &mddev->disks, same_set)
4183 if (rdev->raid_disk >= 0) {
4185 sprintf(nm, "rd%d", rdev->raid_disk);
4186 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
4187 printk("md: cannot register %s for %s\n",
4191 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4194 md_update_sb(mddev, 0);
4196 set_capacity(disk, mddev->array_sectors);
4198 /* If there is a partially-recovered drive we need to
4199 * start recovery here. If we leave it to md_check_recovery,
4200 * it will remove the drives and not do the right thing
4202 if (mddev->degraded && !mddev->sync_thread) {
4204 list_for_each_entry(rdev, &mddev->disks, same_set)
4205 if (rdev->raid_disk >= 0 &&
4206 !test_bit(In_sync, &rdev->flags) &&
4207 !test_bit(Faulty, &rdev->flags))
4208 /* complete an interrupted recovery */
4210 if (spares && mddev->pers->sync_request) {
4211 mddev->recovery = 0;
4212 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4213 mddev->sync_thread = md_register_thread(md_do_sync,
4216 if (!mddev->sync_thread) {
4217 printk(KERN_ERR "%s: could not start resync"
4220 /* leave the spares where they are, it shouldn't hurt */
4221 mddev->recovery = 0;
4225 md_wakeup_thread(mddev->thread);
4226 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
4229 md_new_event(mddev);
4230 sysfs_notify_dirent(mddev->sysfs_state);
4231 if (mddev->sysfs_action)
4232 sysfs_notify_dirent(mddev->sysfs_action);
4233 sysfs_notify(&mddev->kobj, NULL, "degraded");
4234 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4238 static int restart_array(mddev_t *mddev)
4240 struct gendisk *disk = mddev->gendisk;
4242 /* Complain if it has no devices */
4243 if (list_empty(&mddev->disks))
4249 mddev->safemode = 0;
4251 set_disk_ro(disk, 0);
4252 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4254 /* Kick recovery or resync if necessary */
4255 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4256 md_wakeup_thread(mddev->thread);
4257 md_wakeup_thread(mddev->sync_thread);
4258 sysfs_notify_dirent(mddev->sysfs_state);
4262 /* similar to deny_write_access, but accounts for our holding a reference
4263 * to the file ourselves */
4264 static int deny_bitmap_write_access(struct file * file)
4266 struct inode *inode = file->f_mapping->host;
4268 spin_lock(&inode->i_lock);
4269 if (atomic_read(&inode->i_writecount) > 1) {
4270 spin_unlock(&inode->i_lock);
4273 atomic_set(&inode->i_writecount, -1);
4274 spin_unlock(&inode->i_lock);
4279 static void restore_bitmap_write_access(struct file *file)
4281 struct inode *inode = file->f_mapping->host;
4283 spin_lock(&inode->i_lock);
4284 atomic_set(&inode->i_writecount, 1);
4285 spin_unlock(&inode->i_lock);
4289 * 0 - completely stop and dis-assemble array
4290 * 1 - switch to readonly
4291 * 2 - stop but do not disassemble array
4293 static int do_md_stop(mddev_t * mddev, int mode, int is_open)
4296 struct gendisk *disk = mddev->gendisk;
4299 if (atomic_read(&mddev->openers) > is_open) {
4300 printk("md: %s still in use.\n",mdname(mddev));
4306 if (mddev->sync_thread) {
4307 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4308 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4309 md_unregister_thread(mddev->sync_thread);
4310 mddev->sync_thread = NULL;
4313 del_timer_sync(&mddev->safemode_timer);
4316 case 1: /* readonly */
4322 case 0: /* disassemble */
4324 bitmap_flush(mddev);
4325 md_super_wait(mddev);
4327 set_disk_ro(disk, 0);
4329 mddev->pers->stop(mddev);
4330 mddev->queue->merge_bvec_fn = NULL;
4331 mddev->queue->unplug_fn = NULL;
4332 mddev->queue->backing_dev_info.congested_fn = NULL;
4333 module_put(mddev->pers->owner);
4334 if (mddev->pers->sync_request)
4335 mddev->private = &md_redundancy_group;
4337 /* tell userspace to handle 'inactive' */
4338 sysfs_notify_dirent(mddev->sysfs_state);
4340 list_for_each_entry(rdev, &mddev->disks, same_set)
4341 if (rdev->raid_disk >= 0) {
4343 sprintf(nm, "rd%d", rdev->raid_disk);
4344 sysfs_remove_link(&mddev->kobj, nm);
4347 set_capacity(disk, 0);
4353 if (!mddev->in_sync || mddev->flags) {
4354 /* mark array as shutdown cleanly */
4356 md_update_sb(mddev, 1);
4359 set_disk_ro(disk, 1);
4360 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4364 * Free resources if final stop
4368 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4370 bitmap_destroy(mddev);
4371 if (mddev->bitmap_file) {
4372 restore_bitmap_write_access(mddev->bitmap_file);
4373 fput(mddev->bitmap_file);
4374 mddev->bitmap_file = NULL;
4376 mddev->bitmap_offset = 0;
4378 /* make sure all md_delayed_delete calls have finished */
4379 flush_scheduled_work();
4381 export_array(mddev);
4383 mddev->array_sectors = 0;
4384 mddev->external_size = 0;
4385 mddev->dev_sectors = 0;
4386 mddev->raid_disks = 0;
4387 mddev->recovery_cp = 0;
4388 mddev->resync_min = 0;
4389 mddev->resync_max = MaxSector;
4390 mddev->reshape_position = MaxSector;
4391 mddev->external = 0;
4392 mddev->persistent = 0;
4393 mddev->level = LEVEL_NONE;
4394 mddev->clevel[0] = 0;
4397 mddev->metadata_type[0] = 0;
4398 mddev->chunk_size = 0;
4399 mddev->ctime = mddev->utime = 0;
4401 mddev->max_disks = 0;
4403 mddev->delta_disks = 0;
4404 mddev->new_level = LEVEL_NONE;
4405 mddev->new_layout = 0;
4406 mddev->new_chunk = 0;
4407 mddev->curr_resync = 0;
4408 mddev->resync_mismatches = 0;
4409 mddev->suspend_lo = mddev->suspend_hi = 0;
4410 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4411 mddev->recovery = 0;
4414 mddev->degraded = 0;
4415 mddev->barriers_work = 0;
4416 mddev->safemode = 0;
4417 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4418 if (mddev->hold_active == UNTIL_STOP)
4419 mddev->hold_active = 0;
4421 } else if (mddev->pers)
4422 printk(KERN_INFO "md: %s switched to read-only mode.\n",
4425 blk_integrity_unregister(disk);
4426 md_new_event(mddev);
4427 sysfs_notify_dirent(mddev->sysfs_state);
4433 static void autorun_array(mddev_t *mddev)
4438 if (list_empty(&mddev->disks))
4441 printk(KERN_INFO "md: running: ");
4443 list_for_each_entry(rdev, &mddev->disks, same_set) {
4444 char b[BDEVNAME_SIZE];
4445 printk("<%s>", bdevname(rdev->bdev,b));
4449 err = do_md_run(mddev);
4451 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
4452 do_md_stop(mddev, 0, 0);
4457 * lets try to run arrays based on all disks that have arrived
4458 * until now. (those are in pending_raid_disks)
4460 * the method: pick the first pending disk, collect all disks with
4461 * the same UUID, remove all from the pending list and put them into
4462 * the 'same_array' list. Then order this list based on superblock
4463 * update time (freshest comes first), kick out 'old' disks and
4464 * compare superblocks. If everything's fine then run it.
4466 * If "unit" is allocated, then bump its reference count
4468 static void autorun_devices(int part)
4470 mdk_rdev_t *rdev0, *rdev, *tmp;
4472 char b[BDEVNAME_SIZE];
4474 printk(KERN_INFO "md: autorun ...\n");
4475 while (!list_empty(&pending_raid_disks)) {
4478 LIST_HEAD(candidates);
4479 rdev0 = list_entry(pending_raid_disks.next,
4480 mdk_rdev_t, same_set);
4482 printk(KERN_INFO "md: considering %s ...\n",
4483 bdevname(rdev0->bdev,b));
4484 INIT_LIST_HEAD(&candidates);
4485 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
4486 if (super_90_load(rdev, rdev0, 0) >= 0) {
4487 printk(KERN_INFO "md: adding %s ...\n",
4488 bdevname(rdev->bdev,b));
4489 list_move(&rdev->same_set, &candidates);
4492 * now we have a set of devices, with all of them having
4493 * mostly sane superblocks. It's time to allocate the
4497 dev = MKDEV(mdp_major,
4498 rdev0->preferred_minor << MdpMinorShift);
4499 unit = MINOR(dev) >> MdpMinorShift;
4501 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4504 if (rdev0->preferred_minor != unit) {
4505 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4506 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4510 md_probe(dev, NULL, NULL);
4511 mddev = mddev_find(dev);
4512 if (!mddev || !mddev->gendisk) {
4516 "md: cannot allocate memory for md drive.\n");
4519 if (mddev_lock(mddev))
4520 printk(KERN_WARNING "md: %s locked, cannot run\n",
4522 else if (mddev->raid_disks || mddev->major_version
4523 || !list_empty(&mddev->disks)) {
4525 "md: %s already running, cannot run %s\n",
4526 mdname(mddev), bdevname(rdev0->bdev,b));
4527 mddev_unlock(mddev);
4529 printk(KERN_INFO "md: created %s\n", mdname(mddev));
4530 mddev->persistent = 1;
4531 rdev_for_each_list(rdev, tmp, &candidates) {
4532 list_del_init(&rdev->same_set);
4533 if (bind_rdev_to_array(rdev, mddev))
4536 autorun_array(mddev);
4537 mddev_unlock(mddev);
4539 /* on success, candidates will be empty, on error
4542 rdev_for_each_list(rdev, tmp, &candidates) {
4543 list_del_init(&rdev->same_set);
4548 printk(KERN_INFO "md: ... autorun DONE.\n");
4550 #endif /* !MODULE */
4552 static int get_version(void __user * arg)
4556 ver.major = MD_MAJOR_VERSION;
4557 ver.minor = MD_MINOR_VERSION;
4558 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4560 if (copy_to_user(arg, &ver, sizeof(ver)))
4566 static int get_array_info(mddev_t * mddev, void __user * arg)
4568 mdu_array_info_t info;
4569 int nr,working,active,failed,spare;
4572 nr=working=active=failed=spare=0;
4573 list_for_each_entry(rdev, &mddev->disks, same_set) {
4575 if (test_bit(Faulty, &rdev->flags))
4579 if (test_bit(In_sync, &rdev->flags))
4586 info.major_version = mddev->major_version;
4587 info.minor_version = mddev->minor_version;
4588 info.patch_version = MD_PATCHLEVEL_VERSION;
4589 info.ctime = mddev->ctime;
4590 info.level = mddev->level;
4591 info.size = mddev->dev_sectors / 2;
4592 if (info.size != mddev->dev_sectors / 2) /* overflow */
4595 info.raid_disks = mddev->raid_disks;
4596 info.md_minor = mddev->md_minor;
4597 info.not_persistent= !mddev->persistent;
4599 info.utime = mddev->utime;
4602 info.state = (1<<MD_SB_CLEAN);
4603 if (mddev->bitmap && mddev->bitmap_offset)
4604 info.state = (1<<MD_SB_BITMAP_PRESENT);
4605 info.active_disks = active;
4606 info.working_disks = working;
4607 info.failed_disks = failed;
4608 info.spare_disks = spare;
4610 info.layout = mddev->layout;
4611 info.chunk_size = mddev->chunk_size;
4613 if (copy_to_user(arg, &info, sizeof(info)))
4619 static int get_bitmap_file(mddev_t * mddev, void __user * arg)
4621 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4622 char *ptr, *buf = NULL;
4625 if (md_allow_write(mddev))
4626 file = kmalloc(sizeof(*file), GFP_NOIO);
4628 file = kmalloc(sizeof(*file), GFP_KERNEL);
4633 /* bitmap disabled, zero the first byte and copy out */
4634 if (!mddev->bitmap || !mddev->bitmap->file) {
4635 file->pathname[0] = '\0';
4639 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4643 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4647 strcpy(file->pathname, ptr);
4651 if (copy_to_user(arg, file, sizeof(*file)))
4659 static int get_disk_info(mddev_t * mddev, void __user * arg)
4661 mdu_disk_info_t info;
4664 if (copy_from_user(&info, arg, sizeof(info)))
4667 rdev = find_rdev_nr(mddev, info.number);
4669 info.major = MAJOR(rdev->bdev->bd_dev);
4670 info.minor = MINOR(rdev->bdev->bd_dev);
4671 info.raid_disk = rdev->raid_disk;
4673 if (test_bit(Faulty, &rdev->flags))
4674 info.state |= (1<<MD_DISK_FAULTY);
4675 else if (test_bit(In_sync, &rdev->flags)) {
4676 info.state |= (1<<MD_DISK_ACTIVE);
4677 info.state |= (1<<MD_DISK_SYNC);
4679 if (test_bit(WriteMostly, &rdev->flags))
4680 info.state |= (1<<MD_DISK_WRITEMOSTLY);
4682 info.major = info.minor = 0;
4683 info.raid_disk = -1;
4684 info.state = (1<<MD_DISK_REMOVED);
4687 if (copy_to_user(arg, &info, sizeof(info)))
4693 static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4695 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4697 dev_t dev = MKDEV(info->major,info->minor);
4699 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4702 if (!mddev->raid_disks) {
4704 /* expecting a device which has a superblock */
4705 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4708 "md: md_import_device returned %ld\n",
4710 return PTR_ERR(rdev);
4712 if (!list_empty(&mddev->disks)) {
4713 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4714 mdk_rdev_t, same_set);
4715 int err = super_types[mddev->major_version]
4716 .load_super(rdev, rdev0, mddev->minor_version);
4719 "md: %s has different UUID to %s\n",
4720 bdevname(rdev->bdev,b),
4721 bdevname(rdev0->bdev,b2));
4726 err = bind_rdev_to_array(rdev, mddev);
4733 * add_new_disk can be used once the array is assembled
4734 * to add "hot spares". They must already have a superblock
4739 if (!mddev->pers->hot_add_disk) {
4741 "%s: personality does not support diskops!\n",
4745 if (mddev->persistent)
4746 rdev = md_import_device(dev, mddev->major_version,
4747 mddev->minor_version);
4749 rdev = md_import_device(dev, -1, -1);
4752 "md: md_import_device returned %ld\n",
4754 return PTR_ERR(rdev);
4756 /* set save_raid_disk if appropriate */
4757 if (!mddev->persistent) {
4758 if (info->state & (1<<MD_DISK_SYNC) &&
4759 info->raid_disk < mddev->raid_disks)
4760 rdev->raid_disk = info->raid_disk;
4762 rdev->raid_disk = -1;
4764 super_types[mddev->major_version].
4765 validate_super(mddev, rdev);
4766 rdev->saved_raid_disk = rdev->raid_disk;
4768 clear_bit(In_sync, &rdev->flags); /* just to be sure */
4769 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4770 set_bit(WriteMostly, &rdev->flags);
4772 clear_bit(WriteMostly, &rdev->flags);
4774 rdev->raid_disk = -1;
4775 err = bind_rdev_to_array(rdev, mddev);
4776 if (!err && !mddev->pers->hot_remove_disk) {
4777 /* If there is hot_add_disk but no hot_remove_disk
4778 * then added disks for geometry changes,
4779 * and should be added immediately.
4781 super_types[mddev->major_version].
4782 validate_super(mddev, rdev);
4783 err = mddev->pers->hot_add_disk(mddev, rdev);
4785 unbind_rdev_from_array(rdev);
4790 sysfs_notify_dirent(rdev->sysfs_state);
4792 md_update_sb(mddev, 1);
4793 if (mddev->degraded)
4794 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4795 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4796 md_wakeup_thread(mddev->thread);
4800 /* otherwise, add_new_disk is only allowed
4801 * for major_version==0 superblocks
4803 if (mddev->major_version != 0) {
4804 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
4809 if (!(info->state & (1<<MD_DISK_FAULTY))) {
4811 rdev = md_import_device(dev, -1, 0);
4814 "md: error, md_import_device() returned %ld\n",
4816 return PTR_ERR(rdev);
4818 rdev->desc_nr = info->number;
4819 if (info->raid_disk < mddev->raid_disks)
4820 rdev->raid_disk = info->raid_disk;
4822 rdev->raid_disk = -1;
4824 if (rdev->raid_disk < mddev->raid_disks)
4825 if (info->state & (1<<MD_DISK_SYNC))
4826 set_bit(In_sync, &rdev->flags);
4828 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4829 set_bit(WriteMostly, &rdev->flags);
4831 if (!mddev->persistent) {
4832 printk(KERN_INFO "md: nonpersistent superblock ...\n");
4833 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
4835 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
4836 rdev->sectors = calc_num_sectors(rdev, mddev->chunk_size);
4838 err = bind_rdev_to_array(rdev, mddev);
4848 static int hot_remove_disk(mddev_t * mddev, dev_t dev)
4850 char b[BDEVNAME_SIZE];
4853 rdev = find_rdev(mddev, dev);
4857 if (rdev->raid_disk >= 0)
4860 kick_rdev_from_array(rdev);
4861 md_update_sb(mddev, 1);
4862 md_new_event(mddev);
4866 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
4867 bdevname(rdev->bdev,b), mdname(mddev));
4871 static int hot_add_disk(mddev_t * mddev, dev_t dev)
4873 char b[BDEVNAME_SIZE];
4880 if (mddev->major_version != 0) {
4881 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
4882 " version-0 superblocks.\n",
4886 if (!mddev->pers->hot_add_disk) {
4888 "%s: personality does not support diskops!\n",
4893 rdev = md_import_device(dev, -1, 0);
4896 "md: error, md_import_device() returned %ld\n",
4901 if (mddev->persistent)
4902 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
4904 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
4906 rdev->sectors = calc_num_sectors(rdev, mddev->chunk_size);
4908 if (test_bit(Faulty, &rdev->flags)) {
4910 "md: can not hot-add faulty %s disk to %s!\n",
4911 bdevname(rdev->bdev,b), mdname(mddev));
4915 clear_bit(In_sync, &rdev->flags);
4917 rdev->saved_raid_disk = -1;
4918 err = bind_rdev_to_array(rdev, mddev);
4923 * The rest should better be atomic, we can have disk failures
4924 * noticed in interrupt contexts ...
4927 rdev->raid_disk = -1;
4929 md_update_sb(mddev, 1);
4932 * Kick recovery, maybe this spare has to be added to the
4933 * array immediately.
4935 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4936 md_wakeup_thread(mddev->thread);
4937 md_new_event(mddev);
4945 static int set_bitmap_file(mddev_t *mddev, int fd)
4950 if (!mddev->pers->quiesce)
4952 if (mddev->recovery || mddev->sync_thread)
4954 /* we should be able to change the bitmap.. */
4960 return -EEXIST; /* cannot add when bitmap is present */
4961 mddev->bitmap_file = fget(fd);
4963 if (mddev->bitmap_file == NULL) {
4964 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
4969 err = deny_bitmap_write_access(mddev->bitmap_file);
4971 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
4973 fput(mddev->bitmap_file);
4974 mddev->bitmap_file = NULL;
4977 mddev->bitmap_offset = 0; /* file overrides offset */
4978 } else if (mddev->bitmap == NULL)
4979 return -ENOENT; /* cannot remove what isn't there */
4982 mddev->pers->quiesce(mddev, 1);
4984 err = bitmap_create(mddev);
4985 if (fd < 0 || err) {
4986 bitmap_destroy(mddev);
4987 fd = -1; /* make sure to put the file */
4989 mddev->pers->quiesce(mddev, 0);
4992 if (mddev->bitmap_file) {
4993 restore_bitmap_write_access(mddev->bitmap_file);
4994 fput(mddev->bitmap_file);
4996 mddev->bitmap_file = NULL;
5003 * set_array_info is used two different ways
5004 * The original usage is when creating a new array.
5005 * In this usage, raid_disks is > 0 and it together with
5006 * level, size, not_persistent,layout,chunksize determine the
5007 * shape of the array.
5008 * This will always create an array with a type-0.90.0 superblock.
5009 * The newer usage is when assembling an array.
5010 * In this case raid_disks will be 0, and the major_version field is
5011 * use to determine which style super-blocks are to be found on the devices.
5012 * The minor and patch _version numbers are also kept incase the
5013 * super_block handler wishes to interpret them.
5015 static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5018 if (info->raid_disks == 0) {
5019 /* just setting version number for superblock loading */
5020 if (info->major_version < 0 ||
5021 info->major_version >= ARRAY_SIZE(super_types) ||
5022 super_types[info->major_version].name == NULL) {
5023 /* maybe try to auto-load a module? */
5025 "md: superblock version %d not known\n",
5026 info->major_version);
5029 mddev->major_version = info->major_version;
5030 mddev->minor_version = info->minor_version;
5031 mddev->patch_version = info->patch_version;
5032 mddev->persistent = !info->not_persistent;
5035 mddev->major_version = MD_MAJOR_VERSION;
5036 mddev->minor_version = MD_MINOR_VERSION;
5037 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5038 mddev->ctime = get_seconds();
5040 mddev->level = info->level;
5041 mddev->clevel[0] = 0;
5042 mddev->dev_sectors = 2 * (sector_t)info->size;
5043 mddev->raid_disks = info->raid_disks;
5044 /* don't set md_minor, it is determined by which /dev/md* was
5047 if (info->state & (1<<MD_SB_CLEAN))
5048 mddev->recovery_cp = MaxSector;
5050 mddev->recovery_cp = 0;
5051 mddev->persistent = ! info->not_persistent;
5052 mddev->external = 0;
5054 mddev->layout = info->layout;
5055 mddev->chunk_size = info->chunk_size;
5057 mddev->max_disks = MD_SB_DISKS;
5059 if (mddev->persistent)
5061 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5063 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
5064 mddev->bitmap_offset = 0;
5066 mddev->reshape_position = MaxSector;
5069 * Generate a 128 bit UUID
5071 get_random_bytes(mddev->uuid, 16);
5073 mddev->new_level = mddev->level;
5074 mddev->new_chunk = mddev->chunk_size;
5075 mddev->new_layout = mddev->layout;
5076 mddev->delta_disks = 0;
5081 void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5083 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5085 if (mddev->external_size)
5088 mddev->array_sectors = array_sectors;
5090 EXPORT_SYMBOL(md_set_array_sectors);
5092 static int update_size(mddev_t *mddev, sector_t num_sectors)
5096 int fit = (num_sectors == 0);
5098 if (mddev->pers->resize == NULL)
5100 /* The "num_sectors" is the number of sectors of each device that
5101 * is used. This can only make sense for arrays with redundancy.
5102 * linear and raid0 always use whatever space is available. We can only
5103 * consider changing this number if no resync or reconstruction is
5104 * happening, and if the new size is acceptable. It must fit before the
5105 * sb_start or, if that is <data_offset, it must fit before the size
5106 * of each device. If num_sectors is zero, we find the largest size
5110 if (mddev->sync_thread)
5113 /* Sorry, cannot grow a bitmap yet, just remove it,
5117 list_for_each_entry(rdev, &mddev->disks, same_set) {
5118 sector_t avail = rdev->sectors;
5120 if (fit && (num_sectors == 0 || num_sectors > avail))
5121 num_sectors = avail;
5122 if (avail < num_sectors)
5125 rv = mddev->pers->resize(mddev, num_sectors);
5127 struct block_device *bdev;
5129 bdev = bdget_disk(mddev->gendisk, 0);
5131 mutex_lock(&bdev->bd_inode->i_mutex);
5132 i_size_write(bdev->bd_inode,
5133 (loff_t)mddev->array_sectors << 9);
5134 mutex_unlock(&bdev->bd_inode->i_mutex);
5141 static int update_raid_disks(mddev_t *mddev, int raid_disks)
5144 /* change the number of raid disks */
5145 if (mddev->pers->check_reshape == NULL)
5147 if (raid_disks <= 0 ||
5148 raid_disks >= mddev->max_disks)
5150 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
5152 mddev->delta_disks = raid_disks - mddev->raid_disks;
5154 rv = mddev->pers->check_reshape(mddev);
5160 * update_array_info is used to change the configuration of an
5162 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5163 * fields in the info are checked against the array.
5164 * Any differences that cannot be handled will cause an error.
5165 * Normally, only one change can be managed at a time.
5167 static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5173 /* calculate expected state,ignoring low bits */
5174 if (mddev->bitmap && mddev->bitmap_offset)
5175 state |= (1 << MD_SB_BITMAP_PRESENT);
5177 if (mddev->major_version != info->major_version ||
5178 mddev->minor_version != info->minor_version ||
5179 /* mddev->patch_version != info->patch_version || */
5180 mddev->ctime != info->ctime ||
5181 mddev->level != info->level ||
5182 /* mddev->layout != info->layout || */
5183 !mddev->persistent != info->not_persistent||
5184 mddev->chunk_size != info->chunk_size ||
5185 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5186 ((state^info->state) & 0xfffffe00)
5189 /* Check there is only one change */
5190 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5192 if (mddev->raid_disks != info->raid_disks)
5194 if (mddev->layout != info->layout)
5196 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5203 if (mddev->layout != info->layout) {
5205 * we don't need to do anything at the md level, the
5206 * personality will take care of it all.
5208 if (mddev->pers->reconfig == NULL)
5211 return mddev->pers->reconfig(mddev, info->layout, -1);
5213 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5214 rv = update_size(mddev, (sector_t)info->size * 2);
5216 if (mddev->raid_disks != info->raid_disks)
5217 rv = update_raid_disks(mddev, info->raid_disks);
5219 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5220 if (mddev->pers->quiesce == NULL)
5222 if (mddev->recovery || mddev->sync_thread)
5224 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5225 /* add the bitmap */
5228 if (mddev->default_bitmap_offset == 0)
5230 mddev->bitmap_offset = mddev->default_bitmap_offset;
5231 mddev->pers->quiesce(mddev, 1);
5232 rv = bitmap_create(mddev);
5234 bitmap_destroy(mddev);
5235 mddev->pers->quiesce(mddev, 0);
5237 /* remove the bitmap */
5240 if (mddev->bitmap->file)
5242 mddev->pers->quiesce(mddev, 1);
5243 bitmap_destroy(mddev);
5244 mddev->pers->quiesce(mddev, 0);
5245 mddev->bitmap_offset = 0;
5248 md_update_sb(mddev, 1);
5252 static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5256 if (mddev->pers == NULL)
5259 rdev = find_rdev(mddev, dev);
5263 md_error(mddev, rdev);
5268 * We have a problem here : there is no easy way to give a CHS
5269 * virtual geometry. We currently pretend that we have a 2 heads
5270 * 4 sectors (with a BIG number of cylinders...). This drives
5271 * dosfs just mad... ;-)
5273 static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5275 mddev_t *mddev = bdev->bd_disk->private_data;
5279 geo->cylinders = get_capacity(mddev->gendisk) / 8;
5283 static int md_ioctl(struct block_device *bdev, fmode_t mode,
5284 unsigned int cmd, unsigned long arg)
5287 void __user *argp = (void __user *)arg;
5288 mddev_t *mddev = NULL;
5290 if (!capable(CAP_SYS_ADMIN))
5294 * Commands dealing with the RAID driver but not any
5300 err = get_version(argp);
5303 case PRINT_RAID_DEBUG:
5311 autostart_arrays(arg);
5318 * Commands creating/starting a new array:
5321 mddev = bdev->bd_disk->private_data;
5328 err = mddev_lock(mddev);
5331 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5338 case SET_ARRAY_INFO:
5340 mdu_array_info_t info;
5342 memset(&info, 0, sizeof(info));
5343 else if (copy_from_user(&info, argp, sizeof(info))) {
5348 err = update_array_info(mddev, &info);
5350 printk(KERN_WARNING "md: couldn't update"
5351 " array info. %d\n", err);
5356 if (!list_empty(&mddev->disks)) {
5358 "md: array %s already has disks!\n",
5363 if (mddev->raid_disks) {
5365 "md: array %s already initialised!\n",
5370 err = set_array_info(mddev, &info);
5372 printk(KERN_WARNING "md: couldn't set"
5373 " array info. %d\n", err);
5383 * Commands querying/configuring an existing array:
5385 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
5386 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
5387 if ((!mddev->raid_disks && !mddev->external)
5388 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5389 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5390 && cmd != GET_BITMAP_FILE) {
5396 * Commands even a read-only array can execute:
5400 case GET_ARRAY_INFO:
5401 err = get_array_info(mddev, argp);
5404 case GET_BITMAP_FILE:
5405 err = get_bitmap_file(mddev, argp);
5409 err = get_disk_info(mddev, argp);
5412 case RESTART_ARRAY_RW:
5413 err = restart_array(mddev);
5417 err = do_md_stop(mddev, 0, 1);
5421 err = do_md_stop(mddev, 1, 1);
5427 * The remaining ioctls are changing the state of the
5428 * superblock, so we do not allow them on read-only arrays.
5429 * However non-MD ioctls (e.g. get-size) will still come through
5430 * here and hit the 'default' below, so only disallow
5431 * 'md' ioctls, and switch to rw mode if started auto-readonly.
5433 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
5434 if (mddev->ro == 2) {
5436 sysfs_notify_dirent(mddev->sysfs_state);
5437 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5438 md_wakeup_thread(mddev->thread);
5449 mdu_disk_info_t info;
5450 if (copy_from_user(&info, argp, sizeof(info)))
5453 err = add_new_disk(mddev, &info);
5457 case HOT_REMOVE_DISK:
5458 err = hot_remove_disk(mddev, new_decode_dev(arg));
5462 err = hot_add_disk(mddev, new_decode_dev(arg));
5465 case SET_DISK_FAULTY:
5466 err = set_disk_faulty(mddev, new_decode_dev(arg));
5470 err = do_md_run(mddev);
5473 case SET_BITMAP_FILE:
5474 err = set_bitmap_file(mddev, (int)arg);
5484 if (mddev->hold_active == UNTIL_IOCTL &&
5486 mddev->hold_active = 0;
5487 mddev_unlock(mddev);
5497 static int md_open(struct block_device *bdev, fmode_t mode)
5500 * Succeed if we can lock the mddev, which confirms that
5501 * it isn't being stopped right now.
5503 mddev_t *mddev = mddev_find(bdev->bd_dev);
5506 if (mddev->gendisk != bdev->bd_disk) {
5507 /* we are racing with mddev_put which is discarding this
5511 /* Wait until bdev->bd_disk is definitely gone */
5512 flush_scheduled_work();
5513 /* Then retry the open from the top */
5514 return -ERESTARTSYS;
5516 BUG_ON(mddev != bdev->bd_disk->private_data);
5518 if ((err = mutex_lock_interruptible_nested(&mddev->reconfig_mutex, 1)))
5522 atomic_inc(&mddev->openers);
5523 mddev_unlock(mddev);
5525 check_disk_change(bdev);
5530 static int md_release(struct gendisk *disk, fmode_t mode)
5532 mddev_t *mddev = disk->private_data;
5535 atomic_dec(&mddev->openers);
5541 static int md_media_changed(struct gendisk *disk)
5543 mddev_t *mddev = disk->private_data;
5545 return mddev->changed;
5548 static int md_revalidate(struct gendisk *disk)
5550 mddev_t *mddev = disk->private_data;
5555 static struct block_device_operations md_fops =
5557 .owner = THIS_MODULE,
5559 .release = md_release,
5560 .locked_ioctl = md_ioctl,
5561 .getgeo = md_getgeo,
5562 .media_changed = md_media_changed,
5563 .revalidate_disk= md_revalidate,
5566 static int md_thread(void * arg)
5568 mdk_thread_t *thread = arg;
5571 * md_thread is a 'system-thread', it's priority should be very
5572 * high. We avoid resource deadlocks individually in each
5573 * raid personality. (RAID5 does preallocation) We also use RR and
5574 * the very same RT priority as kswapd, thus we will never get
5575 * into a priority inversion deadlock.
5577 * we definitely have to have equal or higher priority than
5578 * bdflush, otherwise bdflush will deadlock if there are too
5579 * many dirty RAID5 blocks.
5582 allow_signal(SIGKILL);
5583 while (!kthread_should_stop()) {
5585 /* We need to wait INTERRUPTIBLE so that
5586 * we don't add to the load-average.
5587 * That means we need to be sure no signals are
5590 if (signal_pending(current))
5591 flush_signals(current);
5593 wait_event_interruptible_timeout
5595 test_bit(THREAD_WAKEUP, &thread->flags)
5596 || kthread_should_stop(),
5599 clear_bit(THREAD_WAKEUP, &thread->flags);
5601 thread->run(thread->mddev);
5607 void md_wakeup_thread(mdk_thread_t *thread)
5610 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5611 set_bit(THREAD_WAKEUP, &thread->flags);
5612 wake_up(&thread->wqueue);
5616 mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5619 mdk_thread_t *thread;
5621 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
5625 init_waitqueue_head(&thread->wqueue);
5628 thread->mddev = mddev;
5629 thread->timeout = MAX_SCHEDULE_TIMEOUT;
5630 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
5631 if (IS_ERR(thread->tsk)) {
5638 void md_unregister_thread(mdk_thread_t *thread)
5642 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
5644 kthread_stop(thread->tsk);
5648 void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
5655 if (!rdev || test_bit(Faulty, &rdev->flags))
5658 if (mddev->external)
5659 set_bit(Blocked, &rdev->flags);
5661 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5663 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5664 __builtin_return_address(0),__builtin_return_address(1),
5665 __builtin_return_address(2),__builtin_return_address(3));
5669 if (!mddev->pers->error_handler)
5671 mddev->pers->error_handler(mddev,rdev);
5672 if (mddev->degraded)
5673 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
5674 set_bit(StateChanged, &rdev->flags);
5675 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5676 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5677 md_wakeup_thread(mddev->thread);
5678 md_new_event_inintr(mddev);
5681 /* seq_file implementation /proc/mdstat */
5683 static void status_unused(struct seq_file *seq)
5688 seq_printf(seq, "unused devices: ");
5690 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
5691 char b[BDEVNAME_SIZE];
5693 seq_printf(seq, "%s ",
5694 bdevname(rdev->bdev,b));
5697 seq_printf(seq, "<none>");
5699 seq_printf(seq, "\n");
5703 static void status_resync(struct seq_file *seq, mddev_t * mddev)
5705 sector_t max_sectors, resync, res;
5706 unsigned long dt, db;
5709 unsigned int per_milli;
5711 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
5713 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
5714 max_sectors = mddev->resync_max_sectors;
5716 max_sectors = mddev->dev_sectors;
5719 * Should not happen.
5725 /* Pick 'scale' such that (resync>>scale)*1000 will fit
5726 * in a sector_t, and (max_sectors>>scale) will fit in a
5727 * u32, as those are the requirements for sector_div.
5728 * Thus 'scale' must be at least 10
5731 if (sizeof(sector_t) > sizeof(unsigned long)) {
5732 while ( max_sectors/2 > (1ULL<<(scale+32)))
5735 res = (resync>>scale)*1000;
5736 sector_div(res, (u32)((max_sectors>>scale)+1));
5740 int i, x = per_milli/50, y = 20-x;
5741 seq_printf(seq, "[");
5742 for (i = 0; i < x; i++)
5743 seq_printf(seq, "=");
5744 seq_printf(seq, ">");
5745 for (i = 0; i < y; i++)
5746 seq_printf(seq, ".");
5747 seq_printf(seq, "] ");
5749 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
5750 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
5752 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
5754 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
5755 "resync" : "recovery"))),
5756 per_milli/10, per_milli % 10,
5757 (unsigned long long) resync/2,
5758 (unsigned long long) max_sectors/2);
5761 * dt: time from mark until now
5762 * db: blocks written from mark until now
5763 * rt: remaining time
5765 * rt is a sector_t, so could be 32bit or 64bit.
5766 * So we divide before multiply in case it is 32bit and close
5768 * We scale the divisor (db) by 32 to avoid loosing precision
5769 * near the end of resync when the number of remaining sectors
5771 * We then divide rt by 32 after multiplying by db to compensate.
5772 * The '+1' avoids division by zero if db is very small.
5774 dt = ((jiffies - mddev->resync_mark) / HZ);
5776 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
5777 - mddev->resync_mark_cnt;
5779 rt = max_sectors - resync; /* number of remaining sectors */
5780 sector_div(rt, db/32+1);
5784 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
5785 ((unsigned long)rt % 60)/6);
5787 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
5790 static void *md_seq_start(struct seq_file *seq, loff_t *pos)
5792 struct list_head *tmp;
5802 spin_lock(&all_mddevs_lock);
5803 list_for_each(tmp,&all_mddevs)
5805 mddev = list_entry(tmp, mddev_t, all_mddevs);
5807 spin_unlock(&all_mddevs_lock);
5810 spin_unlock(&all_mddevs_lock);
5812 return (void*)2;/* tail */
5816 static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
5818 struct list_head *tmp;
5819 mddev_t *next_mddev, *mddev = v;
5825 spin_lock(&all_mddevs_lock);
5827 tmp = all_mddevs.next;
5829 tmp = mddev->all_mddevs.next;
5830 if (tmp != &all_mddevs)
5831 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
5833 next_mddev = (void*)2;
5836 spin_unlock(&all_mddevs_lock);
5844 static void md_seq_stop(struct seq_file *seq, void *v)
5848 if (mddev && v != (void*)1 && v != (void*)2)
5852 struct mdstat_info {
5856 static int md_seq_show(struct seq_file *seq, void *v)
5861 struct mdstat_info *mi = seq->private;
5862 struct bitmap *bitmap;
5864 if (v == (void*)1) {
5865 struct mdk_personality *pers;
5866 seq_printf(seq, "Personalities : ");
5867 spin_lock(&pers_lock);
5868 list_for_each_entry(pers, &pers_list, list)
5869 seq_printf(seq, "[%s] ", pers->name);
5871 spin_unlock(&pers_lock);
5872 seq_printf(seq, "\n");
5873 mi->event = atomic_read(&md_event_count);
5876 if (v == (void*)2) {
5881 if (mddev_lock(mddev) < 0)
5884 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
5885 seq_printf(seq, "%s : %sactive", mdname(mddev),
5886 mddev->pers ? "" : "in");
5889 seq_printf(seq, " (read-only)");
5891 seq_printf(seq, " (auto-read-only)");
5892 seq_printf(seq, " %s", mddev->pers->name);
5896 list_for_each_entry(rdev, &mddev->disks, same_set) {
5897 char b[BDEVNAME_SIZE];
5898 seq_printf(seq, " %s[%d]",
5899 bdevname(rdev->bdev,b), rdev->desc_nr);
5900 if (test_bit(WriteMostly, &rdev->flags))
5901 seq_printf(seq, "(W)");
5902 if (test_bit(Faulty, &rdev->flags)) {
5903 seq_printf(seq, "(F)");
5905 } else if (rdev->raid_disk < 0)
5906 seq_printf(seq, "(S)"); /* spare */
5907 sectors += rdev->sectors;
5910 if (!list_empty(&mddev->disks)) {
5912 seq_printf(seq, "\n %llu blocks",
5913 (unsigned long long)
5914 mddev->array_sectors / 2);
5916 seq_printf(seq, "\n %llu blocks",
5917 (unsigned long long)sectors / 2);
5919 if (mddev->persistent) {
5920 if (mddev->major_version != 0 ||
5921 mddev->minor_version != 90) {
5922 seq_printf(seq," super %d.%d",
5923 mddev->major_version,
5924 mddev->minor_version);
5926 } else if (mddev->external)
5927 seq_printf(seq, " super external:%s",
5928 mddev->metadata_type);
5930 seq_printf(seq, " super non-persistent");
5933 mddev->pers->status(seq, mddev);
5934 seq_printf(seq, "\n ");
5935 if (mddev->pers->sync_request) {
5936 if (mddev->curr_resync > 2) {
5937 status_resync(seq, mddev);
5938 seq_printf(seq, "\n ");
5939 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
5940 seq_printf(seq, "\tresync=DELAYED\n ");
5941 else if (mddev->recovery_cp < MaxSector)
5942 seq_printf(seq, "\tresync=PENDING\n ");
5945 seq_printf(seq, "\n ");
5947 if ((bitmap = mddev->bitmap)) {
5948 unsigned long chunk_kb;
5949 unsigned long flags;
5950 spin_lock_irqsave(&bitmap->lock, flags);
5951 chunk_kb = bitmap->chunksize >> 10;
5952 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
5954 bitmap->pages - bitmap->missing_pages,
5956 (bitmap->pages - bitmap->missing_pages)
5957 << (PAGE_SHIFT - 10),
5958 chunk_kb ? chunk_kb : bitmap->chunksize,
5959 chunk_kb ? "KB" : "B");
5961 seq_printf(seq, ", file: ");
5962 seq_path(seq, &bitmap->file->f_path, " \t\n");
5965 seq_printf(seq, "\n");
5966 spin_unlock_irqrestore(&bitmap->lock, flags);
5969 seq_printf(seq, "\n");
5971 mddev_unlock(mddev);
5976 static const struct seq_operations md_seq_ops = {
5977 .start = md_seq_start,
5978 .next = md_seq_next,
5979 .stop = md_seq_stop,
5980 .show = md_seq_show,
5983 static int md_seq_open(struct inode *inode, struct file *file)
5986 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
5990 error = seq_open(file, &md_seq_ops);
5994 struct seq_file *p = file->private_data;
5996 mi->event = atomic_read(&md_event_count);
6001 static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6003 struct seq_file *m = filp->private_data;
6004 struct mdstat_info *mi = m->private;
6007 poll_wait(filp, &md_event_waiters, wait);
6009 /* always allow read */
6010 mask = POLLIN | POLLRDNORM;
6012 if (mi->event != atomic_read(&md_event_count))
6013 mask |= POLLERR | POLLPRI;
6017 static const struct file_operations md_seq_fops = {
6018 .owner = THIS_MODULE,
6019 .open = md_seq_open,
6021 .llseek = seq_lseek,
6022 .release = seq_release_private,
6023 .poll = mdstat_poll,
6026 int register_md_personality(struct mdk_personality *p)
6028 spin_lock(&pers_lock);
6029 list_add_tail(&p->list, &pers_list);
6030 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
6031 spin_unlock(&pers_lock);
6035 int unregister_md_personality(struct mdk_personality *p)
6037 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
6038 spin_lock(&pers_lock);
6039 list_del_init(&p->list);
6040 spin_unlock(&pers_lock);
6044 static int is_mddev_idle(mddev_t *mddev, int init)
6052 rdev_for_each_rcu(rdev, mddev) {
6053 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
6054 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6055 (int)part_stat_read(&disk->part0, sectors[1]) -
6056 atomic_read(&disk->sync_io);
6057 /* sync IO will cause sync_io to increase before the disk_stats
6058 * as sync_io is counted when a request starts, and
6059 * disk_stats is counted when it completes.
6060 * So resync activity will cause curr_events to be smaller than
6061 * when there was no such activity.
6062 * non-sync IO will cause disk_stat to increase without
6063 * increasing sync_io so curr_events will (eventually)
6064 * be larger than it was before. Once it becomes
6065 * substantially larger, the test below will cause
6066 * the array to appear non-idle, and resync will slow
6068 * If there is a lot of outstanding resync activity when
6069 * we set last_event to curr_events, then all that activity
6070 * completing might cause the array to appear non-idle
6071 * and resync will be slowed down even though there might
6072 * not have been non-resync activity. This will only
6073 * happen once though. 'last_events' will soon reflect
6074 * the state where there is little or no outstanding
6075 * resync requests, and further resync activity will
6076 * always make curr_events less than last_events.
6079 if (init || curr_events - rdev->last_events > 64) {
6080 rdev->last_events = curr_events;
6088 void md_done_sync(mddev_t *mddev, int blocks, int ok)
6090 /* another "blocks" (512byte) blocks have been synced */
6091 atomic_sub(blocks, &mddev->recovery_active);
6092 wake_up(&mddev->recovery_wait);
6094 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6095 md_wakeup_thread(mddev->thread);
6096 // stop recovery, signal do_sync ....
6101 /* md_write_start(mddev, bi)
6102 * If we need to update some array metadata (e.g. 'active' flag
6103 * in superblock) before writing, schedule a superblock update
6104 * and wait for it to complete.
6106 void md_write_start(mddev_t *mddev, struct bio *bi)
6109 if (bio_data_dir(bi) != WRITE)
6112 BUG_ON(mddev->ro == 1);
6113 if (mddev->ro == 2) {
6114 /* need to switch to read/write */
6116 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6117 md_wakeup_thread(mddev->thread);
6118 md_wakeup_thread(mddev->sync_thread);
6121 atomic_inc(&mddev->writes_pending);
6122 if (mddev->safemode == 1)
6123 mddev->safemode = 0;
6124 if (mddev->in_sync) {
6125 spin_lock_irq(&mddev->write_lock);
6126 if (mddev->in_sync) {
6128 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6129 md_wakeup_thread(mddev->thread);
6132 spin_unlock_irq(&mddev->write_lock);
6135 sysfs_notify_dirent(mddev->sysfs_state);
6136 wait_event(mddev->sb_wait,
6137 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
6138 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
6141 void md_write_end(mddev_t *mddev)
6143 if (atomic_dec_and_test(&mddev->writes_pending)) {
6144 if (mddev->safemode == 2)
6145 md_wakeup_thread(mddev->thread);
6146 else if (mddev->safemode_delay)
6147 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6151 /* md_allow_write(mddev)
6152 * Calling this ensures that the array is marked 'active' so that writes
6153 * may proceed without blocking. It is important to call this before
6154 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6155 * Must be called with mddev_lock held.
6157 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6158 * is dropped, so return -EAGAIN after notifying userspace.
6160 int md_allow_write(mddev_t *mddev)
6166 if (!mddev->pers->sync_request)
6169 spin_lock_irq(&mddev->write_lock);
6170 if (mddev->in_sync) {
6172 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6173 if (mddev->safemode_delay &&
6174 mddev->safemode == 0)
6175 mddev->safemode = 1;
6176 spin_unlock_irq(&mddev->write_lock);
6177 md_update_sb(mddev, 0);
6178 sysfs_notify_dirent(mddev->sysfs_state);
6180 spin_unlock_irq(&mddev->write_lock);
6182 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
6187 EXPORT_SYMBOL_GPL(md_allow_write);
6189 #define SYNC_MARKS 10
6190 #define SYNC_MARK_STEP (3*HZ)
6191 void md_do_sync(mddev_t *mddev)
6194 unsigned int currspeed = 0,
6196 sector_t max_sectors,j, io_sectors;
6197 unsigned long mark[SYNC_MARKS];
6198 sector_t mark_cnt[SYNC_MARKS];
6200 struct list_head *tmp;
6201 sector_t last_check;
6206 /* just incase thread restarts... */
6207 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6209 if (mddev->ro) /* never try to sync a read-only array */
6212 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6213 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6214 desc = "data-check";
6215 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6216 desc = "requested-resync";
6219 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6224 /* we overload curr_resync somewhat here.
6225 * 0 == not engaged in resync at all
6226 * 2 == checking that there is no conflict with another sync
6227 * 1 == like 2, but have yielded to allow conflicting resync to
6229 * other == active in resync - this many blocks
6231 * Before starting a resync we must have set curr_resync to
6232 * 2, and then checked that every "conflicting" array has curr_resync
6233 * less than ours. When we find one that is the same or higher
6234 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6235 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6236 * This will mean we have to start checking from the beginning again.
6241 mddev->curr_resync = 2;
6244 if (kthread_should_stop()) {
6245 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6248 for_each_mddev(mddev2, tmp) {
6249 if (mddev2 == mddev)
6251 if (!mddev->parallel_resync
6252 && mddev2->curr_resync
6253 && match_mddev_units(mddev, mddev2)) {
6255 if (mddev < mddev2 && mddev->curr_resync == 2) {
6256 /* arbitrarily yield */
6257 mddev->curr_resync = 1;
6258 wake_up(&resync_wait);
6260 if (mddev > mddev2 && mddev->curr_resync == 1)
6261 /* no need to wait here, we can wait the next
6262 * time 'round when curr_resync == 2
6265 /* We need to wait 'interruptible' so as not to
6266 * contribute to the load average, and not to
6267 * be caught by 'softlockup'
6269 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
6270 if (!kthread_should_stop() &&
6271 mddev2->curr_resync >= mddev->curr_resync) {
6272 printk(KERN_INFO "md: delaying %s of %s"
6273 " until %s has finished (they"
6274 " share one or more physical units)\n",
6275 desc, mdname(mddev), mdname(mddev2));
6277 if (signal_pending(current))
6278 flush_signals(current);
6280 finish_wait(&resync_wait, &wq);
6283 finish_wait(&resync_wait, &wq);
6286 } while (mddev->curr_resync < 2);
6289 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6290 /* resync follows the size requested by the personality,
6291 * which defaults to physical size, but can be virtual size
6293 max_sectors = mddev->resync_max_sectors;
6294 mddev->resync_mismatches = 0;
6295 /* we don't use the checkpoint if there's a bitmap */
6296 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6297 j = mddev->resync_min;
6298 else if (!mddev->bitmap)
6299 j = mddev->recovery_cp;
6301 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6302 max_sectors = mddev->dev_sectors;
6304 /* recovery follows the physical size of devices */
6305 max_sectors = mddev->dev_sectors;
6307 list_for_each_entry(rdev, &mddev->disks, same_set)
6308 if (rdev->raid_disk >= 0 &&
6309 !test_bit(Faulty, &rdev->flags) &&
6310 !test_bit(In_sync, &rdev->flags) &&
6311 rdev->recovery_offset < j)
6312 j = rdev->recovery_offset;
6315 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6316 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6317 " %d KB/sec/disk.\n", speed_min(mddev));
6318 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
6319 "(but not more than %d KB/sec) for %s.\n",
6320 speed_max(mddev), desc);
6322 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
6325 for (m = 0; m < SYNC_MARKS; m++) {
6327 mark_cnt[m] = io_sectors;
6330 mddev->resync_mark = mark[last_mark];
6331 mddev->resync_mark_cnt = mark_cnt[last_mark];
6334 * Tune reconstruction:
6336 window = 32*(PAGE_SIZE/512);
6337 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6338 window/2,(unsigned long long) max_sectors/2);
6340 atomic_set(&mddev->recovery_active, 0);
6345 "md: resuming %s of %s from checkpoint.\n",
6346 desc, mdname(mddev));
6347 mddev->curr_resync = j;
6350 while (j < max_sectors) {
6355 if ((mddev->curr_resync > mddev->curr_resync_completed &&
6356 (mddev->curr_resync - mddev->curr_resync_completed)
6357 > (max_sectors >> 4)) ||
6358 (j - mddev->curr_resync_completed)*2
6359 >= mddev->resync_max - mddev->curr_resync_completed
6361 /* time to update curr_resync_completed */
6362 blk_unplug(mddev->queue);
6363 wait_event(mddev->recovery_wait,
6364 atomic_read(&mddev->recovery_active) == 0);
6365 mddev->curr_resync_completed =
6367 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6368 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
6371 if (j >= mddev->resync_max)
6372 wait_event(mddev->recovery_wait,
6373 mddev->resync_max > j
6374 || kthread_should_stop());
6376 if (kthread_should_stop())
6379 sectors = mddev->pers->sync_request(mddev, j, &skipped,
6380 currspeed < speed_min(mddev));
6382 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6386 if (!skipped) { /* actual IO requested */
6387 io_sectors += sectors;
6388 atomic_add(sectors, &mddev->recovery_active);
6392 if (j>1) mddev->curr_resync = j;
6393 mddev->curr_mark_cnt = io_sectors;
6394 if (last_check == 0)
6395 /* this is the earliers that rebuilt will be
6396 * visible in /proc/mdstat
6398 md_new_event(mddev);
6400 if (last_check + window > io_sectors || j == max_sectors)
6403 last_check = io_sectors;
6405 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6409 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6411 int next = (last_mark+1) % SYNC_MARKS;
6413 mddev->resync_mark = mark[next];
6414 mddev->resync_mark_cnt = mark_cnt[next];
6415 mark[next] = jiffies;
6416 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
6421 if (kthread_should_stop())
6426 * this loop exits only if either when we are slower than
6427 * the 'hard' speed limit, or the system was IO-idle for
6429 * the system might be non-idle CPU-wise, but we only care
6430 * about not overloading the IO subsystem. (things like an
6431 * e2fsck being done on the RAID array should execute fast)
6433 blk_unplug(mddev->queue);
6436 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6437 /((jiffies-mddev->resync_mark)/HZ +1) +1;
6439 if (currspeed > speed_min(mddev)) {
6440 if ((currspeed > speed_max(mddev)) ||
6441 !is_mddev_idle(mddev, 0)) {
6447 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
6449 * this also signals 'finished resyncing' to md_stop
6452 blk_unplug(mddev->queue);
6454 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6456 /* tell personality that we are finished */
6457 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
6459 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
6460 mddev->curr_resync > 2) {
6461 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6462 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6463 if (mddev->curr_resync >= mddev->recovery_cp) {
6465 "md: checkpointing %s of %s.\n",
6466 desc, mdname(mddev));
6467 mddev->recovery_cp = mddev->curr_resync;
6470 mddev->recovery_cp = MaxSector;
6472 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6473 mddev->curr_resync = MaxSector;
6474 list_for_each_entry(rdev, &mddev->disks, same_set)
6475 if (rdev->raid_disk >= 0 &&
6476 !test_bit(Faulty, &rdev->flags) &&
6477 !test_bit(In_sync, &rdev->flags) &&
6478 rdev->recovery_offset < mddev->curr_resync)
6479 rdev->recovery_offset = mddev->curr_resync;
6482 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6485 mddev->curr_resync = 0;
6486 mddev->curr_resync_completed = 0;
6487 mddev->resync_min = 0;
6488 mddev->resync_max = MaxSector;
6489 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
6490 wake_up(&resync_wait);
6491 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6492 md_wakeup_thread(mddev->thread);
6497 * got a signal, exit.
6500 "md: md_do_sync() got signal ... exiting\n");
6501 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6505 EXPORT_SYMBOL_GPL(md_do_sync);
6508 static int remove_and_add_spares(mddev_t *mddev)
6513 mddev->curr_resync_completed = 0;
6515 list_for_each_entry(rdev, &mddev->disks, same_set)
6516 if (rdev->raid_disk >= 0 &&
6517 !test_bit(Blocked, &rdev->flags) &&
6518 (test_bit(Faulty, &rdev->flags) ||
6519 ! test_bit(In_sync, &rdev->flags)) &&
6520 atomic_read(&rdev->nr_pending)==0) {
6521 if (mddev->pers->hot_remove_disk(
6522 mddev, rdev->raid_disk)==0) {
6524 sprintf(nm,"rd%d", rdev->raid_disk);
6525 sysfs_remove_link(&mddev->kobj, nm);
6526 rdev->raid_disk = -1;
6530 if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) {
6531 list_for_each_entry(rdev, &mddev->disks, same_set) {
6532 if (rdev->raid_disk >= 0 &&
6533 !test_bit(In_sync, &rdev->flags) &&
6534 !test_bit(Blocked, &rdev->flags))
6536 if (rdev->raid_disk < 0
6537 && !test_bit(Faulty, &rdev->flags)) {
6538 rdev->recovery_offset = 0;
6540 hot_add_disk(mddev, rdev) == 0) {
6542 sprintf(nm, "rd%d", rdev->raid_disk);
6543 if (sysfs_create_link(&mddev->kobj,
6546 "md: cannot register "
6550 md_new_event(mddev);
6559 * This routine is regularly called by all per-raid-array threads to
6560 * deal with generic issues like resync and super-block update.
6561 * Raid personalities that don't have a thread (linear/raid0) do not
6562 * need this as they never do any recovery or update the superblock.
6564 * It does not do any resync itself, but rather "forks" off other threads
6565 * to do that as needed.
6566 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6567 * "->recovery" and create a thread at ->sync_thread.
6568 * When the thread finishes it sets MD_RECOVERY_DONE
6569 * and wakeups up this thread which will reap the thread and finish up.
6570 * This thread also removes any faulty devices (with nr_pending == 0).
6572 * The overall approach is:
6573 * 1/ if the superblock needs updating, update it.
6574 * 2/ If a recovery thread is running, don't do anything else.
6575 * 3/ If recovery has finished, clean up, possibly marking spares active.
6576 * 4/ If there are any faulty devices, remove them.
6577 * 5/ If array is degraded, try to add spares devices
6578 * 6/ If array has spares or is not in-sync, start a resync thread.
6580 void md_check_recovery(mddev_t *mddev)
6586 bitmap_daemon_work(mddev->bitmap);
6591 if (signal_pending(current)) {
6592 if (mddev->pers->sync_request && !mddev->external) {
6593 printk(KERN_INFO "md: %s in immediate safe mode\n",
6595 mddev->safemode = 2;
6597 flush_signals(current);
6600 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
6603 (mddev->flags && !mddev->external) ||
6604 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
6605 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
6606 (mddev->external == 0 && mddev->safemode == 1) ||
6607 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6608 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
6612 if (mddev_trylock(mddev)) {
6616 /* Only thing we do on a ro array is remove
6619 remove_and_add_spares(mddev);
6620 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6624 if (!mddev->external) {
6626 spin_lock_irq(&mddev->write_lock);
6627 if (mddev->safemode &&
6628 !atomic_read(&mddev->writes_pending) &&
6630 mddev->recovery_cp == MaxSector) {
6633 if (mddev->persistent)
6634 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6636 if (mddev->safemode == 1)
6637 mddev->safemode = 0;
6638 spin_unlock_irq(&mddev->write_lock);
6640 sysfs_notify_dirent(mddev->sysfs_state);
6644 md_update_sb(mddev, 0);
6646 list_for_each_entry(rdev, &mddev->disks, same_set)
6647 if (test_and_clear_bit(StateChanged, &rdev->flags))
6648 sysfs_notify_dirent(rdev->sysfs_state);
6651 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
6652 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
6653 /* resync/recovery still happening */
6654 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6657 if (mddev->sync_thread) {
6658 /* resync has finished, collect result */
6659 md_unregister_thread(mddev->sync_thread);
6660 mddev->sync_thread = NULL;
6661 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
6662 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
6664 /* activate any spares */
6665 if (mddev->pers->spare_active(mddev))
6666 sysfs_notify(&mddev->kobj, NULL,
6669 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6670 mddev->pers->finish_reshape)
6671 mddev->pers->finish_reshape(mddev);
6672 md_update_sb(mddev, 1);
6674 /* if array is no-longer degraded, then any saved_raid_disk
6675 * information must be scrapped
6677 if (!mddev->degraded)
6678 list_for_each_entry(rdev, &mddev->disks, same_set)
6679 rdev->saved_raid_disk = -1;
6681 mddev->recovery = 0;
6682 /* flag recovery needed just to double check */
6683 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6684 sysfs_notify_dirent(mddev->sysfs_action);
6685 md_new_event(mddev);
6688 /* Set RUNNING before clearing NEEDED to avoid
6689 * any transients in the value of "sync_action".
6691 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6692 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6693 /* Clear some bits that don't mean anything, but
6696 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
6697 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
6699 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
6701 /* no recovery is running.
6702 * remove any failed drives, then
6703 * add spares if possible.
6704 * Spare are also removed and re-added, to allow
6705 * the personality to fail the re-add.
6708 if (mddev->reshape_position != MaxSector) {
6709 if (mddev->pers->check_reshape(mddev) != 0)
6710 /* Cannot proceed */
6712 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
6713 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
6714 } else if ((spares = remove_and_add_spares(mddev))) {
6715 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6716 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
6717 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
6718 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
6719 } else if (mddev->recovery_cp < MaxSector) {
6720 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6721 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
6722 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
6723 /* nothing to be done ... */
6726 if (mddev->pers->sync_request) {
6727 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
6728 /* We are adding a device or devices to an array
6729 * which has the bitmap stored on all devices.
6730 * So make sure all bitmap pages get written
6732 bitmap_write_all(mddev->bitmap);
6734 mddev->sync_thread = md_register_thread(md_do_sync,
6737 if (!mddev->sync_thread) {
6738 printk(KERN_ERR "%s: could not start resync"
6741 /* leave the spares where they are, it shouldn't hurt */
6742 mddev->recovery = 0;
6744 md_wakeup_thread(mddev->sync_thread);
6745 sysfs_notify_dirent(mddev->sysfs_action);
6746 md_new_event(mddev);
6749 if (!mddev->sync_thread) {
6750 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6751 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
6753 if (mddev->sysfs_action)
6754 sysfs_notify_dirent(mddev->sysfs_action);
6756 mddev_unlock(mddev);
6760 void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
6762 sysfs_notify_dirent(rdev->sysfs_state);
6763 wait_event_timeout(rdev->blocked_wait,
6764 !test_bit(Blocked, &rdev->flags),
6765 msecs_to_jiffies(5000));
6766 rdev_dec_pending(rdev, mddev);
6768 EXPORT_SYMBOL(md_wait_for_blocked_rdev);
6770 static int md_notify_reboot(struct notifier_block *this,
6771 unsigned long code, void *x)
6773 struct list_head *tmp;
6776 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
6778 printk(KERN_INFO "md: stopping all md devices.\n");
6780 for_each_mddev(mddev, tmp)
6781 if (mddev_trylock(mddev)) {
6782 /* Force a switch to readonly even array
6783 * appears to still be in use. Hence
6786 do_md_stop(mddev, 1, 100);
6787 mddev_unlock(mddev);
6790 * certain more exotic SCSI devices are known to be
6791 * volatile wrt too early system reboots. While the
6792 * right place to handle this issue is the given
6793 * driver, we do want to have a safe RAID driver ...
6800 static struct notifier_block md_notifier = {
6801 .notifier_call = md_notify_reboot,
6803 .priority = INT_MAX, /* before any real devices */
6806 static void md_geninit(void)
6808 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
6810 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
6813 static int __init md_init(void)
6815 if (register_blkdev(MD_MAJOR, "md"))
6817 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
6818 unregister_blkdev(MD_MAJOR, "md");
6821 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
6822 md_probe, NULL, NULL);
6823 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
6824 md_probe, NULL, NULL);
6826 register_reboot_notifier(&md_notifier);
6827 raid_table_header = register_sysctl_table(raid_root_table);
6837 * Searches all registered partitions for autorun RAID arrays
6841 static LIST_HEAD(all_detected_devices);
6842 struct detected_devices_node {
6843 struct list_head list;
6847 void md_autodetect_dev(dev_t dev)
6849 struct detected_devices_node *node_detected_dev;
6851 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
6852 if (node_detected_dev) {
6853 node_detected_dev->dev = dev;
6854 list_add_tail(&node_detected_dev->list, &all_detected_devices);
6856 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
6857 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
6862 static void autostart_arrays(int part)
6865 struct detected_devices_node *node_detected_dev;
6867 int i_scanned, i_passed;
6872 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
6874 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
6876 node_detected_dev = list_entry(all_detected_devices.next,
6877 struct detected_devices_node, list);
6878 list_del(&node_detected_dev->list);
6879 dev = node_detected_dev->dev;
6880 kfree(node_detected_dev);
6881 rdev = md_import_device(dev,0, 90);
6885 if (test_bit(Faulty, &rdev->flags)) {
6889 set_bit(AutoDetected, &rdev->flags);
6890 list_add(&rdev->same_set, &pending_raid_disks);
6894 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
6895 i_scanned, i_passed);
6897 autorun_devices(part);
6900 #endif /* !MODULE */
6902 static __exit void md_exit(void)
6905 struct list_head *tmp;
6907 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
6908 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
6910 unregister_blkdev(MD_MAJOR,"md");
6911 unregister_blkdev(mdp_major, "mdp");
6912 unregister_reboot_notifier(&md_notifier);
6913 unregister_sysctl_table(raid_table_header);
6914 remove_proc_entry("mdstat", NULL);
6915 for_each_mddev(mddev, tmp) {
6916 export_array(mddev);
6917 mddev->hold_active = 0;
6921 subsys_initcall(md_init);
6922 module_exit(md_exit)
6924 static int get_ro(char *buffer, struct kernel_param *kp)
6926 return sprintf(buffer, "%d", start_readonly);
6928 static int set_ro(const char *val, struct kernel_param *kp)
6931 int num = simple_strtoul(val, &e, 10);
6932 if (*val && (*e == '\0' || *e == '\n')) {
6933 start_readonly = num;
6939 module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
6940 module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
6942 module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
6944 EXPORT_SYMBOL(register_md_personality);
6945 EXPORT_SYMBOL(unregister_md_personality);
6946 EXPORT_SYMBOL(md_error);
6947 EXPORT_SYMBOL(md_done_sync);
6948 EXPORT_SYMBOL(md_write_start);
6949 EXPORT_SYMBOL(md_write_end);
6950 EXPORT_SYMBOL(md_register_thread);
6951 EXPORT_SYMBOL(md_unregister_thread);
6952 EXPORT_SYMBOL(md_wakeup_thread);
6953 EXPORT_SYMBOL(md_check_recovery);
6954 MODULE_LICENSE("GPL");
6956 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);