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/module.h>
36 #include <linux/kthread.h>
37 #include <linux/linkage.h>
38 #include <linux/raid/md.h>
39 #include <linux/raid/bitmap.h>
40 #include <linux/sysctl.h>
41 #include <linux/buffer_head.h> /* for invalidate_bdev */
42 #include <linux/suspend.h>
43 #include <linux/poll.h>
44 #include <linux/mutex.h>
45 #include <linux/ctype.h>
47 #include <linux/init.h>
49 #include <linux/file.h>
52 #include <linux/kmod.h>
55 #include <asm/unaligned.h>
57 #define MAJOR_NR MD_MAJOR
60 /* 63 partitions with the alternate major number (mdp) */
61 #define MdpMinorShift 6
64 #define dprintk(x...) ((void)(DEBUG && printk(x)))
68 static void autostart_arrays (int part);
71 static LIST_HEAD(pers_list);
72 static DEFINE_SPINLOCK(pers_lock);
74 static void md_print_devices(void);
76 #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
79 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
80 * is 1000 KB/sec, so the extra system load does not show up that much.
81 * Increase it if you want to have more _guaranteed_ speed. Note that
82 * the RAID driver will use the maximum available bandwidth if the IO
83 * subsystem is idle. There is also an 'absolute maximum' reconstruction
84 * speed limit - in case reconstruction slows down your system despite
87 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
88 * or /sys/block/mdX/md/sync_speed_{min,max}
91 static int sysctl_speed_limit_min = 1000;
92 static int sysctl_speed_limit_max = 200000;
93 static inline int speed_min(mddev_t *mddev)
95 return mddev->sync_speed_min ?
96 mddev->sync_speed_min : sysctl_speed_limit_min;
99 static inline int speed_max(mddev_t *mddev)
101 return mddev->sync_speed_max ?
102 mddev->sync_speed_max : sysctl_speed_limit_max;
105 static struct ctl_table_header *raid_table_header;
107 static ctl_table raid_table[] = {
109 .ctl_name = DEV_RAID_SPEED_LIMIT_MIN,
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 .ctl_name = DEV_RAID_SPEED_LIMIT_MAX,
118 .procname = "speed_limit_max",
119 .data = &sysctl_speed_limit_max,
120 .maxlen = sizeof(int),
121 .mode = S_IRUGO|S_IWUSR,
122 .proc_handler = &proc_dointvec,
127 static ctl_table raid_dir_table[] = {
129 .ctl_name = DEV_RAID,
132 .mode = S_IRUGO|S_IXUGO,
138 static ctl_table raid_root_table[] = {
144 .child = raid_dir_table,
149 static struct block_device_operations md_fops;
151 static int start_readonly;
154 * We have a system wide 'event count' that is incremented
155 * on any 'interesting' event, and readers of /proc/mdstat
156 * can use 'poll' or 'select' to find out when the event
160 * start array, stop array, error, add device, remove device,
161 * start build, activate spare
163 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
164 static atomic_t md_event_count;
165 void md_new_event(mddev_t *mddev)
167 atomic_inc(&md_event_count);
168 wake_up(&md_event_waiters);
169 sysfs_notify(&mddev->kobj, NULL, "sync_action");
171 EXPORT_SYMBOL_GPL(md_new_event);
173 /* Alternate version that can be called from interrupts
174 * when calling sysfs_notify isn't needed.
176 static void md_new_event_inintr(mddev_t *mddev)
178 atomic_inc(&md_event_count);
179 wake_up(&md_event_waiters);
183 * Enables to iterate over all existing md arrays
184 * all_mddevs_lock protects this list.
186 static LIST_HEAD(all_mddevs);
187 static DEFINE_SPINLOCK(all_mddevs_lock);
191 * iterates through all used mddevs in the system.
192 * We take care to grab the all_mddevs_lock whenever navigating
193 * the list, and to always hold a refcount when unlocked.
194 * Any code which breaks out of this loop while own
195 * a reference to the current mddev and must mddev_put it.
197 #define ITERATE_MDDEV(mddev,tmp) \
199 for (({ spin_lock(&all_mddevs_lock); \
200 tmp = all_mddevs.next; \
202 ({ if (tmp != &all_mddevs) \
203 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
204 spin_unlock(&all_mddevs_lock); \
205 if (mddev) mddev_put(mddev); \
206 mddev = list_entry(tmp, mddev_t, all_mddevs); \
207 tmp != &all_mddevs;}); \
208 ({ spin_lock(&all_mddevs_lock); \
213 static int md_fail_request (request_queue_t *q, struct bio *bio)
215 bio_io_error(bio, bio->bi_size);
219 static inline mddev_t *mddev_get(mddev_t *mddev)
221 atomic_inc(&mddev->active);
225 static void mddev_put(mddev_t *mddev)
227 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
229 if (!mddev->raid_disks && list_empty(&mddev->disks)) {
230 list_del(&mddev->all_mddevs);
231 spin_unlock(&all_mddevs_lock);
232 blk_cleanup_queue(mddev->queue);
233 kobject_unregister(&mddev->kobj);
235 spin_unlock(&all_mddevs_lock);
238 static mddev_t * mddev_find(dev_t unit)
240 mddev_t *mddev, *new = NULL;
243 spin_lock(&all_mddevs_lock);
244 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
245 if (mddev->unit == unit) {
247 spin_unlock(&all_mddevs_lock);
253 list_add(&new->all_mddevs, &all_mddevs);
254 spin_unlock(&all_mddevs_lock);
257 spin_unlock(&all_mddevs_lock);
259 new = kzalloc(sizeof(*new), GFP_KERNEL);
264 if (MAJOR(unit) == MD_MAJOR)
265 new->md_minor = MINOR(unit);
267 new->md_minor = MINOR(unit) >> MdpMinorShift;
269 mutex_init(&new->reconfig_mutex);
270 INIT_LIST_HEAD(&new->disks);
271 INIT_LIST_HEAD(&new->all_mddevs);
272 init_timer(&new->safemode_timer);
273 atomic_set(&new->active, 1);
274 spin_lock_init(&new->write_lock);
275 init_waitqueue_head(&new->sb_wait);
277 new->queue = blk_alloc_queue(GFP_KERNEL);
282 set_bit(QUEUE_FLAG_CLUSTER, &new->queue->queue_flags);
284 blk_queue_make_request(new->queue, md_fail_request);
289 static inline int mddev_lock(mddev_t * mddev)
291 return mutex_lock_interruptible(&mddev->reconfig_mutex);
294 static inline int mddev_trylock(mddev_t * mddev)
296 return mutex_trylock(&mddev->reconfig_mutex);
299 static inline void mddev_unlock(mddev_t * mddev)
301 mutex_unlock(&mddev->reconfig_mutex);
303 md_wakeup_thread(mddev->thread);
306 static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
309 struct list_head *tmp;
311 ITERATE_RDEV(mddev,rdev,tmp) {
312 if (rdev->desc_nr == nr)
318 static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
320 struct list_head *tmp;
323 ITERATE_RDEV(mddev,rdev,tmp) {
324 if (rdev->bdev->bd_dev == dev)
330 static struct mdk_personality *find_pers(int level, char *clevel)
332 struct mdk_personality *pers;
333 list_for_each_entry(pers, &pers_list, list) {
334 if (level != LEVEL_NONE && pers->level == level)
336 if (strcmp(pers->name, clevel)==0)
342 static inline sector_t calc_dev_sboffset(struct block_device *bdev)
344 sector_t size = bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
345 return MD_NEW_SIZE_BLOCKS(size);
348 static sector_t calc_dev_size(mdk_rdev_t *rdev, unsigned chunk_size)
352 size = rdev->sb_offset;
355 size &= ~((sector_t)chunk_size/1024 - 1);
359 static int alloc_disk_sb(mdk_rdev_t * rdev)
364 rdev->sb_page = alloc_page(GFP_KERNEL);
365 if (!rdev->sb_page) {
366 printk(KERN_ALERT "md: out of memory.\n");
373 static void free_disk_sb(mdk_rdev_t * rdev)
376 put_page(rdev->sb_page);
378 rdev->sb_page = NULL;
385 static int super_written(struct bio *bio, unsigned int bytes_done, int error)
387 mdk_rdev_t *rdev = bio->bi_private;
388 mddev_t *mddev = rdev->mddev;
392 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
393 printk("md: super_written gets error=%d, uptodate=%d\n",
394 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
395 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
396 md_error(mddev, rdev);
399 if (atomic_dec_and_test(&mddev->pending_writes))
400 wake_up(&mddev->sb_wait);
405 static int super_written_barrier(struct bio *bio, unsigned int bytes_done, int error)
407 struct bio *bio2 = bio->bi_private;
408 mdk_rdev_t *rdev = bio2->bi_private;
409 mddev_t *mddev = rdev->mddev;
413 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
414 error == -EOPNOTSUPP) {
416 /* barriers don't appear to be supported :-( */
417 set_bit(BarriersNotsupp, &rdev->flags);
418 mddev->barriers_work = 0;
419 spin_lock_irqsave(&mddev->write_lock, flags);
420 bio2->bi_next = mddev->biolist;
421 mddev->biolist = bio2;
422 spin_unlock_irqrestore(&mddev->write_lock, flags);
423 wake_up(&mddev->sb_wait);
428 bio->bi_private = rdev;
429 return super_written(bio, bytes_done, error);
432 void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
433 sector_t sector, int size, struct page *page)
435 /* write first size bytes of page to sector of rdev
436 * Increment mddev->pending_writes before returning
437 * and decrement it on completion, waking up sb_wait
438 * if zero is reached.
439 * If an error occurred, call md_error
441 * As we might need to resubmit the request if BIO_RW_BARRIER
442 * causes ENOTSUPP, we allocate a spare bio...
444 struct bio *bio = bio_alloc(GFP_NOIO, 1);
445 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNC);
447 bio->bi_bdev = rdev->bdev;
448 bio->bi_sector = sector;
449 bio_add_page(bio, page, size, 0);
450 bio->bi_private = rdev;
451 bio->bi_end_io = super_written;
454 atomic_inc(&mddev->pending_writes);
455 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
457 rw |= (1<<BIO_RW_BARRIER);
458 rbio = bio_clone(bio, GFP_NOIO);
459 rbio->bi_private = bio;
460 rbio->bi_end_io = super_written_barrier;
461 submit_bio(rw, rbio);
466 void md_super_wait(mddev_t *mddev)
468 /* wait for all superblock writes that were scheduled to complete.
469 * if any had to be retried (due to BARRIER problems), retry them
473 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
474 if (atomic_read(&mddev->pending_writes)==0)
476 while (mddev->biolist) {
478 spin_lock_irq(&mddev->write_lock);
479 bio = mddev->biolist;
480 mddev->biolist = bio->bi_next ;
482 spin_unlock_irq(&mddev->write_lock);
483 submit_bio(bio->bi_rw, bio);
487 finish_wait(&mddev->sb_wait, &wq);
490 static int bi_complete(struct bio *bio, unsigned int bytes_done, int error)
495 complete((struct completion*)bio->bi_private);
499 int sync_page_io(struct block_device *bdev, sector_t sector, int size,
500 struct page *page, int rw)
502 struct bio *bio = bio_alloc(GFP_NOIO, 1);
503 struct completion event;
506 rw |= (1 << BIO_RW_SYNC);
509 bio->bi_sector = sector;
510 bio_add_page(bio, page, size, 0);
511 init_completion(&event);
512 bio->bi_private = &event;
513 bio->bi_end_io = bi_complete;
515 wait_for_completion(&event);
517 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
521 EXPORT_SYMBOL_GPL(sync_page_io);
523 static int read_disk_sb(mdk_rdev_t * rdev, int size)
525 char b[BDEVNAME_SIZE];
526 if (!rdev->sb_page) {
534 if (!sync_page_io(rdev->bdev, rdev->sb_offset<<1, size, rdev->sb_page, READ))
540 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
541 bdevname(rdev->bdev,b));
545 static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
547 if ( (sb1->set_uuid0 == sb2->set_uuid0) &&
548 (sb1->set_uuid1 == sb2->set_uuid1) &&
549 (sb1->set_uuid2 == sb2->set_uuid2) &&
550 (sb1->set_uuid3 == sb2->set_uuid3))
558 static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
561 mdp_super_t *tmp1, *tmp2;
563 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
564 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
566 if (!tmp1 || !tmp2) {
568 printk(KERN_INFO "md.c: sb1 is not equal to sb2!\n");
576 * nr_disks is not constant
581 if (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4))
592 static unsigned int calc_sb_csum(mdp_super_t * sb)
594 unsigned int disk_csum, csum;
596 disk_csum = sb->sb_csum;
598 csum = csum_partial((void *)sb, MD_SB_BYTES, 0);
599 sb->sb_csum = disk_csum;
605 * Handle superblock details.
606 * We want to be able to handle multiple superblock formats
607 * so we have a common interface to them all, and an array of
608 * different handlers.
609 * We rely on user-space to write the initial superblock, and support
610 * reading and updating of superblocks.
611 * Interface methods are:
612 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
613 * loads and validates a superblock on dev.
614 * if refdev != NULL, compare superblocks on both devices
616 * 0 - dev has a superblock that is compatible with refdev
617 * 1 - dev has a superblock that is compatible and newer than refdev
618 * so dev should be used as the refdev in future
619 * -EINVAL superblock incompatible or invalid
620 * -othererror e.g. -EIO
622 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
623 * Verify that dev is acceptable into mddev.
624 * The first time, mddev->raid_disks will be 0, and data from
625 * dev should be merged in. Subsequent calls check that dev
626 * is new enough. Return 0 or -EINVAL
628 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
629 * Update the superblock for rdev with data in mddev
630 * This does not write to disc.
636 struct module *owner;
637 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version);
638 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
639 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
643 * load_super for 0.90.0
645 static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
647 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
653 * Calculate the position of the superblock,
654 * it's at the end of the disk.
656 * It also happens to be a multiple of 4Kb.
658 sb_offset = calc_dev_sboffset(rdev->bdev);
659 rdev->sb_offset = sb_offset;
661 ret = read_disk_sb(rdev, MD_SB_BYTES);
666 bdevname(rdev->bdev, b);
667 sb = (mdp_super_t*)page_address(rdev->sb_page);
669 if (sb->md_magic != MD_SB_MAGIC) {
670 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
675 if (sb->major_version != 0 ||
676 sb->minor_version < 90 ||
677 sb->minor_version > 91) {
678 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
679 sb->major_version, sb->minor_version,
684 if (sb->raid_disks <= 0)
687 if (csum_fold(calc_sb_csum(sb)) != csum_fold(sb->sb_csum)) {
688 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
693 rdev->preferred_minor = sb->md_minor;
694 rdev->data_offset = 0;
695 rdev->sb_size = MD_SB_BYTES;
697 if (sb->level == LEVEL_MULTIPATH)
700 rdev->desc_nr = sb->this_disk.number;
706 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
707 if (!uuid_equal(refsb, sb)) {
708 printk(KERN_WARNING "md: %s has different UUID to %s\n",
709 b, bdevname(refdev->bdev,b2));
712 if (!sb_equal(refsb, sb)) {
713 printk(KERN_WARNING "md: %s has same UUID"
714 " but different superblock to %s\n",
715 b, bdevname(refdev->bdev, b2));
719 ev2 = md_event(refsb);
725 rdev->size = calc_dev_size(rdev, sb->chunk_size);
727 if (rdev->size < sb->size && sb->level > 1)
728 /* "this cannot possibly happen" ... */
736 * validate_super for 0.90.0
738 static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
741 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
742 __u64 ev1 = md_event(sb);
744 rdev->raid_disk = -1;
746 if (mddev->raid_disks == 0) {
747 mddev->major_version = 0;
748 mddev->minor_version = sb->minor_version;
749 mddev->patch_version = sb->patch_version;
750 mddev->persistent = ! sb->not_persistent;
751 mddev->chunk_size = sb->chunk_size;
752 mddev->ctime = sb->ctime;
753 mddev->utime = sb->utime;
754 mddev->level = sb->level;
755 mddev->clevel[0] = 0;
756 mddev->layout = sb->layout;
757 mddev->raid_disks = sb->raid_disks;
758 mddev->size = sb->size;
760 mddev->bitmap_offset = 0;
761 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
763 if (mddev->minor_version >= 91) {
764 mddev->reshape_position = sb->reshape_position;
765 mddev->delta_disks = sb->delta_disks;
766 mddev->new_level = sb->new_level;
767 mddev->new_layout = sb->new_layout;
768 mddev->new_chunk = sb->new_chunk;
770 mddev->reshape_position = MaxSector;
771 mddev->delta_disks = 0;
772 mddev->new_level = mddev->level;
773 mddev->new_layout = mddev->layout;
774 mddev->new_chunk = mddev->chunk_size;
777 if (sb->state & (1<<MD_SB_CLEAN))
778 mddev->recovery_cp = MaxSector;
780 if (sb->events_hi == sb->cp_events_hi &&
781 sb->events_lo == sb->cp_events_lo) {
782 mddev->recovery_cp = sb->recovery_cp;
784 mddev->recovery_cp = 0;
787 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
788 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
789 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
790 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
792 mddev->max_disks = MD_SB_DISKS;
794 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
795 mddev->bitmap_file == NULL) {
796 if (mddev->level != 1 && mddev->level != 4
797 && mddev->level != 5 && mddev->level != 6
798 && mddev->level != 10) {
799 /* FIXME use a better test */
800 printk(KERN_WARNING "md: bitmaps not supported for this level.\n");
803 mddev->bitmap_offset = mddev->default_bitmap_offset;
806 } else if (mddev->pers == NULL) {
807 /* Insist on good event counter while assembling */
809 if (ev1 < mddev->events)
811 } else if (mddev->bitmap) {
812 /* if adding to array with a bitmap, then we can accept an
813 * older device ... but not too old.
815 if (ev1 < mddev->bitmap->events_cleared)
818 if (ev1 < mddev->events)
819 /* just a hot-add of a new device, leave raid_disk at -1 */
823 if (mddev->level != LEVEL_MULTIPATH) {
824 desc = sb->disks + rdev->desc_nr;
826 if (desc->state & (1<<MD_DISK_FAULTY))
827 set_bit(Faulty, &rdev->flags);
828 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
829 desc->raid_disk < mddev->raid_disks */) {
830 set_bit(In_sync, &rdev->flags);
831 rdev->raid_disk = desc->raid_disk;
833 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
834 set_bit(WriteMostly, &rdev->flags);
835 } else /* MULTIPATH are always insync */
836 set_bit(In_sync, &rdev->flags);
841 * sync_super for 0.90.0
843 static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
846 struct list_head *tmp;
848 int next_spare = mddev->raid_disks;
851 /* make rdev->sb match mddev data..
854 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
855 * 3/ any empty disks < next_spare become removed
857 * disks[0] gets initialised to REMOVED because
858 * we cannot be sure from other fields if it has
859 * been initialised or not.
862 int active=0, working=0,failed=0,spare=0,nr_disks=0;
864 rdev->sb_size = MD_SB_BYTES;
866 sb = (mdp_super_t*)page_address(rdev->sb_page);
868 memset(sb, 0, sizeof(*sb));
870 sb->md_magic = MD_SB_MAGIC;
871 sb->major_version = mddev->major_version;
872 sb->patch_version = mddev->patch_version;
873 sb->gvalid_words = 0; /* ignored */
874 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
875 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
876 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
877 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
879 sb->ctime = mddev->ctime;
880 sb->level = mddev->level;
881 sb->size = mddev->size;
882 sb->raid_disks = mddev->raid_disks;
883 sb->md_minor = mddev->md_minor;
884 sb->not_persistent = !mddev->persistent;
885 sb->utime = mddev->utime;
887 sb->events_hi = (mddev->events>>32);
888 sb->events_lo = (u32)mddev->events;
890 if (mddev->reshape_position == MaxSector)
891 sb->minor_version = 90;
893 sb->minor_version = 91;
894 sb->reshape_position = mddev->reshape_position;
895 sb->new_level = mddev->new_level;
896 sb->delta_disks = mddev->delta_disks;
897 sb->new_layout = mddev->new_layout;
898 sb->new_chunk = mddev->new_chunk;
900 mddev->minor_version = sb->minor_version;
903 sb->recovery_cp = mddev->recovery_cp;
904 sb->cp_events_hi = (mddev->events>>32);
905 sb->cp_events_lo = (u32)mddev->events;
906 if (mddev->recovery_cp == MaxSector)
907 sb->state = (1<< MD_SB_CLEAN);
911 sb->layout = mddev->layout;
912 sb->chunk_size = mddev->chunk_size;
914 if (mddev->bitmap && mddev->bitmap_file == NULL)
915 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
917 sb->disks[0].state = (1<<MD_DISK_REMOVED);
918 ITERATE_RDEV(mddev,rdev2,tmp) {
921 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
922 && !test_bit(Faulty, &rdev2->flags))
923 desc_nr = rdev2->raid_disk;
925 desc_nr = next_spare++;
926 rdev2->desc_nr = desc_nr;
927 d = &sb->disks[rdev2->desc_nr];
929 d->number = rdev2->desc_nr;
930 d->major = MAJOR(rdev2->bdev->bd_dev);
931 d->minor = MINOR(rdev2->bdev->bd_dev);
932 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
933 && !test_bit(Faulty, &rdev2->flags))
934 d->raid_disk = rdev2->raid_disk;
936 d->raid_disk = rdev2->desc_nr; /* compatibility */
937 if (test_bit(Faulty, &rdev2->flags))
938 d->state = (1<<MD_DISK_FAULTY);
939 else if (test_bit(In_sync, &rdev2->flags)) {
940 d->state = (1<<MD_DISK_ACTIVE);
941 d->state |= (1<<MD_DISK_SYNC);
949 if (test_bit(WriteMostly, &rdev2->flags))
950 d->state |= (1<<MD_DISK_WRITEMOSTLY);
952 /* now set the "removed" and "faulty" bits on any missing devices */
953 for (i=0 ; i < mddev->raid_disks ; i++) {
954 mdp_disk_t *d = &sb->disks[i];
955 if (d->state == 0 && d->number == 0) {
958 d->state = (1<<MD_DISK_REMOVED);
959 d->state |= (1<<MD_DISK_FAULTY);
963 sb->nr_disks = nr_disks;
964 sb->active_disks = active;
965 sb->working_disks = working;
966 sb->failed_disks = failed;
967 sb->spare_disks = spare;
969 sb->this_disk = sb->disks[rdev->desc_nr];
970 sb->sb_csum = calc_sb_csum(sb);
974 * version 1 superblock
977 static unsigned int calc_sb_1_csum(struct mdp_superblock_1 * sb)
979 unsigned int disk_csum, csum;
980 unsigned long long newcsum;
981 int size = 256 + le32_to_cpu(sb->max_dev)*2;
982 unsigned int *isuper = (unsigned int*)sb;
985 disk_csum = sb->sb_csum;
988 for (i=0; size>=4; size -= 4 )
989 newcsum += le32_to_cpu(*isuper++);
992 newcsum += le16_to_cpu(*(unsigned short*) isuper);
994 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
995 sb->sb_csum = disk_csum;
996 return cpu_to_le32(csum);
999 static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1001 struct mdp_superblock_1 *sb;
1004 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1008 * Calculate the position of the superblock.
1009 * It is always aligned to a 4K boundary and
1010 * depeding on minor_version, it can be:
1011 * 0: At least 8K, but less than 12K, from end of device
1012 * 1: At start of device
1013 * 2: 4K from start of device.
1015 switch(minor_version) {
1017 sb_offset = rdev->bdev->bd_inode->i_size >> 9;
1019 sb_offset &= ~(sector_t)(4*2-1);
1020 /* convert from sectors to K */
1032 rdev->sb_offset = sb_offset;
1034 /* superblock is rarely larger than 1K, but it can be larger,
1035 * and it is safe to read 4k, so we do that
1037 ret = read_disk_sb(rdev, 4096);
1038 if (ret) return ret;
1041 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1043 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1044 sb->major_version != cpu_to_le32(1) ||
1045 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1046 le64_to_cpu(sb->super_offset) != (rdev->sb_offset<<1) ||
1047 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
1050 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1051 printk("md: invalid superblock checksum on %s\n",
1052 bdevname(rdev->bdev,b));
1055 if (le64_to_cpu(sb->data_size) < 10) {
1056 printk("md: data_size too small on %s\n",
1057 bdevname(rdev->bdev,b));
1060 rdev->preferred_minor = 0xffff;
1061 rdev->data_offset = le64_to_cpu(sb->data_offset);
1062 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
1064 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
1065 bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
1066 if (rdev->sb_size & bmask)
1067 rdev-> sb_size = (rdev->sb_size | bmask)+1;
1069 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1072 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1078 struct mdp_superblock_1 *refsb =
1079 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1081 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1082 sb->level != refsb->level ||
1083 sb->layout != refsb->layout ||
1084 sb->chunksize != refsb->chunksize) {
1085 printk(KERN_WARNING "md: %s has strangely different"
1086 " superblock to %s\n",
1087 bdevname(rdev->bdev,b),
1088 bdevname(refdev->bdev,b2));
1091 ev1 = le64_to_cpu(sb->events);
1092 ev2 = le64_to_cpu(refsb->events);
1100 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
1102 rdev->size = rdev->sb_offset;
1103 if (rdev->size < le64_to_cpu(sb->data_size)/2)
1105 rdev->size = le64_to_cpu(sb->data_size)/2;
1106 if (le32_to_cpu(sb->chunksize))
1107 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
1109 if (le32_to_cpu(sb->size) > rdev->size*2)
1114 static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1116 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1117 __u64 ev1 = le64_to_cpu(sb->events);
1119 rdev->raid_disk = -1;
1121 if (mddev->raid_disks == 0) {
1122 mddev->major_version = 1;
1123 mddev->patch_version = 0;
1124 mddev->persistent = 1;
1125 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
1126 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1127 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1128 mddev->level = le32_to_cpu(sb->level);
1129 mddev->clevel[0] = 0;
1130 mddev->layout = le32_to_cpu(sb->layout);
1131 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1132 mddev->size = le64_to_cpu(sb->size)/2;
1133 mddev->events = ev1;
1134 mddev->bitmap_offset = 0;
1135 mddev->default_bitmap_offset = 1024 >> 9;
1137 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1138 memcpy(mddev->uuid, sb->set_uuid, 16);
1140 mddev->max_disks = (4096-256)/2;
1142 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
1143 mddev->bitmap_file == NULL ) {
1144 if (mddev->level != 1 && mddev->level != 5 && mddev->level != 6
1145 && mddev->level != 10) {
1146 printk(KERN_WARNING "md: bitmaps not supported for this level.\n");
1149 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
1151 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1152 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1153 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1154 mddev->new_level = le32_to_cpu(sb->new_level);
1155 mddev->new_layout = le32_to_cpu(sb->new_layout);
1156 mddev->new_chunk = le32_to_cpu(sb->new_chunk)<<9;
1158 mddev->reshape_position = MaxSector;
1159 mddev->delta_disks = 0;
1160 mddev->new_level = mddev->level;
1161 mddev->new_layout = mddev->layout;
1162 mddev->new_chunk = mddev->chunk_size;
1165 } else if (mddev->pers == NULL) {
1166 /* Insist of good event counter while assembling */
1168 if (ev1 < mddev->events)
1170 } else if (mddev->bitmap) {
1171 /* If adding to array with a bitmap, then we can accept an
1172 * older device, but not too old.
1174 if (ev1 < mddev->bitmap->events_cleared)
1177 if (ev1 < mddev->events)
1178 /* just a hot-add of a new device, leave raid_disk at -1 */
1181 if (mddev->level != LEVEL_MULTIPATH) {
1183 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1185 case 0xffff: /* spare */
1187 case 0xfffe: /* faulty */
1188 set_bit(Faulty, &rdev->flags);
1191 if ((le32_to_cpu(sb->feature_map) &
1192 MD_FEATURE_RECOVERY_OFFSET))
1193 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1195 set_bit(In_sync, &rdev->flags);
1196 rdev->raid_disk = role;
1199 if (sb->devflags & WriteMostly1)
1200 set_bit(WriteMostly, &rdev->flags);
1201 } else /* MULTIPATH are always insync */
1202 set_bit(In_sync, &rdev->flags);
1207 static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1209 struct mdp_superblock_1 *sb;
1210 struct list_head *tmp;
1213 /* make rdev->sb match mddev and rdev data. */
1215 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1217 sb->feature_map = 0;
1219 sb->recovery_offset = cpu_to_le64(0);
1220 memset(sb->pad1, 0, sizeof(sb->pad1));
1221 memset(sb->pad2, 0, sizeof(sb->pad2));
1222 memset(sb->pad3, 0, sizeof(sb->pad3));
1224 sb->utime = cpu_to_le64((__u64)mddev->utime);
1225 sb->events = cpu_to_le64(mddev->events);
1227 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1229 sb->resync_offset = cpu_to_le64(0);
1231 sb->cnt_corrected_read = atomic_read(&rdev->corrected_errors);
1233 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
1234 sb->size = cpu_to_le64(mddev->size<<1);
1236 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1237 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
1238 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
1241 if (rdev->raid_disk >= 0 &&
1242 !test_bit(In_sync, &rdev->flags) &&
1243 rdev->recovery_offset > 0) {
1244 sb->feature_map |= cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1245 sb->recovery_offset = cpu_to_le64(rdev->recovery_offset);
1248 if (mddev->reshape_position != MaxSector) {
1249 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1250 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1251 sb->new_layout = cpu_to_le32(mddev->new_layout);
1252 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1253 sb->new_level = cpu_to_le32(mddev->new_level);
1254 sb->new_chunk = cpu_to_le32(mddev->new_chunk>>9);
1258 ITERATE_RDEV(mddev,rdev2,tmp)
1259 if (rdev2->desc_nr+1 > max_dev)
1260 max_dev = rdev2->desc_nr+1;
1262 sb->max_dev = cpu_to_le32(max_dev);
1263 for (i=0; i<max_dev;i++)
1264 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1266 ITERATE_RDEV(mddev,rdev2,tmp) {
1268 if (test_bit(Faulty, &rdev2->flags))
1269 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1270 else if (test_bit(In_sync, &rdev2->flags))
1271 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1272 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1273 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1275 sb->dev_roles[i] = cpu_to_le16(0xffff);
1278 sb->sb_csum = calc_sb_1_csum(sb);
1282 static struct super_type super_types[] = {
1285 .owner = THIS_MODULE,
1286 .load_super = super_90_load,
1287 .validate_super = super_90_validate,
1288 .sync_super = super_90_sync,
1292 .owner = THIS_MODULE,
1293 .load_super = super_1_load,
1294 .validate_super = super_1_validate,
1295 .sync_super = super_1_sync,
1299 static mdk_rdev_t * match_dev_unit(mddev_t *mddev, mdk_rdev_t *dev)
1301 struct list_head *tmp;
1304 ITERATE_RDEV(mddev,rdev,tmp)
1305 if (rdev->bdev->bd_contains == dev->bdev->bd_contains)
1311 static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1313 struct list_head *tmp;
1316 ITERATE_RDEV(mddev1,rdev,tmp)
1317 if (match_dev_unit(mddev2, rdev))
1323 static LIST_HEAD(pending_raid_disks);
1325 static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1327 mdk_rdev_t *same_pdev;
1328 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1336 /* make sure rdev->size exceeds mddev->size */
1337 if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) {
1339 /* Cannot change size, so fail */
1342 mddev->size = rdev->size;
1344 same_pdev = match_dev_unit(mddev, rdev);
1347 "%s: WARNING: %s appears to be on the same physical"
1348 " disk as %s. True\n protection against single-disk"
1349 " failure might be compromised.\n",
1350 mdname(mddev), bdevname(rdev->bdev,b),
1351 bdevname(same_pdev->bdev,b2));
1353 /* Verify rdev->desc_nr is unique.
1354 * If it is -1, assign a free number, else
1355 * check number is not in use
1357 if (rdev->desc_nr < 0) {
1359 if (mddev->pers) choice = mddev->raid_disks;
1360 while (find_rdev_nr(mddev, choice))
1362 rdev->desc_nr = choice;
1364 if (find_rdev_nr(mddev, rdev->desc_nr))
1367 bdevname(rdev->bdev,b);
1368 if (kobject_set_name(&rdev->kobj, "dev-%s", b) < 0)
1370 while ( (s=strchr(rdev->kobj.k_name, '/')) != NULL)
1373 list_add(&rdev->same_set, &mddev->disks);
1374 rdev->mddev = mddev;
1375 printk(KERN_INFO "md: bind<%s>\n", b);
1377 rdev->kobj.parent = &mddev->kobj;
1378 kobject_add(&rdev->kobj);
1380 if (rdev->bdev->bd_part)
1381 ko = &rdev->bdev->bd_part->kobj;
1383 ko = &rdev->bdev->bd_disk->kobj;
1384 sysfs_create_link(&rdev->kobj, ko, "block");
1385 bd_claim_by_disk(rdev->bdev, rdev, mddev->gendisk);
1389 static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1391 char b[BDEVNAME_SIZE];
1396 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
1397 list_del_init(&rdev->same_set);
1398 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1400 sysfs_remove_link(&rdev->kobj, "block");
1401 kobject_del(&rdev->kobj);
1405 * prevent the device from being mounted, repartitioned or
1406 * otherwise reused by a RAID array (or any other kernel
1407 * subsystem), by bd_claiming the device.
1409 static int lock_rdev(mdk_rdev_t *rdev, dev_t dev)
1412 struct block_device *bdev;
1413 char b[BDEVNAME_SIZE];
1415 bdev = open_partition_by_devnum(dev, FMODE_READ|FMODE_WRITE);
1417 printk(KERN_ERR "md: could not open %s.\n",
1418 __bdevname(dev, b));
1419 return PTR_ERR(bdev);
1421 err = bd_claim(bdev, rdev);
1423 printk(KERN_ERR "md: could not bd_claim %s.\n",
1425 blkdev_put_partition(bdev);
1432 static void unlock_rdev(mdk_rdev_t *rdev)
1434 struct block_device *bdev = rdev->bdev;
1439 blkdev_put_partition(bdev);
1442 void md_autodetect_dev(dev_t dev);
1444 static void export_rdev(mdk_rdev_t * rdev)
1446 char b[BDEVNAME_SIZE];
1447 printk(KERN_INFO "md: export_rdev(%s)\n",
1448 bdevname(rdev->bdev,b));
1452 list_del_init(&rdev->same_set);
1454 md_autodetect_dev(rdev->bdev->bd_dev);
1457 kobject_put(&rdev->kobj);
1460 static void kick_rdev_from_array(mdk_rdev_t * rdev)
1462 unbind_rdev_from_array(rdev);
1466 static void export_array(mddev_t *mddev)
1468 struct list_head *tmp;
1471 ITERATE_RDEV(mddev,rdev,tmp) {
1476 kick_rdev_from_array(rdev);
1478 if (!list_empty(&mddev->disks))
1480 mddev->raid_disks = 0;
1481 mddev->major_version = 0;
1484 static void print_desc(mdp_disk_t *desc)
1486 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1487 desc->major,desc->minor,desc->raid_disk,desc->state);
1490 static void print_sb(mdp_super_t *sb)
1495 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1496 sb->major_version, sb->minor_version, sb->patch_version,
1497 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1499 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1500 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1501 sb->md_minor, sb->layout, sb->chunk_size);
1502 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1503 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1504 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1505 sb->failed_disks, sb->spare_disks,
1506 sb->sb_csum, (unsigned long)sb->events_lo);
1509 for (i = 0; i < MD_SB_DISKS; i++) {
1512 desc = sb->disks + i;
1513 if (desc->number || desc->major || desc->minor ||
1514 desc->raid_disk || (desc->state && (desc->state != 4))) {
1515 printk(" D %2d: ", i);
1519 printk(KERN_INFO "md: THIS: ");
1520 print_desc(&sb->this_disk);
1524 static void print_rdev(mdk_rdev_t *rdev)
1526 char b[BDEVNAME_SIZE];
1527 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1528 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
1529 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1531 if (rdev->sb_loaded) {
1532 printk(KERN_INFO "md: rdev superblock:\n");
1533 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1535 printk(KERN_INFO "md: no rdev superblock!\n");
1538 static void md_print_devices(void)
1540 struct list_head *tmp, *tmp2;
1543 char b[BDEVNAME_SIZE];
1546 printk("md: **********************************\n");
1547 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1548 printk("md: **********************************\n");
1549 ITERATE_MDDEV(mddev,tmp) {
1552 bitmap_print_sb(mddev->bitmap);
1554 printk("%s: ", mdname(mddev));
1555 ITERATE_RDEV(mddev,rdev,tmp2)
1556 printk("<%s>", bdevname(rdev->bdev,b));
1559 ITERATE_RDEV(mddev,rdev,tmp2)
1562 printk("md: **********************************\n");
1567 static void sync_sbs(mddev_t * mddev, int nospares)
1569 /* Update each superblock (in-memory image), but
1570 * if we are allowed to, skip spares which already
1571 * have the right event counter, or have one earlier
1572 * (which would mean they aren't being marked as dirty
1573 * with the rest of the array)
1576 struct list_head *tmp;
1578 ITERATE_RDEV(mddev,rdev,tmp) {
1579 if (rdev->sb_events == mddev->events ||
1581 rdev->raid_disk < 0 &&
1582 (rdev->sb_events&1)==0 &&
1583 rdev->sb_events+1 == mddev->events)) {
1584 /* Don't update this superblock */
1585 rdev->sb_loaded = 2;
1587 super_types[mddev->major_version].
1588 sync_super(mddev, rdev);
1589 rdev->sb_loaded = 1;
1594 static void md_update_sb(mddev_t * mddev, int force_change)
1597 struct list_head *tmp;
1603 spin_lock_irq(&mddev->write_lock);
1605 set_bit(MD_CHANGE_PENDING, &mddev->flags);
1606 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
1608 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
1609 /* just a clean<-> dirty transition, possibly leave spares alone,
1610 * though if events isn't the right even/odd, we will have to do
1616 if (mddev->degraded)
1617 /* If the array is degraded, then skipping spares is both
1618 * dangerous and fairly pointless.
1619 * Dangerous because a device that was removed from the array
1620 * might have a event_count that still looks up-to-date,
1621 * so it can be re-added without a resync.
1622 * Pointless because if there are any spares to skip,
1623 * then a recovery will happen and soon that array won't
1624 * be degraded any more and the spare can go back to sleep then.
1628 sync_req = mddev->in_sync;
1629 mddev->utime = get_seconds();
1631 /* If this is just a dirty<->clean transition, and the array is clean
1632 * and 'events' is odd, we can roll back to the previous clean state */
1634 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
1635 && (mddev->events & 1))
1638 /* otherwise we have to go forward and ... */
1640 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
1641 /* .. if the array isn't clean, insist on an odd 'events' */
1642 if ((mddev->events&1)==0) {
1647 /* otherwise insist on an even 'events' (for clean states) */
1648 if ((mddev->events&1)) {
1655 if (!mddev->events) {
1657 * oops, this 64-bit counter should never wrap.
1658 * Either we are in around ~1 trillion A.C., assuming
1659 * 1 reboot per second, or we have a bug:
1664 sync_sbs(mddev, nospares);
1667 * do not write anything to disk if using
1668 * nonpersistent superblocks
1670 if (!mddev->persistent) {
1671 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
1672 spin_unlock_irq(&mddev->write_lock);
1673 wake_up(&mddev->sb_wait);
1676 spin_unlock_irq(&mddev->write_lock);
1679 "md: updating %s RAID superblock on device (in sync %d)\n",
1680 mdname(mddev),mddev->in_sync);
1682 err = bitmap_update_sb(mddev->bitmap);
1683 ITERATE_RDEV(mddev,rdev,tmp) {
1684 char b[BDEVNAME_SIZE];
1685 dprintk(KERN_INFO "md: ");
1686 if (rdev->sb_loaded != 1)
1687 continue; /* no noise on spare devices */
1688 if (test_bit(Faulty, &rdev->flags))
1689 dprintk("(skipping faulty ");
1691 dprintk("%s ", bdevname(rdev->bdev,b));
1692 if (!test_bit(Faulty, &rdev->flags)) {
1693 md_super_write(mddev,rdev,
1694 rdev->sb_offset<<1, rdev->sb_size,
1696 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1697 bdevname(rdev->bdev,b),
1698 (unsigned long long)rdev->sb_offset);
1699 rdev->sb_events = mddev->events;
1703 if (mddev->level == LEVEL_MULTIPATH)
1704 /* only need to write one superblock... */
1707 md_super_wait(mddev);
1708 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
1710 spin_lock_irq(&mddev->write_lock);
1711 if (mddev->in_sync != sync_req ||
1712 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
1713 /* have to write it out again */
1714 spin_unlock_irq(&mddev->write_lock);
1717 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
1718 spin_unlock_irq(&mddev->write_lock);
1719 wake_up(&mddev->sb_wait);
1723 /* words written to sysfs files may, or my not, be \n terminated.
1724 * We want to accept with case. For this we use cmd_match.
1726 static int cmd_match(const char *cmd, const char *str)
1728 /* See if cmd, written into a sysfs file, matches
1729 * str. They must either be the same, or cmd can
1730 * have a trailing newline
1732 while (*cmd && *str && *cmd == *str) {
1743 struct rdev_sysfs_entry {
1744 struct attribute attr;
1745 ssize_t (*show)(mdk_rdev_t *, char *);
1746 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1750 state_show(mdk_rdev_t *rdev, char *page)
1755 if (test_bit(Faulty, &rdev->flags)) {
1756 len+= sprintf(page+len, "%sfaulty",sep);
1759 if (test_bit(In_sync, &rdev->flags)) {
1760 len += sprintf(page+len, "%sin_sync",sep);
1763 if (test_bit(WriteMostly, &rdev->flags)) {
1764 len += sprintf(page+len, "%swrite_mostly",sep);
1767 if (!test_bit(Faulty, &rdev->flags) &&
1768 !test_bit(In_sync, &rdev->flags)) {
1769 len += sprintf(page+len, "%sspare", sep);
1772 return len+sprintf(page+len, "\n");
1776 state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1779 * faulty - simulates and error
1780 * remove - disconnects the device
1781 * writemostly - sets write_mostly
1782 * -writemostly - clears write_mostly
1785 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
1786 md_error(rdev->mddev, rdev);
1788 } else if (cmd_match(buf, "remove")) {
1789 if (rdev->raid_disk >= 0)
1792 mddev_t *mddev = rdev->mddev;
1793 kick_rdev_from_array(rdev);
1794 md_update_sb(mddev, 1);
1795 md_new_event(mddev);
1798 } else if (cmd_match(buf, "writemostly")) {
1799 set_bit(WriteMostly, &rdev->flags);
1801 } else if (cmd_match(buf, "-writemostly")) {
1802 clear_bit(WriteMostly, &rdev->flags);
1805 return err ? err : len;
1807 static struct rdev_sysfs_entry rdev_state =
1808 __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
1811 super_show(mdk_rdev_t *rdev, char *page)
1813 if (rdev->sb_loaded && rdev->sb_size) {
1814 memcpy(page, page_address(rdev->sb_page), rdev->sb_size);
1815 return rdev->sb_size;
1819 static struct rdev_sysfs_entry rdev_super = __ATTR_RO(super);
1822 errors_show(mdk_rdev_t *rdev, char *page)
1824 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
1828 errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1831 unsigned long n = simple_strtoul(buf, &e, 10);
1832 if (*buf && (*e == 0 || *e == '\n')) {
1833 atomic_set(&rdev->corrected_errors, n);
1838 static struct rdev_sysfs_entry rdev_errors =
1839 __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
1842 slot_show(mdk_rdev_t *rdev, char *page)
1844 if (rdev->raid_disk < 0)
1845 return sprintf(page, "none\n");
1847 return sprintf(page, "%d\n", rdev->raid_disk);
1851 slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1854 int slot = simple_strtoul(buf, &e, 10);
1855 if (strncmp(buf, "none", 4)==0)
1857 else if (e==buf || (*e && *e!= '\n'))
1859 if (rdev->mddev->pers)
1860 /* Cannot set slot in active array (yet) */
1862 if (slot >= rdev->mddev->raid_disks)
1864 rdev->raid_disk = slot;
1865 /* assume it is working */
1867 set_bit(In_sync, &rdev->flags);
1872 static struct rdev_sysfs_entry rdev_slot =
1873 __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
1876 offset_show(mdk_rdev_t *rdev, char *page)
1878 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
1882 offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1885 unsigned long long offset = simple_strtoull(buf, &e, 10);
1886 if (e==buf || (*e && *e != '\n'))
1888 if (rdev->mddev->pers)
1890 rdev->data_offset = offset;
1894 static struct rdev_sysfs_entry rdev_offset =
1895 __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
1898 rdev_size_show(mdk_rdev_t *rdev, char *page)
1900 return sprintf(page, "%llu\n", (unsigned long long)rdev->size);
1904 rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1907 unsigned long long size = simple_strtoull(buf, &e, 10);
1908 if (e==buf || (*e && *e != '\n'))
1910 if (rdev->mddev->pers)
1913 if (size < rdev->mddev->size || rdev->mddev->size == 0)
1914 rdev->mddev->size = size;
1918 static struct rdev_sysfs_entry rdev_size =
1919 __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
1921 static struct attribute *rdev_default_attrs[] = {
1931 rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
1933 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1934 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1938 return entry->show(rdev, page);
1942 rdev_attr_store(struct kobject *kobj, struct attribute *attr,
1943 const char *page, size_t length)
1945 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1946 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1950 if (!capable(CAP_SYS_ADMIN))
1952 return entry->store(rdev, page, length);
1955 static void rdev_free(struct kobject *ko)
1957 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
1960 static struct sysfs_ops rdev_sysfs_ops = {
1961 .show = rdev_attr_show,
1962 .store = rdev_attr_store,
1964 static struct kobj_type rdev_ktype = {
1965 .release = rdev_free,
1966 .sysfs_ops = &rdev_sysfs_ops,
1967 .default_attrs = rdev_default_attrs,
1971 * Import a device. If 'super_format' >= 0, then sanity check the superblock
1973 * mark the device faulty if:
1975 * - the device is nonexistent (zero size)
1976 * - the device has no valid superblock
1978 * a faulty rdev _never_ has rdev->sb set.
1980 static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
1982 char b[BDEVNAME_SIZE];
1987 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
1989 printk(KERN_ERR "md: could not alloc mem for new device!\n");
1990 return ERR_PTR(-ENOMEM);
1993 if ((err = alloc_disk_sb(rdev)))
1996 err = lock_rdev(rdev, newdev);
2000 rdev->kobj.parent = NULL;
2001 rdev->kobj.ktype = &rdev_ktype;
2002 kobject_init(&rdev->kobj);
2006 rdev->data_offset = 0;
2007 rdev->sb_events = 0;
2008 atomic_set(&rdev->nr_pending, 0);
2009 atomic_set(&rdev->read_errors, 0);
2010 atomic_set(&rdev->corrected_errors, 0);
2012 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2015 "md: %s has zero or unknown size, marking faulty!\n",
2016 bdevname(rdev->bdev,b));
2021 if (super_format >= 0) {
2022 err = super_types[super_format].
2023 load_super(rdev, NULL, super_minor);
2024 if (err == -EINVAL) {
2026 "md: %s has invalid sb, not importing!\n",
2027 bdevname(rdev->bdev,b));
2032 "md: could not read %s's sb, not importing!\n",
2033 bdevname(rdev->bdev,b));
2037 INIT_LIST_HEAD(&rdev->same_set);
2042 if (rdev->sb_page) {
2048 return ERR_PTR(err);
2052 * Check a full RAID array for plausibility
2056 static void analyze_sbs(mddev_t * mddev)
2059 struct list_head *tmp;
2060 mdk_rdev_t *rdev, *freshest;
2061 char b[BDEVNAME_SIZE];
2064 ITERATE_RDEV(mddev,rdev,tmp)
2065 switch (super_types[mddev->major_version].
2066 load_super(rdev, freshest, mddev->minor_version)) {
2074 "md: fatal superblock inconsistency in %s"
2075 " -- removing from array\n",
2076 bdevname(rdev->bdev,b));
2077 kick_rdev_from_array(rdev);
2081 super_types[mddev->major_version].
2082 validate_super(mddev, freshest);
2085 ITERATE_RDEV(mddev,rdev,tmp) {
2086 if (rdev != freshest)
2087 if (super_types[mddev->major_version].
2088 validate_super(mddev, rdev)) {
2089 printk(KERN_WARNING "md: kicking non-fresh %s"
2091 bdevname(rdev->bdev,b));
2092 kick_rdev_from_array(rdev);
2095 if (mddev->level == LEVEL_MULTIPATH) {
2096 rdev->desc_nr = i++;
2097 rdev->raid_disk = rdev->desc_nr;
2098 set_bit(In_sync, &rdev->flags);
2104 if (mddev->recovery_cp != MaxSector &&
2106 printk(KERN_ERR "md: %s: raid array is not clean"
2107 " -- starting background reconstruction\n",
2113 safe_delay_show(mddev_t *mddev, char *page)
2115 int msec = (mddev->safemode_delay*1000)/HZ;
2116 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2119 safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2127 /* remove a period, and count digits after it */
2128 if (len >= sizeof(buf))
2130 strlcpy(buf, cbuf, len);
2132 for (i=0; i<len; i++) {
2134 if (isdigit(buf[i])) {
2139 } else if (buf[i] == '.') {
2144 msec = simple_strtoul(buf, &e, 10);
2145 if (e == buf || (*e && *e != '\n'))
2147 msec = (msec * 1000) / scale;
2149 mddev->safemode_delay = 0;
2151 mddev->safemode_delay = (msec*HZ)/1000;
2152 if (mddev->safemode_delay == 0)
2153 mddev->safemode_delay = 1;
2157 static struct md_sysfs_entry md_safe_delay =
2158 __ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
2161 level_show(mddev_t *mddev, char *page)
2163 struct mdk_personality *p = mddev->pers;
2165 return sprintf(page, "%s\n", p->name);
2166 else if (mddev->clevel[0])
2167 return sprintf(page, "%s\n", mddev->clevel);
2168 else if (mddev->level != LEVEL_NONE)
2169 return sprintf(page, "%d\n", mddev->level);
2175 level_store(mddev_t *mddev, const char *buf, size_t len)
2182 if (len >= sizeof(mddev->clevel))
2184 strncpy(mddev->clevel, buf, len);
2185 if (mddev->clevel[len-1] == '\n')
2187 mddev->clevel[len] = 0;
2188 mddev->level = LEVEL_NONE;
2192 static struct md_sysfs_entry md_level =
2193 __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
2197 layout_show(mddev_t *mddev, char *page)
2199 /* just a number, not meaningful for all levels */
2200 return sprintf(page, "%d\n", mddev->layout);
2204 layout_store(mddev_t *mddev, const char *buf, size_t len)
2207 unsigned long n = simple_strtoul(buf, &e, 10);
2211 if (!*buf || (*e && *e != '\n'))
2217 static struct md_sysfs_entry md_layout =
2218 __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
2222 raid_disks_show(mddev_t *mddev, char *page)
2224 if (mddev->raid_disks == 0)
2226 return sprintf(page, "%d\n", mddev->raid_disks);
2229 static int update_raid_disks(mddev_t *mddev, int raid_disks);
2232 raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2234 /* can only set raid_disks if array is not yet active */
2237 unsigned long n = simple_strtoul(buf, &e, 10);
2239 if (!*buf || (*e && *e != '\n'))
2243 rv = update_raid_disks(mddev, n);
2245 mddev->raid_disks = n;
2246 return rv ? rv : len;
2248 static struct md_sysfs_entry md_raid_disks =
2249 __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
2252 chunk_size_show(mddev_t *mddev, char *page)
2254 return sprintf(page, "%d\n", mddev->chunk_size);
2258 chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2260 /* can only set chunk_size if array is not yet active */
2262 unsigned long n = simple_strtoul(buf, &e, 10);
2266 if (!*buf || (*e && *e != '\n'))
2269 mddev->chunk_size = n;
2272 static struct md_sysfs_entry md_chunk_size =
2273 __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
2276 resync_start_show(mddev_t *mddev, char *page)
2278 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2282 resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2284 /* can only set chunk_size if array is not yet active */
2286 unsigned long long n = simple_strtoull(buf, &e, 10);
2290 if (!*buf || (*e && *e != '\n'))
2293 mddev->recovery_cp = n;
2296 static struct md_sysfs_entry md_resync_start =
2297 __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
2300 * The array state can be:
2303 * No devices, no size, no level
2304 * Equivalent to STOP_ARRAY ioctl
2306 * May have some settings, but array is not active
2307 * all IO results in error
2308 * When written, doesn't tear down array, but just stops it
2309 * suspended (not supported yet)
2310 * All IO requests will block. The array can be reconfigured.
2311 * Writing this, if accepted, will block until array is quiessent
2313 * no resync can happen. no superblocks get written.
2314 * write requests fail
2316 * like readonly, but behaves like 'clean' on a write request.
2318 * clean - no pending writes, but otherwise active.
2319 * When written to inactive array, starts without resync
2320 * If a write request arrives then
2321 * if metadata is known, mark 'dirty' and switch to 'active'.
2322 * if not known, block and switch to write-pending
2323 * If written to an active array that has pending writes, then fails.
2325 * fully active: IO and resync can be happening.
2326 * When written to inactive array, starts with resync
2329 * clean, but writes are blocked waiting for 'active' to be written.
2332 * like active, but no writes have been seen for a while (100msec).
2335 enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
2336 write_pending, active_idle, bad_word};
2337 static char *array_states[] = {
2338 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2339 "write-pending", "active-idle", NULL };
2341 static int match_word(const char *word, char **list)
2344 for (n=0; list[n]; n++)
2345 if (cmd_match(word, list[n]))
2351 array_state_show(mddev_t *mddev, char *page)
2353 enum array_state st = inactive;
2366 else if (mddev->safemode)
2372 if (list_empty(&mddev->disks) &&
2373 mddev->raid_disks == 0 &&
2379 return sprintf(page, "%s\n", array_states[st]);
2382 static int do_md_stop(mddev_t * mddev, int ro);
2383 static int do_md_run(mddev_t * mddev);
2384 static int restart_array(mddev_t *mddev);
2387 array_state_store(mddev_t *mddev, const char *buf, size_t len)
2390 enum array_state st = match_word(buf, array_states);
2395 /* stopping an active array */
2397 if (atomic_read(&mddev->active) > 1)
2399 err = do_md_stop(mddev, 0);
2403 /* stopping an active array */
2405 if (atomic_read(&mddev->active) > 1)
2407 err = do_md_stop(mddev, 2);
2411 break; /* not supported yet */
2414 err = do_md_stop(mddev, 1);
2417 err = do_md_run(mddev);
2421 /* stopping an active array */
2423 err = do_md_stop(mddev, 1);
2425 mddev->ro = 2; /* FIXME mark devices writable */
2428 err = do_md_run(mddev);
2433 restart_array(mddev);
2434 spin_lock_irq(&mddev->write_lock);
2435 if (atomic_read(&mddev->writes_pending) == 0) {
2437 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
2439 spin_unlock_irq(&mddev->write_lock);
2442 mddev->recovery_cp = MaxSector;
2443 err = do_md_run(mddev);
2448 restart_array(mddev);
2449 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
2450 wake_up(&mddev->sb_wait);
2454 err = do_md_run(mddev);
2459 /* these cannot be set */
2467 static struct md_sysfs_entry md_array_state =
2468 __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
2471 null_show(mddev_t *mddev, char *page)
2477 new_dev_store(mddev_t *mddev, const char *buf, size_t len)
2479 /* buf must be %d:%d\n? giving major and minor numbers */
2480 /* The new device is added to the array.
2481 * If the array has a persistent superblock, we read the
2482 * superblock to initialise info and check validity.
2483 * Otherwise, only checking done is that in bind_rdev_to_array,
2484 * which mainly checks size.
2487 int major = simple_strtoul(buf, &e, 10);
2493 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
2495 minor = simple_strtoul(e+1, &e, 10);
2496 if (*e && *e != '\n')
2498 dev = MKDEV(major, minor);
2499 if (major != MAJOR(dev) ||
2500 minor != MINOR(dev))
2504 if (mddev->persistent) {
2505 rdev = md_import_device(dev, mddev->major_version,
2506 mddev->minor_version);
2507 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
2508 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2509 mdk_rdev_t, same_set);
2510 err = super_types[mddev->major_version]
2511 .load_super(rdev, rdev0, mddev->minor_version);
2516 rdev = md_import_device(dev, -1, -1);
2519 return PTR_ERR(rdev);
2520 err = bind_rdev_to_array(rdev, mddev);
2524 return err ? err : len;
2527 static struct md_sysfs_entry md_new_device =
2528 __ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
2531 bitmap_store(mddev_t *mddev, const char *buf, size_t len)
2534 unsigned long chunk, end_chunk;
2538 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
2540 chunk = end_chunk = simple_strtoul(buf, &end, 0);
2541 if (buf == end) break;
2542 if (*end == '-') { /* range */
2544 end_chunk = simple_strtoul(buf, &end, 0);
2545 if (buf == end) break;
2547 if (*end && !isspace(*end)) break;
2548 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
2550 while (isspace(*buf)) buf++;
2552 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
2557 static struct md_sysfs_entry md_bitmap =
2558 __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
2561 size_show(mddev_t *mddev, char *page)
2563 return sprintf(page, "%llu\n", (unsigned long long)mddev->size);
2566 static int update_size(mddev_t *mddev, unsigned long size);
2569 size_store(mddev_t *mddev, const char *buf, size_t len)
2571 /* If array is inactive, we can reduce the component size, but
2572 * not increase it (except from 0).
2573 * If array is active, we can try an on-line resize
2577 unsigned long long size = simple_strtoull(buf, &e, 10);
2578 if (!*buf || *buf == '\n' ||
2583 err = update_size(mddev, size);
2584 md_update_sb(mddev, 1);
2586 if (mddev->size == 0 ||
2592 return err ? err : len;
2595 static struct md_sysfs_entry md_size =
2596 __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
2600 * This is either 'none' for arrays with externally managed metadata,
2601 * or N.M for internally known formats
2604 metadata_show(mddev_t *mddev, char *page)
2606 if (mddev->persistent)
2607 return sprintf(page, "%d.%d\n",
2608 mddev->major_version, mddev->minor_version);
2610 return sprintf(page, "none\n");
2614 metadata_store(mddev_t *mddev, const char *buf, size_t len)
2618 if (!list_empty(&mddev->disks))
2621 if (cmd_match(buf, "none")) {
2622 mddev->persistent = 0;
2623 mddev->major_version = 0;
2624 mddev->minor_version = 90;
2627 major = simple_strtoul(buf, &e, 10);
2628 if (e==buf || *e != '.')
2631 minor = simple_strtoul(buf, &e, 10);
2632 if (e==buf || *e != '\n')
2634 if (major >= sizeof(super_types)/sizeof(super_types[0]) ||
2635 super_types[major].name == NULL)
2637 mddev->major_version = major;
2638 mddev->minor_version = minor;
2639 mddev->persistent = 1;
2643 static struct md_sysfs_entry md_metadata =
2644 __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
2647 action_show(mddev_t *mddev, char *page)
2649 char *type = "idle";
2650 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2651 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) {
2652 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2654 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2655 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2657 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2664 return sprintf(page, "%s\n", type);
2668 action_store(mddev_t *mddev, const char *page, size_t len)
2670 if (!mddev->pers || !mddev->pers->sync_request)
2673 if (cmd_match(page, "idle")) {
2674 if (mddev->sync_thread) {
2675 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2676 md_unregister_thread(mddev->sync_thread);
2677 mddev->sync_thread = NULL;
2678 mddev->recovery = 0;
2680 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2681 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
2683 else if (cmd_match(page, "resync") || cmd_match(page, "recover"))
2684 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2685 else if (cmd_match(page, "reshape")) {
2687 if (mddev->pers->start_reshape == NULL)
2689 err = mddev->pers->start_reshape(mddev);
2693 if (cmd_match(page, "check"))
2694 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
2695 else if (!cmd_match(page, "repair"))
2697 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
2698 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
2700 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2701 md_wakeup_thread(mddev->thread);
2706 mismatch_cnt_show(mddev_t *mddev, char *page)
2708 return sprintf(page, "%llu\n",
2709 (unsigned long long) mddev->resync_mismatches);
2712 static struct md_sysfs_entry md_scan_mode =
2713 __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
2716 static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
2719 sync_min_show(mddev_t *mddev, char *page)
2721 return sprintf(page, "%d (%s)\n", speed_min(mddev),
2722 mddev->sync_speed_min ? "local": "system");
2726 sync_min_store(mddev_t *mddev, const char *buf, size_t len)
2730 if (strncmp(buf, "system", 6)==0) {
2731 mddev->sync_speed_min = 0;
2734 min = simple_strtoul(buf, &e, 10);
2735 if (buf == e || (*e && *e != '\n') || min <= 0)
2737 mddev->sync_speed_min = min;
2741 static struct md_sysfs_entry md_sync_min =
2742 __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
2745 sync_max_show(mddev_t *mddev, char *page)
2747 return sprintf(page, "%d (%s)\n", speed_max(mddev),
2748 mddev->sync_speed_max ? "local": "system");
2752 sync_max_store(mddev_t *mddev, const char *buf, size_t len)
2756 if (strncmp(buf, "system", 6)==0) {
2757 mddev->sync_speed_max = 0;
2760 max = simple_strtoul(buf, &e, 10);
2761 if (buf == e || (*e && *e != '\n') || max <= 0)
2763 mddev->sync_speed_max = max;
2767 static struct md_sysfs_entry md_sync_max =
2768 __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
2772 sync_speed_show(mddev_t *mddev, char *page)
2774 unsigned long resync, dt, db;
2775 resync = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active));
2776 dt = ((jiffies - mddev->resync_mark) / HZ);
2778 db = resync - (mddev->resync_mark_cnt);
2779 return sprintf(page, "%ld\n", db/dt/2); /* K/sec */
2782 static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
2785 sync_completed_show(mddev_t *mddev, char *page)
2787 unsigned long max_blocks, resync;
2789 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2790 max_blocks = mddev->resync_max_sectors;
2792 max_blocks = mddev->size << 1;
2794 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
2795 return sprintf(page, "%lu / %lu\n", resync, max_blocks);
2798 static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
2801 suspend_lo_show(mddev_t *mddev, char *page)
2803 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
2807 suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
2810 unsigned long long new = simple_strtoull(buf, &e, 10);
2812 if (mddev->pers->quiesce == NULL)
2814 if (buf == e || (*e && *e != '\n'))
2816 if (new >= mddev->suspend_hi ||
2817 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
2818 mddev->suspend_lo = new;
2819 mddev->pers->quiesce(mddev, 2);
2824 static struct md_sysfs_entry md_suspend_lo =
2825 __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
2829 suspend_hi_show(mddev_t *mddev, char *page)
2831 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
2835 suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
2838 unsigned long long new = simple_strtoull(buf, &e, 10);
2840 if (mddev->pers->quiesce == NULL)
2842 if (buf == e || (*e && *e != '\n'))
2844 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
2845 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
2846 mddev->suspend_hi = new;
2847 mddev->pers->quiesce(mddev, 1);
2848 mddev->pers->quiesce(mddev, 0);
2853 static struct md_sysfs_entry md_suspend_hi =
2854 __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
2857 static struct attribute *md_default_attrs[] = {
2860 &md_raid_disks.attr,
2861 &md_chunk_size.attr,
2863 &md_resync_start.attr,
2865 &md_new_device.attr,
2866 &md_safe_delay.attr,
2867 &md_array_state.attr,
2871 static struct attribute *md_redundancy_attrs[] = {
2873 &md_mismatches.attr,
2876 &md_sync_speed.attr,
2877 &md_sync_completed.attr,
2878 &md_suspend_lo.attr,
2879 &md_suspend_hi.attr,
2883 static struct attribute_group md_redundancy_group = {
2885 .attrs = md_redundancy_attrs,
2890 md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2892 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
2893 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
2898 rv = mddev_lock(mddev);
2900 rv = entry->show(mddev, page);
2901 mddev_unlock(mddev);
2907 md_attr_store(struct kobject *kobj, struct attribute *attr,
2908 const char *page, size_t length)
2910 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
2911 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
2916 if (!capable(CAP_SYS_ADMIN))
2918 rv = mddev_lock(mddev);
2920 rv = entry->store(mddev, page, length);
2921 mddev_unlock(mddev);
2926 static void md_free(struct kobject *ko)
2928 mddev_t *mddev = container_of(ko, mddev_t, kobj);
2932 static struct sysfs_ops md_sysfs_ops = {
2933 .show = md_attr_show,
2934 .store = md_attr_store,
2936 static struct kobj_type md_ktype = {
2938 .sysfs_ops = &md_sysfs_ops,
2939 .default_attrs = md_default_attrs,
2944 static struct kobject *md_probe(dev_t dev, int *part, void *data)
2946 static DEFINE_MUTEX(disks_mutex);
2947 mddev_t *mddev = mddev_find(dev);
2948 struct gendisk *disk;
2949 int partitioned = (MAJOR(dev) != MD_MAJOR);
2950 int shift = partitioned ? MdpMinorShift : 0;
2951 int unit = MINOR(dev) >> shift;
2956 mutex_lock(&disks_mutex);
2957 if (mddev->gendisk) {
2958 mutex_unlock(&disks_mutex);
2962 disk = alloc_disk(1 << shift);
2964 mutex_unlock(&disks_mutex);
2968 disk->major = MAJOR(dev);
2969 disk->first_minor = unit << shift;
2971 sprintf(disk->disk_name, "md_d%d", unit);
2973 sprintf(disk->disk_name, "md%d", unit);
2974 disk->fops = &md_fops;
2975 disk->private_data = mddev;
2976 disk->queue = mddev->queue;
2978 mddev->gendisk = disk;
2979 mutex_unlock(&disks_mutex);
2980 mddev->kobj.parent = &disk->kobj;
2981 mddev->kobj.k_name = NULL;
2982 snprintf(mddev->kobj.name, KOBJ_NAME_LEN, "%s", "md");
2983 mddev->kobj.ktype = &md_ktype;
2984 kobject_register(&mddev->kobj);
2988 static void md_safemode_timeout(unsigned long data)
2990 mddev_t *mddev = (mddev_t *) data;
2992 mddev->safemode = 1;
2993 md_wakeup_thread(mddev->thread);
2996 static int start_dirty_degraded;
2998 static int do_md_run(mddev_t * mddev)
3002 struct list_head *tmp;
3004 struct gendisk *disk;
3005 struct mdk_personality *pers;
3006 char b[BDEVNAME_SIZE];
3008 if (list_empty(&mddev->disks))
3009 /* cannot run an array with no devices.. */
3016 * Analyze all RAID superblock(s)
3018 if (!mddev->raid_disks)
3021 chunk_size = mddev->chunk_size;
3024 if (chunk_size > MAX_CHUNK_SIZE) {
3025 printk(KERN_ERR "too big chunk_size: %d > %d\n",
3026 chunk_size, MAX_CHUNK_SIZE);
3030 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
3032 if ( (1 << ffz(~chunk_size)) != chunk_size) {
3033 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
3036 if (chunk_size < PAGE_SIZE) {
3037 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
3038 chunk_size, PAGE_SIZE);
3042 /* devices must have minimum size of one chunk */
3043 ITERATE_RDEV(mddev,rdev,tmp) {
3044 if (test_bit(Faulty, &rdev->flags))
3046 if (rdev->size < chunk_size / 1024) {
3048 "md: Dev %s smaller than chunk_size:"
3050 bdevname(rdev->bdev,b),
3051 (unsigned long long)rdev->size,
3059 if (mddev->level != LEVEL_NONE)
3060 request_module("md-level-%d", mddev->level);
3061 else if (mddev->clevel[0])
3062 request_module("md-%s", mddev->clevel);
3066 * Drop all container device buffers, from now on
3067 * the only valid external interface is through the md
3069 * Also find largest hardsector size
3071 ITERATE_RDEV(mddev,rdev,tmp) {
3072 if (test_bit(Faulty, &rdev->flags))
3074 sync_blockdev(rdev->bdev);
3075 invalidate_bdev(rdev->bdev, 0);
3078 md_probe(mddev->unit, NULL, NULL);
3079 disk = mddev->gendisk;
3083 spin_lock(&pers_lock);
3084 pers = find_pers(mddev->level, mddev->clevel);
3085 if (!pers || !try_module_get(pers->owner)) {
3086 spin_unlock(&pers_lock);
3087 if (mddev->level != LEVEL_NONE)
3088 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
3091 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
3096 spin_unlock(&pers_lock);
3097 mddev->level = pers->level;
3098 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3100 if (mddev->reshape_position != MaxSector &&
3101 pers->start_reshape == NULL) {
3102 /* This personality cannot handle reshaping... */
3104 module_put(pers->owner);
3108 mddev->recovery = 0;
3109 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
3110 mddev->barriers_work = 1;
3111 mddev->ok_start_degraded = start_dirty_degraded;
3114 mddev->ro = 2; /* read-only, but switch on first write */
3116 err = mddev->pers->run(mddev);
3117 if (!err && mddev->pers->sync_request) {
3118 err = bitmap_create(mddev);
3120 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
3121 mdname(mddev), err);
3122 mddev->pers->stop(mddev);
3126 printk(KERN_ERR "md: pers->run() failed ...\n");
3127 module_put(mddev->pers->owner);
3129 bitmap_destroy(mddev);
3132 if (mddev->pers->sync_request)
3133 sysfs_create_group(&mddev->kobj, &md_redundancy_group);
3134 else if (mddev->ro == 2) /* auto-readonly not meaningful */
3137 atomic_set(&mddev->writes_pending,0);
3138 mddev->safemode = 0;
3139 mddev->safemode_timer.function = md_safemode_timeout;
3140 mddev->safemode_timer.data = (unsigned long) mddev;
3141 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
3144 ITERATE_RDEV(mddev,rdev,tmp)
3145 if (rdev->raid_disk >= 0) {
3147 sprintf(nm, "rd%d", rdev->raid_disk);
3148 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
3151 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3154 md_update_sb(mddev, 0);
3156 set_capacity(disk, mddev->array_size<<1);
3158 /* If we call blk_queue_make_request here, it will
3159 * re-initialise max_sectors etc which may have been
3160 * refined inside -> run. So just set the bits we need to set.
3161 * Most initialisation happended when we called
3162 * blk_queue_make_request(..., md_fail_request)
3165 mddev->queue->queuedata = mddev;
3166 mddev->queue->make_request_fn = mddev->pers->make_request;
3168 /* If there is a partially-recovered drive we need to
3169 * start recovery here. If we leave it to md_check_recovery,
3170 * it will remove the drives and not do the right thing
3172 if (mddev->degraded && !mddev->sync_thread) {
3173 struct list_head *rtmp;
3175 ITERATE_RDEV(mddev,rdev,rtmp)
3176 if (rdev->raid_disk >= 0 &&
3177 !test_bit(In_sync, &rdev->flags) &&
3178 !test_bit(Faulty, &rdev->flags))
3179 /* complete an interrupted recovery */
3181 if (spares && mddev->pers->sync_request) {
3182 mddev->recovery = 0;
3183 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3184 mddev->sync_thread = md_register_thread(md_do_sync,
3187 if (!mddev->sync_thread) {
3188 printk(KERN_ERR "%s: could not start resync"
3191 /* leave the spares where they are, it shouldn't hurt */
3192 mddev->recovery = 0;
3196 md_wakeup_thread(mddev->thread);
3197 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
3200 md_new_event(mddev);
3204 static int restart_array(mddev_t *mddev)
3206 struct gendisk *disk = mddev->gendisk;
3210 * Complain if it has no devices
3213 if (list_empty(&mddev->disks))
3221 mddev->safemode = 0;
3223 set_disk_ro(disk, 0);
3225 printk(KERN_INFO "md: %s switched to read-write mode.\n",
3228 * Kick recovery or resync if necessary
3230 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3231 md_wakeup_thread(mddev->thread);
3232 md_wakeup_thread(mddev->sync_thread);
3241 /* similar to deny_write_access, but accounts for our holding a reference
3242 * to the file ourselves */
3243 static int deny_bitmap_write_access(struct file * file)
3245 struct inode *inode = file->f_mapping->host;
3247 spin_lock(&inode->i_lock);
3248 if (atomic_read(&inode->i_writecount) > 1) {
3249 spin_unlock(&inode->i_lock);
3252 atomic_set(&inode->i_writecount, -1);
3253 spin_unlock(&inode->i_lock);
3258 static void restore_bitmap_write_access(struct file *file)
3260 struct inode *inode = file->f_mapping->host;
3262 spin_lock(&inode->i_lock);
3263 atomic_set(&inode->i_writecount, 1);
3264 spin_unlock(&inode->i_lock);
3268 * 0 - completely stop and dis-assemble array
3269 * 1 - switch to readonly
3270 * 2 - stop but do not disassemble array
3272 static int do_md_stop(mddev_t * mddev, int mode)
3275 struct gendisk *disk = mddev->gendisk;
3278 if (atomic_read(&mddev->active)>2) {
3279 printk("md: %s still in use.\n",mdname(mddev));
3283 if (mddev->sync_thread) {
3284 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3285 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3286 md_unregister_thread(mddev->sync_thread);
3287 mddev->sync_thread = NULL;
3290 del_timer_sync(&mddev->safemode_timer);
3292 invalidate_partition(disk, 0);
3295 case 1: /* readonly */
3301 case 0: /* disassemble */
3303 bitmap_flush(mddev);
3304 md_super_wait(mddev);
3306 set_disk_ro(disk, 0);
3307 blk_queue_make_request(mddev->queue, md_fail_request);
3308 mddev->pers->stop(mddev);
3309 if (mddev->pers->sync_request)
3310 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
3312 module_put(mddev->pers->owner);
3317 if (!mddev->in_sync || mddev->flags) {
3318 /* mark array as shutdown cleanly */
3320 md_update_sb(mddev, 1);
3323 set_disk_ro(disk, 1);
3324 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3328 * Free resources if final stop
3332 struct list_head *tmp;
3333 struct gendisk *disk;
3334 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
3336 bitmap_destroy(mddev);
3337 if (mddev->bitmap_file) {
3338 restore_bitmap_write_access(mddev->bitmap_file);
3339 fput(mddev->bitmap_file);
3340 mddev->bitmap_file = NULL;
3342 mddev->bitmap_offset = 0;
3344 ITERATE_RDEV(mddev,rdev,tmp)
3345 if (rdev->raid_disk >= 0) {
3347 sprintf(nm, "rd%d", rdev->raid_disk);
3348 sysfs_remove_link(&mddev->kobj, nm);
3351 export_array(mddev);
3353 mddev->array_size = 0;
3355 mddev->raid_disks = 0;
3356 mddev->recovery_cp = 0;
3358 disk = mddev->gendisk;
3360 set_capacity(disk, 0);
3362 } else if (mddev->pers)
3363 printk(KERN_INFO "md: %s switched to read-only mode.\n",
3366 md_new_event(mddev);
3371 static void autorun_array(mddev_t *mddev)
3374 struct list_head *tmp;
3377 if (list_empty(&mddev->disks))
3380 printk(KERN_INFO "md: running: ");
3382 ITERATE_RDEV(mddev,rdev,tmp) {
3383 char b[BDEVNAME_SIZE];
3384 printk("<%s>", bdevname(rdev->bdev,b));
3388 err = do_md_run (mddev);
3390 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
3391 do_md_stop (mddev, 0);
3396 * lets try to run arrays based on all disks that have arrived
3397 * until now. (those are in pending_raid_disks)
3399 * the method: pick the first pending disk, collect all disks with
3400 * the same UUID, remove all from the pending list and put them into
3401 * the 'same_array' list. Then order this list based on superblock
3402 * update time (freshest comes first), kick out 'old' disks and
3403 * compare superblocks. If everything's fine then run it.
3405 * If "unit" is allocated, then bump its reference count
3407 static void autorun_devices(int part)
3409 struct list_head *tmp;
3410 mdk_rdev_t *rdev0, *rdev;
3412 char b[BDEVNAME_SIZE];
3414 printk(KERN_INFO "md: autorun ...\n");
3415 while (!list_empty(&pending_raid_disks)) {
3418 LIST_HEAD(candidates);
3419 rdev0 = list_entry(pending_raid_disks.next,
3420 mdk_rdev_t, same_set);
3422 printk(KERN_INFO "md: considering %s ...\n",
3423 bdevname(rdev0->bdev,b));
3424 INIT_LIST_HEAD(&candidates);
3425 ITERATE_RDEV_PENDING(rdev,tmp)
3426 if (super_90_load(rdev, rdev0, 0) >= 0) {
3427 printk(KERN_INFO "md: adding %s ...\n",
3428 bdevname(rdev->bdev,b));
3429 list_move(&rdev->same_set, &candidates);
3432 * now we have a set of devices, with all of them having
3433 * mostly sane superblocks. It's time to allocate the
3437 dev = MKDEV(mdp_major,
3438 rdev0->preferred_minor << MdpMinorShift);
3439 unit = MINOR(dev) >> MdpMinorShift;
3441 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
3444 if (rdev0->preferred_minor != unit) {
3445 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
3446 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
3450 md_probe(dev, NULL, NULL);
3451 mddev = mddev_find(dev);
3454 "md: cannot allocate memory for md drive.\n");
3457 if (mddev_lock(mddev))
3458 printk(KERN_WARNING "md: %s locked, cannot run\n",
3460 else if (mddev->raid_disks || mddev->major_version
3461 || !list_empty(&mddev->disks)) {
3463 "md: %s already running, cannot run %s\n",
3464 mdname(mddev), bdevname(rdev0->bdev,b));
3465 mddev_unlock(mddev);
3467 printk(KERN_INFO "md: created %s\n", mdname(mddev));
3468 ITERATE_RDEV_GENERIC(candidates,rdev,tmp) {
3469 list_del_init(&rdev->same_set);
3470 if (bind_rdev_to_array(rdev, mddev))
3473 autorun_array(mddev);
3474 mddev_unlock(mddev);
3476 /* on success, candidates will be empty, on error
3479 ITERATE_RDEV_GENERIC(candidates,rdev,tmp)
3483 printk(KERN_INFO "md: ... autorun DONE.\n");
3486 static int get_version(void __user * arg)
3490 ver.major = MD_MAJOR_VERSION;
3491 ver.minor = MD_MINOR_VERSION;
3492 ver.patchlevel = MD_PATCHLEVEL_VERSION;
3494 if (copy_to_user(arg, &ver, sizeof(ver)))
3500 static int get_array_info(mddev_t * mddev, void __user * arg)
3502 mdu_array_info_t info;
3503 int nr,working,active,failed,spare;
3505 struct list_head *tmp;
3507 nr=working=active=failed=spare=0;
3508 ITERATE_RDEV(mddev,rdev,tmp) {
3510 if (test_bit(Faulty, &rdev->flags))
3514 if (test_bit(In_sync, &rdev->flags))
3521 info.major_version = mddev->major_version;
3522 info.minor_version = mddev->minor_version;
3523 info.patch_version = MD_PATCHLEVEL_VERSION;
3524 info.ctime = mddev->ctime;
3525 info.level = mddev->level;
3526 info.size = mddev->size;
3527 if (info.size != mddev->size) /* overflow */
3530 info.raid_disks = mddev->raid_disks;
3531 info.md_minor = mddev->md_minor;
3532 info.not_persistent= !mddev->persistent;
3534 info.utime = mddev->utime;
3537 info.state = (1<<MD_SB_CLEAN);
3538 if (mddev->bitmap && mddev->bitmap_offset)
3539 info.state = (1<<MD_SB_BITMAP_PRESENT);
3540 info.active_disks = active;
3541 info.working_disks = working;
3542 info.failed_disks = failed;
3543 info.spare_disks = spare;
3545 info.layout = mddev->layout;
3546 info.chunk_size = mddev->chunk_size;
3548 if (copy_to_user(arg, &info, sizeof(info)))
3554 static int get_bitmap_file(mddev_t * mddev, void __user * arg)
3556 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
3557 char *ptr, *buf = NULL;
3560 file = kmalloc(sizeof(*file), GFP_KERNEL);
3564 /* bitmap disabled, zero the first byte and copy out */
3565 if (!mddev->bitmap || !mddev->bitmap->file) {
3566 file->pathname[0] = '\0';
3570 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
3574 ptr = file_path(mddev->bitmap->file, buf, sizeof(file->pathname));
3578 strcpy(file->pathname, ptr);
3582 if (copy_to_user(arg, file, sizeof(*file)))
3590 static int get_disk_info(mddev_t * mddev, void __user * arg)
3592 mdu_disk_info_t info;
3596 if (copy_from_user(&info, arg, sizeof(info)))
3601 rdev = find_rdev_nr(mddev, nr);
3603 info.major = MAJOR(rdev->bdev->bd_dev);
3604 info.minor = MINOR(rdev->bdev->bd_dev);
3605 info.raid_disk = rdev->raid_disk;
3607 if (test_bit(Faulty, &rdev->flags))
3608 info.state |= (1<<MD_DISK_FAULTY);
3609 else if (test_bit(In_sync, &rdev->flags)) {
3610 info.state |= (1<<MD_DISK_ACTIVE);
3611 info.state |= (1<<MD_DISK_SYNC);
3613 if (test_bit(WriteMostly, &rdev->flags))
3614 info.state |= (1<<MD_DISK_WRITEMOSTLY);
3616 info.major = info.minor = 0;
3617 info.raid_disk = -1;
3618 info.state = (1<<MD_DISK_REMOVED);
3621 if (copy_to_user(arg, &info, sizeof(info)))
3627 static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
3629 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3631 dev_t dev = MKDEV(info->major,info->minor);
3633 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
3636 if (!mddev->raid_disks) {
3638 /* expecting a device which has a superblock */
3639 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
3642 "md: md_import_device returned %ld\n",
3644 return PTR_ERR(rdev);
3646 if (!list_empty(&mddev->disks)) {
3647 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3648 mdk_rdev_t, same_set);
3649 int err = super_types[mddev->major_version]
3650 .load_super(rdev, rdev0, mddev->minor_version);
3653 "md: %s has different UUID to %s\n",
3654 bdevname(rdev->bdev,b),
3655 bdevname(rdev0->bdev,b2));
3660 err = bind_rdev_to_array(rdev, mddev);
3667 * add_new_disk can be used once the array is assembled
3668 * to add "hot spares". They must already have a superblock
3673 if (!mddev->pers->hot_add_disk) {
3675 "%s: personality does not support diskops!\n",
3679 if (mddev->persistent)
3680 rdev = md_import_device(dev, mddev->major_version,
3681 mddev->minor_version);
3683 rdev = md_import_device(dev, -1, -1);
3686 "md: md_import_device returned %ld\n",
3688 return PTR_ERR(rdev);
3690 /* set save_raid_disk if appropriate */
3691 if (!mddev->persistent) {
3692 if (info->state & (1<<MD_DISK_SYNC) &&
3693 info->raid_disk < mddev->raid_disks)
3694 rdev->raid_disk = info->raid_disk;
3696 rdev->raid_disk = -1;
3698 super_types[mddev->major_version].
3699 validate_super(mddev, rdev);
3700 rdev->saved_raid_disk = rdev->raid_disk;
3702 clear_bit(In_sync, &rdev->flags); /* just to be sure */
3703 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
3704 set_bit(WriteMostly, &rdev->flags);
3706 rdev->raid_disk = -1;
3707 err = bind_rdev_to_array(rdev, mddev);
3708 if (!err && !mddev->pers->hot_remove_disk) {
3709 /* If there is hot_add_disk but no hot_remove_disk
3710 * then added disks for geometry changes,
3711 * and should be added immediately.
3713 super_types[mddev->major_version].
3714 validate_super(mddev, rdev);
3715 err = mddev->pers->hot_add_disk(mddev, rdev);
3717 unbind_rdev_from_array(rdev);
3722 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3723 md_wakeup_thread(mddev->thread);
3727 /* otherwise, add_new_disk is only allowed
3728 * for major_version==0 superblocks
3730 if (mddev->major_version != 0) {
3731 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
3736 if (!(info->state & (1<<MD_DISK_FAULTY))) {
3738 rdev = md_import_device (dev, -1, 0);
3741 "md: error, md_import_device() returned %ld\n",
3743 return PTR_ERR(rdev);
3745 rdev->desc_nr = info->number;
3746 if (info->raid_disk < mddev->raid_disks)
3747 rdev->raid_disk = info->raid_disk;
3749 rdev->raid_disk = -1;
3753 if (rdev->raid_disk < mddev->raid_disks)
3754 if (info->state & (1<<MD_DISK_SYNC))
3755 set_bit(In_sync, &rdev->flags);
3757 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
3758 set_bit(WriteMostly, &rdev->flags);
3760 if (!mddev->persistent) {
3761 printk(KERN_INFO "md: nonpersistent superblock ...\n");
3762 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
3764 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
3765 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
3767 err = bind_rdev_to_array(rdev, mddev);
3777 static int hot_remove_disk(mddev_t * mddev, dev_t dev)
3779 char b[BDEVNAME_SIZE];
3785 rdev = find_rdev(mddev, dev);
3789 if (rdev->raid_disk >= 0)
3792 kick_rdev_from_array(rdev);
3793 md_update_sb(mddev, 1);
3794 md_new_event(mddev);
3798 printk(KERN_WARNING "md: cannot remove active disk %s from %s ... \n",
3799 bdevname(rdev->bdev,b), mdname(mddev));
3803 static int hot_add_disk(mddev_t * mddev, dev_t dev)
3805 char b[BDEVNAME_SIZE];
3813 if (mddev->major_version != 0) {
3814 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
3815 " version-0 superblocks.\n",
3819 if (!mddev->pers->hot_add_disk) {
3821 "%s: personality does not support diskops!\n",
3826 rdev = md_import_device (dev, -1, 0);
3829 "md: error, md_import_device() returned %ld\n",
3834 if (mddev->persistent)
3835 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
3838 rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
3840 size = calc_dev_size(rdev, mddev->chunk_size);
3843 if (test_bit(Faulty, &rdev->flags)) {
3845 "md: can not hot-add faulty %s disk to %s!\n",
3846 bdevname(rdev->bdev,b), mdname(mddev));
3850 clear_bit(In_sync, &rdev->flags);
3852 rdev->saved_raid_disk = -1;
3853 err = bind_rdev_to_array(rdev, mddev);
3858 * The rest should better be atomic, we can have disk failures
3859 * noticed in interrupt contexts ...
3862 if (rdev->desc_nr == mddev->max_disks) {
3863 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
3866 goto abort_unbind_export;
3869 rdev->raid_disk = -1;
3871 md_update_sb(mddev, 1);
3874 * Kick recovery, maybe this spare has to be added to the
3875 * array immediately.
3877 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3878 md_wakeup_thread(mddev->thread);
3879 md_new_event(mddev);
3882 abort_unbind_export:
3883 unbind_rdev_from_array(rdev);
3890 static int set_bitmap_file(mddev_t *mddev, int fd)
3895 if (!mddev->pers->quiesce)
3897 if (mddev->recovery || mddev->sync_thread)
3899 /* we should be able to change the bitmap.. */
3905 return -EEXIST; /* cannot add when bitmap is present */
3906 mddev->bitmap_file = fget(fd);
3908 if (mddev->bitmap_file == NULL) {
3909 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
3914 err = deny_bitmap_write_access(mddev->bitmap_file);
3916 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
3918 fput(mddev->bitmap_file);
3919 mddev->bitmap_file = NULL;
3922 mddev->bitmap_offset = 0; /* file overrides offset */
3923 } else if (mddev->bitmap == NULL)
3924 return -ENOENT; /* cannot remove what isn't there */
3927 mddev->pers->quiesce(mddev, 1);
3929 err = bitmap_create(mddev);
3930 if (fd < 0 || err) {
3931 bitmap_destroy(mddev);
3932 fd = -1; /* make sure to put the file */
3934 mddev->pers->quiesce(mddev, 0);
3937 if (mddev->bitmap_file) {
3938 restore_bitmap_write_access(mddev->bitmap_file);
3939 fput(mddev->bitmap_file);
3941 mddev->bitmap_file = NULL;
3948 * set_array_info is used two different ways
3949 * The original usage is when creating a new array.
3950 * In this usage, raid_disks is > 0 and it together with
3951 * level, size, not_persistent,layout,chunksize determine the
3952 * shape of the array.
3953 * This will always create an array with a type-0.90.0 superblock.
3954 * The newer usage is when assembling an array.
3955 * In this case raid_disks will be 0, and the major_version field is
3956 * use to determine which style super-blocks are to be found on the devices.
3957 * The minor and patch _version numbers are also kept incase the
3958 * super_block handler wishes to interpret them.
3960 static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
3963 if (info->raid_disks == 0) {
3964 /* just setting version number for superblock loading */
3965 if (info->major_version < 0 ||
3966 info->major_version >= sizeof(super_types)/sizeof(super_types[0]) ||
3967 super_types[info->major_version].name == NULL) {
3968 /* maybe try to auto-load a module? */
3970 "md: superblock version %d not known\n",
3971 info->major_version);
3974 mddev->major_version = info->major_version;
3975 mddev->minor_version = info->minor_version;
3976 mddev->patch_version = info->patch_version;
3979 mddev->major_version = MD_MAJOR_VERSION;
3980 mddev->minor_version = MD_MINOR_VERSION;
3981 mddev->patch_version = MD_PATCHLEVEL_VERSION;
3982 mddev->ctime = get_seconds();
3984 mddev->level = info->level;
3985 mddev->clevel[0] = 0;
3986 mddev->size = info->size;
3987 mddev->raid_disks = info->raid_disks;
3988 /* don't set md_minor, it is determined by which /dev/md* was
3991 if (info->state & (1<<MD_SB_CLEAN))
3992 mddev->recovery_cp = MaxSector;
3994 mddev->recovery_cp = 0;
3995 mddev->persistent = ! info->not_persistent;
3997 mddev->layout = info->layout;
3998 mddev->chunk_size = info->chunk_size;
4000 mddev->max_disks = MD_SB_DISKS;
4003 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4005 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
4006 mddev->bitmap_offset = 0;
4008 mddev->reshape_position = MaxSector;
4011 * Generate a 128 bit UUID
4013 get_random_bytes(mddev->uuid, 16);
4015 mddev->new_level = mddev->level;
4016 mddev->new_chunk = mddev->chunk_size;
4017 mddev->new_layout = mddev->layout;
4018 mddev->delta_disks = 0;
4023 static int update_size(mddev_t *mddev, unsigned long size)
4027 struct list_head *tmp;
4028 int fit = (size == 0);
4030 if (mddev->pers->resize == NULL)
4032 /* The "size" is the amount of each device that is used.
4033 * This can only make sense for arrays with redundancy.
4034 * linear and raid0 always use whatever space is available
4035 * We can only consider changing the size if no resync
4036 * or reconstruction is happening, and if the new size
4037 * is acceptable. It must fit before the sb_offset or,
4038 * if that is <data_offset, it must fit before the
4039 * size of each device.
4040 * If size is zero, we find the largest size that fits.
4042 if (mddev->sync_thread)
4044 ITERATE_RDEV(mddev,rdev,tmp) {
4046 if (rdev->sb_offset > rdev->data_offset)
4047 avail = (rdev->sb_offset*2) - rdev->data_offset;
4049 avail = get_capacity(rdev->bdev->bd_disk)
4050 - rdev->data_offset;
4051 if (fit && (size == 0 || size > avail/2))
4053 if (avail < ((sector_t)size << 1))
4056 rv = mddev->pers->resize(mddev, (sector_t)size *2);
4058 struct block_device *bdev;
4060 bdev = bdget_disk(mddev->gendisk, 0);
4062 mutex_lock(&bdev->bd_inode->i_mutex);
4063 i_size_write(bdev->bd_inode, (loff_t)mddev->array_size << 10);
4064 mutex_unlock(&bdev->bd_inode->i_mutex);
4071 static int update_raid_disks(mddev_t *mddev, int raid_disks)
4074 /* change the number of raid disks */
4075 if (mddev->pers->check_reshape == NULL)
4077 if (raid_disks <= 0 ||
4078 raid_disks >= mddev->max_disks)
4080 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
4082 mddev->delta_disks = raid_disks - mddev->raid_disks;
4084 rv = mddev->pers->check_reshape(mddev);
4090 * update_array_info is used to change the configuration of an
4092 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
4093 * fields in the info are checked against the array.
4094 * Any differences that cannot be handled will cause an error.
4095 * Normally, only one change can be managed at a time.
4097 static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
4103 /* calculate expected state,ignoring low bits */
4104 if (mddev->bitmap && mddev->bitmap_offset)
4105 state |= (1 << MD_SB_BITMAP_PRESENT);
4107 if (mddev->major_version != info->major_version ||
4108 mddev->minor_version != info->minor_version ||
4109 /* mddev->patch_version != info->patch_version || */
4110 mddev->ctime != info->ctime ||
4111 mddev->level != info->level ||
4112 /* mddev->layout != info->layout || */
4113 !mddev->persistent != info->not_persistent||
4114 mddev->chunk_size != info->chunk_size ||
4115 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
4116 ((state^info->state) & 0xfffffe00)
4119 /* Check there is only one change */
4120 if (info->size >= 0 && mddev->size != info->size) cnt++;
4121 if (mddev->raid_disks != info->raid_disks) cnt++;
4122 if (mddev->layout != info->layout) cnt++;
4123 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
4124 if (cnt == 0) return 0;
4125 if (cnt > 1) return -EINVAL;
4127 if (mddev->layout != info->layout) {
4129 * we don't need to do anything at the md level, the
4130 * personality will take care of it all.
4132 if (mddev->pers->reconfig == NULL)
4135 return mddev->pers->reconfig(mddev, info->layout, -1);
4137 if (info->size >= 0 && mddev->size != info->size)
4138 rv = update_size(mddev, info->size);
4140 if (mddev->raid_disks != info->raid_disks)
4141 rv = update_raid_disks(mddev, info->raid_disks);
4143 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
4144 if (mddev->pers->quiesce == NULL)
4146 if (mddev->recovery || mddev->sync_thread)
4148 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
4149 /* add the bitmap */
4152 if (mddev->default_bitmap_offset == 0)
4154 mddev->bitmap_offset = mddev->default_bitmap_offset;
4155 mddev->pers->quiesce(mddev, 1);
4156 rv = bitmap_create(mddev);
4158 bitmap_destroy(mddev);
4159 mddev->pers->quiesce(mddev, 0);
4161 /* remove the bitmap */
4164 if (mddev->bitmap->file)
4166 mddev->pers->quiesce(mddev, 1);
4167 bitmap_destroy(mddev);
4168 mddev->pers->quiesce(mddev, 0);
4169 mddev->bitmap_offset = 0;
4172 md_update_sb(mddev, 1);
4176 static int set_disk_faulty(mddev_t *mddev, dev_t dev)
4180 if (mddev->pers == NULL)
4183 rdev = find_rdev(mddev, dev);
4187 md_error(mddev, rdev);
4191 static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4193 mddev_t *mddev = bdev->bd_disk->private_data;
4197 geo->cylinders = get_capacity(mddev->gendisk) / 8;
4201 static int md_ioctl(struct inode *inode, struct file *file,
4202 unsigned int cmd, unsigned long arg)
4205 void __user *argp = (void __user *)arg;
4206 mddev_t *mddev = NULL;
4208 if (!capable(CAP_SYS_ADMIN))
4212 * Commands dealing with the RAID driver but not any
4218 err = get_version(argp);
4221 case PRINT_RAID_DEBUG:
4229 autostart_arrays(arg);
4236 * Commands creating/starting a new array:
4239 mddev = inode->i_bdev->bd_disk->private_data;
4246 err = mddev_lock(mddev);
4249 "md: ioctl lock interrupted, reason %d, cmd %d\n",
4256 case SET_ARRAY_INFO:
4258 mdu_array_info_t info;
4260 memset(&info, 0, sizeof(info));
4261 else if (copy_from_user(&info, argp, sizeof(info))) {
4266 err = update_array_info(mddev, &info);
4268 printk(KERN_WARNING "md: couldn't update"
4269 " array info. %d\n", err);
4274 if (!list_empty(&mddev->disks)) {
4276 "md: array %s already has disks!\n",
4281 if (mddev->raid_disks) {
4283 "md: array %s already initialised!\n",
4288 err = set_array_info(mddev, &info);
4290 printk(KERN_WARNING "md: couldn't set"
4291 " array info. %d\n", err);
4301 * Commands querying/configuring an existing array:
4303 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
4304 * RUN_ARRAY, and SET_BITMAP_FILE are allowed */
4305 if (!mddev->raid_disks && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
4306 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE) {
4312 * Commands even a read-only array can execute:
4316 case GET_ARRAY_INFO:
4317 err = get_array_info(mddev, argp);
4320 case GET_BITMAP_FILE:
4321 err = get_bitmap_file(mddev, argp);
4325 err = get_disk_info(mddev, argp);
4328 case RESTART_ARRAY_RW:
4329 err = restart_array(mddev);
4333 err = do_md_stop (mddev, 0);
4337 err = do_md_stop (mddev, 1);
4341 * We have a problem here : there is no easy way to give a CHS
4342 * virtual geometry. We currently pretend that we have a 2 heads
4343 * 4 sectors (with a BIG number of cylinders...). This drives
4344 * dosfs just mad... ;-)
4349 * The remaining ioctls are changing the state of the
4350 * superblock, so we do not allow them on read-only arrays.
4351 * However non-MD ioctls (e.g. get-size) will still come through
4352 * here and hit the 'default' below, so only disallow
4353 * 'md' ioctls, and switch to rw mode if started auto-readonly.
4355 if (_IOC_TYPE(cmd) == MD_MAJOR &&
4356 mddev->ro && mddev->pers) {
4357 if (mddev->ro == 2) {
4359 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4360 md_wakeup_thread(mddev->thread);
4372 mdu_disk_info_t info;
4373 if (copy_from_user(&info, argp, sizeof(info)))
4376 err = add_new_disk(mddev, &info);
4380 case HOT_REMOVE_DISK:
4381 err = hot_remove_disk(mddev, new_decode_dev(arg));
4385 err = hot_add_disk(mddev, new_decode_dev(arg));
4388 case SET_DISK_FAULTY:
4389 err = set_disk_faulty(mddev, new_decode_dev(arg));
4393 err = do_md_run (mddev);
4396 case SET_BITMAP_FILE:
4397 err = set_bitmap_file(mddev, (int)arg);
4407 mddev_unlock(mddev);
4417 static int md_open(struct inode *inode, struct file *file)
4420 * Succeed if we can lock the mddev, which confirms that
4421 * it isn't being stopped right now.
4423 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4426 if ((err = mddev_lock(mddev)))
4431 mddev_unlock(mddev);
4433 check_disk_change(inode->i_bdev);
4438 static int md_release(struct inode *inode, struct file * file)
4440 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4448 static int md_media_changed(struct gendisk *disk)
4450 mddev_t *mddev = disk->private_data;
4452 return mddev->changed;
4455 static int md_revalidate(struct gendisk *disk)
4457 mddev_t *mddev = disk->private_data;
4462 static struct block_device_operations md_fops =
4464 .owner = THIS_MODULE,
4466 .release = md_release,
4468 .getgeo = md_getgeo,
4469 .media_changed = md_media_changed,
4470 .revalidate_disk= md_revalidate,
4473 static int md_thread(void * arg)
4475 mdk_thread_t *thread = arg;
4478 * md_thread is a 'system-thread', it's priority should be very
4479 * high. We avoid resource deadlocks individually in each
4480 * raid personality. (RAID5 does preallocation) We also use RR and
4481 * the very same RT priority as kswapd, thus we will never get
4482 * into a priority inversion deadlock.
4484 * we definitely have to have equal or higher priority than
4485 * bdflush, otherwise bdflush will deadlock if there are too
4486 * many dirty RAID5 blocks.
4489 allow_signal(SIGKILL);
4490 while (!kthread_should_stop()) {
4492 /* We need to wait INTERRUPTIBLE so that
4493 * we don't add to the load-average.
4494 * That means we need to be sure no signals are
4497 if (signal_pending(current))
4498 flush_signals(current);
4500 wait_event_interruptible_timeout
4502 test_bit(THREAD_WAKEUP, &thread->flags)
4503 || kthread_should_stop(),
4507 clear_bit(THREAD_WAKEUP, &thread->flags);
4509 thread->run(thread->mddev);
4515 void md_wakeup_thread(mdk_thread_t *thread)
4518 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
4519 set_bit(THREAD_WAKEUP, &thread->flags);
4520 wake_up(&thread->wqueue);
4524 mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
4527 mdk_thread_t *thread;
4529 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
4533 init_waitqueue_head(&thread->wqueue);
4536 thread->mddev = mddev;
4537 thread->timeout = MAX_SCHEDULE_TIMEOUT;
4538 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
4539 if (IS_ERR(thread->tsk)) {
4546 void md_unregister_thread(mdk_thread_t *thread)
4548 dprintk("interrupting MD-thread pid %d\n", thread->tsk->pid);
4550 kthread_stop(thread->tsk);
4554 void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
4561 if (!rdev || test_bit(Faulty, &rdev->flags))
4564 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
4566 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
4567 __builtin_return_address(0),__builtin_return_address(1),
4568 __builtin_return_address(2),__builtin_return_address(3));
4572 if (!mddev->pers->error_handler)
4574 mddev->pers->error_handler(mddev,rdev);
4575 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4576 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4577 md_wakeup_thread(mddev->thread);
4578 md_new_event_inintr(mddev);
4581 /* seq_file implementation /proc/mdstat */
4583 static void status_unused(struct seq_file *seq)
4587 struct list_head *tmp;
4589 seq_printf(seq, "unused devices: ");
4591 ITERATE_RDEV_PENDING(rdev,tmp) {
4592 char b[BDEVNAME_SIZE];
4594 seq_printf(seq, "%s ",
4595 bdevname(rdev->bdev,b));
4598 seq_printf(seq, "<none>");
4600 seq_printf(seq, "\n");
4604 static void status_resync(struct seq_file *seq, mddev_t * mddev)
4606 sector_t max_blocks, resync, res;
4607 unsigned long dt, db, rt;
4609 unsigned int per_milli;
4611 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
4613 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
4614 max_blocks = mddev->resync_max_sectors >> 1;
4616 max_blocks = mddev->size;
4619 * Should not happen.
4625 /* Pick 'scale' such that (resync>>scale)*1000 will fit
4626 * in a sector_t, and (max_blocks>>scale) will fit in a
4627 * u32, as those are the requirements for sector_div.
4628 * Thus 'scale' must be at least 10
4631 if (sizeof(sector_t) > sizeof(unsigned long)) {
4632 while ( max_blocks/2 > (1ULL<<(scale+32)))
4635 res = (resync>>scale)*1000;
4636 sector_div(res, (u32)((max_blocks>>scale)+1));
4640 int i, x = per_milli/50, y = 20-x;
4641 seq_printf(seq, "[");
4642 for (i = 0; i < x; i++)
4643 seq_printf(seq, "=");
4644 seq_printf(seq, ">");
4645 for (i = 0; i < y; i++)
4646 seq_printf(seq, ".");
4647 seq_printf(seq, "] ");
4649 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
4650 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
4652 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
4654 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
4655 "resync" : "recovery"))),
4656 per_milli/10, per_milli % 10,
4657 (unsigned long long) resync,
4658 (unsigned long long) max_blocks);
4661 * We do not want to overflow, so the order of operands and
4662 * the * 100 / 100 trick are important. We do a +1 to be
4663 * safe against division by zero. We only estimate anyway.
4665 * dt: time from mark until now
4666 * db: blocks written from mark until now
4667 * rt: remaining time
4669 dt = ((jiffies - mddev->resync_mark) / HZ);
4671 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
4672 - mddev->resync_mark_cnt;
4673 rt = (dt * ((unsigned long)(max_blocks-resync) / (db/2/100+1)))/100;
4675 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
4677 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
4680 static void *md_seq_start(struct seq_file *seq, loff_t *pos)
4682 struct list_head *tmp;
4692 spin_lock(&all_mddevs_lock);
4693 list_for_each(tmp,&all_mddevs)
4695 mddev = list_entry(tmp, mddev_t, all_mddevs);
4697 spin_unlock(&all_mddevs_lock);
4700 spin_unlock(&all_mddevs_lock);
4702 return (void*)2;/* tail */
4706 static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4708 struct list_head *tmp;
4709 mddev_t *next_mddev, *mddev = v;
4715 spin_lock(&all_mddevs_lock);
4717 tmp = all_mddevs.next;
4719 tmp = mddev->all_mddevs.next;
4720 if (tmp != &all_mddevs)
4721 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
4723 next_mddev = (void*)2;
4726 spin_unlock(&all_mddevs_lock);
4734 static void md_seq_stop(struct seq_file *seq, void *v)
4738 if (mddev && v != (void*)1 && v != (void*)2)
4742 struct mdstat_info {
4746 static int md_seq_show(struct seq_file *seq, void *v)
4750 struct list_head *tmp2;
4752 struct mdstat_info *mi = seq->private;
4753 struct bitmap *bitmap;
4755 if (v == (void*)1) {
4756 struct mdk_personality *pers;
4757 seq_printf(seq, "Personalities : ");
4758 spin_lock(&pers_lock);
4759 list_for_each_entry(pers, &pers_list, list)
4760 seq_printf(seq, "[%s] ", pers->name);
4762 spin_unlock(&pers_lock);
4763 seq_printf(seq, "\n");
4764 mi->event = atomic_read(&md_event_count);
4767 if (v == (void*)2) {
4772 if (mddev_lock(mddev) < 0)
4775 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
4776 seq_printf(seq, "%s : %sactive", mdname(mddev),
4777 mddev->pers ? "" : "in");
4780 seq_printf(seq, " (read-only)");
4782 seq_printf(seq, "(auto-read-only)");
4783 seq_printf(seq, " %s", mddev->pers->name);
4787 ITERATE_RDEV(mddev,rdev,tmp2) {
4788 char b[BDEVNAME_SIZE];
4789 seq_printf(seq, " %s[%d]",
4790 bdevname(rdev->bdev,b), rdev->desc_nr);
4791 if (test_bit(WriteMostly, &rdev->flags))
4792 seq_printf(seq, "(W)");
4793 if (test_bit(Faulty, &rdev->flags)) {
4794 seq_printf(seq, "(F)");
4796 } else if (rdev->raid_disk < 0)
4797 seq_printf(seq, "(S)"); /* spare */
4801 if (!list_empty(&mddev->disks)) {
4803 seq_printf(seq, "\n %llu blocks",
4804 (unsigned long long)mddev->array_size);
4806 seq_printf(seq, "\n %llu blocks",
4807 (unsigned long long)size);
4809 if (mddev->persistent) {
4810 if (mddev->major_version != 0 ||
4811 mddev->minor_version != 90) {
4812 seq_printf(seq," super %d.%d",
4813 mddev->major_version,
4814 mddev->minor_version);
4817 seq_printf(seq, " super non-persistent");
4820 mddev->pers->status (seq, mddev);
4821 seq_printf(seq, "\n ");
4822 if (mddev->pers->sync_request) {
4823 if (mddev->curr_resync > 2) {
4824 status_resync (seq, mddev);
4825 seq_printf(seq, "\n ");
4826 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
4827 seq_printf(seq, "\tresync=DELAYED\n ");
4828 else if (mddev->recovery_cp < MaxSector)
4829 seq_printf(seq, "\tresync=PENDING\n ");
4832 seq_printf(seq, "\n ");
4834 if ((bitmap = mddev->bitmap)) {
4835 unsigned long chunk_kb;
4836 unsigned long flags;
4837 spin_lock_irqsave(&bitmap->lock, flags);
4838 chunk_kb = bitmap->chunksize >> 10;
4839 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
4841 bitmap->pages - bitmap->missing_pages,
4843 (bitmap->pages - bitmap->missing_pages)
4844 << (PAGE_SHIFT - 10),
4845 chunk_kb ? chunk_kb : bitmap->chunksize,
4846 chunk_kb ? "KB" : "B");
4848 seq_printf(seq, ", file: ");
4849 seq_path(seq, bitmap->file->f_vfsmnt,
4850 bitmap->file->f_dentry," \t\n");
4853 seq_printf(seq, "\n");
4854 spin_unlock_irqrestore(&bitmap->lock, flags);
4857 seq_printf(seq, "\n");
4859 mddev_unlock(mddev);
4864 static struct seq_operations md_seq_ops = {
4865 .start = md_seq_start,
4866 .next = md_seq_next,
4867 .stop = md_seq_stop,
4868 .show = md_seq_show,
4871 static int md_seq_open(struct inode *inode, struct file *file)
4874 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
4878 error = seq_open(file, &md_seq_ops);
4882 struct seq_file *p = file->private_data;
4884 mi->event = atomic_read(&md_event_count);
4889 static int md_seq_release(struct inode *inode, struct file *file)
4891 struct seq_file *m = file->private_data;
4892 struct mdstat_info *mi = m->private;
4895 return seq_release(inode, file);
4898 static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
4900 struct seq_file *m = filp->private_data;
4901 struct mdstat_info *mi = m->private;
4904 poll_wait(filp, &md_event_waiters, wait);
4906 /* always allow read */
4907 mask = POLLIN | POLLRDNORM;
4909 if (mi->event != atomic_read(&md_event_count))
4910 mask |= POLLERR | POLLPRI;
4914 static struct file_operations md_seq_fops = {
4915 .open = md_seq_open,
4917 .llseek = seq_lseek,
4918 .release = md_seq_release,
4919 .poll = mdstat_poll,
4922 int register_md_personality(struct mdk_personality *p)
4924 spin_lock(&pers_lock);
4925 list_add_tail(&p->list, &pers_list);
4926 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
4927 spin_unlock(&pers_lock);
4931 int unregister_md_personality(struct mdk_personality *p)
4933 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
4934 spin_lock(&pers_lock);
4935 list_del_init(&p->list);
4936 spin_unlock(&pers_lock);
4940 static int is_mddev_idle(mddev_t *mddev)
4943 struct list_head *tmp;
4945 unsigned long curr_events;
4948 ITERATE_RDEV(mddev,rdev,tmp) {
4949 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
4950 curr_events = disk_stat_read(disk, sectors[0]) +
4951 disk_stat_read(disk, sectors[1]) -
4952 atomic_read(&disk->sync_io);
4953 /* The difference between curr_events and last_events
4954 * will be affected by any new non-sync IO (making
4955 * curr_events bigger) and any difference in the amount of
4956 * in-flight syncio (making current_events bigger or smaller)
4957 * The amount in-flight is currently limited to
4958 * 32*64K in raid1/10 and 256*PAGE_SIZE in raid5/6
4959 * which is at most 4096 sectors.
4960 * These numbers are fairly fragile and should be made
4961 * more robust, probably by enforcing the
4962 * 'window size' that md_do_sync sort-of uses.
4964 * Note: the following is an unsigned comparison.
4966 if ((curr_events - rdev->last_events + 4096) > 8192) {
4967 rdev->last_events = curr_events;
4974 void md_done_sync(mddev_t *mddev, int blocks, int ok)
4976 /* another "blocks" (512byte) blocks have been synced */
4977 atomic_sub(blocks, &mddev->recovery_active);
4978 wake_up(&mddev->recovery_wait);
4980 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
4981 md_wakeup_thread(mddev->thread);
4982 // stop recovery, signal do_sync ....
4987 /* md_write_start(mddev, bi)
4988 * If we need to update some array metadata (e.g. 'active' flag
4989 * in superblock) before writing, schedule a superblock update
4990 * and wait for it to complete.
4992 void md_write_start(mddev_t *mddev, struct bio *bi)
4994 if (bio_data_dir(bi) != WRITE)
4997 BUG_ON(mddev->ro == 1);
4998 if (mddev->ro == 2) {
4999 /* need to switch to read/write */
5001 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5002 md_wakeup_thread(mddev->thread);
5004 atomic_inc(&mddev->writes_pending);
5005 if (mddev->in_sync) {
5006 spin_lock_irq(&mddev->write_lock);
5007 if (mddev->in_sync) {
5009 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
5010 md_wakeup_thread(mddev->thread);
5012 spin_unlock_irq(&mddev->write_lock);
5014 wait_event(mddev->sb_wait, mddev->flags==0);
5017 void md_write_end(mddev_t *mddev)
5019 if (atomic_dec_and_test(&mddev->writes_pending)) {
5020 if (mddev->safemode == 2)
5021 md_wakeup_thread(mddev->thread);
5022 else if (mddev->safemode_delay)
5023 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
5027 static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
5029 #define SYNC_MARKS 10
5030 #define SYNC_MARK_STEP (3*HZ)
5031 void md_do_sync(mddev_t *mddev)
5034 unsigned int currspeed = 0,
5036 sector_t max_sectors,j, io_sectors;
5037 unsigned long mark[SYNC_MARKS];
5038 sector_t mark_cnt[SYNC_MARKS];
5040 struct list_head *tmp;
5041 sector_t last_check;
5043 struct list_head *rtmp;
5047 /* just incase thread restarts... */
5048 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
5050 if (mddev->ro) /* never try to sync a read-only array */
5053 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5054 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
5055 desc = "data-check";
5056 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5057 desc = "requested-resync";
5060 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5065 /* we overload curr_resync somewhat here.
5066 * 0 == not engaged in resync at all
5067 * 2 == checking that there is no conflict with another sync
5068 * 1 == like 2, but have yielded to allow conflicting resync to
5070 * other == active in resync - this many blocks
5072 * Before starting a resync we must have set curr_resync to
5073 * 2, and then checked that every "conflicting" array has curr_resync
5074 * less than ours. When we find one that is the same or higher
5075 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
5076 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
5077 * This will mean we have to start checking from the beginning again.
5082 mddev->curr_resync = 2;
5085 if (kthread_should_stop()) {
5086 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5089 ITERATE_MDDEV(mddev2,tmp) {
5090 if (mddev2 == mddev)
5092 if (mddev2->curr_resync &&
5093 match_mddev_units(mddev,mddev2)) {
5095 if (mddev < mddev2 && mddev->curr_resync == 2) {
5096 /* arbitrarily yield */
5097 mddev->curr_resync = 1;
5098 wake_up(&resync_wait);
5100 if (mddev > mddev2 && mddev->curr_resync == 1)
5101 /* no need to wait here, we can wait the next
5102 * time 'round when curr_resync == 2
5105 prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE);
5106 if (!kthread_should_stop() &&
5107 mddev2->curr_resync >= mddev->curr_resync) {
5108 printk(KERN_INFO "md: delaying %s of %s"
5109 " until %s has finished (they"
5110 " share one or more physical units)\n",
5111 desc, mdname(mddev), mdname(mddev2));
5114 finish_wait(&resync_wait, &wq);
5117 finish_wait(&resync_wait, &wq);
5120 } while (mddev->curr_resync < 2);
5123 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5124 /* resync follows the size requested by the personality,
5125 * which defaults to physical size, but can be virtual size
5127 max_sectors = mddev->resync_max_sectors;
5128 mddev->resync_mismatches = 0;
5129 /* we don't use the checkpoint if there's a bitmap */
5130 if (!mddev->bitmap &&
5131 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5132 j = mddev->recovery_cp;
5133 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5134 max_sectors = mddev->size << 1;
5136 /* recovery follows the physical size of devices */
5137 max_sectors = mddev->size << 1;
5139 ITERATE_RDEV(mddev,rdev,rtmp)
5140 if (rdev->raid_disk >= 0 &&
5141 !test_bit(Faulty, &rdev->flags) &&
5142 !test_bit(In_sync, &rdev->flags) &&
5143 rdev->recovery_offset < j)
5144 j = rdev->recovery_offset;
5147 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
5148 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
5149 " %d KB/sec/disk.\n", speed_min(mddev));
5150 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
5151 "(but not more than %d KB/sec) for %s.\n",
5152 speed_max(mddev), desc);
5154 is_mddev_idle(mddev); /* this also initializes IO event counters */
5157 for (m = 0; m < SYNC_MARKS; m++) {
5159 mark_cnt[m] = io_sectors;
5162 mddev->resync_mark = mark[last_mark];
5163 mddev->resync_mark_cnt = mark_cnt[last_mark];
5166 * Tune reconstruction:
5168 window = 32*(PAGE_SIZE/512);
5169 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
5170 window/2,(unsigned long long) max_sectors/2);
5172 atomic_set(&mddev->recovery_active, 0);
5173 init_waitqueue_head(&mddev->recovery_wait);
5178 "md: resuming %s of %s from checkpoint.\n",
5179 desc, mdname(mddev));
5180 mddev->curr_resync = j;
5183 while (j < max_sectors) {
5187 sectors = mddev->pers->sync_request(mddev, j, &skipped,
5188 currspeed < speed_min(mddev));
5190 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
5194 if (!skipped) { /* actual IO requested */
5195 io_sectors += sectors;
5196 atomic_add(sectors, &mddev->recovery_active);
5200 if (j>1) mddev->curr_resync = j;
5201 mddev->curr_mark_cnt = io_sectors;
5202 if (last_check == 0)
5203 /* this is the earliers that rebuilt will be
5204 * visible in /proc/mdstat
5206 md_new_event(mddev);
5208 if (last_check + window > io_sectors || j == max_sectors)
5211 last_check = io_sectors;
5213 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery) ||
5214 test_bit(MD_RECOVERY_ERR, &mddev->recovery))
5218 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
5220 int next = (last_mark+1) % SYNC_MARKS;
5222 mddev->resync_mark = mark[next];
5223 mddev->resync_mark_cnt = mark_cnt[next];
5224 mark[next] = jiffies;
5225 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
5230 if (kthread_should_stop()) {
5232 * got a signal, exit.
5235 "md: md_do_sync() got signal ... exiting\n");
5236 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5241 * this loop exits only if either when we are slower than
5242 * the 'hard' speed limit, or the system was IO-idle for
5244 * the system might be non-idle CPU-wise, but we only care
5245 * about not overloading the IO subsystem. (things like an
5246 * e2fsck being done on the RAID array should execute fast)
5248 mddev->queue->unplug_fn(mddev->queue);
5251 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
5252 /((jiffies-mddev->resync_mark)/HZ +1) +1;
5254 if (currspeed > speed_min(mddev)) {
5255 if ((currspeed > speed_max(mddev)) ||
5256 !is_mddev_idle(mddev)) {
5262 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
5264 * this also signals 'finished resyncing' to md_stop
5267 mddev->queue->unplug_fn(mddev->queue);
5269 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
5271 /* tell personality that we are finished */
5272 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
5274 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
5275 test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
5276 !test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
5277 mddev->curr_resync > 2) {
5278 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5279 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5280 if (mddev->curr_resync >= mddev->recovery_cp) {
5282 "md: checkpointing %s of %s.\n",
5283 desc, mdname(mddev));
5284 mddev->recovery_cp = mddev->curr_resync;
5287 mddev->recovery_cp = MaxSector;
5289 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5290 mddev->curr_resync = MaxSector;
5291 ITERATE_RDEV(mddev,rdev,rtmp)
5292 if (rdev->raid_disk >= 0 &&
5293 !test_bit(Faulty, &rdev->flags) &&
5294 !test_bit(In_sync, &rdev->flags) &&
5295 rdev->recovery_offset < mddev->curr_resync)
5296 rdev->recovery_offset = mddev->curr_resync;
5301 mddev->curr_resync = 0;
5302 wake_up(&resync_wait);
5303 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
5304 md_wakeup_thread(mddev->thread);
5306 EXPORT_SYMBOL_GPL(md_do_sync);
5310 * This routine is regularly called by all per-raid-array threads to
5311 * deal with generic issues like resync and super-block update.
5312 * Raid personalities that don't have a thread (linear/raid0) do not
5313 * need this as they never do any recovery or update the superblock.
5315 * It does not do any resync itself, but rather "forks" off other threads
5316 * to do that as needed.
5317 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
5318 * "->recovery" and create a thread at ->sync_thread.
5319 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
5320 * and wakeups up this thread which will reap the thread and finish up.
5321 * This thread also removes any faulty devices (with nr_pending == 0).
5323 * The overall approach is:
5324 * 1/ if the superblock needs updating, update it.
5325 * 2/ If a recovery thread is running, don't do anything else.
5326 * 3/ If recovery has finished, clean up, possibly marking spares active.
5327 * 4/ If there are any faulty devices, remove them.
5328 * 5/ If array is degraded, try to add spares devices
5329 * 6/ If array has spares or is not in-sync, start a resync thread.
5331 void md_check_recovery(mddev_t *mddev)
5334 struct list_head *rtmp;
5338 bitmap_daemon_work(mddev->bitmap);
5343 if (signal_pending(current)) {
5344 if (mddev->pers->sync_request) {
5345 printk(KERN_INFO "md: %s in immediate safe mode\n",
5347 mddev->safemode = 2;
5349 flush_signals(current);
5354 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
5355 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
5356 (mddev->safemode == 1) ||
5357 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
5358 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
5362 if (mddev_trylock(mddev)) {
5365 spin_lock_irq(&mddev->write_lock);
5366 if (mddev->safemode && !atomic_read(&mddev->writes_pending) &&
5367 !mddev->in_sync && mddev->recovery_cp == MaxSector) {
5369 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
5371 if (mddev->safemode == 1)
5372 mddev->safemode = 0;
5373 spin_unlock_irq(&mddev->write_lock);
5376 md_update_sb(mddev, 0);
5379 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
5380 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
5381 /* resync/recovery still happening */
5382 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5385 if (mddev->sync_thread) {
5386 /* resync has finished, collect result */
5387 md_unregister_thread(mddev->sync_thread);
5388 mddev->sync_thread = NULL;
5389 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
5390 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5392 /* activate any spares */
5393 mddev->pers->spare_active(mddev);
5395 md_update_sb(mddev, 1);
5397 /* if array is no-longer degraded, then any saved_raid_disk
5398 * information must be scrapped
5400 if (!mddev->degraded)
5401 ITERATE_RDEV(mddev,rdev,rtmp)
5402 rdev->saved_raid_disk = -1;
5404 mddev->recovery = 0;
5405 /* flag recovery needed just to double check */
5406 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5407 md_new_event(mddev);
5410 /* Clear some bits that don't mean anything, but
5413 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5414 clear_bit(MD_RECOVERY_ERR, &mddev->recovery);
5415 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
5416 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
5418 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
5420 /* no recovery is running.
5421 * remove any failed drives, then
5422 * add spares if possible.
5423 * Spare are also removed and re-added, to allow
5424 * the personality to fail the re-add.
5426 ITERATE_RDEV(mddev,rdev,rtmp)
5427 if (rdev->raid_disk >= 0 &&
5428 (test_bit(Faulty, &rdev->flags) || ! test_bit(In_sync, &rdev->flags)) &&
5429 atomic_read(&rdev->nr_pending)==0) {
5430 if (mddev->pers->hot_remove_disk(mddev, rdev->raid_disk)==0) {
5432 sprintf(nm,"rd%d", rdev->raid_disk);
5433 sysfs_remove_link(&mddev->kobj, nm);
5434 rdev->raid_disk = -1;
5438 if (mddev->degraded) {
5439 ITERATE_RDEV(mddev,rdev,rtmp)
5440 if (rdev->raid_disk < 0
5441 && !test_bit(Faulty, &rdev->flags)) {
5442 rdev->recovery_offset = 0;
5443 if (mddev->pers->hot_add_disk(mddev,rdev)) {
5445 sprintf(nm, "rd%d", rdev->raid_disk);
5446 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
5448 md_new_event(mddev);
5455 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5456 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5457 } else if (mddev->recovery_cp < MaxSector) {
5458 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5459 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
5460 /* nothing to be done ... */
5463 if (mddev->pers->sync_request) {
5464 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5465 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
5466 /* We are adding a device or devices to an array
5467 * which has the bitmap stored on all devices.
5468 * So make sure all bitmap pages get written
5470 bitmap_write_all(mddev->bitmap);
5472 mddev->sync_thread = md_register_thread(md_do_sync,
5475 if (!mddev->sync_thread) {
5476 printk(KERN_ERR "%s: could not start resync"
5479 /* leave the spares where they are, it shouldn't hurt */
5480 mddev->recovery = 0;
5482 md_wakeup_thread(mddev->sync_thread);
5483 md_new_event(mddev);
5486 mddev_unlock(mddev);
5490 static int md_notify_reboot(struct notifier_block *this,
5491 unsigned long code, void *x)
5493 struct list_head *tmp;
5496 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
5498 printk(KERN_INFO "md: stopping all md devices.\n");
5500 ITERATE_MDDEV(mddev,tmp)
5501 if (mddev_trylock(mddev)) {
5502 do_md_stop (mddev, 1);
5503 mddev_unlock(mddev);
5506 * certain more exotic SCSI devices are known to be
5507 * volatile wrt too early system reboots. While the
5508 * right place to handle this issue is the given
5509 * driver, we do want to have a safe RAID driver ...
5516 static struct notifier_block md_notifier = {
5517 .notifier_call = md_notify_reboot,
5519 .priority = INT_MAX, /* before any real devices */
5522 static void md_geninit(void)
5524 struct proc_dir_entry *p;
5526 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
5528 p = create_proc_entry("mdstat", S_IRUGO, NULL);
5530 p->proc_fops = &md_seq_fops;
5533 static int __init md_init(void)
5535 if (register_blkdev(MAJOR_NR, "md"))
5537 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
5538 unregister_blkdev(MAJOR_NR, "md");
5541 blk_register_region(MKDEV(MAJOR_NR, 0), 1UL<<MINORBITS, THIS_MODULE,
5542 md_probe, NULL, NULL);
5543 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
5544 md_probe, NULL, NULL);
5546 register_reboot_notifier(&md_notifier);
5547 raid_table_header = register_sysctl_table(raid_root_table, 1);
5557 * Searches all registered partitions for autorun RAID arrays
5560 static dev_t detected_devices[128];
5563 void md_autodetect_dev(dev_t dev)
5565 if (dev_cnt >= 0 && dev_cnt < 127)
5566 detected_devices[dev_cnt++] = dev;
5570 static void autostart_arrays(int part)
5575 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
5577 for (i = 0; i < dev_cnt; i++) {
5578 dev_t dev = detected_devices[i];
5580 rdev = md_import_device(dev,0, 0);
5584 if (test_bit(Faulty, &rdev->flags)) {
5588 list_add(&rdev->same_set, &pending_raid_disks);
5592 autorun_devices(part);
5597 static __exit void md_exit(void)
5600 struct list_head *tmp;
5602 blk_unregister_region(MKDEV(MAJOR_NR,0), 1U << MINORBITS);
5603 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
5605 unregister_blkdev(MAJOR_NR,"md");
5606 unregister_blkdev(mdp_major, "mdp");
5607 unregister_reboot_notifier(&md_notifier);
5608 unregister_sysctl_table(raid_table_header);
5609 remove_proc_entry("mdstat", NULL);
5610 ITERATE_MDDEV(mddev,tmp) {
5611 struct gendisk *disk = mddev->gendisk;
5614 export_array(mddev);
5617 mddev->gendisk = NULL;
5622 module_init(md_init)
5623 module_exit(md_exit)
5625 static int get_ro(char *buffer, struct kernel_param *kp)
5627 return sprintf(buffer, "%d", start_readonly);
5629 static int set_ro(const char *val, struct kernel_param *kp)
5632 int num = simple_strtoul(val, &e, 10);
5633 if (*val && (*e == '\0' || *e == '\n')) {
5634 start_readonly = num;
5640 module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
5641 module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
5644 EXPORT_SYMBOL(register_md_personality);
5645 EXPORT_SYMBOL(unregister_md_personality);
5646 EXPORT_SYMBOL(md_error);
5647 EXPORT_SYMBOL(md_done_sync);
5648 EXPORT_SYMBOL(md_write_start);
5649 EXPORT_SYMBOL(md_write_end);
5650 EXPORT_SYMBOL(md_register_thread);
5651 EXPORT_SYMBOL(md_unregister_thread);
5652 EXPORT_SYMBOL(md_wakeup_thread);
5653 EXPORT_SYMBOL(md_check_recovery);
5654 MODULE_LICENSE("GPL");
5656 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);