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 This program is free software; you can redistribute it and/or modify
23 it under the terms of the GNU General Public License as published by
24 the Free Software Foundation; either version 2, or (at your option)
27 You should have received a copy of the GNU General Public License
28 (for example /usr/src/linux/COPYING); if not, write to the Free
29 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 #include <linux/module.h>
33 #include <linux/config.h>
34 #include <linux/linkage.h>
35 #include <linux/raid/md.h>
36 #include <linux/sysctl.h>
37 #include <linux/devfs_fs_kernel.h>
38 #include <linux/buffer_head.h> /* for invalidate_bdev */
39 #include <linux/suspend.h>
41 #include <linux/init.h>
44 #include <linux/kmod.h>
47 #include <asm/unaligned.h>
49 #define MAJOR_NR MD_MAJOR
52 /* 63 partitions with the alternate major number (mdp) */
53 #define MdpMinorShift 6
56 #define dprintk(x...) ((void)(DEBUG && printk(x)))
60 static void autostart_arrays (int part
);
63 static mdk_personality_t
*pers
[MAX_PERSONALITY
];
64 static spinlock_t pers_lock
= SPIN_LOCK_UNLOCKED
;
67 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
68 * is 1000 KB/sec, so the extra system load does not show up that much.
69 * Increase it if you want to have more _guaranteed_ speed. Note that
70 * the RAID driver will use the maximum available bandwith if the IO
71 * subsystem is idle. There is also an 'absolute maximum' reconstruction
72 * speed limit - in case reconstruction slows down your system despite
75 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
78 static int sysctl_speed_limit_min
= 1000;
79 static int sysctl_speed_limit_max
= 200000;
81 static struct ctl_table_header
*raid_table_header
;
83 static ctl_table raid_table
[] = {
85 .ctl_name
= DEV_RAID_SPEED_LIMIT_MIN
,
86 .procname
= "speed_limit_min",
87 .data
= &sysctl_speed_limit_min
,
88 .maxlen
= sizeof(int),
90 .proc_handler
= &proc_dointvec
,
93 .ctl_name
= DEV_RAID_SPEED_LIMIT_MAX
,
94 .procname
= "speed_limit_max",
95 .data
= &sysctl_speed_limit_max
,
96 .maxlen
= sizeof(int),
98 .proc_handler
= &proc_dointvec
,
103 static ctl_table raid_dir_table
[] = {
105 .ctl_name
= DEV_RAID
,
114 static ctl_table raid_root_table
[] = {
120 .child
= raid_dir_table
,
125 static struct block_device_operations md_fops
;
128 * Enables to iterate over all existing md arrays
129 * all_mddevs_lock protects this list.
131 static LIST_HEAD(all_mddevs
);
132 static spinlock_t all_mddevs_lock
= SPIN_LOCK_UNLOCKED
;
136 * iterates through all used mddevs in the system.
137 * We take care to grab the all_mddevs_lock whenever navigating
138 * the list, and to always hold a refcount when unlocked.
139 * Any code which breaks out of this loop while own
140 * a reference to the current mddev and must mddev_put it.
142 #define ITERATE_MDDEV(mddev,tmp) \
144 for (({ spin_lock(&all_mddevs_lock); \
145 tmp = all_mddevs.next; \
147 ({ if (tmp != &all_mddevs) \
148 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
149 spin_unlock(&all_mddevs_lock); \
150 if (mddev) mddev_put(mddev); \
151 mddev = list_entry(tmp, mddev_t, all_mddevs); \
152 tmp != &all_mddevs;}); \
153 ({ spin_lock(&all_mddevs_lock); \
157 int md_flush_mddev(mddev_t
*mddev
, sector_t
*error_sector
)
159 struct list_head
*tmp
;
164 * this list iteration is done without any locking in md?!
166 ITERATE_RDEV(mddev
, rdev
, tmp
) {
167 request_queue_t
*r_queue
= bdev_get_queue(rdev
->bdev
);
170 if (!r_queue
->issue_flush_fn
)
173 err
= r_queue
->issue_flush_fn(r_queue
, rdev
->bdev
->bd_disk
, error_sector
);
182 static int md_flush_all(request_queue_t
*q
, struct gendisk
*disk
,
183 sector_t
*error_sector
)
185 mddev_t
*mddev
= q
->queuedata
;
187 return md_flush_mddev(mddev
, error_sector
);
190 static int md_fail_request (request_queue_t
*q
, struct bio
*bio
)
192 bio_io_error(bio
, bio
->bi_size
);
196 static inline mddev_t
*mddev_get(mddev_t
*mddev
)
198 atomic_inc(&mddev
->active
);
202 static void mddev_put(mddev_t
*mddev
)
204 if (!atomic_dec_and_lock(&mddev
->active
, &all_mddevs_lock
))
206 if (!mddev
->raid_disks
&& list_empty(&mddev
->disks
)) {
207 list_del(&mddev
->all_mddevs
);
208 blk_put_queue(mddev
->queue
);
211 spin_unlock(&all_mddevs_lock
);
214 static mddev_t
* mddev_find(dev_t unit
)
216 mddev_t
*mddev
, *new = NULL
;
219 spin_lock(&all_mddevs_lock
);
220 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
221 if (mddev
->unit
== unit
) {
223 spin_unlock(&all_mddevs_lock
);
230 list_add(&new->all_mddevs
, &all_mddevs
);
231 spin_unlock(&all_mddevs_lock
);
234 spin_unlock(&all_mddevs_lock
);
236 new = (mddev_t
*) kmalloc(sizeof(*new), GFP_KERNEL
);
240 memset(new, 0, sizeof(*new));
243 if (MAJOR(unit
) == MD_MAJOR
)
244 new->md_minor
= MINOR(unit
);
246 new->md_minor
= MINOR(unit
) >> MdpMinorShift
;
248 init_MUTEX(&new->reconfig_sem
);
249 INIT_LIST_HEAD(&new->disks
);
250 INIT_LIST_HEAD(&new->all_mddevs
);
251 init_timer(&new->safemode_timer
);
252 atomic_set(&new->active
, 1);
254 new->queue
= blk_alloc_queue(GFP_KERNEL
);
260 blk_queue_make_request(new->queue
, md_fail_request
);
265 static inline int mddev_lock(mddev_t
* mddev
)
267 return down_interruptible(&mddev
->reconfig_sem
);
270 static inline void mddev_lock_uninterruptible(mddev_t
* mddev
)
272 down(&mddev
->reconfig_sem
);
275 static inline int mddev_trylock(mddev_t
* mddev
)
277 return down_trylock(&mddev
->reconfig_sem
);
280 static inline void mddev_unlock(mddev_t
* mddev
)
282 up(&mddev
->reconfig_sem
);
285 md_wakeup_thread(mddev
->thread
);
288 mdk_rdev_t
* find_rdev_nr(mddev_t
*mddev
, int nr
)
291 struct list_head
*tmp
;
293 ITERATE_RDEV(mddev
,rdev
,tmp
) {
294 if (rdev
->desc_nr
== nr
)
300 static mdk_rdev_t
* find_rdev(mddev_t
* mddev
, dev_t dev
)
302 struct list_head
*tmp
;
305 ITERATE_RDEV(mddev
,rdev
,tmp
) {
306 if (rdev
->bdev
->bd_dev
== dev
)
312 inline static sector_t
calc_dev_sboffset(struct block_device
*bdev
)
314 sector_t size
= bdev
->bd_inode
->i_size
>> BLOCK_SIZE_BITS
;
315 return MD_NEW_SIZE_BLOCKS(size
);
318 static sector_t
calc_dev_size(mdk_rdev_t
*rdev
, unsigned chunk_size
)
322 size
= rdev
->sb_offset
;
325 size
&= ~((sector_t
)chunk_size
/1024 - 1);
329 static int alloc_disk_sb(mdk_rdev_t
* rdev
)
334 rdev
->sb_page
= alloc_page(GFP_KERNEL
);
335 if (!rdev
->sb_page
) {
336 printk(KERN_ALERT
"md: out of memory.\n");
343 static void free_disk_sb(mdk_rdev_t
* rdev
)
346 page_cache_release(rdev
->sb_page
);
348 rdev
->sb_page
= NULL
;
355 static int bi_complete(struct bio
*bio
, unsigned int bytes_done
, int error
)
360 complete((struct completion
*)bio
->bi_private
);
364 static int sync_page_io(struct block_device
*bdev
, sector_t sector
, int size
,
365 struct page
*page
, int rw
)
369 struct completion event
;
371 rw
|= (1 << BIO_RW_SYNC
);
374 bio
.bi_io_vec
= &vec
;
382 bio
.bi_sector
= sector
;
383 init_completion(&event
);
384 bio
.bi_private
= &event
;
385 bio
.bi_end_io
= bi_complete
;
386 submit_bio(rw
, &bio
);
387 wait_for_completion(&event
);
389 return test_bit(BIO_UPTODATE
, &bio
.bi_flags
);
392 static int read_disk_sb(mdk_rdev_t
* rdev
)
394 char b
[BDEVNAME_SIZE
];
395 if (!rdev
->sb_page
) {
403 if (!sync_page_io(rdev
->bdev
, rdev
->sb_offset
<<1, MD_SB_BYTES
, rdev
->sb_page
, READ
))
409 printk(KERN_WARNING
"md: disabled device %s, could not read superblock.\n",
410 bdevname(rdev
->bdev
,b
));
414 static int uuid_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
416 if ( (sb1
->set_uuid0
== sb2
->set_uuid0
) &&
417 (sb1
->set_uuid1
== sb2
->set_uuid1
) &&
418 (sb1
->set_uuid2
== sb2
->set_uuid2
) &&
419 (sb1
->set_uuid3
== sb2
->set_uuid3
))
427 static int sb_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
430 mdp_super_t
*tmp1
, *tmp2
;
432 tmp1
= kmalloc(sizeof(*tmp1
),GFP_KERNEL
);
433 tmp2
= kmalloc(sizeof(*tmp2
),GFP_KERNEL
);
435 if (!tmp1
|| !tmp2
) {
437 printk(KERN_INFO
"md.c: sb1 is not equal to sb2!\n");
445 * nr_disks is not constant
450 if (memcmp(tmp1
, tmp2
, MD_SB_GENERIC_CONSTANT_WORDS
* 4))
464 static unsigned int calc_sb_csum(mdp_super_t
* sb
)
466 unsigned int disk_csum
, csum
;
468 disk_csum
= sb
->sb_csum
;
470 csum
= csum_partial((void *)sb
, MD_SB_BYTES
, 0);
471 sb
->sb_csum
= disk_csum
;
475 /* csum_partial is not consistent between different architectures.
476 * Some (i386) do a 32bit csum. Some (alpha) do 16 bit.
477 * This makes it hard for user-space to know what to do.
478 * So we use calc_sb_csum to set the checksum to allow working
479 * with older kernels, but allow calc_sb_csum_common to
480 * be used when checking if a checksum is correct, to
481 * make life easier for user-space tools that might write
484 static unsigned int calc_sb_csum_common(mdp_super_t
*super
)
486 unsigned int disk_csum
= super
->sb_csum
;
487 unsigned long long newcsum
= 0;
490 unsigned int *superc
= (int*) super
;
493 for (i
=0; i
<MD_SB_BYTES
/4; i
++)
495 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
496 super
->sb_csum
= disk_csum
;
501 * Handle superblock details.
502 * We want to be able to handle multiple superblock formats
503 * so we have a common interface to them all, and an array of
504 * different handlers.
505 * We rely on user-space to write the initial superblock, and support
506 * reading and updating of superblocks.
507 * Interface methods are:
508 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
509 * loads and validates a superblock on dev.
510 * if refdev != NULL, compare superblocks on both devices
512 * 0 - dev has a superblock that is compatible with refdev
513 * 1 - dev has a superblock that is compatible and newer than refdev
514 * so dev should be used as the refdev in future
515 * -EINVAL superblock incompatible or invalid
516 * -othererror e.g. -EIO
518 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
519 * Verify that dev is acceptable into mddev.
520 * The first time, mddev->raid_disks will be 0, and data from
521 * dev should be merged in. Subsequent calls check that dev
522 * is new enough. Return 0 or -EINVAL
524 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
525 * Update the superblock for rdev with data in mddev
526 * This does not write to disc.
532 struct module
*owner
;
533 int (*load_super
)(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
);
534 int (*validate_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
535 void (*sync_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
539 * load_super for 0.90.0
541 static int super_90_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
543 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
549 * Calculate the position of the superblock,
550 * it's at the end of the disk.
552 * It also happens to be a multiple of 4Kb.
554 sb_offset
= calc_dev_sboffset(rdev
->bdev
);
555 rdev
->sb_offset
= sb_offset
;
557 ret
= read_disk_sb(rdev
);
562 bdevname(rdev
->bdev
, b
);
563 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
565 if (sb
->md_magic
!= MD_SB_MAGIC
) {
566 printk(KERN_ERR
"md: invalid raid superblock magic on %s\n",
571 if (sb
->major_version
!= 0 ||
572 sb
->minor_version
!= 90) {
573 printk(KERN_WARNING
"Bad version number %d.%d on %s\n",
574 sb
->major_version
, sb
->minor_version
,
579 if (sb
->raid_disks
<= 0)
582 if (calc_sb_csum(sb
) != sb
->sb_csum
&&
583 calc_sb_csum_common(sb
) != sb
->sb_csum
) {
584 printk(KERN_WARNING
"md: invalid superblock checksum on %s\n",
589 rdev
->preferred_minor
= sb
->md_minor
;
590 rdev
->data_offset
= 0;
592 if (sb
->level
== MULTIPATH
)
595 rdev
->desc_nr
= sb
->this_disk
.number
;
601 mdp_super_t
*refsb
= (mdp_super_t
*)page_address(refdev
->sb_page
);
602 if (!uuid_equal(refsb
, sb
)) {
603 printk(KERN_WARNING
"md: %s has different UUID to %s\n",
604 b
, bdevname(refdev
->bdev
,b2
));
607 if (!sb_equal(refsb
, sb
)) {
608 printk(KERN_WARNING
"md: %s has same UUID"
609 " but different superblock to %s\n",
610 b
, bdevname(refdev
->bdev
, b2
));
614 ev2
= md_event(refsb
);
620 rdev
->size
= calc_dev_size(rdev
, sb
->chunk_size
);
627 * validate_super for 0.90.0
629 static int super_90_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
632 mdp_super_t
*sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
634 if (mddev
->raid_disks
== 0) {
635 mddev
->major_version
= 0;
636 mddev
->minor_version
= sb
->minor_version
;
637 mddev
->patch_version
= sb
->patch_version
;
638 mddev
->persistent
= ! sb
->not_persistent
;
639 mddev
->chunk_size
= sb
->chunk_size
;
640 mddev
->ctime
= sb
->ctime
;
641 mddev
->utime
= sb
->utime
;
642 mddev
->level
= sb
->level
;
643 mddev
->layout
= sb
->layout
;
644 mddev
->raid_disks
= sb
->raid_disks
;
645 mddev
->size
= sb
->size
;
646 mddev
->events
= md_event(sb
);
648 if (sb
->state
& (1<<MD_SB_CLEAN
))
649 mddev
->recovery_cp
= MaxSector
;
651 if (sb
->events_hi
== sb
->cp_events_hi
&&
652 sb
->events_lo
== sb
->cp_events_lo
) {
653 mddev
->recovery_cp
= sb
->recovery_cp
;
655 mddev
->recovery_cp
= 0;
658 memcpy(mddev
->uuid
+0, &sb
->set_uuid0
, 4);
659 memcpy(mddev
->uuid
+4, &sb
->set_uuid1
, 4);
660 memcpy(mddev
->uuid
+8, &sb
->set_uuid2
, 4);
661 memcpy(mddev
->uuid
+12,&sb
->set_uuid3
, 4);
663 mddev
->max_disks
= MD_SB_DISKS
;
668 if (ev1
< mddev
->events
)
671 if (mddev
->level
!= LEVEL_MULTIPATH
) {
672 rdev
->raid_disk
= -1;
673 rdev
->in_sync
= rdev
->faulty
= 0;
674 desc
= sb
->disks
+ rdev
->desc_nr
;
676 if (desc
->state
& (1<<MD_DISK_FAULTY
))
678 else if (desc
->state
& (1<<MD_DISK_SYNC
) &&
679 desc
->raid_disk
< mddev
->raid_disks
) {
681 rdev
->raid_disk
= desc
->raid_disk
;
688 * sync_super for 0.90.0
690 static void super_90_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
693 struct list_head
*tmp
;
695 int next_spare
= mddev
->raid_disks
;
697 /* make rdev->sb match mddev data..
700 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
701 * 3/ any empty disks < next_spare become removed
703 * disks[0] gets initialised to REMOVED because
704 * we cannot be sure from other fields if it has
705 * been initialised or not.
708 int active
=0, working
=0,failed
=0,spare
=0,nr_disks
=0;
710 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
712 memset(sb
, 0, sizeof(*sb
));
714 sb
->md_magic
= MD_SB_MAGIC
;
715 sb
->major_version
= mddev
->major_version
;
716 sb
->minor_version
= mddev
->minor_version
;
717 sb
->patch_version
= mddev
->patch_version
;
718 sb
->gvalid_words
= 0; /* ignored */
719 memcpy(&sb
->set_uuid0
, mddev
->uuid
+0, 4);
720 memcpy(&sb
->set_uuid1
, mddev
->uuid
+4, 4);
721 memcpy(&sb
->set_uuid2
, mddev
->uuid
+8, 4);
722 memcpy(&sb
->set_uuid3
, mddev
->uuid
+12,4);
724 sb
->ctime
= mddev
->ctime
;
725 sb
->level
= mddev
->level
;
726 sb
->size
= mddev
->size
;
727 sb
->raid_disks
= mddev
->raid_disks
;
728 sb
->md_minor
= mddev
->md_minor
;
729 sb
->not_persistent
= !mddev
->persistent
;
730 sb
->utime
= mddev
->utime
;
732 sb
->events_hi
= (mddev
->events
>>32);
733 sb
->events_lo
= (u32
)mddev
->events
;
737 sb
->recovery_cp
= mddev
->recovery_cp
;
738 sb
->cp_events_hi
= (mddev
->events
>>32);
739 sb
->cp_events_lo
= (u32
)mddev
->events
;
740 if (mddev
->recovery_cp
== MaxSector
)
741 sb
->state
= (1<< MD_SB_CLEAN
);
745 sb
->layout
= mddev
->layout
;
746 sb
->chunk_size
= mddev
->chunk_size
;
748 sb
->disks
[0].state
= (1<<MD_DISK_REMOVED
);
749 ITERATE_RDEV(mddev
,rdev2
,tmp
) {
751 if (rdev2
->raid_disk
>= 0 && rdev2
->in_sync
&& !rdev2
->faulty
)
752 rdev2
->desc_nr
= rdev2
->raid_disk
;
754 rdev2
->desc_nr
= next_spare
++;
755 d
= &sb
->disks
[rdev2
->desc_nr
];
757 d
->number
= rdev2
->desc_nr
;
758 d
->major
= MAJOR(rdev2
->bdev
->bd_dev
);
759 d
->minor
= MINOR(rdev2
->bdev
->bd_dev
);
760 if (rdev2
->raid_disk
>= 0 && rdev
->in_sync
&& !rdev2
->faulty
)
761 d
->raid_disk
= rdev2
->raid_disk
;
763 d
->raid_disk
= rdev2
->desc_nr
; /* compatibility */
765 d
->state
= (1<<MD_DISK_FAULTY
);
767 } else if (rdev2
->in_sync
) {
768 d
->state
= (1<<MD_DISK_ACTIVE
);
769 d
->state
|= (1<<MD_DISK_SYNC
);
779 /* now set the "removed" and "faulty" bits on any missing devices */
780 for (i
=0 ; i
< mddev
->raid_disks
; i
++) {
781 mdp_disk_t
*d
= &sb
->disks
[i
];
782 if (d
->state
== 0 && d
->number
== 0) {
785 d
->state
= (1<<MD_DISK_REMOVED
);
786 d
->state
|= (1<<MD_DISK_FAULTY
);
790 sb
->nr_disks
= nr_disks
;
791 sb
->active_disks
= active
;
792 sb
->working_disks
= working
;
793 sb
->failed_disks
= failed
;
794 sb
->spare_disks
= spare
;
796 sb
->this_disk
= sb
->disks
[rdev
->desc_nr
];
797 sb
->sb_csum
= calc_sb_csum(sb
);
801 * version 1 superblock
804 static unsigned int calc_sb_1_csum(struct mdp_superblock_1
* sb
)
806 unsigned int disk_csum
, csum
;
807 unsigned long long newcsum
;
808 int size
= 256 + sb
->max_dev
*2;
809 unsigned int *isuper
= (unsigned int*)sb
;
812 disk_csum
= sb
->sb_csum
;
815 for (i
=0; size
>=4; size
-= 4 )
816 newcsum
+= le32_to_cpu(*isuper
++);
819 newcsum
+= le16_to_cpu(*(unsigned short*) isuper
);
821 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
822 sb
->sb_csum
= disk_csum
;
826 static int super_1_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
828 struct mdp_superblock_1
*sb
;
831 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
834 * Calculate the position of the superblock.
835 * It is always aligned to a 4K boundary and
836 * depeding on minor_version, it can be:
837 * 0: At least 8K, but less than 12K, from end of device
838 * 1: At start of device
839 * 2: 4K from start of device.
841 switch(minor_version
) {
843 sb_offset
= rdev
->bdev
->bd_inode
->i_size
>> 9;
846 /* convert from sectors to K */
858 rdev
->sb_offset
= sb_offset
;
860 ret
= read_disk_sb(rdev
);
864 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
866 if (sb
->magic
!= cpu_to_le32(MD_SB_MAGIC
) ||
867 sb
->major_version
!= cpu_to_le32(1) ||
868 le32_to_cpu(sb
->max_dev
) > (4096-256)/2 ||
869 le64_to_cpu(sb
->super_offset
) != (rdev
->sb_offset
<<1) ||
870 sb
->feature_map
!= 0)
873 if (calc_sb_1_csum(sb
) != sb
->sb_csum
) {
874 printk("md: invalid superblock checksum on %s\n",
875 bdevname(rdev
->bdev
,b
));
878 rdev
->preferred_minor
= 0xffff;
879 rdev
->data_offset
= le64_to_cpu(sb
->data_offset
);
885 struct mdp_superblock_1
*refsb
=
886 (struct mdp_superblock_1
*)page_address(refdev
->sb_page
);
888 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16) != 0 ||
889 sb
->level
!= refsb
->level
||
890 sb
->layout
!= refsb
->layout
||
891 sb
->chunksize
!= refsb
->chunksize
) {
892 printk(KERN_WARNING
"md: %s has strangely different"
893 " superblock to %s\n",
894 bdevname(rdev
->bdev
,b
),
895 bdevname(refdev
->bdev
,b2
));
898 ev1
= le64_to_cpu(sb
->events
);
899 ev2
= le64_to_cpu(refsb
->events
);
905 rdev
->size
= ((rdev
->bdev
->bd_inode
->i_size
>>9) - le64_to_cpu(sb
->data_offset
)) / 2;
907 rdev
->size
= rdev
->sb_offset
;
908 if (rdev
->size
< le64_to_cpu(sb
->data_size
)/2)
910 rdev
->size
= le64_to_cpu(sb
->data_size
)/2;
911 if (le32_to_cpu(sb
->chunksize
))
912 rdev
->size
&= ~((sector_t
)le32_to_cpu(sb
->chunksize
)/2 - 1);
916 static int super_1_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
918 struct mdp_superblock_1
*sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
920 if (mddev
->raid_disks
== 0) {
921 mddev
->major_version
= 1;
922 mddev
->minor_version
= 0;
923 mddev
->patch_version
= 0;
924 mddev
->persistent
= 1;
925 mddev
->chunk_size
= le32_to_cpu(sb
->chunksize
) << 9;
926 mddev
->ctime
= le64_to_cpu(sb
->ctime
) & ((1ULL << 32)-1);
927 mddev
->utime
= le64_to_cpu(sb
->utime
) & ((1ULL << 32)-1);
928 mddev
->level
= le32_to_cpu(sb
->level
);
929 mddev
->layout
= le32_to_cpu(sb
->layout
);
930 mddev
->raid_disks
= le32_to_cpu(sb
->raid_disks
);
931 mddev
->size
= (u32
)le64_to_cpu(sb
->size
);
932 mddev
->events
= le64_to_cpu(sb
->events
);
934 mddev
->recovery_cp
= le64_to_cpu(sb
->resync_offset
);
935 memcpy(mddev
->uuid
, sb
->set_uuid
, 16);
937 mddev
->max_disks
= (4096-256)/2;
940 ev1
= le64_to_cpu(sb
->events
);
942 if (ev1
< mddev
->events
)
946 if (mddev
->level
!= LEVEL_MULTIPATH
) {
948 rdev
->desc_nr
= le32_to_cpu(sb
->dev_number
);
949 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
951 case 0xffff: /* spare */
954 rdev
->raid_disk
= -1;
956 case 0xfffe: /* faulty */
959 rdev
->raid_disk
= -1;
964 rdev
->raid_disk
= role
;
971 static void super_1_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
973 struct mdp_superblock_1
*sb
;
974 struct list_head
*tmp
;
977 /* make rdev->sb match mddev and rdev data. */
979 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
983 memset(sb
->pad1
, 0, sizeof(sb
->pad1
));
984 memset(sb
->pad2
, 0, sizeof(sb
->pad2
));
985 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
987 sb
->utime
= cpu_to_le64((__u64
)mddev
->utime
);
988 sb
->events
= cpu_to_le64(mddev
->events
);
990 sb
->resync_offset
= cpu_to_le64(mddev
->recovery_cp
);
992 sb
->resync_offset
= cpu_to_le64(0);
995 ITERATE_RDEV(mddev
,rdev2
,tmp
)
996 if (rdev2
->desc_nr
> max_dev
)
997 max_dev
= rdev2
->desc_nr
;
999 sb
->max_dev
= max_dev
;
1000 for (i
=0; i
<max_dev
;i
++)
1001 sb
->dev_roles
[max_dev
] = cpu_to_le16(0xfffe);
1003 ITERATE_RDEV(mddev
,rdev2
,tmp
) {
1006 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1007 else if (rdev2
->in_sync
)
1008 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1010 sb
->dev_roles
[i
] = cpu_to_le16(0xffff);
1013 sb
->recovery_offset
= cpu_to_le64(0); /* not supported yet */
1017 struct super_type super_types
[] = {
1020 .owner
= THIS_MODULE
,
1021 .load_super
= super_90_load
,
1022 .validate_super
= super_90_validate
,
1023 .sync_super
= super_90_sync
,
1027 .owner
= THIS_MODULE
,
1028 .load_super
= super_1_load
,
1029 .validate_super
= super_1_validate
,
1030 .sync_super
= super_1_sync
,
1034 static mdk_rdev_t
* match_dev_unit(mddev_t
*mddev
, mdk_rdev_t
*dev
)
1036 struct list_head
*tmp
;
1039 ITERATE_RDEV(mddev
,rdev
,tmp
)
1040 if (rdev
->bdev
->bd_contains
== dev
->bdev
->bd_contains
)
1046 static int match_mddev_units(mddev_t
*mddev1
, mddev_t
*mddev2
)
1048 struct list_head
*tmp
;
1051 ITERATE_RDEV(mddev1
,rdev
,tmp
)
1052 if (match_dev_unit(mddev2
, rdev
))
1058 static LIST_HEAD(pending_raid_disks
);
1060 static int bind_rdev_to_array(mdk_rdev_t
* rdev
, mddev_t
* mddev
)
1062 mdk_rdev_t
*same_pdev
;
1063 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1069 same_pdev
= match_dev_unit(mddev
, rdev
);
1072 "%s: WARNING: %s appears to be on the same physical"
1073 " disk as %s. True\n protection against single-disk"
1074 " failure might be compromised.\n",
1075 mdname(mddev
), bdevname(rdev
->bdev
,b
),
1076 bdevname(same_pdev
->bdev
,b2
));
1078 /* Verify rdev->desc_nr is unique.
1079 * If it is -1, assign a free number, else
1080 * check number is not in use
1082 if (rdev
->desc_nr
< 0) {
1084 if (mddev
->pers
) choice
= mddev
->raid_disks
;
1085 while (find_rdev_nr(mddev
, choice
))
1087 rdev
->desc_nr
= choice
;
1089 if (find_rdev_nr(mddev
, rdev
->desc_nr
))
1093 list_add(&rdev
->same_set
, &mddev
->disks
);
1094 rdev
->mddev
= mddev
;
1095 printk(KERN_INFO
"md: bind<%s>\n", bdevname(rdev
->bdev
,b
));
1099 static void unbind_rdev_from_array(mdk_rdev_t
* rdev
)
1101 char b
[BDEVNAME_SIZE
];
1106 list_del_init(&rdev
->same_set
);
1107 printk(KERN_INFO
"md: unbind<%s>\n", bdevname(rdev
->bdev
,b
));
1112 * prevent the device from being mounted, repartitioned or
1113 * otherwise reused by a RAID array (or any other kernel
1114 * subsystem), by bd_claiming the device.
1116 static int lock_rdev(mdk_rdev_t
*rdev
, dev_t dev
)
1119 struct block_device
*bdev
;
1120 char b
[BDEVNAME_SIZE
];
1122 bdev
= open_by_devnum(dev
, FMODE_READ
|FMODE_WRITE
);
1124 printk(KERN_ERR
"md: could not open %s.\n",
1125 __bdevname(dev
, b
));
1126 return PTR_ERR(bdev
);
1128 err
= bd_claim(bdev
, rdev
);
1130 printk(KERN_ERR
"md: could not bd_claim %s.\n",
1139 static void unlock_rdev(mdk_rdev_t
*rdev
)
1141 struct block_device
*bdev
= rdev
->bdev
;
1149 void md_autodetect_dev(dev_t dev
);
1151 static void export_rdev(mdk_rdev_t
* rdev
)
1153 char b
[BDEVNAME_SIZE
];
1154 printk(KERN_INFO
"md: export_rdev(%s)\n",
1155 bdevname(rdev
->bdev
,b
));
1159 list_del_init(&rdev
->same_set
);
1161 md_autodetect_dev(rdev
->bdev
->bd_dev
);
1167 static void kick_rdev_from_array(mdk_rdev_t
* rdev
)
1169 unbind_rdev_from_array(rdev
);
1173 static void export_array(mddev_t
*mddev
)
1175 struct list_head
*tmp
;
1178 ITERATE_RDEV(mddev
,rdev
,tmp
) {
1183 kick_rdev_from_array(rdev
);
1185 if (!list_empty(&mddev
->disks
))
1187 mddev
->raid_disks
= 0;
1188 mddev
->major_version
= 0;
1191 static void print_desc(mdp_disk_t
*desc
)
1193 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc
->number
,
1194 desc
->major
,desc
->minor
,desc
->raid_disk
,desc
->state
);
1197 static void print_sb(mdp_super_t
*sb
)
1202 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1203 sb
->major_version
, sb
->minor_version
, sb
->patch_version
,
1204 sb
->set_uuid0
, sb
->set_uuid1
, sb
->set_uuid2
, sb
->set_uuid3
,
1206 printk(KERN_INFO
"md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1207 sb
->level
, sb
->size
, sb
->nr_disks
, sb
->raid_disks
,
1208 sb
->md_minor
, sb
->layout
, sb
->chunk_size
);
1209 printk(KERN_INFO
"md: UT:%08x ST:%d AD:%d WD:%d"
1210 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1211 sb
->utime
, sb
->state
, sb
->active_disks
, sb
->working_disks
,
1212 sb
->failed_disks
, sb
->spare_disks
,
1213 sb
->sb_csum
, (unsigned long)sb
->events_lo
);
1216 for (i
= 0; i
< MD_SB_DISKS
; i
++) {
1219 desc
= sb
->disks
+ i
;
1220 if (desc
->number
|| desc
->major
|| desc
->minor
||
1221 desc
->raid_disk
|| (desc
->state
&& (desc
->state
!= 4))) {
1222 printk(" D %2d: ", i
);
1226 printk(KERN_INFO
"md: THIS: ");
1227 print_desc(&sb
->this_disk
);
1231 static void print_rdev(mdk_rdev_t
*rdev
)
1233 char b
[BDEVNAME_SIZE
];
1234 printk(KERN_INFO
"md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1235 bdevname(rdev
->bdev
,b
), (unsigned long long)rdev
->size
,
1236 rdev
->faulty
, rdev
->in_sync
, rdev
->desc_nr
);
1237 if (rdev
->sb_loaded
) {
1238 printk(KERN_INFO
"md: rdev superblock:\n");
1239 print_sb((mdp_super_t
*)page_address(rdev
->sb_page
));
1241 printk(KERN_INFO
"md: no rdev superblock!\n");
1244 void md_print_devices(void)
1246 struct list_head
*tmp
, *tmp2
;
1249 char b
[BDEVNAME_SIZE
];
1252 printk("md: **********************************\n");
1253 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1254 printk("md: **********************************\n");
1255 ITERATE_MDDEV(mddev
,tmp
) {
1256 printk("%s: ", mdname(mddev
));
1258 ITERATE_RDEV(mddev
,rdev
,tmp2
)
1259 printk("<%s>", bdevname(rdev
->bdev
,b
));
1262 ITERATE_RDEV(mddev
,rdev
,tmp2
)
1265 printk("md: **********************************\n");
1270 static int write_disk_sb(mdk_rdev_t
* rdev
)
1272 char b
[BDEVNAME_SIZE
];
1273 if (!rdev
->sb_loaded
) {
1282 dprintk(KERN_INFO
"(write) %s's sb offset: %llu\n",
1283 bdevname(rdev
->bdev
,b
),
1284 (unsigned long long)rdev
->sb_offset
);
1286 if (sync_page_io(rdev
->bdev
, rdev
->sb_offset
<<1, MD_SB_BYTES
, rdev
->sb_page
, WRITE
))
1289 printk("md: write_disk_sb failed for device %s\n",
1290 bdevname(rdev
->bdev
,b
));
1294 static void sync_sbs(mddev_t
* mddev
)
1297 struct list_head
*tmp
;
1299 ITERATE_RDEV(mddev
,rdev
,tmp
) {
1300 super_types
[mddev
->major_version
].
1301 sync_super(mddev
, rdev
);
1302 rdev
->sb_loaded
= 1;
1306 static void md_update_sb(mddev_t
* mddev
)
1308 int err
, count
= 100;
1309 struct list_head
*tmp
;
1312 mddev
->sb_dirty
= 0;
1314 mddev
->utime
= get_seconds();
1317 if (!mddev
->events
) {
1319 * oops, this 64-bit counter should never wrap.
1320 * Either we are in around ~1 trillion A.C., assuming
1321 * 1 reboot per second, or we have a bug:
1329 * do not write anything to disk if using
1330 * nonpersistent superblocks
1332 if (!mddev
->persistent
)
1336 "md: updating %s RAID superblock on device (in sync %d)\n",
1337 mdname(mddev
),mddev
->in_sync
);
1340 ITERATE_RDEV(mddev
,rdev
,tmp
) {
1341 char b
[BDEVNAME_SIZE
];
1342 dprintk(KERN_INFO
"md: ");
1344 dprintk("(skipping faulty ");
1346 dprintk("%s ", bdevname(rdev
->bdev
,b
));
1347 if (!rdev
->faulty
) {
1348 err
+= write_disk_sb(rdev
);
1351 if (!err
&& mddev
->level
== LEVEL_MULTIPATH
)
1352 /* only need to write one superblock... */
1357 printk(KERN_ERR
"md: errors occurred during superblock"
1358 " update, repeating\n");
1362 "md: excessive errors occurred during superblock update, exiting\n");
1367 * Import a device. If 'super_format' >= 0, then sanity check the superblock
1369 * mark the device faulty if:
1371 * - the device is nonexistent (zero size)
1372 * - the device has no valid superblock
1374 * a faulty rdev _never_ has rdev->sb set.
1376 static mdk_rdev_t
*md_import_device(dev_t newdev
, int super_format
, int super_minor
)
1378 char b
[BDEVNAME_SIZE
];
1383 rdev
= (mdk_rdev_t
*) kmalloc(sizeof(*rdev
), GFP_KERNEL
);
1385 printk(KERN_ERR
"md: could not alloc mem for new device!\n");
1386 return ERR_PTR(-ENOMEM
);
1388 memset(rdev
, 0, sizeof(*rdev
));
1390 if ((err
= alloc_disk_sb(rdev
)))
1393 err
= lock_rdev(rdev
, newdev
);
1400 rdev
->data_offset
= 0;
1401 atomic_set(&rdev
->nr_pending
, 0);
1403 size
= rdev
->bdev
->bd_inode
->i_size
>> BLOCK_SIZE_BITS
;
1406 "md: %s has zero or unknown size, marking faulty!\n",
1407 bdevname(rdev
->bdev
,b
));
1412 if (super_format
>= 0) {
1413 err
= super_types
[super_format
].
1414 load_super(rdev
, NULL
, super_minor
);
1415 if (err
== -EINVAL
) {
1417 "md: %s has invalid sb, not importing!\n",
1418 bdevname(rdev
->bdev
,b
));
1423 "md: could not read %s's sb, not importing!\n",
1424 bdevname(rdev
->bdev
,b
));
1428 INIT_LIST_HEAD(&rdev
->same_set
);
1433 if (rdev
->sb_page
) {
1439 return ERR_PTR(err
);
1443 * Check a full RAID array for plausibility
1447 static int analyze_sbs(mddev_t
* mddev
)
1450 struct list_head
*tmp
;
1451 mdk_rdev_t
*rdev
, *freshest
;
1452 char b
[BDEVNAME_SIZE
];
1455 ITERATE_RDEV(mddev
,rdev
,tmp
)
1456 switch (super_types
[mddev
->major_version
].
1457 load_super(rdev
, freshest
, mddev
->minor_version
)) {
1465 "md: fatal superblock inconsistency in %s"
1466 " -- removing from array\n",
1467 bdevname(rdev
->bdev
,b
));
1468 kick_rdev_from_array(rdev
);
1472 super_types
[mddev
->major_version
].
1473 validate_super(mddev
, freshest
);
1476 ITERATE_RDEV(mddev
,rdev
,tmp
) {
1477 if (rdev
!= freshest
)
1478 if (super_types
[mddev
->major_version
].
1479 validate_super(mddev
, rdev
)) {
1480 printk(KERN_WARNING
"md: kicking non-fresh %s"
1482 bdevname(rdev
->bdev
,b
));
1483 kick_rdev_from_array(rdev
);
1486 if (mddev
->level
== LEVEL_MULTIPATH
) {
1487 rdev
->desc_nr
= i
++;
1488 rdev
->raid_disk
= rdev
->desc_nr
;
1495 * Check if we can support this RAID array
1497 if (mddev
->major_version
!= MD_MAJOR_VERSION
||
1498 mddev
->minor_version
> MD_MINOR_VERSION
) {
1500 "md: %s: unsupported raid array version %d.%d.%d\n",
1501 mdname(mddev
), mddev
->major_version
,
1502 mddev
->minor_version
, mddev
->patch_version
);
1506 if ((mddev
->recovery_cp
!= MaxSector
) &&
1507 ((mddev
->level
== 1) ||
1508 ((mddev
->level
>= 4) && (mddev
->level
<= 6))))
1509 printk(KERN_ERR
"md: %s: raid array is not clean"
1510 " -- starting background reconstruction\n",
1520 static struct kobject
*md_probe(dev_t dev
, int *part
, void *data
)
1522 static DECLARE_MUTEX(disks_sem
);
1523 mddev_t
*mddev
= mddev_find(dev
);
1524 struct gendisk
*disk
;
1525 int partitioned
= (MAJOR(dev
) != MD_MAJOR
);
1526 int shift
= partitioned
? MdpMinorShift
: 0;
1527 int unit
= MINOR(dev
) >> shift
;
1533 if (mddev
->gendisk
) {
1538 disk
= alloc_disk(1 << shift
);
1544 disk
->major
= MAJOR(dev
);
1545 disk
->first_minor
= unit
<< shift
;
1547 sprintf(disk
->disk_name
, "md_d%d", unit
);
1549 sprintf(disk
->disk_name
, "md%d", unit
);
1550 disk
->fops
= &md_fops
;
1551 disk
->private_data
= mddev
;
1552 disk
->queue
= mddev
->queue
;
1554 mddev
->gendisk
= disk
;
1559 void md_wakeup_thread(mdk_thread_t
*thread
);
1561 static void md_safemode_timeout(unsigned long data
)
1563 mddev_t
*mddev
= (mddev_t
*) data
;
1565 mddev
->safemode
= 1;
1566 md_wakeup_thread(mddev
->thread
);
1570 static int do_md_run(mddev_t
* mddev
)
1574 struct list_head
*tmp
;
1576 struct gendisk
*disk
;
1577 char b
[BDEVNAME_SIZE
];
1579 if (list_empty(&mddev
->disks
)) {
1588 * Analyze all RAID superblock(s)
1590 if (!mddev
->raid_disks
&& analyze_sbs(mddev
)) {
1595 chunk_size
= mddev
->chunk_size
;
1596 pnum
= level_to_pers(mddev
->level
);
1598 if ((pnum
!= MULTIPATH
) && (pnum
!= RAID1
)) {
1601 * 'default chunksize' in the old md code used to
1602 * be PAGE_SIZE, baaad.
1603 * we abort here to be on the safe side. We don't
1604 * want to continue the bad practice.
1607 "no chunksize specified, see 'man raidtab'\n");
1610 if (chunk_size
> MAX_CHUNK_SIZE
) {
1611 printk(KERN_ERR
"too big chunk_size: %d > %d\n",
1612 chunk_size
, MAX_CHUNK_SIZE
);
1616 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
1618 if ( (1 << ffz(~chunk_size
)) != chunk_size
) {
1622 if (chunk_size
< PAGE_SIZE
) {
1623 printk(KERN_ERR
"too small chunk_size: %d < %ld\n",
1624 chunk_size
, PAGE_SIZE
);
1628 /* devices must have minimum size of one chunk */
1629 ITERATE_RDEV(mddev
,rdev
,tmp
) {
1632 if (rdev
->size
< chunk_size
/ 1024) {
1634 "md: Dev %s smaller than chunk_size:"
1636 bdevname(rdev
->bdev
,b
),
1637 (unsigned long long)rdev
->size
,
1644 if (pnum
>= MAX_PERSONALITY
) {
1652 request_module("md-personality-%d", pnum
);
1657 * Drop all container device buffers, from now on
1658 * the only valid external interface is through the md
1660 * Also find largest hardsector size
1662 ITERATE_RDEV(mddev
,rdev
,tmp
) {
1665 sync_blockdev(rdev
->bdev
);
1666 invalidate_bdev(rdev
->bdev
, 0);
1669 md_probe(mddev
->unit
, NULL
, NULL
);
1670 disk
= mddev
->gendisk
;
1674 spin_lock(&pers_lock
);
1675 if (!pers
[pnum
] || !try_module_get(pers
[pnum
]->owner
)) {
1676 spin_unlock(&pers_lock
);
1677 printk(KERN_WARNING
"md: personality %d is not loaded!\n",
1682 mddev
->pers
= pers
[pnum
];
1683 spin_unlock(&pers_lock
);
1685 mddev
->resync_max_sectors
= mddev
->size
<< 1; /* may be over-ridden by personality */
1687 err
= mddev
->pers
->run(mddev
);
1689 printk(KERN_ERR
"md: pers->run() failed ...\n");
1690 module_put(mddev
->pers
->owner
);
1694 atomic_set(&mddev
->writes_pending
,0);
1695 mddev
->safemode
= 0;
1696 mddev
->safemode_timer
.function
= md_safemode_timeout
;
1697 mddev
->safemode_timer
.data
= (unsigned long) mddev
;
1698 mddev
->safemode_delay
= (20 * HZ
)/1000 +1; /* 20 msec delay */
1701 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
1703 if (mddev
->sb_dirty
)
1704 md_update_sb(mddev
);
1706 set_capacity(disk
, mddev
->array_size
<<1);
1708 /* If we call blk_queue_make_request here, it will
1709 * re-initialise max_sectors etc which may have been
1710 * refined inside -> run. So just set the bits we need to set.
1711 * Most initialisation happended when we called
1712 * blk_queue_make_request(..., md_fail_request)
1715 mddev
->queue
->queuedata
= mddev
;
1716 mddev
->queue
->make_request_fn
= mddev
->pers
->make_request
;
1717 mddev
->queue
->issue_flush_fn
= md_flush_all
;
1723 static int restart_array(mddev_t
*mddev
)
1725 struct gendisk
*disk
= mddev
->gendisk
;
1729 * Complain if it has no devices
1732 if (list_empty(&mddev
->disks
))
1740 mddev
->safemode
= 0;
1742 set_disk_ro(disk
, 0);
1744 printk(KERN_INFO
"md: %s switched to read-write mode.\n",
1747 * Kick recovery or resync if necessary
1749 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
1750 md_wakeup_thread(mddev
->thread
);
1753 printk(KERN_ERR
"md: %s has no personality assigned.\n",
1762 static int do_md_stop(mddev_t
* mddev
, int ro
)
1765 struct gendisk
*disk
= mddev
->gendisk
;
1768 if (atomic_read(&mddev
->active
)>2) {
1769 printk("md: %s still in use.\n",mdname(mddev
));
1773 if (mddev
->sync_thread
) {
1774 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
1775 md_unregister_thread(mddev
->sync_thread
);
1776 mddev
->sync_thread
= NULL
;
1779 del_timer_sync(&mddev
->safemode_timer
);
1781 invalidate_partition(disk
, 0);
1790 set_disk_ro(disk
, 0);
1791 blk_queue_make_request(mddev
->queue
, md_fail_request
);
1792 mddev
->pers
->stop(mddev
);
1793 module_put(mddev
->pers
->owner
);
1798 if (!mddev
->in_sync
) {
1799 /* mark array as shutdown cleanly */
1801 md_update_sb(mddev
);
1804 set_disk_ro(disk
, 1);
1807 * Free resources if final stop
1810 struct gendisk
*disk
;
1811 printk(KERN_INFO
"md: %s stopped.\n", mdname(mddev
));
1813 export_array(mddev
);
1815 mddev
->array_size
= 0;
1816 disk
= mddev
->gendisk
;
1818 set_capacity(disk
, 0);
1821 printk(KERN_INFO
"md: %s switched to read-only mode.\n",
1828 static void autorun_array(mddev_t
*mddev
)
1831 struct list_head
*tmp
;
1834 if (list_empty(&mddev
->disks
)) {
1839 printk(KERN_INFO
"md: running: ");
1841 ITERATE_RDEV(mddev
,rdev
,tmp
) {
1842 char b
[BDEVNAME_SIZE
];
1843 printk("<%s>", bdevname(rdev
->bdev
,b
));
1847 err
= do_md_run (mddev
);
1849 printk(KERN_WARNING
"md :do_md_run() returned %d\n", err
);
1850 do_md_stop (mddev
, 0);
1855 * lets try to run arrays based on all disks that have arrived
1856 * until now. (those are in pending_raid_disks)
1858 * the method: pick the first pending disk, collect all disks with
1859 * the same UUID, remove all from the pending list and put them into
1860 * the 'same_array' list. Then order this list based on superblock
1861 * update time (freshest comes first), kick out 'old' disks and
1862 * compare superblocks. If everything's fine then run it.
1864 * If "unit" is allocated, then bump its reference count
1866 static void autorun_devices(int part
)
1868 struct list_head candidates
;
1869 struct list_head
*tmp
;
1870 mdk_rdev_t
*rdev0
, *rdev
;
1872 char b
[BDEVNAME_SIZE
];
1874 printk(KERN_INFO
"md: autorun ...\n");
1875 while (!list_empty(&pending_raid_disks
)) {
1877 rdev0
= list_entry(pending_raid_disks
.next
,
1878 mdk_rdev_t
, same_set
);
1880 printk(KERN_INFO
"md: considering %s ...\n",
1881 bdevname(rdev0
->bdev
,b
));
1882 INIT_LIST_HEAD(&candidates
);
1883 ITERATE_RDEV_PENDING(rdev
,tmp
)
1884 if (super_90_load(rdev
, rdev0
, 0) >= 0) {
1885 printk(KERN_INFO
"md: adding %s ...\n",
1886 bdevname(rdev
->bdev
,b
));
1887 list_move(&rdev
->same_set
, &candidates
);
1890 * now we have a set of devices, with all of them having
1891 * mostly sane superblocks. It's time to allocate the
1894 if (rdev0
->preferred_minor
< 0 || rdev0
->preferred_minor
>= MAX_MD_DEVS
) {
1895 printk(KERN_INFO
"md: unit number in %s is bad: %d\n",
1896 bdevname(rdev0
->bdev
, b
), rdev0
->preferred_minor
);
1900 dev
= MKDEV(mdp_major
,
1901 rdev0
->preferred_minor
<< MdpMinorShift
);
1903 dev
= MKDEV(MD_MAJOR
, rdev0
->preferred_minor
);
1905 md_probe(dev
, NULL
, NULL
);
1906 mddev
= mddev_find(dev
);
1909 "md: cannot allocate memory for md drive.\n");
1912 if (mddev_lock(mddev
))
1913 printk(KERN_WARNING
"md: %s locked, cannot run\n",
1915 else if (mddev
->raid_disks
|| mddev
->major_version
1916 || !list_empty(&mddev
->disks
)) {
1918 "md: %s already running, cannot run %s\n",
1919 mdname(mddev
), bdevname(rdev0
->bdev
,b
));
1920 mddev_unlock(mddev
);
1922 printk(KERN_INFO
"md: created %s\n", mdname(mddev
));
1923 ITERATE_RDEV_GENERIC(candidates
,rdev
,tmp
) {
1924 list_del_init(&rdev
->same_set
);
1925 if (bind_rdev_to_array(rdev
, mddev
))
1928 autorun_array(mddev
);
1929 mddev_unlock(mddev
);
1931 /* on success, candidates will be empty, on error
1934 ITERATE_RDEV_GENERIC(candidates
,rdev
,tmp
)
1938 printk(KERN_INFO
"md: ... autorun DONE.\n");
1942 * import RAID devices based on one partition
1943 * if possible, the array gets run as well.
1946 static int autostart_array(dev_t startdev
)
1948 char b
[BDEVNAME_SIZE
];
1949 int err
= -EINVAL
, i
;
1950 mdp_super_t
*sb
= NULL
;
1951 mdk_rdev_t
*start_rdev
= NULL
, *rdev
;
1953 start_rdev
= md_import_device(startdev
, 0, 0);
1954 if (IS_ERR(start_rdev
))
1958 /* NOTE: this can only work for 0.90.0 superblocks */
1959 sb
= (mdp_super_t
*)page_address(start_rdev
->sb_page
);
1960 if (sb
->major_version
!= 0 ||
1961 sb
->minor_version
!= 90 ) {
1962 printk(KERN_WARNING
"md: can only autostart 0.90.0 arrays\n");
1963 export_rdev(start_rdev
);
1967 if (start_rdev
->faulty
) {
1969 "md: can not autostart based on faulty %s!\n",
1970 bdevname(start_rdev
->bdev
,b
));
1971 export_rdev(start_rdev
);
1974 list_add(&start_rdev
->same_set
, &pending_raid_disks
);
1976 for (i
= 0; i
< MD_SB_DISKS
; i
++) {
1977 mdp_disk_t
*desc
= sb
->disks
+ i
;
1978 dev_t dev
= MKDEV(desc
->major
, desc
->minor
);
1982 if (dev
== startdev
)
1984 if (MAJOR(dev
) != desc
->major
|| MINOR(dev
) != desc
->minor
)
1986 rdev
= md_import_device(dev
, 0, 0);
1990 list_add(&rdev
->same_set
, &pending_raid_disks
);
1994 * possibly return codes
2002 static int get_version(void __user
* arg
)
2006 ver
.major
= MD_MAJOR_VERSION
;
2007 ver
.minor
= MD_MINOR_VERSION
;
2008 ver
.patchlevel
= MD_PATCHLEVEL_VERSION
;
2010 if (copy_to_user(arg
, &ver
, sizeof(ver
)))
2016 static int get_array_info(mddev_t
* mddev
, void __user
* arg
)
2018 mdu_array_info_t info
;
2019 int nr
,working
,active
,failed
,spare
;
2021 struct list_head
*tmp
;
2023 nr
=working
=active
=failed
=spare
=0;
2024 ITERATE_RDEV(mddev
,rdev
,tmp
) {
2037 info
.major_version
= mddev
->major_version
;
2038 info
.minor_version
= mddev
->minor_version
;
2039 info
.patch_version
= 1;
2040 info
.ctime
= mddev
->ctime
;
2041 info
.level
= mddev
->level
;
2042 info
.size
= mddev
->size
;
2044 info
.raid_disks
= mddev
->raid_disks
;
2045 info
.md_minor
= mddev
->md_minor
;
2046 info
.not_persistent
= !mddev
->persistent
;
2048 info
.utime
= mddev
->utime
;
2051 info
.state
= (1<<MD_SB_CLEAN
);
2052 info
.active_disks
= active
;
2053 info
.working_disks
= working
;
2054 info
.failed_disks
= failed
;
2055 info
.spare_disks
= spare
;
2057 info
.layout
= mddev
->layout
;
2058 info
.chunk_size
= mddev
->chunk_size
;
2060 if (copy_to_user(arg
, &info
, sizeof(info
)))
2066 static int get_disk_info(mddev_t
* mddev
, void __user
* arg
)
2068 mdu_disk_info_t info
;
2072 if (copy_from_user(&info
, arg
, sizeof(info
)))
2077 rdev
= find_rdev_nr(mddev
, nr
);
2079 info
.major
= MAJOR(rdev
->bdev
->bd_dev
);
2080 info
.minor
= MINOR(rdev
->bdev
->bd_dev
);
2081 info
.raid_disk
= rdev
->raid_disk
;
2084 info
.state
|= (1<<MD_DISK_FAULTY
);
2085 else if (rdev
->in_sync
) {
2086 info
.state
|= (1<<MD_DISK_ACTIVE
);
2087 info
.state
|= (1<<MD_DISK_SYNC
);
2090 info
.major
= info
.minor
= 0;
2091 info
.raid_disk
= -1;
2092 info
.state
= (1<<MD_DISK_REMOVED
);
2095 if (copy_to_user(arg
, &info
, sizeof(info
)))
2101 static int add_new_disk(mddev_t
* mddev
, mdu_disk_info_t
*info
)
2103 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
2105 dev_t dev
= MKDEV(info
->major
,info
->minor
);
2107 if (info
->major
!= MAJOR(dev
) || info
->minor
!= MINOR(dev
))
2110 if (!mddev
->raid_disks
) {
2112 /* expecting a device which has a superblock */
2113 rdev
= md_import_device(dev
, mddev
->major_version
, mddev
->minor_version
);
2116 "md: md_import_device returned %ld\n",
2118 return PTR_ERR(rdev
);
2120 if (!list_empty(&mddev
->disks
)) {
2121 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
2122 mdk_rdev_t
, same_set
);
2123 int err
= super_types
[mddev
->major_version
]
2124 .load_super(rdev
, rdev0
, mddev
->minor_version
);
2127 "md: %s has different UUID to %s\n",
2128 bdevname(rdev
->bdev
,b
),
2129 bdevname(rdev0
->bdev
,b2
));
2134 err
= bind_rdev_to_array(rdev
, mddev
);
2141 * add_new_disk can be used once the array is assembled
2142 * to add "hot spares". They must already have a superblock
2147 if (!mddev
->pers
->hot_add_disk
) {
2149 "%s: personality does not support diskops!\n",
2153 rdev
= md_import_device(dev
, mddev
->major_version
,
2154 mddev
->minor_version
);
2157 "md: md_import_device returned %ld\n",
2159 return PTR_ERR(rdev
);
2161 rdev
->in_sync
= 0; /* just to be sure */
2162 rdev
->raid_disk
= -1;
2163 err
= bind_rdev_to_array(rdev
, mddev
);
2167 md_wakeup_thread(mddev
->thread
);
2171 /* otherwise, add_new_disk is only allowed
2172 * for major_version==0 superblocks
2174 if (mddev
->major_version
!= 0) {
2175 printk(KERN_WARNING
"%s: ADD_NEW_DISK not supported\n",
2180 if (!(info
->state
& (1<<MD_DISK_FAULTY
))) {
2182 rdev
= md_import_device (dev
, -1, 0);
2185 "md: error, md_import_device() returned %ld\n",
2187 return PTR_ERR(rdev
);
2189 rdev
->desc_nr
= info
->number
;
2190 if (info
->raid_disk
< mddev
->raid_disks
)
2191 rdev
->raid_disk
= info
->raid_disk
;
2193 rdev
->raid_disk
= -1;
2196 if (rdev
->raid_disk
< mddev
->raid_disks
)
2197 rdev
->in_sync
= (info
->state
& (1<<MD_DISK_SYNC
));
2201 err
= bind_rdev_to_array(rdev
, mddev
);
2207 if (!mddev
->persistent
) {
2208 printk(KERN_INFO
"md: nonpersistent superblock ...\n");
2209 rdev
->sb_offset
= rdev
->bdev
->bd_inode
->i_size
>> BLOCK_SIZE_BITS
;
2211 rdev
->sb_offset
= calc_dev_sboffset(rdev
->bdev
);
2212 rdev
->size
= calc_dev_size(rdev
, mddev
->chunk_size
);
2214 if (!mddev
->size
|| (mddev
->size
> rdev
->size
))
2215 mddev
->size
= rdev
->size
;
2221 static int hot_remove_disk(mddev_t
* mddev
, dev_t dev
)
2223 char b
[BDEVNAME_SIZE
];
2229 rdev
= find_rdev(mddev
, dev
);
2233 if (rdev
->raid_disk
>= 0)
2236 kick_rdev_from_array(rdev
);
2237 md_update_sb(mddev
);
2241 printk(KERN_WARNING
"md: cannot remove active disk %s from %s ... \n",
2242 bdevname(rdev
->bdev
,b
), mdname(mddev
));
2246 static int hot_add_disk(mddev_t
* mddev
, dev_t dev
)
2248 char b
[BDEVNAME_SIZE
];
2256 if (mddev
->major_version
!= 0) {
2257 printk(KERN_WARNING
"%s: HOT_ADD may only be used with"
2258 " version-0 superblocks.\n",
2262 if (!mddev
->pers
->hot_add_disk
) {
2264 "%s: personality does not support diskops!\n",
2269 rdev
= md_import_device (dev
, -1, 0);
2272 "md: error, md_import_device() returned %ld\n",
2277 if (mddev
->persistent
)
2278 rdev
->sb_offset
= calc_dev_sboffset(rdev
->bdev
);
2281 rdev
->bdev
->bd_inode
->i_size
>> BLOCK_SIZE_BITS
;
2283 size
= calc_dev_size(rdev
, mddev
->chunk_size
);
2286 if (size
< mddev
->size
) {
2288 "%s: disk size %llu blocks < array size %llu\n",
2289 mdname(mddev
), (unsigned long long)size
,
2290 (unsigned long long)mddev
->size
);
2297 "md: can not hot-add faulty %s disk to %s!\n",
2298 bdevname(rdev
->bdev
,b
), mdname(mddev
));
2304 bind_rdev_to_array(rdev
, mddev
);
2307 * The rest should better be atomic, we can have disk failures
2308 * noticed in interrupt contexts ...
2311 if (rdev
->desc_nr
== mddev
->max_disks
) {
2312 printk(KERN_WARNING
"%s: can not hot-add to full array!\n",
2315 goto abort_unbind_export
;
2318 rdev
->raid_disk
= -1;
2320 md_update_sb(mddev
);
2323 * Kick recovery, maybe this spare has to be added to the
2324 * array immediately.
2326 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
2327 md_wakeup_thread(mddev
->thread
);
2331 abort_unbind_export
:
2332 unbind_rdev_from_array(rdev
);
2340 * set_array_info is used two different ways
2341 * The original usage is when creating a new array.
2342 * In this usage, raid_disks is > 0 and it together with
2343 * level, size, not_persistent,layout,chunksize determine the
2344 * shape of the array.
2345 * This will always create an array with a type-0.90.0 superblock.
2346 * The newer usage is when assembling an array.
2347 * In this case raid_disks will be 0, and the major_version field is
2348 * use to determine which style super-blocks are to be found on the devices.
2349 * The minor and patch _version numbers are also kept incase the
2350 * super_block handler wishes to interpret them.
2352 static int set_array_info(mddev_t
* mddev
, mdu_array_info_t
*info
)
2355 if (info
->raid_disks
== 0) {
2356 /* just setting version number for superblock loading */
2357 if (info
->major_version
< 0 ||
2358 info
->major_version
>= sizeof(super_types
)/sizeof(super_types
[0]) ||
2359 super_types
[info
->major_version
].name
== NULL
) {
2360 /* maybe try to auto-load a module? */
2362 "md: superblock version %d not known\n",
2363 info
->major_version
);
2366 mddev
->major_version
= info
->major_version
;
2367 mddev
->minor_version
= info
->minor_version
;
2368 mddev
->patch_version
= info
->patch_version
;
2371 mddev
->major_version
= MD_MAJOR_VERSION
;
2372 mddev
->minor_version
= MD_MINOR_VERSION
;
2373 mddev
->patch_version
= MD_PATCHLEVEL_VERSION
;
2374 mddev
->ctime
= get_seconds();
2376 mddev
->level
= info
->level
;
2377 mddev
->size
= info
->size
;
2378 mddev
->raid_disks
= info
->raid_disks
;
2379 /* don't set md_minor, it is determined by which /dev/md* was
2382 if (info
->state
& (1<<MD_SB_CLEAN
))
2383 mddev
->recovery_cp
= MaxSector
;
2385 mddev
->recovery_cp
= 0;
2386 mddev
->persistent
= ! info
->not_persistent
;
2388 mddev
->layout
= info
->layout
;
2389 mddev
->chunk_size
= info
->chunk_size
;
2391 mddev
->max_disks
= MD_SB_DISKS
;
2393 mddev
->sb_dirty
= 1;
2396 * Generate a 128 bit UUID
2398 get_random_bytes(mddev
->uuid
, 16);
2404 * update_array_info is used to change the configuration of an
2406 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
2407 * fields in the info are checked against the array.
2408 * Any differences that cannot be handled will cause an error.
2409 * Normally, only one change can be managed at a time.
2411 static int update_array_info(mddev_t
*mddev
, mdu_array_info_t
*info
)
2416 if (mddev
->major_version
!= info
->major_version
||
2417 mddev
->minor_version
!= info
->minor_version
||
2418 /* mddev->patch_version != info->patch_version || */
2419 mddev
->ctime
!= info
->ctime
||
2420 mddev
->level
!= info
->level
||
2421 mddev
->layout
!= info
->layout
||
2422 !mddev
->persistent
!= info
->not_persistent
||
2423 mddev
->chunk_size
!= info
->chunk_size
)
2425 /* Check there is only one change */
2426 if (mddev
->size
!= info
->size
) cnt
++;
2427 if (mddev
->raid_disks
!= info
->raid_disks
) cnt
++;
2428 if (cnt
== 0) return 0;
2429 if (cnt
> 1) return -EINVAL
;
2431 if (mddev
->size
!= info
->size
) {
2433 struct list_head
*tmp
;
2434 if (mddev
->pers
->resize
== NULL
)
2436 /* The "size" is the amount of each device that is used.
2437 * This can only make sense for arrays with redundancy.
2438 * linear and raid0 always use whatever space is available
2439 * We can only consider changing the size of no resync
2440 * or reconstruction is happening, and if the new size
2441 * is acceptable. It must fit before the sb_offset or,
2442 * if that is <data_offset, it must fit before the
2443 * size of each device.
2444 * If size is zero, we find the largest size that fits.
2446 if (mddev
->sync_thread
)
2448 ITERATE_RDEV(mddev
,rdev
,tmp
) {
2450 int fit
= (info
->size
== 0);
2451 if (rdev
->sb_offset
> rdev
->data_offset
)
2452 avail
= (rdev
->sb_offset
*2) - rdev
->data_offset
;
2454 avail
= get_capacity(rdev
->bdev
->bd_disk
)
2455 - rdev
->data_offset
;
2456 if (fit
&& (info
->size
== 0 || info
->size
> avail
/2))
2457 info
->size
= avail
/2;
2458 if (avail
< ((sector_t
)info
->size
<< 1))
2461 rv
= mddev
->pers
->resize(mddev
, (sector_t
)info
->size
*2);
2463 struct block_device
*bdev
;
2465 bdev
= bdget_disk(mddev
->gendisk
, 0);
2467 down(&bdev
->bd_inode
->i_sem
);
2468 i_size_write(bdev
->bd_inode
, mddev
->array_size
<< 10);
2469 up(&bdev
->bd_inode
->i_sem
);
2474 if (mddev
->raid_disks
!= info
->raid_disks
) {
2475 /* change the number of raid disks */
2476 if (mddev
->pers
->reshape
== NULL
)
2478 if (info
->raid_disks
<= 0 ||
2479 info
->raid_disks
>= mddev
->max_disks
)
2481 if (mddev
->sync_thread
)
2483 rv
= mddev
->pers
->reshape(mddev
, info
->raid_disks
);
2485 struct block_device
*bdev
;
2487 bdev
= bdget_disk(mddev
->gendisk
, 0);
2489 down(&bdev
->bd_inode
->i_sem
);
2490 i_size_write(bdev
->bd_inode
, mddev
->array_size
<< 10);
2491 up(&bdev
->bd_inode
->i_sem
);
2496 md_update_sb(mddev
);
2500 static int set_disk_faulty(mddev_t
*mddev
, dev_t dev
)
2504 rdev
= find_rdev(mddev
, dev
);
2508 md_error(mddev
, rdev
);
2512 static int md_ioctl(struct inode
*inode
, struct file
*file
,
2513 unsigned int cmd
, unsigned long arg
)
2516 void __user
*argp
= (void __user
*)arg
;
2517 struct hd_geometry __user
*loc
= argp
;
2518 mddev_t
*mddev
= NULL
;
2520 if (!capable(CAP_SYS_ADMIN
))
2524 * Commands dealing with the RAID driver but not any
2530 err
= get_version(argp
);
2533 case PRINT_RAID_DEBUG
:
2541 autostart_arrays(arg
);
2548 * Commands creating/starting a new array:
2551 mddev
= inode
->i_bdev
->bd_disk
->private_data
;
2559 if (cmd
== START_ARRAY
) {
2560 /* START_ARRAY doesn't need to lock the array as autostart_array
2561 * does the locking, and it could even be a different array
2566 "md: %s(pid %d) used deprecated START_ARRAY ioctl. "
2567 "This will not be supported beyond 2.6\n",
2568 current
->comm
, current
->pid
);
2571 err
= autostart_array(new_decode_dev(arg
));
2573 printk(KERN_WARNING
"md: autostart failed!\n");
2579 err
= mddev_lock(mddev
);
2582 "md: ioctl lock interrupted, reason %d, cmd %d\n",
2589 case SET_ARRAY_INFO
:
2591 mdu_array_info_t info
;
2593 memset(&info
, 0, sizeof(info
));
2594 else if (copy_from_user(&info
, argp
, sizeof(info
))) {
2599 err
= update_array_info(mddev
, &info
);
2601 printk(KERN_WARNING
"md: couldn't update"
2602 " array info. %d\n", err
);
2607 if (!list_empty(&mddev
->disks
)) {
2609 "md: array %s already has disks!\n",
2614 if (mddev
->raid_disks
) {
2616 "md: array %s already initialised!\n",
2621 err
= set_array_info(mddev
, &info
);
2623 printk(KERN_WARNING
"md: couldn't set"
2624 " array info. %d\n", err
);
2634 * Commands querying/configuring an existing array:
2636 /* if we are initialised yet, only ADD_NEW_DISK or STOP_ARRAY is allowed */
2637 if (!mddev
->raid_disks
&& cmd
!= ADD_NEW_DISK
&& cmd
!= STOP_ARRAY
&& cmd
!= RUN_ARRAY
) {
2643 * Commands even a read-only array can execute:
2647 case GET_ARRAY_INFO
:
2648 err
= get_array_info(mddev
, argp
);
2652 err
= get_disk_info(mddev
, argp
);
2655 case RESTART_ARRAY_RW
:
2656 err
= restart_array(mddev
);
2660 err
= do_md_stop (mddev
, 0);
2664 err
= do_md_stop (mddev
, 1);
2668 * We have a problem here : there is no easy way to give a CHS
2669 * virtual geometry. We currently pretend that we have a 2 heads
2670 * 4 sectors (with a BIG number of cylinders...). This drives
2671 * dosfs just mad... ;-)
2678 err
= put_user (2, (char __user
*) &loc
->heads
);
2681 err
= put_user (4, (char __user
*) &loc
->sectors
);
2684 err
= put_user(get_capacity(mddev
->gendisk
)/8,
2685 (short __user
*) &loc
->cylinders
);
2688 err
= put_user (get_start_sect(inode
->i_bdev
),
2689 (long __user
*) &loc
->start
);
2694 * The remaining ioctls are changing the state of the
2695 * superblock, so we do not allow read-only arrays
2707 mdu_disk_info_t info
;
2708 if (copy_from_user(&info
, argp
, sizeof(info
)))
2711 err
= add_new_disk(mddev
, &info
);
2715 case HOT_REMOVE_DISK
:
2716 err
= hot_remove_disk(mddev
, new_decode_dev(arg
));
2720 err
= hot_add_disk(mddev
, new_decode_dev(arg
));
2723 case SET_DISK_FAULTY
:
2724 err
= set_disk_faulty(mddev
, new_decode_dev(arg
));
2728 err
= do_md_run (mddev
);
2732 if (_IOC_TYPE(cmd
) == MD_MAJOR
)
2733 printk(KERN_WARNING
"md: %s(pid %d) used"
2734 " obsolete MD ioctl, upgrade your"
2735 " software to use new ictls.\n",
2736 current
->comm
, current
->pid
);
2743 mddev_unlock(mddev
);
2753 static int md_open(struct inode
*inode
, struct file
*file
)
2756 * Succeed if we can lock the mddev, which confirms that
2757 * it isn't being stopped right now.
2759 mddev_t
*mddev
= inode
->i_bdev
->bd_disk
->private_data
;
2762 if ((err
= mddev_lock(mddev
)))
2767 mddev_unlock(mddev
);
2769 check_disk_change(inode
->i_bdev
);
2774 static int md_release(struct inode
*inode
, struct file
* file
)
2776 mddev_t
*mddev
= inode
->i_bdev
->bd_disk
->private_data
;
2785 static int md_media_changed(struct gendisk
*disk
)
2787 mddev_t
*mddev
= disk
->private_data
;
2789 return mddev
->changed
;
2792 static int md_revalidate(struct gendisk
*disk
)
2794 mddev_t
*mddev
= disk
->private_data
;
2799 static struct block_device_operations md_fops
=
2801 .owner
= THIS_MODULE
,
2803 .release
= md_release
,
2805 .media_changed
= md_media_changed
,
2806 .revalidate_disk
= md_revalidate
,
2809 int md_thread(void * arg
)
2811 mdk_thread_t
*thread
= arg
;
2819 daemonize(thread
->name
, mdname(thread
->mddev
));
2821 current
->exit_signal
= SIGCHLD
;
2822 allow_signal(SIGKILL
);
2823 thread
->tsk
= current
;
2826 * md_thread is a 'system-thread', it's priority should be very
2827 * high. We avoid resource deadlocks individually in each
2828 * raid personality. (RAID5 does preallocation) We also use RR and
2829 * the very same RT priority as kswapd, thus we will never get
2830 * into a priority inversion deadlock.
2832 * we definitely have to have equal or higher priority than
2833 * bdflush, otherwise bdflush will deadlock if there are too
2834 * many dirty RAID5 blocks.
2838 complete(thread
->event
);
2839 while (thread
->run
) {
2840 void (*run
)(mddev_t
*);
2842 wait_event_interruptible(thread
->wqueue
,
2843 test_bit(THREAD_WAKEUP
, &thread
->flags
));
2844 if (current
->flags
& PF_FREEZE
)
2845 refrigerator(PF_FREEZE
);
2847 clear_bit(THREAD_WAKEUP
, &thread
->flags
);
2853 if (signal_pending(current
))
2854 flush_signals(current
);
2856 complete(thread
->event
);
2860 void md_wakeup_thread(mdk_thread_t
*thread
)
2863 dprintk("md: waking up MD thread %s.\n", thread
->tsk
->comm
);
2864 set_bit(THREAD_WAKEUP
, &thread
->flags
);
2865 wake_up(&thread
->wqueue
);
2869 mdk_thread_t
*md_register_thread(void (*run
) (mddev_t
*), mddev_t
*mddev
,
2872 mdk_thread_t
*thread
;
2874 struct completion event
;
2876 thread
= (mdk_thread_t
*) kmalloc
2877 (sizeof(mdk_thread_t
), GFP_KERNEL
);
2881 memset(thread
, 0, sizeof(mdk_thread_t
));
2882 init_waitqueue_head(&thread
->wqueue
);
2884 init_completion(&event
);
2885 thread
->event
= &event
;
2887 thread
->mddev
= mddev
;
2888 thread
->name
= name
;
2889 ret
= kernel_thread(md_thread
, thread
, 0);
2894 wait_for_completion(&event
);
2898 static void md_interrupt_thread(mdk_thread_t
*thread
)
2904 dprintk("interrupting MD-thread pid %d\n", thread
->tsk
->pid
);
2905 send_sig(SIGKILL
, thread
->tsk
, 1);
2908 void md_unregister_thread(mdk_thread_t
*thread
)
2910 struct completion event
;
2912 init_completion(&event
);
2914 thread
->event
= &event
;
2916 thread
->name
= NULL
;
2917 md_interrupt_thread(thread
);
2918 wait_for_completion(&event
);
2922 void md_error(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
2929 if (!rdev
|| rdev
->faulty
)
2932 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
2934 MAJOR(rdev
->bdev
->bd_dev
), MINOR(rdev
->bdev
->bd_dev
),
2935 __builtin_return_address(0),__builtin_return_address(1),
2936 __builtin_return_address(2),__builtin_return_address(3));
2938 if (!mddev
->pers
->error_handler
)
2940 mddev
->pers
->error_handler(mddev
,rdev
);
2941 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
2942 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
2943 md_wakeup_thread(mddev
->thread
);
2946 /* seq_file implementation /proc/mdstat */
2948 static void status_unused(struct seq_file
*seq
)
2952 struct list_head
*tmp
;
2954 seq_printf(seq
, "unused devices: ");
2956 ITERATE_RDEV_PENDING(rdev
,tmp
) {
2957 char b
[BDEVNAME_SIZE
];
2959 seq_printf(seq
, "%s ",
2960 bdevname(rdev
->bdev
,b
));
2963 seq_printf(seq
, "<none>");
2965 seq_printf(seq
, "\n");
2969 static void status_resync(struct seq_file
*seq
, mddev_t
* mddev
)
2971 unsigned long max_blocks
, resync
, res
, dt
, db
, rt
;
2973 resync
= (mddev
->curr_resync
- atomic_read(&mddev
->recovery_active
))/2;
2975 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
2976 max_blocks
= mddev
->resync_max_sectors
>> 1;
2978 max_blocks
= mddev
->size
;
2981 * Should not happen.
2987 res
= (resync
/1024)*1000/(max_blocks
/1024 + 1);
2989 int i
, x
= res
/50, y
= 20-x
;
2990 seq_printf(seq
, "[");
2991 for (i
= 0; i
< x
; i
++)
2992 seq_printf(seq
, "=");
2993 seq_printf(seq
, ">");
2994 for (i
= 0; i
< y
; i
++)
2995 seq_printf(seq
, ".");
2996 seq_printf(seq
, "] ");
2998 seq_printf(seq
, " %s =%3lu.%lu%% (%lu/%lu)",
2999 (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ?
3000 "resync" : "recovery"),
3001 res
/10, res
% 10, resync
, max_blocks
);
3004 * We do not want to overflow, so the order of operands and
3005 * the * 100 / 100 trick are important. We do a +1 to be
3006 * safe against division by zero. We only estimate anyway.
3008 * dt: time from mark until now
3009 * db: blocks written from mark until now
3010 * rt: remaining time
3012 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
3014 db
= resync
- (mddev
->resync_mark_cnt
/2);
3015 rt
= (dt
* ((max_blocks
-resync
) / (db
/100+1)))/100;
3017 seq_printf(seq
, " finish=%lu.%lumin", rt
/ 60, (rt
% 60)/6);
3019 seq_printf(seq
, " speed=%ldK/sec", db
/dt
);
3022 static void *md_seq_start(struct seq_file
*seq
, loff_t
*pos
)
3024 struct list_head
*tmp
;
3034 spin_lock(&all_mddevs_lock
);
3035 list_for_each(tmp
,&all_mddevs
)
3037 mddev
= list_entry(tmp
, mddev_t
, all_mddevs
);
3039 spin_unlock(&all_mddevs_lock
);
3042 spin_unlock(&all_mddevs_lock
);
3044 return (void*)2;/* tail */
3048 static void *md_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
3050 struct list_head
*tmp
;
3051 mddev_t
*next_mddev
, *mddev
= v
;
3057 spin_lock(&all_mddevs_lock
);
3059 tmp
= all_mddevs
.next
;
3061 tmp
= mddev
->all_mddevs
.next
;
3062 if (tmp
!= &all_mddevs
)
3063 next_mddev
= mddev_get(list_entry(tmp
,mddev_t
,all_mddevs
));
3065 next_mddev
= (void*)2;
3068 spin_unlock(&all_mddevs_lock
);
3076 static void md_seq_stop(struct seq_file
*seq
, void *v
)
3080 if (mddev
&& v
!= (void*)1 && v
!= (void*)2)
3084 static int md_seq_show(struct seq_file
*seq
, void *v
)
3088 struct list_head
*tmp2
;
3092 if (v
== (void*)1) {
3093 seq_printf(seq
, "Personalities : ");
3094 spin_lock(&pers_lock
);
3095 for (i
= 0; i
< MAX_PERSONALITY
; i
++)
3097 seq_printf(seq
, "[%s] ", pers
[i
]->name
);
3099 spin_unlock(&pers_lock
);
3100 seq_printf(seq
, "\n");
3103 if (v
== (void*)2) {
3108 if (mddev_lock(mddev
)!=0)
3110 if (mddev
->pers
|| mddev
->raid_disks
|| !list_empty(&mddev
->disks
)) {
3111 seq_printf(seq
, "%s : %sactive", mdname(mddev
),
3112 mddev
->pers
? "" : "in");
3115 seq_printf(seq
, " (read-only)");
3116 seq_printf(seq
, " %s", mddev
->pers
->name
);
3120 ITERATE_RDEV(mddev
,rdev
,tmp2
) {
3121 char b
[BDEVNAME_SIZE
];
3122 seq_printf(seq
, " %s[%d]",
3123 bdevname(rdev
->bdev
,b
), rdev
->desc_nr
);
3125 seq_printf(seq
, "(F)");
3131 if (!list_empty(&mddev
->disks
)) {
3133 seq_printf(seq
, "\n %llu blocks",
3134 (unsigned long long)mddev
->array_size
);
3136 seq_printf(seq
, "\n %llu blocks",
3137 (unsigned long long)size
);
3141 mddev
->pers
->status (seq
, mddev
);
3142 seq_printf(seq
, "\n ");
3143 if (mddev
->curr_resync
> 2)
3144 status_resync (seq
, mddev
);
3145 else if (mddev
->curr_resync
== 1 || mddev
->curr_resync
== 2)
3146 seq_printf(seq
, " resync=DELAYED");
3149 seq_printf(seq
, "\n");
3151 mddev_unlock(mddev
);
3156 static struct seq_operations md_seq_ops
= {
3157 .start
= md_seq_start
,
3158 .next
= md_seq_next
,
3159 .stop
= md_seq_stop
,
3160 .show
= md_seq_show
,
3163 static int md_seq_open(struct inode
*inode
, struct file
*file
)
3167 error
= seq_open(file
, &md_seq_ops
);
3171 static struct file_operations md_seq_fops
= {
3172 .open
= md_seq_open
,
3174 .llseek
= seq_lseek
,
3175 .release
= seq_release
,
3178 int register_md_personality(int pnum
, mdk_personality_t
*p
)
3180 if (pnum
>= MAX_PERSONALITY
) {
3182 "md: tried to install personality %s as nr %d, but max is %lu\n",
3183 p
->name
, pnum
, MAX_PERSONALITY
-1);
3187 spin_lock(&pers_lock
);
3189 spin_unlock(&pers_lock
);
3195 printk(KERN_INFO
"md: %s personality registered as nr %d\n", p
->name
, pnum
);
3196 spin_unlock(&pers_lock
);
3200 int unregister_md_personality(int pnum
)
3202 if (pnum
>= MAX_PERSONALITY
) {
3207 printk(KERN_INFO
"md: %s personality unregistered\n", pers
[pnum
]->name
);
3208 spin_lock(&pers_lock
);
3210 spin_unlock(&pers_lock
);
3214 static int is_mddev_idle(mddev_t
*mddev
)
3217 struct list_head
*tmp
;
3219 unsigned long curr_events
;
3222 ITERATE_RDEV(mddev
,rdev
,tmp
) {
3223 struct gendisk
*disk
= rdev
->bdev
->bd_contains
->bd_disk
;
3224 curr_events
= disk_stat_read(disk
, read_sectors
) +
3225 disk_stat_read(disk
, write_sectors
) -
3226 atomic_read(&disk
->sync_io
);
3227 /* Allow some slack between valud of curr_events and last_events,
3228 * as there are some uninteresting races.
3229 * Note: the following is an unsigned comparison.
3231 if ((curr_events
- rdev
->last_events
+ 32) > 64) {
3232 rdev
->last_events
= curr_events
;
3239 void md_done_sync(mddev_t
*mddev
, int blocks
, int ok
)
3241 /* another "blocks" (512byte) blocks have been synced */
3242 atomic_sub(blocks
, &mddev
->recovery_active
);
3243 wake_up(&mddev
->recovery_wait
);
3245 set_bit(MD_RECOVERY_ERR
, &mddev
->recovery
);
3246 md_wakeup_thread(mddev
->thread
);
3247 // stop recovery, signal do_sync ....
3252 void md_write_start(mddev_t
*mddev
)
3254 if (!atomic_read(&mddev
->writes_pending
)) {
3255 mddev_lock_uninterruptible(mddev
);
3256 if (mddev
->in_sync
) {
3258 del_timer(&mddev
->safemode_timer
);
3259 md_update_sb(mddev
);
3261 atomic_inc(&mddev
->writes_pending
);
3262 mddev_unlock(mddev
);
3264 atomic_inc(&mddev
->writes_pending
);
3267 void md_write_end(mddev_t
*mddev
)
3269 if (atomic_dec_and_test(&mddev
->writes_pending
)) {
3270 if (mddev
->safemode
== 2)
3271 md_wakeup_thread(mddev
->thread
);
3273 mod_timer(&mddev
->safemode_timer
, jiffies
+ mddev
->safemode_delay
);
3277 static inline void md_enter_safemode(mddev_t
*mddev
)
3279 if (!mddev
->safemode
) return;
3280 if (mddev
->safemode
== 2 &&
3281 (atomic_read(&mddev
->writes_pending
) || mddev
->in_sync
||
3282 mddev
->recovery_cp
!= MaxSector
))
3283 return; /* avoid the lock */
3284 mddev_lock_uninterruptible(mddev
);
3285 if (mddev
->safemode
&& !atomic_read(&mddev
->writes_pending
) &&
3286 !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
) {
3288 md_update_sb(mddev
);
3290 mddev_unlock(mddev
);
3292 if (mddev
->safemode
== 1)
3293 mddev
->safemode
= 0;
3296 void md_handle_safemode(mddev_t
*mddev
)
3298 if (signal_pending(current
)) {
3299 printk(KERN_INFO
"md: %s in immediate safe mode\n",
3301 mddev
->safemode
= 2;
3302 flush_signals(current
);
3304 md_enter_safemode(mddev
);
3308 DECLARE_WAIT_QUEUE_HEAD(resync_wait
);
3310 #define SYNC_MARKS 10
3311 #define SYNC_MARK_STEP (3*HZ)
3312 static void md_do_sync(mddev_t
*mddev
)
3315 unsigned int currspeed
= 0,
3317 sector_t max_sectors
,j
;
3318 unsigned long mark
[SYNC_MARKS
];
3319 sector_t mark_cnt
[SYNC_MARKS
];
3321 struct list_head
*tmp
;
3322 sector_t last_check
;
3324 /* just incase thread restarts... */
3325 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
3328 /* we overload curr_resync somewhat here.
3329 * 0 == not engaged in resync at all
3330 * 2 == checking that there is no conflict with another sync
3331 * 1 == like 2, but have yielded to allow conflicting resync to
3333 * other == active in resync - this many blocks
3336 mddev
->curr_resync
= 2;
3338 ITERATE_MDDEV(mddev2
,tmp
) {
3339 if (mddev2
== mddev
)
3341 if (mddev2
->curr_resync
&&
3342 match_mddev_units(mddev
,mddev2
)) {
3343 printk(KERN_INFO
"md: delaying resync of %s"
3344 " until %s has finished resync (they"
3345 " share one or more physical units)\n",
3346 mdname(mddev
), mdname(mddev2
));
3347 if (mddev
< mddev2
) {/* arbitrarily yield */
3348 mddev
->curr_resync
= 1;
3349 wake_up(&resync_wait
);
3351 if (wait_event_interruptible(resync_wait
,
3352 mddev2
->curr_resync
< mddev
->curr_resync
)) {
3353 flush_signals(current
);
3358 if (mddev
->curr_resync
== 1) {
3363 } while (mddev
->curr_resync
< 2);
3365 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
3366 /* resync follows the size requested by the personality,
3367 * which default to physical size, but can be virtual size
3369 max_sectors
= mddev
->resync_max_sectors
;
3371 /* recovery follows the physical size of devices */
3372 max_sectors
= mddev
->size
<< 1;
3374 printk(KERN_INFO
"md: syncing RAID array %s\n", mdname(mddev
));
3375 printk(KERN_INFO
"md: minimum _guaranteed_ reconstruction speed:"
3376 " %d KB/sec/disc.\n", sysctl_speed_limit_min
);
3377 printk(KERN_INFO
"md: using maximum available idle IO bandwith "
3378 "(but not more than %d KB/sec) for reconstruction.\n",
3379 sysctl_speed_limit_max
);
3381 is_mddev_idle(mddev
); /* this also initializes IO event counters */
3382 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
3383 j
= mddev
->recovery_cp
;
3386 for (m
= 0; m
< SYNC_MARKS
; m
++) {
3391 mddev
->resync_mark
= mark
[last_mark
];
3392 mddev
->resync_mark_cnt
= mark_cnt
[last_mark
];
3395 * Tune reconstruction:
3397 window
= 32*(PAGE_SIZE
/512);
3398 printk(KERN_INFO
"md: using %dk window, over a total of %Lu blocks.\n",
3399 window
/2,(unsigned long long) max_sectors
/2);
3401 atomic_set(&mddev
->recovery_active
, 0);
3402 init_waitqueue_head(&mddev
->recovery_wait
);
3407 "md: resuming recovery of %s from checkpoint.\n",
3410 while (j
< max_sectors
) {
3413 sectors
= mddev
->pers
->sync_request(mddev
, j
, currspeed
< sysctl_speed_limit_min
);
3415 set_bit(MD_RECOVERY_ERR
, &mddev
->recovery
);
3418 atomic_add(sectors
, &mddev
->recovery_active
);
3420 if (j
>1) mddev
->curr_resync
= j
;
3422 if (last_check
+ window
> j
|| j
== max_sectors
)
3427 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) ||
3428 test_bit(MD_RECOVERY_ERR
, &mddev
->recovery
))
3432 if (jiffies
>= mark
[last_mark
] + SYNC_MARK_STEP
) {
3434 int next
= (last_mark
+1) % SYNC_MARKS
;
3436 mddev
->resync_mark
= mark
[next
];
3437 mddev
->resync_mark_cnt
= mark_cnt
[next
];
3438 mark
[next
] = jiffies
;
3439 mark_cnt
[next
] = j
- atomic_read(&mddev
->recovery_active
);
3444 if (signal_pending(current
)) {
3446 * got a signal, exit.
3449 "md: md_do_sync() got signal ... exiting\n");
3450 flush_signals(current
);
3451 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
3456 * this loop exits only if either when we are slower than
3457 * the 'hard' speed limit, or the system was IO-idle for
3459 * the system might be non-idle CPU-wise, but we only care
3460 * about not overloading the IO subsystem. (things like an
3461 * e2fsck being done on the RAID array should execute fast)
3463 mddev
->queue
->unplug_fn(mddev
->queue
);
3466 currspeed
= ((unsigned long)(j
-mddev
->resync_mark_cnt
))/2/((jiffies
-mddev
->resync_mark
)/HZ
+1) +1;
3468 if (currspeed
> sysctl_speed_limit_min
) {
3469 if ((currspeed
> sysctl_speed_limit_max
) ||
3470 !is_mddev_idle(mddev
)) {
3471 current
->state
= TASK_INTERRUPTIBLE
;
3472 schedule_timeout(HZ
/4);
3477 printk(KERN_INFO
"md: %s: sync done.\n",mdname(mddev
));
3479 * this also signals 'finished resyncing' to md_stop
3482 mddev
->queue
->unplug_fn(mddev
->queue
);
3484 wait_event(mddev
->recovery_wait
, !atomic_read(&mddev
->recovery_active
));
3486 /* tell personality that we are finished */
3487 mddev
->pers
->sync_request(mddev
, max_sectors
, 1);
3489 if (!test_bit(MD_RECOVERY_ERR
, &mddev
->recovery
) &&
3490 mddev
->curr_resync
> 2 &&
3491 mddev
->curr_resync
> mddev
->recovery_cp
) {
3492 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
3494 "md: checkpointing recovery of %s.\n",
3496 mddev
->recovery_cp
= mddev
->curr_resync
;
3498 mddev
->recovery_cp
= MaxSector
;
3501 md_enter_safemode(mddev
);
3503 mddev
->curr_resync
= 0;
3504 set_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
3505 md_wakeup_thread(mddev
->thread
);
3510 * This routine is regularly called by all per-raid-array threads to
3511 * deal with generic issues like resync and super-block update.
3512 * Raid personalities that don't have a thread (linear/raid0) do not
3513 * need this as they never do any recovery or update the superblock.
3515 * It does not do any resync itself, but rather "forks" off other threads
3516 * to do that as needed.
3517 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
3518 * "->recovery" and create a thread at ->sync_thread.
3519 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
3520 * and wakeups up this thread which will reap the thread and finish up.
3521 * This thread also removes any faulty devices (with nr_pending == 0).
3523 * The overall approach is:
3524 * 1/ if the superblock needs updating, update it.
3525 * 2/ If a recovery thread is running, don't do anything else.
3526 * 3/ If recovery has finished, clean up, possibly marking spares active.
3527 * 4/ If there are any faulty devices, remove them.
3528 * 5/ If array is degraded, try to add spares devices
3529 * 6/ If array has spares or is not in-sync, start a resync thread.
3531 void md_check_recovery(mddev_t
*mddev
)
3534 struct list_head
*rtmp
;
3537 dprintk(KERN_INFO
"md: recovery thread got woken up ...\n");
3543 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
3544 test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
)
3547 if (mddev_trylock(mddev
)==0) {
3549 if (mddev
->sb_dirty
)
3550 md_update_sb(mddev
);
3551 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
3552 !test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
)) {
3553 /* resync/recovery still happening */
3554 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3557 if (mddev
->sync_thread
) {
3558 /* resync has finished, collect result */
3559 md_unregister_thread(mddev
->sync_thread
);
3560 mddev
->sync_thread
= NULL
;
3561 if (!test_bit(MD_RECOVERY_ERR
, &mddev
->recovery
) &&
3562 !test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
3564 /* activate any spares */
3565 mddev
->pers
->spare_active(mddev
);
3567 md_update_sb(mddev
);
3568 mddev
->recovery
= 0;
3569 /* flag recovery needed just to double check */
3570 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3571 wake_up(&resync_wait
);
3574 if (mddev
->recovery
) {
3575 /* probably just the RECOVERY_NEEDED flag */
3576 mddev
->recovery
= 0;
3577 wake_up(&resync_wait
);
3580 /* no recovery is running.
3581 * remove any failed drives, then
3582 * add spares if possible
3584 ITERATE_RDEV(mddev
,rdev
,rtmp
) {
3585 if (rdev
->raid_disk
>= 0 &&
3587 atomic_read(&rdev
->nr_pending
)==0) {
3588 if (mddev
->pers
->hot_remove_disk(mddev
, rdev
->raid_disk
)==0)
3589 rdev
->raid_disk
= -1;
3591 if (!rdev
->faulty
&& rdev
->raid_disk
>= 0 && !rdev
->in_sync
)
3594 if (mddev
->degraded
) {
3595 ITERATE_RDEV(mddev
,rdev
,rtmp
)
3596 if (rdev
->raid_disk
< 0
3598 if (mddev
->pers
->hot_add_disk(mddev
,rdev
))
3605 if (!spares
&& (mddev
->recovery_cp
== MaxSector
)) {
3606 /* nothing we can do ... */
3609 if (mddev
->pers
->sync_request
) {
3610 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
3612 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
3613 mddev
->sync_thread
= md_register_thread(md_do_sync
,
3616 if (!mddev
->sync_thread
) {
3617 printk(KERN_ERR
"%s: could not start resync"
3620 /* leave the spares where they are, it shouldn't hurt */
3621 mddev
->recovery
= 0;
3623 md_wakeup_thread(mddev
->sync_thread
);
3627 mddev_unlock(mddev
);
3631 int md_notify_reboot(struct notifier_block
*this,
3632 unsigned long code
, void *x
)
3634 struct list_head
*tmp
;
3637 if ((code
== SYS_DOWN
) || (code
== SYS_HALT
) || (code
== SYS_POWER_OFF
)) {
3639 printk(KERN_INFO
"md: stopping all md devices.\n");
3641 ITERATE_MDDEV(mddev
,tmp
)
3642 if (mddev_trylock(mddev
)==0)
3643 do_md_stop (mddev
, 1);
3645 * certain more exotic SCSI devices are known to be
3646 * volatile wrt too early system reboots. While the
3647 * right place to handle this issue is the given
3648 * driver, we do want to have a safe RAID driver ...
3655 struct notifier_block md_notifier
= {
3656 .notifier_call
= md_notify_reboot
,
3658 .priority
= INT_MAX
, /* before any real devices */
3661 static void md_geninit(void)
3663 struct proc_dir_entry
*p
;
3665 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t
));
3667 p
= create_proc_entry("mdstat", S_IRUGO
, NULL
);
3669 p
->proc_fops
= &md_seq_fops
;
3672 int __init
md_init(void)
3676 printk(KERN_INFO
"md: md driver %d.%d.%d MAX_MD_DEVS=%d,"
3677 " MD_SB_DISKS=%d\n",
3678 MD_MAJOR_VERSION
, MD_MINOR_VERSION
,
3679 MD_PATCHLEVEL_VERSION
, MAX_MD_DEVS
, MD_SB_DISKS
);
3681 if (register_blkdev(MAJOR_NR
, "md"))
3683 if ((mdp_major
=register_blkdev(0, "mdp"))<=0) {
3684 unregister_blkdev(MAJOR_NR
, "md");
3688 blk_register_region(MKDEV(MAJOR_NR
, 0), MAX_MD_DEVS
, THIS_MODULE
,
3689 md_probe
, NULL
, NULL
);
3690 blk_register_region(MKDEV(mdp_major
, 0), MAX_MD_DEVS
<<MdpMinorShift
, THIS_MODULE
,
3691 md_probe
, NULL
, NULL
);
3693 for (minor
=0; minor
< MAX_MD_DEVS
; ++minor
)
3694 devfs_mk_bdev(MKDEV(MAJOR_NR
, minor
),
3695 S_IFBLK
|S_IRUSR
|S_IWUSR
,
3698 for (minor
=0; minor
< MAX_MD_DEVS
; ++minor
)
3699 devfs_mk_bdev(MKDEV(mdp_major
, minor
<<MdpMinorShift
),
3700 S_IFBLK
|S_IRUSR
|S_IWUSR
,
3704 register_reboot_notifier(&md_notifier
);
3705 raid_table_header
= register_sysctl_table(raid_root_table
, 1);
3715 * Searches all registered partitions for autorun RAID arrays
3718 static dev_t detected_devices
[128];
3721 void md_autodetect_dev(dev_t dev
)
3723 if (dev_cnt
>= 0 && dev_cnt
< 127)
3724 detected_devices
[dev_cnt
++] = dev
;
3728 static void autostart_arrays(int part
)
3733 printk(KERN_INFO
"md: Autodetecting RAID arrays.\n");
3735 for (i
= 0; i
< dev_cnt
; i
++) {
3736 dev_t dev
= detected_devices
[i
];
3738 rdev
= md_import_device(dev
,0, 0);
3746 list_add(&rdev
->same_set
, &pending_raid_disks
);
3750 autorun_devices(part
);
3755 static __exit
void md_exit(void)
3758 struct list_head
*tmp
;
3760 blk_unregister_region(MKDEV(MAJOR_NR
,0), MAX_MD_DEVS
);
3761 blk_unregister_region(MKDEV(mdp_major
,0), MAX_MD_DEVS
<< MdpMinorShift
);
3762 for (i
=0; i
< MAX_MD_DEVS
; i
++)
3763 devfs_remove("md/%d", i
);
3764 for (i
=0; i
< MAX_MD_DEVS
; i
++)
3765 devfs_remove("md/d%d", i
);
3769 unregister_blkdev(MAJOR_NR
,"md");
3770 unregister_blkdev(mdp_major
, "mdp");
3771 unregister_reboot_notifier(&md_notifier
);
3772 unregister_sysctl_table(raid_table_header
);
3773 remove_proc_entry("mdstat", NULL
);
3774 ITERATE_MDDEV(mddev
,tmp
) {
3775 struct gendisk
*disk
= mddev
->gendisk
;
3778 export_array(mddev
);
3781 mddev
->gendisk
= NULL
;
3786 module_init(md_init
)
3787 module_exit(md_exit
)
3789 EXPORT_SYMBOL(register_md_personality
);
3790 EXPORT_SYMBOL(unregister_md_personality
);
3791 EXPORT_SYMBOL(md_error
);
3792 EXPORT_SYMBOL(md_done_sync
);
3793 EXPORT_SYMBOL(md_write_start
);
3794 EXPORT_SYMBOL(md_write_end
);
3795 EXPORT_SYMBOL(md_handle_safemode
);
3796 EXPORT_SYMBOL(md_register_thread
);
3797 EXPORT_SYMBOL(md_unregister_thread
);
3798 EXPORT_SYMBOL(md_wakeup_thread
);
3799 EXPORT_SYMBOL(md_print_devices
);
3800 EXPORT_SYMBOL(md_check_recovery
);
3801 MODULE_LICENSE("GPL");