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/compat.h>
48 #include <linux/delay.h>
49 #include <linux/raid/md_p.h>
50 #include <linux/raid/md_u.h>
55 #define dprintk(x...) ((void)(DEBUG && printk(x)))
59 static void autostart_arrays(int part);
62 static LIST_HEAD(pers_list);
63 static DEFINE_SPINLOCK(pers_lock);
65 static void md_print_devices(void);
67 static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
69 #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
72 * Default number of read corrections we'll attempt on an rdev
73 * before ejecting it from the array. We divide the read error
74 * count by 2 for every hour elapsed between read errors.
76 #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
78 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
79 * is 1000 KB/sec, so the extra system load does not show up that much.
80 * Increase it if you want to have more _guaranteed_ speed. Note that
81 * the RAID driver will use the maximum available bandwidth if the IO
82 * subsystem is idle. There is also an 'absolute maximum' reconstruction
83 * speed limit - in case reconstruction slows down your system despite
86 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
87 * or /sys/block/mdX/md/sync_speed_{min,max}
90 static int sysctl_speed_limit_min = 1000;
91 static int sysctl_speed_limit_max = 200000;
92 static inline int speed_min(mddev_t *mddev)
94 return mddev->sync_speed_min ?
95 mddev->sync_speed_min : sysctl_speed_limit_min;
98 static inline int speed_max(mddev_t *mddev)
100 return mddev->sync_speed_max ?
101 mddev->sync_speed_max : sysctl_speed_limit_max;
104 static struct ctl_table_header *raid_table_header;
106 static ctl_table raid_table[] = {
108 .procname = "speed_limit_min",
109 .data = &sysctl_speed_limit_min,
110 .maxlen = sizeof(int),
111 .mode = S_IRUGO|S_IWUSR,
112 .proc_handler = proc_dointvec,
115 .procname = "speed_limit_max",
116 .data = &sysctl_speed_limit_max,
117 .maxlen = sizeof(int),
118 .mode = S_IRUGO|S_IWUSR,
119 .proc_handler = proc_dointvec,
124 static ctl_table raid_dir_table[] = {
128 .mode = S_IRUGO|S_IXUGO,
134 static ctl_table raid_root_table[] = {
139 .child = raid_dir_table,
144 static const struct block_device_operations md_fops;
146 static int start_readonly;
149 * We have a system wide 'event count' that is incremented
150 * on any 'interesting' event, and readers of /proc/mdstat
151 * can use 'poll' or 'select' to find out when the event
155 * start array, stop array, error, add device, remove device,
156 * start build, activate spare
158 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
159 static atomic_t md_event_count;
160 void md_new_event(mddev_t *mddev)
162 atomic_inc(&md_event_count);
163 wake_up(&md_event_waiters);
165 EXPORT_SYMBOL_GPL(md_new_event);
167 /* Alternate version that can be called from interrupts
168 * when calling sysfs_notify isn't needed.
170 static void md_new_event_inintr(mddev_t *mddev)
172 atomic_inc(&md_event_count);
173 wake_up(&md_event_waiters);
177 * Enables to iterate over all existing md arrays
178 * all_mddevs_lock protects this list.
180 static LIST_HEAD(all_mddevs);
181 static DEFINE_SPINLOCK(all_mddevs_lock);
185 * iterates through all used mddevs in the system.
186 * We take care to grab the all_mddevs_lock whenever navigating
187 * the list, and to always hold a refcount when unlocked.
188 * Any code which breaks out of this loop while own
189 * a reference to the current mddev and must mddev_put it.
191 #define for_each_mddev(mddev,tmp) \
193 for (({ spin_lock(&all_mddevs_lock); \
194 tmp = all_mddevs.next; \
196 ({ if (tmp != &all_mddevs) \
197 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
198 spin_unlock(&all_mddevs_lock); \
199 if (mddev) mddev_put(mddev); \
200 mddev = list_entry(tmp, mddev_t, all_mddevs); \
201 tmp != &all_mddevs;}); \
202 ({ spin_lock(&all_mddevs_lock); \
207 /* Rather than calling directly into the personality make_request function,
208 * IO requests come here first so that we can check if the device is
209 * being suspended pending a reconfiguration.
210 * We hold a refcount over the call to ->make_request. By the time that
211 * call has finished, the bio has been linked into some internal structure
212 * and so is visible to ->quiesce(), so we don't need the refcount any more.
214 static int md_make_request(struct request_queue *q, struct bio *bio)
216 mddev_t *mddev = q->queuedata;
218 if (mddev == NULL || mddev->pers == NULL) {
223 if (mddev->suspended || mddev->barrier) {
226 prepare_to_wait(&mddev->sb_wait, &__wait,
227 TASK_UNINTERRUPTIBLE);
228 if (!mddev->suspended && !mddev->barrier)
234 finish_wait(&mddev->sb_wait, &__wait);
236 atomic_inc(&mddev->active_io);
238 rv = mddev->pers->make_request(q, bio);
239 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
240 wake_up(&mddev->sb_wait);
245 static void mddev_suspend(mddev_t *mddev)
247 BUG_ON(mddev->suspended);
248 mddev->suspended = 1;
250 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
251 mddev->pers->quiesce(mddev, 1);
252 md_unregister_thread(mddev->thread);
253 mddev->thread = NULL;
254 /* we now know that no code is executing in the personality module,
255 * except possibly the tail end of a ->bi_end_io function, but that
256 * is certain to complete before the module has a chance to get
261 static void mddev_resume(mddev_t *mddev)
263 mddev->suspended = 0;
264 wake_up(&mddev->sb_wait);
265 mddev->pers->quiesce(mddev, 0);
268 int mddev_congested(mddev_t *mddev, int bits)
272 return mddev->suspended;
274 EXPORT_SYMBOL(mddev_congested);
277 * Generic barrier handling for md
280 #define POST_REQUEST_BARRIER ((void*)1)
282 static void md_end_barrier(struct bio *bio, int err)
284 mdk_rdev_t *rdev = bio->bi_private;
285 mddev_t *mddev = rdev->mddev;
286 if (err == -EOPNOTSUPP && mddev->barrier != POST_REQUEST_BARRIER)
287 set_bit(BIO_EOPNOTSUPP, &mddev->barrier->bi_flags);
289 rdev_dec_pending(rdev, mddev);
291 if (atomic_dec_and_test(&mddev->flush_pending)) {
292 if (mddev->barrier == POST_REQUEST_BARRIER) {
293 /* This was a post-request barrier */
294 mddev->barrier = NULL;
295 wake_up(&mddev->sb_wait);
297 /* The pre-request barrier has finished */
298 schedule_work(&mddev->barrier_work);
303 static void submit_barriers(mddev_t *mddev)
308 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
309 if (rdev->raid_disk >= 0 &&
310 !test_bit(Faulty, &rdev->flags)) {
311 /* Take two references, one is dropped
312 * when request finishes, one after
313 * we reclaim rcu_read_lock
316 atomic_inc(&rdev->nr_pending);
317 atomic_inc(&rdev->nr_pending);
319 bi = bio_alloc(GFP_KERNEL, 0);
320 bi->bi_end_io = md_end_barrier;
321 bi->bi_private = rdev;
322 bi->bi_bdev = rdev->bdev;
323 atomic_inc(&mddev->flush_pending);
324 submit_bio(WRITE_BARRIER, bi);
326 rdev_dec_pending(rdev, mddev);
331 static void md_submit_barrier(struct work_struct *ws)
333 mddev_t *mddev = container_of(ws, mddev_t, barrier_work);
334 struct bio *bio = mddev->barrier;
336 atomic_set(&mddev->flush_pending, 1);
338 if (test_bit(BIO_EOPNOTSUPP, &bio->bi_flags))
339 bio_endio(bio, -EOPNOTSUPP);
340 else if (bio->bi_size == 0)
341 /* an empty barrier - all done */
344 bio->bi_rw &= ~(1<<BIO_RW_BARRIER);
345 if (mddev->pers->make_request(mddev->queue, bio))
346 generic_make_request(bio);
347 mddev->barrier = POST_REQUEST_BARRIER;
348 submit_barriers(mddev);
350 if (atomic_dec_and_test(&mddev->flush_pending)) {
351 mddev->barrier = NULL;
352 wake_up(&mddev->sb_wait);
356 void md_barrier_request(mddev_t *mddev, struct bio *bio)
358 spin_lock_irq(&mddev->write_lock);
359 wait_event_lock_irq(mddev->sb_wait,
361 mddev->write_lock, /*nothing*/);
362 mddev->barrier = bio;
363 spin_unlock_irq(&mddev->write_lock);
365 atomic_set(&mddev->flush_pending, 1);
366 INIT_WORK(&mddev->barrier_work, md_submit_barrier);
368 submit_barriers(mddev);
370 if (atomic_dec_and_test(&mddev->flush_pending))
371 schedule_work(&mddev->barrier_work);
373 EXPORT_SYMBOL(md_barrier_request);
375 static inline mddev_t *mddev_get(mddev_t *mddev)
377 atomic_inc(&mddev->active);
381 static void mddev_delayed_delete(struct work_struct *ws);
383 static void mddev_put(mddev_t *mddev)
385 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
387 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
388 !mddev->hold_active) {
389 list_del(&mddev->all_mddevs);
390 if (mddev->gendisk) {
391 /* we did a probe so need to clean up.
392 * Call schedule_work inside the spinlock
393 * so that flush_scheduled_work() after
394 * mddev_find will succeed in waiting for the
397 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
398 schedule_work(&mddev->del_work);
402 spin_unlock(&all_mddevs_lock);
405 static mddev_t * mddev_find(dev_t unit)
407 mddev_t *mddev, *new = NULL;
410 spin_lock(&all_mddevs_lock);
413 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
414 if (mddev->unit == unit) {
416 spin_unlock(&all_mddevs_lock);
422 list_add(&new->all_mddevs, &all_mddevs);
423 spin_unlock(&all_mddevs_lock);
424 new->hold_active = UNTIL_IOCTL;
428 /* find an unused unit number */
429 static int next_minor = 512;
430 int start = next_minor;
434 dev = MKDEV(MD_MAJOR, next_minor);
436 if (next_minor > MINORMASK)
438 if (next_minor == start) {
439 /* Oh dear, all in use. */
440 spin_unlock(&all_mddevs_lock);
446 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
447 if (mddev->unit == dev) {
453 new->md_minor = MINOR(dev);
454 new->hold_active = UNTIL_STOP;
455 list_add(&new->all_mddevs, &all_mddevs);
456 spin_unlock(&all_mddevs_lock);
459 spin_unlock(&all_mddevs_lock);
461 new = kzalloc(sizeof(*new), GFP_KERNEL);
466 if (MAJOR(unit) == MD_MAJOR)
467 new->md_minor = MINOR(unit);
469 new->md_minor = MINOR(unit) >> MdpMinorShift;
471 mutex_init(&new->open_mutex);
472 mutex_init(&new->reconfig_mutex);
473 mutex_init(&new->bitmap_info.mutex);
474 INIT_LIST_HEAD(&new->disks);
475 INIT_LIST_HEAD(&new->all_mddevs);
476 init_timer(&new->safemode_timer);
477 atomic_set(&new->active, 1);
478 atomic_set(&new->openers, 0);
479 atomic_set(&new->active_io, 0);
480 spin_lock_init(&new->write_lock);
481 atomic_set(&new->flush_pending, 0);
482 init_waitqueue_head(&new->sb_wait);
483 init_waitqueue_head(&new->recovery_wait);
484 new->reshape_position = MaxSector;
486 new->resync_max = MaxSector;
487 new->level = LEVEL_NONE;
492 static inline int mddev_lock(mddev_t * mddev)
494 return mutex_lock_interruptible(&mddev->reconfig_mutex);
497 static inline int mddev_is_locked(mddev_t *mddev)
499 return mutex_is_locked(&mddev->reconfig_mutex);
502 static inline int mddev_trylock(mddev_t * mddev)
504 return mutex_trylock(&mddev->reconfig_mutex);
507 static inline void mddev_unlock(mddev_t * mddev)
509 mutex_unlock(&mddev->reconfig_mutex);
511 md_wakeup_thread(mddev->thread);
514 static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
518 list_for_each_entry(rdev, &mddev->disks, same_set)
519 if (rdev->desc_nr == nr)
525 static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
529 list_for_each_entry(rdev, &mddev->disks, same_set)
530 if (rdev->bdev->bd_dev == dev)
536 static struct mdk_personality *find_pers(int level, char *clevel)
538 struct mdk_personality *pers;
539 list_for_each_entry(pers, &pers_list, list) {
540 if (level != LEVEL_NONE && pers->level == level)
542 if (strcmp(pers->name, clevel)==0)
548 /* return the offset of the super block in 512byte sectors */
549 static inline sector_t calc_dev_sboffset(struct block_device *bdev)
551 sector_t num_sectors = bdev->bd_inode->i_size / 512;
552 return MD_NEW_SIZE_SECTORS(num_sectors);
555 static int alloc_disk_sb(mdk_rdev_t * rdev)
560 rdev->sb_page = alloc_page(GFP_KERNEL);
561 if (!rdev->sb_page) {
562 printk(KERN_ALERT "md: out of memory.\n");
569 static void free_disk_sb(mdk_rdev_t * rdev)
572 put_page(rdev->sb_page);
574 rdev->sb_page = NULL;
581 static void super_written(struct bio *bio, int error)
583 mdk_rdev_t *rdev = bio->bi_private;
584 mddev_t *mddev = rdev->mddev;
586 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
587 printk("md: super_written gets error=%d, uptodate=%d\n",
588 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
589 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
590 md_error(mddev, rdev);
593 if (atomic_dec_and_test(&mddev->pending_writes))
594 wake_up(&mddev->sb_wait);
598 static void super_written_barrier(struct bio *bio, int error)
600 struct bio *bio2 = bio->bi_private;
601 mdk_rdev_t *rdev = bio2->bi_private;
602 mddev_t *mddev = rdev->mddev;
604 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
605 error == -EOPNOTSUPP) {
607 /* barriers don't appear to be supported :-( */
608 set_bit(BarriersNotsupp, &rdev->flags);
609 mddev->barriers_work = 0;
610 spin_lock_irqsave(&mddev->write_lock, flags);
611 bio2->bi_next = mddev->biolist;
612 mddev->biolist = bio2;
613 spin_unlock_irqrestore(&mddev->write_lock, flags);
614 wake_up(&mddev->sb_wait);
618 bio->bi_private = rdev;
619 super_written(bio, error);
623 void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
624 sector_t sector, int size, struct page *page)
626 /* write first size bytes of page to sector of rdev
627 * Increment mddev->pending_writes before returning
628 * and decrement it on completion, waking up sb_wait
629 * if zero is reached.
630 * If an error occurred, call md_error
632 * As we might need to resubmit the request if BIO_RW_BARRIER
633 * causes ENOTSUPP, we allocate a spare bio...
635 struct bio *bio = bio_alloc(GFP_NOIO, 1);
636 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNCIO) | (1<<BIO_RW_UNPLUG);
638 bio->bi_bdev = rdev->bdev;
639 bio->bi_sector = sector;
640 bio_add_page(bio, page, size, 0);
641 bio->bi_private = rdev;
642 bio->bi_end_io = super_written;
645 atomic_inc(&mddev->pending_writes);
646 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
648 rw |= (1<<BIO_RW_BARRIER);
649 rbio = bio_clone(bio, GFP_NOIO);
650 rbio->bi_private = bio;
651 rbio->bi_end_io = super_written_barrier;
652 submit_bio(rw, rbio);
657 void md_super_wait(mddev_t *mddev)
659 /* wait for all superblock writes that were scheduled to complete.
660 * if any had to be retried (due to BARRIER problems), retry them
664 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
665 if (atomic_read(&mddev->pending_writes)==0)
667 while (mddev->biolist) {
669 spin_lock_irq(&mddev->write_lock);
670 bio = mddev->biolist;
671 mddev->biolist = bio->bi_next ;
673 spin_unlock_irq(&mddev->write_lock);
674 submit_bio(bio->bi_rw, bio);
678 finish_wait(&mddev->sb_wait, &wq);
681 static void bi_complete(struct bio *bio, int error)
683 complete((struct completion*)bio->bi_private);
686 int sync_page_io(struct block_device *bdev, sector_t sector, int size,
687 struct page *page, int rw)
689 struct bio *bio = bio_alloc(GFP_NOIO, 1);
690 struct completion event;
693 rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG);
696 bio->bi_sector = sector;
697 bio_add_page(bio, page, size, 0);
698 init_completion(&event);
699 bio->bi_private = &event;
700 bio->bi_end_io = bi_complete;
702 wait_for_completion(&event);
704 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
708 EXPORT_SYMBOL_GPL(sync_page_io);
710 static int read_disk_sb(mdk_rdev_t * rdev, int size)
712 char b[BDEVNAME_SIZE];
713 if (!rdev->sb_page) {
721 if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ))
727 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
728 bdevname(rdev->bdev,b));
732 static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
734 return sb1->set_uuid0 == sb2->set_uuid0 &&
735 sb1->set_uuid1 == sb2->set_uuid1 &&
736 sb1->set_uuid2 == sb2->set_uuid2 &&
737 sb1->set_uuid3 == sb2->set_uuid3;
740 static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
743 mdp_super_t *tmp1, *tmp2;
745 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
746 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
748 if (!tmp1 || !tmp2) {
750 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
758 * nr_disks is not constant
763 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
771 static u32 md_csum_fold(u32 csum)
773 csum = (csum & 0xffff) + (csum >> 16);
774 return (csum & 0xffff) + (csum >> 16);
777 static unsigned int calc_sb_csum(mdp_super_t * sb)
780 u32 *sb32 = (u32*)sb;
782 unsigned int disk_csum, csum;
784 disk_csum = sb->sb_csum;
787 for (i = 0; i < MD_SB_BYTES/4 ; i++)
789 csum = (newcsum & 0xffffffff) + (newcsum>>32);
793 /* This used to use csum_partial, which was wrong for several
794 * reasons including that different results are returned on
795 * different architectures. It isn't critical that we get exactly
796 * the same return value as before (we always csum_fold before
797 * testing, and that removes any differences). However as we
798 * know that csum_partial always returned a 16bit value on
799 * alphas, do a fold to maximise conformity to previous behaviour.
801 sb->sb_csum = md_csum_fold(disk_csum);
803 sb->sb_csum = disk_csum;
810 * Handle superblock details.
811 * We want to be able to handle multiple superblock formats
812 * so we have a common interface to them all, and an array of
813 * different handlers.
814 * We rely on user-space to write the initial superblock, and support
815 * reading and updating of superblocks.
816 * Interface methods are:
817 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
818 * loads and validates a superblock on dev.
819 * if refdev != NULL, compare superblocks on both devices
821 * 0 - dev has a superblock that is compatible with refdev
822 * 1 - dev has a superblock that is compatible and newer than refdev
823 * so dev should be used as the refdev in future
824 * -EINVAL superblock incompatible or invalid
825 * -othererror e.g. -EIO
827 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
828 * Verify that dev is acceptable into mddev.
829 * The first time, mddev->raid_disks will be 0, and data from
830 * dev should be merged in. Subsequent calls check that dev
831 * is new enough. Return 0 or -EINVAL
833 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
834 * Update the superblock for rdev with data in mddev
835 * This does not write to disc.
841 struct module *owner;
842 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
844 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
845 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
846 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
847 sector_t num_sectors);
851 * Check that the given mddev has no bitmap.
853 * This function is called from the run method of all personalities that do not
854 * support bitmaps. It prints an error message and returns non-zero if mddev
855 * has a bitmap. Otherwise, it returns 0.
858 int md_check_no_bitmap(mddev_t *mddev)
860 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
862 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
863 mdname(mddev), mddev->pers->name);
866 EXPORT_SYMBOL(md_check_no_bitmap);
869 * load_super for 0.90.0
871 static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
873 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
878 * Calculate the position of the superblock (512byte sectors),
879 * it's at the end of the disk.
881 * It also happens to be a multiple of 4Kb.
883 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
885 ret = read_disk_sb(rdev, MD_SB_BYTES);
890 bdevname(rdev->bdev, b);
891 sb = (mdp_super_t*)page_address(rdev->sb_page);
893 if (sb->md_magic != MD_SB_MAGIC) {
894 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
899 if (sb->major_version != 0 ||
900 sb->minor_version < 90 ||
901 sb->minor_version > 91) {
902 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
903 sb->major_version, sb->minor_version,
908 if (sb->raid_disks <= 0)
911 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
912 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
917 rdev->preferred_minor = sb->md_minor;
918 rdev->data_offset = 0;
919 rdev->sb_size = MD_SB_BYTES;
921 if (sb->level == LEVEL_MULTIPATH)
924 rdev->desc_nr = sb->this_disk.number;
930 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
931 if (!uuid_equal(refsb, sb)) {
932 printk(KERN_WARNING "md: %s has different UUID to %s\n",
933 b, bdevname(refdev->bdev,b2));
936 if (!sb_equal(refsb, sb)) {
937 printk(KERN_WARNING "md: %s has same UUID"
938 " but different superblock to %s\n",
939 b, bdevname(refdev->bdev, b2));
943 ev2 = md_event(refsb);
949 rdev->sectors = rdev->sb_start;
951 if (rdev->sectors < sb->size * 2 && sb->level > 1)
952 /* "this cannot possibly happen" ... */
960 * validate_super for 0.90.0
962 static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
965 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
966 __u64 ev1 = md_event(sb);
968 rdev->raid_disk = -1;
969 clear_bit(Faulty, &rdev->flags);
970 clear_bit(In_sync, &rdev->flags);
971 clear_bit(WriteMostly, &rdev->flags);
972 clear_bit(BarriersNotsupp, &rdev->flags);
974 if (mddev->raid_disks == 0) {
975 mddev->major_version = 0;
976 mddev->minor_version = sb->minor_version;
977 mddev->patch_version = sb->patch_version;
979 mddev->chunk_sectors = sb->chunk_size >> 9;
980 mddev->ctime = sb->ctime;
981 mddev->utime = sb->utime;
982 mddev->level = sb->level;
983 mddev->clevel[0] = 0;
984 mddev->layout = sb->layout;
985 mddev->raid_disks = sb->raid_disks;
986 mddev->dev_sectors = sb->size * 2;
988 mddev->bitmap_info.offset = 0;
989 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
991 if (mddev->minor_version >= 91) {
992 mddev->reshape_position = sb->reshape_position;
993 mddev->delta_disks = sb->delta_disks;
994 mddev->new_level = sb->new_level;
995 mddev->new_layout = sb->new_layout;
996 mddev->new_chunk_sectors = sb->new_chunk >> 9;
998 mddev->reshape_position = MaxSector;
999 mddev->delta_disks = 0;
1000 mddev->new_level = mddev->level;
1001 mddev->new_layout = mddev->layout;
1002 mddev->new_chunk_sectors = mddev->chunk_sectors;
1005 if (sb->state & (1<<MD_SB_CLEAN))
1006 mddev->recovery_cp = MaxSector;
1008 if (sb->events_hi == sb->cp_events_hi &&
1009 sb->events_lo == sb->cp_events_lo) {
1010 mddev->recovery_cp = sb->recovery_cp;
1012 mddev->recovery_cp = 0;
1015 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1016 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1017 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1018 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1020 mddev->max_disks = MD_SB_DISKS;
1022 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
1023 mddev->bitmap_info.file == NULL)
1024 mddev->bitmap_info.offset =
1025 mddev->bitmap_info.default_offset;
1027 } else if (mddev->pers == NULL) {
1028 /* Insist on good event counter while assembling */
1030 if (ev1 < mddev->events)
1032 } else if (mddev->bitmap) {
1033 /* if adding to array with a bitmap, then we can accept an
1034 * older device ... but not too old.
1036 if (ev1 < mddev->bitmap->events_cleared)
1039 if (ev1 < mddev->events)
1040 /* just a hot-add of a new device, leave raid_disk at -1 */
1044 if (mddev->level != LEVEL_MULTIPATH) {
1045 desc = sb->disks + rdev->desc_nr;
1047 if (desc->state & (1<<MD_DISK_FAULTY))
1048 set_bit(Faulty, &rdev->flags);
1049 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1050 desc->raid_disk < mddev->raid_disks */) {
1051 set_bit(In_sync, &rdev->flags);
1052 rdev->raid_disk = desc->raid_disk;
1053 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1054 /* active but not in sync implies recovery up to
1055 * reshape position. We don't know exactly where
1056 * that is, so set to zero for now */
1057 if (mddev->minor_version >= 91) {
1058 rdev->recovery_offset = 0;
1059 rdev->raid_disk = desc->raid_disk;
1062 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1063 set_bit(WriteMostly, &rdev->flags);
1064 } else /* MULTIPATH are always insync */
1065 set_bit(In_sync, &rdev->flags);
1070 * sync_super for 0.90.0
1072 static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1076 int next_spare = mddev->raid_disks;
1079 /* make rdev->sb match mddev data..
1082 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1083 * 3/ any empty disks < next_spare become removed
1085 * disks[0] gets initialised to REMOVED because
1086 * we cannot be sure from other fields if it has
1087 * been initialised or not.
1090 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1092 rdev->sb_size = MD_SB_BYTES;
1094 sb = (mdp_super_t*)page_address(rdev->sb_page);
1096 memset(sb, 0, sizeof(*sb));
1098 sb->md_magic = MD_SB_MAGIC;
1099 sb->major_version = mddev->major_version;
1100 sb->patch_version = mddev->patch_version;
1101 sb->gvalid_words = 0; /* ignored */
1102 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1103 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1104 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1105 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1107 sb->ctime = mddev->ctime;
1108 sb->level = mddev->level;
1109 sb->size = mddev->dev_sectors / 2;
1110 sb->raid_disks = mddev->raid_disks;
1111 sb->md_minor = mddev->md_minor;
1112 sb->not_persistent = 0;
1113 sb->utime = mddev->utime;
1115 sb->events_hi = (mddev->events>>32);
1116 sb->events_lo = (u32)mddev->events;
1118 if (mddev->reshape_position == MaxSector)
1119 sb->minor_version = 90;
1121 sb->minor_version = 91;
1122 sb->reshape_position = mddev->reshape_position;
1123 sb->new_level = mddev->new_level;
1124 sb->delta_disks = mddev->delta_disks;
1125 sb->new_layout = mddev->new_layout;
1126 sb->new_chunk = mddev->new_chunk_sectors << 9;
1128 mddev->minor_version = sb->minor_version;
1131 sb->recovery_cp = mddev->recovery_cp;
1132 sb->cp_events_hi = (mddev->events>>32);
1133 sb->cp_events_lo = (u32)mddev->events;
1134 if (mddev->recovery_cp == MaxSector)
1135 sb->state = (1<< MD_SB_CLEAN);
1137 sb->recovery_cp = 0;
1139 sb->layout = mddev->layout;
1140 sb->chunk_size = mddev->chunk_sectors << 9;
1142 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
1143 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1145 sb->disks[0].state = (1<<MD_DISK_REMOVED);
1146 list_for_each_entry(rdev2, &mddev->disks, same_set) {
1149 int is_active = test_bit(In_sync, &rdev2->flags);
1151 if (rdev2->raid_disk >= 0 &&
1152 sb->minor_version >= 91)
1153 /* we have nowhere to store the recovery_offset,
1154 * but if it is not below the reshape_position,
1155 * we can piggy-back on that.
1158 if (rdev2->raid_disk < 0 ||
1159 test_bit(Faulty, &rdev2->flags))
1162 desc_nr = rdev2->raid_disk;
1164 desc_nr = next_spare++;
1165 rdev2->desc_nr = desc_nr;
1166 d = &sb->disks[rdev2->desc_nr];
1168 d->number = rdev2->desc_nr;
1169 d->major = MAJOR(rdev2->bdev->bd_dev);
1170 d->minor = MINOR(rdev2->bdev->bd_dev);
1172 d->raid_disk = rdev2->raid_disk;
1174 d->raid_disk = rdev2->desc_nr; /* compatibility */
1175 if (test_bit(Faulty, &rdev2->flags))
1176 d->state = (1<<MD_DISK_FAULTY);
1177 else if (is_active) {
1178 d->state = (1<<MD_DISK_ACTIVE);
1179 if (test_bit(In_sync, &rdev2->flags))
1180 d->state |= (1<<MD_DISK_SYNC);
1188 if (test_bit(WriteMostly, &rdev2->flags))
1189 d->state |= (1<<MD_DISK_WRITEMOSTLY);
1191 /* now set the "removed" and "faulty" bits on any missing devices */
1192 for (i=0 ; i < mddev->raid_disks ; i++) {
1193 mdp_disk_t *d = &sb->disks[i];
1194 if (d->state == 0 && d->number == 0) {
1197 d->state = (1<<MD_DISK_REMOVED);
1198 d->state |= (1<<MD_DISK_FAULTY);
1202 sb->nr_disks = nr_disks;
1203 sb->active_disks = active;
1204 sb->working_disks = working;
1205 sb->failed_disks = failed;
1206 sb->spare_disks = spare;
1208 sb->this_disk = sb->disks[rdev->desc_nr];
1209 sb->sb_csum = calc_sb_csum(sb);
1213 * rdev_size_change for 0.90.0
1215 static unsigned long long
1216 super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
1218 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1219 return 0; /* component must fit device */
1220 if (rdev->mddev->bitmap_info.offset)
1221 return 0; /* can't move bitmap */
1222 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
1223 if (!num_sectors || num_sectors > rdev->sb_start)
1224 num_sectors = rdev->sb_start;
1225 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1227 md_super_wait(rdev->mddev);
1228 return num_sectors / 2; /* kB for sysfs */
1233 * version 1 superblock
1236 static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
1240 unsigned long long newcsum;
1241 int size = 256 + le32_to_cpu(sb->max_dev)*2;
1242 __le32 *isuper = (__le32*)sb;
1245 disk_csum = sb->sb_csum;
1248 for (i=0; size>=4; size -= 4 )
1249 newcsum += le32_to_cpu(*isuper++);
1252 newcsum += le16_to_cpu(*(__le16*) isuper);
1254 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1255 sb->sb_csum = disk_csum;
1256 return cpu_to_le32(csum);
1259 static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1261 struct mdp_superblock_1 *sb;
1264 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1268 * Calculate the position of the superblock in 512byte sectors.
1269 * It is always aligned to a 4K boundary and
1270 * depeding on minor_version, it can be:
1271 * 0: At least 8K, but less than 12K, from end of device
1272 * 1: At start of device
1273 * 2: 4K from start of device.
1275 switch(minor_version) {
1277 sb_start = rdev->bdev->bd_inode->i_size >> 9;
1279 sb_start &= ~(sector_t)(4*2-1);
1290 rdev->sb_start = sb_start;
1292 /* superblock is rarely larger than 1K, but it can be larger,
1293 * and it is safe to read 4k, so we do that
1295 ret = read_disk_sb(rdev, 4096);
1296 if (ret) return ret;
1299 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1301 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1302 sb->major_version != cpu_to_le32(1) ||
1303 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1304 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
1305 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
1308 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1309 printk("md: invalid superblock checksum on %s\n",
1310 bdevname(rdev->bdev,b));
1313 if (le64_to_cpu(sb->data_size) < 10) {
1314 printk("md: data_size too small on %s\n",
1315 bdevname(rdev->bdev,b));
1319 rdev->preferred_minor = 0xffff;
1320 rdev->data_offset = le64_to_cpu(sb->data_offset);
1321 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
1323 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
1324 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1325 if (rdev->sb_size & bmask)
1326 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1329 && rdev->data_offset < sb_start + (rdev->sb_size/512))
1332 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1335 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1341 struct mdp_superblock_1 *refsb =
1342 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1344 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1345 sb->level != refsb->level ||
1346 sb->layout != refsb->layout ||
1347 sb->chunksize != refsb->chunksize) {
1348 printk(KERN_WARNING "md: %s has strangely different"
1349 " superblock to %s\n",
1350 bdevname(rdev->bdev,b),
1351 bdevname(refdev->bdev,b2));
1354 ev1 = le64_to_cpu(sb->events);
1355 ev2 = le64_to_cpu(refsb->events);
1363 rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) -
1364 le64_to_cpu(sb->data_offset);
1366 rdev->sectors = rdev->sb_start;
1367 if (rdev->sectors < le64_to_cpu(sb->data_size))
1369 rdev->sectors = le64_to_cpu(sb->data_size);
1370 if (le64_to_cpu(sb->size) > rdev->sectors)
1375 static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1377 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1378 __u64 ev1 = le64_to_cpu(sb->events);
1380 rdev->raid_disk = -1;
1381 clear_bit(Faulty, &rdev->flags);
1382 clear_bit(In_sync, &rdev->flags);
1383 clear_bit(WriteMostly, &rdev->flags);
1384 clear_bit(BarriersNotsupp, &rdev->flags);
1386 if (mddev->raid_disks == 0) {
1387 mddev->major_version = 1;
1388 mddev->patch_version = 0;
1389 mddev->external = 0;
1390 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
1391 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1392 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1393 mddev->level = le32_to_cpu(sb->level);
1394 mddev->clevel[0] = 0;
1395 mddev->layout = le32_to_cpu(sb->layout);
1396 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1397 mddev->dev_sectors = le64_to_cpu(sb->size);
1398 mddev->events = ev1;
1399 mddev->bitmap_info.offset = 0;
1400 mddev->bitmap_info.default_offset = 1024 >> 9;
1402 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1403 memcpy(mddev->uuid, sb->set_uuid, 16);
1405 mddev->max_disks = (4096-256)/2;
1407 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
1408 mddev->bitmap_info.file == NULL )
1409 mddev->bitmap_info.offset =
1410 (__s32)le32_to_cpu(sb->bitmap_offset);
1412 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1413 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1414 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1415 mddev->new_level = le32_to_cpu(sb->new_level);
1416 mddev->new_layout = le32_to_cpu(sb->new_layout);
1417 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
1419 mddev->reshape_position = MaxSector;
1420 mddev->delta_disks = 0;
1421 mddev->new_level = mddev->level;
1422 mddev->new_layout = mddev->layout;
1423 mddev->new_chunk_sectors = mddev->chunk_sectors;
1426 } else if (mddev->pers == NULL) {
1427 /* Insist of good event counter while assembling */
1429 if (ev1 < mddev->events)
1431 } else if (mddev->bitmap) {
1432 /* If adding to array with a bitmap, then we can accept an
1433 * older device, but not too old.
1435 if (ev1 < mddev->bitmap->events_cleared)
1438 if (ev1 < mddev->events)
1439 /* just a hot-add of a new device, leave raid_disk at -1 */
1442 if (mddev->level != LEVEL_MULTIPATH) {
1444 if (rdev->desc_nr < 0 ||
1445 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1449 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1451 case 0xffff: /* spare */
1453 case 0xfffe: /* faulty */
1454 set_bit(Faulty, &rdev->flags);
1457 if ((le32_to_cpu(sb->feature_map) &
1458 MD_FEATURE_RECOVERY_OFFSET))
1459 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1461 set_bit(In_sync, &rdev->flags);
1462 rdev->raid_disk = role;
1465 if (sb->devflags & WriteMostly1)
1466 set_bit(WriteMostly, &rdev->flags);
1467 } else /* MULTIPATH are always insync */
1468 set_bit(In_sync, &rdev->flags);
1473 static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1475 struct mdp_superblock_1 *sb;
1478 /* make rdev->sb match mddev and rdev data. */
1480 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1482 sb->feature_map = 0;
1484 sb->recovery_offset = cpu_to_le64(0);
1485 memset(sb->pad1, 0, sizeof(sb->pad1));
1486 memset(sb->pad2, 0, sizeof(sb->pad2));
1487 memset(sb->pad3, 0, sizeof(sb->pad3));
1489 sb->utime = cpu_to_le64((__u64)mddev->utime);
1490 sb->events = cpu_to_le64(mddev->events);
1492 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1494 sb->resync_offset = cpu_to_le64(0);
1496 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
1498 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
1499 sb->size = cpu_to_le64(mddev->dev_sectors);
1500 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
1501 sb->level = cpu_to_le32(mddev->level);
1502 sb->layout = cpu_to_le32(mddev->layout);
1504 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1505 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
1506 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
1509 if (rdev->raid_disk >= 0 &&
1510 !test_bit(In_sync, &rdev->flags)) {
1512 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1513 sb->recovery_offset =
1514 cpu_to_le64(rdev->recovery_offset);
1517 if (mddev->reshape_position != MaxSector) {
1518 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1519 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1520 sb->new_layout = cpu_to_le32(mddev->new_layout);
1521 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1522 sb->new_level = cpu_to_le32(mddev->new_level);
1523 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
1527 list_for_each_entry(rdev2, &mddev->disks, same_set)
1528 if (rdev2->desc_nr+1 > max_dev)
1529 max_dev = rdev2->desc_nr+1;
1531 if (max_dev > le32_to_cpu(sb->max_dev)) {
1533 sb->max_dev = cpu_to_le32(max_dev);
1534 rdev->sb_size = max_dev * 2 + 256;
1535 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1536 if (rdev->sb_size & bmask)
1537 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1539 for (i=0; i<max_dev;i++)
1540 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1542 list_for_each_entry(rdev2, &mddev->disks, same_set) {
1544 if (test_bit(Faulty, &rdev2->flags))
1545 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1546 else if (test_bit(In_sync, &rdev2->flags))
1547 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1548 else if (rdev2->raid_disk >= 0)
1549 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1551 sb->dev_roles[i] = cpu_to_le16(0xffff);
1554 sb->sb_csum = calc_sb_1_csum(sb);
1557 static unsigned long long
1558 super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
1560 struct mdp_superblock_1 *sb;
1561 sector_t max_sectors;
1562 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1563 return 0; /* component must fit device */
1564 if (rdev->sb_start < rdev->data_offset) {
1565 /* minor versions 1 and 2; superblock before data */
1566 max_sectors = rdev->bdev->bd_inode->i_size >> 9;
1567 max_sectors -= rdev->data_offset;
1568 if (!num_sectors || num_sectors > max_sectors)
1569 num_sectors = max_sectors;
1570 } else if (rdev->mddev->bitmap_info.offset) {
1571 /* minor version 0 with bitmap we can't move */
1574 /* minor version 0; superblock after data */
1576 sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1577 sb_start &= ~(sector_t)(4*2 - 1);
1578 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
1579 if (!num_sectors || num_sectors > max_sectors)
1580 num_sectors = max_sectors;
1581 rdev->sb_start = sb_start;
1583 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
1584 sb->data_size = cpu_to_le64(num_sectors);
1585 sb->super_offset = rdev->sb_start;
1586 sb->sb_csum = calc_sb_1_csum(sb);
1587 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1589 md_super_wait(rdev->mddev);
1590 return num_sectors / 2; /* kB for sysfs */
1593 static struct super_type super_types[] = {
1596 .owner = THIS_MODULE,
1597 .load_super = super_90_load,
1598 .validate_super = super_90_validate,
1599 .sync_super = super_90_sync,
1600 .rdev_size_change = super_90_rdev_size_change,
1604 .owner = THIS_MODULE,
1605 .load_super = super_1_load,
1606 .validate_super = super_1_validate,
1607 .sync_super = super_1_sync,
1608 .rdev_size_change = super_1_rdev_size_change,
1612 static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1614 mdk_rdev_t *rdev, *rdev2;
1617 rdev_for_each_rcu(rdev, mddev1)
1618 rdev_for_each_rcu(rdev2, mddev2)
1619 if (rdev->bdev->bd_contains ==
1620 rdev2->bdev->bd_contains) {
1628 static LIST_HEAD(pending_raid_disks);
1631 * Try to register data integrity profile for an mddev
1633 * This is called when an array is started and after a disk has been kicked
1634 * from the array. It only succeeds if all working and active component devices
1635 * are integrity capable with matching profiles.
1637 int md_integrity_register(mddev_t *mddev)
1639 mdk_rdev_t *rdev, *reference = NULL;
1641 if (list_empty(&mddev->disks))
1642 return 0; /* nothing to do */
1643 if (blk_get_integrity(mddev->gendisk))
1644 return 0; /* already registered */
1645 list_for_each_entry(rdev, &mddev->disks, same_set) {
1646 /* skip spares and non-functional disks */
1647 if (test_bit(Faulty, &rdev->flags))
1649 if (rdev->raid_disk < 0)
1652 * If at least one rdev is not integrity capable, we can not
1653 * enable data integrity for the md device.
1655 if (!bdev_get_integrity(rdev->bdev))
1658 /* Use the first rdev as the reference */
1662 /* does this rdev's profile match the reference profile? */
1663 if (blk_integrity_compare(reference->bdev->bd_disk,
1664 rdev->bdev->bd_disk) < 0)
1668 * All component devices are integrity capable and have matching
1669 * profiles, register the common profile for the md device.
1671 if (blk_integrity_register(mddev->gendisk,
1672 bdev_get_integrity(reference->bdev)) != 0) {
1673 printk(KERN_ERR "md: failed to register integrity for %s\n",
1677 printk(KERN_NOTICE "md: data integrity on %s enabled\n",
1681 EXPORT_SYMBOL(md_integrity_register);
1683 /* Disable data integrity if non-capable/non-matching disk is being added */
1684 void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
1686 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1687 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1689 if (!bi_mddev) /* nothing to do */
1691 if (rdev->raid_disk < 0) /* skip spares */
1693 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1694 rdev->bdev->bd_disk) >= 0)
1696 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1697 blk_integrity_unregister(mddev->gendisk);
1699 EXPORT_SYMBOL(md_integrity_add_rdev);
1701 static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1703 char b[BDEVNAME_SIZE];
1713 /* prevent duplicates */
1714 if (find_rdev(mddev, rdev->bdev->bd_dev))
1717 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1718 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1719 rdev->sectors < mddev->dev_sectors)) {
1721 /* Cannot change size, so fail
1722 * If mddev->level <= 0, then we don't care
1723 * about aligning sizes (e.g. linear)
1725 if (mddev->level > 0)
1728 mddev->dev_sectors = rdev->sectors;
1731 /* Verify rdev->desc_nr is unique.
1732 * If it is -1, assign a free number, else
1733 * check number is not in use
1735 if (rdev->desc_nr < 0) {
1737 if (mddev->pers) choice = mddev->raid_disks;
1738 while (find_rdev_nr(mddev, choice))
1740 rdev->desc_nr = choice;
1742 if (find_rdev_nr(mddev, rdev->desc_nr))
1745 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1746 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1747 mdname(mddev), mddev->max_disks);
1750 bdevname(rdev->bdev,b);
1751 while ( (s=strchr(b, '/')) != NULL)
1754 rdev->mddev = mddev;
1755 printk(KERN_INFO "md: bind<%s>\n", b);
1757 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
1760 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
1761 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1762 kobject_del(&rdev->kobj);
1765 rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state");
1767 list_add_rcu(&rdev->same_set, &mddev->disks);
1768 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
1770 /* May as well allow recovery to be retried once */
1771 mddev->recovery_disabled = 0;
1776 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1781 static void md_delayed_delete(struct work_struct *ws)
1783 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1784 kobject_del(&rdev->kobj);
1785 kobject_put(&rdev->kobj);
1788 static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1790 char b[BDEVNAME_SIZE];
1795 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
1796 list_del_rcu(&rdev->same_set);
1797 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1799 sysfs_remove_link(&rdev->kobj, "block");
1800 sysfs_put(rdev->sysfs_state);
1801 rdev->sysfs_state = NULL;
1802 /* We need to delay this, otherwise we can deadlock when
1803 * writing to 'remove' to "dev/state". We also need
1804 * to delay it due to rcu usage.
1807 INIT_WORK(&rdev->del_work, md_delayed_delete);
1808 kobject_get(&rdev->kobj);
1809 schedule_work(&rdev->del_work);
1813 * prevent the device from being mounted, repartitioned or
1814 * otherwise reused by a RAID array (or any other kernel
1815 * subsystem), by bd_claiming the device.
1817 static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
1820 struct block_device *bdev;
1821 char b[BDEVNAME_SIZE];
1823 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
1825 printk(KERN_ERR "md: could not open %s.\n",
1826 __bdevname(dev, b));
1827 return PTR_ERR(bdev);
1829 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
1831 printk(KERN_ERR "md: could not bd_claim %s.\n",
1833 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
1837 set_bit(AllReserved, &rdev->flags);
1842 static void unlock_rdev(mdk_rdev_t *rdev)
1844 struct block_device *bdev = rdev->bdev;
1849 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
1852 void md_autodetect_dev(dev_t dev);
1854 static void export_rdev(mdk_rdev_t * rdev)
1856 char b[BDEVNAME_SIZE];
1857 printk(KERN_INFO "md: export_rdev(%s)\n",
1858 bdevname(rdev->bdev,b));
1863 if (test_bit(AutoDetected, &rdev->flags))
1864 md_autodetect_dev(rdev->bdev->bd_dev);
1867 kobject_put(&rdev->kobj);
1870 static void kick_rdev_from_array(mdk_rdev_t * rdev)
1872 unbind_rdev_from_array(rdev);
1876 static void export_array(mddev_t *mddev)
1878 mdk_rdev_t *rdev, *tmp;
1880 rdev_for_each(rdev, tmp, mddev) {
1885 kick_rdev_from_array(rdev);
1887 if (!list_empty(&mddev->disks))
1889 mddev->raid_disks = 0;
1890 mddev->major_version = 0;
1893 static void print_desc(mdp_disk_t *desc)
1895 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1896 desc->major,desc->minor,desc->raid_disk,desc->state);
1899 static void print_sb_90(mdp_super_t *sb)
1904 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1905 sb->major_version, sb->minor_version, sb->patch_version,
1906 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1908 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1909 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1910 sb->md_minor, sb->layout, sb->chunk_size);
1911 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1912 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1913 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1914 sb->failed_disks, sb->spare_disks,
1915 sb->sb_csum, (unsigned long)sb->events_lo);
1918 for (i = 0; i < MD_SB_DISKS; i++) {
1921 desc = sb->disks + i;
1922 if (desc->number || desc->major || desc->minor ||
1923 desc->raid_disk || (desc->state && (desc->state != 4))) {
1924 printk(" D %2d: ", i);
1928 printk(KERN_INFO "md: THIS: ");
1929 print_desc(&sb->this_disk);
1932 static void print_sb_1(struct mdp_superblock_1 *sb)
1936 uuid = sb->set_uuid;
1938 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%02x%02x%02x%02x"
1939 ":%02x%02x:%02x%02x:%02x%02x:%02x%02x%02x%02x%02x%02x>\n"
1940 "md: Name: \"%s\" CT:%llu\n",
1941 le32_to_cpu(sb->major_version),
1942 le32_to_cpu(sb->feature_map),
1943 uuid[0], uuid[1], uuid[2], uuid[3],
1944 uuid[4], uuid[5], uuid[6], uuid[7],
1945 uuid[8], uuid[9], uuid[10], uuid[11],
1946 uuid[12], uuid[13], uuid[14], uuid[15],
1948 (unsigned long long)le64_to_cpu(sb->ctime)
1949 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
1951 uuid = sb->device_uuid;
1953 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
1955 "md: Dev:%08x UUID: %02x%02x%02x%02x:%02x%02x:%02x%02x:%02x%02x"
1956 ":%02x%02x%02x%02x%02x%02x\n"
1957 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
1958 "md: (MaxDev:%u) \n",
1959 le32_to_cpu(sb->level),
1960 (unsigned long long)le64_to_cpu(sb->size),
1961 le32_to_cpu(sb->raid_disks),
1962 le32_to_cpu(sb->layout),
1963 le32_to_cpu(sb->chunksize),
1964 (unsigned long long)le64_to_cpu(sb->data_offset),
1965 (unsigned long long)le64_to_cpu(sb->data_size),
1966 (unsigned long long)le64_to_cpu(sb->super_offset),
1967 (unsigned long long)le64_to_cpu(sb->recovery_offset),
1968 le32_to_cpu(sb->dev_number),
1969 uuid[0], uuid[1], uuid[2], uuid[3],
1970 uuid[4], uuid[5], uuid[6], uuid[7],
1971 uuid[8], uuid[9], uuid[10], uuid[11],
1972 uuid[12], uuid[13], uuid[14], uuid[15],
1974 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
1975 (unsigned long long)le64_to_cpu(sb->events),
1976 (unsigned long long)le64_to_cpu(sb->resync_offset),
1977 le32_to_cpu(sb->sb_csum),
1978 le32_to_cpu(sb->max_dev)
1982 static void print_rdev(mdk_rdev_t *rdev, int major_version)
1984 char b[BDEVNAME_SIZE];
1985 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
1986 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
1987 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1989 if (rdev->sb_loaded) {
1990 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
1991 switch (major_version) {
1993 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
1996 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
2000 printk(KERN_INFO "md: no rdev superblock!\n");
2003 static void md_print_devices(void)
2005 struct list_head *tmp;
2008 char b[BDEVNAME_SIZE];
2011 printk("md: **********************************\n");
2012 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2013 printk("md: **********************************\n");
2014 for_each_mddev(mddev, tmp) {
2017 bitmap_print_sb(mddev->bitmap);
2019 printk("%s: ", mdname(mddev));
2020 list_for_each_entry(rdev, &mddev->disks, same_set)
2021 printk("<%s>", bdevname(rdev->bdev,b));
2024 list_for_each_entry(rdev, &mddev->disks, same_set)
2025 print_rdev(rdev, mddev->major_version);
2027 printk("md: **********************************\n");
2032 static void sync_sbs(mddev_t * mddev, int nospares)
2034 /* Update each superblock (in-memory image), but
2035 * if we are allowed to, skip spares which already
2036 * have the right event counter, or have one earlier
2037 * (which would mean they aren't being marked as dirty
2038 * with the rest of the array)
2042 /* First make sure individual recovery_offsets are correct */
2043 list_for_each_entry(rdev, &mddev->disks, same_set) {
2044 if (rdev->raid_disk >= 0 &&
2045 !test_bit(In_sync, &rdev->flags) &&
2046 mddev->curr_resync_completed > rdev->recovery_offset)
2047 rdev->recovery_offset = mddev->curr_resync_completed;
2050 list_for_each_entry(rdev, &mddev->disks, same_set) {
2051 if (rdev->sb_events == mddev->events ||
2053 rdev->raid_disk < 0 &&
2054 (rdev->sb_events&1)==0 &&
2055 rdev->sb_events+1 == mddev->events)) {
2056 /* Don't update this superblock */
2057 rdev->sb_loaded = 2;
2059 super_types[mddev->major_version].
2060 sync_super(mddev, rdev);
2061 rdev->sb_loaded = 1;
2066 static void md_update_sb(mddev_t * mddev, int force_change)
2072 mddev->utime = get_seconds();
2073 if (mddev->external)
2076 spin_lock_irq(&mddev->write_lock);
2078 set_bit(MD_CHANGE_PENDING, &mddev->flags);
2079 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2081 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2082 /* just a clean<-> dirty transition, possibly leave spares alone,
2083 * though if events isn't the right even/odd, we will have to do
2089 if (mddev->degraded)
2090 /* If the array is degraded, then skipping spares is both
2091 * dangerous and fairly pointless.
2092 * Dangerous because a device that was removed from the array
2093 * might have a event_count that still looks up-to-date,
2094 * so it can be re-added without a resync.
2095 * Pointless because if there are any spares to skip,
2096 * then a recovery will happen and soon that array won't
2097 * be degraded any more and the spare can go back to sleep then.
2101 sync_req = mddev->in_sync;
2103 /* If this is just a dirty<->clean transition, and the array is clean
2104 * and 'events' is odd, we can roll back to the previous clean state */
2106 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
2107 && (mddev->events & 1)
2108 && mddev->events != 1)
2111 /* otherwise we have to go forward and ... */
2113 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
2114 /* .. if the array isn't clean, an 'even' event must also go
2116 if ((mddev->events&1)==0)
2119 /* otherwise an 'odd' event must go to spares */
2120 if ((mddev->events&1))
2125 if (!mddev->events) {
2127 * oops, this 64-bit counter should never wrap.
2128 * Either we are in around ~1 trillion A.C., assuming
2129 * 1 reboot per second, or we have a bug:
2136 * do not write anything to disk if using
2137 * nonpersistent superblocks
2139 if (!mddev->persistent) {
2140 if (!mddev->external)
2141 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2143 spin_unlock_irq(&mddev->write_lock);
2144 wake_up(&mddev->sb_wait);
2147 sync_sbs(mddev, nospares);
2148 spin_unlock_irq(&mddev->write_lock);
2151 "md: updating %s RAID superblock on device (in sync %d)\n",
2152 mdname(mddev),mddev->in_sync);
2154 bitmap_update_sb(mddev->bitmap);
2155 list_for_each_entry(rdev, &mddev->disks, same_set) {
2156 char b[BDEVNAME_SIZE];
2157 dprintk(KERN_INFO "md: ");
2158 if (rdev->sb_loaded != 1)
2159 continue; /* no noise on spare devices */
2160 if (test_bit(Faulty, &rdev->flags))
2161 dprintk("(skipping faulty ");
2163 dprintk("%s ", bdevname(rdev->bdev,b));
2164 if (!test_bit(Faulty, &rdev->flags)) {
2165 md_super_write(mddev,rdev,
2166 rdev->sb_start, rdev->sb_size,
2168 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
2169 bdevname(rdev->bdev,b),
2170 (unsigned long long)rdev->sb_start);
2171 rdev->sb_events = mddev->events;
2175 if (mddev->level == LEVEL_MULTIPATH)
2176 /* only need to write one superblock... */
2179 md_super_wait(mddev);
2180 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
2182 spin_lock_irq(&mddev->write_lock);
2183 if (mddev->in_sync != sync_req ||
2184 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
2185 /* have to write it out again */
2186 spin_unlock_irq(&mddev->write_lock);
2189 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2190 spin_unlock_irq(&mddev->write_lock);
2191 wake_up(&mddev->sb_wait);
2192 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2193 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
2197 /* words written to sysfs files may, or may not, be \n terminated.
2198 * We want to accept with case. For this we use cmd_match.
2200 static int cmd_match(const char *cmd, const char *str)
2202 /* See if cmd, written into a sysfs file, matches
2203 * str. They must either be the same, or cmd can
2204 * have a trailing newline
2206 while (*cmd && *str && *cmd == *str) {
2217 struct rdev_sysfs_entry {
2218 struct attribute attr;
2219 ssize_t (*show)(mdk_rdev_t *, char *);
2220 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2224 state_show(mdk_rdev_t *rdev, char *page)
2229 if (test_bit(Faulty, &rdev->flags)) {
2230 len+= sprintf(page+len, "%sfaulty",sep);
2233 if (test_bit(In_sync, &rdev->flags)) {
2234 len += sprintf(page+len, "%sin_sync",sep);
2237 if (test_bit(WriteMostly, &rdev->flags)) {
2238 len += sprintf(page+len, "%swrite_mostly",sep);
2241 if (test_bit(Blocked, &rdev->flags)) {
2242 len += sprintf(page+len, "%sblocked", sep);
2245 if (!test_bit(Faulty, &rdev->flags) &&
2246 !test_bit(In_sync, &rdev->flags)) {
2247 len += sprintf(page+len, "%sspare", sep);
2250 return len+sprintf(page+len, "\n");
2254 state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2257 * faulty - simulates and error
2258 * remove - disconnects the device
2259 * writemostly - sets write_mostly
2260 * -writemostly - clears write_mostly
2261 * blocked - sets the Blocked flag
2262 * -blocked - clears the Blocked flag
2263 * insync - sets Insync providing device isn't active
2266 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2267 md_error(rdev->mddev, rdev);
2269 } else if (cmd_match(buf, "remove")) {
2270 if (rdev->raid_disk >= 0)
2273 mddev_t *mddev = rdev->mddev;
2274 kick_rdev_from_array(rdev);
2276 md_update_sb(mddev, 1);
2277 md_new_event(mddev);
2280 } else if (cmd_match(buf, "writemostly")) {
2281 set_bit(WriteMostly, &rdev->flags);
2283 } else if (cmd_match(buf, "-writemostly")) {
2284 clear_bit(WriteMostly, &rdev->flags);
2286 } else if (cmd_match(buf, "blocked")) {
2287 set_bit(Blocked, &rdev->flags);
2289 } else if (cmd_match(buf, "-blocked")) {
2290 clear_bit(Blocked, &rdev->flags);
2291 wake_up(&rdev->blocked_wait);
2292 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2293 md_wakeup_thread(rdev->mddev->thread);
2296 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2297 set_bit(In_sync, &rdev->flags);
2300 if (!err && rdev->sysfs_state)
2301 sysfs_notify_dirent(rdev->sysfs_state);
2302 return err ? err : len;
2304 static struct rdev_sysfs_entry rdev_state =
2305 __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
2308 errors_show(mdk_rdev_t *rdev, char *page)
2310 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2314 errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2317 unsigned long n = simple_strtoul(buf, &e, 10);
2318 if (*buf && (*e == 0 || *e == '\n')) {
2319 atomic_set(&rdev->corrected_errors, n);
2324 static struct rdev_sysfs_entry rdev_errors =
2325 __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
2328 slot_show(mdk_rdev_t *rdev, char *page)
2330 if (rdev->raid_disk < 0)
2331 return sprintf(page, "none\n");
2333 return sprintf(page, "%d\n", rdev->raid_disk);
2337 slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2342 int slot = simple_strtoul(buf, &e, 10);
2343 if (strncmp(buf, "none", 4)==0)
2345 else if (e==buf || (*e && *e!= '\n'))
2347 if (rdev->mddev->pers && slot == -1) {
2348 /* Setting 'slot' on an active array requires also
2349 * updating the 'rd%d' link, and communicating
2350 * with the personality with ->hot_*_disk.
2351 * For now we only support removing
2352 * failed/spare devices. This normally happens automatically,
2353 * but not when the metadata is externally managed.
2355 if (rdev->raid_disk == -1)
2357 /* personality does all needed checks */
2358 if (rdev->mddev->pers->hot_add_disk == NULL)
2360 err = rdev->mddev->pers->
2361 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2364 sprintf(nm, "rd%d", rdev->raid_disk);
2365 sysfs_remove_link(&rdev->mddev->kobj, nm);
2366 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2367 md_wakeup_thread(rdev->mddev->thread);
2368 } else if (rdev->mddev->pers) {
2370 /* Activating a spare .. or possibly reactivating
2371 * if we ever get bitmaps working here.
2374 if (rdev->raid_disk != -1)
2377 if (rdev->mddev->pers->hot_add_disk == NULL)
2380 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
2381 if (rdev2->raid_disk == slot)
2384 rdev->raid_disk = slot;
2385 if (test_bit(In_sync, &rdev->flags))
2386 rdev->saved_raid_disk = slot;
2388 rdev->saved_raid_disk = -1;
2389 err = rdev->mddev->pers->
2390 hot_add_disk(rdev->mddev, rdev);
2392 rdev->raid_disk = -1;
2395 sysfs_notify_dirent(rdev->sysfs_state);
2396 sprintf(nm, "rd%d", rdev->raid_disk);
2397 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2399 "md: cannot register "
2401 nm, mdname(rdev->mddev));
2403 /* don't wakeup anyone, leave that to userspace. */
2405 if (slot >= rdev->mddev->raid_disks)
2407 rdev->raid_disk = slot;
2408 /* assume it is working */
2409 clear_bit(Faulty, &rdev->flags);
2410 clear_bit(WriteMostly, &rdev->flags);
2411 set_bit(In_sync, &rdev->flags);
2412 sysfs_notify_dirent(rdev->sysfs_state);
2418 static struct rdev_sysfs_entry rdev_slot =
2419 __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
2422 offset_show(mdk_rdev_t *rdev, char *page)
2424 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
2428 offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2431 unsigned long long offset = simple_strtoull(buf, &e, 10);
2432 if (e==buf || (*e && *e != '\n'))
2434 if (rdev->mddev->pers && rdev->raid_disk >= 0)
2436 if (rdev->sectors && rdev->mddev->external)
2437 /* Must set offset before size, so overlap checks
2440 rdev->data_offset = offset;
2444 static struct rdev_sysfs_entry rdev_offset =
2445 __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
2448 rdev_size_show(mdk_rdev_t *rdev, char *page)
2450 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
2453 static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2455 /* check if two start/length pairs overlap */
2463 static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2465 unsigned long long blocks;
2468 if (strict_strtoull(buf, 10, &blocks) < 0)
2471 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2472 return -EINVAL; /* sector conversion overflow */
2475 if (new != blocks * 2)
2476 return -EINVAL; /* unsigned long long to sector_t overflow */
2483 rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2485 mddev_t *my_mddev = rdev->mddev;
2486 sector_t oldsectors = rdev->sectors;
2489 if (strict_blocks_to_sectors(buf, §ors) < 0)
2491 if (my_mddev->pers && rdev->raid_disk >= 0) {
2492 if (my_mddev->persistent) {
2493 sectors = super_types[my_mddev->major_version].
2494 rdev_size_change(rdev, sectors);
2497 } else if (!sectors)
2498 sectors = (rdev->bdev->bd_inode->i_size >> 9) -
2501 if (sectors < my_mddev->dev_sectors)
2502 return -EINVAL; /* component must fit device */
2504 rdev->sectors = sectors;
2505 if (sectors > oldsectors && my_mddev->external) {
2506 /* need to check that all other rdevs with the same ->bdev
2507 * do not overlap. We need to unlock the mddev to avoid
2508 * a deadlock. We have already changed rdev->sectors, and if
2509 * we have to change it back, we will have the lock again.
2513 struct list_head *tmp;
2515 mddev_unlock(my_mddev);
2516 for_each_mddev(mddev, tmp) {
2520 list_for_each_entry(rdev2, &mddev->disks, same_set)
2521 if (test_bit(AllReserved, &rdev2->flags) ||
2522 (rdev->bdev == rdev2->bdev &&
2524 overlaps(rdev->data_offset, rdev->sectors,
2530 mddev_unlock(mddev);
2536 mddev_lock(my_mddev);
2538 /* Someone else could have slipped in a size
2539 * change here, but doing so is just silly.
2540 * We put oldsectors back because we *know* it is
2541 * safe, and trust userspace not to race with
2544 rdev->sectors = oldsectors;
2551 static struct rdev_sysfs_entry rdev_size =
2552 __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
2554 static struct attribute *rdev_default_attrs[] = {
2563 rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2565 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2566 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
2567 mddev_t *mddev = rdev->mddev;
2573 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2575 if (rdev->mddev == NULL)
2578 rv = entry->show(rdev, page);
2579 mddev_unlock(mddev);
2585 rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2586 const char *page, size_t length)
2588 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2589 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
2591 mddev_t *mddev = rdev->mddev;
2595 if (!capable(CAP_SYS_ADMIN))
2597 rv = mddev ? mddev_lock(mddev): -EBUSY;
2599 if (rdev->mddev == NULL)
2602 rv = entry->store(rdev, page, length);
2603 mddev_unlock(mddev);
2608 static void rdev_free(struct kobject *ko)
2610 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2613 static struct sysfs_ops rdev_sysfs_ops = {
2614 .show = rdev_attr_show,
2615 .store = rdev_attr_store,
2617 static struct kobj_type rdev_ktype = {
2618 .release = rdev_free,
2619 .sysfs_ops = &rdev_sysfs_ops,
2620 .default_attrs = rdev_default_attrs,
2624 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2626 * mark the device faulty if:
2628 * - the device is nonexistent (zero size)
2629 * - the device has no valid superblock
2631 * a faulty rdev _never_ has rdev->sb set.
2633 static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2635 char b[BDEVNAME_SIZE];
2640 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
2642 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2643 return ERR_PTR(-ENOMEM);
2646 if ((err = alloc_disk_sb(rdev)))
2649 err = lock_rdev(rdev, newdev, super_format == -2);
2653 kobject_init(&rdev->kobj, &rdev_ktype);
2656 rdev->saved_raid_disk = -1;
2657 rdev->raid_disk = -1;
2659 rdev->data_offset = 0;
2660 rdev->sb_events = 0;
2661 rdev->last_read_error.tv_sec = 0;
2662 rdev->last_read_error.tv_nsec = 0;
2663 atomic_set(&rdev->nr_pending, 0);
2664 atomic_set(&rdev->read_errors, 0);
2665 atomic_set(&rdev->corrected_errors, 0);
2667 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2670 "md: %s has zero or unknown size, marking faulty!\n",
2671 bdevname(rdev->bdev,b));
2676 if (super_format >= 0) {
2677 err = super_types[super_format].
2678 load_super(rdev, NULL, super_minor);
2679 if (err == -EINVAL) {
2681 "md: %s does not have a valid v%d.%d "
2682 "superblock, not importing!\n",
2683 bdevname(rdev->bdev,b),
2684 super_format, super_minor);
2689 "md: could not read %s's sb, not importing!\n",
2690 bdevname(rdev->bdev,b));
2695 INIT_LIST_HEAD(&rdev->same_set);
2696 init_waitqueue_head(&rdev->blocked_wait);
2701 if (rdev->sb_page) {
2707 return ERR_PTR(err);
2711 * Check a full RAID array for plausibility
2715 static void analyze_sbs(mddev_t * mddev)
2718 mdk_rdev_t *rdev, *freshest, *tmp;
2719 char b[BDEVNAME_SIZE];
2722 rdev_for_each(rdev, tmp, mddev)
2723 switch (super_types[mddev->major_version].
2724 load_super(rdev, freshest, mddev->minor_version)) {
2732 "md: fatal superblock inconsistency in %s"
2733 " -- removing from array\n",
2734 bdevname(rdev->bdev,b));
2735 kick_rdev_from_array(rdev);
2739 super_types[mddev->major_version].
2740 validate_super(mddev, freshest);
2743 rdev_for_each(rdev, tmp, mddev) {
2744 if (rdev->desc_nr >= mddev->max_disks ||
2745 i > mddev->max_disks) {
2747 "md: %s: %s: only %d devices permitted\n",
2748 mdname(mddev), bdevname(rdev->bdev, b),
2750 kick_rdev_from_array(rdev);
2753 if (rdev != freshest)
2754 if (super_types[mddev->major_version].
2755 validate_super(mddev, rdev)) {
2756 printk(KERN_WARNING "md: kicking non-fresh %s"
2758 bdevname(rdev->bdev,b));
2759 kick_rdev_from_array(rdev);
2762 if (mddev->level == LEVEL_MULTIPATH) {
2763 rdev->desc_nr = i++;
2764 rdev->raid_disk = rdev->desc_nr;
2765 set_bit(In_sync, &rdev->flags);
2766 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
2767 rdev->raid_disk = -1;
2768 clear_bit(In_sync, &rdev->flags);
2773 /* Read a fixed-point number.
2774 * Numbers in sysfs attributes should be in "standard" units where
2775 * possible, so time should be in seconds.
2776 * However we internally use a a much smaller unit such as
2777 * milliseconds or jiffies.
2778 * This function takes a decimal number with a possible fractional
2779 * component, and produces an integer which is the result of
2780 * multiplying that number by 10^'scale'.
2781 * all without any floating-point arithmetic.
2783 int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
2785 unsigned long result = 0;
2787 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
2790 else if (decimals < scale) {
2793 result = result * 10 + value;
2805 while (decimals < scale) {
2814 static void md_safemode_timeout(unsigned long data);
2817 safe_delay_show(mddev_t *mddev, char *page)
2819 int msec = (mddev->safemode_delay*1000)/HZ;
2820 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2823 safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2827 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
2830 mddev->safemode_delay = 0;
2832 unsigned long old_delay = mddev->safemode_delay;
2833 mddev->safemode_delay = (msec*HZ)/1000;
2834 if (mddev->safemode_delay == 0)
2835 mddev->safemode_delay = 1;
2836 if (mddev->safemode_delay < old_delay)
2837 md_safemode_timeout((unsigned long)mddev);
2841 static struct md_sysfs_entry md_safe_delay =
2842 __ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
2845 level_show(mddev_t *mddev, char *page)
2847 struct mdk_personality *p = mddev->pers;
2849 return sprintf(page, "%s\n", p->name);
2850 else if (mddev->clevel[0])
2851 return sprintf(page, "%s\n", mddev->clevel);
2852 else if (mddev->level != LEVEL_NONE)
2853 return sprintf(page, "%d\n", mddev->level);
2859 level_store(mddev_t *mddev, const char *buf, size_t len)
2863 struct mdk_personality *pers;
2867 if (mddev->pers == NULL) {
2870 if (len >= sizeof(mddev->clevel))
2872 strncpy(mddev->clevel, buf, len);
2873 if (mddev->clevel[len-1] == '\n')
2875 mddev->clevel[len] = 0;
2876 mddev->level = LEVEL_NONE;
2880 /* request to change the personality. Need to ensure:
2881 * - array is not engaged in resync/recovery/reshape
2882 * - old personality can be suspended
2883 * - new personality will access other array.
2886 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
2889 if (!mddev->pers->quiesce) {
2890 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
2891 mdname(mddev), mddev->pers->name);
2895 /* Now find the new personality */
2896 if (len == 0 || len >= sizeof(level))
2898 strncpy(level, buf, len);
2899 if (level[len-1] == '\n')
2903 request_module("md-%s", level);
2904 spin_lock(&pers_lock);
2905 pers = find_pers(LEVEL_NONE, level);
2906 if (!pers || !try_module_get(pers->owner)) {
2907 spin_unlock(&pers_lock);
2908 printk(KERN_WARNING "md: personality %s not loaded\n", level);
2911 spin_unlock(&pers_lock);
2913 if (pers == mddev->pers) {
2914 /* Nothing to do! */
2915 module_put(pers->owner);
2918 if (!pers->takeover) {
2919 module_put(pers->owner);
2920 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
2921 mdname(mddev), level);
2925 /* ->takeover must set new_* and/or delta_disks
2926 * if it succeeds, and may set them when it fails.
2928 priv = pers->takeover(mddev);
2930 mddev->new_level = mddev->level;
2931 mddev->new_layout = mddev->layout;
2932 mddev->new_chunk_sectors = mddev->chunk_sectors;
2933 mddev->raid_disks -= mddev->delta_disks;
2934 mddev->delta_disks = 0;
2935 module_put(pers->owner);
2936 printk(KERN_WARNING "md: %s: %s would not accept array\n",
2937 mdname(mddev), level);
2938 return PTR_ERR(priv);
2941 /* Looks like we have a winner */
2942 mddev_suspend(mddev);
2943 mddev->pers->stop(mddev);
2944 module_put(mddev->pers->owner);
2945 /* Invalidate devices that are now superfluous */
2946 list_for_each_entry(rdev, &mddev->disks, same_set)
2947 if (rdev->raid_disk >= mddev->raid_disks) {
2948 rdev->raid_disk = -1;
2949 clear_bit(In_sync, &rdev->flags);
2952 mddev->private = priv;
2953 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
2954 mddev->level = mddev->new_level;
2955 mddev->layout = mddev->new_layout;
2956 mddev->chunk_sectors = mddev->new_chunk_sectors;
2957 mddev->delta_disks = 0;
2959 mddev_resume(mddev);
2960 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2961 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2962 md_wakeup_thread(mddev->thread);
2966 static struct md_sysfs_entry md_level =
2967 __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
2971 layout_show(mddev_t *mddev, char *page)
2973 /* just a number, not meaningful for all levels */
2974 if (mddev->reshape_position != MaxSector &&
2975 mddev->layout != mddev->new_layout)
2976 return sprintf(page, "%d (%d)\n",
2977 mddev->new_layout, mddev->layout);
2978 return sprintf(page, "%d\n", mddev->layout);
2982 layout_store(mddev_t *mddev, const char *buf, size_t len)
2985 unsigned long n = simple_strtoul(buf, &e, 10);
2987 if (!*buf || (*e && *e != '\n'))
2992 if (mddev->pers->check_reshape == NULL)
2994 mddev->new_layout = n;
2995 err = mddev->pers->check_reshape(mddev);
2997 mddev->new_layout = mddev->layout;
3001 mddev->new_layout = n;
3002 if (mddev->reshape_position == MaxSector)
3007 static struct md_sysfs_entry md_layout =
3008 __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
3012 raid_disks_show(mddev_t *mddev, char *page)
3014 if (mddev->raid_disks == 0)
3016 if (mddev->reshape_position != MaxSector &&
3017 mddev->delta_disks != 0)
3018 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3019 mddev->raid_disks - mddev->delta_disks);
3020 return sprintf(page, "%d\n", mddev->raid_disks);
3023 static int update_raid_disks(mddev_t *mddev, int raid_disks);
3026 raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
3030 unsigned long n = simple_strtoul(buf, &e, 10);
3032 if (!*buf || (*e && *e != '\n'))
3036 rv = update_raid_disks(mddev, n);
3037 else if (mddev->reshape_position != MaxSector) {
3038 int olddisks = mddev->raid_disks - mddev->delta_disks;
3039 mddev->delta_disks = n - olddisks;
3040 mddev->raid_disks = n;
3042 mddev->raid_disks = n;
3043 return rv ? rv : len;
3045 static struct md_sysfs_entry md_raid_disks =
3046 __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
3049 chunk_size_show(mddev_t *mddev, char *page)
3051 if (mddev->reshape_position != MaxSector &&
3052 mddev->chunk_sectors != mddev->new_chunk_sectors)
3053 return sprintf(page, "%d (%d)\n",
3054 mddev->new_chunk_sectors << 9,
3055 mddev->chunk_sectors << 9);
3056 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
3060 chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
3063 unsigned long n = simple_strtoul(buf, &e, 10);
3065 if (!*buf || (*e && *e != '\n'))
3070 if (mddev->pers->check_reshape == NULL)
3072 mddev->new_chunk_sectors = n >> 9;
3073 err = mddev->pers->check_reshape(mddev);
3075 mddev->new_chunk_sectors = mddev->chunk_sectors;
3079 mddev->new_chunk_sectors = n >> 9;
3080 if (mddev->reshape_position == MaxSector)
3081 mddev->chunk_sectors = n >> 9;
3085 static struct md_sysfs_entry md_chunk_size =
3086 __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
3089 resync_start_show(mddev_t *mddev, char *page)
3091 if (mddev->recovery_cp == MaxSector)
3092 return sprintf(page, "none\n");
3093 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3097 resync_start_store(mddev_t *mddev, const char *buf, size_t len)
3100 unsigned long long n = simple_strtoull(buf, &e, 10);
3104 if (!*buf || (*e && *e != '\n'))
3107 mddev->recovery_cp = n;
3110 static struct md_sysfs_entry md_resync_start =
3111 __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
3114 * The array state can be:
3117 * No devices, no size, no level
3118 * Equivalent to STOP_ARRAY ioctl
3120 * May have some settings, but array is not active
3121 * all IO results in error
3122 * When written, doesn't tear down array, but just stops it
3123 * suspended (not supported yet)
3124 * All IO requests will block. The array can be reconfigured.
3125 * Writing this, if accepted, will block until array is quiescent
3127 * no resync can happen. no superblocks get written.
3128 * write requests fail
3130 * like readonly, but behaves like 'clean' on a write request.
3132 * clean - no pending writes, but otherwise active.
3133 * When written to inactive array, starts without resync
3134 * If a write request arrives then
3135 * if metadata is known, mark 'dirty' and switch to 'active'.
3136 * if not known, block and switch to write-pending
3137 * If written to an active array that has pending writes, then fails.
3139 * fully active: IO and resync can be happening.
3140 * When written to inactive array, starts with resync
3143 * clean, but writes are blocked waiting for 'active' to be written.
3146 * like active, but no writes have been seen for a while (100msec).
3149 enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3150 write_pending, active_idle, bad_word};
3151 static char *array_states[] = {
3152 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3153 "write-pending", "active-idle", NULL };
3155 static int match_word(const char *word, char **list)
3158 for (n=0; list[n]; n++)
3159 if (cmd_match(word, list[n]))
3165 array_state_show(mddev_t *mddev, char *page)
3167 enum array_state st = inactive;
3180 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
3182 else if (mddev->safemode)
3188 if (list_empty(&mddev->disks) &&
3189 mddev->raid_disks == 0 &&
3190 mddev->dev_sectors == 0)
3195 return sprintf(page, "%s\n", array_states[st]);
3198 static int do_md_stop(mddev_t * mddev, int ro, int is_open);
3199 static int do_md_run(mddev_t * mddev);
3200 static int restart_array(mddev_t *mddev);
3203 array_state_store(mddev_t *mddev, const char *buf, size_t len)
3206 enum array_state st = match_word(buf, array_states);
3211 /* stopping an active array */
3212 if (atomic_read(&mddev->openers) > 0)
3214 err = do_md_stop(mddev, 0, 0);
3217 /* stopping an active array */
3219 if (atomic_read(&mddev->openers) > 0)
3221 err = do_md_stop(mddev, 2, 0);
3223 err = 0; /* already inactive */
3226 break; /* not supported yet */
3229 err = do_md_stop(mddev, 1, 0);
3232 set_disk_ro(mddev->gendisk, 1);
3233 err = do_md_run(mddev);
3239 err = do_md_stop(mddev, 1, 0);
3240 else if (mddev->ro == 1)
3241 err = restart_array(mddev);
3244 set_disk_ro(mddev->gendisk, 0);
3248 err = do_md_run(mddev);
3253 restart_array(mddev);
3254 spin_lock_irq(&mddev->write_lock);
3255 if (atomic_read(&mddev->writes_pending) == 0) {
3256 if (mddev->in_sync == 0) {
3258 if (mddev->safemode == 1)
3259 mddev->safemode = 0;
3260 if (mddev->persistent)
3261 set_bit(MD_CHANGE_CLEAN,
3267 spin_unlock_irq(&mddev->write_lock);
3273 restart_array(mddev);
3274 if (mddev->external)
3275 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
3276 wake_up(&mddev->sb_wait);
3280 set_disk_ro(mddev->gendisk, 0);
3281 err = do_md_run(mddev);
3286 /* these cannot be set */
3292 sysfs_notify_dirent(mddev->sysfs_state);
3296 static struct md_sysfs_entry md_array_state =
3297 __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
3300 max_corrected_read_errors_show(mddev_t *mddev, char *page) {
3301 return sprintf(page, "%d\n",
3302 atomic_read(&mddev->max_corr_read_errors));
3306 max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len)
3309 unsigned long n = simple_strtoul(buf, &e, 10);
3311 if (*buf && (*e == 0 || *e == '\n')) {
3312 atomic_set(&mddev->max_corr_read_errors, n);
3318 static struct md_sysfs_entry max_corr_read_errors =
3319 __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3320 max_corrected_read_errors_store);
3323 null_show(mddev_t *mddev, char *page)
3329 new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3331 /* buf must be %d:%d\n? giving major and minor numbers */
3332 /* The new device is added to the array.
3333 * If the array has a persistent superblock, we read the
3334 * superblock to initialise info and check validity.
3335 * Otherwise, only checking done is that in bind_rdev_to_array,
3336 * which mainly checks size.
3339 int major = simple_strtoul(buf, &e, 10);
3345 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3347 minor = simple_strtoul(e+1, &e, 10);
3348 if (*e && *e != '\n')
3350 dev = MKDEV(major, minor);
3351 if (major != MAJOR(dev) ||
3352 minor != MINOR(dev))
3356 if (mddev->persistent) {
3357 rdev = md_import_device(dev, mddev->major_version,
3358 mddev->minor_version);
3359 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3360 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3361 mdk_rdev_t, same_set);
3362 err = super_types[mddev->major_version]
3363 .load_super(rdev, rdev0, mddev->minor_version);
3367 } else if (mddev->external)
3368 rdev = md_import_device(dev, -2, -1);
3370 rdev = md_import_device(dev, -1, -1);
3373 return PTR_ERR(rdev);
3374 err = bind_rdev_to_array(rdev, mddev);
3378 return err ? err : len;
3381 static struct md_sysfs_entry md_new_device =
3382 __ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
3385 bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3388 unsigned long chunk, end_chunk;
3392 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3394 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3395 if (buf == end) break;
3396 if (*end == '-') { /* range */
3398 end_chunk = simple_strtoul(buf, &end, 0);
3399 if (buf == end) break;
3401 if (*end && !isspace(*end)) break;
3402 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
3404 while (isspace(*buf)) buf++;
3406 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3411 static struct md_sysfs_entry md_bitmap =
3412 __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3415 size_show(mddev_t *mddev, char *page)
3417 return sprintf(page, "%llu\n",
3418 (unsigned long long)mddev->dev_sectors / 2);
3421 static int update_size(mddev_t *mddev, sector_t num_sectors);
3424 size_store(mddev_t *mddev, const char *buf, size_t len)
3426 /* If array is inactive, we can reduce the component size, but
3427 * not increase it (except from 0).
3428 * If array is active, we can try an on-line resize
3431 int err = strict_blocks_to_sectors(buf, §ors);
3436 err = update_size(mddev, sectors);
3437 md_update_sb(mddev, 1);
3439 if (mddev->dev_sectors == 0 ||
3440 mddev->dev_sectors > sectors)
3441 mddev->dev_sectors = sectors;
3445 return err ? err : len;
3448 static struct md_sysfs_entry md_size =
3449 __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
3454 * 'none' for arrays with no metadata (good luck...)
3455 * 'external' for arrays with externally managed metadata,
3456 * or N.M for internally known formats
3459 metadata_show(mddev_t *mddev, char *page)
3461 if (mddev->persistent)
3462 return sprintf(page, "%d.%d\n",
3463 mddev->major_version, mddev->minor_version);
3464 else if (mddev->external)
3465 return sprintf(page, "external:%s\n", mddev->metadata_type);
3467 return sprintf(page, "none\n");
3471 metadata_store(mddev_t *mddev, const char *buf, size_t len)
3475 /* Changing the details of 'external' metadata is
3476 * always permitted. Otherwise there must be
3477 * no devices attached to the array.
3479 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3481 else if (!list_empty(&mddev->disks))
3484 if (cmd_match(buf, "none")) {
3485 mddev->persistent = 0;
3486 mddev->external = 0;
3487 mddev->major_version = 0;
3488 mddev->minor_version = 90;
3491 if (strncmp(buf, "external:", 9) == 0) {
3492 size_t namelen = len-9;
3493 if (namelen >= sizeof(mddev->metadata_type))
3494 namelen = sizeof(mddev->metadata_type)-1;
3495 strncpy(mddev->metadata_type, buf+9, namelen);
3496 mddev->metadata_type[namelen] = 0;
3497 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3498 mddev->metadata_type[--namelen] = 0;
3499 mddev->persistent = 0;
3500 mddev->external = 1;
3501 mddev->major_version = 0;
3502 mddev->minor_version = 90;
3505 major = simple_strtoul(buf, &e, 10);
3506 if (e==buf || *e != '.')
3509 minor = simple_strtoul(buf, &e, 10);
3510 if (e==buf || (*e && *e != '\n') )
3512 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
3514 mddev->major_version = major;
3515 mddev->minor_version = minor;
3516 mddev->persistent = 1;
3517 mddev->external = 0;
3521 static struct md_sysfs_entry md_metadata =
3522 __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
3525 action_show(mddev_t *mddev, char *page)
3527 char *type = "idle";
3528 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3530 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3531 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
3532 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3534 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3535 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3537 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3541 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
3544 return sprintf(page, "%s\n", type);
3548 action_store(mddev_t *mddev, const char *page, size_t len)
3550 if (!mddev->pers || !mddev->pers->sync_request)
3553 if (cmd_match(page, "frozen"))
3554 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3556 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3558 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
3559 if (mddev->sync_thread) {
3560 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3561 md_unregister_thread(mddev->sync_thread);
3562 mddev->sync_thread = NULL;
3563 mddev->recovery = 0;
3565 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3566 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
3568 else if (cmd_match(page, "resync"))
3569 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3570 else if (cmd_match(page, "recover")) {
3571 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3572 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3573 } else if (cmd_match(page, "reshape")) {
3575 if (mddev->pers->start_reshape == NULL)
3577 err = mddev->pers->start_reshape(mddev);
3580 sysfs_notify(&mddev->kobj, NULL, "degraded");
3582 if (cmd_match(page, "check"))
3583 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3584 else if (!cmd_match(page, "repair"))
3586 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3587 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3589 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3590 md_wakeup_thread(mddev->thread);
3591 sysfs_notify_dirent(mddev->sysfs_action);
3596 mismatch_cnt_show(mddev_t *mddev, char *page)
3598 return sprintf(page, "%llu\n",
3599 (unsigned long long) mddev->resync_mismatches);
3602 static struct md_sysfs_entry md_scan_mode =
3603 __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
3606 static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
3609 sync_min_show(mddev_t *mddev, char *page)
3611 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3612 mddev->sync_speed_min ? "local": "system");
3616 sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3620 if (strncmp(buf, "system", 6)==0) {
3621 mddev->sync_speed_min = 0;
3624 min = simple_strtoul(buf, &e, 10);
3625 if (buf == e || (*e && *e != '\n') || min <= 0)
3627 mddev->sync_speed_min = min;
3631 static struct md_sysfs_entry md_sync_min =
3632 __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3635 sync_max_show(mddev_t *mddev, char *page)
3637 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3638 mddev->sync_speed_max ? "local": "system");
3642 sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3646 if (strncmp(buf, "system", 6)==0) {
3647 mddev->sync_speed_max = 0;
3650 max = simple_strtoul(buf, &e, 10);
3651 if (buf == e || (*e && *e != '\n') || max <= 0)
3653 mddev->sync_speed_max = max;
3657 static struct md_sysfs_entry md_sync_max =
3658 __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3661 degraded_show(mddev_t *mddev, char *page)
3663 return sprintf(page, "%d\n", mddev->degraded);
3665 static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
3668 sync_force_parallel_show(mddev_t *mddev, char *page)
3670 return sprintf(page, "%d\n", mddev->parallel_resync);
3674 sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3678 if (strict_strtol(buf, 10, &n))
3681 if (n != 0 && n != 1)
3684 mddev->parallel_resync = n;
3686 if (mddev->sync_thread)
3687 wake_up(&resync_wait);
3692 /* force parallel resync, even with shared block devices */
3693 static struct md_sysfs_entry md_sync_force_parallel =
3694 __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3695 sync_force_parallel_show, sync_force_parallel_store);
3698 sync_speed_show(mddev_t *mddev, char *page)
3700 unsigned long resync, dt, db;
3701 if (mddev->curr_resync == 0)
3702 return sprintf(page, "none\n");
3703 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3704 dt = (jiffies - mddev->resync_mark) / HZ;
3706 db = resync - mddev->resync_mark_cnt;
3707 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
3710 static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
3713 sync_completed_show(mddev_t *mddev, char *page)
3715 unsigned long max_sectors, resync;
3717 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3718 return sprintf(page, "none\n");
3720 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3721 max_sectors = mddev->resync_max_sectors;
3723 max_sectors = mddev->dev_sectors;
3725 resync = mddev->curr_resync_completed;
3726 return sprintf(page, "%lu / %lu\n", resync, max_sectors);
3729 static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
3732 min_sync_show(mddev_t *mddev, char *page)
3734 return sprintf(page, "%llu\n",
3735 (unsigned long long)mddev->resync_min);
3738 min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3740 unsigned long long min;
3741 if (strict_strtoull(buf, 10, &min))
3743 if (min > mddev->resync_max)
3745 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3748 /* Must be a multiple of chunk_size */
3749 if (mddev->chunk_sectors) {
3750 sector_t temp = min;
3751 if (sector_div(temp, mddev->chunk_sectors))
3754 mddev->resync_min = min;
3759 static struct md_sysfs_entry md_min_sync =
3760 __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3763 max_sync_show(mddev_t *mddev, char *page)
3765 if (mddev->resync_max == MaxSector)
3766 return sprintf(page, "max\n");
3768 return sprintf(page, "%llu\n",
3769 (unsigned long long)mddev->resync_max);
3772 max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3774 if (strncmp(buf, "max", 3) == 0)
3775 mddev->resync_max = MaxSector;
3777 unsigned long long max;
3778 if (strict_strtoull(buf, 10, &max))
3780 if (max < mddev->resync_min)
3782 if (max < mddev->resync_max &&
3784 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3787 /* Must be a multiple of chunk_size */
3788 if (mddev->chunk_sectors) {
3789 sector_t temp = max;
3790 if (sector_div(temp, mddev->chunk_sectors))
3793 mddev->resync_max = max;
3795 wake_up(&mddev->recovery_wait);
3799 static struct md_sysfs_entry md_max_sync =
3800 __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3803 suspend_lo_show(mddev_t *mddev, char *page)
3805 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3809 suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3812 unsigned long long new = simple_strtoull(buf, &e, 10);
3814 if (mddev->pers == NULL ||
3815 mddev->pers->quiesce == NULL)
3817 if (buf == e || (*e && *e != '\n'))
3819 if (new >= mddev->suspend_hi ||
3820 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3821 mddev->suspend_lo = new;
3822 mddev->pers->quiesce(mddev, 2);
3827 static struct md_sysfs_entry md_suspend_lo =
3828 __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3832 suspend_hi_show(mddev_t *mddev, char *page)
3834 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3838 suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3841 unsigned long long new = simple_strtoull(buf, &e, 10);
3843 if (mddev->pers == NULL ||
3844 mddev->pers->quiesce == NULL)
3846 if (buf == e || (*e && *e != '\n'))
3848 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3849 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3850 mddev->suspend_hi = new;
3851 mddev->pers->quiesce(mddev, 1);
3852 mddev->pers->quiesce(mddev, 0);
3857 static struct md_sysfs_entry md_suspend_hi =
3858 __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3861 reshape_position_show(mddev_t *mddev, char *page)
3863 if (mddev->reshape_position != MaxSector)
3864 return sprintf(page, "%llu\n",
3865 (unsigned long long)mddev->reshape_position);
3866 strcpy(page, "none\n");
3871 reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3874 unsigned long long new = simple_strtoull(buf, &e, 10);
3877 if (buf == e || (*e && *e != '\n'))
3879 mddev->reshape_position = new;
3880 mddev->delta_disks = 0;
3881 mddev->new_level = mddev->level;
3882 mddev->new_layout = mddev->layout;
3883 mddev->new_chunk_sectors = mddev->chunk_sectors;
3887 static struct md_sysfs_entry md_reshape_position =
3888 __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3889 reshape_position_store);
3892 array_size_show(mddev_t *mddev, char *page)
3894 if (mddev->external_size)
3895 return sprintf(page, "%llu\n",
3896 (unsigned long long)mddev->array_sectors/2);
3898 return sprintf(page, "default\n");
3902 array_size_store(mddev_t *mddev, const char *buf, size_t len)
3906 if (strncmp(buf, "default", 7) == 0) {
3908 sectors = mddev->pers->size(mddev, 0, 0);
3910 sectors = mddev->array_sectors;
3912 mddev->external_size = 0;
3914 if (strict_blocks_to_sectors(buf, §ors) < 0)
3916 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
3919 mddev->external_size = 1;
3922 mddev->array_sectors = sectors;
3923 set_capacity(mddev->gendisk, mddev->array_sectors);
3925 revalidate_disk(mddev->gendisk);
3930 static struct md_sysfs_entry md_array_size =
3931 __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
3934 static struct attribute *md_default_attrs[] = {
3937 &md_raid_disks.attr,
3938 &md_chunk_size.attr,
3940 &md_resync_start.attr,
3942 &md_new_device.attr,
3943 &md_safe_delay.attr,
3944 &md_array_state.attr,
3945 &md_reshape_position.attr,
3946 &md_array_size.attr,
3947 &max_corr_read_errors.attr,
3951 static struct attribute *md_redundancy_attrs[] = {
3953 &md_mismatches.attr,
3956 &md_sync_speed.attr,
3957 &md_sync_force_parallel.attr,
3958 &md_sync_completed.attr,
3961 &md_suspend_lo.attr,
3962 &md_suspend_hi.attr,
3967 static struct attribute_group md_redundancy_group = {
3969 .attrs = md_redundancy_attrs,
3974 md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3976 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3977 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
3982 rv = mddev_lock(mddev);
3984 rv = entry->show(mddev, page);
3985 mddev_unlock(mddev);
3991 md_attr_store(struct kobject *kobj, struct attribute *attr,
3992 const char *page, size_t length)
3994 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3995 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
4000 if (!capable(CAP_SYS_ADMIN))
4002 rv = mddev_lock(mddev);
4003 if (mddev->hold_active == UNTIL_IOCTL)
4004 mddev->hold_active = 0;
4006 rv = entry->store(mddev, page, length);
4007 mddev_unlock(mddev);
4012 static void md_free(struct kobject *ko)
4014 mddev_t *mddev = container_of(ko, mddev_t, kobj);
4016 if (mddev->sysfs_state)
4017 sysfs_put(mddev->sysfs_state);
4019 if (mddev->gendisk) {
4020 del_gendisk(mddev->gendisk);
4021 put_disk(mddev->gendisk);
4024 blk_cleanup_queue(mddev->queue);
4029 static struct sysfs_ops md_sysfs_ops = {
4030 .show = md_attr_show,
4031 .store = md_attr_store,
4033 static struct kobj_type md_ktype = {
4035 .sysfs_ops = &md_sysfs_ops,
4036 .default_attrs = md_default_attrs,
4041 static void mddev_delayed_delete(struct work_struct *ws)
4043 mddev_t *mddev = container_of(ws, mddev_t, del_work);
4045 if (mddev->private == &md_redundancy_group) {
4046 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
4047 if (mddev->sysfs_action)
4048 sysfs_put(mddev->sysfs_action);
4049 mddev->sysfs_action = NULL;
4050 mddev->private = NULL;
4052 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
4053 kobject_del(&mddev->kobj);
4054 kobject_put(&mddev->kobj);
4057 static int md_alloc(dev_t dev, char *name)
4059 static DEFINE_MUTEX(disks_mutex);
4060 mddev_t *mddev = mddev_find(dev);
4061 struct gendisk *disk;
4070 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4071 shift = partitioned ? MdpMinorShift : 0;
4072 unit = MINOR(mddev->unit) >> shift;
4074 /* wait for any previous instance if this device
4075 * to be completed removed (mddev_delayed_delete).
4077 flush_scheduled_work();
4079 mutex_lock(&disks_mutex);
4085 /* Need to ensure that 'name' is not a duplicate.
4088 spin_lock(&all_mddevs_lock);
4090 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4091 if (mddev2->gendisk &&
4092 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4093 spin_unlock(&all_mddevs_lock);
4096 spin_unlock(&all_mddevs_lock);
4100 mddev->queue = blk_alloc_queue(GFP_KERNEL);
4103 mddev->queue->queuedata = mddev;
4105 /* Can be unlocked because the queue is new: no concurrency */
4106 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue);
4108 blk_queue_make_request(mddev->queue, md_make_request);
4110 disk = alloc_disk(1 << shift);
4112 blk_cleanup_queue(mddev->queue);
4113 mddev->queue = NULL;
4116 disk->major = MAJOR(mddev->unit);
4117 disk->first_minor = unit << shift;
4119 strcpy(disk->disk_name, name);
4120 else if (partitioned)
4121 sprintf(disk->disk_name, "md_d%d", unit);
4123 sprintf(disk->disk_name, "md%d", unit);
4124 disk->fops = &md_fops;
4125 disk->private_data = mddev;
4126 disk->queue = mddev->queue;
4127 /* Allow extended partitions. This makes the
4128 * 'mdp' device redundant, but we can't really
4131 disk->flags |= GENHD_FL_EXT_DEVT;
4133 mddev->gendisk = disk;
4134 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4135 &disk_to_dev(disk)->kobj, "%s", "md");
4137 /* This isn't possible, but as kobject_init_and_add is marked
4138 * __must_check, we must do something with the result
4140 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4144 if (sysfs_create_group(&mddev->kobj, &md_bitmap_group))
4145 printk(KERN_DEBUG "pointless warning\n");
4147 mutex_unlock(&disks_mutex);
4149 kobject_uevent(&mddev->kobj, KOBJ_ADD);
4150 mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state");
4156 static struct kobject *md_probe(dev_t dev, int *part, void *data)
4158 md_alloc(dev, NULL);
4162 static int add_named_array(const char *val, struct kernel_param *kp)
4164 /* val must be "md_*" where * is not all digits.
4165 * We allocate an array with a large free minor number, and
4166 * set the name to val. val must not already be an active name.
4168 int len = strlen(val);
4169 char buf[DISK_NAME_LEN];
4171 while (len && val[len-1] == '\n')
4173 if (len >= DISK_NAME_LEN)
4175 strlcpy(buf, val, len+1);
4176 if (strncmp(buf, "md_", 3) != 0)
4178 return md_alloc(0, buf);
4181 static void md_safemode_timeout(unsigned long data)
4183 mddev_t *mddev = (mddev_t *) data;
4185 if (!atomic_read(&mddev->writes_pending)) {
4186 mddev->safemode = 1;
4187 if (mddev->external)
4188 sysfs_notify_dirent(mddev->sysfs_state);
4190 md_wakeup_thread(mddev->thread);
4193 static int start_dirty_degraded;
4195 static int do_md_run(mddev_t * mddev)
4199 struct gendisk *disk;
4200 struct mdk_personality *pers;
4202 if (list_empty(&mddev->disks))
4203 /* cannot run an array with no devices.. */
4210 * Analyze all RAID superblock(s)
4212 if (!mddev->raid_disks) {
4213 if (!mddev->persistent)
4218 if (mddev->level != LEVEL_NONE)
4219 request_module("md-level-%d", mddev->level);
4220 else if (mddev->clevel[0])
4221 request_module("md-%s", mddev->clevel);
4224 * Drop all container device buffers, from now on
4225 * the only valid external interface is through the md
4228 list_for_each_entry(rdev, &mddev->disks, same_set) {
4229 if (test_bit(Faulty, &rdev->flags))
4231 sync_blockdev(rdev->bdev);
4232 invalidate_bdev(rdev->bdev);
4234 /* perform some consistency tests on the device.
4235 * We don't want the data to overlap the metadata,
4236 * Internal Bitmap issues have been handled elsewhere.
4238 if (rdev->data_offset < rdev->sb_start) {
4239 if (mddev->dev_sectors &&
4240 rdev->data_offset + mddev->dev_sectors
4242 printk("md: %s: data overlaps metadata\n",
4247 if (rdev->sb_start + rdev->sb_size/512
4248 > rdev->data_offset) {
4249 printk("md: %s: metadata overlaps data\n",
4254 sysfs_notify_dirent(rdev->sysfs_state);
4257 md_probe(mddev->unit, NULL, NULL);
4258 disk = mddev->gendisk;
4262 spin_lock(&pers_lock);
4263 pers = find_pers(mddev->level, mddev->clevel);
4264 if (!pers || !try_module_get(pers->owner)) {
4265 spin_unlock(&pers_lock);
4266 if (mddev->level != LEVEL_NONE)
4267 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4270 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4275 spin_unlock(&pers_lock);
4276 if (mddev->level != pers->level) {
4277 mddev->level = pers->level;
4278 mddev->new_level = pers->level;
4280 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
4282 if (mddev->reshape_position != MaxSector &&
4283 pers->start_reshape == NULL) {
4284 /* This personality cannot handle reshaping... */
4286 module_put(pers->owner);
4290 if (pers->sync_request) {
4291 /* Warn if this is a potentially silly
4294 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4298 list_for_each_entry(rdev, &mddev->disks, same_set)
4299 list_for_each_entry(rdev2, &mddev->disks, same_set) {
4301 rdev->bdev->bd_contains ==
4302 rdev2->bdev->bd_contains) {
4304 "%s: WARNING: %s appears to be"
4305 " on the same physical disk as"
4308 bdevname(rdev->bdev,b),
4309 bdevname(rdev2->bdev,b2));
4316 "True protection against single-disk"
4317 " failure might be compromised.\n");
4320 mddev->recovery = 0;
4321 /* may be over-ridden by personality */
4322 mddev->resync_max_sectors = mddev->dev_sectors;
4324 mddev->barriers_work = 1;
4325 mddev->ok_start_degraded = start_dirty_degraded;
4328 mddev->ro = 2; /* read-only, but switch on first write */
4330 err = mddev->pers->run(mddev);
4332 printk(KERN_ERR "md: pers->run() failed ...\n");
4333 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4334 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4335 " but 'external_size' not in effect?\n", __func__);
4337 "md: invalid array_size %llu > default size %llu\n",
4338 (unsigned long long)mddev->array_sectors / 2,
4339 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4341 mddev->pers->stop(mddev);
4343 if (err == 0 && mddev->pers->sync_request) {
4344 err = bitmap_create(mddev);
4346 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4347 mdname(mddev), err);
4348 mddev->pers->stop(mddev);
4352 module_put(mddev->pers->owner);
4354 bitmap_destroy(mddev);
4357 if (mddev->pers->sync_request) {
4358 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
4360 "md: cannot register extra attributes for %s\n",
4362 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
4363 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
4366 atomic_set(&mddev->writes_pending,0);
4367 atomic_set(&mddev->max_corr_read_errors,
4368 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
4369 mddev->safemode = 0;
4370 mddev->safemode_timer.function = md_safemode_timeout;
4371 mddev->safemode_timer.data = (unsigned long) mddev;
4372 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
4375 list_for_each_entry(rdev, &mddev->disks, same_set)
4376 if (rdev->raid_disk >= 0) {
4378 sprintf(nm, "rd%d", rdev->raid_disk);
4379 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
4380 printk("md: cannot register %s for %s\n",
4384 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4387 md_update_sb(mddev, 0);
4389 set_capacity(disk, mddev->array_sectors);
4391 /* If there is a partially-recovered drive we need to
4392 * start recovery here. If we leave it to md_check_recovery,
4393 * it will remove the drives and not do the right thing
4395 if (mddev->degraded && !mddev->sync_thread) {
4397 list_for_each_entry(rdev, &mddev->disks, same_set)
4398 if (rdev->raid_disk >= 0 &&
4399 !test_bit(In_sync, &rdev->flags) &&
4400 !test_bit(Faulty, &rdev->flags))
4401 /* complete an interrupted recovery */
4403 if (spares && mddev->pers->sync_request) {
4404 mddev->recovery = 0;
4405 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4406 mddev->sync_thread = md_register_thread(md_do_sync,
4409 if (!mddev->sync_thread) {
4410 printk(KERN_ERR "%s: could not start resync"
4413 /* leave the spares where they are, it shouldn't hurt */
4414 mddev->recovery = 0;
4418 md_wakeup_thread(mddev->thread);
4419 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
4421 revalidate_disk(mddev->gendisk);
4423 md_new_event(mddev);
4424 sysfs_notify_dirent(mddev->sysfs_state);
4425 if (mddev->sysfs_action)
4426 sysfs_notify_dirent(mddev->sysfs_action);
4427 sysfs_notify(&mddev->kobj, NULL, "degraded");
4428 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4432 static int restart_array(mddev_t *mddev)
4434 struct gendisk *disk = mddev->gendisk;
4436 /* Complain if it has no devices */
4437 if (list_empty(&mddev->disks))
4443 mddev->safemode = 0;
4445 set_disk_ro(disk, 0);
4446 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4448 /* Kick recovery or resync if necessary */
4449 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4450 md_wakeup_thread(mddev->thread);
4451 md_wakeup_thread(mddev->sync_thread);
4452 sysfs_notify_dirent(mddev->sysfs_state);
4456 /* similar to deny_write_access, but accounts for our holding a reference
4457 * to the file ourselves */
4458 static int deny_bitmap_write_access(struct file * file)
4460 struct inode *inode = file->f_mapping->host;
4462 spin_lock(&inode->i_lock);
4463 if (atomic_read(&inode->i_writecount) > 1) {
4464 spin_unlock(&inode->i_lock);
4467 atomic_set(&inode->i_writecount, -1);
4468 spin_unlock(&inode->i_lock);
4473 void restore_bitmap_write_access(struct file *file)
4475 struct inode *inode = file->f_mapping->host;
4477 spin_lock(&inode->i_lock);
4478 atomic_set(&inode->i_writecount, 1);
4479 spin_unlock(&inode->i_lock);
4483 * 0 - completely stop and dis-assemble array
4484 * 1 - switch to readonly
4485 * 2 - stop but do not disassemble array
4487 static int do_md_stop(mddev_t * mddev, int mode, int is_open)
4490 struct gendisk *disk = mddev->gendisk;
4493 mutex_lock(&mddev->open_mutex);
4494 if (atomic_read(&mddev->openers) > is_open) {
4495 printk("md: %s still in use.\n",mdname(mddev));
4497 } else if (mddev->pers) {
4499 if (mddev->sync_thread) {
4500 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4501 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4502 md_unregister_thread(mddev->sync_thread);
4503 mddev->sync_thread = NULL;
4506 del_timer_sync(&mddev->safemode_timer);
4509 case 1: /* readonly */
4515 case 0: /* disassemble */
4517 bitmap_flush(mddev);
4518 md_super_wait(mddev);
4520 set_disk_ro(disk, 0);
4522 mddev->pers->stop(mddev);
4523 mddev->queue->merge_bvec_fn = NULL;
4524 mddev->queue->unplug_fn = NULL;
4525 mddev->queue->backing_dev_info.congested_fn = NULL;
4526 module_put(mddev->pers->owner);
4527 if (mddev->pers->sync_request)
4528 mddev->private = &md_redundancy_group;
4530 /* tell userspace to handle 'inactive' */
4531 sysfs_notify_dirent(mddev->sysfs_state);
4533 list_for_each_entry(rdev, &mddev->disks, same_set)
4534 if (rdev->raid_disk >= 0) {
4536 sprintf(nm, "rd%d", rdev->raid_disk);
4537 sysfs_remove_link(&mddev->kobj, nm);
4540 set_capacity(disk, 0);
4546 if (!mddev->in_sync || mddev->flags) {
4547 /* mark array as shutdown cleanly */
4549 md_update_sb(mddev, 1);
4552 set_disk_ro(disk, 1);
4553 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4557 mutex_unlock(&mddev->open_mutex);
4561 * Free resources if final stop
4565 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4567 bitmap_destroy(mddev);
4568 if (mddev->bitmap_info.file) {
4569 restore_bitmap_write_access(mddev->bitmap_info.file);
4570 fput(mddev->bitmap_info.file);
4571 mddev->bitmap_info.file = NULL;
4573 mddev->bitmap_info.offset = 0;
4575 /* make sure all md_delayed_delete calls have finished */
4576 flush_scheduled_work();
4578 export_array(mddev);
4580 mddev->array_sectors = 0;
4581 mddev->external_size = 0;
4582 mddev->dev_sectors = 0;
4583 mddev->raid_disks = 0;
4584 mddev->recovery_cp = 0;
4585 mddev->resync_min = 0;
4586 mddev->resync_max = MaxSector;
4587 mddev->reshape_position = MaxSector;
4588 mddev->external = 0;
4589 mddev->persistent = 0;
4590 mddev->level = LEVEL_NONE;
4591 mddev->clevel[0] = 0;
4594 mddev->metadata_type[0] = 0;
4595 mddev->chunk_sectors = 0;
4596 mddev->ctime = mddev->utime = 0;
4598 mddev->max_disks = 0;
4600 mddev->delta_disks = 0;
4601 mddev->new_level = LEVEL_NONE;
4602 mddev->new_layout = 0;
4603 mddev->new_chunk_sectors = 0;
4604 mddev->curr_resync = 0;
4605 mddev->resync_mismatches = 0;
4606 mddev->suspend_lo = mddev->suspend_hi = 0;
4607 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4608 mddev->recovery = 0;
4611 mddev->degraded = 0;
4612 mddev->barriers_work = 0;
4613 mddev->safemode = 0;
4614 mddev->bitmap_info.offset = 0;
4615 mddev->bitmap_info.default_offset = 0;
4616 mddev->bitmap_info.chunksize = 0;
4617 mddev->bitmap_info.daemon_sleep = 0;
4618 mddev->bitmap_info.max_write_behind = 0;
4619 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4620 if (mddev->hold_active == UNTIL_STOP)
4621 mddev->hold_active = 0;
4623 } else if (mddev->pers)
4624 printk(KERN_INFO "md: %s switched to read-only mode.\n",
4627 blk_integrity_unregister(disk);
4628 md_new_event(mddev);
4629 sysfs_notify_dirent(mddev->sysfs_state);
4634 static void autorun_array(mddev_t *mddev)
4639 if (list_empty(&mddev->disks))
4642 printk(KERN_INFO "md: running: ");
4644 list_for_each_entry(rdev, &mddev->disks, same_set) {
4645 char b[BDEVNAME_SIZE];
4646 printk("<%s>", bdevname(rdev->bdev,b));
4650 err = do_md_run(mddev);
4652 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
4653 do_md_stop(mddev, 0, 0);
4658 * lets try to run arrays based on all disks that have arrived
4659 * until now. (those are in pending_raid_disks)
4661 * the method: pick the first pending disk, collect all disks with
4662 * the same UUID, remove all from the pending list and put them into
4663 * the 'same_array' list. Then order this list based on superblock
4664 * update time (freshest comes first), kick out 'old' disks and
4665 * compare superblocks. If everything's fine then run it.
4667 * If "unit" is allocated, then bump its reference count
4669 static void autorun_devices(int part)
4671 mdk_rdev_t *rdev0, *rdev, *tmp;
4673 char b[BDEVNAME_SIZE];
4675 printk(KERN_INFO "md: autorun ...\n");
4676 while (!list_empty(&pending_raid_disks)) {
4679 LIST_HEAD(candidates);
4680 rdev0 = list_entry(pending_raid_disks.next,
4681 mdk_rdev_t, same_set);
4683 printk(KERN_INFO "md: considering %s ...\n",
4684 bdevname(rdev0->bdev,b));
4685 INIT_LIST_HEAD(&candidates);
4686 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
4687 if (super_90_load(rdev, rdev0, 0) >= 0) {
4688 printk(KERN_INFO "md: adding %s ...\n",
4689 bdevname(rdev->bdev,b));
4690 list_move(&rdev->same_set, &candidates);
4693 * now we have a set of devices, with all of them having
4694 * mostly sane superblocks. It's time to allocate the
4698 dev = MKDEV(mdp_major,
4699 rdev0->preferred_minor << MdpMinorShift);
4700 unit = MINOR(dev) >> MdpMinorShift;
4702 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4705 if (rdev0->preferred_minor != unit) {
4706 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4707 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4711 md_probe(dev, NULL, NULL);
4712 mddev = mddev_find(dev);
4713 if (!mddev || !mddev->gendisk) {
4717 "md: cannot allocate memory for md drive.\n");
4720 if (mddev_lock(mddev))
4721 printk(KERN_WARNING "md: %s locked, cannot run\n",
4723 else if (mddev->raid_disks || mddev->major_version
4724 || !list_empty(&mddev->disks)) {
4726 "md: %s already running, cannot run %s\n",
4727 mdname(mddev), bdevname(rdev0->bdev,b));
4728 mddev_unlock(mddev);
4730 printk(KERN_INFO "md: created %s\n", mdname(mddev));
4731 mddev->persistent = 1;
4732 rdev_for_each_list(rdev, tmp, &candidates) {
4733 list_del_init(&rdev->same_set);
4734 if (bind_rdev_to_array(rdev, mddev))
4737 autorun_array(mddev);
4738 mddev_unlock(mddev);
4740 /* on success, candidates will be empty, on error
4743 rdev_for_each_list(rdev, tmp, &candidates) {
4744 list_del_init(&rdev->same_set);
4749 printk(KERN_INFO "md: ... autorun DONE.\n");
4751 #endif /* !MODULE */
4753 static int get_version(void __user * arg)
4757 ver.major = MD_MAJOR_VERSION;
4758 ver.minor = MD_MINOR_VERSION;
4759 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4761 if (copy_to_user(arg, &ver, sizeof(ver)))
4767 static int get_array_info(mddev_t * mddev, void __user * arg)
4769 mdu_array_info_t info;
4770 int nr,working,insync,failed,spare;
4773 nr=working=insync=failed=spare=0;
4774 list_for_each_entry(rdev, &mddev->disks, same_set) {
4776 if (test_bit(Faulty, &rdev->flags))
4780 if (test_bit(In_sync, &rdev->flags))
4787 info.major_version = mddev->major_version;
4788 info.minor_version = mddev->minor_version;
4789 info.patch_version = MD_PATCHLEVEL_VERSION;
4790 info.ctime = mddev->ctime;
4791 info.level = mddev->level;
4792 info.size = mddev->dev_sectors / 2;
4793 if (info.size != mddev->dev_sectors / 2) /* overflow */
4796 info.raid_disks = mddev->raid_disks;
4797 info.md_minor = mddev->md_minor;
4798 info.not_persistent= !mddev->persistent;
4800 info.utime = mddev->utime;
4803 info.state = (1<<MD_SB_CLEAN);
4804 if (mddev->bitmap && mddev->bitmap_info.offset)
4805 info.state = (1<<MD_SB_BITMAP_PRESENT);
4806 info.active_disks = insync;
4807 info.working_disks = working;
4808 info.failed_disks = failed;
4809 info.spare_disks = spare;
4811 info.layout = mddev->layout;
4812 info.chunk_size = mddev->chunk_sectors << 9;
4814 if (copy_to_user(arg, &info, sizeof(info)))
4820 static int get_bitmap_file(mddev_t * mddev, void __user * arg)
4822 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4823 char *ptr, *buf = NULL;
4826 if (md_allow_write(mddev))
4827 file = kmalloc(sizeof(*file), GFP_NOIO);
4829 file = kmalloc(sizeof(*file), GFP_KERNEL);
4834 /* bitmap disabled, zero the first byte and copy out */
4835 if (!mddev->bitmap || !mddev->bitmap->file) {
4836 file->pathname[0] = '\0';
4840 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4844 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4848 strcpy(file->pathname, ptr);
4852 if (copy_to_user(arg, file, sizeof(*file)))
4860 static int get_disk_info(mddev_t * mddev, void __user * arg)
4862 mdu_disk_info_t info;
4865 if (copy_from_user(&info, arg, sizeof(info)))
4868 rdev = find_rdev_nr(mddev, info.number);
4870 info.major = MAJOR(rdev->bdev->bd_dev);
4871 info.minor = MINOR(rdev->bdev->bd_dev);
4872 info.raid_disk = rdev->raid_disk;
4874 if (test_bit(Faulty, &rdev->flags))
4875 info.state |= (1<<MD_DISK_FAULTY);
4876 else if (test_bit(In_sync, &rdev->flags)) {
4877 info.state |= (1<<MD_DISK_ACTIVE);
4878 info.state |= (1<<MD_DISK_SYNC);
4880 if (test_bit(WriteMostly, &rdev->flags))
4881 info.state |= (1<<MD_DISK_WRITEMOSTLY);
4883 info.major = info.minor = 0;
4884 info.raid_disk = -1;
4885 info.state = (1<<MD_DISK_REMOVED);
4888 if (copy_to_user(arg, &info, sizeof(info)))
4894 static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4896 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4898 dev_t dev = MKDEV(info->major,info->minor);
4900 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4903 if (!mddev->raid_disks) {
4905 /* expecting a device which has a superblock */
4906 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4909 "md: md_import_device returned %ld\n",
4911 return PTR_ERR(rdev);
4913 if (!list_empty(&mddev->disks)) {
4914 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4915 mdk_rdev_t, same_set);
4916 err = super_types[mddev->major_version]
4917 .load_super(rdev, rdev0, mddev->minor_version);
4920 "md: %s has different UUID to %s\n",
4921 bdevname(rdev->bdev,b),
4922 bdevname(rdev0->bdev,b2));
4927 err = bind_rdev_to_array(rdev, mddev);
4934 * add_new_disk can be used once the array is assembled
4935 * to add "hot spares". They must already have a superblock
4940 if (!mddev->pers->hot_add_disk) {
4942 "%s: personality does not support diskops!\n",
4946 if (mddev->persistent)
4947 rdev = md_import_device(dev, mddev->major_version,
4948 mddev->minor_version);
4950 rdev = md_import_device(dev, -1, -1);
4953 "md: md_import_device returned %ld\n",
4955 return PTR_ERR(rdev);
4957 /* set save_raid_disk if appropriate */
4958 if (!mddev->persistent) {
4959 if (info->state & (1<<MD_DISK_SYNC) &&
4960 info->raid_disk < mddev->raid_disks)
4961 rdev->raid_disk = info->raid_disk;
4963 rdev->raid_disk = -1;
4965 super_types[mddev->major_version].
4966 validate_super(mddev, rdev);
4967 rdev->saved_raid_disk = rdev->raid_disk;
4969 clear_bit(In_sync, &rdev->flags); /* just to be sure */
4970 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4971 set_bit(WriteMostly, &rdev->flags);
4973 clear_bit(WriteMostly, &rdev->flags);
4975 rdev->raid_disk = -1;
4976 err = bind_rdev_to_array(rdev, mddev);
4977 if (!err && !mddev->pers->hot_remove_disk) {
4978 /* If there is hot_add_disk but no hot_remove_disk
4979 * then added disks for geometry changes,
4980 * and should be added immediately.
4982 super_types[mddev->major_version].
4983 validate_super(mddev, rdev);
4984 err = mddev->pers->hot_add_disk(mddev, rdev);
4986 unbind_rdev_from_array(rdev);
4991 sysfs_notify_dirent(rdev->sysfs_state);
4993 md_update_sb(mddev, 1);
4994 if (mddev->degraded)
4995 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4996 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4997 md_wakeup_thread(mddev->thread);
5001 /* otherwise, add_new_disk is only allowed
5002 * for major_version==0 superblocks
5004 if (mddev->major_version != 0) {
5005 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5010 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5012 rdev = md_import_device(dev, -1, 0);
5015 "md: error, md_import_device() returned %ld\n",
5017 return PTR_ERR(rdev);
5019 rdev->desc_nr = info->number;
5020 if (info->raid_disk < mddev->raid_disks)
5021 rdev->raid_disk = info->raid_disk;
5023 rdev->raid_disk = -1;
5025 if (rdev->raid_disk < mddev->raid_disks)
5026 if (info->state & (1<<MD_DISK_SYNC))
5027 set_bit(In_sync, &rdev->flags);
5029 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5030 set_bit(WriteMostly, &rdev->flags);
5032 if (!mddev->persistent) {
5033 printk(KERN_INFO "md: nonpersistent superblock ...\n");
5034 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
5036 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
5037 rdev->sectors = rdev->sb_start;
5039 err = bind_rdev_to_array(rdev, mddev);
5049 static int hot_remove_disk(mddev_t * mddev, dev_t dev)
5051 char b[BDEVNAME_SIZE];
5054 rdev = find_rdev(mddev, dev);
5058 if (rdev->raid_disk >= 0)
5061 kick_rdev_from_array(rdev);
5062 md_update_sb(mddev, 1);
5063 md_new_event(mddev);
5067 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
5068 bdevname(rdev->bdev,b), mdname(mddev));
5072 static int hot_add_disk(mddev_t * mddev, dev_t dev)
5074 char b[BDEVNAME_SIZE];
5081 if (mddev->major_version != 0) {
5082 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5083 " version-0 superblocks.\n",
5087 if (!mddev->pers->hot_add_disk) {
5089 "%s: personality does not support diskops!\n",
5094 rdev = md_import_device(dev, -1, 0);
5097 "md: error, md_import_device() returned %ld\n",
5102 if (mddev->persistent)
5103 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
5105 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
5107 rdev->sectors = rdev->sb_start;
5109 if (test_bit(Faulty, &rdev->flags)) {
5111 "md: can not hot-add faulty %s disk to %s!\n",
5112 bdevname(rdev->bdev,b), mdname(mddev));
5116 clear_bit(In_sync, &rdev->flags);
5118 rdev->saved_raid_disk = -1;
5119 err = bind_rdev_to_array(rdev, mddev);
5124 * The rest should better be atomic, we can have disk failures
5125 * noticed in interrupt contexts ...
5128 rdev->raid_disk = -1;
5130 md_update_sb(mddev, 1);
5133 * Kick recovery, maybe this spare has to be added to the
5134 * array immediately.
5136 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5137 md_wakeup_thread(mddev->thread);
5138 md_new_event(mddev);
5146 static int set_bitmap_file(mddev_t *mddev, int fd)
5151 if (!mddev->pers->quiesce)
5153 if (mddev->recovery || mddev->sync_thread)
5155 /* we should be able to change the bitmap.. */
5161 return -EEXIST; /* cannot add when bitmap is present */
5162 mddev->bitmap_info.file = fget(fd);
5164 if (mddev->bitmap_info.file == NULL) {
5165 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5170 err = deny_bitmap_write_access(mddev->bitmap_info.file);
5172 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5174 fput(mddev->bitmap_info.file);
5175 mddev->bitmap_info.file = NULL;
5178 mddev->bitmap_info.offset = 0; /* file overrides offset */
5179 } else if (mddev->bitmap == NULL)
5180 return -ENOENT; /* cannot remove what isn't there */
5183 mddev->pers->quiesce(mddev, 1);
5185 err = bitmap_create(mddev);
5186 if (fd < 0 || err) {
5187 bitmap_destroy(mddev);
5188 fd = -1; /* make sure to put the file */
5190 mddev->pers->quiesce(mddev, 0);
5193 if (mddev->bitmap_info.file) {
5194 restore_bitmap_write_access(mddev->bitmap_info.file);
5195 fput(mddev->bitmap_info.file);
5197 mddev->bitmap_info.file = NULL;
5204 * set_array_info is used two different ways
5205 * The original usage is when creating a new array.
5206 * In this usage, raid_disks is > 0 and it together with
5207 * level, size, not_persistent,layout,chunksize determine the
5208 * shape of the array.
5209 * This will always create an array with a type-0.90.0 superblock.
5210 * The newer usage is when assembling an array.
5211 * In this case raid_disks will be 0, and the major_version field is
5212 * use to determine which style super-blocks are to be found on the devices.
5213 * The minor and patch _version numbers are also kept incase the
5214 * super_block handler wishes to interpret them.
5216 static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5219 if (info->raid_disks == 0) {
5220 /* just setting version number for superblock loading */
5221 if (info->major_version < 0 ||
5222 info->major_version >= ARRAY_SIZE(super_types) ||
5223 super_types[info->major_version].name == NULL) {
5224 /* maybe try to auto-load a module? */
5226 "md: superblock version %d not known\n",
5227 info->major_version);
5230 mddev->major_version = info->major_version;
5231 mddev->minor_version = info->minor_version;
5232 mddev->patch_version = info->patch_version;
5233 mddev->persistent = !info->not_persistent;
5236 mddev->major_version = MD_MAJOR_VERSION;
5237 mddev->minor_version = MD_MINOR_VERSION;
5238 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5239 mddev->ctime = get_seconds();
5241 mddev->level = info->level;
5242 mddev->clevel[0] = 0;
5243 mddev->dev_sectors = 2 * (sector_t)info->size;
5244 mddev->raid_disks = info->raid_disks;
5245 /* don't set md_minor, it is determined by which /dev/md* was
5248 if (info->state & (1<<MD_SB_CLEAN))
5249 mddev->recovery_cp = MaxSector;
5251 mddev->recovery_cp = 0;
5252 mddev->persistent = ! info->not_persistent;
5253 mddev->external = 0;
5255 mddev->layout = info->layout;
5256 mddev->chunk_sectors = info->chunk_size >> 9;
5258 mddev->max_disks = MD_SB_DISKS;
5260 if (mddev->persistent)
5262 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5264 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5265 mddev->bitmap_info.offset = 0;
5267 mddev->reshape_position = MaxSector;
5270 * Generate a 128 bit UUID
5272 get_random_bytes(mddev->uuid, 16);
5274 mddev->new_level = mddev->level;
5275 mddev->new_chunk_sectors = mddev->chunk_sectors;
5276 mddev->new_layout = mddev->layout;
5277 mddev->delta_disks = 0;
5282 void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5284 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5286 if (mddev->external_size)
5289 mddev->array_sectors = array_sectors;
5291 EXPORT_SYMBOL(md_set_array_sectors);
5293 static int update_size(mddev_t *mddev, sector_t num_sectors)
5297 int fit = (num_sectors == 0);
5299 if (mddev->pers->resize == NULL)
5301 /* The "num_sectors" is the number of sectors of each device that
5302 * is used. This can only make sense for arrays with redundancy.
5303 * linear and raid0 always use whatever space is available. We can only
5304 * consider changing this number if no resync or reconstruction is
5305 * happening, and if the new size is acceptable. It must fit before the
5306 * sb_start or, if that is <data_offset, it must fit before the size
5307 * of each device. If num_sectors is zero, we find the largest size
5311 if (mddev->sync_thread)
5314 /* Sorry, cannot grow a bitmap yet, just remove it,
5318 list_for_each_entry(rdev, &mddev->disks, same_set) {
5319 sector_t avail = rdev->sectors;
5321 if (fit && (num_sectors == 0 || num_sectors > avail))
5322 num_sectors = avail;
5323 if (avail < num_sectors)
5326 rv = mddev->pers->resize(mddev, num_sectors);
5328 revalidate_disk(mddev->gendisk);
5332 static int update_raid_disks(mddev_t *mddev, int raid_disks)
5335 /* change the number of raid disks */
5336 if (mddev->pers->check_reshape == NULL)
5338 if (raid_disks <= 0 ||
5339 raid_disks >= mddev->max_disks)
5341 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
5343 mddev->delta_disks = raid_disks - mddev->raid_disks;
5345 rv = mddev->pers->check_reshape(mddev);
5351 * update_array_info is used to change the configuration of an
5353 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5354 * fields in the info are checked against the array.
5355 * Any differences that cannot be handled will cause an error.
5356 * Normally, only one change can be managed at a time.
5358 static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5364 /* calculate expected state,ignoring low bits */
5365 if (mddev->bitmap && mddev->bitmap_info.offset)
5366 state |= (1 << MD_SB_BITMAP_PRESENT);
5368 if (mddev->major_version != info->major_version ||
5369 mddev->minor_version != info->minor_version ||
5370 /* mddev->patch_version != info->patch_version || */
5371 mddev->ctime != info->ctime ||
5372 mddev->level != info->level ||
5373 /* mddev->layout != info->layout || */
5374 !mddev->persistent != info->not_persistent||
5375 mddev->chunk_sectors != info->chunk_size >> 9 ||
5376 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5377 ((state^info->state) & 0xfffffe00)
5380 /* Check there is only one change */
5381 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5383 if (mddev->raid_disks != info->raid_disks)
5385 if (mddev->layout != info->layout)
5387 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5394 if (mddev->layout != info->layout) {
5396 * we don't need to do anything at the md level, the
5397 * personality will take care of it all.
5399 if (mddev->pers->check_reshape == NULL)
5402 mddev->new_layout = info->layout;
5403 rv = mddev->pers->check_reshape(mddev);
5405 mddev->new_layout = mddev->layout;
5409 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5410 rv = update_size(mddev, (sector_t)info->size * 2);
5412 if (mddev->raid_disks != info->raid_disks)
5413 rv = update_raid_disks(mddev, info->raid_disks);
5415 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5416 if (mddev->pers->quiesce == NULL)
5418 if (mddev->recovery || mddev->sync_thread)
5420 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5421 /* add the bitmap */
5424 if (mddev->bitmap_info.default_offset == 0)
5426 mddev->bitmap_info.offset =
5427 mddev->bitmap_info.default_offset;
5428 mddev->pers->quiesce(mddev, 1);
5429 rv = bitmap_create(mddev);
5431 bitmap_destroy(mddev);
5432 mddev->pers->quiesce(mddev, 0);
5434 /* remove the bitmap */
5437 if (mddev->bitmap->file)
5439 mddev->pers->quiesce(mddev, 1);
5440 bitmap_destroy(mddev);
5441 mddev->pers->quiesce(mddev, 0);
5442 mddev->bitmap_info.offset = 0;
5445 md_update_sb(mddev, 1);
5449 static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5453 if (mddev->pers == NULL)
5456 rdev = find_rdev(mddev, dev);
5460 md_error(mddev, rdev);
5465 * We have a problem here : there is no easy way to give a CHS
5466 * virtual geometry. We currently pretend that we have a 2 heads
5467 * 4 sectors (with a BIG number of cylinders...). This drives
5468 * dosfs just mad... ;-)
5470 static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5472 mddev_t *mddev = bdev->bd_disk->private_data;
5476 geo->cylinders = get_capacity(mddev->gendisk) / 8;
5480 static int md_ioctl(struct block_device *bdev, fmode_t mode,
5481 unsigned int cmd, unsigned long arg)
5484 void __user *argp = (void __user *)arg;
5485 mddev_t *mddev = NULL;
5487 if (!capable(CAP_SYS_ADMIN))
5491 * Commands dealing with the RAID driver but not any
5497 err = get_version(argp);
5500 case PRINT_RAID_DEBUG:
5508 autostart_arrays(arg);
5515 * Commands creating/starting a new array:
5518 mddev = bdev->bd_disk->private_data;
5525 err = mddev_lock(mddev);
5528 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5535 case SET_ARRAY_INFO:
5537 mdu_array_info_t info;
5539 memset(&info, 0, sizeof(info));
5540 else if (copy_from_user(&info, argp, sizeof(info))) {
5545 err = update_array_info(mddev, &info);
5547 printk(KERN_WARNING "md: couldn't update"
5548 " array info. %d\n", err);
5553 if (!list_empty(&mddev->disks)) {
5555 "md: array %s already has disks!\n",
5560 if (mddev->raid_disks) {
5562 "md: array %s already initialised!\n",
5567 err = set_array_info(mddev, &info);
5569 printk(KERN_WARNING "md: couldn't set"
5570 " array info. %d\n", err);
5580 * Commands querying/configuring an existing array:
5582 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
5583 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
5584 if ((!mddev->raid_disks && !mddev->external)
5585 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5586 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5587 && cmd != GET_BITMAP_FILE) {
5593 * Commands even a read-only array can execute:
5597 case GET_ARRAY_INFO:
5598 err = get_array_info(mddev, argp);
5601 case GET_BITMAP_FILE:
5602 err = get_bitmap_file(mddev, argp);
5606 err = get_disk_info(mddev, argp);
5609 case RESTART_ARRAY_RW:
5610 err = restart_array(mddev);
5614 err = do_md_stop(mddev, 0, 1);
5618 err = do_md_stop(mddev, 1, 1);
5624 * The remaining ioctls are changing the state of the
5625 * superblock, so we do not allow them on read-only arrays.
5626 * However non-MD ioctls (e.g. get-size) will still come through
5627 * here and hit the 'default' below, so only disallow
5628 * 'md' ioctls, and switch to rw mode if started auto-readonly.
5630 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
5631 if (mddev->ro == 2) {
5633 sysfs_notify_dirent(mddev->sysfs_state);
5634 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5635 md_wakeup_thread(mddev->thread);
5646 mdu_disk_info_t info;
5647 if (copy_from_user(&info, argp, sizeof(info)))
5650 err = add_new_disk(mddev, &info);
5654 case HOT_REMOVE_DISK:
5655 err = hot_remove_disk(mddev, new_decode_dev(arg));
5659 err = hot_add_disk(mddev, new_decode_dev(arg));
5662 case SET_DISK_FAULTY:
5663 err = set_disk_faulty(mddev, new_decode_dev(arg));
5667 err = do_md_run(mddev);
5670 case SET_BITMAP_FILE:
5671 err = set_bitmap_file(mddev, (int)arg);
5681 if (mddev->hold_active == UNTIL_IOCTL &&
5683 mddev->hold_active = 0;
5684 mddev_unlock(mddev);
5693 #ifdef CONFIG_COMPAT
5694 static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
5695 unsigned int cmd, unsigned long arg)
5698 case HOT_REMOVE_DISK:
5700 case SET_DISK_FAULTY:
5701 case SET_BITMAP_FILE:
5702 /* These take in integer arg, do not convert */
5705 arg = (unsigned long)compat_ptr(arg);
5709 return md_ioctl(bdev, mode, cmd, arg);
5711 #endif /* CONFIG_COMPAT */
5713 static int md_open(struct block_device *bdev, fmode_t mode)
5716 * Succeed if we can lock the mddev, which confirms that
5717 * it isn't being stopped right now.
5719 mddev_t *mddev = mddev_find(bdev->bd_dev);
5722 if (mddev->gendisk != bdev->bd_disk) {
5723 /* we are racing with mddev_put which is discarding this
5727 /* Wait until bdev->bd_disk is definitely gone */
5728 flush_scheduled_work();
5729 /* Then retry the open from the top */
5730 return -ERESTARTSYS;
5732 BUG_ON(mddev != bdev->bd_disk->private_data);
5734 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
5738 atomic_inc(&mddev->openers);
5739 mutex_unlock(&mddev->open_mutex);
5741 check_disk_change(bdev);
5746 static int md_release(struct gendisk *disk, fmode_t mode)
5748 mddev_t *mddev = disk->private_data;
5751 atomic_dec(&mddev->openers);
5757 static int md_media_changed(struct gendisk *disk)
5759 mddev_t *mddev = disk->private_data;
5761 return mddev->changed;
5764 static int md_revalidate(struct gendisk *disk)
5766 mddev_t *mddev = disk->private_data;
5771 static const struct block_device_operations md_fops =
5773 .owner = THIS_MODULE,
5775 .release = md_release,
5777 #ifdef CONFIG_COMPAT
5778 .compat_ioctl = md_compat_ioctl,
5780 .getgeo = md_getgeo,
5781 .media_changed = md_media_changed,
5782 .revalidate_disk= md_revalidate,
5785 static int md_thread(void * arg)
5787 mdk_thread_t *thread = arg;
5790 * md_thread is a 'system-thread', it's priority should be very
5791 * high. We avoid resource deadlocks individually in each
5792 * raid personality. (RAID5 does preallocation) We also use RR and
5793 * the very same RT priority as kswapd, thus we will never get
5794 * into a priority inversion deadlock.
5796 * we definitely have to have equal or higher priority than
5797 * bdflush, otherwise bdflush will deadlock if there are too
5798 * many dirty RAID5 blocks.
5801 allow_signal(SIGKILL);
5802 while (!kthread_should_stop()) {
5804 /* We need to wait INTERRUPTIBLE so that
5805 * we don't add to the load-average.
5806 * That means we need to be sure no signals are
5809 if (signal_pending(current))
5810 flush_signals(current);
5812 wait_event_interruptible_timeout
5814 test_bit(THREAD_WAKEUP, &thread->flags)
5815 || kthread_should_stop(),
5818 clear_bit(THREAD_WAKEUP, &thread->flags);
5820 thread->run(thread->mddev);
5826 void md_wakeup_thread(mdk_thread_t *thread)
5829 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5830 set_bit(THREAD_WAKEUP, &thread->flags);
5831 wake_up(&thread->wqueue);
5835 mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5838 mdk_thread_t *thread;
5840 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
5844 init_waitqueue_head(&thread->wqueue);
5847 thread->mddev = mddev;
5848 thread->timeout = MAX_SCHEDULE_TIMEOUT;
5849 thread->tsk = kthread_run(md_thread, thread,
5851 mdname(thread->mddev),
5852 name ?: mddev->pers->name);
5853 if (IS_ERR(thread->tsk)) {
5860 void md_unregister_thread(mdk_thread_t *thread)
5864 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
5866 kthread_stop(thread->tsk);
5870 void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
5877 if (!rdev || test_bit(Faulty, &rdev->flags))
5880 if (mddev->external)
5881 set_bit(Blocked, &rdev->flags);
5883 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5885 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5886 __builtin_return_address(0),__builtin_return_address(1),
5887 __builtin_return_address(2),__builtin_return_address(3));
5891 if (!mddev->pers->error_handler)
5893 mddev->pers->error_handler(mddev,rdev);
5894 if (mddev->degraded)
5895 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
5896 set_bit(StateChanged, &rdev->flags);
5897 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5898 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5899 md_wakeup_thread(mddev->thread);
5900 md_new_event_inintr(mddev);
5903 /* seq_file implementation /proc/mdstat */
5905 static void status_unused(struct seq_file *seq)
5910 seq_printf(seq, "unused devices: ");
5912 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
5913 char b[BDEVNAME_SIZE];
5915 seq_printf(seq, "%s ",
5916 bdevname(rdev->bdev,b));
5919 seq_printf(seq, "<none>");
5921 seq_printf(seq, "\n");
5925 static void status_resync(struct seq_file *seq, mddev_t * mddev)
5927 sector_t max_sectors, resync, res;
5928 unsigned long dt, db;
5931 unsigned int per_milli;
5933 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
5935 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
5936 max_sectors = mddev->resync_max_sectors;
5938 max_sectors = mddev->dev_sectors;
5941 * Should not happen.
5947 /* Pick 'scale' such that (resync>>scale)*1000 will fit
5948 * in a sector_t, and (max_sectors>>scale) will fit in a
5949 * u32, as those are the requirements for sector_div.
5950 * Thus 'scale' must be at least 10
5953 if (sizeof(sector_t) > sizeof(unsigned long)) {
5954 while ( max_sectors/2 > (1ULL<<(scale+32)))
5957 res = (resync>>scale)*1000;
5958 sector_div(res, (u32)((max_sectors>>scale)+1));
5962 int i, x = per_milli/50, y = 20-x;
5963 seq_printf(seq, "[");
5964 for (i = 0; i < x; i++)
5965 seq_printf(seq, "=");
5966 seq_printf(seq, ">");
5967 for (i = 0; i < y; i++)
5968 seq_printf(seq, ".");
5969 seq_printf(seq, "] ");
5971 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
5972 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
5974 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
5976 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
5977 "resync" : "recovery"))),
5978 per_milli/10, per_milli % 10,
5979 (unsigned long long) resync/2,
5980 (unsigned long long) max_sectors/2);
5983 * dt: time from mark until now
5984 * db: blocks written from mark until now
5985 * rt: remaining time
5987 * rt is a sector_t, so could be 32bit or 64bit.
5988 * So we divide before multiply in case it is 32bit and close
5990 * We scale the divisor (db) by 32 to avoid loosing precision
5991 * near the end of resync when the number of remaining sectors
5993 * We then divide rt by 32 after multiplying by db to compensate.
5994 * The '+1' avoids division by zero if db is very small.
5996 dt = ((jiffies - mddev->resync_mark) / HZ);
5998 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
5999 - mddev->resync_mark_cnt;
6001 rt = max_sectors - resync; /* number of remaining sectors */
6002 sector_div(rt, db/32+1);
6006 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6007 ((unsigned long)rt % 60)/6);
6009 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
6012 static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6014 struct list_head *tmp;
6024 spin_lock(&all_mddevs_lock);
6025 list_for_each(tmp,&all_mddevs)
6027 mddev = list_entry(tmp, mddev_t, all_mddevs);
6029 spin_unlock(&all_mddevs_lock);
6032 spin_unlock(&all_mddevs_lock);
6034 return (void*)2;/* tail */
6038 static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6040 struct list_head *tmp;
6041 mddev_t *next_mddev, *mddev = v;
6047 spin_lock(&all_mddevs_lock);
6049 tmp = all_mddevs.next;
6051 tmp = mddev->all_mddevs.next;
6052 if (tmp != &all_mddevs)
6053 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
6055 next_mddev = (void*)2;
6058 spin_unlock(&all_mddevs_lock);
6066 static void md_seq_stop(struct seq_file *seq, void *v)
6070 if (mddev && v != (void*)1 && v != (void*)2)
6074 struct mdstat_info {
6078 static int md_seq_show(struct seq_file *seq, void *v)
6083 struct mdstat_info *mi = seq->private;
6084 struct bitmap *bitmap;
6086 if (v == (void*)1) {
6087 struct mdk_personality *pers;
6088 seq_printf(seq, "Personalities : ");
6089 spin_lock(&pers_lock);
6090 list_for_each_entry(pers, &pers_list, list)
6091 seq_printf(seq, "[%s] ", pers->name);
6093 spin_unlock(&pers_lock);
6094 seq_printf(seq, "\n");
6095 mi->event = atomic_read(&md_event_count);
6098 if (v == (void*)2) {
6103 if (mddev_lock(mddev) < 0)
6106 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6107 seq_printf(seq, "%s : %sactive", mdname(mddev),
6108 mddev->pers ? "" : "in");
6111 seq_printf(seq, " (read-only)");
6113 seq_printf(seq, " (auto-read-only)");
6114 seq_printf(seq, " %s", mddev->pers->name);
6118 list_for_each_entry(rdev, &mddev->disks, same_set) {
6119 char b[BDEVNAME_SIZE];
6120 seq_printf(seq, " %s[%d]",
6121 bdevname(rdev->bdev,b), rdev->desc_nr);
6122 if (test_bit(WriteMostly, &rdev->flags))
6123 seq_printf(seq, "(W)");
6124 if (test_bit(Faulty, &rdev->flags)) {
6125 seq_printf(seq, "(F)");
6127 } else if (rdev->raid_disk < 0)
6128 seq_printf(seq, "(S)"); /* spare */
6129 sectors += rdev->sectors;
6132 if (!list_empty(&mddev->disks)) {
6134 seq_printf(seq, "\n %llu blocks",
6135 (unsigned long long)
6136 mddev->array_sectors / 2);
6138 seq_printf(seq, "\n %llu blocks",
6139 (unsigned long long)sectors / 2);
6141 if (mddev->persistent) {
6142 if (mddev->major_version != 0 ||
6143 mddev->minor_version != 90) {
6144 seq_printf(seq," super %d.%d",
6145 mddev->major_version,
6146 mddev->minor_version);
6148 } else if (mddev->external)
6149 seq_printf(seq, " super external:%s",
6150 mddev->metadata_type);
6152 seq_printf(seq, " super non-persistent");
6155 mddev->pers->status(seq, mddev);
6156 seq_printf(seq, "\n ");
6157 if (mddev->pers->sync_request) {
6158 if (mddev->curr_resync > 2) {
6159 status_resync(seq, mddev);
6160 seq_printf(seq, "\n ");
6161 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6162 seq_printf(seq, "\tresync=DELAYED\n ");
6163 else if (mddev->recovery_cp < MaxSector)
6164 seq_printf(seq, "\tresync=PENDING\n ");
6167 seq_printf(seq, "\n ");
6169 if ((bitmap = mddev->bitmap)) {
6170 unsigned long chunk_kb;
6171 unsigned long flags;
6172 spin_lock_irqsave(&bitmap->lock, flags);
6173 chunk_kb = mddev->bitmap_info.chunksize >> 10;
6174 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6176 bitmap->pages - bitmap->missing_pages,
6178 (bitmap->pages - bitmap->missing_pages)
6179 << (PAGE_SHIFT - 10),
6180 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
6181 chunk_kb ? "KB" : "B");
6183 seq_printf(seq, ", file: ");
6184 seq_path(seq, &bitmap->file->f_path, " \t\n");
6187 seq_printf(seq, "\n");
6188 spin_unlock_irqrestore(&bitmap->lock, flags);
6191 seq_printf(seq, "\n");
6193 mddev_unlock(mddev);
6198 static const struct seq_operations md_seq_ops = {
6199 .start = md_seq_start,
6200 .next = md_seq_next,
6201 .stop = md_seq_stop,
6202 .show = md_seq_show,
6205 static int md_seq_open(struct inode *inode, struct file *file)
6208 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
6212 error = seq_open(file, &md_seq_ops);
6216 struct seq_file *p = file->private_data;
6218 mi->event = atomic_read(&md_event_count);
6223 static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6225 struct seq_file *m = filp->private_data;
6226 struct mdstat_info *mi = m->private;
6229 poll_wait(filp, &md_event_waiters, wait);
6231 /* always allow read */
6232 mask = POLLIN | POLLRDNORM;
6234 if (mi->event != atomic_read(&md_event_count))
6235 mask |= POLLERR | POLLPRI;
6239 static const struct file_operations md_seq_fops = {
6240 .owner = THIS_MODULE,
6241 .open = md_seq_open,
6243 .llseek = seq_lseek,
6244 .release = seq_release_private,
6245 .poll = mdstat_poll,
6248 int register_md_personality(struct mdk_personality *p)
6250 spin_lock(&pers_lock);
6251 list_add_tail(&p->list, &pers_list);
6252 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
6253 spin_unlock(&pers_lock);
6257 int unregister_md_personality(struct mdk_personality *p)
6259 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
6260 spin_lock(&pers_lock);
6261 list_del_init(&p->list);
6262 spin_unlock(&pers_lock);
6266 static int is_mddev_idle(mddev_t *mddev, int init)
6274 rdev_for_each_rcu(rdev, mddev) {
6275 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
6276 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6277 (int)part_stat_read(&disk->part0, sectors[1]) -
6278 atomic_read(&disk->sync_io);
6279 /* sync IO will cause sync_io to increase before the disk_stats
6280 * as sync_io is counted when a request starts, and
6281 * disk_stats is counted when it completes.
6282 * So resync activity will cause curr_events to be smaller than
6283 * when there was no such activity.
6284 * non-sync IO will cause disk_stat to increase without
6285 * increasing sync_io so curr_events will (eventually)
6286 * be larger than it was before. Once it becomes
6287 * substantially larger, the test below will cause
6288 * the array to appear non-idle, and resync will slow
6290 * If there is a lot of outstanding resync activity when
6291 * we set last_event to curr_events, then all that activity
6292 * completing might cause the array to appear non-idle
6293 * and resync will be slowed down even though there might
6294 * not have been non-resync activity. This will only
6295 * happen once though. 'last_events' will soon reflect
6296 * the state where there is little or no outstanding
6297 * resync requests, and further resync activity will
6298 * always make curr_events less than last_events.
6301 if (init || curr_events - rdev->last_events > 64) {
6302 rdev->last_events = curr_events;
6310 void md_done_sync(mddev_t *mddev, int blocks, int ok)
6312 /* another "blocks" (512byte) blocks have been synced */
6313 atomic_sub(blocks, &mddev->recovery_active);
6314 wake_up(&mddev->recovery_wait);
6316 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6317 md_wakeup_thread(mddev->thread);
6318 // stop recovery, signal do_sync ....
6323 /* md_write_start(mddev, bi)
6324 * If we need to update some array metadata (e.g. 'active' flag
6325 * in superblock) before writing, schedule a superblock update
6326 * and wait for it to complete.
6328 void md_write_start(mddev_t *mddev, struct bio *bi)
6331 if (bio_data_dir(bi) != WRITE)
6334 BUG_ON(mddev->ro == 1);
6335 if (mddev->ro == 2) {
6336 /* need to switch to read/write */
6338 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6339 md_wakeup_thread(mddev->thread);
6340 md_wakeup_thread(mddev->sync_thread);
6343 atomic_inc(&mddev->writes_pending);
6344 if (mddev->safemode == 1)
6345 mddev->safemode = 0;
6346 if (mddev->in_sync) {
6347 spin_lock_irq(&mddev->write_lock);
6348 if (mddev->in_sync) {
6350 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6351 md_wakeup_thread(mddev->thread);
6354 spin_unlock_irq(&mddev->write_lock);
6357 sysfs_notify_dirent(mddev->sysfs_state);
6358 wait_event(mddev->sb_wait,
6359 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
6360 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
6363 void md_write_end(mddev_t *mddev)
6365 if (atomic_dec_and_test(&mddev->writes_pending)) {
6366 if (mddev->safemode == 2)
6367 md_wakeup_thread(mddev->thread);
6368 else if (mddev->safemode_delay)
6369 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6373 /* md_allow_write(mddev)
6374 * Calling this ensures that the array is marked 'active' so that writes
6375 * may proceed without blocking. It is important to call this before
6376 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6377 * Must be called with mddev_lock held.
6379 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6380 * is dropped, so return -EAGAIN after notifying userspace.
6382 int md_allow_write(mddev_t *mddev)
6388 if (!mddev->pers->sync_request)
6391 spin_lock_irq(&mddev->write_lock);
6392 if (mddev->in_sync) {
6394 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6395 if (mddev->safemode_delay &&
6396 mddev->safemode == 0)
6397 mddev->safemode = 1;
6398 spin_unlock_irq(&mddev->write_lock);
6399 md_update_sb(mddev, 0);
6400 sysfs_notify_dirent(mddev->sysfs_state);
6402 spin_unlock_irq(&mddev->write_lock);
6404 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
6409 EXPORT_SYMBOL_GPL(md_allow_write);
6411 #define SYNC_MARKS 10
6412 #define SYNC_MARK_STEP (3*HZ)
6413 void md_do_sync(mddev_t *mddev)
6416 unsigned int currspeed = 0,
6418 sector_t max_sectors,j, io_sectors;
6419 unsigned long mark[SYNC_MARKS];
6420 sector_t mark_cnt[SYNC_MARKS];
6422 struct list_head *tmp;
6423 sector_t last_check;
6428 /* just incase thread restarts... */
6429 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6431 if (mddev->ro) /* never try to sync a read-only array */
6434 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6435 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6436 desc = "data-check";
6437 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6438 desc = "requested-resync";
6441 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6446 /* we overload curr_resync somewhat here.
6447 * 0 == not engaged in resync at all
6448 * 2 == checking that there is no conflict with another sync
6449 * 1 == like 2, but have yielded to allow conflicting resync to
6451 * other == active in resync - this many blocks
6453 * Before starting a resync we must have set curr_resync to
6454 * 2, and then checked that every "conflicting" array has curr_resync
6455 * less than ours. When we find one that is the same or higher
6456 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6457 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6458 * This will mean we have to start checking from the beginning again.
6463 mddev->curr_resync = 2;
6466 if (kthread_should_stop()) {
6467 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6470 for_each_mddev(mddev2, tmp) {
6471 if (mddev2 == mddev)
6473 if (!mddev->parallel_resync
6474 && mddev2->curr_resync
6475 && match_mddev_units(mddev, mddev2)) {
6477 if (mddev < mddev2 && mddev->curr_resync == 2) {
6478 /* arbitrarily yield */
6479 mddev->curr_resync = 1;
6480 wake_up(&resync_wait);
6482 if (mddev > mddev2 && mddev->curr_resync == 1)
6483 /* no need to wait here, we can wait the next
6484 * time 'round when curr_resync == 2
6487 /* We need to wait 'interruptible' so as not to
6488 * contribute to the load average, and not to
6489 * be caught by 'softlockup'
6491 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
6492 if (!kthread_should_stop() &&
6493 mddev2->curr_resync >= mddev->curr_resync) {
6494 printk(KERN_INFO "md: delaying %s of %s"
6495 " until %s has finished (they"
6496 " share one or more physical units)\n",
6497 desc, mdname(mddev), mdname(mddev2));
6499 if (signal_pending(current))
6500 flush_signals(current);
6502 finish_wait(&resync_wait, &wq);
6505 finish_wait(&resync_wait, &wq);
6508 } while (mddev->curr_resync < 2);
6511 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6512 /* resync follows the size requested by the personality,
6513 * which defaults to physical size, but can be virtual size
6515 max_sectors = mddev->resync_max_sectors;
6516 mddev->resync_mismatches = 0;
6517 /* we don't use the checkpoint if there's a bitmap */
6518 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6519 j = mddev->resync_min;
6520 else if (!mddev->bitmap)
6521 j = mddev->recovery_cp;
6523 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6524 max_sectors = mddev->dev_sectors;
6526 /* recovery follows the physical size of devices */
6527 max_sectors = mddev->dev_sectors;
6530 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
6531 if (rdev->raid_disk >= 0 &&
6532 !test_bit(Faulty, &rdev->flags) &&
6533 !test_bit(In_sync, &rdev->flags) &&
6534 rdev->recovery_offset < j)
6535 j = rdev->recovery_offset;
6539 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6540 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6541 " %d KB/sec/disk.\n", speed_min(mddev));
6542 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
6543 "(but not more than %d KB/sec) for %s.\n",
6544 speed_max(mddev), desc);
6546 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
6549 for (m = 0; m < SYNC_MARKS; m++) {
6551 mark_cnt[m] = io_sectors;
6554 mddev->resync_mark = mark[last_mark];
6555 mddev->resync_mark_cnt = mark_cnt[last_mark];
6558 * Tune reconstruction:
6560 window = 32*(PAGE_SIZE/512);
6561 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6562 window/2,(unsigned long long) max_sectors/2);
6564 atomic_set(&mddev->recovery_active, 0);
6569 "md: resuming %s of %s from checkpoint.\n",
6570 desc, mdname(mddev));
6571 mddev->curr_resync = j;
6573 mddev->curr_resync_completed = mddev->curr_resync;
6575 while (j < max_sectors) {
6580 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6581 ((mddev->curr_resync > mddev->curr_resync_completed &&
6582 (mddev->curr_resync - mddev->curr_resync_completed)
6583 > (max_sectors >> 4)) ||
6584 (j - mddev->curr_resync_completed)*2
6585 >= mddev->resync_max - mddev->curr_resync_completed
6587 /* time to update curr_resync_completed */
6588 blk_unplug(mddev->queue);
6589 wait_event(mddev->recovery_wait,
6590 atomic_read(&mddev->recovery_active) == 0);
6591 mddev->curr_resync_completed =
6593 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6594 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
6597 while (j >= mddev->resync_max && !kthread_should_stop()) {
6598 /* As this condition is controlled by user-space,
6599 * we can block indefinitely, so use '_interruptible'
6600 * to avoid triggering warnings.
6602 flush_signals(current); /* just in case */
6603 wait_event_interruptible(mddev->recovery_wait,
6604 mddev->resync_max > j
6605 || kthread_should_stop());
6608 if (kthread_should_stop())
6611 sectors = mddev->pers->sync_request(mddev, j, &skipped,
6612 currspeed < speed_min(mddev));
6614 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6618 if (!skipped) { /* actual IO requested */
6619 io_sectors += sectors;
6620 atomic_add(sectors, &mddev->recovery_active);
6624 if (j>1) mddev->curr_resync = j;
6625 mddev->curr_mark_cnt = io_sectors;
6626 if (last_check == 0)
6627 /* this is the earliers that rebuilt will be
6628 * visible in /proc/mdstat
6630 md_new_event(mddev);
6632 if (last_check + window > io_sectors || j == max_sectors)
6635 last_check = io_sectors;
6637 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6641 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6643 int next = (last_mark+1) % SYNC_MARKS;
6645 mddev->resync_mark = mark[next];
6646 mddev->resync_mark_cnt = mark_cnt[next];
6647 mark[next] = jiffies;
6648 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
6653 if (kthread_should_stop())
6658 * this loop exits only if either when we are slower than
6659 * the 'hard' speed limit, or the system was IO-idle for
6661 * the system might be non-idle CPU-wise, but we only care
6662 * about not overloading the IO subsystem. (things like an
6663 * e2fsck being done on the RAID array should execute fast)
6665 blk_unplug(mddev->queue);
6668 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6669 /((jiffies-mddev->resync_mark)/HZ +1) +1;
6671 if (currspeed > speed_min(mddev)) {
6672 if ((currspeed > speed_max(mddev)) ||
6673 !is_mddev_idle(mddev, 0)) {
6679 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
6681 * this also signals 'finished resyncing' to md_stop
6684 blk_unplug(mddev->queue);
6686 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6688 /* tell personality that we are finished */
6689 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
6691 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
6692 mddev->curr_resync > 2) {
6693 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6694 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6695 if (mddev->curr_resync >= mddev->recovery_cp) {
6697 "md: checkpointing %s of %s.\n",
6698 desc, mdname(mddev));
6699 mddev->recovery_cp = mddev->curr_resync;
6702 mddev->recovery_cp = MaxSector;
6704 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6705 mddev->curr_resync = MaxSector;
6707 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
6708 if (rdev->raid_disk >= 0 &&
6709 !test_bit(Faulty, &rdev->flags) &&
6710 !test_bit(In_sync, &rdev->flags) &&
6711 rdev->recovery_offset < mddev->curr_resync)
6712 rdev->recovery_offset = mddev->curr_resync;
6716 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6719 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6720 /* We completed so min/max setting can be forgotten if used. */
6721 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6722 mddev->resync_min = 0;
6723 mddev->resync_max = MaxSector;
6724 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6725 mddev->resync_min = mddev->curr_resync_completed;
6726 mddev->curr_resync = 0;
6727 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6728 mddev->curr_resync_completed = 0;
6729 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
6730 wake_up(&resync_wait);
6731 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6732 md_wakeup_thread(mddev->thread);
6737 * got a signal, exit.
6740 "md: md_do_sync() got signal ... exiting\n");
6741 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6745 EXPORT_SYMBOL_GPL(md_do_sync);
6748 static int remove_and_add_spares(mddev_t *mddev)
6753 mddev->curr_resync_completed = 0;
6755 list_for_each_entry(rdev, &mddev->disks, same_set)
6756 if (rdev->raid_disk >= 0 &&
6757 !test_bit(Blocked, &rdev->flags) &&
6758 (test_bit(Faulty, &rdev->flags) ||
6759 ! test_bit(In_sync, &rdev->flags)) &&
6760 atomic_read(&rdev->nr_pending)==0) {
6761 if (mddev->pers->hot_remove_disk(
6762 mddev, rdev->raid_disk)==0) {
6764 sprintf(nm,"rd%d", rdev->raid_disk);
6765 sysfs_remove_link(&mddev->kobj, nm);
6766 rdev->raid_disk = -1;
6770 if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) {
6771 list_for_each_entry(rdev, &mddev->disks, same_set) {
6772 if (rdev->raid_disk >= 0 &&
6773 !test_bit(In_sync, &rdev->flags) &&
6774 !test_bit(Blocked, &rdev->flags))
6776 if (rdev->raid_disk < 0
6777 && !test_bit(Faulty, &rdev->flags)) {
6778 rdev->recovery_offset = 0;
6780 hot_add_disk(mddev, rdev) == 0) {
6782 sprintf(nm, "rd%d", rdev->raid_disk);
6783 if (sysfs_create_link(&mddev->kobj,
6786 "md: cannot register "
6790 md_new_event(mddev);
6791 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6800 * This routine is regularly called by all per-raid-array threads to
6801 * deal with generic issues like resync and super-block update.
6802 * Raid personalities that don't have a thread (linear/raid0) do not
6803 * need this as they never do any recovery or update the superblock.
6805 * It does not do any resync itself, but rather "forks" off other threads
6806 * to do that as needed.
6807 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6808 * "->recovery" and create a thread at ->sync_thread.
6809 * When the thread finishes it sets MD_RECOVERY_DONE
6810 * and wakeups up this thread which will reap the thread and finish up.
6811 * This thread also removes any faulty devices (with nr_pending == 0).
6813 * The overall approach is:
6814 * 1/ if the superblock needs updating, update it.
6815 * 2/ If a recovery thread is running, don't do anything else.
6816 * 3/ If recovery has finished, clean up, possibly marking spares active.
6817 * 4/ If there are any faulty devices, remove them.
6818 * 5/ If array is degraded, try to add spares devices
6819 * 6/ If array has spares or is not in-sync, start a resync thread.
6821 void md_check_recovery(mddev_t *mddev)
6827 bitmap_daemon_work(mddev);
6832 if (signal_pending(current)) {
6833 if (mddev->pers->sync_request && !mddev->external) {
6834 printk(KERN_INFO "md: %s in immediate safe mode\n",
6836 mddev->safemode = 2;
6838 flush_signals(current);
6841 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
6844 (mddev->flags && !mddev->external) ||
6845 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
6846 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
6847 (mddev->external == 0 && mddev->safemode == 1) ||
6848 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6849 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
6853 if (mddev_trylock(mddev)) {
6857 /* Only thing we do on a ro array is remove
6860 remove_and_add_spares(mddev);
6861 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6865 if (!mddev->external) {
6867 spin_lock_irq(&mddev->write_lock);
6868 if (mddev->safemode &&
6869 !atomic_read(&mddev->writes_pending) &&
6871 mddev->recovery_cp == MaxSector) {
6874 if (mddev->persistent)
6875 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6877 if (mddev->safemode == 1)
6878 mddev->safemode = 0;
6879 spin_unlock_irq(&mddev->write_lock);
6881 sysfs_notify_dirent(mddev->sysfs_state);
6885 md_update_sb(mddev, 0);
6887 list_for_each_entry(rdev, &mddev->disks, same_set)
6888 if (test_and_clear_bit(StateChanged, &rdev->flags))
6889 sysfs_notify_dirent(rdev->sysfs_state);
6892 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
6893 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
6894 /* resync/recovery still happening */
6895 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6898 if (mddev->sync_thread) {
6899 /* resync has finished, collect result */
6900 md_unregister_thread(mddev->sync_thread);
6901 mddev->sync_thread = NULL;
6902 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
6903 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
6905 /* activate any spares */
6906 if (mddev->pers->spare_active(mddev))
6907 sysfs_notify(&mddev->kobj, NULL,
6910 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6911 mddev->pers->finish_reshape)
6912 mddev->pers->finish_reshape(mddev);
6913 md_update_sb(mddev, 1);
6915 /* if array is no-longer degraded, then any saved_raid_disk
6916 * information must be scrapped
6918 if (!mddev->degraded)
6919 list_for_each_entry(rdev, &mddev->disks, same_set)
6920 rdev->saved_raid_disk = -1;
6922 mddev->recovery = 0;
6923 /* flag recovery needed just to double check */
6924 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6925 sysfs_notify_dirent(mddev->sysfs_action);
6926 md_new_event(mddev);
6929 /* Set RUNNING before clearing NEEDED to avoid
6930 * any transients in the value of "sync_action".
6932 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6933 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6934 /* Clear some bits that don't mean anything, but
6937 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
6938 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
6940 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
6942 /* no recovery is running.
6943 * remove any failed drives, then
6944 * add spares if possible.
6945 * Spare are also removed and re-added, to allow
6946 * the personality to fail the re-add.
6949 if (mddev->reshape_position != MaxSector) {
6950 if (mddev->pers->check_reshape == NULL ||
6951 mddev->pers->check_reshape(mddev) != 0)
6952 /* Cannot proceed */
6954 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
6955 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
6956 } else if ((spares = remove_and_add_spares(mddev))) {
6957 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6958 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
6959 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
6960 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
6961 } else if (mddev->recovery_cp < MaxSector) {
6962 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6963 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
6964 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
6965 /* nothing to be done ... */
6968 if (mddev->pers->sync_request) {
6969 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
6970 /* We are adding a device or devices to an array
6971 * which has the bitmap stored on all devices.
6972 * So make sure all bitmap pages get written
6974 bitmap_write_all(mddev->bitmap);
6976 mddev->sync_thread = md_register_thread(md_do_sync,
6979 if (!mddev->sync_thread) {
6980 printk(KERN_ERR "%s: could not start resync"
6983 /* leave the spares where they are, it shouldn't hurt */
6984 mddev->recovery = 0;
6986 md_wakeup_thread(mddev->sync_thread);
6987 sysfs_notify_dirent(mddev->sysfs_action);
6988 md_new_event(mddev);
6991 if (!mddev->sync_thread) {
6992 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6993 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
6995 if (mddev->sysfs_action)
6996 sysfs_notify_dirent(mddev->sysfs_action);
6998 mddev_unlock(mddev);
7002 void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
7004 sysfs_notify_dirent(rdev->sysfs_state);
7005 wait_event_timeout(rdev->blocked_wait,
7006 !test_bit(Blocked, &rdev->flags),
7007 msecs_to_jiffies(5000));
7008 rdev_dec_pending(rdev, mddev);
7010 EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7012 static int md_notify_reboot(struct notifier_block *this,
7013 unsigned long code, void *x)
7015 struct list_head *tmp;
7018 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
7020 printk(KERN_INFO "md: stopping all md devices.\n");
7022 for_each_mddev(mddev, tmp)
7023 if (mddev_trylock(mddev)) {
7024 /* Force a switch to readonly even array
7025 * appears to still be in use. Hence
7028 do_md_stop(mddev, 1, 100);
7029 mddev_unlock(mddev);
7032 * certain more exotic SCSI devices are known to be
7033 * volatile wrt too early system reboots. While the
7034 * right place to handle this issue is the given
7035 * driver, we do want to have a safe RAID driver ...
7042 static struct notifier_block md_notifier = {
7043 .notifier_call = md_notify_reboot,
7045 .priority = INT_MAX, /* before any real devices */
7048 static void md_geninit(void)
7050 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
7052 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
7055 static int __init md_init(void)
7057 if (register_blkdev(MD_MAJOR, "md"))
7059 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
7060 unregister_blkdev(MD_MAJOR, "md");
7063 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
7064 md_probe, NULL, NULL);
7065 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
7066 md_probe, NULL, NULL);
7068 register_reboot_notifier(&md_notifier);
7069 raid_table_header = register_sysctl_table(raid_root_table);
7079 * Searches all registered partitions for autorun RAID arrays
7083 static LIST_HEAD(all_detected_devices);
7084 struct detected_devices_node {
7085 struct list_head list;
7089 void md_autodetect_dev(dev_t dev)
7091 struct detected_devices_node *node_detected_dev;
7093 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
7094 if (node_detected_dev) {
7095 node_detected_dev->dev = dev;
7096 list_add_tail(&node_detected_dev->list, &all_detected_devices);
7098 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
7099 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
7104 static void autostart_arrays(int part)
7107 struct detected_devices_node *node_detected_dev;
7109 int i_scanned, i_passed;
7114 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
7116 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
7118 node_detected_dev = list_entry(all_detected_devices.next,
7119 struct detected_devices_node, list);
7120 list_del(&node_detected_dev->list);
7121 dev = node_detected_dev->dev;
7122 kfree(node_detected_dev);
7123 rdev = md_import_device(dev,0, 90);
7127 if (test_bit(Faulty, &rdev->flags)) {
7131 set_bit(AutoDetected, &rdev->flags);
7132 list_add(&rdev->same_set, &pending_raid_disks);
7136 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
7137 i_scanned, i_passed);
7139 autorun_devices(part);
7142 #endif /* !MODULE */
7144 static __exit void md_exit(void)
7147 struct list_head *tmp;
7149 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
7150 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
7152 unregister_blkdev(MD_MAJOR,"md");
7153 unregister_blkdev(mdp_major, "mdp");
7154 unregister_reboot_notifier(&md_notifier);
7155 unregister_sysctl_table(raid_table_header);
7156 remove_proc_entry("mdstat", NULL);
7157 for_each_mddev(mddev, tmp) {
7158 export_array(mddev);
7159 mddev->hold_active = 0;
7163 subsys_initcall(md_init);
7164 module_exit(md_exit)
7166 static int get_ro(char *buffer, struct kernel_param *kp)
7168 return sprintf(buffer, "%d", start_readonly);
7170 static int set_ro(const char *val, struct kernel_param *kp)
7173 int num = simple_strtoul(val, &e, 10);
7174 if (*val && (*e == '\0' || *e == '\n')) {
7175 start_readonly = num;
7181 module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
7182 module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
7184 module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
7186 EXPORT_SYMBOL(register_md_personality);
7187 EXPORT_SYMBOL(unregister_md_personality);
7188 EXPORT_SYMBOL(md_error);
7189 EXPORT_SYMBOL(md_done_sync);
7190 EXPORT_SYMBOL(md_write_start);
7191 EXPORT_SYMBOL(md_write_end);
7192 EXPORT_SYMBOL(md_register_thread);
7193 EXPORT_SYMBOL(md_unregister_thread);
7194 EXPORT_SYMBOL(md_wakeup_thread);
7195 EXPORT_SYMBOL(md_check_recovery);
7196 MODULE_LICENSE("GPL");
7197 MODULE_DESCRIPTION("MD RAID framework");
7199 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);