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/config.h>
37 #include <linux/kthread.h>
38 #include <linux/linkage.h>
39 #include <linux/raid/md.h>
40 #include <linux/raid/bitmap.h>
41 #include <linux/sysctl.h>
42 #include <linux/devfs_fs_kernel.h>
43 #include <linux/buffer_head.h> /* for invalidate_bdev */
44 #include <linux/suspend.h>
45 #include <linux/poll.h>
46 #include <linux/mutex.h>
47 #include <linux/ctype.h>
49 #include <linux/init.h>
51 #include <linux/file.h>
54 #include <linux/kmod.h>
57 #include <asm/unaligned.h>
59 #define MAJOR_NR MD_MAJOR
62 /* 63 partitions with the alternate major number (mdp) */
63 #define MdpMinorShift 6
66 #define dprintk(x...) ((void)(DEBUG && printk(x)))
70 static void autostart_arrays (int part);
73 static LIST_HEAD(pers_list);
74 static DEFINE_SPINLOCK(pers_lock);
76 static void md_print_devices(void);
78 #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
81 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
82 * is 1000 KB/sec, so the extra system load does not show up that much.
83 * Increase it if you want to have more _guaranteed_ speed. Note that
84 * the RAID driver will use the maximum available bandwidth if the IO
85 * subsystem is idle. There is also an 'absolute maximum' reconstruction
86 * speed limit - in case reconstruction slows down your system despite
89 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
90 * or /sys/block/mdX/md/sync_speed_{min,max}
93 static int sysctl_speed_limit_min = 1000;
94 static int sysctl_speed_limit_max = 200000;
95 static inline int speed_min(mddev_t *mddev)
97 return mddev->sync_speed_min ?
98 mddev->sync_speed_min : sysctl_speed_limit_min;
101 static inline int speed_max(mddev_t *mddev)
103 return mddev->sync_speed_max ?
104 mddev->sync_speed_max : sysctl_speed_limit_max;
107 static struct ctl_table_header *raid_table_header;
109 static ctl_table raid_table[] = {
111 .ctl_name = DEV_RAID_SPEED_LIMIT_MIN,
112 .procname = "speed_limit_min",
113 .data = &sysctl_speed_limit_min,
114 .maxlen = sizeof(int),
116 .proc_handler = &proc_dointvec,
119 .ctl_name = DEV_RAID_SPEED_LIMIT_MAX,
120 .procname = "speed_limit_max",
121 .data = &sysctl_speed_limit_max,
122 .maxlen = sizeof(int),
124 .proc_handler = &proc_dointvec,
129 static ctl_table raid_dir_table[] = {
131 .ctl_name = DEV_RAID,
140 static ctl_table raid_root_table[] = {
146 .child = raid_dir_table,
151 static struct block_device_operations md_fops;
153 static int start_readonly;
156 * We have a system wide 'event count' that is incremented
157 * on any 'interesting' event, and readers of /proc/mdstat
158 * can use 'poll' or 'select' to find out when the event
162 * start array, stop array, error, add device, remove device,
163 * start build, activate spare
165 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
166 static atomic_t md_event_count;
167 void md_new_event(mddev_t *mddev)
169 atomic_inc(&md_event_count);
170 wake_up(&md_event_waiters);
171 sysfs_notify(&mddev->kobj, NULL, "sync_action");
173 EXPORT_SYMBOL_GPL(md_new_event);
175 /* Alternate version that can be called from interrupts
176 * when calling sysfs_notify isn't needed.
178 static void md_new_event_inintr(mddev_t *mddev)
180 atomic_inc(&md_event_count);
181 wake_up(&md_event_waiters);
185 * Enables to iterate over all existing md arrays
186 * all_mddevs_lock protects this list.
188 static LIST_HEAD(all_mddevs);
189 static DEFINE_SPINLOCK(all_mddevs_lock);
193 * iterates through all used mddevs in the system.
194 * We take care to grab the all_mddevs_lock whenever navigating
195 * the list, and to always hold a refcount when unlocked.
196 * Any code which breaks out of this loop while own
197 * a reference to the current mddev and must mddev_put it.
199 #define ITERATE_MDDEV(mddev,tmp) \
201 for (({ spin_lock(&all_mddevs_lock); \
202 tmp = all_mddevs.next; \
204 ({ if (tmp != &all_mddevs) \
205 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
206 spin_unlock(&all_mddevs_lock); \
207 if (mddev) mddev_put(mddev); \
208 mddev = list_entry(tmp, mddev_t, all_mddevs); \
209 tmp != &all_mddevs;}); \
210 ({ spin_lock(&all_mddevs_lock); \
215 static int md_fail_request (request_queue_t *q, struct bio *bio)
217 bio_io_error(bio, bio->bi_size);
221 static inline mddev_t *mddev_get(mddev_t *mddev)
223 atomic_inc(&mddev->active);
227 static void mddev_put(mddev_t *mddev)
229 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
231 if (!mddev->raid_disks && list_empty(&mddev->disks)) {
232 list_del(&mddev->all_mddevs);
233 spin_unlock(&all_mddevs_lock);
234 blk_cleanup_queue(mddev->queue);
235 kobject_unregister(&mddev->kobj);
237 spin_unlock(&all_mddevs_lock);
240 static mddev_t * mddev_find(dev_t unit)
242 mddev_t *mddev, *new = NULL;
245 spin_lock(&all_mddevs_lock);
246 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
247 if (mddev->unit == unit) {
249 spin_unlock(&all_mddevs_lock);
255 list_add(&new->all_mddevs, &all_mddevs);
256 spin_unlock(&all_mddevs_lock);
259 spin_unlock(&all_mddevs_lock);
261 new = kzalloc(sizeof(*new), GFP_KERNEL);
266 if (MAJOR(unit) == MD_MAJOR)
267 new->md_minor = MINOR(unit);
269 new->md_minor = MINOR(unit) >> MdpMinorShift;
271 mutex_init(&new->reconfig_mutex);
272 INIT_LIST_HEAD(&new->disks);
273 INIT_LIST_HEAD(&new->all_mddevs);
274 init_timer(&new->safemode_timer);
275 atomic_set(&new->active, 1);
276 spin_lock_init(&new->write_lock);
277 init_waitqueue_head(&new->sb_wait);
279 new->queue = blk_alloc_queue(GFP_KERNEL);
284 set_bit(QUEUE_FLAG_CLUSTER, &new->queue->queue_flags);
286 blk_queue_make_request(new->queue, md_fail_request);
291 static inline int mddev_lock(mddev_t * mddev)
293 return mutex_lock_interruptible(&mddev->reconfig_mutex);
296 static inline int mddev_trylock(mddev_t * mddev)
298 return mutex_trylock(&mddev->reconfig_mutex);
301 static inline void mddev_unlock(mddev_t * mddev)
303 mutex_unlock(&mddev->reconfig_mutex);
305 md_wakeup_thread(mddev->thread);
308 static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
311 struct list_head *tmp;
313 ITERATE_RDEV(mddev,rdev,tmp) {
314 if (rdev->desc_nr == nr)
320 static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
322 struct list_head *tmp;
325 ITERATE_RDEV(mddev,rdev,tmp) {
326 if (rdev->bdev->bd_dev == dev)
332 static struct mdk_personality *find_pers(int level, char *clevel)
334 struct mdk_personality *pers;
335 list_for_each_entry(pers, &pers_list, list) {
336 if (level != LEVEL_NONE && pers->level == level)
338 if (strcmp(pers->name, clevel)==0)
344 static inline sector_t calc_dev_sboffset(struct block_device *bdev)
346 sector_t size = bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
347 return MD_NEW_SIZE_BLOCKS(size);
350 static sector_t calc_dev_size(mdk_rdev_t *rdev, unsigned chunk_size)
354 size = rdev->sb_offset;
357 size &= ~((sector_t)chunk_size/1024 - 1);
361 static int alloc_disk_sb(mdk_rdev_t * rdev)
366 rdev->sb_page = alloc_page(GFP_KERNEL);
367 if (!rdev->sb_page) {
368 printk(KERN_ALERT "md: out of memory.\n");
375 static void free_disk_sb(mdk_rdev_t * rdev)
378 put_page(rdev->sb_page);
380 rdev->sb_page = NULL;
387 static int super_written(struct bio *bio, unsigned int bytes_done, int error)
389 mdk_rdev_t *rdev = bio->bi_private;
390 mddev_t *mddev = rdev->mddev;
394 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags))
395 md_error(mddev, rdev);
397 if (atomic_dec_and_test(&mddev->pending_writes))
398 wake_up(&mddev->sb_wait);
403 static int super_written_barrier(struct bio *bio, unsigned int bytes_done, int error)
405 struct bio *bio2 = bio->bi_private;
406 mdk_rdev_t *rdev = bio2->bi_private;
407 mddev_t *mddev = rdev->mddev;
411 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
412 error == -EOPNOTSUPP) {
414 /* barriers don't appear to be supported :-( */
415 set_bit(BarriersNotsupp, &rdev->flags);
416 mddev->barriers_work = 0;
417 spin_lock_irqsave(&mddev->write_lock, flags);
418 bio2->bi_next = mddev->biolist;
419 mddev->biolist = bio2;
420 spin_unlock_irqrestore(&mddev->write_lock, flags);
421 wake_up(&mddev->sb_wait);
426 bio->bi_private = rdev;
427 return super_written(bio, bytes_done, error);
430 void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
431 sector_t sector, int size, struct page *page)
433 /* write first size bytes of page to sector of rdev
434 * Increment mddev->pending_writes before returning
435 * and decrement it on completion, waking up sb_wait
436 * if zero is reached.
437 * If an error occurred, call md_error
439 * As we might need to resubmit the request if BIO_RW_BARRIER
440 * causes ENOTSUPP, we allocate a spare bio...
442 struct bio *bio = bio_alloc(GFP_NOIO, 1);
443 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNC);
445 bio->bi_bdev = rdev->bdev;
446 bio->bi_sector = sector;
447 bio_add_page(bio, page, size, 0);
448 bio->bi_private = rdev;
449 bio->bi_end_io = super_written;
452 atomic_inc(&mddev->pending_writes);
453 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
455 rw |= (1<<BIO_RW_BARRIER);
456 rbio = bio_clone(bio, GFP_NOIO);
457 rbio->bi_private = bio;
458 rbio->bi_end_io = super_written_barrier;
459 submit_bio(rw, rbio);
464 void md_super_wait(mddev_t *mddev)
466 /* wait for all superblock writes that were scheduled to complete.
467 * if any had to be retried (due to BARRIER problems), retry them
471 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
472 if (atomic_read(&mddev->pending_writes)==0)
474 while (mddev->biolist) {
476 spin_lock_irq(&mddev->write_lock);
477 bio = mddev->biolist;
478 mddev->biolist = bio->bi_next ;
480 spin_unlock_irq(&mddev->write_lock);
481 submit_bio(bio->bi_rw, bio);
485 finish_wait(&mddev->sb_wait, &wq);
488 static int bi_complete(struct bio *bio, unsigned int bytes_done, int error)
493 complete((struct completion*)bio->bi_private);
497 int sync_page_io(struct block_device *bdev, sector_t sector, int size,
498 struct page *page, int rw)
500 struct bio *bio = bio_alloc(GFP_NOIO, 1);
501 struct completion event;
504 rw |= (1 << BIO_RW_SYNC);
507 bio->bi_sector = sector;
508 bio_add_page(bio, page, size, 0);
509 init_completion(&event);
510 bio->bi_private = &event;
511 bio->bi_end_io = bi_complete;
513 wait_for_completion(&event);
515 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
519 EXPORT_SYMBOL_GPL(sync_page_io);
521 static int read_disk_sb(mdk_rdev_t * rdev, int size)
523 char b[BDEVNAME_SIZE];
524 if (!rdev->sb_page) {
532 if (!sync_page_io(rdev->bdev, rdev->sb_offset<<1, size, rdev->sb_page, READ))
538 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
539 bdevname(rdev->bdev,b));
543 static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
545 if ( (sb1->set_uuid0 == sb2->set_uuid0) &&
546 (sb1->set_uuid1 == sb2->set_uuid1) &&
547 (sb1->set_uuid2 == sb2->set_uuid2) &&
548 (sb1->set_uuid3 == sb2->set_uuid3))
556 static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
559 mdp_super_t *tmp1, *tmp2;
561 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
562 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
564 if (!tmp1 || !tmp2) {
566 printk(KERN_INFO "md.c: sb1 is not equal to sb2!\n");
574 * nr_disks is not constant
579 if (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4))
590 static unsigned int calc_sb_csum(mdp_super_t * sb)
592 unsigned int disk_csum, csum;
594 disk_csum = sb->sb_csum;
596 csum = csum_partial((void *)sb, MD_SB_BYTES, 0);
597 sb->sb_csum = disk_csum;
603 * Handle superblock details.
604 * We want to be able to handle multiple superblock formats
605 * so we have a common interface to them all, and an array of
606 * different handlers.
607 * We rely on user-space to write the initial superblock, and support
608 * reading and updating of superblocks.
609 * Interface methods are:
610 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
611 * loads and validates a superblock on dev.
612 * if refdev != NULL, compare superblocks on both devices
614 * 0 - dev has a superblock that is compatible with refdev
615 * 1 - dev has a superblock that is compatible and newer than refdev
616 * so dev should be used as the refdev in future
617 * -EINVAL superblock incompatible or invalid
618 * -othererror e.g. -EIO
620 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
621 * Verify that dev is acceptable into mddev.
622 * The first time, mddev->raid_disks will be 0, and data from
623 * dev should be merged in. Subsequent calls check that dev
624 * is new enough. Return 0 or -EINVAL
626 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
627 * Update the superblock for rdev with data in mddev
628 * This does not write to disc.
634 struct module *owner;
635 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version);
636 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
637 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
641 * load_super for 0.90.0
643 static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
645 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
651 * Calculate the position of the superblock,
652 * it's at the end of the disk.
654 * It also happens to be a multiple of 4Kb.
656 sb_offset = calc_dev_sboffset(rdev->bdev);
657 rdev->sb_offset = sb_offset;
659 ret = read_disk_sb(rdev, MD_SB_BYTES);
664 bdevname(rdev->bdev, b);
665 sb = (mdp_super_t*)page_address(rdev->sb_page);
667 if (sb->md_magic != MD_SB_MAGIC) {
668 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
673 if (sb->major_version != 0 ||
674 sb->minor_version < 90 ||
675 sb->minor_version > 91) {
676 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
677 sb->major_version, sb->minor_version,
682 if (sb->raid_disks <= 0)
685 if (csum_fold(calc_sb_csum(sb)) != csum_fold(sb->sb_csum)) {
686 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
691 rdev->preferred_minor = sb->md_minor;
692 rdev->data_offset = 0;
693 rdev->sb_size = MD_SB_BYTES;
695 if (sb->level == LEVEL_MULTIPATH)
698 rdev->desc_nr = sb->this_disk.number;
704 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
705 if (!uuid_equal(refsb, sb)) {
706 printk(KERN_WARNING "md: %s has different UUID to %s\n",
707 b, bdevname(refdev->bdev,b2));
710 if (!sb_equal(refsb, sb)) {
711 printk(KERN_WARNING "md: %s has same UUID"
712 " but different superblock to %s\n",
713 b, bdevname(refdev->bdev, b2));
717 ev2 = md_event(refsb);
723 rdev->size = calc_dev_size(rdev, sb->chunk_size);
725 if (rdev->size < sb->size && sb->level > 1)
726 /* "this cannot possibly happen" ... */
734 * validate_super for 0.90.0
736 static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
739 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
740 __u64 ev1 = md_event(sb);
742 rdev->raid_disk = -1;
744 if (mddev->raid_disks == 0) {
745 mddev->major_version = 0;
746 mddev->minor_version = sb->minor_version;
747 mddev->patch_version = sb->patch_version;
748 mddev->persistent = ! sb->not_persistent;
749 mddev->chunk_size = sb->chunk_size;
750 mddev->ctime = sb->ctime;
751 mddev->utime = sb->utime;
752 mddev->level = sb->level;
753 mddev->clevel[0] = 0;
754 mddev->layout = sb->layout;
755 mddev->raid_disks = sb->raid_disks;
756 mddev->size = sb->size;
758 mddev->bitmap_offset = 0;
759 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
761 if (mddev->minor_version >= 91) {
762 mddev->reshape_position = sb->reshape_position;
763 mddev->delta_disks = sb->delta_disks;
764 mddev->new_level = sb->new_level;
765 mddev->new_layout = sb->new_layout;
766 mddev->new_chunk = sb->new_chunk;
768 mddev->reshape_position = MaxSector;
769 mddev->delta_disks = 0;
770 mddev->new_level = mddev->level;
771 mddev->new_layout = mddev->layout;
772 mddev->new_chunk = mddev->chunk_size;
775 if (sb->state & (1<<MD_SB_CLEAN))
776 mddev->recovery_cp = MaxSector;
778 if (sb->events_hi == sb->cp_events_hi &&
779 sb->events_lo == sb->cp_events_lo) {
780 mddev->recovery_cp = sb->recovery_cp;
782 mddev->recovery_cp = 0;
785 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
786 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
787 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
788 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
790 mddev->max_disks = MD_SB_DISKS;
792 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
793 mddev->bitmap_file == NULL) {
794 if (mddev->level != 1 && mddev->level != 4
795 && mddev->level != 5 && mddev->level != 6
796 && mddev->level != 10) {
797 /* FIXME use a better test */
798 printk(KERN_WARNING "md: bitmaps not supported for this level.\n");
801 mddev->bitmap_offset = mddev->default_bitmap_offset;
804 } else if (mddev->pers == NULL) {
805 /* Insist on good event counter while assembling */
807 if (ev1 < mddev->events)
809 } else if (mddev->bitmap) {
810 /* if adding to array with a bitmap, then we can accept an
811 * older device ... but not too old.
813 if (ev1 < mddev->bitmap->events_cleared)
816 if (ev1 < mddev->events)
817 /* just a hot-add of a new device, leave raid_disk at -1 */
821 if (mddev->level != LEVEL_MULTIPATH) {
822 desc = sb->disks + rdev->desc_nr;
824 if (desc->state & (1<<MD_DISK_FAULTY))
825 set_bit(Faulty, &rdev->flags);
826 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
827 desc->raid_disk < mddev->raid_disks */) {
828 set_bit(In_sync, &rdev->flags);
829 rdev->raid_disk = desc->raid_disk;
831 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
832 set_bit(WriteMostly, &rdev->flags);
833 } else /* MULTIPATH are always insync */
834 set_bit(In_sync, &rdev->flags);
839 * sync_super for 0.90.0
841 static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
844 struct list_head *tmp;
846 int next_spare = mddev->raid_disks;
849 /* make rdev->sb match mddev data..
852 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
853 * 3/ any empty disks < next_spare become removed
855 * disks[0] gets initialised to REMOVED because
856 * we cannot be sure from other fields if it has
857 * been initialised or not.
860 int active=0, working=0,failed=0,spare=0,nr_disks=0;
862 rdev->sb_size = MD_SB_BYTES;
864 sb = (mdp_super_t*)page_address(rdev->sb_page);
866 memset(sb, 0, sizeof(*sb));
868 sb->md_magic = MD_SB_MAGIC;
869 sb->major_version = mddev->major_version;
870 sb->patch_version = mddev->patch_version;
871 sb->gvalid_words = 0; /* ignored */
872 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
873 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
874 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
875 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
877 sb->ctime = mddev->ctime;
878 sb->level = mddev->level;
879 sb->size = mddev->size;
880 sb->raid_disks = mddev->raid_disks;
881 sb->md_minor = mddev->md_minor;
882 sb->not_persistent = !mddev->persistent;
883 sb->utime = mddev->utime;
885 sb->events_hi = (mddev->events>>32);
886 sb->events_lo = (u32)mddev->events;
888 if (mddev->reshape_position == MaxSector)
889 sb->minor_version = 90;
891 sb->minor_version = 91;
892 sb->reshape_position = mddev->reshape_position;
893 sb->new_level = mddev->new_level;
894 sb->delta_disks = mddev->delta_disks;
895 sb->new_layout = mddev->new_layout;
896 sb->new_chunk = mddev->new_chunk;
898 mddev->minor_version = sb->minor_version;
901 sb->recovery_cp = mddev->recovery_cp;
902 sb->cp_events_hi = (mddev->events>>32);
903 sb->cp_events_lo = (u32)mddev->events;
904 if (mddev->recovery_cp == MaxSector)
905 sb->state = (1<< MD_SB_CLEAN);
909 sb->layout = mddev->layout;
910 sb->chunk_size = mddev->chunk_size;
912 if (mddev->bitmap && mddev->bitmap_file == NULL)
913 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
915 sb->disks[0].state = (1<<MD_DISK_REMOVED);
916 ITERATE_RDEV(mddev,rdev2,tmp) {
919 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
920 && !test_bit(Faulty, &rdev2->flags))
921 desc_nr = rdev2->raid_disk;
923 desc_nr = next_spare++;
924 rdev2->desc_nr = desc_nr;
925 d = &sb->disks[rdev2->desc_nr];
927 d->number = rdev2->desc_nr;
928 d->major = MAJOR(rdev2->bdev->bd_dev);
929 d->minor = MINOR(rdev2->bdev->bd_dev);
930 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
931 && !test_bit(Faulty, &rdev2->flags))
932 d->raid_disk = rdev2->raid_disk;
934 d->raid_disk = rdev2->desc_nr; /* compatibility */
935 if (test_bit(Faulty, &rdev2->flags))
936 d->state = (1<<MD_DISK_FAULTY);
937 else if (test_bit(In_sync, &rdev2->flags)) {
938 d->state = (1<<MD_DISK_ACTIVE);
939 d->state |= (1<<MD_DISK_SYNC);
947 if (test_bit(WriteMostly, &rdev2->flags))
948 d->state |= (1<<MD_DISK_WRITEMOSTLY);
950 /* now set the "removed" and "faulty" bits on any missing devices */
951 for (i=0 ; i < mddev->raid_disks ; i++) {
952 mdp_disk_t *d = &sb->disks[i];
953 if (d->state == 0 && d->number == 0) {
956 d->state = (1<<MD_DISK_REMOVED);
957 d->state |= (1<<MD_DISK_FAULTY);
961 sb->nr_disks = nr_disks;
962 sb->active_disks = active;
963 sb->working_disks = working;
964 sb->failed_disks = failed;
965 sb->spare_disks = spare;
967 sb->this_disk = sb->disks[rdev->desc_nr];
968 sb->sb_csum = calc_sb_csum(sb);
972 * version 1 superblock
975 static unsigned int calc_sb_1_csum(struct mdp_superblock_1 * sb)
977 unsigned int disk_csum, csum;
978 unsigned long long newcsum;
979 int size = 256 + le32_to_cpu(sb->max_dev)*2;
980 unsigned int *isuper = (unsigned int*)sb;
983 disk_csum = sb->sb_csum;
986 for (i=0; size>=4; size -= 4 )
987 newcsum += le32_to_cpu(*isuper++);
990 newcsum += le16_to_cpu(*(unsigned short*) isuper);
992 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
993 sb->sb_csum = disk_csum;
994 return cpu_to_le32(csum);
997 static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
999 struct mdp_superblock_1 *sb;
1002 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1006 * Calculate the position of the superblock.
1007 * It is always aligned to a 4K boundary and
1008 * depeding on minor_version, it can be:
1009 * 0: At least 8K, but less than 12K, from end of device
1010 * 1: At start of device
1011 * 2: 4K from start of device.
1013 switch(minor_version) {
1015 sb_offset = rdev->bdev->bd_inode->i_size >> 9;
1017 sb_offset &= ~(sector_t)(4*2-1);
1018 /* convert from sectors to K */
1030 rdev->sb_offset = sb_offset;
1032 /* superblock is rarely larger than 1K, but it can be larger,
1033 * and it is safe to read 4k, so we do that
1035 ret = read_disk_sb(rdev, 4096);
1036 if (ret) return ret;
1039 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1041 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1042 sb->major_version != cpu_to_le32(1) ||
1043 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1044 le64_to_cpu(sb->super_offset) != (rdev->sb_offset<<1) ||
1045 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
1048 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1049 printk("md: invalid superblock checksum on %s\n",
1050 bdevname(rdev->bdev,b));
1053 if (le64_to_cpu(sb->data_size) < 10) {
1054 printk("md: data_size too small on %s\n",
1055 bdevname(rdev->bdev,b));
1058 rdev->preferred_minor = 0xffff;
1059 rdev->data_offset = le64_to_cpu(sb->data_offset);
1060 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
1062 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
1063 bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
1064 if (rdev->sb_size & bmask)
1065 rdev-> sb_size = (rdev->sb_size | bmask)+1;
1071 struct mdp_superblock_1 *refsb =
1072 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1074 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1075 sb->level != refsb->level ||
1076 sb->layout != refsb->layout ||
1077 sb->chunksize != refsb->chunksize) {
1078 printk(KERN_WARNING "md: %s has strangely different"
1079 " superblock to %s\n",
1080 bdevname(rdev->bdev,b),
1081 bdevname(refdev->bdev,b2));
1084 ev1 = le64_to_cpu(sb->events);
1085 ev2 = le64_to_cpu(refsb->events);
1093 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
1095 rdev->size = rdev->sb_offset;
1096 if (rdev->size < le64_to_cpu(sb->data_size)/2)
1098 rdev->size = le64_to_cpu(sb->data_size)/2;
1099 if (le32_to_cpu(sb->chunksize))
1100 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
1102 if (le32_to_cpu(sb->size) > rdev->size*2)
1107 static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1109 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1110 __u64 ev1 = le64_to_cpu(sb->events);
1112 rdev->raid_disk = -1;
1114 if (mddev->raid_disks == 0) {
1115 mddev->major_version = 1;
1116 mddev->patch_version = 0;
1117 mddev->persistent = 1;
1118 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
1119 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1120 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1121 mddev->level = le32_to_cpu(sb->level);
1122 mddev->clevel[0] = 0;
1123 mddev->layout = le32_to_cpu(sb->layout);
1124 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1125 mddev->size = le64_to_cpu(sb->size)/2;
1126 mddev->events = ev1;
1127 mddev->bitmap_offset = 0;
1128 mddev->default_bitmap_offset = 1024 >> 9;
1130 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1131 memcpy(mddev->uuid, sb->set_uuid, 16);
1133 mddev->max_disks = (4096-256)/2;
1135 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
1136 mddev->bitmap_file == NULL ) {
1137 if (mddev->level != 1 && mddev->level != 5 && mddev->level != 6
1138 && mddev->level != 10) {
1139 printk(KERN_WARNING "md: bitmaps not supported for this level.\n");
1142 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
1144 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1145 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1146 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1147 mddev->new_level = le32_to_cpu(sb->new_level);
1148 mddev->new_layout = le32_to_cpu(sb->new_layout);
1149 mddev->new_chunk = le32_to_cpu(sb->new_chunk)<<9;
1151 mddev->reshape_position = MaxSector;
1152 mddev->delta_disks = 0;
1153 mddev->new_level = mddev->level;
1154 mddev->new_layout = mddev->layout;
1155 mddev->new_chunk = mddev->chunk_size;
1158 } else if (mddev->pers == NULL) {
1159 /* Insist of good event counter while assembling */
1161 if (ev1 < mddev->events)
1163 } else if (mddev->bitmap) {
1164 /* If adding to array with a bitmap, then we can accept an
1165 * older device, but not too old.
1167 if (ev1 < mddev->bitmap->events_cleared)
1170 if (ev1 < mddev->events)
1171 /* just a hot-add of a new device, leave raid_disk at -1 */
1174 if (mddev->level != LEVEL_MULTIPATH) {
1176 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1177 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1179 case 0xffff: /* spare */
1181 case 0xfffe: /* faulty */
1182 set_bit(Faulty, &rdev->flags);
1185 if ((le32_to_cpu(sb->feature_map) &
1186 MD_FEATURE_RECOVERY_OFFSET))
1187 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1189 set_bit(In_sync, &rdev->flags);
1190 rdev->raid_disk = role;
1193 if (sb->devflags & WriteMostly1)
1194 set_bit(WriteMostly, &rdev->flags);
1195 } else /* MULTIPATH are always insync */
1196 set_bit(In_sync, &rdev->flags);
1201 static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1203 struct mdp_superblock_1 *sb;
1204 struct list_head *tmp;
1207 /* make rdev->sb match mddev and rdev data. */
1209 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1211 sb->feature_map = 0;
1213 sb->recovery_offset = cpu_to_le64(0);
1214 memset(sb->pad1, 0, sizeof(sb->pad1));
1215 memset(sb->pad2, 0, sizeof(sb->pad2));
1216 memset(sb->pad3, 0, sizeof(sb->pad3));
1218 sb->utime = cpu_to_le64((__u64)mddev->utime);
1219 sb->events = cpu_to_le64(mddev->events);
1221 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1223 sb->resync_offset = cpu_to_le64(0);
1225 sb->cnt_corrected_read = atomic_read(&rdev->corrected_errors);
1227 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
1228 sb->size = cpu_to_le64(mddev->size<<1);
1230 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1231 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
1232 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
1235 if (rdev->raid_disk >= 0 &&
1236 !test_bit(In_sync, &rdev->flags) &&
1237 rdev->recovery_offset > 0) {
1238 sb->feature_map |= cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1239 sb->recovery_offset = cpu_to_le64(rdev->recovery_offset);
1242 if (mddev->reshape_position != MaxSector) {
1243 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1244 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1245 sb->new_layout = cpu_to_le32(mddev->new_layout);
1246 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1247 sb->new_level = cpu_to_le32(mddev->new_level);
1248 sb->new_chunk = cpu_to_le32(mddev->new_chunk>>9);
1252 ITERATE_RDEV(mddev,rdev2,tmp)
1253 if (rdev2->desc_nr+1 > max_dev)
1254 max_dev = rdev2->desc_nr+1;
1256 sb->max_dev = cpu_to_le32(max_dev);
1257 for (i=0; i<max_dev;i++)
1258 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1260 ITERATE_RDEV(mddev,rdev2,tmp) {
1262 if (test_bit(Faulty, &rdev2->flags))
1263 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1264 else if (test_bit(In_sync, &rdev2->flags))
1265 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1266 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1267 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1269 sb->dev_roles[i] = cpu_to_le16(0xffff);
1272 sb->sb_csum = calc_sb_1_csum(sb);
1276 static struct super_type super_types[] = {
1279 .owner = THIS_MODULE,
1280 .load_super = super_90_load,
1281 .validate_super = super_90_validate,
1282 .sync_super = super_90_sync,
1286 .owner = THIS_MODULE,
1287 .load_super = super_1_load,
1288 .validate_super = super_1_validate,
1289 .sync_super = super_1_sync,
1293 static mdk_rdev_t * match_dev_unit(mddev_t *mddev, mdk_rdev_t *dev)
1295 struct list_head *tmp;
1298 ITERATE_RDEV(mddev,rdev,tmp)
1299 if (rdev->bdev->bd_contains == dev->bdev->bd_contains)
1305 static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1307 struct list_head *tmp;
1310 ITERATE_RDEV(mddev1,rdev,tmp)
1311 if (match_dev_unit(mddev2, rdev))
1317 static LIST_HEAD(pending_raid_disks);
1319 static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1321 mdk_rdev_t *same_pdev;
1322 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1330 /* make sure rdev->size exceeds mddev->size */
1331 if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) {
1333 /* Cannot change size, so fail */
1336 mddev->size = rdev->size;
1338 same_pdev = match_dev_unit(mddev, rdev);
1341 "%s: WARNING: %s appears to be on the same physical"
1342 " disk as %s. True\n protection against single-disk"
1343 " failure might be compromised.\n",
1344 mdname(mddev), bdevname(rdev->bdev,b),
1345 bdevname(same_pdev->bdev,b2));
1347 /* Verify rdev->desc_nr is unique.
1348 * If it is -1, assign a free number, else
1349 * check number is not in use
1351 if (rdev->desc_nr < 0) {
1353 if (mddev->pers) choice = mddev->raid_disks;
1354 while (find_rdev_nr(mddev, choice))
1356 rdev->desc_nr = choice;
1358 if (find_rdev_nr(mddev, rdev->desc_nr))
1361 bdevname(rdev->bdev,b);
1362 if (kobject_set_name(&rdev->kobj, "dev-%s", b) < 0)
1364 while ( (s=strchr(rdev->kobj.k_name, '/')) != NULL)
1367 list_add(&rdev->same_set, &mddev->disks);
1368 rdev->mddev = mddev;
1369 printk(KERN_INFO "md: bind<%s>\n", b);
1371 rdev->kobj.parent = &mddev->kobj;
1372 kobject_add(&rdev->kobj);
1374 if (rdev->bdev->bd_part)
1375 ko = &rdev->bdev->bd_part->kobj;
1377 ko = &rdev->bdev->bd_disk->kobj;
1378 sysfs_create_link(&rdev->kobj, ko, "block");
1379 bd_claim_by_disk(rdev->bdev, rdev, mddev->gendisk);
1383 static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1385 char b[BDEVNAME_SIZE];
1390 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
1391 list_del_init(&rdev->same_set);
1392 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1394 sysfs_remove_link(&rdev->kobj, "block");
1395 kobject_del(&rdev->kobj);
1399 * prevent the device from being mounted, repartitioned or
1400 * otherwise reused by a RAID array (or any other kernel
1401 * subsystem), by bd_claiming the device.
1403 static int lock_rdev(mdk_rdev_t *rdev, dev_t dev)
1406 struct block_device *bdev;
1407 char b[BDEVNAME_SIZE];
1409 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
1411 printk(KERN_ERR "md: could not open %s.\n",
1412 __bdevname(dev, b));
1413 return PTR_ERR(bdev);
1415 err = bd_claim(bdev, rdev);
1417 printk(KERN_ERR "md: could not bd_claim %s.\n",
1426 static void unlock_rdev(mdk_rdev_t *rdev)
1428 struct block_device *bdev = rdev->bdev;
1436 void md_autodetect_dev(dev_t dev);
1438 static void export_rdev(mdk_rdev_t * rdev)
1440 char b[BDEVNAME_SIZE];
1441 printk(KERN_INFO "md: export_rdev(%s)\n",
1442 bdevname(rdev->bdev,b));
1446 list_del_init(&rdev->same_set);
1448 md_autodetect_dev(rdev->bdev->bd_dev);
1451 kobject_put(&rdev->kobj);
1454 static void kick_rdev_from_array(mdk_rdev_t * rdev)
1456 unbind_rdev_from_array(rdev);
1460 static void export_array(mddev_t *mddev)
1462 struct list_head *tmp;
1465 ITERATE_RDEV(mddev,rdev,tmp) {
1470 kick_rdev_from_array(rdev);
1472 if (!list_empty(&mddev->disks))
1474 mddev->raid_disks = 0;
1475 mddev->major_version = 0;
1478 static void print_desc(mdp_disk_t *desc)
1480 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1481 desc->major,desc->minor,desc->raid_disk,desc->state);
1484 static void print_sb(mdp_super_t *sb)
1489 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1490 sb->major_version, sb->minor_version, sb->patch_version,
1491 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1493 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1494 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1495 sb->md_minor, sb->layout, sb->chunk_size);
1496 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1497 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1498 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1499 sb->failed_disks, sb->spare_disks,
1500 sb->sb_csum, (unsigned long)sb->events_lo);
1503 for (i = 0; i < MD_SB_DISKS; i++) {
1506 desc = sb->disks + i;
1507 if (desc->number || desc->major || desc->minor ||
1508 desc->raid_disk || (desc->state && (desc->state != 4))) {
1509 printk(" D %2d: ", i);
1513 printk(KERN_INFO "md: THIS: ");
1514 print_desc(&sb->this_disk);
1518 static void print_rdev(mdk_rdev_t *rdev)
1520 char b[BDEVNAME_SIZE];
1521 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1522 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
1523 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1525 if (rdev->sb_loaded) {
1526 printk(KERN_INFO "md: rdev superblock:\n");
1527 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1529 printk(KERN_INFO "md: no rdev superblock!\n");
1532 static void md_print_devices(void)
1534 struct list_head *tmp, *tmp2;
1537 char b[BDEVNAME_SIZE];
1540 printk("md: **********************************\n");
1541 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1542 printk("md: **********************************\n");
1543 ITERATE_MDDEV(mddev,tmp) {
1546 bitmap_print_sb(mddev->bitmap);
1548 printk("%s: ", mdname(mddev));
1549 ITERATE_RDEV(mddev,rdev,tmp2)
1550 printk("<%s>", bdevname(rdev->bdev,b));
1553 ITERATE_RDEV(mddev,rdev,tmp2)
1556 printk("md: **********************************\n");
1561 static void sync_sbs(mddev_t * mddev, int nospares)
1563 /* Update each superblock (in-memory image), but
1564 * if we are allowed to, skip spares which already
1565 * have the right event counter, or have one earlier
1566 * (which would mean they aren't being marked as dirty
1567 * with the rest of the array)
1570 struct list_head *tmp;
1572 ITERATE_RDEV(mddev,rdev,tmp) {
1573 if (rdev->sb_events == mddev->events ||
1575 rdev->raid_disk < 0 &&
1576 (rdev->sb_events&1)==0 &&
1577 rdev->sb_events+1 == mddev->events)) {
1578 /* Don't update this superblock */
1579 rdev->sb_loaded = 2;
1581 super_types[mddev->major_version].
1582 sync_super(mddev, rdev);
1583 rdev->sb_loaded = 1;
1588 void md_update_sb(mddev_t * mddev)
1591 struct list_head *tmp;
1597 spin_lock_irq(&mddev->write_lock);
1598 sync_req = mddev->in_sync;
1599 mddev->utime = get_seconds();
1600 if (mddev->sb_dirty == 3)
1601 /* just a clean<-> dirty transition, possibly leave spares alone,
1602 * though if events isn't the right even/odd, we will have to do
1607 /* If this is just a dirty<->clean transition, and the array is clean
1608 * and 'events' is odd, we can roll back to the previous clean state */
1609 if (mddev->sb_dirty == 3
1610 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
1611 && (mddev->events & 1))
1614 /* otherwise we have to go forward and ... */
1616 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
1617 /* .. if the array isn't clean, insist on an odd 'events' */
1618 if ((mddev->events&1)==0) {
1623 /* otherwise insist on an even 'events' (for clean states) */
1624 if ((mddev->events&1)) {
1631 if (!mddev->events) {
1633 * oops, this 64-bit counter should never wrap.
1634 * Either we are in around ~1 trillion A.C., assuming
1635 * 1 reboot per second, or we have a bug:
1640 mddev->sb_dirty = 2;
1641 sync_sbs(mddev, nospares);
1644 * do not write anything to disk if using
1645 * nonpersistent superblocks
1647 if (!mddev->persistent) {
1648 mddev->sb_dirty = 0;
1649 spin_unlock_irq(&mddev->write_lock);
1650 wake_up(&mddev->sb_wait);
1653 spin_unlock_irq(&mddev->write_lock);
1656 "md: updating %s RAID superblock on device (in sync %d)\n",
1657 mdname(mddev),mddev->in_sync);
1659 err = bitmap_update_sb(mddev->bitmap);
1660 ITERATE_RDEV(mddev,rdev,tmp) {
1661 char b[BDEVNAME_SIZE];
1662 dprintk(KERN_INFO "md: ");
1663 if (rdev->sb_loaded != 1)
1664 continue; /* no noise on spare devices */
1665 if (test_bit(Faulty, &rdev->flags))
1666 dprintk("(skipping faulty ");
1668 dprintk("%s ", bdevname(rdev->bdev,b));
1669 if (!test_bit(Faulty, &rdev->flags)) {
1670 md_super_write(mddev,rdev,
1671 rdev->sb_offset<<1, rdev->sb_size,
1673 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1674 bdevname(rdev->bdev,b),
1675 (unsigned long long)rdev->sb_offset);
1676 rdev->sb_events = mddev->events;
1680 if (mddev->level == LEVEL_MULTIPATH)
1681 /* only need to write one superblock... */
1684 md_super_wait(mddev);
1685 /* if there was a failure, sb_dirty was set to 1, and we re-write super */
1687 spin_lock_irq(&mddev->write_lock);
1688 if (mddev->in_sync != sync_req|| mddev->sb_dirty == 1) {
1689 /* have to write it out again */
1690 spin_unlock_irq(&mddev->write_lock);
1693 mddev->sb_dirty = 0;
1694 spin_unlock_irq(&mddev->write_lock);
1695 wake_up(&mddev->sb_wait);
1698 EXPORT_SYMBOL_GPL(md_update_sb);
1700 /* words written to sysfs files may, or my not, be \n terminated.
1701 * We want to accept with case. For this we use cmd_match.
1703 static int cmd_match(const char *cmd, const char *str)
1705 /* See if cmd, written into a sysfs file, matches
1706 * str. They must either be the same, or cmd can
1707 * have a trailing newline
1709 while (*cmd && *str && *cmd == *str) {
1720 struct rdev_sysfs_entry {
1721 struct attribute attr;
1722 ssize_t (*show)(mdk_rdev_t *, char *);
1723 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1727 state_show(mdk_rdev_t *rdev, char *page)
1732 if (test_bit(Faulty, &rdev->flags)) {
1733 len+= sprintf(page+len, "%sfaulty",sep);
1736 if (test_bit(In_sync, &rdev->flags)) {
1737 len += sprintf(page+len, "%sin_sync",sep);
1740 if (test_bit(WriteMostly, &rdev->flags)) {
1741 len += sprintf(page+len, "%swrite_mostly",sep);
1744 if (!test_bit(Faulty, &rdev->flags) &&
1745 !test_bit(In_sync, &rdev->flags)) {
1746 len += sprintf(page+len, "%sspare", sep);
1749 return len+sprintf(page+len, "\n");
1753 state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1756 * faulty - simulates and error
1757 * remove - disconnects the device
1758 * writemostly - sets write_mostly
1759 * -writemostly - clears write_mostly
1762 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
1763 md_error(rdev->mddev, rdev);
1765 } else if (cmd_match(buf, "remove")) {
1766 if (rdev->raid_disk >= 0)
1769 mddev_t *mddev = rdev->mddev;
1770 kick_rdev_from_array(rdev);
1771 md_update_sb(mddev);
1772 md_new_event(mddev);
1775 } else if (cmd_match(buf, "writemostly")) {
1776 set_bit(WriteMostly, &rdev->flags);
1778 } else if (cmd_match(buf, "-writemostly")) {
1779 clear_bit(WriteMostly, &rdev->flags);
1782 return err ? err : len;
1784 static struct rdev_sysfs_entry
1785 rdev_state = __ATTR(state, 0644, state_show, state_store);
1788 super_show(mdk_rdev_t *rdev, char *page)
1790 if (rdev->sb_loaded && rdev->sb_size) {
1791 memcpy(page, page_address(rdev->sb_page), rdev->sb_size);
1792 return rdev->sb_size;
1796 static struct rdev_sysfs_entry rdev_super = __ATTR_RO(super);
1799 errors_show(mdk_rdev_t *rdev, char *page)
1801 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
1805 errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1808 unsigned long n = simple_strtoul(buf, &e, 10);
1809 if (*buf && (*e == 0 || *e == '\n')) {
1810 atomic_set(&rdev->corrected_errors, n);
1815 static struct rdev_sysfs_entry rdev_errors =
1816 __ATTR(errors, 0644, errors_show, errors_store);
1819 slot_show(mdk_rdev_t *rdev, char *page)
1821 if (rdev->raid_disk < 0)
1822 return sprintf(page, "none\n");
1824 return sprintf(page, "%d\n", rdev->raid_disk);
1828 slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1831 int slot = simple_strtoul(buf, &e, 10);
1832 if (strncmp(buf, "none", 4)==0)
1834 else if (e==buf || (*e && *e!= '\n'))
1836 if (rdev->mddev->pers)
1837 /* Cannot set slot in active array (yet) */
1839 if (slot >= rdev->mddev->raid_disks)
1841 rdev->raid_disk = slot;
1842 /* assume it is working */
1844 set_bit(In_sync, &rdev->flags);
1849 static struct rdev_sysfs_entry rdev_slot =
1850 __ATTR(slot, 0644, slot_show, slot_store);
1853 offset_show(mdk_rdev_t *rdev, char *page)
1855 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
1859 offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1862 unsigned long long offset = simple_strtoull(buf, &e, 10);
1863 if (e==buf || (*e && *e != '\n'))
1865 if (rdev->mddev->pers)
1867 rdev->data_offset = offset;
1871 static struct rdev_sysfs_entry rdev_offset =
1872 __ATTR(offset, 0644, offset_show, offset_store);
1875 rdev_size_show(mdk_rdev_t *rdev, char *page)
1877 return sprintf(page, "%llu\n", (unsigned long long)rdev->size);
1881 rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1884 unsigned long long size = simple_strtoull(buf, &e, 10);
1885 if (e==buf || (*e && *e != '\n'))
1887 if (rdev->mddev->pers)
1890 if (size < rdev->mddev->size || rdev->mddev->size == 0)
1891 rdev->mddev->size = size;
1895 static struct rdev_sysfs_entry rdev_size =
1896 __ATTR(size, 0644, rdev_size_show, rdev_size_store);
1898 static struct attribute *rdev_default_attrs[] = {
1908 rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
1910 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1911 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1915 return entry->show(rdev, page);
1919 rdev_attr_store(struct kobject *kobj, struct attribute *attr,
1920 const char *page, size_t length)
1922 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1923 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1927 return entry->store(rdev, page, length);
1930 static void rdev_free(struct kobject *ko)
1932 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
1935 static struct sysfs_ops rdev_sysfs_ops = {
1936 .show = rdev_attr_show,
1937 .store = rdev_attr_store,
1939 static struct kobj_type rdev_ktype = {
1940 .release = rdev_free,
1941 .sysfs_ops = &rdev_sysfs_ops,
1942 .default_attrs = rdev_default_attrs,
1946 * Import a device. If 'super_format' >= 0, then sanity check the superblock
1948 * mark the device faulty if:
1950 * - the device is nonexistent (zero size)
1951 * - the device has no valid superblock
1953 * a faulty rdev _never_ has rdev->sb set.
1955 static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
1957 char b[BDEVNAME_SIZE];
1962 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
1964 printk(KERN_ERR "md: could not alloc mem for new device!\n");
1965 return ERR_PTR(-ENOMEM);
1968 if ((err = alloc_disk_sb(rdev)))
1971 err = lock_rdev(rdev, newdev);
1975 rdev->kobj.parent = NULL;
1976 rdev->kobj.ktype = &rdev_ktype;
1977 kobject_init(&rdev->kobj);
1981 rdev->data_offset = 0;
1982 rdev->sb_events = 0;
1983 atomic_set(&rdev->nr_pending, 0);
1984 atomic_set(&rdev->read_errors, 0);
1985 atomic_set(&rdev->corrected_errors, 0);
1987 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
1990 "md: %s has zero or unknown size, marking faulty!\n",
1991 bdevname(rdev->bdev,b));
1996 if (super_format >= 0) {
1997 err = super_types[super_format].
1998 load_super(rdev, NULL, super_minor);
1999 if (err == -EINVAL) {
2001 "md: %s has invalid sb, not importing!\n",
2002 bdevname(rdev->bdev,b));
2007 "md: could not read %s's sb, not importing!\n",
2008 bdevname(rdev->bdev,b));
2012 INIT_LIST_HEAD(&rdev->same_set);
2017 if (rdev->sb_page) {
2023 return ERR_PTR(err);
2027 * Check a full RAID array for plausibility
2031 static void analyze_sbs(mddev_t * mddev)
2034 struct list_head *tmp;
2035 mdk_rdev_t *rdev, *freshest;
2036 char b[BDEVNAME_SIZE];
2039 ITERATE_RDEV(mddev,rdev,tmp)
2040 switch (super_types[mddev->major_version].
2041 load_super(rdev, freshest, mddev->minor_version)) {
2049 "md: fatal superblock inconsistency in %s"
2050 " -- removing from array\n",
2051 bdevname(rdev->bdev,b));
2052 kick_rdev_from_array(rdev);
2056 super_types[mddev->major_version].
2057 validate_super(mddev, freshest);
2060 ITERATE_RDEV(mddev,rdev,tmp) {
2061 if (rdev != freshest)
2062 if (super_types[mddev->major_version].
2063 validate_super(mddev, rdev)) {
2064 printk(KERN_WARNING "md: kicking non-fresh %s"
2066 bdevname(rdev->bdev,b));
2067 kick_rdev_from_array(rdev);
2070 if (mddev->level == LEVEL_MULTIPATH) {
2071 rdev->desc_nr = i++;
2072 rdev->raid_disk = rdev->desc_nr;
2073 set_bit(In_sync, &rdev->flags);
2079 if (mddev->recovery_cp != MaxSector &&
2081 printk(KERN_ERR "md: %s: raid array is not clean"
2082 " -- starting background reconstruction\n",
2088 safe_delay_show(mddev_t *mddev, char *page)
2090 int msec = (mddev->safemode_delay*1000)/HZ;
2091 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2094 safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2102 /* remove a period, and count digits after it */
2103 if (len >= sizeof(buf))
2105 strlcpy(buf, cbuf, len);
2107 for (i=0; i<len; i++) {
2109 if (isdigit(buf[i])) {
2114 } else if (buf[i] == '.') {
2119 msec = simple_strtoul(buf, &e, 10);
2120 if (e == buf || (*e && *e != '\n'))
2122 msec = (msec * 1000) / scale;
2124 mddev->safemode_delay = 0;
2126 mddev->safemode_delay = (msec*HZ)/1000;
2127 if (mddev->safemode_delay == 0)
2128 mddev->safemode_delay = 1;
2132 static struct md_sysfs_entry md_safe_delay =
2133 __ATTR(safe_mode_delay, 0644,safe_delay_show, safe_delay_store);
2136 level_show(mddev_t *mddev, char *page)
2138 struct mdk_personality *p = mddev->pers;
2140 return sprintf(page, "%s\n", p->name);
2141 else if (mddev->clevel[0])
2142 return sprintf(page, "%s\n", mddev->clevel);
2143 else if (mddev->level != LEVEL_NONE)
2144 return sprintf(page, "%d\n", mddev->level);
2150 level_store(mddev_t *mddev, const char *buf, size_t len)
2157 if (len >= sizeof(mddev->clevel))
2159 strncpy(mddev->clevel, buf, len);
2160 if (mddev->clevel[len-1] == '\n')
2162 mddev->clevel[len] = 0;
2163 mddev->level = LEVEL_NONE;
2167 static struct md_sysfs_entry md_level =
2168 __ATTR(level, 0644, level_show, level_store);
2172 layout_show(mddev_t *mddev, char *page)
2174 /* just a number, not meaningful for all levels */
2175 return sprintf(page, "%d\n", mddev->layout);
2179 layout_store(mddev_t *mddev, const char *buf, size_t len)
2182 unsigned long n = simple_strtoul(buf, &e, 10);
2186 if (!*buf || (*e && *e != '\n'))
2192 static struct md_sysfs_entry md_layout =
2193 __ATTR(layout, 0655, layout_show, layout_store);
2197 raid_disks_show(mddev_t *mddev, char *page)
2199 if (mddev->raid_disks == 0)
2201 return sprintf(page, "%d\n", mddev->raid_disks);
2204 static int update_raid_disks(mddev_t *mddev, int raid_disks);
2207 raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2209 /* can only set raid_disks if array is not yet active */
2212 unsigned long n = simple_strtoul(buf, &e, 10);
2214 if (!*buf || (*e && *e != '\n'))
2218 rv = update_raid_disks(mddev, n);
2220 mddev->raid_disks = n;
2221 return rv ? rv : len;
2223 static struct md_sysfs_entry md_raid_disks =
2224 __ATTR(raid_disks, 0644, raid_disks_show, raid_disks_store);
2227 chunk_size_show(mddev_t *mddev, char *page)
2229 return sprintf(page, "%d\n", mddev->chunk_size);
2233 chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2235 /* can only set chunk_size if array is not yet active */
2237 unsigned long n = simple_strtoul(buf, &e, 10);
2241 if (!*buf || (*e && *e != '\n'))
2244 mddev->chunk_size = n;
2247 static struct md_sysfs_entry md_chunk_size =
2248 __ATTR(chunk_size, 0644, chunk_size_show, chunk_size_store);
2251 resync_start_show(mddev_t *mddev, char *page)
2253 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2257 resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2259 /* can only set chunk_size if array is not yet active */
2261 unsigned long long n = simple_strtoull(buf, &e, 10);
2265 if (!*buf || (*e && *e != '\n'))
2268 mddev->recovery_cp = n;
2271 static struct md_sysfs_entry md_resync_start =
2272 __ATTR(resync_start, 0644, resync_start_show, resync_start_store);
2275 * The array state can be:
2278 * No devices, no size, no level
2279 * Equivalent to STOP_ARRAY ioctl
2281 * May have some settings, but array is not active
2282 * all IO results in error
2283 * When written, doesn't tear down array, but just stops it
2284 * suspended (not supported yet)
2285 * All IO requests will block. The array can be reconfigured.
2286 * Writing this, if accepted, will block until array is quiessent
2288 * no resync can happen. no superblocks get written.
2289 * write requests fail
2291 * like readonly, but behaves like 'clean' on a write request.
2293 * clean - no pending writes, but otherwise active.
2294 * When written to inactive array, starts without resync
2295 * If a write request arrives then
2296 * if metadata is known, mark 'dirty' and switch to 'active'.
2297 * if not known, block and switch to write-pending
2298 * If written to an active array that has pending writes, then fails.
2300 * fully active: IO and resync can be happening.
2301 * When written to inactive array, starts with resync
2304 * clean, but writes are blocked waiting for 'active' to be written.
2307 * like active, but no writes have been seen for a while (100msec).
2310 enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
2311 write_pending, active_idle, bad_word};
2312 static char *array_states[] = {
2313 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2314 "write-pending", "active-idle", NULL };
2316 static int match_word(const char *word, char **list)
2319 for (n=0; list[n]; n++)
2320 if (cmd_match(word, list[n]))
2326 array_state_show(mddev_t *mddev, char *page)
2328 enum array_state st = inactive;
2341 else if (mddev->safemode)
2347 if (list_empty(&mddev->disks) &&
2348 mddev->raid_disks == 0 &&
2354 return sprintf(page, "%s\n", array_states[st]);
2357 static int do_md_stop(mddev_t * mddev, int ro);
2358 static int do_md_run(mddev_t * mddev);
2359 static int restart_array(mddev_t *mddev);
2362 array_state_store(mddev_t *mddev, const char *buf, size_t len)
2365 enum array_state st = match_word(buf, array_states);
2370 /* stopping an active array */
2372 if (atomic_read(&mddev->active) > 1)
2374 err = do_md_stop(mddev, 0);
2378 /* stopping an active array */
2380 if (atomic_read(&mddev->active) > 1)
2382 err = do_md_stop(mddev, 2);
2386 break; /* not supported yet */
2389 err = do_md_stop(mddev, 1);
2392 err = do_md_run(mddev);
2396 /* stopping an active array */
2398 err = do_md_stop(mddev, 1);
2400 mddev->ro = 2; /* FIXME mark devices writable */
2403 err = do_md_run(mddev);
2408 restart_array(mddev);
2409 spin_lock_irq(&mddev->write_lock);
2410 if (atomic_read(&mddev->writes_pending) == 0) {
2412 mddev->sb_dirty = 1;
2414 spin_unlock_irq(&mddev->write_lock);
2417 mddev->recovery_cp = MaxSector;
2418 err = do_md_run(mddev);
2423 restart_array(mddev);
2424 mddev->sb_dirty = 0;
2425 wake_up(&mddev->sb_wait);
2429 err = do_md_run(mddev);
2434 /* these cannot be set */
2442 static struct md_sysfs_entry md_array_state = __ATTR(array_state, 0644, array_state_show, array_state_store);
2445 null_show(mddev_t *mddev, char *page)
2451 new_dev_store(mddev_t *mddev, const char *buf, size_t len)
2453 /* buf must be %d:%d\n? giving major and minor numbers */
2454 /* The new device is added to the array.
2455 * If the array has a persistent superblock, we read the
2456 * superblock to initialise info and check validity.
2457 * Otherwise, only checking done is that in bind_rdev_to_array,
2458 * which mainly checks size.
2461 int major = simple_strtoul(buf, &e, 10);
2467 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
2469 minor = simple_strtoul(e+1, &e, 10);
2470 if (*e && *e != '\n')
2472 dev = MKDEV(major, minor);
2473 if (major != MAJOR(dev) ||
2474 minor != MINOR(dev))
2478 if (mddev->persistent) {
2479 rdev = md_import_device(dev, mddev->major_version,
2480 mddev->minor_version);
2481 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
2482 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2483 mdk_rdev_t, same_set);
2484 err = super_types[mddev->major_version]
2485 .load_super(rdev, rdev0, mddev->minor_version);
2490 rdev = md_import_device(dev, -1, -1);
2493 return PTR_ERR(rdev);
2494 err = bind_rdev_to_array(rdev, mddev);
2498 return err ? err : len;
2501 static struct md_sysfs_entry md_new_device =
2502 __ATTR(new_dev, 0200, null_show, new_dev_store);
2505 size_show(mddev_t *mddev, char *page)
2507 return sprintf(page, "%llu\n", (unsigned long long)mddev->size);
2510 static int update_size(mddev_t *mddev, unsigned long size);
2513 size_store(mddev_t *mddev, const char *buf, size_t len)
2515 /* If array is inactive, we can reduce the component size, but
2516 * not increase it (except from 0).
2517 * If array is active, we can try an on-line resize
2521 unsigned long long size = simple_strtoull(buf, &e, 10);
2522 if (!*buf || *buf == '\n' ||
2527 err = update_size(mddev, size);
2528 md_update_sb(mddev);
2530 if (mddev->size == 0 ||
2536 return err ? err : len;
2539 static struct md_sysfs_entry md_size =
2540 __ATTR(component_size, 0644, size_show, size_store);
2544 * This is either 'none' for arrays with externally managed metadata,
2545 * or N.M for internally known formats
2548 metadata_show(mddev_t *mddev, char *page)
2550 if (mddev->persistent)
2551 return sprintf(page, "%d.%d\n",
2552 mddev->major_version, mddev->minor_version);
2554 return sprintf(page, "none\n");
2558 metadata_store(mddev_t *mddev, const char *buf, size_t len)
2562 if (!list_empty(&mddev->disks))
2565 if (cmd_match(buf, "none")) {
2566 mddev->persistent = 0;
2567 mddev->major_version = 0;
2568 mddev->minor_version = 90;
2571 major = simple_strtoul(buf, &e, 10);
2572 if (e==buf || *e != '.')
2575 minor = simple_strtoul(buf, &e, 10);
2576 if (e==buf || *e != '\n')
2578 if (major >= sizeof(super_types)/sizeof(super_types[0]) ||
2579 super_types[major].name == NULL)
2581 mddev->major_version = major;
2582 mddev->minor_version = minor;
2583 mddev->persistent = 1;
2587 static struct md_sysfs_entry md_metadata =
2588 __ATTR(metadata_version, 0644, metadata_show, metadata_store);
2591 action_show(mddev_t *mddev, char *page)
2593 char *type = "idle";
2594 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2595 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) {
2596 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2598 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2599 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2601 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2608 return sprintf(page, "%s\n", type);
2612 action_store(mddev_t *mddev, const char *page, size_t len)
2614 if (!mddev->pers || !mddev->pers->sync_request)
2617 if (cmd_match(page, "idle")) {
2618 if (mddev->sync_thread) {
2619 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2620 md_unregister_thread(mddev->sync_thread);
2621 mddev->sync_thread = NULL;
2622 mddev->recovery = 0;
2624 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2625 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
2627 else if (cmd_match(page, "resync") || cmd_match(page, "recover"))
2628 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2629 else if (cmd_match(page, "reshape")) {
2631 if (mddev->pers->start_reshape == NULL)
2633 err = mddev->pers->start_reshape(mddev);
2637 if (cmd_match(page, "check"))
2638 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
2639 else if (!cmd_match(page, "repair"))
2641 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
2642 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
2644 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2645 md_wakeup_thread(mddev->thread);
2650 mismatch_cnt_show(mddev_t *mddev, char *page)
2652 return sprintf(page, "%llu\n",
2653 (unsigned long long) mddev->resync_mismatches);
2656 static struct md_sysfs_entry
2657 md_scan_mode = __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
2660 static struct md_sysfs_entry
2661 md_mismatches = __ATTR_RO(mismatch_cnt);
2664 sync_min_show(mddev_t *mddev, char *page)
2666 return sprintf(page, "%d (%s)\n", speed_min(mddev),
2667 mddev->sync_speed_min ? "local": "system");
2671 sync_min_store(mddev_t *mddev, const char *buf, size_t len)
2675 if (strncmp(buf, "system", 6)==0) {
2676 mddev->sync_speed_min = 0;
2679 min = simple_strtoul(buf, &e, 10);
2680 if (buf == e || (*e && *e != '\n') || min <= 0)
2682 mddev->sync_speed_min = min;
2686 static struct md_sysfs_entry md_sync_min =
2687 __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
2690 sync_max_show(mddev_t *mddev, char *page)
2692 return sprintf(page, "%d (%s)\n", speed_max(mddev),
2693 mddev->sync_speed_max ? "local": "system");
2697 sync_max_store(mddev_t *mddev, const char *buf, size_t len)
2701 if (strncmp(buf, "system", 6)==0) {
2702 mddev->sync_speed_max = 0;
2705 max = simple_strtoul(buf, &e, 10);
2706 if (buf == e || (*e && *e != '\n') || max <= 0)
2708 mddev->sync_speed_max = max;
2712 static struct md_sysfs_entry md_sync_max =
2713 __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
2717 sync_speed_show(mddev_t *mddev, char *page)
2719 unsigned long resync, dt, db;
2720 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
2721 dt = ((jiffies - mddev->resync_mark) / HZ);
2723 db = resync - (mddev->resync_mark_cnt);
2724 return sprintf(page, "%ld\n", db/dt/2); /* K/sec */
2727 static struct md_sysfs_entry
2728 md_sync_speed = __ATTR_RO(sync_speed);
2731 sync_completed_show(mddev_t *mddev, char *page)
2733 unsigned long max_blocks, resync;
2735 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2736 max_blocks = mddev->resync_max_sectors;
2738 max_blocks = mddev->size << 1;
2740 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
2741 return sprintf(page, "%lu / %lu\n", resync, max_blocks);
2744 static struct md_sysfs_entry
2745 md_sync_completed = __ATTR_RO(sync_completed);
2748 suspend_lo_show(mddev_t *mddev, char *page)
2750 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
2754 suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
2757 unsigned long long new = simple_strtoull(buf, &e, 10);
2759 if (mddev->pers->quiesce == NULL)
2761 if (buf == e || (*e && *e != '\n'))
2763 if (new >= mddev->suspend_hi ||
2764 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
2765 mddev->suspend_lo = new;
2766 mddev->pers->quiesce(mddev, 2);
2771 static struct md_sysfs_entry md_suspend_lo =
2772 __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
2776 suspend_hi_show(mddev_t *mddev, char *page)
2778 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
2782 suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
2785 unsigned long long new = simple_strtoull(buf, &e, 10);
2787 if (mddev->pers->quiesce == NULL)
2789 if (buf == e || (*e && *e != '\n'))
2791 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
2792 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
2793 mddev->suspend_hi = new;
2794 mddev->pers->quiesce(mddev, 1);
2795 mddev->pers->quiesce(mddev, 0);
2800 static struct md_sysfs_entry md_suspend_hi =
2801 __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
2804 static struct attribute *md_default_attrs[] = {
2807 &md_raid_disks.attr,
2808 &md_chunk_size.attr,
2810 &md_resync_start.attr,
2812 &md_new_device.attr,
2813 &md_safe_delay.attr,
2814 &md_array_state.attr,
2818 static struct attribute *md_redundancy_attrs[] = {
2820 &md_mismatches.attr,
2823 &md_sync_speed.attr,
2824 &md_sync_completed.attr,
2825 &md_suspend_lo.attr,
2826 &md_suspend_hi.attr,
2829 static struct attribute_group md_redundancy_group = {
2831 .attrs = md_redundancy_attrs,
2836 md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2838 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
2839 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
2844 rv = mddev_lock(mddev);
2846 rv = entry->show(mddev, page);
2847 mddev_unlock(mddev);
2853 md_attr_store(struct kobject *kobj, struct attribute *attr,
2854 const char *page, size_t length)
2856 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
2857 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
2862 rv = mddev_lock(mddev);
2864 rv = entry->store(mddev, page, length);
2865 mddev_unlock(mddev);
2870 static void md_free(struct kobject *ko)
2872 mddev_t *mddev = container_of(ko, mddev_t, kobj);
2876 static struct sysfs_ops md_sysfs_ops = {
2877 .show = md_attr_show,
2878 .store = md_attr_store,
2880 static struct kobj_type md_ktype = {
2882 .sysfs_ops = &md_sysfs_ops,
2883 .default_attrs = md_default_attrs,
2888 static struct kobject *md_probe(dev_t dev, int *part, void *data)
2890 static DEFINE_MUTEX(disks_mutex);
2891 mddev_t *mddev = mddev_find(dev);
2892 struct gendisk *disk;
2893 int partitioned = (MAJOR(dev) != MD_MAJOR);
2894 int shift = partitioned ? MdpMinorShift : 0;
2895 int unit = MINOR(dev) >> shift;
2900 mutex_lock(&disks_mutex);
2901 if (mddev->gendisk) {
2902 mutex_unlock(&disks_mutex);
2906 disk = alloc_disk(1 << shift);
2908 mutex_unlock(&disks_mutex);
2912 disk->major = MAJOR(dev);
2913 disk->first_minor = unit << shift;
2915 sprintf(disk->disk_name, "md_d%d", unit);
2916 sprintf(disk->devfs_name, "md/d%d", unit);
2918 sprintf(disk->disk_name, "md%d", unit);
2919 sprintf(disk->devfs_name, "md/%d", unit);
2921 disk->fops = &md_fops;
2922 disk->private_data = mddev;
2923 disk->queue = mddev->queue;
2925 mddev->gendisk = disk;
2926 mutex_unlock(&disks_mutex);
2927 mddev->kobj.parent = &disk->kobj;
2928 mddev->kobj.k_name = NULL;
2929 snprintf(mddev->kobj.name, KOBJ_NAME_LEN, "%s", "md");
2930 mddev->kobj.ktype = &md_ktype;
2931 kobject_register(&mddev->kobj);
2935 static void md_safemode_timeout(unsigned long data)
2937 mddev_t *mddev = (mddev_t *) data;
2939 mddev->safemode = 1;
2940 md_wakeup_thread(mddev->thread);
2943 static int start_dirty_degraded;
2945 static int do_md_run(mddev_t * mddev)
2949 struct list_head *tmp;
2951 struct gendisk *disk;
2952 struct mdk_personality *pers;
2953 char b[BDEVNAME_SIZE];
2955 if (list_empty(&mddev->disks))
2956 /* cannot run an array with no devices.. */
2963 * Analyze all RAID superblock(s)
2965 if (!mddev->raid_disks)
2968 chunk_size = mddev->chunk_size;
2971 if (chunk_size > MAX_CHUNK_SIZE) {
2972 printk(KERN_ERR "too big chunk_size: %d > %d\n",
2973 chunk_size, MAX_CHUNK_SIZE);
2977 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
2979 if ( (1 << ffz(~chunk_size)) != chunk_size) {
2980 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
2983 if (chunk_size < PAGE_SIZE) {
2984 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
2985 chunk_size, PAGE_SIZE);
2989 /* devices must have minimum size of one chunk */
2990 ITERATE_RDEV(mddev,rdev,tmp) {
2991 if (test_bit(Faulty, &rdev->flags))
2993 if (rdev->size < chunk_size / 1024) {
2995 "md: Dev %s smaller than chunk_size:"
2997 bdevname(rdev->bdev,b),
2998 (unsigned long long)rdev->size,
3006 if (mddev->level != LEVEL_NONE)
3007 request_module("md-level-%d", mddev->level);
3008 else if (mddev->clevel[0])
3009 request_module("md-%s", mddev->clevel);
3013 * Drop all container device buffers, from now on
3014 * the only valid external interface is through the md
3016 * Also find largest hardsector size
3018 ITERATE_RDEV(mddev,rdev,tmp) {
3019 if (test_bit(Faulty, &rdev->flags))
3021 sync_blockdev(rdev->bdev);
3022 invalidate_bdev(rdev->bdev, 0);
3025 md_probe(mddev->unit, NULL, NULL);
3026 disk = mddev->gendisk;
3030 spin_lock(&pers_lock);
3031 pers = find_pers(mddev->level, mddev->clevel);
3032 if (!pers || !try_module_get(pers->owner)) {
3033 spin_unlock(&pers_lock);
3034 if (mddev->level != LEVEL_NONE)
3035 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
3038 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
3043 spin_unlock(&pers_lock);
3044 mddev->level = pers->level;
3045 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3047 if (mddev->reshape_position != MaxSector &&
3048 pers->start_reshape == NULL) {
3049 /* This personality cannot handle reshaping... */
3051 module_put(pers->owner);
3055 mddev->recovery = 0;
3056 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
3057 mddev->barriers_work = 1;
3058 mddev->ok_start_degraded = start_dirty_degraded;
3061 mddev->ro = 2; /* read-only, but switch on first write */
3063 err = mddev->pers->run(mddev);
3064 if (!err && mddev->pers->sync_request) {
3065 err = bitmap_create(mddev);
3067 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
3068 mdname(mddev), err);
3069 mddev->pers->stop(mddev);
3073 printk(KERN_ERR "md: pers->run() failed ...\n");
3074 module_put(mddev->pers->owner);
3076 bitmap_destroy(mddev);
3079 if (mddev->pers->sync_request)
3080 sysfs_create_group(&mddev->kobj, &md_redundancy_group);
3081 else if (mddev->ro == 2) /* auto-readonly not meaningful */
3084 atomic_set(&mddev->writes_pending,0);
3085 mddev->safemode = 0;
3086 mddev->safemode_timer.function = md_safemode_timeout;
3087 mddev->safemode_timer.data = (unsigned long) mddev;
3088 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
3091 ITERATE_RDEV(mddev,rdev,tmp)
3092 if (rdev->raid_disk >= 0) {
3094 sprintf(nm, "rd%d", rdev->raid_disk);
3095 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
3098 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3099 md_wakeup_thread(mddev->thread);
3101 if (mddev->sb_dirty)
3102 md_update_sb(mddev);
3104 set_capacity(disk, mddev->array_size<<1);
3106 /* If we call blk_queue_make_request here, it will
3107 * re-initialise max_sectors etc which may have been
3108 * refined inside -> run. So just set the bits we need to set.
3109 * Most initialisation happended when we called
3110 * blk_queue_make_request(..., md_fail_request)
3113 mddev->queue->queuedata = mddev;
3114 mddev->queue->make_request_fn = mddev->pers->make_request;
3116 /* If there is a partially-recovered drive we need to
3117 * start recovery here. If we leave it to md_check_recovery,
3118 * it will remove the drives and not do the right thing
3120 if (mddev->degraded) {
3121 struct list_head *rtmp;
3123 ITERATE_RDEV(mddev,rdev,rtmp)
3124 if (rdev->raid_disk >= 0 &&
3125 !test_bit(In_sync, &rdev->flags) &&
3126 !test_bit(Faulty, &rdev->flags))
3127 /* complete an interrupted recovery */
3129 if (spares && mddev->pers->sync_request) {
3130 mddev->recovery = 0;
3131 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3132 mddev->sync_thread = md_register_thread(md_do_sync,
3135 if (!mddev->sync_thread) {
3136 printk(KERN_ERR "%s: could not start resync"
3139 /* leave the spares where they are, it shouldn't hurt */
3140 mddev->recovery = 0;
3142 md_wakeup_thread(mddev->sync_thread);
3147 md_new_event(mddev);
3151 static int restart_array(mddev_t *mddev)
3153 struct gendisk *disk = mddev->gendisk;
3157 * Complain if it has no devices
3160 if (list_empty(&mddev->disks))
3168 mddev->safemode = 0;
3170 set_disk_ro(disk, 0);
3172 printk(KERN_INFO "md: %s switched to read-write mode.\n",
3175 * Kick recovery or resync if necessary
3177 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3178 md_wakeup_thread(mddev->thread);
3179 md_wakeup_thread(mddev->sync_thread);
3188 /* similar to deny_write_access, but accounts for our holding a reference
3189 * to the file ourselves */
3190 static int deny_bitmap_write_access(struct file * file)
3192 struct inode *inode = file->f_mapping->host;
3194 spin_lock(&inode->i_lock);
3195 if (atomic_read(&inode->i_writecount) > 1) {
3196 spin_unlock(&inode->i_lock);
3199 atomic_set(&inode->i_writecount, -1);
3200 spin_unlock(&inode->i_lock);
3205 static void restore_bitmap_write_access(struct file *file)
3207 struct inode *inode = file->f_mapping->host;
3209 spin_lock(&inode->i_lock);
3210 atomic_set(&inode->i_writecount, 1);
3211 spin_unlock(&inode->i_lock);
3215 * 0 - completely stop and dis-assemble array
3216 * 1 - switch to readonly
3217 * 2 - stop but do not disassemble array
3219 static int do_md_stop(mddev_t * mddev, int mode)
3222 struct gendisk *disk = mddev->gendisk;
3225 if (atomic_read(&mddev->active)>2) {
3226 printk("md: %s still in use.\n",mdname(mddev));
3230 if (mddev->sync_thread) {
3231 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3232 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3233 md_unregister_thread(mddev->sync_thread);
3234 mddev->sync_thread = NULL;
3237 del_timer_sync(&mddev->safemode_timer);
3239 invalidate_partition(disk, 0);
3242 case 1: /* readonly */
3248 case 0: /* disassemble */
3250 bitmap_flush(mddev);
3251 md_super_wait(mddev);
3253 set_disk_ro(disk, 0);
3254 blk_queue_make_request(mddev->queue, md_fail_request);
3255 mddev->pers->stop(mddev);
3256 if (mddev->pers->sync_request)
3257 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
3259 module_put(mddev->pers->owner);
3264 if (!mddev->in_sync || mddev->sb_dirty) {
3265 /* mark array as shutdown cleanly */
3267 md_update_sb(mddev);
3270 set_disk_ro(disk, 1);
3271 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3275 * Free resources if final stop
3279 struct list_head *tmp;
3280 struct gendisk *disk;
3281 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
3283 bitmap_destroy(mddev);
3284 if (mddev->bitmap_file) {
3285 restore_bitmap_write_access(mddev->bitmap_file);
3286 fput(mddev->bitmap_file);
3287 mddev->bitmap_file = NULL;
3289 mddev->bitmap_offset = 0;
3291 ITERATE_RDEV(mddev,rdev,tmp)
3292 if (rdev->raid_disk >= 0) {
3294 sprintf(nm, "rd%d", rdev->raid_disk);
3295 sysfs_remove_link(&mddev->kobj, nm);
3298 export_array(mddev);
3300 mddev->array_size = 0;
3302 mddev->raid_disks = 0;
3303 mddev->recovery_cp = 0;
3305 disk = mddev->gendisk;
3307 set_capacity(disk, 0);
3309 } else if (mddev->pers)
3310 printk(KERN_INFO "md: %s switched to read-only mode.\n",
3313 md_new_event(mddev);
3318 static void autorun_array(mddev_t *mddev)
3321 struct list_head *tmp;
3324 if (list_empty(&mddev->disks))
3327 printk(KERN_INFO "md: running: ");
3329 ITERATE_RDEV(mddev,rdev,tmp) {
3330 char b[BDEVNAME_SIZE];
3331 printk("<%s>", bdevname(rdev->bdev,b));
3335 err = do_md_run (mddev);
3337 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
3338 do_md_stop (mddev, 0);
3343 * lets try to run arrays based on all disks that have arrived
3344 * until now. (those are in pending_raid_disks)
3346 * the method: pick the first pending disk, collect all disks with
3347 * the same UUID, remove all from the pending list and put them into
3348 * the 'same_array' list. Then order this list based on superblock
3349 * update time (freshest comes first), kick out 'old' disks and
3350 * compare superblocks. If everything's fine then run it.
3352 * If "unit" is allocated, then bump its reference count
3354 static void autorun_devices(int part)
3356 struct list_head *tmp;
3357 mdk_rdev_t *rdev0, *rdev;
3359 char b[BDEVNAME_SIZE];
3361 printk(KERN_INFO "md: autorun ...\n");
3362 while (!list_empty(&pending_raid_disks)) {
3364 LIST_HEAD(candidates);
3365 rdev0 = list_entry(pending_raid_disks.next,
3366 mdk_rdev_t, same_set);
3368 printk(KERN_INFO "md: considering %s ...\n",
3369 bdevname(rdev0->bdev,b));
3370 INIT_LIST_HEAD(&candidates);
3371 ITERATE_RDEV_PENDING(rdev,tmp)
3372 if (super_90_load(rdev, rdev0, 0) >= 0) {
3373 printk(KERN_INFO "md: adding %s ...\n",
3374 bdevname(rdev->bdev,b));
3375 list_move(&rdev->same_set, &candidates);
3378 * now we have a set of devices, with all of them having
3379 * mostly sane superblocks. It's time to allocate the
3382 if (rdev0->preferred_minor < 0 || rdev0->preferred_minor >= MAX_MD_DEVS) {
3383 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
3384 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
3388 dev = MKDEV(mdp_major,
3389 rdev0->preferred_minor << MdpMinorShift);
3391 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
3393 md_probe(dev, NULL, NULL);
3394 mddev = mddev_find(dev);
3397 "md: cannot allocate memory for md drive.\n");
3400 if (mddev_lock(mddev))
3401 printk(KERN_WARNING "md: %s locked, cannot run\n",
3403 else if (mddev->raid_disks || mddev->major_version
3404 || !list_empty(&mddev->disks)) {
3406 "md: %s already running, cannot run %s\n",
3407 mdname(mddev), bdevname(rdev0->bdev,b));
3408 mddev_unlock(mddev);
3410 printk(KERN_INFO "md: created %s\n", mdname(mddev));
3411 ITERATE_RDEV_GENERIC(candidates,rdev,tmp) {
3412 list_del_init(&rdev->same_set);
3413 if (bind_rdev_to_array(rdev, mddev))
3416 autorun_array(mddev);
3417 mddev_unlock(mddev);
3419 /* on success, candidates will be empty, on error
3422 ITERATE_RDEV_GENERIC(candidates,rdev,tmp)
3426 printk(KERN_INFO "md: ... autorun DONE.\n");
3430 * import RAID devices based on one partition
3431 * if possible, the array gets run as well.
3434 static int autostart_array(dev_t startdev)
3436 char b[BDEVNAME_SIZE];
3437 int err = -EINVAL, i;
3438 mdp_super_t *sb = NULL;
3439 mdk_rdev_t *start_rdev = NULL, *rdev;
3441 start_rdev = md_import_device(startdev, 0, 0);
3442 if (IS_ERR(start_rdev))
3446 /* NOTE: this can only work for 0.90.0 superblocks */
3447 sb = (mdp_super_t*)page_address(start_rdev->sb_page);
3448 if (sb->major_version != 0 ||
3449 sb->minor_version != 90 ) {
3450 printk(KERN_WARNING "md: can only autostart 0.90.0 arrays\n");
3451 export_rdev(start_rdev);
3455 if (test_bit(Faulty, &start_rdev->flags)) {
3457 "md: can not autostart based on faulty %s!\n",
3458 bdevname(start_rdev->bdev,b));
3459 export_rdev(start_rdev);
3462 list_add(&start_rdev->same_set, &pending_raid_disks);
3464 for (i = 0; i < MD_SB_DISKS; i++) {
3465 mdp_disk_t *desc = sb->disks + i;
3466 dev_t dev = MKDEV(desc->major, desc->minor);
3470 if (dev == startdev)
3472 if (MAJOR(dev) != desc->major || MINOR(dev) != desc->minor)
3474 rdev = md_import_device(dev, 0, 0);
3478 list_add(&rdev->same_set, &pending_raid_disks);
3482 * possibly return codes
3490 static int get_version(void __user * arg)
3494 ver.major = MD_MAJOR_VERSION;
3495 ver.minor = MD_MINOR_VERSION;
3496 ver.patchlevel = MD_PATCHLEVEL_VERSION;
3498 if (copy_to_user(arg, &ver, sizeof(ver)))
3504 static int get_array_info(mddev_t * mddev, void __user * arg)
3506 mdu_array_info_t info;
3507 int nr,working,active,failed,spare;
3509 struct list_head *tmp;
3511 nr=working=active=failed=spare=0;
3512 ITERATE_RDEV(mddev,rdev,tmp) {
3514 if (test_bit(Faulty, &rdev->flags))
3518 if (test_bit(In_sync, &rdev->flags))
3525 info.major_version = mddev->major_version;
3526 info.minor_version = mddev->minor_version;
3527 info.patch_version = MD_PATCHLEVEL_VERSION;
3528 info.ctime = mddev->ctime;
3529 info.level = mddev->level;
3530 info.size = mddev->size;
3531 if (info.size != mddev->size) /* overflow */
3534 info.raid_disks = mddev->raid_disks;
3535 info.md_minor = mddev->md_minor;
3536 info.not_persistent= !mddev->persistent;
3538 info.utime = mddev->utime;
3541 info.state = (1<<MD_SB_CLEAN);
3542 if (mddev->bitmap && mddev->bitmap_offset)
3543 info.state = (1<<MD_SB_BITMAP_PRESENT);
3544 info.active_disks = active;
3545 info.working_disks = working;
3546 info.failed_disks = failed;
3547 info.spare_disks = spare;
3549 info.layout = mddev->layout;
3550 info.chunk_size = mddev->chunk_size;
3552 if (copy_to_user(arg, &info, sizeof(info)))
3558 static int get_bitmap_file(mddev_t * mddev, void __user * arg)
3560 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
3561 char *ptr, *buf = NULL;
3564 file = kmalloc(sizeof(*file), GFP_KERNEL);
3568 /* bitmap disabled, zero the first byte and copy out */
3569 if (!mddev->bitmap || !mddev->bitmap->file) {
3570 file->pathname[0] = '\0';
3574 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
3578 ptr = file_path(mddev->bitmap->file, buf, sizeof(file->pathname));
3582 strcpy(file->pathname, ptr);
3586 if (copy_to_user(arg, file, sizeof(*file)))
3594 static int get_disk_info(mddev_t * mddev, void __user * arg)
3596 mdu_disk_info_t info;
3600 if (copy_from_user(&info, arg, sizeof(info)))
3605 rdev = find_rdev_nr(mddev, nr);
3607 info.major = MAJOR(rdev->bdev->bd_dev);
3608 info.minor = MINOR(rdev->bdev->bd_dev);
3609 info.raid_disk = rdev->raid_disk;
3611 if (test_bit(Faulty, &rdev->flags))
3612 info.state |= (1<<MD_DISK_FAULTY);
3613 else if (test_bit(In_sync, &rdev->flags)) {
3614 info.state |= (1<<MD_DISK_ACTIVE);
3615 info.state |= (1<<MD_DISK_SYNC);
3617 if (test_bit(WriteMostly, &rdev->flags))
3618 info.state |= (1<<MD_DISK_WRITEMOSTLY);
3620 info.major = info.minor = 0;
3621 info.raid_disk = -1;
3622 info.state = (1<<MD_DISK_REMOVED);
3625 if (copy_to_user(arg, &info, sizeof(info)))
3631 static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
3633 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3635 dev_t dev = MKDEV(info->major,info->minor);
3637 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
3640 if (!mddev->raid_disks) {
3642 /* expecting a device which has a superblock */
3643 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
3646 "md: md_import_device returned %ld\n",
3648 return PTR_ERR(rdev);
3650 if (!list_empty(&mddev->disks)) {
3651 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3652 mdk_rdev_t, same_set);
3653 int err = super_types[mddev->major_version]
3654 .load_super(rdev, rdev0, mddev->minor_version);
3657 "md: %s has different UUID to %s\n",
3658 bdevname(rdev->bdev,b),
3659 bdevname(rdev0->bdev,b2));
3664 err = bind_rdev_to_array(rdev, mddev);
3671 * add_new_disk can be used once the array is assembled
3672 * to add "hot spares". They must already have a superblock
3677 if (!mddev->pers->hot_add_disk) {
3679 "%s: personality does not support diskops!\n",
3683 if (mddev->persistent)
3684 rdev = md_import_device(dev, mddev->major_version,
3685 mddev->minor_version);
3687 rdev = md_import_device(dev, -1, -1);
3690 "md: md_import_device returned %ld\n",
3692 return PTR_ERR(rdev);
3694 /* set save_raid_disk if appropriate */
3695 if (!mddev->persistent) {
3696 if (info->state & (1<<MD_DISK_SYNC) &&
3697 info->raid_disk < mddev->raid_disks)
3698 rdev->raid_disk = info->raid_disk;
3700 rdev->raid_disk = -1;
3702 super_types[mddev->major_version].
3703 validate_super(mddev, rdev);
3704 rdev->saved_raid_disk = rdev->raid_disk;
3706 clear_bit(In_sync, &rdev->flags); /* just to be sure */
3707 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
3708 set_bit(WriteMostly, &rdev->flags);
3710 rdev->raid_disk = -1;
3711 err = bind_rdev_to_array(rdev, mddev);
3712 if (!err && !mddev->pers->hot_remove_disk) {
3713 /* If there is hot_add_disk but no hot_remove_disk
3714 * then added disks for geometry changes,
3715 * and should be added immediately.
3717 super_types[mddev->major_version].
3718 validate_super(mddev, rdev);
3719 err = mddev->pers->hot_add_disk(mddev, rdev);
3721 unbind_rdev_from_array(rdev);
3726 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3727 md_wakeup_thread(mddev->thread);
3731 /* otherwise, add_new_disk is only allowed
3732 * for major_version==0 superblocks
3734 if (mddev->major_version != 0) {
3735 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
3740 if (!(info->state & (1<<MD_DISK_FAULTY))) {
3742 rdev = md_import_device (dev, -1, 0);
3745 "md: error, md_import_device() returned %ld\n",
3747 return PTR_ERR(rdev);
3749 rdev->desc_nr = info->number;
3750 if (info->raid_disk < mddev->raid_disks)
3751 rdev->raid_disk = info->raid_disk;
3753 rdev->raid_disk = -1;
3757 if (rdev->raid_disk < mddev->raid_disks)
3758 if (info->state & (1<<MD_DISK_SYNC))
3759 set_bit(In_sync, &rdev->flags);
3761 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
3762 set_bit(WriteMostly, &rdev->flags);
3764 if (!mddev->persistent) {
3765 printk(KERN_INFO "md: nonpersistent superblock ...\n");
3766 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
3768 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
3769 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
3771 err = bind_rdev_to_array(rdev, mddev);
3781 static int hot_remove_disk(mddev_t * mddev, dev_t dev)
3783 char b[BDEVNAME_SIZE];
3789 rdev = find_rdev(mddev, dev);
3793 if (rdev->raid_disk >= 0)
3796 kick_rdev_from_array(rdev);
3797 md_update_sb(mddev);
3798 md_new_event(mddev);
3802 printk(KERN_WARNING "md: cannot remove active disk %s from %s ... \n",
3803 bdevname(rdev->bdev,b), mdname(mddev));
3807 static int hot_add_disk(mddev_t * mddev, dev_t dev)
3809 char b[BDEVNAME_SIZE];
3817 if (mddev->major_version != 0) {
3818 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
3819 " version-0 superblocks.\n",
3823 if (!mddev->pers->hot_add_disk) {
3825 "%s: personality does not support diskops!\n",
3830 rdev = md_import_device (dev, -1, 0);
3833 "md: error, md_import_device() returned %ld\n",
3838 if (mddev->persistent)
3839 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
3842 rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
3844 size = calc_dev_size(rdev, mddev->chunk_size);
3847 if (test_bit(Faulty, &rdev->flags)) {
3849 "md: can not hot-add faulty %s disk to %s!\n",
3850 bdevname(rdev->bdev,b), mdname(mddev));
3854 clear_bit(In_sync, &rdev->flags);
3856 err = bind_rdev_to_array(rdev, mddev);
3861 * The rest should better be atomic, we can have disk failures
3862 * noticed in interrupt contexts ...
3865 if (rdev->desc_nr == mddev->max_disks) {
3866 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
3869 goto abort_unbind_export;
3872 rdev->raid_disk = -1;
3874 md_update_sb(mddev);
3877 * Kick recovery, maybe this spare has to be added to the
3878 * array immediately.
3880 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3881 md_wakeup_thread(mddev->thread);
3882 md_new_event(mddev);
3885 abort_unbind_export:
3886 unbind_rdev_from_array(rdev);
3893 static int set_bitmap_file(mddev_t *mddev, int fd)
3898 if (!mddev->pers->quiesce)
3900 if (mddev->recovery || mddev->sync_thread)
3902 /* we should be able to change the bitmap.. */
3908 return -EEXIST; /* cannot add when bitmap is present */
3909 mddev->bitmap_file = fget(fd);
3911 if (mddev->bitmap_file == NULL) {
3912 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
3917 err = deny_bitmap_write_access(mddev->bitmap_file);
3919 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
3921 fput(mddev->bitmap_file);
3922 mddev->bitmap_file = NULL;
3925 mddev->bitmap_offset = 0; /* file overrides offset */
3926 } else if (mddev->bitmap == NULL)
3927 return -ENOENT; /* cannot remove what isn't there */
3930 mddev->pers->quiesce(mddev, 1);
3932 err = bitmap_create(mddev);
3933 if (fd < 0 || err) {
3934 bitmap_destroy(mddev);
3935 fd = -1; /* make sure to put the file */
3937 mddev->pers->quiesce(mddev, 0);
3940 if (mddev->bitmap_file) {
3941 restore_bitmap_write_access(mddev->bitmap_file);
3942 fput(mddev->bitmap_file);
3944 mddev->bitmap_file = NULL;
3951 * set_array_info is used two different ways
3952 * The original usage is when creating a new array.
3953 * In this usage, raid_disks is > 0 and it together with
3954 * level, size, not_persistent,layout,chunksize determine the
3955 * shape of the array.
3956 * This will always create an array with a type-0.90.0 superblock.
3957 * The newer usage is when assembling an array.
3958 * In this case raid_disks will be 0, and the major_version field is
3959 * use to determine which style super-blocks are to be found on the devices.
3960 * The minor and patch _version numbers are also kept incase the
3961 * super_block handler wishes to interpret them.
3963 static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
3966 if (info->raid_disks == 0) {
3967 /* just setting version number for superblock loading */
3968 if (info->major_version < 0 ||
3969 info->major_version >= sizeof(super_types)/sizeof(super_types[0]) ||
3970 super_types[info->major_version].name == NULL) {
3971 /* maybe try to auto-load a module? */
3973 "md: superblock version %d not known\n",
3974 info->major_version);
3977 mddev->major_version = info->major_version;
3978 mddev->minor_version = info->minor_version;
3979 mddev->patch_version = info->patch_version;
3982 mddev->major_version = MD_MAJOR_VERSION;
3983 mddev->minor_version = MD_MINOR_VERSION;
3984 mddev->patch_version = MD_PATCHLEVEL_VERSION;
3985 mddev->ctime = get_seconds();
3987 mddev->level = info->level;
3988 mddev->clevel[0] = 0;
3989 mddev->size = info->size;
3990 mddev->raid_disks = info->raid_disks;
3991 /* don't set md_minor, it is determined by which /dev/md* was
3994 if (info->state & (1<<MD_SB_CLEAN))
3995 mddev->recovery_cp = MaxSector;
3997 mddev->recovery_cp = 0;
3998 mddev->persistent = ! info->not_persistent;
4000 mddev->layout = info->layout;
4001 mddev->chunk_size = info->chunk_size;
4003 mddev->max_disks = MD_SB_DISKS;
4005 mddev->sb_dirty = 1;
4007 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
4008 mddev->bitmap_offset = 0;
4010 mddev->reshape_position = MaxSector;
4013 * Generate a 128 bit UUID
4015 get_random_bytes(mddev->uuid, 16);
4017 mddev->new_level = mddev->level;
4018 mddev->new_chunk = mddev->chunk_size;
4019 mddev->new_layout = mddev->layout;
4020 mddev->delta_disks = 0;
4025 static int update_size(mddev_t *mddev, unsigned long size)
4029 struct list_head *tmp;
4030 int fit = (size == 0);
4032 if (mddev->pers->resize == NULL)
4034 /* The "size" is the amount of each device that is used.
4035 * This can only make sense for arrays with redundancy.
4036 * linear and raid0 always use whatever space is available
4037 * We can only consider changing the size if no resync
4038 * or reconstruction is happening, and if the new size
4039 * is acceptable. It must fit before the sb_offset or,
4040 * if that is <data_offset, it must fit before the
4041 * size of each device.
4042 * If size is zero, we find the largest size that fits.
4044 if (mddev->sync_thread)
4046 ITERATE_RDEV(mddev,rdev,tmp) {
4048 if (rdev->sb_offset > rdev->data_offset)
4049 avail = (rdev->sb_offset*2) - rdev->data_offset;
4051 avail = get_capacity(rdev->bdev->bd_disk)
4052 - rdev->data_offset;
4053 if (fit && (size == 0 || size > avail/2))
4055 if (avail < ((sector_t)size << 1))
4058 rv = mddev->pers->resize(mddev, (sector_t)size *2);
4060 struct block_device *bdev;
4062 bdev = bdget_disk(mddev->gendisk, 0);
4064 mutex_lock(&bdev->bd_inode->i_mutex);
4065 i_size_write(bdev->bd_inode, (loff_t)mddev->array_size << 10);
4066 mutex_unlock(&bdev->bd_inode->i_mutex);
4073 static int update_raid_disks(mddev_t *mddev, int raid_disks)
4076 /* change the number of raid disks */
4077 if (mddev->pers->check_reshape == NULL)
4079 if (raid_disks <= 0 ||
4080 raid_disks >= mddev->max_disks)
4082 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
4084 mddev->delta_disks = raid_disks - mddev->raid_disks;
4086 rv = mddev->pers->check_reshape(mddev);
4092 * update_array_info is used to change the configuration of an
4094 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
4095 * fields in the info are checked against the array.
4096 * Any differences that cannot be handled will cause an error.
4097 * Normally, only one change can be managed at a time.
4099 static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
4105 /* calculate expected state,ignoring low bits */
4106 if (mddev->bitmap && mddev->bitmap_offset)
4107 state |= (1 << MD_SB_BITMAP_PRESENT);
4109 if (mddev->major_version != info->major_version ||
4110 mddev->minor_version != info->minor_version ||
4111 /* mddev->patch_version != info->patch_version || */
4112 mddev->ctime != info->ctime ||
4113 mddev->level != info->level ||
4114 /* mddev->layout != info->layout || */
4115 !mddev->persistent != info->not_persistent||
4116 mddev->chunk_size != info->chunk_size ||
4117 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
4118 ((state^info->state) & 0xfffffe00)
4121 /* Check there is only one change */
4122 if (info->size >= 0 && mddev->size != info->size) cnt++;
4123 if (mddev->raid_disks != info->raid_disks) cnt++;
4124 if (mddev->layout != info->layout) cnt++;
4125 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
4126 if (cnt == 0) return 0;
4127 if (cnt > 1) return -EINVAL;
4129 if (mddev->layout != info->layout) {
4131 * we don't need to do anything at the md level, the
4132 * personality will take care of it all.
4134 if (mddev->pers->reconfig == NULL)
4137 return mddev->pers->reconfig(mddev, info->layout, -1);
4139 if (info->size >= 0 && mddev->size != info->size)
4140 rv = update_size(mddev, info->size);
4142 if (mddev->raid_disks != info->raid_disks)
4143 rv = update_raid_disks(mddev, info->raid_disks);
4145 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
4146 if (mddev->pers->quiesce == NULL)
4148 if (mddev->recovery || mddev->sync_thread)
4150 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
4151 /* add the bitmap */
4154 if (mddev->default_bitmap_offset == 0)
4156 mddev->bitmap_offset = mddev->default_bitmap_offset;
4157 mddev->pers->quiesce(mddev, 1);
4158 rv = bitmap_create(mddev);
4160 bitmap_destroy(mddev);
4161 mddev->pers->quiesce(mddev, 0);
4163 /* remove the bitmap */
4166 if (mddev->bitmap->file)
4168 mddev->pers->quiesce(mddev, 1);
4169 bitmap_destroy(mddev);
4170 mddev->pers->quiesce(mddev, 0);
4171 mddev->bitmap_offset = 0;
4174 md_update_sb(mddev);
4178 static int set_disk_faulty(mddev_t *mddev, dev_t dev)
4182 if (mddev->pers == NULL)
4185 rdev = find_rdev(mddev, dev);
4189 md_error(mddev, rdev);
4193 static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4195 mddev_t *mddev = bdev->bd_disk->private_data;
4199 geo->cylinders = get_capacity(mddev->gendisk) / 8;
4203 static int md_ioctl(struct inode *inode, struct file *file,
4204 unsigned int cmd, unsigned long arg)
4207 void __user *argp = (void __user *)arg;
4208 mddev_t *mddev = NULL;
4210 if (!capable(CAP_SYS_ADMIN))
4214 * Commands dealing with the RAID driver but not any
4220 err = get_version(argp);
4223 case PRINT_RAID_DEBUG:
4231 autostart_arrays(arg);
4238 * Commands creating/starting a new array:
4241 mddev = inode->i_bdev->bd_disk->private_data;
4249 if (cmd == START_ARRAY) {
4250 /* START_ARRAY doesn't need to lock the array as autostart_array
4251 * does the locking, and it could even be a different array
4256 "md: %s(pid %d) used deprecated START_ARRAY ioctl. "
4257 "This will not be supported beyond July 2006\n",
4258 current->comm, current->pid);
4261 err = autostart_array(new_decode_dev(arg));
4263 printk(KERN_WARNING "md: autostart failed!\n");
4269 err = mddev_lock(mddev);
4272 "md: ioctl lock interrupted, reason %d, cmd %d\n",
4279 case SET_ARRAY_INFO:
4281 mdu_array_info_t info;
4283 memset(&info, 0, sizeof(info));
4284 else if (copy_from_user(&info, argp, sizeof(info))) {
4289 err = update_array_info(mddev, &info);
4291 printk(KERN_WARNING "md: couldn't update"
4292 " array info. %d\n", err);
4297 if (!list_empty(&mddev->disks)) {
4299 "md: array %s already has disks!\n",
4304 if (mddev->raid_disks) {
4306 "md: array %s already initialised!\n",
4311 err = set_array_info(mddev, &info);
4313 printk(KERN_WARNING "md: couldn't set"
4314 " array info. %d\n", err);
4324 * Commands querying/configuring an existing array:
4326 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
4327 * RUN_ARRAY, and SET_BITMAP_FILE are allowed */
4328 if (!mddev->raid_disks && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
4329 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE) {
4335 * Commands even a read-only array can execute:
4339 case GET_ARRAY_INFO:
4340 err = get_array_info(mddev, argp);
4343 case GET_BITMAP_FILE:
4344 err = get_bitmap_file(mddev, argp);
4348 err = get_disk_info(mddev, argp);
4351 case RESTART_ARRAY_RW:
4352 err = restart_array(mddev);
4356 err = do_md_stop (mddev, 0);
4360 err = do_md_stop (mddev, 1);
4364 * We have a problem here : there is no easy way to give a CHS
4365 * virtual geometry. We currently pretend that we have a 2 heads
4366 * 4 sectors (with a BIG number of cylinders...). This drives
4367 * dosfs just mad... ;-)
4372 * The remaining ioctls are changing the state of the
4373 * superblock, so we do not allow them on read-only arrays.
4374 * However non-MD ioctls (e.g. get-size) will still come through
4375 * here and hit the 'default' below, so only disallow
4376 * 'md' ioctls, and switch to rw mode if started auto-readonly.
4378 if (_IOC_TYPE(cmd) == MD_MAJOR &&
4379 mddev->ro && mddev->pers) {
4380 if (mddev->ro == 2) {
4382 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4383 md_wakeup_thread(mddev->thread);
4395 mdu_disk_info_t info;
4396 if (copy_from_user(&info, argp, sizeof(info)))
4399 err = add_new_disk(mddev, &info);
4403 case HOT_REMOVE_DISK:
4404 err = hot_remove_disk(mddev, new_decode_dev(arg));
4408 err = hot_add_disk(mddev, new_decode_dev(arg));
4411 case SET_DISK_FAULTY:
4412 err = set_disk_faulty(mddev, new_decode_dev(arg));
4416 err = do_md_run (mddev);
4419 case SET_BITMAP_FILE:
4420 err = set_bitmap_file(mddev, (int)arg);
4430 mddev_unlock(mddev);
4440 static int md_open(struct inode *inode, struct file *file)
4443 * Succeed if we can lock the mddev, which confirms that
4444 * it isn't being stopped right now.
4446 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4449 if ((err = mddev_lock(mddev)))
4454 mddev_unlock(mddev);
4456 check_disk_change(inode->i_bdev);
4461 static int md_release(struct inode *inode, struct file * file)
4463 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4472 static int md_media_changed(struct gendisk *disk)
4474 mddev_t *mddev = disk->private_data;
4476 return mddev->changed;
4479 static int md_revalidate(struct gendisk *disk)
4481 mddev_t *mddev = disk->private_data;
4486 static struct block_device_operations md_fops =
4488 .owner = THIS_MODULE,
4490 .release = md_release,
4492 .getgeo = md_getgeo,
4493 .media_changed = md_media_changed,
4494 .revalidate_disk= md_revalidate,
4497 static int md_thread(void * arg)
4499 mdk_thread_t *thread = arg;
4502 * md_thread is a 'system-thread', it's priority should be very
4503 * high. We avoid resource deadlocks individually in each
4504 * raid personality. (RAID5 does preallocation) We also use RR and
4505 * the very same RT priority as kswapd, thus we will never get
4506 * into a priority inversion deadlock.
4508 * we definitely have to have equal or higher priority than
4509 * bdflush, otherwise bdflush will deadlock if there are too
4510 * many dirty RAID5 blocks.
4513 allow_signal(SIGKILL);
4514 while (!kthread_should_stop()) {
4516 /* We need to wait INTERRUPTIBLE so that
4517 * we don't add to the load-average.
4518 * That means we need to be sure no signals are
4521 if (signal_pending(current))
4522 flush_signals(current);
4524 wait_event_interruptible_timeout
4526 test_bit(THREAD_WAKEUP, &thread->flags)
4527 || kthread_should_stop(),
4531 clear_bit(THREAD_WAKEUP, &thread->flags);
4533 thread->run(thread->mddev);
4539 void md_wakeup_thread(mdk_thread_t *thread)
4542 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
4543 set_bit(THREAD_WAKEUP, &thread->flags);
4544 wake_up(&thread->wqueue);
4548 mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
4551 mdk_thread_t *thread;
4553 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
4557 init_waitqueue_head(&thread->wqueue);
4560 thread->mddev = mddev;
4561 thread->timeout = MAX_SCHEDULE_TIMEOUT;
4562 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
4563 if (IS_ERR(thread->tsk)) {
4570 void md_unregister_thread(mdk_thread_t *thread)
4572 dprintk("interrupting MD-thread pid %d\n", thread->tsk->pid);
4574 kthread_stop(thread->tsk);
4578 void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
4585 if (!rdev || test_bit(Faulty, &rdev->flags))
4588 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
4590 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
4591 __builtin_return_address(0),__builtin_return_address(1),
4592 __builtin_return_address(2),__builtin_return_address(3));
4594 if (!mddev->pers->error_handler)
4596 mddev->pers->error_handler(mddev,rdev);
4597 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4598 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4599 md_wakeup_thread(mddev->thread);
4600 md_new_event_inintr(mddev);
4603 /* seq_file implementation /proc/mdstat */
4605 static void status_unused(struct seq_file *seq)
4609 struct list_head *tmp;
4611 seq_printf(seq, "unused devices: ");
4613 ITERATE_RDEV_PENDING(rdev,tmp) {
4614 char b[BDEVNAME_SIZE];
4616 seq_printf(seq, "%s ",
4617 bdevname(rdev->bdev,b));
4620 seq_printf(seq, "<none>");
4622 seq_printf(seq, "\n");
4626 static void status_resync(struct seq_file *seq, mddev_t * mddev)
4628 sector_t max_blocks, resync, res;
4629 unsigned long dt, db, rt;
4631 unsigned int per_milli;
4633 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
4635 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
4636 max_blocks = mddev->resync_max_sectors >> 1;
4638 max_blocks = mddev->size;
4641 * Should not happen.
4647 /* Pick 'scale' such that (resync>>scale)*1000 will fit
4648 * in a sector_t, and (max_blocks>>scale) will fit in a
4649 * u32, as those are the requirements for sector_div.
4650 * Thus 'scale' must be at least 10
4653 if (sizeof(sector_t) > sizeof(unsigned long)) {
4654 while ( max_blocks/2 > (1ULL<<(scale+32)))
4657 res = (resync>>scale)*1000;
4658 sector_div(res, (u32)((max_blocks>>scale)+1));
4662 int i, x = per_milli/50, y = 20-x;
4663 seq_printf(seq, "[");
4664 for (i = 0; i < x; i++)
4665 seq_printf(seq, "=");
4666 seq_printf(seq, ">");
4667 for (i = 0; i < y; i++)
4668 seq_printf(seq, ".");
4669 seq_printf(seq, "] ");
4671 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
4672 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
4674 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
4675 "resync" : "recovery")),
4676 per_milli/10, per_milli % 10,
4677 (unsigned long long) resync,
4678 (unsigned long long) max_blocks);
4681 * We do not want to overflow, so the order of operands and
4682 * the * 100 / 100 trick are important. We do a +1 to be
4683 * safe against division by zero. We only estimate anyway.
4685 * dt: time from mark until now
4686 * db: blocks written from mark until now
4687 * rt: remaining time
4689 dt = ((jiffies - mddev->resync_mark) / HZ);
4691 db = resync - (mddev->resync_mark_cnt/2);
4692 rt = (dt * ((unsigned long)(max_blocks-resync) / (db/100+1)))/100;
4694 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
4696 seq_printf(seq, " speed=%ldK/sec", db/dt);
4699 static void *md_seq_start(struct seq_file *seq, loff_t *pos)
4701 struct list_head *tmp;
4711 spin_lock(&all_mddevs_lock);
4712 list_for_each(tmp,&all_mddevs)
4714 mddev = list_entry(tmp, mddev_t, all_mddevs);
4716 spin_unlock(&all_mddevs_lock);
4719 spin_unlock(&all_mddevs_lock);
4721 return (void*)2;/* tail */
4725 static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4727 struct list_head *tmp;
4728 mddev_t *next_mddev, *mddev = v;
4734 spin_lock(&all_mddevs_lock);
4736 tmp = all_mddevs.next;
4738 tmp = mddev->all_mddevs.next;
4739 if (tmp != &all_mddevs)
4740 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
4742 next_mddev = (void*)2;
4745 spin_unlock(&all_mddevs_lock);
4753 static void md_seq_stop(struct seq_file *seq, void *v)
4757 if (mddev && v != (void*)1 && v != (void*)2)
4761 struct mdstat_info {
4765 static int md_seq_show(struct seq_file *seq, void *v)
4769 struct list_head *tmp2;
4771 struct mdstat_info *mi = seq->private;
4772 struct bitmap *bitmap;
4774 if (v == (void*)1) {
4775 struct mdk_personality *pers;
4776 seq_printf(seq, "Personalities : ");
4777 spin_lock(&pers_lock);
4778 list_for_each_entry(pers, &pers_list, list)
4779 seq_printf(seq, "[%s] ", pers->name);
4781 spin_unlock(&pers_lock);
4782 seq_printf(seq, "\n");
4783 mi->event = atomic_read(&md_event_count);
4786 if (v == (void*)2) {
4791 if (mddev_lock(mddev) < 0)
4794 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
4795 seq_printf(seq, "%s : %sactive", mdname(mddev),
4796 mddev->pers ? "" : "in");
4799 seq_printf(seq, " (read-only)");
4801 seq_printf(seq, "(auto-read-only)");
4802 seq_printf(seq, " %s", mddev->pers->name);
4806 ITERATE_RDEV(mddev,rdev,tmp2) {
4807 char b[BDEVNAME_SIZE];
4808 seq_printf(seq, " %s[%d]",
4809 bdevname(rdev->bdev,b), rdev->desc_nr);
4810 if (test_bit(WriteMostly, &rdev->flags))
4811 seq_printf(seq, "(W)");
4812 if (test_bit(Faulty, &rdev->flags)) {
4813 seq_printf(seq, "(F)");
4815 } else if (rdev->raid_disk < 0)
4816 seq_printf(seq, "(S)"); /* spare */
4820 if (!list_empty(&mddev->disks)) {
4822 seq_printf(seq, "\n %llu blocks",
4823 (unsigned long long)mddev->array_size);
4825 seq_printf(seq, "\n %llu blocks",
4826 (unsigned long long)size);
4828 if (mddev->persistent) {
4829 if (mddev->major_version != 0 ||
4830 mddev->minor_version != 90) {
4831 seq_printf(seq," super %d.%d",
4832 mddev->major_version,
4833 mddev->minor_version);
4836 seq_printf(seq, " super non-persistent");
4839 mddev->pers->status (seq, mddev);
4840 seq_printf(seq, "\n ");
4841 if (mddev->pers->sync_request) {
4842 if (mddev->curr_resync > 2) {
4843 status_resync (seq, mddev);
4844 seq_printf(seq, "\n ");
4845 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
4846 seq_printf(seq, "\tresync=DELAYED\n ");
4847 else if (mddev->recovery_cp < MaxSector)
4848 seq_printf(seq, "\tresync=PENDING\n ");
4851 seq_printf(seq, "\n ");
4853 if ((bitmap = mddev->bitmap)) {
4854 unsigned long chunk_kb;
4855 unsigned long flags;
4856 spin_lock_irqsave(&bitmap->lock, flags);
4857 chunk_kb = bitmap->chunksize >> 10;
4858 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
4860 bitmap->pages - bitmap->missing_pages,
4862 (bitmap->pages - bitmap->missing_pages)
4863 << (PAGE_SHIFT - 10),
4864 chunk_kb ? chunk_kb : bitmap->chunksize,
4865 chunk_kb ? "KB" : "B");
4867 seq_printf(seq, ", file: ");
4868 seq_path(seq, bitmap->file->f_vfsmnt,
4869 bitmap->file->f_dentry," \t\n");
4872 seq_printf(seq, "\n");
4873 spin_unlock_irqrestore(&bitmap->lock, flags);
4876 seq_printf(seq, "\n");
4878 mddev_unlock(mddev);
4883 static struct seq_operations md_seq_ops = {
4884 .start = md_seq_start,
4885 .next = md_seq_next,
4886 .stop = md_seq_stop,
4887 .show = md_seq_show,
4890 static int md_seq_open(struct inode *inode, struct file *file)
4893 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
4897 error = seq_open(file, &md_seq_ops);
4901 struct seq_file *p = file->private_data;
4903 mi->event = atomic_read(&md_event_count);
4908 static int md_seq_release(struct inode *inode, struct file *file)
4910 struct seq_file *m = file->private_data;
4911 struct mdstat_info *mi = m->private;
4914 return seq_release(inode, file);
4917 static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
4919 struct seq_file *m = filp->private_data;
4920 struct mdstat_info *mi = m->private;
4923 poll_wait(filp, &md_event_waiters, wait);
4925 /* always allow read */
4926 mask = POLLIN | POLLRDNORM;
4928 if (mi->event != atomic_read(&md_event_count))
4929 mask |= POLLERR | POLLPRI;
4933 static struct file_operations md_seq_fops = {
4934 .open = md_seq_open,
4936 .llseek = seq_lseek,
4937 .release = md_seq_release,
4938 .poll = mdstat_poll,
4941 int register_md_personality(struct mdk_personality *p)
4943 spin_lock(&pers_lock);
4944 list_add_tail(&p->list, &pers_list);
4945 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
4946 spin_unlock(&pers_lock);
4950 int unregister_md_personality(struct mdk_personality *p)
4952 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
4953 spin_lock(&pers_lock);
4954 list_del_init(&p->list);
4955 spin_unlock(&pers_lock);
4959 static int is_mddev_idle(mddev_t *mddev)
4962 struct list_head *tmp;
4964 unsigned long curr_events;
4967 ITERATE_RDEV(mddev,rdev,tmp) {
4968 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
4969 curr_events = disk_stat_read(disk, sectors[0]) +
4970 disk_stat_read(disk, sectors[1]) -
4971 atomic_read(&disk->sync_io);
4972 /* The difference between curr_events and last_events
4973 * will be affected by any new non-sync IO (making
4974 * curr_events bigger) and any difference in the amount of
4975 * in-flight syncio (making current_events bigger or smaller)
4976 * The amount in-flight is currently limited to
4977 * 32*64K in raid1/10 and 256*PAGE_SIZE in raid5/6
4978 * which is at most 4096 sectors.
4979 * These numbers are fairly fragile and should be made
4980 * more robust, probably by enforcing the
4981 * 'window size' that md_do_sync sort-of uses.
4983 * Note: the following is an unsigned comparison.
4985 if ((curr_events - rdev->last_events + 4096) > 8192) {
4986 rdev->last_events = curr_events;
4993 void md_done_sync(mddev_t *mddev, int blocks, int ok)
4995 /* another "blocks" (512byte) blocks have been synced */
4996 atomic_sub(blocks, &mddev->recovery_active);
4997 wake_up(&mddev->recovery_wait);
4999 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
5000 md_wakeup_thread(mddev->thread);
5001 // stop recovery, signal do_sync ....
5006 /* md_write_start(mddev, bi)
5007 * If we need to update some array metadata (e.g. 'active' flag
5008 * in superblock) before writing, schedule a superblock update
5009 * and wait for it to complete.
5011 void md_write_start(mddev_t *mddev, struct bio *bi)
5013 if (bio_data_dir(bi) != WRITE)
5016 BUG_ON(mddev->ro == 1);
5017 if (mddev->ro == 2) {
5018 /* need to switch to read/write */
5020 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5021 md_wakeup_thread(mddev->thread);
5023 atomic_inc(&mddev->writes_pending);
5024 if (mddev->in_sync) {
5025 spin_lock_irq(&mddev->write_lock);
5026 if (mddev->in_sync) {
5028 mddev->sb_dirty = 3;
5029 md_wakeup_thread(mddev->thread);
5031 spin_unlock_irq(&mddev->write_lock);
5033 wait_event(mddev->sb_wait, mddev->sb_dirty==0);
5036 void md_write_end(mddev_t *mddev)
5038 if (atomic_dec_and_test(&mddev->writes_pending)) {
5039 if (mddev->safemode == 2)
5040 md_wakeup_thread(mddev->thread);
5041 else if (mddev->safemode_delay)
5042 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
5046 static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
5048 #define SYNC_MARKS 10
5049 #define SYNC_MARK_STEP (3*HZ)
5050 void md_do_sync(mddev_t *mddev)
5053 unsigned int currspeed = 0,
5055 sector_t max_sectors,j, io_sectors;
5056 unsigned long mark[SYNC_MARKS];
5057 sector_t mark_cnt[SYNC_MARKS];
5059 struct list_head *tmp;
5060 sector_t last_check;
5062 struct list_head *rtmp;
5065 /* just incase thread restarts... */
5066 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
5068 if (mddev->ro) /* never try to sync a read-only array */
5071 /* we overload curr_resync somewhat here.
5072 * 0 == not engaged in resync at all
5073 * 2 == checking that there is no conflict with another sync
5074 * 1 == like 2, but have yielded to allow conflicting resync to
5076 * other == active in resync - this many blocks
5078 * Before starting a resync we must have set curr_resync to
5079 * 2, and then checked that every "conflicting" array has curr_resync
5080 * less than ours. When we find one that is the same or higher
5081 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
5082 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
5083 * This will mean we have to start checking from the beginning again.
5088 mddev->curr_resync = 2;
5091 if (kthread_should_stop()) {
5092 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5095 ITERATE_MDDEV(mddev2,tmp) {
5096 if (mddev2 == mddev)
5098 if (mddev2->curr_resync &&
5099 match_mddev_units(mddev,mddev2)) {
5101 if (mddev < mddev2 && mddev->curr_resync == 2) {
5102 /* arbitrarily yield */
5103 mddev->curr_resync = 1;
5104 wake_up(&resync_wait);
5106 if (mddev > mddev2 && mddev->curr_resync == 1)
5107 /* no need to wait here, we can wait the next
5108 * time 'round when curr_resync == 2
5111 prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE);
5112 if (!kthread_should_stop() &&
5113 mddev2->curr_resync >= mddev->curr_resync) {
5114 printk(KERN_INFO "md: delaying resync of %s"
5115 " until %s has finished resync (they"
5116 " share one or more physical units)\n",
5117 mdname(mddev), mdname(mddev2));
5120 finish_wait(&resync_wait, &wq);
5123 finish_wait(&resync_wait, &wq);
5126 } while (mddev->curr_resync < 2);
5129 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5130 /* resync follows the size requested by the personality,
5131 * which defaults to physical size, but can be virtual size
5133 max_sectors = mddev->resync_max_sectors;
5134 mddev->resync_mismatches = 0;
5135 /* we don't use the checkpoint if there's a bitmap */
5136 if (!mddev->bitmap &&
5137 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5138 j = mddev->recovery_cp;
5139 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5140 max_sectors = mddev->size << 1;
5142 /* recovery follows the physical size of devices */
5143 max_sectors = mddev->size << 1;
5145 ITERATE_RDEV(mddev,rdev,rtmp)
5146 if (rdev->raid_disk >= 0 &&
5147 !test_bit(Faulty, &rdev->flags) &&
5148 !test_bit(In_sync, &rdev->flags) &&
5149 rdev->recovery_offset < j)
5150 j = rdev->recovery_offset;
5153 printk(KERN_INFO "md: syncing RAID array %s\n", mdname(mddev));
5154 printk(KERN_INFO "md: minimum _guaranteed_ reconstruction speed:"
5155 " %d KB/sec/disc.\n", speed_min(mddev));
5156 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
5157 "(but not more than %d KB/sec) for reconstruction.\n",
5160 is_mddev_idle(mddev); /* this also initializes IO event counters */
5163 for (m = 0; m < SYNC_MARKS; m++) {
5165 mark_cnt[m] = io_sectors;
5168 mddev->resync_mark = mark[last_mark];
5169 mddev->resync_mark_cnt = mark_cnt[last_mark];
5172 * Tune reconstruction:
5174 window = 32*(PAGE_SIZE/512);
5175 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
5176 window/2,(unsigned long long) max_sectors/2);
5178 atomic_set(&mddev->recovery_active, 0);
5179 init_waitqueue_head(&mddev->recovery_wait);
5184 "md: resuming recovery of %s from checkpoint.\n",
5186 mddev->curr_resync = j;
5189 while (j < max_sectors) {
5193 sectors = mddev->pers->sync_request(mddev, j, &skipped,
5194 currspeed < speed_min(mddev));
5196 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
5200 if (!skipped) { /* actual IO requested */
5201 io_sectors += sectors;
5202 atomic_add(sectors, &mddev->recovery_active);
5206 if (j>1) mddev->curr_resync = j;
5207 if (last_check == 0)
5208 /* this is the earliers that rebuilt will be
5209 * visible in /proc/mdstat
5211 md_new_event(mddev);
5213 if (last_check + window > io_sectors || j == max_sectors)
5216 last_check = io_sectors;
5218 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery) ||
5219 test_bit(MD_RECOVERY_ERR, &mddev->recovery))
5223 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
5225 int next = (last_mark+1) % SYNC_MARKS;
5227 mddev->resync_mark = mark[next];
5228 mddev->resync_mark_cnt = mark_cnt[next];
5229 mark[next] = jiffies;
5230 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
5235 if (kthread_should_stop()) {
5237 * got a signal, exit.
5240 "md: md_do_sync() got signal ... exiting\n");
5241 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5246 * this loop exits only if either when we are slower than
5247 * the 'hard' speed limit, or the system was IO-idle for
5249 * the system might be non-idle CPU-wise, but we only care
5250 * about not overloading the IO subsystem. (things like an
5251 * e2fsck being done on the RAID array should execute fast)
5253 mddev->queue->unplug_fn(mddev->queue);
5256 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
5257 /((jiffies-mddev->resync_mark)/HZ +1) +1;
5259 if (currspeed > speed_min(mddev)) {
5260 if ((currspeed > speed_max(mddev)) ||
5261 !is_mddev_idle(mddev)) {
5267 printk(KERN_INFO "md: %s: sync done.\n",mdname(mddev));
5269 * this also signals 'finished resyncing' to md_stop
5272 mddev->queue->unplug_fn(mddev->queue);
5274 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
5276 /* tell personality that we are finished */
5277 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
5279 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
5280 test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
5281 !test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
5282 mddev->curr_resync > 2) {
5283 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5284 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5285 if (mddev->curr_resync >= mddev->recovery_cp) {
5287 "md: checkpointing recovery of %s.\n",
5289 mddev->recovery_cp = mddev->curr_resync;
5292 mddev->recovery_cp = MaxSector;
5294 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5295 mddev->curr_resync = MaxSector;
5296 ITERATE_RDEV(mddev,rdev,rtmp)
5297 if (rdev->raid_disk >= 0 &&
5298 !test_bit(Faulty, &rdev->flags) &&
5299 !test_bit(In_sync, &rdev->flags) &&
5300 rdev->recovery_offset < mddev->curr_resync)
5301 rdev->recovery_offset = mddev->curr_resync;
5302 mddev->sb_dirty = 1;
5307 mddev->curr_resync = 0;
5308 wake_up(&resync_wait);
5309 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
5310 md_wakeup_thread(mddev->thread);
5312 EXPORT_SYMBOL_GPL(md_do_sync);
5316 * This routine is regularly called by all per-raid-array threads to
5317 * deal with generic issues like resync and super-block update.
5318 * Raid personalities that don't have a thread (linear/raid0) do not
5319 * need this as they never do any recovery or update the superblock.
5321 * It does not do any resync itself, but rather "forks" off other threads
5322 * to do that as needed.
5323 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
5324 * "->recovery" and create a thread at ->sync_thread.
5325 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
5326 * and wakeups up this thread which will reap the thread and finish up.
5327 * This thread also removes any faulty devices (with nr_pending == 0).
5329 * The overall approach is:
5330 * 1/ if the superblock needs updating, update it.
5331 * 2/ If a recovery thread is running, don't do anything else.
5332 * 3/ If recovery has finished, clean up, possibly marking spares active.
5333 * 4/ If there are any faulty devices, remove them.
5334 * 5/ If array is degraded, try to add spares devices
5335 * 6/ If array has spares or is not in-sync, start a resync thread.
5337 void md_check_recovery(mddev_t *mddev)
5340 struct list_head *rtmp;
5344 bitmap_daemon_work(mddev->bitmap);
5349 if (signal_pending(current)) {
5350 if (mddev->pers->sync_request) {
5351 printk(KERN_INFO "md: %s in immediate safe mode\n",
5353 mddev->safemode = 2;
5355 flush_signals(current);
5360 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
5361 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
5362 (mddev->safemode == 1) ||
5363 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
5364 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
5368 if (mddev_trylock(mddev)) {
5371 spin_lock_irq(&mddev->write_lock);
5372 if (mddev->safemode && !atomic_read(&mddev->writes_pending) &&
5373 !mddev->in_sync && mddev->recovery_cp == MaxSector) {
5375 mddev->sb_dirty = 3;
5377 if (mddev->safemode == 1)
5378 mddev->safemode = 0;
5379 spin_unlock_irq(&mddev->write_lock);
5381 if (mddev->sb_dirty)
5382 md_update_sb(mddev);
5385 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
5386 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
5387 /* resync/recovery still happening */
5388 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5391 if (mddev->sync_thread) {
5392 /* resync has finished, collect result */
5393 md_unregister_thread(mddev->sync_thread);
5394 mddev->sync_thread = NULL;
5395 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
5396 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5398 /* activate any spares */
5399 mddev->pers->spare_active(mddev);
5401 md_update_sb(mddev);
5403 /* if array is no-longer degraded, then any saved_raid_disk
5404 * information must be scrapped
5406 if (!mddev->degraded)
5407 ITERATE_RDEV(mddev,rdev,rtmp)
5408 rdev->saved_raid_disk = -1;
5410 mddev->recovery = 0;
5411 /* flag recovery needed just to double check */
5412 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5413 md_new_event(mddev);
5416 /* Clear some bits that don't mean anything, but
5419 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5420 clear_bit(MD_RECOVERY_ERR, &mddev->recovery);
5421 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
5422 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
5424 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
5426 /* no recovery is running.
5427 * remove any failed drives, then
5428 * add spares if possible.
5429 * Spare are also removed and re-added, to allow
5430 * the personality to fail the re-add.
5432 ITERATE_RDEV(mddev,rdev,rtmp)
5433 if (rdev->raid_disk >= 0 &&
5434 (test_bit(Faulty, &rdev->flags) || ! test_bit(In_sync, &rdev->flags)) &&
5435 atomic_read(&rdev->nr_pending)==0) {
5436 if (mddev->pers->hot_remove_disk(mddev, rdev->raid_disk)==0) {
5438 sprintf(nm,"rd%d", rdev->raid_disk);
5439 sysfs_remove_link(&mddev->kobj, nm);
5440 rdev->raid_disk = -1;
5444 if (mddev->degraded) {
5445 ITERATE_RDEV(mddev,rdev,rtmp)
5446 if (rdev->raid_disk < 0
5447 && !test_bit(Faulty, &rdev->flags)) {
5448 rdev->recovery_offset = 0;
5449 if (mddev->pers->hot_add_disk(mddev,rdev)) {
5451 sprintf(nm, "rd%d", rdev->raid_disk);
5452 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
5454 md_new_event(mddev);
5461 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5462 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5463 } else if (mddev->recovery_cp < MaxSector) {
5464 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5465 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
5466 /* nothing to be done ... */
5469 if (mddev->pers->sync_request) {
5470 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5471 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
5472 /* We are adding a device or devices to an array
5473 * which has the bitmap stored on all devices.
5474 * So make sure all bitmap pages get written
5476 bitmap_write_all(mddev->bitmap);
5478 mddev->sync_thread = md_register_thread(md_do_sync,
5481 if (!mddev->sync_thread) {
5482 printk(KERN_ERR "%s: could not start resync"
5485 /* leave the spares where they are, it shouldn't hurt */
5486 mddev->recovery = 0;
5488 md_wakeup_thread(mddev->sync_thread);
5489 md_new_event(mddev);
5492 mddev_unlock(mddev);
5496 static int md_notify_reboot(struct notifier_block *this,
5497 unsigned long code, void *x)
5499 struct list_head *tmp;
5502 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
5504 printk(KERN_INFO "md: stopping all md devices.\n");
5506 ITERATE_MDDEV(mddev,tmp)
5507 if (mddev_trylock(mddev)) {
5508 do_md_stop (mddev, 1);
5509 mddev_unlock(mddev);
5512 * certain more exotic SCSI devices are known to be
5513 * volatile wrt too early system reboots. While the
5514 * right place to handle this issue is the given
5515 * driver, we do want to have a safe RAID driver ...
5522 static struct notifier_block md_notifier = {
5523 .notifier_call = md_notify_reboot,
5525 .priority = INT_MAX, /* before any real devices */
5528 static void md_geninit(void)
5530 struct proc_dir_entry *p;
5532 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
5534 p = create_proc_entry("mdstat", S_IRUGO, NULL);
5536 p->proc_fops = &md_seq_fops;
5539 static int __init md_init(void)
5543 printk(KERN_INFO "md: md driver %d.%d.%d MAX_MD_DEVS=%d,"
5544 " MD_SB_DISKS=%d\n",
5545 MD_MAJOR_VERSION, MD_MINOR_VERSION,
5546 MD_PATCHLEVEL_VERSION, MAX_MD_DEVS, MD_SB_DISKS);
5547 printk(KERN_INFO "md: bitmap version %d.%d\n", BITMAP_MAJOR_HI,
5550 if (register_blkdev(MAJOR_NR, "md"))
5552 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
5553 unregister_blkdev(MAJOR_NR, "md");
5557 blk_register_region(MKDEV(MAJOR_NR, 0), MAX_MD_DEVS, THIS_MODULE,
5558 md_probe, NULL, NULL);
5559 blk_register_region(MKDEV(mdp_major, 0), MAX_MD_DEVS<<MdpMinorShift, THIS_MODULE,
5560 md_probe, NULL, NULL);
5562 for (minor=0; minor < MAX_MD_DEVS; ++minor)
5563 devfs_mk_bdev(MKDEV(MAJOR_NR, minor),
5564 S_IFBLK|S_IRUSR|S_IWUSR,
5567 for (minor=0; minor < MAX_MD_DEVS; ++minor)
5568 devfs_mk_bdev(MKDEV(mdp_major, minor<<MdpMinorShift),
5569 S_IFBLK|S_IRUSR|S_IWUSR,
5573 register_reboot_notifier(&md_notifier);
5574 raid_table_header = register_sysctl_table(raid_root_table, 1);
5584 * Searches all registered partitions for autorun RAID arrays
5587 static dev_t detected_devices[128];
5590 void md_autodetect_dev(dev_t dev)
5592 if (dev_cnt >= 0 && dev_cnt < 127)
5593 detected_devices[dev_cnt++] = dev;
5597 static void autostart_arrays(int part)
5602 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
5604 for (i = 0; i < dev_cnt; i++) {
5605 dev_t dev = detected_devices[i];
5607 rdev = md_import_device(dev,0, 0);
5611 if (test_bit(Faulty, &rdev->flags)) {
5615 list_add(&rdev->same_set, &pending_raid_disks);
5619 autorun_devices(part);
5624 static __exit void md_exit(void)
5627 struct list_head *tmp;
5629 blk_unregister_region(MKDEV(MAJOR_NR,0), MAX_MD_DEVS);
5630 blk_unregister_region(MKDEV(mdp_major,0), MAX_MD_DEVS << MdpMinorShift);
5631 for (i=0; i < MAX_MD_DEVS; i++)
5632 devfs_remove("md/%d", i);
5633 for (i=0; i < MAX_MD_DEVS; i++)
5634 devfs_remove("md/d%d", i);
5638 unregister_blkdev(MAJOR_NR,"md");
5639 unregister_blkdev(mdp_major, "mdp");
5640 unregister_reboot_notifier(&md_notifier);
5641 unregister_sysctl_table(raid_table_header);
5642 remove_proc_entry("mdstat", NULL);
5643 ITERATE_MDDEV(mddev,tmp) {
5644 struct gendisk *disk = mddev->gendisk;
5647 export_array(mddev);
5650 mddev->gendisk = NULL;
5655 module_init(md_init)
5656 module_exit(md_exit)
5658 static int get_ro(char *buffer, struct kernel_param *kp)
5660 return sprintf(buffer, "%d", start_readonly);
5662 static int set_ro(const char *val, struct kernel_param *kp)
5665 int num = simple_strtoul(val, &e, 10);
5666 if (*val && (*e == '\0' || *e == '\n')) {
5667 start_readonly = num;
5673 module_param_call(start_ro, set_ro, get_ro, NULL, 0600);
5674 module_param(start_dirty_degraded, int, 0644);
5677 EXPORT_SYMBOL(register_md_personality);
5678 EXPORT_SYMBOL(unregister_md_personality);
5679 EXPORT_SYMBOL(md_error);
5680 EXPORT_SYMBOL(md_done_sync);
5681 EXPORT_SYMBOL(md_write_start);
5682 EXPORT_SYMBOL(md_write_end);
5683 EXPORT_SYMBOL(md_register_thread);
5684 EXPORT_SYMBOL(md_unregister_thread);
5685 EXPORT_SYMBOL(md_wakeup_thread);
5686 EXPORT_SYMBOL(md_check_recovery);
5687 MODULE_LICENSE("GPL");
5689 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);