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>
52 #include <linux/slab.h>
57 #define dprintk(x...) ((void)(DEBUG && printk(x)))
61 static void autostart_arrays(int part);
64 static LIST_HEAD(pers_list);
65 static DEFINE_SPINLOCK(pers_lock);
67 static void md_print_devices(void);
69 static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
71 #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
74 * Default number of read corrections we'll attempt on an rdev
75 * before ejecting it from the array. We divide the read error
76 * count by 2 for every hour elapsed between read errors.
78 #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
80 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
81 * is 1000 KB/sec, so the extra system load does not show up that much.
82 * Increase it if you want to have more _guaranteed_ speed. Note that
83 * the RAID driver will use the maximum available bandwidth if the IO
84 * subsystem is idle. There is also an 'absolute maximum' reconstruction
85 * speed limit - in case reconstruction slows down your system despite
88 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
89 * or /sys/block/mdX/md/sync_speed_{min,max}
92 static int sysctl_speed_limit_min = 1000;
93 static int sysctl_speed_limit_max = 200000;
94 static inline int speed_min(mddev_t *mddev)
96 return mddev->sync_speed_min ?
97 mddev->sync_speed_min : sysctl_speed_limit_min;
100 static inline int speed_max(mddev_t *mddev)
102 return mddev->sync_speed_max ?
103 mddev->sync_speed_max : sysctl_speed_limit_max;
106 static struct ctl_table_header *raid_table_header;
108 static ctl_table raid_table[] = {
110 .procname = "speed_limit_min",
111 .data = &sysctl_speed_limit_min,
112 .maxlen = sizeof(int),
113 .mode = S_IRUGO|S_IWUSR,
114 .proc_handler = proc_dointvec,
117 .procname = "speed_limit_max",
118 .data = &sysctl_speed_limit_max,
119 .maxlen = sizeof(int),
120 .mode = S_IRUGO|S_IWUSR,
121 .proc_handler = proc_dointvec,
126 static ctl_table raid_dir_table[] = {
130 .mode = S_IRUGO|S_IXUGO,
136 static ctl_table raid_root_table[] = {
141 .child = raid_dir_table,
146 static const struct block_device_operations md_fops;
148 static int start_readonly;
151 * We have a system wide 'event count' that is incremented
152 * on any 'interesting' event, and readers of /proc/mdstat
153 * can use 'poll' or 'select' to find out when the event
157 * start array, stop array, error, add device, remove device,
158 * start build, activate spare
160 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
161 static atomic_t md_event_count;
162 void md_new_event(mddev_t *mddev)
164 atomic_inc(&md_event_count);
165 wake_up(&md_event_waiters);
167 EXPORT_SYMBOL_GPL(md_new_event);
169 /* Alternate version that can be called from interrupts
170 * when calling sysfs_notify isn't needed.
172 static void md_new_event_inintr(mddev_t *mddev)
174 atomic_inc(&md_event_count);
175 wake_up(&md_event_waiters);
179 * Enables to iterate over all existing md arrays
180 * all_mddevs_lock protects this list.
182 static LIST_HEAD(all_mddevs);
183 static DEFINE_SPINLOCK(all_mddevs_lock);
187 * iterates through all used mddevs in the system.
188 * We take care to grab the all_mddevs_lock whenever navigating
189 * the list, and to always hold a refcount when unlocked.
190 * Any code which breaks out of this loop while own
191 * a reference to the current mddev and must mddev_put it.
193 #define for_each_mddev(mddev,tmp) \
195 for (({ spin_lock(&all_mddevs_lock); \
196 tmp = all_mddevs.next; \
198 ({ if (tmp != &all_mddevs) \
199 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
200 spin_unlock(&all_mddevs_lock); \
201 if (mddev) mddev_put(mddev); \
202 mddev = list_entry(tmp, mddev_t, all_mddevs); \
203 tmp != &all_mddevs;}); \
204 ({ spin_lock(&all_mddevs_lock); \
209 /* Rather than calling directly into the personality make_request function,
210 * IO requests come here first so that we can check if the device is
211 * being suspended pending a reconfiguration.
212 * We hold a refcount over the call to ->make_request. By the time that
213 * call has finished, the bio has been linked into some internal structure
214 * and so is visible to ->quiesce(), so we don't need the refcount any more.
216 static int md_make_request(struct request_queue *q, struct bio *bio)
218 const int rw = bio_data_dir(bio);
219 mddev_t *mddev = q->queuedata;
223 if (mddev == NULL || mddev->pers == NULL) {
228 if (mddev->suspended || mddev->barrier) {
231 prepare_to_wait(&mddev->sb_wait, &__wait,
232 TASK_UNINTERRUPTIBLE);
233 if (!mddev->suspended && !mddev->barrier)
239 finish_wait(&mddev->sb_wait, &__wait);
241 atomic_inc(&mddev->active_io);
244 rv = mddev->pers->make_request(mddev, bio);
246 cpu = part_stat_lock();
247 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
248 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
252 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
253 wake_up(&mddev->sb_wait);
258 /* mddev_suspend makes sure no new requests are submitted
259 * to the device, and that any requests that have been submitted
260 * are completely handled.
261 * Once ->stop is called and completes, the module will be completely
264 static void mddev_suspend(mddev_t *mddev)
266 BUG_ON(mddev->suspended);
267 mddev->suspended = 1;
269 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
270 mddev->pers->quiesce(mddev, 1);
273 static void mddev_resume(mddev_t *mddev)
275 mddev->suspended = 0;
276 wake_up(&mddev->sb_wait);
277 mddev->pers->quiesce(mddev, 0);
280 int mddev_congested(mddev_t *mddev, int bits)
284 return mddev->suspended;
286 EXPORT_SYMBOL(mddev_congested);
289 * Generic barrier handling for md
292 #define POST_REQUEST_BARRIER ((void*)1)
294 static void md_end_barrier(struct bio *bio, int err)
296 mdk_rdev_t *rdev = bio->bi_private;
297 mddev_t *mddev = rdev->mddev;
298 if (err == -EOPNOTSUPP && mddev->barrier != POST_REQUEST_BARRIER)
299 set_bit(BIO_EOPNOTSUPP, &mddev->barrier->bi_flags);
301 rdev_dec_pending(rdev, mddev);
303 if (atomic_dec_and_test(&mddev->flush_pending)) {
304 if (mddev->barrier == POST_REQUEST_BARRIER) {
305 /* This was a post-request barrier */
306 mddev->barrier = NULL;
307 wake_up(&mddev->sb_wait);
309 /* The pre-request barrier has finished */
310 schedule_work(&mddev->barrier_work);
315 static void submit_barriers(mddev_t *mddev)
320 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
321 if (rdev->raid_disk >= 0 &&
322 !test_bit(Faulty, &rdev->flags)) {
323 /* Take two references, one is dropped
324 * when request finishes, one after
325 * we reclaim rcu_read_lock
328 atomic_inc(&rdev->nr_pending);
329 atomic_inc(&rdev->nr_pending);
331 bi = bio_alloc(GFP_KERNEL, 0);
332 bi->bi_end_io = md_end_barrier;
333 bi->bi_private = rdev;
334 bi->bi_bdev = rdev->bdev;
335 atomic_inc(&mddev->flush_pending);
336 submit_bio(WRITE_BARRIER, bi);
338 rdev_dec_pending(rdev, mddev);
343 static void md_submit_barrier(struct work_struct *ws)
345 mddev_t *mddev = container_of(ws, mddev_t, barrier_work);
346 struct bio *bio = mddev->barrier;
348 atomic_set(&mddev->flush_pending, 1);
350 if (test_bit(BIO_EOPNOTSUPP, &bio->bi_flags))
351 bio_endio(bio, -EOPNOTSUPP);
352 else if (bio->bi_size == 0)
353 /* an empty barrier - all done */
356 bio->bi_rw &= ~(1<<BIO_RW_BARRIER);
357 if (mddev->pers->make_request(mddev, bio))
358 generic_make_request(bio);
359 mddev->barrier = POST_REQUEST_BARRIER;
360 submit_barriers(mddev);
362 if (atomic_dec_and_test(&mddev->flush_pending)) {
363 mddev->barrier = NULL;
364 wake_up(&mddev->sb_wait);
368 void md_barrier_request(mddev_t *mddev, struct bio *bio)
370 spin_lock_irq(&mddev->write_lock);
371 wait_event_lock_irq(mddev->sb_wait,
373 mddev->write_lock, /*nothing*/);
374 mddev->barrier = bio;
375 spin_unlock_irq(&mddev->write_lock);
377 atomic_set(&mddev->flush_pending, 1);
378 INIT_WORK(&mddev->barrier_work, md_submit_barrier);
380 submit_barriers(mddev);
382 if (atomic_dec_and_test(&mddev->flush_pending))
383 schedule_work(&mddev->barrier_work);
385 EXPORT_SYMBOL(md_barrier_request);
387 static inline mddev_t *mddev_get(mddev_t *mddev)
389 atomic_inc(&mddev->active);
393 static void mddev_delayed_delete(struct work_struct *ws);
395 static void mddev_put(mddev_t *mddev)
397 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
399 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
400 mddev->ctime == 0 && !mddev->hold_active) {
401 /* Array is not configured at all, and not held active,
403 list_del(&mddev->all_mddevs);
404 if (mddev->gendisk) {
405 /* we did a probe so need to clean up.
406 * Call schedule_work inside the spinlock
407 * so that flush_scheduled_work() after
408 * mddev_find will succeed in waiting for the
411 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
412 schedule_work(&mddev->del_work);
416 spin_unlock(&all_mddevs_lock);
419 static void mddev_init(mddev_t *mddev)
421 mutex_init(&mddev->open_mutex);
422 mutex_init(&mddev->reconfig_mutex);
423 mutex_init(&mddev->bitmap_info.mutex);
424 INIT_LIST_HEAD(&mddev->disks);
425 INIT_LIST_HEAD(&mddev->all_mddevs);
426 init_timer(&mddev->safemode_timer);
427 atomic_set(&mddev->active, 1);
428 atomic_set(&mddev->openers, 0);
429 atomic_set(&mddev->active_io, 0);
430 spin_lock_init(&mddev->write_lock);
431 atomic_set(&mddev->flush_pending, 0);
432 init_waitqueue_head(&mddev->sb_wait);
433 init_waitqueue_head(&mddev->recovery_wait);
434 mddev->reshape_position = MaxSector;
435 mddev->resync_min = 0;
436 mddev->resync_max = MaxSector;
437 mddev->level = LEVEL_NONE;
440 static mddev_t * mddev_find(dev_t unit)
442 mddev_t *mddev, *new = NULL;
445 spin_lock(&all_mddevs_lock);
448 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
449 if (mddev->unit == unit) {
451 spin_unlock(&all_mddevs_lock);
457 list_add(&new->all_mddevs, &all_mddevs);
458 spin_unlock(&all_mddevs_lock);
459 new->hold_active = UNTIL_IOCTL;
463 /* find an unused unit number */
464 static int next_minor = 512;
465 int start = next_minor;
469 dev = MKDEV(MD_MAJOR, next_minor);
471 if (next_minor > MINORMASK)
473 if (next_minor == start) {
474 /* Oh dear, all in use. */
475 spin_unlock(&all_mddevs_lock);
481 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
482 if (mddev->unit == dev) {
488 new->md_minor = MINOR(dev);
489 new->hold_active = UNTIL_STOP;
490 list_add(&new->all_mddevs, &all_mddevs);
491 spin_unlock(&all_mddevs_lock);
494 spin_unlock(&all_mddevs_lock);
496 new = kzalloc(sizeof(*new), GFP_KERNEL);
501 if (MAJOR(unit) == MD_MAJOR)
502 new->md_minor = MINOR(unit);
504 new->md_minor = MINOR(unit) >> MdpMinorShift;
511 static inline int mddev_lock(mddev_t * mddev)
513 return mutex_lock_interruptible(&mddev->reconfig_mutex);
516 static inline int mddev_is_locked(mddev_t *mddev)
518 return mutex_is_locked(&mddev->reconfig_mutex);
521 static inline int mddev_trylock(mddev_t * mddev)
523 return mutex_trylock(&mddev->reconfig_mutex);
526 static struct attribute_group md_redundancy_group;
528 static void mddev_unlock(mddev_t * mddev)
530 if (mddev->to_remove) {
531 /* These cannot be removed under reconfig_mutex as
532 * an access to the files will try to take reconfig_mutex
533 * while holding the file unremovable, which leads to
535 * So hold open_mutex instead - we are allowed to take
536 * it while holding reconfig_mutex, and md_run can
537 * use it to wait for the remove to complete.
539 struct attribute_group *to_remove = mddev->to_remove;
540 mddev->to_remove = NULL;
541 mutex_lock(&mddev->open_mutex);
542 mutex_unlock(&mddev->reconfig_mutex);
544 if (to_remove != &md_redundancy_group)
545 sysfs_remove_group(&mddev->kobj, to_remove);
546 if (mddev->pers == NULL ||
547 mddev->pers->sync_request == NULL) {
548 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
549 if (mddev->sysfs_action)
550 sysfs_put(mddev->sysfs_action);
551 mddev->sysfs_action = NULL;
553 mutex_unlock(&mddev->open_mutex);
555 mutex_unlock(&mddev->reconfig_mutex);
557 md_wakeup_thread(mddev->thread);
560 static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
564 list_for_each_entry(rdev, &mddev->disks, same_set)
565 if (rdev->desc_nr == nr)
571 static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
575 list_for_each_entry(rdev, &mddev->disks, same_set)
576 if (rdev->bdev->bd_dev == dev)
582 static struct mdk_personality *find_pers(int level, char *clevel)
584 struct mdk_personality *pers;
585 list_for_each_entry(pers, &pers_list, list) {
586 if (level != LEVEL_NONE && pers->level == level)
588 if (strcmp(pers->name, clevel)==0)
594 /* return the offset of the super block in 512byte sectors */
595 static inline sector_t calc_dev_sboffset(struct block_device *bdev)
597 sector_t num_sectors = bdev->bd_inode->i_size / 512;
598 return MD_NEW_SIZE_SECTORS(num_sectors);
601 static int alloc_disk_sb(mdk_rdev_t * rdev)
606 rdev->sb_page = alloc_page(GFP_KERNEL);
607 if (!rdev->sb_page) {
608 printk(KERN_ALERT "md: out of memory.\n");
615 static void free_disk_sb(mdk_rdev_t * rdev)
618 put_page(rdev->sb_page);
620 rdev->sb_page = NULL;
627 static void super_written(struct bio *bio, int error)
629 mdk_rdev_t *rdev = bio->bi_private;
630 mddev_t *mddev = rdev->mddev;
632 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
633 printk("md: super_written gets error=%d, uptodate=%d\n",
634 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
635 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
636 md_error(mddev, rdev);
639 if (atomic_dec_and_test(&mddev->pending_writes))
640 wake_up(&mddev->sb_wait);
644 static void super_written_barrier(struct bio *bio, int error)
646 struct bio *bio2 = bio->bi_private;
647 mdk_rdev_t *rdev = bio2->bi_private;
648 mddev_t *mddev = rdev->mddev;
650 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
651 error == -EOPNOTSUPP) {
653 /* barriers don't appear to be supported :-( */
654 set_bit(BarriersNotsupp, &rdev->flags);
655 mddev->barriers_work = 0;
656 spin_lock_irqsave(&mddev->write_lock, flags);
657 bio2->bi_next = mddev->biolist;
658 mddev->biolist = bio2;
659 spin_unlock_irqrestore(&mddev->write_lock, flags);
660 wake_up(&mddev->sb_wait);
664 bio->bi_private = rdev;
665 super_written(bio, error);
669 void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
670 sector_t sector, int size, struct page *page)
672 /* write first size bytes of page to sector of rdev
673 * Increment mddev->pending_writes before returning
674 * and decrement it on completion, waking up sb_wait
675 * if zero is reached.
676 * If an error occurred, call md_error
678 * As we might need to resubmit the request if BIO_RW_BARRIER
679 * causes ENOTSUPP, we allocate a spare bio...
681 struct bio *bio = bio_alloc(GFP_NOIO, 1);
682 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNCIO) | (1<<BIO_RW_UNPLUG);
684 bio->bi_bdev = rdev->bdev;
685 bio->bi_sector = sector;
686 bio_add_page(bio, page, size, 0);
687 bio->bi_private = rdev;
688 bio->bi_end_io = super_written;
691 atomic_inc(&mddev->pending_writes);
692 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
694 rw |= (1<<BIO_RW_BARRIER);
695 rbio = bio_clone(bio, GFP_NOIO);
696 rbio->bi_private = bio;
697 rbio->bi_end_io = super_written_barrier;
698 submit_bio(rw, rbio);
703 void md_super_wait(mddev_t *mddev)
705 /* wait for all superblock writes that were scheduled to complete.
706 * if any had to be retried (due to BARRIER problems), retry them
710 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
711 if (atomic_read(&mddev->pending_writes)==0)
713 while (mddev->biolist) {
715 spin_lock_irq(&mddev->write_lock);
716 bio = mddev->biolist;
717 mddev->biolist = bio->bi_next ;
719 spin_unlock_irq(&mddev->write_lock);
720 submit_bio(bio->bi_rw, bio);
724 finish_wait(&mddev->sb_wait, &wq);
727 static void bi_complete(struct bio *bio, int error)
729 complete((struct completion*)bio->bi_private);
732 int sync_page_io(struct block_device *bdev, sector_t sector, int size,
733 struct page *page, int rw)
735 struct bio *bio = bio_alloc(GFP_NOIO, 1);
736 struct completion event;
739 rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG);
742 bio->bi_sector = sector;
743 bio_add_page(bio, page, size, 0);
744 init_completion(&event);
745 bio->bi_private = &event;
746 bio->bi_end_io = bi_complete;
748 wait_for_completion(&event);
750 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
754 EXPORT_SYMBOL_GPL(sync_page_io);
756 static int read_disk_sb(mdk_rdev_t * rdev, int size)
758 char b[BDEVNAME_SIZE];
759 if (!rdev->sb_page) {
767 if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ))
773 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
774 bdevname(rdev->bdev,b));
778 static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
780 return sb1->set_uuid0 == sb2->set_uuid0 &&
781 sb1->set_uuid1 == sb2->set_uuid1 &&
782 sb1->set_uuid2 == sb2->set_uuid2 &&
783 sb1->set_uuid3 == sb2->set_uuid3;
786 static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
789 mdp_super_t *tmp1, *tmp2;
791 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
792 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
794 if (!tmp1 || !tmp2) {
796 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
804 * nr_disks is not constant
809 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
817 static u32 md_csum_fold(u32 csum)
819 csum = (csum & 0xffff) + (csum >> 16);
820 return (csum & 0xffff) + (csum >> 16);
823 static unsigned int calc_sb_csum(mdp_super_t * sb)
826 u32 *sb32 = (u32*)sb;
828 unsigned int disk_csum, csum;
830 disk_csum = sb->sb_csum;
833 for (i = 0; i < MD_SB_BYTES/4 ; i++)
835 csum = (newcsum & 0xffffffff) + (newcsum>>32);
839 /* This used to use csum_partial, which was wrong for several
840 * reasons including that different results are returned on
841 * different architectures. It isn't critical that we get exactly
842 * the same return value as before (we always csum_fold before
843 * testing, and that removes any differences). However as we
844 * know that csum_partial always returned a 16bit value on
845 * alphas, do a fold to maximise conformity to previous behaviour.
847 sb->sb_csum = md_csum_fold(disk_csum);
849 sb->sb_csum = disk_csum;
856 * Handle superblock details.
857 * We want to be able to handle multiple superblock formats
858 * so we have a common interface to them all, and an array of
859 * different handlers.
860 * We rely on user-space to write the initial superblock, and support
861 * reading and updating of superblocks.
862 * Interface methods are:
863 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
864 * loads and validates a superblock on dev.
865 * if refdev != NULL, compare superblocks on both devices
867 * 0 - dev has a superblock that is compatible with refdev
868 * 1 - dev has a superblock that is compatible and newer than refdev
869 * so dev should be used as the refdev in future
870 * -EINVAL superblock incompatible or invalid
871 * -othererror e.g. -EIO
873 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
874 * Verify that dev is acceptable into mddev.
875 * The first time, mddev->raid_disks will be 0, and data from
876 * dev should be merged in. Subsequent calls check that dev
877 * is new enough. Return 0 or -EINVAL
879 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
880 * Update the superblock for rdev with data in mddev
881 * This does not write to disc.
887 struct module *owner;
888 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
890 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
891 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
892 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
893 sector_t num_sectors);
897 * Check that the given mddev has no bitmap.
899 * This function is called from the run method of all personalities that do not
900 * support bitmaps. It prints an error message and returns non-zero if mddev
901 * has a bitmap. Otherwise, it returns 0.
904 int md_check_no_bitmap(mddev_t *mddev)
906 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
908 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
909 mdname(mddev), mddev->pers->name);
912 EXPORT_SYMBOL(md_check_no_bitmap);
915 * load_super for 0.90.0
917 static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
919 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
924 * Calculate the position of the superblock (512byte sectors),
925 * it's at the end of the disk.
927 * It also happens to be a multiple of 4Kb.
929 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
931 ret = read_disk_sb(rdev, MD_SB_BYTES);
936 bdevname(rdev->bdev, b);
937 sb = (mdp_super_t*)page_address(rdev->sb_page);
939 if (sb->md_magic != MD_SB_MAGIC) {
940 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
945 if (sb->major_version != 0 ||
946 sb->minor_version < 90 ||
947 sb->minor_version > 91) {
948 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
949 sb->major_version, sb->minor_version,
954 if (sb->raid_disks <= 0)
957 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
958 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
963 rdev->preferred_minor = sb->md_minor;
964 rdev->data_offset = 0;
965 rdev->sb_size = MD_SB_BYTES;
967 if (sb->level == LEVEL_MULTIPATH)
970 rdev->desc_nr = sb->this_disk.number;
976 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
977 if (!uuid_equal(refsb, sb)) {
978 printk(KERN_WARNING "md: %s has different UUID to %s\n",
979 b, bdevname(refdev->bdev,b2));
982 if (!sb_equal(refsb, sb)) {
983 printk(KERN_WARNING "md: %s has same UUID"
984 " but different superblock to %s\n",
985 b, bdevname(refdev->bdev, b2));
989 ev2 = md_event(refsb);
995 rdev->sectors = rdev->sb_start;
997 if (rdev->sectors < sb->size * 2 && sb->level > 1)
998 /* "this cannot possibly happen" ... */
1006 * validate_super for 0.90.0
1008 static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1011 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
1012 __u64 ev1 = md_event(sb);
1014 rdev->raid_disk = -1;
1015 clear_bit(Faulty, &rdev->flags);
1016 clear_bit(In_sync, &rdev->flags);
1017 clear_bit(WriteMostly, &rdev->flags);
1018 clear_bit(BarriersNotsupp, &rdev->flags);
1020 if (mddev->raid_disks == 0) {
1021 mddev->major_version = 0;
1022 mddev->minor_version = sb->minor_version;
1023 mddev->patch_version = sb->patch_version;
1024 mddev->external = 0;
1025 mddev->chunk_sectors = sb->chunk_size >> 9;
1026 mddev->ctime = sb->ctime;
1027 mddev->utime = sb->utime;
1028 mddev->level = sb->level;
1029 mddev->clevel[0] = 0;
1030 mddev->layout = sb->layout;
1031 mddev->raid_disks = sb->raid_disks;
1032 mddev->dev_sectors = sb->size * 2;
1033 mddev->events = ev1;
1034 mddev->bitmap_info.offset = 0;
1035 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
1037 if (mddev->minor_version >= 91) {
1038 mddev->reshape_position = sb->reshape_position;
1039 mddev->delta_disks = sb->delta_disks;
1040 mddev->new_level = sb->new_level;
1041 mddev->new_layout = sb->new_layout;
1042 mddev->new_chunk_sectors = sb->new_chunk >> 9;
1044 mddev->reshape_position = MaxSector;
1045 mddev->delta_disks = 0;
1046 mddev->new_level = mddev->level;
1047 mddev->new_layout = mddev->layout;
1048 mddev->new_chunk_sectors = mddev->chunk_sectors;
1051 if (sb->state & (1<<MD_SB_CLEAN))
1052 mddev->recovery_cp = MaxSector;
1054 if (sb->events_hi == sb->cp_events_hi &&
1055 sb->events_lo == sb->cp_events_lo) {
1056 mddev->recovery_cp = sb->recovery_cp;
1058 mddev->recovery_cp = 0;
1061 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1062 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1063 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1064 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1066 mddev->max_disks = MD_SB_DISKS;
1068 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
1069 mddev->bitmap_info.file == NULL)
1070 mddev->bitmap_info.offset =
1071 mddev->bitmap_info.default_offset;
1073 } else if (mddev->pers == NULL) {
1074 /* Insist on good event counter while assembling, except
1075 * for spares (which don't need an event count) */
1077 if (sb->disks[rdev->desc_nr].state & (
1078 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1079 if (ev1 < mddev->events)
1081 } else if (mddev->bitmap) {
1082 /* if adding to array with a bitmap, then we can accept an
1083 * older device ... but not too old.
1085 if (ev1 < mddev->bitmap->events_cleared)
1088 if (ev1 < mddev->events)
1089 /* just a hot-add of a new device, leave raid_disk at -1 */
1093 if (mddev->level != LEVEL_MULTIPATH) {
1094 desc = sb->disks + rdev->desc_nr;
1096 if (desc->state & (1<<MD_DISK_FAULTY))
1097 set_bit(Faulty, &rdev->flags);
1098 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1099 desc->raid_disk < mddev->raid_disks */) {
1100 set_bit(In_sync, &rdev->flags);
1101 rdev->raid_disk = desc->raid_disk;
1102 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1103 /* active but not in sync implies recovery up to
1104 * reshape position. We don't know exactly where
1105 * that is, so set to zero for now */
1106 if (mddev->minor_version >= 91) {
1107 rdev->recovery_offset = 0;
1108 rdev->raid_disk = desc->raid_disk;
1111 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1112 set_bit(WriteMostly, &rdev->flags);
1113 } else /* MULTIPATH are always insync */
1114 set_bit(In_sync, &rdev->flags);
1119 * sync_super for 0.90.0
1121 static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1125 int next_spare = mddev->raid_disks;
1128 /* make rdev->sb match mddev data..
1131 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1132 * 3/ any empty disks < next_spare become removed
1134 * disks[0] gets initialised to REMOVED because
1135 * we cannot be sure from other fields if it has
1136 * been initialised or not.
1139 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1141 rdev->sb_size = MD_SB_BYTES;
1143 sb = (mdp_super_t*)page_address(rdev->sb_page);
1145 memset(sb, 0, sizeof(*sb));
1147 sb->md_magic = MD_SB_MAGIC;
1148 sb->major_version = mddev->major_version;
1149 sb->patch_version = mddev->patch_version;
1150 sb->gvalid_words = 0; /* ignored */
1151 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1152 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1153 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1154 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1156 sb->ctime = mddev->ctime;
1157 sb->level = mddev->level;
1158 sb->size = mddev->dev_sectors / 2;
1159 sb->raid_disks = mddev->raid_disks;
1160 sb->md_minor = mddev->md_minor;
1161 sb->not_persistent = 0;
1162 sb->utime = mddev->utime;
1164 sb->events_hi = (mddev->events>>32);
1165 sb->events_lo = (u32)mddev->events;
1167 if (mddev->reshape_position == MaxSector)
1168 sb->minor_version = 90;
1170 sb->minor_version = 91;
1171 sb->reshape_position = mddev->reshape_position;
1172 sb->new_level = mddev->new_level;
1173 sb->delta_disks = mddev->delta_disks;
1174 sb->new_layout = mddev->new_layout;
1175 sb->new_chunk = mddev->new_chunk_sectors << 9;
1177 mddev->minor_version = sb->minor_version;
1180 sb->recovery_cp = mddev->recovery_cp;
1181 sb->cp_events_hi = (mddev->events>>32);
1182 sb->cp_events_lo = (u32)mddev->events;
1183 if (mddev->recovery_cp == MaxSector)
1184 sb->state = (1<< MD_SB_CLEAN);
1186 sb->recovery_cp = 0;
1188 sb->layout = mddev->layout;
1189 sb->chunk_size = mddev->chunk_sectors << 9;
1191 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
1192 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1194 sb->disks[0].state = (1<<MD_DISK_REMOVED);
1195 list_for_each_entry(rdev2, &mddev->disks, same_set) {
1198 int is_active = test_bit(In_sync, &rdev2->flags);
1200 if (rdev2->raid_disk >= 0 &&
1201 sb->minor_version >= 91)
1202 /* we have nowhere to store the recovery_offset,
1203 * but if it is not below the reshape_position,
1204 * we can piggy-back on that.
1207 if (rdev2->raid_disk < 0 ||
1208 test_bit(Faulty, &rdev2->flags))
1211 desc_nr = rdev2->raid_disk;
1213 desc_nr = next_spare++;
1214 rdev2->desc_nr = desc_nr;
1215 d = &sb->disks[rdev2->desc_nr];
1217 d->number = rdev2->desc_nr;
1218 d->major = MAJOR(rdev2->bdev->bd_dev);
1219 d->minor = MINOR(rdev2->bdev->bd_dev);
1221 d->raid_disk = rdev2->raid_disk;
1223 d->raid_disk = rdev2->desc_nr; /* compatibility */
1224 if (test_bit(Faulty, &rdev2->flags))
1225 d->state = (1<<MD_DISK_FAULTY);
1226 else if (is_active) {
1227 d->state = (1<<MD_DISK_ACTIVE);
1228 if (test_bit(In_sync, &rdev2->flags))
1229 d->state |= (1<<MD_DISK_SYNC);
1237 if (test_bit(WriteMostly, &rdev2->flags))
1238 d->state |= (1<<MD_DISK_WRITEMOSTLY);
1240 /* now set the "removed" and "faulty" bits on any missing devices */
1241 for (i=0 ; i < mddev->raid_disks ; i++) {
1242 mdp_disk_t *d = &sb->disks[i];
1243 if (d->state == 0 && d->number == 0) {
1246 d->state = (1<<MD_DISK_REMOVED);
1247 d->state |= (1<<MD_DISK_FAULTY);
1251 sb->nr_disks = nr_disks;
1252 sb->active_disks = active;
1253 sb->working_disks = working;
1254 sb->failed_disks = failed;
1255 sb->spare_disks = spare;
1257 sb->this_disk = sb->disks[rdev->desc_nr];
1258 sb->sb_csum = calc_sb_csum(sb);
1262 * rdev_size_change for 0.90.0
1264 static unsigned long long
1265 super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
1267 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1268 return 0; /* component must fit device */
1269 if (rdev->mddev->bitmap_info.offset)
1270 return 0; /* can't move bitmap */
1271 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
1272 if (!num_sectors || num_sectors > rdev->sb_start)
1273 num_sectors = rdev->sb_start;
1274 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1276 md_super_wait(rdev->mddev);
1277 return num_sectors / 2; /* kB for sysfs */
1282 * version 1 superblock
1285 static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
1289 unsigned long long newcsum;
1290 int size = 256 + le32_to_cpu(sb->max_dev)*2;
1291 __le32 *isuper = (__le32*)sb;
1294 disk_csum = sb->sb_csum;
1297 for (i=0; size>=4; size -= 4 )
1298 newcsum += le32_to_cpu(*isuper++);
1301 newcsum += le16_to_cpu(*(__le16*) isuper);
1303 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1304 sb->sb_csum = disk_csum;
1305 return cpu_to_le32(csum);
1308 static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1310 struct mdp_superblock_1 *sb;
1313 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1317 * Calculate the position of the superblock in 512byte sectors.
1318 * It is always aligned to a 4K boundary and
1319 * depeding on minor_version, it can be:
1320 * 0: At least 8K, but less than 12K, from end of device
1321 * 1: At start of device
1322 * 2: 4K from start of device.
1324 switch(minor_version) {
1326 sb_start = rdev->bdev->bd_inode->i_size >> 9;
1328 sb_start &= ~(sector_t)(4*2-1);
1339 rdev->sb_start = sb_start;
1341 /* superblock is rarely larger than 1K, but it can be larger,
1342 * and it is safe to read 4k, so we do that
1344 ret = read_disk_sb(rdev, 4096);
1345 if (ret) return ret;
1348 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1350 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1351 sb->major_version != cpu_to_le32(1) ||
1352 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1353 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
1354 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
1357 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1358 printk("md: invalid superblock checksum on %s\n",
1359 bdevname(rdev->bdev,b));
1362 if (le64_to_cpu(sb->data_size) < 10) {
1363 printk("md: data_size too small on %s\n",
1364 bdevname(rdev->bdev,b));
1368 rdev->preferred_minor = 0xffff;
1369 rdev->data_offset = le64_to_cpu(sb->data_offset);
1370 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
1372 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
1373 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1374 if (rdev->sb_size & bmask)
1375 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1378 && rdev->data_offset < sb_start + (rdev->sb_size/512))
1381 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1384 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1390 struct mdp_superblock_1 *refsb =
1391 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1393 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1394 sb->level != refsb->level ||
1395 sb->layout != refsb->layout ||
1396 sb->chunksize != refsb->chunksize) {
1397 printk(KERN_WARNING "md: %s has strangely different"
1398 " superblock to %s\n",
1399 bdevname(rdev->bdev,b),
1400 bdevname(refdev->bdev,b2));
1403 ev1 = le64_to_cpu(sb->events);
1404 ev2 = le64_to_cpu(refsb->events);
1412 rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) -
1413 le64_to_cpu(sb->data_offset);
1415 rdev->sectors = rdev->sb_start;
1416 if (rdev->sectors < le64_to_cpu(sb->data_size))
1418 rdev->sectors = le64_to_cpu(sb->data_size);
1419 if (le64_to_cpu(sb->size) > rdev->sectors)
1424 static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1426 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1427 __u64 ev1 = le64_to_cpu(sb->events);
1429 rdev->raid_disk = -1;
1430 clear_bit(Faulty, &rdev->flags);
1431 clear_bit(In_sync, &rdev->flags);
1432 clear_bit(WriteMostly, &rdev->flags);
1433 clear_bit(BarriersNotsupp, &rdev->flags);
1435 if (mddev->raid_disks == 0) {
1436 mddev->major_version = 1;
1437 mddev->patch_version = 0;
1438 mddev->external = 0;
1439 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
1440 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1441 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1442 mddev->level = le32_to_cpu(sb->level);
1443 mddev->clevel[0] = 0;
1444 mddev->layout = le32_to_cpu(sb->layout);
1445 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1446 mddev->dev_sectors = le64_to_cpu(sb->size);
1447 mddev->events = ev1;
1448 mddev->bitmap_info.offset = 0;
1449 mddev->bitmap_info.default_offset = 1024 >> 9;
1451 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1452 memcpy(mddev->uuid, sb->set_uuid, 16);
1454 mddev->max_disks = (4096-256)/2;
1456 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
1457 mddev->bitmap_info.file == NULL )
1458 mddev->bitmap_info.offset =
1459 (__s32)le32_to_cpu(sb->bitmap_offset);
1461 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1462 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1463 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1464 mddev->new_level = le32_to_cpu(sb->new_level);
1465 mddev->new_layout = le32_to_cpu(sb->new_layout);
1466 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
1468 mddev->reshape_position = MaxSector;
1469 mddev->delta_disks = 0;
1470 mddev->new_level = mddev->level;
1471 mddev->new_layout = mddev->layout;
1472 mddev->new_chunk_sectors = mddev->chunk_sectors;
1475 } else if (mddev->pers == NULL) {
1476 /* Insist of good event counter while assembling, except for
1477 * spares (which don't need an event count) */
1479 if (rdev->desc_nr >= 0 &&
1480 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1481 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1482 if (ev1 < mddev->events)
1484 } else if (mddev->bitmap) {
1485 /* If adding to array with a bitmap, then we can accept an
1486 * older device, but not too old.
1488 if (ev1 < mddev->bitmap->events_cleared)
1491 if (ev1 < mddev->events)
1492 /* just a hot-add of a new device, leave raid_disk at -1 */
1495 if (mddev->level != LEVEL_MULTIPATH) {
1497 if (rdev->desc_nr < 0 ||
1498 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1502 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1504 case 0xffff: /* spare */
1506 case 0xfffe: /* faulty */
1507 set_bit(Faulty, &rdev->flags);
1510 if ((le32_to_cpu(sb->feature_map) &
1511 MD_FEATURE_RECOVERY_OFFSET))
1512 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1514 set_bit(In_sync, &rdev->flags);
1515 rdev->raid_disk = role;
1518 if (sb->devflags & WriteMostly1)
1519 set_bit(WriteMostly, &rdev->flags);
1520 } else /* MULTIPATH are always insync */
1521 set_bit(In_sync, &rdev->flags);
1526 static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1528 struct mdp_superblock_1 *sb;
1531 /* make rdev->sb match mddev and rdev data. */
1533 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1535 sb->feature_map = 0;
1537 sb->recovery_offset = cpu_to_le64(0);
1538 memset(sb->pad1, 0, sizeof(sb->pad1));
1539 memset(sb->pad2, 0, sizeof(sb->pad2));
1540 memset(sb->pad3, 0, sizeof(sb->pad3));
1542 sb->utime = cpu_to_le64((__u64)mddev->utime);
1543 sb->events = cpu_to_le64(mddev->events);
1545 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1547 sb->resync_offset = cpu_to_le64(0);
1549 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
1551 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
1552 sb->size = cpu_to_le64(mddev->dev_sectors);
1553 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
1554 sb->level = cpu_to_le32(mddev->level);
1555 sb->layout = cpu_to_le32(mddev->layout);
1557 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1558 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
1559 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
1562 if (rdev->raid_disk >= 0 &&
1563 !test_bit(In_sync, &rdev->flags)) {
1565 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1566 sb->recovery_offset =
1567 cpu_to_le64(rdev->recovery_offset);
1570 if (mddev->reshape_position != MaxSector) {
1571 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1572 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1573 sb->new_layout = cpu_to_le32(mddev->new_layout);
1574 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1575 sb->new_level = cpu_to_le32(mddev->new_level);
1576 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
1580 list_for_each_entry(rdev2, &mddev->disks, same_set)
1581 if (rdev2->desc_nr+1 > max_dev)
1582 max_dev = rdev2->desc_nr+1;
1584 if (max_dev > le32_to_cpu(sb->max_dev)) {
1586 sb->max_dev = cpu_to_le32(max_dev);
1587 rdev->sb_size = max_dev * 2 + 256;
1588 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1589 if (rdev->sb_size & bmask)
1590 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1592 for (i=0; i<max_dev;i++)
1593 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1595 list_for_each_entry(rdev2, &mddev->disks, same_set) {
1597 if (test_bit(Faulty, &rdev2->flags))
1598 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1599 else if (test_bit(In_sync, &rdev2->flags))
1600 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1601 else if (rdev2->raid_disk >= 0)
1602 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1604 sb->dev_roles[i] = cpu_to_le16(0xffff);
1607 sb->sb_csum = calc_sb_1_csum(sb);
1610 static unsigned long long
1611 super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
1613 struct mdp_superblock_1 *sb;
1614 sector_t max_sectors;
1615 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1616 return 0; /* component must fit device */
1617 if (rdev->sb_start < rdev->data_offset) {
1618 /* minor versions 1 and 2; superblock before data */
1619 max_sectors = rdev->bdev->bd_inode->i_size >> 9;
1620 max_sectors -= rdev->data_offset;
1621 if (!num_sectors || num_sectors > max_sectors)
1622 num_sectors = max_sectors;
1623 } else if (rdev->mddev->bitmap_info.offset) {
1624 /* minor version 0 with bitmap we can't move */
1627 /* minor version 0; superblock after data */
1629 sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1630 sb_start &= ~(sector_t)(4*2 - 1);
1631 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
1632 if (!num_sectors || num_sectors > max_sectors)
1633 num_sectors = max_sectors;
1634 rdev->sb_start = sb_start;
1636 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
1637 sb->data_size = cpu_to_le64(num_sectors);
1638 sb->super_offset = rdev->sb_start;
1639 sb->sb_csum = calc_sb_1_csum(sb);
1640 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1642 md_super_wait(rdev->mddev);
1643 return num_sectors / 2; /* kB for sysfs */
1646 static struct super_type super_types[] = {
1649 .owner = THIS_MODULE,
1650 .load_super = super_90_load,
1651 .validate_super = super_90_validate,
1652 .sync_super = super_90_sync,
1653 .rdev_size_change = super_90_rdev_size_change,
1657 .owner = THIS_MODULE,
1658 .load_super = super_1_load,
1659 .validate_super = super_1_validate,
1660 .sync_super = super_1_sync,
1661 .rdev_size_change = super_1_rdev_size_change,
1665 static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1667 mdk_rdev_t *rdev, *rdev2;
1670 rdev_for_each_rcu(rdev, mddev1)
1671 rdev_for_each_rcu(rdev2, mddev2)
1672 if (rdev->bdev->bd_contains ==
1673 rdev2->bdev->bd_contains) {
1681 static LIST_HEAD(pending_raid_disks);
1684 * Try to register data integrity profile for an mddev
1686 * This is called when an array is started and after a disk has been kicked
1687 * from the array. It only succeeds if all working and active component devices
1688 * are integrity capable with matching profiles.
1690 int md_integrity_register(mddev_t *mddev)
1692 mdk_rdev_t *rdev, *reference = NULL;
1694 if (list_empty(&mddev->disks))
1695 return 0; /* nothing to do */
1696 if (blk_get_integrity(mddev->gendisk))
1697 return 0; /* already registered */
1698 list_for_each_entry(rdev, &mddev->disks, same_set) {
1699 /* skip spares and non-functional disks */
1700 if (test_bit(Faulty, &rdev->flags))
1702 if (rdev->raid_disk < 0)
1705 * If at least one rdev is not integrity capable, we can not
1706 * enable data integrity for the md device.
1708 if (!bdev_get_integrity(rdev->bdev))
1711 /* Use the first rdev as the reference */
1715 /* does this rdev's profile match the reference profile? */
1716 if (blk_integrity_compare(reference->bdev->bd_disk,
1717 rdev->bdev->bd_disk) < 0)
1721 * All component devices are integrity capable and have matching
1722 * profiles, register the common profile for the md device.
1724 if (blk_integrity_register(mddev->gendisk,
1725 bdev_get_integrity(reference->bdev)) != 0) {
1726 printk(KERN_ERR "md: failed to register integrity for %s\n",
1730 printk(KERN_NOTICE "md: data integrity on %s enabled\n",
1734 EXPORT_SYMBOL(md_integrity_register);
1736 /* Disable data integrity if non-capable/non-matching disk is being added */
1737 void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
1739 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1740 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1742 if (!bi_mddev) /* nothing to do */
1744 if (rdev->raid_disk < 0) /* skip spares */
1746 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1747 rdev->bdev->bd_disk) >= 0)
1749 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1750 blk_integrity_unregister(mddev->gendisk);
1752 EXPORT_SYMBOL(md_integrity_add_rdev);
1754 static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1756 char b[BDEVNAME_SIZE];
1766 /* prevent duplicates */
1767 if (find_rdev(mddev, rdev->bdev->bd_dev))
1770 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1771 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1772 rdev->sectors < mddev->dev_sectors)) {
1774 /* Cannot change size, so fail
1775 * If mddev->level <= 0, then we don't care
1776 * about aligning sizes (e.g. linear)
1778 if (mddev->level > 0)
1781 mddev->dev_sectors = rdev->sectors;
1784 /* Verify rdev->desc_nr is unique.
1785 * If it is -1, assign a free number, else
1786 * check number is not in use
1788 if (rdev->desc_nr < 0) {
1790 if (mddev->pers) choice = mddev->raid_disks;
1791 while (find_rdev_nr(mddev, choice))
1793 rdev->desc_nr = choice;
1795 if (find_rdev_nr(mddev, rdev->desc_nr))
1798 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1799 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1800 mdname(mddev), mddev->max_disks);
1803 bdevname(rdev->bdev,b);
1804 while ( (s=strchr(b, '/')) != NULL)
1807 rdev->mddev = mddev;
1808 printk(KERN_INFO "md: bind<%s>\n", b);
1810 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
1813 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
1814 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1815 kobject_del(&rdev->kobj);
1818 rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, NULL, "state");
1820 list_add_rcu(&rdev->same_set, &mddev->disks);
1821 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
1823 /* May as well allow recovery to be retried once */
1824 mddev->recovery_disabled = 0;
1829 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1834 static void md_delayed_delete(struct work_struct *ws)
1836 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1837 kobject_del(&rdev->kobj);
1838 kobject_put(&rdev->kobj);
1841 static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1843 char b[BDEVNAME_SIZE];
1848 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
1849 list_del_rcu(&rdev->same_set);
1850 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1852 sysfs_remove_link(&rdev->kobj, "block");
1853 sysfs_put(rdev->sysfs_state);
1854 rdev->sysfs_state = NULL;
1855 /* We need to delay this, otherwise we can deadlock when
1856 * writing to 'remove' to "dev/state". We also need
1857 * to delay it due to rcu usage.
1860 INIT_WORK(&rdev->del_work, md_delayed_delete);
1861 kobject_get(&rdev->kobj);
1862 schedule_work(&rdev->del_work);
1866 * prevent the device from being mounted, repartitioned or
1867 * otherwise reused by a RAID array (or any other kernel
1868 * subsystem), by bd_claiming the device.
1870 static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
1873 struct block_device *bdev;
1874 char b[BDEVNAME_SIZE];
1876 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
1878 printk(KERN_ERR "md: could not open %s.\n",
1879 __bdevname(dev, b));
1880 return PTR_ERR(bdev);
1882 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
1884 printk(KERN_ERR "md: could not bd_claim %s.\n",
1886 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
1890 set_bit(AllReserved, &rdev->flags);
1895 static void unlock_rdev(mdk_rdev_t *rdev)
1897 struct block_device *bdev = rdev->bdev;
1902 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
1905 void md_autodetect_dev(dev_t dev);
1907 static void export_rdev(mdk_rdev_t * rdev)
1909 char b[BDEVNAME_SIZE];
1910 printk(KERN_INFO "md: export_rdev(%s)\n",
1911 bdevname(rdev->bdev,b));
1916 if (test_bit(AutoDetected, &rdev->flags))
1917 md_autodetect_dev(rdev->bdev->bd_dev);
1920 kobject_put(&rdev->kobj);
1923 static void kick_rdev_from_array(mdk_rdev_t * rdev)
1925 unbind_rdev_from_array(rdev);
1929 static void export_array(mddev_t *mddev)
1931 mdk_rdev_t *rdev, *tmp;
1933 rdev_for_each(rdev, tmp, mddev) {
1938 kick_rdev_from_array(rdev);
1940 if (!list_empty(&mddev->disks))
1942 mddev->raid_disks = 0;
1943 mddev->major_version = 0;
1946 static void print_desc(mdp_disk_t *desc)
1948 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1949 desc->major,desc->minor,desc->raid_disk,desc->state);
1952 static void print_sb_90(mdp_super_t *sb)
1957 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1958 sb->major_version, sb->minor_version, sb->patch_version,
1959 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1961 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1962 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1963 sb->md_minor, sb->layout, sb->chunk_size);
1964 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1965 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1966 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1967 sb->failed_disks, sb->spare_disks,
1968 sb->sb_csum, (unsigned long)sb->events_lo);
1971 for (i = 0; i < MD_SB_DISKS; i++) {
1974 desc = sb->disks + i;
1975 if (desc->number || desc->major || desc->minor ||
1976 desc->raid_disk || (desc->state && (desc->state != 4))) {
1977 printk(" D %2d: ", i);
1981 printk(KERN_INFO "md: THIS: ");
1982 print_desc(&sb->this_disk);
1985 static void print_sb_1(struct mdp_superblock_1 *sb)
1989 uuid = sb->set_uuid;
1991 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
1992 "md: Name: \"%s\" CT:%llu\n",
1993 le32_to_cpu(sb->major_version),
1994 le32_to_cpu(sb->feature_map),
1997 (unsigned long long)le64_to_cpu(sb->ctime)
1998 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
2000 uuid = sb->device_uuid;
2002 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
2004 "md: Dev:%08x UUID: %pU\n"
2005 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2006 "md: (MaxDev:%u) \n",
2007 le32_to_cpu(sb->level),
2008 (unsigned long long)le64_to_cpu(sb->size),
2009 le32_to_cpu(sb->raid_disks),
2010 le32_to_cpu(sb->layout),
2011 le32_to_cpu(sb->chunksize),
2012 (unsigned long long)le64_to_cpu(sb->data_offset),
2013 (unsigned long long)le64_to_cpu(sb->data_size),
2014 (unsigned long long)le64_to_cpu(sb->super_offset),
2015 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2016 le32_to_cpu(sb->dev_number),
2019 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2020 (unsigned long long)le64_to_cpu(sb->events),
2021 (unsigned long long)le64_to_cpu(sb->resync_offset),
2022 le32_to_cpu(sb->sb_csum),
2023 le32_to_cpu(sb->max_dev)
2027 static void print_rdev(mdk_rdev_t *rdev, int major_version)
2029 char b[BDEVNAME_SIZE];
2030 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2031 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
2032 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2034 if (rdev->sb_loaded) {
2035 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2036 switch (major_version) {
2038 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
2041 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
2045 printk(KERN_INFO "md: no rdev superblock!\n");
2048 static void md_print_devices(void)
2050 struct list_head *tmp;
2053 char b[BDEVNAME_SIZE];
2056 printk("md: **********************************\n");
2057 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2058 printk("md: **********************************\n");
2059 for_each_mddev(mddev, tmp) {
2062 bitmap_print_sb(mddev->bitmap);
2064 printk("%s: ", mdname(mddev));
2065 list_for_each_entry(rdev, &mddev->disks, same_set)
2066 printk("<%s>", bdevname(rdev->bdev,b));
2069 list_for_each_entry(rdev, &mddev->disks, same_set)
2070 print_rdev(rdev, mddev->major_version);
2072 printk("md: **********************************\n");
2077 static void sync_sbs(mddev_t * mddev, int nospares)
2079 /* Update each superblock (in-memory image), but
2080 * if we are allowed to, skip spares which already
2081 * have the right event counter, or have one earlier
2082 * (which would mean they aren't being marked as dirty
2083 * with the rest of the array)
2087 /* First make sure individual recovery_offsets are correct */
2088 list_for_each_entry(rdev, &mddev->disks, same_set) {
2089 if (rdev->raid_disk >= 0 &&
2090 mddev->delta_disks >= 0 &&
2091 !test_bit(In_sync, &rdev->flags) &&
2092 mddev->curr_resync_completed > rdev->recovery_offset)
2093 rdev->recovery_offset = mddev->curr_resync_completed;
2096 list_for_each_entry(rdev, &mddev->disks, same_set) {
2097 if (rdev->sb_events == mddev->events ||
2099 rdev->raid_disk < 0 &&
2100 rdev->sb_events+1 == mddev->events)) {
2101 /* Don't update this superblock */
2102 rdev->sb_loaded = 2;
2104 super_types[mddev->major_version].
2105 sync_super(mddev, rdev);
2106 rdev->sb_loaded = 1;
2111 static void md_update_sb(mddev_t * mddev, int force_change)
2117 mddev->utime = get_seconds();
2118 if (mddev->external)
2121 spin_lock_irq(&mddev->write_lock);
2123 set_bit(MD_CHANGE_PENDING, &mddev->flags);
2124 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2126 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2127 /* just a clean<-> dirty transition, possibly leave spares alone,
2128 * though if events isn't the right even/odd, we will have to do
2134 if (mddev->degraded)
2135 /* If the array is degraded, then skipping spares is both
2136 * dangerous and fairly pointless.
2137 * Dangerous because a device that was removed from the array
2138 * might have a event_count that still looks up-to-date,
2139 * so it can be re-added without a resync.
2140 * Pointless because if there are any spares to skip,
2141 * then a recovery will happen and soon that array won't
2142 * be degraded any more and the spare can go back to sleep then.
2146 sync_req = mddev->in_sync;
2148 /* If this is just a dirty<->clean transition, and the array is clean
2149 * and 'events' is odd, we can roll back to the previous clean state */
2151 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
2152 && mddev->can_decrease_events
2153 && mddev->events != 1) {
2155 mddev->can_decrease_events = 0;
2157 /* otherwise we have to go forward and ... */
2159 mddev->can_decrease_events = nospares;
2162 if (!mddev->events) {
2164 * oops, this 64-bit counter should never wrap.
2165 * Either we are in around ~1 trillion A.C., assuming
2166 * 1 reboot per second, or we have a bug:
2173 * do not write anything to disk if using
2174 * nonpersistent superblocks
2176 if (!mddev->persistent) {
2177 if (!mddev->external)
2178 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2180 spin_unlock_irq(&mddev->write_lock);
2181 wake_up(&mddev->sb_wait);
2184 sync_sbs(mddev, nospares);
2185 spin_unlock_irq(&mddev->write_lock);
2188 "md: updating %s RAID superblock on device (in sync %d)\n",
2189 mdname(mddev),mddev->in_sync);
2191 bitmap_update_sb(mddev->bitmap);
2192 list_for_each_entry(rdev, &mddev->disks, same_set) {
2193 char b[BDEVNAME_SIZE];
2194 dprintk(KERN_INFO "md: ");
2195 if (rdev->sb_loaded != 1)
2196 continue; /* no noise on spare devices */
2197 if (test_bit(Faulty, &rdev->flags))
2198 dprintk("(skipping faulty ");
2200 dprintk("%s ", bdevname(rdev->bdev,b));
2201 if (!test_bit(Faulty, &rdev->flags)) {
2202 md_super_write(mddev,rdev,
2203 rdev->sb_start, rdev->sb_size,
2205 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
2206 bdevname(rdev->bdev,b),
2207 (unsigned long long)rdev->sb_start);
2208 rdev->sb_events = mddev->events;
2212 if (mddev->level == LEVEL_MULTIPATH)
2213 /* only need to write one superblock... */
2216 md_super_wait(mddev);
2217 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
2219 spin_lock_irq(&mddev->write_lock);
2220 if (mddev->in_sync != sync_req ||
2221 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
2222 /* have to write it out again */
2223 spin_unlock_irq(&mddev->write_lock);
2226 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2227 spin_unlock_irq(&mddev->write_lock);
2228 wake_up(&mddev->sb_wait);
2229 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2230 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
2234 /* words written to sysfs files may, or may not, be \n terminated.
2235 * We want to accept with case. For this we use cmd_match.
2237 static int cmd_match(const char *cmd, const char *str)
2239 /* See if cmd, written into a sysfs file, matches
2240 * str. They must either be the same, or cmd can
2241 * have a trailing newline
2243 while (*cmd && *str && *cmd == *str) {
2254 struct rdev_sysfs_entry {
2255 struct attribute attr;
2256 ssize_t (*show)(mdk_rdev_t *, char *);
2257 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2261 state_show(mdk_rdev_t *rdev, char *page)
2266 if (test_bit(Faulty, &rdev->flags)) {
2267 len+= sprintf(page+len, "%sfaulty",sep);
2270 if (test_bit(In_sync, &rdev->flags)) {
2271 len += sprintf(page+len, "%sin_sync",sep);
2274 if (test_bit(WriteMostly, &rdev->flags)) {
2275 len += sprintf(page+len, "%swrite_mostly",sep);
2278 if (test_bit(Blocked, &rdev->flags)) {
2279 len += sprintf(page+len, "%sblocked", sep);
2282 if (!test_bit(Faulty, &rdev->flags) &&
2283 !test_bit(In_sync, &rdev->flags)) {
2284 len += sprintf(page+len, "%sspare", sep);
2287 return len+sprintf(page+len, "\n");
2291 state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2294 * faulty - simulates and error
2295 * remove - disconnects the device
2296 * writemostly - sets write_mostly
2297 * -writemostly - clears write_mostly
2298 * blocked - sets the Blocked flag
2299 * -blocked - clears the Blocked flag
2300 * insync - sets Insync providing device isn't active
2303 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2304 md_error(rdev->mddev, rdev);
2306 } else if (cmd_match(buf, "remove")) {
2307 if (rdev->raid_disk >= 0)
2310 mddev_t *mddev = rdev->mddev;
2311 kick_rdev_from_array(rdev);
2313 md_update_sb(mddev, 1);
2314 md_new_event(mddev);
2317 } else if (cmd_match(buf, "writemostly")) {
2318 set_bit(WriteMostly, &rdev->flags);
2320 } else if (cmd_match(buf, "-writemostly")) {
2321 clear_bit(WriteMostly, &rdev->flags);
2323 } else if (cmd_match(buf, "blocked")) {
2324 set_bit(Blocked, &rdev->flags);
2326 } else if (cmd_match(buf, "-blocked")) {
2327 clear_bit(Blocked, &rdev->flags);
2328 wake_up(&rdev->blocked_wait);
2329 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2330 md_wakeup_thread(rdev->mddev->thread);
2333 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2334 set_bit(In_sync, &rdev->flags);
2337 if (!err && rdev->sysfs_state)
2338 sysfs_notify_dirent(rdev->sysfs_state);
2339 return err ? err : len;
2341 static struct rdev_sysfs_entry rdev_state =
2342 __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
2345 errors_show(mdk_rdev_t *rdev, char *page)
2347 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2351 errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2354 unsigned long n = simple_strtoul(buf, &e, 10);
2355 if (*buf && (*e == 0 || *e == '\n')) {
2356 atomic_set(&rdev->corrected_errors, n);
2361 static struct rdev_sysfs_entry rdev_errors =
2362 __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
2365 slot_show(mdk_rdev_t *rdev, char *page)
2367 if (rdev->raid_disk < 0)
2368 return sprintf(page, "none\n");
2370 return sprintf(page, "%d\n", rdev->raid_disk);
2374 slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2379 int slot = simple_strtoul(buf, &e, 10);
2380 if (strncmp(buf, "none", 4)==0)
2382 else if (e==buf || (*e && *e!= '\n'))
2384 if (rdev->mddev->pers && slot == -1) {
2385 /* Setting 'slot' on an active array requires also
2386 * updating the 'rd%d' link, and communicating
2387 * with the personality with ->hot_*_disk.
2388 * For now we only support removing
2389 * failed/spare devices. This normally happens automatically,
2390 * but not when the metadata is externally managed.
2392 if (rdev->raid_disk == -1)
2394 /* personality does all needed checks */
2395 if (rdev->mddev->pers->hot_add_disk == NULL)
2397 err = rdev->mddev->pers->
2398 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2401 sprintf(nm, "rd%d", rdev->raid_disk);
2402 sysfs_remove_link(&rdev->mddev->kobj, nm);
2403 rdev->raid_disk = -1;
2404 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2405 md_wakeup_thread(rdev->mddev->thread);
2406 } else if (rdev->mddev->pers) {
2408 /* Activating a spare .. or possibly reactivating
2409 * if we ever get bitmaps working here.
2412 if (rdev->raid_disk != -1)
2415 if (rdev->mddev->pers->hot_add_disk == NULL)
2418 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
2419 if (rdev2->raid_disk == slot)
2422 rdev->raid_disk = slot;
2423 if (test_bit(In_sync, &rdev->flags))
2424 rdev->saved_raid_disk = slot;
2426 rdev->saved_raid_disk = -1;
2427 err = rdev->mddev->pers->
2428 hot_add_disk(rdev->mddev, rdev);
2430 rdev->raid_disk = -1;
2433 sysfs_notify_dirent(rdev->sysfs_state);
2434 sprintf(nm, "rd%d", rdev->raid_disk);
2435 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2437 "md: cannot register "
2439 nm, mdname(rdev->mddev));
2441 /* don't wakeup anyone, leave that to userspace. */
2443 if (slot >= rdev->mddev->raid_disks)
2445 rdev->raid_disk = slot;
2446 /* assume it is working */
2447 clear_bit(Faulty, &rdev->flags);
2448 clear_bit(WriteMostly, &rdev->flags);
2449 set_bit(In_sync, &rdev->flags);
2450 sysfs_notify_dirent(rdev->sysfs_state);
2456 static struct rdev_sysfs_entry rdev_slot =
2457 __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
2460 offset_show(mdk_rdev_t *rdev, char *page)
2462 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
2466 offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2469 unsigned long long offset = simple_strtoull(buf, &e, 10);
2470 if (e==buf || (*e && *e != '\n'))
2472 if (rdev->mddev->pers && rdev->raid_disk >= 0)
2474 if (rdev->sectors && rdev->mddev->external)
2475 /* Must set offset before size, so overlap checks
2478 rdev->data_offset = offset;
2482 static struct rdev_sysfs_entry rdev_offset =
2483 __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
2486 rdev_size_show(mdk_rdev_t *rdev, char *page)
2488 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
2491 static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2493 /* check if two start/length pairs overlap */
2501 static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2503 unsigned long long blocks;
2506 if (strict_strtoull(buf, 10, &blocks) < 0)
2509 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2510 return -EINVAL; /* sector conversion overflow */
2513 if (new != blocks * 2)
2514 return -EINVAL; /* unsigned long long to sector_t overflow */
2521 rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2523 mddev_t *my_mddev = rdev->mddev;
2524 sector_t oldsectors = rdev->sectors;
2527 if (strict_blocks_to_sectors(buf, §ors) < 0)
2529 if (my_mddev->pers && rdev->raid_disk >= 0) {
2530 if (my_mddev->persistent) {
2531 sectors = super_types[my_mddev->major_version].
2532 rdev_size_change(rdev, sectors);
2535 } else if (!sectors)
2536 sectors = (rdev->bdev->bd_inode->i_size >> 9) -
2539 if (sectors < my_mddev->dev_sectors)
2540 return -EINVAL; /* component must fit device */
2542 rdev->sectors = sectors;
2543 if (sectors > oldsectors && my_mddev->external) {
2544 /* need to check that all other rdevs with the same ->bdev
2545 * do not overlap. We need to unlock the mddev to avoid
2546 * a deadlock. We have already changed rdev->sectors, and if
2547 * we have to change it back, we will have the lock again.
2551 struct list_head *tmp;
2553 mddev_unlock(my_mddev);
2554 for_each_mddev(mddev, tmp) {
2558 list_for_each_entry(rdev2, &mddev->disks, same_set)
2559 if (test_bit(AllReserved, &rdev2->flags) ||
2560 (rdev->bdev == rdev2->bdev &&
2562 overlaps(rdev->data_offset, rdev->sectors,
2568 mddev_unlock(mddev);
2574 mddev_lock(my_mddev);
2576 /* Someone else could have slipped in a size
2577 * change here, but doing so is just silly.
2578 * We put oldsectors back because we *know* it is
2579 * safe, and trust userspace not to race with
2582 rdev->sectors = oldsectors;
2589 static struct rdev_sysfs_entry rdev_size =
2590 __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
2593 static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page)
2595 unsigned long long recovery_start = rdev->recovery_offset;
2597 if (test_bit(In_sync, &rdev->flags) ||
2598 recovery_start == MaxSector)
2599 return sprintf(page, "none\n");
2601 return sprintf(page, "%llu\n", recovery_start);
2604 static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2606 unsigned long long recovery_start;
2608 if (cmd_match(buf, "none"))
2609 recovery_start = MaxSector;
2610 else if (strict_strtoull(buf, 10, &recovery_start))
2613 if (rdev->mddev->pers &&
2614 rdev->raid_disk >= 0)
2617 rdev->recovery_offset = recovery_start;
2618 if (recovery_start == MaxSector)
2619 set_bit(In_sync, &rdev->flags);
2621 clear_bit(In_sync, &rdev->flags);
2625 static struct rdev_sysfs_entry rdev_recovery_start =
2626 __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2628 static struct attribute *rdev_default_attrs[] = {
2634 &rdev_recovery_start.attr,
2638 rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2640 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2641 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
2642 mddev_t *mddev = rdev->mddev;
2648 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2650 if (rdev->mddev == NULL)
2653 rv = entry->show(rdev, page);
2654 mddev_unlock(mddev);
2660 rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2661 const char *page, size_t length)
2663 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2664 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
2666 mddev_t *mddev = rdev->mddev;
2670 if (!capable(CAP_SYS_ADMIN))
2672 rv = mddev ? mddev_lock(mddev): -EBUSY;
2674 if (rdev->mddev == NULL)
2677 rv = entry->store(rdev, page, length);
2678 mddev_unlock(mddev);
2683 static void rdev_free(struct kobject *ko)
2685 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2688 static const struct sysfs_ops rdev_sysfs_ops = {
2689 .show = rdev_attr_show,
2690 .store = rdev_attr_store,
2692 static struct kobj_type rdev_ktype = {
2693 .release = rdev_free,
2694 .sysfs_ops = &rdev_sysfs_ops,
2695 .default_attrs = rdev_default_attrs,
2699 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2701 * mark the device faulty if:
2703 * - the device is nonexistent (zero size)
2704 * - the device has no valid superblock
2706 * a faulty rdev _never_ has rdev->sb set.
2708 static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2710 char b[BDEVNAME_SIZE];
2715 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
2717 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2718 return ERR_PTR(-ENOMEM);
2721 if ((err = alloc_disk_sb(rdev)))
2724 err = lock_rdev(rdev, newdev, super_format == -2);
2728 kobject_init(&rdev->kobj, &rdev_ktype);
2731 rdev->saved_raid_disk = -1;
2732 rdev->raid_disk = -1;
2734 rdev->data_offset = 0;
2735 rdev->sb_events = 0;
2736 rdev->last_read_error.tv_sec = 0;
2737 rdev->last_read_error.tv_nsec = 0;
2738 atomic_set(&rdev->nr_pending, 0);
2739 atomic_set(&rdev->read_errors, 0);
2740 atomic_set(&rdev->corrected_errors, 0);
2742 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2745 "md: %s has zero or unknown size, marking faulty!\n",
2746 bdevname(rdev->bdev,b));
2751 if (super_format >= 0) {
2752 err = super_types[super_format].
2753 load_super(rdev, NULL, super_minor);
2754 if (err == -EINVAL) {
2756 "md: %s does not have a valid v%d.%d "
2757 "superblock, not importing!\n",
2758 bdevname(rdev->bdev,b),
2759 super_format, super_minor);
2764 "md: could not read %s's sb, not importing!\n",
2765 bdevname(rdev->bdev,b));
2770 INIT_LIST_HEAD(&rdev->same_set);
2771 init_waitqueue_head(&rdev->blocked_wait);
2776 if (rdev->sb_page) {
2782 return ERR_PTR(err);
2786 * Check a full RAID array for plausibility
2790 static void analyze_sbs(mddev_t * mddev)
2793 mdk_rdev_t *rdev, *freshest, *tmp;
2794 char b[BDEVNAME_SIZE];
2797 rdev_for_each(rdev, tmp, mddev)
2798 switch (super_types[mddev->major_version].
2799 load_super(rdev, freshest, mddev->minor_version)) {
2807 "md: fatal superblock inconsistency in %s"
2808 " -- removing from array\n",
2809 bdevname(rdev->bdev,b));
2810 kick_rdev_from_array(rdev);
2814 super_types[mddev->major_version].
2815 validate_super(mddev, freshest);
2818 rdev_for_each(rdev, tmp, mddev) {
2819 if (mddev->max_disks &&
2820 (rdev->desc_nr >= mddev->max_disks ||
2821 i > mddev->max_disks)) {
2823 "md: %s: %s: only %d devices permitted\n",
2824 mdname(mddev), bdevname(rdev->bdev, b),
2826 kick_rdev_from_array(rdev);
2829 if (rdev != freshest)
2830 if (super_types[mddev->major_version].
2831 validate_super(mddev, rdev)) {
2832 printk(KERN_WARNING "md: kicking non-fresh %s"
2834 bdevname(rdev->bdev,b));
2835 kick_rdev_from_array(rdev);
2838 if (mddev->level == LEVEL_MULTIPATH) {
2839 rdev->desc_nr = i++;
2840 rdev->raid_disk = rdev->desc_nr;
2841 set_bit(In_sync, &rdev->flags);
2842 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
2843 rdev->raid_disk = -1;
2844 clear_bit(In_sync, &rdev->flags);
2849 /* Read a fixed-point number.
2850 * Numbers in sysfs attributes should be in "standard" units where
2851 * possible, so time should be in seconds.
2852 * However we internally use a a much smaller unit such as
2853 * milliseconds or jiffies.
2854 * This function takes a decimal number with a possible fractional
2855 * component, and produces an integer which is the result of
2856 * multiplying that number by 10^'scale'.
2857 * all without any floating-point arithmetic.
2859 int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
2861 unsigned long result = 0;
2863 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
2866 else if (decimals < scale) {
2869 result = result * 10 + value;
2881 while (decimals < scale) {
2890 static void md_safemode_timeout(unsigned long data);
2893 safe_delay_show(mddev_t *mddev, char *page)
2895 int msec = (mddev->safemode_delay*1000)/HZ;
2896 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2899 safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2903 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
2906 mddev->safemode_delay = 0;
2908 unsigned long old_delay = mddev->safemode_delay;
2909 mddev->safemode_delay = (msec*HZ)/1000;
2910 if (mddev->safemode_delay == 0)
2911 mddev->safemode_delay = 1;
2912 if (mddev->safemode_delay < old_delay)
2913 md_safemode_timeout((unsigned long)mddev);
2917 static struct md_sysfs_entry md_safe_delay =
2918 __ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
2921 level_show(mddev_t *mddev, char *page)
2923 struct mdk_personality *p = mddev->pers;
2925 return sprintf(page, "%s\n", p->name);
2926 else if (mddev->clevel[0])
2927 return sprintf(page, "%s\n", mddev->clevel);
2928 else if (mddev->level != LEVEL_NONE)
2929 return sprintf(page, "%d\n", mddev->level);
2935 level_store(mddev_t *mddev, const char *buf, size_t len)
2939 struct mdk_personality *pers;
2944 if (mddev->pers == NULL) {
2947 if (len >= sizeof(mddev->clevel))
2949 strncpy(mddev->clevel, buf, len);
2950 if (mddev->clevel[len-1] == '\n')
2952 mddev->clevel[len] = 0;
2953 mddev->level = LEVEL_NONE;
2957 /* request to change the personality. Need to ensure:
2958 * - array is not engaged in resync/recovery/reshape
2959 * - old personality can be suspended
2960 * - new personality will access other array.
2963 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
2966 if (!mddev->pers->quiesce) {
2967 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
2968 mdname(mddev), mddev->pers->name);
2972 /* Now find the new personality */
2973 if (len == 0 || len >= sizeof(clevel))
2975 strncpy(clevel, buf, len);
2976 if (clevel[len-1] == '\n')
2979 if (strict_strtol(clevel, 10, &level))
2982 if (request_module("md-%s", clevel) != 0)
2983 request_module("md-level-%s", clevel);
2984 spin_lock(&pers_lock);
2985 pers = find_pers(level, clevel);
2986 if (!pers || !try_module_get(pers->owner)) {
2987 spin_unlock(&pers_lock);
2988 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
2991 spin_unlock(&pers_lock);
2993 if (pers == mddev->pers) {
2994 /* Nothing to do! */
2995 module_put(pers->owner);
2998 if (!pers->takeover) {
2999 module_put(pers->owner);
3000 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
3001 mdname(mddev), clevel);
3005 list_for_each_entry(rdev, &mddev->disks, same_set)
3006 rdev->new_raid_disk = rdev->raid_disk;
3008 /* ->takeover must set new_* and/or delta_disks
3009 * if it succeeds, and may set them when it fails.
3011 priv = pers->takeover(mddev);
3013 mddev->new_level = mddev->level;
3014 mddev->new_layout = mddev->layout;
3015 mddev->new_chunk_sectors = mddev->chunk_sectors;
3016 mddev->raid_disks -= mddev->delta_disks;
3017 mddev->delta_disks = 0;
3018 module_put(pers->owner);
3019 printk(KERN_WARNING "md: %s: %s would not accept array\n",
3020 mdname(mddev), clevel);
3021 return PTR_ERR(priv);
3024 /* Looks like we have a winner */
3025 mddev_suspend(mddev);
3026 mddev->pers->stop(mddev);
3028 if (mddev->pers->sync_request == NULL &&
3029 pers->sync_request != NULL) {
3030 /* need to add the md_redundancy_group */
3031 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3033 "md: cannot register extra attributes for %s\n",
3035 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action");
3037 if (mddev->pers->sync_request != NULL &&
3038 pers->sync_request == NULL) {
3039 /* need to remove the md_redundancy_group */
3040 if (mddev->to_remove == NULL)
3041 mddev->to_remove = &md_redundancy_group;
3044 if (mddev->pers->sync_request == NULL &&
3046 /* We are converting from a no-redundancy array
3047 * to a redundancy array and metadata is managed
3048 * externally so we need to be sure that writes
3049 * won't block due to a need to transition
3051 * until external management is started.
3054 mddev->safemode_delay = 0;
3055 mddev->safemode = 0;
3058 list_for_each_entry(rdev, &mddev->disks, same_set) {
3060 if (rdev->raid_disk < 0)
3062 if (rdev->new_raid_disk > mddev->raid_disks)
3063 rdev->new_raid_disk = -1;
3064 if (rdev->new_raid_disk == rdev->raid_disk)
3066 sprintf(nm, "rd%d", rdev->raid_disk);
3067 sysfs_remove_link(&mddev->kobj, nm);
3069 list_for_each_entry(rdev, &mddev->disks, same_set) {
3070 if (rdev->raid_disk < 0)
3072 if (rdev->new_raid_disk == rdev->raid_disk)
3074 rdev->raid_disk = rdev->new_raid_disk;
3075 if (rdev->raid_disk < 0)
3076 clear_bit(In_sync, &rdev->flags);
3079 sprintf(nm, "rd%d", rdev->raid_disk);
3080 if(sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
3081 printk("md: cannot register %s for %s after level change\n",
3086 module_put(mddev->pers->owner);
3088 mddev->private = priv;
3089 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3090 mddev->level = mddev->new_level;
3091 mddev->layout = mddev->new_layout;
3092 mddev->chunk_sectors = mddev->new_chunk_sectors;
3093 mddev->delta_disks = 0;
3094 if (mddev->pers->sync_request == NULL) {
3095 /* this is now an array without redundancy, so
3096 * it must always be in_sync
3099 del_timer_sync(&mddev->safemode_timer);
3102 mddev_resume(mddev);
3103 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3104 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3105 md_wakeup_thread(mddev->thread);
3106 sysfs_notify(&mddev->kobj, NULL, "level");
3107 md_new_event(mddev);
3111 static struct md_sysfs_entry md_level =
3112 __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
3116 layout_show(mddev_t *mddev, char *page)
3118 /* just a number, not meaningful for all levels */
3119 if (mddev->reshape_position != MaxSector &&
3120 mddev->layout != mddev->new_layout)
3121 return sprintf(page, "%d (%d)\n",
3122 mddev->new_layout, mddev->layout);
3123 return sprintf(page, "%d\n", mddev->layout);
3127 layout_store(mddev_t *mddev, const char *buf, size_t len)
3130 unsigned long n = simple_strtoul(buf, &e, 10);
3132 if (!*buf || (*e && *e != '\n'))
3137 if (mddev->pers->check_reshape == NULL)
3139 mddev->new_layout = n;
3140 err = mddev->pers->check_reshape(mddev);
3142 mddev->new_layout = mddev->layout;
3146 mddev->new_layout = n;
3147 if (mddev->reshape_position == MaxSector)
3152 static struct md_sysfs_entry md_layout =
3153 __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
3157 raid_disks_show(mddev_t *mddev, char *page)
3159 if (mddev->raid_disks == 0)
3161 if (mddev->reshape_position != MaxSector &&
3162 mddev->delta_disks != 0)
3163 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3164 mddev->raid_disks - mddev->delta_disks);
3165 return sprintf(page, "%d\n", mddev->raid_disks);
3168 static int update_raid_disks(mddev_t *mddev, int raid_disks);
3171 raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
3175 unsigned long n = simple_strtoul(buf, &e, 10);
3177 if (!*buf || (*e && *e != '\n'))
3181 rv = update_raid_disks(mddev, n);
3182 else if (mddev->reshape_position != MaxSector) {
3183 int olddisks = mddev->raid_disks - mddev->delta_disks;
3184 mddev->delta_disks = n - olddisks;
3185 mddev->raid_disks = n;
3187 mddev->raid_disks = n;
3188 return rv ? rv : len;
3190 static struct md_sysfs_entry md_raid_disks =
3191 __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
3194 chunk_size_show(mddev_t *mddev, char *page)
3196 if (mddev->reshape_position != MaxSector &&
3197 mddev->chunk_sectors != mddev->new_chunk_sectors)
3198 return sprintf(page, "%d (%d)\n",
3199 mddev->new_chunk_sectors << 9,
3200 mddev->chunk_sectors << 9);
3201 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
3205 chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
3208 unsigned long n = simple_strtoul(buf, &e, 10);
3210 if (!*buf || (*e && *e != '\n'))
3215 if (mddev->pers->check_reshape == NULL)
3217 mddev->new_chunk_sectors = n >> 9;
3218 err = mddev->pers->check_reshape(mddev);
3220 mddev->new_chunk_sectors = mddev->chunk_sectors;
3224 mddev->new_chunk_sectors = n >> 9;
3225 if (mddev->reshape_position == MaxSector)
3226 mddev->chunk_sectors = n >> 9;
3230 static struct md_sysfs_entry md_chunk_size =
3231 __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
3234 resync_start_show(mddev_t *mddev, char *page)
3236 if (mddev->recovery_cp == MaxSector)
3237 return sprintf(page, "none\n");
3238 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3242 resync_start_store(mddev_t *mddev, const char *buf, size_t len)
3245 unsigned long long n = simple_strtoull(buf, &e, 10);
3249 if (cmd_match(buf, "none"))
3251 else if (!*buf || (*e && *e != '\n'))
3254 mddev->recovery_cp = n;
3257 static struct md_sysfs_entry md_resync_start =
3258 __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
3261 * The array state can be:
3264 * No devices, no size, no level
3265 * Equivalent to STOP_ARRAY ioctl
3267 * May have some settings, but array is not active
3268 * all IO results in error
3269 * When written, doesn't tear down array, but just stops it
3270 * suspended (not supported yet)
3271 * All IO requests will block. The array can be reconfigured.
3272 * Writing this, if accepted, will block until array is quiescent
3274 * no resync can happen. no superblocks get written.
3275 * write requests fail
3277 * like readonly, but behaves like 'clean' on a write request.
3279 * clean - no pending writes, but otherwise active.
3280 * When written to inactive array, starts without resync
3281 * If a write request arrives then
3282 * if metadata is known, mark 'dirty' and switch to 'active'.
3283 * if not known, block and switch to write-pending
3284 * If written to an active array that has pending writes, then fails.
3286 * fully active: IO and resync can be happening.
3287 * When written to inactive array, starts with resync
3290 * clean, but writes are blocked waiting for 'active' to be written.
3293 * like active, but no writes have been seen for a while (100msec).
3296 enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3297 write_pending, active_idle, bad_word};
3298 static char *array_states[] = {
3299 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3300 "write-pending", "active-idle", NULL };
3302 static int match_word(const char *word, char **list)
3305 for (n=0; list[n]; n++)
3306 if (cmd_match(word, list[n]))
3312 array_state_show(mddev_t *mddev, char *page)
3314 enum array_state st = inactive;
3327 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
3329 else if (mddev->safemode)
3335 if (list_empty(&mddev->disks) &&
3336 mddev->raid_disks == 0 &&
3337 mddev->dev_sectors == 0)
3342 return sprintf(page, "%s\n", array_states[st]);
3345 static int do_md_stop(mddev_t * mddev, int ro, int is_open);
3346 static int md_set_readonly(mddev_t * mddev, int is_open);
3347 static int do_md_run(mddev_t * mddev);
3348 static int restart_array(mddev_t *mddev);
3351 array_state_store(mddev_t *mddev, const char *buf, size_t len)
3354 enum array_state st = match_word(buf, array_states);
3359 /* stopping an active array */
3360 if (atomic_read(&mddev->openers) > 0)
3362 err = do_md_stop(mddev, 0, 0);
3365 /* stopping an active array */
3367 if (atomic_read(&mddev->openers) > 0)
3369 err = do_md_stop(mddev, 2, 0);
3371 err = 0; /* already inactive */
3374 break; /* not supported yet */
3377 err = md_set_readonly(mddev, 0);
3380 set_disk_ro(mddev->gendisk, 1);
3381 err = do_md_run(mddev);
3387 err = md_set_readonly(mddev, 0);
3388 else if (mddev->ro == 1)
3389 err = restart_array(mddev);
3392 set_disk_ro(mddev->gendisk, 0);
3396 err = do_md_run(mddev);
3401 restart_array(mddev);
3402 spin_lock_irq(&mddev->write_lock);
3403 if (atomic_read(&mddev->writes_pending) == 0) {
3404 if (mddev->in_sync == 0) {
3406 if (mddev->safemode == 1)
3407 mddev->safemode = 0;
3408 if (mddev->persistent)
3409 set_bit(MD_CHANGE_CLEAN,
3415 spin_unlock_irq(&mddev->write_lock);
3421 restart_array(mddev);
3422 if (mddev->external)
3423 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
3424 wake_up(&mddev->sb_wait);
3428 set_disk_ro(mddev->gendisk, 0);
3429 err = do_md_run(mddev);
3434 /* these cannot be set */
3440 sysfs_notify_dirent(mddev->sysfs_state);
3444 static struct md_sysfs_entry md_array_state =
3445 __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
3448 max_corrected_read_errors_show(mddev_t *mddev, char *page) {
3449 return sprintf(page, "%d\n",
3450 atomic_read(&mddev->max_corr_read_errors));
3454 max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len)
3457 unsigned long n = simple_strtoul(buf, &e, 10);
3459 if (*buf && (*e == 0 || *e == '\n')) {
3460 atomic_set(&mddev->max_corr_read_errors, n);
3466 static struct md_sysfs_entry max_corr_read_errors =
3467 __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3468 max_corrected_read_errors_store);
3471 null_show(mddev_t *mddev, char *page)
3477 new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3479 /* buf must be %d:%d\n? giving major and minor numbers */
3480 /* The new device is added to the array.
3481 * If the array has a persistent superblock, we read the
3482 * superblock to initialise info and check validity.
3483 * Otherwise, only checking done is that in bind_rdev_to_array,
3484 * which mainly checks size.
3487 int major = simple_strtoul(buf, &e, 10);
3493 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3495 minor = simple_strtoul(e+1, &e, 10);
3496 if (*e && *e != '\n')
3498 dev = MKDEV(major, minor);
3499 if (major != MAJOR(dev) ||
3500 minor != MINOR(dev))
3504 if (mddev->persistent) {
3505 rdev = md_import_device(dev, mddev->major_version,
3506 mddev->minor_version);
3507 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3508 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3509 mdk_rdev_t, same_set);
3510 err = super_types[mddev->major_version]
3511 .load_super(rdev, rdev0, mddev->minor_version);
3515 } else if (mddev->external)
3516 rdev = md_import_device(dev, -2, -1);
3518 rdev = md_import_device(dev, -1, -1);
3521 return PTR_ERR(rdev);
3522 err = bind_rdev_to_array(rdev, mddev);
3526 return err ? err : len;
3529 static struct md_sysfs_entry md_new_device =
3530 __ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
3533 bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3536 unsigned long chunk, end_chunk;
3540 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3542 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3543 if (buf == end) break;
3544 if (*end == '-') { /* range */
3546 end_chunk = simple_strtoul(buf, &end, 0);
3547 if (buf == end) break;
3549 if (*end && !isspace(*end)) break;
3550 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
3551 buf = skip_spaces(end);
3553 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3558 static struct md_sysfs_entry md_bitmap =
3559 __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3562 size_show(mddev_t *mddev, char *page)
3564 return sprintf(page, "%llu\n",
3565 (unsigned long long)mddev->dev_sectors / 2);
3568 static int update_size(mddev_t *mddev, sector_t num_sectors);
3571 size_store(mddev_t *mddev, const char *buf, size_t len)
3573 /* If array is inactive, we can reduce the component size, but
3574 * not increase it (except from 0).
3575 * If array is active, we can try an on-line resize
3578 int err = strict_blocks_to_sectors(buf, §ors);
3583 err = update_size(mddev, sectors);
3584 md_update_sb(mddev, 1);
3586 if (mddev->dev_sectors == 0 ||
3587 mddev->dev_sectors > sectors)
3588 mddev->dev_sectors = sectors;
3592 return err ? err : len;
3595 static struct md_sysfs_entry md_size =
3596 __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
3601 * 'none' for arrays with no metadata (good luck...)
3602 * 'external' for arrays with externally managed metadata,
3603 * or N.M for internally known formats
3606 metadata_show(mddev_t *mddev, char *page)
3608 if (mddev->persistent)
3609 return sprintf(page, "%d.%d\n",
3610 mddev->major_version, mddev->minor_version);
3611 else if (mddev->external)
3612 return sprintf(page, "external:%s\n", mddev->metadata_type);
3614 return sprintf(page, "none\n");
3618 metadata_store(mddev_t *mddev, const char *buf, size_t len)
3622 /* Changing the details of 'external' metadata is
3623 * always permitted. Otherwise there must be
3624 * no devices attached to the array.
3626 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3628 else if (!list_empty(&mddev->disks))
3631 if (cmd_match(buf, "none")) {
3632 mddev->persistent = 0;
3633 mddev->external = 0;
3634 mddev->major_version = 0;
3635 mddev->minor_version = 90;
3638 if (strncmp(buf, "external:", 9) == 0) {
3639 size_t namelen = len-9;
3640 if (namelen >= sizeof(mddev->metadata_type))
3641 namelen = sizeof(mddev->metadata_type)-1;
3642 strncpy(mddev->metadata_type, buf+9, namelen);
3643 mddev->metadata_type[namelen] = 0;
3644 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3645 mddev->metadata_type[--namelen] = 0;
3646 mddev->persistent = 0;
3647 mddev->external = 1;
3648 mddev->major_version = 0;
3649 mddev->minor_version = 90;
3652 major = simple_strtoul(buf, &e, 10);
3653 if (e==buf || *e != '.')
3656 minor = simple_strtoul(buf, &e, 10);
3657 if (e==buf || (*e && *e != '\n') )
3659 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
3661 mddev->major_version = major;
3662 mddev->minor_version = minor;
3663 mddev->persistent = 1;
3664 mddev->external = 0;
3668 static struct md_sysfs_entry md_metadata =
3669 __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
3672 action_show(mddev_t *mddev, char *page)
3674 char *type = "idle";
3675 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3677 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3678 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
3679 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3681 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3682 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3684 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3688 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
3691 return sprintf(page, "%s\n", type);
3695 action_store(mddev_t *mddev, const char *page, size_t len)
3697 if (!mddev->pers || !mddev->pers->sync_request)
3700 if (cmd_match(page, "frozen"))
3701 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3703 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3705 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
3706 if (mddev->sync_thread) {
3707 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3708 md_unregister_thread(mddev->sync_thread);
3709 mddev->sync_thread = NULL;
3710 mddev->recovery = 0;
3712 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3713 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
3715 else if (cmd_match(page, "resync"))
3716 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3717 else if (cmd_match(page, "recover")) {
3718 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3719 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3720 } else if (cmd_match(page, "reshape")) {
3722 if (mddev->pers->start_reshape == NULL)
3724 err = mddev->pers->start_reshape(mddev);
3727 sysfs_notify(&mddev->kobj, NULL, "degraded");
3729 if (cmd_match(page, "check"))
3730 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3731 else if (!cmd_match(page, "repair"))
3733 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3734 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3736 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3737 md_wakeup_thread(mddev->thread);
3738 sysfs_notify_dirent(mddev->sysfs_action);
3743 mismatch_cnt_show(mddev_t *mddev, char *page)
3745 return sprintf(page, "%llu\n",
3746 (unsigned long long) mddev->resync_mismatches);
3749 static struct md_sysfs_entry md_scan_mode =
3750 __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
3753 static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
3756 sync_min_show(mddev_t *mddev, char *page)
3758 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3759 mddev->sync_speed_min ? "local": "system");
3763 sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3767 if (strncmp(buf, "system", 6)==0) {
3768 mddev->sync_speed_min = 0;
3771 min = simple_strtoul(buf, &e, 10);
3772 if (buf == e || (*e && *e != '\n') || min <= 0)
3774 mddev->sync_speed_min = min;
3778 static struct md_sysfs_entry md_sync_min =
3779 __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3782 sync_max_show(mddev_t *mddev, char *page)
3784 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3785 mddev->sync_speed_max ? "local": "system");
3789 sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3793 if (strncmp(buf, "system", 6)==0) {
3794 mddev->sync_speed_max = 0;
3797 max = simple_strtoul(buf, &e, 10);
3798 if (buf == e || (*e && *e != '\n') || max <= 0)
3800 mddev->sync_speed_max = max;
3804 static struct md_sysfs_entry md_sync_max =
3805 __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3808 degraded_show(mddev_t *mddev, char *page)
3810 return sprintf(page, "%d\n", mddev->degraded);
3812 static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
3815 sync_force_parallel_show(mddev_t *mddev, char *page)
3817 return sprintf(page, "%d\n", mddev->parallel_resync);
3821 sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3825 if (strict_strtol(buf, 10, &n))
3828 if (n != 0 && n != 1)
3831 mddev->parallel_resync = n;
3833 if (mddev->sync_thread)
3834 wake_up(&resync_wait);
3839 /* force parallel resync, even with shared block devices */
3840 static struct md_sysfs_entry md_sync_force_parallel =
3841 __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3842 sync_force_parallel_show, sync_force_parallel_store);
3845 sync_speed_show(mddev_t *mddev, char *page)
3847 unsigned long resync, dt, db;
3848 if (mddev->curr_resync == 0)
3849 return sprintf(page, "none\n");
3850 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3851 dt = (jiffies - mddev->resync_mark) / HZ;
3853 db = resync - mddev->resync_mark_cnt;
3854 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
3857 static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
3860 sync_completed_show(mddev_t *mddev, char *page)
3862 unsigned long max_sectors, resync;
3864 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3865 return sprintf(page, "none\n");
3867 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3868 max_sectors = mddev->resync_max_sectors;
3870 max_sectors = mddev->dev_sectors;
3872 resync = mddev->curr_resync_completed;
3873 return sprintf(page, "%lu / %lu\n", resync, max_sectors);
3876 static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
3879 min_sync_show(mddev_t *mddev, char *page)
3881 return sprintf(page, "%llu\n",
3882 (unsigned long long)mddev->resync_min);
3885 min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3887 unsigned long long min;
3888 if (strict_strtoull(buf, 10, &min))
3890 if (min > mddev->resync_max)
3892 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3895 /* Must be a multiple of chunk_size */
3896 if (mddev->chunk_sectors) {
3897 sector_t temp = min;
3898 if (sector_div(temp, mddev->chunk_sectors))
3901 mddev->resync_min = min;
3906 static struct md_sysfs_entry md_min_sync =
3907 __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3910 max_sync_show(mddev_t *mddev, char *page)
3912 if (mddev->resync_max == MaxSector)
3913 return sprintf(page, "max\n");
3915 return sprintf(page, "%llu\n",
3916 (unsigned long long)mddev->resync_max);
3919 max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3921 if (strncmp(buf, "max", 3) == 0)
3922 mddev->resync_max = MaxSector;
3924 unsigned long long max;
3925 if (strict_strtoull(buf, 10, &max))
3927 if (max < mddev->resync_min)
3929 if (max < mddev->resync_max &&
3931 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3934 /* Must be a multiple of chunk_size */
3935 if (mddev->chunk_sectors) {
3936 sector_t temp = max;
3937 if (sector_div(temp, mddev->chunk_sectors))
3940 mddev->resync_max = max;
3942 wake_up(&mddev->recovery_wait);
3946 static struct md_sysfs_entry md_max_sync =
3947 __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3950 suspend_lo_show(mddev_t *mddev, char *page)
3952 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3956 suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3959 unsigned long long new = simple_strtoull(buf, &e, 10);
3961 if (mddev->pers == NULL ||
3962 mddev->pers->quiesce == NULL)
3964 if (buf == e || (*e && *e != '\n'))
3966 if (new >= mddev->suspend_hi ||
3967 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3968 mddev->suspend_lo = new;
3969 mddev->pers->quiesce(mddev, 2);
3974 static struct md_sysfs_entry md_suspend_lo =
3975 __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3979 suspend_hi_show(mddev_t *mddev, char *page)
3981 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3985 suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3988 unsigned long long new = simple_strtoull(buf, &e, 10);
3990 if (mddev->pers == NULL ||
3991 mddev->pers->quiesce == NULL)
3993 if (buf == e || (*e && *e != '\n'))
3995 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3996 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3997 mddev->suspend_hi = new;
3998 mddev->pers->quiesce(mddev, 1);
3999 mddev->pers->quiesce(mddev, 0);
4004 static struct md_sysfs_entry md_suspend_hi =
4005 __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4008 reshape_position_show(mddev_t *mddev, char *page)
4010 if (mddev->reshape_position != MaxSector)
4011 return sprintf(page, "%llu\n",
4012 (unsigned long long)mddev->reshape_position);
4013 strcpy(page, "none\n");
4018 reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
4021 unsigned long long new = simple_strtoull(buf, &e, 10);
4024 if (buf == e || (*e && *e != '\n'))
4026 mddev->reshape_position = new;
4027 mddev->delta_disks = 0;
4028 mddev->new_level = mddev->level;
4029 mddev->new_layout = mddev->layout;
4030 mddev->new_chunk_sectors = mddev->chunk_sectors;
4034 static struct md_sysfs_entry md_reshape_position =
4035 __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4036 reshape_position_store);
4039 array_size_show(mddev_t *mddev, char *page)
4041 if (mddev->external_size)
4042 return sprintf(page, "%llu\n",
4043 (unsigned long long)mddev->array_sectors/2);
4045 return sprintf(page, "default\n");
4049 array_size_store(mddev_t *mddev, const char *buf, size_t len)
4053 if (strncmp(buf, "default", 7) == 0) {
4055 sectors = mddev->pers->size(mddev, 0, 0);
4057 sectors = mddev->array_sectors;
4059 mddev->external_size = 0;
4061 if (strict_blocks_to_sectors(buf, §ors) < 0)
4063 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
4066 mddev->external_size = 1;
4069 mddev->array_sectors = sectors;
4070 set_capacity(mddev->gendisk, mddev->array_sectors);
4072 revalidate_disk(mddev->gendisk);
4077 static struct md_sysfs_entry md_array_size =
4078 __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4081 static struct attribute *md_default_attrs[] = {
4084 &md_raid_disks.attr,
4085 &md_chunk_size.attr,
4087 &md_resync_start.attr,
4089 &md_new_device.attr,
4090 &md_safe_delay.attr,
4091 &md_array_state.attr,
4092 &md_reshape_position.attr,
4093 &md_array_size.attr,
4094 &max_corr_read_errors.attr,
4098 static struct attribute *md_redundancy_attrs[] = {
4100 &md_mismatches.attr,
4103 &md_sync_speed.attr,
4104 &md_sync_force_parallel.attr,
4105 &md_sync_completed.attr,
4108 &md_suspend_lo.attr,
4109 &md_suspend_hi.attr,
4114 static struct attribute_group md_redundancy_group = {
4116 .attrs = md_redundancy_attrs,
4121 md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4123 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4124 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
4129 rv = mddev_lock(mddev);
4131 rv = entry->show(mddev, page);
4132 mddev_unlock(mddev);
4138 md_attr_store(struct kobject *kobj, struct attribute *attr,
4139 const char *page, size_t length)
4141 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4142 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
4147 if (!capable(CAP_SYS_ADMIN))
4149 rv = mddev_lock(mddev);
4150 if (mddev->hold_active == UNTIL_IOCTL)
4151 mddev->hold_active = 0;
4153 rv = entry->store(mddev, page, length);
4154 mddev_unlock(mddev);
4159 static void md_free(struct kobject *ko)
4161 mddev_t *mddev = container_of(ko, mddev_t, kobj);
4163 if (mddev->sysfs_state)
4164 sysfs_put(mddev->sysfs_state);
4166 if (mddev->gendisk) {
4167 del_gendisk(mddev->gendisk);
4168 put_disk(mddev->gendisk);
4171 blk_cleanup_queue(mddev->queue);
4176 static const struct sysfs_ops md_sysfs_ops = {
4177 .show = md_attr_show,
4178 .store = md_attr_store,
4180 static struct kobj_type md_ktype = {
4182 .sysfs_ops = &md_sysfs_ops,
4183 .default_attrs = md_default_attrs,
4188 static void mddev_delayed_delete(struct work_struct *ws)
4190 mddev_t *mddev = container_of(ws, mddev_t, del_work);
4192 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
4193 kobject_del(&mddev->kobj);
4194 kobject_put(&mddev->kobj);
4197 static int md_alloc(dev_t dev, char *name)
4199 static DEFINE_MUTEX(disks_mutex);
4200 mddev_t *mddev = mddev_find(dev);
4201 struct gendisk *disk;
4210 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4211 shift = partitioned ? MdpMinorShift : 0;
4212 unit = MINOR(mddev->unit) >> shift;
4214 /* wait for any previous instance if this device
4215 * to be completed removed (mddev_delayed_delete).
4217 flush_scheduled_work();
4219 mutex_lock(&disks_mutex);
4225 /* Need to ensure that 'name' is not a duplicate.
4228 spin_lock(&all_mddevs_lock);
4230 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4231 if (mddev2->gendisk &&
4232 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4233 spin_unlock(&all_mddevs_lock);
4236 spin_unlock(&all_mddevs_lock);
4240 mddev->queue = blk_alloc_queue(GFP_KERNEL);
4243 mddev->queue->queuedata = mddev;
4245 /* Can be unlocked because the queue is new: no concurrency */
4246 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue);
4248 blk_queue_make_request(mddev->queue, md_make_request);
4250 disk = alloc_disk(1 << shift);
4252 blk_cleanup_queue(mddev->queue);
4253 mddev->queue = NULL;
4256 disk->major = MAJOR(mddev->unit);
4257 disk->first_minor = unit << shift;
4259 strcpy(disk->disk_name, name);
4260 else if (partitioned)
4261 sprintf(disk->disk_name, "md_d%d", unit);
4263 sprintf(disk->disk_name, "md%d", unit);
4264 disk->fops = &md_fops;
4265 disk->private_data = mddev;
4266 disk->queue = mddev->queue;
4267 /* Allow extended partitions. This makes the
4268 * 'mdp' device redundant, but we can't really
4271 disk->flags |= GENHD_FL_EXT_DEVT;
4273 mddev->gendisk = disk;
4274 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4275 &disk_to_dev(disk)->kobj, "%s", "md");
4277 /* This isn't possible, but as kobject_init_and_add is marked
4278 * __must_check, we must do something with the result
4280 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4284 if (sysfs_create_group(&mddev->kobj, &md_bitmap_group))
4285 printk(KERN_DEBUG "pointless warning\n");
4287 mutex_unlock(&disks_mutex);
4289 kobject_uevent(&mddev->kobj, KOBJ_ADD);
4290 mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, NULL, "array_state");
4296 static struct kobject *md_probe(dev_t dev, int *part, void *data)
4298 md_alloc(dev, NULL);
4302 static int add_named_array(const char *val, struct kernel_param *kp)
4304 /* val must be "md_*" where * is not all digits.
4305 * We allocate an array with a large free minor number, and
4306 * set the name to val. val must not already be an active name.
4308 int len = strlen(val);
4309 char buf[DISK_NAME_LEN];
4311 while (len && val[len-1] == '\n')
4313 if (len >= DISK_NAME_LEN)
4315 strlcpy(buf, val, len+1);
4316 if (strncmp(buf, "md_", 3) != 0)
4318 return md_alloc(0, buf);
4321 static void md_safemode_timeout(unsigned long data)
4323 mddev_t *mddev = (mddev_t *) data;
4325 if (!atomic_read(&mddev->writes_pending)) {
4326 mddev->safemode = 1;
4327 if (mddev->external)
4328 sysfs_notify_dirent(mddev->sysfs_state);
4330 md_wakeup_thread(mddev->thread);
4333 static int start_dirty_degraded;
4335 static int md_run(mddev_t *mddev)
4339 struct mdk_personality *pers;
4341 if (list_empty(&mddev->disks))
4342 /* cannot run an array with no devices.. */
4348 /* These two calls synchronise us with the
4349 * sysfs_remove_group calls in mddev_unlock,
4350 * so they must have completed.
4352 mutex_lock(&mddev->open_mutex);
4353 mutex_unlock(&mddev->open_mutex);
4356 * Analyze all RAID superblock(s)
4358 if (!mddev->raid_disks) {
4359 if (!mddev->persistent)
4364 if (mddev->level != LEVEL_NONE)
4365 request_module("md-level-%d", mddev->level);
4366 else if (mddev->clevel[0])
4367 request_module("md-%s", mddev->clevel);
4370 * Drop all container device buffers, from now on
4371 * the only valid external interface is through the md
4374 list_for_each_entry(rdev, &mddev->disks, same_set) {
4375 if (test_bit(Faulty, &rdev->flags))
4377 sync_blockdev(rdev->bdev);
4378 invalidate_bdev(rdev->bdev);
4380 /* perform some consistency tests on the device.
4381 * We don't want the data to overlap the metadata,
4382 * Internal Bitmap issues have been handled elsewhere.
4384 if (rdev->data_offset < rdev->sb_start) {
4385 if (mddev->dev_sectors &&
4386 rdev->data_offset + mddev->dev_sectors
4388 printk("md: %s: data overlaps metadata\n",
4393 if (rdev->sb_start + rdev->sb_size/512
4394 > rdev->data_offset) {
4395 printk("md: %s: metadata overlaps data\n",
4400 sysfs_notify_dirent(rdev->sysfs_state);
4403 spin_lock(&pers_lock);
4404 pers = find_pers(mddev->level, mddev->clevel);
4405 if (!pers || !try_module_get(pers->owner)) {
4406 spin_unlock(&pers_lock);
4407 if (mddev->level != LEVEL_NONE)
4408 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4411 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4416 spin_unlock(&pers_lock);
4417 if (mddev->level != pers->level) {
4418 mddev->level = pers->level;
4419 mddev->new_level = pers->level;
4421 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
4423 if (mddev->reshape_position != MaxSector &&
4424 pers->start_reshape == NULL) {
4425 /* This personality cannot handle reshaping... */
4427 module_put(pers->owner);
4431 if (pers->sync_request) {
4432 /* Warn if this is a potentially silly
4435 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4439 list_for_each_entry(rdev, &mddev->disks, same_set)
4440 list_for_each_entry(rdev2, &mddev->disks, same_set) {
4442 rdev->bdev->bd_contains ==
4443 rdev2->bdev->bd_contains) {
4445 "%s: WARNING: %s appears to be"
4446 " on the same physical disk as"
4449 bdevname(rdev->bdev,b),
4450 bdevname(rdev2->bdev,b2));
4457 "True protection against single-disk"
4458 " failure might be compromised.\n");
4461 mddev->recovery = 0;
4462 /* may be over-ridden by personality */
4463 mddev->resync_max_sectors = mddev->dev_sectors;
4465 mddev->barriers_work = 1;
4466 mddev->ok_start_degraded = start_dirty_degraded;
4468 if (start_readonly && mddev->ro == 0)
4469 mddev->ro = 2; /* read-only, but switch on first write */
4471 err = mddev->pers->run(mddev);
4473 printk(KERN_ERR "md: pers->run() failed ...\n");
4474 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4475 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4476 " but 'external_size' not in effect?\n", __func__);
4478 "md: invalid array_size %llu > default size %llu\n",
4479 (unsigned long long)mddev->array_sectors / 2,
4480 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4482 mddev->pers->stop(mddev);
4484 if (err == 0 && mddev->pers->sync_request) {
4485 err = bitmap_create(mddev);
4487 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4488 mdname(mddev), err);
4489 mddev->pers->stop(mddev);
4493 module_put(mddev->pers->owner);
4495 bitmap_destroy(mddev);
4498 if (mddev->pers->sync_request) {
4499 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
4501 "md: cannot register extra attributes for %s\n",
4503 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action");
4504 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
4507 atomic_set(&mddev->writes_pending,0);
4508 atomic_set(&mddev->max_corr_read_errors,
4509 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
4510 mddev->safemode = 0;
4511 mddev->safemode_timer.function = md_safemode_timeout;
4512 mddev->safemode_timer.data = (unsigned long) mddev;
4513 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
4516 list_for_each_entry(rdev, &mddev->disks, same_set)
4517 if (rdev->raid_disk >= 0) {
4519 sprintf(nm, "rd%d", rdev->raid_disk);
4520 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
4521 printk("md: cannot register %s for %s\n",
4525 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4528 md_update_sb(mddev, 0);
4530 md_wakeup_thread(mddev->thread);
4531 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
4533 md_new_event(mddev);
4534 sysfs_notify_dirent(mddev->sysfs_state);
4535 if (mddev->sysfs_action)
4536 sysfs_notify_dirent(mddev->sysfs_action);
4537 sysfs_notify(&mddev->kobj, NULL, "degraded");
4541 static int do_md_run(mddev_t *mddev)
4545 err = md_run(mddev);
4549 set_capacity(mddev->gendisk, mddev->array_sectors);
4550 revalidate_disk(mddev->gendisk);
4551 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4556 static int restart_array(mddev_t *mddev)
4558 struct gendisk *disk = mddev->gendisk;
4560 /* Complain if it has no devices */
4561 if (list_empty(&mddev->disks))
4567 mddev->safemode = 0;
4569 set_disk_ro(disk, 0);
4570 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4572 /* Kick recovery or resync if necessary */
4573 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4574 md_wakeup_thread(mddev->thread);
4575 md_wakeup_thread(mddev->sync_thread);
4576 sysfs_notify_dirent(mddev->sysfs_state);
4580 /* similar to deny_write_access, but accounts for our holding a reference
4581 * to the file ourselves */
4582 static int deny_bitmap_write_access(struct file * file)
4584 struct inode *inode = file->f_mapping->host;
4586 spin_lock(&inode->i_lock);
4587 if (atomic_read(&inode->i_writecount) > 1) {
4588 spin_unlock(&inode->i_lock);
4591 atomic_set(&inode->i_writecount, -1);
4592 spin_unlock(&inode->i_lock);
4597 void restore_bitmap_write_access(struct file *file)
4599 struct inode *inode = file->f_mapping->host;
4601 spin_lock(&inode->i_lock);
4602 atomic_set(&inode->i_writecount, 1);
4603 spin_unlock(&inode->i_lock);
4606 static void md_clean(mddev_t *mddev)
4608 mddev->array_sectors = 0;
4609 mddev->external_size = 0;
4610 mddev->dev_sectors = 0;
4611 mddev->raid_disks = 0;
4612 mddev->recovery_cp = 0;
4613 mddev->resync_min = 0;
4614 mddev->resync_max = MaxSector;
4615 mddev->reshape_position = MaxSector;
4616 mddev->external = 0;
4617 mddev->persistent = 0;
4618 mddev->level = LEVEL_NONE;
4619 mddev->clevel[0] = 0;
4622 mddev->metadata_type[0] = 0;
4623 mddev->chunk_sectors = 0;
4624 mddev->ctime = mddev->utime = 0;
4626 mddev->max_disks = 0;
4628 mddev->can_decrease_events = 0;
4629 mddev->delta_disks = 0;
4630 mddev->new_level = LEVEL_NONE;
4631 mddev->new_layout = 0;
4632 mddev->new_chunk_sectors = 0;
4633 mddev->curr_resync = 0;
4634 mddev->resync_mismatches = 0;
4635 mddev->suspend_lo = mddev->suspend_hi = 0;
4636 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4637 mddev->recovery = 0;
4639 mddev->degraded = 0;
4640 mddev->barriers_work = 0;
4641 mddev->safemode = 0;
4642 mddev->bitmap_info.offset = 0;
4643 mddev->bitmap_info.default_offset = 0;
4644 mddev->bitmap_info.chunksize = 0;
4645 mddev->bitmap_info.daemon_sleep = 0;
4646 mddev->bitmap_info.max_write_behind = 0;
4649 static void md_stop_writes(mddev_t *mddev)
4651 if (mddev->sync_thread) {
4652 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4653 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4654 md_unregister_thread(mddev->sync_thread);
4655 mddev->sync_thread = NULL;
4658 del_timer_sync(&mddev->safemode_timer);
4660 bitmap_flush(mddev);
4661 md_super_wait(mddev);
4663 if (!mddev->in_sync || mddev->flags) {
4664 /* mark array as shutdown cleanly */
4666 md_update_sb(mddev, 1);
4670 static void md_stop(mddev_t *mddev)
4672 md_stop_writes(mddev);
4674 mddev->pers->stop(mddev);
4675 if (mddev->pers->sync_request && mddev->to_remove == NULL)
4676 mddev->to_remove = &md_redundancy_group;
4677 module_put(mddev->pers->owner);
4679 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4682 static int md_set_readonly(mddev_t *mddev, int is_open)
4685 mutex_lock(&mddev->open_mutex);
4686 if (atomic_read(&mddev->openers) > is_open) {
4687 printk("md: %s still in use.\n",mdname(mddev));
4692 md_stop_writes(mddev);
4698 set_disk_ro(mddev->gendisk, 1);
4699 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4700 sysfs_notify_dirent(mddev->sysfs_state);
4704 mutex_unlock(&mddev->open_mutex);
4709 * 0 - completely stop and dis-assemble array
4710 * 2 - stop but do not disassemble array
4712 static int do_md_stop(mddev_t * mddev, int mode, int is_open)
4715 struct gendisk *disk = mddev->gendisk;
4718 mutex_lock(&mddev->open_mutex);
4719 if (atomic_read(&mddev->openers) > is_open) {
4720 printk("md: %s still in use.\n",mdname(mddev));
4722 } else if (mddev->pers) {
4725 set_disk_ro(disk, 0);
4728 mddev->queue->merge_bvec_fn = NULL;
4729 mddev->queue->unplug_fn = NULL;
4730 mddev->queue->backing_dev_info.congested_fn = NULL;
4732 /* tell userspace to handle 'inactive' */
4733 sysfs_notify_dirent(mddev->sysfs_state);
4735 list_for_each_entry(rdev, &mddev->disks, same_set)
4736 if (rdev->raid_disk >= 0) {
4738 sprintf(nm, "rd%d", rdev->raid_disk);
4739 sysfs_remove_link(&mddev->kobj, nm);
4742 set_capacity(disk, 0);
4743 revalidate_disk(disk);
4750 mutex_unlock(&mddev->open_mutex);
4754 * Free resources if final stop
4758 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4760 bitmap_destroy(mddev);
4761 if (mddev->bitmap_info.file) {
4762 restore_bitmap_write_access(mddev->bitmap_info.file);
4763 fput(mddev->bitmap_info.file);
4764 mddev->bitmap_info.file = NULL;
4766 mddev->bitmap_info.offset = 0;
4768 export_array(mddev);
4771 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4772 if (mddev->hold_active == UNTIL_STOP)
4773 mddev->hold_active = 0;
4777 blk_integrity_unregister(disk);
4778 md_new_event(mddev);
4779 sysfs_notify_dirent(mddev->sysfs_state);
4784 static void autorun_array(mddev_t *mddev)
4789 if (list_empty(&mddev->disks))
4792 printk(KERN_INFO "md: running: ");
4794 list_for_each_entry(rdev, &mddev->disks, same_set) {
4795 char b[BDEVNAME_SIZE];
4796 printk("<%s>", bdevname(rdev->bdev,b));
4800 err = do_md_run(mddev);
4802 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
4803 do_md_stop(mddev, 0, 0);
4808 * lets try to run arrays based on all disks that have arrived
4809 * until now. (those are in pending_raid_disks)
4811 * the method: pick the first pending disk, collect all disks with
4812 * the same UUID, remove all from the pending list and put them into
4813 * the 'same_array' list. Then order this list based on superblock
4814 * update time (freshest comes first), kick out 'old' disks and
4815 * compare superblocks. If everything's fine then run it.
4817 * If "unit" is allocated, then bump its reference count
4819 static void autorun_devices(int part)
4821 mdk_rdev_t *rdev0, *rdev, *tmp;
4823 char b[BDEVNAME_SIZE];
4825 printk(KERN_INFO "md: autorun ...\n");
4826 while (!list_empty(&pending_raid_disks)) {
4829 LIST_HEAD(candidates);
4830 rdev0 = list_entry(pending_raid_disks.next,
4831 mdk_rdev_t, same_set);
4833 printk(KERN_INFO "md: considering %s ...\n",
4834 bdevname(rdev0->bdev,b));
4835 INIT_LIST_HEAD(&candidates);
4836 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
4837 if (super_90_load(rdev, rdev0, 0) >= 0) {
4838 printk(KERN_INFO "md: adding %s ...\n",
4839 bdevname(rdev->bdev,b));
4840 list_move(&rdev->same_set, &candidates);
4843 * now we have a set of devices, with all of them having
4844 * mostly sane superblocks. It's time to allocate the
4848 dev = MKDEV(mdp_major,
4849 rdev0->preferred_minor << MdpMinorShift);
4850 unit = MINOR(dev) >> MdpMinorShift;
4852 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4855 if (rdev0->preferred_minor != unit) {
4856 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4857 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4861 md_probe(dev, NULL, NULL);
4862 mddev = mddev_find(dev);
4863 if (!mddev || !mddev->gendisk) {
4867 "md: cannot allocate memory for md drive.\n");
4870 if (mddev_lock(mddev))
4871 printk(KERN_WARNING "md: %s locked, cannot run\n",
4873 else if (mddev->raid_disks || mddev->major_version
4874 || !list_empty(&mddev->disks)) {
4876 "md: %s already running, cannot run %s\n",
4877 mdname(mddev), bdevname(rdev0->bdev,b));
4878 mddev_unlock(mddev);
4880 printk(KERN_INFO "md: created %s\n", mdname(mddev));
4881 mddev->persistent = 1;
4882 rdev_for_each_list(rdev, tmp, &candidates) {
4883 list_del_init(&rdev->same_set);
4884 if (bind_rdev_to_array(rdev, mddev))
4887 autorun_array(mddev);
4888 mddev_unlock(mddev);
4890 /* on success, candidates will be empty, on error
4893 rdev_for_each_list(rdev, tmp, &candidates) {
4894 list_del_init(&rdev->same_set);
4899 printk(KERN_INFO "md: ... autorun DONE.\n");
4901 #endif /* !MODULE */
4903 static int get_version(void __user * arg)
4907 ver.major = MD_MAJOR_VERSION;
4908 ver.minor = MD_MINOR_VERSION;
4909 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4911 if (copy_to_user(arg, &ver, sizeof(ver)))
4917 static int get_array_info(mddev_t * mddev, void __user * arg)
4919 mdu_array_info_t info;
4920 int nr,working,insync,failed,spare;
4923 nr=working=insync=failed=spare=0;
4924 list_for_each_entry(rdev, &mddev->disks, same_set) {
4926 if (test_bit(Faulty, &rdev->flags))
4930 if (test_bit(In_sync, &rdev->flags))
4937 info.major_version = mddev->major_version;
4938 info.minor_version = mddev->minor_version;
4939 info.patch_version = MD_PATCHLEVEL_VERSION;
4940 info.ctime = mddev->ctime;
4941 info.level = mddev->level;
4942 info.size = mddev->dev_sectors / 2;
4943 if (info.size != mddev->dev_sectors / 2) /* overflow */
4946 info.raid_disks = mddev->raid_disks;
4947 info.md_minor = mddev->md_minor;
4948 info.not_persistent= !mddev->persistent;
4950 info.utime = mddev->utime;
4953 info.state = (1<<MD_SB_CLEAN);
4954 if (mddev->bitmap && mddev->bitmap_info.offset)
4955 info.state = (1<<MD_SB_BITMAP_PRESENT);
4956 info.active_disks = insync;
4957 info.working_disks = working;
4958 info.failed_disks = failed;
4959 info.spare_disks = spare;
4961 info.layout = mddev->layout;
4962 info.chunk_size = mddev->chunk_sectors << 9;
4964 if (copy_to_user(arg, &info, sizeof(info)))
4970 static int get_bitmap_file(mddev_t * mddev, void __user * arg)
4972 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4973 char *ptr, *buf = NULL;
4976 if (md_allow_write(mddev))
4977 file = kmalloc(sizeof(*file), GFP_NOIO);
4979 file = kmalloc(sizeof(*file), GFP_KERNEL);
4984 /* bitmap disabled, zero the first byte and copy out */
4985 if (!mddev->bitmap || !mddev->bitmap->file) {
4986 file->pathname[0] = '\0';
4990 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4994 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4998 strcpy(file->pathname, ptr);
5002 if (copy_to_user(arg, file, sizeof(*file)))
5010 static int get_disk_info(mddev_t * mddev, void __user * arg)
5012 mdu_disk_info_t info;
5015 if (copy_from_user(&info, arg, sizeof(info)))
5018 rdev = find_rdev_nr(mddev, info.number);
5020 info.major = MAJOR(rdev->bdev->bd_dev);
5021 info.minor = MINOR(rdev->bdev->bd_dev);
5022 info.raid_disk = rdev->raid_disk;
5024 if (test_bit(Faulty, &rdev->flags))
5025 info.state |= (1<<MD_DISK_FAULTY);
5026 else if (test_bit(In_sync, &rdev->flags)) {
5027 info.state |= (1<<MD_DISK_ACTIVE);
5028 info.state |= (1<<MD_DISK_SYNC);
5030 if (test_bit(WriteMostly, &rdev->flags))
5031 info.state |= (1<<MD_DISK_WRITEMOSTLY);
5033 info.major = info.minor = 0;
5034 info.raid_disk = -1;
5035 info.state = (1<<MD_DISK_REMOVED);
5038 if (copy_to_user(arg, &info, sizeof(info)))
5044 static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
5046 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
5048 dev_t dev = MKDEV(info->major,info->minor);
5050 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5053 if (!mddev->raid_disks) {
5055 /* expecting a device which has a superblock */
5056 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5059 "md: md_import_device returned %ld\n",
5061 return PTR_ERR(rdev);
5063 if (!list_empty(&mddev->disks)) {
5064 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
5065 mdk_rdev_t, same_set);
5066 err = super_types[mddev->major_version]
5067 .load_super(rdev, rdev0, mddev->minor_version);
5070 "md: %s has different UUID to %s\n",
5071 bdevname(rdev->bdev,b),
5072 bdevname(rdev0->bdev,b2));
5077 err = bind_rdev_to_array(rdev, mddev);
5084 * add_new_disk can be used once the array is assembled
5085 * to add "hot spares". They must already have a superblock
5090 if (!mddev->pers->hot_add_disk) {
5092 "%s: personality does not support diskops!\n",
5096 if (mddev->persistent)
5097 rdev = md_import_device(dev, mddev->major_version,
5098 mddev->minor_version);
5100 rdev = md_import_device(dev, -1, -1);
5103 "md: md_import_device returned %ld\n",
5105 return PTR_ERR(rdev);
5107 /* set save_raid_disk if appropriate */
5108 if (!mddev->persistent) {
5109 if (info->state & (1<<MD_DISK_SYNC) &&
5110 info->raid_disk < mddev->raid_disks)
5111 rdev->raid_disk = info->raid_disk;
5113 rdev->raid_disk = -1;
5115 super_types[mddev->major_version].
5116 validate_super(mddev, rdev);
5117 rdev->saved_raid_disk = rdev->raid_disk;
5119 clear_bit(In_sync, &rdev->flags); /* just to be sure */
5120 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5121 set_bit(WriteMostly, &rdev->flags);
5123 clear_bit(WriteMostly, &rdev->flags);
5125 rdev->raid_disk = -1;
5126 err = bind_rdev_to_array(rdev, mddev);
5127 if (!err && !mddev->pers->hot_remove_disk) {
5128 /* If there is hot_add_disk but no hot_remove_disk
5129 * then added disks for geometry changes,
5130 * and should be added immediately.
5132 super_types[mddev->major_version].
5133 validate_super(mddev, rdev);
5134 err = mddev->pers->hot_add_disk(mddev, rdev);
5136 unbind_rdev_from_array(rdev);
5141 sysfs_notify_dirent(rdev->sysfs_state);
5143 md_update_sb(mddev, 1);
5144 if (mddev->degraded)
5145 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
5146 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5147 md_wakeup_thread(mddev->thread);
5151 /* otherwise, add_new_disk is only allowed
5152 * for major_version==0 superblocks
5154 if (mddev->major_version != 0) {
5155 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5160 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5162 rdev = md_import_device(dev, -1, 0);
5165 "md: error, md_import_device() returned %ld\n",
5167 return PTR_ERR(rdev);
5169 rdev->desc_nr = info->number;
5170 if (info->raid_disk < mddev->raid_disks)
5171 rdev->raid_disk = info->raid_disk;
5173 rdev->raid_disk = -1;
5175 if (rdev->raid_disk < mddev->raid_disks)
5176 if (info->state & (1<<MD_DISK_SYNC))
5177 set_bit(In_sync, &rdev->flags);
5179 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5180 set_bit(WriteMostly, &rdev->flags);
5182 if (!mddev->persistent) {
5183 printk(KERN_INFO "md: nonpersistent superblock ...\n");
5184 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
5186 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
5187 rdev->sectors = rdev->sb_start;
5189 err = bind_rdev_to_array(rdev, mddev);
5199 static int hot_remove_disk(mddev_t * mddev, dev_t dev)
5201 char b[BDEVNAME_SIZE];
5204 rdev = find_rdev(mddev, dev);
5208 if (rdev->raid_disk >= 0)
5211 kick_rdev_from_array(rdev);
5212 md_update_sb(mddev, 1);
5213 md_new_event(mddev);
5217 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
5218 bdevname(rdev->bdev,b), mdname(mddev));
5222 static int hot_add_disk(mddev_t * mddev, dev_t dev)
5224 char b[BDEVNAME_SIZE];
5231 if (mddev->major_version != 0) {
5232 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5233 " version-0 superblocks.\n",
5237 if (!mddev->pers->hot_add_disk) {
5239 "%s: personality does not support diskops!\n",
5244 rdev = md_import_device(dev, -1, 0);
5247 "md: error, md_import_device() returned %ld\n",
5252 if (mddev->persistent)
5253 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
5255 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
5257 rdev->sectors = rdev->sb_start;
5259 if (test_bit(Faulty, &rdev->flags)) {
5261 "md: can not hot-add faulty %s disk to %s!\n",
5262 bdevname(rdev->bdev,b), mdname(mddev));
5266 clear_bit(In_sync, &rdev->flags);
5268 rdev->saved_raid_disk = -1;
5269 err = bind_rdev_to_array(rdev, mddev);
5274 * The rest should better be atomic, we can have disk failures
5275 * noticed in interrupt contexts ...
5278 rdev->raid_disk = -1;
5280 md_update_sb(mddev, 1);
5283 * Kick recovery, maybe this spare has to be added to the
5284 * array immediately.
5286 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5287 md_wakeup_thread(mddev->thread);
5288 md_new_event(mddev);
5296 static int set_bitmap_file(mddev_t *mddev, int fd)
5301 if (!mddev->pers->quiesce)
5303 if (mddev->recovery || mddev->sync_thread)
5305 /* we should be able to change the bitmap.. */
5311 return -EEXIST; /* cannot add when bitmap is present */
5312 mddev->bitmap_info.file = fget(fd);
5314 if (mddev->bitmap_info.file == NULL) {
5315 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5320 err = deny_bitmap_write_access(mddev->bitmap_info.file);
5322 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5324 fput(mddev->bitmap_info.file);
5325 mddev->bitmap_info.file = NULL;
5328 mddev->bitmap_info.offset = 0; /* file overrides offset */
5329 } else if (mddev->bitmap == NULL)
5330 return -ENOENT; /* cannot remove what isn't there */
5333 mddev->pers->quiesce(mddev, 1);
5335 err = bitmap_create(mddev);
5336 if (fd < 0 || err) {
5337 bitmap_destroy(mddev);
5338 fd = -1; /* make sure to put the file */
5340 mddev->pers->quiesce(mddev, 0);
5343 if (mddev->bitmap_info.file) {
5344 restore_bitmap_write_access(mddev->bitmap_info.file);
5345 fput(mddev->bitmap_info.file);
5347 mddev->bitmap_info.file = NULL;
5354 * set_array_info is used two different ways
5355 * The original usage is when creating a new array.
5356 * In this usage, raid_disks is > 0 and it together with
5357 * level, size, not_persistent,layout,chunksize determine the
5358 * shape of the array.
5359 * This will always create an array with a type-0.90.0 superblock.
5360 * The newer usage is when assembling an array.
5361 * In this case raid_disks will be 0, and the major_version field is
5362 * use to determine which style super-blocks are to be found on the devices.
5363 * The minor and patch _version numbers are also kept incase the
5364 * super_block handler wishes to interpret them.
5366 static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5369 if (info->raid_disks == 0) {
5370 /* just setting version number for superblock loading */
5371 if (info->major_version < 0 ||
5372 info->major_version >= ARRAY_SIZE(super_types) ||
5373 super_types[info->major_version].name == NULL) {
5374 /* maybe try to auto-load a module? */
5376 "md: superblock version %d not known\n",
5377 info->major_version);
5380 mddev->major_version = info->major_version;
5381 mddev->minor_version = info->minor_version;
5382 mddev->patch_version = info->patch_version;
5383 mddev->persistent = !info->not_persistent;
5384 /* ensure mddev_put doesn't delete this now that there
5385 * is some minimal configuration.
5387 mddev->ctime = get_seconds();
5390 mddev->major_version = MD_MAJOR_VERSION;
5391 mddev->minor_version = MD_MINOR_VERSION;
5392 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5393 mddev->ctime = get_seconds();
5395 mddev->level = info->level;
5396 mddev->clevel[0] = 0;
5397 mddev->dev_sectors = 2 * (sector_t)info->size;
5398 mddev->raid_disks = info->raid_disks;
5399 /* don't set md_minor, it is determined by which /dev/md* was
5402 if (info->state & (1<<MD_SB_CLEAN))
5403 mddev->recovery_cp = MaxSector;
5405 mddev->recovery_cp = 0;
5406 mddev->persistent = ! info->not_persistent;
5407 mddev->external = 0;
5409 mddev->layout = info->layout;
5410 mddev->chunk_sectors = info->chunk_size >> 9;
5412 mddev->max_disks = MD_SB_DISKS;
5414 if (mddev->persistent)
5416 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5418 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5419 mddev->bitmap_info.offset = 0;
5421 mddev->reshape_position = MaxSector;
5424 * Generate a 128 bit UUID
5426 get_random_bytes(mddev->uuid, 16);
5428 mddev->new_level = mddev->level;
5429 mddev->new_chunk_sectors = mddev->chunk_sectors;
5430 mddev->new_layout = mddev->layout;
5431 mddev->delta_disks = 0;
5436 void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5438 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5440 if (mddev->external_size)
5443 mddev->array_sectors = array_sectors;
5445 EXPORT_SYMBOL(md_set_array_sectors);
5447 static int update_size(mddev_t *mddev, sector_t num_sectors)
5451 int fit = (num_sectors == 0);
5453 if (mddev->pers->resize == NULL)
5455 /* The "num_sectors" is the number of sectors of each device that
5456 * is used. This can only make sense for arrays with redundancy.
5457 * linear and raid0 always use whatever space is available. We can only
5458 * consider changing this number if no resync or reconstruction is
5459 * happening, and if the new size is acceptable. It must fit before the
5460 * sb_start or, if that is <data_offset, it must fit before the size
5461 * of each device. If num_sectors is zero, we find the largest size
5465 if (mddev->sync_thread)
5468 /* Sorry, cannot grow a bitmap yet, just remove it,
5472 list_for_each_entry(rdev, &mddev->disks, same_set) {
5473 sector_t avail = rdev->sectors;
5475 if (fit && (num_sectors == 0 || num_sectors > avail))
5476 num_sectors = avail;
5477 if (avail < num_sectors)
5480 rv = mddev->pers->resize(mddev, num_sectors);
5482 revalidate_disk(mddev->gendisk);
5486 static int update_raid_disks(mddev_t *mddev, int raid_disks)
5489 /* change the number of raid disks */
5490 if (mddev->pers->check_reshape == NULL)
5492 if (raid_disks <= 0 ||
5493 (mddev->max_disks && raid_disks >= mddev->max_disks))
5495 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
5497 mddev->delta_disks = raid_disks - mddev->raid_disks;
5499 rv = mddev->pers->check_reshape(mddev);
5505 * update_array_info is used to change the configuration of an
5507 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5508 * fields in the info are checked against the array.
5509 * Any differences that cannot be handled will cause an error.
5510 * Normally, only one change can be managed at a time.
5512 static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5518 /* calculate expected state,ignoring low bits */
5519 if (mddev->bitmap && mddev->bitmap_info.offset)
5520 state |= (1 << MD_SB_BITMAP_PRESENT);
5522 if (mddev->major_version != info->major_version ||
5523 mddev->minor_version != info->minor_version ||
5524 /* mddev->patch_version != info->patch_version || */
5525 mddev->ctime != info->ctime ||
5526 mddev->level != info->level ||
5527 /* mddev->layout != info->layout || */
5528 !mddev->persistent != info->not_persistent||
5529 mddev->chunk_sectors != info->chunk_size >> 9 ||
5530 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5531 ((state^info->state) & 0xfffffe00)
5534 /* Check there is only one change */
5535 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5537 if (mddev->raid_disks != info->raid_disks)
5539 if (mddev->layout != info->layout)
5541 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5548 if (mddev->layout != info->layout) {
5550 * we don't need to do anything at the md level, the
5551 * personality will take care of it all.
5553 if (mddev->pers->check_reshape == NULL)
5556 mddev->new_layout = info->layout;
5557 rv = mddev->pers->check_reshape(mddev);
5559 mddev->new_layout = mddev->layout;
5563 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5564 rv = update_size(mddev, (sector_t)info->size * 2);
5566 if (mddev->raid_disks != info->raid_disks)
5567 rv = update_raid_disks(mddev, info->raid_disks);
5569 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5570 if (mddev->pers->quiesce == NULL)
5572 if (mddev->recovery || mddev->sync_thread)
5574 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5575 /* add the bitmap */
5578 if (mddev->bitmap_info.default_offset == 0)
5580 mddev->bitmap_info.offset =
5581 mddev->bitmap_info.default_offset;
5582 mddev->pers->quiesce(mddev, 1);
5583 rv = bitmap_create(mddev);
5585 bitmap_destroy(mddev);
5586 mddev->pers->quiesce(mddev, 0);
5588 /* remove the bitmap */
5591 if (mddev->bitmap->file)
5593 mddev->pers->quiesce(mddev, 1);
5594 bitmap_destroy(mddev);
5595 mddev->pers->quiesce(mddev, 0);
5596 mddev->bitmap_info.offset = 0;
5599 md_update_sb(mddev, 1);
5603 static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5607 if (mddev->pers == NULL)
5610 rdev = find_rdev(mddev, dev);
5614 md_error(mddev, rdev);
5619 * We have a problem here : there is no easy way to give a CHS
5620 * virtual geometry. We currently pretend that we have a 2 heads
5621 * 4 sectors (with a BIG number of cylinders...). This drives
5622 * dosfs just mad... ;-)
5624 static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5626 mddev_t *mddev = bdev->bd_disk->private_data;
5630 geo->cylinders = mddev->array_sectors / 8;
5634 static int md_ioctl(struct block_device *bdev, fmode_t mode,
5635 unsigned int cmd, unsigned long arg)
5638 void __user *argp = (void __user *)arg;
5639 mddev_t *mddev = NULL;
5642 if (!capable(CAP_SYS_ADMIN))
5646 * Commands dealing with the RAID driver but not any
5652 err = get_version(argp);
5655 case PRINT_RAID_DEBUG:
5663 autostart_arrays(arg);
5670 * Commands creating/starting a new array:
5673 mddev = bdev->bd_disk->private_data;
5680 err = mddev_lock(mddev);
5683 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5690 case SET_ARRAY_INFO:
5692 mdu_array_info_t info;
5694 memset(&info, 0, sizeof(info));
5695 else if (copy_from_user(&info, argp, sizeof(info))) {
5700 err = update_array_info(mddev, &info);
5702 printk(KERN_WARNING "md: couldn't update"
5703 " array info. %d\n", err);
5708 if (!list_empty(&mddev->disks)) {
5710 "md: array %s already has disks!\n",
5715 if (mddev->raid_disks) {
5717 "md: array %s already initialised!\n",
5722 err = set_array_info(mddev, &info);
5724 printk(KERN_WARNING "md: couldn't set"
5725 " array info. %d\n", err);
5735 * Commands querying/configuring an existing array:
5737 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
5738 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
5739 if ((!mddev->raid_disks && !mddev->external)
5740 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5741 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5742 && cmd != GET_BITMAP_FILE) {
5748 * Commands even a read-only array can execute:
5752 case GET_ARRAY_INFO:
5753 err = get_array_info(mddev, argp);
5756 case GET_BITMAP_FILE:
5757 err = get_bitmap_file(mddev, argp);
5761 err = get_disk_info(mddev, argp);
5764 case RESTART_ARRAY_RW:
5765 err = restart_array(mddev);
5769 err = do_md_stop(mddev, 0, 1);
5773 err = md_set_readonly(mddev, 1);
5777 if (get_user(ro, (int __user *)(arg))) {
5783 /* if the bdev is going readonly the value of mddev->ro
5784 * does not matter, no writes are coming
5789 /* are we are already prepared for writes? */
5793 /* transitioning to readauto need only happen for
5794 * arrays that call md_write_start
5797 err = restart_array(mddev);
5800 set_disk_ro(mddev->gendisk, 0);
5807 * The remaining ioctls are changing the state of the
5808 * superblock, so we do not allow them on read-only arrays.
5809 * However non-MD ioctls (e.g. get-size) will still come through
5810 * here and hit the 'default' below, so only disallow
5811 * 'md' ioctls, and switch to rw mode if started auto-readonly.
5813 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
5814 if (mddev->ro == 2) {
5816 sysfs_notify_dirent(mddev->sysfs_state);
5817 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5818 md_wakeup_thread(mddev->thread);
5829 mdu_disk_info_t info;
5830 if (copy_from_user(&info, argp, sizeof(info)))
5833 err = add_new_disk(mddev, &info);
5837 case HOT_REMOVE_DISK:
5838 err = hot_remove_disk(mddev, new_decode_dev(arg));
5842 err = hot_add_disk(mddev, new_decode_dev(arg));
5845 case SET_DISK_FAULTY:
5846 err = set_disk_faulty(mddev, new_decode_dev(arg));
5850 err = do_md_run(mddev);
5853 case SET_BITMAP_FILE:
5854 err = set_bitmap_file(mddev, (int)arg);
5864 if (mddev->hold_active == UNTIL_IOCTL &&
5866 mddev->hold_active = 0;
5867 mddev_unlock(mddev);
5876 #ifdef CONFIG_COMPAT
5877 static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
5878 unsigned int cmd, unsigned long arg)
5881 case HOT_REMOVE_DISK:
5883 case SET_DISK_FAULTY:
5884 case SET_BITMAP_FILE:
5885 /* These take in integer arg, do not convert */
5888 arg = (unsigned long)compat_ptr(arg);
5892 return md_ioctl(bdev, mode, cmd, arg);
5894 #endif /* CONFIG_COMPAT */
5896 static int md_open(struct block_device *bdev, fmode_t mode)
5899 * Succeed if we can lock the mddev, which confirms that
5900 * it isn't being stopped right now.
5902 mddev_t *mddev = mddev_find(bdev->bd_dev);
5905 if (mddev->gendisk != bdev->bd_disk) {
5906 /* we are racing with mddev_put which is discarding this
5910 /* Wait until bdev->bd_disk is definitely gone */
5911 flush_scheduled_work();
5912 /* Then retry the open from the top */
5913 return -ERESTARTSYS;
5915 BUG_ON(mddev != bdev->bd_disk->private_data);
5917 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
5921 atomic_inc(&mddev->openers);
5922 mutex_unlock(&mddev->open_mutex);
5924 check_disk_size_change(mddev->gendisk, bdev);
5929 static int md_release(struct gendisk *disk, fmode_t mode)
5931 mddev_t *mddev = disk->private_data;
5934 atomic_dec(&mddev->openers);
5939 static const struct block_device_operations md_fops =
5941 .owner = THIS_MODULE,
5943 .release = md_release,
5945 #ifdef CONFIG_COMPAT
5946 .compat_ioctl = md_compat_ioctl,
5948 .getgeo = md_getgeo,
5951 static int md_thread(void * arg)
5953 mdk_thread_t *thread = arg;
5956 * md_thread is a 'system-thread', it's priority should be very
5957 * high. We avoid resource deadlocks individually in each
5958 * raid personality. (RAID5 does preallocation) We also use RR and
5959 * the very same RT priority as kswapd, thus we will never get
5960 * into a priority inversion deadlock.
5962 * we definitely have to have equal or higher priority than
5963 * bdflush, otherwise bdflush will deadlock if there are too
5964 * many dirty RAID5 blocks.
5967 allow_signal(SIGKILL);
5968 while (!kthread_should_stop()) {
5970 /* We need to wait INTERRUPTIBLE so that
5971 * we don't add to the load-average.
5972 * That means we need to be sure no signals are
5975 if (signal_pending(current))
5976 flush_signals(current);
5978 wait_event_interruptible_timeout
5980 test_bit(THREAD_WAKEUP, &thread->flags)
5981 || kthread_should_stop(),
5984 clear_bit(THREAD_WAKEUP, &thread->flags);
5986 thread->run(thread->mddev);
5992 void md_wakeup_thread(mdk_thread_t *thread)
5995 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5996 set_bit(THREAD_WAKEUP, &thread->flags);
5997 wake_up(&thread->wqueue);
6001 mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
6004 mdk_thread_t *thread;
6006 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
6010 init_waitqueue_head(&thread->wqueue);
6013 thread->mddev = mddev;
6014 thread->timeout = MAX_SCHEDULE_TIMEOUT;
6015 thread->tsk = kthread_run(md_thread, thread,
6017 mdname(thread->mddev),
6018 name ?: mddev->pers->name);
6019 if (IS_ERR(thread->tsk)) {
6026 void md_unregister_thread(mdk_thread_t *thread)
6030 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
6032 kthread_stop(thread->tsk);
6036 void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
6043 if (!rdev || test_bit(Faulty, &rdev->flags))
6046 if (mddev->external)
6047 set_bit(Blocked, &rdev->flags);
6049 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
6051 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
6052 __builtin_return_address(0),__builtin_return_address(1),
6053 __builtin_return_address(2),__builtin_return_address(3));
6057 if (!mddev->pers->error_handler)
6059 mddev->pers->error_handler(mddev,rdev);
6060 if (mddev->degraded)
6061 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
6062 sysfs_notify_dirent(rdev->sysfs_state);
6063 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6064 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6065 md_wakeup_thread(mddev->thread);
6066 md_new_event_inintr(mddev);
6069 /* seq_file implementation /proc/mdstat */
6071 static void status_unused(struct seq_file *seq)
6076 seq_printf(seq, "unused devices: ");
6078 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
6079 char b[BDEVNAME_SIZE];
6081 seq_printf(seq, "%s ",
6082 bdevname(rdev->bdev,b));
6085 seq_printf(seq, "<none>");
6087 seq_printf(seq, "\n");
6091 static void status_resync(struct seq_file *seq, mddev_t * mddev)
6093 sector_t max_sectors, resync, res;
6094 unsigned long dt, db;
6097 unsigned int per_milli;
6099 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
6101 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
6102 max_sectors = mddev->resync_max_sectors;
6104 max_sectors = mddev->dev_sectors;
6107 * Should not happen.
6113 /* Pick 'scale' such that (resync>>scale)*1000 will fit
6114 * in a sector_t, and (max_sectors>>scale) will fit in a
6115 * u32, as those are the requirements for sector_div.
6116 * Thus 'scale' must be at least 10
6119 if (sizeof(sector_t) > sizeof(unsigned long)) {
6120 while ( max_sectors/2 > (1ULL<<(scale+32)))
6123 res = (resync>>scale)*1000;
6124 sector_div(res, (u32)((max_sectors>>scale)+1));
6128 int i, x = per_milli/50, y = 20-x;
6129 seq_printf(seq, "[");
6130 for (i = 0; i < x; i++)
6131 seq_printf(seq, "=");
6132 seq_printf(seq, ">");
6133 for (i = 0; i < y; i++)
6134 seq_printf(seq, ".");
6135 seq_printf(seq, "] ");
6137 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
6138 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6140 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6142 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6143 "resync" : "recovery"))),
6144 per_milli/10, per_milli % 10,
6145 (unsigned long long) resync/2,
6146 (unsigned long long) max_sectors/2);
6149 * dt: time from mark until now
6150 * db: blocks written from mark until now
6151 * rt: remaining time
6153 * rt is a sector_t, so could be 32bit or 64bit.
6154 * So we divide before multiply in case it is 32bit and close
6156 * We scale the divisor (db) by 32 to avoid loosing precision
6157 * near the end of resync when the number of remaining sectors
6159 * We then divide rt by 32 after multiplying by db to compensate.
6160 * The '+1' avoids division by zero if db is very small.
6162 dt = ((jiffies - mddev->resync_mark) / HZ);
6164 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6165 - mddev->resync_mark_cnt;
6167 rt = max_sectors - resync; /* number of remaining sectors */
6168 sector_div(rt, db/32+1);
6172 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6173 ((unsigned long)rt % 60)/6);
6175 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
6178 static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6180 struct list_head *tmp;
6190 spin_lock(&all_mddevs_lock);
6191 list_for_each(tmp,&all_mddevs)
6193 mddev = list_entry(tmp, mddev_t, all_mddevs);
6195 spin_unlock(&all_mddevs_lock);
6198 spin_unlock(&all_mddevs_lock);
6200 return (void*)2;/* tail */
6204 static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6206 struct list_head *tmp;
6207 mddev_t *next_mddev, *mddev = v;
6213 spin_lock(&all_mddevs_lock);
6215 tmp = all_mddevs.next;
6217 tmp = mddev->all_mddevs.next;
6218 if (tmp != &all_mddevs)
6219 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
6221 next_mddev = (void*)2;
6224 spin_unlock(&all_mddevs_lock);
6232 static void md_seq_stop(struct seq_file *seq, void *v)
6236 if (mddev && v != (void*)1 && v != (void*)2)
6240 struct mdstat_info {
6244 static int md_seq_show(struct seq_file *seq, void *v)
6249 struct mdstat_info *mi = seq->private;
6250 struct bitmap *bitmap;
6252 if (v == (void*)1) {
6253 struct mdk_personality *pers;
6254 seq_printf(seq, "Personalities : ");
6255 spin_lock(&pers_lock);
6256 list_for_each_entry(pers, &pers_list, list)
6257 seq_printf(seq, "[%s] ", pers->name);
6259 spin_unlock(&pers_lock);
6260 seq_printf(seq, "\n");
6261 mi->event = atomic_read(&md_event_count);
6264 if (v == (void*)2) {
6269 if (mddev_lock(mddev) < 0)
6272 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6273 seq_printf(seq, "%s : %sactive", mdname(mddev),
6274 mddev->pers ? "" : "in");
6277 seq_printf(seq, " (read-only)");
6279 seq_printf(seq, " (auto-read-only)");
6280 seq_printf(seq, " %s", mddev->pers->name);
6284 list_for_each_entry(rdev, &mddev->disks, same_set) {
6285 char b[BDEVNAME_SIZE];
6286 seq_printf(seq, " %s[%d]",
6287 bdevname(rdev->bdev,b), rdev->desc_nr);
6288 if (test_bit(WriteMostly, &rdev->flags))
6289 seq_printf(seq, "(W)");
6290 if (test_bit(Faulty, &rdev->flags)) {
6291 seq_printf(seq, "(F)");
6293 } else if (rdev->raid_disk < 0)
6294 seq_printf(seq, "(S)"); /* spare */
6295 sectors += rdev->sectors;
6298 if (!list_empty(&mddev->disks)) {
6300 seq_printf(seq, "\n %llu blocks",
6301 (unsigned long long)
6302 mddev->array_sectors / 2);
6304 seq_printf(seq, "\n %llu blocks",
6305 (unsigned long long)sectors / 2);
6307 if (mddev->persistent) {
6308 if (mddev->major_version != 0 ||
6309 mddev->minor_version != 90) {
6310 seq_printf(seq," super %d.%d",
6311 mddev->major_version,
6312 mddev->minor_version);
6314 } else if (mddev->external)
6315 seq_printf(seq, " super external:%s",
6316 mddev->metadata_type);
6318 seq_printf(seq, " super non-persistent");
6321 mddev->pers->status(seq, mddev);
6322 seq_printf(seq, "\n ");
6323 if (mddev->pers->sync_request) {
6324 if (mddev->curr_resync > 2) {
6325 status_resync(seq, mddev);
6326 seq_printf(seq, "\n ");
6327 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6328 seq_printf(seq, "\tresync=DELAYED\n ");
6329 else if (mddev->recovery_cp < MaxSector)
6330 seq_printf(seq, "\tresync=PENDING\n ");
6333 seq_printf(seq, "\n ");
6335 if ((bitmap = mddev->bitmap)) {
6336 unsigned long chunk_kb;
6337 unsigned long flags;
6338 spin_lock_irqsave(&bitmap->lock, flags);
6339 chunk_kb = mddev->bitmap_info.chunksize >> 10;
6340 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6342 bitmap->pages - bitmap->missing_pages,
6344 (bitmap->pages - bitmap->missing_pages)
6345 << (PAGE_SHIFT - 10),
6346 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
6347 chunk_kb ? "KB" : "B");
6349 seq_printf(seq, ", file: ");
6350 seq_path(seq, &bitmap->file->f_path, " \t\n");
6353 seq_printf(seq, "\n");
6354 spin_unlock_irqrestore(&bitmap->lock, flags);
6357 seq_printf(seq, "\n");
6359 mddev_unlock(mddev);
6364 static const struct seq_operations md_seq_ops = {
6365 .start = md_seq_start,
6366 .next = md_seq_next,
6367 .stop = md_seq_stop,
6368 .show = md_seq_show,
6371 static int md_seq_open(struct inode *inode, struct file *file)
6374 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
6378 error = seq_open(file, &md_seq_ops);
6382 struct seq_file *p = file->private_data;
6384 mi->event = atomic_read(&md_event_count);
6389 static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6391 struct seq_file *m = filp->private_data;
6392 struct mdstat_info *mi = m->private;
6395 poll_wait(filp, &md_event_waiters, wait);
6397 /* always allow read */
6398 mask = POLLIN | POLLRDNORM;
6400 if (mi->event != atomic_read(&md_event_count))
6401 mask |= POLLERR | POLLPRI;
6405 static const struct file_operations md_seq_fops = {
6406 .owner = THIS_MODULE,
6407 .open = md_seq_open,
6409 .llseek = seq_lseek,
6410 .release = seq_release_private,
6411 .poll = mdstat_poll,
6414 int register_md_personality(struct mdk_personality *p)
6416 spin_lock(&pers_lock);
6417 list_add_tail(&p->list, &pers_list);
6418 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
6419 spin_unlock(&pers_lock);
6423 int unregister_md_personality(struct mdk_personality *p)
6425 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
6426 spin_lock(&pers_lock);
6427 list_del_init(&p->list);
6428 spin_unlock(&pers_lock);
6432 static int is_mddev_idle(mddev_t *mddev, int init)
6440 rdev_for_each_rcu(rdev, mddev) {
6441 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
6442 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6443 (int)part_stat_read(&disk->part0, sectors[1]) -
6444 atomic_read(&disk->sync_io);
6445 /* sync IO will cause sync_io to increase before the disk_stats
6446 * as sync_io is counted when a request starts, and
6447 * disk_stats is counted when it completes.
6448 * So resync activity will cause curr_events to be smaller than
6449 * when there was no such activity.
6450 * non-sync IO will cause disk_stat to increase without
6451 * increasing sync_io so curr_events will (eventually)
6452 * be larger than it was before. Once it becomes
6453 * substantially larger, the test below will cause
6454 * the array to appear non-idle, and resync will slow
6456 * If there is a lot of outstanding resync activity when
6457 * we set last_event to curr_events, then all that activity
6458 * completing might cause the array to appear non-idle
6459 * and resync will be slowed down even though there might
6460 * not have been non-resync activity. This will only
6461 * happen once though. 'last_events' will soon reflect
6462 * the state where there is little or no outstanding
6463 * resync requests, and further resync activity will
6464 * always make curr_events less than last_events.
6467 if (init || curr_events - rdev->last_events > 64) {
6468 rdev->last_events = curr_events;
6476 void md_done_sync(mddev_t *mddev, int blocks, int ok)
6478 /* another "blocks" (512byte) blocks have been synced */
6479 atomic_sub(blocks, &mddev->recovery_active);
6480 wake_up(&mddev->recovery_wait);
6482 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6483 md_wakeup_thread(mddev->thread);
6484 // stop recovery, signal do_sync ....
6489 /* md_write_start(mddev, bi)
6490 * If we need to update some array metadata (e.g. 'active' flag
6491 * in superblock) before writing, schedule a superblock update
6492 * and wait for it to complete.
6494 void md_write_start(mddev_t *mddev, struct bio *bi)
6497 if (bio_data_dir(bi) != WRITE)
6500 BUG_ON(mddev->ro == 1);
6501 if (mddev->ro == 2) {
6502 /* need to switch to read/write */
6504 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6505 md_wakeup_thread(mddev->thread);
6506 md_wakeup_thread(mddev->sync_thread);
6509 atomic_inc(&mddev->writes_pending);
6510 if (mddev->safemode == 1)
6511 mddev->safemode = 0;
6512 if (mddev->in_sync) {
6513 spin_lock_irq(&mddev->write_lock);
6514 if (mddev->in_sync) {
6516 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6517 md_wakeup_thread(mddev->thread);
6520 spin_unlock_irq(&mddev->write_lock);
6523 sysfs_notify_dirent(mddev->sysfs_state);
6524 wait_event(mddev->sb_wait,
6525 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
6526 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
6529 void md_write_end(mddev_t *mddev)
6531 if (atomic_dec_and_test(&mddev->writes_pending)) {
6532 if (mddev->safemode == 2)
6533 md_wakeup_thread(mddev->thread);
6534 else if (mddev->safemode_delay)
6535 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6539 /* md_allow_write(mddev)
6540 * Calling this ensures that the array is marked 'active' so that writes
6541 * may proceed without blocking. It is important to call this before
6542 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6543 * Must be called with mddev_lock held.
6545 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6546 * is dropped, so return -EAGAIN after notifying userspace.
6548 int md_allow_write(mddev_t *mddev)
6554 if (!mddev->pers->sync_request)
6557 spin_lock_irq(&mddev->write_lock);
6558 if (mddev->in_sync) {
6560 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6561 if (mddev->safemode_delay &&
6562 mddev->safemode == 0)
6563 mddev->safemode = 1;
6564 spin_unlock_irq(&mddev->write_lock);
6565 md_update_sb(mddev, 0);
6566 sysfs_notify_dirent(mddev->sysfs_state);
6568 spin_unlock_irq(&mddev->write_lock);
6570 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
6575 EXPORT_SYMBOL_GPL(md_allow_write);
6577 #define SYNC_MARKS 10
6578 #define SYNC_MARK_STEP (3*HZ)
6579 void md_do_sync(mddev_t *mddev)
6582 unsigned int currspeed = 0,
6584 sector_t max_sectors,j, io_sectors;
6585 unsigned long mark[SYNC_MARKS];
6586 sector_t mark_cnt[SYNC_MARKS];
6588 struct list_head *tmp;
6589 sector_t last_check;
6594 /* just incase thread restarts... */
6595 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6597 if (mddev->ro) /* never try to sync a read-only array */
6600 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6601 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6602 desc = "data-check";
6603 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6604 desc = "requested-resync";
6607 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6612 /* we overload curr_resync somewhat here.
6613 * 0 == not engaged in resync at all
6614 * 2 == checking that there is no conflict with another sync
6615 * 1 == like 2, but have yielded to allow conflicting resync to
6617 * other == active in resync - this many blocks
6619 * Before starting a resync we must have set curr_resync to
6620 * 2, and then checked that every "conflicting" array has curr_resync
6621 * less than ours. When we find one that is the same or higher
6622 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6623 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6624 * This will mean we have to start checking from the beginning again.
6629 mddev->curr_resync = 2;
6632 if (kthread_should_stop())
6633 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6635 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6637 for_each_mddev(mddev2, tmp) {
6638 if (mddev2 == mddev)
6640 if (!mddev->parallel_resync
6641 && mddev2->curr_resync
6642 && match_mddev_units(mddev, mddev2)) {
6644 if (mddev < mddev2 && mddev->curr_resync == 2) {
6645 /* arbitrarily yield */
6646 mddev->curr_resync = 1;
6647 wake_up(&resync_wait);
6649 if (mddev > mddev2 && mddev->curr_resync == 1)
6650 /* no need to wait here, we can wait the next
6651 * time 'round when curr_resync == 2
6654 /* We need to wait 'interruptible' so as not to
6655 * contribute to the load average, and not to
6656 * be caught by 'softlockup'
6658 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
6659 if (!kthread_should_stop() &&
6660 mddev2->curr_resync >= mddev->curr_resync) {
6661 printk(KERN_INFO "md: delaying %s of %s"
6662 " until %s has finished (they"
6663 " share one or more physical units)\n",
6664 desc, mdname(mddev), mdname(mddev2));
6666 if (signal_pending(current))
6667 flush_signals(current);
6669 finish_wait(&resync_wait, &wq);
6672 finish_wait(&resync_wait, &wq);
6675 } while (mddev->curr_resync < 2);
6678 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6679 /* resync follows the size requested by the personality,
6680 * which defaults to physical size, but can be virtual size
6682 max_sectors = mddev->resync_max_sectors;
6683 mddev->resync_mismatches = 0;
6684 /* we don't use the checkpoint if there's a bitmap */
6685 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6686 j = mddev->resync_min;
6687 else if (!mddev->bitmap)
6688 j = mddev->recovery_cp;
6690 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6691 max_sectors = mddev->dev_sectors;
6693 /* recovery follows the physical size of devices */
6694 max_sectors = mddev->dev_sectors;
6697 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
6698 if (rdev->raid_disk >= 0 &&
6699 !test_bit(Faulty, &rdev->flags) &&
6700 !test_bit(In_sync, &rdev->flags) &&
6701 rdev->recovery_offset < j)
6702 j = rdev->recovery_offset;
6706 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6707 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6708 " %d KB/sec/disk.\n", speed_min(mddev));
6709 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
6710 "(but not more than %d KB/sec) for %s.\n",
6711 speed_max(mddev), desc);
6713 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
6716 for (m = 0; m < SYNC_MARKS; m++) {
6718 mark_cnt[m] = io_sectors;
6721 mddev->resync_mark = mark[last_mark];
6722 mddev->resync_mark_cnt = mark_cnt[last_mark];
6725 * Tune reconstruction:
6727 window = 32*(PAGE_SIZE/512);
6728 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6729 window/2,(unsigned long long) max_sectors/2);
6731 atomic_set(&mddev->recovery_active, 0);
6736 "md: resuming %s of %s from checkpoint.\n",
6737 desc, mdname(mddev));
6738 mddev->curr_resync = j;
6740 mddev->curr_resync_completed = mddev->curr_resync;
6742 while (j < max_sectors) {
6747 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6748 ((mddev->curr_resync > mddev->curr_resync_completed &&
6749 (mddev->curr_resync - mddev->curr_resync_completed)
6750 > (max_sectors >> 4)) ||
6751 (j - mddev->curr_resync_completed)*2
6752 >= mddev->resync_max - mddev->curr_resync_completed
6754 /* time to update curr_resync_completed */
6755 blk_unplug(mddev->queue);
6756 wait_event(mddev->recovery_wait,
6757 atomic_read(&mddev->recovery_active) == 0);
6758 mddev->curr_resync_completed =
6760 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6761 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
6764 while (j >= mddev->resync_max && !kthread_should_stop()) {
6765 /* As this condition is controlled by user-space,
6766 * we can block indefinitely, so use '_interruptible'
6767 * to avoid triggering warnings.
6769 flush_signals(current); /* just in case */
6770 wait_event_interruptible(mddev->recovery_wait,
6771 mddev->resync_max > j
6772 || kthread_should_stop());
6775 if (kthread_should_stop())
6778 sectors = mddev->pers->sync_request(mddev, j, &skipped,
6779 currspeed < speed_min(mddev));
6781 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6785 if (!skipped) { /* actual IO requested */
6786 io_sectors += sectors;
6787 atomic_add(sectors, &mddev->recovery_active);
6791 if (j>1) mddev->curr_resync = j;
6792 mddev->curr_mark_cnt = io_sectors;
6793 if (last_check == 0)
6794 /* this is the earliers that rebuilt will be
6795 * visible in /proc/mdstat
6797 md_new_event(mddev);
6799 if (last_check + window > io_sectors || j == max_sectors)
6802 last_check = io_sectors;
6804 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6808 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6810 int next = (last_mark+1) % SYNC_MARKS;
6812 mddev->resync_mark = mark[next];
6813 mddev->resync_mark_cnt = mark_cnt[next];
6814 mark[next] = jiffies;
6815 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
6820 if (kthread_should_stop())
6825 * this loop exits only if either when we are slower than
6826 * the 'hard' speed limit, or the system was IO-idle for
6828 * the system might be non-idle CPU-wise, but we only care
6829 * about not overloading the IO subsystem. (things like an
6830 * e2fsck being done on the RAID array should execute fast)
6832 blk_unplug(mddev->queue);
6835 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6836 /((jiffies-mddev->resync_mark)/HZ +1) +1;
6838 if (currspeed > speed_min(mddev)) {
6839 if ((currspeed > speed_max(mddev)) ||
6840 !is_mddev_idle(mddev, 0)) {
6846 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
6848 * this also signals 'finished resyncing' to md_stop
6851 blk_unplug(mddev->queue);
6853 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6855 /* tell personality that we are finished */
6856 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
6858 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
6859 mddev->curr_resync > 2) {
6860 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6861 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6862 if (mddev->curr_resync >= mddev->recovery_cp) {
6864 "md: checkpointing %s of %s.\n",
6865 desc, mdname(mddev));
6866 mddev->recovery_cp = mddev->curr_resync;
6869 mddev->recovery_cp = MaxSector;
6871 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6872 mddev->curr_resync = MaxSector;
6874 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
6875 if (rdev->raid_disk >= 0 &&
6876 mddev->delta_disks >= 0 &&
6877 !test_bit(Faulty, &rdev->flags) &&
6878 !test_bit(In_sync, &rdev->flags) &&
6879 rdev->recovery_offset < mddev->curr_resync)
6880 rdev->recovery_offset = mddev->curr_resync;
6884 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6887 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6888 /* We completed so min/max setting can be forgotten if used. */
6889 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6890 mddev->resync_min = 0;
6891 mddev->resync_max = MaxSector;
6892 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6893 mddev->resync_min = mddev->curr_resync_completed;
6894 mddev->curr_resync = 0;
6895 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6896 mddev->curr_resync_completed = 0;
6897 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
6898 wake_up(&resync_wait);
6899 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6900 md_wakeup_thread(mddev->thread);
6905 * got a signal, exit.
6908 "md: md_do_sync() got signal ... exiting\n");
6909 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6913 EXPORT_SYMBOL_GPL(md_do_sync);
6916 static int remove_and_add_spares(mddev_t *mddev)
6921 mddev->curr_resync_completed = 0;
6923 list_for_each_entry(rdev, &mddev->disks, same_set)
6924 if (rdev->raid_disk >= 0 &&
6925 !test_bit(Blocked, &rdev->flags) &&
6926 (test_bit(Faulty, &rdev->flags) ||
6927 ! test_bit(In_sync, &rdev->flags)) &&
6928 atomic_read(&rdev->nr_pending)==0) {
6929 if (mddev->pers->hot_remove_disk(
6930 mddev, rdev->raid_disk)==0) {
6932 sprintf(nm,"rd%d", rdev->raid_disk);
6933 sysfs_remove_link(&mddev->kobj, nm);
6934 rdev->raid_disk = -1;
6938 if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) {
6939 list_for_each_entry(rdev, &mddev->disks, same_set) {
6940 if (rdev->raid_disk >= 0 &&
6941 !test_bit(In_sync, &rdev->flags) &&
6942 !test_bit(Blocked, &rdev->flags))
6944 if (rdev->raid_disk < 0
6945 && !test_bit(Faulty, &rdev->flags)) {
6946 rdev->recovery_offset = 0;
6948 hot_add_disk(mddev, rdev) == 0) {
6950 sprintf(nm, "rd%d", rdev->raid_disk);
6951 if (sysfs_create_link(&mddev->kobj,
6954 "md: cannot register "
6958 md_new_event(mddev);
6959 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6968 * This routine is regularly called by all per-raid-array threads to
6969 * deal with generic issues like resync and super-block update.
6970 * Raid personalities that don't have a thread (linear/raid0) do not
6971 * need this as they never do any recovery or update the superblock.
6973 * It does not do any resync itself, but rather "forks" off other threads
6974 * to do that as needed.
6975 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6976 * "->recovery" and create a thread at ->sync_thread.
6977 * When the thread finishes it sets MD_RECOVERY_DONE
6978 * and wakeups up this thread which will reap the thread and finish up.
6979 * This thread also removes any faulty devices (with nr_pending == 0).
6981 * The overall approach is:
6982 * 1/ if the superblock needs updating, update it.
6983 * 2/ If a recovery thread is running, don't do anything else.
6984 * 3/ If recovery has finished, clean up, possibly marking spares active.
6985 * 4/ If there are any faulty devices, remove them.
6986 * 5/ If array is degraded, try to add spares devices
6987 * 6/ If array has spares or is not in-sync, start a resync thread.
6989 void md_check_recovery(mddev_t *mddev)
6995 bitmap_daemon_work(mddev);
7000 if (signal_pending(current)) {
7001 if (mddev->pers->sync_request && !mddev->external) {
7002 printk(KERN_INFO "md: %s in immediate safe mode\n",
7004 mddev->safemode = 2;
7006 flush_signals(current);
7009 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7012 (mddev->flags && !mddev->external) ||
7013 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
7014 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
7015 (mddev->external == 0 && mddev->safemode == 1) ||
7016 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7017 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
7021 if (mddev_trylock(mddev)) {
7025 /* Only thing we do on a ro array is remove
7028 remove_and_add_spares(mddev);
7029 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7033 if (!mddev->external) {
7035 spin_lock_irq(&mddev->write_lock);
7036 if (mddev->safemode &&
7037 !atomic_read(&mddev->writes_pending) &&
7039 mddev->recovery_cp == MaxSector) {
7042 if (mddev->persistent)
7043 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
7045 if (mddev->safemode == 1)
7046 mddev->safemode = 0;
7047 spin_unlock_irq(&mddev->write_lock);
7049 sysfs_notify_dirent(mddev->sysfs_state);
7053 md_update_sb(mddev, 0);
7055 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7056 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7057 /* resync/recovery still happening */
7058 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7061 if (mddev->sync_thread) {
7062 /* resync has finished, collect result */
7063 md_unregister_thread(mddev->sync_thread);
7064 mddev->sync_thread = NULL;
7065 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7066 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7068 /* activate any spares */
7069 if (mddev->pers->spare_active(mddev))
7070 sysfs_notify(&mddev->kobj, NULL,
7073 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7074 mddev->pers->finish_reshape)
7075 mddev->pers->finish_reshape(mddev);
7076 md_update_sb(mddev, 1);
7078 /* if array is no-longer degraded, then any saved_raid_disk
7079 * information must be scrapped
7081 if (!mddev->degraded)
7082 list_for_each_entry(rdev, &mddev->disks, same_set)
7083 rdev->saved_raid_disk = -1;
7085 mddev->recovery = 0;
7086 /* flag recovery needed just to double check */
7087 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7088 sysfs_notify_dirent(mddev->sysfs_action);
7089 md_new_event(mddev);
7092 /* Set RUNNING before clearing NEEDED to avoid
7093 * any transients in the value of "sync_action".
7095 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7096 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7097 /* Clear some bits that don't mean anything, but
7100 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7101 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
7103 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
7105 /* no recovery is running.
7106 * remove any failed drives, then
7107 * add spares if possible.
7108 * Spare are also removed and re-added, to allow
7109 * the personality to fail the re-add.
7112 if (mddev->reshape_position != MaxSector) {
7113 if (mddev->pers->check_reshape == NULL ||
7114 mddev->pers->check_reshape(mddev) != 0)
7115 /* Cannot proceed */
7117 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7118 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
7119 } else if ((spares = remove_and_add_spares(mddev))) {
7120 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7121 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7122 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7123 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
7124 } else if (mddev->recovery_cp < MaxSector) {
7125 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7126 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
7127 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7128 /* nothing to be done ... */
7131 if (mddev->pers->sync_request) {
7132 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
7133 /* We are adding a device or devices to an array
7134 * which has the bitmap stored on all devices.
7135 * So make sure all bitmap pages get written
7137 bitmap_write_all(mddev->bitmap);
7139 mddev->sync_thread = md_register_thread(md_do_sync,
7142 if (!mddev->sync_thread) {
7143 printk(KERN_ERR "%s: could not start resync"
7146 /* leave the spares where they are, it shouldn't hurt */
7147 mddev->recovery = 0;
7149 md_wakeup_thread(mddev->sync_thread);
7150 sysfs_notify_dirent(mddev->sysfs_action);
7151 md_new_event(mddev);
7154 if (!mddev->sync_thread) {
7155 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7156 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7158 if (mddev->sysfs_action)
7159 sysfs_notify_dirent(mddev->sysfs_action);
7161 mddev_unlock(mddev);
7165 void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
7167 sysfs_notify_dirent(rdev->sysfs_state);
7168 wait_event_timeout(rdev->blocked_wait,
7169 !test_bit(Blocked, &rdev->flags),
7170 msecs_to_jiffies(5000));
7171 rdev_dec_pending(rdev, mddev);
7173 EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7175 static int md_notify_reboot(struct notifier_block *this,
7176 unsigned long code, void *x)
7178 struct list_head *tmp;
7181 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
7183 printk(KERN_INFO "md: stopping all md devices.\n");
7185 for_each_mddev(mddev, tmp)
7186 if (mddev_trylock(mddev)) {
7187 /* Force a switch to readonly even array
7188 * appears to still be in use. Hence
7191 md_set_readonly(mddev, 100);
7192 mddev_unlock(mddev);
7195 * certain more exotic SCSI devices are known to be
7196 * volatile wrt too early system reboots. While the
7197 * right place to handle this issue is the given
7198 * driver, we do want to have a safe RAID driver ...
7205 static struct notifier_block md_notifier = {
7206 .notifier_call = md_notify_reboot,
7208 .priority = INT_MAX, /* before any real devices */
7211 static void md_geninit(void)
7213 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
7215 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
7218 static int __init md_init(void)
7220 if (register_blkdev(MD_MAJOR, "md"))
7222 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
7223 unregister_blkdev(MD_MAJOR, "md");
7226 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
7227 md_probe, NULL, NULL);
7228 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
7229 md_probe, NULL, NULL);
7231 register_reboot_notifier(&md_notifier);
7232 raid_table_header = register_sysctl_table(raid_root_table);
7242 * Searches all registered partitions for autorun RAID arrays
7246 static LIST_HEAD(all_detected_devices);
7247 struct detected_devices_node {
7248 struct list_head list;
7252 void md_autodetect_dev(dev_t dev)
7254 struct detected_devices_node *node_detected_dev;
7256 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
7257 if (node_detected_dev) {
7258 node_detected_dev->dev = dev;
7259 list_add_tail(&node_detected_dev->list, &all_detected_devices);
7261 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
7262 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
7267 static void autostart_arrays(int part)
7270 struct detected_devices_node *node_detected_dev;
7272 int i_scanned, i_passed;
7277 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
7279 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
7281 node_detected_dev = list_entry(all_detected_devices.next,
7282 struct detected_devices_node, list);
7283 list_del(&node_detected_dev->list);
7284 dev = node_detected_dev->dev;
7285 kfree(node_detected_dev);
7286 rdev = md_import_device(dev,0, 90);
7290 if (test_bit(Faulty, &rdev->flags)) {
7294 set_bit(AutoDetected, &rdev->flags);
7295 list_add(&rdev->same_set, &pending_raid_disks);
7299 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
7300 i_scanned, i_passed);
7302 autorun_devices(part);
7305 #endif /* !MODULE */
7307 static __exit void md_exit(void)
7310 struct list_head *tmp;
7312 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
7313 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
7315 unregister_blkdev(MD_MAJOR,"md");
7316 unregister_blkdev(mdp_major, "mdp");
7317 unregister_reboot_notifier(&md_notifier);
7318 unregister_sysctl_table(raid_table_header);
7319 remove_proc_entry("mdstat", NULL);
7320 for_each_mddev(mddev, tmp) {
7321 export_array(mddev);
7322 mddev->hold_active = 0;
7326 subsys_initcall(md_init);
7327 module_exit(md_exit)
7329 static int get_ro(char *buffer, struct kernel_param *kp)
7331 return sprintf(buffer, "%d", start_readonly);
7333 static int set_ro(const char *val, struct kernel_param *kp)
7336 int num = simple_strtoul(val, &e, 10);
7337 if (*val && (*e == '\0' || *e == '\n')) {
7338 start_readonly = num;
7344 module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
7345 module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
7347 module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
7349 EXPORT_SYMBOL(register_md_personality);
7350 EXPORT_SYMBOL(unregister_md_personality);
7351 EXPORT_SYMBOL(md_error);
7352 EXPORT_SYMBOL(md_done_sync);
7353 EXPORT_SYMBOL(md_write_start);
7354 EXPORT_SYMBOL(md_write_end);
7355 EXPORT_SYMBOL(md_register_thread);
7356 EXPORT_SYMBOL(md_unregister_thread);
7357 EXPORT_SYMBOL(md_wakeup_thread);
7358 EXPORT_SYMBOL(md_check_recovery);
7359 MODULE_LICENSE("GPL");
7360 MODULE_DESCRIPTION("MD RAID framework");
7362 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);