2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
5 completely rewritten, based on the MD driver code from Marc Zyngier
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
20 Neil Brown <neilb@cse.unsw.edu.au>.
22 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
25 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 #include <linux/kthread.h>
36 #include <linux/raid/md.h>
37 #include <linux/raid/bitmap.h>
38 #include <linux/sysctl.h>
39 #include <linux/buffer_head.h> /* for invalidate_bdev */
40 #include <linux/poll.h>
41 #include <linux/ctype.h>
42 #include <linux/hdreg.h>
43 #include <linux/proc_fs.h>
44 #include <linux/random.h>
45 #include <linux/reboot.h>
46 #include <linux/file.h>
47 #include <linux/delay.h>
49 #define MAJOR_NR MD_MAJOR
51 /* 63 partitions with the alternate major number (mdp) */
52 #define MdpMinorShift 6
55 #define dprintk(x...) ((void)(DEBUG && printk(x)))
59 static void autostart_arrays(int part);
62 static LIST_HEAD(pers_list);
63 static DEFINE_SPINLOCK(pers_lock);
65 static void md_print_devices(void);
67 static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
69 #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
72 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
73 * is 1000 KB/sec, so the extra system load does not show up that much.
74 * Increase it if you want to have more _guaranteed_ speed. Note that
75 * the RAID driver will use the maximum available bandwidth if the IO
76 * subsystem is idle. There is also an 'absolute maximum' reconstruction
77 * speed limit - in case reconstruction slows down your system despite
80 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
81 * or /sys/block/mdX/md/sync_speed_{min,max}
84 static int sysctl_speed_limit_min = 1000;
85 static int sysctl_speed_limit_max = 200000;
86 static inline int speed_min(mddev_t *mddev)
88 return mddev->sync_speed_min ?
89 mddev->sync_speed_min : sysctl_speed_limit_min;
92 static inline int speed_max(mddev_t *mddev)
94 return mddev->sync_speed_max ?
95 mddev->sync_speed_max : sysctl_speed_limit_max;
98 static struct ctl_table_header *raid_table_header;
100 static ctl_table raid_table[] = {
102 .ctl_name = DEV_RAID_SPEED_LIMIT_MIN,
103 .procname = "speed_limit_min",
104 .data = &sysctl_speed_limit_min,
105 .maxlen = sizeof(int),
106 .mode = S_IRUGO|S_IWUSR,
107 .proc_handler = &proc_dointvec,
110 .ctl_name = DEV_RAID_SPEED_LIMIT_MAX,
111 .procname = "speed_limit_max",
112 .data = &sysctl_speed_limit_max,
113 .maxlen = sizeof(int),
114 .mode = S_IRUGO|S_IWUSR,
115 .proc_handler = &proc_dointvec,
120 static ctl_table raid_dir_table[] = {
122 .ctl_name = DEV_RAID,
125 .mode = S_IRUGO|S_IXUGO,
131 static ctl_table raid_root_table[] = {
137 .child = raid_dir_table,
142 static struct block_device_operations md_fops;
144 static int start_readonly;
147 * We have a system wide 'event count' that is incremented
148 * on any 'interesting' event, and readers of /proc/mdstat
149 * can use 'poll' or 'select' to find out when the event
153 * start array, stop array, error, add device, remove device,
154 * start build, activate spare
156 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
157 static atomic_t md_event_count;
158 void md_new_event(mddev_t *mddev)
160 atomic_inc(&md_event_count);
161 wake_up(&md_event_waiters);
163 EXPORT_SYMBOL_GPL(md_new_event);
165 /* Alternate version that can be called from interrupts
166 * when calling sysfs_notify isn't needed.
168 static void md_new_event_inintr(mddev_t *mddev)
170 atomic_inc(&md_event_count);
171 wake_up(&md_event_waiters);
175 * Enables to iterate over all existing md arrays
176 * all_mddevs_lock protects this list.
178 static LIST_HEAD(all_mddevs);
179 static DEFINE_SPINLOCK(all_mddevs_lock);
183 * iterates through all used mddevs in the system.
184 * We take care to grab the all_mddevs_lock whenever navigating
185 * the list, and to always hold a refcount when unlocked.
186 * Any code which breaks out of this loop while own
187 * a reference to the current mddev and must mddev_put it.
189 #define for_each_mddev(mddev,tmp) \
191 for (({ spin_lock(&all_mddevs_lock); \
192 tmp = all_mddevs.next; \
194 ({ if (tmp != &all_mddevs) \
195 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
196 spin_unlock(&all_mddevs_lock); \
197 if (mddev) mddev_put(mddev); \
198 mddev = list_entry(tmp, mddev_t, all_mddevs); \
199 tmp != &all_mddevs;}); \
200 ({ spin_lock(&all_mddevs_lock); \
205 static int md_fail_request(struct request_queue *q, struct bio *bio)
211 static inline mddev_t *mddev_get(mddev_t *mddev)
213 atomic_inc(&mddev->active);
217 static void mddev_delayed_delete(struct work_struct *ws)
219 mddev_t *mddev = container_of(ws, mddev_t, del_work);
220 kobject_del(&mddev->kobj);
221 kobject_put(&mddev->kobj);
224 static void mddev_put(mddev_t *mddev)
226 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
228 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
229 !mddev->hold_active) {
230 list_del(&mddev->all_mddevs);
231 if (mddev->gendisk) {
232 /* we did a probe so need to clean up.
233 * Call schedule_work inside the spinlock
234 * so that flush_scheduled_work() after
235 * mddev_find will succeed in waiting for the
238 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
239 schedule_work(&mddev->del_work);
243 spin_unlock(&all_mddevs_lock);
246 static mddev_t * mddev_find(dev_t unit)
248 mddev_t *mddev, *new = NULL;
251 spin_lock(&all_mddevs_lock);
252 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
253 if (mddev->unit == unit) {
255 spin_unlock(&all_mddevs_lock);
261 list_add(&new->all_mddevs, &all_mddevs);
262 mddev->hold_active = UNTIL_IOCTL;
263 spin_unlock(&all_mddevs_lock);
266 spin_unlock(&all_mddevs_lock);
268 new = kzalloc(sizeof(*new), GFP_KERNEL);
273 if (MAJOR(unit) == MD_MAJOR)
274 new->md_minor = MINOR(unit);
276 new->md_minor = MINOR(unit) >> MdpMinorShift;
278 mutex_init(&new->reconfig_mutex);
279 INIT_LIST_HEAD(&new->disks);
280 INIT_LIST_HEAD(&new->all_mddevs);
281 init_timer(&new->safemode_timer);
282 atomic_set(&new->active, 1);
283 atomic_set(&new->openers, 0);
284 spin_lock_init(&new->write_lock);
285 init_waitqueue_head(&new->sb_wait);
286 init_waitqueue_head(&new->recovery_wait);
287 new->reshape_position = MaxSector;
289 new->resync_max = MaxSector;
290 new->level = LEVEL_NONE;
295 static inline int mddev_lock(mddev_t * mddev)
297 return mutex_lock_interruptible(&mddev->reconfig_mutex);
300 static inline int mddev_trylock(mddev_t * mddev)
302 return mutex_trylock(&mddev->reconfig_mutex);
305 static inline void mddev_unlock(mddev_t * mddev)
307 mutex_unlock(&mddev->reconfig_mutex);
309 md_wakeup_thread(mddev->thread);
312 static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
316 list_for_each_entry(rdev, &mddev->disks, same_set)
317 if (rdev->desc_nr == nr)
323 static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
327 list_for_each_entry(rdev, &mddev->disks, same_set)
328 if (rdev->bdev->bd_dev == dev)
334 static struct mdk_personality *find_pers(int level, char *clevel)
336 struct mdk_personality *pers;
337 list_for_each_entry(pers, &pers_list, list) {
338 if (level != LEVEL_NONE && pers->level == level)
340 if (strcmp(pers->name, clevel)==0)
346 /* return the offset of the super block in 512byte sectors */
347 static inline sector_t calc_dev_sboffset(struct block_device *bdev)
349 sector_t num_sectors = bdev->bd_inode->i_size / 512;
350 return MD_NEW_SIZE_SECTORS(num_sectors);
353 static sector_t calc_num_sectors(mdk_rdev_t *rdev, unsigned chunk_size)
355 sector_t num_sectors = rdev->sb_start;
358 num_sectors &= ~((sector_t)chunk_size/512 - 1);
362 static int alloc_disk_sb(mdk_rdev_t * rdev)
367 rdev->sb_page = alloc_page(GFP_KERNEL);
368 if (!rdev->sb_page) {
369 printk(KERN_ALERT "md: out of memory.\n");
376 static void free_disk_sb(mdk_rdev_t * rdev)
379 put_page(rdev->sb_page);
381 rdev->sb_page = NULL;
388 static void super_written(struct bio *bio, int error)
390 mdk_rdev_t *rdev = bio->bi_private;
391 mddev_t *mddev = rdev->mddev;
393 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
394 printk("md: super_written gets error=%d, uptodate=%d\n",
395 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
396 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
397 md_error(mddev, rdev);
400 if (atomic_dec_and_test(&mddev->pending_writes))
401 wake_up(&mddev->sb_wait);
405 static void super_written_barrier(struct bio *bio, int error)
407 struct bio *bio2 = bio->bi_private;
408 mdk_rdev_t *rdev = bio2->bi_private;
409 mddev_t *mddev = rdev->mddev;
411 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
412 error == -EOPNOTSUPP) {
414 /* barriers don't appear to be supported :-( */
415 set_bit(BarriersNotsupp, &rdev->flags);
416 mddev->barriers_work = 0;
417 spin_lock_irqsave(&mddev->write_lock, flags);
418 bio2->bi_next = mddev->biolist;
419 mddev->biolist = bio2;
420 spin_unlock_irqrestore(&mddev->write_lock, flags);
421 wake_up(&mddev->sb_wait);
425 bio->bi_private = rdev;
426 super_written(bio, error);
430 void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
431 sector_t sector, int size, struct page *page)
433 /* write first size bytes of page to sector of rdev
434 * Increment mddev->pending_writes before returning
435 * and decrement it on completion, waking up sb_wait
436 * if zero is reached.
437 * If an error occurred, call md_error
439 * As we might need to resubmit the request if BIO_RW_BARRIER
440 * causes ENOTSUPP, we allocate a spare bio...
442 struct bio *bio = bio_alloc(GFP_NOIO, 1);
443 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNC);
445 bio->bi_bdev = rdev->bdev;
446 bio->bi_sector = sector;
447 bio_add_page(bio, page, size, 0);
448 bio->bi_private = rdev;
449 bio->bi_end_io = super_written;
452 atomic_inc(&mddev->pending_writes);
453 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
455 rw |= (1<<BIO_RW_BARRIER);
456 rbio = bio_clone(bio, GFP_NOIO);
457 rbio->bi_private = bio;
458 rbio->bi_end_io = super_written_barrier;
459 submit_bio(rw, rbio);
464 void md_super_wait(mddev_t *mddev)
466 /* wait for all superblock writes that were scheduled to complete.
467 * if any had to be retried (due to BARRIER problems), retry them
471 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
472 if (atomic_read(&mddev->pending_writes)==0)
474 while (mddev->biolist) {
476 spin_lock_irq(&mddev->write_lock);
477 bio = mddev->biolist;
478 mddev->biolist = bio->bi_next ;
480 spin_unlock_irq(&mddev->write_lock);
481 submit_bio(bio->bi_rw, bio);
485 finish_wait(&mddev->sb_wait, &wq);
488 static void bi_complete(struct bio *bio, int error)
490 complete((struct completion*)bio->bi_private);
493 int sync_page_io(struct block_device *bdev, sector_t sector, int size,
494 struct page *page, int rw)
496 struct bio *bio = bio_alloc(GFP_NOIO, 1);
497 struct completion event;
500 rw |= (1 << BIO_RW_SYNC);
503 bio->bi_sector = sector;
504 bio_add_page(bio, page, size, 0);
505 init_completion(&event);
506 bio->bi_private = &event;
507 bio->bi_end_io = bi_complete;
509 wait_for_completion(&event);
511 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
515 EXPORT_SYMBOL_GPL(sync_page_io);
517 static int read_disk_sb(mdk_rdev_t * rdev, int size)
519 char b[BDEVNAME_SIZE];
520 if (!rdev->sb_page) {
528 if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ))
534 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
535 bdevname(rdev->bdev,b));
539 static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
541 return sb1->set_uuid0 == sb2->set_uuid0 &&
542 sb1->set_uuid1 == sb2->set_uuid1 &&
543 sb1->set_uuid2 == sb2->set_uuid2 &&
544 sb1->set_uuid3 == sb2->set_uuid3;
547 static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
550 mdp_super_t *tmp1, *tmp2;
552 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
553 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
555 if (!tmp1 || !tmp2) {
557 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
565 * nr_disks is not constant
570 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
578 static u32 md_csum_fold(u32 csum)
580 csum = (csum & 0xffff) + (csum >> 16);
581 return (csum & 0xffff) + (csum >> 16);
584 static unsigned int calc_sb_csum(mdp_super_t * sb)
587 u32 *sb32 = (u32*)sb;
589 unsigned int disk_csum, csum;
591 disk_csum = sb->sb_csum;
594 for (i = 0; i < MD_SB_BYTES/4 ; i++)
596 csum = (newcsum & 0xffffffff) + (newcsum>>32);
600 /* This used to use csum_partial, which was wrong for several
601 * reasons including that different results are returned on
602 * different architectures. It isn't critical that we get exactly
603 * the same return value as before (we always csum_fold before
604 * testing, and that removes any differences). However as we
605 * know that csum_partial always returned a 16bit value on
606 * alphas, do a fold to maximise conformity to previous behaviour.
608 sb->sb_csum = md_csum_fold(disk_csum);
610 sb->sb_csum = disk_csum;
617 * Handle superblock details.
618 * We want to be able to handle multiple superblock formats
619 * so we have a common interface to them all, and an array of
620 * different handlers.
621 * We rely on user-space to write the initial superblock, and support
622 * reading and updating of superblocks.
623 * Interface methods are:
624 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
625 * loads and validates a superblock on dev.
626 * if refdev != NULL, compare superblocks on both devices
628 * 0 - dev has a superblock that is compatible with refdev
629 * 1 - dev has a superblock that is compatible and newer than refdev
630 * so dev should be used as the refdev in future
631 * -EINVAL superblock incompatible or invalid
632 * -othererror e.g. -EIO
634 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
635 * Verify that dev is acceptable into mddev.
636 * The first time, mddev->raid_disks will be 0, and data from
637 * dev should be merged in. Subsequent calls check that dev
638 * is new enough. Return 0 or -EINVAL
640 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
641 * Update the superblock for rdev with data in mddev
642 * This does not write to disc.
648 struct module *owner;
649 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
651 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
652 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
653 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
654 sector_t num_sectors);
658 * load_super for 0.90.0
660 static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
662 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
667 * Calculate the position of the superblock (512byte sectors),
668 * it's at the end of the disk.
670 * It also happens to be a multiple of 4Kb.
672 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
674 ret = read_disk_sb(rdev, MD_SB_BYTES);
679 bdevname(rdev->bdev, b);
680 sb = (mdp_super_t*)page_address(rdev->sb_page);
682 if (sb->md_magic != MD_SB_MAGIC) {
683 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
688 if (sb->major_version != 0 ||
689 sb->minor_version < 90 ||
690 sb->minor_version > 91) {
691 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
692 sb->major_version, sb->minor_version,
697 if (sb->raid_disks <= 0)
700 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
701 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
706 rdev->preferred_minor = sb->md_minor;
707 rdev->data_offset = 0;
708 rdev->sb_size = MD_SB_BYTES;
710 if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) {
711 if (sb->level != 1 && sb->level != 4
712 && sb->level != 5 && sb->level != 6
713 && sb->level != 10) {
714 /* FIXME use a better test */
716 "md: bitmaps not supported for this level.\n");
721 if (sb->level == LEVEL_MULTIPATH)
724 rdev->desc_nr = sb->this_disk.number;
730 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
731 if (!uuid_equal(refsb, sb)) {
732 printk(KERN_WARNING "md: %s has different UUID to %s\n",
733 b, bdevname(refdev->bdev,b2));
736 if (!sb_equal(refsb, sb)) {
737 printk(KERN_WARNING "md: %s has same UUID"
738 " but different superblock to %s\n",
739 b, bdevname(refdev->bdev, b2));
743 ev2 = md_event(refsb);
749 rdev->size = calc_num_sectors(rdev, sb->chunk_size) / 2;
751 if (rdev->size < sb->size && sb->level > 1)
752 /* "this cannot possibly happen" ... */
760 * validate_super for 0.90.0
762 static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
765 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
766 __u64 ev1 = md_event(sb);
768 rdev->raid_disk = -1;
769 clear_bit(Faulty, &rdev->flags);
770 clear_bit(In_sync, &rdev->flags);
771 clear_bit(WriteMostly, &rdev->flags);
772 clear_bit(BarriersNotsupp, &rdev->flags);
774 if (mddev->raid_disks == 0) {
775 mddev->major_version = 0;
776 mddev->minor_version = sb->minor_version;
777 mddev->patch_version = sb->patch_version;
779 mddev->chunk_size = sb->chunk_size;
780 mddev->ctime = sb->ctime;
781 mddev->utime = sb->utime;
782 mddev->level = sb->level;
783 mddev->clevel[0] = 0;
784 mddev->layout = sb->layout;
785 mddev->raid_disks = sb->raid_disks;
786 mddev->size = sb->size;
788 mddev->bitmap_offset = 0;
789 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
791 if (mddev->minor_version >= 91) {
792 mddev->reshape_position = sb->reshape_position;
793 mddev->delta_disks = sb->delta_disks;
794 mddev->new_level = sb->new_level;
795 mddev->new_layout = sb->new_layout;
796 mddev->new_chunk = sb->new_chunk;
798 mddev->reshape_position = MaxSector;
799 mddev->delta_disks = 0;
800 mddev->new_level = mddev->level;
801 mddev->new_layout = mddev->layout;
802 mddev->new_chunk = mddev->chunk_size;
805 if (sb->state & (1<<MD_SB_CLEAN))
806 mddev->recovery_cp = MaxSector;
808 if (sb->events_hi == sb->cp_events_hi &&
809 sb->events_lo == sb->cp_events_lo) {
810 mddev->recovery_cp = sb->recovery_cp;
812 mddev->recovery_cp = 0;
815 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
816 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
817 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
818 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
820 mddev->max_disks = MD_SB_DISKS;
822 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
823 mddev->bitmap_file == NULL)
824 mddev->bitmap_offset = mddev->default_bitmap_offset;
826 } else if (mddev->pers == NULL) {
827 /* Insist on good event counter while assembling */
829 if (ev1 < mddev->events)
831 } else if (mddev->bitmap) {
832 /* if adding to array with a bitmap, then we can accept an
833 * older device ... but not too old.
835 if (ev1 < mddev->bitmap->events_cleared)
838 if (ev1 < mddev->events)
839 /* just a hot-add of a new device, leave raid_disk at -1 */
843 if (mddev->level != LEVEL_MULTIPATH) {
844 desc = sb->disks + rdev->desc_nr;
846 if (desc->state & (1<<MD_DISK_FAULTY))
847 set_bit(Faulty, &rdev->flags);
848 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
849 desc->raid_disk < mddev->raid_disks */) {
850 set_bit(In_sync, &rdev->flags);
851 rdev->raid_disk = desc->raid_disk;
853 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
854 set_bit(WriteMostly, &rdev->flags);
855 } else /* MULTIPATH are always insync */
856 set_bit(In_sync, &rdev->flags);
861 * sync_super for 0.90.0
863 static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
867 int next_spare = mddev->raid_disks;
870 /* make rdev->sb match mddev data..
873 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
874 * 3/ any empty disks < next_spare become removed
876 * disks[0] gets initialised to REMOVED because
877 * we cannot be sure from other fields if it has
878 * been initialised or not.
881 int active=0, working=0,failed=0,spare=0,nr_disks=0;
883 rdev->sb_size = MD_SB_BYTES;
885 sb = (mdp_super_t*)page_address(rdev->sb_page);
887 memset(sb, 0, sizeof(*sb));
889 sb->md_magic = MD_SB_MAGIC;
890 sb->major_version = mddev->major_version;
891 sb->patch_version = mddev->patch_version;
892 sb->gvalid_words = 0; /* ignored */
893 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
894 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
895 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
896 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
898 sb->ctime = mddev->ctime;
899 sb->level = mddev->level;
900 sb->size = mddev->size;
901 sb->raid_disks = mddev->raid_disks;
902 sb->md_minor = mddev->md_minor;
903 sb->not_persistent = 0;
904 sb->utime = mddev->utime;
906 sb->events_hi = (mddev->events>>32);
907 sb->events_lo = (u32)mddev->events;
909 if (mddev->reshape_position == MaxSector)
910 sb->minor_version = 90;
912 sb->minor_version = 91;
913 sb->reshape_position = mddev->reshape_position;
914 sb->new_level = mddev->new_level;
915 sb->delta_disks = mddev->delta_disks;
916 sb->new_layout = mddev->new_layout;
917 sb->new_chunk = mddev->new_chunk;
919 mddev->minor_version = sb->minor_version;
922 sb->recovery_cp = mddev->recovery_cp;
923 sb->cp_events_hi = (mddev->events>>32);
924 sb->cp_events_lo = (u32)mddev->events;
925 if (mddev->recovery_cp == MaxSector)
926 sb->state = (1<< MD_SB_CLEAN);
930 sb->layout = mddev->layout;
931 sb->chunk_size = mddev->chunk_size;
933 if (mddev->bitmap && mddev->bitmap_file == NULL)
934 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
936 sb->disks[0].state = (1<<MD_DISK_REMOVED);
937 list_for_each_entry(rdev2, &mddev->disks, same_set) {
940 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
941 && !test_bit(Faulty, &rdev2->flags))
942 desc_nr = rdev2->raid_disk;
944 desc_nr = next_spare++;
945 rdev2->desc_nr = desc_nr;
946 d = &sb->disks[rdev2->desc_nr];
948 d->number = rdev2->desc_nr;
949 d->major = MAJOR(rdev2->bdev->bd_dev);
950 d->minor = MINOR(rdev2->bdev->bd_dev);
951 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
952 && !test_bit(Faulty, &rdev2->flags))
953 d->raid_disk = rdev2->raid_disk;
955 d->raid_disk = rdev2->desc_nr; /* compatibility */
956 if (test_bit(Faulty, &rdev2->flags))
957 d->state = (1<<MD_DISK_FAULTY);
958 else if (test_bit(In_sync, &rdev2->flags)) {
959 d->state = (1<<MD_DISK_ACTIVE);
960 d->state |= (1<<MD_DISK_SYNC);
968 if (test_bit(WriteMostly, &rdev2->flags))
969 d->state |= (1<<MD_DISK_WRITEMOSTLY);
971 /* now set the "removed" and "faulty" bits on any missing devices */
972 for (i=0 ; i < mddev->raid_disks ; i++) {
973 mdp_disk_t *d = &sb->disks[i];
974 if (d->state == 0 && d->number == 0) {
977 d->state = (1<<MD_DISK_REMOVED);
978 d->state |= (1<<MD_DISK_FAULTY);
982 sb->nr_disks = nr_disks;
983 sb->active_disks = active;
984 sb->working_disks = working;
985 sb->failed_disks = failed;
986 sb->spare_disks = spare;
988 sb->this_disk = sb->disks[rdev->desc_nr];
989 sb->sb_csum = calc_sb_csum(sb);
993 * rdev_size_change for 0.90.0
995 static unsigned long long
996 super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
998 if (num_sectors && num_sectors < rdev->mddev->size * 2)
999 return 0; /* component must fit device */
1000 if (rdev->mddev->bitmap_offset)
1001 return 0; /* can't move bitmap */
1002 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
1003 if (!num_sectors || num_sectors > rdev->sb_start)
1004 num_sectors = rdev->sb_start;
1005 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1007 md_super_wait(rdev->mddev);
1008 return num_sectors / 2; /* kB for sysfs */
1013 * version 1 superblock
1016 static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
1020 unsigned long long newcsum;
1021 int size = 256 + le32_to_cpu(sb->max_dev)*2;
1022 __le32 *isuper = (__le32*)sb;
1025 disk_csum = sb->sb_csum;
1028 for (i=0; size>=4; size -= 4 )
1029 newcsum += le32_to_cpu(*isuper++);
1032 newcsum += le16_to_cpu(*(__le16*) isuper);
1034 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1035 sb->sb_csum = disk_csum;
1036 return cpu_to_le32(csum);
1039 static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1041 struct mdp_superblock_1 *sb;
1044 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1048 * Calculate the position of the superblock in 512byte sectors.
1049 * It is always aligned to a 4K boundary and
1050 * depeding on minor_version, it can be:
1051 * 0: At least 8K, but less than 12K, from end of device
1052 * 1: At start of device
1053 * 2: 4K from start of device.
1055 switch(minor_version) {
1057 sb_start = rdev->bdev->bd_inode->i_size >> 9;
1059 sb_start &= ~(sector_t)(4*2-1);
1070 rdev->sb_start = sb_start;
1072 /* superblock is rarely larger than 1K, but it can be larger,
1073 * and it is safe to read 4k, so we do that
1075 ret = read_disk_sb(rdev, 4096);
1076 if (ret) return ret;
1079 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1081 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1082 sb->major_version != cpu_to_le32(1) ||
1083 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1084 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
1085 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
1088 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1089 printk("md: invalid superblock checksum on %s\n",
1090 bdevname(rdev->bdev,b));
1093 if (le64_to_cpu(sb->data_size) < 10) {
1094 printk("md: data_size too small on %s\n",
1095 bdevname(rdev->bdev,b));
1098 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET)) {
1099 if (sb->level != cpu_to_le32(1) &&
1100 sb->level != cpu_to_le32(4) &&
1101 sb->level != cpu_to_le32(5) &&
1102 sb->level != cpu_to_le32(6) &&
1103 sb->level != cpu_to_le32(10)) {
1105 "md: bitmaps not supported for this level.\n");
1110 rdev->preferred_minor = 0xffff;
1111 rdev->data_offset = le64_to_cpu(sb->data_offset);
1112 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
1114 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
1115 bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
1116 if (rdev->sb_size & bmask)
1117 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1120 && rdev->data_offset < sb_start + (rdev->sb_size/512))
1123 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1126 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1132 struct mdp_superblock_1 *refsb =
1133 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1135 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1136 sb->level != refsb->level ||
1137 sb->layout != refsb->layout ||
1138 sb->chunksize != refsb->chunksize) {
1139 printk(KERN_WARNING "md: %s has strangely different"
1140 " superblock to %s\n",
1141 bdevname(rdev->bdev,b),
1142 bdevname(refdev->bdev,b2));
1145 ev1 = le64_to_cpu(sb->events);
1146 ev2 = le64_to_cpu(refsb->events);
1154 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
1156 rdev->size = rdev->sb_start / 2;
1157 if (rdev->size < le64_to_cpu(sb->data_size)/2)
1159 rdev->size = le64_to_cpu(sb->data_size)/2;
1160 if (le32_to_cpu(sb->chunksize))
1161 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
1163 if (le64_to_cpu(sb->size) > rdev->size*2)
1168 static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1170 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1171 __u64 ev1 = le64_to_cpu(sb->events);
1173 rdev->raid_disk = -1;
1174 clear_bit(Faulty, &rdev->flags);
1175 clear_bit(In_sync, &rdev->flags);
1176 clear_bit(WriteMostly, &rdev->flags);
1177 clear_bit(BarriersNotsupp, &rdev->flags);
1179 if (mddev->raid_disks == 0) {
1180 mddev->major_version = 1;
1181 mddev->patch_version = 0;
1182 mddev->external = 0;
1183 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
1184 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1185 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1186 mddev->level = le32_to_cpu(sb->level);
1187 mddev->clevel[0] = 0;
1188 mddev->layout = le32_to_cpu(sb->layout);
1189 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1190 mddev->size = le64_to_cpu(sb->size)/2;
1191 mddev->events = ev1;
1192 mddev->bitmap_offset = 0;
1193 mddev->default_bitmap_offset = 1024 >> 9;
1195 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1196 memcpy(mddev->uuid, sb->set_uuid, 16);
1198 mddev->max_disks = (4096-256)/2;
1200 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
1201 mddev->bitmap_file == NULL )
1202 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
1204 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1205 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1206 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1207 mddev->new_level = le32_to_cpu(sb->new_level);
1208 mddev->new_layout = le32_to_cpu(sb->new_layout);
1209 mddev->new_chunk = le32_to_cpu(sb->new_chunk)<<9;
1211 mddev->reshape_position = MaxSector;
1212 mddev->delta_disks = 0;
1213 mddev->new_level = mddev->level;
1214 mddev->new_layout = mddev->layout;
1215 mddev->new_chunk = mddev->chunk_size;
1218 } else if (mddev->pers == NULL) {
1219 /* Insist of good event counter while assembling */
1221 if (ev1 < mddev->events)
1223 } else if (mddev->bitmap) {
1224 /* If adding to array with a bitmap, then we can accept an
1225 * older device, but not too old.
1227 if (ev1 < mddev->bitmap->events_cleared)
1230 if (ev1 < mddev->events)
1231 /* just a hot-add of a new device, leave raid_disk at -1 */
1234 if (mddev->level != LEVEL_MULTIPATH) {
1236 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1238 case 0xffff: /* spare */
1240 case 0xfffe: /* faulty */
1241 set_bit(Faulty, &rdev->flags);
1244 if ((le32_to_cpu(sb->feature_map) &
1245 MD_FEATURE_RECOVERY_OFFSET))
1246 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1248 set_bit(In_sync, &rdev->flags);
1249 rdev->raid_disk = role;
1252 if (sb->devflags & WriteMostly1)
1253 set_bit(WriteMostly, &rdev->flags);
1254 } else /* MULTIPATH are always insync */
1255 set_bit(In_sync, &rdev->flags);
1260 static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1262 struct mdp_superblock_1 *sb;
1265 /* make rdev->sb match mddev and rdev data. */
1267 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1269 sb->feature_map = 0;
1271 sb->recovery_offset = cpu_to_le64(0);
1272 memset(sb->pad1, 0, sizeof(sb->pad1));
1273 memset(sb->pad2, 0, sizeof(sb->pad2));
1274 memset(sb->pad3, 0, sizeof(sb->pad3));
1276 sb->utime = cpu_to_le64((__u64)mddev->utime);
1277 sb->events = cpu_to_le64(mddev->events);
1279 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1281 sb->resync_offset = cpu_to_le64(0);
1283 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
1285 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
1286 sb->size = cpu_to_le64(mddev->size<<1);
1288 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1289 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
1290 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
1293 if (rdev->raid_disk >= 0 &&
1294 !test_bit(In_sync, &rdev->flags) &&
1295 rdev->recovery_offset > 0) {
1296 sb->feature_map |= cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1297 sb->recovery_offset = cpu_to_le64(rdev->recovery_offset);
1300 if (mddev->reshape_position != MaxSector) {
1301 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1302 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1303 sb->new_layout = cpu_to_le32(mddev->new_layout);
1304 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1305 sb->new_level = cpu_to_le32(mddev->new_level);
1306 sb->new_chunk = cpu_to_le32(mddev->new_chunk>>9);
1310 list_for_each_entry(rdev2, &mddev->disks, same_set)
1311 if (rdev2->desc_nr+1 > max_dev)
1312 max_dev = rdev2->desc_nr+1;
1314 if (max_dev > le32_to_cpu(sb->max_dev))
1315 sb->max_dev = cpu_to_le32(max_dev);
1316 for (i=0; i<max_dev;i++)
1317 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1319 list_for_each_entry(rdev2, &mddev->disks, same_set) {
1321 if (test_bit(Faulty, &rdev2->flags))
1322 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1323 else if (test_bit(In_sync, &rdev2->flags))
1324 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1325 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1326 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1328 sb->dev_roles[i] = cpu_to_le16(0xffff);
1331 sb->sb_csum = calc_sb_1_csum(sb);
1334 static unsigned long long
1335 super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
1337 struct mdp_superblock_1 *sb;
1338 sector_t max_sectors;
1339 if (num_sectors && num_sectors < rdev->mddev->size * 2)
1340 return 0; /* component must fit device */
1341 if (rdev->sb_start < rdev->data_offset) {
1342 /* minor versions 1 and 2; superblock before data */
1343 max_sectors = rdev->bdev->bd_inode->i_size >> 9;
1344 max_sectors -= rdev->data_offset;
1345 if (!num_sectors || num_sectors > max_sectors)
1346 num_sectors = max_sectors;
1347 } else if (rdev->mddev->bitmap_offset) {
1348 /* minor version 0 with bitmap we can't move */
1351 /* minor version 0; superblock after data */
1353 sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1354 sb_start &= ~(sector_t)(4*2 - 1);
1355 max_sectors = rdev->size * 2 + sb_start - rdev->sb_start;
1356 if (!num_sectors || num_sectors > max_sectors)
1357 num_sectors = max_sectors;
1358 rdev->sb_start = sb_start;
1360 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
1361 sb->data_size = cpu_to_le64(num_sectors);
1362 sb->super_offset = rdev->sb_start;
1363 sb->sb_csum = calc_sb_1_csum(sb);
1364 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1366 md_super_wait(rdev->mddev);
1367 return num_sectors / 2; /* kB for sysfs */
1370 static struct super_type super_types[] = {
1373 .owner = THIS_MODULE,
1374 .load_super = super_90_load,
1375 .validate_super = super_90_validate,
1376 .sync_super = super_90_sync,
1377 .rdev_size_change = super_90_rdev_size_change,
1381 .owner = THIS_MODULE,
1382 .load_super = super_1_load,
1383 .validate_super = super_1_validate,
1384 .sync_super = super_1_sync,
1385 .rdev_size_change = super_1_rdev_size_change,
1389 static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1391 mdk_rdev_t *rdev, *rdev2;
1394 rdev_for_each_rcu(rdev, mddev1)
1395 rdev_for_each_rcu(rdev2, mddev2)
1396 if (rdev->bdev->bd_contains ==
1397 rdev2->bdev->bd_contains) {
1405 static LIST_HEAD(pending_raid_disks);
1407 static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1409 char b[BDEVNAME_SIZE];
1419 /* prevent duplicates */
1420 if (find_rdev(mddev, rdev->bdev->bd_dev))
1423 /* make sure rdev->size exceeds mddev->size */
1424 if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) {
1426 /* Cannot change size, so fail
1427 * If mddev->level <= 0, then we don't care
1428 * about aligning sizes (e.g. linear)
1430 if (mddev->level > 0)
1433 mddev->size = rdev->size;
1436 /* Verify rdev->desc_nr is unique.
1437 * If it is -1, assign a free number, else
1438 * check number is not in use
1440 if (rdev->desc_nr < 0) {
1442 if (mddev->pers) choice = mddev->raid_disks;
1443 while (find_rdev_nr(mddev, choice))
1445 rdev->desc_nr = choice;
1447 if (find_rdev_nr(mddev, rdev->desc_nr))
1450 bdevname(rdev->bdev,b);
1451 while ( (s=strchr(b, '/')) != NULL)
1454 rdev->mddev = mddev;
1455 printk(KERN_INFO "md: bind<%s>\n", b);
1457 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
1460 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
1461 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1462 kobject_del(&rdev->kobj);
1465 rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state");
1467 list_add_rcu(&rdev->same_set, &mddev->disks);
1468 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
1472 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1477 static void md_delayed_delete(struct work_struct *ws)
1479 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1480 kobject_del(&rdev->kobj);
1481 kobject_put(&rdev->kobj);
1484 static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1486 char b[BDEVNAME_SIZE];
1491 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
1492 list_del_rcu(&rdev->same_set);
1493 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1495 sysfs_remove_link(&rdev->kobj, "block");
1496 sysfs_put(rdev->sysfs_state);
1497 rdev->sysfs_state = NULL;
1498 /* We need to delay this, otherwise we can deadlock when
1499 * writing to 'remove' to "dev/state". We also need
1500 * to delay it due to rcu usage.
1503 INIT_WORK(&rdev->del_work, md_delayed_delete);
1504 kobject_get(&rdev->kobj);
1505 schedule_work(&rdev->del_work);
1509 * prevent the device from being mounted, repartitioned or
1510 * otherwise reused by a RAID array (or any other kernel
1511 * subsystem), by bd_claiming the device.
1513 static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
1516 struct block_device *bdev;
1517 char b[BDEVNAME_SIZE];
1519 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
1521 printk(KERN_ERR "md: could not open %s.\n",
1522 __bdevname(dev, b));
1523 return PTR_ERR(bdev);
1525 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
1527 printk(KERN_ERR "md: could not bd_claim %s.\n",
1529 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
1533 set_bit(AllReserved, &rdev->flags);
1538 static void unlock_rdev(mdk_rdev_t *rdev)
1540 struct block_device *bdev = rdev->bdev;
1545 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
1548 void md_autodetect_dev(dev_t dev);
1550 static void export_rdev(mdk_rdev_t * rdev)
1552 char b[BDEVNAME_SIZE];
1553 printk(KERN_INFO "md: export_rdev(%s)\n",
1554 bdevname(rdev->bdev,b));
1559 if (test_bit(AutoDetected, &rdev->flags))
1560 md_autodetect_dev(rdev->bdev->bd_dev);
1563 kobject_put(&rdev->kobj);
1566 static void kick_rdev_from_array(mdk_rdev_t * rdev)
1568 unbind_rdev_from_array(rdev);
1572 static void export_array(mddev_t *mddev)
1574 mdk_rdev_t *rdev, *tmp;
1576 rdev_for_each(rdev, tmp, mddev) {
1581 kick_rdev_from_array(rdev);
1583 if (!list_empty(&mddev->disks))
1585 mddev->raid_disks = 0;
1586 mddev->major_version = 0;
1589 static void print_desc(mdp_disk_t *desc)
1591 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1592 desc->major,desc->minor,desc->raid_disk,desc->state);
1595 static void print_sb_90(mdp_super_t *sb)
1600 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1601 sb->major_version, sb->minor_version, sb->patch_version,
1602 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1604 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1605 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1606 sb->md_minor, sb->layout, sb->chunk_size);
1607 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1608 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1609 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1610 sb->failed_disks, sb->spare_disks,
1611 sb->sb_csum, (unsigned long)sb->events_lo);
1614 for (i = 0; i < MD_SB_DISKS; i++) {
1617 desc = sb->disks + i;
1618 if (desc->number || desc->major || desc->minor ||
1619 desc->raid_disk || (desc->state && (desc->state != 4))) {
1620 printk(" D %2d: ", i);
1624 printk(KERN_INFO "md: THIS: ");
1625 print_desc(&sb->this_disk);
1628 static void print_sb_1(struct mdp_superblock_1 *sb)
1632 uuid = sb->set_uuid;
1633 printk(KERN_INFO "md: SB: (V:%u) (F:0x%08x) Array-ID:<%02x%02x%02x%02x"
1634 ":%02x%02x:%02x%02x:%02x%02x:%02x%02x%02x%02x%02x%02x>\n"
1635 KERN_INFO "md: Name: \"%s\" CT:%llu\n",
1636 le32_to_cpu(sb->major_version),
1637 le32_to_cpu(sb->feature_map),
1638 uuid[0], uuid[1], uuid[2], uuid[3],
1639 uuid[4], uuid[5], uuid[6], uuid[7],
1640 uuid[8], uuid[9], uuid[10], uuid[11],
1641 uuid[12], uuid[13], uuid[14], uuid[15],
1643 (unsigned long long)le64_to_cpu(sb->ctime)
1644 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
1646 uuid = sb->device_uuid;
1647 printk(KERN_INFO "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
1649 KERN_INFO "md: Dev:%08x UUID: %02x%02x%02x%02x:%02x%02x:%02x%02x:%02x%02x"
1650 ":%02x%02x%02x%02x%02x%02x\n"
1651 KERN_INFO "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
1652 KERN_INFO "md: (MaxDev:%u) \n",
1653 le32_to_cpu(sb->level),
1654 (unsigned long long)le64_to_cpu(sb->size),
1655 le32_to_cpu(sb->raid_disks),
1656 le32_to_cpu(sb->layout),
1657 le32_to_cpu(sb->chunksize),
1658 (unsigned long long)le64_to_cpu(sb->data_offset),
1659 (unsigned long long)le64_to_cpu(sb->data_size),
1660 (unsigned long long)le64_to_cpu(sb->super_offset),
1661 (unsigned long long)le64_to_cpu(sb->recovery_offset),
1662 le32_to_cpu(sb->dev_number),
1663 uuid[0], uuid[1], uuid[2], uuid[3],
1664 uuid[4], uuid[5], uuid[6], uuid[7],
1665 uuid[8], uuid[9], uuid[10], uuid[11],
1666 uuid[12], uuid[13], uuid[14], uuid[15],
1668 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
1669 (unsigned long long)le64_to_cpu(sb->events),
1670 (unsigned long long)le64_to_cpu(sb->resync_offset),
1671 le32_to_cpu(sb->sb_csum),
1672 le32_to_cpu(sb->max_dev)
1676 static void print_rdev(mdk_rdev_t *rdev, int major_version)
1678 char b[BDEVNAME_SIZE];
1679 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1680 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
1681 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1683 if (rdev->sb_loaded) {
1684 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
1685 switch (major_version) {
1687 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
1690 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
1694 printk(KERN_INFO "md: no rdev superblock!\n");
1697 static void md_print_devices(void)
1699 struct list_head *tmp;
1702 char b[BDEVNAME_SIZE];
1705 printk("md: **********************************\n");
1706 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1707 printk("md: **********************************\n");
1708 for_each_mddev(mddev, tmp) {
1711 bitmap_print_sb(mddev->bitmap);
1713 printk("%s: ", mdname(mddev));
1714 list_for_each_entry(rdev, &mddev->disks, same_set)
1715 printk("<%s>", bdevname(rdev->bdev,b));
1718 list_for_each_entry(rdev, &mddev->disks, same_set)
1719 print_rdev(rdev, mddev->major_version);
1721 printk("md: **********************************\n");
1726 static void sync_sbs(mddev_t * mddev, int nospares)
1728 /* Update each superblock (in-memory image), but
1729 * if we are allowed to, skip spares which already
1730 * have the right event counter, or have one earlier
1731 * (which would mean they aren't being marked as dirty
1732 * with the rest of the array)
1736 list_for_each_entry(rdev, &mddev->disks, same_set) {
1737 if (rdev->sb_events == mddev->events ||
1739 rdev->raid_disk < 0 &&
1740 (rdev->sb_events&1)==0 &&
1741 rdev->sb_events+1 == mddev->events)) {
1742 /* Don't update this superblock */
1743 rdev->sb_loaded = 2;
1745 super_types[mddev->major_version].
1746 sync_super(mddev, rdev);
1747 rdev->sb_loaded = 1;
1752 static void md_update_sb(mddev_t * mddev, int force_change)
1758 if (mddev->external)
1761 spin_lock_irq(&mddev->write_lock);
1763 set_bit(MD_CHANGE_PENDING, &mddev->flags);
1764 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
1766 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
1767 /* just a clean<-> dirty transition, possibly leave spares alone,
1768 * though if events isn't the right even/odd, we will have to do
1774 if (mddev->degraded)
1775 /* If the array is degraded, then skipping spares is both
1776 * dangerous and fairly pointless.
1777 * Dangerous because a device that was removed from the array
1778 * might have a event_count that still looks up-to-date,
1779 * so it can be re-added without a resync.
1780 * Pointless because if there are any spares to skip,
1781 * then a recovery will happen and soon that array won't
1782 * be degraded any more and the spare can go back to sleep then.
1786 sync_req = mddev->in_sync;
1787 mddev->utime = get_seconds();
1789 /* If this is just a dirty<->clean transition, and the array is clean
1790 * and 'events' is odd, we can roll back to the previous clean state */
1792 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
1793 && (mddev->events & 1)
1794 && mddev->events != 1)
1797 /* otherwise we have to go forward and ... */
1799 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
1800 /* .. if the array isn't clean, insist on an odd 'events' */
1801 if ((mddev->events&1)==0) {
1806 /* otherwise insist on an even 'events' (for clean states) */
1807 if ((mddev->events&1)) {
1814 if (!mddev->events) {
1816 * oops, this 64-bit counter should never wrap.
1817 * Either we are in around ~1 trillion A.C., assuming
1818 * 1 reboot per second, or we have a bug:
1825 * do not write anything to disk if using
1826 * nonpersistent superblocks
1828 if (!mddev->persistent) {
1829 if (!mddev->external)
1830 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
1832 spin_unlock_irq(&mddev->write_lock);
1833 wake_up(&mddev->sb_wait);
1836 sync_sbs(mddev, nospares);
1837 spin_unlock_irq(&mddev->write_lock);
1840 "md: updating %s RAID superblock on device (in sync %d)\n",
1841 mdname(mddev),mddev->in_sync);
1843 bitmap_update_sb(mddev->bitmap);
1844 list_for_each_entry(rdev, &mddev->disks, same_set) {
1845 char b[BDEVNAME_SIZE];
1846 dprintk(KERN_INFO "md: ");
1847 if (rdev->sb_loaded != 1)
1848 continue; /* no noise on spare devices */
1849 if (test_bit(Faulty, &rdev->flags))
1850 dprintk("(skipping faulty ");
1852 dprintk("%s ", bdevname(rdev->bdev,b));
1853 if (!test_bit(Faulty, &rdev->flags)) {
1854 md_super_write(mddev,rdev,
1855 rdev->sb_start, rdev->sb_size,
1857 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1858 bdevname(rdev->bdev,b),
1859 (unsigned long long)rdev->sb_start);
1860 rdev->sb_events = mddev->events;
1864 if (mddev->level == LEVEL_MULTIPATH)
1865 /* only need to write one superblock... */
1868 md_super_wait(mddev);
1869 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
1871 spin_lock_irq(&mddev->write_lock);
1872 if (mddev->in_sync != sync_req ||
1873 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
1874 /* have to write it out again */
1875 spin_unlock_irq(&mddev->write_lock);
1878 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
1879 spin_unlock_irq(&mddev->write_lock);
1880 wake_up(&mddev->sb_wait);
1884 /* words written to sysfs files may, or may not, be \n terminated.
1885 * We want to accept with case. For this we use cmd_match.
1887 static int cmd_match(const char *cmd, const char *str)
1889 /* See if cmd, written into a sysfs file, matches
1890 * str. They must either be the same, or cmd can
1891 * have a trailing newline
1893 while (*cmd && *str && *cmd == *str) {
1904 struct rdev_sysfs_entry {
1905 struct attribute attr;
1906 ssize_t (*show)(mdk_rdev_t *, char *);
1907 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1911 state_show(mdk_rdev_t *rdev, char *page)
1916 if (test_bit(Faulty, &rdev->flags)) {
1917 len+= sprintf(page+len, "%sfaulty",sep);
1920 if (test_bit(In_sync, &rdev->flags)) {
1921 len += sprintf(page+len, "%sin_sync",sep);
1924 if (test_bit(WriteMostly, &rdev->flags)) {
1925 len += sprintf(page+len, "%swrite_mostly",sep);
1928 if (test_bit(Blocked, &rdev->flags)) {
1929 len += sprintf(page+len, "%sblocked", sep);
1932 if (!test_bit(Faulty, &rdev->flags) &&
1933 !test_bit(In_sync, &rdev->flags)) {
1934 len += sprintf(page+len, "%sspare", sep);
1937 return len+sprintf(page+len, "\n");
1941 state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1944 * faulty - simulates and error
1945 * remove - disconnects the device
1946 * writemostly - sets write_mostly
1947 * -writemostly - clears write_mostly
1948 * blocked - sets the Blocked flag
1949 * -blocked - clears the Blocked flag
1952 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
1953 md_error(rdev->mddev, rdev);
1955 } else if (cmd_match(buf, "remove")) {
1956 if (rdev->raid_disk >= 0)
1959 mddev_t *mddev = rdev->mddev;
1960 kick_rdev_from_array(rdev);
1962 md_update_sb(mddev, 1);
1963 md_new_event(mddev);
1966 } else if (cmd_match(buf, "writemostly")) {
1967 set_bit(WriteMostly, &rdev->flags);
1969 } else if (cmd_match(buf, "-writemostly")) {
1970 clear_bit(WriteMostly, &rdev->flags);
1972 } else if (cmd_match(buf, "blocked")) {
1973 set_bit(Blocked, &rdev->flags);
1975 } else if (cmd_match(buf, "-blocked")) {
1976 clear_bit(Blocked, &rdev->flags);
1977 wake_up(&rdev->blocked_wait);
1978 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
1979 md_wakeup_thread(rdev->mddev->thread);
1983 if (!err && rdev->sysfs_state)
1984 sysfs_notify_dirent(rdev->sysfs_state);
1985 return err ? err : len;
1987 static struct rdev_sysfs_entry rdev_state =
1988 __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
1991 errors_show(mdk_rdev_t *rdev, char *page)
1993 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
1997 errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2000 unsigned long n = simple_strtoul(buf, &e, 10);
2001 if (*buf && (*e == 0 || *e == '\n')) {
2002 atomic_set(&rdev->corrected_errors, n);
2007 static struct rdev_sysfs_entry rdev_errors =
2008 __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
2011 slot_show(mdk_rdev_t *rdev, char *page)
2013 if (rdev->raid_disk < 0)
2014 return sprintf(page, "none\n");
2016 return sprintf(page, "%d\n", rdev->raid_disk);
2020 slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2025 int slot = simple_strtoul(buf, &e, 10);
2026 if (strncmp(buf, "none", 4)==0)
2028 else if (e==buf || (*e && *e!= '\n'))
2030 if (rdev->mddev->pers && slot == -1) {
2031 /* Setting 'slot' on an active array requires also
2032 * updating the 'rd%d' link, and communicating
2033 * with the personality with ->hot_*_disk.
2034 * For now we only support removing
2035 * failed/spare devices. This normally happens automatically,
2036 * but not when the metadata is externally managed.
2038 if (rdev->raid_disk == -1)
2040 /* personality does all needed checks */
2041 if (rdev->mddev->pers->hot_add_disk == NULL)
2043 err = rdev->mddev->pers->
2044 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2047 sprintf(nm, "rd%d", rdev->raid_disk);
2048 sysfs_remove_link(&rdev->mddev->kobj, nm);
2049 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2050 md_wakeup_thread(rdev->mddev->thread);
2051 } else if (rdev->mddev->pers) {
2053 /* Activating a spare .. or possibly reactivating
2054 * if we every get bitmaps working here.
2057 if (rdev->raid_disk != -1)
2060 if (rdev->mddev->pers->hot_add_disk == NULL)
2063 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
2064 if (rdev2->raid_disk == slot)
2067 rdev->raid_disk = slot;
2068 if (test_bit(In_sync, &rdev->flags))
2069 rdev->saved_raid_disk = slot;
2071 rdev->saved_raid_disk = -1;
2072 err = rdev->mddev->pers->
2073 hot_add_disk(rdev->mddev, rdev);
2075 rdev->raid_disk = -1;
2078 sysfs_notify_dirent(rdev->sysfs_state);
2079 sprintf(nm, "rd%d", rdev->raid_disk);
2080 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2082 "md: cannot register "
2084 nm, mdname(rdev->mddev));
2086 /* don't wakeup anyone, leave that to userspace. */
2088 if (slot >= rdev->mddev->raid_disks)
2090 rdev->raid_disk = slot;
2091 /* assume it is working */
2092 clear_bit(Faulty, &rdev->flags);
2093 clear_bit(WriteMostly, &rdev->flags);
2094 set_bit(In_sync, &rdev->flags);
2095 sysfs_notify_dirent(rdev->sysfs_state);
2101 static struct rdev_sysfs_entry rdev_slot =
2102 __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
2105 offset_show(mdk_rdev_t *rdev, char *page)
2107 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
2111 offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2114 unsigned long long offset = simple_strtoull(buf, &e, 10);
2115 if (e==buf || (*e && *e != '\n'))
2117 if (rdev->mddev->pers && rdev->raid_disk >= 0)
2119 if (rdev->size && rdev->mddev->external)
2120 /* Must set offset before size, so overlap checks
2123 rdev->data_offset = offset;
2127 static struct rdev_sysfs_entry rdev_offset =
2128 __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
2131 rdev_size_show(mdk_rdev_t *rdev, char *page)
2133 return sprintf(page, "%llu\n", (unsigned long long)rdev->size);
2136 static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2138 /* check if two start/length pairs overlap */
2147 rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2149 unsigned long long size;
2150 unsigned long long oldsize = rdev->size;
2151 mddev_t *my_mddev = rdev->mddev;
2153 if (strict_strtoull(buf, 10, &size) < 0)
2155 if (my_mddev->pers && rdev->raid_disk >= 0) {
2156 if (my_mddev->persistent) {
2157 size = super_types[my_mddev->major_version].
2158 rdev_size_change(rdev, size * 2);
2162 size = (rdev->bdev->bd_inode->i_size >> 10);
2163 size -= rdev->data_offset/2;
2166 if (size < my_mddev->size)
2167 return -EINVAL; /* component must fit device */
2170 if (size > oldsize && my_mddev->external) {
2171 /* need to check that all other rdevs with the same ->bdev
2172 * do not overlap. We need to unlock the mddev to avoid
2173 * a deadlock. We have already changed rdev->size, and if
2174 * we have to change it back, we will have the lock again.
2178 struct list_head *tmp;
2180 mddev_unlock(my_mddev);
2181 for_each_mddev(mddev, tmp) {
2185 list_for_each_entry(rdev2, &mddev->disks, same_set)
2186 if (test_bit(AllReserved, &rdev2->flags) ||
2187 (rdev->bdev == rdev2->bdev &&
2189 overlaps(rdev->data_offset, rdev->size * 2,
2191 rdev2->size * 2))) {
2195 mddev_unlock(mddev);
2201 mddev_lock(my_mddev);
2203 /* Someone else could have slipped in a size
2204 * change here, but doing so is just silly.
2205 * We put oldsize back because we *know* it is
2206 * safe, and trust userspace not to race with
2209 rdev->size = oldsize;
2216 static struct rdev_sysfs_entry rdev_size =
2217 __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
2219 static struct attribute *rdev_default_attrs[] = {
2228 rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2230 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2231 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
2232 mddev_t *mddev = rdev->mddev;
2238 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2240 if (rdev->mddev == NULL)
2243 rv = entry->show(rdev, page);
2244 mddev_unlock(mddev);
2250 rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2251 const char *page, size_t length)
2253 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2254 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
2256 mddev_t *mddev = rdev->mddev;
2260 if (!capable(CAP_SYS_ADMIN))
2262 rv = mddev ? mddev_lock(mddev): -EBUSY;
2264 if (rdev->mddev == NULL)
2267 rv = entry->store(rdev, page, length);
2268 mddev_unlock(mddev);
2273 static void rdev_free(struct kobject *ko)
2275 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2278 static struct sysfs_ops rdev_sysfs_ops = {
2279 .show = rdev_attr_show,
2280 .store = rdev_attr_store,
2282 static struct kobj_type rdev_ktype = {
2283 .release = rdev_free,
2284 .sysfs_ops = &rdev_sysfs_ops,
2285 .default_attrs = rdev_default_attrs,
2289 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2291 * mark the device faulty if:
2293 * - the device is nonexistent (zero size)
2294 * - the device has no valid superblock
2296 * a faulty rdev _never_ has rdev->sb set.
2298 static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2300 char b[BDEVNAME_SIZE];
2305 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
2307 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2308 return ERR_PTR(-ENOMEM);
2311 if ((err = alloc_disk_sb(rdev)))
2314 err = lock_rdev(rdev, newdev, super_format == -2);
2318 kobject_init(&rdev->kobj, &rdev_ktype);
2321 rdev->saved_raid_disk = -1;
2322 rdev->raid_disk = -1;
2324 rdev->data_offset = 0;
2325 rdev->sb_events = 0;
2326 atomic_set(&rdev->nr_pending, 0);
2327 atomic_set(&rdev->read_errors, 0);
2328 atomic_set(&rdev->corrected_errors, 0);
2330 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2333 "md: %s has zero or unknown size, marking faulty!\n",
2334 bdevname(rdev->bdev,b));
2339 if (super_format >= 0) {
2340 err = super_types[super_format].
2341 load_super(rdev, NULL, super_minor);
2342 if (err == -EINVAL) {
2344 "md: %s does not have a valid v%d.%d "
2345 "superblock, not importing!\n",
2346 bdevname(rdev->bdev,b),
2347 super_format, super_minor);
2352 "md: could not read %s's sb, not importing!\n",
2353 bdevname(rdev->bdev,b));
2358 INIT_LIST_HEAD(&rdev->same_set);
2359 init_waitqueue_head(&rdev->blocked_wait);
2364 if (rdev->sb_page) {
2370 return ERR_PTR(err);
2374 * Check a full RAID array for plausibility
2378 static void analyze_sbs(mddev_t * mddev)
2381 mdk_rdev_t *rdev, *freshest, *tmp;
2382 char b[BDEVNAME_SIZE];
2385 rdev_for_each(rdev, tmp, mddev)
2386 switch (super_types[mddev->major_version].
2387 load_super(rdev, freshest, mddev->minor_version)) {
2395 "md: fatal superblock inconsistency in %s"
2396 " -- removing from array\n",
2397 bdevname(rdev->bdev,b));
2398 kick_rdev_from_array(rdev);
2402 super_types[mddev->major_version].
2403 validate_super(mddev, freshest);
2406 rdev_for_each(rdev, tmp, mddev) {
2407 if (rdev != freshest)
2408 if (super_types[mddev->major_version].
2409 validate_super(mddev, rdev)) {
2410 printk(KERN_WARNING "md: kicking non-fresh %s"
2412 bdevname(rdev->bdev,b));
2413 kick_rdev_from_array(rdev);
2416 if (mddev->level == LEVEL_MULTIPATH) {
2417 rdev->desc_nr = i++;
2418 rdev->raid_disk = rdev->desc_nr;
2419 set_bit(In_sync, &rdev->flags);
2420 } else if (rdev->raid_disk >= mddev->raid_disks) {
2421 rdev->raid_disk = -1;
2422 clear_bit(In_sync, &rdev->flags);
2428 if (mddev->recovery_cp != MaxSector &&
2430 printk(KERN_ERR "md: %s: raid array is not clean"
2431 " -- starting background reconstruction\n",
2436 static void md_safemode_timeout(unsigned long data);
2439 safe_delay_show(mddev_t *mddev, char *page)
2441 int msec = (mddev->safemode_delay*1000)/HZ;
2442 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2445 safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2453 /* remove a period, and count digits after it */
2454 if (len >= sizeof(buf))
2456 strlcpy(buf, cbuf, sizeof(buf));
2457 for (i=0; i<len; i++) {
2459 if (isdigit(buf[i])) {
2464 } else if (buf[i] == '.') {
2469 if (strict_strtoul(buf, 10, &msec) < 0)
2471 msec = (msec * 1000) / scale;
2473 mddev->safemode_delay = 0;
2475 unsigned long old_delay = mddev->safemode_delay;
2476 mddev->safemode_delay = (msec*HZ)/1000;
2477 if (mddev->safemode_delay == 0)
2478 mddev->safemode_delay = 1;
2479 if (mddev->safemode_delay < old_delay)
2480 md_safemode_timeout((unsigned long)mddev);
2484 static struct md_sysfs_entry md_safe_delay =
2485 __ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
2488 level_show(mddev_t *mddev, char *page)
2490 struct mdk_personality *p = mddev->pers;
2492 return sprintf(page, "%s\n", p->name);
2493 else if (mddev->clevel[0])
2494 return sprintf(page, "%s\n", mddev->clevel);
2495 else if (mddev->level != LEVEL_NONE)
2496 return sprintf(page, "%d\n", mddev->level);
2502 level_store(mddev_t *mddev, const char *buf, size_t len)
2509 if (len >= sizeof(mddev->clevel))
2511 strncpy(mddev->clevel, buf, len);
2512 if (mddev->clevel[len-1] == '\n')
2514 mddev->clevel[len] = 0;
2515 mddev->level = LEVEL_NONE;
2519 static struct md_sysfs_entry md_level =
2520 __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
2524 layout_show(mddev_t *mddev, char *page)
2526 /* just a number, not meaningful for all levels */
2527 if (mddev->reshape_position != MaxSector &&
2528 mddev->layout != mddev->new_layout)
2529 return sprintf(page, "%d (%d)\n",
2530 mddev->new_layout, mddev->layout);
2531 return sprintf(page, "%d\n", mddev->layout);
2535 layout_store(mddev_t *mddev, const char *buf, size_t len)
2538 unsigned long n = simple_strtoul(buf, &e, 10);
2540 if (!*buf || (*e && *e != '\n'))
2545 if (mddev->reshape_position != MaxSector)
2546 mddev->new_layout = n;
2551 static struct md_sysfs_entry md_layout =
2552 __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
2556 raid_disks_show(mddev_t *mddev, char *page)
2558 if (mddev->raid_disks == 0)
2560 if (mddev->reshape_position != MaxSector &&
2561 mddev->delta_disks != 0)
2562 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
2563 mddev->raid_disks - mddev->delta_disks);
2564 return sprintf(page, "%d\n", mddev->raid_disks);
2567 static int update_raid_disks(mddev_t *mddev, int raid_disks);
2570 raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2574 unsigned long n = simple_strtoul(buf, &e, 10);
2576 if (!*buf || (*e && *e != '\n'))
2580 rv = update_raid_disks(mddev, n);
2581 else if (mddev->reshape_position != MaxSector) {
2582 int olddisks = mddev->raid_disks - mddev->delta_disks;
2583 mddev->delta_disks = n - olddisks;
2584 mddev->raid_disks = n;
2586 mddev->raid_disks = n;
2587 return rv ? rv : len;
2589 static struct md_sysfs_entry md_raid_disks =
2590 __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
2593 chunk_size_show(mddev_t *mddev, char *page)
2595 if (mddev->reshape_position != MaxSector &&
2596 mddev->chunk_size != mddev->new_chunk)
2597 return sprintf(page, "%d (%d)\n", mddev->new_chunk,
2599 return sprintf(page, "%d\n", mddev->chunk_size);
2603 chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2605 /* can only set chunk_size if array is not yet active */
2607 unsigned long n = simple_strtoul(buf, &e, 10);
2609 if (!*buf || (*e && *e != '\n'))
2614 else if (mddev->reshape_position != MaxSector)
2615 mddev->new_chunk = n;
2617 mddev->chunk_size = n;
2620 static struct md_sysfs_entry md_chunk_size =
2621 __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
2624 resync_start_show(mddev_t *mddev, char *page)
2626 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2630 resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2633 unsigned long long n = simple_strtoull(buf, &e, 10);
2637 if (!*buf || (*e && *e != '\n'))
2640 mddev->recovery_cp = n;
2643 static struct md_sysfs_entry md_resync_start =
2644 __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
2647 * The array state can be:
2650 * No devices, no size, no level
2651 * Equivalent to STOP_ARRAY ioctl
2653 * May have some settings, but array is not active
2654 * all IO results in error
2655 * When written, doesn't tear down array, but just stops it
2656 * suspended (not supported yet)
2657 * All IO requests will block. The array can be reconfigured.
2658 * Writing this, if accepted, will block until array is quiescent
2660 * no resync can happen. no superblocks get written.
2661 * write requests fail
2663 * like readonly, but behaves like 'clean' on a write request.
2665 * clean - no pending writes, but otherwise active.
2666 * When written to inactive array, starts without resync
2667 * If a write request arrives then
2668 * if metadata is known, mark 'dirty' and switch to 'active'.
2669 * if not known, block and switch to write-pending
2670 * If written to an active array that has pending writes, then fails.
2672 * fully active: IO and resync can be happening.
2673 * When written to inactive array, starts with resync
2676 * clean, but writes are blocked waiting for 'active' to be written.
2679 * like active, but no writes have been seen for a while (100msec).
2682 enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
2683 write_pending, active_idle, bad_word};
2684 static char *array_states[] = {
2685 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2686 "write-pending", "active-idle", NULL };
2688 static int match_word(const char *word, char **list)
2691 for (n=0; list[n]; n++)
2692 if (cmd_match(word, list[n]))
2698 array_state_show(mddev_t *mddev, char *page)
2700 enum array_state st = inactive;
2713 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
2715 else if (mddev->safemode)
2721 if (list_empty(&mddev->disks) &&
2722 mddev->raid_disks == 0 &&
2728 return sprintf(page, "%s\n", array_states[st]);
2731 static int do_md_stop(mddev_t * mddev, int ro, int is_open);
2732 static int do_md_run(mddev_t * mddev);
2733 static int restart_array(mddev_t *mddev);
2736 array_state_store(mddev_t *mddev, const char *buf, size_t len)
2739 enum array_state st = match_word(buf, array_states);
2744 /* stopping an active array */
2745 if (atomic_read(&mddev->openers) > 0)
2747 err = do_md_stop(mddev, 0, 0);
2750 /* stopping an active array */
2752 if (atomic_read(&mddev->openers) > 0)
2754 err = do_md_stop(mddev, 2, 0);
2756 err = 0; /* already inactive */
2759 break; /* not supported yet */
2762 err = do_md_stop(mddev, 1, 0);
2765 set_disk_ro(mddev->gendisk, 1);
2766 err = do_md_run(mddev);
2772 err = do_md_stop(mddev, 1, 0);
2773 else if (mddev->ro == 1)
2774 err = restart_array(mddev);
2777 set_disk_ro(mddev->gendisk, 0);
2781 err = do_md_run(mddev);
2786 restart_array(mddev);
2787 spin_lock_irq(&mddev->write_lock);
2788 if (atomic_read(&mddev->writes_pending) == 0) {
2789 if (mddev->in_sync == 0) {
2791 if (mddev->safemode == 1)
2792 mddev->safemode = 0;
2793 if (mddev->persistent)
2794 set_bit(MD_CHANGE_CLEAN,
2800 spin_unlock_irq(&mddev->write_lock);
2803 mddev->recovery_cp = MaxSector;
2804 err = do_md_run(mddev);
2809 restart_array(mddev);
2810 if (mddev->external)
2811 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
2812 wake_up(&mddev->sb_wait);
2816 set_disk_ro(mddev->gendisk, 0);
2817 err = do_md_run(mddev);
2822 /* these cannot be set */
2828 sysfs_notify_dirent(mddev->sysfs_state);
2832 static struct md_sysfs_entry md_array_state =
2833 __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
2836 null_show(mddev_t *mddev, char *page)
2842 new_dev_store(mddev_t *mddev, const char *buf, size_t len)
2844 /* buf must be %d:%d\n? giving major and minor numbers */
2845 /* The new device is added to the array.
2846 * If the array has a persistent superblock, we read the
2847 * superblock to initialise info and check validity.
2848 * Otherwise, only checking done is that in bind_rdev_to_array,
2849 * which mainly checks size.
2852 int major = simple_strtoul(buf, &e, 10);
2858 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
2860 minor = simple_strtoul(e+1, &e, 10);
2861 if (*e && *e != '\n')
2863 dev = MKDEV(major, minor);
2864 if (major != MAJOR(dev) ||
2865 minor != MINOR(dev))
2869 if (mddev->persistent) {
2870 rdev = md_import_device(dev, mddev->major_version,
2871 mddev->minor_version);
2872 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
2873 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2874 mdk_rdev_t, same_set);
2875 err = super_types[mddev->major_version]
2876 .load_super(rdev, rdev0, mddev->minor_version);
2880 } else if (mddev->external)
2881 rdev = md_import_device(dev, -2, -1);
2883 rdev = md_import_device(dev, -1, -1);
2886 return PTR_ERR(rdev);
2887 err = bind_rdev_to_array(rdev, mddev);
2891 return err ? err : len;
2894 static struct md_sysfs_entry md_new_device =
2895 __ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
2898 bitmap_store(mddev_t *mddev, const char *buf, size_t len)
2901 unsigned long chunk, end_chunk;
2905 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
2907 chunk = end_chunk = simple_strtoul(buf, &end, 0);
2908 if (buf == end) break;
2909 if (*end == '-') { /* range */
2911 end_chunk = simple_strtoul(buf, &end, 0);
2912 if (buf == end) break;
2914 if (*end && !isspace(*end)) break;
2915 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
2917 while (isspace(*buf)) buf++;
2919 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
2924 static struct md_sysfs_entry md_bitmap =
2925 __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
2928 size_show(mddev_t *mddev, char *page)
2930 return sprintf(page, "%llu\n", (unsigned long long)mddev->size);
2933 static int update_size(mddev_t *mddev, sector_t num_sectors);
2936 size_store(mddev_t *mddev, const char *buf, size_t len)
2938 /* If array is inactive, we can reduce the component size, but
2939 * not increase it (except from 0).
2940 * If array is active, we can try an on-line resize
2944 unsigned long long size = simple_strtoull(buf, &e, 10);
2945 if (!*buf || *buf == '\n' ||
2950 err = update_size(mddev, size * 2);
2951 md_update_sb(mddev, 1);
2953 if (mddev->size == 0 ||
2959 return err ? err : len;
2962 static struct md_sysfs_entry md_size =
2963 __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
2968 * 'none' for arrays with no metadata (good luck...)
2969 * 'external' for arrays with externally managed metadata,
2970 * or N.M for internally known formats
2973 metadata_show(mddev_t *mddev, char *page)
2975 if (mddev->persistent)
2976 return sprintf(page, "%d.%d\n",
2977 mddev->major_version, mddev->minor_version);
2978 else if (mddev->external)
2979 return sprintf(page, "external:%s\n", mddev->metadata_type);
2981 return sprintf(page, "none\n");
2985 metadata_store(mddev_t *mddev, const char *buf, size_t len)
2989 /* Changing the details of 'external' metadata is
2990 * always permitted. Otherwise there must be
2991 * no devices attached to the array.
2993 if (mddev->external && strncmp(buf, "external:", 9) == 0)
2995 else if (!list_empty(&mddev->disks))
2998 if (cmd_match(buf, "none")) {
2999 mddev->persistent = 0;
3000 mddev->external = 0;
3001 mddev->major_version = 0;
3002 mddev->minor_version = 90;
3005 if (strncmp(buf, "external:", 9) == 0) {
3006 size_t namelen = len-9;
3007 if (namelen >= sizeof(mddev->metadata_type))
3008 namelen = sizeof(mddev->metadata_type)-1;
3009 strncpy(mddev->metadata_type, buf+9, namelen);
3010 mddev->metadata_type[namelen] = 0;
3011 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3012 mddev->metadata_type[--namelen] = 0;
3013 mddev->persistent = 0;
3014 mddev->external = 1;
3015 mddev->major_version = 0;
3016 mddev->minor_version = 90;
3019 major = simple_strtoul(buf, &e, 10);
3020 if (e==buf || *e != '.')
3023 minor = simple_strtoul(buf, &e, 10);
3024 if (e==buf || (*e && *e != '\n') )
3026 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
3028 mddev->major_version = major;
3029 mddev->minor_version = minor;
3030 mddev->persistent = 1;
3031 mddev->external = 0;
3035 static struct md_sysfs_entry md_metadata =
3036 __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
3039 action_show(mddev_t *mddev, char *page)
3041 char *type = "idle";
3042 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3043 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
3044 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3046 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3047 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3049 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3053 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
3056 return sprintf(page, "%s\n", type);
3060 action_store(mddev_t *mddev, const char *page, size_t len)
3062 if (!mddev->pers || !mddev->pers->sync_request)
3065 if (cmd_match(page, "idle")) {
3066 if (mddev->sync_thread) {
3067 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3068 md_unregister_thread(mddev->sync_thread);
3069 mddev->sync_thread = NULL;
3070 mddev->recovery = 0;
3072 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3073 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
3075 else if (cmd_match(page, "resync"))
3076 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3077 else if (cmd_match(page, "recover")) {
3078 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3079 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3080 } else if (cmd_match(page, "reshape")) {
3082 if (mddev->pers->start_reshape == NULL)
3084 err = mddev->pers->start_reshape(mddev);
3087 sysfs_notify(&mddev->kobj, NULL, "degraded");
3089 if (cmd_match(page, "check"))
3090 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3091 else if (!cmd_match(page, "repair"))
3093 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3094 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3096 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3097 md_wakeup_thread(mddev->thread);
3098 sysfs_notify_dirent(mddev->sysfs_action);
3103 mismatch_cnt_show(mddev_t *mddev, char *page)
3105 return sprintf(page, "%llu\n",
3106 (unsigned long long) mddev->resync_mismatches);
3109 static struct md_sysfs_entry md_scan_mode =
3110 __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
3113 static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
3116 sync_min_show(mddev_t *mddev, char *page)
3118 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3119 mddev->sync_speed_min ? "local": "system");
3123 sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3127 if (strncmp(buf, "system", 6)==0) {
3128 mddev->sync_speed_min = 0;
3131 min = simple_strtoul(buf, &e, 10);
3132 if (buf == e || (*e && *e != '\n') || min <= 0)
3134 mddev->sync_speed_min = min;
3138 static struct md_sysfs_entry md_sync_min =
3139 __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3142 sync_max_show(mddev_t *mddev, char *page)
3144 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3145 mddev->sync_speed_max ? "local": "system");
3149 sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3153 if (strncmp(buf, "system", 6)==0) {
3154 mddev->sync_speed_max = 0;
3157 max = simple_strtoul(buf, &e, 10);
3158 if (buf == e || (*e && *e != '\n') || max <= 0)
3160 mddev->sync_speed_max = max;
3164 static struct md_sysfs_entry md_sync_max =
3165 __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3168 degraded_show(mddev_t *mddev, char *page)
3170 return sprintf(page, "%d\n", mddev->degraded);
3172 static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
3175 sync_force_parallel_show(mddev_t *mddev, char *page)
3177 return sprintf(page, "%d\n", mddev->parallel_resync);
3181 sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3185 if (strict_strtol(buf, 10, &n))
3188 if (n != 0 && n != 1)
3191 mddev->parallel_resync = n;
3193 if (mddev->sync_thread)
3194 wake_up(&resync_wait);
3199 /* force parallel resync, even with shared block devices */
3200 static struct md_sysfs_entry md_sync_force_parallel =
3201 __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3202 sync_force_parallel_show, sync_force_parallel_store);
3205 sync_speed_show(mddev_t *mddev, char *page)
3207 unsigned long resync, dt, db;
3208 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3209 dt = (jiffies - mddev->resync_mark) / HZ;
3211 db = resync - mddev->resync_mark_cnt;
3212 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
3215 static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
3218 sync_completed_show(mddev_t *mddev, char *page)
3220 unsigned long max_blocks, resync;
3222 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3223 max_blocks = mddev->resync_max_sectors;
3225 max_blocks = mddev->size << 1;
3227 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
3228 return sprintf(page, "%lu / %lu\n", resync, max_blocks);
3231 static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
3234 min_sync_show(mddev_t *mddev, char *page)
3236 return sprintf(page, "%llu\n",
3237 (unsigned long long)mddev->resync_min);
3240 min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3242 unsigned long long min;
3243 if (strict_strtoull(buf, 10, &min))
3245 if (min > mddev->resync_max)
3247 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3250 /* Must be a multiple of chunk_size */
3251 if (mddev->chunk_size) {
3252 if (min & (sector_t)((mddev->chunk_size>>9)-1))
3255 mddev->resync_min = min;
3260 static struct md_sysfs_entry md_min_sync =
3261 __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3264 max_sync_show(mddev_t *mddev, char *page)
3266 if (mddev->resync_max == MaxSector)
3267 return sprintf(page, "max\n");
3269 return sprintf(page, "%llu\n",
3270 (unsigned long long)mddev->resync_max);
3273 max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3275 if (strncmp(buf, "max", 3) == 0)
3276 mddev->resync_max = MaxSector;
3278 unsigned long long max;
3279 if (strict_strtoull(buf, 10, &max))
3281 if (max < mddev->resync_min)
3283 if (max < mddev->resync_max &&
3284 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3287 /* Must be a multiple of chunk_size */
3288 if (mddev->chunk_size) {
3289 if (max & (sector_t)((mddev->chunk_size>>9)-1))
3292 mddev->resync_max = max;
3294 wake_up(&mddev->recovery_wait);
3298 static struct md_sysfs_entry md_max_sync =
3299 __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3302 suspend_lo_show(mddev_t *mddev, char *page)
3304 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3308 suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3311 unsigned long long new = simple_strtoull(buf, &e, 10);
3313 if (mddev->pers->quiesce == NULL)
3315 if (buf == e || (*e && *e != '\n'))
3317 if (new >= mddev->suspend_hi ||
3318 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3319 mddev->suspend_lo = new;
3320 mddev->pers->quiesce(mddev, 2);
3325 static struct md_sysfs_entry md_suspend_lo =
3326 __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3330 suspend_hi_show(mddev_t *mddev, char *page)
3332 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3336 suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3339 unsigned long long new = simple_strtoull(buf, &e, 10);
3341 if (mddev->pers->quiesce == NULL)
3343 if (buf == e || (*e && *e != '\n'))
3345 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3346 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3347 mddev->suspend_hi = new;
3348 mddev->pers->quiesce(mddev, 1);
3349 mddev->pers->quiesce(mddev, 0);
3354 static struct md_sysfs_entry md_suspend_hi =
3355 __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3358 reshape_position_show(mddev_t *mddev, char *page)
3360 if (mddev->reshape_position != MaxSector)
3361 return sprintf(page, "%llu\n",
3362 (unsigned long long)mddev->reshape_position);
3363 strcpy(page, "none\n");
3368 reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3371 unsigned long long new = simple_strtoull(buf, &e, 10);
3374 if (buf == e || (*e && *e != '\n'))
3376 mddev->reshape_position = new;
3377 mddev->delta_disks = 0;
3378 mddev->new_level = mddev->level;
3379 mddev->new_layout = mddev->layout;
3380 mddev->new_chunk = mddev->chunk_size;
3384 static struct md_sysfs_entry md_reshape_position =
3385 __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3386 reshape_position_store);
3389 static struct attribute *md_default_attrs[] = {
3392 &md_raid_disks.attr,
3393 &md_chunk_size.attr,
3395 &md_resync_start.attr,
3397 &md_new_device.attr,
3398 &md_safe_delay.attr,
3399 &md_array_state.attr,
3400 &md_reshape_position.attr,
3404 static struct attribute *md_redundancy_attrs[] = {
3406 &md_mismatches.attr,
3409 &md_sync_speed.attr,
3410 &md_sync_force_parallel.attr,
3411 &md_sync_completed.attr,
3414 &md_suspend_lo.attr,
3415 &md_suspend_hi.attr,
3420 static struct attribute_group md_redundancy_group = {
3422 .attrs = md_redundancy_attrs,
3427 md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3429 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3430 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
3435 rv = mddev_lock(mddev);
3437 rv = entry->show(mddev, page);
3438 mddev_unlock(mddev);
3444 md_attr_store(struct kobject *kobj, struct attribute *attr,
3445 const char *page, size_t length)
3447 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3448 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
3453 if (!capable(CAP_SYS_ADMIN))
3455 rv = mddev_lock(mddev);
3456 if (mddev->hold_active == UNTIL_IOCTL)
3457 mddev->hold_active = 0;
3459 rv = entry->store(mddev, page, length);
3460 mddev_unlock(mddev);
3465 static void md_free(struct kobject *ko)
3467 mddev_t *mddev = container_of(ko, mddev_t, kobj);
3469 if (mddev->sysfs_state)
3470 sysfs_put(mddev->sysfs_state);
3472 if (mddev->gendisk) {
3473 del_gendisk(mddev->gendisk);
3474 put_disk(mddev->gendisk);
3477 blk_cleanup_queue(mddev->queue);
3482 static struct sysfs_ops md_sysfs_ops = {
3483 .show = md_attr_show,
3484 .store = md_attr_store,
3486 static struct kobj_type md_ktype = {
3488 .sysfs_ops = &md_sysfs_ops,
3489 .default_attrs = md_default_attrs,
3494 static struct kobject *md_probe(dev_t dev, int *part, void *data)
3496 static DEFINE_MUTEX(disks_mutex);
3497 mddev_t *mddev = mddev_find(dev);
3498 struct gendisk *disk;
3499 int partitioned = (MAJOR(dev) != MD_MAJOR);
3500 int shift = partitioned ? MdpMinorShift : 0;
3501 int unit = MINOR(dev) >> shift;
3507 /* wait for any previous instance if this device
3508 * to be completed removed (mddev_delayed_delete).
3510 flush_scheduled_work();
3512 mutex_lock(&disks_mutex);
3513 if (mddev->gendisk) {
3514 mutex_unlock(&disks_mutex);
3519 mddev->queue = blk_alloc_queue(GFP_KERNEL);
3520 if (!mddev->queue) {
3521 mutex_unlock(&disks_mutex);
3525 /* Can be unlocked because the queue is new: no concurrency */
3526 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue);
3528 blk_queue_make_request(mddev->queue, md_fail_request);
3530 disk = alloc_disk(1 << shift);
3532 mutex_unlock(&disks_mutex);
3533 blk_cleanup_queue(mddev->queue);
3534 mddev->queue = NULL;
3538 disk->major = MAJOR(dev);
3539 disk->first_minor = unit << shift;
3541 sprintf(disk->disk_name, "md_d%d", unit);
3543 sprintf(disk->disk_name, "md%d", unit);
3544 disk->fops = &md_fops;
3545 disk->private_data = mddev;
3546 disk->queue = mddev->queue;
3547 /* Allow extended partitions. This makes the
3548 * 'mdp' device redundant, but we can't really
3551 disk->flags |= GENHD_FL_EXT_DEVT;
3553 mddev->gendisk = disk;
3554 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
3555 &disk_to_dev(disk)->kobj, "%s", "md");
3556 mutex_unlock(&disks_mutex);
3558 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
3561 kobject_uevent(&mddev->kobj, KOBJ_ADD);
3562 mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state");
3568 static void md_safemode_timeout(unsigned long data)
3570 mddev_t *mddev = (mddev_t *) data;
3572 if (!atomic_read(&mddev->writes_pending)) {
3573 mddev->safemode = 1;
3574 if (mddev->external)
3575 sysfs_notify_dirent(mddev->sysfs_state);
3577 md_wakeup_thread(mddev->thread);
3580 static int start_dirty_degraded;
3582 static int do_md_run(mddev_t * mddev)
3587 struct gendisk *disk;
3588 struct mdk_personality *pers;
3589 char b[BDEVNAME_SIZE];
3591 if (list_empty(&mddev->disks))
3592 /* cannot run an array with no devices.. */
3599 * Analyze all RAID superblock(s)
3601 if (!mddev->raid_disks) {
3602 if (!mddev->persistent)
3607 chunk_size = mddev->chunk_size;
3610 if (chunk_size > MAX_CHUNK_SIZE) {
3611 printk(KERN_ERR "too big chunk_size: %d > %d\n",
3612 chunk_size, MAX_CHUNK_SIZE);
3616 * chunk-size has to be a power of 2
3618 if ( (1 << ffz(~chunk_size)) != chunk_size) {
3619 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
3623 /* devices must have minimum size of one chunk */
3624 list_for_each_entry(rdev, &mddev->disks, same_set) {
3625 if (test_bit(Faulty, &rdev->flags))
3627 if (rdev->size < chunk_size / 1024) {
3629 "md: Dev %s smaller than chunk_size:"
3631 bdevname(rdev->bdev,b),
3632 (unsigned long long)rdev->size,
3639 if (mddev->level != LEVEL_NONE)
3640 request_module("md-level-%d", mddev->level);
3641 else if (mddev->clevel[0])
3642 request_module("md-%s", mddev->clevel);
3645 * Drop all container device buffers, from now on
3646 * the only valid external interface is through the md
3649 list_for_each_entry(rdev, &mddev->disks, same_set) {
3650 if (test_bit(Faulty, &rdev->flags))
3652 sync_blockdev(rdev->bdev);
3653 invalidate_bdev(rdev->bdev);
3655 /* perform some consistency tests on the device.
3656 * We don't want the data to overlap the metadata,
3657 * Internal Bitmap issues has handled elsewhere.
3659 if (rdev->data_offset < rdev->sb_start) {
3661 rdev->data_offset + mddev->size*2
3663 printk("md: %s: data overlaps metadata\n",
3668 if (rdev->sb_start + rdev->sb_size/512
3669 > rdev->data_offset) {
3670 printk("md: %s: metadata overlaps data\n",
3675 sysfs_notify_dirent(rdev->sysfs_state);
3678 md_probe(mddev->unit, NULL, NULL);
3679 disk = mddev->gendisk;
3683 spin_lock(&pers_lock);
3684 pers = find_pers(mddev->level, mddev->clevel);
3685 if (!pers || !try_module_get(pers->owner)) {
3686 spin_unlock(&pers_lock);
3687 if (mddev->level != LEVEL_NONE)
3688 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
3691 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
3696 spin_unlock(&pers_lock);
3697 mddev->level = pers->level;
3698 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3700 if (mddev->reshape_position != MaxSector &&
3701 pers->start_reshape == NULL) {
3702 /* This personality cannot handle reshaping... */
3704 module_put(pers->owner);
3708 if (pers->sync_request) {
3709 /* Warn if this is a potentially silly
3712 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3716 list_for_each_entry(rdev, &mddev->disks, same_set)
3717 list_for_each_entry(rdev2, &mddev->disks, same_set) {
3719 rdev->bdev->bd_contains ==
3720 rdev2->bdev->bd_contains) {
3722 "%s: WARNING: %s appears to be"
3723 " on the same physical disk as"
3726 bdevname(rdev->bdev,b),
3727 bdevname(rdev2->bdev,b2));
3734 "True protection against single-disk"
3735 " failure might be compromised.\n");
3738 mddev->recovery = 0;
3739 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
3740 mddev->barriers_work = 1;
3741 mddev->ok_start_degraded = start_dirty_degraded;
3744 mddev->ro = 2; /* read-only, but switch on first write */
3746 err = mddev->pers->run(mddev);
3748 printk(KERN_ERR "md: pers->run() failed ...\n");
3749 else if (mddev->pers->sync_request) {
3750 err = bitmap_create(mddev);
3752 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
3753 mdname(mddev), err);
3754 mddev->pers->stop(mddev);
3758 module_put(mddev->pers->owner);
3760 bitmap_destroy(mddev);
3763 if (mddev->pers->sync_request) {
3764 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3766 "md: cannot register extra attributes for %s\n",
3768 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3769 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
3772 atomic_set(&mddev->writes_pending,0);
3773 mddev->safemode = 0;
3774 mddev->safemode_timer.function = md_safemode_timeout;
3775 mddev->safemode_timer.data = (unsigned long) mddev;
3776 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
3779 list_for_each_entry(rdev, &mddev->disks, same_set)
3780 if (rdev->raid_disk >= 0) {
3782 sprintf(nm, "rd%d", rdev->raid_disk);
3783 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
3784 printk("md: cannot register %s for %s\n",
3788 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3791 md_update_sb(mddev, 0);
3793 set_capacity(disk, mddev->array_sectors);
3795 /* If we call blk_queue_make_request here, it will
3796 * re-initialise max_sectors etc which may have been
3797 * refined inside -> run. So just set the bits we need to set.
3798 * Most initialisation happended when we called
3799 * blk_queue_make_request(..., md_fail_request)
3802 mddev->queue->queuedata = mddev;
3803 mddev->queue->make_request_fn = mddev->pers->make_request;
3805 /* If there is a partially-recovered drive we need to
3806 * start recovery here. If we leave it to md_check_recovery,
3807 * it will remove the drives and not do the right thing
3809 if (mddev->degraded && !mddev->sync_thread) {
3811 list_for_each_entry(rdev, &mddev->disks, same_set)
3812 if (rdev->raid_disk >= 0 &&
3813 !test_bit(In_sync, &rdev->flags) &&
3814 !test_bit(Faulty, &rdev->flags))
3815 /* complete an interrupted recovery */
3817 if (spares && mddev->pers->sync_request) {
3818 mddev->recovery = 0;
3819 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3820 mddev->sync_thread = md_register_thread(md_do_sync,
3823 if (!mddev->sync_thread) {
3824 printk(KERN_ERR "%s: could not start resync"
3827 /* leave the spares where they are, it shouldn't hurt */
3828 mddev->recovery = 0;
3832 md_wakeup_thread(mddev->thread);
3833 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
3836 md_new_event(mddev);
3837 sysfs_notify_dirent(mddev->sysfs_state);
3838 if (mddev->sysfs_action)
3839 sysfs_notify_dirent(mddev->sysfs_action);
3840 sysfs_notify(&mddev->kobj, NULL, "degraded");
3841 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
3845 static int restart_array(mddev_t *mddev)
3847 struct gendisk *disk = mddev->gendisk;
3849 /* Complain if it has no devices */
3850 if (list_empty(&mddev->disks))
3856 mddev->safemode = 0;
3858 set_disk_ro(disk, 0);
3859 printk(KERN_INFO "md: %s switched to read-write mode.\n",
3861 /* Kick recovery or resync if necessary */
3862 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3863 md_wakeup_thread(mddev->thread);
3864 md_wakeup_thread(mddev->sync_thread);
3865 sysfs_notify_dirent(mddev->sysfs_state);
3869 /* similar to deny_write_access, but accounts for our holding a reference
3870 * to the file ourselves */
3871 static int deny_bitmap_write_access(struct file * file)
3873 struct inode *inode = file->f_mapping->host;
3875 spin_lock(&inode->i_lock);
3876 if (atomic_read(&inode->i_writecount) > 1) {
3877 spin_unlock(&inode->i_lock);
3880 atomic_set(&inode->i_writecount, -1);
3881 spin_unlock(&inode->i_lock);
3886 static void restore_bitmap_write_access(struct file *file)
3888 struct inode *inode = file->f_mapping->host;
3890 spin_lock(&inode->i_lock);
3891 atomic_set(&inode->i_writecount, 1);
3892 spin_unlock(&inode->i_lock);
3896 * 0 - completely stop and dis-assemble array
3897 * 1 - switch to readonly
3898 * 2 - stop but do not disassemble array
3900 static int do_md_stop(mddev_t * mddev, int mode, int is_open)
3903 struct gendisk *disk = mddev->gendisk;
3905 if (atomic_read(&mddev->openers) > is_open) {
3906 printk("md: %s still in use.\n",mdname(mddev));
3912 if (mddev->sync_thread) {
3913 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3914 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3915 md_unregister_thread(mddev->sync_thread);
3916 mddev->sync_thread = NULL;
3919 del_timer_sync(&mddev->safemode_timer);
3922 case 1: /* readonly */
3928 case 0: /* disassemble */
3930 bitmap_flush(mddev);
3931 md_super_wait(mddev);
3933 set_disk_ro(disk, 0);
3934 blk_queue_make_request(mddev->queue, md_fail_request);
3935 mddev->pers->stop(mddev);
3936 mddev->queue->merge_bvec_fn = NULL;
3937 mddev->queue->unplug_fn = NULL;
3938 mddev->queue->backing_dev_info.congested_fn = NULL;
3939 if (mddev->pers->sync_request) {
3940 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
3941 if (mddev->sysfs_action)
3942 sysfs_put(mddev->sysfs_action);
3943 mddev->sysfs_action = NULL;
3945 module_put(mddev->pers->owner);
3947 /* tell userspace to handle 'inactive' */
3948 sysfs_notify_dirent(mddev->sysfs_state);
3950 set_capacity(disk, 0);
3956 if (!mddev->in_sync || mddev->flags) {
3957 /* mark array as shutdown cleanly */
3959 md_update_sb(mddev, 1);
3962 set_disk_ro(disk, 1);
3963 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3967 * Free resources if final stop
3972 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
3974 bitmap_destroy(mddev);
3975 if (mddev->bitmap_file) {
3976 restore_bitmap_write_access(mddev->bitmap_file);
3977 fput(mddev->bitmap_file);
3978 mddev->bitmap_file = NULL;
3980 mddev->bitmap_offset = 0;
3982 list_for_each_entry(rdev, &mddev->disks, same_set)
3983 if (rdev->raid_disk >= 0) {
3985 sprintf(nm, "rd%d", rdev->raid_disk);
3986 sysfs_remove_link(&mddev->kobj, nm);
3989 /* make sure all md_delayed_delete calls have finished */
3990 flush_scheduled_work();
3992 export_array(mddev);
3994 mddev->array_sectors = 0;
3996 mddev->raid_disks = 0;
3997 mddev->recovery_cp = 0;
3998 mddev->resync_min = 0;
3999 mddev->resync_max = MaxSector;
4000 mddev->reshape_position = MaxSector;
4001 mddev->external = 0;
4002 mddev->persistent = 0;
4003 mddev->level = LEVEL_NONE;
4004 mddev->clevel[0] = 0;
4007 mddev->metadata_type[0] = 0;
4008 mddev->chunk_size = 0;
4009 mddev->ctime = mddev->utime = 0;
4011 mddev->max_disks = 0;
4013 mddev->delta_disks = 0;
4014 mddev->new_level = LEVEL_NONE;
4015 mddev->new_layout = 0;
4016 mddev->new_chunk = 0;
4017 mddev->curr_resync = 0;
4018 mddev->resync_mismatches = 0;
4019 mddev->suspend_lo = mddev->suspend_hi = 0;
4020 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4021 mddev->recovery = 0;
4024 mddev->degraded = 0;
4025 mddev->barriers_work = 0;
4026 mddev->safemode = 0;
4027 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4029 } else if (mddev->pers)
4030 printk(KERN_INFO "md: %s switched to read-only mode.\n",
4033 md_new_event(mddev);
4034 sysfs_notify_dirent(mddev->sysfs_state);
4040 static void autorun_array(mddev_t *mddev)
4045 if (list_empty(&mddev->disks))
4048 printk(KERN_INFO "md: running: ");
4050 list_for_each_entry(rdev, &mddev->disks, same_set) {
4051 char b[BDEVNAME_SIZE];
4052 printk("<%s>", bdevname(rdev->bdev,b));
4056 err = do_md_run(mddev);
4058 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
4059 do_md_stop(mddev, 0, 0);
4064 * lets try to run arrays based on all disks that have arrived
4065 * until now. (those are in pending_raid_disks)
4067 * the method: pick the first pending disk, collect all disks with
4068 * the same UUID, remove all from the pending list and put them into
4069 * the 'same_array' list. Then order this list based on superblock
4070 * update time (freshest comes first), kick out 'old' disks and
4071 * compare superblocks. If everything's fine then run it.
4073 * If "unit" is allocated, then bump its reference count
4075 static void autorun_devices(int part)
4077 mdk_rdev_t *rdev0, *rdev, *tmp;
4079 char b[BDEVNAME_SIZE];
4081 printk(KERN_INFO "md: autorun ...\n");
4082 while (!list_empty(&pending_raid_disks)) {
4085 LIST_HEAD(candidates);
4086 rdev0 = list_entry(pending_raid_disks.next,
4087 mdk_rdev_t, same_set);
4089 printk(KERN_INFO "md: considering %s ...\n",
4090 bdevname(rdev0->bdev,b));
4091 INIT_LIST_HEAD(&candidates);
4092 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
4093 if (super_90_load(rdev, rdev0, 0) >= 0) {
4094 printk(KERN_INFO "md: adding %s ...\n",
4095 bdevname(rdev->bdev,b));
4096 list_move(&rdev->same_set, &candidates);
4099 * now we have a set of devices, with all of them having
4100 * mostly sane superblocks. It's time to allocate the
4104 dev = MKDEV(mdp_major,
4105 rdev0->preferred_minor << MdpMinorShift);
4106 unit = MINOR(dev) >> MdpMinorShift;
4108 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4111 if (rdev0->preferred_minor != unit) {
4112 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4113 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4117 md_probe(dev, NULL, NULL);
4118 mddev = mddev_find(dev);
4119 if (!mddev || !mddev->gendisk) {
4123 "md: cannot allocate memory for md drive.\n");
4126 if (mddev_lock(mddev))
4127 printk(KERN_WARNING "md: %s locked, cannot run\n",
4129 else if (mddev->raid_disks || mddev->major_version
4130 || !list_empty(&mddev->disks)) {
4132 "md: %s already running, cannot run %s\n",
4133 mdname(mddev), bdevname(rdev0->bdev,b));
4134 mddev_unlock(mddev);
4136 printk(KERN_INFO "md: created %s\n", mdname(mddev));
4137 mddev->persistent = 1;
4138 rdev_for_each_list(rdev, tmp, &candidates) {
4139 list_del_init(&rdev->same_set);
4140 if (bind_rdev_to_array(rdev, mddev))
4143 autorun_array(mddev);
4144 mddev_unlock(mddev);
4146 /* on success, candidates will be empty, on error
4149 rdev_for_each_list(rdev, tmp, &candidates) {
4150 list_del_init(&rdev->same_set);
4155 printk(KERN_INFO "md: ... autorun DONE.\n");
4157 #endif /* !MODULE */
4159 static int get_version(void __user * arg)
4163 ver.major = MD_MAJOR_VERSION;
4164 ver.minor = MD_MINOR_VERSION;
4165 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4167 if (copy_to_user(arg, &ver, sizeof(ver)))
4173 static int get_array_info(mddev_t * mddev, void __user * arg)
4175 mdu_array_info_t info;
4176 int nr,working,active,failed,spare;
4179 nr=working=active=failed=spare=0;
4180 list_for_each_entry(rdev, &mddev->disks, same_set) {
4182 if (test_bit(Faulty, &rdev->flags))
4186 if (test_bit(In_sync, &rdev->flags))
4193 info.major_version = mddev->major_version;
4194 info.minor_version = mddev->minor_version;
4195 info.patch_version = MD_PATCHLEVEL_VERSION;
4196 info.ctime = mddev->ctime;
4197 info.level = mddev->level;
4198 info.size = mddev->size;
4199 if (info.size != mddev->size) /* overflow */
4202 info.raid_disks = mddev->raid_disks;
4203 info.md_minor = mddev->md_minor;
4204 info.not_persistent= !mddev->persistent;
4206 info.utime = mddev->utime;
4209 info.state = (1<<MD_SB_CLEAN);
4210 if (mddev->bitmap && mddev->bitmap_offset)
4211 info.state = (1<<MD_SB_BITMAP_PRESENT);
4212 info.active_disks = active;
4213 info.working_disks = working;
4214 info.failed_disks = failed;
4215 info.spare_disks = spare;
4217 info.layout = mddev->layout;
4218 info.chunk_size = mddev->chunk_size;
4220 if (copy_to_user(arg, &info, sizeof(info)))
4226 static int get_bitmap_file(mddev_t * mddev, void __user * arg)
4228 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4229 char *ptr, *buf = NULL;
4232 if (md_allow_write(mddev))
4233 file = kmalloc(sizeof(*file), GFP_NOIO);
4235 file = kmalloc(sizeof(*file), GFP_KERNEL);
4240 /* bitmap disabled, zero the first byte and copy out */
4241 if (!mddev->bitmap || !mddev->bitmap->file) {
4242 file->pathname[0] = '\0';
4246 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4250 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4254 strcpy(file->pathname, ptr);
4258 if (copy_to_user(arg, file, sizeof(*file)))
4266 static int get_disk_info(mddev_t * mddev, void __user * arg)
4268 mdu_disk_info_t info;
4271 if (copy_from_user(&info, arg, sizeof(info)))
4274 rdev = find_rdev_nr(mddev, info.number);
4276 info.major = MAJOR(rdev->bdev->bd_dev);
4277 info.minor = MINOR(rdev->bdev->bd_dev);
4278 info.raid_disk = rdev->raid_disk;
4280 if (test_bit(Faulty, &rdev->flags))
4281 info.state |= (1<<MD_DISK_FAULTY);
4282 else if (test_bit(In_sync, &rdev->flags)) {
4283 info.state |= (1<<MD_DISK_ACTIVE);
4284 info.state |= (1<<MD_DISK_SYNC);
4286 if (test_bit(WriteMostly, &rdev->flags))
4287 info.state |= (1<<MD_DISK_WRITEMOSTLY);
4289 info.major = info.minor = 0;
4290 info.raid_disk = -1;
4291 info.state = (1<<MD_DISK_REMOVED);
4294 if (copy_to_user(arg, &info, sizeof(info)))
4300 static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4302 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4304 dev_t dev = MKDEV(info->major,info->minor);
4306 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4309 if (!mddev->raid_disks) {
4311 /* expecting a device which has a superblock */
4312 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4315 "md: md_import_device returned %ld\n",
4317 return PTR_ERR(rdev);
4319 if (!list_empty(&mddev->disks)) {
4320 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4321 mdk_rdev_t, same_set);
4322 int err = super_types[mddev->major_version]
4323 .load_super(rdev, rdev0, mddev->minor_version);
4326 "md: %s has different UUID to %s\n",
4327 bdevname(rdev->bdev,b),
4328 bdevname(rdev0->bdev,b2));
4333 err = bind_rdev_to_array(rdev, mddev);
4340 * add_new_disk can be used once the array is assembled
4341 * to add "hot spares". They must already have a superblock
4346 if (!mddev->pers->hot_add_disk) {
4348 "%s: personality does not support diskops!\n",
4352 if (mddev->persistent)
4353 rdev = md_import_device(dev, mddev->major_version,
4354 mddev->minor_version);
4356 rdev = md_import_device(dev, -1, -1);
4359 "md: md_import_device returned %ld\n",
4361 return PTR_ERR(rdev);
4363 /* set save_raid_disk if appropriate */
4364 if (!mddev->persistent) {
4365 if (info->state & (1<<MD_DISK_SYNC) &&
4366 info->raid_disk < mddev->raid_disks)
4367 rdev->raid_disk = info->raid_disk;
4369 rdev->raid_disk = -1;
4371 super_types[mddev->major_version].
4372 validate_super(mddev, rdev);
4373 rdev->saved_raid_disk = rdev->raid_disk;
4375 clear_bit(In_sync, &rdev->flags); /* just to be sure */
4376 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4377 set_bit(WriteMostly, &rdev->flags);
4379 rdev->raid_disk = -1;
4380 err = bind_rdev_to_array(rdev, mddev);
4381 if (!err && !mddev->pers->hot_remove_disk) {
4382 /* If there is hot_add_disk but no hot_remove_disk
4383 * then added disks for geometry changes,
4384 * and should be added immediately.
4386 super_types[mddev->major_version].
4387 validate_super(mddev, rdev);
4388 err = mddev->pers->hot_add_disk(mddev, rdev);
4390 unbind_rdev_from_array(rdev);
4395 sysfs_notify_dirent(rdev->sysfs_state);
4397 md_update_sb(mddev, 1);
4398 if (mddev->degraded)
4399 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4400 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4401 md_wakeup_thread(mddev->thread);
4405 /* otherwise, add_new_disk is only allowed
4406 * for major_version==0 superblocks
4408 if (mddev->major_version != 0) {
4409 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
4414 if (!(info->state & (1<<MD_DISK_FAULTY))) {
4416 rdev = md_import_device(dev, -1, 0);
4419 "md: error, md_import_device() returned %ld\n",
4421 return PTR_ERR(rdev);
4423 rdev->desc_nr = info->number;
4424 if (info->raid_disk < mddev->raid_disks)
4425 rdev->raid_disk = info->raid_disk;
4427 rdev->raid_disk = -1;
4429 if (rdev->raid_disk < mddev->raid_disks)
4430 if (info->state & (1<<MD_DISK_SYNC))
4431 set_bit(In_sync, &rdev->flags);
4433 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4434 set_bit(WriteMostly, &rdev->flags);
4436 if (!mddev->persistent) {
4437 printk(KERN_INFO "md: nonpersistent superblock ...\n");
4438 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
4440 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
4441 rdev->size = calc_num_sectors(rdev, mddev->chunk_size) / 2;
4443 err = bind_rdev_to_array(rdev, mddev);
4453 static int hot_remove_disk(mddev_t * mddev, dev_t dev)
4455 char b[BDEVNAME_SIZE];
4458 rdev = find_rdev(mddev, dev);
4462 if (rdev->raid_disk >= 0)
4465 kick_rdev_from_array(rdev);
4466 md_update_sb(mddev, 1);
4467 md_new_event(mddev);
4471 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
4472 bdevname(rdev->bdev,b), mdname(mddev));
4476 static int hot_add_disk(mddev_t * mddev, dev_t dev)
4478 char b[BDEVNAME_SIZE];
4485 if (mddev->major_version != 0) {
4486 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
4487 " version-0 superblocks.\n",
4491 if (!mddev->pers->hot_add_disk) {
4493 "%s: personality does not support diskops!\n",
4498 rdev = md_import_device(dev, -1, 0);
4501 "md: error, md_import_device() returned %ld\n",
4506 if (mddev->persistent)
4507 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
4509 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
4511 rdev->size = calc_num_sectors(rdev, mddev->chunk_size) / 2;
4513 if (test_bit(Faulty, &rdev->flags)) {
4515 "md: can not hot-add faulty %s disk to %s!\n",
4516 bdevname(rdev->bdev,b), mdname(mddev));
4520 clear_bit(In_sync, &rdev->flags);
4522 rdev->saved_raid_disk = -1;
4523 err = bind_rdev_to_array(rdev, mddev);
4528 * The rest should better be atomic, we can have disk failures
4529 * noticed in interrupt contexts ...
4532 if (rdev->desc_nr == mddev->max_disks) {
4533 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
4536 goto abort_unbind_export;
4539 rdev->raid_disk = -1;
4541 md_update_sb(mddev, 1);
4544 * Kick recovery, maybe this spare has to be added to the
4545 * array immediately.
4547 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4548 md_wakeup_thread(mddev->thread);
4549 md_new_event(mddev);
4552 abort_unbind_export:
4553 unbind_rdev_from_array(rdev);
4560 static int set_bitmap_file(mddev_t *mddev, int fd)
4565 if (!mddev->pers->quiesce)
4567 if (mddev->recovery || mddev->sync_thread)
4569 /* we should be able to change the bitmap.. */
4575 return -EEXIST; /* cannot add when bitmap is present */
4576 mddev->bitmap_file = fget(fd);
4578 if (mddev->bitmap_file == NULL) {
4579 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
4584 err = deny_bitmap_write_access(mddev->bitmap_file);
4586 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
4588 fput(mddev->bitmap_file);
4589 mddev->bitmap_file = NULL;
4592 mddev->bitmap_offset = 0; /* file overrides offset */
4593 } else if (mddev->bitmap == NULL)
4594 return -ENOENT; /* cannot remove what isn't there */
4597 mddev->pers->quiesce(mddev, 1);
4599 err = bitmap_create(mddev);
4600 if (fd < 0 || err) {
4601 bitmap_destroy(mddev);
4602 fd = -1; /* make sure to put the file */
4604 mddev->pers->quiesce(mddev, 0);
4607 if (mddev->bitmap_file) {
4608 restore_bitmap_write_access(mddev->bitmap_file);
4609 fput(mddev->bitmap_file);
4611 mddev->bitmap_file = NULL;
4618 * set_array_info is used two different ways
4619 * The original usage is when creating a new array.
4620 * In this usage, raid_disks is > 0 and it together with
4621 * level, size, not_persistent,layout,chunksize determine the
4622 * shape of the array.
4623 * This will always create an array with a type-0.90.0 superblock.
4624 * The newer usage is when assembling an array.
4625 * In this case raid_disks will be 0, and the major_version field is
4626 * use to determine which style super-blocks are to be found on the devices.
4627 * The minor and patch _version numbers are also kept incase the
4628 * super_block handler wishes to interpret them.
4630 static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
4633 if (info->raid_disks == 0) {
4634 /* just setting version number for superblock loading */
4635 if (info->major_version < 0 ||
4636 info->major_version >= ARRAY_SIZE(super_types) ||
4637 super_types[info->major_version].name == NULL) {
4638 /* maybe try to auto-load a module? */
4640 "md: superblock version %d not known\n",
4641 info->major_version);
4644 mddev->major_version = info->major_version;
4645 mddev->minor_version = info->minor_version;
4646 mddev->patch_version = info->patch_version;
4647 mddev->persistent = !info->not_persistent;
4650 mddev->major_version = MD_MAJOR_VERSION;
4651 mddev->minor_version = MD_MINOR_VERSION;
4652 mddev->patch_version = MD_PATCHLEVEL_VERSION;
4653 mddev->ctime = get_seconds();
4655 mddev->level = info->level;
4656 mddev->clevel[0] = 0;
4657 mddev->size = info->size;
4658 mddev->raid_disks = info->raid_disks;
4659 /* don't set md_minor, it is determined by which /dev/md* was
4662 if (info->state & (1<<MD_SB_CLEAN))
4663 mddev->recovery_cp = MaxSector;
4665 mddev->recovery_cp = 0;
4666 mddev->persistent = ! info->not_persistent;
4667 mddev->external = 0;
4669 mddev->layout = info->layout;
4670 mddev->chunk_size = info->chunk_size;
4672 mddev->max_disks = MD_SB_DISKS;
4674 if (mddev->persistent)
4676 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4678 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
4679 mddev->bitmap_offset = 0;
4681 mddev->reshape_position = MaxSector;
4684 * Generate a 128 bit UUID
4686 get_random_bytes(mddev->uuid, 16);
4688 mddev->new_level = mddev->level;
4689 mddev->new_chunk = mddev->chunk_size;
4690 mddev->new_layout = mddev->layout;
4691 mddev->delta_disks = 0;
4696 static int update_size(mddev_t *mddev, sector_t num_sectors)
4700 int fit = (num_sectors == 0);
4702 if (mddev->pers->resize == NULL)
4704 /* The "num_sectors" is the number of sectors of each device that
4705 * is used. This can only make sense for arrays with redundancy.
4706 * linear and raid0 always use whatever space is available. We can only
4707 * consider changing this number if no resync or reconstruction is
4708 * happening, and if the new size is acceptable. It must fit before the
4709 * sb_start or, if that is <data_offset, it must fit before the size
4710 * of each device. If num_sectors is zero, we find the largest size
4714 if (mddev->sync_thread)
4717 /* Sorry, cannot grow a bitmap yet, just remove it,
4721 list_for_each_entry(rdev, &mddev->disks, same_set) {
4723 avail = rdev->size * 2;
4725 if (fit && (num_sectors == 0 || num_sectors > avail))
4726 num_sectors = avail;
4727 if (avail < num_sectors)
4730 rv = mddev->pers->resize(mddev, num_sectors);
4732 struct block_device *bdev;
4734 bdev = bdget_disk(mddev->gendisk, 0);
4736 mutex_lock(&bdev->bd_inode->i_mutex);
4737 i_size_write(bdev->bd_inode,
4738 (loff_t)mddev->array_sectors << 9);
4739 mutex_unlock(&bdev->bd_inode->i_mutex);
4746 static int update_raid_disks(mddev_t *mddev, int raid_disks)
4749 /* change the number of raid disks */
4750 if (mddev->pers->check_reshape == NULL)
4752 if (raid_disks <= 0 ||
4753 raid_disks >= mddev->max_disks)
4755 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
4757 mddev->delta_disks = raid_disks - mddev->raid_disks;
4759 rv = mddev->pers->check_reshape(mddev);
4765 * update_array_info is used to change the configuration of an
4767 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
4768 * fields in the info are checked against the array.
4769 * Any differences that cannot be handled will cause an error.
4770 * Normally, only one change can be managed at a time.
4772 static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
4778 /* calculate expected state,ignoring low bits */
4779 if (mddev->bitmap && mddev->bitmap_offset)
4780 state |= (1 << MD_SB_BITMAP_PRESENT);
4782 if (mddev->major_version != info->major_version ||
4783 mddev->minor_version != info->minor_version ||
4784 /* mddev->patch_version != info->patch_version || */
4785 mddev->ctime != info->ctime ||
4786 mddev->level != info->level ||
4787 /* mddev->layout != info->layout || */
4788 !mddev->persistent != info->not_persistent||
4789 mddev->chunk_size != info->chunk_size ||
4790 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
4791 ((state^info->state) & 0xfffffe00)
4794 /* Check there is only one change */
4795 if (info->size >= 0 && mddev->size != info->size) cnt++;
4796 if (mddev->raid_disks != info->raid_disks) cnt++;
4797 if (mddev->layout != info->layout) cnt++;
4798 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
4799 if (cnt == 0) return 0;
4800 if (cnt > 1) return -EINVAL;
4802 if (mddev->layout != info->layout) {
4804 * we don't need to do anything at the md level, the
4805 * personality will take care of it all.
4807 if (mddev->pers->reconfig == NULL)
4810 return mddev->pers->reconfig(mddev, info->layout, -1);
4812 if (info->size >= 0 && mddev->size != info->size)
4813 rv = update_size(mddev, (sector_t)info->size * 2);
4815 if (mddev->raid_disks != info->raid_disks)
4816 rv = update_raid_disks(mddev, info->raid_disks);
4818 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
4819 if (mddev->pers->quiesce == NULL)
4821 if (mddev->recovery || mddev->sync_thread)
4823 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
4824 /* add the bitmap */
4827 if (mddev->default_bitmap_offset == 0)
4829 mddev->bitmap_offset = mddev->default_bitmap_offset;
4830 mddev->pers->quiesce(mddev, 1);
4831 rv = bitmap_create(mddev);
4833 bitmap_destroy(mddev);
4834 mddev->pers->quiesce(mddev, 0);
4836 /* remove the bitmap */
4839 if (mddev->bitmap->file)
4841 mddev->pers->quiesce(mddev, 1);
4842 bitmap_destroy(mddev);
4843 mddev->pers->quiesce(mddev, 0);
4844 mddev->bitmap_offset = 0;
4847 md_update_sb(mddev, 1);
4851 static int set_disk_faulty(mddev_t *mddev, dev_t dev)
4855 if (mddev->pers == NULL)
4858 rdev = find_rdev(mddev, dev);
4862 md_error(mddev, rdev);
4867 * We have a problem here : there is no easy way to give a CHS
4868 * virtual geometry. We currently pretend that we have a 2 heads
4869 * 4 sectors (with a BIG number of cylinders...). This drives
4870 * dosfs just mad... ;-)
4872 static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4874 mddev_t *mddev = bdev->bd_disk->private_data;
4878 geo->cylinders = get_capacity(mddev->gendisk) / 8;
4882 static int md_ioctl(struct block_device *bdev, fmode_t mode,
4883 unsigned int cmd, unsigned long arg)
4886 void __user *argp = (void __user *)arg;
4887 mddev_t *mddev = NULL;
4889 if (!capable(CAP_SYS_ADMIN))
4893 * Commands dealing with the RAID driver but not any
4899 err = get_version(argp);
4902 case PRINT_RAID_DEBUG:
4910 autostart_arrays(arg);
4917 * Commands creating/starting a new array:
4920 mddev = bdev->bd_disk->private_data;
4927 err = mddev_lock(mddev);
4930 "md: ioctl lock interrupted, reason %d, cmd %d\n",
4937 case SET_ARRAY_INFO:
4939 mdu_array_info_t info;
4941 memset(&info, 0, sizeof(info));
4942 else if (copy_from_user(&info, argp, sizeof(info))) {
4947 err = update_array_info(mddev, &info);
4949 printk(KERN_WARNING "md: couldn't update"
4950 " array info. %d\n", err);
4955 if (!list_empty(&mddev->disks)) {
4957 "md: array %s already has disks!\n",
4962 if (mddev->raid_disks) {
4964 "md: array %s already initialised!\n",
4969 err = set_array_info(mddev, &info);
4971 printk(KERN_WARNING "md: couldn't set"
4972 " array info. %d\n", err);
4982 * Commands querying/configuring an existing array:
4984 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
4985 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
4986 if ((!mddev->raid_disks && !mddev->external)
4987 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
4988 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
4989 && cmd != GET_BITMAP_FILE) {
4995 * Commands even a read-only array can execute:
4999 case GET_ARRAY_INFO:
5000 err = get_array_info(mddev, argp);
5003 case GET_BITMAP_FILE:
5004 err = get_bitmap_file(mddev, argp);
5008 err = get_disk_info(mddev, argp);
5011 case RESTART_ARRAY_RW:
5012 err = restart_array(mddev);
5016 err = do_md_stop(mddev, 0, 1);
5020 err = do_md_stop(mddev, 1, 1);
5026 * The remaining ioctls are changing the state of the
5027 * superblock, so we do not allow them on read-only arrays.
5028 * However non-MD ioctls (e.g. get-size) will still come through
5029 * here and hit the 'default' below, so only disallow
5030 * 'md' ioctls, and switch to rw mode if started auto-readonly.
5032 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
5033 if (mddev->ro == 2) {
5035 sysfs_notify_dirent(mddev->sysfs_state);
5036 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5037 md_wakeup_thread(mddev->thread);
5048 mdu_disk_info_t info;
5049 if (copy_from_user(&info, argp, sizeof(info)))
5052 err = add_new_disk(mddev, &info);
5056 case HOT_REMOVE_DISK:
5057 err = hot_remove_disk(mddev, new_decode_dev(arg));
5061 err = hot_add_disk(mddev, new_decode_dev(arg));
5064 case SET_DISK_FAULTY:
5065 err = set_disk_faulty(mddev, new_decode_dev(arg));
5069 err = do_md_run(mddev);
5072 case SET_BITMAP_FILE:
5073 err = set_bitmap_file(mddev, (int)arg);
5083 if (mddev->hold_active == UNTIL_IOCTL &&
5085 mddev->hold_active = 0;
5086 mddev_unlock(mddev);
5096 static int md_open(struct block_device *bdev, fmode_t mode)
5099 * Succeed if we can lock the mddev, which confirms that
5100 * it isn't being stopped right now.
5102 mddev_t *mddev = mddev_find(bdev->bd_dev);
5105 if (mddev->gendisk != bdev->bd_disk) {
5106 /* we are racing with mddev_put which is discarding this
5110 /* Wait until bdev->bd_disk is definitely gone */
5111 flush_scheduled_work();
5112 /* Then retry the open from the top */
5113 return -ERESTARTSYS;
5115 BUG_ON(mddev != bdev->bd_disk->private_data);
5117 if ((err = mutex_lock_interruptible_nested(&mddev->reconfig_mutex, 1)))
5121 atomic_inc(&mddev->openers);
5122 mddev_unlock(mddev);
5124 check_disk_change(bdev);
5129 static int md_release(struct gendisk *disk, fmode_t mode)
5131 mddev_t *mddev = disk->private_data;
5134 atomic_dec(&mddev->openers);
5140 static int md_media_changed(struct gendisk *disk)
5142 mddev_t *mddev = disk->private_data;
5144 return mddev->changed;
5147 static int md_revalidate(struct gendisk *disk)
5149 mddev_t *mddev = disk->private_data;
5154 static struct block_device_operations md_fops =
5156 .owner = THIS_MODULE,
5158 .release = md_release,
5159 .locked_ioctl = md_ioctl,
5160 .getgeo = md_getgeo,
5161 .media_changed = md_media_changed,
5162 .revalidate_disk= md_revalidate,
5165 static int md_thread(void * arg)
5167 mdk_thread_t *thread = arg;
5170 * md_thread is a 'system-thread', it's priority should be very
5171 * high. We avoid resource deadlocks individually in each
5172 * raid personality. (RAID5 does preallocation) We also use RR and
5173 * the very same RT priority as kswapd, thus we will never get
5174 * into a priority inversion deadlock.
5176 * we definitely have to have equal or higher priority than
5177 * bdflush, otherwise bdflush will deadlock if there are too
5178 * many dirty RAID5 blocks.
5181 allow_signal(SIGKILL);
5182 while (!kthread_should_stop()) {
5184 /* We need to wait INTERRUPTIBLE so that
5185 * we don't add to the load-average.
5186 * That means we need to be sure no signals are
5189 if (signal_pending(current))
5190 flush_signals(current);
5192 wait_event_interruptible_timeout
5194 test_bit(THREAD_WAKEUP, &thread->flags)
5195 || kthread_should_stop(),
5198 clear_bit(THREAD_WAKEUP, &thread->flags);
5200 thread->run(thread->mddev);
5206 void md_wakeup_thread(mdk_thread_t *thread)
5209 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5210 set_bit(THREAD_WAKEUP, &thread->flags);
5211 wake_up(&thread->wqueue);
5215 mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5218 mdk_thread_t *thread;
5220 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
5224 init_waitqueue_head(&thread->wqueue);
5227 thread->mddev = mddev;
5228 thread->timeout = MAX_SCHEDULE_TIMEOUT;
5229 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
5230 if (IS_ERR(thread->tsk)) {
5237 void md_unregister_thread(mdk_thread_t *thread)
5239 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
5241 kthread_stop(thread->tsk);
5245 void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
5252 if (!rdev || test_bit(Faulty, &rdev->flags))
5255 if (mddev->external)
5256 set_bit(Blocked, &rdev->flags);
5258 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5260 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5261 __builtin_return_address(0),__builtin_return_address(1),
5262 __builtin_return_address(2),__builtin_return_address(3));
5266 if (!mddev->pers->error_handler)
5268 mddev->pers->error_handler(mddev,rdev);
5269 if (mddev->degraded)
5270 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
5271 set_bit(StateChanged, &rdev->flags);
5272 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5273 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5274 md_wakeup_thread(mddev->thread);
5275 md_new_event_inintr(mddev);
5278 /* seq_file implementation /proc/mdstat */
5280 static void status_unused(struct seq_file *seq)
5285 seq_printf(seq, "unused devices: ");
5287 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
5288 char b[BDEVNAME_SIZE];
5290 seq_printf(seq, "%s ",
5291 bdevname(rdev->bdev,b));
5294 seq_printf(seq, "<none>");
5296 seq_printf(seq, "\n");
5300 static void status_resync(struct seq_file *seq, mddev_t * mddev)
5302 sector_t max_blocks, resync, res;
5303 unsigned long dt, db, rt;
5305 unsigned int per_milli;
5307 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
5309 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
5310 max_blocks = mddev->resync_max_sectors >> 1;
5312 max_blocks = mddev->size;
5315 * Should not happen.
5321 /* Pick 'scale' such that (resync>>scale)*1000 will fit
5322 * in a sector_t, and (max_blocks>>scale) will fit in a
5323 * u32, as those are the requirements for sector_div.
5324 * Thus 'scale' must be at least 10
5327 if (sizeof(sector_t) > sizeof(unsigned long)) {
5328 while ( max_blocks/2 > (1ULL<<(scale+32)))
5331 res = (resync>>scale)*1000;
5332 sector_div(res, (u32)((max_blocks>>scale)+1));
5336 int i, x = per_milli/50, y = 20-x;
5337 seq_printf(seq, "[");
5338 for (i = 0; i < x; i++)
5339 seq_printf(seq, "=");
5340 seq_printf(seq, ">");
5341 for (i = 0; i < y; i++)
5342 seq_printf(seq, ".");
5343 seq_printf(seq, "] ");
5345 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
5346 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
5348 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
5350 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
5351 "resync" : "recovery"))),
5352 per_milli/10, per_milli % 10,
5353 (unsigned long long) resync,
5354 (unsigned long long) max_blocks);
5357 * We do not want to overflow, so the order of operands and
5358 * the * 100 / 100 trick are important. We do a +1 to be
5359 * safe against division by zero. We only estimate anyway.
5361 * dt: time from mark until now
5362 * db: blocks written from mark until now
5363 * rt: remaining time
5365 dt = ((jiffies - mddev->resync_mark) / HZ);
5367 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
5368 - mddev->resync_mark_cnt;
5369 rt = (dt * ((unsigned long)(max_blocks-resync) / (db/2/100+1)))/100;
5371 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
5373 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
5376 static void *md_seq_start(struct seq_file *seq, loff_t *pos)
5378 struct list_head *tmp;
5388 spin_lock(&all_mddevs_lock);
5389 list_for_each(tmp,&all_mddevs)
5391 mddev = list_entry(tmp, mddev_t, all_mddevs);
5393 spin_unlock(&all_mddevs_lock);
5396 spin_unlock(&all_mddevs_lock);
5398 return (void*)2;/* tail */
5402 static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
5404 struct list_head *tmp;
5405 mddev_t *next_mddev, *mddev = v;
5411 spin_lock(&all_mddevs_lock);
5413 tmp = all_mddevs.next;
5415 tmp = mddev->all_mddevs.next;
5416 if (tmp != &all_mddevs)
5417 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
5419 next_mddev = (void*)2;
5422 spin_unlock(&all_mddevs_lock);
5430 static void md_seq_stop(struct seq_file *seq, void *v)
5434 if (mddev && v != (void*)1 && v != (void*)2)
5438 struct mdstat_info {
5442 static int md_seq_show(struct seq_file *seq, void *v)
5447 struct mdstat_info *mi = seq->private;
5448 struct bitmap *bitmap;
5450 if (v == (void*)1) {
5451 struct mdk_personality *pers;
5452 seq_printf(seq, "Personalities : ");
5453 spin_lock(&pers_lock);
5454 list_for_each_entry(pers, &pers_list, list)
5455 seq_printf(seq, "[%s] ", pers->name);
5457 spin_unlock(&pers_lock);
5458 seq_printf(seq, "\n");
5459 mi->event = atomic_read(&md_event_count);
5462 if (v == (void*)2) {
5467 if (mddev_lock(mddev) < 0)
5470 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
5471 seq_printf(seq, "%s : %sactive", mdname(mddev),
5472 mddev->pers ? "" : "in");
5475 seq_printf(seq, " (read-only)");
5477 seq_printf(seq, " (auto-read-only)");
5478 seq_printf(seq, " %s", mddev->pers->name);
5482 list_for_each_entry(rdev, &mddev->disks, same_set) {
5483 char b[BDEVNAME_SIZE];
5484 seq_printf(seq, " %s[%d]",
5485 bdevname(rdev->bdev,b), rdev->desc_nr);
5486 if (test_bit(WriteMostly, &rdev->flags))
5487 seq_printf(seq, "(W)");
5488 if (test_bit(Faulty, &rdev->flags)) {
5489 seq_printf(seq, "(F)");
5491 } else if (rdev->raid_disk < 0)
5492 seq_printf(seq, "(S)"); /* spare */
5496 if (!list_empty(&mddev->disks)) {
5498 seq_printf(seq, "\n %llu blocks",
5499 (unsigned long long)
5500 mddev->array_sectors / 2);
5502 seq_printf(seq, "\n %llu blocks",
5503 (unsigned long long)size);
5505 if (mddev->persistent) {
5506 if (mddev->major_version != 0 ||
5507 mddev->minor_version != 90) {
5508 seq_printf(seq," super %d.%d",
5509 mddev->major_version,
5510 mddev->minor_version);
5512 } else if (mddev->external)
5513 seq_printf(seq, " super external:%s",
5514 mddev->metadata_type);
5516 seq_printf(seq, " super non-persistent");
5519 mddev->pers->status(seq, mddev);
5520 seq_printf(seq, "\n ");
5521 if (mddev->pers->sync_request) {
5522 if (mddev->curr_resync > 2) {
5523 status_resync(seq, mddev);
5524 seq_printf(seq, "\n ");
5525 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
5526 seq_printf(seq, "\tresync=DELAYED\n ");
5527 else if (mddev->recovery_cp < MaxSector)
5528 seq_printf(seq, "\tresync=PENDING\n ");
5531 seq_printf(seq, "\n ");
5533 if ((bitmap = mddev->bitmap)) {
5534 unsigned long chunk_kb;
5535 unsigned long flags;
5536 spin_lock_irqsave(&bitmap->lock, flags);
5537 chunk_kb = bitmap->chunksize >> 10;
5538 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
5540 bitmap->pages - bitmap->missing_pages,
5542 (bitmap->pages - bitmap->missing_pages)
5543 << (PAGE_SHIFT - 10),
5544 chunk_kb ? chunk_kb : bitmap->chunksize,
5545 chunk_kb ? "KB" : "B");
5547 seq_printf(seq, ", file: ");
5548 seq_path(seq, &bitmap->file->f_path, " \t\n");
5551 seq_printf(seq, "\n");
5552 spin_unlock_irqrestore(&bitmap->lock, flags);
5555 seq_printf(seq, "\n");
5557 mddev_unlock(mddev);
5562 static struct seq_operations md_seq_ops = {
5563 .start = md_seq_start,
5564 .next = md_seq_next,
5565 .stop = md_seq_stop,
5566 .show = md_seq_show,
5569 static int md_seq_open(struct inode *inode, struct file *file)
5572 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
5576 error = seq_open(file, &md_seq_ops);
5580 struct seq_file *p = file->private_data;
5582 mi->event = atomic_read(&md_event_count);
5587 static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
5589 struct seq_file *m = filp->private_data;
5590 struct mdstat_info *mi = m->private;
5593 poll_wait(filp, &md_event_waiters, wait);
5595 /* always allow read */
5596 mask = POLLIN | POLLRDNORM;
5598 if (mi->event != atomic_read(&md_event_count))
5599 mask |= POLLERR | POLLPRI;
5603 static const struct file_operations md_seq_fops = {
5604 .owner = THIS_MODULE,
5605 .open = md_seq_open,
5607 .llseek = seq_lseek,
5608 .release = seq_release_private,
5609 .poll = mdstat_poll,
5612 int register_md_personality(struct mdk_personality *p)
5614 spin_lock(&pers_lock);
5615 list_add_tail(&p->list, &pers_list);
5616 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
5617 spin_unlock(&pers_lock);
5621 int unregister_md_personality(struct mdk_personality *p)
5623 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
5624 spin_lock(&pers_lock);
5625 list_del_init(&p->list);
5626 spin_unlock(&pers_lock);
5630 static int is_mddev_idle(mddev_t *mddev)
5638 rdev_for_each_rcu(rdev, mddev) {
5639 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
5640 curr_events = part_stat_read(&disk->part0, sectors[0]) +
5641 part_stat_read(&disk->part0, sectors[1]) -
5642 atomic_read(&disk->sync_io);
5643 /* sync IO will cause sync_io to increase before the disk_stats
5644 * as sync_io is counted when a request starts, and
5645 * disk_stats is counted when it completes.
5646 * So resync activity will cause curr_events to be smaller than
5647 * when there was no such activity.
5648 * non-sync IO will cause disk_stat to increase without
5649 * increasing sync_io so curr_events will (eventually)
5650 * be larger than it was before. Once it becomes
5651 * substantially larger, the test below will cause
5652 * the array to appear non-idle, and resync will slow
5654 * If there is a lot of outstanding resync activity when
5655 * we set last_event to curr_events, then all that activity
5656 * completing might cause the array to appear non-idle
5657 * and resync will be slowed down even though there might
5658 * not have been non-resync activity. This will only
5659 * happen once though. 'last_events' will soon reflect
5660 * the state where there is little or no outstanding
5661 * resync requests, and further resync activity will
5662 * always make curr_events less than last_events.
5665 if (curr_events - rdev->last_events > 4096) {
5666 rdev->last_events = curr_events;
5674 void md_done_sync(mddev_t *mddev, int blocks, int ok)
5676 /* another "blocks" (512byte) blocks have been synced */
5677 atomic_sub(blocks, &mddev->recovery_active);
5678 wake_up(&mddev->recovery_wait);
5680 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5681 md_wakeup_thread(mddev->thread);
5682 // stop recovery, signal do_sync ....
5687 /* md_write_start(mddev, bi)
5688 * If we need to update some array metadata (e.g. 'active' flag
5689 * in superblock) before writing, schedule a superblock update
5690 * and wait for it to complete.
5692 void md_write_start(mddev_t *mddev, struct bio *bi)
5695 if (bio_data_dir(bi) != WRITE)
5698 BUG_ON(mddev->ro == 1);
5699 if (mddev->ro == 2) {
5700 /* need to switch to read/write */
5702 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5703 md_wakeup_thread(mddev->thread);
5704 md_wakeup_thread(mddev->sync_thread);
5707 atomic_inc(&mddev->writes_pending);
5708 if (mddev->safemode == 1)
5709 mddev->safemode = 0;
5710 if (mddev->in_sync) {
5711 spin_lock_irq(&mddev->write_lock);
5712 if (mddev->in_sync) {
5714 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
5715 md_wakeup_thread(mddev->thread);
5718 spin_unlock_irq(&mddev->write_lock);
5721 sysfs_notify_dirent(mddev->sysfs_state);
5722 wait_event(mddev->sb_wait,
5723 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
5724 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
5727 void md_write_end(mddev_t *mddev)
5729 if (atomic_dec_and_test(&mddev->writes_pending)) {
5730 if (mddev->safemode == 2)
5731 md_wakeup_thread(mddev->thread);
5732 else if (mddev->safemode_delay)
5733 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
5737 /* md_allow_write(mddev)
5738 * Calling this ensures that the array is marked 'active' so that writes
5739 * may proceed without blocking. It is important to call this before
5740 * attempting a GFP_KERNEL allocation while holding the mddev lock.
5741 * Must be called with mddev_lock held.
5743 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
5744 * is dropped, so return -EAGAIN after notifying userspace.
5746 int md_allow_write(mddev_t *mddev)
5752 if (!mddev->pers->sync_request)
5755 spin_lock_irq(&mddev->write_lock);
5756 if (mddev->in_sync) {
5758 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
5759 if (mddev->safemode_delay &&
5760 mddev->safemode == 0)
5761 mddev->safemode = 1;
5762 spin_unlock_irq(&mddev->write_lock);
5763 md_update_sb(mddev, 0);
5764 sysfs_notify_dirent(mddev->sysfs_state);
5766 spin_unlock_irq(&mddev->write_lock);
5768 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
5773 EXPORT_SYMBOL_GPL(md_allow_write);
5775 #define SYNC_MARKS 10
5776 #define SYNC_MARK_STEP (3*HZ)
5777 void md_do_sync(mddev_t *mddev)
5780 unsigned int currspeed = 0,
5782 sector_t max_sectors,j, io_sectors;
5783 unsigned long mark[SYNC_MARKS];
5784 sector_t mark_cnt[SYNC_MARKS];
5786 struct list_head *tmp;
5787 sector_t last_check;
5792 /* just incase thread restarts... */
5793 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
5795 if (mddev->ro) /* never try to sync a read-only array */
5798 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5799 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
5800 desc = "data-check";
5801 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5802 desc = "requested-resync";
5805 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5810 /* we overload curr_resync somewhat here.
5811 * 0 == not engaged in resync at all
5812 * 2 == checking that there is no conflict with another sync
5813 * 1 == like 2, but have yielded to allow conflicting resync to
5815 * other == active in resync - this many blocks
5817 * Before starting a resync we must have set curr_resync to
5818 * 2, and then checked that every "conflicting" array has curr_resync
5819 * less than ours. When we find one that is the same or higher
5820 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
5821 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
5822 * This will mean we have to start checking from the beginning again.
5827 mddev->curr_resync = 2;
5830 if (kthread_should_stop()) {
5831 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5834 for_each_mddev(mddev2, tmp) {
5835 if (mddev2 == mddev)
5837 if (!mddev->parallel_resync
5838 && mddev2->curr_resync
5839 && match_mddev_units(mddev, mddev2)) {
5841 if (mddev < mddev2 && mddev->curr_resync == 2) {
5842 /* arbitrarily yield */
5843 mddev->curr_resync = 1;
5844 wake_up(&resync_wait);
5846 if (mddev > mddev2 && mddev->curr_resync == 1)
5847 /* no need to wait here, we can wait the next
5848 * time 'round when curr_resync == 2
5851 /* We need to wait 'interruptible' so as not to
5852 * contribute to the load average, and not to
5853 * be caught by 'softlockup'
5855 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
5856 if (!kthread_should_stop() &&
5857 mddev2->curr_resync >= mddev->curr_resync) {
5858 printk(KERN_INFO "md: delaying %s of %s"
5859 " until %s has finished (they"
5860 " share one or more physical units)\n",
5861 desc, mdname(mddev), mdname(mddev2));
5863 if (signal_pending(current))
5864 flush_signals(current);
5866 finish_wait(&resync_wait, &wq);
5869 finish_wait(&resync_wait, &wq);
5872 } while (mddev->curr_resync < 2);
5875 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5876 /* resync follows the size requested by the personality,
5877 * which defaults to physical size, but can be virtual size
5879 max_sectors = mddev->resync_max_sectors;
5880 mddev->resync_mismatches = 0;
5881 /* we don't use the checkpoint if there's a bitmap */
5882 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5883 j = mddev->resync_min;
5884 else if (!mddev->bitmap)
5885 j = mddev->recovery_cp;
5887 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5888 max_sectors = mddev->size << 1;
5890 /* recovery follows the physical size of devices */
5891 max_sectors = mddev->size << 1;
5893 list_for_each_entry(rdev, &mddev->disks, same_set)
5894 if (rdev->raid_disk >= 0 &&
5895 !test_bit(Faulty, &rdev->flags) &&
5896 !test_bit(In_sync, &rdev->flags) &&
5897 rdev->recovery_offset < j)
5898 j = rdev->recovery_offset;
5901 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
5902 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
5903 " %d KB/sec/disk.\n", speed_min(mddev));
5904 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
5905 "(but not more than %d KB/sec) for %s.\n",
5906 speed_max(mddev), desc);
5908 is_mddev_idle(mddev); /* this also initializes IO event counters */
5911 for (m = 0; m < SYNC_MARKS; m++) {
5913 mark_cnt[m] = io_sectors;
5916 mddev->resync_mark = mark[last_mark];
5917 mddev->resync_mark_cnt = mark_cnt[last_mark];
5920 * Tune reconstruction:
5922 window = 32*(PAGE_SIZE/512);
5923 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
5924 window/2,(unsigned long long) max_sectors/2);
5926 atomic_set(&mddev->recovery_active, 0);
5931 "md: resuming %s of %s from checkpoint.\n",
5932 desc, mdname(mddev));
5933 mddev->curr_resync = j;
5936 while (j < max_sectors) {
5940 if (j >= mddev->resync_max) {
5941 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
5942 wait_event(mddev->recovery_wait,
5943 mddev->resync_max > j
5944 || kthread_should_stop());
5946 if (kthread_should_stop())
5948 sectors = mddev->pers->sync_request(mddev, j, &skipped,
5949 currspeed < speed_min(mddev));
5951 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5955 if (!skipped) { /* actual IO requested */
5956 io_sectors += sectors;
5957 atomic_add(sectors, &mddev->recovery_active);
5961 if (j>1) mddev->curr_resync = j;
5962 mddev->curr_mark_cnt = io_sectors;
5963 if (last_check == 0)
5964 /* this is the earliers that rebuilt will be
5965 * visible in /proc/mdstat
5967 md_new_event(mddev);
5969 if (last_check + window > io_sectors || j == max_sectors)
5972 last_check = io_sectors;
5974 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5978 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
5980 int next = (last_mark+1) % SYNC_MARKS;
5982 mddev->resync_mark = mark[next];
5983 mddev->resync_mark_cnt = mark_cnt[next];
5984 mark[next] = jiffies;
5985 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
5990 if (kthread_should_stop())
5995 * this loop exits only if either when we are slower than
5996 * the 'hard' speed limit, or the system was IO-idle for
5998 * the system might be non-idle CPU-wise, but we only care
5999 * about not overloading the IO subsystem. (things like an
6000 * e2fsck being done on the RAID array should execute fast)
6002 blk_unplug(mddev->queue);
6005 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6006 /((jiffies-mddev->resync_mark)/HZ +1) +1;
6008 if (currspeed > speed_min(mddev)) {
6009 if ((currspeed > speed_max(mddev)) ||
6010 !is_mddev_idle(mddev)) {
6016 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
6018 * this also signals 'finished resyncing' to md_stop
6021 blk_unplug(mddev->queue);
6023 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6025 /* tell personality that we are finished */
6026 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
6028 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
6029 mddev->curr_resync > 2) {
6030 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6031 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6032 if (mddev->curr_resync >= mddev->recovery_cp) {
6034 "md: checkpointing %s of %s.\n",
6035 desc, mdname(mddev));
6036 mddev->recovery_cp = mddev->curr_resync;
6039 mddev->recovery_cp = MaxSector;
6041 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6042 mddev->curr_resync = MaxSector;
6043 list_for_each_entry(rdev, &mddev->disks, same_set)
6044 if (rdev->raid_disk >= 0 &&
6045 !test_bit(Faulty, &rdev->flags) &&
6046 !test_bit(In_sync, &rdev->flags) &&
6047 rdev->recovery_offset < mddev->curr_resync)
6048 rdev->recovery_offset = mddev->curr_resync;
6051 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6054 mddev->curr_resync = 0;
6055 mddev->resync_min = 0;
6056 mddev->resync_max = MaxSector;
6057 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
6058 wake_up(&resync_wait);
6059 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6060 md_wakeup_thread(mddev->thread);
6065 * got a signal, exit.
6068 "md: md_do_sync() got signal ... exiting\n");
6069 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6073 EXPORT_SYMBOL_GPL(md_do_sync);
6076 static int remove_and_add_spares(mddev_t *mddev)
6081 list_for_each_entry(rdev, &mddev->disks, same_set)
6082 if (rdev->raid_disk >= 0 &&
6083 !test_bit(Blocked, &rdev->flags) &&
6084 (test_bit(Faulty, &rdev->flags) ||
6085 ! test_bit(In_sync, &rdev->flags)) &&
6086 atomic_read(&rdev->nr_pending)==0) {
6087 if (mddev->pers->hot_remove_disk(
6088 mddev, rdev->raid_disk)==0) {
6090 sprintf(nm,"rd%d", rdev->raid_disk);
6091 sysfs_remove_link(&mddev->kobj, nm);
6092 rdev->raid_disk = -1;
6096 if (mddev->degraded && ! mddev->ro) {
6097 list_for_each_entry(rdev, &mddev->disks, same_set) {
6098 if (rdev->raid_disk >= 0 &&
6099 !test_bit(In_sync, &rdev->flags) &&
6100 !test_bit(Blocked, &rdev->flags))
6102 if (rdev->raid_disk < 0
6103 && !test_bit(Faulty, &rdev->flags)) {
6104 rdev->recovery_offset = 0;
6106 hot_add_disk(mddev, rdev) == 0) {
6108 sprintf(nm, "rd%d", rdev->raid_disk);
6109 if (sysfs_create_link(&mddev->kobj,
6112 "md: cannot register "
6116 md_new_event(mddev);
6125 * This routine is regularly called by all per-raid-array threads to
6126 * deal with generic issues like resync and super-block update.
6127 * Raid personalities that don't have a thread (linear/raid0) do not
6128 * need this as they never do any recovery or update the superblock.
6130 * It does not do any resync itself, but rather "forks" off other threads
6131 * to do that as needed.
6132 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6133 * "->recovery" and create a thread at ->sync_thread.
6134 * When the thread finishes it sets MD_RECOVERY_DONE
6135 * and wakeups up this thread which will reap the thread and finish up.
6136 * This thread also removes any faulty devices (with nr_pending == 0).
6138 * The overall approach is:
6139 * 1/ if the superblock needs updating, update it.
6140 * 2/ If a recovery thread is running, don't do anything else.
6141 * 3/ If recovery has finished, clean up, possibly marking spares active.
6142 * 4/ If there are any faulty devices, remove them.
6143 * 5/ If array is degraded, try to add spares devices
6144 * 6/ If array has spares or is not in-sync, start a resync thread.
6146 void md_check_recovery(mddev_t *mddev)
6152 bitmap_daemon_work(mddev->bitmap);
6157 if (signal_pending(current)) {
6158 if (mddev->pers->sync_request && !mddev->external) {
6159 printk(KERN_INFO "md: %s in immediate safe mode\n",
6161 mddev->safemode = 2;
6163 flush_signals(current);
6166 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
6169 (mddev->flags && !mddev->external) ||
6170 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
6171 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
6172 (mddev->external == 0 && mddev->safemode == 1) ||
6173 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6174 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
6178 if (mddev_trylock(mddev)) {
6182 /* Only thing we do on a ro array is remove
6185 remove_and_add_spares(mddev);
6186 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6190 if (!mddev->external) {
6192 spin_lock_irq(&mddev->write_lock);
6193 if (mddev->safemode &&
6194 !atomic_read(&mddev->writes_pending) &&
6196 mddev->recovery_cp == MaxSector) {
6199 if (mddev->persistent)
6200 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6202 if (mddev->safemode == 1)
6203 mddev->safemode = 0;
6204 spin_unlock_irq(&mddev->write_lock);
6206 sysfs_notify_dirent(mddev->sysfs_state);
6210 md_update_sb(mddev, 0);
6212 list_for_each_entry(rdev, &mddev->disks, same_set)
6213 if (test_and_clear_bit(StateChanged, &rdev->flags))
6214 sysfs_notify_dirent(rdev->sysfs_state);
6217 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
6218 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
6219 /* resync/recovery still happening */
6220 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6223 if (mddev->sync_thread) {
6224 /* resync has finished, collect result */
6225 md_unregister_thread(mddev->sync_thread);
6226 mddev->sync_thread = NULL;
6227 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
6228 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
6230 /* activate any spares */
6231 if (mddev->pers->spare_active(mddev))
6232 sysfs_notify(&mddev->kobj, NULL,
6235 md_update_sb(mddev, 1);
6237 /* if array is no-longer degraded, then any saved_raid_disk
6238 * information must be scrapped
6240 if (!mddev->degraded)
6241 list_for_each_entry(rdev, &mddev->disks, same_set)
6242 rdev->saved_raid_disk = -1;
6244 mddev->recovery = 0;
6245 /* flag recovery needed just to double check */
6246 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6247 sysfs_notify_dirent(mddev->sysfs_action);
6248 md_new_event(mddev);
6251 /* Set RUNNING before clearing NEEDED to avoid
6252 * any transients in the value of "sync_action".
6254 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6255 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6256 /* Clear some bits that don't mean anything, but
6259 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
6260 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
6262 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
6264 /* no recovery is running.
6265 * remove any failed drives, then
6266 * add spares if possible.
6267 * Spare are also removed and re-added, to allow
6268 * the personality to fail the re-add.
6271 if (mddev->reshape_position != MaxSector) {
6272 if (mddev->pers->check_reshape(mddev) != 0)
6273 /* Cannot proceed */
6275 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
6276 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
6277 } else if ((spares = remove_and_add_spares(mddev))) {
6278 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6279 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
6280 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
6281 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
6282 } else if (mddev->recovery_cp < MaxSector) {
6283 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6284 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
6285 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
6286 /* nothing to be done ... */
6289 if (mddev->pers->sync_request) {
6290 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
6291 /* We are adding a device or devices to an array
6292 * which has the bitmap stored on all devices.
6293 * So make sure all bitmap pages get written
6295 bitmap_write_all(mddev->bitmap);
6297 mddev->sync_thread = md_register_thread(md_do_sync,
6300 if (!mddev->sync_thread) {
6301 printk(KERN_ERR "%s: could not start resync"
6304 /* leave the spares where they are, it shouldn't hurt */
6305 mddev->recovery = 0;
6307 md_wakeup_thread(mddev->sync_thread);
6308 sysfs_notify_dirent(mddev->sysfs_action);
6309 md_new_event(mddev);
6312 if (!mddev->sync_thread) {
6313 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6314 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
6316 if (mddev->sysfs_action)
6317 sysfs_notify_dirent(mddev->sysfs_action);
6319 mddev_unlock(mddev);
6323 void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
6325 sysfs_notify_dirent(rdev->sysfs_state);
6326 wait_event_timeout(rdev->blocked_wait,
6327 !test_bit(Blocked, &rdev->flags),
6328 msecs_to_jiffies(5000));
6329 rdev_dec_pending(rdev, mddev);
6331 EXPORT_SYMBOL(md_wait_for_blocked_rdev);
6333 static int md_notify_reboot(struct notifier_block *this,
6334 unsigned long code, void *x)
6336 struct list_head *tmp;
6339 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
6341 printk(KERN_INFO "md: stopping all md devices.\n");
6343 for_each_mddev(mddev, tmp)
6344 if (mddev_trylock(mddev)) {
6345 /* Force a switch to readonly even array
6346 * appears to still be in use. Hence
6349 do_md_stop(mddev, 1, 100);
6350 mddev_unlock(mddev);
6353 * certain more exotic SCSI devices are known to be
6354 * volatile wrt too early system reboots. While the
6355 * right place to handle this issue is the given
6356 * driver, we do want to have a safe RAID driver ...
6363 static struct notifier_block md_notifier = {
6364 .notifier_call = md_notify_reboot,
6366 .priority = INT_MAX, /* before any real devices */
6369 static void md_geninit(void)
6371 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
6373 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
6376 static int __init md_init(void)
6378 if (register_blkdev(MAJOR_NR, "md"))
6380 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
6381 unregister_blkdev(MAJOR_NR, "md");
6384 blk_register_region(MKDEV(MAJOR_NR, 0), 1UL<<MINORBITS, THIS_MODULE,
6385 md_probe, NULL, NULL);
6386 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
6387 md_probe, NULL, NULL);
6389 register_reboot_notifier(&md_notifier);
6390 raid_table_header = register_sysctl_table(raid_root_table);
6400 * Searches all registered partitions for autorun RAID arrays
6404 static LIST_HEAD(all_detected_devices);
6405 struct detected_devices_node {
6406 struct list_head list;
6410 void md_autodetect_dev(dev_t dev)
6412 struct detected_devices_node *node_detected_dev;
6414 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
6415 if (node_detected_dev) {
6416 node_detected_dev->dev = dev;
6417 list_add_tail(&node_detected_dev->list, &all_detected_devices);
6419 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
6420 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
6425 static void autostart_arrays(int part)
6428 struct detected_devices_node *node_detected_dev;
6430 int i_scanned, i_passed;
6435 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
6437 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
6439 node_detected_dev = list_entry(all_detected_devices.next,
6440 struct detected_devices_node, list);
6441 list_del(&node_detected_dev->list);
6442 dev = node_detected_dev->dev;
6443 kfree(node_detected_dev);
6444 rdev = md_import_device(dev,0, 90);
6448 if (test_bit(Faulty, &rdev->flags)) {
6452 set_bit(AutoDetected, &rdev->flags);
6453 list_add(&rdev->same_set, &pending_raid_disks);
6457 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
6458 i_scanned, i_passed);
6460 autorun_devices(part);
6463 #endif /* !MODULE */
6465 static __exit void md_exit(void)
6468 struct list_head *tmp;
6470 blk_unregister_region(MKDEV(MAJOR_NR,0), 1U << MINORBITS);
6471 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
6473 unregister_blkdev(MAJOR_NR,"md");
6474 unregister_blkdev(mdp_major, "mdp");
6475 unregister_reboot_notifier(&md_notifier);
6476 unregister_sysctl_table(raid_table_header);
6477 remove_proc_entry("mdstat", NULL);
6478 for_each_mddev(mddev, tmp) {
6479 export_array(mddev);
6480 mddev->hold_active = 0;
6484 subsys_initcall(md_init);
6485 module_exit(md_exit)
6487 static int get_ro(char *buffer, struct kernel_param *kp)
6489 return sprintf(buffer, "%d", start_readonly);
6491 static int set_ro(const char *val, struct kernel_param *kp)
6494 int num = simple_strtoul(val, &e, 10);
6495 if (*val && (*e == '\0' || *e == '\n')) {
6496 start_readonly = num;
6502 module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
6503 module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
6506 EXPORT_SYMBOL(register_md_personality);
6507 EXPORT_SYMBOL(unregister_md_personality);
6508 EXPORT_SYMBOL(md_error);
6509 EXPORT_SYMBOL(md_done_sync);
6510 EXPORT_SYMBOL(md_write_start);
6511 EXPORT_SYMBOL(md_write_end);
6512 EXPORT_SYMBOL(md_register_thread);
6513 EXPORT_SYMBOL(md_unregister_thread);
6514 EXPORT_SYMBOL(md_wakeup_thread);
6515 EXPORT_SYMBOL(md_check_recovery);
6516 MODULE_LICENSE("GPL");
6518 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);