2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
5 completely rewritten, based on the MD driver code from Marc Zyngier
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
20 Neil Brown <neilb@cse.unsw.edu.au>.
22 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
25 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 #include <linux/module.h>
36 #include <linux/config.h>
37 #include <linux/kthread.h>
38 #include <linux/linkage.h>
39 #include <linux/raid/md.h>
40 #include <linux/raid/bitmap.h>
41 #include <linux/sysctl.h>
42 #include <linux/devfs_fs_kernel.h>
43 #include <linux/buffer_head.h> /* for invalidate_bdev */
44 #include <linux/suspend.h>
45 #include <linux/poll.h>
47 #include <linux/init.h>
49 #include <linux/file.h>
52 #include <linux/kmod.h>
55 #include <asm/unaligned.h>
57 #define MAJOR_NR MD_MAJOR
60 /* 63 partitions with the alternate major number (mdp) */
61 #define MdpMinorShift 6
64 #define dprintk(x...) ((void)(DEBUG && printk(x)))
68 static void autostart_arrays (int part);
71 static LIST_HEAD(pers_list);
72 static DEFINE_SPINLOCK(pers_lock);
75 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
76 * is 1000 KB/sec, so the extra system load does not show up that much.
77 * Increase it if you want to have more _guaranteed_ speed. Note that
78 * the RAID driver will use the maximum available bandwidth if the IO
79 * subsystem is idle. There is also an 'absolute maximum' reconstruction
80 * speed limit - in case reconstruction slows down your system despite
83 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
84 * or /sys/block/mdX/md/sync_speed_{min,max}
87 static int sysctl_speed_limit_min = 1000;
88 static int sysctl_speed_limit_max = 200000;
89 static inline int speed_min(mddev_t *mddev)
91 return mddev->sync_speed_min ?
92 mddev->sync_speed_min : sysctl_speed_limit_min;
95 static inline int speed_max(mddev_t *mddev)
97 return mddev->sync_speed_max ?
98 mddev->sync_speed_max : sysctl_speed_limit_max;
101 static struct ctl_table_header *raid_table_header;
103 static ctl_table raid_table[] = {
105 .ctl_name = DEV_RAID_SPEED_LIMIT_MIN,
106 .procname = "speed_limit_min",
107 .data = &sysctl_speed_limit_min,
108 .maxlen = sizeof(int),
110 .proc_handler = &proc_dointvec,
113 .ctl_name = DEV_RAID_SPEED_LIMIT_MAX,
114 .procname = "speed_limit_max",
115 .data = &sysctl_speed_limit_max,
116 .maxlen = sizeof(int),
118 .proc_handler = &proc_dointvec,
123 static ctl_table raid_dir_table[] = {
125 .ctl_name = DEV_RAID,
134 static ctl_table raid_root_table[] = {
140 .child = raid_dir_table,
145 static struct block_device_operations md_fops;
147 static int start_readonly;
150 * We have a system wide 'event count' that is incremented
151 * on any 'interesting' event, and readers of /proc/mdstat
152 * can use 'poll' or 'select' to find out when the event
156 * start array, stop array, error, add device, remove device,
157 * start build, activate spare
159 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
160 static atomic_t md_event_count;
161 static void md_new_event(mddev_t *mddev)
163 atomic_inc(&md_event_count);
164 wake_up(&md_event_waiters);
168 * Enables to iterate over all existing md arrays
169 * all_mddevs_lock protects this list.
171 static LIST_HEAD(all_mddevs);
172 static DEFINE_SPINLOCK(all_mddevs_lock);
176 * iterates through all used mddevs in the system.
177 * We take care to grab the all_mddevs_lock whenever navigating
178 * the list, and to always hold a refcount when unlocked.
179 * Any code which breaks out of this loop while own
180 * a reference to the current mddev and must mddev_put it.
182 #define ITERATE_MDDEV(mddev,tmp) \
184 for (({ spin_lock(&all_mddevs_lock); \
185 tmp = all_mddevs.next; \
187 ({ if (tmp != &all_mddevs) \
188 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
189 spin_unlock(&all_mddevs_lock); \
190 if (mddev) mddev_put(mddev); \
191 mddev = list_entry(tmp, mddev_t, all_mddevs); \
192 tmp != &all_mddevs;}); \
193 ({ spin_lock(&all_mddevs_lock); \
198 static int md_fail_request (request_queue_t *q, struct bio *bio)
200 bio_io_error(bio, bio->bi_size);
204 static inline mddev_t *mddev_get(mddev_t *mddev)
206 atomic_inc(&mddev->active);
210 static void mddev_put(mddev_t *mddev)
212 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
214 if (!mddev->raid_disks && list_empty(&mddev->disks)) {
215 list_del(&mddev->all_mddevs);
216 blk_put_queue(mddev->queue);
217 kobject_unregister(&mddev->kobj);
219 spin_unlock(&all_mddevs_lock);
222 static mddev_t * mddev_find(dev_t unit)
224 mddev_t *mddev, *new = NULL;
227 spin_lock(&all_mddevs_lock);
228 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
229 if (mddev->unit == unit) {
231 spin_unlock(&all_mddevs_lock);
237 list_add(&new->all_mddevs, &all_mddevs);
238 spin_unlock(&all_mddevs_lock);
241 spin_unlock(&all_mddevs_lock);
243 new = kzalloc(sizeof(*new), GFP_KERNEL);
248 if (MAJOR(unit) == MD_MAJOR)
249 new->md_minor = MINOR(unit);
251 new->md_minor = MINOR(unit) >> MdpMinorShift;
253 init_MUTEX(&new->reconfig_sem);
254 INIT_LIST_HEAD(&new->disks);
255 INIT_LIST_HEAD(&new->all_mddevs);
256 init_timer(&new->safemode_timer);
257 atomic_set(&new->active, 1);
258 spin_lock_init(&new->write_lock);
259 init_waitqueue_head(&new->sb_wait);
261 new->queue = blk_alloc_queue(GFP_KERNEL);
267 blk_queue_make_request(new->queue, md_fail_request);
272 static inline int mddev_lock(mddev_t * mddev)
274 return down_interruptible(&mddev->reconfig_sem);
277 static inline void mddev_lock_uninterruptible(mddev_t * mddev)
279 down(&mddev->reconfig_sem);
282 static inline int mddev_trylock(mddev_t * mddev)
284 return down_trylock(&mddev->reconfig_sem);
287 static inline void mddev_unlock(mddev_t * mddev)
289 up(&mddev->reconfig_sem);
291 md_wakeup_thread(mddev->thread);
294 static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
297 struct list_head *tmp;
299 ITERATE_RDEV(mddev,rdev,tmp) {
300 if (rdev->desc_nr == nr)
306 static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
308 struct list_head *tmp;
311 ITERATE_RDEV(mddev,rdev,tmp) {
312 if (rdev->bdev->bd_dev == dev)
318 static struct mdk_personality *find_pers(int level, char *clevel)
320 struct mdk_personality *pers;
321 list_for_each_entry(pers, &pers_list, list) {
322 if (level != LEVEL_NONE && pers->level == level)
324 if (strcmp(pers->name, clevel)==0)
330 static inline sector_t calc_dev_sboffset(struct block_device *bdev)
332 sector_t size = bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
333 return MD_NEW_SIZE_BLOCKS(size);
336 static sector_t calc_dev_size(mdk_rdev_t *rdev, unsigned chunk_size)
340 size = rdev->sb_offset;
343 size &= ~((sector_t)chunk_size/1024 - 1);
347 static int alloc_disk_sb(mdk_rdev_t * rdev)
352 rdev->sb_page = alloc_page(GFP_KERNEL);
353 if (!rdev->sb_page) {
354 printk(KERN_ALERT "md: out of memory.\n");
361 static void free_disk_sb(mdk_rdev_t * rdev)
364 put_page(rdev->sb_page);
366 rdev->sb_page = NULL;
373 static int super_written(struct bio *bio, unsigned int bytes_done, int error)
375 mdk_rdev_t *rdev = bio->bi_private;
376 mddev_t *mddev = rdev->mddev;
380 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags))
381 md_error(mddev, rdev);
383 if (atomic_dec_and_test(&mddev->pending_writes))
384 wake_up(&mddev->sb_wait);
389 static int super_written_barrier(struct bio *bio, unsigned int bytes_done, int error)
391 struct bio *bio2 = bio->bi_private;
392 mdk_rdev_t *rdev = bio2->bi_private;
393 mddev_t *mddev = rdev->mddev;
397 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
398 error == -EOPNOTSUPP) {
400 /* barriers don't appear to be supported :-( */
401 set_bit(BarriersNotsupp, &rdev->flags);
402 mddev->barriers_work = 0;
403 spin_lock_irqsave(&mddev->write_lock, flags);
404 bio2->bi_next = mddev->biolist;
405 mddev->biolist = bio2;
406 spin_unlock_irqrestore(&mddev->write_lock, flags);
407 wake_up(&mddev->sb_wait);
412 bio->bi_private = rdev;
413 return super_written(bio, bytes_done, error);
416 void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
417 sector_t sector, int size, struct page *page)
419 /* write first size bytes of page to sector of rdev
420 * Increment mddev->pending_writes before returning
421 * and decrement it on completion, waking up sb_wait
422 * if zero is reached.
423 * If an error occurred, call md_error
425 * As we might need to resubmit the request if BIO_RW_BARRIER
426 * causes ENOTSUPP, we allocate a spare bio...
428 struct bio *bio = bio_alloc(GFP_NOIO, 1);
429 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNC);
431 bio->bi_bdev = rdev->bdev;
432 bio->bi_sector = sector;
433 bio_add_page(bio, page, size, 0);
434 bio->bi_private = rdev;
435 bio->bi_end_io = super_written;
438 atomic_inc(&mddev->pending_writes);
439 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
441 rw |= (1<<BIO_RW_BARRIER);
442 rbio = bio_clone(bio, GFP_NOIO);
443 rbio->bi_private = bio;
444 rbio->bi_end_io = super_written_barrier;
445 submit_bio(rw, rbio);
450 void md_super_wait(mddev_t *mddev)
452 /* wait for all superblock writes that were scheduled to complete.
453 * if any had to be retried (due to BARRIER problems), retry them
457 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
458 if (atomic_read(&mddev->pending_writes)==0)
460 while (mddev->biolist) {
462 spin_lock_irq(&mddev->write_lock);
463 bio = mddev->biolist;
464 mddev->biolist = bio->bi_next ;
466 spin_unlock_irq(&mddev->write_lock);
467 submit_bio(bio->bi_rw, bio);
471 finish_wait(&mddev->sb_wait, &wq);
474 static int bi_complete(struct bio *bio, unsigned int bytes_done, int error)
479 complete((struct completion*)bio->bi_private);
483 int sync_page_io(struct block_device *bdev, sector_t sector, int size,
484 struct page *page, int rw)
486 struct bio *bio = bio_alloc(GFP_NOIO, 1);
487 struct completion event;
490 rw |= (1 << BIO_RW_SYNC);
493 bio->bi_sector = sector;
494 bio_add_page(bio, page, size, 0);
495 init_completion(&event);
496 bio->bi_private = &event;
497 bio->bi_end_io = bi_complete;
499 wait_for_completion(&event);
501 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
505 EXPORT_SYMBOL_GPL(sync_page_io);
507 static int read_disk_sb(mdk_rdev_t * rdev, int size)
509 char b[BDEVNAME_SIZE];
510 if (!rdev->sb_page) {
518 if (!sync_page_io(rdev->bdev, rdev->sb_offset<<1, size, rdev->sb_page, READ))
524 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
525 bdevname(rdev->bdev,b));
529 static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
531 if ( (sb1->set_uuid0 == sb2->set_uuid0) &&
532 (sb1->set_uuid1 == sb2->set_uuid1) &&
533 (sb1->set_uuid2 == sb2->set_uuid2) &&
534 (sb1->set_uuid3 == sb2->set_uuid3))
542 static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
545 mdp_super_t *tmp1, *tmp2;
547 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
548 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
550 if (!tmp1 || !tmp2) {
552 printk(KERN_INFO "md.c: sb1 is not equal to sb2!\n");
560 * nr_disks is not constant
565 if (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4))
576 static unsigned int calc_sb_csum(mdp_super_t * sb)
578 unsigned int disk_csum, csum;
580 disk_csum = sb->sb_csum;
582 csum = csum_partial((void *)sb, MD_SB_BYTES, 0);
583 sb->sb_csum = disk_csum;
589 * Handle superblock details.
590 * We want to be able to handle multiple superblock formats
591 * so we have a common interface to them all, and an array of
592 * different handlers.
593 * We rely on user-space to write the initial superblock, and support
594 * reading and updating of superblocks.
595 * Interface methods are:
596 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
597 * loads and validates a superblock on dev.
598 * if refdev != NULL, compare superblocks on both devices
600 * 0 - dev has a superblock that is compatible with refdev
601 * 1 - dev has a superblock that is compatible and newer than refdev
602 * so dev should be used as the refdev in future
603 * -EINVAL superblock incompatible or invalid
604 * -othererror e.g. -EIO
606 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
607 * Verify that dev is acceptable into mddev.
608 * The first time, mddev->raid_disks will be 0, and data from
609 * dev should be merged in. Subsequent calls check that dev
610 * is new enough. Return 0 or -EINVAL
612 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
613 * Update the superblock for rdev with data in mddev
614 * This does not write to disc.
620 struct module *owner;
621 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version);
622 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
623 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
627 * load_super for 0.90.0
629 static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
631 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
637 * Calculate the position of the superblock,
638 * it's at the end of the disk.
640 * It also happens to be a multiple of 4Kb.
642 sb_offset = calc_dev_sboffset(rdev->bdev);
643 rdev->sb_offset = sb_offset;
645 ret = read_disk_sb(rdev, MD_SB_BYTES);
650 bdevname(rdev->bdev, b);
651 sb = (mdp_super_t*)page_address(rdev->sb_page);
653 if (sb->md_magic != MD_SB_MAGIC) {
654 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
659 if (sb->major_version != 0 ||
660 sb->minor_version != 90) {
661 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
662 sb->major_version, sb->minor_version,
667 if (sb->raid_disks <= 0)
670 if (csum_fold(calc_sb_csum(sb)) != csum_fold(sb->sb_csum)) {
671 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
676 rdev->preferred_minor = sb->md_minor;
677 rdev->data_offset = 0;
678 rdev->sb_size = MD_SB_BYTES;
680 if (sb->level == LEVEL_MULTIPATH)
683 rdev->desc_nr = sb->this_disk.number;
689 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
690 if (!uuid_equal(refsb, sb)) {
691 printk(KERN_WARNING "md: %s has different UUID to %s\n",
692 b, bdevname(refdev->bdev,b2));
695 if (!sb_equal(refsb, sb)) {
696 printk(KERN_WARNING "md: %s has same UUID"
697 " but different superblock to %s\n",
698 b, bdevname(refdev->bdev, b2));
702 ev2 = md_event(refsb);
708 rdev->size = calc_dev_size(rdev, sb->chunk_size);
710 if (rdev->size < sb->size && sb->level > 1)
711 /* "this cannot possibly happen" ... */
719 * validate_super for 0.90.0
721 static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
724 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
726 rdev->raid_disk = -1;
728 if (mddev->raid_disks == 0) {
729 mddev->major_version = 0;
730 mddev->minor_version = sb->minor_version;
731 mddev->patch_version = sb->patch_version;
732 mddev->persistent = ! sb->not_persistent;
733 mddev->chunk_size = sb->chunk_size;
734 mddev->ctime = sb->ctime;
735 mddev->utime = sb->utime;
736 mddev->level = sb->level;
737 mddev->clevel[0] = 0;
738 mddev->layout = sb->layout;
739 mddev->raid_disks = sb->raid_disks;
740 mddev->size = sb->size;
741 mddev->events = md_event(sb);
742 mddev->bitmap_offset = 0;
743 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
745 if (sb->state & (1<<MD_SB_CLEAN))
746 mddev->recovery_cp = MaxSector;
748 if (sb->events_hi == sb->cp_events_hi &&
749 sb->events_lo == sb->cp_events_lo) {
750 mddev->recovery_cp = sb->recovery_cp;
752 mddev->recovery_cp = 0;
755 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
756 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
757 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
758 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
760 mddev->max_disks = MD_SB_DISKS;
762 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
763 mddev->bitmap_file == NULL) {
764 if (mddev->level != 1 && mddev->level != 5 && mddev->level != 6
765 && mddev->level != 10) {
766 /* FIXME use a better test */
767 printk(KERN_WARNING "md: bitmaps not supported for this level.\n");
770 mddev->bitmap_offset = mddev->default_bitmap_offset;
773 } else if (mddev->pers == NULL) {
774 /* Insist on good event counter while assembling */
775 __u64 ev1 = md_event(sb);
777 if (ev1 < mddev->events)
779 } else if (mddev->bitmap) {
780 /* if adding to array with a bitmap, then we can accept an
781 * older device ... but not too old.
783 __u64 ev1 = md_event(sb);
784 if (ev1 < mddev->bitmap->events_cleared)
786 } else /* just a hot-add of a new device, leave raid_disk at -1 */
789 if (mddev->level != LEVEL_MULTIPATH) {
790 desc = sb->disks + rdev->desc_nr;
792 if (desc->state & (1<<MD_DISK_FAULTY))
793 set_bit(Faulty, &rdev->flags);
794 else if (desc->state & (1<<MD_DISK_SYNC) &&
795 desc->raid_disk < mddev->raid_disks) {
796 set_bit(In_sync, &rdev->flags);
797 rdev->raid_disk = desc->raid_disk;
799 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
800 set_bit(WriteMostly, &rdev->flags);
801 } else /* MULTIPATH are always insync */
802 set_bit(In_sync, &rdev->flags);
807 * sync_super for 0.90.0
809 static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
812 struct list_head *tmp;
814 int next_spare = mddev->raid_disks;
817 /* make rdev->sb match mddev data..
820 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
821 * 3/ any empty disks < next_spare become removed
823 * disks[0] gets initialised to REMOVED because
824 * we cannot be sure from other fields if it has
825 * been initialised or not.
828 int active=0, working=0,failed=0,spare=0,nr_disks=0;
830 rdev->sb_size = MD_SB_BYTES;
832 sb = (mdp_super_t*)page_address(rdev->sb_page);
834 memset(sb, 0, sizeof(*sb));
836 sb->md_magic = MD_SB_MAGIC;
837 sb->major_version = mddev->major_version;
838 sb->minor_version = mddev->minor_version;
839 sb->patch_version = mddev->patch_version;
840 sb->gvalid_words = 0; /* ignored */
841 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
842 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
843 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
844 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
846 sb->ctime = mddev->ctime;
847 sb->level = mddev->level;
848 sb->size = mddev->size;
849 sb->raid_disks = mddev->raid_disks;
850 sb->md_minor = mddev->md_minor;
851 sb->not_persistent = !mddev->persistent;
852 sb->utime = mddev->utime;
854 sb->events_hi = (mddev->events>>32);
855 sb->events_lo = (u32)mddev->events;
859 sb->recovery_cp = mddev->recovery_cp;
860 sb->cp_events_hi = (mddev->events>>32);
861 sb->cp_events_lo = (u32)mddev->events;
862 if (mddev->recovery_cp == MaxSector)
863 sb->state = (1<< MD_SB_CLEAN);
867 sb->layout = mddev->layout;
868 sb->chunk_size = mddev->chunk_size;
870 if (mddev->bitmap && mddev->bitmap_file == NULL)
871 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
873 sb->disks[0].state = (1<<MD_DISK_REMOVED);
874 ITERATE_RDEV(mddev,rdev2,tmp) {
877 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
878 && !test_bit(Faulty, &rdev2->flags))
879 desc_nr = rdev2->raid_disk;
881 desc_nr = next_spare++;
882 rdev2->desc_nr = desc_nr;
883 d = &sb->disks[rdev2->desc_nr];
885 d->number = rdev2->desc_nr;
886 d->major = MAJOR(rdev2->bdev->bd_dev);
887 d->minor = MINOR(rdev2->bdev->bd_dev);
888 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
889 && !test_bit(Faulty, &rdev2->flags))
890 d->raid_disk = rdev2->raid_disk;
892 d->raid_disk = rdev2->desc_nr; /* compatibility */
893 if (test_bit(Faulty, &rdev2->flags)) {
894 d->state = (1<<MD_DISK_FAULTY);
896 } else if (test_bit(In_sync, &rdev2->flags)) {
897 d->state = (1<<MD_DISK_ACTIVE);
898 d->state |= (1<<MD_DISK_SYNC);
906 if (test_bit(WriteMostly, &rdev2->flags))
907 d->state |= (1<<MD_DISK_WRITEMOSTLY);
909 /* now set the "removed" and "faulty" bits on any missing devices */
910 for (i=0 ; i < mddev->raid_disks ; i++) {
911 mdp_disk_t *d = &sb->disks[i];
912 if (d->state == 0 && d->number == 0) {
915 d->state = (1<<MD_DISK_REMOVED);
916 d->state |= (1<<MD_DISK_FAULTY);
920 sb->nr_disks = nr_disks;
921 sb->active_disks = active;
922 sb->working_disks = working;
923 sb->failed_disks = failed;
924 sb->spare_disks = spare;
926 sb->this_disk = sb->disks[rdev->desc_nr];
927 sb->sb_csum = calc_sb_csum(sb);
931 * version 1 superblock
934 static unsigned int calc_sb_1_csum(struct mdp_superblock_1 * sb)
936 unsigned int disk_csum, csum;
937 unsigned long long newcsum;
938 int size = 256 + le32_to_cpu(sb->max_dev)*2;
939 unsigned int *isuper = (unsigned int*)sb;
942 disk_csum = sb->sb_csum;
945 for (i=0; size>=4; size -= 4 )
946 newcsum += le32_to_cpu(*isuper++);
949 newcsum += le16_to_cpu(*(unsigned short*) isuper);
951 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
952 sb->sb_csum = disk_csum;
953 return cpu_to_le32(csum);
956 static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
958 struct mdp_superblock_1 *sb;
961 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
965 * Calculate the position of the superblock.
966 * It is always aligned to a 4K boundary and
967 * depeding on minor_version, it can be:
968 * 0: At least 8K, but less than 12K, from end of device
969 * 1: At start of device
970 * 2: 4K from start of device.
972 switch(minor_version) {
974 sb_offset = rdev->bdev->bd_inode->i_size >> 9;
976 sb_offset &= ~(sector_t)(4*2-1);
977 /* convert from sectors to K */
989 rdev->sb_offset = sb_offset;
991 /* superblock is rarely larger than 1K, but it can be larger,
992 * and it is safe to read 4k, so we do that
994 ret = read_disk_sb(rdev, 4096);
998 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1000 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1001 sb->major_version != cpu_to_le32(1) ||
1002 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1003 le64_to_cpu(sb->super_offset) != (rdev->sb_offset<<1) ||
1004 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
1007 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1008 printk("md: invalid superblock checksum on %s\n",
1009 bdevname(rdev->bdev,b));
1012 if (le64_to_cpu(sb->data_size) < 10) {
1013 printk("md: data_size too small on %s\n",
1014 bdevname(rdev->bdev,b));
1017 rdev->preferred_minor = 0xffff;
1018 rdev->data_offset = le64_to_cpu(sb->data_offset);
1019 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
1021 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
1022 bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
1023 if (rdev->sb_size & bmask)
1024 rdev-> sb_size = (rdev->sb_size | bmask)+1;
1030 struct mdp_superblock_1 *refsb =
1031 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1033 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1034 sb->level != refsb->level ||
1035 sb->layout != refsb->layout ||
1036 sb->chunksize != refsb->chunksize) {
1037 printk(KERN_WARNING "md: %s has strangely different"
1038 " superblock to %s\n",
1039 bdevname(rdev->bdev,b),
1040 bdevname(refdev->bdev,b2));
1043 ev1 = le64_to_cpu(sb->events);
1044 ev2 = le64_to_cpu(refsb->events);
1052 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
1054 rdev->size = rdev->sb_offset;
1055 if (rdev->size < le64_to_cpu(sb->data_size)/2)
1057 rdev->size = le64_to_cpu(sb->data_size)/2;
1058 if (le32_to_cpu(sb->chunksize))
1059 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
1061 if (le32_to_cpu(sb->size) > rdev->size*2)
1066 static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1068 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1070 rdev->raid_disk = -1;
1072 if (mddev->raid_disks == 0) {
1073 mddev->major_version = 1;
1074 mddev->patch_version = 0;
1075 mddev->persistent = 1;
1076 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
1077 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1078 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1079 mddev->level = le32_to_cpu(sb->level);
1080 mddev->clevel[0] = 0;
1081 mddev->layout = le32_to_cpu(sb->layout);
1082 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1083 mddev->size = le64_to_cpu(sb->size)/2;
1084 mddev->events = le64_to_cpu(sb->events);
1085 mddev->bitmap_offset = 0;
1086 mddev->default_bitmap_offset = 1024 >> 9;
1088 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1089 memcpy(mddev->uuid, sb->set_uuid, 16);
1091 mddev->max_disks = (4096-256)/2;
1093 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
1094 mddev->bitmap_file == NULL ) {
1095 if (mddev->level != 1 && mddev->level != 5 && mddev->level != 6
1096 && mddev->level != 10) {
1097 printk(KERN_WARNING "md: bitmaps not supported for this level.\n");
1100 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
1102 } else if (mddev->pers == NULL) {
1103 /* Insist of good event counter while assembling */
1104 __u64 ev1 = le64_to_cpu(sb->events);
1106 if (ev1 < mddev->events)
1108 } else if (mddev->bitmap) {
1109 /* If adding to array with a bitmap, then we can accept an
1110 * older device, but not too old.
1112 __u64 ev1 = le64_to_cpu(sb->events);
1113 if (ev1 < mddev->bitmap->events_cleared)
1115 } else /* just a hot-add of a new device, leave raid_disk at -1 */
1118 if (mddev->level != LEVEL_MULTIPATH) {
1120 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1121 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1123 case 0xffff: /* spare */
1125 case 0xfffe: /* faulty */
1126 set_bit(Faulty, &rdev->flags);
1129 set_bit(In_sync, &rdev->flags);
1130 rdev->raid_disk = role;
1133 if (sb->devflags & WriteMostly1)
1134 set_bit(WriteMostly, &rdev->flags);
1135 } else /* MULTIPATH are always insync */
1136 set_bit(In_sync, &rdev->flags);
1141 static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1143 struct mdp_superblock_1 *sb;
1144 struct list_head *tmp;
1147 /* make rdev->sb match mddev and rdev data. */
1149 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1151 sb->feature_map = 0;
1153 memset(sb->pad1, 0, sizeof(sb->pad1));
1154 memset(sb->pad2, 0, sizeof(sb->pad2));
1155 memset(sb->pad3, 0, sizeof(sb->pad3));
1157 sb->utime = cpu_to_le64((__u64)mddev->utime);
1158 sb->events = cpu_to_le64(mddev->events);
1160 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1162 sb->resync_offset = cpu_to_le64(0);
1164 sb->cnt_corrected_read = atomic_read(&rdev->corrected_errors);
1166 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
1167 sb->size = cpu_to_le64(mddev->size<<1);
1169 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1170 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
1171 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
1175 ITERATE_RDEV(mddev,rdev2,tmp)
1176 if (rdev2->desc_nr+1 > max_dev)
1177 max_dev = rdev2->desc_nr+1;
1179 sb->max_dev = cpu_to_le32(max_dev);
1180 for (i=0; i<max_dev;i++)
1181 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1183 ITERATE_RDEV(mddev,rdev2,tmp) {
1185 if (test_bit(Faulty, &rdev2->flags))
1186 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1187 else if (test_bit(In_sync, &rdev2->flags))
1188 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1190 sb->dev_roles[i] = cpu_to_le16(0xffff);
1193 sb->recovery_offset = cpu_to_le64(0); /* not supported yet */
1194 sb->sb_csum = calc_sb_1_csum(sb);
1198 static struct super_type super_types[] = {
1201 .owner = THIS_MODULE,
1202 .load_super = super_90_load,
1203 .validate_super = super_90_validate,
1204 .sync_super = super_90_sync,
1208 .owner = THIS_MODULE,
1209 .load_super = super_1_load,
1210 .validate_super = super_1_validate,
1211 .sync_super = super_1_sync,
1215 static mdk_rdev_t * match_dev_unit(mddev_t *mddev, mdk_rdev_t *dev)
1217 struct list_head *tmp;
1220 ITERATE_RDEV(mddev,rdev,tmp)
1221 if (rdev->bdev->bd_contains == dev->bdev->bd_contains)
1227 static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1229 struct list_head *tmp;
1232 ITERATE_RDEV(mddev1,rdev,tmp)
1233 if (match_dev_unit(mddev2, rdev))
1239 static LIST_HEAD(pending_raid_disks);
1241 static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1243 mdk_rdev_t *same_pdev;
1244 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1252 /* make sure rdev->size exceeds mddev->size */
1253 if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) {
1255 /* Cannot change size, so fail */
1258 mddev->size = rdev->size;
1260 same_pdev = match_dev_unit(mddev, rdev);
1263 "%s: WARNING: %s appears to be on the same physical"
1264 " disk as %s. True\n protection against single-disk"
1265 " failure might be compromised.\n",
1266 mdname(mddev), bdevname(rdev->bdev,b),
1267 bdevname(same_pdev->bdev,b2));
1269 /* Verify rdev->desc_nr is unique.
1270 * If it is -1, assign a free number, else
1271 * check number is not in use
1273 if (rdev->desc_nr < 0) {
1275 if (mddev->pers) choice = mddev->raid_disks;
1276 while (find_rdev_nr(mddev, choice))
1278 rdev->desc_nr = choice;
1280 if (find_rdev_nr(mddev, rdev->desc_nr))
1283 bdevname(rdev->bdev,b);
1284 if (kobject_set_name(&rdev->kobj, "dev-%s", b) < 0)
1286 while ( (s=strchr(rdev->kobj.k_name, '/')) != NULL)
1289 list_add(&rdev->same_set, &mddev->disks);
1290 rdev->mddev = mddev;
1291 printk(KERN_INFO "md: bind<%s>\n", b);
1293 rdev->kobj.parent = &mddev->kobj;
1294 kobject_add(&rdev->kobj);
1296 if (rdev->bdev->bd_part)
1297 ko = &rdev->bdev->bd_part->kobj;
1299 ko = &rdev->bdev->bd_disk->kobj;
1300 sysfs_create_link(&rdev->kobj, ko, "block");
1304 static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1306 char b[BDEVNAME_SIZE];
1311 list_del_init(&rdev->same_set);
1312 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1314 sysfs_remove_link(&rdev->kobj, "block");
1315 kobject_del(&rdev->kobj);
1319 * prevent the device from being mounted, repartitioned or
1320 * otherwise reused by a RAID array (or any other kernel
1321 * subsystem), by bd_claiming the device.
1323 static int lock_rdev(mdk_rdev_t *rdev, dev_t dev)
1326 struct block_device *bdev;
1327 char b[BDEVNAME_SIZE];
1329 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
1331 printk(KERN_ERR "md: could not open %s.\n",
1332 __bdevname(dev, b));
1333 return PTR_ERR(bdev);
1335 err = bd_claim(bdev, rdev);
1337 printk(KERN_ERR "md: could not bd_claim %s.\n",
1346 static void unlock_rdev(mdk_rdev_t *rdev)
1348 struct block_device *bdev = rdev->bdev;
1356 void md_autodetect_dev(dev_t dev);
1358 static void export_rdev(mdk_rdev_t * rdev)
1360 char b[BDEVNAME_SIZE];
1361 printk(KERN_INFO "md: export_rdev(%s)\n",
1362 bdevname(rdev->bdev,b));
1366 list_del_init(&rdev->same_set);
1368 md_autodetect_dev(rdev->bdev->bd_dev);
1371 kobject_put(&rdev->kobj);
1374 static void kick_rdev_from_array(mdk_rdev_t * rdev)
1376 unbind_rdev_from_array(rdev);
1380 static void export_array(mddev_t *mddev)
1382 struct list_head *tmp;
1385 ITERATE_RDEV(mddev,rdev,tmp) {
1390 kick_rdev_from_array(rdev);
1392 if (!list_empty(&mddev->disks))
1394 mddev->raid_disks = 0;
1395 mddev->major_version = 0;
1398 static void print_desc(mdp_disk_t *desc)
1400 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1401 desc->major,desc->minor,desc->raid_disk,desc->state);
1404 static void print_sb(mdp_super_t *sb)
1409 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1410 sb->major_version, sb->minor_version, sb->patch_version,
1411 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1413 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1414 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1415 sb->md_minor, sb->layout, sb->chunk_size);
1416 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1417 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1418 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1419 sb->failed_disks, sb->spare_disks,
1420 sb->sb_csum, (unsigned long)sb->events_lo);
1423 for (i = 0; i < MD_SB_DISKS; i++) {
1426 desc = sb->disks + i;
1427 if (desc->number || desc->major || desc->minor ||
1428 desc->raid_disk || (desc->state && (desc->state != 4))) {
1429 printk(" D %2d: ", i);
1433 printk(KERN_INFO "md: THIS: ");
1434 print_desc(&sb->this_disk);
1438 static void print_rdev(mdk_rdev_t *rdev)
1440 char b[BDEVNAME_SIZE];
1441 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1442 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
1443 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1445 if (rdev->sb_loaded) {
1446 printk(KERN_INFO "md: rdev superblock:\n");
1447 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1449 printk(KERN_INFO "md: no rdev superblock!\n");
1452 void md_print_devices(void)
1454 struct list_head *tmp, *tmp2;
1457 char b[BDEVNAME_SIZE];
1460 printk("md: **********************************\n");
1461 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1462 printk("md: **********************************\n");
1463 ITERATE_MDDEV(mddev,tmp) {
1466 bitmap_print_sb(mddev->bitmap);
1468 printk("%s: ", mdname(mddev));
1469 ITERATE_RDEV(mddev,rdev,tmp2)
1470 printk("<%s>", bdevname(rdev->bdev,b));
1473 ITERATE_RDEV(mddev,rdev,tmp2)
1476 printk("md: **********************************\n");
1481 static void sync_sbs(mddev_t * mddev)
1484 struct list_head *tmp;
1486 ITERATE_RDEV(mddev,rdev,tmp) {
1487 super_types[mddev->major_version].
1488 sync_super(mddev, rdev);
1489 rdev->sb_loaded = 1;
1493 static void md_update_sb(mddev_t * mddev)
1496 struct list_head *tmp;
1501 spin_lock_irq(&mddev->write_lock);
1502 sync_req = mddev->in_sync;
1503 mddev->utime = get_seconds();
1506 if (!mddev->events) {
1508 * oops, this 64-bit counter should never wrap.
1509 * Either we are in around ~1 trillion A.C., assuming
1510 * 1 reboot per second, or we have a bug:
1515 mddev->sb_dirty = 2;
1519 * do not write anything to disk if using
1520 * nonpersistent superblocks
1522 if (!mddev->persistent) {
1523 mddev->sb_dirty = 0;
1524 spin_unlock_irq(&mddev->write_lock);
1525 wake_up(&mddev->sb_wait);
1528 spin_unlock_irq(&mddev->write_lock);
1531 "md: updating %s RAID superblock on device (in sync %d)\n",
1532 mdname(mddev),mddev->in_sync);
1534 err = bitmap_update_sb(mddev->bitmap);
1535 ITERATE_RDEV(mddev,rdev,tmp) {
1536 char b[BDEVNAME_SIZE];
1537 dprintk(KERN_INFO "md: ");
1538 if (test_bit(Faulty, &rdev->flags))
1539 dprintk("(skipping faulty ");
1541 dprintk("%s ", bdevname(rdev->bdev,b));
1542 if (!test_bit(Faulty, &rdev->flags)) {
1543 md_super_write(mddev,rdev,
1544 rdev->sb_offset<<1, rdev->sb_size,
1546 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1547 bdevname(rdev->bdev,b),
1548 (unsigned long long)rdev->sb_offset);
1552 if (mddev->level == LEVEL_MULTIPATH)
1553 /* only need to write one superblock... */
1556 md_super_wait(mddev);
1557 /* if there was a failure, sb_dirty was set to 1, and we re-write super */
1559 spin_lock_irq(&mddev->write_lock);
1560 if (mddev->in_sync != sync_req|| mddev->sb_dirty == 1) {
1561 /* have to write it out again */
1562 spin_unlock_irq(&mddev->write_lock);
1565 mddev->sb_dirty = 0;
1566 spin_unlock_irq(&mddev->write_lock);
1567 wake_up(&mddev->sb_wait);
1571 /* words written to sysfs files may, or my not, be \n terminated.
1572 * We want to accept with case. For this we use cmd_match.
1574 static int cmd_match(const char *cmd, const char *str)
1576 /* See if cmd, written into a sysfs file, matches
1577 * str. They must either be the same, or cmd can
1578 * have a trailing newline
1580 while (*cmd && *str && *cmd == *str) {
1591 struct rdev_sysfs_entry {
1592 struct attribute attr;
1593 ssize_t (*show)(mdk_rdev_t *, char *);
1594 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1598 state_show(mdk_rdev_t *rdev, char *page)
1603 if (test_bit(Faulty, &rdev->flags)) {
1604 len+= sprintf(page+len, "%sfaulty",sep);
1607 if (test_bit(In_sync, &rdev->flags)) {
1608 len += sprintf(page+len, "%sin_sync",sep);
1611 if (!test_bit(Faulty, &rdev->flags) &&
1612 !test_bit(In_sync, &rdev->flags)) {
1613 len += sprintf(page+len, "%sspare", sep);
1616 return len+sprintf(page+len, "\n");
1619 static struct rdev_sysfs_entry
1620 rdev_state = __ATTR_RO(state);
1623 super_show(mdk_rdev_t *rdev, char *page)
1625 if (rdev->sb_loaded && rdev->sb_size) {
1626 memcpy(page, page_address(rdev->sb_page), rdev->sb_size);
1627 return rdev->sb_size;
1631 static struct rdev_sysfs_entry rdev_super = __ATTR_RO(super);
1634 errors_show(mdk_rdev_t *rdev, char *page)
1636 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
1640 errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1643 unsigned long n = simple_strtoul(buf, &e, 10);
1644 if (*buf && (*e == 0 || *e == '\n')) {
1645 atomic_set(&rdev->corrected_errors, n);
1650 static struct rdev_sysfs_entry rdev_errors =
1651 __ATTR(errors, 0644, errors_show, errors_store);
1654 slot_show(mdk_rdev_t *rdev, char *page)
1656 if (rdev->raid_disk < 0)
1657 return sprintf(page, "none\n");
1659 return sprintf(page, "%d\n", rdev->raid_disk);
1663 slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1666 int slot = simple_strtoul(buf, &e, 10);
1667 if (strncmp(buf, "none", 4)==0)
1669 else if (e==buf || (*e && *e!= '\n'))
1671 if (rdev->mddev->pers)
1672 /* Cannot set slot in active array (yet) */
1674 if (slot >= rdev->mddev->raid_disks)
1676 rdev->raid_disk = slot;
1677 /* assume it is working */
1679 set_bit(In_sync, &rdev->flags);
1684 static struct rdev_sysfs_entry rdev_slot =
1685 __ATTR(slot, 0644, slot_show, slot_store);
1688 offset_show(mdk_rdev_t *rdev, char *page)
1690 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
1694 offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1697 unsigned long long offset = simple_strtoull(buf, &e, 10);
1698 if (e==buf || (*e && *e != '\n'))
1700 if (rdev->mddev->pers)
1702 rdev->data_offset = offset;
1706 static struct rdev_sysfs_entry rdev_offset =
1707 __ATTR(offset, 0644, offset_show, offset_store);
1710 rdev_size_show(mdk_rdev_t *rdev, char *page)
1712 return sprintf(page, "%llu\n", (unsigned long long)rdev->size);
1716 rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1719 unsigned long long size = simple_strtoull(buf, &e, 10);
1720 if (e==buf || (*e && *e != '\n'))
1722 if (rdev->mddev->pers)
1725 if (size < rdev->mddev->size || rdev->mddev->size == 0)
1726 rdev->mddev->size = size;
1730 static struct rdev_sysfs_entry rdev_size =
1731 __ATTR(size, 0644, rdev_size_show, rdev_size_store);
1733 static struct attribute *rdev_default_attrs[] = {
1743 rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
1745 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1746 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1750 return entry->show(rdev, page);
1754 rdev_attr_store(struct kobject *kobj, struct attribute *attr,
1755 const char *page, size_t length)
1757 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1758 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1762 return entry->store(rdev, page, length);
1765 static void rdev_free(struct kobject *ko)
1767 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
1770 static struct sysfs_ops rdev_sysfs_ops = {
1771 .show = rdev_attr_show,
1772 .store = rdev_attr_store,
1774 static struct kobj_type rdev_ktype = {
1775 .release = rdev_free,
1776 .sysfs_ops = &rdev_sysfs_ops,
1777 .default_attrs = rdev_default_attrs,
1781 * Import a device. If 'super_format' >= 0, then sanity check the superblock
1783 * mark the device faulty if:
1785 * - the device is nonexistent (zero size)
1786 * - the device has no valid superblock
1788 * a faulty rdev _never_ has rdev->sb set.
1790 static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
1792 char b[BDEVNAME_SIZE];
1797 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
1799 printk(KERN_ERR "md: could not alloc mem for new device!\n");
1800 return ERR_PTR(-ENOMEM);
1803 if ((err = alloc_disk_sb(rdev)))
1806 err = lock_rdev(rdev, newdev);
1810 rdev->kobj.parent = NULL;
1811 rdev->kobj.ktype = &rdev_ktype;
1812 kobject_init(&rdev->kobj);
1816 rdev->data_offset = 0;
1817 atomic_set(&rdev->nr_pending, 0);
1818 atomic_set(&rdev->read_errors, 0);
1819 atomic_set(&rdev->corrected_errors, 0);
1821 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
1824 "md: %s has zero or unknown size, marking faulty!\n",
1825 bdevname(rdev->bdev,b));
1830 if (super_format >= 0) {
1831 err = super_types[super_format].
1832 load_super(rdev, NULL, super_minor);
1833 if (err == -EINVAL) {
1835 "md: %s has invalid sb, not importing!\n",
1836 bdevname(rdev->bdev,b));
1841 "md: could not read %s's sb, not importing!\n",
1842 bdevname(rdev->bdev,b));
1846 INIT_LIST_HEAD(&rdev->same_set);
1851 if (rdev->sb_page) {
1857 return ERR_PTR(err);
1861 * Check a full RAID array for plausibility
1865 static void analyze_sbs(mddev_t * mddev)
1868 struct list_head *tmp;
1869 mdk_rdev_t *rdev, *freshest;
1870 char b[BDEVNAME_SIZE];
1873 ITERATE_RDEV(mddev,rdev,tmp)
1874 switch (super_types[mddev->major_version].
1875 load_super(rdev, freshest, mddev->minor_version)) {
1883 "md: fatal superblock inconsistency in %s"
1884 " -- removing from array\n",
1885 bdevname(rdev->bdev,b));
1886 kick_rdev_from_array(rdev);
1890 super_types[mddev->major_version].
1891 validate_super(mddev, freshest);
1894 ITERATE_RDEV(mddev,rdev,tmp) {
1895 if (rdev != freshest)
1896 if (super_types[mddev->major_version].
1897 validate_super(mddev, rdev)) {
1898 printk(KERN_WARNING "md: kicking non-fresh %s"
1900 bdevname(rdev->bdev,b));
1901 kick_rdev_from_array(rdev);
1904 if (mddev->level == LEVEL_MULTIPATH) {
1905 rdev->desc_nr = i++;
1906 rdev->raid_disk = rdev->desc_nr;
1907 set_bit(In_sync, &rdev->flags);
1913 if (mddev->recovery_cp != MaxSector &&
1915 printk(KERN_ERR "md: %s: raid array is not clean"
1916 " -- starting background reconstruction\n",
1922 level_show(mddev_t *mddev, char *page)
1924 struct mdk_personality *p = mddev->pers;
1926 return sprintf(page, "%s\n", p->name);
1927 else if (mddev->clevel[0])
1928 return sprintf(page, "%s\n", mddev->clevel);
1929 else if (mddev->level != LEVEL_NONE)
1930 return sprintf(page, "%d\n", mddev->level);
1936 level_store(mddev_t *mddev, const char *buf, size_t len)
1943 if (len >= sizeof(mddev->clevel))
1945 strncpy(mddev->clevel, buf, len);
1946 if (mddev->clevel[len-1] == '\n')
1948 mddev->clevel[len] = 0;
1949 mddev->level = LEVEL_NONE;
1953 static struct md_sysfs_entry md_level =
1954 __ATTR(level, 0644, level_show, level_store);
1957 raid_disks_show(mddev_t *mddev, char *page)
1959 if (mddev->raid_disks == 0)
1961 return sprintf(page, "%d\n", mddev->raid_disks);
1964 static int update_raid_disks(mddev_t *mddev, int raid_disks);
1967 raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
1969 /* can only set raid_disks if array is not yet active */
1972 unsigned long n = simple_strtoul(buf, &e, 10);
1974 if (!*buf || (*e && *e != '\n'))
1978 rv = update_raid_disks(mddev, n);
1980 mddev->raid_disks = n;
1981 return rv ? rv : len;
1983 static struct md_sysfs_entry md_raid_disks =
1984 __ATTR(raid_disks, 0644, raid_disks_show, raid_disks_store);
1987 chunk_size_show(mddev_t *mddev, char *page)
1989 return sprintf(page, "%d\n", mddev->chunk_size);
1993 chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
1995 /* can only set chunk_size if array is not yet active */
1997 unsigned long n = simple_strtoul(buf, &e, 10);
2001 if (!*buf || (*e && *e != '\n'))
2004 mddev->chunk_size = n;
2007 static struct md_sysfs_entry md_chunk_size =
2008 __ATTR(chunk_size, 0644, chunk_size_show, chunk_size_store);
2011 null_show(mddev_t *mddev, char *page)
2017 new_dev_store(mddev_t *mddev, const char *buf, size_t len)
2019 /* buf must be %d:%d\n? giving major and minor numbers */
2020 /* The new device is added to the array.
2021 * If the array has a persistent superblock, we read the
2022 * superblock to initialise info and check validity.
2023 * Otherwise, only checking done is that in bind_rdev_to_array,
2024 * which mainly checks size.
2027 int major = simple_strtoul(buf, &e, 10);
2033 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
2035 minor = simple_strtoul(e+1, &e, 10);
2036 if (*e && *e != '\n')
2038 dev = MKDEV(major, minor);
2039 if (major != MAJOR(dev) ||
2040 minor != MINOR(dev))
2044 if (mddev->persistent) {
2045 rdev = md_import_device(dev, mddev->major_version,
2046 mddev->minor_version);
2047 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
2048 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2049 mdk_rdev_t, same_set);
2050 err = super_types[mddev->major_version]
2051 .load_super(rdev, rdev0, mddev->minor_version);
2056 rdev = md_import_device(dev, -1, -1);
2059 return PTR_ERR(rdev);
2060 err = bind_rdev_to_array(rdev, mddev);
2064 return err ? err : len;
2067 static struct md_sysfs_entry md_new_device =
2068 __ATTR(new_dev, 0200, null_show, new_dev_store);
2071 size_show(mddev_t *mddev, char *page)
2073 return sprintf(page, "%llu\n", (unsigned long long)mddev->size);
2076 static int update_size(mddev_t *mddev, unsigned long size);
2079 size_store(mddev_t *mddev, const char *buf, size_t len)
2081 /* If array is inactive, we can reduce the component size, but
2082 * not increase it (except from 0).
2083 * If array is active, we can try an on-line resize
2087 unsigned long long size = simple_strtoull(buf, &e, 10);
2088 if (!*buf || *buf == '\n' ||
2093 err = update_size(mddev, size);
2094 md_update_sb(mddev);
2096 if (mddev->size == 0 ||
2102 return err ? err : len;
2105 static struct md_sysfs_entry md_size =
2106 __ATTR(component_size, 0644, size_show, size_store);
2110 * This is either 'none' for arrays with externally managed metadata,
2111 * or N.M for internally known formats
2114 metadata_show(mddev_t *mddev, char *page)
2116 if (mddev->persistent)
2117 return sprintf(page, "%d.%d\n",
2118 mddev->major_version, mddev->minor_version);
2120 return sprintf(page, "none\n");
2124 metadata_store(mddev_t *mddev, const char *buf, size_t len)
2128 if (!list_empty(&mddev->disks))
2131 if (cmd_match(buf, "none")) {
2132 mddev->persistent = 0;
2133 mddev->major_version = 0;
2134 mddev->minor_version = 90;
2137 major = simple_strtoul(buf, &e, 10);
2138 if (e==buf || *e != '.')
2141 minor = simple_strtoul(buf, &e, 10);
2142 if (e==buf || *e != '\n')
2144 if (major >= sizeof(super_types)/sizeof(super_types[0]) ||
2145 super_types[major].name == NULL)
2147 mddev->major_version = major;
2148 mddev->minor_version = minor;
2149 mddev->persistent = 1;
2153 static struct md_sysfs_entry md_metadata =
2154 __ATTR(metadata_version, 0644, metadata_show, metadata_store);
2157 action_show(mddev_t *mddev, char *page)
2159 char *type = "idle";
2160 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2161 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) {
2162 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2163 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2165 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2172 return sprintf(page, "%s\n", type);
2176 action_store(mddev_t *mddev, const char *page, size_t len)
2178 if (!mddev->pers || !mddev->pers->sync_request)
2181 if (cmd_match(page, "idle")) {
2182 if (mddev->sync_thread) {
2183 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2184 md_unregister_thread(mddev->sync_thread);
2185 mddev->sync_thread = NULL;
2186 mddev->recovery = 0;
2188 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2189 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
2191 else if (cmd_match(page, "resync") || cmd_match(page, "recover"))
2192 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2194 if (cmd_match(page, "check"))
2195 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
2196 else if (cmd_match(page, "repair"))
2198 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
2199 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
2201 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2202 md_wakeup_thread(mddev->thread);
2207 mismatch_cnt_show(mddev_t *mddev, char *page)
2209 return sprintf(page, "%llu\n",
2210 (unsigned long long) mddev->resync_mismatches);
2213 static struct md_sysfs_entry
2214 md_scan_mode = __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
2217 static struct md_sysfs_entry
2218 md_mismatches = __ATTR_RO(mismatch_cnt);
2221 sync_min_show(mddev_t *mddev, char *page)
2223 return sprintf(page, "%d (%s)\n", speed_min(mddev),
2224 mddev->sync_speed_min ? "local": "system");
2228 sync_min_store(mddev_t *mddev, const char *buf, size_t len)
2232 if (strncmp(buf, "system", 6)==0) {
2233 mddev->sync_speed_min = 0;
2236 min = simple_strtoul(buf, &e, 10);
2237 if (buf == e || (*e && *e != '\n') || min <= 0)
2239 mddev->sync_speed_min = min;
2243 static struct md_sysfs_entry md_sync_min =
2244 __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
2247 sync_max_show(mddev_t *mddev, char *page)
2249 return sprintf(page, "%d (%s)\n", speed_max(mddev),
2250 mddev->sync_speed_max ? "local": "system");
2254 sync_max_store(mddev_t *mddev, const char *buf, size_t len)
2258 if (strncmp(buf, "system", 6)==0) {
2259 mddev->sync_speed_max = 0;
2262 max = simple_strtoul(buf, &e, 10);
2263 if (buf == e || (*e && *e != '\n') || max <= 0)
2265 mddev->sync_speed_max = max;
2269 static struct md_sysfs_entry md_sync_max =
2270 __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
2274 sync_speed_show(mddev_t *mddev, char *page)
2276 unsigned long resync, dt, db;
2277 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
2278 dt = ((jiffies - mddev->resync_mark) / HZ);
2280 db = resync - (mddev->resync_mark_cnt);
2281 return sprintf(page, "%ld\n", db/dt/2); /* K/sec */
2284 static struct md_sysfs_entry
2285 md_sync_speed = __ATTR_RO(sync_speed);
2288 sync_completed_show(mddev_t *mddev, char *page)
2290 unsigned long max_blocks, resync;
2292 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2293 max_blocks = mddev->resync_max_sectors;
2295 max_blocks = mddev->size << 1;
2297 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
2298 return sprintf(page, "%lu / %lu\n", resync, max_blocks);
2301 static struct md_sysfs_entry
2302 md_sync_completed = __ATTR_RO(sync_completed);
2304 static struct attribute *md_default_attrs[] = {
2306 &md_raid_disks.attr,
2307 &md_chunk_size.attr,
2310 &md_new_device.attr,
2314 static struct attribute *md_redundancy_attrs[] = {
2316 &md_mismatches.attr,
2319 &md_sync_speed.attr,
2320 &md_sync_completed.attr,
2323 static struct attribute_group md_redundancy_group = {
2325 .attrs = md_redundancy_attrs,
2330 md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2332 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
2333 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
2339 rv = entry->show(mddev, page);
2340 mddev_unlock(mddev);
2345 md_attr_store(struct kobject *kobj, struct attribute *attr,
2346 const char *page, size_t length)
2348 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
2349 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
2355 rv = entry->store(mddev, page, length);
2356 mddev_unlock(mddev);
2360 static void md_free(struct kobject *ko)
2362 mddev_t *mddev = container_of(ko, mddev_t, kobj);
2366 static struct sysfs_ops md_sysfs_ops = {
2367 .show = md_attr_show,
2368 .store = md_attr_store,
2370 static struct kobj_type md_ktype = {
2372 .sysfs_ops = &md_sysfs_ops,
2373 .default_attrs = md_default_attrs,
2378 static struct kobject *md_probe(dev_t dev, int *part, void *data)
2380 static DECLARE_MUTEX(disks_sem);
2381 mddev_t *mddev = mddev_find(dev);
2382 struct gendisk *disk;
2383 int partitioned = (MAJOR(dev) != MD_MAJOR);
2384 int shift = partitioned ? MdpMinorShift : 0;
2385 int unit = MINOR(dev) >> shift;
2391 if (mddev->gendisk) {
2396 disk = alloc_disk(1 << shift);
2402 disk->major = MAJOR(dev);
2403 disk->first_minor = unit << shift;
2405 sprintf(disk->disk_name, "md_d%d", unit);
2406 sprintf(disk->devfs_name, "md/d%d", unit);
2408 sprintf(disk->disk_name, "md%d", unit);
2409 sprintf(disk->devfs_name, "md/%d", unit);
2411 disk->fops = &md_fops;
2412 disk->private_data = mddev;
2413 disk->queue = mddev->queue;
2415 mddev->gendisk = disk;
2417 mddev->kobj.parent = &disk->kobj;
2418 mddev->kobj.k_name = NULL;
2419 snprintf(mddev->kobj.name, KOBJ_NAME_LEN, "%s", "md");
2420 mddev->kobj.ktype = &md_ktype;
2421 kobject_register(&mddev->kobj);
2425 void md_wakeup_thread(mdk_thread_t *thread);
2427 static void md_safemode_timeout(unsigned long data)
2429 mddev_t *mddev = (mddev_t *) data;
2431 mddev->safemode = 1;
2432 md_wakeup_thread(mddev->thread);
2435 static int start_dirty_degraded;
2437 static int do_md_run(mddev_t * mddev)
2441 struct list_head *tmp;
2443 struct gendisk *disk;
2444 struct mdk_personality *pers;
2445 char b[BDEVNAME_SIZE];
2447 if (list_empty(&mddev->disks))
2448 /* cannot run an array with no devices.. */
2455 * Analyze all RAID superblock(s)
2457 if (!mddev->raid_disks)
2460 chunk_size = mddev->chunk_size;
2463 if (chunk_size > MAX_CHUNK_SIZE) {
2464 printk(KERN_ERR "too big chunk_size: %d > %d\n",
2465 chunk_size, MAX_CHUNK_SIZE);
2469 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
2471 if ( (1 << ffz(~chunk_size)) != chunk_size) {
2472 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
2475 if (chunk_size < PAGE_SIZE) {
2476 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
2477 chunk_size, PAGE_SIZE);
2481 /* devices must have minimum size of one chunk */
2482 ITERATE_RDEV(mddev,rdev,tmp) {
2483 if (test_bit(Faulty, &rdev->flags))
2485 if (rdev->size < chunk_size / 1024) {
2487 "md: Dev %s smaller than chunk_size:"
2489 bdevname(rdev->bdev,b),
2490 (unsigned long long)rdev->size,
2498 if (mddev->level != LEVEL_NONE)
2499 request_module("md-level-%d", mddev->level);
2500 else if (mddev->clevel[0])
2501 request_module("md-%s", mddev->clevel);
2505 * Drop all container device buffers, from now on
2506 * the only valid external interface is through the md
2508 * Also find largest hardsector size
2510 ITERATE_RDEV(mddev,rdev,tmp) {
2511 if (test_bit(Faulty, &rdev->flags))
2513 sync_blockdev(rdev->bdev);
2514 invalidate_bdev(rdev->bdev, 0);
2517 md_probe(mddev->unit, NULL, NULL);
2518 disk = mddev->gendisk;
2522 spin_lock(&pers_lock);
2523 pers = find_pers(mddev->level, mddev->clevel);
2524 if (!pers || !try_module_get(pers->owner)) {
2525 spin_unlock(&pers_lock);
2526 if (mddev->level != LEVEL_NONE)
2527 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
2530 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
2535 spin_unlock(&pers_lock);
2536 mddev->level = pers->level;
2537 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
2539 mddev->recovery = 0;
2540 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
2541 mddev->barriers_work = 1;
2542 mddev->ok_start_degraded = start_dirty_degraded;
2545 mddev->ro = 2; /* read-only, but switch on first write */
2547 err = mddev->pers->run(mddev);
2548 if (!err && mddev->pers->sync_request) {
2549 err = bitmap_create(mddev);
2551 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
2552 mdname(mddev), err);
2553 mddev->pers->stop(mddev);
2557 printk(KERN_ERR "md: pers->run() failed ...\n");
2558 module_put(mddev->pers->owner);
2560 bitmap_destroy(mddev);
2563 if (mddev->pers->sync_request)
2564 sysfs_create_group(&mddev->kobj, &md_redundancy_group);
2565 else if (mddev->ro == 2) /* auto-readonly not meaningful */
2568 atomic_set(&mddev->writes_pending,0);
2569 mddev->safemode = 0;
2570 mddev->safemode_timer.function = md_safemode_timeout;
2571 mddev->safemode_timer.data = (unsigned long) mddev;
2572 mddev->safemode_delay = (20 * HZ)/1000 +1; /* 20 msec delay */
2575 ITERATE_RDEV(mddev,rdev,tmp)
2576 if (rdev->raid_disk >= 0) {
2578 sprintf(nm, "rd%d", rdev->raid_disk);
2579 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
2582 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2583 md_wakeup_thread(mddev->thread);
2585 if (mddev->sb_dirty)
2586 md_update_sb(mddev);
2588 set_capacity(disk, mddev->array_size<<1);
2590 /* If we call blk_queue_make_request here, it will
2591 * re-initialise max_sectors etc which may have been
2592 * refined inside -> run. So just set the bits we need to set.
2593 * Most initialisation happended when we called
2594 * blk_queue_make_request(..., md_fail_request)
2597 mddev->queue->queuedata = mddev;
2598 mddev->queue->make_request_fn = mddev->pers->make_request;
2601 md_new_event(mddev);
2605 static int restart_array(mddev_t *mddev)
2607 struct gendisk *disk = mddev->gendisk;
2611 * Complain if it has no devices
2614 if (list_empty(&mddev->disks))
2622 mddev->safemode = 0;
2624 set_disk_ro(disk, 0);
2626 printk(KERN_INFO "md: %s switched to read-write mode.\n",
2629 * Kick recovery or resync if necessary
2631 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2632 md_wakeup_thread(mddev->thread);
2635 printk(KERN_ERR "md: %s has no personality assigned.\n",
2644 static int do_md_stop(mddev_t * mddev, int ro)
2647 struct gendisk *disk = mddev->gendisk;
2650 if (atomic_read(&mddev->active)>2) {
2651 printk("md: %s still in use.\n",mdname(mddev));
2655 if (mddev->sync_thread) {
2656 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2657 md_unregister_thread(mddev->sync_thread);
2658 mddev->sync_thread = NULL;
2661 del_timer_sync(&mddev->safemode_timer);
2663 invalidate_partition(disk, 0);
2671 bitmap_flush(mddev);
2672 md_super_wait(mddev);
2674 set_disk_ro(disk, 0);
2675 blk_queue_make_request(mddev->queue, md_fail_request);
2676 mddev->pers->stop(mddev);
2677 if (mddev->pers->sync_request)
2678 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
2680 module_put(mddev->pers->owner);
2685 if (!mddev->in_sync) {
2686 /* mark array as shutdown cleanly */
2688 md_update_sb(mddev);
2691 set_disk_ro(disk, 1);
2695 * Free resources if final stop
2699 struct list_head *tmp;
2700 struct gendisk *disk;
2701 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
2703 bitmap_destroy(mddev);
2704 if (mddev->bitmap_file) {
2705 atomic_set(&mddev->bitmap_file->f_dentry->d_inode->i_writecount, 1);
2706 fput(mddev->bitmap_file);
2707 mddev->bitmap_file = NULL;
2709 mddev->bitmap_offset = 0;
2711 ITERATE_RDEV(mddev,rdev,tmp)
2712 if (rdev->raid_disk >= 0) {
2714 sprintf(nm, "rd%d", rdev->raid_disk);
2715 sysfs_remove_link(&mddev->kobj, nm);
2718 export_array(mddev);
2720 mddev->array_size = 0;
2721 disk = mddev->gendisk;
2723 set_capacity(disk, 0);
2726 printk(KERN_INFO "md: %s switched to read-only mode.\n",
2729 md_new_event(mddev);
2734 static void autorun_array(mddev_t *mddev)
2737 struct list_head *tmp;
2740 if (list_empty(&mddev->disks))
2743 printk(KERN_INFO "md: running: ");
2745 ITERATE_RDEV(mddev,rdev,tmp) {
2746 char b[BDEVNAME_SIZE];
2747 printk("<%s>", bdevname(rdev->bdev,b));
2751 err = do_md_run (mddev);
2753 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
2754 do_md_stop (mddev, 0);
2759 * lets try to run arrays based on all disks that have arrived
2760 * until now. (those are in pending_raid_disks)
2762 * the method: pick the first pending disk, collect all disks with
2763 * the same UUID, remove all from the pending list and put them into
2764 * the 'same_array' list. Then order this list based on superblock
2765 * update time (freshest comes first), kick out 'old' disks and
2766 * compare superblocks. If everything's fine then run it.
2768 * If "unit" is allocated, then bump its reference count
2770 static void autorun_devices(int part)
2772 struct list_head candidates;
2773 struct list_head *tmp;
2774 mdk_rdev_t *rdev0, *rdev;
2776 char b[BDEVNAME_SIZE];
2778 printk(KERN_INFO "md: autorun ...\n");
2779 while (!list_empty(&pending_raid_disks)) {
2781 rdev0 = list_entry(pending_raid_disks.next,
2782 mdk_rdev_t, same_set);
2784 printk(KERN_INFO "md: considering %s ...\n",
2785 bdevname(rdev0->bdev,b));
2786 INIT_LIST_HEAD(&candidates);
2787 ITERATE_RDEV_PENDING(rdev,tmp)
2788 if (super_90_load(rdev, rdev0, 0) >= 0) {
2789 printk(KERN_INFO "md: adding %s ...\n",
2790 bdevname(rdev->bdev,b));
2791 list_move(&rdev->same_set, &candidates);
2794 * now we have a set of devices, with all of them having
2795 * mostly sane superblocks. It's time to allocate the
2798 if (rdev0->preferred_minor < 0 || rdev0->preferred_minor >= MAX_MD_DEVS) {
2799 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
2800 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
2804 dev = MKDEV(mdp_major,
2805 rdev0->preferred_minor << MdpMinorShift);
2807 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
2809 md_probe(dev, NULL, NULL);
2810 mddev = mddev_find(dev);
2813 "md: cannot allocate memory for md drive.\n");
2816 if (mddev_lock(mddev))
2817 printk(KERN_WARNING "md: %s locked, cannot run\n",
2819 else if (mddev->raid_disks || mddev->major_version
2820 || !list_empty(&mddev->disks)) {
2822 "md: %s already running, cannot run %s\n",
2823 mdname(mddev), bdevname(rdev0->bdev,b));
2824 mddev_unlock(mddev);
2826 printk(KERN_INFO "md: created %s\n", mdname(mddev));
2827 ITERATE_RDEV_GENERIC(candidates,rdev,tmp) {
2828 list_del_init(&rdev->same_set);
2829 if (bind_rdev_to_array(rdev, mddev))
2832 autorun_array(mddev);
2833 mddev_unlock(mddev);
2835 /* on success, candidates will be empty, on error
2838 ITERATE_RDEV_GENERIC(candidates,rdev,tmp)
2842 printk(KERN_INFO "md: ... autorun DONE.\n");
2846 * import RAID devices based on one partition
2847 * if possible, the array gets run as well.
2850 static int autostart_array(dev_t startdev)
2852 char b[BDEVNAME_SIZE];
2853 int err = -EINVAL, i;
2854 mdp_super_t *sb = NULL;
2855 mdk_rdev_t *start_rdev = NULL, *rdev;
2857 start_rdev = md_import_device(startdev, 0, 0);
2858 if (IS_ERR(start_rdev))
2862 /* NOTE: this can only work for 0.90.0 superblocks */
2863 sb = (mdp_super_t*)page_address(start_rdev->sb_page);
2864 if (sb->major_version != 0 ||
2865 sb->minor_version != 90 ) {
2866 printk(KERN_WARNING "md: can only autostart 0.90.0 arrays\n");
2867 export_rdev(start_rdev);
2871 if (test_bit(Faulty, &start_rdev->flags)) {
2873 "md: can not autostart based on faulty %s!\n",
2874 bdevname(start_rdev->bdev,b));
2875 export_rdev(start_rdev);
2878 list_add(&start_rdev->same_set, &pending_raid_disks);
2880 for (i = 0; i < MD_SB_DISKS; i++) {
2881 mdp_disk_t *desc = sb->disks + i;
2882 dev_t dev = MKDEV(desc->major, desc->minor);
2886 if (dev == startdev)
2888 if (MAJOR(dev) != desc->major || MINOR(dev) != desc->minor)
2890 rdev = md_import_device(dev, 0, 0);
2894 list_add(&rdev->same_set, &pending_raid_disks);
2898 * possibly return codes
2906 static int get_version(void __user * arg)
2910 ver.major = MD_MAJOR_VERSION;
2911 ver.minor = MD_MINOR_VERSION;
2912 ver.patchlevel = MD_PATCHLEVEL_VERSION;
2914 if (copy_to_user(arg, &ver, sizeof(ver)))
2920 static int get_array_info(mddev_t * mddev, void __user * arg)
2922 mdu_array_info_t info;
2923 int nr,working,active,failed,spare;
2925 struct list_head *tmp;
2927 nr=working=active=failed=spare=0;
2928 ITERATE_RDEV(mddev,rdev,tmp) {
2930 if (test_bit(Faulty, &rdev->flags))
2934 if (test_bit(In_sync, &rdev->flags))
2941 info.major_version = mddev->major_version;
2942 info.minor_version = mddev->minor_version;
2943 info.patch_version = MD_PATCHLEVEL_VERSION;
2944 info.ctime = mddev->ctime;
2945 info.level = mddev->level;
2946 info.size = mddev->size;
2947 if (info.size != mddev->size) /* overflow */
2950 info.raid_disks = mddev->raid_disks;
2951 info.md_minor = mddev->md_minor;
2952 info.not_persistent= !mddev->persistent;
2954 info.utime = mddev->utime;
2957 info.state = (1<<MD_SB_CLEAN);
2958 if (mddev->bitmap && mddev->bitmap_offset)
2959 info.state = (1<<MD_SB_BITMAP_PRESENT);
2960 info.active_disks = active;
2961 info.working_disks = working;
2962 info.failed_disks = failed;
2963 info.spare_disks = spare;
2965 info.layout = mddev->layout;
2966 info.chunk_size = mddev->chunk_size;
2968 if (copy_to_user(arg, &info, sizeof(info)))
2974 static int get_bitmap_file(mddev_t * mddev, void __user * arg)
2976 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
2977 char *ptr, *buf = NULL;
2980 file = kmalloc(sizeof(*file), GFP_KERNEL);
2984 /* bitmap disabled, zero the first byte and copy out */
2985 if (!mddev->bitmap || !mddev->bitmap->file) {
2986 file->pathname[0] = '\0';
2990 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
2994 ptr = file_path(mddev->bitmap->file, buf, sizeof(file->pathname));
2998 strcpy(file->pathname, ptr);
3002 if (copy_to_user(arg, file, sizeof(*file)))
3010 static int get_disk_info(mddev_t * mddev, void __user * arg)
3012 mdu_disk_info_t info;
3016 if (copy_from_user(&info, arg, sizeof(info)))
3021 rdev = find_rdev_nr(mddev, nr);
3023 info.major = MAJOR(rdev->bdev->bd_dev);
3024 info.minor = MINOR(rdev->bdev->bd_dev);
3025 info.raid_disk = rdev->raid_disk;
3027 if (test_bit(Faulty, &rdev->flags))
3028 info.state |= (1<<MD_DISK_FAULTY);
3029 else if (test_bit(In_sync, &rdev->flags)) {
3030 info.state |= (1<<MD_DISK_ACTIVE);
3031 info.state |= (1<<MD_DISK_SYNC);
3033 if (test_bit(WriteMostly, &rdev->flags))
3034 info.state |= (1<<MD_DISK_WRITEMOSTLY);
3036 info.major = info.minor = 0;
3037 info.raid_disk = -1;
3038 info.state = (1<<MD_DISK_REMOVED);
3041 if (copy_to_user(arg, &info, sizeof(info)))
3047 static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
3049 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3051 dev_t dev = MKDEV(info->major,info->minor);
3053 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
3056 if (!mddev->raid_disks) {
3058 /* expecting a device which has a superblock */
3059 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
3062 "md: md_import_device returned %ld\n",
3064 return PTR_ERR(rdev);
3066 if (!list_empty(&mddev->disks)) {
3067 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3068 mdk_rdev_t, same_set);
3069 int err = super_types[mddev->major_version]
3070 .load_super(rdev, rdev0, mddev->minor_version);
3073 "md: %s has different UUID to %s\n",
3074 bdevname(rdev->bdev,b),
3075 bdevname(rdev0->bdev,b2));
3080 err = bind_rdev_to_array(rdev, mddev);
3087 * add_new_disk can be used once the array is assembled
3088 * to add "hot spares". They must already have a superblock
3093 if (!mddev->pers->hot_add_disk) {
3095 "%s: personality does not support diskops!\n",
3099 if (mddev->persistent)
3100 rdev = md_import_device(dev, mddev->major_version,
3101 mddev->minor_version);
3103 rdev = md_import_device(dev, -1, -1);
3106 "md: md_import_device returned %ld\n",
3108 return PTR_ERR(rdev);
3110 /* set save_raid_disk if appropriate */
3111 if (!mddev->persistent) {
3112 if (info->state & (1<<MD_DISK_SYNC) &&
3113 info->raid_disk < mddev->raid_disks)
3114 rdev->raid_disk = info->raid_disk;
3116 rdev->raid_disk = -1;
3118 super_types[mddev->major_version].
3119 validate_super(mddev, rdev);
3120 rdev->saved_raid_disk = rdev->raid_disk;
3122 clear_bit(In_sync, &rdev->flags); /* just to be sure */
3123 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
3124 set_bit(WriteMostly, &rdev->flags);
3126 rdev->raid_disk = -1;
3127 err = bind_rdev_to_array(rdev, mddev);
3131 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3132 md_wakeup_thread(mddev->thread);
3136 /* otherwise, add_new_disk is only allowed
3137 * for major_version==0 superblocks
3139 if (mddev->major_version != 0) {
3140 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
3145 if (!(info->state & (1<<MD_DISK_FAULTY))) {
3147 rdev = md_import_device (dev, -1, 0);
3150 "md: error, md_import_device() returned %ld\n",
3152 return PTR_ERR(rdev);
3154 rdev->desc_nr = info->number;
3155 if (info->raid_disk < mddev->raid_disks)
3156 rdev->raid_disk = info->raid_disk;
3158 rdev->raid_disk = -1;
3162 if (rdev->raid_disk < mddev->raid_disks)
3163 if (info->state & (1<<MD_DISK_SYNC))
3164 set_bit(In_sync, &rdev->flags);
3166 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
3167 set_bit(WriteMostly, &rdev->flags);
3169 if (!mddev->persistent) {
3170 printk(KERN_INFO "md: nonpersistent superblock ...\n");
3171 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
3173 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
3174 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
3176 err = bind_rdev_to_array(rdev, mddev);
3186 static int hot_remove_disk(mddev_t * mddev, dev_t dev)
3188 char b[BDEVNAME_SIZE];
3194 rdev = find_rdev(mddev, dev);
3198 if (rdev->raid_disk >= 0)
3201 kick_rdev_from_array(rdev);
3202 md_update_sb(mddev);
3203 md_new_event(mddev);
3207 printk(KERN_WARNING "md: cannot remove active disk %s from %s ... \n",
3208 bdevname(rdev->bdev,b), mdname(mddev));
3212 static int hot_add_disk(mddev_t * mddev, dev_t dev)
3214 char b[BDEVNAME_SIZE];
3222 if (mddev->major_version != 0) {
3223 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
3224 " version-0 superblocks.\n",
3228 if (!mddev->pers->hot_add_disk) {
3230 "%s: personality does not support diskops!\n",
3235 rdev = md_import_device (dev, -1, 0);
3238 "md: error, md_import_device() returned %ld\n",
3243 if (mddev->persistent)
3244 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
3247 rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
3249 size = calc_dev_size(rdev, mddev->chunk_size);
3252 if (test_bit(Faulty, &rdev->flags)) {
3254 "md: can not hot-add faulty %s disk to %s!\n",
3255 bdevname(rdev->bdev,b), mdname(mddev));
3259 clear_bit(In_sync, &rdev->flags);
3261 err = bind_rdev_to_array(rdev, mddev);
3266 * The rest should better be atomic, we can have disk failures
3267 * noticed in interrupt contexts ...
3270 if (rdev->desc_nr == mddev->max_disks) {
3271 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
3274 goto abort_unbind_export;
3277 rdev->raid_disk = -1;
3279 md_update_sb(mddev);
3282 * Kick recovery, maybe this spare has to be added to the
3283 * array immediately.
3285 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3286 md_wakeup_thread(mddev->thread);
3287 md_new_event(mddev);
3290 abort_unbind_export:
3291 unbind_rdev_from_array(rdev);
3298 /* similar to deny_write_access, but accounts for our holding a reference
3299 * to the file ourselves */
3300 static int deny_bitmap_write_access(struct file * file)
3302 struct inode *inode = file->f_mapping->host;
3304 spin_lock(&inode->i_lock);
3305 if (atomic_read(&inode->i_writecount) > 1) {
3306 spin_unlock(&inode->i_lock);
3309 atomic_set(&inode->i_writecount, -1);
3310 spin_unlock(&inode->i_lock);
3315 static int set_bitmap_file(mddev_t *mddev, int fd)
3320 if (!mddev->pers->quiesce)
3322 if (mddev->recovery || mddev->sync_thread)
3324 /* we should be able to change the bitmap.. */
3330 return -EEXIST; /* cannot add when bitmap is present */
3331 mddev->bitmap_file = fget(fd);
3333 if (mddev->bitmap_file == NULL) {
3334 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
3339 err = deny_bitmap_write_access(mddev->bitmap_file);
3341 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
3343 fput(mddev->bitmap_file);
3344 mddev->bitmap_file = NULL;
3347 mddev->bitmap_offset = 0; /* file overrides offset */
3348 } else if (mddev->bitmap == NULL)
3349 return -ENOENT; /* cannot remove what isn't there */
3352 mddev->pers->quiesce(mddev, 1);
3354 err = bitmap_create(mddev);
3356 bitmap_destroy(mddev);
3357 mddev->pers->quiesce(mddev, 0);
3358 } else if (fd < 0) {
3359 if (mddev->bitmap_file)
3360 fput(mddev->bitmap_file);
3361 mddev->bitmap_file = NULL;
3368 * set_array_info is used two different ways
3369 * The original usage is when creating a new array.
3370 * In this usage, raid_disks is > 0 and it together with
3371 * level, size, not_persistent,layout,chunksize determine the
3372 * shape of the array.
3373 * This will always create an array with a type-0.90.0 superblock.
3374 * The newer usage is when assembling an array.
3375 * In this case raid_disks will be 0, and the major_version field is
3376 * use to determine which style super-blocks are to be found on the devices.
3377 * The minor and patch _version numbers are also kept incase the
3378 * super_block handler wishes to interpret them.
3380 static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
3383 if (info->raid_disks == 0) {
3384 /* just setting version number for superblock loading */
3385 if (info->major_version < 0 ||
3386 info->major_version >= sizeof(super_types)/sizeof(super_types[0]) ||
3387 super_types[info->major_version].name == NULL) {
3388 /* maybe try to auto-load a module? */
3390 "md: superblock version %d not known\n",
3391 info->major_version);
3394 mddev->major_version = info->major_version;
3395 mddev->minor_version = info->minor_version;
3396 mddev->patch_version = info->patch_version;
3399 mddev->major_version = MD_MAJOR_VERSION;
3400 mddev->minor_version = MD_MINOR_VERSION;
3401 mddev->patch_version = MD_PATCHLEVEL_VERSION;
3402 mddev->ctime = get_seconds();
3404 mddev->level = info->level;
3405 mddev->clevel[0] = 0;
3406 mddev->size = info->size;
3407 mddev->raid_disks = info->raid_disks;
3408 /* don't set md_minor, it is determined by which /dev/md* was
3411 if (info->state & (1<<MD_SB_CLEAN))
3412 mddev->recovery_cp = MaxSector;
3414 mddev->recovery_cp = 0;
3415 mddev->persistent = ! info->not_persistent;
3417 mddev->layout = info->layout;
3418 mddev->chunk_size = info->chunk_size;
3420 mddev->max_disks = MD_SB_DISKS;
3422 mddev->sb_dirty = 1;
3424 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
3425 mddev->bitmap_offset = 0;
3428 * Generate a 128 bit UUID
3430 get_random_bytes(mddev->uuid, 16);
3435 static int update_size(mddev_t *mddev, unsigned long size)
3439 struct list_head *tmp;
3441 if (mddev->pers->resize == NULL)
3443 /* The "size" is the amount of each device that is used.
3444 * This can only make sense for arrays with redundancy.
3445 * linear and raid0 always use whatever space is available
3446 * We can only consider changing the size if no resync
3447 * or reconstruction is happening, and if the new size
3448 * is acceptable. It must fit before the sb_offset or,
3449 * if that is <data_offset, it must fit before the
3450 * size of each device.
3451 * If size is zero, we find the largest size that fits.
3453 if (mddev->sync_thread)
3455 ITERATE_RDEV(mddev,rdev,tmp) {
3457 int fit = (size == 0);
3458 if (rdev->sb_offset > rdev->data_offset)
3459 avail = (rdev->sb_offset*2) - rdev->data_offset;
3461 avail = get_capacity(rdev->bdev->bd_disk)
3462 - rdev->data_offset;
3463 if (fit && (size == 0 || size > avail/2))
3465 if (avail < ((sector_t)size << 1))
3468 rv = mddev->pers->resize(mddev, (sector_t)size *2);
3470 struct block_device *bdev;
3472 bdev = bdget_disk(mddev->gendisk, 0);
3474 mutex_lock(&bdev->bd_inode->i_mutex);
3475 i_size_write(bdev->bd_inode, (loff_t)mddev->array_size << 10);
3476 mutex_unlock(&bdev->bd_inode->i_mutex);
3483 static int update_raid_disks(mddev_t *mddev, int raid_disks)
3486 /* change the number of raid disks */
3487 if (mddev->pers->reshape == NULL)
3489 if (raid_disks <= 0 ||
3490 raid_disks >= mddev->max_disks)
3492 if (mddev->sync_thread)
3494 rv = mddev->pers->reshape(mddev, raid_disks);
3500 * update_array_info is used to change the configuration of an
3502 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
3503 * fields in the info are checked against the array.
3504 * Any differences that cannot be handled will cause an error.
3505 * Normally, only one change can be managed at a time.
3507 static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
3513 /* calculate expected state,ignoring low bits */
3514 if (mddev->bitmap && mddev->bitmap_offset)
3515 state |= (1 << MD_SB_BITMAP_PRESENT);
3517 if (mddev->major_version != info->major_version ||
3518 mddev->minor_version != info->minor_version ||
3519 /* mddev->patch_version != info->patch_version || */
3520 mddev->ctime != info->ctime ||
3521 mddev->level != info->level ||
3522 /* mddev->layout != info->layout || */
3523 !mddev->persistent != info->not_persistent||
3524 mddev->chunk_size != info->chunk_size ||
3525 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
3526 ((state^info->state) & 0xfffffe00)
3529 /* Check there is only one change */
3530 if (info->size >= 0 && mddev->size != info->size) cnt++;
3531 if (mddev->raid_disks != info->raid_disks) cnt++;
3532 if (mddev->layout != info->layout) cnt++;
3533 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
3534 if (cnt == 0) return 0;
3535 if (cnt > 1) return -EINVAL;
3537 if (mddev->layout != info->layout) {
3539 * we don't need to do anything at the md level, the
3540 * personality will take care of it all.
3542 if (mddev->pers->reconfig == NULL)
3545 return mddev->pers->reconfig(mddev, info->layout, -1);
3547 if (info->size >= 0 && mddev->size != info->size)
3548 rv = update_size(mddev, info->size);
3550 if (mddev->raid_disks != info->raid_disks)
3551 rv = update_raid_disks(mddev, info->raid_disks);
3553 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
3554 if (mddev->pers->quiesce == NULL)
3556 if (mddev->recovery || mddev->sync_thread)
3558 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
3559 /* add the bitmap */
3562 if (mddev->default_bitmap_offset == 0)
3564 mddev->bitmap_offset = mddev->default_bitmap_offset;
3565 mddev->pers->quiesce(mddev, 1);
3566 rv = bitmap_create(mddev);
3568 bitmap_destroy(mddev);
3569 mddev->pers->quiesce(mddev, 0);
3571 /* remove the bitmap */
3574 if (mddev->bitmap->file)
3576 mddev->pers->quiesce(mddev, 1);
3577 bitmap_destroy(mddev);
3578 mddev->pers->quiesce(mddev, 0);
3579 mddev->bitmap_offset = 0;
3582 md_update_sb(mddev);
3586 static int set_disk_faulty(mddev_t *mddev, dev_t dev)
3590 if (mddev->pers == NULL)
3593 rdev = find_rdev(mddev, dev);
3597 md_error(mddev, rdev);
3601 static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
3603 mddev_t *mddev = bdev->bd_disk->private_data;
3607 geo->cylinders = get_capacity(mddev->gendisk) / 8;
3611 static int md_ioctl(struct inode *inode, struct file *file,
3612 unsigned int cmd, unsigned long arg)
3615 void __user *argp = (void __user *)arg;
3616 mddev_t *mddev = NULL;
3618 if (!capable(CAP_SYS_ADMIN))
3622 * Commands dealing with the RAID driver but not any
3628 err = get_version(argp);
3631 case PRINT_RAID_DEBUG:
3639 autostart_arrays(arg);
3646 * Commands creating/starting a new array:
3649 mddev = inode->i_bdev->bd_disk->private_data;
3657 if (cmd == START_ARRAY) {
3658 /* START_ARRAY doesn't need to lock the array as autostart_array
3659 * does the locking, and it could even be a different array
3664 "md: %s(pid %d) used deprecated START_ARRAY ioctl. "
3665 "This will not be supported beyond July 2006\n",
3666 current->comm, current->pid);
3669 err = autostart_array(new_decode_dev(arg));
3671 printk(KERN_WARNING "md: autostart failed!\n");
3677 err = mddev_lock(mddev);
3680 "md: ioctl lock interrupted, reason %d, cmd %d\n",
3687 case SET_ARRAY_INFO:
3689 mdu_array_info_t info;
3691 memset(&info, 0, sizeof(info));
3692 else if (copy_from_user(&info, argp, sizeof(info))) {
3697 err = update_array_info(mddev, &info);
3699 printk(KERN_WARNING "md: couldn't update"
3700 " array info. %d\n", err);
3705 if (!list_empty(&mddev->disks)) {
3707 "md: array %s already has disks!\n",
3712 if (mddev->raid_disks) {
3714 "md: array %s already initialised!\n",
3719 err = set_array_info(mddev, &info);
3721 printk(KERN_WARNING "md: couldn't set"
3722 " array info. %d\n", err);
3732 * Commands querying/configuring an existing array:
3734 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
3735 * RUN_ARRAY, and SET_BITMAP_FILE are allowed */
3736 if (!mddev->raid_disks && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
3737 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE) {
3743 * Commands even a read-only array can execute:
3747 case GET_ARRAY_INFO:
3748 err = get_array_info(mddev, argp);
3751 case GET_BITMAP_FILE:
3752 err = get_bitmap_file(mddev, argp);
3756 err = get_disk_info(mddev, argp);
3759 case RESTART_ARRAY_RW:
3760 err = restart_array(mddev);
3764 err = do_md_stop (mddev, 0);
3768 err = do_md_stop (mddev, 1);
3772 * We have a problem here : there is no easy way to give a CHS
3773 * virtual geometry. We currently pretend that we have a 2 heads
3774 * 4 sectors (with a BIG number of cylinders...). This drives
3775 * dosfs just mad... ;-)
3780 * The remaining ioctls are changing the state of the
3781 * superblock, so we do not allow them on read-only arrays.
3782 * However non-MD ioctls (e.g. get-size) will still come through
3783 * here and hit the 'default' below, so only disallow
3784 * 'md' ioctls, and switch to rw mode if started auto-readonly.
3786 if (_IOC_TYPE(cmd) == MD_MAJOR &&
3787 mddev->ro && mddev->pers) {
3788 if (mddev->ro == 2) {
3790 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3791 md_wakeup_thread(mddev->thread);
3803 mdu_disk_info_t info;
3804 if (copy_from_user(&info, argp, sizeof(info)))
3807 err = add_new_disk(mddev, &info);
3811 case HOT_REMOVE_DISK:
3812 err = hot_remove_disk(mddev, new_decode_dev(arg));
3816 err = hot_add_disk(mddev, new_decode_dev(arg));
3819 case SET_DISK_FAULTY:
3820 err = set_disk_faulty(mddev, new_decode_dev(arg));
3824 err = do_md_run (mddev);
3827 case SET_BITMAP_FILE:
3828 err = set_bitmap_file(mddev, (int)arg);
3832 if (_IOC_TYPE(cmd) == MD_MAJOR)
3833 printk(KERN_WARNING "md: %s(pid %d) used"
3834 " obsolete MD ioctl, upgrade your"
3835 " software to use new ictls.\n",
3836 current->comm, current->pid);
3843 mddev_unlock(mddev);
3853 static int md_open(struct inode *inode, struct file *file)
3856 * Succeed if we can lock the mddev, which confirms that
3857 * it isn't being stopped right now.
3859 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
3862 if ((err = mddev_lock(mddev)))
3867 mddev_unlock(mddev);
3869 check_disk_change(inode->i_bdev);
3874 static int md_release(struct inode *inode, struct file * file)
3876 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
3885 static int md_media_changed(struct gendisk *disk)
3887 mddev_t *mddev = disk->private_data;
3889 return mddev->changed;
3892 static int md_revalidate(struct gendisk *disk)
3894 mddev_t *mddev = disk->private_data;
3899 static struct block_device_operations md_fops =
3901 .owner = THIS_MODULE,
3903 .release = md_release,
3905 .getgeo = md_getgeo,
3906 .media_changed = md_media_changed,
3907 .revalidate_disk= md_revalidate,
3910 static int md_thread(void * arg)
3912 mdk_thread_t *thread = arg;
3915 * md_thread is a 'system-thread', it's priority should be very
3916 * high. We avoid resource deadlocks individually in each
3917 * raid personality. (RAID5 does preallocation) We also use RR and
3918 * the very same RT priority as kswapd, thus we will never get
3919 * into a priority inversion deadlock.
3921 * we definitely have to have equal or higher priority than
3922 * bdflush, otherwise bdflush will deadlock if there are too
3923 * many dirty RAID5 blocks.
3926 allow_signal(SIGKILL);
3927 while (!kthread_should_stop()) {
3929 /* We need to wait INTERRUPTIBLE so that
3930 * we don't add to the load-average.
3931 * That means we need to be sure no signals are
3934 if (signal_pending(current))
3935 flush_signals(current);
3937 wait_event_interruptible_timeout
3939 test_bit(THREAD_WAKEUP, &thread->flags)
3940 || kthread_should_stop(),
3944 clear_bit(THREAD_WAKEUP, &thread->flags);
3946 thread->run(thread->mddev);
3952 void md_wakeup_thread(mdk_thread_t *thread)
3955 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
3956 set_bit(THREAD_WAKEUP, &thread->flags);
3957 wake_up(&thread->wqueue);
3961 mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
3964 mdk_thread_t *thread;
3966 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
3970 init_waitqueue_head(&thread->wqueue);
3973 thread->mddev = mddev;
3974 thread->timeout = MAX_SCHEDULE_TIMEOUT;
3975 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
3976 if (IS_ERR(thread->tsk)) {
3983 void md_unregister_thread(mdk_thread_t *thread)
3985 dprintk("interrupting MD-thread pid %d\n", thread->tsk->pid);
3987 kthread_stop(thread->tsk);
3991 void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
3998 if (!rdev || test_bit(Faulty, &rdev->flags))
4001 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
4003 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
4004 __builtin_return_address(0),__builtin_return_address(1),
4005 __builtin_return_address(2),__builtin_return_address(3));
4007 if (!mddev->pers->error_handler)
4009 mddev->pers->error_handler(mddev,rdev);
4010 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4011 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4012 md_wakeup_thread(mddev->thread);
4013 md_new_event(mddev);
4016 /* seq_file implementation /proc/mdstat */
4018 static void status_unused(struct seq_file *seq)
4022 struct list_head *tmp;
4024 seq_printf(seq, "unused devices: ");
4026 ITERATE_RDEV_PENDING(rdev,tmp) {
4027 char b[BDEVNAME_SIZE];
4029 seq_printf(seq, "%s ",
4030 bdevname(rdev->bdev,b));
4033 seq_printf(seq, "<none>");
4035 seq_printf(seq, "\n");
4039 static void status_resync(struct seq_file *seq, mddev_t * mddev)
4041 unsigned long max_blocks, resync, res, dt, db, rt;
4043 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
4045 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
4046 max_blocks = mddev->resync_max_sectors >> 1;
4048 max_blocks = mddev->size;
4051 * Should not happen.
4057 res = (resync/1024)*1000/(max_blocks/1024 + 1);
4059 int i, x = res/50, y = 20-x;
4060 seq_printf(seq, "[");
4061 for (i = 0; i < x; i++)
4062 seq_printf(seq, "=");
4063 seq_printf(seq, ">");
4064 for (i = 0; i < y; i++)
4065 seq_printf(seq, ".");
4066 seq_printf(seq, "] ");
4068 seq_printf(seq, " %s =%3lu.%lu%% (%lu/%lu)",
4069 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
4070 "resync" : "recovery"),
4071 res/10, res % 10, resync, max_blocks);
4074 * We do not want to overflow, so the order of operands and
4075 * the * 100 / 100 trick are important. We do a +1 to be
4076 * safe against division by zero. We only estimate anyway.
4078 * dt: time from mark until now
4079 * db: blocks written from mark until now
4080 * rt: remaining time
4082 dt = ((jiffies - mddev->resync_mark) / HZ);
4084 db = resync - (mddev->resync_mark_cnt/2);
4085 rt = (dt * ((max_blocks-resync) / (db/100+1)))/100;
4087 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
4089 seq_printf(seq, " speed=%ldK/sec", db/dt);
4092 static void *md_seq_start(struct seq_file *seq, loff_t *pos)
4094 struct list_head *tmp;
4104 spin_lock(&all_mddevs_lock);
4105 list_for_each(tmp,&all_mddevs)
4107 mddev = list_entry(tmp, mddev_t, all_mddevs);
4109 spin_unlock(&all_mddevs_lock);
4112 spin_unlock(&all_mddevs_lock);
4114 return (void*)2;/* tail */
4118 static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4120 struct list_head *tmp;
4121 mddev_t *next_mddev, *mddev = v;
4127 spin_lock(&all_mddevs_lock);
4129 tmp = all_mddevs.next;
4131 tmp = mddev->all_mddevs.next;
4132 if (tmp != &all_mddevs)
4133 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
4135 next_mddev = (void*)2;
4138 spin_unlock(&all_mddevs_lock);
4146 static void md_seq_stop(struct seq_file *seq, void *v)
4150 if (mddev && v != (void*)1 && v != (void*)2)
4154 struct mdstat_info {
4158 static int md_seq_show(struct seq_file *seq, void *v)
4162 struct list_head *tmp2;
4164 struct mdstat_info *mi = seq->private;
4165 struct bitmap *bitmap;
4167 if (v == (void*)1) {
4168 struct mdk_personality *pers;
4169 seq_printf(seq, "Personalities : ");
4170 spin_lock(&pers_lock);
4171 list_for_each_entry(pers, &pers_list, list)
4172 seq_printf(seq, "[%s] ", pers->name);
4174 spin_unlock(&pers_lock);
4175 seq_printf(seq, "\n");
4176 mi->event = atomic_read(&md_event_count);
4179 if (v == (void*)2) {
4184 if (mddev_lock(mddev)!=0)
4186 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
4187 seq_printf(seq, "%s : %sactive", mdname(mddev),
4188 mddev->pers ? "" : "in");
4191 seq_printf(seq, " (read-only)");
4193 seq_printf(seq, "(auto-read-only)");
4194 seq_printf(seq, " %s", mddev->pers->name);
4198 ITERATE_RDEV(mddev,rdev,tmp2) {
4199 char b[BDEVNAME_SIZE];
4200 seq_printf(seq, " %s[%d]",
4201 bdevname(rdev->bdev,b), rdev->desc_nr);
4202 if (test_bit(WriteMostly, &rdev->flags))
4203 seq_printf(seq, "(W)");
4204 if (test_bit(Faulty, &rdev->flags)) {
4205 seq_printf(seq, "(F)");
4207 } else if (rdev->raid_disk < 0)
4208 seq_printf(seq, "(S)"); /* spare */
4212 if (!list_empty(&mddev->disks)) {
4214 seq_printf(seq, "\n %llu blocks",
4215 (unsigned long long)mddev->array_size);
4217 seq_printf(seq, "\n %llu blocks",
4218 (unsigned long long)size);
4220 if (mddev->persistent) {
4221 if (mddev->major_version != 0 ||
4222 mddev->minor_version != 90) {
4223 seq_printf(seq," super %d.%d",
4224 mddev->major_version,
4225 mddev->minor_version);
4228 seq_printf(seq, " super non-persistent");
4231 mddev->pers->status (seq, mddev);
4232 seq_printf(seq, "\n ");
4233 if (mddev->pers->sync_request) {
4234 if (mddev->curr_resync > 2) {
4235 status_resync (seq, mddev);
4236 seq_printf(seq, "\n ");
4237 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
4238 seq_printf(seq, "\tresync=DELAYED\n ");
4239 else if (mddev->recovery_cp < MaxSector)
4240 seq_printf(seq, "\tresync=PENDING\n ");
4243 seq_printf(seq, "\n ");
4245 if ((bitmap = mddev->bitmap)) {
4246 unsigned long chunk_kb;
4247 unsigned long flags;
4248 spin_lock_irqsave(&bitmap->lock, flags);
4249 chunk_kb = bitmap->chunksize >> 10;
4250 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
4252 bitmap->pages - bitmap->missing_pages,
4254 (bitmap->pages - bitmap->missing_pages)
4255 << (PAGE_SHIFT - 10),
4256 chunk_kb ? chunk_kb : bitmap->chunksize,
4257 chunk_kb ? "KB" : "B");
4259 seq_printf(seq, ", file: ");
4260 seq_path(seq, bitmap->file->f_vfsmnt,
4261 bitmap->file->f_dentry," \t\n");
4264 seq_printf(seq, "\n");
4265 spin_unlock_irqrestore(&bitmap->lock, flags);
4268 seq_printf(seq, "\n");
4270 mddev_unlock(mddev);
4275 static struct seq_operations md_seq_ops = {
4276 .start = md_seq_start,
4277 .next = md_seq_next,
4278 .stop = md_seq_stop,
4279 .show = md_seq_show,
4282 static int md_seq_open(struct inode *inode, struct file *file)
4285 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
4289 error = seq_open(file, &md_seq_ops);
4293 struct seq_file *p = file->private_data;
4295 mi->event = atomic_read(&md_event_count);
4300 static int md_seq_release(struct inode *inode, struct file *file)
4302 struct seq_file *m = file->private_data;
4303 struct mdstat_info *mi = m->private;
4306 return seq_release(inode, file);
4309 static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
4311 struct seq_file *m = filp->private_data;
4312 struct mdstat_info *mi = m->private;
4315 poll_wait(filp, &md_event_waiters, wait);
4317 /* always allow read */
4318 mask = POLLIN | POLLRDNORM;
4320 if (mi->event != atomic_read(&md_event_count))
4321 mask |= POLLERR | POLLPRI;
4325 static struct file_operations md_seq_fops = {
4326 .open = md_seq_open,
4328 .llseek = seq_lseek,
4329 .release = md_seq_release,
4330 .poll = mdstat_poll,
4333 int register_md_personality(struct mdk_personality *p)
4335 spin_lock(&pers_lock);
4336 list_add_tail(&p->list, &pers_list);
4337 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
4338 spin_unlock(&pers_lock);
4342 int unregister_md_personality(struct mdk_personality *p)
4344 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
4345 spin_lock(&pers_lock);
4346 list_del_init(&p->list);
4347 spin_unlock(&pers_lock);
4351 static int is_mddev_idle(mddev_t *mddev)
4354 struct list_head *tmp;
4356 unsigned long curr_events;
4359 ITERATE_RDEV(mddev,rdev,tmp) {
4360 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
4361 curr_events = disk_stat_read(disk, sectors[0]) +
4362 disk_stat_read(disk, sectors[1]) -
4363 atomic_read(&disk->sync_io);
4364 /* The difference between curr_events and last_events
4365 * will be affected by any new non-sync IO (making
4366 * curr_events bigger) and any difference in the amount of
4367 * in-flight syncio (making current_events bigger or smaller)
4368 * The amount in-flight is currently limited to
4369 * 32*64K in raid1/10 and 256*PAGE_SIZE in raid5/6
4370 * which is at most 4096 sectors.
4371 * These numbers are fairly fragile and should be made
4372 * more robust, probably by enforcing the
4373 * 'window size' that md_do_sync sort-of uses.
4375 * Note: the following is an unsigned comparison.
4377 if ((curr_events - rdev->last_events + 4096) > 8192) {
4378 rdev->last_events = curr_events;
4385 void md_done_sync(mddev_t *mddev, int blocks, int ok)
4387 /* another "blocks" (512byte) blocks have been synced */
4388 atomic_sub(blocks, &mddev->recovery_active);
4389 wake_up(&mddev->recovery_wait);
4391 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
4392 md_wakeup_thread(mddev->thread);
4393 // stop recovery, signal do_sync ....
4398 /* md_write_start(mddev, bi)
4399 * If we need to update some array metadata (e.g. 'active' flag
4400 * in superblock) before writing, schedule a superblock update
4401 * and wait for it to complete.
4403 void md_write_start(mddev_t *mddev, struct bio *bi)
4405 if (bio_data_dir(bi) != WRITE)
4408 BUG_ON(mddev->ro == 1);
4409 if (mddev->ro == 2) {
4410 /* need to switch to read/write */
4412 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4413 md_wakeup_thread(mddev->thread);
4415 atomic_inc(&mddev->writes_pending);
4416 if (mddev->in_sync) {
4417 spin_lock_irq(&mddev->write_lock);
4418 if (mddev->in_sync) {
4420 mddev->sb_dirty = 1;
4421 md_wakeup_thread(mddev->thread);
4423 spin_unlock_irq(&mddev->write_lock);
4425 wait_event(mddev->sb_wait, mddev->sb_dirty==0);
4428 void md_write_end(mddev_t *mddev)
4430 if (atomic_dec_and_test(&mddev->writes_pending)) {
4431 if (mddev->safemode == 2)
4432 md_wakeup_thread(mddev->thread);
4434 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
4438 static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
4440 #define SYNC_MARKS 10
4441 #define SYNC_MARK_STEP (3*HZ)
4442 static void md_do_sync(mddev_t *mddev)
4445 unsigned int currspeed = 0,
4447 sector_t max_sectors,j, io_sectors;
4448 unsigned long mark[SYNC_MARKS];
4449 sector_t mark_cnt[SYNC_MARKS];
4451 struct list_head *tmp;
4452 sector_t last_check;
4455 /* just incase thread restarts... */
4456 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
4459 /* we overload curr_resync somewhat here.
4460 * 0 == not engaged in resync at all
4461 * 2 == checking that there is no conflict with another sync
4462 * 1 == like 2, but have yielded to allow conflicting resync to
4464 * other == active in resync - this many blocks
4466 * Before starting a resync we must have set curr_resync to
4467 * 2, and then checked that every "conflicting" array has curr_resync
4468 * less than ours. When we find one that is the same or higher
4469 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
4470 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
4471 * This will mean we have to start checking from the beginning again.
4476 mddev->curr_resync = 2;
4479 if (kthread_should_stop()) {
4480 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4483 ITERATE_MDDEV(mddev2,tmp) {
4484 if (mddev2 == mddev)
4486 if (mddev2->curr_resync &&
4487 match_mddev_units(mddev,mddev2)) {
4489 if (mddev < mddev2 && mddev->curr_resync == 2) {
4490 /* arbitrarily yield */
4491 mddev->curr_resync = 1;
4492 wake_up(&resync_wait);
4494 if (mddev > mddev2 && mddev->curr_resync == 1)
4495 /* no need to wait here, we can wait the next
4496 * time 'round when curr_resync == 2
4499 prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE);
4500 if (!kthread_should_stop() &&
4501 mddev2->curr_resync >= mddev->curr_resync) {
4502 printk(KERN_INFO "md: delaying resync of %s"
4503 " until %s has finished resync (they"
4504 " share one or more physical units)\n",
4505 mdname(mddev), mdname(mddev2));
4508 finish_wait(&resync_wait, &wq);
4511 finish_wait(&resync_wait, &wq);
4514 } while (mddev->curr_resync < 2);
4516 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
4517 /* resync follows the size requested by the personality,
4518 * which defaults to physical size, but can be virtual size
4520 max_sectors = mddev->resync_max_sectors;
4521 mddev->resync_mismatches = 0;
4523 /* recovery follows the physical size of devices */
4524 max_sectors = mddev->size << 1;
4526 printk(KERN_INFO "md: syncing RAID array %s\n", mdname(mddev));
4527 printk(KERN_INFO "md: minimum _guaranteed_ reconstruction speed:"
4528 " %d KB/sec/disc.\n", speed_min(mddev));
4529 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
4530 "(but not more than %d KB/sec) for reconstruction.\n",
4533 is_mddev_idle(mddev); /* this also initializes IO event counters */
4534 /* we don't use the checkpoint if there's a bitmap */
4535 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && !mddev->bitmap
4536 && ! test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
4537 j = mddev->recovery_cp;
4541 for (m = 0; m < SYNC_MARKS; m++) {
4543 mark_cnt[m] = io_sectors;
4546 mddev->resync_mark = mark[last_mark];
4547 mddev->resync_mark_cnt = mark_cnt[last_mark];
4550 * Tune reconstruction:
4552 window = 32*(PAGE_SIZE/512);
4553 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
4554 window/2,(unsigned long long) max_sectors/2);
4556 atomic_set(&mddev->recovery_active, 0);
4557 init_waitqueue_head(&mddev->recovery_wait);
4562 "md: resuming recovery of %s from checkpoint.\n",
4564 mddev->curr_resync = j;
4567 while (j < max_sectors) {
4571 sectors = mddev->pers->sync_request(mddev, j, &skipped,
4572 currspeed < speed_min(mddev));
4574 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
4578 if (!skipped) { /* actual IO requested */
4579 io_sectors += sectors;
4580 atomic_add(sectors, &mddev->recovery_active);
4584 if (j>1) mddev->curr_resync = j;
4585 if (last_check == 0)
4586 /* this is the earliers that rebuilt will be
4587 * visible in /proc/mdstat
4589 md_new_event(mddev);
4591 if (last_check + window > io_sectors || j == max_sectors)
4594 last_check = io_sectors;
4596 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery) ||
4597 test_bit(MD_RECOVERY_ERR, &mddev->recovery))
4601 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
4603 int next = (last_mark+1) % SYNC_MARKS;
4605 mddev->resync_mark = mark[next];
4606 mddev->resync_mark_cnt = mark_cnt[next];
4607 mark[next] = jiffies;
4608 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
4613 if (kthread_should_stop()) {
4615 * got a signal, exit.
4618 "md: md_do_sync() got signal ... exiting\n");
4619 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4624 * this loop exits only if either when we are slower than
4625 * the 'hard' speed limit, or the system was IO-idle for
4627 * the system might be non-idle CPU-wise, but we only care
4628 * about not overloading the IO subsystem. (things like an
4629 * e2fsck being done on the RAID array should execute fast)
4631 mddev->queue->unplug_fn(mddev->queue);
4634 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
4635 /((jiffies-mddev->resync_mark)/HZ +1) +1;
4637 if (currspeed > speed_min(mddev)) {
4638 if ((currspeed > speed_max(mddev)) ||
4639 !is_mddev_idle(mddev)) {
4645 printk(KERN_INFO "md: %s: sync done.\n",mdname(mddev));
4647 * this also signals 'finished resyncing' to md_stop
4650 mddev->queue->unplug_fn(mddev->queue);
4652 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
4654 /* tell personality that we are finished */
4655 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
4657 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
4658 mddev->curr_resync > 2 &&
4659 mddev->curr_resync >= mddev->recovery_cp) {
4660 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4662 "md: checkpointing recovery of %s.\n",
4664 mddev->recovery_cp = mddev->curr_resync;
4666 mddev->recovery_cp = MaxSector;
4670 mddev->curr_resync = 0;
4671 wake_up(&resync_wait);
4672 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
4673 md_wakeup_thread(mddev->thread);
4678 * This routine is regularly called by all per-raid-array threads to
4679 * deal with generic issues like resync and super-block update.
4680 * Raid personalities that don't have a thread (linear/raid0) do not
4681 * need this as they never do any recovery or update the superblock.
4683 * It does not do any resync itself, but rather "forks" off other threads
4684 * to do that as needed.
4685 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
4686 * "->recovery" and create a thread at ->sync_thread.
4687 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
4688 * and wakeups up this thread which will reap the thread and finish up.
4689 * This thread also removes any faulty devices (with nr_pending == 0).
4691 * The overall approach is:
4692 * 1/ if the superblock needs updating, update it.
4693 * 2/ If a recovery thread is running, don't do anything else.
4694 * 3/ If recovery has finished, clean up, possibly marking spares active.
4695 * 4/ If there are any faulty devices, remove them.
4696 * 5/ If array is degraded, try to add spares devices
4697 * 6/ If array has spares or is not in-sync, start a resync thread.
4699 void md_check_recovery(mddev_t *mddev)
4702 struct list_head *rtmp;
4706 bitmap_daemon_work(mddev->bitmap);
4711 if (signal_pending(current)) {
4712 if (mddev->pers->sync_request) {
4713 printk(KERN_INFO "md: %s in immediate safe mode\n",
4715 mddev->safemode = 2;
4717 flush_signals(current);
4722 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
4723 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
4724 (mddev->safemode == 1) ||
4725 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
4726 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
4730 if (mddev_trylock(mddev)==0) {
4733 spin_lock_irq(&mddev->write_lock);
4734 if (mddev->safemode && !atomic_read(&mddev->writes_pending) &&
4735 !mddev->in_sync && mddev->recovery_cp == MaxSector) {
4737 mddev->sb_dirty = 1;
4739 if (mddev->safemode == 1)
4740 mddev->safemode = 0;
4741 spin_unlock_irq(&mddev->write_lock);
4743 if (mddev->sb_dirty)
4744 md_update_sb(mddev);
4747 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4748 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
4749 /* resync/recovery still happening */
4750 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4753 if (mddev->sync_thread) {
4754 /* resync has finished, collect result */
4755 md_unregister_thread(mddev->sync_thread);
4756 mddev->sync_thread = NULL;
4757 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
4758 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4760 /* activate any spares */
4761 mddev->pers->spare_active(mddev);
4763 md_update_sb(mddev);
4765 /* if array is no-longer degraded, then any saved_raid_disk
4766 * information must be scrapped
4768 if (!mddev->degraded)
4769 ITERATE_RDEV(mddev,rdev,rtmp)
4770 rdev->saved_raid_disk = -1;
4772 mddev->recovery = 0;
4773 /* flag recovery needed just to double check */
4774 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4775 md_new_event(mddev);
4778 /* Clear some bits that don't mean anything, but
4781 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4782 clear_bit(MD_RECOVERY_ERR, &mddev->recovery);
4783 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
4784 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
4786 /* no recovery is running.
4787 * remove any failed drives, then
4788 * add spares if possible.
4789 * Spare are also removed and re-added, to allow
4790 * the personality to fail the re-add.
4792 ITERATE_RDEV(mddev,rdev,rtmp)
4793 if (rdev->raid_disk >= 0 &&
4794 (test_bit(Faulty, &rdev->flags) || ! test_bit(In_sync, &rdev->flags)) &&
4795 atomic_read(&rdev->nr_pending)==0) {
4796 if (mddev->pers->hot_remove_disk(mddev, rdev->raid_disk)==0) {
4798 sprintf(nm,"rd%d", rdev->raid_disk);
4799 sysfs_remove_link(&mddev->kobj, nm);
4800 rdev->raid_disk = -1;
4804 if (mddev->degraded) {
4805 ITERATE_RDEV(mddev,rdev,rtmp)
4806 if (rdev->raid_disk < 0
4807 && !test_bit(Faulty, &rdev->flags)) {
4808 if (mddev->pers->hot_add_disk(mddev,rdev)) {
4810 sprintf(nm, "rd%d", rdev->raid_disk);
4811 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
4813 md_new_event(mddev);
4820 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4821 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4822 } else if (mddev->recovery_cp < MaxSector) {
4823 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4824 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
4825 /* nothing to be done ... */
4828 if (mddev->pers->sync_request) {
4829 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4830 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
4831 /* We are adding a device or devices to an array
4832 * which has the bitmap stored on all devices.
4833 * So make sure all bitmap pages get written
4835 bitmap_write_all(mddev->bitmap);
4837 mddev->sync_thread = md_register_thread(md_do_sync,
4840 if (!mddev->sync_thread) {
4841 printk(KERN_ERR "%s: could not start resync"
4844 /* leave the spares where they are, it shouldn't hurt */
4845 mddev->recovery = 0;
4847 md_wakeup_thread(mddev->sync_thread);
4848 md_new_event(mddev);
4851 mddev_unlock(mddev);
4855 static int md_notify_reboot(struct notifier_block *this,
4856 unsigned long code, void *x)
4858 struct list_head *tmp;
4861 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
4863 printk(KERN_INFO "md: stopping all md devices.\n");
4865 ITERATE_MDDEV(mddev,tmp)
4866 if (mddev_trylock(mddev)==0)
4867 do_md_stop (mddev, 1);
4869 * certain more exotic SCSI devices are known to be
4870 * volatile wrt too early system reboots. While the
4871 * right place to handle this issue is the given
4872 * driver, we do want to have a safe RAID driver ...
4879 static struct notifier_block md_notifier = {
4880 .notifier_call = md_notify_reboot,
4882 .priority = INT_MAX, /* before any real devices */
4885 static void md_geninit(void)
4887 struct proc_dir_entry *p;
4889 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
4891 p = create_proc_entry("mdstat", S_IRUGO, NULL);
4893 p->proc_fops = &md_seq_fops;
4896 static int __init md_init(void)
4900 printk(KERN_INFO "md: md driver %d.%d.%d MAX_MD_DEVS=%d,"
4901 " MD_SB_DISKS=%d\n",
4902 MD_MAJOR_VERSION, MD_MINOR_VERSION,
4903 MD_PATCHLEVEL_VERSION, MAX_MD_DEVS, MD_SB_DISKS);
4904 printk(KERN_INFO "md: bitmap version %d.%d\n", BITMAP_MAJOR_HI,
4907 if (register_blkdev(MAJOR_NR, "md"))
4909 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
4910 unregister_blkdev(MAJOR_NR, "md");
4914 blk_register_region(MKDEV(MAJOR_NR, 0), MAX_MD_DEVS, THIS_MODULE,
4915 md_probe, NULL, NULL);
4916 blk_register_region(MKDEV(mdp_major, 0), MAX_MD_DEVS<<MdpMinorShift, THIS_MODULE,
4917 md_probe, NULL, NULL);
4919 for (minor=0; minor < MAX_MD_DEVS; ++minor)
4920 devfs_mk_bdev(MKDEV(MAJOR_NR, minor),
4921 S_IFBLK|S_IRUSR|S_IWUSR,
4924 for (minor=0; minor < MAX_MD_DEVS; ++minor)
4925 devfs_mk_bdev(MKDEV(mdp_major, minor<<MdpMinorShift),
4926 S_IFBLK|S_IRUSR|S_IWUSR,
4930 register_reboot_notifier(&md_notifier);
4931 raid_table_header = register_sysctl_table(raid_root_table, 1);
4941 * Searches all registered partitions for autorun RAID arrays
4944 static dev_t detected_devices[128];
4947 void md_autodetect_dev(dev_t dev)
4949 if (dev_cnt >= 0 && dev_cnt < 127)
4950 detected_devices[dev_cnt++] = dev;
4954 static void autostart_arrays(int part)
4959 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
4961 for (i = 0; i < dev_cnt; i++) {
4962 dev_t dev = detected_devices[i];
4964 rdev = md_import_device(dev,0, 0);
4968 if (test_bit(Faulty, &rdev->flags)) {
4972 list_add(&rdev->same_set, &pending_raid_disks);
4976 autorun_devices(part);
4981 static __exit void md_exit(void)
4984 struct list_head *tmp;
4986 blk_unregister_region(MKDEV(MAJOR_NR,0), MAX_MD_DEVS);
4987 blk_unregister_region(MKDEV(mdp_major,0), MAX_MD_DEVS << MdpMinorShift);
4988 for (i=0; i < MAX_MD_DEVS; i++)
4989 devfs_remove("md/%d", i);
4990 for (i=0; i < MAX_MD_DEVS; i++)
4991 devfs_remove("md/d%d", i);
4995 unregister_blkdev(MAJOR_NR,"md");
4996 unregister_blkdev(mdp_major, "mdp");
4997 unregister_reboot_notifier(&md_notifier);
4998 unregister_sysctl_table(raid_table_header);
4999 remove_proc_entry("mdstat", NULL);
5000 ITERATE_MDDEV(mddev,tmp) {
5001 struct gendisk *disk = mddev->gendisk;
5004 export_array(mddev);
5007 mddev->gendisk = NULL;
5012 module_init(md_init)
5013 module_exit(md_exit)
5015 static int get_ro(char *buffer, struct kernel_param *kp)
5017 return sprintf(buffer, "%d", start_readonly);
5019 static int set_ro(const char *val, struct kernel_param *kp)
5022 int num = simple_strtoul(val, &e, 10);
5023 if (*val && (*e == '\0' || *e == '\n')) {
5024 start_readonly = num;
5030 module_param_call(start_ro, set_ro, get_ro, NULL, 0600);
5031 module_param(start_dirty_degraded, int, 0644);
5034 EXPORT_SYMBOL(register_md_personality);
5035 EXPORT_SYMBOL(unregister_md_personality);
5036 EXPORT_SYMBOL(md_error);
5037 EXPORT_SYMBOL(md_done_sync);
5038 EXPORT_SYMBOL(md_write_start);
5039 EXPORT_SYMBOL(md_write_end);
5040 EXPORT_SYMBOL(md_register_thread);
5041 EXPORT_SYMBOL(md_unregister_thread);
5042 EXPORT_SYMBOL(md_wakeup_thread);
5043 EXPORT_SYMBOL(md_print_devices);
5044 EXPORT_SYMBOL(md_check_recovery);
5045 MODULE_LICENSE("GPL");
5047 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);