2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
5 completely rewritten, based on the MD driver code from Marc Zyngier
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
20 Neil Brown <neilb@cse.unsw.edu.au>.
22 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
25 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 #include <linux/module.h>
36 #include <linux/config.h>
37 #include <linux/kthread.h>
38 #include <linux/linkage.h>
39 #include <linux/raid/md.h>
40 #include <linux/raid/bitmap.h>
41 #include <linux/sysctl.h>
42 #include <linux/devfs_fs_kernel.h>
43 #include <linux/buffer_head.h> /* for invalidate_bdev */
44 #include <linux/suspend.h>
45 #include <linux/poll.h>
46 #include <linux/mutex.h>
48 #include <linux/init.h>
50 #include <linux/file.h>
53 #include <linux/kmod.h>
56 #include <asm/unaligned.h>
58 #define MAJOR_NR MD_MAJOR
61 /* 63 partitions with the alternate major number (mdp) */
62 #define MdpMinorShift 6
65 #define dprintk(x...) ((void)(DEBUG && printk(x)))
69 static void autostart_arrays (int part);
72 static LIST_HEAD(pers_list);
73 static DEFINE_SPINLOCK(pers_lock);
76 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
77 * is 1000 KB/sec, so the extra system load does not show up that much.
78 * Increase it if you want to have more _guaranteed_ speed. Note that
79 * the RAID driver will use the maximum available bandwidth if the IO
80 * subsystem is idle. There is also an 'absolute maximum' reconstruction
81 * speed limit - in case reconstruction slows down your system despite
84 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
85 * or /sys/block/mdX/md/sync_speed_{min,max}
88 static int sysctl_speed_limit_min = 1000;
89 static int sysctl_speed_limit_max = 200000;
90 static inline int speed_min(mddev_t *mddev)
92 return mddev->sync_speed_min ?
93 mddev->sync_speed_min : sysctl_speed_limit_min;
96 static inline int speed_max(mddev_t *mddev)
98 return mddev->sync_speed_max ?
99 mddev->sync_speed_max : sysctl_speed_limit_max;
102 static struct ctl_table_header *raid_table_header;
104 static ctl_table raid_table[] = {
106 .ctl_name = DEV_RAID_SPEED_LIMIT_MIN,
107 .procname = "speed_limit_min",
108 .data = &sysctl_speed_limit_min,
109 .maxlen = sizeof(int),
111 .proc_handler = &proc_dointvec,
114 .ctl_name = DEV_RAID_SPEED_LIMIT_MAX,
115 .procname = "speed_limit_max",
116 .data = &sysctl_speed_limit_max,
117 .maxlen = sizeof(int),
119 .proc_handler = &proc_dointvec,
124 static ctl_table raid_dir_table[] = {
126 .ctl_name = DEV_RAID,
135 static ctl_table raid_root_table[] = {
141 .child = raid_dir_table,
146 static struct block_device_operations md_fops;
148 static int start_readonly;
151 * We have a system wide 'event count' that is incremented
152 * on any 'interesting' event, and readers of /proc/mdstat
153 * can use 'poll' or 'select' to find out when the event
157 * start array, stop array, error, add device, remove device,
158 * start build, activate spare
160 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
161 static atomic_t md_event_count;
162 void md_new_event(mddev_t *mddev)
164 atomic_inc(&md_event_count);
165 wake_up(&md_event_waiters);
166 sysfs_notify(&mddev->kobj, NULL, "sync_action");
168 EXPORT_SYMBOL_GPL(md_new_event);
171 * Enables to iterate over all existing md arrays
172 * all_mddevs_lock protects this list.
174 static LIST_HEAD(all_mddevs);
175 static DEFINE_SPINLOCK(all_mddevs_lock);
179 * iterates through all used mddevs in the system.
180 * We take care to grab the all_mddevs_lock whenever navigating
181 * the list, and to always hold a refcount when unlocked.
182 * Any code which breaks out of this loop while own
183 * a reference to the current mddev and must mddev_put it.
185 #define ITERATE_MDDEV(mddev,tmp) \
187 for (({ spin_lock(&all_mddevs_lock); \
188 tmp = all_mddevs.next; \
190 ({ if (tmp != &all_mddevs) \
191 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
192 spin_unlock(&all_mddevs_lock); \
193 if (mddev) mddev_put(mddev); \
194 mddev = list_entry(tmp, mddev_t, all_mddevs); \
195 tmp != &all_mddevs;}); \
196 ({ spin_lock(&all_mddevs_lock); \
201 static int md_fail_request (request_queue_t *q, struct bio *bio)
203 bio_io_error(bio, bio->bi_size);
207 static inline mddev_t *mddev_get(mddev_t *mddev)
209 atomic_inc(&mddev->active);
213 static void mddev_put(mddev_t *mddev)
215 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
217 if (!mddev->raid_disks && list_empty(&mddev->disks)) {
218 list_del(&mddev->all_mddevs);
219 spin_unlock(&all_mddevs_lock);
220 blk_cleanup_queue(mddev->queue);
221 kobject_unregister(&mddev->kobj);
223 spin_unlock(&all_mddevs_lock);
226 static mddev_t * mddev_find(dev_t unit)
228 mddev_t *mddev, *new = NULL;
231 spin_lock(&all_mddevs_lock);
232 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
233 if (mddev->unit == unit) {
235 spin_unlock(&all_mddevs_lock);
241 list_add(&new->all_mddevs, &all_mddevs);
242 spin_unlock(&all_mddevs_lock);
245 spin_unlock(&all_mddevs_lock);
247 new = kzalloc(sizeof(*new), GFP_KERNEL);
252 if (MAJOR(unit) == MD_MAJOR)
253 new->md_minor = MINOR(unit);
255 new->md_minor = MINOR(unit) >> MdpMinorShift;
257 mutex_init(&new->reconfig_mutex);
258 INIT_LIST_HEAD(&new->disks);
259 INIT_LIST_HEAD(&new->all_mddevs);
260 init_timer(&new->safemode_timer);
261 atomic_set(&new->active, 1);
262 spin_lock_init(&new->write_lock);
263 init_waitqueue_head(&new->sb_wait);
265 new->queue = blk_alloc_queue(GFP_KERNEL);
270 set_bit(QUEUE_FLAG_CLUSTER, &new->queue->queue_flags);
272 blk_queue_make_request(new->queue, md_fail_request);
277 static inline int mddev_lock(mddev_t * mddev)
279 return mutex_lock_interruptible(&mddev->reconfig_mutex);
282 static inline int mddev_trylock(mddev_t * mddev)
284 return mutex_trylock(&mddev->reconfig_mutex);
287 static inline void mddev_unlock(mddev_t * mddev)
289 mutex_unlock(&mddev->reconfig_mutex);
291 md_wakeup_thread(mddev->thread);
294 static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
297 struct list_head *tmp;
299 ITERATE_RDEV(mddev,rdev,tmp) {
300 if (rdev->desc_nr == nr)
306 static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
308 struct list_head *tmp;
311 ITERATE_RDEV(mddev,rdev,tmp) {
312 if (rdev->bdev->bd_dev == dev)
318 static struct mdk_personality *find_pers(int level, char *clevel)
320 struct mdk_personality *pers;
321 list_for_each_entry(pers, &pers_list, list) {
322 if (level != LEVEL_NONE && pers->level == level)
324 if (strcmp(pers->name, clevel)==0)
330 static inline sector_t calc_dev_sboffset(struct block_device *bdev)
332 sector_t size = bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
333 return MD_NEW_SIZE_BLOCKS(size);
336 static sector_t calc_dev_size(mdk_rdev_t *rdev, unsigned chunk_size)
340 size = rdev->sb_offset;
343 size &= ~((sector_t)chunk_size/1024 - 1);
347 static int alloc_disk_sb(mdk_rdev_t * rdev)
352 rdev->sb_page = alloc_page(GFP_KERNEL);
353 if (!rdev->sb_page) {
354 printk(KERN_ALERT "md: out of memory.\n");
361 static void free_disk_sb(mdk_rdev_t * rdev)
364 put_page(rdev->sb_page);
366 rdev->sb_page = NULL;
373 static int super_written(struct bio *bio, unsigned int bytes_done, int error)
375 mdk_rdev_t *rdev = bio->bi_private;
376 mddev_t *mddev = rdev->mddev;
380 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags))
381 md_error(mddev, rdev);
383 if (atomic_dec_and_test(&mddev->pending_writes))
384 wake_up(&mddev->sb_wait);
389 static int super_written_barrier(struct bio *bio, unsigned int bytes_done, int error)
391 struct bio *bio2 = bio->bi_private;
392 mdk_rdev_t *rdev = bio2->bi_private;
393 mddev_t *mddev = rdev->mddev;
397 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
398 error == -EOPNOTSUPP) {
400 /* barriers don't appear to be supported :-( */
401 set_bit(BarriersNotsupp, &rdev->flags);
402 mddev->barriers_work = 0;
403 spin_lock_irqsave(&mddev->write_lock, flags);
404 bio2->bi_next = mddev->biolist;
405 mddev->biolist = bio2;
406 spin_unlock_irqrestore(&mddev->write_lock, flags);
407 wake_up(&mddev->sb_wait);
412 bio->bi_private = rdev;
413 return super_written(bio, bytes_done, error);
416 void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
417 sector_t sector, int size, struct page *page)
419 /* write first size bytes of page to sector of rdev
420 * Increment mddev->pending_writes before returning
421 * and decrement it on completion, waking up sb_wait
422 * if zero is reached.
423 * If an error occurred, call md_error
425 * As we might need to resubmit the request if BIO_RW_BARRIER
426 * causes ENOTSUPP, we allocate a spare bio...
428 struct bio *bio = bio_alloc(GFP_NOIO, 1);
429 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNC);
431 bio->bi_bdev = rdev->bdev;
432 bio->bi_sector = sector;
433 bio_add_page(bio, page, size, 0);
434 bio->bi_private = rdev;
435 bio->bi_end_io = super_written;
438 atomic_inc(&mddev->pending_writes);
439 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
441 rw |= (1<<BIO_RW_BARRIER);
442 rbio = bio_clone(bio, GFP_NOIO);
443 rbio->bi_private = bio;
444 rbio->bi_end_io = super_written_barrier;
445 submit_bio(rw, rbio);
450 void md_super_wait(mddev_t *mddev)
452 /* wait for all superblock writes that were scheduled to complete.
453 * if any had to be retried (due to BARRIER problems), retry them
457 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
458 if (atomic_read(&mddev->pending_writes)==0)
460 while (mddev->biolist) {
462 spin_lock_irq(&mddev->write_lock);
463 bio = mddev->biolist;
464 mddev->biolist = bio->bi_next ;
466 spin_unlock_irq(&mddev->write_lock);
467 submit_bio(bio->bi_rw, bio);
471 finish_wait(&mddev->sb_wait, &wq);
474 static int bi_complete(struct bio *bio, unsigned int bytes_done, int error)
479 complete((struct completion*)bio->bi_private);
483 int sync_page_io(struct block_device *bdev, sector_t sector, int size,
484 struct page *page, int rw)
486 struct bio *bio = bio_alloc(GFP_NOIO, 1);
487 struct completion event;
490 rw |= (1 << BIO_RW_SYNC);
493 bio->bi_sector = sector;
494 bio_add_page(bio, page, size, 0);
495 init_completion(&event);
496 bio->bi_private = &event;
497 bio->bi_end_io = bi_complete;
499 wait_for_completion(&event);
501 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
505 EXPORT_SYMBOL_GPL(sync_page_io);
507 static int read_disk_sb(mdk_rdev_t * rdev, int size)
509 char b[BDEVNAME_SIZE];
510 if (!rdev->sb_page) {
518 if (!sync_page_io(rdev->bdev, rdev->sb_offset<<1, size, rdev->sb_page, READ))
524 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
525 bdevname(rdev->bdev,b));
529 static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
531 if ( (sb1->set_uuid0 == sb2->set_uuid0) &&
532 (sb1->set_uuid1 == sb2->set_uuid1) &&
533 (sb1->set_uuid2 == sb2->set_uuid2) &&
534 (sb1->set_uuid3 == sb2->set_uuid3))
542 static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
545 mdp_super_t *tmp1, *tmp2;
547 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
548 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
550 if (!tmp1 || !tmp2) {
552 printk(KERN_INFO "md.c: sb1 is not equal to sb2!\n");
560 * nr_disks is not constant
565 if (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4))
576 static unsigned int calc_sb_csum(mdp_super_t * sb)
578 unsigned int disk_csum, csum;
580 disk_csum = sb->sb_csum;
582 csum = csum_partial((void *)sb, MD_SB_BYTES, 0);
583 sb->sb_csum = disk_csum;
589 * Handle superblock details.
590 * We want to be able to handle multiple superblock formats
591 * so we have a common interface to them all, and an array of
592 * different handlers.
593 * We rely on user-space to write the initial superblock, and support
594 * reading and updating of superblocks.
595 * Interface methods are:
596 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
597 * loads and validates a superblock on dev.
598 * if refdev != NULL, compare superblocks on both devices
600 * 0 - dev has a superblock that is compatible with refdev
601 * 1 - dev has a superblock that is compatible and newer than refdev
602 * so dev should be used as the refdev in future
603 * -EINVAL superblock incompatible or invalid
604 * -othererror e.g. -EIO
606 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
607 * Verify that dev is acceptable into mddev.
608 * The first time, mddev->raid_disks will be 0, and data from
609 * dev should be merged in. Subsequent calls check that dev
610 * is new enough. Return 0 or -EINVAL
612 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
613 * Update the superblock for rdev with data in mddev
614 * This does not write to disc.
620 struct module *owner;
621 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version);
622 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
623 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
627 * load_super for 0.90.0
629 static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
631 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
637 * Calculate the position of the superblock,
638 * it's at the end of the disk.
640 * It also happens to be a multiple of 4Kb.
642 sb_offset = calc_dev_sboffset(rdev->bdev);
643 rdev->sb_offset = sb_offset;
645 ret = read_disk_sb(rdev, MD_SB_BYTES);
650 bdevname(rdev->bdev, b);
651 sb = (mdp_super_t*)page_address(rdev->sb_page);
653 if (sb->md_magic != MD_SB_MAGIC) {
654 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
659 if (sb->major_version != 0 ||
660 sb->minor_version < 90 ||
661 sb->minor_version > 91) {
662 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
663 sb->major_version, sb->minor_version,
668 if (sb->raid_disks <= 0)
671 if (csum_fold(calc_sb_csum(sb)) != csum_fold(sb->sb_csum)) {
672 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
677 rdev->preferred_minor = sb->md_minor;
678 rdev->data_offset = 0;
679 rdev->sb_size = MD_SB_BYTES;
681 if (sb->level == LEVEL_MULTIPATH)
684 rdev->desc_nr = sb->this_disk.number;
690 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
691 if (!uuid_equal(refsb, sb)) {
692 printk(KERN_WARNING "md: %s has different UUID to %s\n",
693 b, bdevname(refdev->bdev,b2));
696 if (!sb_equal(refsb, sb)) {
697 printk(KERN_WARNING "md: %s has same UUID"
698 " but different superblock to %s\n",
699 b, bdevname(refdev->bdev, b2));
703 ev2 = md_event(refsb);
709 rdev->size = calc_dev_size(rdev, sb->chunk_size);
711 if (rdev->size < sb->size && sb->level > 1)
712 /* "this cannot possibly happen" ... */
720 * validate_super for 0.90.0
722 static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
725 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
727 rdev->raid_disk = -1;
729 if (mddev->raid_disks == 0) {
730 mddev->major_version = 0;
731 mddev->minor_version = sb->minor_version;
732 mddev->patch_version = sb->patch_version;
733 mddev->persistent = ! sb->not_persistent;
734 mddev->chunk_size = sb->chunk_size;
735 mddev->ctime = sb->ctime;
736 mddev->utime = sb->utime;
737 mddev->level = sb->level;
738 mddev->clevel[0] = 0;
739 mddev->layout = sb->layout;
740 mddev->raid_disks = sb->raid_disks;
741 mddev->size = sb->size;
742 mddev->events = md_event(sb);
743 mddev->bitmap_offset = 0;
744 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
746 if (mddev->minor_version >= 91) {
747 mddev->reshape_position = sb->reshape_position;
748 mddev->delta_disks = sb->delta_disks;
749 mddev->new_level = sb->new_level;
750 mddev->new_layout = sb->new_layout;
751 mddev->new_chunk = sb->new_chunk;
753 mddev->reshape_position = MaxSector;
754 mddev->delta_disks = 0;
755 mddev->new_level = mddev->level;
756 mddev->new_layout = mddev->layout;
757 mddev->new_chunk = mddev->chunk_size;
760 if (sb->state & (1<<MD_SB_CLEAN))
761 mddev->recovery_cp = MaxSector;
763 if (sb->events_hi == sb->cp_events_hi &&
764 sb->events_lo == sb->cp_events_lo) {
765 mddev->recovery_cp = sb->recovery_cp;
767 mddev->recovery_cp = 0;
770 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
771 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
772 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
773 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
775 mddev->max_disks = MD_SB_DISKS;
777 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
778 mddev->bitmap_file == NULL) {
779 if (mddev->level != 1 && mddev->level != 4
780 && mddev->level != 5 && mddev->level != 6
781 && mddev->level != 10) {
782 /* FIXME use a better test */
783 printk(KERN_WARNING "md: bitmaps not supported for this level.\n");
786 mddev->bitmap_offset = mddev->default_bitmap_offset;
789 } else if (mddev->pers == NULL) {
790 /* Insist on good event counter while assembling */
791 __u64 ev1 = md_event(sb);
793 if (ev1 < mddev->events)
795 } else if (mddev->bitmap) {
796 /* if adding to array with a bitmap, then we can accept an
797 * older device ... but not too old.
799 __u64 ev1 = md_event(sb);
800 if (ev1 < mddev->bitmap->events_cleared)
802 } else /* just a hot-add of a new device, leave raid_disk at -1 */
805 if (mddev->level != LEVEL_MULTIPATH) {
806 desc = sb->disks + rdev->desc_nr;
808 if (desc->state & (1<<MD_DISK_FAULTY))
809 set_bit(Faulty, &rdev->flags);
810 else if (desc->state & (1<<MD_DISK_SYNC) &&
811 desc->raid_disk < mddev->raid_disks) {
812 set_bit(In_sync, &rdev->flags);
813 rdev->raid_disk = desc->raid_disk;
815 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
816 set_bit(WriteMostly, &rdev->flags);
817 } else /* MULTIPATH are always insync */
818 set_bit(In_sync, &rdev->flags);
823 * sync_super for 0.90.0
825 static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
828 struct list_head *tmp;
830 int next_spare = mddev->raid_disks;
833 /* make rdev->sb match mddev data..
836 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
837 * 3/ any empty disks < next_spare become removed
839 * disks[0] gets initialised to REMOVED because
840 * we cannot be sure from other fields if it has
841 * been initialised or not.
844 int active=0, working=0,failed=0,spare=0,nr_disks=0;
846 rdev->sb_size = MD_SB_BYTES;
848 sb = (mdp_super_t*)page_address(rdev->sb_page);
850 memset(sb, 0, sizeof(*sb));
852 sb->md_magic = MD_SB_MAGIC;
853 sb->major_version = mddev->major_version;
854 sb->patch_version = mddev->patch_version;
855 sb->gvalid_words = 0; /* ignored */
856 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
857 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
858 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
859 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
861 sb->ctime = mddev->ctime;
862 sb->level = mddev->level;
863 sb->size = mddev->size;
864 sb->raid_disks = mddev->raid_disks;
865 sb->md_minor = mddev->md_minor;
866 sb->not_persistent = !mddev->persistent;
867 sb->utime = mddev->utime;
869 sb->events_hi = (mddev->events>>32);
870 sb->events_lo = (u32)mddev->events;
872 if (mddev->reshape_position == MaxSector)
873 sb->minor_version = 90;
875 sb->minor_version = 91;
876 sb->reshape_position = mddev->reshape_position;
877 sb->new_level = mddev->new_level;
878 sb->delta_disks = mddev->delta_disks;
879 sb->new_layout = mddev->new_layout;
880 sb->new_chunk = mddev->new_chunk;
882 mddev->minor_version = sb->minor_version;
885 sb->recovery_cp = mddev->recovery_cp;
886 sb->cp_events_hi = (mddev->events>>32);
887 sb->cp_events_lo = (u32)mddev->events;
888 if (mddev->recovery_cp == MaxSector)
889 sb->state = (1<< MD_SB_CLEAN);
893 sb->layout = mddev->layout;
894 sb->chunk_size = mddev->chunk_size;
896 if (mddev->bitmap && mddev->bitmap_file == NULL)
897 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
899 sb->disks[0].state = (1<<MD_DISK_REMOVED);
900 ITERATE_RDEV(mddev,rdev2,tmp) {
903 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
904 && !test_bit(Faulty, &rdev2->flags))
905 desc_nr = rdev2->raid_disk;
907 desc_nr = next_spare++;
908 rdev2->desc_nr = desc_nr;
909 d = &sb->disks[rdev2->desc_nr];
911 d->number = rdev2->desc_nr;
912 d->major = MAJOR(rdev2->bdev->bd_dev);
913 d->minor = MINOR(rdev2->bdev->bd_dev);
914 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
915 && !test_bit(Faulty, &rdev2->flags))
916 d->raid_disk = rdev2->raid_disk;
918 d->raid_disk = rdev2->desc_nr; /* compatibility */
919 if (test_bit(Faulty, &rdev2->flags))
920 d->state = (1<<MD_DISK_FAULTY);
921 else if (test_bit(In_sync, &rdev2->flags)) {
922 d->state = (1<<MD_DISK_ACTIVE);
923 d->state |= (1<<MD_DISK_SYNC);
931 if (test_bit(WriteMostly, &rdev2->flags))
932 d->state |= (1<<MD_DISK_WRITEMOSTLY);
934 /* now set the "removed" and "faulty" bits on any missing devices */
935 for (i=0 ; i < mddev->raid_disks ; i++) {
936 mdp_disk_t *d = &sb->disks[i];
937 if (d->state == 0 && d->number == 0) {
940 d->state = (1<<MD_DISK_REMOVED);
941 d->state |= (1<<MD_DISK_FAULTY);
945 sb->nr_disks = nr_disks;
946 sb->active_disks = active;
947 sb->working_disks = working;
948 sb->failed_disks = failed;
949 sb->spare_disks = spare;
951 sb->this_disk = sb->disks[rdev->desc_nr];
952 sb->sb_csum = calc_sb_csum(sb);
956 * version 1 superblock
959 static unsigned int calc_sb_1_csum(struct mdp_superblock_1 * sb)
961 unsigned int disk_csum, csum;
962 unsigned long long newcsum;
963 int size = 256 + le32_to_cpu(sb->max_dev)*2;
964 unsigned int *isuper = (unsigned int*)sb;
967 disk_csum = sb->sb_csum;
970 for (i=0; size>=4; size -= 4 )
971 newcsum += le32_to_cpu(*isuper++);
974 newcsum += le16_to_cpu(*(unsigned short*) isuper);
976 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
977 sb->sb_csum = disk_csum;
978 return cpu_to_le32(csum);
981 static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
983 struct mdp_superblock_1 *sb;
986 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
990 * Calculate the position of the superblock.
991 * It is always aligned to a 4K boundary and
992 * depeding on minor_version, it can be:
993 * 0: At least 8K, but less than 12K, from end of device
994 * 1: At start of device
995 * 2: 4K from start of device.
997 switch(minor_version) {
999 sb_offset = rdev->bdev->bd_inode->i_size >> 9;
1001 sb_offset &= ~(sector_t)(4*2-1);
1002 /* convert from sectors to K */
1014 rdev->sb_offset = sb_offset;
1016 /* superblock is rarely larger than 1K, but it can be larger,
1017 * and it is safe to read 4k, so we do that
1019 ret = read_disk_sb(rdev, 4096);
1020 if (ret) return ret;
1023 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1025 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1026 sb->major_version != cpu_to_le32(1) ||
1027 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1028 le64_to_cpu(sb->super_offset) != (rdev->sb_offset<<1) ||
1029 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
1032 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1033 printk("md: invalid superblock checksum on %s\n",
1034 bdevname(rdev->bdev,b));
1037 if (le64_to_cpu(sb->data_size) < 10) {
1038 printk("md: data_size too small on %s\n",
1039 bdevname(rdev->bdev,b));
1042 rdev->preferred_minor = 0xffff;
1043 rdev->data_offset = le64_to_cpu(sb->data_offset);
1044 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
1046 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
1047 bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
1048 if (rdev->sb_size & bmask)
1049 rdev-> sb_size = (rdev->sb_size | bmask)+1;
1055 struct mdp_superblock_1 *refsb =
1056 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1058 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1059 sb->level != refsb->level ||
1060 sb->layout != refsb->layout ||
1061 sb->chunksize != refsb->chunksize) {
1062 printk(KERN_WARNING "md: %s has strangely different"
1063 " superblock to %s\n",
1064 bdevname(rdev->bdev,b),
1065 bdevname(refdev->bdev,b2));
1068 ev1 = le64_to_cpu(sb->events);
1069 ev2 = le64_to_cpu(refsb->events);
1077 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
1079 rdev->size = rdev->sb_offset;
1080 if (rdev->size < le64_to_cpu(sb->data_size)/2)
1082 rdev->size = le64_to_cpu(sb->data_size)/2;
1083 if (le32_to_cpu(sb->chunksize))
1084 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
1086 if (le32_to_cpu(sb->size) > rdev->size*2)
1091 static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1093 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1095 rdev->raid_disk = -1;
1097 if (mddev->raid_disks == 0) {
1098 mddev->major_version = 1;
1099 mddev->patch_version = 0;
1100 mddev->persistent = 1;
1101 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
1102 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1103 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1104 mddev->level = le32_to_cpu(sb->level);
1105 mddev->clevel[0] = 0;
1106 mddev->layout = le32_to_cpu(sb->layout);
1107 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1108 mddev->size = le64_to_cpu(sb->size)/2;
1109 mddev->events = le64_to_cpu(sb->events);
1110 mddev->bitmap_offset = 0;
1111 mddev->default_bitmap_offset = 1024 >> 9;
1113 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1114 memcpy(mddev->uuid, sb->set_uuid, 16);
1116 mddev->max_disks = (4096-256)/2;
1118 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
1119 mddev->bitmap_file == NULL ) {
1120 if (mddev->level != 1 && mddev->level != 5 && mddev->level != 6
1121 && mddev->level != 10) {
1122 printk(KERN_WARNING "md: bitmaps not supported for this level.\n");
1125 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
1127 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1128 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1129 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1130 mddev->new_level = le32_to_cpu(sb->new_level);
1131 mddev->new_layout = le32_to_cpu(sb->new_layout);
1132 mddev->new_chunk = le32_to_cpu(sb->new_chunk)<<9;
1134 mddev->reshape_position = MaxSector;
1135 mddev->delta_disks = 0;
1136 mddev->new_level = mddev->level;
1137 mddev->new_layout = mddev->layout;
1138 mddev->new_chunk = mddev->chunk_size;
1141 } else if (mddev->pers == NULL) {
1142 /* Insist of good event counter while assembling */
1143 __u64 ev1 = le64_to_cpu(sb->events);
1145 if (ev1 < mddev->events)
1147 } else if (mddev->bitmap) {
1148 /* If adding to array with a bitmap, then we can accept an
1149 * older device, but not too old.
1151 __u64 ev1 = le64_to_cpu(sb->events);
1152 if (ev1 < mddev->bitmap->events_cleared)
1154 } else /* just a hot-add of a new device, leave raid_disk at -1 */
1157 if (mddev->level != LEVEL_MULTIPATH) {
1159 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1160 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1162 case 0xffff: /* spare */
1164 case 0xfffe: /* faulty */
1165 set_bit(Faulty, &rdev->flags);
1168 set_bit(In_sync, &rdev->flags);
1169 rdev->raid_disk = role;
1172 if (sb->devflags & WriteMostly1)
1173 set_bit(WriteMostly, &rdev->flags);
1174 } else /* MULTIPATH are always insync */
1175 set_bit(In_sync, &rdev->flags);
1180 static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1182 struct mdp_superblock_1 *sb;
1183 struct list_head *tmp;
1186 /* make rdev->sb match mddev and rdev data. */
1188 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1190 sb->feature_map = 0;
1192 memset(sb->pad1, 0, sizeof(sb->pad1));
1193 memset(sb->pad2, 0, sizeof(sb->pad2));
1194 memset(sb->pad3, 0, sizeof(sb->pad3));
1196 sb->utime = cpu_to_le64((__u64)mddev->utime);
1197 sb->events = cpu_to_le64(mddev->events);
1199 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1201 sb->resync_offset = cpu_to_le64(0);
1203 sb->cnt_corrected_read = atomic_read(&rdev->corrected_errors);
1205 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
1206 sb->size = cpu_to_le64(mddev->size<<1);
1208 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1209 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
1210 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
1212 if (mddev->reshape_position != MaxSector) {
1213 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1214 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1215 sb->new_layout = cpu_to_le32(mddev->new_layout);
1216 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1217 sb->new_level = cpu_to_le32(mddev->new_level);
1218 sb->new_chunk = cpu_to_le32(mddev->new_chunk>>9);
1222 ITERATE_RDEV(mddev,rdev2,tmp)
1223 if (rdev2->desc_nr+1 > max_dev)
1224 max_dev = rdev2->desc_nr+1;
1226 sb->max_dev = cpu_to_le32(max_dev);
1227 for (i=0; i<max_dev;i++)
1228 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1230 ITERATE_RDEV(mddev,rdev2,tmp) {
1232 if (test_bit(Faulty, &rdev2->flags))
1233 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1234 else if (test_bit(In_sync, &rdev2->flags))
1235 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1237 sb->dev_roles[i] = cpu_to_le16(0xffff);
1240 sb->recovery_offset = cpu_to_le64(0); /* not supported yet */
1241 sb->sb_csum = calc_sb_1_csum(sb);
1245 static struct super_type super_types[] = {
1248 .owner = THIS_MODULE,
1249 .load_super = super_90_load,
1250 .validate_super = super_90_validate,
1251 .sync_super = super_90_sync,
1255 .owner = THIS_MODULE,
1256 .load_super = super_1_load,
1257 .validate_super = super_1_validate,
1258 .sync_super = super_1_sync,
1262 static mdk_rdev_t * match_dev_unit(mddev_t *mddev, mdk_rdev_t *dev)
1264 struct list_head *tmp;
1267 ITERATE_RDEV(mddev,rdev,tmp)
1268 if (rdev->bdev->bd_contains == dev->bdev->bd_contains)
1274 static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1276 struct list_head *tmp;
1279 ITERATE_RDEV(mddev1,rdev,tmp)
1280 if (match_dev_unit(mddev2, rdev))
1286 static LIST_HEAD(pending_raid_disks);
1288 static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1290 mdk_rdev_t *same_pdev;
1291 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1299 /* make sure rdev->size exceeds mddev->size */
1300 if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) {
1302 /* Cannot change size, so fail */
1305 mddev->size = rdev->size;
1307 same_pdev = match_dev_unit(mddev, rdev);
1310 "%s: WARNING: %s appears to be on the same physical"
1311 " disk as %s. True\n protection against single-disk"
1312 " failure might be compromised.\n",
1313 mdname(mddev), bdevname(rdev->bdev,b),
1314 bdevname(same_pdev->bdev,b2));
1316 /* Verify rdev->desc_nr is unique.
1317 * If it is -1, assign a free number, else
1318 * check number is not in use
1320 if (rdev->desc_nr < 0) {
1322 if (mddev->pers) choice = mddev->raid_disks;
1323 while (find_rdev_nr(mddev, choice))
1325 rdev->desc_nr = choice;
1327 if (find_rdev_nr(mddev, rdev->desc_nr))
1330 bdevname(rdev->bdev,b);
1331 if (kobject_set_name(&rdev->kobj, "dev-%s", b) < 0)
1333 while ( (s=strchr(rdev->kobj.k_name, '/')) != NULL)
1336 list_add(&rdev->same_set, &mddev->disks);
1337 rdev->mddev = mddev;
1338 printk(KERN_INFO "md: bind<%s>\n", b);
1340 rdev->kobj.parent = &mddev->kobj;
1341 kobject_add(&rdev->kobj);
1343 if (rdev->bdev->bd_part)
1344 ko = &rdev->bdev->bd_part->kobj;
1346 ko = &rdev->bdev->bd_disk->kobj;
1347 sysfs_create_link(&rdev->kobj, ko, "block");
1348 bd_claim_by_disk(rdev->bdev, rdev, mddev->gendisk);
1352 static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1354 char b[BDEVNAME_SIZE];
1359 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
1360 list_del_init(&rdev->same_set);
1361 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1363 sysfs_remove_link(&rdev->kobj, "block");
1364 kobject_del(&rdev->kobj);
1368 * prevent the device from being mounted, repartitioned or
1369 * otherwise reused by a RAID array (or any other kernel
1370 * subsystem), by bd_claiming the device.
1372 static int lock_rdev(mdk_rdev_t *rdev, dev_t dev)
1375 struct block_device *bdev;
1376 char b[BDEVNAME_SIZE];
1378 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
1380 printk(KERN_ERR "md: could not open %s.\n",
1381 __bdevname(dev, b));
1382 return PTR_ERR(bdev);
1384 err = bd_claim(bdev, rdev);
1386 printk(KERN_ERR "md: could not bd_claim %s.\n",
1395 static void unlock_rdev(mdk_rdev_t *rdev)
1397 struct block_device *bdev = rdev->bdev;
1405 void md_autodetect_dev(dev_t dev);
1407 static void export_rdev(mdk_rdev_t * rdev)
1409 char b[BDEVNAME_SIZE];
1410 printk(KERN_INFO "md: export_rdev(%s)\n",
1411 bdevname(rdev->bdev,b));
1415 list_del_init(&rdev->same_set);
1417 md_autodetect_dev(rdev->bdev->bd_dev);
1420 kobject_put(&rdev->kobj);
1423 static void kick_rdev_from_array(mdk_rdev_t * rdev)
1425 unbind_rdev_from_array(rdev);
1429 static void export_array(mddev_t *mddev)
1431 struct list_head *tmp;
1434 ITERATE_RDEV(mddev,rdev,tmp) {
1439 kick_rdev_from_array(rdev);
1441 if (!list_empty(&mddev->disks))
1443 mddev->raid_disks = 0;
1444 mddev->major_version = 0;
1447 static void print_desc(mdp_disk_t *desc)
1449 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1450 desc->major,desc->minor,desc->raid_disk,desc->state);
1453 static void print_sb(mdp_super_t *sb)
1458 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1459 sb->major_version, sb->minor_version, sb->patch_version,
1460 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1462 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1463 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1464 sb->md_minor, sb->layout, sb->chunk_size);
1465 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1466 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1467 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1468 sb->failed_disks, sb->spare_disks,
1469 sb->sb_csum, (unsigned long)sb->events_lo);
1472 for (i = 0; i < MD_SB_DISKS; i++) {
1475 desc = sb->disks + i;
1476 if (desc->number || desc->major || desc->minor ||
1477 desc->raid_disk || (desc->state && (desc->state != 4))) {
1478 printk(" D %2d: ", i);
1482 printk(KERN_INFO "md: THIS: ");
1483 print_desc(&sb->this_disk);
1487 static void print_rdev(mdk_rdev_t *rdev)
1489 char b[BDEVNAME_SIZE];
1490 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1491 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
1492 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1494 if (rdev->sb_loaded) {
1495 printk(KERN_INFO "md: rdev superblock:\n");
1496 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1498 printk(KERN_INFO "md: no rdev superblock!\n");
1501 void md_print_devices(void)
1503 struct list_head *tmp, *tmp2;
1506 char b[BDEVNAME_SIZE];
1509 printk("md: **********************************\n");
1510 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1511 printk("md: **********************************\n");
1512 ITERATE_MDDEV(mddev,tmp) {
1515 bitmap_print_sb(mddev->bitmap);
1517 printk("%s: ", mdname(mddev));
1518 ITERATE_RDEV(mddev,rdev,tmp2)
1519 printk("<%s>", bdevname(rdev->bdev,b));
1522 ITERATE_RDEV(mddev,rdev,tmp2)
1525 printk("md: **********************************\n");
1530 static void sync_sbs(mddev_t * mddev)
1533 struct list_head *tmp;
1535 ITERATE_RDEV(mddev,rdev,tmp) {
1536 super_types[mddev->major_version].
1537 sync_super(mddev, rdev);
1538 rdev->sb_loaded = 1;
1542 void md_update_sb(mddev_t * mddev)
1545 struct list_head *tmp;
1550 spin_lock_irq(&mddev->write_lock);
1551 sync_req = mddev->in_sync;
1552 mddev->utime = get_seconds();
1555 if (!mddev->events) {
1557 * oops, this 64-bit counter should never wrap.
1558 * Either we are in around ~1 trillion A.C., assuming
1559 * 1 reboot per second, or we have a bug:
1564 mddev->sb_dirty = 2;
1568 * do not write anything to disk if using
1569 * nonpersistent superblocks
1571 if (!mddev->persistent) {
1572 mddev->sb_dirty = 0;
1573 spin_unlock_irq(&mddev->write_lock);
1574 wake_up(&mddev->sb_wait);
1577 spin_unlock_irq(&mddev->write_lock);
1580 "md: updating %s RAID superblock on device (in sync %d)\n",
1581 mdname(mddev),mddev->in_sync);
1583 err = bitmap_update_sb(mddev->bitmap);
1584 ITERATE_RDEV(mddev,rdev,tmp) {
1585 char b[BDEVNAME_SIZE];
1586 dprintk(KERN_INFO "md: ");
1587 if (test_bit(Faulty, &rdev->flags))
1588 dprintk("(skipping faulty ");
1590 dprintk("%s ", bdevname(rdev->bdev,b));
1591 if (!test_bit(Faulty, &rdev->flags)) {
1592 md_super_write(mddev,rdev,
1593 rdev->sb_offset<<1, rdev->sb_size,
1595 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1596 bdevname(rdev->bdev,b),
1597 (unsigned long long)rdev->sb_offset);
1601 if (mddev->level == LEVEL_MULTIPATH)
1602 /* only need to write one superblock... */
1605 md_super_wait(mddev);
1606 /* if there was a failure, sb_dirty was set to 1, and we re-write super */
1608 spin_lock_irq(&mddev->write_lock);
1609 if (mddev->in_sync != sync_req|| mddev->sb_dirty == 1) {
1610 /* have to write it out again */
1611 spin_unlock_irq(&mddev->write_lock);
1614 mddev->sb_dirty = 0;
1615 spin_unlock_irq(&mddev->write_lock);
1616 wake_up(&mddev->sb_wait);
1619 EXPORT_SYMBOL_GPL(md_update_sb);
1621 /* words written to sysfs files may, or my not, be \n terminated.
1622 * We want to accept with case. For this we use cmd_match.
1624 static int cmd_match(const char *cmd, const char *str)
1626 /* See if cmd, written into a sysfs file, matches
1627 * str. They must either be the same, or cmd can
1628 * have a trailing newline
1630 while (*cmd && *str && *cmd == *str) {
1641 struct rdev_sysfs_entry {
1642 struct attribute attr;
1643 ssize_t (*show)(mdk_rdev_t *, char *);
1644 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1648 state_show(mdk_rdev_t *rdev, char *page)
1653 if (test_bit(Faulty, &rdev->flags)) {
1654 len+= sprintf(page+len, "%sfaulty",sep);
1657 if (test_bit(In_sync, &rdev->flags)) {
1658 len += sprintf(page+len, "%sin_sync",sep);
1661 if (!test_bit(Faulty, &rdev->flags) &&
1662 !test_bit(In_sync, &rdev->flags)) {
1663 len += sprintf(page+len, "%sspare", sep);
1666 return len+sprintf(page+len, "\n");
1669 static struct rdev_sysfs_entry
1670 rdev_state = __ATTR_RO(state);
1673 super_show(mdk_rdev_t *rdev, char *page)
1675 if (rdev->sb_loaded && rdev->sb_size) {
1676 memcpy(page, page_address(rdev->sb_page), rdev->sb_size);
1677 return rdev->sb_size;
1681 static struct rdev_sysfs_entry rdev_super = __ATTR_RO(super);
1684 errors_show(mdk_rdev_t *rdev, char *page)
1686 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
1690 errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1693 unsigned long n = simple_strtoul(buf, &e, 10);
1694 if (*buf && (*e == 0 || *e == '\n')) {
1695 atomic_set(&rdev->corrected_errors, n);
1700 static struct rdev_sysfs_entry rdev_errors =
1701 __ATTR(errors, 0644, errors_show, errors_store);
1704 slot_show(mdk_rdev_t *rdev, char *page)
1706 if (rdev->raid_disk < 0)
1707 return sprintf(page, "none\n");
1709 return sprintf(page, "%d\n", rdev->raid_disk);
1713 slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1716 int slot = simple_strtoul(buf, &e, 10);
1717 if (strncmp(buf, "none", 4)==0)
1719 else if (e==buf || (*e && *e!= '\n'))
1721 if (rdev->mddev->pers)
1722 /* Cannot set slot in active array (yet) */
1724 if (slot >= rdev->mddev->raid_disks)
1726 rdev->raid_disk = slot;
1727 /* assume it is working */
1729 set_bit(In_sync, &rdev->flags);
1734 static struct rdev_sysfs_entry rdev_slot =
1735 __ATTR(slot, 0644, slot_show, slot_store);
1738 offset_show(mdk_rdev_t *rdev, char *page)
1740 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
1744 offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1747 unsigned long long offset = simple_strtoull(buf, &e, 10);
1748 if (e==buf || (*e && *e != '\n'))
1750 if (rdev->mddev->pers)
1752 rdev->data_offset = offset;
1756 static struct rdev_sysfs_entry rdev_offset =
1757 __ATTR(offset, 0644, offset_show, offset_store);
1760 rdev_size_show(mdk_rdev_t *rdev, char *page)
1762 return sprintf(page, "%llu\n", (unsigned long long)rdev->size);
1766 rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1769 unsigned long long size = simple_strtoull(buf, &e, 10);
1770 if (e==buf || (*e && *e != '\n'))
1772 if (rdev->mddev->pers)
1775 if (size < rdev->mddev->size || rdev->mddev->size == 0)
1776 rdev->mddev->size = size;
1780 static struct rdev_sysfs_entry rdev_size =
1781 __ATTR(size, 0644, rdev_size_show, rdev_size_store);
1783 static struct attribute *rdev_default_attrs[] = {
1793 rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
1795 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1796 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1800 return entry->show(rdev, page);
1804 rdev_attr_store(struct kobject *kobj, struct attribute *attr,
1805 const char *page, size_t length)
1807 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1808 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1812 return entry->store(rdev, page, length);
1815 static void rdev_free(struct kobject *ko)
1817 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
1820 static struct sysfs_ops rdev_sysfs_ops = {
1821 .show = rdev_attr_show,
1822 .store = rdev_attr_store,
1824 static struct kobj_type rdev_ktype = {
1825 .release = rdev_free,
1826 .sysfs_ops = &rdev_sysfs_ops,
1827 .default_attrs = rdev_default_attrs,
1831 * Import a device. If 'super_format' >= 0, then sanity check the superblock
1833 * mark the device faulty if:
1835 * - the device is nonexistent (zero size)
1836 * - the device has no valid superblock
1838 * a faulty rdev _never_ has rdev->sb set.
1840 static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
1842 char b[BDEVNAME_SIZE];
1847 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
1849 printk(KERN_ERR "md: could not alloc mem for new device!\n");
1850 return ERR_PTR(-ENOMEM);
1853 if ((err = alloc_disk_sb(rdev)))
1856 err = lock_rdev(rdev, newdev);
1860 rdev->kobj.parent = NULL;
1861 rdev->kobj.ktype = &rdev_ktype;
1862 kobject_init(&rdev->kobj);
1866 rdev->data_offset = 0;
1867 atomic_set(&rdev->nr_pending, 0);
1868 atomic_set(&rdev->read_errors, 0);
1869 atomic_set(&rdev->corrected_errors, 0);
1871 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
1874 "md: %s has zero or unknown size, marking faulty!\n",
1875 bdevname(rdev->bdev,b));
1880 if (super_format >= 0) {
1881 err = super_types[super_format].
1882 load_super(rdev, NULL, super_minor);
1883 if (err == -EINVAL) {
1885 "md: %s has invalid sb, not importing!\n",
1886 bdevname(rdev->bdev,b));
1891 "md: could not read %s's sb, not importing!\n",
1892 bdevname(rdev->bdev,b));
1896 INIT_LIST_HEAD(&rdev->same_set);
1901 if (rdev->sb_page) {
1907 return ERR_PTR(err);
1911 * Check a full RAID array for plausibility
1915 static void analyze_sbs(mddev_t * mddev)
1918 struct list_head *tmp;
1919 mdk_rdev_t *rdev, *freshest;
1920 char b[BDEVNAME_SIZE];
1923 ITERATE_RDEV(mddev,rdev,tmp)
1924 switch (super_types[mddev->major_version].
1925 load_super(rdev, freshest, mddev->minor_version)) {
1933 "md: fatal superblock inconsistency in %s"
1934 " -- removing from array\n",
1935 bdevname(rdev->bdev,b));
1936 kick_rdev_from_array(rdev);
1940 super_types[mddev->major_version].
1941 validate_super(mddev, freshest);
1944 ITERATE_RDEV(mddev,rdev,tmp) {
1945 if (rdev != freshest)
1946 if (super_types[mddev->major_version].
1947 validate_super(mddev, rdev)) {
1948 printk(KERN_WARNING "md: kicking non-fresh %s"
1950 bdevname(rdev->bdev,b));
1951 kick_rdev_from_array(rdev);
1954 if (mddev->level == LEVEL_MULTIPATH) {
1955 rdev->desc_nr = i++;
1956 rdev->raid_disk = rdev->desc_nr;
1957 set_bit(In_sync, &rdev->flags);
1963 if (mddev->recovery_cp != MaxSector &&
1965 printk(KERN_ERR "md: %s: raid array is not clean"
1966 " -- starting background reconstruction\n",
1972 level_show(mddev_t *mddev, char *page)
1974 struct mdk_personality *p = mddev->pers;
1976 return sprintf(page, "%s\n", p->name);
1977 else if (mddev->clevel[0])
1978 return sprintf(page, "%s\n", mddev->clevel);
1979 else if (mddev->level != LEVEL_NONE)
1980 return sprintf(page, "%d\n", mddev->level);
1986 level_store(mddev_t *mddev, const char *buf, size_t len)
1993 if (len >= sizeof(mddev->clevel))
1995 strncpy(mddev->clevel, buf, len);
1996 if (mddev->clevel[len-1] == '\n')
1998 mddev->clevel[len] = 0;
1999 mddev->level = LEVEL_NONE;
2003 static struct md_sysfs_entry md_level =
2004 __ATTR(level, 0644, level_show, level_store);
2007 raid_disks_show(mddev_t *mddev, char *page)
2009 if (mddev->raid_disks == 0)
2011 return sprintf(page, "%d\n", mddev->raid_disks);
2014 static int update_raid_disks(mddev_t *mddev, int raid_disks);
2017 raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2019 /* can only set raid_disks if array is not yet active */
2022 unsigned long n = simple_strtoul(buf, &e, 10);
2024 if (!*buf || (*e && *e != '\n'))
2028 rv = update_raid_disks(mddev, n);
2030 mddev->raid_disks = n;
2031 return rv ? rv : len;
2033 static struct md_sysfs_entry md_raid_disks =
2034 __ATTR(raid_disks, 0644, raid_disks_show, raid_disks_store);
2037 chunk_size_show(mddev_t *mddev, char *page)
2039 return sprintf(page, "%d\n", mddev->chunk_size);
2043 chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2045 /* can only set chunk_size if array is not yet active */
2047 unsigned long n = simple_strtoul(buf, &e, 10);
2051 if (!*buf || (*e && *e != '\n'))
2054 mddev->chunk_size = n;
2057 static struct md_sysfs_entry md_chunk_size =
2058 __ATTR(chunk_size, 0644, chunk_size_show, chunk_size_store);
2061 null_show(mddev_t *mddev, char *page)
2067 new_dev_store(mddev_t *mddev, const char *buf, size_t len)
2069 /* buf must be %d:%d\n? giving major and minor numbers */
2070 /* The new device is added to the array.
2071 * If the array has a persistent superblock, we read the
2072 * superblock to initialise info and check validity.
2073 * Otherwise, only checking done is that in bind_rdev_to_array,
2074 * which mainly checks size.
2077 int major = simple_strtoul(buf, &e, 10);
2083 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
2085 minor = simple_strtoul(e+1, &e, 10);
2086 if (*e && *e != '\n')
2088 dev = MKDEV(major, minor);
2089 if (major != MAJOR(dev) ||
2090 minor != MINOR(dev))
2094 if (mddev->persistent) {
2095 rdev = md_import_device(dev, mddev->major_version,
2096 mddev->minor_version);
2097 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
2098 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2099 mdk_rdev_t, same_set);
2100 err = super_types[mddev->major_version]
2101 .load_super(rdev, rdev0, mddev->minor_version);
2106 rdev = md_import_device(dev, -1, -1);
2109 return PTR_ERR(rdev);
2110 err = bind_rdev_to_array(rdev, mddev);
2114 return err ? err : len;
2117 static struct md_sysfs_entry md_new_device =
2118 __ATTR(new_dev, 0200, null_show, new_dev_store);
2121 size_show(mddev_t *mddev, char *page)
2123 return sprintf(page, "%llu\n", (unsigned long long)mddev->size);
2126 static int update_size(mddev_t *mddev, unsigned long size);
2129 size_store(mddev_t *mddev, const char *buf, size_t len)
2131 /* If array is inactive, we can reduce the component size, but
2132 * not increase it (except from 0).
2133 * If array is active, we can try an on-line resize
2137 unsigned long long size = simple_strtoull(buf, &e, 10);
2138 if (!*buf || *buf == '\n' ||
2143 err = update_size(mddev, size);
2144 md_update_sb(mddev);
2146 if (mddev->size == 0 ||
2152 return err ? err : len;
2155 static struct md_sysfs_entry md_size =
2156 __ATTR(component_size, 0644, size_show, size_store);
2160 * This is either 'none' for arrays with externally managed metadata,
2161 * or N.M for internally known formats
2164 metadata_show(mddev_t *mddev, char *page)
2166 if (mddev->persistent)
2167 return sprintf(page, "%d.%d\n",
2168 mddev->major_version, mddev->minor_version);
2170 return sprintf(page, "none\n");
2174 metadata_store(mddev_t *mddev, const char *buf, size_t len)
2178 if (!list_empty(&mddev->disks))
2181 if (cmd_match(buf, "none")) {
2182 mddev->persistent = 0;
2183 mddev->major_version = 0;
2184 mddev->minor_version = 90;
2187 major = simple_strtoul(buf, &e, 10);
2188 if (e==buf || *e != '.')
2191 minor = simple_strtoul(buf, &e, 10);
2192 if (e==buf || *e != '\n')
2194 if (major >= sizeof(super_types)/sizeof(super_types[0]) ||
2195 super_types[major].name == NULL)
2197 mddev->major_version = major;
2198 mddev->minor_version = minor;
2199 mddev->persistent = 1;
2203 static struct md_sysfs_entry md_metadata =
2204 __ATTR(metadata_version, 0644, metadata_show, metadata_store);
2207 action_show(mddev_t *mddev, char *page)
2209 char *type = "idle";
2210 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2211 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) {
2212 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2214 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2215 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2217 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2224 return sprintf(page, "%s\n", type);
2228 action_store(mddev_t *mddev, const char *page, size_t len)
2230 if (!mddev->pers || !mddev->pers->sync_request)
2233 if (cmd_match(page, "idle")) {
2234 if (mddev->sync_thread) {
2235 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2236 md_unregister_thread(mddev->sync_thread);
2237 mddev->sync_thread = NULL;
2238 mddev->recovery = 0;
2240 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2241 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
2243 else if (cmd_match(page, "resync") || cmd_match(page, "recover"))
2244 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2245 else if (cmd_match(page, "reshape")) {
2247 if (mddev->pers->start_reshape == NULL)
2249 err = mddev->pers->start_reshape(mddev);
2253 if (cmd_match(page, "check"))
2254 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
2255 else if (!cmd_match(page, "repair"))
2257 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
2258 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
2260 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2261 md_wakeup_thread(mddev->thread);
2266 mismatch_cnt_show(mddev_t *mddev, char *page)
2268 return sprintf(page, "%llu\n",
2269 (unsigned long long) mddev->resync_mismatches);
2272 static struct md_sysfs_entry
2273 md_scan_mode = __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
2276 static struct md_sysfs_entry
2277 md_mismatches = __ATTR_RO(mismatch_cnt);
2280 sync_min_show(mddev_t *mddev, char *page)
2282 return sprintf(page, "%d (%s)\n", speed_min(mddev),
2283 mddev->sync_speed_min ? "local": "system");
2287 sync_min_store(mddev_t *mddev, const char *buf, size_t len)
2291 if (strncmp(buf, "system", 6)==0) {
2292 mddev->sync_speed_min = 0;
2295 min = simple_strtoul(buf, &e, 10);
2296 if (buf == e || (*e && *e != '\n') || min <= 0)
2298 mddev->sync_speed_min = min;
2302 static struct md_sysfs_entry md_sync_min =
2303 __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
2306 sync_max_show(mddev_t *mddev, char *page)
2308 return sprintf(page, "%d (%s)\n", speed_max(mddev),
2309 mddev->sync_speed_max ? "local": "system");
2313 sync_max_store(mddev_t *mddev, const char *buf, size_t len)
2317 if (strncmp(buf, "system", 6)==0) {
2318 mddev->sync_speed_max = 0;
2321 max = simple_strtoul(buf, &e, 10);
2322 if (buf == e || (*e && *e != '\n') || max <= 0)
2324 mddev->sync_speed_max = max;
2328 static struct md_sysfs_entry md_sync_max =
2329 __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
2333 sync_speed_show(mddev_t *mddev, char *page)
2335 unsigned long resync, dt, db;
2336 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
2337 dt = ((jiffies - mddev->resync_mark) / HZ);
2339 db = resync - (mddev->resync_mark_cnt);
2340 return sprintf(page, "%ld\n", db/dt/2); /* K/sec */
2343 static struct md_sysfs_entry
2344 md_sync_speed = __ATTR_RO(sync_speed);
2347 sync_completed_show(mddev_t *mddev, char *page)
2349 unsigned long max_blocks, resync;
2351 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2352 max_blocks = mddev->resync_max_sectors;
2354 max_blocks = mddev->size << 1;
2356 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
2357 return sprintf(page, "%lu / %lu\n", resync, max_blocks);
2360 static struct md_sysfs_entry
2361 md_sync_completed = __ATTR_RO(sync_completed);
2364 suspend_lo_show(mddev_t *mddev, char *page)
2366 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
2370 suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
2373 unsigned long long new = simple_strtoull(buf, &e, 10);
2375 if (mddev->pers->quiesce == NULL)
2377 if (buf == e || (*e && *e != '\n'))
2379 if (new >= mddev->suspend_hi ||
2380 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
2381 mddev->suspend_lo = new;
2382 mddev->pers->quiesce(mddev, 2);
2387 static struct md_sysfs_entry md_suspend_lo =
2388 __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
2392 suspend_hi_show(mddev_t *mddev, char *page)
2394 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
2398 suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
2401 unsigned long long new = simple_strtoull(buf, &e, 10);
2403 if (mddev->pers->quiesce == NULL)
2405 if (buf == e || (*e && *e != '\n'))
2407 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
2408 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
2409 mddev->suspend_hi = new;
2410 mddev->pers->quiesce(mddev, 1);
2411 mddev->pers->quiesce(mddev, 0);
2416 static struct md_sysfs_entry md_suspend_hi =
2417 __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
2420 static struct attribute *md_default_attrs[] = {
2422 &md_raid_disks.attr,
2423 &md_chunk_size.attr,
2426 &md_new_device.attr,
2430 static struct attribute *md_redundancy_attrs[] = {
2432 &md_mismatches.attr,
2435 &md_sync_speed.attr,
2436 &md_sync_completed.attr,
2437 &md_suspend_lo.attr,
2438 &md_suspend_hi.attr,
2441 static struct attribute_group md_redundancy_group = {
2443 .attrs = md_redundancy_attrs,
2448 md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2450 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
2451 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
2456 rv = mddev_lock(mddev);
2458 rv = entry->show(mddev, page);
2459 mddev_unlock(mddev);
2465 md_attr_store(struct kobject *kobj, struct attribute *attr,
2466 const char *page, size_t length)
2468 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
2469 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
2474 rv = mddev_lock(mddev);
2476 rv = entry->store(mddev, page, length);
2477 mddev_unlock(mddev);
2482 static void md_free(struct kobject *ko)
2484 mddev_t *mddev = container_of(ko, mddev_t, kobj);
2488 static struct sysfs_ops md_sysfs_ops = {
2489 .show = md_attr_show,
2490 .store = md_attr_store,
2492 static struct kobj_type md_ktype = {
2494 .sysfs_ops = &md_sysfs_ops,
2495 .default_attrs = md_default_attrs,
2500 static struct kobject *md_probe(dev_t dev, int *part, void *data)
2502 static DEFINE_MUTEX(disks_mutex);
2503 mddev_t *mddev = mddev_find(dev);
2504 struct gendisk *disk;
2505 int partitioned = (MAJOR(dev) != MD_MAJOR);
2506 int shift = partitioned ? MdpMinorShift : 0;
2507 int unit = MINOR(dev) >> shift;
2512 mutex_lock(&disks_mutex);
2513 if (mddev->gendisk) {
2514 mutex_unlock(&disks_mutex);
2518 disk = alloc_disk(1 << shift);
2520 mutex_unlock(&disks_mutex);
2524 disk->major = MAJOR(dev);
2525 disk->first_minor = unit << shift;
2527 sprintf(disk->disk_name, "md_d%d", unit);
2528 sprintf(disk->devfs_name, "md/d%d", unit);
2530 sprintf(disk->disk_name, "md%d", unit);
2531 sprintf(disk->devfs_name, "md/%d", unit);
2533 disk->fops = &md_fops;
2534 disk->private_data = mddev;
2535 disk->queue = mddev->queue;
2537 mddev->gendisk = disk;
2538 mutex_unlock(&disks_mutex);
2539 mddev->kobj.parent = &disk->kobj;
2540 mddev->kobj.k_name = NULL;
2541 snprintf(mddev->kobj.name, KOBJ_NAME_LEN, "%s", "md");
2542 mddev->kobj.ktype = &md_ktype;
2543 kobject_register(&mddev->kobj);
2547 void md_wakeup_thread(mdk_thread_t *thread);
2549 static void md_safemode_timeout(unsigned long data)
2551 mddev_t *mddev = (mddev_t *) data;
2553 mddev->safemode = 1;
2554 md_wakeup_thread(mddev->thread);
2557 static int start_dirty_degraded;
2559 static int do_md_run(mddev_t * mddev)
2563 struct list_head *tmp;
2565 struct gendisk *disk;
2566 struct mdk_personality *pers;
2567 char b[BDEVNAME_SIZE];
2569 if (list_empty(&mddev->disks))
2570 /* cannot run an array with no devices.. */
2577 * Analyze all RAID superblock(s)
2579 if (!mddev->raid_disks)
2582 chunk_size = mddev->chunk_size;
2585 if (chunk_size > MAX_CHUNK_SIZE) {
2586 printk(KERN_ERR "too big chunk_size: %d > %d\n",
2587 chunk_size, MAX_CHUNK_SIZE);
2591 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
2593 if ( (1 << ffz(~chunk_size)) != chunk_size) {
2594 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
2597 if (chunk_size < PAGE_SIZE) {
2598 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
2599 chunk_size, PAGE_SIZE);
2603 /* devices must have minimum size of one chunk */
2604 ITERATE_RDEV(mddev,rdev,tmp) {
2605 if (test_bit(Faulty, &rdev->flags))
2607 if (rdev->size < chunk_size / 1024) {
2609 "md: Dev %s smaller than chunk_size:"
2611 bdevname(rdev->bdev,b),
2612 (unsigned long long)rdev->size,
2620 if (mddev->level != LEVEL_NONE)
2621 request_module("md-level-%d", mddev->level);
2622 else if (mddev->clevel[0])
2623 request_module("md-%s", mddev->clevel);
2627 * Drop all container device buffers, from now on
2628 * the only valid external interface is through the md
2630 * Also find largest hardsector size
2632 ITERATE_RDEV(mddev,rdev,tmp) {
2633 if (test_bit(Faulty, &rdev->flags))
2635 sync_blockdev(rdev->bdev);
2636 invalidate_bdev(rdev->bdev, 0);
2639 md_probe(mddev->unit, NULL, NULL);
2640 disk = mddev->gendisk;
2644 spin_lock(&pers_lock);
2645 pers = find_pers(mddev->level, mddev->clevel);
2646 if (!pers || !try_module_get(pers->owner)) {
2647 spin_unlock(&pers_lock);
2648 if (mddev->level != LEVEL_NONE)
2649 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
2652 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
2657 spin_unlock(&pers_lock);
2658 mddev->level = pers->level;
2659 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
2661 if (mddev->reshape_position != MaxSector &&
2662 pers->start_reshape == NULL) {
2663 /* This personality cannot handle reshaping... */
2665 module_put(pers->owner);
2669 mddev->recovery = 0;
2670 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
2671 mddev->barriers_work = 1;
2672 mddev->ok_start_degraded = start_dirty_degraded;
2675 mddev->ro = 2; /* read-only, but switch on first write */
2677 err = mddev->pers->run(mddev);
2678 if (!err && mddev->pers->sync_request) {
2679 err = bitmap_create(mddev);
2681 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
2682 mdname(mddev), err);
2683 mddev->pers->stop(mddev);
2687 printk(KERN_ERR "md: pers->run() failed ...\n");
2688 module_put(mddev->pers->owner);
2690 bitmap_destroy(mddev);
2693 if (mddev->pers->sync_request)
2694 sysfs_create_group(&mddev->kobj, &md_redundancy_group);
2695 else if (mddev->ro == 2) /* auto-readonly not meaningful */
2698 atomic_set(&mddev->writes_pending,0);
2699 mddev->safemode = 0;
2700 mddev->safemode_timer.function = md_safemode_timeout;
2701 mddev->safemode_timer.data = (unsigned long) mddev;
2702 mddev->safemode_delay = (20 * HZ)/1000 +1; /* 20 msec delay */
2705 ITERATE_RDEV(mddev,rdev,tmp)
2706 if (rdev->raid_disk >= 0) {
2708 sprintf(nm, "rd%d", rdev->raid_disk);
2709 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
2712 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2713 md_wakeup_thread(mddev->thread);
2715 if (mddev->sb_dirty)
2716 md_update_sb(mddev);
2718 set_capacity(disk, mddev->array_size<<1);
2720 /* If we call blk_queue_make_request here, it will
2721 * re-initialise max_sectors etc which may have been
2722 * refined inside -> run. So just set the bits we need to set.
2723 * Most initialisation happended when we called
2724 * blk_queue_make_request(..., md_fail_request)
2727 mddev->queue->queuedata = mddev;
2728 mddev->queue->make_request_fn = mddev->pers->make_request;
2731 md_new_event(mddev);
2735 static int restart_array(mddev_t *mddev)
2737 struct gendisk *disk = mddev->gendisk;
2741 * Complain if it has no devices
2744 if (list_empty(&mddev->disks))
2752 mddev->safemode = 0;
2754 set_disk_ro(disk, 0);
2756 printk(KERN_INFO "md: %s switched to read-write mode.\n",
2759 * Kick recovery or resync if necessary
2761 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2762 md_wakeup_thread(mddev->thread);
2765 printk(KERN_ERR "md: %s has no personality assigned.\n",
2774 static int do_md_stop(mddev_t * mddev, int ro)
2777 struct gendisk *disk = mddev->gendisk;
2780 if (atomic_read(&mddev->active)>2) {
2781 printk("md: %s still in use.\n",mdname(mddev));
2785 if (mddev->sync_thread) {
2786 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2787 md_unregister_thread(mddev->sync_thread);
2788 mddev->sync_thread = NULL;
2791 del_timer_sync(&mddev->safemode_timer);
2793 invalidate_partition(disk, 0);
2801 bitmap_flush(mddev);
2802 md_super_wait(mddev);
2804 set_disk_ro(disk, 0);
2805 blk_queue_make_request(mddev->queue, md_fail_request);
2806 mddev->pers->stop(mddev);
2807 if (mddev->pers->sync_request)
2808 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
2810 module_put(mddev->pers->owner);
2815 if (!mddev->in_sync) {
2816 /* mark array as shutdown cleanly */
2818 md_update_sb(mddev);
2821 set_disk_ro(disk, 1);
2825 * Free resources if final stop
2829 struct list_head *tmp;
2830 struct gendisk *disk;
2831 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
2833 bitmap_destroy(mddev);
2834 if (mddev->bitmap_file) {
2835 atomic_set(&mddev->bitmap_file->f_dentry->d_inode->i_writecount, 1);
2836 fput(mddev->bitmap_file);
2837 mddev->bitmap_file = NULL;
2839 mddev->bitmap_offset = 0;
2841 ITERATE_RDEV(mddev,rdev,tmp)
2842 if (rdev->raid_disk >= 0) {
2844 sprintf(nm, "rd%d", rdev->raid_disk);
2845 sysfs_remove_link(&mddev->kobj, nm);
2848 export_array(mddev);
2850 mddev->array_size = 0;
2851 disk = mddev->gendisk;
2853 set_capacity(disk, 0);
2856 printk(KERN_INFO "md: %s switched to read-only mode.\n",
2859 md_new_event(mddev);
2864 static void autorun_array(mddev_t *mddev)
2867 struct list_head *tmp;
2870 if (list_empty(&mddev->disks))
2873 printk(KERN_INFO "md: running: ");
2875 ITERATE_RDEV(mddev,rdev,tmp) {
2876 char b[BDEVNAME_SIZE];
2877 printk("<%s>", bdevname(rdev->bdev,b));
2881 err = do_md_run (mddev);
2883 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
2884 do_md_stop (mddev, 0);
2889 * lets try to run arrays based on all disks that have arrived
2890 * until now. (those are in pending_raid_disks)
2892 * the method: pick the first pending disk, collect all disks with
2893 * the same UUID, remove all from the pending list and put them into
2894 * the 'same_array' list. Then order this list based on superblock
2895 * update time (freshest comes first), kick out 'old' disks and
2896 * compare superblocks. If everything's fine then run it.
2898 * If "unit" is allocated, then bump its reference count
2900 static void autorun_devices(int part)
2902 struct list_head *tmp;
2903 mdk_rdev_t *rdev0, *rdev;
2905 char b[BDEVNAME_SIZE];
2907 printk(KERN_INFO "md: autorun ...\n");
2908 while (!list_empty(&pending_raid_disks)) {
2910 LIST_HEAD(candidates);
2911 rdev0 = list_entry(pending_raid_disks.next,
2912 mdk_rdev_t, same_set);
2914 printk(KERN_INFO "md: considering %s ...\n",
2915 bdevname(rdev0->bdev,b));
2916 INIT_LIST_HEAD(&candidates);
2917 ITERATE_RDEV_PENDING(rdev,tmp)
2918 if (super_90_load(rdev, rdev0, 0) >= 0) {
2919 printk(KERN_INFO "md: adding %s ...\n",
2920 bdevname(rdev->bdev,b));
2921 list_move(&rdev->same_set, &candidates);
2924 * now we have a set of devices, with all of them having
2925 * mostly sane superblocks. It's time to allocate the
2928 if (rdev0->preferred_minor < 0 || rdev0->preferred_minor >= MAX_MD_DEVS) {
2929 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
2930 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
2934 dev = MKDEV(mdp_major,
2935 rdev0->preferred_minor << MdpMinorShift);
2937 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
2939 md_probe(dev, NULL, NULL);
2940 mddev = mddev_find(dev);
2943 "md: cannot allocate memory for md drive.\n");
2946 if (mddev_lock(mddev))
2947 printk(KERN_WARNING "md: %s locked, cannot run\n",
2949 else if (mddev->raid_disks || mddev->major_version
2950 || !list_empty(&mddev->disks)) {
2952 "md: %s already running, cannot run %s\n",
2953 mdname(mddev), bdevname(rdev0->bdev,b));
2954 mddev_unlock(mddev);
2956 printk(KERN_INFO "md: created %s\n", mdname(mddev));
2957 ITERATE_RDEV_GENERIC(candidates,rdev,tmp) {
2958 list_del_init(&rdev->same_set);
2959 if (bind_rdev_to_array(rdev, mddev))
2962 autorun_array(mddev);
2963 mddev_unlock(mddev);
2965 /* on success, candidates will be empty, on error
2968 ITERATE_RDEV_GENERIC(candidates,rdev,tmp)
2972 printk(KERN_INFO "md: ... autorun DONE.\n");
2976 * import RAID devices based on one partition
2977 * if possible, the array gets run as well.
2980 static int autostart_array(dev_t startdev)
2982 char b[BDEVNAME_SIZE];
2983 int err = -EINVAL, i;
2984 mdp_super_t *sb = NULL;
2985 mdk_rdev_t *start_rdev = NULL, *rdev;
2987 start_rdev = md_import_device(startdev, 0, 0);
2988 if (IS_ERR(start_rdev))
2992 /* NOTE: this can only work for 0.90.0 superblocks */
2993 sb = (mdp_super_t*)page_address(start_rdev->sb_page);
2994 if (sb->major_version != 0 ||
2995 sb->minor_version != 90 ) {
2996 printk(KERN_WARNING "md: can only autostart 0.90.0 arrays\n");
2997 export_rdev(start_rdev);
3001 if (test_bit(Faulty, &start_rdev->flags)) {
3003 "md: can not autostart based on faulty %s!\n",
3004 bdevname(start_rdev->bdev,b));
3005 export_rdev(start_rdev);
3008 list_add(&start_rdev->same_set, &pending_raid_disks);
3010 for (i = 0; i < MD_SB_DISKS; i++) {
3011 mdp_disk_t *desc = sb->disks + i;
3012 dev_t dev = MKDEV(desc->major, desc->minor);
3016 if (dev == startdev)
3018 if (MAJOR(dev) != desc->major || MINOR(dev) != desc->minor)
3020 rdev = md_import_device(dev, 0, 0);
3024 list_add(&rdev->same_set, &pending_raid_disks);
3028 * possibly return codes
3036 static int get_version(void __user * arg)
3040 ver.major = MD_MAJOR_VERSION;
3041 ver.minor = MD_MINOR_VERSION;
3042 ver.patchlevel = MD_PATCHLEVEL_VERSION;
3044 if (copy_to_user(arg, &ver, sizeof(ver)))
3050 static int get_array_info(mddev_t * mddev, void __user * arg)
3052 mdu_array_info_t info;
3053 int nr,working,active,failed,spare;
3055 struct list_head *tmp;
3057 nr=working=active=failed=spare=0;
3058 ITERATE_RDEV(mddev,rdev,tmp) {
3060 if (test_bit(Faulty, &rdev->flags))
3064 if (test_bit(In_sync, &rdev->flags))
3071 info.major_version = mddev->major_version;
3072 info.minor_version = mddev->minor_version;
3073 info.patch_version = MD_PATCHLEVEL_VERSION;
3074 info.ctime = mddev->ctime;
3075 info.level = mddev->level;
3076 info.size = mddev->size;
3077 if (info.size != mddev->size) /* overflow */
3080 info.raid_disks = mddev->raid_disks;
3081 info.md_minor = mddev->md_minor;
3082 info.not_persistent= !mddev->persistent;
3084 info.utime = mddev->utime;
3087 info.state = (1<<MD_SB_CLEAN);
3088 if (mddev->bitmap && mddev->bitmap_offset)
3089 info.state = (1<<MD_SB_BITMAP_PRESENT);
3090 info.active_disks = active;
3091 info.working_disks = working;
3092 info.failed_disks = failed;
3093 info.spare_disks = spare;
3095 info.layout = mddev->layout;
3096 info.chunk_size = mddev->chunk_size;
3098 if (copy_to_user(arg, &info, sizeof(info)))
3104 static int get_bitmap_file(mddev_t * mddev, void __user * arg)
3106 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
3107 char *ptr, *buf = NULL;
3110 file = kmalloc(sizeof(*file), GFP_KERNEL);
3114 /* bitmap disabled, zero the first byte and copy out */
3115 if (!mddev->bitmap || !mddev->bitmap->file) {
3116 file->pathname[0] = '\0';
3120 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
3124 ptr = file_path(mddev->bitmap->file, buf, sizeof(file->pathname));
3128 strcpy(file->pathname, ptr);
3132 if (copy_to_user(arg, file, sizeof(*file)))
3140 static int get_disk_info(mddev_t * mddev, void __user * arg)
3142 mdu_disk_info_t info;
3146 if (copy_from_user(&info, arg, sizeof(info)))
3151 rdev = find_rdev_nr(mddev, nr);
3153 info.major = MAJOR(rdev->bdev->bd_dev);
3154 info.minor = MINOR(rdev->bdev->bd_dev);
3155 info.raid_disk = rdev->raid_disk;
3157 if (test_bit(Faulty, &rdev->flags))
3158 info.state |= (1<<MD_DISK_FAULTY);
3159 else if (test_bit(In_sync, &rdev->flags)) {
3160 info.state |= (1<<MD_DISK_ACTIVE);
3161 info.state |= (1<<MD_DISK_SYNC);
3163 if (test_bit(WriteMostly, &rdev->flags))
3164 info.state |= (1<<MD_DISK_WRITEMOSTLY);
3166 info.major = info.minor = 0;
3167 info.raid_disk = -1;
3168 info.state = (1<<MD_DISK_REMOVED);
3171 if (copy_to_user(arg, &info, sizeof(info)))
3177 static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
3179 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3181 dev_t dev = MKDEV(info->major,info->minor);
3183 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
3186 if (!mddev->raid_disks) {
3188 /* expecting a device which has a superblock */
3189 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
3192 "md: md_import_device returned %ld\n",
3194 return PTR_ERR(rdev);
3196 if (!list_empty(&mddev->disks)) {
3197 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3198 mdk_rdev_t, same_set);
3199 int err = super_types[mddev->major_version]
3200 .load_super(rdev, rdev0, mddev->minor_version);
3203 "md: %s has different UUID to %s\n",
3204 bdevname(rdev->bdev,b),
3205 bdevname(rdev0->bdev,b2));
3210 err = bind_rdev_to_array(rdev, mddev);
3217 * add_new_disk can be used once the array is assembled
3218 * to add "hot spares". They must already have a superblock
3223 if (!mddev->pers->hot_add_disk) {
3225 "%s: personality does not support diskops!\n",
3229 if (mddev->persistent)
3230 rdev = md_import_device(dev, mddev->major_version,
3231 mddev->minor_version);
3233 rdev = md_import_device(dev, -1, -1);
3236 "md: md_import_device returned %ld\n",
3238 return PTR_ERR(rdev);
3240 /* set save_raid_disk if appropriate */
3241 if (!mddev->persistent) {
3242 if (info->state & (1<<MD_DISK_SYNC) &&
3243 info->raid_disk < mddev->raid_disks)
3244 rdev->raid_disk = info->raid_disk;
3246 rdev->raid_disk = -1;
3248 super_types[mddev->major_version].
3249 validate_super(mddev, rdev);
3250 rdev->saved_raid_disk = rdev->raid_disk;
3252 clear_bit(In_sync, &rdev->flags); /* just to be sure */
3253 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
3254 set_bit(WriteMostly, &rdev->flags);
3256 rdev->raid_disk = -1;
3257 err = bind_rdev_to_array(rdev, mddev);
3261 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3262 md_wakeup_thread(mddev->thread);
3266 /* otherwise, add_new_disk is only allowed
3267 * for major_version==0 superblocks
3269 if (mddev->major_version != 0) {
3270 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
3275 if (!(info->state & (1<<MD_DISK_FAULTY))) {
3277 rdev = md_import_device (dev, -1, 0);
3280 "md: error, md_import_device() returned %ld\n",
3282 return PTR_ERR(rdev);
3284 rdev->desc_nr = info->number;
3285 if (info->raid_disk < mddev->raid_disks)
3286 rdev->raid_disk = info->raid_disk;
3288 rdev->raid_disk = -1;
3292 if (rdev->raid_disk < mddev->raid_disks)
3293 if (info->state & (1<<MD_DISK_SYNC))
3294 set_bit(In_sync, &rdev->flags);
3296 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
3297 set_bit(WriteMostly, &rdev->flags);
3299 if (!mddev->persistent) {
3300 printk(KERN_INFO "md: nonpersistent superblock ...\n");
3301 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
3303 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
3304 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
3306 err = bind_rdev_to_array(rdev, mddev);
3316 static int hot_remove_disk(mddev_t * mddev, dev_t dev)
3318 char b[BDEVNAME_SIZE];
3324 rdev = find_rdev(mddev, dev);
3328 if (rdev->raid_disk >= 0)
3331 kick_rdev_from_array(rdev);
3332 md_update_sb(mddev);
3333 md_new_event(mddev);
3337 printk(KERN_WARNING "md: cannot remove active disk %s from %s ... \n",
3338 bdevname(rdev->bdev,b), mdname(mddev));
3342 static int hot_add_disk(mddev_t * mddev, dev_t dev)
3344 char b[BDEVNAME_SIZE];
3352 if (mddev->major_version != 0) {
3353 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
3354 " version-0 superblocks.\n",
3358 if (!mddev->pers->hot_add_disk) {
3360 "%s: personality does not support diskops!\n",
3365 rdev = md_import_device (dev, -1, 0);
3368 "md: error, md_import_device() returned %ld\n",
3373 if (mddev->persistent)
3374 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
3377 rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
3379 size = calc_dev_size(rdev, mddev->chunk_size);
3382 if (test_bit(Faulty, &rdev->flags)) {
3384 "md: can not hot-add faulty %s disk to %s!\n",
3385 bdevname(rdev->bdev,b), mdname(mddev));
3389 clear_bit(In_sync, &rdev->flags);
3391 err = bind_rdev_to_array(rdev, mddev);
3396 * The rest should better be atomic, we can have disk failures
3397 * noticed in interrupt contexts ...
3400 if (rdev->desc_nr == mddev->max_disks) {
3401 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
3404 goto abort_unbind_export;
3407 rdev->raid_disk = -1;
3409 md_update_sb(mddev);
3412 * Kick recovery, maybe this spare has to be added to the
3413 * array immediately.
3415 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3416 md_wakeup_thread(mddev->thread);
3417 md_new_event(mddev);
3420 abort_unbind_export:
3421 unbind_rdev_from_array(rdev);
3428 /* similar to deny_write_access, but accounts for our holding a reference
3429 * to the file ourselves */
3430 static int deny_bitmap_write_access(struct file * file)
3432 struct inode *inode = file->f_mapping->host;
3434 spin_lock(&inode->i_lock);
3435 if (atomic_read(&inode->i_writecount) > 1) {
3436 spin_unlock(&inode->i_lock);
3439 atomic_set(&inode->i_writecount, -1);
3440 spin_unlock(&inode->i_lock);
3445 static int set_bitmap_file(mddev_t *mddev, int fd)
3450 if (!mddev->pers->quiesce)
3452 if (mddev->recovery || mddev->sync_thread)
3454 /* we should be able to change the bitmap.. */
3460 return -EEXIST; /* cannot add when bitmap is present */
3461 mddev->bitmap_file = fget(fd);
3463 if (mddev->bitmap_file == NULL) {
3464 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
3469 err = deny_bitmap_write_access(mddev->bitmap_file);
3471 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
3473 fput(mddev->bitmap_file);
3474 mddev->bitmap_file = NULL;
3477 mddev->bitmap_offset = 0; /* file overrides offset */
3478 } else if (mddev->bitmap == NULL)
3479 return -ENOENT; /* cannot remove what isn't there */
3482 mddev->pers->quiesce(mddev, 1);
3484 err = bitmap_create(mddev);
3486 bitmap_destroy(mddev);
3487 mddev->pers->quiesce(mddev, 0);
3488 } else if (fd < 0) {
3489 if (mddev->bitmap_file)
3490 fput(mddev->bitmap_file);
3491 mddev->bitmap_file = NULL;
3498 * set_array_info is used two different ways
3499 * The original usage is when creating a new array.
3500 * In this usage, raid_disks is > 0 and it together with
3501 * level, size, not_persistent,layout,chunksize determine the
3502 * shape of the array.
3503 * This will always create an array with a type-0.90.0 superblock.
3504 * The newer usage is when assembling an array.
3505 * In this case raid_disks will be 0, and the major_version field is
3506 * use to determine which style super-blocks are to be found on the devices.
3507 * The minor and patch _version numbers are also kept incase the
3508 * super_block handler wishes to interpret them.
3510 static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
3513 if (info->raid_disks == 0) {
3514 /* just setting version number for superblock loading */
3515 if (info->major_version < 0 ||
3516 info->major_version >= sizeof(super_types)/sizeof(super_types[0]) ||
3517 super_types[info->major_version].name == NULL) {
3518 /* maybe try to auto-load a module? */
3520 "md: superblock version %d not known\n",
3521 info->major_version);
3524 mddev->major_version = info->major_version;
3525 mddev->minor_version = info->minor_version;
3526 mddev->patch_version = info->patch_version;
3529 mddev->major_version = MD_MAJOR_VERSION;
3530 mddev->minor_version = MD_MINOR_VERSION;
3531 mddev->patch_version = MD_PATCHLEVEL_VERSION;
3532 mddev->ctime = get_seconds();
3534 mddev->level = info->level;
3535 mddev->clevel[0] = 0;
3536 mddev->size = info->size;
3537 mddev->raid_disks = info->raid_disks;
3538 /* don't set md_minor, it is determined by which /dev/md* was
3541 if (info->state & (1<<MD_SB_CLEAN))
3542 mddev->recovery_cp = MaxSector;
3544 mddev->recovery_cp = 0;
3545 mddev->persistent = ! info->not_persistent;
3547 mddev->layout = info->layout;
3548 mddev->chunk_size = info->chunk_size;
3550 mddev->max_disks = MD_SB_DISKS;
3552 mddev->sb_dirty = 1;
3554 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
3555 mddev->bitmap_offset = 0;
3557 mddev->reshape_position = MaxSector;
3560 * Generate a 128 bit UUID
3562 get_random_bytes(mddev->uuid, 16);
3564 mddev->new_level = mddev->level;
3565 mddev->new_chunk = mddev->chunk_size;
3566 mddev->new_layout = mddev->layout;
3567 mddev->delta_disks = 0;
3572 static int update_size(mddev_t *mddev, unsigned long size)
3576 struct list_head *tmp;
3577 int fit = (size == 0);
3579 if (mddev->pers->resize == NULL)
3581 /* The "size" is the amount of each device that is used.
3582 * This can only make sense for arrays with redundancy.
3583 * linear and raid0 always use whatever space is available
3584 * We can only consider changing the size if no resync
3585 * or reconstruction is happening, and if the new size
3586 * is acceptable. It must fit before the sb_offset or,
3587 * if that is <data_offset, it must fit before the
3588 * size of each device.
3589 * If size is zero, we find the largest size that fits.
3591 if (mddev->sync_thread)
3593 ITERATE_RDEV(mddev,rdev,tmp) {
3595 if (rdev->sb_offset > rdev->data_offset)
3596 avail = (rdev->sb_offset*2) - rdev->data_offset;
3598 avail = get_capacity(rdev->bdev->bd_disk)
3599 - rdev->data_offset;
3600 if (fit && (size == 0 || size > avail/2))
3602 if (avail < ((sector_t)size << 1))
3605 rv = mddev->pers->resize(mddev, (sector_t)size *2);
3607 struct block_device *bdev;
3609 bdev = bdget_disk(mddev->gendisk, 0);
3611 mutex_lock(&bdev->bd_inode->i_mutex);
3612 i_size_write(bdev->bd_inode, (loff_t)mddev->array_size << 10);
3613 mutex_unlock(&bdev->bd_inode->i_mutex);
3620 static int update_raid_disks(mddev_t *mddev, int raid_disks)
3623 /* change the number of raid disks */
3624 if (mddev->pers->check_reshape == NULL)
3626 if (raid_disks <= 0 ||
3627 raid_disks >= mddev->max_disks)
3629 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
3631 mddev->delta_disks = raid_disks - mddev->raid_disks;
3633 rv = mddev->pers->check_reshape(mddev);
3639 * update_array_info is used to change the configuration of an
3641 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
3642 * fields in the info are checked against the array.
3643 * Any differences that cannot be handled will cause an error.
3644 * Normally, only one change can be managed at a time.
3646 static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
3652 /* calculate expected state,ignoring low bits */
3653 if (mddev->bitmap && mddev->bitmap_offset)
3654 state |= (1 << MD_SB_BITMAP_PRESENT);
3656 if (mddev->major_version != info->major_version ||
3657 mddev->minor_version != info->minor_version ||
3658 /* mddev->patch_version != info->patch_version || */
3659 mddev->ctime != info->ctime ||
3660 mddev->level != info->level ||
3661 /* mddev->layout != info->layout || */
3662 !mddev->persistent != info->not_persistent||
3663 mddev->chunk_size != info->chunk_size ||
3664 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
3665 ((state^info->state) & 0xfffffe00)
3668 /* Check there is only one change */
3669 if (info->size >= 0 && mddev->size != info->size) cnt++;
3670 if (mddev->raid_disks != info->raid_disks) cnt++;
3671 if (mddev->layout != info->layout) cnt++;
3672 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
3673 if (cnt == 0) return 0;
3674 if (cnt > 1) return -EINVAL;
3676 if (mddev->layout != info->layout) {
3678 * we don't need to do anything at the md level, the
3679 * personality will take care of it all.
3681 if (mddev->pers->reconfig == NULL)
3684 return mddev->pers->reconfig(mddev, info->layout, -1);
3686 if (info->size >= 0 && mddev->size != info->size)
3687 rv = update_size(mddev, info->size);
3689 if (mddev->raid_disks != info->raid_disks)
3690 rv = update_raid_disks(mddev, info->raid_disks);
3692 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
3693 if (mddev->pers->quiesce == NULL)
3695 if (mddev->recovery || mddev->sync_thread)
3697 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
3698 /* add the bitmap */
3701 if (mddev->default_bitmap_offset == 0)
3703 mddev->bitmap_offset = mddev->default_bitmap_offset;
3704 mddev->pers->quiesce(mddev, 1);
3705 rv = bitmap_create(mddev);
3707 bitmap_destroy(mddev);
3708 mddev->pers->quiesce(mddev, 0);
3710 /* remove the bitmap */
3713 if (mddev->bitmap->file)
3715 mddev->pers->quiesce(mddev, 1);
3716 bitmap_destroy(mddev);
3717 mddev->pers->quiesce(mddev, 0);
3718 mddev->bitmap_offset = 0;
3721 md_update_sb(mddev);
3725 static int set_disk_faulty(mddev_t *mddev, dev_t dev)
3729 if (mddev->pers == NULL)
3732 rdev = find_rdev(mddev, dev);
3736 md_error(mddev, rdev);
3740 static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
3742 mddev_t *mddev = bdev->bd_disk->private_data;
3746 geo->cylinders = get_capacity(mddev->gendisk) / 8;
3750 static int md_ioctl(struct inode *inode, struct file *file,
3751 unsigned int cmd, unsigned long arg)
3754 void __user *argp = (void __user *)arg;
3755 mddev_t *mddev = NULL;
3757 if (!capable(CAP_SYS_ADMIN))
3761 * Commands dealing with the RAID driver but not any
3767 err = get_version(argp);
3770 case PRINT_RAID_DEBUG:
3778 autostart_arrays(arg);
3785 * Commands creating/starting a new array:
3788 mddev = inode->i_bdev->bd_disk->private_data;
3796 if (cmd == START_ARRAY) {
3797 /* START_ARRAY doesn't need to lock the array as autostart_array
3798 * does the locking, and it could even be a different array
3803 "md: %s(pid %d) used deprecated START_ARRAY ioctl. "
3804 "This will not be supported beyond July 2006\n",
3805 current->comm, current->pid);
3808 err = autostart_array(new_decode_dev(arg));
3810 printk(KERN_WARNING "md: autostart failed!\n");
3816 err = mddev_lock(mddev);
3819 "md: ioctl lock interrupted, reason %d, cmd %d\n",
3826 case SET_ARRAY_INFO:
3828 mdu_array_info_t info;
3830 memset(&info, 0, sizeof(info));
3831 else if (copy_from_user(&info, argp, sizeof(info))) {
3836 err = update_array_info(mddev, &info);
3838 printk(KERN_WARNING "md: couldn't update"
3839 " array info. %d\n", err);
3844 if (!list_empty(&mddev->disks)) {
3846 "md: array %s already has disks!\n",
3851 if (mddev->raid_disks) {
3853 "md: array %s already initialised!\n",
3858 err = set_array_info(mddev, &info);
3860 printk(KERN_WARNING "md: couldn't set"
3861 " array info. %d\n", err);
3871 * Commands querying/configuring an existing array:
3873 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
3874 * RUN_ARRAY, and SET_BITMAP_FILE are allowed */
3875 if (!mddev->raid_disks && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
3876 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE) {
3882 * Commands even a read-only array can execute:
3886 case GET_ARRAY_INFO:
3887 err = get_array_info(mddev, argp);
3890 case GET_BITMAP_FILE:
3891 err = get_bitmap_file(mddev, argp);
3895 err = get_disk_info(mddev, argp);
3898 case RESTART_ARRAY_RW:
3899 err = restart_array(mddev);
3903 err = do_md_stop (mddev, 0);
3907 err = do_md_stop (mddev, 1);
3911 * We have a problem here : there is no easy way to give a CHS
3912 * virtual geometry. We currently pretend that we have a 2 heads
3913 * 4 sectors (with a BIG number of cylinders...). This drives
3914 * dosfs just mad... ;-)
3919 * The remaining ioctls are changing the state of the
3920 * superblock, so we do not allow them on read-only arrays.
3921 * However non-MD ioctls (e.g. get-size) will still come through
3922 * here and hit the 'default' below, so only disallow
3923 * 'md' ioctls, and switch to rw mode if started auto-readonly.
3925 if (_IOC_TYPE(cmd) == MD_MAJOR &&
3926 mddev->ro && mddev->pers) {
3927 if (mddev->ro == 2) {
3929 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3930 md_wakeup_thread(mddev->thread);
3942 mdu_disk_info_t info;
3943 if (copy_from_user(&info, argp, sizeof(info)))
3946 err = add_new_disk(mddev, &info);
3950 case HOT_REMOVE_DISK:
3951 err = hot_remove_disk(mddev, new_decode_dev(arg));
3955 err = hot_add_disk(mddev, new_decode_dev(arg));
3958 case SET_DISK_FAULTY:
3959 err = set_disk_faulty(mddev, new_decode_dev(arg));
3963 err = do_md_run (mddev);
3966 case SET_BITMAP_FILE:
3967 err = set_bitmap_file(mddev, (int)arg);
3971 if (_IOC_TYPE(cmd) == MD_MAJOR)
3972 printk(KERN_WARNING "md: %s(pid %d) used"
3973 " obsolete MD ioctl, upgrade your"
3974 " software to use new ictls.\n",
3975 current->comm, current->pid);
3982 mddev_unlock(mddev);
3992 static int md_open(struct inode *inode, struct file *file)
3995 * Succeed if we can lock the mddev, which confirms that
3996 * it isn't being stopped right now.
3998 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4001 if ((err = mddev_lock(mddev)))
4006 mddev_unlock(mddev);
4008 check_disk_change(inode->i_bdev);
4013 static int md_release(struct inode *inode, struct file * file)
4015 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4024 static int md_media_changed(struct gendisk *disk)
4026 mddev_t *mddev = disk->private_data;
4028 return mddev->changed;
4031 static int md_revalidate(struct gendisk *disk)
4033 mddev_t *mddev = disk->private_data;
4038 static struct block_device_operations md_fops =
4040 .owner = THIS_MODULE,
4042 .release = md_release,
4044 .getgeo = md_getgeo,
4045 .media_changed = md_media_changed,
4046 .revalidate_disk= md_revalidate,
4049 static int md_thread(void * arg)
4051 mdk_thread_t *thread = arg;
4054 * md_thread is a 'system-thread', it's priority should be very
4055 * high. We avoid resource deadlocks individually in each
4056 * raid personality. (RAID5 does preallocation) We also use RR and
4057 * the very same RT priority as kswapd, thus we will never get
4058 * into a priority inversion deadlock.
4060 * we definitely have to have equal or higher priority than
4061 * bdflush, otherwise bdflush will deadlock if there are too
4062 * many dirty RAID5 blocks.
4065 allow_signal(SIGKILL);
4066 while (!kthread_should_stop()) {
4068 /* We need to wait INTERRUPTIBLE so that
4069 * we don't add to the load-average.
4070 * That means we need to be sure no signals are
4073 if (signal_pending(current))
4074 flush_signals(current);
4076 wait_event_interruptible_timeout
4078 test_bit(THREAD_WAKEUP, &thread->flags)
4079 || kthread_should_stop(),
4083 clear_bit(THREAD_WAKEUP, &thread->flags);
4085 thread->run(thread->mddev);
4091 void md_wakeup_thread(mdk_thread_t *thread)
4094 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
4095 set_bit(THREAD_WAKEUP, &thread->flags);
4096 wake_up(&thread->wqueue);
4100 mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
4103 mdk_thread_t *thread;
4105 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
4109 init_waitqueue_head(&thread->wqueue);
4112 thread->mddev = mddev;
4113 thread->timeout = MAX_SCHEDULE_TIMEOUT;
4114 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
4115 if (IS_ERR(thread->tsk)) {
4122 void md_unregister_thread(mdk_thread_t *thread)
4124 dprintk("interrupting MD-thread pid %d\n", thread->tsk->pid);
4126 kthread_stop(thread->tsk);
4130 void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
4137 if (!rdev || test_bit(Faulty, &rdev->flags))
4140 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
4142 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
4143 __builtin_return_address(0),__builtin_return_address(1),
4144 __builtin_return_address(2),__builtin_return_address(3));
4146 if (!mddev->pers->error_handler)
4148 mddev->pers->error_handler(mddev,rdev);
4149 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4150 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4151 md_wakeup_thread(mddev->thread);
4152 md_new_event(mddev);
4155 /* seq_file implementation /proc/mdstat */
4157 static void status_unused(struct seq_file *seq)
4161 struct list_head *tmp;
4163 seq_printf(seq, "unused devices: ");
4165 ITERATE_RDEV_PENDING(rdev,tmp) {
4166 char b[BDEVNAME_SIZE];
4168 seq_printf(seq, "%s ",
4169 bdevname(rdev->bdev,b));
4172 seq_printf(seq, "<none>");
4174 seq_printf(seq, "\n");
4178 static void status_resync(struct seq_file *seq, mddev_t * mddev)
4180 sector_t max_blocks, resync, res;
4181 unsigned long dt, db, rt;
4183 unsigned int per_milli;
4185 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
4187 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
4188 max_blocks = mddev->resync_max_sectors >> 1;
4190 max_blocks = mddev->size;
4193 * Should not happen.
4199 /* Pick 'scale' such that (resync>>scale)*1000 will fit
4200 * in a sector_t, and (max_blocks>>scale) will fit in a
4201 * u32, as those are the requirements for sector_div.
4202 * Thus 'scale' must be at least 10
4205 if (sizeof(sector_t) > sizeof(unsigned long)) {
4206 while ( max_blocks/2 > (1ULL<<(scale+32)))
4209 res = (resync>>scale)*1000;
4210 sector_div(res, (u32)((max_blocks>>scale)+1));
4214 int i, x = per_milli/50, y = 20-x;
4215 seq_printf(seq, "[");
4216 for (i = 0; i < x; i++)
4217 seq_printf(seq, "=");
4218 seq_printf(seq, ">");
4219 for (i = 0; i < y; i++)
4220 seq_printf(seq, ".");
4221 seq_printf(seq, "] ");
4223 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
4224 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
4226 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
4227 "resync" : "recovery")),
4228 per_milli/10, per_milli % 10,
4229 (unsigned long long) resync,
4230 (unsigned long long) max_blocks);
4233 * We do not want to overflow, so the order of operands and
4234 * the * 100 / 100 trick are important. We do a +1 to be
4235 * safe against division by zero. We only estimate anyway.
4237 * dt: time from mark until now
4238 * db: blocks written from mark until now
4239 * rt: remaining time
4241 dt = ((jiffies - mddev->resync_mark) / HZ);
4243 db = resync - (mddev->resync_mark_cnt/2);
4244 rt = (dt * ((unsigned long)(max_blocks-resync) / (db/100+1)))/100;
4246 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
4248 seq_printf(seq, " speed=%ldK/sec", db/dt);
4251 static void *md_seq_start(struct seq_file *seq, loff_t *pos)
4253 struct list_head *tmp;
4263 spin_lock(&all_mddevs_lock);
4264 list_for_each(tmp,&all_mddevs)
4266 mddev = list_entry(tmp, mddev_t, all_mddevs);
4268 spin_unlock(&all_mddevs_lock);
4271 spin_unlock(&all_mddevs_lock);
4273 return (void*)2;/* tail */
4277 static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4279 struct list_head *tmp;
4280 mddev_t *next_mddev, *mddev = v;
4286 spin_lock(&all_mddevs_lock);
4288 tmp = all_mddevs.next;
4290 tmp = mddev->all_mddevs.next;
4291 if (tmp != &all_mddevs)
4292 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
4294 next_mddev = (void*)2;
4297 spin_unlock(&all_mddevs_lock);
4305 static void md_seq_stop(struct seq_file *seq, void *v)
4309 if (mddev && v != (void*)1 && v != (void*)2)
4313 struct mdstat_info {
4317 static int md_seq_show(struct seq_file *seq, void *v)
4321 struct list_head *tmp2;
4323 struct mdstat_info *mi = seq->private;
4324 struct bitmap *bitmap;
4326 if (v == (void*)1) {
4327 struct mdk_personality *pers;
4328 seq_printf(seq, "Personalities : ");
4329 spin_lock(&pers_lock);
4330 list_for_each_entry(pers, &pers_list, list)
4331 seq_printf(seq, "[%s] ", pers->name);
4333 spin_unlock(&pers_lock);
4334 seq_printf(seq, "\n");
4335 mi->event = atomic_read(&md_event_count);
4338 if (v == (void*)2) {
4343 if (mddev_lock(mddev) < 0)
4346 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
4347 seq_printf(seq, "%s : %sactive", mdname(mddev),
4348 mddev->pers ? "" : "in");
4351 seq_printf(seq, " (read-only)");
4353 seq_printf(seq, "(auto-read-only)");
4354 seq_printf(seq, " %s", mddev->pers->name);
4358 ITERATE_RDEV(mddev,rdev,tmp2) {
4359 char b[BDEVNAME_SIZE];
4360 seq_printf(seq, " %s[%d]",
4361 bdevname(rdev->bdev,b), rdev->desc_nr);
4362 if (test_bit(WriteMostly, &rdev->flags))
4363 seq_printf(seq, "(W)");
4364 if (test_bit(Faulty, &rdev->flags)) {
4365 seq_printf(seq, "(F)");
4367 } else if (rdev->raid_disk < 0)
4368 seq_printf(seq, "(S)"); /* spare */
4372 if (!list_empty(&mddev->disks)) {
4374 seq_printf(seq, "\n %llu blocks",
4375 (unsigned long long)mddev->array_size);
4377 seq_printf(seq, "\n %llu blocks",
4378 (unsigned long long)size);
4380 if (mddev->persistent) {
4381 if (mddev->major_version != 0 ||
4382 mddev->minor_version != 90) {
4383 seq_printf(seq," super %d.%d",
4384 mddev->major_version,
4385 mddev->minor_version);
4388 seq_printf(seq, " super non-persistent");
4391 mddev->pers->status (seq, mddev);
4392 seq_printf(seq, "\n ");
4393 if (mddev->pers->sync_request) {
4394 if (mddev->curr_resync > 2) {
4395 status_resync (seq, mddev);
4396 seq_printf(seq, "\n ");
4397 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
4398 seq_printf(seq, "\tresync=DELAYED\n ");
4399 else if (mddev->recovery_cp < MaxSector)
4400 seq_printf(seq, "\tresync=PENDING\n ");
4403 seq_printf(seq, "\n ");
4405 if ((bitmap = mddev->bitmap)) {
4406 unsigned long chunk_kb;
4407 unsigned long flags;
4408 spin_lock_irqsave(&bitmap->lock, flags);
4409 chunk_kb = bitmap->chunksize >> 10;
4410 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
4412 bitmap->pages - bitmap->missing_pages,
4414 (bitmap->pages - bitmap->missing_pages)
4415 << (PAGE_SHIFT - 10),
4416 chunk_kb ? chunk_kb : bitmap->chunksize,
4417 chunk_kb ? "KB" : "B");
4419 seq_printf(seq, ", file: ");
4420 seq_path(seq, bitmap->file->f_vfsmnt,
4421 bitmap->file->f_dentry," \t\n");
4424 seq_printf(seq, "\n");
4425 spin_unlock_irqrestore(&bitmap->lock, flags);
4428 seq_printf(seq, "\n");
4430 mddev_unlock(mddev);
4435 static struct seq_operations md_seq_ops = {
4436 .start = md_seq_start,
4437 .next = md_seq_next,
4438 .stop = md_seq_stop,
4439 .show = md_seq_show,
4442 static int md_seq_open(struct inode *inode, struct file *file)
4445 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
4449 error = seq_open(file, &md_seq_ops);
4453 struct seq_file *p = file->private_data;
4455 mi->event = atomic_read(&md_event_count);
4460 static int md_seq_release(struct inode *inode, struct file *file)
4462 struct seq_file *m = file->private_data;
4463 struct mdstat_info *mi = m->private;
4466 return seq_release(inode, file);
4469 static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
4471 struct seq_file *m = filp->private_data;
4472 struct mdstat_info *mi = m->private;
4475 poll_wait(filp, &md_event_waiters, wait);
4477 /* always allow read */
4478 mask = POLLIN | POLLRDNORM;
4480 if (mi->event != atomic_read(&md_event_count))
4481 mask |= POLLERR | POLLPRI;
4485 static struct file_operations md_seq_fops = {
4486 .open = md_seq_open,
4488 .llseek = seq_lseek,
4489 .release = md_seq_release,
4490 .poll = mdstat_poll,
4493 int register_md_personality(struct mdk_personality *p)
4495 spin_lock(&pers_lock);
4496 list_add_tail(&p->list, &pers_list);
4497 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
4498 spin_unlock(&pers_lock);
4502 int unregister_md_personality(struct mdk_personality *p)
4504 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
4505 spin_lock(&pers_lock);
4506 list_del_init(&p->list);
4507 spin_unlock(&pers_lock);
4511 static int is_mddev_idle(mddev_t *mddev)
4514 struct list_head *tmp;
4516 unsigned long curr_events;
4519 ITERATE_RDEV(mddev,rdev,tmp) {
4520 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
4521 curr_events = disk_stat_read(disk, sectors[0]) +
4522 disk_stat_read(disk, sectors[1]) -
4523 atomic_read(&disk->sync_io);
4524 /* The difference between curr_events and last_events
4525 * will be affected by any new non-sync IO (making
4526 * curr_events bigger) and any difference in the amount of
4527 * in-flight syncio (making current_events bigger or smaller)
4528 * The amount in-flight is currently limited to
4529 * 32*64K in raid1/10 and 256*PAGE_SIZE in raid5/6
4530 * which is at most 4096 sectors.
4531 * These numbers are fairly fragile and should be made
4532 * more robust, probably by enforcing the
4533 * 'window size' that md_do_sync sort-of uses.
4535 * Note: the following is an unsigned comparison.
4537 if ((curr_events - rdev->last_events + 4096) > 8192) {
4538 rdev->last_events = curr_events;
4545 void md_done_sync(mddev_t *mddev, int blocks, int ok)
4547 /* another "blocks" (512byte) blocks have been synced */
4548 atomic_sub(blocks, &mddev->recovery_active);
4549 wake_up(&mddev->recovery_wait);
4551 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
4552 md_wakeup_thread(mddev->thread);
4553 // stop recovery, signal do_sync ....
4558 /* md_write_start(mddev, bi)
4559 * If we need to update some array metadata (e.g. 'active' flag
4560 * in superblock) before writing, schedule a superblock update
4561 * and wait for it to complete.
4563 void md_write_start(mddev_t *mddev, struct bio *bi)
4565 if (bio_data_dir(bi) != WRITE)
4568 BUG_ON(mddev->ro == 1);
4569 if (mddev->ro == 2) {
4570 /* need to switch to read/write */
4572 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4573 md_wakeup_thread(mddev->thread);
4575 atomic_inc(&mddev->writes_pending);
4576 if (mddev->in_sync) {
4577 spin_lock_irq(&mddev->write_lock);
4578 if (mddev->in_sync) {
4580 mddev->sb_dirty = 1;
4581 md_wakeup_thread(mddev->thread);
4583 spin_unlock_irq(&mddev->write_lock);
4585 wait_event(mddev->sb_wait, mddev->sb_dirty==0);
4588 void md_write_end(mddev_t *mddev)
4590 if (atomic_dec_and_test(&mddev->writes_pending)) {
4591 if (mddev->safemode == 2)
4592 md_wakeup_thread(mddev->thread);
4594 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
4598 static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
4600 #define SYNC_MARKS 10
4601 #define SYNC_MARK_STEP (3*HZ)
4602 void md_do_sync(mddev_t *mddev)
4605 unsigned int currspeed = 0,
4607 sector_t max_sectors,j, io_sectors;
4608 unsigned long mark[SYNC_MARKS];
4609 sector_t mark_cnt[SYNC_MARKS];
4611 struct list_head *tmp;
4612 sector_t last_check;
4615 /* just incase thread restarts... */
4616 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
4619 /* we overload curr_resync somewhat here.
4620 * 0 == not engaged in resync at all
4621 * 2 == checking that there is no conflict with another sync
4622 * 1 == like 2, but have yielded to allow conflicting resync to
4624 * other == active in resync - this many blocks
4626 * Before starting a resync we must have set curr_resync to
4627 * 2, and then checked that every "conflicting" array has curr_resync
4628 * less than ours. When we find one that is the same or higher
4629 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
4630 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
4631 * This will mean we have to start checking from the beginning again.
4636 mddev->curr_resync = 2;
4639 if (kthread_should_stop()) {
4640 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4643 ITERATE_MDDEV(mddev2,tmp) {
4644 if (mddev2 == mddev)
4646 if (mddev2->curr_resync &&
4647 match_mddev_units(mddev,mddev2)) {
4649 if (mddev < mddev2 && mddev->curr_resync == 2) {
4650 /* arbitrarily yield */
4651 mddev->curr_resync = 1;
4652 wake_up(&resync_wait);
4654 if (mddev > mddev2 && mddev->curr_resync == 1)
4655 /* no need to wait here, we can wait the next
4656 * time 'round when curr_resync == 2
4659 prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE);
4660 if (!kthread_should_stop() &&
4661 mddev2->curr_resync >= mddev->curr_resync) {
4662 printk(KERN_INFO "md: delaying resync of %s"
4663 " until %s has finished resync (they"
4664 " share one or more physical units)\n",
4665 mdname(mddev), mdname(mddev2));
4668 finish_wait(&resync_wait, &wq);
4671 finish_wait(&resync_wait, &wq);
4674 } while (mddev->curr_resync < 2);
4676 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
4677 /* resync follows the size requested by the personality,
4678 * which defaults to physical size, but can be virtual size
4680 max_sectors = mddev->resync_max_sectors;
4681 mddev->resync_mismatches = 0;
4682 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4683 max_sectors = mddev->size << 1;
4685 /* recovery follows the physical size of devices */
4686 max_sectors = mddev->size << 1;
4688 printk(KERN_INFO "md: syncing RAID array %s\n", mdname(mddev));
4689 printk(KERN_INFO "md: minimum _guaranteed_ reconstruction speed:"
4690 " %d KB/sec/disc.\n", speed_min(mddev));
4691 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
4692 "(but not more than %d KB/sec) for reconstruction.\n",
4695 is_mddev_idle(mddev); /* this also initializes IO event counters */
4696 /* we don't use the checkpoint if there's a bitmap */
4697 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && !mddev->bitmap
4698 && ! test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
4699 j = mddev->recovery_cp;
4703 for (m = 0; m < SYNC_MARKS; m++) {
4705 mark_cnt[m] = io_sectors;
4708 mddev->resync_mark = mark[last_mark];
4709 mddev->resync_mark_cnt = mark_cnt[last_mark];
4712 * Tune reconstruction:
4714 window = 32*(PAGE_SIZE/512);
4715 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
4716 window/2,(unsigned long long) max_sectors/2);
4718 atomic_set(&mddev->recovery_active, 0);
4719 init_waitqueue_head(&mddev->recovery_wait);
4724 "md: resuming recovery of %s from checkpoint.\n",
4726 mddev->curr_resync = j;
4729 while (j < max_sectors) {
4733 sectors = mddev->pers->sync_request(mddev, j, &skipped,
4734 currspeed < speed_min(mddev));
4736 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
4740 if (!skipped) { /* actual IO requested */
4741 io_sectors += sectors;
4742 atomic_add(sectors, &mddev->recovery_active);
4746 if (j>1) mddev->curr_resync = j;
4747 if (last_check == 0)
4748 /* this is the earliers that rebuilt will be
4749 * visible in /proc/mdstat
4751 md_new_event(mddev);
4753 if (last_check + window > io_sectors || j == max_sectors)
4756 last_check = io_sectors;
4758 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery) ||
4759 test_bit(MD_RECOVERY_ERR, &mddev->recovery))
4763 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
4765 int next = (last_mark+1) % SYNC_MARKS;
4767 mddev->resync_mark = mark[next];
4768 mddev->resync_mark_cnt = mark_cnt[next];
4769 mark[next] = jiffies;
4770 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
4775 if (kthread_should_stop()) {
4777 * got a signal, exit.
4780 "md: md_do_sync() got signal ... exiting\n");
4781 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4786 * this loop exits only if either when we are slower than
4787 * the 'hard' speed limit, or the system was IO-idle for
4789 * the system might be non-idle CPU-wise, but we only care
4790 * about not overloading the IO subsystem. (things like an
4791 * e2fsck being done on the RAID array should execute fast)
4793 mddev->queue->unplug_fn(mddev->queue);
4796 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
4797 /((jiffies-mddev->resync_mark)/HZ +1) +1;
4799 if (currspeed > speed_min(mddev)) {
4800 if ((currspeed > speed_max(mddev)) ||
4801 !is_mddev_idle(mddev)) {
4807 printk(KERN_INFO "md: %s: sync done.\n",mdname(mddev));
4809 * this also signals 'finished resyncing' to md_stop
4812 mddev->queue->unplug_fn(mddev->queue);
4814 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
4816 /* tell personality that we are finished */
4817 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
4819 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
4820 test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
4821 !test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
4822 mddev->curr_resync > 2 &&
4823 mddev->curr_resync >= mddev->recovery_cp) {
4824 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4826 "md: checkpointing recovery of %s.\n",
4828 mddev->recovery_cp = mddev->curr_resync;
4830 mddev->recovery_cp = MaxSector;
4834 mddev->curr_resync = 0;
4835 wake_up(&resync_wait);
4836 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
4837 md_wakeup_thread(mddev->thread);
4839 EXPORT_SYMBOL_GPL(md_do_sync);
4843 * This routine is regularly called by all per-raid-array threads to
4844 * deal with generic issues like resync and super-block update.
4845 * Raid personalities that don't have a thread (linear/raid0) do not
4846 * need this as they never do any recovery or update the superblock.
4848 * It does not do any resync itself, but rather "forks" off other threads
4849 * to do that as needed.
4850 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
4851 * "->recovery" and create a thread at ->sync_thread.
4852 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
4853 * and wakeups up this thread which will reap the thread and finish up.
4854 * This thread also removes any faulty devices (with nr_pending == 0).
4856 * The overall approach is:
4857 * 1/ if the superblock needs updating, update it.
4858 * 2/ If a recovery thread is running, don't do anything else.
4859 * 3/ If recovery has finished, clean up, possibly marking spares active.
4860 * 4/ If there are any faulty devices, remove them.
4861 * 5/ If array is degraded, try to add spares devices
4862 * 6/ If array has spares or is not in-sync, start a resync thread.
4864 void md_check_recovery(mddev_t *mddev)
4867 struct list_head *rtmp;
4871 bitmap_daemon_work(mddev->bitmap);
4876 if (signal_pending(current)) {
4877 if (mddev->pers->sync_request) {
4878 printk(KERN_INFO "md: %s in immediate safe mode\n",
4880 mddev->safemode = 2;
4882 flush_signals(current);
4887 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
4888 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
4889 (mddev->safemode == 1) ||
4890 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
4891 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
4895 if (mddev_trylock(mddev)) {
4898 spin_lock_irq(&mddev->write_lock);
4899 if (mddev->safemode && !atomic_read(&mddev->writes_pending) &&
4900 !mddev->in_sync && mddev->recovery_cp == MaxSector) {
4902 mddev->sb_dirty = 1;
4904 if (mddev->safemode == 1)
4905 mddev->safemode = 0;
4906 spin_unlock_irq(&mddev->write_lock);
4908 if (mddev->sb_dirty)
4909 md_update_sb(mddev);
4912 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4913 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
4914 /* resync/recovery still happening */
4915 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4918 if (mddev->sync_thread) {
4919 /* resync has finished, collect result */
4920 md_unregister_thread(mddev->sync_thread);
4921 mddev->sync_thread = NULL;
4922 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
4923 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4925 /* activate any spares */
4926 mddev->pers->spare_active(mddev);
4928 md_update_sb(mddev);
4930 /* if array is no-longer degraded, then any saved_raid_disk
4931 * information must be scrapped
4933 if (!mddev->degraded)
4934 ITERATE_RDEV(mddev,rdev,rtmp)
4935 rdev->saved_raid_disk = -1;
4937 mddev->recovery = 0;
4938 /* flag recovery needed just to double check */
4939 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4940 md_new_event(mddev);
4943 /* Clear some bits that don't mean anything, but
4946 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4947 clear_bit(MD_RECOVERY_ERR, &mddev->recovery);
4948 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
4949 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
4951 /* no recovery is running.
4952 * remove any failed drives, then
4953 * add spares if possible.
4954 * Spare are also removed and re-added, to allow
4955 * the personality to fail the re-add.
4957 ITERATE_RDEV(mddev,rdev,rtmp)
4958 if (rdev->raid_disk >= 0 &&
4959 (test_bit(Faulty, &rdev->flags) || ! test_bit(In_sync, &rdev->flags)) &&
4960 atomic_read(&rdev->nr_pending)==0) {
4961 if (mddev->pers->hot_remove_disk(mddev, rdev->raid_disk)==0) {
4963 sprintf(nm,"rd%d", rdev->raid_disk);
4964 sysfs_remove_link(&mddev->kobj, nm);
4965 rdev->raid_disk = -1;
4969 if (mddev->degraded) {
4970 ITERATE_RDEV(mddev,rdev,rtmp)
4971 if (rdev->raid_disk < 0
4972 && !test_bit(Faulty, &rdev->flags)) {
4973 if (mddev->pers->hot_add_disk(mddev,rdev)) {
4975 sprintf(nm, "rd%d", rdev->raid_disk);
4976 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
4978 md_new_event(mddev);
4985 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4986 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4987 } else if (mddev->recovery_cp < MaxSector) {
4988 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4989 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
4990 /* nothing to be done ... */
4993 if (mddev->pers->sync_request) {
4994 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4995 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
4996 /* We are adding a device or devices to an array
4997 * which has the bitmap stored on all devices.
4998 * So make sure all bitmap pages get written
5000 bitmap_write_all(mddev->bitmap);
5002 mddev->sync_thread = md_register_thread(md_do_sync,
5005 if (!mddev->sync_thread) {
5006 printk(KERN_ERR "%s: could not start resync"
5009 /* leave the spares where they are, it shouldn't hurt */
5010 mddev->recovery = 0;
5012 md_wakeup_thread(mddev->sync_thread);
5013 md_new_event(mddev);
5016 mddev_unlock(mddev);
5020 static int md_notify_reboot(struct notifier_block *this,
5021 unsigned long code, void *x)
5023 struct list_head *tmp;
5026 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
5028 printk(KERN_INFO "md: stopping all md devices.\n");
5030 ITERATE_MDDEV(mddev,tmp)
5031 if (mddev_trylock(mddev)) {
5032 do_md_stop (mddev, 1);
5033 mddev_unlock(mddev);
5036 * certain more exotic SCSI devices are known to be
5037 * volatile wrt too early system reboots. While the
5038 * right place to handle this issue is the given
5039 * driver, we do want to have a safe RAID driver ...
5046 static struct notifier_block md_notifier = {
5047 .notifier_call = md_notify_reboot,
5049 .priority = INT_MAX, /* before any real devices */
5052 static void md_geninit(void)
5054 struct proc_dir_entry *p;
5056 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
5058 p = create_proc_entry("mdstat", S_IRUGO, NULL);
5060 p->proc_fops = &md_seq_fops;
5063 static int __init md_init(void)
5067 printk(KERN_INFO "md: md driver %d.%d.%d MAX_MD_DEVS=%d,"
5068 " MD_SB_DISKS=%d\n",
5069 MD_MAJOR_VERSION, MD_MINOR_VERSION,
5070 MD_PATCHLEVEL_VERSION, MAX_MD_DEVS, MD_SB_DISKS);
5071 printk(KERN_INFO "md: bitmap version %d.%d\n", BITMAP_MAJOR_HI,
5074 if (register_blkdev(MAJOR_NR, "md"))
5076 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
5077 unregister_blkdev(MAJOR_NR, "md");
5081 blk_register_region(MKDEV(MAJOR_NR, 0), MAX_MD_DEVS, THIS_MODULE,
5082 md_probe, NULL, NULL);
5083 blk_register_region(MKDEV(mdp_major, 0), MAX_MD_DEVS<<MdpMinorShift, THIS_MODULE,
5084 md_probe, NULL, NULL);
5086 for (minor=0; minor < MAX_MD_DEVS; ++minor)
5087 devfs_mk_bdev(MKDEV(MAJOR_NR, minor),
5088 S_IFBLK|S_IRUSR|S_IWUSR,
5091 for (minor=0; minor < MAX_MD_DEVS; ++minor)
5092 devfs_mk_bdev(MKDEV(mdp_major, minor<<MdpMinorShift),
5093 S_IFBLK|S_IRUSR|S_IWUSR,
5097 register_reboot_notifier(&md_notifier);
5098 raid_table_header = register_sysctl_table(raid_root_table, 1);
5108 * Searches all registered partitions for autorun RAID arrays
5111 static dev_t detected_devices[128];
5114 void md_autodetect_dev(dev_t dev)
5116 if (dev_cnt >= 0 && dev_cnt < 127)
5117 detected_devices[dev_cnt++] = dev;
5121 static void autostart_arrays(int part)
5126 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
5128 for (i = 0; i < dev_cnt; i++) {
5129 dev_t dev = detected_devices[i];
5131 rdev = md_import_device(dev,0, 0);
5135 if (test_bit(Faulty, &rdev->flags)) {
5139 list_add(&rdev->same_set, &pending_raid_disks);
5143 autorun_devices(part);
5148 static __exit void md_exit(void)
5151 struct list_head *tmp;
5153 blk_unregister_region(MKDEV(MAJOR_NR,0), MAX_MD_DEVS);
5154 blk_unregister_region(MKDEV(mdp_major,0), MAX_MD_DEVS << MdpMinorShift);
5155 for (i=0; i < MAX_MD_DEVS; i++)
5156 devfs_remove("md/%d", i);
5157 for (i=0; i < MAX_MD_DEVS; i++)
5158 devfs_remove("md/d%d", i);
5162 unregister_blkdev(MAJOR_NR,"md");
5163 unregister_blkdev(mdp_major, "mdp");
5164 unregister_reboot_notifier(&md_notifier);
5165 unregister_sysctl_table(raid_table_header);
5166 remove_proc_entry("mdstat", NULL);
5167 ITERATE_MDDEV(mddev,tmp) {
5168 struct gendisk *disk = mddev->gendisk;
5171 export_array(mddev);
5174 mddev->gendisk = NULL;
5179 module_init(md_init)
5180 module_exit(md_exit)
5182 static int get_ro(char *buffer, struct kernel_param *kp)
5184 return sprintf(buffer, "%d", start_readonly);
5186 static int set_ro(const char *val, struct kernel_param *kp)
5189 int num = simple_strtoul(val, &e, 10);
5190 if (*val && (*e == '\0' || *e == '\n')) {
5191 start_readonly = num;
5197 module_param_call(start_ro, set_ro, get_ro, NULL, 0600);
5198 module_param(start_dirty_degraded, int, 0644);
5201 EXPORT_SYMBOL(register_md_personality);
5202 EXPORT_SYMBOL(unregister_md_personality);
5203 EXPORT_SYMBOL(md_error);
5204 EXPORT_SYMBOL(md_done_sync);
5205 EXPORT_SYMBOL(md_write_start);
5206 EXPORT_SYMBOL(md_write_end);
5207 EXPORT_SYMBOL(md_register_thread);
5208 EXPORT_SYMBOL(md_unregister_thread);
5209 EXPORT_SYMBOL(md_wakeup_thread);
5210 EXPORT_SYMBOL(md_print_devices);
5211 EXPORT_SYMBOL(md_check_recovery);
5212 MODULE_LICENSE("GPL");
5214 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);