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/buffer_head.h> /* for invalidate_bdev */
43 #include <linux/suspend.h>
44 #include <linux/poll.h>
45 #include <linux/mutex.h>
46 #include <linux/ctype.h>
48 #include <linux/init.h>
50 #include <linux/file.h>
53 #include <linux/kmod.h>
56 #include <asm/unaligned.h>
58 #define MAJOR_NR MD_MAJOR
61 /* 63 partitions with the alternate major number (mdp) */
62 #define MdpMinorShift 6
65 #define dprintk(x...) ((void)(DEBUG && printk(x)))
69 static void autostart_arrays (int part);
72 static LIST_HEAD(pers_list);
73 static DEFINE_SPINLOCK(pers_lock);
75 static void md_print_devices(void);
77 #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
80 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
81 * is 1000 KB/sec, so the extra system load does not show up that much.
82 * Increase it if you want to have more _guaranteed_ speed. Note that
83 * the RAID driver will use the maximum available bandwidth if the IO
84 * subsystem is idle. There is also an 'absolute maximum' reconstruction
85 * speed limit - in case reconstruction slows down your system despite
88 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
89 * or /sys/block/mdX/md/sync_speed_{min,max}
92 static int sysctl_speed_limit_min = 1000;
93 static int sysctl_speed_limit_max = 200000;
94 static inline int speed_min(mddev_t *mddev)
96 return mddev->sync_speed_min ?
97 mddev->sync_speed_min : sysctl_speed_limit_min;
100 static inline int speed_max(mddev_t *mddev)
102 return mddev->sync_speed_max ?
103 mddev->sync_speed_max : sysctl_speed_limit_max;
106 static struct ctl_table_header *raid_table_header;
108 static ctl_table raid_table[] = {
110 .ctl_name = DEV_RAID_SPEED_LIMIT_MIN,
111 .procname = "speed_limit_min",
112 .data = &sysctl_speed_limit_min,
113 .maxlen = sizeof(int),
115 .proc_handler = &proc_dointvec,
118 .ctl_name = DEV_RAID_SPEED_LIMIT_MAX,
119 .procname = "speed_limit_max",
120 .data = &sysctl_speed_limit_max,
121 .maxlen = sizeof(int),
123 .proc_handler = &proc_dointvec,
128 static ctl_table raid_dir_table[] = {
130 .ctl_name = DEV_RAID,
139 static ctl_table raid_root_table[] = {
145 .child = raid_dir_table,
150 static struct block_device_operations md_fops;
152 static int start_readonly;
155 * We have a system wide 'event count' that is incremented
156 * on any 'interesting' event, and readers of /proc/mdstat
157 * can use 'poll' or 'select' to find out when the event
161 * start array, stop array, error, add device, remove device,
162 * start build, activate spare
164 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
165 static atomic_t md_event_count;
166 void md_new_event(mddev_t *mddev)
168 atomic_inc(&md_event_count);
169 wake_up(&md_event_waiters);
170 sysfs_notify(&mddev->kobj, NULL, "sync_action");
172 EXPORT_SYMBOL_GPL(md_new_event);
174 /* Alternate version that can be called from interrupts
175 * when calling sysfs_notify isn't needed.
177 static void md_new_event_inintr(mddev_t *mddev)
179 atomic_inc(&md_event_count);
180 wake_up(&md_event_waiters);
184 * Enables to iterate over all existing md arrays
185 * all_mddevs_lock protects this list.
187 static LIST_HEAD(all_mddevs);
188 static DEFINE_SPINLOCK(all_mddevs_lock);
192 * iterates through all used mddevs in the system.
193 * We take care to grab the all_mddevs_lock whenever navigating
194 * the list, and to always hold a refcount when unlocked.
195 * Any code which breaks out of this loop while own
196 * a reference to the current mddev and must mddev_put it.
198 #define ITERATE_MDDEV(mddev,tmp) \
200 for (({ spin_lock(&all_mddevs_lock); \
201 tmp = all_mddevs.next; \
203 ({ if (tmp != &all_mddevs) \
204 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
205 spin_unlock(&all_mddevs_lock); \
206 if (mddev) mddev_put(mddev); \
207 mddev = list_entry(tmp, mddev_t, all_mddevs); \
208 tmp != &all_mddevs;}); \
209 ({ spin_lock(&all_mddevs_lock); \
214 static int md_fail_request (request_queue_t *q, struct bio *bio)
216 bio_io_error(bio, bio->bi_size);
220 static inline mddev_t *mddev_get(mddev_t *mddev)
222 atomic_inc(&mddev->active);
226 static void mddev_put(mddev_t *mddev)
228 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
230 if (!mddev->raid_disks && list_empty(&mddev->disks)) {
231 list_del(&mddev->all_mddevs);
232 spin_unlock(&all_mddevs_lock);
233 blk_cleanup_queue(mddev->queue);
234 kobject_unregister(&mddev->kobj);
236 spin_unlock(&all_mddevs_lock);
239 static mddev_t * mddev_find(dev_t unit)
241 mddev_t *mddev, *new = NULL;
244 spin_lock(&all_mddevs_lock);
245 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
246 if (mddev->unit == unit) {
248 spin_unlock(&all_mddevs_lock);
254 list_add(&new->all_mddevs, &all_mddevs);
255 spin_unlock(&all_mddevs_lock);
258 spin_unlock(&all_mddevs_lock);
260 new = kzalloc(sizeof(*new), GFP_KERNEL);
265 if (MAJOR(unit) == MD_MAJOR)
266 new->md_minor = MINOR(unit);
268 new->md_minor = MINOR(unit) >> MdpMinorShift;
270 mutex_init(&new->reconfig_mutex);
271 INIT_LIST_HEAD(&new->disks);
272 INIT_LIST_HEAD(&new->all_mddevs);
273 init_timer(&new->safemode_timer);
274 atomic_set(&new->active, 1);
275 spin_lock_init(&new->write_lock);
276 init_waitqueue_head(&new->sb_wait);
278 new->queue = blk_alloc_queue(GFP_KERNEL);
283 set_bit(QUEUE_FLAG_CLUSTER, &new->queue->queue_flags);
285 blk_queue_make_request(new->queue, md_fail_request);
290 static inline int mddev_lock(mddev_t * mddev)
292 return mutex_lock_interruptible(&mddev->reconfig_mutex);
295 static inline int mddev_trylock(mddev_t * mddev)
297 return mutex_trylock(&mddev->reconfig_mutex);
300 static inline void mddev_unlock(mddev_t * mddev)
302 mutex_unlock(&mddev->reconfig_mutex);
304 md_wakeup_thread(mddev->thread);
307 static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
310 struct list_head *tmp;
312 ITERATE_RDEV(mddev,rdev,tmp) {
313 if (rdev->desc_nr == nr)
319 static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
321 struct list_head *tmp;
324 ITERATE_RDEV(mddev,rdev,tmp) {
325 if (rdev->bdev->bd_dev == dev)
331 static struct mdk_personality *find_pers(int level, char *clevel)
333 struct mdk_personality *pers;
334 list_for_each_entry(pers, &pers_list, list) {
335 if (level != LEVEL_NONE && pers->level == level)
337 if (strcmp(pers->name, clevel)==0)
343 static inline sector_t calc_dev_sboffset(struct block_device *bdev)
345 sector_t size = bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
346 return MD_NEW_SIZE_BLOCKS(size);
349 static sector_t calc_dev_size(mdk_rdev_t *rdev, unsigned chunk_size)
353 size = rdev->sb_offset;
356 size &= ~((sector_t)chunk_size/1024 - 1);
360 static int alloc_disk_sb(mdk_rdev_t * rdev)
365 rdev->sb_page = alloc_page(GFP_KERNEL);
366 if (!rdev->sb_page) {
367 printk(KERN_ALERT "md: out of memory.\n");
374 static void free_disk_sb(mdk_rdev_t * rdev)
377 put_page(rdev->sb_page);
379 rdev->sb_page = NULL;
386 static int super_written(struct bio *bio, unsigned int bytes_done, int error)
388 mdk_rdev_t *rdev = bio->bi_private;
389 mddev_t *mddev = rdev->mddev;
393 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags))
394 md_error(mddev, rdev);
396 if (atomic_dec_and_test(&mddev->pending_writes))
397 wake_up(&mddev->sb_wait);
402 static int super_written_barrier(struct bio *bio, unsigned int bytes_done, int error)
404 struct bio *bio2 = bio->bi_private;
405 mdk_rdev_t *rdev = bio2->bi_private;
406 mddev_t *mddev = rdev->mddev;
410 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
411 error == -EOPNOTSUPP) {
413 /* barriers don't appear to be supported :-( */
414 set_bit(BarriersNotsupp, &rdev->flags);
415 mddev->barriers_work = 0;
416 spin_lock_irqsave(&mddev->write_lock, flags);
417 bio2->bi_next = mddev->biolist;
418 mddev->biolist = bio2;
419 spin_unlock_irqrestore(&mddev->write_lock, flags);
420 wake_up(&mddev->sb_wait);
425 bio->bi_private = rdev;
426 return super_written(bio, bytes_done, error);
429 void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
430 sector_t sector, int size, struct page *page)
432 /* write first size bytes of page to sector of rdev
433 * Increment mddev->pending_writes before returning
434 * and decrement it on completion, waking up sb_wait
435 * if zero is reached.
436 * If an error occurred, call md_error
438 * As we might need to resubmit the request if BIO_RW_BARRIER
439 * causes ENOTSUPP, we allocate a spare bio...
441 struct bio *bio = bio_alloc(GFP_NOIO, 1);
442 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNC);
444 bio->bi_bdev = rdev->bdev;
445 bio->bi_sector = sector;
446 bio_add_page(bio, page, size, 0);
447 bio->bi_private = rdev;
448 bio->bi_end_io = super_written;
451 atomic_inc(&mddev->pending_writes);
452 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
454 rw |= (1<<BIO_RW_BARRIER);
455 rbio = bio_clone(bio, GFP_NOIO);
456 rbio->bi_private = bio;
457 rbio->bi_end_io = super_written_barrier;
458 submit_bio(rw, rbio);
463 void md_super_wait(mddev_t *mddev)
465 /* wait for all superblock writes that were scheduled to complete.
466 * if any had to be retried (due to BARRIER problems), retry them
470 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
471 if (atomic_read(&mddev->pending_writes)==0)
473 while (mddev->biolist) {
475 spin_lock_irq(&mddev->write_lock);
476 bio = mddev->biolist;
477 mddev->biolist = bio->bi_next ;
479 spin_unlock_irq(&mddev->write_lock);
480 submit_bio(bio->bi_rw, bio);
484 finish_wait(&mddev->sb_wait, &wq);
487 static int bi_complete(struct bio *bio, unsigned int bytes_done, int error)
492 complete((struct completion*)bio->bi_private);
496 int sync_page_io(struct block_device *bdev, sector_t sector, int size,
497 struct page *page, int rw)
499 struct bio *bio = bio_alloc(GFP_NOIO, 1);
500 struct completion event;
503 rw |= (1 << BIO_RW_SYNC);
506 bio->bi_sector = sector;
507 bio_add_page(bio, page, size, 0);
508 init_completion(&event);
509 bio->bi_private = &event;
510 bio->bi_end_io = bi_complete;
512 wait_for_completion(&event);
514 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
518 EXPORT_SYMBOL_GPL(sync_page_io);
520 static int read_disk_sb(mdk_rdev_t * rdev, int size)
522 char b[BDEVNAME_SIZE];
523 if (!rdev->sb_page) {
531 if (!sync_page_io(rdev->bdev, rdev->sb_offset<<1, size, rdev->sb_page, READ))
537 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
538 bdevname(rdev->bdev,b));
542 static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
544 if ( (sb1->set_uuid0 == sb2->set_uuid0) &&
545 (sb1->set_uuid1 == sb2->set_uuid1) &&
546 (sb1->set_uuid2 == sb2->set_uuid2) &&
547 (sb1->set_uuid3 == sb2->set_uuid3))
555 static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
558 mdp_super_t *tmp1, *tmp2;
560 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
561 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
563 if (!tmp1 || !tmp2) {
565 printk(KERN_INFO "md.c: sb1 is not equal to sb2!\n");
573 * nr_disks is not constant
578 if (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4))
589 static unsigned int calc_sb_csum(mdp_super_t * sb)
591 unsigned int disk_csum, csum;
593 disk_csum = sb->sb_csum;
595 csum = csum_partial((void *)sb, MD_SB_BYTES, 0);
596 sb->sb_csum = disk_csum;
602 * Handle superblock details.
603 * We want to be able to handle multiple superblock formats
604 * so we have a common interface to them all, and an array of
605 * different handlers.
606 * We rely on user-space to write the initial superblock, and support
607 * reading and updating of superblocks.
608 * Interface methods are:
609 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
610 * loads and validates a superblock on dev.
611 * if refdev != NULL, compare superblocks on both devices
613 * 0 - dev has a superblock that is compatible with refdev
614 * 1 - dev has a superblock that is compatible and newer than refdev
615 * so dev should be used as the refdev in future
616 * -EINVAL superblock incompatible or invalid
617 * -othererror e.g. -EIO
619 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
620 * Verify that dev is acceptable into mddev.
621 * The first time, mddev->raid_disks will be 0, and data from
622 * dev should be merged in. Subsequent calls check that dev
623 * is new enough. Return 0 or -EINVAL
625 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
626 * Update the superblock for rdev with data in mddev
627 * This does not write to disc.
633 struct module *owner;
634 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version);
635 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
636 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
640 * load_super for 0.90.0
642 static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
644 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
650 * Calculate the position of the superblock,
651 * it's at the end of the disk.
653 * It also happens to be a multiple of 4Kb.
655 sb_offset = calc_dev_sboffset(rdev->bdev);
656 rdev->sb_offset = sb_offset;
658 ret = read_disk_sb(rdev, MD_SB_BYTES);
663 bdevname(rdev->bdev, b);
664 sb = (mdp_super_t*)page_address(rdev->sb_page);
666 if (sb->md_magic != MD_SB_MAGIC) {
667 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
672 if (sb->major_version != 0 ||
673 sb->minor_version < 90 ||
674 sb->minor_version > 91) {
675 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
676 sb->major_version, sb->minor_version,
681 if (sb->raid_disks <= 0)
684 if (csum_fold(calc_sb_csum(sb)) != csum_fold(sb->sb_csum)) {
685 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
690 rdev->preferred_minor = sb->md_minor;
691 rdev->data_offset = 0;
692 rdev->sb_size = MD_SB_BYTES;
694 if (sb->level == LEVEL_MULTIPATH)
697 rdev->desc_nr = sb->this_disk.number;
703 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
704 if (!uuid_equal(refsb, sb)) {
705 printk(KERN_WARNING "md: %s has different UUID to %s\n",
706 b, bdevname(refdev->bdev,b2));
709 if (!sb_equal(refsb, sb)) {
710 printk(KERN_WARNING "md: %s has same UUID"
711 " but different superblock to %s\n",
712 b, bdevname(refdev->bdev, b2));
716 ev2 = md_event(refsb);
722 rdev->size = calc_dev_size(rdev, sb->chunk_size);
724 if (rdev->size < sb->size && sb->level > 1)
725 /* "this cannot possibly happen" ... */
733 * validate_super for 0.90.0
735 static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
738 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
739 __u64 ev1 = md_event(sb);
741 rdev->raid_disk = -1;
743 if (mddev->raid_disks == 0) {
744 mddev->major_version = 0;
745 mddev->minor_version = sb->minor_version;
746 mddev->patch_version = sb->patch_version;
747 mddev->persistent = ! sb->not_persistent;
748 mddev->chunk_size = sb->chunk_size;
749 mddev->ctime = sb->ctime;
750 mddev->utime = sb->utime;
751 mddev->level = sb->level;
752 mddev->clevel[0] = 0;
753 mddev->layout = sb->layout;
754 mddev->raid_disks = sb->raid_disks;
755 mddev->size = sb->size;
757 mddev->bitmap_offset = 0;
758 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
760 if (mddev->minor_version >= 91) {
761 mddev->reshape_position = sb->reshape_position;
762 mddev->delta_disks = sb->delta_disks;
763 mddev->new_level = sb->new_level;
764 mddev->new_layout = sb->new_layout;
765 mddev->new_chunk = sb->new_chunk;
767 mddev->reshape_position = MaxSector;
768 mddev->delta_disks = 0;
769 mddev->new_level = mddev->level;
770 mddev->new_layout = mddev->layout;
771 mddev->new_chunk = mddev->chunk_size;
774 if (sb->state & (1<<MD_SB_CLEAN))
775 mddev->recovery_cp = MaxSector;
777 if (sb->events_hi == sb->cp_events_hi &&
778 sb->events_lo == sb->cp_events_lo) {
779 mddev->recovery_cp = sb->recovery_cp;
781 mddev->recovery_cp = 0;
784 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
785 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
786 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
787 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
789 mddev->max_disks = MD_SB_DISKS;
791 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
792 mddev->bitmap_file == NULL) {
793 if (mddev->level != 1 && mddev->level != 4
794 && mddev->level != 5 && mddev->level != 6
795 && mddev->level != 10) {
796 /* FIXME use a better test */
797 printk(KERN_WARNING "md: bitmaps not supported for this level.\n");
800 mddev->bitmap_offset = mddev->default_bitmap_offset;
803 } else if (mddev->pers == NULL) {
804 /* Insist on good event counter while assembling */
806 if (ev1 < mddev->events)
808 } else if (mddev->bitmap) {
809 /* if adding to array with a bitmap, then we can accept an
810 * older device ... but not too old.
812 if (ev1 < mddev->bitmap->events_cleared)
815 if (ev1 < mddev->events)
816 /* just a hot-add of a new device, leave raid_disk at -1 */
820 if (mddev->level != LEVEL_MULTIPATH) {
821 desc = sb->disks + rdev->desc_nr;
823 if (desc->state & (1<<MD_DISK_FAULTY))
824 set_bit(Faulty, &rdev->flags);
825 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
826 desc->raid_disk < mddev->raid_disks */) {
827 set_bit(In_sync, &rdev->flags);
828 rdev->raid_disk = desc->raid_disk;
830 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
831 set_bit(WriteMostly, &rdev->flags);
832 } else /* MULTIPATH are always insync */
833 set_bit(In_sync, &rdev->flags);
838 * sync_super for 0.90.0
840 static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
843 struct list_head *tmp;
845 int next_spare = mddev->raid_disks;
848 /* make rdev->sb match mddev data..
851 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
852 * 3/ any empty disks < next_spare become removed
854 * disks[0] gets initialised to REMOVED because
855 * we cannot be sure from other fields if it has
856 * been initialised or not.
859 int active=0, working=0,failed=0,spare=0,nr_disks=0;
861 rdev->sb_size = MD_SB_BYTES;
863 sb = (mdp_super_t*)page_address(rdev->sb_page);
865 memset(sb, 0, sizeof(*sb));
867 sb->md_magic = MD_SB_MAGIC;
868 sb->major_version = mddev->major_version;
869 sb->patch_version = mddev->patch_version;
870 sb->gvalid_words = 0; /* ignored */
871 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
872 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
873 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
874 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
876 sb->ctime = mddev->ctime;
877 sb->level = mddev->level;
878 sb->size = mddev->size;
879 sb->raid_disks = mddev->raid_disks;
880 sb->md_minor = mddev->md_minor;
881 sb->not_persistent = !mddev->persistent;
882 sb->utime = mddev->utime;
884 sb->events_hi = (mddev->events>>32);
885 sb->events_lo = (u32)mddev->events;
887 if (mddev->reshape_position == MaxSector)
888 sb->minor_version = 90;
890 sb->minor_version = 91;
891 sb->reshape_position = mddev->reshape_position;
892 sb->new_level = mddev->new_level;
893 sb->delta_disks = mddev->delta_disks;
894 sb->new_layout = mddev->new_layout;
895 sb->new_chunk = mddev->new_chunk;
897 mddev->minor_version = sb->minor_version;
900 sb->recovery_cp = mddev->recovery_cp;
901 sb->cp_events_hi = (mddev->events>>32);
902 sb->cp_events_lo = (u32)mddev->events;
903 if (mddev->recovery_cp == MaxSector)
904 sb->state = (1<< MD_SB_CLEAN);
908 sb->layout = mddev->layout;
909 sb->chunk_size = mddev->chunk_size;
911 if (mddev->bitmap && mddev->bitmap_file == NULL)
912 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
914 sb->disks[0].state = (1<<MD_DISK_REMOVED);
915 ITERATE_RDEV(mddev,rdev2,tmp) {
918 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
919 && !test_bit(Faulty, &rdev2->flags))
920 desc_nr = rdev2->raid_disk;
922 desc_nr = next_spare++;
923 rdev2->desc_nr = desc_nr;
924 d = &sb->disks[rdev2->desc_nr];
926 d->number = rdev2->desc_nr;
927 d->major = MAJOR(rdev2->bdev->bd_dev);
928 d->minor = MINOR(rdev2->bdev->bd_dev);
929 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
930 && !test_bit(Faulty, &rdev2->flags))
931 d->raid_disk = rdev2->raid_disk;
933 d->raid_disk = rdev2->desc_nr; /* compatibility */
934 if (test_bit(Faulty, &rdev2->flags))
935 d->state = (1<<MD_DISK_FAULTY);
936 else if (test_bit(In_sync, &rdev2->flags)) {
937 d->state = (1<<MD_DISK_ACTIVE);
938 d->state |= (1<<MD_DISK_SYNC);
946 if (test_bit(WriteMostly, &rdev2->flags))
947 d->state |= (1<<MD_DISK_WRITEMOSTLY);
949 /* now set the "removed" and "faulty" bits on any missing devices */
950 for (i=0 ; i < mddev->raid_disks ; i++) {
951 mdp_disk_t *d = &sb->disks[i];
952 if (d->state == 0 && d->number == 0) {
955 d->state = (1<<MD_DISK_REMOVED);
956 d->state |= (1<<MD_DISK_FAULTY);
960 sb->nr_disks = nr_disks;
961 sb->active_disks = active;
962 sb->working_disks = working;
963 sb->failed_disks = failed;
964 sb->spare_disks = spare;
966 sb->this_disk = sb->disks[rdev->desc_nr];
967 sb->sb_csum = calc_sb_csum(sb);
971 * version 1 superblock
974 static unsigned int calc_sb_1_csum(struct mdp_superblock_1 * sb)
976 unsigned int disk_csum, csum;
977 unsigned long long newcsum;
978 int size = 256 + le32_to_cpu(sb->max_dev)*2;
979 unsigned int *isuper = (unsigned int*)sb;
982 disk_csum = sb->sb_csum;
985 for (i=0; size>=4; size -= 4 )
986 newcsum += le32_to_cpu(*isuper++);
989 newcsum += le16_to_cpu(*(unsigned short*) isuper);
991 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
992 sb->sb_csum = disk_csum;
993 return cpu_to_le32(csum);
996 static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
998 struct mdp_superblock_1 *sb;
1001 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1005 * Calculate the position of the superblock.
1006 * It is always aligned to a 4K boundary and
1007 * depeding on minor_version, it can be:
1008 * 0: At least 8K, but less than 12K, from end of device
1009 * 1: At start of device
1010 * 2: 4K from start of device.
1012 switch(minor_version) {
1014 sb_offset = rdev->bdev->bd_inode->i_size >> 9;
1016 sb_offset &= ~(sector_t)(4*2-1);
1017 /* convert from sectors to K */
1029 rdev->sb_offset = sb_offset;
1031 /* superblock is rarely larger than 1K, but it can be larger,
1032 * and it is safe to read 4k, so we do that
1034 ret = read_disk_sb(rdev, 4096);
1035 if (ret) return ret;
1038 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1040 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1041 sb->major_version != cpu_to_le32(1) ||
1042 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1043 le64_to_cpu(sb->super_offset) != (rdev->sb_offset<<1) ||
1044 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
1047 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1048 printk("md: invalid superblock checksum on %s\n",
1049 bdevname(rdev->bdev,b));
1052 if (le64_to_cpu(sb->data_size) < 10) {
1053 printk("md: data_size too small on %s\n",
1054 bdevname(rdev->bdev,b));
1057 rdev->preferred_minor = 0xffff;
1058 rdev->data_offset = le64_to_cpu(sb->data_offset);
1059 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
1061 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
1062 bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
1063 if (rdev->sb_size & bmask)
1064 rdev-> sb_size = (rdev->sb_size | bmask)+1;
1070 struct mdp_superblock_1 *refsb =
1071 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1073 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1074 sb->level != refsb->level ||
1075 sb->layout != refsb->layout ||
1076 sb->chunksize != refsb->chunksize) {
1077 printk(KERN_WARNING "md: %s has strangely different"
1078 " superblock to %s\n",
1079 bdevname(rdev->bdev,b),
1080 bdevname(refdev->bdev,b2));
1083 ev1 = le64_to_cpu(sb->events);
1084 ev2 = le64_to_cpu(refsb->events);
1092 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
1094 rdev->size = rdev->sb_offset;
1095 if (rdev->size < le64_to_cpu(sb->data_size)/2)
1097 rdev->size = le64_to_cpu(sb->data_size)/2;
1098 if (le32_to_cpu(sb->chunksize))
1099 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
1101 if (le32_to_cpu(sb->size) > rdev->size*2)
1106 static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1108 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1109 __u64 ev1 = le64_to_cpu(sb->events);
1111 rdev->raid_disk = -1;
1113 if (mddev->raid_disks == 0) {
1114 mddev->major_version = 1;
1115 mddev->patch_version = 0;
1116 mddev->persistent = 1;
1117 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
1118 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1119 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1120 mddev->level = le32_to_cpu(sb->level);
1121 mddev->clevel[0] = 0;
1122 mddev->layout = le32_to_cpu(sb->layout);
1123 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1124 mddev->size = le64_to_cpu(sb->size)/2;
1125 mddev->events = ev1;
1126 mddev->bitmap_offset = 0;
1127 mddev->default_bitmap_offset = 1024 >> 9;
1129 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1130 memcpy(mddev->uuid, sb->set_uuid, 16);
1132 mddev->max_disks = (4096-256)/2;
1134 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
1135 mddev->bitmap_file == NULL ) {
1136 if (mddev->level != 1 && mddev->level != 5 && mddev->level != 6
1137 && mddev->level != 10) {
1138 printk(KERN_WARNING "md: bitmaps not supported for this level.\n");
1141 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
1143 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1144 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1145 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1146 mddev->new_level = le32_to_cpu(sb->new_level);
1147 mddev->new_layout = le32_to_cpu(sb->new_layout);
1148 mddev->new_chunk = le32_to_cpu(sb->new_chunk)<<9;
1150 mddev->reshape_position = MaxSector;
1151 mddev->delta_disks = 0;
1152 mddev->new_level = mddev->level;
1153 mddev->new_layout = mddev->layout;
1154 mddev->new_chunk = mddev->chunk_size;
1157 } else if (mddev->pers == NULL) {
1158 /* Insist of good event counter while assembling */
1160 if (ev1 < mddev->events)
1162 } else if (mddev->bitmap) {
1163 /* If adding to array with a bitmap, then we can accept an
1164 * older device, but not too old.
1166 if (ev1 < mddev->bitmap->events_cleared)
1169 if (ev1 < mddev->events)
1170 /* just a hot-add of a new device, leave raid_disk at -1 */
1173 if (mddev->level != LEVEL_MULTIPATH) {
1175 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1176 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1178 case 0xffff: /* spare */
1180 case 0xfffe: /* faulty */
1181 set_bit(Faulty, &rdev->flags);
1184 if ((le32_to_cpu(sb->feature_map) &
1185 MD_FEATURE_RECOVERY_OFFSET))
1186 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1188 set_bit(In_sync, &rdev->flags);
1189 rdev->raid_disk = role;
1192 if (sb->devflags & WriteMostly1)
1193 set_bit(WriteMostly, &rdev->flags);
1194 } else /* MULTIPATH are always insync */
1195 set_bit(In_sync, &rdev->flags);
1200 static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1202 struct mdp_superblock_1 *sb;
1203 struct list_head *tmp;
1206 /* make rdev->sb match mddev and rdev data. */
1208 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1210 sb->feature_map = 0;
1212 sb->recovery_offset = cpu_to_le64(0);
1213 memset(sb->pad1, 0, sizeof(sb->pad1));
1214 memset(sb->pad2, 0, sizeof(sb->pad2));
1215 memset(sb->pad3, 0, sizeof(sb->pad3));
1217 sb->utime = cpu_to_le64((__u64)mddev->utime);
1218 sb->events = cpu_to_le64(mddev->events);
1220 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1222 sb->resync_offset = cpu_to_le64(0);
1224 sb->cnt_corrected_read = atomic_read(&rdev->corrected_errors);
1226 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
1227 sb->size = cpu_to_le64(mddev->size<<1);
1229 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1230 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
1231 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
1234 if (rdev->raid_disk >= 0 &&
1235 !test_bit(In_sync, &rdev->flags) &&
1236 rdev->recovery_offset > 0) {
1237 sb->feature_map |= cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1238 sb->recovery_offset = cpu_to_le64(rdev->recovery_offset);
1241 if (mddev->reshape_position != MaxSector) {
1242 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1243 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1244 sb->new_layout = cpu_to_le32(mddev->new_layout);
1245 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1246 sb->new_level = cpu_to_le32(mddev->new_level);
1247 sb->new_chunk = cpu_to_le32(mddev->new_chunk>>9);
1251 ITERATE_RDEV(mddev,rdev2,tmp)
1252 if (rdev2->desc_nr+1 > max_dev)
1253 max_dev = rdev2->desc_nr+1;
1255 sb->max_dev = cpu_to_le32(max_dev);
1256 for (i=0; i<max_dev;i++)
1257 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1259 ITERATE_RDEV(mddev,rdev2,tmp) {
1261 if (test_bit(Faulty, &rdev2->flags))
1262 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1263 else if (test_bit(In_sync, &rdev2->flags))
1264 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1265 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1266 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1268 sb->dev_roles[i] = cpu_to_le16(0xffff);
1271 sb->sb_csum = calc_sb_1_csum(sb);
1275 static struct super_type super_types[] = {
1278 .owner = THIS_MODULE,
1279 .load_super = super_90_load,
1280 .validate_super = super_90_validate,
1281 .sync_super = super_90_sync,
1285 .owner = THIS_MODULE,
1286 .load_super = super_1_load,
1287 .validate_super = super_1_validate,
1288 .sync_super = super_1_sync,
1292 static mdk_rdev_t * match_dev_unit(mddev_t *mddev, mdk_rdev_t *dev)
1294 struct list_head *tmp;
1297 ITERATE_RDEV(mddev,rdev,tmp)
1298 if (rdev->bdev->bd_contains == dev->bdev->bd_contains)
1304 static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1306 struct list_head *tmp;
1309 ITERATE_RDEV(mddev1,rdev,tmp)
1310 if (match_dev_unit(mddev2, rdev))
1316 static LIST_HEAD(pending_raid_disks);
1318 static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1320 mdk_rdev_t *same_pdev;
1321 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1329 /* make sure rdev->size exceeds mddev->size */
1330 if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) {
1332 /* Cannot change size, so fail */
1335 mddev->size = rdev->size;
1337 same_pdev = match_dev_unit(mddev, rdev);
1340 "%s: WARNING: %s appears to be on the same physical"
1341 " disk as %s. True\n protection against single-disk"
1342 " failure might be compromised.\n",
1343 mdname(mddev), bdevname(rdev->bdev,b),
1344 bdevname(same_pdev->bdev,b2));
1346 /* Verify rdev->desc_nr is unique.
1347 * If it is -1, assign a free number, else
1348 * check number is not in use
1350 if (rdev->desc_nr < 0) {
1352 if (mddev->pers) choice = mddev->raid_disks;
1353 while (find_rdev_nr(mddev, choice))
1355 rdev->desc_nr = choice;
1357 if (find_rdev_nr(mddev, rdev->desc_nr))
1360 bdevname(rdev->bdev,b);
1361 if (kobject_set_name(&rdev->kobj, "dev-%s", b) < 0)
1363 while ( (s=strchr(rdev->kobj.k_name, '/')) != NULL)
1366 list_add(&rdev->same_set, &mddev->disks);
1367 rdev->mddev = mddev;
1368 printk(KERN_INFO "md: bind<%s>\n", b);
1370 rdev->kobj.parent = &mddev->kobj;
1371 kobject_add(&rdev->kobj);
1373 if (rdev->bdev->bd_part)
1374 ko = &rdev->bdev->bd_part->kobj;
1376 ko = &rdev->bdev->bd_disk->kobj;
1377 sysfs_create_link(&rdev->kobj, ko, "block");
1378 bd_claim_by_disk(rdev->bdev, rdev, mddev->gendisk);
1382 static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1384 char b[BDEVNAME_SIZE];
1389 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
1390 list_del_init(&rdev->same_set);
1391 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1393 sysfs_remove_link(&rdev->kobj, "block");
1394 kobject_del(&rdev->kobj);
1398 * prevent the device from being mounted, repartitioned or
1399 * otherwise reused by a RAID array (or any other kernel
1400 * subsystem), by bd_claiming the device.
1402 static int lock_rdev(mdk_rdev_t *rdev, dev_t dev)
1405 struct block_device *bdev;
1406 char b[BDEVNAME_SIZE];
1408 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
1410 printk(KERN_ERR "md: could not open %s.\n",
1411 __bdevname(dev, b));
1412 return PTR_ERR(bdev);
1414 err = bd_claim(bdev, rdev);
1416 printk(KERN_ERR "md: could not bd_claim %s.\n",
1425 static void unlock_rdev(mdk_rdev_t *rdev)
1427 struct block_device *bdev = rdev->bdev;
1435 void md_autodetect_dev(dev_t dev);
1437 static void export_rdev(mdk_rdev_t * rdev)
1439 char b[BDEVNAME_SIZE];
1440 printk(KERN_INFO "md: export_rdev(%s)\n",
1441 bdevname(rdev->bdev,b));
1445 list_del_init(&rdev->same_set);
1447 md_autodetect_dev(rdev->bdev->bd_dev);
1450 kobject_put(&rdev->kobj);
1453 static void kick_rdev_from_array(mdk_rdev_t * rdev)
1455 unbind_rdev_from_array(rdev);
1459 static void export_array(mddev_t *mddev)
1461 struct list_head *tmp;
1464 ITERATE_RDEV(mddev,rdev,tmp) {
1469 kick_rdev_from_array(rdev);
1471 if (!list_empty(&mddev->disks))
1473 mddev->raid_disks = 0;
1474 mddev->major_version = 0;
1477 static void print_desc(mdp_disk_t *desc)
1479 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1480 desc->major,desc->minor,desc->raid_disk,desc->state);
1483 static void print_sb(mdp_super_t *sb)
1488 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1489 sb->major_version, sb->minor_version, sb->patch_version,
1490 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1492 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1493 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1494 sb->md_minor, sb->layout, sb->chunk_size);
1495 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1496 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1497 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1498 sb->failed_disks, sb->spare_disks,
1499 sb->sb_csum, (unsigned long)sb->events_lo);
1502 for (i = 0; i < MD_SB_DISKS; i++) {
1505 desc = sb->disks + i;
1506 if (desc->number || desc->major || desc->minor ||
1507 desc->raid_disk || (desc->state && (desc->state != 4))) {
1508 printk(" D %2d: ", i);
1512 printk(KERN_INFO "md: THIS: ");
1513 print_desc(&sb->this_disk);
1517 static void print_rdev(mdk_rdev_t *rdev)
1519 char b[BDEVNAME_SIZE];
1520 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1521 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
1522 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1524 if (rdev->sb_loaded) {
1525 printk(KERN_INFO "md: rdev superblock:\n");
1526 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1528 printk(KERN_INFO "md: no rdev superblock!\n");
1531 static void md_print_devices(void)
1533 struct list_head *tmp, *tmp2;
1536 char b[BDEVNAME_SIZE];
1539 printk("md: **********************************\n");
1540 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1541 printk("md: **********************************\n");
1542 ITERATE_MDDEV(mddev,tmp) {
1545 bitmap_print_sb(mddev->bitmap);
1547 printk("%s: ", mdname(mddev));
1548 ITERATE_RDEV(mddev,rdev,tmp2)
1549 printk("<%s>", bdevname(rdev->bdev,b));
1552 ITERATE_RDEV(mddev,rdev,tmp2)
1555 printk("md: **********************************\n");
1560 static void sync_sbs(mddev_t * mddev, int nospares)
1562 /* Update each superblock (in-memory image), but
1563 * if we are allowed to, skip spares which already
1564 * have the right event counter, or have one earlier
1565 * (which would mean they aren't being marked as dirty
1566 * with the rest of the array)
1569 struct list_head *tmp;
1571 ITERATE_RDEV(mddev,rdev,tmp) {
1572 if (rdev->sb_events == mddev->events ||
1574 rdev->raid_disk < 0 &&
1575 (rdev->sb_events&1)==0 &&
1576 rdev->sb_events+1 == mddev->events)) {
1577 /* Don't update this superblock */
1578 rdev->sb_loaded = 2;
1580 super_types[mddev->major_version].
1581 sync_super(mddev, rdev);
1582 rdev->sb_loaded = 1;
1587 void md_update_sb(mddev_t * mddev)
1590 struct list_head *tmp;
1596 spin_lock_irq(&mddev->write_lock);
1597 sync_req = mddev->in_sync;
1598 mddev->utime = get_seconds();
1599 if (mddev->sb_dirty == 3)
1600 /* just a clean<-> dirty transition, possibly leave spares alone,
1601 * though if events isn't the right even/odd, we will have to do
1606 /* If this is just a dirty<->clean transition, and the array is clean
1607 * and 'events' is odd, we can roll back to the previous clean state */
1608 if (mddev->sb_dirty == 3
1609 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
1610 && (mddev->events & 1))
1613 /* otherwise we have to go forward and ... */
1615 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
1616 /* .. if the array isn't clean, insist on an odd 'events' */
1617 if ((mddev->events&1)==0) {
1622 /* otherwise insist on an even 'events' (for clean states) */
1623 if ((mddev->events&1)) {
1630 if (!mddev->events) {
1632 * oops, this 64-bit counter should never wrap.
1633 * Either we are in around ~1 trillion A.C., assuming
1634 * 1 reboot per second, or we have a bug:
1639 mddev->sb_dirty = 2;
1640 sync_sbs(mddev, nospares);
1643 * do not write anything to disk if using
1644 * nonpersistent superblocks
1646 if (!mddev->persistent) {
1647 mddev->sb_dirty = 0;
1648 spin_unlock_irq(&mddev->write_lock);
1649 wake_up(&mddev->sb_wait);
1652 spin_unlock_irq(&mddev->write_lock);
1655 "md: updating %s RAID superblock on device (in sync %d)\n",
1656 mdname(mddev),mddev->in_sync);
1658 err = bitmap_update_sb(mddev->bitmap);
1659 ITERATE_RDEV(mddev,rdev,tmp) {
1660 char b[BDEVNAME_SIZE];
1661 dprintk(KERN_INFO "md: ");
1662 if (rdev->sb_loaded != 1)
1663 continue; /* no noise on spare devices */
1664 if (test_bit(Faulty, &rdev->flags))
1665 dprintk("(skipping faulty ");
1667 dprintk("%s ", bdevname(rdev->bdev,b));
1668 if (!test_bit(Faulty, &rdev->flags)) {
1669 md_super_write(mddev,rdev,
1670 rdev->sb_offset<<1, rdev->sb_size,
1672 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1673 bdevname(rdev->bdev,b),
1674 (unsigned long long)rdev->sb_offset);
1675 rdev->sb_events = mddev->events;
1679 if (mddev->level == LEVEL_MULTIPATH)
1680 /* only need to write one superblock... */
1683 md_super_wait(mddev);
1684 /* if there was a failure, sb_dirty was set to 1, and we re-write super */
1686 spin_lock_irq(&mddev->write_lock);
1687 if (mddev->in_sync != sync_req|| mddev->sb_dirty == 1) {
1688 /* have to write it out again */
1689 spin_unlock_irq(&mddev->write_lock);
1692 mddev->sb_dirty = 0;
1693 spin_unlock_irq(&mddev->write_lock);
1694 wake_up(&mddev->sb_wait);
1697 EXPORT_SYMBOL_GPL(md_update_sb);
1699 /* words written to sysfs files may, or my not, be \n terminated.
1700 * We want to accept with case. For this we use cmd_match.
1702 static int cmd_match(const char *cmd, const char *str)
1704 /* See if cmd, written into a sysfs file, matches
1705 * str. They must either be the same, or cmd can
1706 * have a trailing newline
1708 while (*cmd && *str && *cmd == *str) {
1719 struct rdev_sysfs_entry {
1720 struct attribute attr;
1721 ssize_t (*show)(mdk_rdev_t *, char *);
1722 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1726 state_show(mdk_rdev_t *rdev, char *page)
1731 if (test_bit(Faulty, &rdev->flags)) {
1732 len+= sprintf(page+len, "%sfaulty",sep);
1735 if (test_bit(In_sync, &rdev->flags)) {
1736 len += sprintf(page+len, "%sin_sync",sep);
1739 if (test_bit(WriteMostly, &rdev->flags)) {
1740 len += sprintf(page+len, "%swrite_mostly",sep);
1743 if (!test_bit(Faulty, &rdev->flags) &&
1744 !test_bit(In_sync, &rdev->flags)) {
1745 len += sprintf(page+len, "%sspare", sep);
1748 return len+sprintf(page+len, "\n");
1752 state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1755 * faulty - simulates and error
1756 * remove - disconnects the device
1757 * writemostly - sets write_mostly
1758 * -writemostly - clears write_mostly
1761 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
1762 md_error(rdev->mddev, rdev);
1764 } else if (cmd_match(buf, "remove")) {
1765 if (rdev->raid_disk >= 0)
1768 mddev_t *mddev = rdev->mddev;
1769 kick_rdev_from_array(rdev);
1770 md_update_sb(mddev);
1771 md_new_event(mddev);
1774 } else if (cmd_match(buf, "writemostly")) {
1775 set_bit(WriteMostly, &rdev->flags);
1777 } else if (cmd_match(buf, "-writemostly")) {
1778 clear_bit(WriteMostly, &rdev->flags);
1781 return err ? err : len;
1783 static struct rdev_sysfs_entry
1784 rdev_state = __ATTR(state, 0644, state_show, state_store);
1787 super_show(mdk_rdev_t *rdev, char *page)
1789 if (rdev->sb_loaded && rdev->sb_size) {
1790 memcpy(page, page_address(rdev->sb_page), rdev->sb_size);
1791 return rdev->sb_size;
1795 static struct rdev_sysfs_entry rdev_super = __ATTR_RO(super);
1798 errors_show(mdk_rdev_t *rdev, char *page)
1800 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
1804 errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1807 unsigned long n = simple_strtoul(buf, &e, 10);
1808 if (*buf && (*e == 0 || *e == '\n')) {
1809 atomic_set(&rdev->corrected_errors, n);
1814 static struct rdev_sysfs_entry rdev_errors =
1815 __ATTR(errors, 0644, errors_show, errors_store);
1818 slot_show(mdk_rdev_t *rdev, char *page)
1820 if (rdev->raid_disk < 0)
1821 return sprintf(page, "none\n");
1823 return sprintf(page, "%d\n", rdev->raid_disk);
1827 slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1830 int slot = simple_strtoul(buf, &e, 10);
1831 if (strncmp(buf, "none", 4)==0)
1833 else if (e==buf || (*e && *e!= '\n'))
1835 if (rdev->mddev->pers)
1836 /* Cannot set slot in active array (yet) */
1838 if (slot >= rdev->mddev->raid_disks)
1840 rdev->raid_disk = slot;
1841 /* assume it is working */
1843 set_bit(In_sync, &rdev->flags);
1848 static struct rdev_sysfs_entry rdev_slot =
1849 __ATTR(slot, 0644, slot_show, slot_store);
1852 offset_show(mdk_rdev_t *rdev, char *page)
1854 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
1858 offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1861 unsigned long long offset = simple_strtoull(buf, &e, 10);
1862 if (e==buf || (*e && *e != '\n'))
1864 if (rdev->mddev->pers)
1866 rdev->data_offset = offset;
1870 static struct rdev_sysfs_entry rdev_offset =
1871 __ATTR(offset, 0644, offset_show, offset_store);
1874 rdev_size_show(mdk_rdev_t *rdev, char *page)
1876 return sprintf(page, "%llu\n", (unsigned long long)rdev->size);
1880 rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1883 unsigned long long size = simple_strtoull(buf, &e, 10);
1884 if (e==buf || (*e && *e != '\n'))
1886 if (rdev->mddev->pers)
1889 if (size < rdev->mddev->size || rdev->mddev->size == 0)
1890 rdev->mddev->size = size;
1894 static struct rdev_sysfs_entry rdev_size =
1895 __ATTR(size, 0644, rdev_size_show, rdev_size_store);
1897 static struct attribute *rdev_default_attrs[] = {
1907 rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
1909 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1910 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1914 return entry->show(rdev, page);
1918 rdev_attr_store(struct kobject *kobj, struct attribute *attr,
1919 const char *page, size_t length)
1921 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1922 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1926 return entry->store(rdev, page, length);
1929 static void rdev_free(struct kobject *ko)
1931 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
1934 static struct sysfs_ops rdev_sysfs_ops = {
1935 .show = rdev_attr_show,
1936 .store = rdev_attr_store,
1938 static struct kobj_type rdev_ktype = {
1939 .release = rdev_free,
1940 .sysfs_ops = &rdev_sysfs_ops,
1941 .default_attrs = rdev_default_attrs,
1945 * Import a device. If 'super_format' >= 0, then sanity check the superblock
1947 * mark the device faulty if:
1949 * - the device is nonexistent (zero size)
1950 * - the device has no valid superblock
1952 * a faulty rdev _never_ has rdev->sb set.
1954 static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
1956 char b[BDEVNAME_SIZE];
1961 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
1963 printk(KERN_ERR "md: could not alloc mem for new device!\n");
1964 return ERR_PTR(-ENOMEM);
1967 if ((err = alloc_disk_sb(rdev)))
1970 err = lock_rdev(rdev, newdev);
1974 rdev->kobj.parent = NULL;
1975 rdev->kobj.ktype = &rdev_ktype;
1976 kobject_init(&rdev->kobj);
1980 rdev->data_offset = 0;
1981 rdev->sb_events = 0;
1982 atomic_set(&rdev->nr_pending, 0);
1983 atomic_set(&rdev->read_errors, 0);
1984 atomic_set(&rdev->corrected_errors, 0);
1986 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
1989 "md: %s has zero or unknown size, marking faulty!\n",
1990 bdevname(rdev->bdev,b));
1995 if (super_format >= 0) {
1996 err = super_types[super_format].
1997 load_super(rdev, NULL, super_minor);
1998 if (err == -EINVAL) {
2000 "md: %s has invalid sb, not importing!\n",
2001 bdevname(rdev->bdev,b));
2006 "md: could not read %s's sb, not importing!\n",
2007 bdevname(rdev->bdev,b));
2011 INIT_LIST_HEAD(&rdev->same_set);
2016 if (rdev->sb_page) {
2022 return ERR_PTR(err);
2026 * Check a full RAID array for plausibility
2030 static void analyze_sbs(mddev_t * mddev)
2033 struct list_head *tmp;
2034 mdk_rdev_t *rdev, *freshest;
2035 char b[BDEVNAME_SIZE];
2038 ITERATE_RDEV(mddev,rdev,tmp)
2039 switch (super_types[mddev->major_version].
2040 load_super(rdev, freshest, mddev->minor_version)) {
2048 "md: fatal superblock inconsistency in %s"
2049 " -- removing from array\n",
2050 bdevname(rdev->bdev,b));
2051 kick_rdev_from_array(rdev);
2055 super_types[mddev->major_version].
2056 validate_super(mddev, freshest);
2059 ITERATE_RDEV(mddev,rdev,tmp) {
2060 if (rdev != freshest)
2061 if (super_types[mddev->major_version].
2062 validate_super(mddev, rdev)) {
2063 printk(KERN_WARNING "md: kicking non-fresh %s"
2065 bdevname(rdev->bdev,b));
2066 kick_rdev_from_array(rdev);
2069 if (mddev->level == LEVEL_MULTIPATH) {
2070 rdev->desc_nr = i++;
2071 rdev->raid_disk = rdev->desc_nr;
2072 set_bit(In_sync, &rdev->flags);
2078 if (mddev->recovery_cp != MaxSector &&
2080 printk(KERN_ERR "md: %s: raid array is not clean"
2081 " -- starting background reconstruction\n",
2087 safe_delay_show(mddev_t *mddev, char *page)
2089 int msec = (mddev->safemode_delay*1000)/HZ;
2090 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2093 safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2101 /* remove a period, and count digits after it */
2102 if (len >= sizeof(buf))
2104 strlcpy(buf, cbuf, len);
2106 for (i=0; i<len; i++) {
2108 if (isdigit(buf[i])) {
2113 } else if (buf[i] == '.') {
2118 msec = simple_strtoul(buf, &e, 10);
2119 if (e == buf || (*e && *e != '\n'))
2121 msec = (msec * 1000) / scale;
2123 mddev->safemode_delay = 0;
2125 mddev->safemode_delay = (msec*HZ)/1000;
2126 if (mddev->safemode_delay == 0)
2127 mddev->safemode_delay = 1;
2131 static struct md_sysfs_entry md_safe_delay =
2132 __ATTR(safe_mode_delay, 0644,safe_delay_show, safe_delay_store);
2135 level_show(mddev_t *mddev, char *page)
2137 struct mdk_personality *p = mddev->pers;
2139 return sprintf(page, "%s\n", p->name);
2140 else if (mddev->clevel[0])
2141 return sprintf(page, "%s\n", mddev->clevel);
2142 else if (mddev->level != LEVEL_NONE)
2143 return sprintf(page, "%d\n", mddev->level);
2149 level_store(mddev_t *mddev, const char *buf, size_t len)
2156 if (len >= sizeof(mddev->clevel))
2158 strncpy(mddev->clevel, buf, len);
2159 if (mddev->clevel[len-1] == '\n')
2161 mddev->clevel[len] = 0;
2162 mddev->level = LEVEL_NONE;
2166 static struct md_sysfs_entry md_level =
2167 __ATTR(level, 0644, level_show, level_store);
2171 layout_show(mddev_t *mddev, char *page)
2173 /* just a number, not meaningful for all levels */
2174 return sprintf(page, "%d\n", mddev->layout);
2178 layout_store(mddev_t *mddev, const char *buf, size_t len)
2181 unsigned long n = simple_strtoul(buf, &e, 10);
2185 if (!*buf || (*e && *e != '\n'))
2191 static struct md_sysfs_entry md_layout =
2192 __ATTR(layout, 0655, layout_show, layout_store);
2196 raid_disks_show(mddev_t *mddev, char *page)
2198 if (mddev->raid_disks == 0)
2200 return sprintf(page, "%d\n", mddev->raid_disks);
2203 static int update_raid_disks(mddev_t *mddev, int raid_disks);
2206 raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2208 /* can only set raid_disks if array is not yet active */
2211 unsigned long n = simple_strtoul(buf, &e, 10);
2213 if (!*buf || (*e && *e != '\n'))
2217 rv = update_raid_disks(mddev, n);
2219 mddev->raid_disks = n;
2220 return rv ? rv : len;
2222 static struct md_sysfs_entry md_raid_disks =
2223 __ATTR(raid_disks, 0644, raid_disks_show, raid_disks_store);
2226 chunk_size_show(mddev_t *mddev, char *page)
2228 return sprintf(page, "%d\n", mddev->chunk_size);
2232 chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2234 /* can only set chunk_size if array is not yet active */
2236 unsigned long n = simple_strtoul(buf, &e, 10);
2240 if (!*buf || (*e && *e != '\n'))
2243 mddev->chunk_size = n;
2246 static struct md_sysfs_entry md_chunk_size =
2247 __ATTR(chunk_size, 0644, chunk_size_show, chunk_size_store);
2250 resync_start_show(mddev_t *mddev, char *page)
2252 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2256 resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2258 /* can only set chunk_size if array is not yet active */
2260 unsigned long long n = simple_strtoull(buf, &e, 10);
2264 if (!*buf || (*e && *e != '\n'))
2267 mddev->recovery_cp = n;
2270 static struct md_sysfs_entry md_resync_start =
2271 __ATTR(resync_start, 0644, resync_start_show, resync_start_store);
2274 * The array state can be:
2277 * No devices, no size, no level
2278 * Equivalent to STOP_ARRAY ioctl
2280 * May have some settings, but array is not active
2281 * all IO results in error
2282 * When written, doesn't tear down array, but just stops it
2283 * suspended (not supported yet)
2284 * All IO requests will block. The array can be reconfigured.
2285 * Writing this, if accepted, will block until array is quiessent
2287 * no resync can happen. no superblocks get written.
2288 * write requests fail
2290 * like readonly, but behaves like 'clean' on a write request.
2292 * clean - no pending writes, but otherwise active.
2293 * When written to inactive array, starts without resync
2294 * If a write request arrives then
2295 * if metadata is known, mark 'dirty' and switch to 'active'.
2296 * if not known, block and switch to write-pending
2297 * If written to an active array that has pending writes, then fails.
2299 * fully active: IO and resync can be happening.
2300 * When written to inactive array, starts with resync
2303 * clean, but writes are blocked waiting for 'active' to be written.
2306 * like active, but no writes have been seen for a while (100msec).
2309 enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
2310 write_pending, active_idle, bad_word};
2311 static char *array_states[] = {
2312 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2313 "write-pending", "active-idle", NULL };
2315 static int match_word(const char *word, char **list)
2318 for (n=0; list[n]; n++)
2319 if (cmd_match(word, list[n]))
2325 array_state_show(mddev_t *mddev, char *page)
2327 enum array_state st = inactive;
2340 else if (mddev->safemode)
2346 if (list_empty(&mddev->disks) &&
2347 mddev->raid_disks == 0 &&
2353 return sprintf(page, "%s\n", array_states[st]);
2356 static int do_md_stop(mddev_t * mddev, int ro);
2357 static int do_md_run(mddev_t * mddev);
2358 static int restart_array(mddev_t *mddev);
2361 array_state_store(mddev_t *mddev, const char *buf, size_t len)
2364 enum array_state st = match_word(buf, array_states);
2369 /* stopping an active array */
2371 if (atomic_read(&mddev->active) > 1)
2373 err = do_md_stop(mddev, 0);
2377 /* stopping an active array */
2379 if (atomic_read(&mddev->active) > 1)
2381 err = do_md_stop(mddev, 2);
2385 break; /* not supported yet */
2388 err = do_md_stop(mddev, 1);
2391 err = do_md_run(mddev);
2395 /* stopping an active array */
2397 err = do_md_stop(mddev, 1);
2399 mddev->ro = 2; /* FIXME mark devices writable */
2402 err = do_md_run(mddev);
2407 restart_array(mddev);
2408 spin_lock_irq(&mddev->write_lock);
2409 if (atomic_read(&mddev->writes_pending) == 0) {
2411 mddev->sb_dirty = 1;
2413 spin_unlock_irq(&mddev->write_lock);
2416 mddev->recovery_cp = MaxSector;
2417 err = do_md_run(mddev);
2422 restart_array(mddev);
2423 mddev->sb_dirty = 0;
2424 wake_up(&mddev->sb_wait);
2428 err = do_md_run(mddev);
2433 /* these cannot be set */
2441 static struct md_sysfs_entry md_array_state = __ATTR(array_state, 0644, array_state_show, array_state_store);
2444 null_show(mddev_t *mddev, char *page)
2450 new_dev_store(mddev_t *mddev, const char *buf, size_t len)
2452 /* buf must be %d:%d\n? giving major and minor numbers */
2453 /* The new device is added to the array.
2454 * If the array has a persistent superblock, we read the
2455 * superblock to initialise info and check validity.
2456 * Otherwise, only checking done is that in bind_rdev_to_array,
2457 * which mainly checks size.
2460 int major = simple_strtoul(buf, &e, 10);
2466 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
2468 minor = simple_strtoul(e+1, &e, 10);
2469 if (*e && *e != '\n')
2471 dev = MKDEV(major, minor);
2472 if (major != MAJOR(dev) ||
2473 minor != MINOR(dev))
2477 if (mddev->persistent) {
2478 rdev = md_import_device(dev, mddev->major_version,
2479 mddev->minor_version);
2480 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
2481 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2482 mdk_rdev_t, same_set);
2483 err = super_types[mddev->major_version]
2484 .load_super(rdev, rdev0, mddev->minor_version);
2489 rdev = md_import_device(dev, -1, -1);
2492 return PTR_ERR(rdev);
2493 err = bind_rdev_to_array(rdev, mddev);
2497 return err ? err : len;
2500 static struct md_sysfs_entry md_new_device =
2501 __ATTR(new_dev, 0200, null_show, new_dev_store);
2504 size_show(mddev_t *mddev, char *page)
2506 return sprintf(page, "%llu\n", (unsigned long long)mddev->size);
2509 static int update_size(mddev_t *mddev, unsigned long size);
2512 size_store(mddev_t *mddev, const char *buf, size_t len)
2514 /* If array is inactive, we can reduce the component size, but
2515 * not increase it (except from 0).
2516 * If array is active, we can try an on-line resize
2520 unsigned long long size = simple_strtoull(buf, &e, 10);
2521 if (!*buf || *buf == '\n' ||
2526 err = update_size(mddev, size);
2527 md_update_sb(mddev);
2529 if (mddev->size == 0 ||
2535 return err ? err : len;
2538 static struct md_sysfs_entry md_size =
2539 __ATTR(component_size, 0644, size_show, size_store);
2543 * This is either 'none' for arrays with externally managed metadata,
2544 * or N.M for internally known formats
2547 metadata_show(mddev_t *mddev, char *page)
2549 if (mddev->persistent)
2550 return sprintf(page, "%d.%d\n",
2551 mddev->major_version, mddev->minor_version);
2553 return sprintf(page, "none\n");
2557 metadata_store(mddev_t *mddev, const char *buf, size_t len)
2561 if (!list_empty(&mddev->disks))
2564 if (cmd_match(buf, "none")) {
2565 mddev->persistent = 0;
2566 mddev->major_version = 0;
2567 mddev->minor_version = 90;
2570 major = simple_strtoul(buf, &e, 10);
2571 if (e==buf || *e != '.')
2574 minor = simple_strtoul(buf, &e, 10);
2575 if (e==buf || *e != '\n')
2577 if (major >= sizeof(super_types)/sizeof(super_types[0]) ||
2578 super_types[major].name == NULL)
2580 mddev->major_version = major;
2581 mddev->minor_version = minor;
2582 mddev->persistent = 1;
2586 static struct md_sysfs_entry md_metadata =
2587 __ATTR(metadata_version, 0644, metadata_show, metadata_store);
2590 action_show(mddev_t *mddev, char *page)
2592 char *type = "idle";
2593 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2594 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) {
2595 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2597 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2598 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2600 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2607 return sprintf(page, "%s\n", type);
2611 action_store(mddev_t *mddev, const char *page, size_t len)
2613 if (!mddev->pers || !mddev->pers->sync_request)
2616 if (cmd_match(page, "idle")) {
2617 if (mddev->sync_thread) {
2618 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2619 md_unregister_thread(mddev->sync_thread);
2620 mddev->sync_thread = NULL;
2621 mddev->recovery = 0;
2623 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2624 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
2626 else if (cmd_match(page, "resync") || cmd_match(page, "recover"))
2627 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2628 else if (cmd_match(page, "reshape")) {
2630 if (mddev->pers->start_reshape == NULL)
2632 err = mddev->pers->start_reshape(mddev);
2636 if (cmd_match(page, "check"))
2637 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
2638 else if (!cmd_match(page, "repair"))
2640 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
2641 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
2643 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2644 md_wakeup_thread(mddev->thread);
2649 mismatch_cnt_show(mddev_t *mddev, char *page)
2651 return sprintf(page, "%llu\n",
2652 (unsigned long long) mddev->resync_mismatches);
2655 static struct md_sysfs_entry
2656 md_scan_mode = __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
2659 static struct md_sysfs_entry
2660 md_mismatches = __ATTR_RO(mismatch_cnt);
2663 sync_min_show(mddev_t *mddev, char *page)
2665 return sprintf(page, "%d (%s)\n", speed_min(mddev),
2666 mddev->sync_speed_min ? "local": "system");
2670 sync_min_store(mddev_t *mddev, const char *buf, size_t len)
2674 if (strncmp(buf, "system", 6)==0) {
2675 mddev->sync_speed_min = 0;
2678 min = simple_strtoul(buf, &e, 10);
2679 if (buf == e || (*e && *e != '\n') || min <= 0)
2681 mddev->sync_speed_min = min;
2685 static struct md_sysfs_entry md_sync_min =
2686 __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
2689 sync_max_show(mddev_t *mddev, char *page)
2691 return sprintf(page, "%d (%s)\n", speed_max(mddev),
2692 mddev->sync_speed_max ? "local": "system");
2696 sync_max_store(mddev_t *mddev, const char *buf, size_t len)
2700 if (strncmp(buf, "system", 6)==0) {
2701 mddev->sync_speed_max = 0;
2704 max = simple_strtoul(buf, &e, 10);
2705 if (buf == e || (*e && *e != '\n') || max <= 0)
2707 mddev->sync_speed_max = max;
2711 static struct md_sysfs_entry md_sync_max =
2712 __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
2716 sync_speed_show(mddev_t *mddev, char *page)
2718 unsigned long resync, dt, db;
2719 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
2720 dt = ((jiffies - mddev->resync_mark) / HZ);
2722 db = resync - (mddev->resync_mark_cnt);
2723 return sprintf(page, "%ld\n", db/dt/2); /* K/sec */
2726 static struct md_sysfs_entry
2727 md_sync_speed = __ATTR_RO(sync_speed);
2730 sync_completed_show(mddev_t *mddev, char *page)
2732 unsigned long max_blocks, resync;
2734 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2735 max_blocks = mddev->resync_max_sectors;
2737 max_blocks = mddev->size << 1;
2739 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
2740 return sprintf(page, "%lu / %lu\n", resync, max_blocks);
2743 static struct md_sysfs_entry
2744 md_sync_completed = __ATTR_RO(sync_completed);
2747 suspend_lo_show(mddev_t *mddev, char *page)
2749 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
2753 suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
2756 unsigned long long new = simple_strtoull(buf, &e, 10);
2758 if (mddev->pers->quiesce == NULL)
2760 if (buf == e || (*e && *e != '\n'))
2762 if (new >= mddev->suspend_hi ||
2763 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
2764 mddev->suspend_lo = new;
2765 mddev->pers->quiesce(mddev, 2);
2770 static struct md_sysfs_entry md_suspend_lo =
2771 __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
2775 suspend_hi_show(mddev_t *mddev, char *page)
2777 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
2781 suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
2784 unsigned long long new = simple_strtoull(buf, &e, 10);
2786 if (mddev->pers->quiesce == NULL)
2788 if (buf == e || (*e && *e != '\n'))
2790 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
2791 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
2792 mddev->suspend_hi = new;
2793 mddev->pers->quiesce(mddev, 1);
2794 mddev->pers->quiesce(mddev, 0);
2799 static struct md_sysfs_entry md_suspend_hi =
2800 __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
2803 static struct attribute *md_default_attrs[] = {
2806 &md_raid_disks.attr,
2807 &md_chunk_size.attr,
2809 &md_resync_start.attr,
2811 &md_new_device.attr,
2812 &md_safe_delay.attr,
2813 &md_array_state.attr,
2817 static struct attribute *md_redundancy_attrs[] = {
2819 &md_mismatches.attr,
2822 &md_sync_speed.attr,
2823 &md_sync_completed.attr,
2824 &md_suspend_lo.attr,
2825 &md_suspend_hi.attr,
2828 static struct attribute_group md_redundancy_group = {
2830 .attrs = md_redundancy_attrs,
2835 md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2837 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
2838 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
2843 rv = mddev_lock(mddev);
2845 rv = entry->show(mddev, page);
2846 mddev_unlock(mddev);
2852 md_attr_store(struct kobject *kobj, struct attribute *attr,
2853 const char *page, size_t length)
2855 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
2856 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
2861 rv = mddev_lock(mddev);
2863 rv = entry->store(mddev, page, length);
2864 mddev_unlock(mddev);
2869 static void md_free(struct kobject *ko)
2871 mddev_t *mddev = container_of(ko, mddev_t, kobj);
2875 static struct sysfs_ops md_sysfs_ops = {
2876 .show = md_attr_show,
2877 .store = md_attr_store,
2879 static struct kobj_type md_ktype = {
2881 .sysfs_ops = &md_sysfs_ops,
2882 .default_attrs = md_default_attrs,
2887 static struct kobject *md_probe(dev_t dev, int *part, void *data)
2889 static DEFINE_MUTEX(disks_mutex);
2890 mddev_t *mddev = mddev_find(dev);
2891 struct gendisk *disk;
2892 int partitioned = (MAJOR(dev) != MD_MAJOR);
2893 int shift = partitioned ? MdpMinorShift : 0;
2894 int unit = MINOR(dev) >> shift;
2899 mutex_lock(&disks_mutex);
2900 if (mddev->gendisk) {
2901 mutex_unlock(&disks_mutex);
2905 disk = alloc_disk(1 << shift);
2907 mutex_unlock(&disks_mutex);
2911 disk->major = MAJOR(dev);
2912 disk->first_minor = unit << shift;
2914 sprintf(disk->disk_name, "md_d%d", unit);
2915 sprintf(disk->devfs_name, "md/d%d", unit);
2917 sprintf(disk->disk_name, "md%d", unit);
2918 sprintf(disk->devfs_name, "md/%d", unit);
2920 disk->fops = &md_fops;
2921 disk->private_data = mddev;
2922 disk->queue = mddev->queue;
2924 mddev->gendisk = disk;
2925 mutex_unlock(&disks_mutex);
2926 mddev->kobj.parent = &disk->kobj;
2927 mddev->kobj.k_name = NULL;
2928 snprintf(mddev->kobj.name, KOBJ_NAME_LEN, "%s", "md");
2929 mddev->kobj.ktype = &md_ktype;
2930 kobject_register(&mddev->kobj);
2934 static void md_safemode_timeout(unsigned long data)
2936 mddev_t *mddev = (mddev_t *) data;
2938 mddev->safemode = 1;
2939 md_wakeup_thread(mddev->thread);
2942 static int start_dirty_degraded;
2944 static int do_md_run(mddev_t * mddev)
2948 struct list_head *tmp;
2950 struct gendisk *disk;
2951 struct mdk_personality *pers;
2952 char b[BDEVNAME_SIZE];
2954 if (list_empty(&mddev->disks))
2955 /* cannot run an array with no devices.. */
2962 * Analyze all RAID superblock(s)
2964 if (!mddev->raid_disks)
2967 chunk_size = mddev->chunk_size;
2970 if (chunk_size > MAX_CHUNK_SIZE) {
2971 printk(KERN_ERR "too big chunk_size: %d > %d\n",
2972 chunk_size, MAX_CHUNK_SIZE);
2976 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
2978 if ( (1 << ffz(~chunk_size)) != chunk_size) {
2979 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
2982 if (chunk_size < PAGE_SIZE) {
2983 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
2984 chunk_size, PAGE_SIZE);
2988 /* devices must have minimum size of one chunk */
2989 ITERATE_RDEV(mddev,rdev,tmp) {
2990 if (test_bit(Faulty, &rdev->flags))
2992 if (rdev->size < chunk_size / 1024) {
2994 "md: Dev %s smaller than chunk_size:"
2996 bdevname(rdev->bdev,b),
2997 (unsigned long long)rdev->size,
3005 if (mddev->level != LEVEL_NONE)
3006 request_module("md-level-%d", mddev->level);
3007 else if (mddev->clevel[0])
3008 request_module("md-%s", mddev->clevel);
3012 * Drop all container device buffers, from now on
3013 * the only valid external interface is through the md
3015 * Also find largest hardsector size
3017 ITERATE_RDEV(mddev,rdev,tmp) {
3018 if (test_bit(Faulty, &rdev->flags))
3020 sync_blockdev(rdev->bdev);
3021 invalidate_bdev(rdev->bdev, 0);
3024 md_probe(mddev->unit, NULL, NULL);
3025 disk = mddev->gendisk;
3029 spin_lock(&pers_lock);
3030 pers = find_pers(mddev->level, mddev->clevel);
3031 if (!pers || !try_module_get(pers->owner)) {
3032 spin_unlock(&pers_lock);
3033 if (mddev->level != LEVEL_NONE)
3034 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
3037 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
3042 spin_unlock(&pers_lock);
3043 mddev->level = pers->level;
3044 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3046 if (mddev->reshape_position != MaxSector &&
3047 pers->start_reshape == NULL) {
3048 /* This personality cannot handle reshaping... */
3050 module_put(pers->owner);
3054 mddev->recovery = 0;
3055 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
3056 mddev->barriers_work = 1;
3057 mddev->ok_start_degraded = start_dirty_degraded;
3060 mddev->ro = 2; /* read-only, but switch on first write */
3062 err = mddev->pers->run(mddev);
3063 if (!err && mddev->pers->sync_request) {
3064 err = bitmap_create(mddev);
3066 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
3067 mdname(mddev), err);
3068 mddev->pers->stop(mddev);
3072 printk(KERN_ERR "md: pers->run() failed ...\n");
3073 module_put(mddev->pers->owner);
3075 bitmap_destroy(mddev);
3078 if (mddev->pers->sync_request)
3079 sysfs_create_group(&mddev->kobj, &md_redundancy_group);
3080 else if (mddev->ro == 2) /* auto-readonly not meaningful */
3083 atomic_set(&mddev->writes_pending,0);
3084 mddev->safemode = 0;
3085 mddev->safemode_timer.function = md_safemode_timeout;
3086 mddev->safemode_timer.data = (unsigned long) mddev;
3087 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
3090 ITERATE_RDEV(mddev,rdev,tmp)
3091 if (rdev->raid_disk >= 0) {
3093 sprintf(nm, "rd%d", rdev->raid_disk);
3094 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
3097 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3098 md_wakeup_thread(mddev->thread);
3100 if (mddev->sb_dirty)
3101 md_update_sb(mddev);
3103 set_capacity(disk, mddev->array_size<<1);
3105 /* If we call blk_queue_make_request here, it will
3106 * re-initialise max_sectors etc which may have been
3107 * refined inside -> run. So just set the bits we need to set.
3108 * Most initialisation happended when we called
3109 * blk_queue_make_request(..., md_fail_request)
3112 mddev->queue->queuedata = mddev;
3113 mddev->queue->make_request_fn = mddev->pers->make_request;
3115 /* If there is a partially-recovered drive we need to
3116 * start recovery here. If we leave it to md_check_recovery,
3117 * it will remove the drives and not do the right thing
3119 if (mddev->degraded) {
3120 struct list_head *rtmp;
3122 ITERATE_RDEV(mddev,rdev,rtmp)
3123 if (rdev->raid_disk >= 0 &&
3124 !test_bit(In_sync, &rdev->flags) &&
3125 !test_bit(Faulty, &rdev->flags))
3126 /* complete an interrupted recovery */
3128 if (spares && mddev->pers->sync_request) {
3129 mddev->recovery = 0;
3130 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3131 mddev->sync_thread = md_register_thread(md_do_sync,
3134 if (!mddev->sync_thread) {
3135 printk(KERN_ERR "%s: could not start resync"
3138 /* leave the spares where they are, it shouldn't hurt */
3139 mddev->recovery = 0;
3141 md_wakeup_thread(mddev->sync_thread);
3146 md_new_event(mddev);
3150 static int restart_array(mddev_t *mddev)
3152 struct gendisk *disk = mddev->gendisk;
3156 * Complain if it has no devices
3159 if (list_empty(&mddev->disks))
3167 mddev->safemode = 0;
3169 set_disk_ro(disk, 0);
3171 printk(KERN_INFO "md: %s switched to read-write mode.\n",
3174 * Kick recovery or resync if necessary
3176 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3177 md_wakeup_thread(mddev->thread);
3178 md_wakeup_thread(mddev->sync_thread);
3187 /* similar to deny_write_access, but accounts for our holding a reference
3188 * to the file ourselves */
3189 static int deny_bitmap_write_access(struct file * file)
3191 struct inode *inode = file->f_mapping->host;
3193 spin_lock(&inode->i_lock);
3194 if (atomic_read(&inode->i_writecount) > 1) {
3195 spin_unlock(&inode->i_lock);
3198 atomic_set(&inode->i_writecount, -1);
3199 spin_unlock(&inode->i_lock);
3204 static void restore_bitmap_write_access(struct file *file)
3206 struct inode *inode = file->f_mapping->host;
3208 spin_lock(&inode->i_lock);
3209 atomic_set(&inode->i_writecount, 1);
3210 spin_unlock(&inode->i_lock);
3214 * 0 - completely stop and dis-assemble array
3215 * 1 - switch to readonly
3216 * 2 - stop but do not disassemble array
3218 static int do_md_stop(mddev_t * mddev, int mode)
3221 struct gendisk *disk = mddev->gendisk;
3224 if (atomic_read(&mddev->active)>2) {
3225 printk("md: %s still in use.\n",mdname(mddev));
3229 if (mddev->sync_thread) {
3230 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3231 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3232 md_unregister_thread(mddev->sync_thread);
3233 mddev->sync_thread = NULL;
3236 del_timer_sync(&mddev->safemode_timer);
3238 invalidate_partition(disk, 0);
3241 case 1: /* readonly */
3247 case 0: /* disassemble */
3249 bitmap_flush(mddev);
3250 md_super_wait(mddev);
3252 set_disk_ro(disk, 0);
3253 blk_queue_make_request(mddev->queue, md_fail_request);
3254 mddev->pers->stop(mddev);
3255 if (mddev->pers->sync_request)
3256 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
3258 module_put(mddev->pers->owner);
3263 if (!mddev->in_sync || mddev->sb_dirty) {
3264 /* mark array as shutdown cleanly */
3266 md_update_sb(mddev);
3269 set_disk_ro(disk, 1);
3270 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3274 * Free resources if final stop
3278 struct list_head *tmp;
3279 struct gendisk *disk;
3280 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
3282 bitmap_destroy(mddev);
3283 if (mddev->bitmap_file) {
3284 restore_bitmap_write_access(mddev->bitmap_file);
3285 fput(mddev->bitmap_file);
3286 mddev->bitmap_file = NULL;
3288 mddev->bitmap_offset = 0;
3290 ITERATE_RDEV(mddev,rdev,tmp)
3291 if (rdev->raid_disk >= 0) {
3293 sprintf(nm, "rd%d", rdev->raid_disk);
3294 sysfs_remove_link(&mddev->kobj, nm);
3297 export_array(mddev);
3299 mddev->array_size = 0;
3301 mddev->raid_disks = 0;
3302 mddev->recovery_cp = 0;
3304 disk = mddev->gendisk;
3306 set_capacity(disk, 0);
3308 } else if (mddev->pers)
3309 printk(KERN_INFO "md: %s switched to read-only mode.\n",
3312 md_new_event(mddev);
3317 static void autorun_array(mddev_t *mddev)
3320 struct list_head *tmp;
3323 if (list_empty(&mddev->disks))
3326 printk(KERN_INFO "md: running: ");
3328 ITERATE_RDEV(mddev,rdev,tmp) {
3329 char b[BDEVNAME_SIZE];
3330 printk("<%s>", bdevname(rdev->bdev,b));
3334 err = do_md_run (mddev);
3336 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
3337 do_md_stop (mddev, 0);
3342 * lets try to run arrays based on all disks that have arrived
3343 * until now. (those are in pending_raid_disks)
3345 * the method: pick the first pending disk, collect all disks with
3346 * the same UUID, remove all from the pending list and put them into
3347 * the 'same_array' list. Then order this list based on superblock
3348 * update time (freshest comes first), kick out 'old' disks and
3349 * compare superblocks. If everything's fine then run it.
3351 * If "unit" is allocated, then bump its reference count
3353 static void autorun_devices(int part)
3355 struct list_head *tmp;
3356 mdk_rdev_t *rdev0, *rdev;
3358 char b[BDEVNAME_SIZE];
3360 printk(KERN_INFO "md: autorun ...\n");
3361 while (!list_empty(&pending_raid_disks)) {
3363 LIST_HEAD(candidates);
3364 rdev0 = list_entry(pending_raid_disks.next,
3365 mdk_rdev_t, same_set);
3367 printk(KERN_INFO "md: considering %s ...\n",
3368 bdevname(rdev0->bdev,b));
3369 INIT_LIST_HEAD(&candidates);
3370 ITERATE_RDEV_PENDING(rdev,tmp)
3371 if (super_90_load(rdev, rdev0, 0) >= 0) {
3372 printk(KERN_INFO "md: adding %s ...\n",
3373 bdevname(rdev->bdev,b));
3374 list_move(&rdev->same_set, &candidates);
3377 * now we have a set of devices, with all of them having
3378 * mostly sane superblocks. It's time to allocate the
3381 if (rdev0->preferred_minor < 0 || rdev0->preferred_minor >= MAX_MD_DEVS) {
3382 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
3383 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
3387 dev = MKDEV(mdp_major,
3388 rdev0->preferred_minor << MdpMinorShift);
3390 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
3392 md_probe(dev, NULL, NULL);
3393 mddev = mddev_find(dev);
3396 "md: cannot allocate memory for md drive.\n");
3399 if (mddev_lock(mddev))
3400 printk(KERN_WARNING "md: %s locked, cannot run\n",
3402 else if (mddev->raid_disks || mddev->major_version
3403 || !list_empty(&mddev->disks)) {
3405 "md: %s already running, cannot run %s\n",
3406 mdname(mddev), bdevname(rdev0->bdev,b));
3407 mddev_unlock(mddev);
3409 printk(KERN_INFO "md: created %s\n", mdname(mddev));
3410 ITERATE_RDEV_GENERIC(candidates,rdev,tmp) {
3411 list_del_init(&rdev->same_set);
3412 if (bind_rdev_to_array(rdev, mddev))
3415 autorun_array(mddev);
3416 mddev_unlock(mddev);
3418 /* on success, candidates will be empty, on error
3421 ITERATE_RDEV_GENERIC(candidates,rdev,tmp)
3425 printk(KERN_INFO "md: ... autorun DONE.\n");
3429 * import RAID devices based on one partition
3430 * if possible, the array gets run as well.
3433 static int autostart_array(dev_t startdev)
3435 char b[BDEVNAME_SIZE];
3436 int err = -EINVAL, i;
3437 mdp_super_t *sb = NULL;
3438 mdk_rdev_t *start_rdev = NULL, *rdev;
3440 start_rdev = md_import_device(startdev, 0, 0);
3441 if (IS_ERR(start_rdev))
3445 /* NOTE: this can only work for 0.90.0 superblocks */
3446 sb = (mdp_super_t*)page_address(start_rdev->sb_page);
3447 if (sb->major_version != 0 ||
3448 sb->minor_version != 90 ) {
3449 printk(KERN_WARNING "md: can only autostart 0.90.0 arrays\n");
3450 export_rdev(start_rdev);
3454 if (test_bit(Faulty, &start_rdev->flags)) {
3456 "md: can not autostart based on faulty %s!\n",
3457 bdevname(start_rdev->bdev,b));
3458 export_rdev(start_rdev);
3461 list_add(&start_rdev->same_set, &pending_raid_disks);
3463 for (i = 0; i < MD_SB_DISKS; i++) {
3464 mdp_disk_t *desc = sb->disks + i;
3465 dev_t dev = MKDEV(desc->major, desc->minor);
3469 if (dev == startdev)
3471 if (MAJOR(dev) != desc->major || MINOR(dev) != desc->minor)
3473 rdev = md_import_device(dev, 0, 0);
3477 list_add(&rdev->same_set, &pending_raid_disks);
3481 * possibly return codes
3489 static int get_version(void __user * arg)
3493 ver.major = MD_MAJOR_VERSION;
3494 ver.minor = MD_MINOR_VERSION;
3495 ver.patchlevel = MD_PATCHLEVEL_VERSION;
3497 if (copy_to_user(arg, &ver, sizeof(ver)))
3503 static int get_array_info(mddev_t * mddev, void __user * arg)
3505 mdu_array_info_t info;
3506 int nr,working,active,failed,spare;
3508 struct list_head *tmp;
3510 nr=working=active=failed=spare=0;
3511 ITERATE_RDEV(mddev,rdev,tmp) {
3513 if (test_bit(Faulty, &rdev->flags))
3517 if (test_bit(In_sync, &rdev->flags))
3524 info.major_version = mddev->major_version;
3525 info.minor_version = mddev->minor_version;
3526 info.patch_version = MD_PATCHLEVEL_VERSION;
3527 info.ctime = mddev->ctime;
3528 info.level = mddev->level;
3529 info.size = mddev->size;
3530 if (info.size != mddev->size) /* overflow */
3533 info.raid_disks = mddev->raid_disks;
3534 info.md_minor = mddev->md_minor;
3535 info.not_persistent= !mddev->persistent;
3537 info.utime = mddev->utime;
3540 info.state = (1<<MD_SB_CLEAN);
3541 if (mddev->bitmap && mddev->bitmap_offset)
3542 info.state = (1<<MD_SB_BITMAP_PRESENT);
3543 info.active_disks = active;
3544 info.working_disks = working;
3545 info.failed_disks = failed;
3546 info.spare_disks = spare;
3548 info.layout = mddev->layout;
3549 info.chunk_size = mddev->chunk_size;
3551 if (copy_to_user(arg, &info, sizeof(info)))
3557 static int get_bitmap_file(mddev_t * mddev, void __user * arg)
3559 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
3560 char *ptr, *buf = NULL;
3563 file = kmalloc(sizeof(*file), GFP_KERNEL);
3567 /* bitmap disabled, zero the first byte and copy out */
3568 if (!mddev->bitmap || !mddev->bitmap->file) {
3569 file->pathname[0] = '\0';
3573 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
3577 ptr = file_path(mddev->bitmap->file, buf, sizeof(file->pathname));
3581 strcpy(file->pathname, ptr);
3585 if (copy_to_user(arg, file, sizeof(*file)))
3593 static int get_disk_info(mddev_t * mddev, void __user * arg)
3595 mdu_disk_info_t info;
3599 if (copy_from_user(&info, arg, sizeof(info)))
3604 rdev = find_rdev_nr(mddev, nr);
3606 info.major = MAJOR(rdev->bdev->bd_dev);
3607 info.minor = MINOR(rdev->bdev->bd_dev);
3608 info.raid_disk = rdev->raid_disk;
3610 if (test_bit(Faulty, &rdev->flags))
3611 info.state |= (1<<MD_DISK_FAULTY);
3612 else if (test_bit(In_sync, &rdev->flags)) {
3613 info.state |= (1<<MD_DISK_ACTIVE);
3614 info.state |= (1<<MD_DISK_SYNC);
3616 if (test_bit(WriteMostly, &rdev->flags))
3617 info.state |= (1<<MD_DISK_WRITEMOSTLY);
3619 info.major = info.minor = 0;
3620 info.raid_disk = -1;
3621 info.state = (1<<MD_DISK_REMOVED);
3624 if (copy_to_user(arg, &info, sizeof(info)))
3630 static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
3632 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3634 dev_t dev = MKDEV(info->major,info->minor);
3636 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
3639 if (!mddev->raid_disks) {
3641 /* expecting a device which has a superblock */
3642 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
3645 "md: md_import_device returned %ld\n",
3647 return PTR_ERR(rdev);
3649 if (!list_empty(&mddev->disks)) {
3650 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3651 mdk_rdev_t, same_set);
3652 int err = super_types[mddev->major_version]
3653 .load_super(rdev, rdev0, mddev->minor_version);
3656 "md: %s has different UUID to %s\n",
3657 bdevname(rdev->bdev,b),
3658 bdevname(rdev0->bdev,b2));
3663 err = bind_rdev_to_array(rdev, mddev);
3670 * add_new_disk can be used once the array is assembled
3671 * to add "hot spares". They must already have a superblock
3676 if (!mddev->pers->hot_add_disk) {
3678 "%s: personality does not support diskops!\n",
3682 if (mddev->persistent)
3683 rdev = md_import_device(dev, mddev->major_version,
3684 mddev->minor_version);
3686 rdev = md_import_device(dev, -1, -1);
3689 "md: md_import_device returned %ld\n",
3691 return PTR_ERR(rdev);
3693 /* set save_raid_disk if appropriate */
3694 if (!mddev->persistent) {
3695 if (info->state & (1<<MD_DISK_SYNC) &&
3696 info->raid_disk < mddev->raid_disks)
3697 rdev->raid_disk = info->raid_disk;
3699 rdev->raid_disk = -1;
3701 super_types[mddev->major_version].
3702 validate_super(mddev, rdev);
3703 rdev->saved_raid_disk = rdev->raid_disk;
3705 clear_bit(In_sync, &rdev->flags); /* just to be sure */
3706 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
3707 set_bit(WriteMostly, &rdev->flags);
3709 rdev->raid_disk = -1;
3710 err = bind_rdev_to_array(rdev, mddev);
3711 if (!err && !mddev->pers->hot_remove_disk) {
3712 /* If there is hot_add_disk but no hot_remove_disk
3713 * then added disks for geometry changes,
3714 * and should be added immediately.
3716 super_types[mddev->major_version].
3717 validate_super(mddev, rdev);
3718 err = mddev->pers->hot_add_disk(mddev, rdev);
3720 unbind_rdev_from_array(rdev);
3725 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3726 md_wakeup_thread(mddev->thread);
3730 /* otherwise, add_new_disk is only allowed
3731 * for major_version==0 superblocks
3733 if (mddev->major_version != 0) {
3734 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
3739 if (!(info->state & (1<<MD_DISK_FAULTY))) {
3741 rdev = md_import_device (dev, -1, 0);
3744 "md: error, md_import_device() returned %ld\n",
3746 return PTR_ERR(rdev);
3748 rdev->desc_nr = info->number;
3749 if (info->raid_disk < mddev->raid_disks)
3750 rdev->raid_disk = info->raid_disk;
3752 rdev->raid_disk = -1;
3756 if (rdev->raid_disk < mddev->raid_disks)
3757 if (info->state & (1<<MD_DISK_SYNC))
3758 set_bit(In_sync, &rdev->flags);
3760 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
3761 set_bit(WriteMostly, &rdev->flags);
3763 if (!mddev->persistent) {
3764 printk(KERN_INFO "md: nonpersistent superblock ...\n");
3765 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
3767 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
3768 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
3770 err = bind_rdev_to_array(rdev, mddev);
3780 static int hot_remove_disk(mddev_t * mddev, dev_t dev)
3782 char b[BDEVNAME_SIZE];
3788 rdev = find_rdev(mddev, dev);
3792 if (rdev->raid_disk >= 0)
3795 kick_rdev_from_array(rdev);
3796 md_update_sb(mddev);
3797 md_new_event(mddev);
3801 printk(KERN_WARNING "md: cannot remove active disk %s from %s ... \n",
3802 bdevname(rdev->bdev,b), mdname(mddev));
3806 static int hot_add_disk(mddev_t * mddev, dev_t dev)
3808 char b[BDEVNAME_SIZE];
3816 if (mddev->major_version != 0) {
3817 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
3818 " version-0 superblocks.\n",
3822 if (!mddev->pers->hot_add_disk) {
3824 "%s: personality does not support diskops!\n",
3829 rdev = md_import_device (dev, -1, 0);
3832 "md: error, md_import_device() returned %ld\n",
3837 if (mddev->persistent)
3838 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
3841 rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
3843 size = calc_dev_size(rdev, mddev->chunk_size);
3846 if (test_bit(Faulty, &rdev->flags)) {
3848 "md: can not hot-add faulty %s disk to %s!\n",
3849 bdevname(rdev->bdev,b), mdname(mddev));
3853 clear_bit(In_sync, &rdev->flags);
3855 err = bind_rdev_to_array(rdev, mddev);
3860 * The rest should better be atomic, we can have disk failures
3861 * noticed in interrupt contexts ...
3864 if (rdev->desc_nr == mddev->max_disks) {
3865 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
3868 goto abort_unbind_export;
3871 rdev->raid_disk = -1;
3873 md_update_sb(mddev);
3876 * Kick recovery, maybe this spare has to be added to the
3877 * array immediately.
3879 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3880 md_wakeup_thread(mddev->thread);
3881 md_new_event(mddev);
3884 abort_unbind_export:
3885 unbind_rdev_from_array(rdev);
3892 static int set_bitmap_file(mddev_t *mddev, int fd)
3897 if (!mddev->pers->quiesce)
3899 if (mddev->recovery || mddev->sync_thread)
3901 /* we should be able to change the bitmap.. */
3907 return -EEXIST; /* cannot add when bitmap is present */
3908 mddev->bitmap_file = fget(fd);
3910 if (mddev->bitmap_file == NULL) {
3911 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
3916 err = deny_bitmap_write_access(mddev->bitmap_file);
3918 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
3920 fput(mddev->bitmap_file);
3921 mddev->bitmap_file = NULL;
3924 mddev->bitmap_offset = 0; /* file overrides offset */
3925 } else if (mddev->bitmap == NULL)
3926 return -ENOENT; /* cannot remove what isn't there */
3929 mddev->pers->quiesce(mddev, 1);
3931 err = bitmap_create(mddev);
3932 if (fd < 0 || err) {
3933 bitmap_destroy(mddev);
3934 fd = -1; /* make sure to put the file */
3936 mddev->pers->quiesce(mddev, 0);
3939 if (mddev->bitmap_file) {
3940 restore_bitmap_write_access(mddev->bitmap_file);
3941 fput(mddev->bitmap_file);
3943 mddev->bitmap_file = NULL;
3950 * set_array_info is used two different ways
3951 * The original usage is when creating a new array.
3952 * In this usage, raid_disks is > 0 and it together with
3953 * level, size, not_persistent,layout,chunksize determine the
3954 * shape of the array.
3955 * This will always create an array with a type-0.90.0 superblock.
3956 * The newer usage is when assembling an array.
3957 * In this case raid_disks will be 0, and the major_version field is
3958 * use to determine which style super-blocks are to be found on the devices.
3959 * The minor and patch _version numbers are also kept incase the
3960 * super_block handler wishes to interpret them.
3962 static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
3965 if (info->raid_disks == 0) {
3966 /* just setting version number for superblock loading */
3967 if (info->major_version < 0 ||
3968 info->major_version >= sizeof(super_types)/sizeof(super_types[0]) ||
3969 super_types[info->major_version].name == NULL) {
3970 /* maybe try to auto-load a module? */
3972 "md: superblock version %d not known\n",
3973 info->major_version);
3976 mddev->major_version = info->major_version;
3977 mddev->minor_version = info->minor_version;
3978 mddev->patch_version = info->patch_version;
3981 mddev->major_version = MD_MAJOR_VERSION;
3982 mddev->minor_version = MD_MINOR_VERSION;
3983 mddev->patch_version = MD_PATCHLEVEL_VERSION;
3984 mddev->ctime = get_seconds();
3986 mddev->level = info->level;
3987 mddev->clevel[0] = 0;
3988 mddev->size = info->size;
3989 mddev->raid_disks = info->raid_disks;
3990 /* don't set md_minor, it is determined by which /dev/md* was
3993 if (info->state & (1<<MD_SB_CLEAN))
3994 mddev->recovery_cp = MaxSector;
3996 mddev->recovery_cp = 0;
3997 mddev->persistent = ! info->not_persistent;
3999 mddev->layout = info->layout;
4000 mddev->chunk_size = info->chunk_size;
4002 mddev->max_disks = MD_SB_DISKS;
4004 mddev->sb_dirty = 1;
4006 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
4007 mddev->bitmap_offset = 0;
4009 mddev->reshape_position = MaxSector;
4012 * Generate a 128 bit UUID
4014 get_random_bytes(mddev->uuid, 16);
4016 mddev->new_level = mddev->level;
4017 mddev->new_chunk = mddev->chunk_size;
4018 mddev->new_layout = mddev->layout;
4019 mddev->delta_disks = 0;
4024 static int update_size(mddev_t *mddev, unsigned long size)
4028 struct list_head *tmp;
4029 int fit = (size == 0);
4031 if (mddev->pers->resize == NULL)
4033 /* The "size" is the amount of each device that is used.
4034 * This can only make sense for arrays with redundancy.
4035 * linear and raid0 always use whatever space is available
4036 * We can only consider changing the size if no resync
4037 * or reconstruction is happening, and if the new size
4038 * is acceptable. It must fit before the sb_offset or,
4039 * if that is <data_offset, it must fit before the
4040 * size of each device.
4041 * If size is zero, we find the largest size that fits.
4043 if (mddev->sync_thread)
4045 ITERATE_RDEV(mddev,rdev,tmp) {
4047 if (rdev->sb_offset > rdev->data_offset)
4048 avail = (rdev->sb_offset*2) - rdev->data_offset;
4050 avail = get_capacity(rdev->bdev->bd_disk)
4051 - rdev->data_offset;
4052 if (fit && (size == 0 || size > avail/2))
4054 if (avail < ((sector_t)size << 1))
4057 rv = mddev->pers->resize(mddev, (sector_t)size *2);
4059 struct block_device *bdev;
4061 bdev = bdget_disk(mddev->gendisk, 0);
4063 mutex_lock(&bdev->bd_inode->i_mutex);
4064 i_size_write(bdev->bd_inode, (loff_t)mddev->array_size << 10);
4065 mutex_unlock(&bdev->bd_inode->i_mutex);
4072 static int update_raid_disks(mddev_t *mddev, int raid_disks)
4075 /* change the number of raid disks */
4076 if (mddev->pers->check_reshape == NULL)
4078 if (raid_disks <= 0 ||
4079 raid_disks >= mddev->max_disks)
4081 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
4083 mddev->delta_disks = raid_disks - mddev->raid_disks;
4085 rv = mddev->pers->check_reshape(mddev);
4091 * update_array_info is used to change the configuration of an
4093 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
4094 * fields in the info are checked against the array.
4095 * Any differences that cannot be handled will cause an error.
4096 * Normally, only one change can be managed at a time.
4098 static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
4104 /* calculate expected state,ignoring low bits */
4105 if (mddev->bitmap && mddev->bitmap_offset)
4106 state |= (1 << MD_SB_BITMAP_PRESENT);
4108 if (mddev->major_version != info->major_version ||
4109 mddev->minor_version != info->minor_version ||
4110 /* mddev->patch_version != info->patch_version || */
4111 mddev->ctime != info->ctime ||
4112 mddev->level != info->level ||
4113 /* mddev->layout != info->layout || */
4114 !mddev->persistent != info->not_persistent||
4115 mddev->chunk_size != info->chunk_size ||
4116 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
4117 ((state^info->state) & 0xfffffe00)
4120 /* Check there is only one change */
4121 if (info->size >= 0 && mddev->size != info->size) cnt++;
4122 if (mddev->raid_disks != info->raid_disks) cnt++;
4123 if (mddev->layout != info->layout) cnt++;
4124 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
4125 if (cnt == 0) return 0;
4126 if (cnt > 1) return -EINVAL;
4128 if (mddev->layout != info->layout) {
4130 * we don't need to do anything at the md level, the
4131 * personality will take care of it all.
4133 if (mddev->pers->reconfig == NULL)
4136 return mddev->pers->reconfig(mddev, info->layout, -1);
4138 if (info->size >= 0 && mddev->size != info->size)
4139 rv = update_size(mddev, info->size);
4141 if (mddev->raid_disks != info->raid_disks)
4142 rv = update_raid_disks(mddev, info->raid_disks);
4144 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
4145 if (mddev->pers->quiesce == NULL)
4147 if (mddev->recovery || mddev->sync_thread)
4149 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
4150 /* add the bitmap */
4153 if (mddev->default_bitmap_offset == 0)
4155 mddev->bitmap_offset = mddev->default_bitmap_offset;
4156 mddev->pers->quiesce(mddev, 1);
4157 rv = bitmap_create(mddev);
4159 bitmap_destroy(mddev);
4160 mddev->pers->quiesce(mddev, 0);
4162 /* remove the bitmap */
4165 if (mddev->bitmap->file)
4167 mddev->pers->quiesce(mddev, 1);
4168 bitmap_destroy(mddev);
4169 mddev->pers->quiesce(mddev, 0);
4170 mddev->bitmap_offset = 0;
4173 md_update_sb(mddev);
4177 static int set_disk_faulty(mddev_t *mddev, dev_t dev)
4181 if (mddev->pers == NULL)
4184 rdev = find_rdev(mddev, dev);
4188 md_error(mddev, rdev);
4192 static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4194 mddev_t *mddev = bdev->bd_disk->private_data;
4198 geo->cylinders = get_capacity(mddev->gendisk) / 8;
4202 static int md_ioctl(struct inode *inode, struct file *file,
4203 unsigned int cmd, unsigned long arg)
4206 void __user *argp = (void __user *)arg;
4207 mddev_t *mddev = NULL;
4209 if (!capable(CAP_SYS_ADMIN))
4213 * Commands dealing with the RAID driver but not any
4219 err = get_version(argp);
4222 case PRINT_RAID_DEBUG:
4230 autostart_arrays(arg);
4237 * Commands creating/starting a new array:
4240 mddev = inode->i_bdev->bd_disk->private_data;
4248 if (cmd == START_ARRAY) {
4249 /* START_ARRAY doesn't need to lock the array as autostart_array
4250 * does the locking, and it could even be a different array
4255 "md: %s(pid %d) used deprecated START_ARRAY ioctl. "
4256 "This will not be supported beyond July 2006\n",
4257 current->comm, current->pid);
4260 err = autostart_array(new_decode_dev(arg));
4262 printk(KERN_WARNING "md: autostart failed!\n");
4268 err = mddev_lock(mddev);
4271 "md: ioctl lock interrupted, reason %d, cmd %d\n",
4278 case SET_ARRAY_INFO:
4280 mdu_array_info_t info;
4282 memset(&info, 0, sizeof(info));
4283 else if (copy_from_user(&info, argp, sizeof(info))) {
4288 err = update_array_info(mddev, &info);
4290 printk(KERN_WARNING "md: couldn't update"
4291 " array info. %d\n", err);
4296 if (!list_empty(&mddev->disks)) {
4298 "md: array %s already has disks!\n",
4303 if (mddev->raid_disks) {
4305 "md: array %s already initialised!\n",
4310 err = set_array_info(mddev, &info);
4312 printk(KERN_WARNING "md: couldn't set"
4313 " array info. %d\n", err);
4323 * Commands querying/configuring an existing array:
4325 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
4326 * RUN_ARRAY, and SET_BITMAP_FILE are allowed */
4327 if (!mddev->raid_disks && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
4328 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE) {
4334 * Commands even a read-only array can execute:
4338 case GET_ARRAY_INFO:
4339 err = get_array_info(mddev, argp);
4342 case GET_BITMAP_FILE:
4343 err = get_bitmap_file(mddev, argp);
4347 err = get_disk_info(mddev, argp);
4350 case RESTART_ARRAY_RW:
4351 err = restart_array(mddev);
4355 err = do_md_stop (mddev, 0);
4359 err = do_md_stop (mddev, 1);
4363 * We have a problem here : there is no easy way to give a CHS
4364 * virtual geometry. We currently pretend that we have a 2 heads
4365 * 4 sectors (with a BIG number of cylinders...). This drives
4366 * dosfs just mad... ;-)
4371 * The remaining ioctls are changing the state of the
4372 * superblock, so we do not allow them on read-only arrays.
4373 * However non-MD ioctls (e.g. get-size) will still come through
4374 * here and hit the 'default' below, so only disallow
4375 * 'md' ioctls, and switch to rw mode if started auto-readonly.
4377 if (_IOC_TYPE(cmd) == MD_MAJOR &&
4378 mddev->ro && mddev->pers) {
4379 if (mddev->ro == 2) {
4381 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4382 md_wakeup_thread(mddev->thread);
4394 mdu_disk_info_t info;
4395 if (copy_from_user(&info, argp, sizeof(info)))
4398 err = add_new_disk(mddev, &info);
4402 case HOT_REMOVE_DISK:
4403 err = hot_remove_disk(mddev, new_decode_dev(arg));
4407 err = hot_add_disk(mddev, new_decode_dev(arg));
4410 case SET_DISK_FAULTY:
4411 err = set_disk_faulty(mddev, new_decode_dev(arg));
4415 err = do_md_run (mddev);
4418 case SET_BITMAP_FILE:
4419 err = set_bitmap_file(mddev, (int)arg);
4429 mddev_unlock(mddev);
4439 static int md_open(struct inode *inode, struct file *file)
4442 * Succeed if we can lock the mddev, which confirms that
4443 * it isn't being stopped right now.
4445 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4448 if ((err = mddev_lock(mddev)))
4453 mddev_unlock(mddev);
4455 check_disk_change(inode->i_bdev);
4460 static int md_release(struct inode *inode, struct file * file)
4462 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4471 static int md_media_changed(struct gendisk *disk)
4473 mddev_t *mddev = disk->private_data;
4475 return mddev->changed;
4478 static int md_revalidate(struct gendisk *disk)
4480 mddev_t *mddev = disk->private_data;
4485 static struct block_device_operations md_fops =
4487 .owner = THIS_MODULE,
4489 .release = md_release,
4491 .getgeo = md_getgeo,
4492 .media_changed = md_media_changed,
4493 .revalidate_disk= md_revalidate,
4496 static int md_thread(void * arg)
4498 mdk_thread_t *thread = arg;
4501 * md_thread is a 'system-thread', it's priority should be very
4502 * high. We avoid resource deadlocks individually in each
4503 * raid personality. (RAID5 does preallocation) We also use RR and
4504 * the very same RT priority as kswapd, thus we will never get
4505 * into a priority inversion deadlock.
4507 * we definitely have to have equal or higher priority than
4508 * bdflush, otherwise bdflush will deadlock if there are too
4509 * many dirty RAID5 blocks.
4512 allow_signal(SIGKILL);
4513 while (!kthread_should_stop()) {
4515 /* We need to wait INTERRUPTIBLE so that
4516 * we don't add to the load-average.
4517 * That means we need to be sure no signals are
4520 if (signal_pending(current))
4521 flush_signals(current);
4523 wait_event_interruptible_timeout
4525 test_bit(THREAD_WAKEUP, &thread->flags)
4526 || kthread_should_stop(),
4530 clear_bit(THREAD_WAKEUP, &thread->flags);
4532 thread->run(thread->mddev);
4538 void md_wakeup_thread(mdk_thread_t *thread)
4541 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
4542 set_bit(THREAD_WAKEUP, &thread->flags);
4543 wake_up(&thread->wqueue);
4547 mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
4550 mdk_thread_t *thread;
4552 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
4556 init_waitqueue_head(&thread->wqueue);
4559 thread->mddev = mddev;
4560 thread->timeout = MAX_SCHEDULE_TIMEOUT;
4561 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
4562 if (IS_ERR(thread->tsk)) {
4569 void md_unregister_thread(mdk_thread_t *thread)
4571 dprintk("interrupting MD-thread pid %d\n", thread->tsk->pid);
4573 kthread_stop(thread->tsk);
4577 void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
4584 if (!rdev || test_bit(Faulty, &rdev->flags))
4587 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
4589 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
4590 __builtin_return_address(0),__builtin_return_address(1),
4591 __builtin_return_address(2),__builtin_return_address(3));
4593 if (!mddev->pers->error_handler)
4595 mddev->pers->error_handler(mddev,rdev);
4596 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4597 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4598 md_wakeup_thread(mddev->thread);
4599 md_new_event_inintr(mddev);
4602 /* seq_file implementation /proc/mdstat */
4604 static void status_unused(struct seq_file *seq)
4608 struct list_head *tmp;
4610 seq_printf(seq, "unused devices: ");
4612 ITERATE_RDEV_PENDING(rdev,tmp) {
4613 char b[BDEVNAME_SIZE];
4615 seq_printf(seq, "%s ",
4616 bdevname(rdev->bdev,b));
4619 seq_printf(seq, "<none>");
4621 seq_printf(seq, "\n");
4625 static void status_resync(struct seq_file *seq, mddev_t * mddev)
4627 sector_t max_blocks, resync, res;
4628 unsigned long dt, db, rt;
4630 unsigned int per_milli;
4632 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
4634 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
4635 max_blocks = mddev->resync_max_sectors >> 1;
4637 max_blocks = mddev->size;
4640 * Should not happen.
4646 /* Pick 'scale' such that (resync>>scale)*1000 will fit
4647 * in a sector_t, and (max_blocks>>scale) will fit in a
4648 * u32, as those are the requirements for sector_div.
4649 * Thus 'scale' must be at least 10
4652 if (sizeof(sector_t) > sizeof(unsigned long)) {
4653 while ( max_blocks/2 > (1ULL<<(scale+32)))
4656 res = (resync>>scale)*1000;
4657 sector_div(res, (u32)((max_blocks>>scale)+1));
4661 int i, x = per_milli/50, y = 20-x;
4662 seq_printf(seq, "[");
4663 for (i = 0; i < x; i++)
4664 seq_printf(seq, "=");
4665 seq_printf(seq, ">");
4666 for (i = 0; i < y; i++)
4667 seq_printf(seq, ".");
4668 seq_printf(seq, "] ");
4670 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
4671 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
4673 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
4674 "resync" : "recovery")),
4675 per_milli/10, per_milli % 10,
4676 (unsigned long long) resync,
4677 (unsigned long long) max_blocks);
4680 * We do not want to overflow, so the order of operands and
4681 * the * 100 / 100 trick are important. We do a +1 to be
4682 * safe against division by zero. We only estimate anyway.
4684 * dt: time from mark until now
4685 * db: blocks written from mark until now
4686 * rt: remaining time
4688 dt = ((jiffies - mddev->resync_mark) / HZ);
4690 db = resync - (mddev->resync_mark_cnt/2);
4691 rt = (dt * ((unsigned long)(max_blocks-resync) / (db/100+1)))/100;
4693 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
4695 seq_printf(seq, " speed=%ldK/sec", db/dt);
4698 static void *md_seq_start(struct seq_file *seq, loff_t *pos)
4700 struct list_head *tmp;
4710 spin_lock(&all_mddevs_lock);
4711 list_for_each(tmp,&all_mddevs)
4713 mddev = list_entry(tmp, mddev_t, all_mddevs);
4715 spin_unlock(&all_mddevs_lock);
4718 spin_unlock(&all_mddevs_lock);
4720 return (void*)2;/* tail */
4724 static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4726 struct list_head *tmp;
4727 mddev_t *next_mddev, *mddev = v;
4733 spin_lock(&all_mddevs_lock);
4735 tmp = all_mddevs.next;
4737 tmp = mddev->all_mddevs.next;
4738 if (tmp != &all_mddevs)
4739 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
4741 next_mddev = (void*)2;
4744 spin_unlock(&all_mddevs_lock);
4752 static void md_seq_stop(struct seq_file *seq, void *v)
4756 if (mddev && v != (void*)1 && v != (void*)2)
4760 struct mdstat_info {
4764 static int md_seq_show(struct seq_file *seq, void *v)
4768 struct list_head *tmp2;
4770 struct mdstat_info *mi = seq->private;
4771 struct bitmap *bitmap;
4773 if (v == (void*)1) {
4774 struct mdk_personality *pers;
4775 seq_printf(seq, "Personalities : ");
4776 spin_lock(&pers_lock);
4777 list_for_each_entry(pers, &pers_list, list)
4778 seq_printf(seq, "[%s] ", pers->name);
4780 spin_unlock(&pers_lock);
4781 seq_printf(seq, "\n");
4782 mi->event = atomic_read(&md_event_count);
4785 if (v == (void*)2) {
4790 if (mddev_lock(mddev) < 0)
4793 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
4794 seq_printf(seq, "%s : %sactive", mdname(mddev),
4795 mddev->pers ? "" : "in");
4798 seq_printf(seq, " (read-only)");
4800 seq_printf(seq, "(auto-read-only)");
4801 seq_printf(seq, " %s", mddev->pers->name);
4805 ITERATE_RDEV(mddev,rdev,tmp2) {
4806 char b[BDEVNAME_SIZE];
4807 seq_printf(seq, " %s[%d]",
4808 bdevname(rdev->bdev,b), rdev->desc_nr);
4809 if (test_bit(WriteMostly, &rdev->flags))
4810 seq_printf(seq, "(W)");
4811 if (test_bit(Faulty, &rdev->flags)) {
4812 seq_printf(seq, "(F)");
4814 } else if (rdev->raid_disk < 0)
4815 seq_printf(seq, "(S)"); /* spare */
4819 if (!list_empty(&mddev->disks)) {
4821 seq_printf(seq, "\n %llu blocks",
4822 (unsigned long long)mddev->array_size);
4824 seq_printf(seq, "\n %llu blocks",
4825 (unsigned long long)size);
4827 if (mddev->persistent) {
4828 if (mddev->major_version != 0 ||
4829 mddev->minor_version != 90) {
4830 seq_printf(seq," super %d.%d",
4831 mddev->major_version,
4832 mddev->minor_version);
4835 seq_printf(seq, " super non-persistent");
4838 mddev->pers->status (seq, mddev);
4839 seq_printf(seq, "\n ");
4840 if (mddev->pers->sync_request) {
4841 if (mddev->curr_resync > 2) {
4842 status_resync (seq, mddev);
4843 seq_printf(seq, "\n ");
4844 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
4845 seq_printf(seq, "\tresync=DELAYED\n ");
4846 else if (mddev->recovery_cp < MaxSector)
4847 seq_printf(seq, "\tresync=PENDING\n ");
4850 seq_printf(seq, "\n ");
4852 if ((bitmap = mddev->bitmap)) {
4853 unsigned long chunk_kb;
4854 unsigned long flags;
4855 spin_lock_irqsave(&bitmap->lock, flags);
4856 chunk_kb = bitmap->chunksize >> 10;
4857 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
4859 bitmap->pages - bitmap->missing_pages,
4861 (bitmap->pages - bitmap->missing_pages)
4862 << (PAGE_SHIFT - 10),
4863 chunk_kb ? chunk_kb : bitmap->chunksize,
4864 chunk_kb ? "KB" : "B");
4866 seq_printf(seq, ", file: ");
4867 seq_path(seq, bitmap->file->f_vfsmnt,
4868 bitmap->file->f_dentry," \t\n");
4871 seq_printf(seq, "\n");
4872 spin_unlock_irqrestore(&bitmap->lock, flags);
4875 seq_printf(seq, "\n");
4877 mddev_unlock(mddev);
4882 static struct seq_operations md_seq_ops = {
4883 .start = md_seq_start,
4884 .next = md_seq_next,
4885 .stop = md_seq_stop,
4886 .show = md_seq_show,
4889 static int md_seq_open(struct inode *inode, struct file *file)
4892 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
4896 error = seq_open(file, &md_seq_ops);
4900 struct seq_file *p = file->private_data;
4902 mi->event = atomic_read(&md_event_count);
4907 static int md_seq_release(struct inode *inode, struct file *file)
4909 struct seq_file *m = file->private_data;
4910 struct mdstat_info *mi = m->private;
4913 return seq_release(inode, file);
4916 static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
4918 struct seq_file *m = filp->private_data;
4919 struct mdstat_info *mi = m->private;
4922 poll_wait(filp, &md_event_waiters, wait);
4924 /* always allow read */
4925 mask = POLLIN | POLLRDNORM;
4927 if (mi->event != atomic_read(&md_event_count))
4928 mask |= POLLERR | POLLPRI;
4932 static struct file_operations md_seq_fops = {
4933 .open = md_seq_open,
4935 .llseek = seq_lseek,
4936 .release = md_seq_release,
4937 .poll = mdstat_poll,
4940 int register_md_personality(struct mdk_personality *p)
4942 spin_lock(&pers_lock);
4943 list_add_tail(&p->list, &pers_list);
4944 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
4945 spin_unlock(&pers_lock);
4949 int unregister_md_personality(struct mdk_personality *p)
4951 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
4952 spin_lock(&pers_lock);
4953 list_del_init(&p->list);
4954 spin_unlock(&pers_lock);
4958 static int is_mddev_idle(mddev_t *mddev)
4961 struct list_head *tmp;
4963 unsigned long curr_events;
4966 ITERATE_RDEV(mddev,rdev,tmp) {
4967 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
4968 curr_events = disk_stat_read(disk, sectors[0]) +
4969 disk_stat_read(disk, sectors[1]) -
4970 atomic_read(&disk->sync_io);
4971 /* The difference between curr_events and last_events
4972 * will be affected by any new non-sync IO (making
4973 * curr_events bigger) and any difference in the amount of
4974 * in-flight syncio (making current_events bigger or smaller)
4975 * The amount in-flight is currently limited to
4976 * 32*64K in raid1/10 and 256*PAGE_SIZE in raid5/6
4977 * which is at most 4096 sectors.
4978 * These numbers are fairly fragile and should be made
4979 * more robust, probably by enforcing the
4980 * 'window size' that md_do_sync sort-of uses.
4982 * Note: the following is an unsigned comparison.
4984 if ((curr_events - rdev->last_events + 4096) > 8192) {
4985 rdev->last_events = curr_events;
4992 void md_done_sync(mddev_t *mddev, int blocks, int ok)
4994 /* another "blocks" (512byte) blocks have been synced */
4995 atomic_sub(blocks, &mddev->recovery_active);
4996 wake_up(&mddev->recovery_wait);
4998 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
4999 md_wakeup_thread(mddev->thread);
5000 // stop recovery, signal do_sync ....
5005 /* md_write_start(mddev, bi)
5006 * If we need to update some array metadata (e.g. 'active' flag
5007 * in superblock) before writing, schedule a superblock update
5008 * and wait for it to complete.
5010 void md_write_start(mddev_t *mddev, struct bio *bi)
5012 if (bio_data_dir(bi) != WRITE)
5015 BUG_ON(mddev->ro == 1);
5016 if (mddev->ro == 2) {
5017 /* need to switch to read/write */
5019 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5020 md_wakeup_thread(mddev->thread);
5022 atomic_inc(&mddev->writes_pending);
5023 if (mddev->in_sync) {
5024 spin_lock_irq(&mddev->write_lock);
5025 if (mddev->in_sync) {
5027 mddev->sb_dirty = 3;
5028 md_wakeup_thread(mddev->thread);
5030 spin_unlock_irq(&mddev->write_lock);
5032 wait_event(mddev->sb_wait, mddev->sb_dirty==0);
5035 void md_write_end(mddev_t *mddev)
5037 if (atomic_dec_and_test(&mddev->writes_pending)) {
5038 if (mddev->safemode == 2)
5039 md_wakeup_thread(mddev->thread);
5040 else if (mddev->safemode_delay)
5041 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
5045 static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
5047 #define SYNC_MARKS 10
5048 #define SYNC_MARK_STEP (3*HZ)
5049 void md_do_sync(mddev_t *mddev)
5052 unsigned int currspeed = 0,
5054 sector_t max_sectors,j, io_sectors;
5055 unsigned long mark[SYNC_MARKS];
5056 sector_t mark_cnt[SYNC_MARKS];
5058 struct list_head *tmp;
5059 sector_t last_check;
5061 struct list_head *rtmp;
5064 /* just incase thread restarts... */
5065 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
5067 if (mddev->ro) /* never try to sync a read-only array */
5070 /* we overload curr_resync somewhat here.
5071 * 0 == not engaged in resync at all
5072 * 2 == checking that there is no conflict with another sync
5073 * 1 == like 2, but have yielded to allow conflicting resync to
5075 * other == active in resync - this many blocks
5077 * Before starting a resync we must have set curr_resync to
5078 * 2, and then checked that every "conflicting" array has curr_resync
5079 * less than ours. When we find one that is the same or higher
5080 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
5081 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
5082 * This will mean we have to start checking from the beginning again.
5087 mddev->curr_resync = 2;
5090 if (kthread_should_stop()) {
5091 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5094 ITERATE_MDDEV(mddev2,tmp) {
5095 if (mddev2 == mddev)
5097 if (mddev2->curr_resync &&
5098 match_mddev_units(mddev,mddev2)) {
5100 if (mddev < mddev2 && mddev->curr_resync == 2) {
5101 /* arbitrarily yield */
5102 mddev->curr_resync = 1;
5103 wake_up(&resync_wait);
5105 if (mddev > mddev2 && mddev->curr_resync == 1)
5106 /* no need to wait here, we can wait the next
5107 * time 'round when curr_resync == 2
5110 prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE);
5111 if (!kthread_should_stop() &&
5112 mddev2->curr_resync >= mddev->curr_resync) {
5113 printk(KERN_INFO "md: delaying resync of %s"
5114 " until %s has finished resync (they"
5115 " share one or more physical units)\n",
5116 mdname(mddev), mdname(mddev2));
5119 finish_wait(&resync_wait, &wq);
5122 finish_wait(&resync_wait, &wq);
5125 } while (mddev->curr_resync < 2);
5128 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5129 /* resync follows the size requested by the personality,
5130 * which defaults to physical size, but can be virtual size
5132 max_sectors = mddev->resync_max_sectors;
5133 mddev->resync_mismatches = 0;
5134 /* we don't use the checkpoint if there's a bitmap */
5135 if (!mddev->bitmap &&
5136 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5137 j = mddev->recovery_cp;
5138 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5139 max_sectors = mddev->size << 1;
5141 /* recovery follows the physical size of devices */
5142 max_sectors = mddev->size << 1;
5144 ITERATE_RDEV(mddev,rdev,rtmp)
5145 if (rdev->raid_disk >= 0 &&
5146 !test_bit(Faulty, &rdev->flags) &&
5147 !test_bit(In_sync, &rdev->flags) &&
5148 rdev->recovery_offset < j)
5149 j = rdev->recovery_offset;
5152 printk(KERN_INFO "md: syncing RAID array %s\n", mdname(mddev));
5153 printk(KERN_INFO "md: minimum _guaranteed_ reconstruction speed:"
5154 " %d KB/sec/disc.\n", speed_min(mddev));
5155 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
5156 "(but not more than %d KB/sec) for reconstruction.\n",
5159 is_mddev_idle(mddev); /* this also initializes IO event counters */
5162 for (m = 0; m < SYNC_MARKS; m++) {
5164 mark_cnt[m] = io_sectors;
5167 mddev->resync_mark = mark[last_mark];
5168 mddev->resync_mark_cnt = mark_cnt[last_mark];
5171 * Tune reconstruction:
5173 window = 32*(PAGE_SIZE/512);
5174 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
5175 window/2,(unsigned long long) max_sectors/2);
5177 atomic_set(&mddev->recovery_active, 0);
5178 init_waitqueue_head(&mddev->recovery_wait);
5183 "md: resuming recovery of %s from checkpoint.\n",
5185 mddev->curr_resync = j;
5188 while (j < max_sectors) {
5192 sectors = mddev->pers->sync_request(mddev, j, &skipped,
5193 currspeed < speed_min(mddev));
5195 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
5199 if (!skipped) { /* actual IO requested */
5200 io_sectors += sectors;
5201 atomic_add(sectors, &mddev->recovery_active);
5205 if (j>1) mddev->curr_resync = j;
5206 if (last_check == 0)
5207 /* this is the earliers that rebuilt will be
5208 * visible in /proc/mdstat
5210 md_new_event(mddev);
5212 if (last_check + window > io_sectors || j == max_sectors)
5215 last_check = io_sectors;
5217 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery) ||
5218 test_bit(MD_RECOVERY_ERR, &mddev->recovery))
5222 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
5224 int next = (last_mark+1) % SYNC_MARKS;
5226 mddev->resync_mark = mark[next];
5227 mddev->resync_mark_cnt = mark_cnt[next];
5228 mark[next] = jiffies;
5229 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
5234 if (kthread_should_stop()) {
5236 * got a signal, exit.
5239 "md: md_do_sync() got signal ... exiting\n");
5240 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5245 * this loop exits only if either when we are slower than
5246 * the 'hard' speed limit, or the system was IO-idle for
5248 * the system might be non-idle CPU-wise, but we only care
5249 * about not overloading the IO subsystem. (things like an
5250 * e2fsck being done on the RAID array should execute fast)
5252 mddev->queue->unplug_fn(mddev->queue);
5255 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
5256 /((jiffies-mddev->resync_mark)/HZ +1) +1;
5258 if (currspeed > speed_min(mddev)) {
5259 if ((currspeed > speed_max(mddev)) ||
5260 !is_mddev_idle(mddev)) {
5266 printk(KERN_INFO "md: %s: sync done.\n",mdname(mddev));
5268 * this also signals 'finished resyncing' to md_stop
5271 mddev->queue->unplug_fn(mddev->queue);
5273 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
5275 /* tell personality that we are finished */
5276 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
5278 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
5279 test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
5280 !test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
5281 mddev->curr_resync > 2) {
5282 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5283 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5284 if (mddev->curr_resync >= mddev->recovery_cp) {
5286 "md: checkpointing recovery of %s.\n",
5288 mddev->recovery_cp = mddev->curr_resync;
5291 mddev->recovery_cp = MaxSector;
5293 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5294 mddev->curr_resync = MaxSector;
5295 ITERATE_RDEV(mddev,rdev,rtmp)
5296 if (rdev->raid_disk >= 0 &&
5297 !test_bit(Faulty, &rdev->flags) &&
5298 !test_bit(In_sync, &rdev->flags) &&
5299 rdev->recovery_offset < mddev->curr_resync)
5300 rdev->recovery_offset = mddev->curr_resync;
5301 mddev->sb_dirty = 1;
5306 mddev->curr_resync = 0;
5307 wake_up(&resync_wait);
5308 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
5309 md_wakeup_thread(mddev->thread);
5311 EXPORT_SYMBOL_GPL(md_do_sync);
5315 * This routine is regularly called by all per-raid-array threads to
5316 * deal with generic issues like resync and super-block update.
5317 * Raid personalities that don't have a thread (linear/raid0) do not
5318 * need this as they never do any recovery or update the superblock.
5320 * It does not do any resync itself, but rather "forks" off other threads
5321 * to do that as needed.
5322 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
5323 * "->recovery" and create a thread at ->sync_thread.
5324 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
5325 * and wakeups up this thread which will reap the thread and finish up.
5326 * This thread also removes any faulty devices (with nr_pending == 0).
5328 * The overall approach is:
5329 * 1/ if the superblock needs updating, update it.
5330 * 2/ If a recovery thread is running, don't do anything else.
5331 * 3/ If recovery has finished, clean up, possibly marking spares active.
5332 * 4/ If there are any faulty devices, remove them.
5333 * 5/ If array is degraded, try to add spares devices
5334 * 6/ If array has spares or is not in-sync, start a resync thread.
5336 void md_check_recovery(mddev_t *mddev)
5339 struct list_head *rtmp;
5343 bitmap_daemon_work(mddev->bitmap);
5348 if (signal_pending(current)) {
5349 if (mddev->pers->sync_request) {
5350 printk(KERN_INFO "md: %s in immediate safe mode\n",
5352 mddev->safemode = 2;
5354 flush_signals(current);
5359 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
5360 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
5361 (mddev->safemode == 1) ||
5362 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
5363 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
5367 if (mddev_trylock(mddev)) {
5370 spin_lock_irq(&mddev->write_lock);
5371 if (mddev->safemode && !atomic_read(&mddev->writes_pending) &&
5372 !mddev->in_sync && mddev->recovery_cp == MaxSector) {
5374 mddev->sb_dirty = 3;
5376 if (mddev->safemode == 1)
5377 mddev->safemode = 0;
5378 spin_unlock_irq(&mddev->write_lock);
5380 if (mddev->sb_dirty)
5381 md_update_sb(mddev);
5384 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
5385 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
5386 /* resync/recovery still happening */
5387 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5390 if (mddev->sync_thread) {
5391 /* resync has finished, collect result */
5392 md_unregister_thread(mddev->sync_thread);
5393 mddev->sync_thread = NULL;
5394 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
5395 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5397 /* activate any spares */
5398 mddev->pers->spare_active(mddev);
5400 md_update_sb(mddev);
5402 /* if array is no-longer degraded, then any saved_raid_disk
5403 * information must be scrapped
5405 if (!mddev->degraded)
5406 ITERATE_RDEV(mddev,rdev,rtmp)
5407 rdev->saved_raid_disk = -1;
5409 mddev->recovery = 0;
5410 /* flag recovery needed just to double check */
5411 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5412 md_new_event(mddev);
5415 /* Clear some bits that don't mean anything, but
5418 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5419 clear_bit(MD_RECOVERY_ERR, &mddev->recovery);
5420 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
5421 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
5423 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
5425 /* no recovery is running.
5426 * remove any failed drives, then
5427 * add spares if possible.
5428 * Spare are also removed and re-added, to allow
5429 * the personality to fail the re-add.
5431 ITERATE_RDEV(mddev,rdev,rtmp)
5432 if (rdev->raid_disk >= 0 &&
5433 (test_bit(Faulty, &rdev->flags) || ! test_bit(In_sync, &rdev->flags)) &&
5434 atomic_read(&rdev->nr_pending)==0) {
5435 if (mddev->pers->hot_remove_disk(mddev, rdev->raid_disk)==0) {
5437 sprintf(nm,"rd%d", rdev->raid_disk);
5438 sysfs_remove_link(&mddev->kobj, nm);
5439 rdev->raid_disk = -1;
5443 if (mddev->degraded) {
5444 ITERATE_RDEV(mddev,rdev,rtmp)
5445 if (rdev->raid_disk < 0
5446 && !test_bit(Faulty, &rdev->flags)) {
5447 rdev->recovery_offset = 0;
5448 if (mddev->pers->hot_add_disk(mddev,rdev)) {
5450 sprintf(nm, "rd%d", rdev->raid_disk);
5451 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
5453 md_new_event(mddev);
5460 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5461 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5462 } else if (mddev->recovery_cp < MaxSector) {
5463 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5464 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
5465 /* nothing to be done ... */
5468 if (mddev->pers->sync_request) {
5469 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5470 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
5471 /* We are adding a device or devices to an array
5472 * which has the bitmap stored on all devices.
5473 * So make sure all bitmap pages get written
5475 bitmap_write_all(mddev->bitmap);
5477 mddev->sync_thread = md_register_thread(md_do_sync,
5480 if (!mddev->sync_thread) {
5481 printk(KERN_ERR "%s: could not start resync"
5484 /* leave the spares where they are, it shouldn't hurt */
5485 mddev->recovery = 0;
5487 md_wakeup_thread(mddev->sync_thread);
5488 md_new_event(mddev);
5491 mddev_unlock(mddev);
5495 static int md_notify_reboot(struct notifier_block *this,
5496 unsigned long code, void *x)
5498 struct list_head *tmp;
5501 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
5503 printk(KERN_INFO "md: stopping all md devices.\n");
5505 ITERATE_MDDEV(mddev,tmp)
5506 if (mddev_trylock(mddev)) {
5507 do_md_stop (mddev, 1);
5508 mddev_unlock(mddev);
5511 * certain more exotic SCSI devices are known to be
5512 * volatile wrt too early system reboots. While the
5513 * right place to handle this issue is the given
5514 * driver, we do want to have a safe RAID driver ...
5521 static struct notifier_block md_notifier = {
5522 .notifier_call = md_notify_reboot,
5524 .priority = INT_MAX, /* before any real devices */
5527 static void md_geninit(void)
5529 struct proc_dir_entry *p;
5531 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
5533 p = create_proc_entry("mdstat", S_IRUGO, NULL);
5535 p->proc_fops = &md_seq_fops;
5538 static int __init md_init(void)
5540 printk(KERN_INFO "md: md driver %d.%d.%d MAX_MD_DEVS=%d,"
5541 " MD_SB_DISKS=%d\n",
5542 MD_MAJOR_VERSION, MD_MINOR_VERSION,
5543 MD_PATCHLEVEL_VERSION, MAX_MD_DEVS, MD_SB_DISKS);
5544 printk(KERN_INFO "md: bitmap version %d.%d\n", BITMAP_MAJOR_HI,
5547 if (register_blkdev(MAJOR_NR, "md"))
5549 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
5550 unregister_blkdev(MAJOR_NR, "md");
5553 blk_register_region(MKDEV(MAJOR_NR, 0), MAX_MD_DEVS, THIS_MODULE,
5554 md_probe, NULL, NULL);
5555 blk_register_region(MKDEV(mdp_major, 0), MAX_MD_DEVS<<MdpMinorShift, THIS_MODULE,
5556 md_probe, NULL, NULL);
5558 register_reboot_notifier(&md_notifier);
5559 raid_table_header = register_sysctl_table(raid_root_table, 1);
5569 * Searches all registered partitions for autorun RAID arrays
5572 static dev_t detected_devices[128];
5575 void md_autodetect_dev(dev_t dev)
5577 if (dev_cnt >= 0 && dev_cnt < 127)
5578 detected_devices[dev_cnt++] = dev;
5582 static void autostart_arrays(int part)
5587 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
5589 for (i = 0; i < dev_cnt; i++) {
5590 dev_t dev = detected_devices[i];
5592 rdev = md_import_device(dev,0, 0);
5596 if (test_bit(Faulty, &rdev->flags)) {
5600 list_add(&rdev->same_set, &pending_raid_disks);
5604 autorun_devices(part);
5609 static __exit void md_exit(void)
5612 struct list_head *tmp;
5614 blk_unregister_region(MKDEV(MAJOR_NR,0), MAX_MD_DEVS);
5615 blk_unregister_region(MKDEV(mdp_major,0), MAX_MD_DEVS << MdpMinorShift);
5617 unregister_blkdev(MAJOR_NR,"md");
5618 unregister_blkdev(mdp_major, "mdp");
5619 unregister_reboot_notifier(&md_notifier);
5620 unregister_sysctl_table(raid_table_header);
5621 remove_proc_entry("mdstat", NULL);
5622 ITERATE_MDDEV(mddev,tmp) {
5623 struct gendisk *disk = mddev->gendisk;
5626 export_array(mddev);
5629 mddev->gendisk = NULL;
5634 module_init(md_init)
5635 module_exit(md_exit)
5637 static int get_ro(char *buffer, struct kernel_param *kp)
5639 return sprintf(buffer, "%d", start_readonly);
5641 static int set_ro(const char *val, struct kernel_param *kp)
5644 int num = simple_strtoul(val, &e, 10);
5645 if (*val && (*e == '\0' || *e == '\n')) {
5646 start_readonly = num;
5652 module_param_call(start_ro, set_ro, get_ro, NULL, 0600);
5653 module_param(start_dirty_degraded, int, 0644);
5656 EXPORT_SYMBOL(register_md_personality);
5657 EXPORT_SYMBOL(unregister_md_personality);
5658 EXPORT_SYMBOL(md_error);
5659 EXPORT_SYMBOL(md_done_sync);
5660 EXPORT_SYMBOL(md_write_start);
5661 EXPORT_SYMBOL(md_write_end);
5662 EXPORT_SYMBOL(md_register_thread);
5663 EXPORT_SYMBOL(md_unregister_thread);
5664 EXPORT_SYMBOL(md_wakeup_thread);
5665 EXPORT_SYMBOL(md_check_recovery);
5666 MODULE_LICENSE("GPL");
5668 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);