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/string.h>
43 #include <linux/hdreg.h>
44 #include <linux/proc_fs.h>
45 #include <linux/random.h>
46 #include <linux/reboot.h>
47 #include <linux/file.h>
48 #include <linux/compat.h>
49 #include <linux/delay.h>
50 #include <linux/raid/md_p.h>
51 #include <linux/raid/md_u.h>
56 #define dprintk(x...) ((void)(DEBUG && printk(x)))
60 static void autostart_arrays(int part);
63 static LIST_HEAD(pers_list);
64 static DEFINE_SPINLOCK(pers_lock);
66 static void md_print_devices(void);
68 static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
70 #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
73 * Default number of read corrections we'll attempt on an rdev
74 * before ejecting it from the array. We divide the read error
75 * count by 2 for every hour elapsed between read errors.
77 #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
79 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
80 * is 1000 KB/sec, so the extra system load does not show up that much.
81 * Increase it if you want to have more _guaranteed_ speed. Note that
82 * the RAID driver will use the maximum available bandwidth if the IO
83 * subsystem is idle. There is also an 'absolute maximum' reconstruction
84 * speed limit - in case reconstruction slows down your system despite
87 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
88 * or /sys/block/mdX/md/sync_speed_{min,max}
91 static int sysctl_speed_limit_min = 1000;
92 static int sysctl_speed_limit_max = 200000;
93 static inline int speed_min(mddev_t *mddev)
95 return mddev->sync_speed_min ?
96 mddev->sync_speed_min : sysctl_speed_limit_min;
99 static inline int speed_max(mddev_t *mddev)
101 return mddev->sync_speed_max ?
102 mddev->sync_speed_max : sysctl_speed_limit_max;
105 static struct ctl_table_header *raid_table_header;
107 static ctl_table raid_table[] = {
109 .procname = "speed_limit_min",
110 .data = &sysctl_speed_limit_min,
111 .maxlen = sizeof(int),
112 .mode = S_IRUGO|S_IWUSR,
113 .proc_handler = proc_dointvec,
116 .procname = "speed_limit_max",
117 .data = &sysctl_speed_limit_max,
118 .maxlen = sizeof(int),
119 .mode = S_IRUGO|S_IWUSR,
120 .proc_handler = proc_dointvec,
125 static ctl_table raid_dir_table[] = {
129 .mode = S_IRUGO|S_IXUGO,
135 static ctl_table raid_root_table[] = {
140 .child = raid_dir_table,
145 static const struct block_device_operations md_fops;
147 static int start_readonly;
150 * We have a system wide 'event count' that is incremented
151 * on any 'interesting' event, and readers of /proc/mdstat
152 * can use 'poll' or 'select' to find out when the event
156 * start array, stop array, error, add device, remove device,
157 * start build, activate spare
159 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
160 static atomic_t md_event_count;
161 void md_new_event(mddev_t *mddev)
163 atomic_inc(&md_event_count);
164 wake_up(&md_event_waiters);
166 EXPORT_SYMBOL_GPL(md_new_event);
168 /* Alternate version that can be called from interrupts
169 * when calling sysfs_notify isn't needed.
171 static void md_new_event_inintr(mddev_t *mddev)
173 atomic_inc(&md_event_count);
174 wake_up(&md_event_waiters);
178 * Enables to iterate over all existing md arrays
179 * all_mddevs_lock protects this list.
181 static LIST_HEAD(all_mddevs);
182 static DEFINE_SPINLOCK(all_mddevs_lock);
186 * iterates through all used mddevs in the system.
187 * We take care to grab the all_mddevs_lock whenever navigating
188 * the list, and to always hold a refcount when unlocked.
189 * Any code which breaks out of this loop while own
190 * a reference to the current mddev and must mddev_put it.
192 #define for_each_mddev(mddev,tmp) \
194 for (({ spin_lock(&all_mddevs_lock); \
195 tmp = all_mddevs.next; \
197 ({ if (tmp != &all_mddevs) \
198 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
199 spin_unlock(&all_mddevs_lock); \
200 if (mddev) mddev_put(mddev); \
201 mddev = list_entry(tmp, mddev_t, all_mddevs); \
202 tmp != &all_mddevs;}); \
203 ({ spin_lock(&all_mddevs_lock); \
208 /* Rather than calling directly into the personality make_request function,
209 * IO requests come here first so that we can check if the device is
210 * being suspended pending a reconfiguration.
211 * We hold a refcount over the call to ->make_request. By the time that
212 * call has finished, the bio has been linked into some internal structure
213 * and so is visible to ->quiesce(), so we don't need the refcount any more.
215 static int md_make_request(struct request_queue *q, struct bio *bio)
217 mddev_t *mddev = q->queuedata;
219 if (mddev == NULL || mddev->pers == NULL) {
224 if (mddev->suspended || mddev->barrier) {
227 prepare_to_wait(&mddev->sb_wait, &__wait,
228 TASK_UNINTERRUPTIBLE);
229 if (!mddev->suspended && !mddev->barrier)
235 finish_wait(&mddev->sb_wait, &__wait);
237 atomic_inc(&mddev->active_io);
239 rv = mddev->pers->make_request(q, bio);
240 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
241 wake_up(&mddev->sb_wait);
246 static void mddev_suspend(mddev_t *mddev)
248 BUG_ON(mddev->suspended);
249 mddev->suspended = 1;
251 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
252 mddev->pers->quiesce(mddev, 1);
253 md_unregister_thread(mddev->thread);
254 mddev->thread = NULL;
255 /* we now know that no code is executing in the personality module,
256 * except possibly the tail end of a ->bi_end_io function, but that
257 * is certain to complete before the module has a chance to get
262 static void mddev_resume(mddev_t *mddev)
264 mddev->suspended = 0;
265 wake_up(&mddev->sb_wait);
266 mddev->pers->quiesce(mddev, 0);
269 int mddev_congested(mddev_t *mddev, int bits)
273 return mddev->suspended;
275 EXPORT_SYMBOL(mddev_congested);
278 * Generic barrier handling for md
281 #define POST_REQUEST_BARRIER ((void*)1)
283 static void md_end_barrier(struct bio *bio, int err)
285 mdk_rdev_t *rdev = bio->bi_private;
286 mddev_t *mddev = rdev->mddev;
287 if (err == -EOPNOTSUPP && mddev->barrier != POST_REQUEST_BARRIER)
288 set_bit(BIO_EOPNOTSUPP, &mddev->barrier->bi_flags);
290 rdev_dec_pending(rdev, mddev);
292 if (atomic_dec_and_test(&mddev->flush_pending)) {
293 if (mddev->barrier == POST_REQUEST_BARRIER) {
294 /* This was a post-request barrier */
295 mddev->barrier = NULL;
296 wake_up(&mddev->sb_wait);
298 /* The pre-request barrier has finished */
299 schedule_work(&mddev->barrier_work);
304 static void submit_barriers(mddev_t *mddev)
309 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
310 if (rdev->raid_disk >= 0 &&
311 !test_bit(Faulty, &rdev->flags)) {
312 /* Take two references, one is dropped
313 * when request finishes, one after
314 * we reclaim rcu_read_lock
317 atomic_inc(&rdev->nr_pending);
318 atomic_inc(&rdev->nr_pending);
320 bi = bio_alloc(GFP_KERNEL, 0);
321 bi->bi_end_io = md_end_barrier;
322 bi->bi_private = rdev;
323 bi->bi_bdev = rdev->bdev;
324 atomic_inc(&mddev->flush_pending);
325 submit_bio(WRITE_BARRIER, bi);
327 rdev_dec_pending(rdev, mddev);
332 static void md_submit_barrier(struct work_struct *ws)
334 mddev_t *mddev = container_of(ws, mddev_t, barrier_work);
335 struct bio *bio = mddev->barrier;
337 atomic_set(&mddev->flush_pending, 1);
339 if (test_bit(BIO_EOPNOTSUPP, &bio->bi_flags))
340 bio_endio(bio, -EOPNOTSUPP);
341 else if (bio->bi_size == 0)
342 /* an empty barrier - all done */
345 bio->bi_rw &= ~(1<<BIO_RW_BARRIER);
346 if (mddev->pers->make_request(mddev->queue, bio))
347 generic_make_request(bio);
348 mddev->barrier = POST_REQUEST_BARRIER;
349 submit_barriers(mddev);
351 if (atomic_dec_and_test(&mddev->flush_pending)) {
352 mddev->barrier = NULL;
353 wake_up(&mddev->sb_wait);
357 void md_barrier_request(mddev_t *mddev, struct bio *bio)
359 spin_lock_irq(&mddev->write_lock);
360 wait_event_lock_irq(mddev->sb_wait,
362 mddev->write_lock, /*nothing*/);
363 mddev->barrier = bio;
364 spin_unlock_irq(&mddev->write_lock);
366 atomic_set(&mddev->flush_pending, 1);
367 INIT_WORK(&mddev->barrier_work, md_submit_barrier);
369 submit_barriers(mddev);
371 if (atomic_dec_and_test(&mddev->flush_pending))
372 schedule_work(&mddev->barrier_work);
374 EXPORT_SYMBOL(md_barrier_request);
376 static inline mddev_t *mddev_get(mddev_t *mddev)
378 atomic_inc(&mddev->active);
382 static void mddev_delayed_delete(struct work_struct *ws);
384 static void mddev_put(mddev_t *mddev)
386 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
388 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
389 mddev->ctime == 0 && !mddev->hold_active) {
390 /* Array is not configured at all, and not held active,
392 list_del(&mddev->all_mddevs);
393 if (mddev->gendisk) {
394 /* we did a probe so need to clean up.
395 * Call schedule_work inside the spinlock
396 * so that flush_scheduled_work() after
397 * mddev_find will succeed in waiting for the
400 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
401 schedule_work(&mddev->del_work);
405 spin_unlock(&all_mddevs_lock);
408 static mddev_t * mddev_find(dev_t unit)
410 mddev_t *mddev, *new = NULL;
413 spin_lock(&all_mddevs_lock);
416 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
417 if (mddev->unit == unit) {
419 spin_unlock(&all_mddevs_lock);
425 list_add(&new->all_mddevs, &all_mddevs);
426 spin_unlock(&all_mddevs_lock);
427 new->hold_active = UNTIL_IOCTL;
431 /* find an unused unit number */
432 static int next_minor = 512;
433 int start = next_minor;
437 dev = MKDEV(MD_MAJOR, next_minor);
439 if (next_minor > MINORMASK)
441 if (next_minor == start) {
442 /* Oh dear, all in use. */
443 spin_unlock(&all_mddevs_lock);
449 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
450 if (mddev->unit == dev) {
456 new->md_minor = MINOR(dev);
457 new->hold_active = UNTIL_STOP;
458 list_add(&new->all_mddevs, &all_mddevs);
459 spin_unlock(&all_mddevs_lock);
462 spin_unlock(&all_mddevs_lock);
464 new = kzalloc(sizeof(*new), GFP_KERNEL);
469 if (MAJOR(unit) == MD_MAJOR)
470 new->md_minor = MINOR(unit);
472 new->md_minor = MINOR(unit) >> MdpMinorShift;
474 mutex_init(&new->open_mutex);
475 mutex_init(&new->reconfig_mutex);
476 mutex_init(&new->bitmap_info.mutex);
477 INIT_LIST_HEAD(&new->disks);
478 INIT_LIST_HEAD(&new->all_mddevs);
479 init_timer(&new->safemode_timer);
480 atomic_set(&new->active, 1);
481 atomic_set(&new->openers, 0);
482 atomic_set(&new->active_io, 0);
483 spin_lock_init(&new->write_lock);
484 atomic_set(&new->flush_pending, 0);
485 init_waitqueue_head(&new->sb_wait);
486 init_waitqueue_head(&new->recovery_wait);
487 new->reshape_position = MaxSector;
489 new->resync_max = MaxSector;
490 new->level = LEVEL_NONE;
495 static inline int mddev_lock(mddev_t * mddev)
497 return mutex_lock_interruptible(&mddev->reconfig_mutex);
500 static inline int mddev_is_locked(mddev_t *mddev)
502 return mutex_is_locked(&mddev->reconfig_mutex);
505 static inline int mddev_trylock(mddev_t * mddev)
507 return mutex_trylock(&mddev->reconfig_mutex);
510 static inline void mddev_unlock(mddev_t * mddev)
512 mutex_unlock(&mddev->reconfig_mutex);
514 md_wakeup_thread(mddev->thread);
517 static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
521 list_for_each_entry(rdev, &mddev->disks, same_set)
522 if (rdev->desc_nr == nr)
528 static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
532 list_for_each_entry(rdev, &mddev->disks, same_set)
533 if (rdev->bdev->bd_dev == dev)
539 static struct mdk_personality *find_pers(int level, char *clevel)
541 struct mdk_personality *pers;
542 list_for_each_entry(pers, &pers_list, list) {
543 if (level != LEVEL_NONE && pers->level == level)
545 if (strcmp(pers->name, clevel)==0)
551 /* return the offset of the super block in 512byte sectors */
552 static inline sector_t calc_dev_sboffset(struct block_device *bdev)
554 sector_t num_sectors = bdev->bd_inode->i_size / 512;
555 return MD_NEW_SIZE_SECTORS(num_sectors);
558 static int alloc_disk_sb(mdk_rdev_t * rdev)
563 rdev->sb_page = alloc_page(GFP_KERNEL);
564 if (!rdev->sb_page) {
565 printk(KERN_ALERT "md: out of memory.\n");
572 static void free_disk_sb(mdk_rdev_t * rdev)
575 put_page(rdev->sb_page);
577 rdev->sb_page = NULL;
584 static void super_written(struct bio *bio, int error)
586 mdk_rdev_t *rdev = bio->bi_private;
587 mddev_t *mddev = rdev->mddev;
589 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
590 printk("md: super_written gets error=%d, uptodate=%d\n",
591 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
592 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
593 md_error(mddev, rdev);
596 if (atomic_dec_and_test(&mddev->pending_writes))
597 wake_up(&mddev->sb_wait);
601 static void super_written_barrier(struct bio *bio, int error)
603 struct bio *bio2 = bio->bi_private;
604 mdk_rdev_t *rdev = bio2->bi_private;
605 mddev_t *mddev = rdev->mddev;
607 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
608 error == -EOPNOTSUPP) {
610 /* barriers don't appear to be supported :-( */
611 set_bit(BarriersNotsupp, &rdev->flags);
612 mddev->barriers_work = 0;
613 spin_lock_irqsave(&mddev->write_lock, flags);
614 bio2->bi_next = mddev->biolist;
615 mddev->biolist = bio2;
616 spin_unlock_irqrestore(&mddev->write_lock, flags);
617 wake_up(&mddev->sb_wait);
621 bio->bi_private = rdev;
622 super_written(bio, error);
626 void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
627 sector_t sector, int size, struct page *page)
629 /* write first size bytes of page to sector of rdev
630 * Increment mddev->pending_writes before returning
631 * and decrement it on completion, waking up sb_wait
632 * if zero is reached.
633 * If an error occurred, call md_error
635 * As we might need to resubmit the request if BIO_RW_BARRIER
636 * causes ENOTSUPP, we allocate a spare bio...
638 struct bio *bio = bio_alloc(GFP_NOIO, 1);
639 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNCIO) | (1<<BIO_RW_UNPLUG);
641 bio->bi_bdev = rdev->bdev;
642 bio->bi_sector = sector;
643 bio_add_page(bio, page, size, 0);
644 bio->bi_private = rdev;
645 bio->bi_end_io = super_written;
648 atomic_inc(&mddev->pending_writes);
649 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
651 rw |= (1<<BIO_RW_BARRIER);
652 rbio = bio_clone(bio, GFP_NOIO);
653 rbio->bi_private = bio;
654 rbio->bi_end_io = super_written_barrier;
655 submit_bio(rw, rbio);
660 void md_super_wait(mddev_t *mddev)
662 /* wait for all superblock writes that were scheduled to complete.
663 * if any had to be retried (due to BARRIER problems), retry them
667 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
668 if (atomic_read(&mddev->pending_writes)==0)
670 while (mddev->biolist) {
672 spin_lock_irq(&mddev->write_lock);
673 bio = mddev->biolist;
674 mddev->biolist = bio->bi_next ;
676 spin_unlock_irq(&mddev->write_lock);
677 submit_bio(bio->bi_rw, bio);
681 finish_wait(&mddev->sb_wait, &wq);
684 static void bi_complete(struct bio *bio, int error)
686 complete((struct completion*)bio->bi_private);
689 int sync_page_io(struct block_device *bdev, sector_t sector, int size,
690 struct page *page, int rw)
692 struct bio *bio = bio_alloc(GFP_NOIO, 1);
693 struct completion event;
696 rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG);
699 bio->bi_sector = sector;
700 bio_add_page(bio, page, size, 0);
701 init_completion(&event);
702 bio->bi_private = &event;
703 bio->bi_end_io = bi_complete;
705 wait_for_completion(&event);
707 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
711 EXPORT_SYMBOL_GPL(sync_page_io);
713 static int read_disk_sb(mdk_rdev_t * rdev, int size)
715 char b[BDEVNAME_SIZE];
716 if (!rdev->sb_page) {
724 if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ))
730 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
731 bdevname(rdev->bdev,b));
735 static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
737 return sb1->set_uuid0 == sb2->set_uuid0 &&
738 sb1->set_uuid1 == sb2->set_uuid1 &&
739 sb1->set_uuid2 == sb2->set_uuid2 &&
740 sb1->set_uuid3 == sb2->set_uuid3;
743 static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
746 mdp_super_t *tmp1, *tmp2;
748 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
749 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
751 if (!tmp1 || !tmp2) {
753 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
761 * nr_disks is not constant
766 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
774 static u32 md_csum_fold(u32 csum)
776 csum = (csum & 0xffff) + (csum >> 16);
777 return (csum & 0xffff) + (csum >> 16);
780 static unsigned int calc_sb_csum(mdp_super_t * sb)
783 u32 *sb32 = (u32*)sb;
785 unsigned int disk_csum, csum;
787 disk_csum = sb->sb_csum;
790 for (i = 0; i < MD_SB_BYTES/4 ; i++)
792 csum = (newcsum & 0xffffffff) + (newcsum>>32);
796 /* This used to use csum_partial, which was wrong for several
797 * reasons including that different results are returned on
798 * different architectures. It isn't critical that we get exactly
799 * the same return value as before (we always csum_fold before
800 * testing, and that removes any differences). However as we
801 * know that csum_partial always returned a 16bit value on
802 * alphas, do a fold to maximise conformity to previous behaviour.
804 sb->sb_csum = md_csum_fold(disk_csum);
806 sb->sb_csum = disk_csum;
813 * Handle superblock details.
814 * We want to be able to handle multiple superblock formats
815 * so we have a common interface to them all, and an array of
816 * different handlers.
817 * We rely on user-space to write the initial superblock, and support
818 * reading and updating of superblocks.
819 * Interface methods are:
820 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
821 * loads and validates a superblock on dev.
822 * if refdev != NULL, compare superblocks on both devices
824 * 0 - dev has a superblock that is compatible with refdev
825 * 1 - dev has a superblock that is compatible and newer than refdev
826 * so dev should be used as the refdev in future
827 * -EINVAL superblock incompatible or invalid
828 * -othererror e.g. -EIO
830 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
831 * Verify that dev is acceptable into mddev.
832 * The first time, mddev->raid_disks will be 0, and data from
833 * dev should be merged in. Subsequent calls check that dev
834 * is new enough. Return 0 or -EINVAL
836 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
837 * Update the superblock for rdev with data in mddev
838 * This does not write to disc.
844 struct module *owner;
845 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
847 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
848 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
849 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
850 sector_t num_sectors);
854 * Check that the given mddev has no bitmap.
856 * This function is called from the run method of all personalities that do not
857 * support bitmaps. It prints an error message and returns non-zero if mddev
858 * has a bitmap. Otherwise, it returns 0.
861 int md_check_no_bitmap(mddev_t *mddev)
863 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
865 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
866 mdname(mddev), mddev->pers->name);
869 EXPORT_SYMBOL(md_check_no_bitmap);
872 * load_super for 0.90.0
874 static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
876 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
881 * Calculate the position of the superblock (512byte sectors),
882 * it's at the end of the disk.
884 * It also happens to be a multiple of 4Kb.
886 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
888 ret = read_disk_sb(rdev, MD_SB_BYTES);
893 bdevname(rdev->bdev, b);
894 sb = (mdp_super_t*)page_address(rdev->sb_page);
896 if (sb->md_magic != MD_SB_MAGIC) {
897 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
902 if (sb->major_version != 0 ||
903 sb->minor_version < 90 ||
904 sb->minor_version > 91) {
905 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
906 sb->major_version, sb->minor_version,
911 if (sb->raid_disks <= 0)
914 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
915 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
920 rdev->preferred_minor = sb->md_minor;
921 rdev->data_offset = 0;
922 rdev->sb_size = MD_SB_BYTES;
924 if (sb->level == LEVEL_MULTIPATH)
927 rdev->desc_nr = sb->this_disk.number;
933 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
934 if (!uuid_equal(refsb, sb)) {
935 printk(KERN_WARNING "md: %s has different UUID to %s\n",
936 b, bdevname(refdev->bdev,b2));
939 if (!sb_equal(refsb, sb)) {
940 printk(KERN_WARNING "md: %s has same UUID"
941 " but different superblock to %s\n",
942 b, bdevname(refdev->bdev, b2));
946 ev2 = md_event(refsb);
952 rdev->sectors = rdev->sb_start;
954 if (rdev->sectors < sb->size * 2 && sb->level > 1)
955 /* "this cannot possibly happen" ... */
963 * validate_super for 0.90.0
965 static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
968 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
969 __u64 ev1 = md_event(sb);
971 rdev->raid_disk = -1;
972 clear_bit(Faulty, &rdev->flags);
973 clear_bit(In_sync, &rdev->flags);
974 clear_bit(WriteMostly, &rdev->flags);
975 clear_bit(BarriersNotsupp, &rdev->flags);
977 if (mddev->raid_disks == 0) {
978 mddev->major_version = 0;
979 mddev->minor_version = sb->minor_version;
980 mddev->patch_version = sb->patch_version;
982 mddev->chunk_sectors = sb->chunk_size >> 9;
983 mddev->ctime = sb->ctime;
984 mddev->utime = sb->utime;
985 mddev->level = sb->level;
986 mddev->clevel[0] = 0;
987 mddev->layout = sb->layout;
988 mddev->raid_disks = sb->raid_disks;
989 mddev->dev_sectors = sb->size * 2;
991 mddev->bitmap_info.offset = 0;
992 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
994 if (mddev->minor_version >= 91) {
995 mddev->reshape_position = sb->reshape_position;
996 mddev->delta_disks = sb->delta_disks;
997 mddev->new_level = sb->new_level;
998 mddev->new_layout = sb->new_layout;
999 mddev->new_chunk_sectors = sb->new_chunk >> 9;
1001 mddev->reshape_position = MaxSector;
1002 mddev->delta_disks = 0;
1003 mddev->new_level = mddev->level;
1004 mddev->new_layout = mddev->layout;
1005 mddev->new_chunk_sectors = mddev->chunk_sectors;
1008 if (sb->state & (1<<MD_SB_CLEAN))
1009 mddev->recovery_cp = MaxSector;
1011 if (sb->events_hi == sb->cp_events_hi &&
1012 sb->events_lo == sb->cp_events_lo) {
1013 mddev->recovery_cp = sb->recovery_cp;
1015 mddev->recovery_cp = 0;
1018 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1019 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1020 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1021 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1023 mddev->max_disks = MD_SB_DISKS;
1025 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
1026 mddev->bitmap_info.file == NULL)
1027 mddev->bitmap_info.offset =
1028 mddev->bitmap_info.default_offset;
1030 } else if (mddev->pers == NULL) {
1031 /* Insist on good event counter while assembling */
1033 if (ev1 < mddev->events)
1035 } else if (mddev->bitmap) {
1036 /* if adding to array with a bitmap, then we can accept an
1037 * older device ... but not too old.
1039 if (ev1 < mddev->bitmap->events_cleared)
1042 if (ev1 < mddev->events)
1043 /* just a hot-add of a new device, leave raid_disk at -1 */
1047 if (mddev->level != LEVEL_MULTIPATH) {
1048 desc = sb->disks + rdev->desc_nr;
1050 if (desc->state & (1<<MD_DISK_FAULTY))
1051 set_bit(Faulty, &rdev->flags);
1052 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1053 desc->raid_disk < mddev->raid_disks */) {
1054 set_bit(In_sync, &rdev->flags);
1055 rdev->raid_disk = desc->raid_disk;
1056 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1057 /* active but not in sync implies recovery up to
1058 * reshape position. We don't know exactly where
1059 * that is, so set to zero for now */
1060 if (mddev->minor_version >= 91) {
1061 rdev->recovery_offset = 0;
1062 rdev->raid_disk = desc->raid_disk;
1065 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1066 set_bit(WriteMostly, &rdev->flags);
1067 } else /* MULTIPATH are always insync */
1068 set_bit(In_sync, &rdev->flags);
1073 * sync_super for 0.90.0
1075 static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1079 int next_spare = mddev->raid_disks;
1082 /* make rdev->sb match mddev data..
1085 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1086 * 3/ any empty disks < next_spare become removed
1088 * disks[0] gets initialised to REMOVED because
1089 * we cannot be sure from other fields if it has
1090 * been initialised or not.
1093 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1095 rdev->sb_size = MD_SB_BYTES;
1097 sb = (mdp_super_t*)page_address(rdev->sb_page);
1099 memset(sb, 0, sizeof(*sb));
1101 sb->md_magic = MD_SB_MAGIC;
1102 sb->major_version = mddev->major_version;
1103 sb->patch_version = mddev->patch_version;
1104 sb->gvalid_words = 0; /* ignored */
1105 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1106 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1107 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1108 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1110 sb->ctime = mddev->ctime;
1111 sb->level = mddev->level;
1112 sb->size = mddev->dev_sectors / 2;
1113 sb->raid_disks = mddev->raid_disks;
1114 sb->md_minor = mddev->md_minor;
1115 sb->not_persistent = 0;
1116 sb->utime = mddev->utime;
1118 sb->events_hi = (mddev->events>>32);
1119 sb->events_lo = (u32)mddev->events;
1121 if (mddev->reshape_position == MaxSector)
1122 sb->minor_version = 90;
1124 sb->minor_version = 91;
1125 sb->reshape_position = mddev->reshape_position;
1126 sb->new_level = mddev->new_level;
1127 sb->delta_disks = mddev->delta_disks;
1128 sb->new_layout = mddev->new_layout;
1129 sb->new_chunk = mddev->new_chunk_sectors << 9;
1131 mddev->minor_version = sb->minor_version;
1134 sb->recovery_cp = mddev->recovery_cp;
1135 sb->cp_events_hi = (mddev->events>>32);
1136 sb->cp_events_lo = (u32)mddev->events;
1137 if (mddev->recovery_cp == MaxSector)
1138 sb->state = (1<< MD_SB_CLEAN);
1140 sb->recovery_cp = 0;
1142 sb->layout = mddev->layout;
1143 sb->chunk_size = mddev->chunk_sectors << 9;
1145 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
1146 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1148 sb->disks[0].state = (1<<MD_DISK_REMOVED);
1149 list_for_each_entry(rdev2, &mddev->disks, same_set) {
1152 int is_active = test_bit(In_sync, &rdev2->flags);
1154 if (rdev2->raid_disk >= 0 &&
1155 sb->minor_version >= 91)
1156 /* we have nowhere to store the recovery_offset,
1157 * but if it is not below the reshape_position,
1158 * we can piggy-back on that.
1161 if (rdev2->raid_disk < 0 ||
1162 test_bit(Faulty, &rdev2->flags))
1165 desc_nr = rdev2->raid_disk;
1167 desc_nr = next_spare++;
1168 rdev2->desc_nr = desc_nr;
1169 d = &sb->disks[rdev2->desc_nr];
1171 d->number = rdev2->desc_nr;
1172 d->major = MAJOR(rdev2->bdev->bd_dev);
1173 d->minor = MINOR(rdev2->bdev->bd_dev);
1175 d->raid_disk = rdev2->raid_disk;
1177 d->raid_disk = rdev2->desc_nr; /* compatibility */
1178 if (test_bit(Faulty, &rdev2->flags))
1179 d->state = (1<<MD_DISK_FAULTY);
1180 else if (is_active) {
1181 d->state = (1<<MD_DISK_ACTIVE);
1182 if (test_bit(In_sync, &rdev2->flags))
1183 d->state |= (1<<MD_DISK_SYNC);
1191 if (test_bit(WriteMostly, &rdev2->flags))
1192 d->state |= (1<<MD_DISK_WRITEMOSTLY);
1194 /* now set the "removed" and "faulty" bits on any missing devices */
1195 for (i=0 ; i < mddev->raid_disks ; i++) {
1196 mdp_disk_t *d = &sb->disks[i];
1197 if (d->state == 0 && d->number == 0) {
1200 d->state = (1<<MD_DISK_REMOVED);
1201 d->state |= (1<<MD_DISK_FAULTY);
1205 sb->nr_disks = nr_disks;
1206 sb->active_disks = active;
1207 sb->working_disks = working;
1208 sb->failed_disks = failed;
1209 sb->spare_disks = spare;
1211 sb->this_disk = sb->disks[rdev->desc_nr];
1212 sb->sb_csum = calc_sb_csum(sb);
1216 * rdev_size_change for 0.90.0
1218 static unsigned long long
1219 super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
1221 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1222 return 0; /* component must fit device */
1223 if (rdev->mddev->bitmap_info.offset)
1224 return 0; /* can't move bitmap */
1225 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
1226 if (!num_sectors || num_sectors > rdev->sb_start)
1227 num_sectors = rdev->sb_start;
1228 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1230 md_super_wait(rdev->mddev);
1231 return num_sectors / 2; /* kB for sysfs */
1236 * version 1 superblock
1239 static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
1243 unsigned long long newcsum;
1244 int size = 256 + le32_to_cpu(sb->max_dev)*2;
1245 __le32 *isuper = (__le32*)sb;
1248 disk_csum = sb->sb_csum;
1251 for (i=0; size>=4; size -= 4 )
1252 newcsum += le32_to_cpu(*isuper++);
1255 newcsum += le16_to_cpu(*(__le16*) isuper);
1257 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1258 sb->sb_csum = disk_csum;
1259 return cpu_to_le32(csum);
1262 static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1264 struct mdp_superblock_1 *sb;
1267 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1271 * Calculate the position of the superblock in 512byte sectors.
1272 * It is always aligned to a 4K boundary and
1273 * depeding on minor_version, it can be:
1274 * 0: At least 8K, but less than 12K, from end of device
1275 * 1: At start of device
1276 * 2: 4K from start of device.
1278 switch(minor_version) {
1280 sb_start = rdev->bdev->bd_inode->i_size >> 9;
1282 sb_start &= ~(sector_t)(4*2-1);
1293 rdev->sb_start = sb_start;
1295 /* superblock is rarely larger than 1K, but it can be larger,
1296 * and it is safe to read 4k, so we do that
1298 ret = read_disk_sb(rdev, 4096);
1299 if (ret) return ret;
1302 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1304 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1305 sb->major_version != cpu_to_le32(1) ||
1306 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1307 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
1308 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
1311 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1312 printk("md: invalid superblock checksum on %s\n",
1313 bdevname(rdev->bdev,b));
1316 if (le64_to_cpu(sb->data_size) < 10) {
1317 printk("md: data_size too small on %s\n",
1318 bdevname(rdev->bdev,b));
1322 rdev->preferred_minor = 0xffff;
1323 rdev->data_offset = le64_to_cpu(sb->data_offset);
1324 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
1326 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
1327 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1328 if (rdev->sb_size & bmask)
1329 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1332 && rdev->data_offset < sb_start + (rdev->sb_size/512))
1335 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1338 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1344 struct mdp_superblock_1 *refsb =
1345 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1347 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1348 sb->level != refsb->level ||
1349 sb->layout != refsb->layout ||
1350 sb->chunksize != refsb->chunksize) {
1351 printk(KERN_WARNING "md: %s has strangely different"
1352 " superblock to %s\n",
1353 bdevname(rdev->bdev,b),
1354 bdevname(refdev->bdev,b2));
1357 ev1 = le64_to_cpu(sb->events);
1358 ev2 = le64_to_cpu(refsb->events);
1366 rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) -
1367 le64_to_cpu(sb->data_offset);
1369 rdev->sectors = rdev->sb_start;
1370 if (rdev->sectors < le64_to_cpu(sb->data_size))
1372 rdev->sectors = le64_to_cpu(sb->data_size);
1373 if (le64_to_cpu(sb->size) > rdev->sectors)
1378 static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1380 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1381 __u64 ev1 = le64_to_cpu(sb->events);
1383 rdev->raid_disk = -1;
1384 clear_bit(Faulty, &rdev->flags);
1385 clear_bit(In_sync, &rdev->flags);
1386 clear_bit(WriteMostly, &rdev->flags);
1387 clear_bit(BarriersNotsupp, &rdev->flags);
1389 if (mddev->raid_disks == 0) {
1390 mddev->major_version = 1;
1391 mddev->patch_version = 0;
1392 mddev->external = 0;
1393 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
1394 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1395 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1396 mddev->level = le32_to_cpu(sb->level);
1397 mddev->clevel[0] = 0;
1398 mddev->layout = le32_to_cpu(sb->layout);
1399 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1400 mddev->dev_sectors = le64_to_cpu(sb->size);
1401 mddev->events = ev1;
1402 mddev->bitmap_info.offset = 0;
1403 mddev->bitmap_info.default_offset = 1024 >> 9;
1405 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1406 memcpy(mddev->uuid, sb->set_uuid, 16);
1408 mddev->max_disks = (4096-256)/2;
1410 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
1411 mddev->bitmap_info.file == NULL )
1412 mddev->bitmap_info.offset =
1413 (__s32)le32_to_cpu(sb->bitmap_offset);
1415 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1416 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1417 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1418 mddev->new_level = le32_to_cpu(sb->new_level);
1419 mddev->new_layout = le32_to_cpu(sb->new_layout);
1420 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
1422 mddev->reshape_position = MaxSector;
1423 mddev->delta_disks = 0;
1424 mddev->new_level = mddev->level;
1425 mddev->new_layout = mddev->layout;
1426 mddev->new_chunk_sectors = mddev->chunk_sectors;
1429 } else if (mddev->pers == NULL) {
1430 /* Insist of good event counter while assembling */
1432 if (ev1 < mddev->events)
1434 } else if (mddev->bitmap) {
1435 /* If adding to array with a bitmap, then we can accept an
1436 * older device, but not too old.
1438 if (ev1 < mddev->bitmap->events_cleared)
1441 if (ev1 < mddev->events)
1442 /* just a hot-add of a new device, leave raid_disk at -1 */
1445 if (mddev->level != LEVEL_MULTIPATH) {
1447 if (rdev->desc_nr < 0 ||
1448 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1452 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1454 case 0xffff: /* spare */
1456 case 0xfffe: /* faulty */
1457 set_bit(Faulty, &rdev->flags);
1460 if ((le32_to_cpu(sb->feature_map) &
1461 MD_FEATURE_RECOVERY_OFFSET))
1462 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1464 set_bit(In_sync, &rdev->flags);
1465 rdev->raid_disk = role;
1468 if (sb->devflags & WriteMostly1)
1469 set_bit(WriteMostly, &rdev->flags);
1470 } else /* MULTIPATH are always insync */
1471 set_bit(In_sync, &rdev->flags);
1476 static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1478 struct mdp_superblock_1 *sb;
1481 /* make rdev->sb match mddev and rdev data. */
1483 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1485 sb->feature_map = 0;
1487 sb->recovery_offset = cpu_to_le64(0);
1488 memset(sb->pad1, 0, sizeof(sb->pad1));
1489 memset(sb->pad2, 0, sizeof(sb->pad2));
1490 memset(sb->pad3, 0, sizeof(sb->pad3));
1492 sb->utime = cpu_to_le64((__u64)mddev->utime);
1493 sb->events = cpu_to_le64(mddev->events);
1495 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1497 sb->resync_offset = cpu_to_le64(0);
1499 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
1501 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
1502 sb->size = cpu_to_le64(mddev->dev_sectors);
1503 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
1504 sb->level = cpu_to_le32(mddev->level);
1505 sb->layout = cpu_to_le32(mddev->layout);
1507 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1508 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
1509 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
1512 if (rdev->raid_disk >= 0 &&
1513 !test_bit(In_sync, &rdev->flags)) {
1515 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1516 sb->recovery_offset =
1517 cpu_to_le64(rdev->recovery_offset);
1520 if (mddev->reshape_position != MaxSector) {
1521 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1522 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1523 sb->new_layout = cpu_to_le32(mddev->new_layout);
1524 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1525 sb->new_level = cpu_to_le32(mddev->new_level);
1526 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
1530 list_for_each_entry(rdev2, &mddev->disks, same_set)
1531 if (rdev2->desc_nr+1 > max_dev)
1532 max_dev = rdev2->desc_nr+1;
1534 if (max_dev > le32_to_cpu(sb->max_dev)) {
1536 sb->max_dev = cpu_to_le32(max_dev);
1537 rdev->sb_size = max_dev * 2 + 256;
1538 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1539 if (rdev->sb_size & bmask)
1540 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1542 for (i=0; i<max_dev;i++)
1543 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1545 list_for_each_entry(rdev2, &mddev->disks, same_set) {
1547 if (test_bit(Faulty, &rdev2->flags))
1548 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1549 else if (test_bit(In_sync, &rdev2->flags))
1550 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1551 else if (rdev2->raid_disk >= 0)
1552 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1554 sb->dev_roles[i] = cpu_to_le16(0xffff);
1557 sb->sb_csum = calc_sb_1_csum(sb);
1560 static unsigned long long
1561 super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
1563 struct mdp_superblock_1 *sb;
1564 sector_t max_sectors;
1565 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1566 return 0; /* component must fit device */
1567 if (rdev->sb_start < rdev->data_offset) {
1568 /* minor versions 1 and 2; superblock before data */
1569 max_sectors = rdev->bdev->bd_inode->i_size >> 9;
1570 max_sectors -= rdev->data_offset;
1571 if (!num_sectors || num_sectors > max_sectors)
1572 num_sectors = max_sectors;
1573 } else if (rdev->mddev->bitmap_info.offset) {
1574 /* minor version 0 with bitmap we can't move */
1577 /* minor version 0; superblock after data */
1579 sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1580 sb_start &= ~(sector_t)(4*2 - 1);
1581 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
1582 if (!num_sectors || num_sectors > max_sectors)
1583 num_sectors = max_sectors;
1584 rdev->sb_start = sb_start;
1586 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
1587 sb->data_size = cpu_to_le64(num_sectors);
1588 sb->super_offset = rdev->sb_start;
1589 sb->sb_csum = calc_sb_1_csum(sb);
1590 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1592 md_super_wait(rdev->mddev);
1593 return num_sectors / 2; /* kB for sysfs */
1596 static struct super_type super_types[] = {
1599 .owner = THIS_MODULE,
1600 .load_super = super_90_load,
1601 .validate_super = super_90_validate,
1602 .sync_super = super_90_sync,
1603 .rdev_size_change = super_90_rdev_size_change,
1607 .owner = THIS_MODULE,
1608 .load_super = super_1_load,
1609 .validate_super = super_1_validate,
1610 .sync_super = super_1_sync,
1611 .rdev_size_change = super_1_rdev_size_change,
1615 static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1617 mdk_rdev_t *rdev, *rdev2;
1620 rdev_for_each_rcu(rdev, mddev1)
1621 rdev_for_each_rcu(rdev2, mddev2)
1622 if (rdev->bdev->bd_contains ==
1623 rdev2->bdev->bd_contains) {
1631 static LIST_HEAD(pending_raid_disks);
1634 * Try to register data integrity profile for an mddev
1636 * This is called when an array is started and after a disk has been kicked
1637 * from the array. It only succeeds if all working and active component devices
1638 * are integrity capable with matching profiles.
1640 int md_integrity_register(mddev_t *mddev)
1642 mdk_rdev_t *rdev, *reference = NULL;
1644 if (list_empty(&mddev->disks))
1645 return 0; /* nothing to do */
1646 if (blk_get_integrity(mddev->gendisk))
1647 return 0; /* already registered */
1648 list_for_each_entry(rdev, &mddev->disks, same_set) {
1649 /* skip spares and non-functional disks */
1650 if (test_bit(Faulty, &rdev->flags))
1652 if (rdev->raid_disk < 0)
1655 * If at least one rdev is not integrity capable, we can not
1656 * enable data integrity for the md device.
1658 if (!bdev_get_integrity(rdev->bdev))
1661 /* Use the first rdev as the reference */
1665 /* does this rdev's profile match the reference profile? */
1666 if (blk_integrity_compare(reference->bdev->bd_disk,
1667 rdev->bdev->bd_disk) < 0)
1671 * All component devices are integrity capable and have matching
1672 * profiles, register the common profile for the md device.
1674 if (blk_integrity_register(mddev->gendisk,
1675 bdev_get_integrity(reference->bdev)) != 0) {
1676 printk(KERN_ERR "md: failed to register integrity for %s\n",
1680 printk(KERN_NOTICE "md: data integrity on %s enabled\n",
1684 EXPORT_SYMBOL(md_integrity_register);
1686 /* Disable data integrity if non-capable/non-matching disk is being added */
1687 void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
1689 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1690 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1692 if (!bi_mddev) /* nothing to do */
1694 if (rdev->raid_disk < 0) /* skip spares */
1696 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1697 rdev->bdev->bd_disk) >= 0)
1699 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1700 blk_integrity_unregister(mddev->gendisk);
1702 EXPORT_SYMBOL(md_integrity_add_rdev);
1704 static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1706 char b[BDEVNAME_SIZE];
1716 /* prevent duplicates */
1717 if (find_rdev(mddev, rdev->bdev->bd_dev))
1720 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1721 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1722 rdev->sectors < mddev->dev_sectors)) {
1724 /* Cannot change size, so fail
1725 * If mddev->level <= 0, then we don't care
1726 * about aligning sizes (e.g. linear)
1728 if (mddev->level > 0)
1731 mddev->dev_sectors = rdev->sectors;
1734 /* Verify rdev->desc_nr is unique.
1735 * If it is -1, assign a free number, else
1736 * check number is not in use
1738 if (rdev->desc_nr < 0) {
1740 if (mddev->pers) choice = mddev->raid_disks;
1741 while (find_rdev_nr(mddev, choice))
1743 rdev->desc_nr = choice;
1745 if (find_rdev_nr(mddev, rdev->desc_nr))
1748 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1749 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1750 mdname(mddev), mddev->max_disks);
1753 bdevname(rdev->bdev,b);
1754 while ( (s=strchr(b, '/')) != NULL)
1757 rdev->mddev = mddev;
1758 printk(KERN_INFO "md: bind<%s>\n", b);
1760 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
1763 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
1764 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1765 kobject_del(&rdev->kobj);
1768 rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state");
1770 list_add_rcu(&rdev->same_set, &mddev->disks);
1771 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
1773 /* May as well allow recovery to be retried once */
1774 mddev->recovery_disabled = 0;
1779 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1784 static void md_delayed_delete(struct work_struct *ws)
1786 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1787 kobject_del(&rdev->kobj);
1788 kobject_put(&rdev->kobj);
1791 static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1793 char b[BDEVNAME_SIZE];
1798 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
1799 list_del_rcu(&rdev->same_set);
1800 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1802 sysfs_remove_link(&rdev->kobj, "block");
1803 sysfs_put(rdev->sysfs_state);
1804 rdev->sysfs_state = NULL;
1805 /* We need to delay this, otherwise we can deadlock when
1806 * writing to 'remove' to "dev/state". We also need
1807 * to delay it due to rcu usage.
1810 INIT_WORK(&rdev->del_work, md_delayed_delete);
1811 kobject_get(&rdev->kobj);
1812 schedule_work(&rdev->del_work);
1816 * prevent the device from being mounted, repartitioned or
1817 * otherwise reused by a RAID array (or any other kernel
1818 * subsystem), by bd_claiming the device.
1820 static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
1823 struct block_device *bdev;
1824 char b[BDEVNAME_SIZE];
1826 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
1828 printk(KERN_ERR "md: could not open %s.\n",
1829 __bdevname(dev, b));
1830 return PTR_ERR(bdev);
1832 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
1834 printk(KERN_ERR "md: could not bd_claim %s.\n",
1836 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
1840 set_bit(AllReserved, &rdev->flags);
1845 static void unlock_rdev(mdk_rdev_t *rdev)
1847 struct block_device *bdev = rdev->bdev;
1852 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
1855 void md_autodetect_dev(dev_t dev);
1857 static void export_rdev(mdk_rdev_t * rdev)
1859 char b[BDEVNAME_SIZE];
1860 printk(KERN_INFO "md: export_rdev(%s)\n",
1861 bdevname(rdev->bdev,b));
1866 if (test_bit(AutoDetected, &rdev->flags))
1867 md_autodetect_dev(rdev->bdev->bd_dev);
1870 kobject_put(&rdev->kobj);
1873 static void kick_rdev_from_array(mdk_rdev_t * rdev)
1875 unbind_rdev_from_array(rdev);
1879 static void export_array(mddev_t *mddev)
1881 mdk_rdev_t *rdev, *tmp;
1883 rdev_for_each(rdev, tmp, mddev) {
1888 kick_rdev_from_array(rdev);
1890 if (!list_empty(&mddev->disks))
1892 mddev->raid_disks = 0;
1893 mddev->major_version = 0;
1896 static void print_desc(mdp_disk_t *desc)
1898 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1899 desc->major,desc->minor,desc->raid_disk,desc->state);
1902 static void print_sb_90(mdp_super_t *sb)
1907 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1908 sb->major_version, sb->minor_version, sb->patch_version,
1909 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1911 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1912 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1913 sb->md_minor, sb->layout, sb->chunk_size);
1914 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1915 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1916 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1917 sb->failed_disks, sb->spare_disks,
1918 sb->sb_csum, (unsigned long)sb->events_lo);
1921 for (i = 0; i < MD_SB_DISKS; i++) {
1924 desc = sb->disks + i;
1925 if (desc->number || desc->major || desc->minor ||
1926 desc->raid_disk || (desc->state && (desc->state != 4))) {
1927 printk(" D %2d: ", i);
1931 printk(KERN_INFO "md: THIS: ");
1932 print_desc(&sb->this_disk);
1935 static void print_sb_1(struct mdp_superblock_1 *sb)
1939 uuid = sb->set_uuid;
1941 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
1942 "md: Name: \"%s\" CT:%llu\n",
1943 le32_to_cpu(sb->major_version),
1944 le32_to_cpu(sb->feature_map),
1947 (unsigned long long)le64_to_cpu(sb->ctime)
1948 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
1950 uuid = sb->device_uuid;
1952 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
1954 "md: Dev:%08x UUID: %pU\n"
1955 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
1956 "md: (MaxDev:%u) \n",
1957 le32_to_cpu(sb->level),
1958 (unsigned long long)le64_to_cpu(sb->size),
1959 le32_to_cpu(sb->raid_disks),
1960 le32_to_cpu(sb->layout),
1961 le32_to_cpu(sb->chunksize),
1962 (unsigned long long)le64_to_cpu(sb->data_offset),
1963 (unsigned long long)le64_to_cpu(sb->data_size),
1964 (unsigned long long)le64_to_cpu(sb->super_offset),
1965 (unsigned long long)le64_to_cpu(sb->recovery_offset),
1966 le32_to_cpu(sb->dev_number),
1969 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
1970 (unsigned long long)le64_to_cpu(sb->events),
1971 (unsigned long long)le64_to_cpu(sb->resync_offset),
1972 le32_to_cpu(sb->sb_csum),
1973 le32_to_cpu(sb->max_dev)
1977 static void print_rdev(mdk_rdev_t *rdev, int major_version)
1979 char b[BDEVNAME_SIZE];
1980 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
1981 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
1982 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1984 if (rdev->sb_loaded) {
1985 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
1986 switch (major_version) {
1988 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
1991 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
1995 printk(KERN_INFO "md: no rdev superblock!\n");
1998 static void md_print_devices(void)
2000 struct list_head *tmp;
2003 char b[BDEVNAME_SIZE];
2006 printk("md: **********************************\n");
2007 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2008 printk("md: **********************************\n");
2009 for_each_mddev(mddev, tmp) {
2012 bitmap_print_sb(mddev->bitmap);
2014 printk("%s: ", mdname(mddev));
2015 list_for_each_entry(rdev, &mddev->disks, same_set)
2016 printk("<%s>", bdevname(rdev->bdev,b));
2019 list_for_each_entry(rdev, &mddev->disks, same_set)
2020 print_rdev(rdev, mddev->major_version);
2022 printk("md: **********************************\n");
2027 static void sync_sbs(mddev_t * mddev, int nospares)
2029 /* Update each superblock (in-memory image), but
2030 * if we are allowed to, skip spares which already
2031 * have the right event counter, or have one earlier
2032 * (which would mean they aren't being marked as dirty
2033 * with the rest of the array)
2037 /* First make sure individual recovery_offsets are correct */
2038 list_for_each_entry(rdev, &mddev->disks, same_set) {
2039 if (rdev->raid_disk >= 0 &&
2040 !test_bit(In_sync, &rdev->flags) &&
2041 mddev->curr_resync_completed > rdev->recovery_offset)
2042 rdev->recovery_offset = mddev->curr_resync_completed;
2045 list_for_each_entry(rdev, &mddev->disks, same_set) {
2046 if (rdev->sb_events == mddev->events ||
2048 rdev->raid_disk < 0 &&
2049 (rdev->sb_events&1)==0 &&
2050 rdev->sb_events+1 == mddev->events)) {
2051 /* Don't update this superblock */
2052 rdev->sb_loaded = 2;
2054 super_types[mddev->major_version].
2055 sync_super(mddev, rdev);
2056 rdev->sb_loaded = 1;
2061 static void md_update_sb(mddev_t * mddev, int force_change)
2067 mddev->utime = get_seconds();
2068 if (mddev->external)
2071 spin_lock_irq(&mddev->write_lock);
2073 set_bit(MD_CHANGE_PENDING, &mddev->flags);
2074 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2076 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2077 /* just a clean<-> dirty transition, possibly leave spares alone,
2078 * though if events isn't the right even/odd, we will have to do
2084 if (mddev->degraded)
2085 /* If the array is degraded, then skipping spares is both
2086 * dangerous and fairly pointless.
2087 * Dangerous because a device that was removed from the array
2088 * might have a event_count that still looks up-to-date,
2089 * so it can be re-added without a resync.
2090 * Pointless because if there are any spares to skip,
2091 * then a recovery will happen and soon that array won't
2092 * be degraded any more and the spare can go back to sleep then.
2096 sync_req = mddev->in_sync;
2098 /* If this is just a dirty<->clean transition, and the array is clean
2099 * and 'events' is odd, we can roll back to the previous clean state */
2101 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
2102 && (mddev->events & 1)
2103 && mddev->events != 1)
2106 /* otherwise we have to go forward and ... */
2108 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
2109 /* .. if the array isn't clean, an 'even' event must also go
2111 if ((mddev->events&1)==0)
2114 /* otherwise an 'odd' event must go to spares */
2115 if ((mddev->events&1))
2120 if (!mddev->events) {
2122 * oops, this 64-bit counter should never wrap.
2123 * Either we are in around ~1 trillion A.C., assuming
2124 * 1 reboot per second, or we have a bug:
2131 * do not write anything to disk if using
2132 * nonpersistent superblocks
2134 if (!mddev->persistent) {
2135 if (!mddev->external)
2136 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2138 spin_unlock_irq(&mddev->write_lock);
2139 wake_up(&mddev->sb_wait);
2142 sync_sbs(mddev, nospares);
2143 spin_unlock_irq(&mddev->write_lock);
2146 "md: updating %s RAID superblock on device (in sync %d)\n",
2147 mdname(mddev),mddev->in_sync);
2149 bitmap_update_sb(mddev->bitmap);
2150 list_for_each_entry(rdev, &mddev->disks, same_set) {
2151 char b[BDEVNAME_SIZE];
2152 dprintk(KERN_INFO "md: ");
2153 if (rdev->sb_loaded != 1)
2154 continue; /* no noise on spare devices */
2155 if (test_bit(Faulty, &rdev->flags))
2156 dprintk("(skipping faulty ");
2158 dprintk("%s ", bdevname(rdev->bdev,b));
2159 if (!test_bit(Faulty, &rdev->flags)) {
2160 md_super_write(mddev,rdev,
2161 rdev->sb_start, rdev->sb_size,
2163 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
2164 bdevname(rdev->bdev,b),
2165 (unsigned long long)rdev->sb_start);
2166 rdev->sb_events = mddev->events;
2170 if (mddev->level == LEVEL_MULTIPATH)
2171 /* only need to write one superblock... */
2174 md_super_wait(mddev);
2175 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
2177 spin_lock_irq(&mddev->write_lock);
2178 if (mddev->in_sync != sync_req ||
2179 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
2180 /* have to write it out again */
2181 spin_unlock_irq(&mddev->write_lock);
2184 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2185 spin_unlock_irq(&mddev->write_lock);
2186 wake_up(&mddev->sb_wait);
2187 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2188 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
2192 /* words written to sysfs files may, or may not, be \n terminated.
2193 * We want to accept with case. For this we use cmd_match.
2195 static int cmd_match(const char *cmd, const char *str)
2197 /* See if cmd, written into a sysfs file, matches
2198 * str. They must either be the same, or cmd can
2199 * have a trailing newline
2201 while (*cmd && *str && *cmd == *str) {
2212 struct rdev_sysfs_entry {
2213 struct attribute attr;
2214 ssize_t (*show)(mdk_rdev_t *, char *);
2215 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2219 state_show(mdk_rdev_t *rdev, char *page)
2224 if (test_bit(Faulty, &rdev->flags)) {
2225 len+= sprintf(page+len, "%sfaulty",sep);
2228 if (test_bit(In_sync, &rdev->flags)) {
2229 len += sprintf(page+len, "%sin_sync",sep);
2232 if (test_bit(WriteMostly, &rdev->flags)) {
2233 len += sprintf(page+len, "%swrite_mostly",sep);
2236 if (test_bit(Blocked, &rdev->flags)) {
2237 len += sprintf(page+len, "%sblocked", sep);
2240 if (!test_bit(Faulty, &rdev->flags) &&
2241 !test_bit(In_sync, &rdev->flags)) {
2242 len += sprintf(page+len, "%sspare", sep);
2245 return len+sprintf(page+len, "\n");
2249 state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2252 * faulty - simulates and error
2253 * remove - disconnects the device
2254 * writemostly - sets write_mostly
2255 * -writemostly - clears write_mostly
2256 * blocked - sets the Blocked flag
2257 * -blocked - clears the Blocked flag
2258 * insync - sets Insync providing device isn't active
2261 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2262 md_error(rdev->mddev, rdev);
2264 } else if (cmd_match(buf, "remove")) {
2265 if (rdev->raid_disk >= 0)
2268 mddev_t *mddev = rdev->mddev;
2269 kick_rdev_from_array(rdev);
2271 md_update_sb(mddev, 1);
2272 md_new_event(mddev);
2275 } else if (cmd_match(buf, "writemostly")) {
2276 set_bit(WriteMostly, &rdev->flags);
2278 } else if (cmd_match(buf, "-writemostly")) {
2279 clear_bit(WriteMostly, &rdev->flags);
2281 } else if (cmd_match(buf, "blocked")) {
2282 set_bit(Blocked, &rdev->flags);
2284 } else if (cmd_match(buf, "-blocked")) {
2285 clear_bit(Blocked, &rdev->flags);
2286 wake_up(&rdev->blocked_wait);
2287 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2288 md_wakeup_thread(rdev->mddev->thread);
2291 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2292 set_bit(In_sync, &rdev->flags);
2295 if (!err && rdev->sysfs_state)
2296 sysfs_notify_dirent(rdev->sysfs_state);
2297 return err ? err : len;
2299 static struct rdev_sysfs_entry rdev_state =
2300 __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
2303 errors_show(mdk_rdev_t *rdev, char *page)
2305 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2309 errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2312 unsigned long n = simple_strtoul(buf, &e, 10);
2313 if (*buf && (*e == 0 || *e == '\n')) {
2314 atomic_set(&rdev->corrected_errors, n);
2319 static struct rdev_sysfs_entry rdev_errors =
2320 __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
2323 slot_show(mdk_rdev_t *rdev, char *page)
2325 if (rdev->raid_disk < 0)
2326 return sprintf(page, "none\n");
2328 return sprintf(page, "%d\n", rdev->raid_disk);
2332 slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2337 int slot = simple_strtoul(buf, &e, 10);
2338 if (strncmp(buf, "none", 4)==0)
2340 else if (e==buf || (*e && *e!= '\n'))
2342 if (rdev->mddev->pers && slot == -1) {
2343 /* Setting 'slot' on an active array requires also
2344 * updating the 'rd%d' link, and communicating
2345 * with the personality with ->hot_*_disk.
2346 * For now we only support removing
2347 * failed/spare devices. This normally happens automatically,
2348 * but not when the metadata is externally managed.
2350 if (rdev->raid_disk == -1)
2352 /* personality does all needed checks */
2353 if (rdev->mddev->pers->hot_add_disk == NULL)
2355 err = rdev->mddev->pers->
2356 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2359 sprintf(nm, "rd%d", rdev->raid_disk);
2360 sysfs_remove_link(&rdev->mddev->kobj, nm);
2361 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2362 md_wakeup_thread(rdev->mddev->thread);
2363 } else if (rdev->mddev->pers) {
2365 /* Activating a spare .. or possibly reactivating
2366 * if we ever get bitmaps working here.
2369 if (rdev->raid_disk != -1)
2372 if (rdev->mddev->pers->hot_add_disk == NULL)
2375 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
2376 if (rdev2->raid_disk == slot)
2379 rdev->raid_disk = slot;
2380 if (test_bit(In_sync, &rdev->flags))
2381 rdev->saved_raid_disk = slot;
2383 rdev->saved_raid_disk = -1;
2384 err = rdev->mddev->pers->
2385 hot_add_disk(rdev->mddev, rdev);
2387 rdev->raid_disk = -1;
2390 sysfs_notify_dirent(rdev->sysfs_state);
2391 sprintf(nm, "rd%d", rdev->raid_disk);
2392 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2394 "md: cannot register "
2396 nm, mdname(rdev->mddev));
2398 /* don't wakeup anyone, leave that to userspace. */
2400 if (slot >= rdev->mddev->raid_disks)
2402 rdev->raid_disk = slot;
2403 /* assume it is working */
2404 clear_bit(Faulty, &rdev->flags);
2405 clear_bit(WriteMostly, &rdev->flags);
2406 set_bit(In_sync, &rdev->flags);
2407 sysfs_notify_dirent(rdev->sysfs_state);
2413 static struct rdev_sysfs_entry rdev_slot =
2414 __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
2417 offset_show(mdk_rdev_t *rdev, char *page)
2419 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
2423 offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2426 unsigned long long offset = simple_strtoull(buf, &e, 10);
2427 if (e==buf || (*e && *e != '\n'))
2429 if (rdev->mddev->pers && rdev->raid_disk >= 0)
2431 if (rdev->sectors && rdev->mddev->external)
2432 /* Must set offset before size, so overlap checks
2435 rdev->data_offset = offset;
2439 static struct rdev_sysfs_entry rdev_offset =
2440 __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
2443 rdev_size_show(mdk_rdev_t *rdev, char *page)
2445 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
2448 static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2450 /* check if two start/length pairs overlap */
2458 static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2460 unsigned long long blocks;
2463 if (strict_strtoull(buf, 10, &blocks) < 0)
2466 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2467 return -EINVAL; /* sector conversion overflow */
2470 if (new != blocks * 2)
2471 return -EINVAL; /* unsigned long long to sector_t overflow */
2478 rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2480 mddev_t *my_mddev = rdev->mddev;
2481 sector_t oldsectors = rdev->sectors;
2484 if (strict_blocks_to_sectors(buf, §ors) < 0)
2486 if (my_mddev->pers && rdev->raid_disk >= 0) {
2487 if (my_mddev->persistent) {
2488 sectors = super_types[my_mddev->major_version].
2489 rdev_size_change(rdev, sectors);
2492 } else if (!sectors)
2493 sectors = (rdev->bdev->bd_inode->i_size >> 9) -
2496 if (sectors < my_mddev->dev_sectors)
2497 return -EINVAL; /* component must fit device */
2499 rdev->sectors = sectors;
2500 if (sectors > oldsectors && my_mddev->external) {
2501 /* need to check that all other rdevs with the same ->bdev
2502 * do not overlap. We need to unlock the mddev to avoid
2503 * a deadlock. We have already changed rdev->sectors, and if
2504 * we have to change it back, we will have the lock again.
2508 struct list_head *tmp;
2510 mddev_unlock(my_mddev);
2511 for_each_mddev(mddev, tmp) {
2515 list_for_each_entry(rdev2, &mddev->disks, same_set)
2516 if (test_bit(AllReserved, &rdev2->flags) ||
2517 (rdev->bdev == rdev2->bdev &&
2519 overlaps(rdev->data_offset, rdev->sectors,
2525 mddev_unlock(mddev);
2531 mddev_lock(my_mddev);
2533 /* Someone else could have slipped in a size
2534 * change here, but doing so is just silly.
2535 * We put oldsectors back because we *know* it is
2536 * safe, and trust userspace not to race with
2539 rdev->sectors = oldsectors;
2546 static struct rdev_sysfs_entry rdev_size =
2547 __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
2550 static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page)
2552 unsigned long long recovery_start = rdev->recovery_offset;
2554 if (test_bit(In_sync, &rdev->flags) ||
2555 recovery_start == MaxSector)
2556 return sprintf(page, "none\n");
2558 return sprintf(page, "%llu\n", recovery_start);
2561 static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2563 unsigned long long recovery_start;
2565 if (cmd_match(buf, "none"))
2566 recovery_start = MaxSector;
2567 else if (strict_strtoull(buf, 10, &recovery_start))
2570 if (rdev->mddev->pers &&
2571 rdev->raid_disk >= 0)
2574 rdev->recovery_offset = recovery_start;
2575 if (recovery_start == MaxSector)
2576 set_bit(In_sync, &rdev->flags);
2578 clear_bit(In_sync, &rdev->flags);
2582 static struct rdev_sysfs_entry rdev_recovery_start =
2583 __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2585 static struct attribute *rdev_default_attrs[] = {
2591 &rdev_recovery_start.attr,
2595 rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2597 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2598 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
2599 mddev_t *mddev = rdev->mddev;
2605 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2607 if (rdev->mddev == NULL)
2610 rv = entry->show(rdev, page);
2611 mddev_unlock(mddev);
2617 rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2618 const char *page, size_t length)
2620 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2621 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
2623 mddev_t *mddev = rdev->mddev;
2627 if (!capable(CAP_SYS_ADMIN))
2629 rv = mddev ? mddev_lock(mddev): -EBUSY;
2631 if (rdev->mddev == NULL)
2634 rv = entry->store(rdev, page, length);
2635 mddev_unlock(mddev);
2640 static void rdev_free(struct kobject *ko)
2642 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2645 static struct sysfs_ops rdev_sysfs_ops = {
2646 .show = rdev_attr_show,
2647 .store = rdev_attr_store,
2649 static struct kobj_type rdev_ktype = {
2650 .release = rdev_free,
2651 .sysfs_ops = &rdev_sysfs_ops,
2652 .default_attrs = rdev_default_attrs,
2656 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2658 * mark the device faulty if:
2660 * - the device is nonexistent (zero size)
2661 * - the device has no valid superblock
2663 * a faulty rdev _never_ has rdev->sb set.
2665 static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2667 char b[BDEVNAME_SIZE];
2672 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
2674 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2675 return ERR_PTR(-ENOMEM);
2678 if ((err = alloc_disk_sb(rdev)))
2681 err = lock_rdev(rdev, newdev, super_format == -2);
2685 kobject_init(&rdev->kobj, &rdev_ktype);
2688 rdev->saved_raid_disk = -1;
2689 rdev->raid_disk = -1;
2691 rdev->data_offset = 0;
2692 rdev->sb_events = 0;
2693 rdev->last_read_error.tv_sec = 0;
2694 rdev->last_read_error.tv_nsec = 0;
2695 atomic_set(&rdev->nr_pending, 0);
2696 atomic_set(&rdev->read_errors, 0);
2697 atomic_set(&rdev->corrected_errors, 0);
2699 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2702 "md: %s has zero or unknown size, marking faulty!\n",
2703 bdevname(rdev->bdev,b));
2708 if (super_format >= 0) {
2709 err = super_types[super_format].
2710 load_super(rdev, NULL, super_minor);
2711 if (err == -EINVAL) {
2713 "md: %s does not have a valid v%d.%d "
2714 "superblock, not importing!\n",
2715 bdevname(rdev->bdev,b),
2716 super_format, super_minor);
2721 "md: could not read %s's sb, not importing!\n",
2722 bdevname(rdev->bdev,b));
2727 INIT_LIST_HEAD(&rdev->same_set);
2728 init_waitqueue_head(&rdev->blocked_wait);
2733 if (rdev->sb_page) {
2739 return ERR_PTR(err);
2743 * Check a full RAID array for plausibility
2747 static void analyze_sbs(mddev_t * mddev)
2750 mdk_rdev_t *rdev, *freshest, *tmp;
2751 char b[BDEVNAME_SIZE];
2754 rdev_for_each(rdev, tmp, mddev)
2755 switch (super_types[mddev->major_version].
2756 load_super(rdev, freshest, mddev->minor_version)) {
2764 "md: fatal superblock inconsistency in %s"
2765 " -- removing from array\n",
2766 bdevname(rdev->bdev,b));
2767 kick_rdev_from_array(rdev);
2771 super_types[mddev->major_version].
2772 validate_super(mddev, freshest);
2775 rdev_for_each(rdev, tmp, mddev) {
2776 if (rdev->desc_nr >= mddev->max_disks ||
2777 i > mddev->max_disks) {
2779 "md: %s: %s: only %d devices permitted\n",
2780 mdname(mddev), bdevname(rdev->bdev, b),
2782 kick_rdev_from_array(rdev);
2785 if (rdev != freshest)
2786 if (super_types[mddev->major_version].
2787 validate_super(mddev, rdev)) {
2788 printk(KERN_WARNING "md: kicking non-fresh %s"
2790 bdevname(rdev->bdev,b));
2791 kick_rdev_from_array(rdev);
2794 if (mddev->level == LEVEL_MULTIPATH) {
2795 rdev->desc_nr = i++;
2796 rdev->raid_disk = rdev->desc_nr;
2797 set_bit(In_sync, &rdev->flags);
2798 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
2799 rdev->raid_disk = -1;
2800 clear_bit(In_sync, &rdev->flags);
2805 /* Read a fixed-point number.
2806 * Numbers in sysfs attributes should be in "standard" units where
2807 * possible, so time should be in seconds.
2808 * However we internally use a a much smaller unit such as
2809 * milliseconds or jiffies.
2810 * This function takes a decimal number with a possible fractional
2811 * component, and produces an integer which is the result of
2812 * multiplying that number by 10^'scale'.
2813 * all without any floating-point arithmetic.
2815 int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
2817 unsigned long result = 0;
2819 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
2822 else if (decimals < scale) {
2825 result = result * 10 + value;
2837 while (decimals < scale) {
2846 static void md_safemode_timeout(unsigned long data);
2849 safe_delay_show(mddev_t *mddev, char *page)
2851 int msec = (mddev->safemode_delay*1000)/HZ;
2852 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2855 safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2859 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
2862 mddev->safemode_delay = 0;
2864 unsigned long old_delay = mddev->safemode_delay;
2865 mddev->safemode_delay = (msec*HZ)/1000;
2866 if (mddev->safemode_delay == 0)
2867 mddev->safemode_delay = 1;
2868 if (mddev->safemode_delay < old_delay)
2869 md_safemode_timeout((unsigned long)mddev);
2873 static struct md_sysfs_entry md_safe_delay =
2874 __ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
2877 level_show(mddev_t *mddev, char *page)
2879 struct mdk_personality *p = mddev->pers;
2881 return sprintf(page, "%s\n", p->name);
2882 else if (mddev->clevel[0])
2883 return sprintf(page, "%s\n", mddev->clevel);
2884 else if (mddev->level != LEVEL_NONE)
2885 return sprintf(page, "%d\n", mddev->level);
2891 level_store(mddev_t *mddev, const char *buf, size_t len)
2895 struct mdk_personality *pers;
2899 if (mddev->pers == NULL) {
2902 if (len >= sizeof(mddev->clevel))
2904 strncpy(mddev->clevel, buf, len);
2905 if (mddev->clevel[len-1] == '\n')
2907 mddev->clevel[len] = 0;
2908 mddev->level = LEVEL_NONE;
2912 /* request to change the personality. Need to ensure:
2913 * - array is not engaged in resync/recovery/reshape
2914 * - old personality can be suspended
2915 * - new personality will access other array.
2918 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
2921 if (!mddev->pers->quiesce) {
2922 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
2923 mdname(mddev), mddev->pers->name);
2927 /* Now find the new personality */
2928 if (len == 0 || len >= sizeof(level))
2930 strncpy(level, buf, len);
2931 if (level[len-1] == '\n')
2935 request_module("md-%s", level);
2936 spin_lock(&pers_lock);
2937 pers = find_pers(LEVEL_NONE, level);
2938 if (!pers || !try_module_get(pers->owner)) {
2939 spin_unlock(&pers_lock);
2940 printk(KERN_WARNING "md: personality %s not loaded\n", level);
2943 spin_unlock(&pers_lock);
2945 if (pers == mddev->pers) {
2946 /* Nothing to do! */
2947 module_put(pers->owner);
2950 if (!pers->takeover) {
2951 module_put(pers->owner);
2952 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
2953 mdname(mddev), level);
2957 /* ->takeover must set new_* and/or delta_disks
2958 * if it succeeds, and may set them when it fails.
2960 priv = pers->takeover(mddev);
2962 mddev->new_level = mddev->level;
2963 mddev->new_layout = mddev->layout;
2964 mddev->new_chunk_sectors = mddev->chunk_sectors;
2965 mddev->raid_disks -= mddev->delta_disks;
2966 mddev->delta_disks = 0;
2967 module_put(pers->owner);
2968 printk(KERN_WARNING "md: %s: %s would not accept array\n",
2969 mdname(mddev), level);
2970 return PTR_ERR(priv);
2973 /* Looks like we have a winner */
2974 mddev_suspend(mddev);
2975 mddev->pers->stop(mddev);
2976 module_put(mddev->pers->owner);
2977 /* Invalidate devices that are now superfluous */
2978 list_for_each_entry(rdev, &mddev->disks, same_set)
2979 if (rdev->raid_disk >= mddev->raid_disks) {
2980 rdev->raid_disk = -1;
2981 clear_bit(In_sync, &rdev->flags);
2984 mddev->private = priv;
2985 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
2986 mddev->level = mddev->new_level;
2987 mddev->layout = mddev->new_layout;
2988 mddev->chunk_sectors = mddev->new_chunk_sectors;
2989 mddev->delta_disks = 0;
2991 mddev_resume(mddev);
2992 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2993 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2994 md_wakeup_thread(mddev->thread);
2998 static struct md_sysfs_entry md_level =
2999 __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
3003 layout_show(mddev_t *mddev, char *page)
3005 /* just a number, not meaningful for all levels */
3006 if (mddev->reshape_position != MaxSector &&
3007 mddev->layout != mddev->new_layout)
3008 return sprintf(page, "%d (%d)\n",
3009 mddev->new_layout, mddev->layout);
3010 return sprintf(page, "%d\n", mddev->layout);
3014 layout_store(mddev_t *mddev, const char *buf, size_t len)
3017 unsigned long n = simple_strtoul(buf, &e, 10);
3019 if (!*buf || (*e && *e != '\n'))
3024 if (mddev->pers->check_reshape == NULL)
3026 mddev->new_layout = n;
3027 err = mddev->pers->check_reshape(mddev);
3029 mddev->new_layout = mddev->layout;
3033 mddev->new_layout = n;
3034 if (mddev->reshape_position == MaxSector)
3039 static struct md_sysfs_entry md_layout =
3040 __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
3044 raid_disks_show(mddev_t *mddev, char *page)
3046 if (mddev->raid_disks == 0)
3048 if (mddev->reshape_position != MaxSector &&
3049 mddev->delta_disks != 0)
3050 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3051 mddev->raid_disks - mddev->delta_disks);
3052 return sprintf(page, "%d\n", mddev->raid_disks);
3055 static int update_raid_disks(mddev_t *mddev, int raid_disks);
3058 raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
3062 unsigned long n = simple_strtoul(buf, &e, 10);
3064 if (!*buf || (*e && *e != '\n'))
3068 rv = update_raid_disks(mddev, n);
3069 else if (mddev->reshape_position != MaxSector) {
3070 int olddisks = mddev->raid_disks - mddev->delta_disks;
3071 mddev->delta_disks = n - olddisks;
3072 mddev->raid_disks = n;
3074 mddev->raid_disks = n;
3075 return rv ? rv : len;
3077 static struct md_sysfs_entry md_raid_disks =
3078 __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
3081 chunk_size_show(mddev_t *mddev, char *page)
3083 if (mddev->reshape_position != MaxSector &&
3084 mddev->chunk_sectors != mddev->new_chunk_sectors)
3085 return sprintf(page, "%d (%d)\n",
3086 mddev->new_chunk_sectors << 9,
3087 mddev->chunk_sectors << 9);
3088 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
3092 chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
3095 unsigned long n = simple_strtoul(buf, &e, 10);
3097 if (!*buf || (*e && *e != '\n'))
3102 if (mddev->pers->check_reshape == NULL)
3104 mddev->new_chunk_sectors = n >> 9;
3105 err = mddev->pers->check_reshape(mddev);
3107 mddev->new_chunk_sectors = mddev->chunk_sectors;
3111 mddev->new_chunk_sectors = n >> 9;
3112 if (mddev->reshape_position == MaxSector)
3113 mddev->chunk_sectors = n >> 9;
3117 static struct md_sysfs_entry md_chunk_size =
3118 __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
3121 resync_start_show(mddev_t *mddev, char *page)
3123 if (mddev->recovery_cp == MaxSector)
3124 return sprintf(page, "none\n");
3125 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3129 resync_start_store(mddev_t *mddev, const char *buf, size_t len)
3132 unsigned long long n = simple_strtoull(buf, &e, 10);
3136 if (cmd_match(buf, "none"))
3138 else if (!*buf || (*e && *e != '\n'))
3141 mddev->recovery_cp = n;
3144 static struct md_sysfs_entry md_resync_start =
3145 __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
3148 * The array state can be:
3151 * No devices, no size, no level
3152 * Equivalent to STOP_ARRAY ioctl
3154 * May have some settings, but array is not active
3155 * all IO results in error
3156 * When written, doesn't tear down array, but just stops it
3157 * suspended (not supported yet)
3158 * All IO requests will block. The array can be reconfigured.
3159 * Writing this, if accepted, will block until array is quiescent
3161 * no resync can happen. no superblocks get written.
3162 * write requests fail
3164 * like readonly, but behaves like 'clean' on a write request.
3166 * clean - no pending writes, but otherwise active.
3167 * When written to inactive array, starts without resync
3168 * If a write request arrives then
3169 * if metadata is known, mark 'dirty' and switch to 'active'.
3170 * if not known, block and switch to write-pending
3171 * If written to an active array that has pending writes, then fails.
3173 * fully active: IO and resync can be happening.
3174 * When written to inactive array, starts with resync
3177 * clean, but writes are blocked waiting for 'active' to be written.
3180 * like active, but no writes have been seen for a while (100msec).
3183 enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3184 write_pending, active_idle, bad_word};
3185 static char *array_states[] = {
3186 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3187 "write-pending", "active-idle", NULL };
3189 static int match_word(const char *word, char **list)
3192 for (n=0; list[n]; n++)
3193 if (cmd_match(word, list[n]))
3199 array_state_show(mddev_t *mddev, char *page)
3201 enum array_state st = inactive;
3214 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
3216 else if (mddev->safemode)
3222 if (list_empty(&mddev->disks) &&
3223 mddev->raid_disks == 0 &&
3224 mddev->dev_sectors == 0)
3229 return sprintf(page, "%s\n", array_states[st]);
3232 static int do_md_stop(mddev_t * mddev, int ro, int is_open);
3233 static int do_md_run(mddev_t * mddev);
3234 static int restart_array(mddev_t *mddev);
3237 array_state_store(mddev_t *mddev, const char *buf, size_t len)
3240 enum array_state st = match_word(buf, array_states);
3245 /* stopping an active array */
3246 if (atomic_read(&mddev->openers) > 0)
3248 err = do_md_stop(mddev, 0, 0);
3251 /* stopping an active array */
3253 if (atomic_read(&mddev->openers) > 0)
3255 err = do_md_stop(mddev, 2, 0);
3257 err = 0; /* already inactive */
3260 break; /* not supported yet */
3263 err = do_md_stop(mddev, 1, 0);
3266 set_disk_ro(mddev->gendisk, 1);
3267 err = do_md_run(mddev);
3273 err = do_md_stop(mddev, 1, 0);
3274 else if (mddev->ro == 1)
3275 err = restart_array(mddev);
3278 set_disk_ro(mddev->gendisk, 0);
3282 err = do_md_run(mddev);
3287 restart_array(mddev);
3288 spin_lock_irq(&mddev->write_lock);
3289 if (atomic_read(&mddev->writes_pending) == 0) {
3290 if (mddev->in_sync == 0) {
3292 if (mddev->safemode == 1)
3293 mddev->safemode = 0;
3294 if (mddev->persistent)
3295 set_bit(MD_CHANGE_CLEAN,
3301 spin_unlock_irq(&mddev->write_lock);
3307 restart_array(mddev);
3308 if (mddev->external)
3309 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
3310 wake_up(&mddev->sb_wait);
3314 set_disk_ro(mddev->gendisk, 0);
3315 err = do_md_run(mddev);
3320 /* these cannot be set */
3326 sysfs_notify_dirent(mddev->sysfs_state);
3330 static struct md_sysfs_entry md_array_state =
3331 __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
3334 max_corrected_read_errors_show(mddev_t *mddev, char *page) {
3335 return sprintf(page, "%d\n",
3336 atomic_read(&mddev->max_corr_read_errors));
3340 max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len)
3343 unsigned long n = simple_strtoul(buf, &e, 10);
3345 if (*buf && (*e == 0 || *e == '\n')) {
3346 atomic_set(&mddev->max_corr_read_errors, n);
3352 static struct md_sysfs_entry max_corr_read_errors =
3353 __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3354 max_corrected_read_errors_store);
3357 null_show(mddev_t *mddev, char *page)
3363 new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3365 /* buf must be %d:%d\n? giving major and minor numbers */
3366 /* The new device is added to the array.
3367 * If the array has a persistent superblock, we read the
3368 * superblock to initialise info and check validity.
3369 * Otherwise, only checking done is that in bind_rdev_to_array,
3370 * which mainly checks size.
3373 int major = simple_strtoul(buf, &e, 10);
3379 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3381 minor = simple_strtoul(e+1, &e, 10);
3382 if (*e && *e != '\n')
3384 dev = MKDEV(major, minor);
3385 if (major != MAJOR(dev) ||
3386 minor != MINOR(dev))
3390 if (mddev->persistent) {
3391 rdev = md_import_device(dev, mddev->major_version,
3392 mddev->minor_version);
3393 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3394 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3395 mdk_rdev_t, same_set);
3396 err = super_types[mddev->major_version]
3397 .load_super(rdev, rdev0, mddev->minor_version);
3401 } else if (mddev->external)
3402 rdev = md_import_device(dev, -2, -1);
3404 rdev = md_import_device(dev, -1, -1);
3407 return PTR_ERR(rdev);
3408 err = bind_rdev_to_array(rdev, mddev);
3412 return err ? err : len;
3415 static struct md_sysfs_entry md_new_device =
3416 __ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
3419 bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3422 unsigned long chunk, end_chunk;
3426 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3428 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3429 if (buf == end) break;
3430 if (*end == '-') { /* range */
3432 end_chunk = simple_strtoul(buf, &end, 0);
3433 if (buf == end) break;
3435 if (*end && !isspace(*end)) break;
3436 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
3437 buf = skip_spaces(end);
3439 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3444 static struct md_sysfs_entry md_bitmap =
3445 __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3448 size_show(mddev_t *mddev, char *page)
3450 return sprintf(page, "%llu\n",
3451 (unsigned long long)mddev->dev_sectors / 2);
3454 static int update_size(mddev_t *mddev, sector_t num_sectors);
3457 size_store(mddev_t *mddev, const char *buf, size_t len)
3459 /* If array is inactive, we can reduce the component size, but
3460 * not increase it (except from 0).
3461 * If array is active, we can try an on-line resize
3464 int err = strict_blocks_to_sectors(buf, §ors);
3469 err = update_size(mddev, sectors);
3470 md_update_sb(mddev, 1);
3472 if (mddev->dev_sectors == 0 ||
3473 mddev->dev_sectors > sectors)
3474 mddev->dev_sectors = sectors;
3478 return err ? err : len;
3481 static struct md_sysfs_entry md_size =
3482 __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
3487 * 'none' for arrays with no metadata (good luck...)
3488 * 'external' for arrays with externally managed metadata,
3489 * or N.M for internally known formats
3492 metadata_show(mddev_t *mddev, char *page)
3494 if (mddev->persistent)
3495 return sprintf(page, "%d.%d\n",
3496 mddev->major_version, mddev->minor_version);
3497 else if (mddev->external)
3498 return sprintf(page, "external:%s\n", mddev->metadata_type);
3500 return sprintf(page, "none\n");
3504 metadata_store(mddev_t *mddev, const char *buf, size_t len)
3508 /* Changing the details of 'external' metadata is
3509 * always permitted. Otherwise there must be
3510 * no devices attached to the array.
3512 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3514 else if (!list_empty(&mddev->disks))
3517 if (cmd_match(buf, "none")) {
3518 mddev->persistent = 0;
3519 mddev->external = 0;
3520 mddev->major_version = 0;
3521 mddev->minor_version = 90;
3524 if (strncmp(buf, "external:", 9) == 0) {
3525 size_t namelen = len-9;
3526 if (namelen >= sizeof(mddev->metadata_type))
3527 namelen = sizeof(mddev->metadata_type)-1;
3528 strncpy(mddev->metadata_type, buf+9, namelen);
3529 mddev->metadata_type[namelen] = 0;
3530 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3531 mddev->metadata_type[--namelen] = 0;
3532 mddev->persistent = 0;
3533 mddev->external = 1;
3534 mddev->major_version = 0;
3535 mddev->minor_version = 90;
3538 major = simple_strtoul(buf, &e, 10);
3539 if (e==buf || *e != '.')
3542 minor = simple_strtoul(buf, &e, 10);
3543 if (e==buf || (*e && *e != '\n') )
3545 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
3547 mddev->major_version = major;
3548 mddev->minor_version = minor;
3549 mddev->persistent = 1;
3550 mddev->external = 0;
3554 static struct md_sysfs_entry md_metadata =
3555 __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
3558 action_show(mddev_t *mddev, char *page)
3560 char *type = "idle";
3561 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3563 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3564 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
3565 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3567 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3568 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3570 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3574 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
3577 return sprintf(page, "%s\n", type);
3581 action_store(mddev_t *mddev, const char *page, size_t len)
3583 if (!mddev->pers || !mddev->pers->sync_request)
3586 if (cmd_match(page, "frozen"))
3587 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3589 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3591 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
3592 if (mddev->sync_thread) {
3593 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3594 md_unregister_thread(mddev->sync_thread);
3595 mddev->sync_thread = NULL;
3596 mddev->recovery = 0;
3598 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3599 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
3601 else if (cmd_match(page, "resync"))
3602 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3603 else if (cmd_match(page, "recover")) {
3604 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3605 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3606 } else if (cmd_match(page, "reshape")) {
3608 if (mddev->pers->start_reshape == NULL)
3610 err = mddev->pers->start_reshape(mddev);
3613 sysfs_notify(&mddev->kobj, NULL, "degraded");
3615 if (cmd_match(page, "check"))
3616 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3617 else if (!cmd_match(page, "repair"))
3619 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3620 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3622 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3623 md_wakeup_thread(mddev->thread);
3624 sysfs_notify_dirent(mddev->sysfs_action);
3629 mismatch_cnt_show(mddev_t *mddev, char *page)
3631 return sprintf(page, "%llu\n",
3632 (unsigned long long) mddev->resync_mismatches);
3635 static struct md_sysfs_entry md_scan_mode =
3636 __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
3639 static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
3642 sync_min_show(mddev_t *mddev, char *page)
3644 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3645 mddev->sync_speed_min ? "local": "system");
3649 sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3653 if (strncmp(buf, "system", 6)==0) {
3654 mddev->sync_speed_min = 0;
3657 min = simple_strtoul(buf, &e, 10);
3658 if (buf == e || (*e && *e != '\n') || min <= 0)
3660 mddev->sync_speed_min = min;
3664 static struct md_sysfs_entry md_sync_min =
3665 __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3668 sync_max_show(mddev_t *mddev, char *page)
3670 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3671 mddev->sync_speed_max ? "local": "system");
3675 sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3679 if (strncmp(buf, "system", 6)==0) {
3680 mddev->sync_speed_max = 0;
3683 max = simple_strtoul(buf, &e, 10);
3684 if (buf == e || (*e && *e != '\n') || max <= 0)
3686 mddev->sync_speed_max = max;
3690 static struct md_sysfs_entry md_sync_max =
3691 __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3694 degraded_show(mddev_t *mddev, char *page)
3696 return sprintf(page, "%d\n", mddev->degraded);
3698 static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
3701 sync_force_parallel_show(mddev_t *mddev, char *page)
3703 return sprintf(page, "%d\n", mddev->parallel_resync);
3707 sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3711 if (strict_strtol(buf, 10, &n))
3714 if (n != 0 && n != 1)
3717 mddev->parallel_resync = n;
3719 if (mddev->sync_thread)
3720 wake_up(&resync_wait);
3725 /* force parallel resync, even with shared block devices */
3726 static struct md_sysfs_entry md_sync_force_parallel =
3727 __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3728 sync_force_parallel_show, sync_force_parallel_store);
3731 sync_speed_show(mddev_t *mddev, char *page)
3733 unsigned long resync, dt, db;
3734 if (mddev->curr_resync == 0)
3735 return sprintf(page, "none\n");
3736 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3737 dt = (jiffies - mddev->resync_mark) / HZ;
3739 db = resync - mddev->resync_mark_cnt;
3740 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
3743 static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
3746 sync_completed_show(mddev_t *mddev, char *page)
3748 unsigned long max_sectors, resync;
3750 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3751 return sprintf(page, "none\n");
3753 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3754 max_sectors = mddev->resync_max_sectors;
3756 max_sectors = mddev->dev_sectors;
3758 resync = mddev->curr_resync_completed;
3759 return sprintf(page, "%lu / %lu\n", resync, max_sectors);
3762 static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
3765 min_sync_show(mddev_t *mddev, char *page)
3767 return sprintf(page, "%llu\n",
3768 (unsigned long long)mddev->resync_min);
3771 min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3773 unsigned long long min;
3774 if (strict_strtoull(buf, 10, &min))
3776 if (min > mddev->resync_max)
3778 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3781 /* Must be a multiple of chunk_size */
3782 if (mddev->chunk_sectors) {
3783 sector_t temp = min;
3784 if (sector_div(temp, mddev->chunk_sectors))
3787 mddev->resync_min = min;
3792 static struct md_sysfs_entry md_min_sync =
3793 __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3796 max_sync_show(mddev_t *mddev, char *page)
3798 if (mddev->resync_max == MaxSector)
3799 return sprintf(page, "max\n");
3801 return sprintf(page, "%llu\n",
3802 (unsigned long long)mddev->resync_max);
3805 max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3807 if (strncmp(buf, "max", 3) == 0)
3808 mddev->resync_max = MaxSector;
3810 unsigned long long max;
3811 if (strict_strtoull(buf, 10, &max))
3813 if (max < mddev->resync_min)
3815 if (max < mddev->resync_max &&
3817 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3820 /* Must be a multiple of chunk_size */
3821 if (mddev->chunk_sectors) {
3822 sector_t temp = max;
3823 if (sector_div(temp, mddev->chunk_sectors))
3826 mddev->resync_max = max;
3828 wake_up(&mddev->recovery_wait);
3832 static struct md_sysfs_entry md_max_sync =
3833 __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3836 suspend_lo_show(mddev_t *mddev, char *page)
3838 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3842 suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3845 unsigned long long new = simple_strtoull(buf, &e, 10);
3847 if (mddev->pers == NULL ||
3848 mddev->pers->quiesce == NULL)
3850 if (buf == e || (*e && *e != '\n'))
3852 if (new >= mddev->suspend_hi ||
3853 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3854 mddev->suspend_lo = new;
3855 mddev->pers->quiesce(mddev, 2);
3860 static struct md_sysfs_entry md_suspend_lo =
3861 __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3865 suspend_hi_show(mddev_t *mddev, char *page)
3867 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3871 suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3874 unsigned long long new = simple_strtoull(buf, &e, 10);
3876 if (mddev->pers == NULL ||
3877 mddev->pers->quiesce == NULL)
3879 if (buf == e || (*e && *e != '\n'))
3881 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3882 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3883 mddev->suspend_hi = new;
3884 mddev->pers->quiesce(mddev, 1);
3885 mddev->pers->quiesce(mddev, 0);
3890 static struct md_sysfs_entry md_suspend_hi =
3891 __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3894 reshape_position_show(mddev_t *mddev, char *page)
3896 if (mddev->reshape_position != MaxSector)
3897 return sprintf(page, "%llu\n",
3898 (unsigned long long)mddev->reshape_position);
3899 strcpy(page, "none\n");
3904 reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3907 unsigned long long new = simple_strtoull(buf, &e, 10);
3910 if (buf == e || (*e && *e != '\n'))
3912 mddev->reshape_position = new;
3913 mddev->delta_disks = 0;
3914 mddev->new_level = mddev->level;
3915 mddev->new_layout = mddev->layout;
3916 mddev->new_chunk_sectors = mddev->chunk_sectors;
3920 static struct md_sysfs_entry md_reshape_position =
3921 __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3922 reshape_position_store);
3925 array_size_show(mddev_t *mddev, char *page)
3927 if (mddev->external_size)
3928 return sprintf(page, "%llu\n",
3929 (unsigned long long)mddev->array_sectors/2);
3931 return sprintf(page, "default\n");
3935 array_size_store(mddev_t *mddev, const char *buf, size_t len)
3939 if (strncmp(buf, "default", 7) == 0) {
3941 sectors = mddev->pers->size(mddev, 0, 0);
3943 sectors = mddev->array_sectors;
3945 mddev->external_size = 0;
3947 if (strict_blocks_to_sectors(buf, §ors) < 0)
3949 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
3952 mddev->external_size = 1;
3955 mddev->array_sectors = sectors;
3956 set_capacity(mddev->gendisk, mddev->array_sectors);
3958 revalidate_disk(mddev->gendisk);
3963 static struct md_sysfs_entry md_array_size =
3964 __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
3967 static struct attribute *md_default_attrs[] = {
3970 &md_raid_disks.attr,
3971 &md_chunk_size.attr,
3973 &md_resync_start.attr,
3975 &md_new_device.attr,
3976 &md_safe_delay.attr,
3977 &md_array_state.attr,
3978 &md_reshape_position.attr,
3979 &md_array_size.attr,
3980 &max_corr_read_errors.attr,
3984 static struct attribute *md_redundancy_attrs[] = {
3986 &md_mismatches.attr,
3989 &md_sync_speed.attr,
3990 &md_sync_force_parallel.attr,
3991 &md_sync_completed.attr,
3994 &md_suspend_lo.attr,
3995 &md_suspend_hi.attr,
4000 static struct attribute_group md_redundancy_group = {
4002 .attrs = md_redundancy_attrs,
4007 md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4009 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4010 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
4015 rv = mddev_lock(mddev);
4017 rv = entry->show(mddev, page);
4018 mddev_unlock(mddev);
4024 md_attr_store(struct kobject *kobj, struct attribute *attr,
4025 const char *page, size_t length)
4027 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4028 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
4033 if (!capable(CAP_SYS_ADMIN))
4035 rv = mddev_lock(mddev);
4036 if (mddev->hold_active == UNTIL_IOCTL)
4037 mddev->hold_active = 0;
4039 rv = entry->store(mddev, page, length);
4040 mddev_unlock(mddev);
4045 static void md_free(struct kobject *ko)
4047 mddev_t *mddev = container_of(ko, mddev_t, kobj);
4049 if (mddev->sysfs_state)
4050 sysfs_put(mddev->sysfs_state);
4052 if (mddev->gendisk) {
4053 del_gendisk(mddev->gendisk);
4054 put_disk(mddev->gendisk);
4057 blk_cleanup_queue(mddev->queue);
4062 static struct sysfs_ops md_sysfs_ops = {
4063 .show = md_attr_show,
4064 .store = md_attr_store,
4066 static struct kobj_type md_ktype = {
4068 .sysfs_ops = &md_sysfs_ops,
4069 .default_attrs = md_default_attrs,
4074 static void mddev_delayed_delete(struct work_struct *ws)
4076 mddev_t *mddev = container_of(ws, mddev_t, del_work);
4078 if (mddev->private == &md_redundancy_group) {
4079 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
4080 if (mddev->sysfs_action)
4081 sysfs_put(mddev->sysfs_action);
4082 mddev->sysfs_action = NULL;
4083 mddev->private = NULL;
4085 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
4086 kobject_del(&mddev->kobj);
4087 kobject_put(&mddev->kobj);
4090 static int md_alloc(dev_t dev, char *name)
4092 static DEFINE_MUTEX(disks_mutex);
4093 mddev_t *mddev = mddev_find(dev);
4094 struct gendisk *disk;
4103 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4104 shift = partitioned ? MdpMinorShift : 0;
4105 unit = MINOR(mddev->unit) >> shift;
4107 /* wait for any previous instance if this device
4108 * to be completed removed (mddev_delayed_delete).
4110 flush_scheduled_work();
4112 mutex_lock(&disks_mutex);
4118 /* Need to ensure that 'name' is not a duplicate.
4121 spin_lock(&all_mddevs_lock);
4123 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4124 if (mddev2->gendisk &&
4125 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4126 spin_unlock(&all_mddevs_lock);
4129 spin_unlock(&all_mddevs_lock);
4133 mddev->queue = blk_alloc_queue(GFP_KERNEL);
4136 mddev->queue->queuedata = mddev;
4138 /* Can be unlocked because the queue is new: no concurrency */
4139 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue);
4141 blk_queue_make_request(mddev->queue, md_make_request);
4143 disk = alloc_disk(1 << shift);
4145 blk_cleanup_queue(mddev->queue);
4146 mddev->queue = NULL;
4149 disk->major = MAJOR(mddev->unit);
4150 disk->first_minor = unit << shift;
4152 strcpy(disk->disk_name, name);
4153 else if (partitioned)
4154 sprintf(disk->disk_name, "md_d%d", unit);
4156 sprintf(disk->disk_name, "md%d", unit);
4157 disk->fops = &md_fops;
4158 disk->private_data = mddev;
4159 disk->queue = mddev->queue;
4160 /* Allow extended partitions. This makes the
4161 * 'mdp' device redundant, but we can't really
4164 disk->flags |= GENHD_FL_EXT_DEVT;
4166 mddev->gendisk = disk;
4167 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4168 &disk_to_dev(disk)->kobj, "%s", "md");
4170 /* This isn't possible, but as kobject_init_and_add is marked
4171 * __must_check, we must do something with the result
4173 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4177 if (sysfs_create_group(&mddev->kobj, &md_bitmap_group))
4178 printk(KERN_DEBUG "pointless warning\n");
4180 mutex_unlock(&disks_mutex);
4182 kobject_uevent(&mddev->kobj, KOBJ_ADD);
4183 mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state");
4189 static struct kobject *md_probe(dev_t dev, int *part, void *data)
4191 md_alloc(dev, NULL);
4195 static int add_named_array(const char *val, struct kernel_param *kp)
4197 /* val must be "md_*" where * is not all digits.
4198 * We allocate an array with a large free minor number, and
4199 * set the name to val. val must not already be an active name.
4201 int len = strlen(val);
4202 char buf[DISK_NAME_LEN];
4204 while (len && val[len-1] == '\n')
4206 if (len >= DISK_NAME_LEN)
4208 strlcpy(buf, val, len+1);
4209 if (strncmp(buf, "md_", 3) != 0)
4211 return md_alloc(0, buf);
4214 static void md_safemode_timeout(unsigned long data)
4216 mddev_t *mddev = (mddev_t *) data;
4218 if (!atomic_read(&mddev->writes_pending)) {
4219 mddev->safemode = 1;
4220 if (mddev->external)
4221 sysfs_notify_dirent(mddev->sysfs_state);
4223 md_wakeup_thread(mddev->thread);
4226 static int start_dirty_degraded;
4228 static int do_md_run(mddev_t * mddev)
4232 struct gendisk *disk;
4233 struct mdk_personality *pers;
4235 if (list_empty(&mddev->disks))
4236 /* cannot run an array with no devices.. */
4243 * Analyze all RAID superblock(s)
4245 if (!mddev->raid_disks) {
4246 if (!mddev->persistent)
4251 if (mddev->level != LEVEL_NONE)
4252 request_module("md-level-%d", mddev->level);
4253 else if (mddev->clevel[0])
4254 request_module("md-%s", mddev->clevel);
4257 * Drop all container device buffers, from now on
4258 * the only valid external interface is through the md
4261 list_for_each_entry(rdev, &mddev->disks, same_set) {
4262 if (test_bit(Faulty, &rdev->flags))
4264 sync_blockdev(rdev->bdev);
4265 invalidate_bdev(rdev->bdev);
4267 /* perform some consistency tests on the device.
4268 * We don't want the data to overlap the metadata,
4269 * Internal Bitmap issues have been handled elsewhere.
4271 if (rdev->data_offset < rdev->sb_start) {
4272 if (mddev->dev_sectors &&
4273 rdev->data_offset + mddev->dev_sectors
4275 printk("md: %s: data overlaps metadata\n",
4280 if (rdev->sb_start + rdev->sb_size/512
4281 > rdev->data_offset) {
4282 printk("md: %s: metadata overlaps data\n",
4287 sysfs_notify_dirent(rdev->sysfs_state);
4290 md_probe(mddev->unit, NULL, NULL);
4291 disk = mddev->gendisk;
4295 spin_lock(&pers_lock);
4296 pers = find_pers(mddev->level, mddev->clevel);
4297 if (!pers || !try_module_get(pers->owner)) {
4298 spin_unlock(&pers_lock);
4299 if (mddev->level != LEVEL_NONE)
4300 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4303 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4308 spin_unlock(&pers_lock);
4309 if (mddev->level != pers->level) {
4310 mddev->level = pers->level;
4311 mddev->new_level = pers->level;
4313 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
4315 if (mddev->reshape_position != MaxSector &&
4316 pers->start_reshape == NULL) {
4317 /* This personality cannot handle reshaping... */
4319 module_put(pers->owner);
4323 if (pers->sync_request) {
4324 /* Warn if this is a potentially silly
4327 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4331 list_for_each_entry(rdev, &mddev->disks, same_set)
4332 list_for_each_entry(rdev2, &mddev->disks, same_set) {
4334 rdev->bdev->bd_contains ==
4335 rdev2->bdev->bd_contains) {
4337 "%s: WARNING: %s appears to be"
4338 " on the same physical disk as"
4341 bdevname(rdev->bdev,b),
4342 bdevname(rdev2->bdev,b2));
4349 "True protection against single-disk"
4350 " failure might be compromised.\n");
4353 mddev->recovery = 0;
4354 /* may be over-ridden by personality */
4355 mddev->resync_max_sectors = mddev->dev_sectors;
4357 mddev->barriers_work = 1;
4358 mddev->ok_start_degraded = start_dirty_degraded;
4360 if (start_readonly && mddev->ro == 0)
4361 mddev->ro = 2; /* read-only, but switch on first write */
4363 err = mddev->pers->run(mddev);
4365 printk(KERN_ERR "md: pers->run() failed ...\n");
4366 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4367 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4368 " but 'external_size' not in effect?\n", __func__);
4370 "md: invalid array_size %llu > default size %llu\n",
4371 (unsigned long long)mddev->array_sectors / 2,
4372 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4374 mddev->pers->stop(mddev);
4376 if (err == 0 && mddev->pers->sync_request) {
4377 err = bitmap_create(mddev);
4379 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4380 mdname(mddev), err);
4381 mddev->pers->stop(mddev);
4385 module_put(mddev->pers->owner);
4387 bitmap_destroy(mddev);
4390 if (mddev->pers->sync_request) {
4391 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
4393 "md: cannot register extra attributes for %s\n",
4395 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
4396 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
4399 atomic_set(&mddev->writes_pending,0);
4400 atomic_set(&mddev->max_corr_read_errors,
4401 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
4402 mddev->safemode = 0;
4403 mddev->safemode_timer.function = md_safemode_timeout;
4404 mddev->safemode_timer.data = (unsigned long) mddev;
4405 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
4408 list_for_each_entry(rdev, &mddev->disks, same_set)
4409 if (rdev->raid_disk >= 0) {
4411 sprintf(nm, "rd%d", rdev->raid_disk);
4412 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
4413 printk("md: cannot register %s for %s\n",
4417 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4420 md_update_sb(mddev, 0);
4422 set_capacity(disk, mddev->array_sectors);
4424 md_wakeup_thread(mddev->thread);
4425 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
4427 revalidate_disk(mddev->gendisk);
4429 md_new_event(mddev);
4430 sysfs_notify_dirent(mddev->sysfs_state);
4431 if (mddev->sysfs_action)
4432 sysfs_notify_dirent(mddev->sysfs_action);
4433 sysfs_notify(&mddev->kobj, NULL, "degraded");
4434 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4438 static int restart_array(mddev_t *mddev)
4440 struct gendisk *disk = mddev->gendisk;
4442 /* Complain if it has no devices */
4443 if (list_empty(&mddev->disks))
4449 mddev->safemode = 0;
4451 set_disk_ro(disk, 0);
4452 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4454 /* Kick recovery or resync if necessary */
4455 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4456 md_wakeup_thread(mddev->thread);
4457 md_wakeup_thread(mddev->sync_thread);
4458 sysfs_notify_dirent(mddev->sysfs_state);
4462 /* similar to deny_write_access, but accounts for our holding a reference
4463 * to the file ourselves */
4464 static int deny_bitmap_write_access(struct file * file)
4466 struct inode *inode = file->f_mapping->host;
4468 spin_lock(&inode->i_lock);
4469 if (atomic_read(&inode->i_writecount) > 1) {
4470 spin_unlock(&inode->i_lock);
4473 atomic_set(&inode->i_writecount, -1);
4474 spin_unlock(&inode->i_lock);
4479 void restore_bitmap_write_access(struct file *file)
4481 struct inode *inode = file->f_mapping->host;
4483 spin_lock(&inode->i_lock);
4484 atomic_set(&inode->i_writecount, 1);
4485 spin_unlock(&inode->i_lock);
4489 * 0 - completely stop and dis-assemble array
4490 * 1 - switch to readonly
4491 * 2 - stop but do not disassemble array
4493 static int do_md_stop(mddev_t * mddev, int mode, int is_open)
4496 struct gendisk *disk = mddev->gendisk;
4499 mutex_lock(&mddev->open_mutex);
4500 if (atomic_read(&mddev->openers) > is_open) {
4501 printk("md: %s still in use.\n",mdname(mddev));
4503 } else if (mddev->pers) {
4505 if (mddev->sync_thread) {
4506 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4507 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4508 md_unregister_thread(mddev->sync_thread);
4509 mddev->sync_thread = NULL;
4512 del_timer_sync(&mddev->safemode_timer);
4515 case 1: /* readonly */
4521 case 0: /* disassemble */
4523 bitmap_flush(mddev);
4524 md_super_wait(mddev);
4526 set_disk_ro(disk, 0);
4528 mddev->pers->stop(mddev);
4529 mddev->queue->merge_bvec_fn = NULL;
4530 mddev->queue->unplug_fn = NULL;
4531 mddev->queue->backing_dev_info.congested_fn = NULL;
4532 module_put(mddev->pers->owner);
4533 if (mddev->pers->sync_request)
4534 mddev->private = &md_redundancy_group;
4536 /* tell userspace to handle 'inactive' */
4537 sysfs_notify_dirent(mddev->sysfs_state);
4539 list_for_each_entry(rdev, &mddev->disks, same_set)
4540 if (rdev->raid_disk >= 0) {
4542 sprintf(nm, "rd%d", rdev->raid_disk);
4543 sysfs_remove_link(&mddev->kobj, nm);
4546 set_capacity(disk, 0);
4552 if (!mddev->in_sync || mddev->flags) {
4553 /* mark array as shutdown cleanly */
4555 md_update_sb(mddev, 1);
4558 set_disk_ro(disk, 1);
4559 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4563 mutex_unlock(&mddev->open_mutex);
4567 * Free resources if final stop
4571 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4573 bitmap_destroy(mddev);
4574 if (mddev->bitmap_info.file) {
4575 restore_bitmap_write_access(mddev->bitmap_info.file);
4576 fput(mddev->bitmap_info.file);
4577 mddev->bitmap_info.file = NULL;
4579 mddev->bitmap_info.offset = 0;
4581 /* make sure all md_delayed_delete calls have finished */
4582 flush_scheduled_work();
4584 export_array(mddev);
4586 mddev->array_sectors = 0;
4587 mddev->external_size = 0;
4588 mddev->dev_sectors = 0;
4589 mddev->raid_disks = 0;
4590 mddev->recovery_cp = 0;
4591 mddev->resync_min = 0;
4592 mddev->resync_max = MaxSector;
4593 mddev->reshape_position = MaxSector;
4594 mddev->external = 0;
4595 mddev->persistent = 0;
4596 mddev->level = LEVEL_NONE;
4597 mddev->clevel[0] = 0;
4600 mddev->metadata_type[0] = 0;
4601 mddev->chunk_sectors = 0;
4602 mddev->ctime = mddev->utime = 0;
4604 mddev->max_disks = 0;
4606 mddev->delta_disks = 0;
4607 mddev->new_level = LEVEL_NONE;
4608 mddev->new_layout = 0;
4609 mddev->new_chunk_sectors = 0;
4610 mddev->curr_resync = 0;
4611 mddev->resync_mismatches = 0;
4612 mddev->suspend_lo = mddev->suspend_hi = 0;
4613 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4614 mddev->recovery = 0;
4617 mddev->degraded = 0;
4618 mddev->barriers_work = 0;
4619 mddev->safemode = 0;
4620 mddev->bitmap_info.offset = 0;
4621 mddev->bitmap_info.default_offset = 0;
4622 mddev->bitmap_info.chunksize = 0;
4623 mddev->bitmap_info.daemon_sleep = 0;
4624 mddev->bitmap_info.max_write_behind = 0;
4625 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4626 if (mddev->hold_active == UNTIL_STOP)
4627 mddev->hold_active = 0;
4629 } else if (mddev->pers)
4630 printk(KERN_INFO "md: %s switched to read-only mode.\n",
4633 blk_integrity_unregister(disk);
4634 md_new_event(mddev);
4635 sysfs_notify_dirent(mddev->sysfs_state);
4640 static void autorun_array(mddev_t *mddev)
4645 if (list_empty(&mddev->disks))
4648 printk(KERN_INFO "md: running: ");
4650 list_for_each_entry(rdev, &mddev->disks, same_set) {
4651 char b[BDEVNAME_SIZE];
4652 printk("<%s>", bdevname(rdev->bdev,b));
4656 err = do_md_run(mddev);
4658 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
4659 do_md_stop(mddev, 0, 0);
4664 * lets try to run arrays based on all disks that have arrived
4665 * until now. (those are in pending_raid_disks)
4667 * the method: pick the first pending disk, collect all disks with
4668 * the same UUID, remove all from the pending list and put them into
4669 * the 'same_array' list. Then order this list based on superblock
4670 * update time (freshest comes first), kick out 'old' disks and
4671 * compare superblocks. If everything's fine then run it.
4673 * If "unit" is allocated, then bump its reference count
4675 static void autorun_devices(int part)
4677 mdk_rdev_t *rdev0, *rdev, *tmp;
4679 char b[BDEVNAME_SIZE];
4681 printk(KERN_INFO "md: autorun ...\n");
4682 while (!list_empty(&pending_raid_disks)) {
4685 LIST_HEAD(candidates);
4686 rdev0 = list_entry(pending_raid_disks.next,
4687 mdk_rdev_t, same_set);
4689 printk(KERN_INFO "md: considering %s ...\n",
4690 bdevname(rdev0->bdev,b));
4691 INIT_LIST_HEAD(&candidates);
4692 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
4693 if (super_90_load(rdev, rdev0, 0) >= 0) {
4694 printk(KERN_INFO "md: adding %s ...\n",
4695 bdevname(rdev->bdev,b));
4696 list_move(&rdev->same_set, &candidates);
4699 * now we have a set of devices, with all of them having
4700 * mostly sane superblocks. It's time to allocate the
4704 dev = MKDEV(mdp_major,
4705 rdev0->preferred_minor << MdpMinorShift);
4706 unit = MINOR(dev) >> MdpMinorShift;
4708 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4711 if (rdev0->preferred_minor != unit) {
4712 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4713 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4717 md_probe(dev, NULL, NULL);
4718 mddev = mddev_find(dev);
4719 if (!mddev || !mddev->gendisk) {
4723 "md: cannot allocate memory for md drive.\n");
4726 if (mddev_lock(mddev))
4727 printk(KERN_WARNING "md: %s locked, cannot run\n",
4729 else if (mddev->raid_disks || mddev->major_version
4730 || !list_empty(&mddev->disks)) {
4732 "md: %s already running, cannot run %s\n",
4733 mdname(mddev), bdevname(rdev0->bdev,b));
4734 mddev_unlock(mddev);
4736 printk(KERN_INFO "md: created %s\n", mdname(mddev));
4737 mddev->persistent = 1;
4738 rdev_for_each_list(rdev, tmp, &candidates) {
4739 list_del_init(&rdev->same_set);
4740 if (bind_rdev_to_array(rdev, mddev))
4743 autorun_array(mddev);
4744 mddev_unlock(mddev);
4746 /* on success, candidates will be empty, on error
4749 rdev_for_each_list(rdev, tmp, &candidates) {
4750 list_del_init(&rdev->same_set);
4755 printk(KERN_INFO "md: ... autorun DONE.\n");
4757 #endif /* !MODULE */
4759 static int get_version(void __user * arg)
4763 ver.major = MD_MAJOR_VERSION;
4764 ver.minor = MD_MINOR_VERSION;
4765 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4767 if (copy_to_user(arg, &ver, sizeof(ver)))
4773 static int get_array_info(mddev_t * mddev, void __user * arg)
4775 mdu_array_info_t info;
4776 int nr,working,insync,failed,spare;
4779 nr=working=insync=failed=spare=0;
4780 list_for_each_entry(rdev, &mddev->disks, same_set) {
4782 if (test_bit(Faulty, &rdev->flags))
4786 if (test_bit(In_sync, &rdev->flags))
4793 info.major_version = mddev->major_version;
4794 info.minor_version = mddev->minor_version;
4795 info.patch_version = MD_PATCHLEVEL_VERSION;
4796 info.ctime = mddev->ctime;
4797 info.level = mddev->level;
4798 info.size = mddev->dev_sectors / 2;
4799 if (info.size != mddev->dev_sectors / 2) /* overflow */
4802 info.raid_disks = mddev->raid_disks;
4803 info.md_minor = mddev->md_minor;
4804 info.not_persistent= !mddev->persistent;
4806 info.utime = mddev->utime;
4809 info.state = (1<<MD_SB_CLEAN);
4810 if (mddev->bitmap && mddev->bitmap_info.offset)
4811 info.state = (1<<MD_SB_BITMAP_PRESENT);
4812 info.active_disks = insync;
4813 info.working_disks = working;
4814 info.failed_disks = failed;
4815 info.spare_disks = spare;
4817 info.layout = mddev->layout;
4818 info.chunk_size = mddev->chunk_sectors << 9;
4820 if (copy_to_user(arg, &info, sizeof(info)))
4826 static int get_bitmap_file(mddev_t * mddev, void __user * arg)
4828 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4829 char *ptr, *buf = NULL;
4832 if (md_allow_write(mddev))
4833 file = kmalloc(sizeof(*file), GFP_NOIO);
4835 file = kmalloc(sizeof(*file), GFP_KERNEL);
4840 /* bitmap disabled, zero the first byte and copy out */
4841 if (!mddev->bitmap || !mddev->bitmap->file) {
4842 file->pathname[0] = '\0';
4846 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4850 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4854 strcpy(file->pathname, ptr);
4858 if (copy_to_user(arg, file, sizeof(*file)))
4866 static int get_disk_info(mddev_t * mddev, void __user * arg)
4868 mdu_disk_info_t info;
4871 if (copy_from_user(&info, arg, sizeof(info)))
4874 rdev = find_rdev_nr(mddev, info.number);
4876 info.major = MAJOR(rdev->bdev->bd_dev);
4877 info.minor = MINOR(rdev->bdev->bd_dev);
4878 info.raid_disk = rdev->raid_disk;
4880 if (test_bit(Faulty, &rdev->flags))
4881 info.state |= (1<<MD_DISK_FAULTY);
4882 else if (test_bit(In_sync, &rdev->flags)) {
4883 info.state |= (1<<MD_DISK_ACTIVE);
4884 info.state |= (1<<MD_DISK_SYNC);
4886 if (test_bit(WriteMostly, &rdev->flags))
4887 info.state |= (1<<MD_DISK_WRITEMOSTLY);
4889 info.major = info.minor = 0;
4890 info.raid_disk = -1;
4891 info.state = (1<<MD_DISK_REMOVED);
4894 if (copy_to_user(arg, &info, sizeof(info)))
4900 static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4902 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4904 dev_t dev = MKDEV(info->major,info->minor);
4906 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4909 if (!mddev->raid_disks) {
4911 /* expecting a device which has a superblock */
4912 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4915 "md: md_import_device returned %ld\n",
4917 return PTR_ERR(rdev);
4919 if (!list_empty(&mddev->disks)) {
4920 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4921 mdk_rdev_t, same_set);
4922 err = super_types[mddev->major_version]
4923 .load_super(rdev, rdev0, mddev->minor_version);
4926 "md: %s has different UUID to %s\n",
4927 bdevname(rdev->bdev,b),
4928 bdevname(rdev0->bdev,b2));
4933 err = bind_rdev_to_array(rdev, mddev);
4940 * add_new_disk can be used once the array is assembled
4941 * to add "hot spares". They must already have a superblock
4946 if (!mddev->pers->hot_add_disk) {
4948 "%s: personality does not support diskops!\n",
4952 if (mddev->persistent)
4953 rdev = md_import_device(dev, mddev->major_version,
4954 mddev->minor_version);
4956 rdev = md_import_device(dev, -1, -1);
4959 "md: md_import_device returned %ld\n",
4961 return PTR_ERR(rdev);
4963 /* set save_raid_disk if appropriate */
4964 if (!mddev->persistent) {
4965 if (info->state & (1<<MD_DISK_SYNC) &&
4966 info->raid_disk < mddev->raid_disks)
4967 rdev->raid_disk = info->raid_disk;
4969 rdev->raid_disk = -1;
4971 super_types[mddev->major_version].
4972 validate_super(mddev, rdev);
4973 rdev->saved_raid_disk = rdev->raid_disk;
4975 clear_bit(In_sync, &rdev->flags); /* just to be sure */
4976 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4977 set_bit(WriteMostly, &rdev->flags);
4979 clear_bit(WriteMostly, &rdev->flags);
4981 rdev->raid_disk = -1;
4982 err = bind_rdev_to_array(rdev, mddev);
4983 if (!err && !mddev->pers->hot_remove_disk) {
4984 /* If there is hot_add_disk but no hot_remove_disk
4985 * then added disks for geometry changes,
4986 * and should be added immediately.
4988 super_types[mddev->major_version].
4989 validate_super(mddev, rdev);
4990 err = mddev->pers->hot_add_disk(mddev, rdev);
4992 unbind_rdev_from_array(rdev);
4997 sysfs_notify_dirent(rdev->sysfs_state);
4999 md_update_sb(mddev, 1);
5000 if (mddev->degraded)
5001 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
5002 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5003 md_wakeup_thread(mddev->thread);
5007 /* otherwise, add_new_disk is only allowed
5008 * for major_version==0 superblocks
5010 if (mddev->major_version != 0) {
5011 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5016 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5018 rdev = md_import_device(dev, -1, 0);
5021 "md: error, md_import_device() returned %ld\n",
5023 return PTR_ERR(rdev);
5025 rdev->desc_nr = info->number;
5026 if (info->raid_disk < mddev->raid_disks)
5027 rdev->raid_disk = info->raid_disk;
5029 rdev->raid_disk = -1;
5031 if (rdev->raid_disk < mddev->raid_disks)
5032 if (info->state & (1<<MD_DISK_SYNC))
5033 set_bit(In_sync, &rdev->flags);
5035 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5036 set_bit(WriteMostly, &rdev->flags);
5038 if (!mddev->persistent) {
5039 printk(KERN_INFO "md: nonpersistent superblock ...\n");
5040 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
5042 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
5043 rdev->sectors = rdev->sb_start;
5045 err = bind_rdev_to_array(rdev, mddev);
5055 static int hot_remove_disk(mddev_t * mddev, dev_t dev)
5057 char b[BDEVNAME_SIZE];
5060 rdev = find_rdev(mddev, dev);
5064 if (rdev->raid_disk >= 0)
5067 kick_rdev_from_array(rdev);
5068 md_update_sb(mddev, 1);
5069 md_new_event(mddev);
5073 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
5074 bdevname(rdev->bdev,b), mdname(mddev));
5078 static int hot_add_disk(mddev_t * mddev, dev_t dev)
5080 char b[BDEVNAME_SIZE];
5087 if (mddev->major_version != 0) {
5088 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5089 " version-0 superblocks.\n",
5093 if (!mddev->pers->hot_add_disk) {
5095 "%s: personality does not support diskops!\n",
5100 rdev = md_import_device(dev, -1, 0);
5103 "md: error, md_import_device() returned %ld\n",
5108 if (mddev->persistent)
5109 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
5111 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
5113 rdev->sectors = rdev->sb_start;
5115 if (test_bit(Faulty, &rdev->flags)) {
5117 "md: can not hot-add faulty %s disk to %s!\n",
5118 bdevname(rdev->bdev,b), mdname(mddev));
5122 clear_bit(In_sync, &rdev->flags);
5124 rdev->saved_raid_disk = -1;
5125 err = bind_rdev_to_array(rdev, mddev);
5130 * The rest should better be atomic, we can have disk failures
5131 * noticed in interrupt contexts ...
5134 rdev->raid_disk = -1;
5136 md_update_sb(mddev, 1);
5139 * Kick recovery, maybe this spare has to be added to the
5140 * array immediately.
5142 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5143 md_wakeup_thread(mddev->thread);
5144 md_new_event(mddev);
5152 static int set_bitmap_file(mddev_t *mddev, int fd)
5157 if (!mddev->pers->quiesce)
5159 if (mddev->recovery || mddev->sync_thread)
5161 /* we should be able to change the bitmap.. */
5167 return -EEXIST; /* cannot add when bitmap is present */
5168 mddev->bitmap_info.file = fget(fd);
5170 if (mddev->bitmap_info.file == NULL) {
5171 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5176 err = deny_bitmap_write_access(mddev->bitmap_info.file);
5178 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5180 fput(mddev->bitmap_info.file);
5181 mddev->bitmap_info.file = NULL;
5184 mddev->bitmap_info.offset = 0; /* file overrides offset */
5185 } else if (mddev->bitmap == NULL)
5186 return -ENOENT; /* cannot remove what isn't there */
5189 mddev->pers->quiesce(mddev, 1);
5191 err = bitmap_create(mddev);
5192 if (fd < 0 || err) {
5193 bitmap_destroy(mddev);
5194 fd = -1; /* make sure to put the file */
5196 mddev->pers->quiesce(mddev, 0);
5199 if (mddev->bitmap_info.file) {
5200 restore_bitmap_write_access(mddev->bitmap_info.file);
5201 fput(mddev->bitmap_info.file);
5203 mddev->bitmap_info.file = NULL;
5210 * set_array_info is used two different ways
5211 * The original usage is when creating a new array.
5212 * In this usage, raid_disks is > 0 and it together with
5213 * level, size, not_persistent,layout,chunksize determine the
5214 * shape of the array.
5215 * This will always create an array with a type-0.90.0 superblock.
5216 * The newer usage is when assembling an array.
5217 * In this case raid_disks will be 0, and the major_version field is
5218 * use to determine which style super-blocks are to be found on the devices.
5219 * The minor and patch _version numbers are also kept incase the
5220 * super_block handler wishes to interpret them.
5222 static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5225 if (info->raid_disks == 0) {
5226 /* just setting version number for superblock loading */
5227 if (info->major_version < 0 ||
5228 info->major_version >= ARRAY_SIZE(super_types) ||
5229 super_types[info->major_version].name == NULL) {
5230 /* maybe try to auto-load a module? */
5232 "md: superblock version %d not known\n",
5233 info->major_version);
5236 mddev->major_version = info->major_version;
5237 mddev->minor_version = info->minor_version;
5238 mddev->patch_version = info->patch_version;
5239 mddev->persistent = !info->not_persistent;
5240 /* ensure mddev_put doesn't delete this now that there
5241 * is some minimal configuration.
5243 mddev->ctime = get_seconds();
5246 mddev->major_version = MD_MAJOR_VERSION;
5247 mddev->minor_version = MD_MINOR_VERSION;
5248 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5249 mddev->ctime = get_seconds();
5251 mddev->level = info->level;
5252 mddev->clevel[0] = 0;
5253 mddev->dev_sectors = 2 * (sector_t)info->size;
5254 mddev->raid_disks = info->raid_disks;
5255 /* don't set md_minor, it is determined by which /dev/md* was
5258 if (info->state & (1<<MD_SB_CLEAN))
5259 mddev->recovery_cp = MaxSector;
5261 mddev->recovery_cp = 0;
5262 mddev->persistent = ! info->not_persistent;
5263 mddev->external = 0;
5265 mddev->layout = info->layout;
5266 mddev->chunk_sectors = info->chunk_size >> 9;
5268 mddev->max_disks = MD_SB_DISKS;
5270 if (mddev->persistent)
5272 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5274 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5275 mddev->bitmap_info.offset = 0;
5277 mddev->reshape_position = MaxSector;
5280 * Generate a 128 bit UUID
5282 get_random_bytes(mddev->uuid, 16);
5284 mddev->new_level = mddev->level;
5285 mddev->new_chunk_sectors = mddev->chunk_sectors;
5286 mddev->new_layout = mddev->layout;
5287 mddev->delta_disks = 0;
5292 void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5294 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5296 if (mddev->external_size)
5299 mddev->array_sectors = array_sectors;
5301 EXPORT_SYMBOL(md_set_array_sectors);
5303 static int update_size(mddev_t *mddev, sector_t num_sectors)
5307 int fit = (num_sectors == 0);
5309 if (mddev->pers->resize == NULL)
5311 /* The "num_sectors" is the number of sectors of each device that
5312 * is used. This can only make sense for arrays with redundancy.
5313 * linear and raid0 always use whatever space is available. We can only
5314 * consider changing this number if no resync or reconstruction is
5315 * happening, and if the new size is acceptable. It must fit before the
5316 * sb_start or, if that is <data_offset, it must fit before the size
5317 * of each device. If num_sectors is zero, we find the largest size
5321 if (mddev->sync_thread)
5324 /* Sorry, cannot grow a bitmap yet, just remove it,
5328 list_for_each_entry(rdev, &mddev->disks, same_set) {
5329 sector_t avail = rdev->sectors;
5331 if (fit && (num_sectors == 0 || num_sectors > avail))
5332 num_sectors = avail;
5333 if (avail < num_sectors)
5336 rv = mddev->pers->resize(mddev, num_sectors);
5338 revalidate_disk(mddev->gendisk);
5342 static int update_raid_disks(mddev_t *mddev, int raid_disks)
5345 /* change the number of raid disks */
5346 if (mddev->pers->check_reshape == NULL)
5348 if (raid_disks <= 0 ||
5349 raid_disks >= mddev->max_disks)
5351 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
5353 mddev->delta_disks = raid_disks - mddev->raid_disks;
5355 rv = mddev->pers->check_reshape(mddev);
5361 * update_array_info is used to change the configuration of an
5363 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5364 * fields in the info are checked against the array.
5365 * Any differences that cannot be handled will cause an error.
5366 * Normally, only one change can be managed at a time.
5368 static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5374 /* calculate expected state,ignoring low bits */
5375 if (mddev->bitmap && mddev->bitmap_info.offset)
5376 state |= (1 << MD_SB_BITMAP_PRESENT);
5378 if (mddev->major_version != info->major_version ||
5379 mddev->minor_version != info->minor_version ||
5380 /* mddev->patch_version != info->patch_version || */
5381 mddev->ctime != info->ctime ||
5382 mddev->level != info->level ||
5383 /* mddev->layout != info->layout || */
5384 !mddev->persistent != info->not_persistent||
5385 mddev->chunk_sectors != info->chunk_size >> 9 ||
5386 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5387 ((state^info->state) & 0xfffffe00)
5390 /* Check there is only one change */
5391 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5393 if (mddev->raid_disks != info->raid_disks)
5395 if (mddev->layout != info->layout)
5397 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5404 if (mddev->layout != info->layout) {
5406 * we don't need to do anything at the md level, the
5407 * personality will take care of it all.
5409 if (mddev->pers->check_reshape == NULL)
5412 mddev->new_layout = info->layout;
5413 rv = mddev->pers->check_reshape(mddev);
5415 mddev->new_layout = mddev->layout;
5419 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5420 rv = update_size(mddev, (sector_t)info->size * 2);
5422 if (mddev->raid_disks != info->raid_disks)
5423 rv = update_raid_disks(mddev, info->raid_disks);
5425 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5426 if (mddev->pers->quiesce == NULL)
5428 if (mddev->recovery || mddev->sync_thread)
5430 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5431 /* add the bitmap */
5434 if (mddev->bitmap_info.default_offset == 0)
5436 mddev->bitmap_info.offset =
5437 mddev->bitmap_info.default_offset;
5438 mddev->pers->quiesce(mddev, 1);
5439 rv = bitmap_create(mddev);
5441 bitmap_destroy(mddev);
5442 mddev->pers->quiesce(mddev, 0);
5444 /* remove the bitmap */
5447 if (mddev->bitmap->file)
5449 mddev->pers->quiesce(mddev, 1);
5450 bitmap_destroy(mddev);
5451 mddev->pers->quiesce(mddev, 0);
5452 mddev->bitmap_info.offset = 0;
5455 md_update_sb(mddev, 1);
5459 static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5463 if (mddev->pers == NULL)
5466 rdev = find_rdev(mddev, dev);
5470 md_error(mddev, rdev);
5475 * We have a problem here : there is no easy way to give a CHS
5476 * virtual geometry. We currently pretend that we have a 2 heads
5477 * 4 sectors (with a BIG number of cylinders...). This drives
5478 * dosfs just mad... ;-)
5480 static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5482 mddev_t *mddev = bdev->bd_disk->private_data;
5486 geo->cylinders = get_capacity(mddev->gendisk) / 8;
5490 static int md_ioctl(struct block_device *bdev, fmode_t mode,
5491 unsigned int cmd, unsigned long arg)
5494 void __user *argp = (void __user *)arg;
5495 mddev_t *mddev = NULL;
5497 if (!capable(CAP_SYS_ADMIN))
5501 * Commands dealing with the RAID driver but not any
5507 err = get_version(argp);
5510 case PRINT_RAID_DEBUG:
5518 autostart_arrays(arg);
5525 * Commands creating/starting a new array:
5528 mddev = bdev->bd_disk->private_data;
5535 err = mddev_lock(mddev);
5538 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5545 case SET_ARRAY_INFO:
5547 mdu_array_info_t info;
5549 memset(&info, 0, sizeof(info));
5550 else if (copy_from_user(&info, argp, sizeof(info))) {
5555 err = update_array_info(mddev, &info);
5557 printk(KERN_WARNING "md: couldn't update"
5558 " array info. %d\n", err);
5563 if (!list_empty(&mddev->disks)) {
5565 "md: array %s already has disks!\n",
5570 if (mddev->raid_disks) {
5572 "md: array %s already initialised!\n",
5577 err = set_array_info(mddev, &info);
5579 printk(KERN_WARNING "md: couldn't set"
5580 " array info. %d\n", err);
5590 * Commands querying/configuring an existing array:
5592 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
5593 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
5594 if ((!mddev->raid_disks && !mddev->external)
5595 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5596 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5597 && cmd != GET_BITMAP_FILE) {
5603 * Commands even a read-only array can execute:
5607 case GET_ARRAY_INFO:
5608 err = get_array_info(mddev, argp);
5611 case GET_BITMAP_FILE:
5612 err = get_bitmap_file(mddev, argp);
5616 err = get_disk_info(mddev, argp);
5619 case RESTART_ARRAY_RW:
5620 err = restart_array(mddev);
5624 err = do_md_stop(mddev, 0, 1);
5628 err = do_md_stop(mddev, 1, 1);
5634 * The remaining ioctls are changing the state of the
5635 * superblock, so we do not allow them on read-only arrays.
5636 * However non-MD ioctls (e.g. get-size) will still come through
5637 * here and hit the 'default' below, so only disallow
5638 * 'md' ioctls, and switch to rw mode if started auto-readonly.
5640 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
5641 if (mddev->ro == 2) {
5643 sysfs_notify_dirent(mddev->sysfs_state);
5644 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5645 md_wakeup_thread(mddev->thread);
5656 mdu_disk_info_t info;
5657 if (copy_from_user(&info, argp, sizeof(info)))
5660 err = add_new_disk(mddev, &info);
5664 case HOT_REMOVE_DISK:
5665 err = hot_remove_disk(mddev, new_decode_dev(arg));
5669 err = hot_add_disk(mddev, new_decode_dev(arg));
5672 case SET_DISK_FAULTY:
5673 err = set_disk_faulty(mddev, new_decode_dev(arg));
5677 err = do_md_run(mddev);
5680 case SET_BITMAP_FILE:
5681 err = set_bitmap_file(mddev, (int)arg);
5691 if (mddev->hold_active == UNTIL_IOCTL &&
5693 mddev->hold_active = 0;
5694 mddev_unlock(mddev);
5703 #ifdef CONFIG_COMPAT
5704 static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
5705 unsigned int cmd, unsigned long arg)
5708 case HOT_REMOVE_DISK:
5710 case SET_DISK_FAULTY:
5711 case SET_BITMAP_FILE:
5712 /* These take in integer arg, do not convert */
5715 arg = (unsigned long)compat_ptr(arg);
5719 return md_ioctl(bdev, mode, cmd, arg);
5721 #endif /* CONFIG_COMPAT */
5723 static int md_open(struct block_device *bdev, fmode_t mode)
5726 * Succeed if we can lock the mddev, which confirms that
5727 * it isn't being stopped right now.
5729 mddev_t *mddev = mddev_find(bdev->bd_dev);
5732 if (mddev->gendisk != bdev->bd_disk) {
5733 /* we are racing with mddev_put which is discarding this
5737 /* Wait until bdev->bd_disk is definitely gone */
5738 flush_scheduled_work();
5739 /* Then retry the open from the top */
5740 return -ERESTARTSYS;
5742 BUG_ON(mddev != bdev->bd_disk->private_data);
5744 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
5748 atomic_inc(&mddev->openers);
5749 mutex_unlock(&mddev->open_mutex);
5751 check_disk_change(bdev);
5756 static int md_release(struct gendisk *disk, fmode_t mode)
5758 mddev_t *mddev = disk->private_data;
5761 atomic_dec(&mddev->openers);
5767 static int md_media_changed(struct gendisk *disk)
5769 mddev_t *mddev = disk->private_data;
5771 return mddev->changed;
5774 static int md_revalidate(struct gendisk *disk)
5776 mddev_t *mddev = disk->private_data;
5781 static const struct block_device_operations md_fops =
5783 .owner = THIS_MODULE,
5785 .release = md_release,
5787 #ifdef CONFIG_COMPAT
5788 .compat_ioctl = md_compat_ioctl,
5790 .getgeo = md_getgeo,
5791 .media_changed = md_media_changed,
5792 .revalidate_disk= md_revalidate,
5795 static int md_thread(void * arg)
5797 mdk_thread_t *thread = arg;
5800 * md_thread is a 'system-thread', it's priority should be very
5801 * high. We avoid resource deadlocks individually in each
5802 * raid personality. (RAID5 does preallocation) We also use RR and
5803 * the very same RT priority as kswapd, thus we will never get
5804 * into a priority inversion deadlock.
5806 * we definitely have to have equal or higher priority than
5807 * bdflush, otherwise bdflush will deadlock if there are too
5808 * many dirty RAID5 blocks.
5811 allow_signal(SIGKILL);
5812 while (!kthread_should_stop()) {
5814 /* We need to wait INTERRUPTIBLE so that
5815 * we don't add to the load-average.
5816 * That means we need to be sure no signals are
5819 if (signal_pending(current))
5820 flush_signals(current);
5822 wait_event_interruptible_timeout
5824 test_bit(THREAD_WAKEUP, &thread->flags)
5825 || kthread_should_stop(),
5828 clear_bit(THREAD_WAKEUP, &thread->flags);
5830 thread->run(thread->mddev);
5836 void md_wakeup_thread(mdk_thread_t *thread)
5839 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5840 set_bit(THREAD_WAKEUP, &thread->flags);
5841 wake_up(&thread->wqueue);
5845 mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5848 mdk_thread_t *thread;
5850 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
5854 init_waitqueue_head(&thread->wqueue);
5857 thread->mddev = mddev;
5858 thread->timeout = MAX_SCHEDULE_TIMEOUT;
5859 thread->tsk = kthread_run(md_thread, thread,
5861 mdname(thread->mddev),
5862 name ?: mddev->pers->name);
5863 if (IS_ERR(thread->tsk)) {
5870 void md_unregister_thread(mdk_thread_t *thread)
5874 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
5876 kthread_stop(thread->tsk);
5880 void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
5887 if (!rdev || test_bit(Faulty, &rdev->flags))
5890 if (mddev->external)
5891 set_bit(Blocked, &rdev->flags);
5893 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5895 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5896 __builtin_return_address(0),__builtin_return_address(1),
5897 __builtin_return_address(2),__builtin_return_address(3));
5901 if (!mddev->pers->error_handler)
5903 mddev->pers->error_handler(mddev,rdev);
5904 if (mddev->degraded)
5905 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
5906 set_bit(StateChanged, &rdev->flags);
5907 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5908 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5909 md_wakeup_thread(mddev->thread);
5910 md_new_event_inintr(mddev);
5913 /* seq_file implementation /proc/mdstat */
5915 static void status_unused(struct seq_file *seq)
5920 seq_printf(seq, "unused devices: ");
5922 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
5923 char b[BDEVNAME_SIZE];
5925 seq_printf(seq, "%s ",
5926 bdevname(rdev->bdev,b));
5929 seq_printf(seq, "<none>");
5931 seq_printf(seq, "\n");
5935 static void status_resync(struct seq_file *seq, mddev_t * mddev)
5937 sector_t max_sectors, resync, res;
5938 unsigned long dt, db;
5941 unsigned int per_milli;
5943 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
5945 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
5946 max_sectors = mddev->resync_max_sectors;
5948 max_sectors = mddev->dev_sectors;
5951 * Should not happen.
5957 /* Pick 'scale' such that (resync>>scale)*1000 will fit
5958 * in a sector_t, and (max_sectors>>scale) will fit in a
5959 * u32, as those are the requirements for sector_div.
5960 * Thus 'scale' must be at least 10
5963 if (sizeof(sector_t) > sizeof(unsigned long)) {
5964 while ( max_sectors/2 > (1ULL<<(scale+32)))
5967 res = (resync>>scale)*1000;
5968 sector_div(res, (u32)((max_sectors>>scale)+1));
5972 int i, x = per_milli/50, y = 20-x;
5973 seq_printf(seq, "[");
5974 for (i = 0; i < x; i++)
5975 seq_printf(seq, "=");
5976 seq_printf(seq, ">");
5977 for (i = 0; i < y; i++)
5978 seq_printf(seq, ".");
5979 seq_printf(seq, "] ");
5981 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
5982 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
5984 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
5986 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
5987 "resync" : "recovery"))),
5988 per_milli/10, per_milli % 10,
5989 (unsigned long long) resync/2,
5990 (unsigned long long) max_sectors/2);
5993 * dt: time from mark until now
5994 * db: blocks written from mark until now
5995 * rt: remaining time
5997 * rt is a sector_t, so could be 32bit or 64bit.
5998 * So we divide before multiply in case it is 32bit and close
6000 * We scale the divisor (db) by 32 to avoid loosing precision
6001 * near the end of resync when the number of remaining sectors
6003 * We then divide rt by 32 after multiplying by db to compensate.
6004 * The '+1' avoids division by zero if db is very small.
6006 dt = ((jiffies - mddev->resync_mark) / HZ);
6008 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6009 - mddev->resync_mark_cnt;
6011 rt = max_sectors - resync; /* number of remaining sectors */
6012 sector_div(rt, db/32+1);
6016 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6017 ((unsigned long)rt % 60)/6);
6019 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
6022 static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6024 struct list_head *tmp;
6034 spin_lock(&all_mddevs_lock);
6035 list_for_each(tmp,&all_mddevs)
6037 mddev = list_entry(tmp, mddev_t, all_mddevs);
6039 spin_unlock(&all_mddevs_lock);
6042 spin_unlock(&all_mddevs_lock);
6044 return (void*)2;/* tail */
6048 static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6050 struct list_head *tmp;
6051 mddev_t *next_mddev, *mddev = v;
6057 spin_lock(&all_mddevs_lock);
6059 tmp = all_mddevs.next;
6061 tmp = mddev->all_mddevs.next;
6062 if (tmp != &all_mddevs)
6063 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
6065 next_mddev = (void*)2;
6068 spin_unlock(&all_mddevs_lock);
6076 static void md_seq_stop(struct seq_file *seq, void *v)
6080 if (mddev && v != (void*)1 && v != (void*)2)
6084 struct mdstat_info {
6088 static int md_seq_show(struct seq_file *seq, void *v)
6093 struct mdstat_info *mi = seq->private;
6094 struct bitmap *bitmap;
6096 if (v == (void*)1) {
6097 struct mdk_personality *pers;
6098 seq_printf(seq, "Personalities : ");
6099 spin_lock(&pers_lock);
6100 list_for_each_entry(pers, &pers_list, list)
6101 seq_printf(seq, "[%s] ", pers->name);
6103 spin_unlock(&pers_lock);
6104 seq_printf(seq, "\n");
6105 mi->event = atomic_read(&md_event_count);
6108 if (v == (void*)2) {
6113 if (mddev_lock(mddev) < 0)
6116 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6117 seq_printf(seq, "%s : %sactive", mdname(mddev),
6118 mddev->pers ? "" : "in");
6121 seq_printf(seq, " (read-only)");
6123 seq_printf(seq, " (auto-read-only)");
6124 seq_printf(seq, " %s", mddev->pers->name);
6128 list_for_each_entry(rdev, &mddev->disks, same_set) {
6129 char b[BDEVNAME_SIZE];
6130 seq_printf(seq, " %s[%d]",
6131 bdevname(rdev->bdev,b), rdev->desc_nr);
6132 if (test_bit(WriteMostly, &rdev->flags))
6133 seq_printf(seq, "(W)");
6134 if (test_bit(Faulty, &rdev->flags)) {
6135 seq_printf(seq, "(F)");
6137 } else if (rdev->raid_disk < 0)
6138 seq_printf(seq, "(S)"); /* spare */
6139 sectors += rdev->sectors;
6142 if (!list_empty(&mddev->disks)) {
6144 seq_printf(seq, "\n %llu blocks",
6145 (unsigned long long)
6146 mddev->array_sectors / 2);
6148 seq_printf(seq, "\n %llu blocks",
6149 (unsigned long long)sectors / 2);
6151 if (mddev->persistent) {
6152 if (mddev->major_version != 0 ||
6153 mddev->minor_version != 90) {
6154 seq_printf(seq," super %d.%d",
6155 mddev->major_version,
6156 mddev->minor_version);
6158 } else if (mddev->external)
6159 seq_printf(seq, " super external:%s",
6160 mddev->metadata_type);
6162 seq_printf(seq, " super non-persistent");
6165 mddev->pers->status(seq, mddev);
6166 seq_printf(seq, "\n ");
6167 if (mddev->pers->sync_request) {
6168 if (mddev->curr_resync > 2) {
6169 status_resync(seq, mddev);
6170 seq_printf(seq, "\n ");
6171 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6172 seq_printf(seq, "\tresync=DELAYED\n ");
6173 else if (mddev->recovery_cp < MaxSector)
6174 seq_printf(seq, "\tresync=PENDING\n ");
6177 seq_printf(seq, "\n ");
6179 if ((bitmap = mddev->bitmap)) {
6180 unsigned long chunk_kb;
6181 unsigned long flags;
6182 spin_lock_irqsave(&bitmap->lock, flags);
6183 chunk_kb = mddev->bitmap_info.chunksize >> 10;
6184 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6186 bitmap->pages - bitmap->missing_pages,
6188 (bitmap->pages - bitmap->missing_pages)
6189 << (PAGE_SHIFT - 10),
6190 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
6191 chunk_kb ? "KB" : "B");
6193 seq_printf(seq, ", file: ");
6194 seq_path(seq, &bitmap->file->f_path, " \t\n");
6197 seq_printf(seq, "\n");
6198 spin_unlock_irqrestore(&bitmap->lock, flags);
6201 seq_printf(seq, "\n");
6203 mddev_unlock(mddev);
6208 static const struct seq_operations md_seq_ops = {
6209 .start = md_seq_start,
6210 .next = md_seq_next,
6211 .stop = md_seq_stop,
6212 .show = md_seq_show,
6215 static int md_seq_open(struct inode *inode, struct file *file)
6218 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
6222 error = seq_open(file, &md_seq_ops);
6226 struct seq_file *p = file->private_data;
6228 mi->event = atomic_read(&md_event_count);
6233 static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6235 struct seq_file *m = filp->private_data;
6236 struct mdstat_info *mi = m->private;
6239 poll_wait(filp, &md_event_waiters, wait);
6241 /* always allow read */
6242 mask = POLLIN | POLLRDNORM;
6244 if (mi->event != atomic_read(&md_event_count))
6245 mask |= POLLERR | POLLPRI;
6249 static const struct file_operations md_seq_fops = {
6250 .owner = THIS_MODULE,
6251 .open = md_seq_open,
6253 .llseek = seq_lseek,
6254 .release = seq_release_private,
6255 .poll = mdstat_poll,
6258 int register_md_personality(struct mdk_personality *p)
6260 spin_lock(&pers_lock);
6261 list_add_tail(&p->list, &pers_list);
6262 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
6263 spin_unlock(&pers_lock);
6267 int unregister_md_personality(struct mdk_personality *p)
6269 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
6270 spin_lock(&pers_lock);
6271 list_del_init(&p->list);
6272 spin_unlock(&pers_lock);
6276 static int is_mddev_idle(mddev_t *mddev, int init)
6284 rdev_for_each_rcu(rdev, mddev) {
6285 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
6286 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6287 (int)part_stat_read(&disk->part0, sectors[1]) -
6288 atomic_read(&disk->sync_io);
6289 /* sync IO will cause sync_io to increase before the disk_stats
6290 * as sync_io is counted when a request starts, and
6291 * disk_stats is counted when it completes.
6292 * So resync activity will cause curr_events to be smaller than
6293 * when there was no such activity.
6294 * non-sync IO will cause disk_stat to increase without
6295 * increasing sync_io so curr_events will (eventually)
6296 * be larger than it was before. Once it becomes
6297 * substantially larger, the test below will cause
6298 * the array to appear non-idle, and resync will slow
6300 * If there is a lot of outstanding resync activity when
6301 * we set last_event to curr_events, then all that activity
6302 * completing might cause the array to appear non-idle
6303 * and resync will be slowed down even though there might
6304 * not have been non-resync activity. This will only
6305 * happen once though. 'last_events' will soon reflect
6306 * the state where there is little or no outstanding
6307 * resync requests, and further resync activity will
6308 * always make curr_events less than last_events.
6311 if (init || curr_events - rdev->last_events > 64) {
6312 rdev->last_events = curr_events;
6320 void md_done_sync(mddev_t *mddev, int blocks, int ok)
6322 /* another "blocks" (512byte) blocks have been synced */
6323 atomic_sub(blocks, &mddev->recovery_active);
6324 wake_up(&mddev->recovery_wait);
6326 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6327 md_wakeup_thread(mddev->thread);
6328 // stop recovery, signal do_sync ....
6333 /* md_write_start(mddev, bi)
6334 * If we need to update some array metadata (e.g. 'active' flag
6335 * in superblock) before writing, schedule a superblock update
6336 * and wait for it to complete.
6338 void md_write_start(mddev_t *mddev, struct bio *bi)
6341 if (bio_data_dir(bi) != WRITE)
6344 BUG_ON(mddev->ro == 1);
6345 if (mddev->ro == 2) {
6346 /* need to switch to read/write */
6348 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6349 md_wakeup_thread(mddev->thread);
6350 md_wakeup_thread(mddev->sync_thread);
6353 atomic_inc(&mddev->writes_pending);
6354 if (mddev->safemode == 1)
6355 mddev->safemode = 0;
6356 if (mddev->in_sync) {
6357 spin_lock_irq(&mddev->write_lock);
6358 if (mddev->in_sync) {
6360 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6361 md_wakeup_thread(mddev->thread);
6364 spin_unlock_irq(&mddev->write_lock);
6367 sysfs_notify_dirent(mddev->sysfs_state);
6368 wait_event(mddev->sb_wait,
6369 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
6370 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
6373 void md_write_end(mddev_t *mddev)
6375 if (atomic_dec_and_test(&mddev->writes_pending)) {
6376 if (mddev->safemode == 2)
6377 md_wakeup_thread(mddev->thread);
6378 else if (mddev->safemode_delay)
6379 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6383 /* md_allow_write(mddev)
6384 * Calling this ensures that the array is marked 'active' so that writes
6385 * may proceed without blocking. It is important to call this before
6386 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6387 * Must be called with mddev_lock held.
6389 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6390 * is dropped, so return -EAGAIN after notifying userspace.
6392 int md_allow_write(mddev_t *mddev)
6398 if (!mddev->pers->sync_request)
6401 spin_lock_irq(&mddev->write_lock);
6402 if (mddev->in_sync) {
6404 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6405 if (mddev->safemode_delay &&
6406 mddev->safemode == 0)
6407 mddev->safemode = 1;
6408 spin_unlock_irq(&mddev->write_lock);
6409 md_update_sb(mddev, 0);
6410 sysfs_notify_dirent(mddev->sysfs_state);
6412 spin_unlock_irq(&mddev->write_lock);
6414 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
6419 EXPORT_SYMBOL_GPL(md_allow_write);
6421 #define SYNC_MARKS 10
6422 #define SYNC_MARK_STEP (3*HZ)
6423 void md_do_sync(mddev_t *mddev)
6426 unsigned int currspeed = 0,
6428 sector_t max_sectors,j, io_sectors;
6429 unsigned long mark[SYNC_MARKS];
6430 sector_t mark_cnt[SYNC_MARKS];
6432 struct list_head *tmp;
6433 sector_t last_check;
6438 /* just incase thread restarts... */
6439 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6441 if (mddev->ro) /* never try to sync a read-only array */
6444 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6445 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6446 desc = "data-check";
6447 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6448 desc = "requested-resync";
6451 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6456 /* we overload curr_resync somewhat here.
6457 * 0 == not engaged in resync at all
6458 * 2 == checking that there is no conflict with another sync
6459 * 1 == like 2, but have yielded to allow conflicting resync to
6461 * other == active in resync - this many blocks
6463 * Before starting a resync we must have set curr_resync to
6464 * 2, and then checked that every "conflicting" array has curr_resync
6465 * less than ours. When we find one that is the same or higher
6466 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6467 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6468 * This will mean we have to start checking from the beginning again.
6473 mddev->curr_resync = 2;
6476 if (kthread_should_stop())
6477 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6479 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6481 for_each_mddev(mddev2, tmp) {
6482 if (mddev2 == mddev)
6484 if (!mddev->parallel_resync
6485 && mddev2->curr_resync
6486 && match_mddev_units(mddev, mddev2)) {
6488 if (mddev < mddev2 && mddev->curr_resync == 2) {
6489 /* arbitrarily yield */
6490 mddev->curr_resync = 1;
6491 wake_up(&resync_wait);
6493 if (mddev > mddev2 && mddev->curr_resync == 1)
6494 /* no need to wait here, we can wait the next
6495 * time 'round when curr_resync == 2
6498 /* We need to wait 'interruptible' so as not to
6499 * contribute to the load average, and not to
6500 * be caught by 'softlockup'
6502 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
6503 if (!kthread_should_stop() &&
6504 mddev2->curr_resync >= mddev->curr_resync) {
6505 printk(KERN_INFO "md: delaying %s of %s"
6506 " until %s has finished (they"
6507 " share one or more physical units)\n",
6508 desc, mdname(mddev), mdname(mddev2));
6510 if (signal_pending(current))
6511 flush_signals(current);
6513 finish_wait(&resync_wait, &wq);
6516 finish_wait(&resync_wait, &wq);
6519 } while (mddev->curr_resync < 2);
6522 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6523 /* resync follows the size requested by the personality,
6524 * which defaults to physical size, but can be virtual size
6526 max_sectors = mddev->resync_max_sectors;
6527 mddev->resync_mismatches = 0;
6528 /* we don't use the checkpoint if there's a bitmap */
6529 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6530 j = mddev->resync_min;
6531 else if (!mddev->bitmap)
6532 j = mddev->recovery_cp;
6534 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6535 max_sectors = mddev->dev_sectors;
6537 /* recovery follows the physical size of devices */
6538 max_sectors = mddev->dev_sectors;
6541 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
6542 if (rdev->raid_disk >= 0 &&
6543 !test_bit(Faulty, &rdev->flags) &&
6544 !test_bit(In_sync, &rdev->flags) &&
6545 rdev->recovery_offset < j)
6546 j = rdev->recovery_offset;
6550 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6551 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6552 " %d KB/sec/disk.\n", speed_min(mddev));
6553 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
6554 "(but not more than %d KB/sec) for %s.\n",
6555 speed_max(mddev), desc);
6557 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
6560 for (m = 0; m < SYNC_MARKS; m++) {
6562 mark_cnt[m] = io_sectors;
6565 mddev->resync_mark = mark[last_mark];
6566 mddev->resync_mark_cnt = mark_cnt[last_mark];
6569 * Tune reconstruction:
6571 window = 32*(PAGE_SIZE/512);
6572 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6573 window/2,(unsigned long long) max_sectors/2);
6575 atomic_set(&mddev->recovery_active, 0);
6580 "md: resuming %s of %s from checkpoint.\n",
6581 desc, mdname(mddev));
6582 mddev->curr_resync = j;
6584 mddev->curr_resync_completed = mddev->curr_resync;
6586 while (j < max_sectors) {
6591 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6592 ((mddev->curr_resync > mddev->curr_resync_completed &&
6593 (mddev->curr_resync - mddev->curr_resync_completed)
6594 > (max_sectors >> 4)) ||
6595 (j - mddev->curr_resync_completed)*2
6596 >= mddev->resync_max - mddev->curr_resync_completed
6598 /* time to update curr_resync_completed */
6599 blk_unplug(mddev->queue);
6600 wait_event(mddev->recovery_wait,
6601 atomic_read(&mddev->recovery_active) == 0);
6602 mddev->curr_resync_completed =
6604 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6605 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
6608 while (j >= mddev->resync_max && !kthread_should_stop()) {
6609 /* As this condition is controlled by user-space,
6610 * we can block indefinitely, so use '_interruptible'
6611 * to avoid triggering warnings.
6613 flush_signals(current); /* just in case */
6614 wait_event_interruptible(mddev->recovery_wait,
6615 mddev->resync_max > j
6616 || kthread_should_stop());
6619 if (kthread_should_stop())
6622 sectors = mddev->pers->sync_request(mddev, j, &skipped,
6623 currspeed < speed_min(mddev));
6625 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6629 if (!skipped) { /* actual IO requested */
6630 io_sectors += sectors;
6631 atomic_add(sectors, &mddev->recovery_active);
6635 if (j>1) mddev->curr_resync = j;
6636 mddev->curr_mark_cnt = io_sectors;
6637 if (last_check == 0)
6638 /* this is the earliers that rebuilt will be
6639 * visible in /proc/mdstat
6641 md_new_event(mddev);
6643 if (last_check + window > io_sectors || j == max_sectors)
6646 last_check = io_sectors;
6648 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6652 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6654 int next = (last_mark+1) % SYNC_MARKS;
6656 mddev->resync_mark = mark[next];
6657 mddev->resync_mark_cnt = mark_cnt[next];
6658 mark[next] = jiffies;
6659 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
6664 if (kthread_should_stop())
6669 * this loop exits only if either when we are slower than
6670 * the 'hard' speed limit, or the system was IO-idle for
6672 * the system might be non-idle CPU-wise, but we only care
6673 * about not overloading the IO subsystem. (things like an
6674 * e2fsck being done on the RAID array should execute fast)
6676 blk_unplug(mddev->queue);
6679 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6680 /((jiffies-mddev->resync_mark)/HZ +1) +1;
6682 if (currspeed > speed_min(mddev)) {
6683 if ((currspeed > speed_max(mddev)) ||
6684 !is_mddev_idle(mddev, 0)) {
6690 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
6692 * this also signals 'finished resyncing' to md_stop
6695 blk_unplug(mddev->queue);
6697 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6699 /* tell personality that we are finished */
6700 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
6702 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
6703 mddev->curr_resync > 2) {
6704 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6705 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6706 if (mddev->curr_resync >= mddev->recovery_cp) {
6708 "md: checkpointing %s of %s.\n",
6709 desc, mdname(mddev));
6710 mddev->recovery_cp = mddev->curr_resync;
6713 mddev->recovery_cp = MaxSector;
6715 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6716 mddev->curr_resync = MaxSector;
6718 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
6719 if (rdev->raid_disk >= 0 &&
6720 !test_bit(Faulty, &rdev->flags) &&
6721 !test_bit(In_sync, &rdev->flags) &&
6722 rdev->recovery_offset < mddev->curr_resync)
6723 rdev->recovery_offset = mddev->curr_resync;
6727 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6730 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6731 /* We completed so min/max setting can be forgotten if used. */
6732 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6733 mddev->resync_min = 0;
6734 mddev->resync_max = MaxSector;
6735 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6736 mddev->resync_min = mddev->curr_resync_completed;
6737 mddev->curr_resync = 0;
6738 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6739 mddev->curr_resync_completed = 0;
6740 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
6741 wake_up(&resync_wait);
6742 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6743 md_wakeup_thread(mddev->thread);
6748 * got a signal, exit.
6751 "md: md_do_sync() got signal ... exiting\n");
6752 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6756 EXPORT_SYMBOL_GPL(md_do_sync);
6759 static int remove_and_add_spares(mddev_t *mddev)
6764 mddev->curr_resync_completed = 0;
6766 list_for_each_entry(rdev, &mddev->disks, same_set)
6767 if (rdev->raid_disk >= 0 &&
6768 !test_bit(Blocked, &rdev->flags) &&
6769 (test_bit(Faulty, &rdev->flags) ||
6770 ! test_bit(In_sync, &rdev->flags)) &&
6771 atomic_read(&rdev->nr_pending)==0) {
6772 if (mddev->pers->hot_remove_disk(
6773 mddev, rdev->raid_disk)==0) {
6775 sprintf(nm,"rd%d", rdev->raid_disk);
6776 sysfs_remove_link(&mddev->kobj, nm);
6777 rdev->raid_disk = -1;
6781 if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) {
6782 list_for_each_entry(rdev, &mddev->disks, same_set) {
6783 if (rdev->raid_disk >= 0 &&
6784 !test_bit(In_sync, &rdev->flags) &&
6785 !test_bit(Blocked, &rdev->flags))
6787 if (rdev->raid_disk < 0
6788 && !test_bit(Faulty, &rdev->flags)) {
6789 rdev->recovery_offset = 0;
6791 hot_add_disk(mddev, rdev) == 0) {
6793 sprintf(nm, "rd%d", rdev->raid_disk);
6794 if (sysfs_create_link(&mddev->kobj,
6797 "md: cannot register "
6801 md_new_event(mddev);
6802 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6811 * This routine is regularly called by all per-raid-array threads to
6812 * deal with generic issues like resync and super-block update.
6813 * Raid personalities that don't have a thread (linear/raid0) do not
6814 * need this as they never do any recovery or update the superblock.
6816 * It does not do any resync itself, but rather "forks" off other threads
6817 * to do that as needed.
6818 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6819 * "->recovery" and create a thread at ->sync_thread.
6820 * When the thread finishes it sets MD_RECOVERY_DONE
6821 * and wakeups up this thread which will reap the thread and finish up.
6822 * This thread also removes any faulty devices (with nr_pending == 0).
6824 * The overall approach is:
6825 * 1/ if the superblock needs updating, update it.
6826 * 2/ If a recovery thread is running, don't do anything else.
6827 * 3/ If recovery has finished, clean up, possibly marking spares active.
6828 * 4/ If there are any faulty devices, remove them.
6829 * 5/ If array is degraded, try to add spares devices
6830 * 6/ If array has spares or is not in-sync, start a resync thread.
6832 void md_check_recovery(mddev_t *mddev)
6838 bitmap_daemon_work(mddev);
6843 if (signal_pending(current)) {
6844 if (mddev->pers->sync_request && !mddev->external) {
6845 printk(KERN_INFO "md: %s in immediate safe mode\n",
6847 mddev->safemode = 2;
6849 flush_signals(current);
6852 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
6855 (mddev->flags && !mddev->external) ||
6856 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
6857 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
6858 (mddev->external == 0 && mddev->safemode == 1) ||
6859 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6860 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
6864 if (mddev_trylock(mddev)) {
6868 /* Only thing we do on a ro array is remove
6871 remove_and_add_spares(mddev);
6872 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6876 if (!mddev->external) {
6878 spin_lock_irq(&mddev->write_lock);
6879 if (mddev->safemode &&
6880 !atomic_read(&mddev->writes_pending) &&
6882 mddev->recovery_cp == MaxSector) {
6885 if (mddev->persistent)
6886 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6888 if (mddev->safemode == 1)
6889 mddev->safemode = 0;
6890 spin_unlock_irq(&mddev->write_lock);
6892 sysfs_notify_dirent(mddev->sysfs_state);
6896 md_update_sb(mddev, 0);
6898 list_for_each_entry(rdev, &mddev->disks, same_set)
6899 if (test_and_clear_bit(StateChanged, &rdev->flags))
6900 sysfs_notify_dirent(rdev->sysfs_state);
6903 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
6904 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
6905 /* resync/recovery still happening */
6906 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6909 if (mddev->sync_thread) {
6910 /* resync has finished, collect result */
6911 md_unregister_thread(mddev->sync_thread);
6912 mddev->sync_thread = NULL;
6913 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
6914 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
6916 /* activate any spares */
6917 if (mddev->pers->spare_active(mddev))
6918 sysfs_notify(&mddev->kobj, NULL,
6921 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6922 mddev->pers->finish_reshape)
6923 mddev->pers->finish_reshape(mddev);
6924 md_update_sb(mddev, 1);
6926 /* if array is no-longer degraded, then any saved_raid_disk
6927 * information must be scrapped
6929 if (!mddev->degraded)
6930 list_for_each_entry(rdev, &mddev->disks, same_set)
6931 rdev->saved_raid_disk = -1;
6933 mddev->recovery = 0;
6934 /* flag recovery needed just to double check */
6935 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6936 sysfs_notify_dirent(mddev->sysfs_action);
6937 md_new_event(mddev);
6940 /* Set RUNNING before clearing NEEDED to avoid
6941 * any transients in the value of "sync_action".
6943 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6944 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6945 /* Clear some bits that don't mean anything, but
6948 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
6949 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
6951 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
6953 /* no recovery is running.
6954 * remove any failed drives, then
6955 * add spares if possible.
6956 * Spare are also removed and re-added, to allow
6957 * the personality to fail the re-add.
6960 if (mddev->reshape_position != MaxSector) {
6961 if (mddev->pers->check_reshape == NULL ||
6962 mddev->pers->check_reshape(mddev) != 0)
6963 /* Cannot proceed */
6965 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
6966 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
6967 } else if ((spares = remove_and_add_spares(mddev))) {
6968 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6969 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
6970 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
6971 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
6972 } else if (mddev->recovery_cp < MaxSector) {
6973 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6974 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
6975 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
6976 /* nothing to be done ... */
6979 if (mddev->pers->sync_request) {
6980 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
6981 /* We are adding a device or devices to an array
6982 * which has the bitmap stored on all devices.
6983 * So make sure all bitmap pages get written
6985 bitmap_write_all(mddev->bitmap);
6987 mddev->sync_thread = md_register_thread(md_do_sync,
6990 if (!mddev->sync_thread) {
6991 printk(KERN_ERR "%s: could not start resync"
6994 /* leave the spares where they are, it shouldn't hurt */
6995 mddev->recovery = 0;
6997 md_wakeup_thread(mddev->sync_thread);
6998 sysfs_notify_dirent(mddev->sysfs_action);
6999 md_new_event(mddev);
7002 if (!mddev->sync_thread) {
7003 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7004 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7006 if (mddev->sysfs_action)
7007 sysfs_notify_dirent(mddev->sysfs_action);
7009 mddev_unlock(mddev);
7013 void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
7015 sysfs_notify_dirent(rdev->sysfs_state);
7016 wait_event_timeout(rdev->blocked_wait,
7017 !test_bit(Blocked, &rdev->flags),
7018 msecs_to_jiffies(5000));
7019 rdev_dec_pending(rdev, mddev);
7021 EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7023 static int md_notify_reboot(struct notifier_block *this,
7024 unsigned long code, void *x)
7026 struct list_head *tmp;
7029 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
7031 printk(KERN_INFO "md: stopping all md devices.\n");
7033 for_each_mddev(mddev, tmp)
7034 if (mddev_trylock(mddev)) {
7035 /* Force a switch to readonly even array
7036 * appears to still be in use. Hence
7039 do_md_stop(mddev, 1, 100);
7040 mddev_unlock(mddev);
7043 * certain more exotic SCSI devices are known to be
7044 * volatile wrt too early system reboots. While the
7045 * right place to handle this issue is the given
7046 * driver, we do want to have a safe RAID driver ...
7053 static struct notifier_block md_notifier = {
7054 .notifier_call = md_notify_reboot,
7056 .priority = INT_MAX, /* before any real devices */
7059 static void md_geninit(void)
7061 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
7063 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
7066 static int __init md_init(void)
7068 if (register_blkdev(MD_MAJOR, "md"))
7070 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
7071 unregister_blkdev(MD_MAJOR, "md");
7074 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
7075 md_probe, NULL, NULL);
7076 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
7077 md_probe, NULL, NULL);
7079 register_reboot_notifier(&md_notifier);
7080 raid_table_header = register_sysctl_table(raid_root_table);
7090 * Searches all registered partitions for autorun RAID arrays
7094 static LIST_HEAD(all_detected_devices);
7095 struct detected_devices_node {
7096 struct list_head list;
7100 void md_autodetect_dev(dev_t dev)
7102 struct detected_devices_node *node_detected_dev;
7104 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
7105 if (node_detected_dev) {
7106 node_detected_dev->dev = dev;
7107 list_add_tail(&node_detected_dev->list, &all_detected_devices);
7109 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
7110 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
7115 static void autostart_arrays(int part)
7118 struct detected_devices_node *node_detected_dev;
7120 int i_scanned, i_passed;
7125 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
7127 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
7129 node_detected_dev = list_entry(all_detected_devices.next,
7130 struct detected_devices_node, list);
7131 list_del(&node_detected_dev->list);
7132 dev = node_detected_dev->dev;
7133 kfree(node_detected_dev);
7134 rdev = md_import_device(dev,0, 90);
7138 if (test_bit(Faulty, &rdev->flags)) {
7142 set_bit(AutoDetected, &rdev->flags);
7143 list_add(&rdev->same_set, &pending_raid_disks);
7147 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
7148 i_scanned, i_passed);
7150 autorun_devices(part);
7153 #endif /* !MODULE */
7155 static __exit void md_exit(void)
7158 struct list_head *tmp;
7160 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
7161 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
7163 unregister_blkdev(MD_MAJOR,"md");
7164 unregister_blkdev(mdp_major, "mdp");
7165 unregister_reboot_notifier(&md_notifier);
7166 unregister_sysctl_table(raid_table_header);
7167 remove_proc_entry("mdstat", NULL);
7168 for_each_mddev(mddev, tmp) {
7169 export_array(mddev);
7170 mddev->hold_active = 0;
7174 subsys_initcall(md_init);
7175 module_exit(md_exit)
7177 static int get_ro(char *buffer, struct kernel_param *kp)
7179 return sprintf(buffer, "%d", start_readonly);
7181 static int set_ro(const char *val, struct kernel_param *kp)
7184 int num = simple_strtoul(val, &e, 10);
7185 if (*val && (*e == '\0' || *e == '\n')) {
7186 start_readonly = num;
7192 module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
7193 module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
7195 module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
7197 EXPORT_SYMBOL(register_md_personality);
7198 EXPORT_SYMBOL(unregister_md_personality);
7199 EXPORT_SYMBOL(md_error);
7200 EXPORT_SYMBOL(md_done_sync);
7201 EXPORT_SYMBOL(md_write_start);
7202 EXPORT_SYMBOL(md_write_end);
7203 EXPORT_SYMBOL(md_register_thread);
7204 EXPORT_SYMBOL(md_unregister_thread);
7205 EXPORT_SYMBOL(md_wakeup_thread);
7206 EXPORT_SYMBOL(md_check_recovery);
7207 MODULE_LICENSE("GPL");
7208 MODULE_DESCRIPTION("MD RAID framework");
7210 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);