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/kernel.h>
37 #include <linux/kthread.h>
38 #include <linux/linkage.h>
39 #include <linux/raid/md.h>
40 #include <linux/raid/bitmap.h>
41 #include <linux/sysctl.h>
42 #include <linux/buffer_head.h> /* for invalidate_bdev */
43 #include <linux/poll.h>
44 #include <linux/mutex.h>
45 #include <linux/ctype.h>
46 #include <linux/freezer.h>
48 #include <linux/init.h>
50 #include <linux/file.h>
53 #include <linux/kmod.h>
56 #include <asm/unaligned.h>
58 #define MAJOR_NR MD_MAJOR
61 /* 63 partitions with the alternate major number (mdp) */
62 #define MdpMinorShift 6
65 #define dprintk(x...) ((void)(DEBUG && printk(x)))
69 static void autostart_arrays (int part
);
72 static LIST_HEAD(pers_list
);
73 static DEFINE_SPINLOCK(pers_lock
);
75 static void md_print_devices(void);
77 static DECLARE_WAIT_QUEUE_HEAD(resync_wait
);
79 #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
82 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
83 * is 1000 KB/sec, so the extra system load does not show up that much.
84 * Increase it if you want to have more _guaranteed_ speed. Note that
85 * the RAID driver will use the maximum available bandwidth if the IO
86 * subsystem is idle. There is also an 'absolute maximum' reconstruction
87 * speed limit - in case reconstruction slows down your system despite
90 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
91 * or /sys/block/mdX/md/sync_speed_{min,max}
94 static int sysctl_speed_limit_min
= 1000;
95 static int sysctl_speed_limit_max
= 200000;
96 static inline int speed_min(mddev_t
*mddev
)
98 return mddev
->sync_speed_min
?
99 mddev
->sync_speed_min
: sysctl_speed_limit_min
;
102 static inline int speed_max(mddev_t
*mddev
)
104 return mddev
->sync_speed_max
?
105 mddev
->sync_speed_max
: sysctl_speed_limit_max
;
108 static struct ctl_table_header
*raid_table_header
;
110 static ctl_table raid_table
[] = {
112 .ctl_name
= DEV_RAID_SPEED_LIMIT_MIN
,
113 .procname
= "speed_limit_min",
114 .data
= &sysctl_speed_limit_min
,
115 .maxlen
= sizeof(int),
116 .mode
= S_IRUGO
|S_IWUSR
,
117 .proc_handler
= &proc_dointvec
,
120 .ctl_name
= DEV_RAID_SPEED_LIMIT_MAX
,
121 .procname
= "speed_limit_max",
122 .data
= &sysctl_speed_limit_max
,
123 .maxlen
= sizeof(int),
124 .mode
= S_IRUGO
|S_IWUSR
,
125 .proc_handler
= &proc_dointvec
,
130 static ctl_table raid_dir_table
[] = {
132 .ctl_name
= DEV_RAID
,
135 .mode
= S_IRUGO
|S_IXUGO
,
141 static ctl_table raid_root_table
[] = {
147 .child
= raid_dir_table
,
152 static struct block_device_operations md_fops
;
154 static int start_readonly
;
157 * We have a system wide 'event count' that is incremented
158 * on any 'interesting' event, and readers of /proc/mdstat
159 * can use 'poll' or 'select' to find out when the event
163 * start array, stop array, error, add device, remove device,
164 * start build, activate spare
166 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters
);
167 static atomic_t md_event_count
;
168 void md_new_event(mddev_t
*mddev
)
170 atomic_inc(&md_event_count
);
171 wake_up(&md_event_waiters
);
173 EXPORT_SYMBOL_GPL(md_new_event
);
175 /* Alternate version that can be called from interrupts
176 * when calling sysfs_notify isn't needed.
178 static void md_new_event_inintr(mddev_t
*mddev
)
180 atomic_inc(&md_event_count
);
181 wake_up(&md_event_waiters
);
185 * Enables to iterate over all existing md arrays
186 * all_mddevs_lock protects this list.
188 static LIST_HEAD(all_mddevs
);
189 static DEFINE_SPINLOCK(all_mddevs_lock
);
193 * iterates through all used mddevs in the system.
194 * We take care to grab the all_mddevs_lock whenever navigating
195 * the list, and to always hold a refcount when unlocked.
196 * Any code which breaks out of this loop while own
197 * a reference to the current mddev and must mddev_put it.
199 #define for_each_mddev(mddev,tmp) \
201 for (({ spin_lock(&all_mddevs_lock); \
202 tmp = all_mddevs.next; \
204 ({ if (tmp != &all_mddevs) \
205 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
206 spin_unlock(&all_mddevs_lock); \
207 if (mddev) mddev_put(mddev); \
208 mddev = list_entry(tmp, mddev_t, all_mddevs); \
209 tmp != &all_mddevs;}); \
210 ({ spin_lock(&all_mddevs_lock); \
215 static int md_fail_request (struct request_queue
*q
, struct bio
*bio
)
221 static inline mddev_t
*mddev_get(mddev_t
*mddev
)
223 atomic_inc(&mddev
->active
);
227 static void mddev_put(mddev_t
*mddev
)
229 if (!atomic_dec_and_lock(&mddev
->active
, &all_mddevs_lock
))
231 if (!mddev
->raid_disks
&& list_empty(&mddev
->disks
)) {
232 list_del(&mddev
->all_mddevs
);
233 spin_unlock(&all_mddevs_lock
);
234 blk_cleanup_queue(mddev
->queue
);
235 kobject_put(&mddev
->kobj
);
237 spin_unlock(&all_mddevs_lock
);
240 static mddev_t
* mddev_find(dev_t unit
)
242 mddev_t
*mddev
, *new = NULL
;
245 spin_lock(&all_mddevs_lock
);
246 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
247 if (mddev
->unit
== unit
) {
249 spin_unlock(&all_mddevs_lock
);
255 list_add(&new->all_mddevs
, &all_mddevs
);
256 spin_unlock(&all_mddevs_lock
);
259 spin_unlock(&all_mddevs_lock
);
261 new = kzalloc(sizeof(*new), GFP_KERNEL
);
266 if (MAJOR(unit
) == MD_MAJOR
)
267 new->md_minor
= MINOR(unit
);
269 new->md_minor
= MINOR(unit
) >> MdpMinorShift
;
271 mutex_init(&new->reconfig_mutex
);
272 INIT_LIST_HEAD(&new->disks
);
273 INIT_LIST_HEAD(&new->all_mddevs
);
274 init_timer(&new->safemode_timer
);
275 atomic_set(&new->active
, 1);
276 atomic_set(&new->openers
, 0);
277 spin_lock_init(&new->write_lock
);
278 init_waitqueue_head(&new->sb_wait
);
279 init_waitqueue_head(&new->recovery_wait
);
280 new->reshape_position
= MaxSector
;
282 new->resync_max
= MaxSector
;
283 new->level
= LEVEL_NONE
;
285 new->queue
= blk_alloc_queue(GFP_KERNEL
);
290 /* Can be unlocked because the queue is new: no concurrency */
291 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER
, new->queue
);
293 blk_queue_make_request(new->queue
, md_fail_request
);
298 static inline int mddev_lock(mddev_t
* mddev
)
300 return mutex_lock_interruptible(&mddev
->reconfig_mutex
);
303 static inline int mddev_trylock(mddev_t
* mddev
)
305 return mutex_trylock(&mddev
->reconfig_mutex
);
308 static inline void mddev_unlock(mddev_t
* mddev
)
310 mutex_unlock(&mddev
->reconfig_mutex
);
312 md_wakeup_thread(mddev
->thread
);
315 static mdk_rdev_t
* find_rdev_nr(mddev_t
*mddev
, int nr
)
318 struct list_head
*tmp
;
320 rdev_for_each(rdev
, tmp
, mddev
) {
321 if (rdev
->desc_nr
== nr
)
327 static mdk_rdev_t
* find_rdev(mddev_t
* mddev
, dev_t dev
)
329 struct list_head
*tmp
;
332 rdev_for_each(rdev
, tmp
, mddev
) {
333 if (rdev
->bdev
->bd_dev
== dev
)
339 static struct mdk_personality
*find_pers(int level
, char *clevel
)
341 struct mdk_personality
*pers
;
342 list_for_each_entry(pers
, &pers_list
, list
) {
343 if (level
!= LEVEL_NONE
&& pers
->level
== level
)
345 if (strcmp(pers
->name
, clevel
)==0)
351 /* return the offset of the super block in 512byte sectors */
352 static inline sector_t
calc_dev_sboffset(struct block_device
*bdev
)
354 sector_t num_sectors
= bdev
->bd_inode
->i_size
/ 512;
355 return MD_NEW_SIZE_SECTORS(num_sectors
);
358 static sector_t
calc_num_sectors(mdk_rdev_t
*rdev
, unsigned chunk_size
)
360 sector_t num_sectors
= rdev
->sb_start
;
363 num_sectors
&= ~((sector_t
)chunk_size
/512 - 1);
367 static int alloc_disk_sb(mdk_rdev_t
* rdev
)
372 rdev
->sb_page
= alloc_page(GFP_KERNEL
);
373 if (!rdev
->sb_page
) {
374 printk(KERN_ALERT
"md: out of memory.\n");
381 static void free_disk_sb(mdk_rdev_t
* rdev
)
384 put_page(rdev
->sb_page
);
386 rdev
->sb_page
= NULL
;
393 static void super_written(struct bio
*bio
, int error
)
395 mdk_rdev_t
*rdev
= bio
->bi_private
;
396 mddev_t
*mddev
= rdev
->mddev
;
398 if (error
|| !test_bit(BIO_UPTODATE
, &bio
->bi_flags
)) {
399 printk("md: super_written gets error=%d, uptodate=%d\n",
400 error
, test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
401 WARN_ON(test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
402 md_error(mddev
, rdev
);
405 if (atomic_dec_and_test(&mddev
->pending_writes
))
406 wake_up(&mddev
->sb_wait
);
410 static void super_written_barrier(struct bio
*bio
, int error
)
412 struct bio
*bio2
= bio
->bi_private
;
413 mdk_rdev_t
*rdev
= bio2
->bi_private
;
414 mddev_t
*mddev
= rdev
->mddev
;
416 if (!test_bit(BIO_UPTODATE
, &bio
->bi_flags
) &&
417 error
== -EOPNOTSUPP
) {
419 /* barriers don't appear to be supported :-( */
420 set_bit(BarriersNotsupp
, &rdev
->flags
);
421 mddev
->barriers_work
= 0;
422 spin_lock_irqsave(&mddev
->write_lock
, flags
);
423 bio2
->bi_next
= mddev
->biolist
;
424 mddev
->biolist
= bio2
;
425 spin_unlock_irqrestore(&mddev
->write_lock
, flags
);
426 wake_up(&mddev
->sb_wait
);
430 bio
->bi_private
= rdev
;
431 super_written(bio
, error
);
435 void md_super_write(mddev_t
*mddev
, mdk_rdev_t
*rdev
,
436 sector_t sector
, int size
, struct page
*page
)
438 /* write first size bytes of page to sector of rdev
439 * Increment mddev->pending_writes before returning
440 * and decrement it on completion, waking up sb_wait
441 * if zero is reached.
442 * If an error occurred, call md_error
444 * As we might need to resubmit the request if BIO_RW_BARRIER
445 * causes ENOTSUPP, we allocate a spare bio...
447 struct bio
*bio
= bio_alloc(GFP_NOIO
, 1);
448 int rw
= (1<<BIO_RW
) | (1<<BIO_RW_SYNC
);
450 bio
->bi_bdev
= rdev
->bdev
;
451 bio
->bi_sector
= sector
;
452 bio_add_page(bio
, page
, size
, 0);
453 bio
->bi_private
= rdev
;
454 bio
->bi_end_io
= super_written
;
457 atomic_inc(&mddev
->pending_writes
);
458 if (!test_bit(BarriersNotsupp
, &rdev
->flags
)) {
460 rw
|= (1<<BIO_RW_BARRIER
);
461 rbio
= bio_clone(bio
, GFP_NOIO
);
462 rbio
->bi_private
= bio
;
463 rbio
->bi_end_io
= super_written_barrier
;
464 submit_bio(rw
, rbio
);
469 void md_super_wait(mddev_t
*mddev
)
471 /* wait for all superblock writes that were scheduled to complete.
472 * if any had to be retried (due to BARRIER problems), retry them
476 prepare_to_wait(&mddev
->sb_wait
, &wq
, TASK_UNINTERRUPTIBLE
);
477 if (atomic_read(&mddev
->pending_writes
)==0)
479 while (mddev
->biolist
) {
481 spin_lock_irq(&mddev
->write_lock
);
482 bio
= mddev
->biolist
;
483 mddev
->biolist
= bio
->bi_next
;
485 spin_unlock_irq(&mddev
->write_lock
);
486 submit_bio(bio
->bi_rw
, bio
);
490 finish_wait(&mddev
->sb_wait
, &wq
);
493 static void bi_complete(struct bio
*bio
, int error
)
495 complete((struct completion
*)bio
->bi_private
);
498 int sync_page_io(struct block_device
*bdev
, sector_t sector
, int size
,
499 struct page
*page
, int rw
)
501 struct bio
*bio
= bio_alloc(GFP_NOIO
, 1);
502 struct completion event
;
505 rw
|= (1 << BIO_RW_SYNC
);
508 bio
->bi_sector
= sector
;
509 bio_add_page(bio
, page
, size
, 0);
510 init_completion(&event
);
511 bio
->bi_private
= &event
;
512 bio
->bi_end_io
= bi_complete
;
514 wait_for_completion(&event
);
516 ret
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
520 EXPORT_SYMBOL_GPL(sync_page_io
);
522 static int read_disk_sb(mdk_rdev_t
* rdev
, int size
)
524 char b
[BDEVNAME_SIZE
];
525 if (!rdev
->sb_page
) {
533 if (!sync_page_io(rdev
->bdev
, rdev
->sb_start
, size
, rdev
->sb_page
, READ
))
539 printk(KERN_WARNING
"md: disabled device %s, could not read superblock.\n",
540 bdevname(rdev
->bdev
,b
));
544 static int uuid_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
546 return sb1
->set_uuid0
== sb2
->set_uuid0
&&
547 sb1
->set_uuid1
== sb2
->set_uuid1
&&
548 sb1
->set_uuid2
== sb2
->set_uuid2
&&
549 sb1
->set_uuid3
== sb2
->set_uuid3
;
552 static int sb_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
555 mdp_super_t
*tmp1
, *tmp2
;
557 tmp1
= kmalloc(sizeof(*tmp1
),GFP_KERNEL
);
558 tmp2
= kmalloc(sizeof(*tmp2
),GFP_KERNEL
);
560 if (!tmp1
|| !tmp2
) {
562 printk(KERN_INFO
"md.c sb_equal(): failed to allocate memory!\n");
570 * nr_disks is not constant
575 ret
= (memcmp(tmp1
, tmp2
, MD_SB_GENERIC_CONSTANT_WORDS
* 4) == 0);
583 static u32
md_csum_fold(u32 csum
)
585 csum
= (csum
& 0xffff) + (csum
>> 16);
586 return (csum
& 0xffff) + (csum
>> 16);
589 static unsigned int calc_sb_csum(mdp_super_t
* sb
)
592 u32
*sb32
= (u32
*)sb
;
594 unsigned int disk_csum
, csum
;
596 disk_csum
= sb
->sb_csum
;
599 for (i
= 0; i
< MD_SB_BYTES
/4 ; i
++)
601 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
605 /* This used to use csum_partial, which was wrong for several
606 * reasons including that different results are returned on
607 * different architectures. It isn't critical that we get exactly
608 * the same return value as before (we always csum_fold before
609 * testing, and that removes any differences). However as we
610 * know that csum_partial always returned a 16bit value on
611 * alphas, do a fold to maximise conformity to previous behaviour.
613 sb
->sb_csum
= md_csum_fold(disk_csum
);
615 sb
->sb_csum
= disk_csum
;
622 * Handle superblock details.
623 * We want to be able to handle multiple superblock formats
624 * so we have a common interface to them all, and an array of
625 * different handlers.
626 * We rely on user-space to write the initial superblock, and support
627 * reading and updating of superblocks.
628 * Interface methods are:
629 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
630 * loads and validates a superblock on dev.
631 * if refdev != NULL, compare superblocks on both devices
633 * 0 - dev has a superblock that is compatible with refdev
634 * 1 - dev has a superblock that is compatible and newer than refdev
635 * so dev should be used as the refdev in future
636 * -EINVAL superblock incompatible or invalid
637 * -othererror e.g. -EIO
639 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
640 * Verify that dev is acceptable into mddev.
641 * The first time, mddev->raid_disks will be 0, and data from
642 * dev should be merged in. Subsequent calls check that dev
643 * is new enough. Return 0 or -EINVAL
645 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
646 * Update the superblock for rdev with data in mddev
647 * This does not write to disc.
653 struct module
*owner
;
654 int (*load_super
)(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
,
656 int (*validate_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
657 void (*sync_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
658 unsigned long long (*rdev_size_change
)(mdk_rdev_t
*rdev
,
659 sector_t num_sectors
);
663 * load_super for 0.90.0
665 static int super_90_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
667 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
672 * Calculate the position of the superblock (512byte sectors),
673 * it's at the end of the disk.
675 * It also happens to be a multiple of 4Kb.
677 rdev
->sb_start
= calc_dev_sboffset(rdev
->bdev
);
679 ret
= read_disk_sb(rdev
, MD_SB_BYTES
);
684 bdevname(rdev
->bdev
, b
);
685 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
687 if (sb
->md_magic
!= MD_SB_MAGIC
) {
688 printk(KERN_ERR
"md: invalid raid superblock magic on %s\n",
693 if (sb
->major_version
!= 0 ||
694 sb
->minor_version
< 90 ||
695 sb
->minor_version
> 91) {
696 printk(KERN_WARNING
"Bad version number %d.%d on %s\n",
697 sb
->major_version
, sb
->minor_version
,
702 if (sb
->raid_disks
<= 0)
705 if (md_csum_fold(calc_sb_csum(sb
)) != md_csum_fold(sb
->sb_csum
)) {
706 printk(KERN_WARNING
"md: invalid superblock checksum on %s\n",
711 rdev
->preferred_minor
= sb
->md_minor
;
712 rdev
->data_offset
= 0;
713 rdev
->sb_size
= MD_SB_BYTES
;
715 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
716 if (sb
->level
!= 1 && sb
->level
!= 4
717 && sb
->level
!= 5 && sb
->level
!= 6
718 && sb
->level
!= 10) {
719 /* FIXME use a better test */
721 "md: bitmaps not supported for this level.\n");
726 if (sb
->level
== LEVEL_MULTIPATH
)
729 rdev
->desc_nr
= sb
->this_disk
.number
;
735 mdp_super_t
*refsb
= (mdp_super_t
*)page_address(refdev
->sb_page
);
736 if (!uuid_equal(refsb
, sb
)) {
737 printk(KERN_WARNING
"md: %s has different UUID to %s\n",
738 b
, bdevname(refdev
->bdev
,b2
));
741 if (!sb_equal(refsb
, sb
)) {
742 printk(KERN_WARNING
"md: %s has same UUID"
743 " but different superblock to %s\n",
744 b
, bdevname(refdev
->bdev
, b2
));
748 ev2
= md_event(refsb
);
754 rdev
->size
= calc_num_sectors(rdev
, sb
->chunk_size
) / 2;
756 if (rdev
->size
< sb
->size
&& sb
->level
> 1)
757 /* "this cannot possibly happen" ... */
765 * validate_super for 0.90.0
767 static int super_90_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
770 mdp_super_t
*sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
771 __u64 ev1
= md_event(sb
);
773 rdev
->raid_disk
= -1;
774 clear_bit(Faulty
, &rdev
->flags
);
775 clear_bit(In_sync
, &rdev
->flags
);
776 clear_bit(WriteMostly
, &rdev
->flags
);
777 clear_bit(BarriersNotsupp
, &rdev
->flags
);
779 if (mddev
->raid_disks
== 0) {
780 mddev
->major_version
= 0;
781 mddev
->minor_version
= sb
->minor_version
;
782 mddev
->patch_version
= sb
->patch_version
;
784 mddev
->chunk_size
= sb
->chunk_size
;
785 mddev
->ctime
= sb
->ctime
;
786 mddev
->utime
= sb
->utime
;
787 mddev
->level
= sb
->level
;
788 mddev
->clevel
[0] = 0;
789 mddev
->layout
= sb
->layout
;
790 mddev
->raid_disks
= sb
->raid_disks
;
791 mddev
->size
= sb
->size
;
793 mddev
->bitmap_offset
= 0;
794 mddev
->default_bitmap_offset
= MD_SB_BYTES
>> 9;
796 if (mddev
->minor_version
>= 91) {
797 mddev
->reshape_position
= sb
->reshape_position
;
798 mddev
->delta_disks
= sb
->delta_disks
;
799 mddev
->new_level
= sb
->new_level
;
800 mddev
->new_layout
= sb
->new_layout
;
801 mddev
->new_chunk
= sb
->new_chunk
;
803 mddev
->reshape_position
= MaxSector
;
804 mddev
->delta_disks
= 0;
805 mddev
->new_level
= mddev
->level
;
806 mddev
->new_layout
= mddev
->layout
;
807 mddev
->new_chunk
= mddev
->chunk_size
;
810 if (sb
->state
& (1<<MD_SB_CLEAN
))
811 mddev
->recovery_cp
= MaxSector
;
813 if (sb
->events_hi
== sb
->cp_events_hi
&&
814 sb
->events_lo
== sb
->cp_events_lo
) {
815 mddev
->recovery_cp
= sb
->recovery_cp
;
817 mddev
->recovery_cp
= 0;
820 memcpy(mddev
->uuid
+0, &sb
->set_uuid0
, 4);
821 memcpy(mddev
->uuid
+4, &sb
->set_uuid1
, 4);
822 memcpy(mddev
->uuid
+8, &sb
->set_uuid2
, 4);
823 memcpy(mddev
->uuid
+12,&sb
->set_uuid3
, 4);
825 mddev
->max_disks
= MD_SB_DISKS
;
827 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
) &&
828 mddev
->bitmap_file
== NULL
)
829 mddev
->bitmap_offset
= mddev
->default_bitmap_offset
;
831 } else if (mddev
->pers
== NULL
) {
832 /* Insist on good event counter while assembling */
834 if (ev1
< mddev
->events
)
836 } else if (mddev
->bitmap
) {
837 /* if adding to array with a bitmap, then we can accept an
838 * older device ... but not too old.
840 if (ev1
< mddev
->bitmap
->events_cleared
)
843 if (ev1
< mddev
->events
)
844 /* just a hot-add of a new device, leave raid_disk at -1 */
848 if (mddev
->level
!= LEVEL_MULTIPATH
) {
849 desc
= sb
->disks
+ rdev
->desc_nr
;
851 if (desc
->state
& (1<<MD_DISK_FAULTY
))
852 set_bit(Faulty
, &rdev
->flags
);
853 else if (desc
->state
& (1<<MD_DISK_SYNC
) /* &&
854 desc->raid_disk < mddev->raid_disks */) {
855 set_bit(In_sync
, &rdev
->flags
);
856 rdev
->raid_disk
= desc
->raid_disk
;
858 if (desc
->state
& (1<<MD_DISK_WRITEMOSTLY
))
859 set_bit(WriteMostly
, &rdev
->flags
);
860 } else /* MULTIPATH are always insync */
861 set_bit(In_sync
, &rdev
->flags
);
866 * sync_super for 0.90.0
868 static void super_90_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
871 struct list_head
*tmp
;
873 int next_spare
= mddev
->raid_disks
;
876 /* make rdev->sb match mddev data..
879 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
880 * 3/ any empty disks < next_spare become removed
882 * disks[0] gets initialised to REMOVED because
883 * we cannot be sure from other fields if it has
884 * been initialised or not.
887 int active
=0, working
=0,failed
=0,spare
=0,nr_disks
=0;
889 rdev
->sb_size
= MD_SB_BYTES
;
891 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
893 memset(sb
, 0, sizeof(*sb
));
895 sb
->md_magic
= MD_SB_MAGIC
;
896 sb
->major_version
= mddev
->major_version
;
897 sb
->patch_version
= mddev
->patch_version
;
898 sb
->gvalid_words
= 0; /* ignored */
899 memcpy(&sb
->set_uuid0
, mddev
->uuid
+0, 4);
900 memcpy(&sb
->set_uuid1
, mddev
->uuid
+4, 4);
901 memcpy(&sb
->set_uuid2
, mddev
->uuid
+8, 4);
902 memcpy(&sb
->set_uuid3
, mddev
->uuid
+12,4);
904 sb
->ctime
= mddev
->ctime
;
905 sb
->level
= mddev
->level
;
906 sb
->size
= mddev
->size
;
907 sb
->raid_disks
= mddev
->raid_disks
;
908 sb
->md_minor
= mddev
->md_minor
;
909 sb
->not_persistent
= 0;
910 sb
->utime
= mddev
->utime
;
912 sb
->events_hi
= (mddev
->events
>>32);
913 sb
->events_lo
= (u32
)mddev
->events
;
915 if (mddev
->reshape_position
== MaxSector
)
916 sb
->minor_version
= 90;
918 sb
->minor_version
= 91;
919 sb
->reshape_position
= mddev
->reshape_position
;
920 sb
->new_level
= mddev
->new_level
;
921 sb
->delta_disks
= mddev
->delta_disks
;
922 sb
->new_layout
= mddev
->new_layout
;
923 sb
->new_chunk
= mddev
->new_chunk
;
925 mddev
->minor_version
= sb
->minor_version
;
928 sb
->recovery_cp
= mddev
->recovery_cp
;
929 sb
->cp_events_hi
= (mddev
->events
>>32);
930 sb
->cp_events_lo
= (u32
)mddev
->events
;
931 if (mddev
->recovery_cp
== MaxSector
)
932 sb
->state
= (1<< MD_SB_CLEAN
);
936 sb
->layout
= mddev
->layout
;
937 sb
->chunk_size
= mddev
->chunk_size
;
939 if (mddev
->bitmap
&& mddev
->bitmap_file
== NULL
)
940 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
942 sb
->disks
[0].state
= (1<<MD_DISK_REMOVED
);
943 rdev_for_each(rdev2
, tmp
, mddev
) {
946 if (rdev2
->raid_disk
>= 0 && test_bit(In_sync
, &rdev2
->flags
)
947 && !test_bit(Faulty
, &rdev2
->flags
))
948 desc_nr
= rdev2
->raid_disk
;
950 desc_nr
= next_spare
++;
951 rdev2
->desc_nr
= desc_nr
;
952 d
= &sb
->disks
[rdev2
->desc_nr
];
954 d
->number
= rdev2
->desc_nr
;
955 d
->major
= MAJOR(rdev2
->bdev
->bd_dev
);
956 d
->minor
= MINOR(rdev2
->bdev
->bd_dev
);
957 if (rdev2
->raid_disk
>= 0 && test_bit(In_sync
, &rdev2
->flags
)
958 && !test_bit(Faulty
, &rdev2
->flags
))
959 d
->raid_disk
= rdev2
->raid_disk
;
961 d
->raid_disk
= rdev2
->desc_nr
; /* compatibility */
962 if (test_bit(Faulty
, &rdev2
->flags
))
963 d
->state
= (1<<MD_DISK_FAULTY
);
964 else if (test_bit(In_sync
, &rdev2
->flags
)) {
965 d
->state
= (1<<MD_DISK_ACTIVE
);
966 d
->state
|= (1<<MD_DISK_SYNC
);
974 if (test_bit(WriteMostly
, &rdev2
->flags
))
975 d
->state
|= (1<<MD_DISK_WRITEMOSTLY
);
977 /* now set the "removed" and "faulty" bits on any missing devices */
978 for (i
=0 ; i
< mddev
->raid_disks
; i
++) {
979 mdp_disk_t
*d
= &sb
->disks
[i
];
980 if (d
->state
== 0 && d
->number
== 0) {
983 d
->state
= (1<<MD_DISK_REMOVED
);
984 d
->state
|= (1<<MD_DISK_FAULTY
);
988 sb
->nr_disks
= nr_disks
;
989 sb
->active_disks
= active
;
990 sb
->working_disks
= working
;
991 sb
->failed_disks
= failed
;
992 sb
->spare_disks
= spare
;
994 sb
->this_disk
= sb
->disks
[rdev
->desc_nr
];
995 sb
->sb_csum
= calc_sb_csum(sb
);
999 * rdev_size_change for 0.90.0
1001 static unsigned long long
1002 super_90_rdev_size_change(mdk_rdev_t
*rdev
, sector_t num_sectors
)
1004 if (num_sectors
&& num_sectors
< rdev
->mddev
->size
* 2)
1005 return 0; /* component must fit device */
1006 if (rdev
->mddev
->bitmap_offset
)
1007 return 0; /* can't move bitmap */
1008 rdev
->sb_start
= calc_dev_sboffset(rdev
->bdev
);
1009 if (!num_sectors
|| num_sectors
> rdev
->sb_start
)
1010 num_sectors
= rdev
->sb_start
;
1011 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1013 md_super_wait(rdev
->mddev
);
1014 return num_sectors
/ 2; /* kB for sysfs */
1019 * version 1 superblock
1022 static __le32
calc_sb_1_csum(struct mdp_superblock_1
* sb
)
1026 unsigned long long newcsum
;
1027 int size
= 256 + le32_to_cpu(sb
->max_dev
)*2;
1028 __le32
*isuper
= (__le32
*)sb
;
1031 disk_csum
= sb
->sb_csum
;
1034 for (i
=0; size
>=4; size
-= 4 )
1035 newcsum
+= le32_to_cpu(*isuper
++);
1038 newcsum
+= le16_to_cpu(*(__le16
*) isuper
);
1040 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
1041 sb
->sb_csum
= disk_csum
;
1042 return cpu_to_le32(csum
);
1045 static int super_1_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
1047 struct mdp_superblock_1
*sb
;
1050 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1054 * Calculate the position of the superblock in 512byte sectors.
1055 * It is always aligned to a 4K boundary and
1056 * depeding on minor_version, it can be:
1057 * 0: At least 8K, but less than 12K, from end of device
1058 * 1: At start of device
1059 * 2: 4K from start of device.
1061 switch(minor_version
) {
1063 sb_start
= rdev
->bdev
->bd_inode
->i_size
>> 9;
1065 sb_start
&= ~(sector_t
)(4*2-1);
1076 rdev
->sb_start
= sb_start
;
1078 /* superblock is rarely larger than 1K, but it can be larger,
1079 * and it is safe to read 4k, so we do that
1081 ret
= read_disk_sb(rdev
, 4096);
1082 if (ret
) return ret
;
1085 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1087 if (sb
->magic
!= cpu_to_le32(MD_SB_MAGIC
) ||
1088 sb
->major_version
!= cpu_to_le32(1) ||
1089 le32_to_cpu(sb
->max_dev
) > (4096-256)/2 ||
1090 le64_to_cpu(sb
->super_offset
) != rdev
->sb_start
||
1091 (le32_to_cpu(sb
->feature_map
) & ~MD_FEATURE_ALL
) != 0)
1094 if (calc_sb_1_csum(sb
) != sb
->sb_csum
) {
1095 printk("md: invalid superblock checksum on %s\n",
1096 bdevname(rdev
->bdev
,b
));
1099 if (le64_to_cpu(sb
->data_size
) < 10) {
1100 printk("md: data_size too small on %s\n",
1101 bdevname(rdev
->bdev
,b
));
1104 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BITMAP_OFFSET
)) {
1105 if (sb
->level
!= cpu_to_le32(1) &&
1106 sb
->level
!= cpu_to_le32(4) &&
1107 sb
->level
!= cpu_to_le32(5) &&
1108 sb
->level
!= cpu_to_le32(6) &&
1109 sb
->level
!= cpu_to_le32(10)) {
1111 "md: bitmaps not supported for this level.\n");
1116 rdev
->preferred_minor
= 0xffff;
1117 rdev
->data_offset
= le64_to_cpu(sb
->data_offset
);
1118 atomic_set(&rdev
->corrected_errors
, le32_to_cpu(sb
->cnt_corrected_read
));
1120 rdev
->sb_size
= le32_to_cpu(sb
->max_dev
) * 2 + 256;
1121 bmask
= queue_hardsect_size(rdev
->bdev
->bd_disk
->queue
)-1;
1122 if (rdev
->sb_size
& bmask
)
1123 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1126 && rdev
->data_offset
< sb_start
+ (rdev
->sb_size
/512))
1129 if (sb
->level
== cpu_to_le32(LEVEL_MULTIPATH
))
1132 rdev
->desc_nr
= le32_to_cpu(sb
->dev_number
);
1138 struct mdp_superblock_1
*refsb
=
1139 (struct mdp_superblock_1
*)page_address(refdev
->sb_page
);
1141 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16) != 0 ||
1142 sb
->level
!= refsb
->level
||
1143 sb
->layout
!= refsb
->layout
||
1144 sb
->chunksize
!= refsb
->chunksize
) {
1145 printk(KERN_WARNING
"md: %s has strangely different"
1146 " superblock to %s\n",
1147 bdevname(rdev
->bdev
,b
),
1148 bdevname(refdev
->bdev
,b2
));
1151 ev1
= le64_to_cpu(sb
->events
);
1152 ev2
= le64_to_cpu(refsb
->events
);
1160 rdev
->size
= ((rdev
->bdev
->bd_inode
->i_size
>>9) - le64_to_cpu(sb
->data_offset
)) / 2;
1162 rdev
->size
= rdev
->sb_start
/ 2;
1163 if (rdev
->size
< le64_to_cpu(sb
->data_size
)/2)
1165 rdev
->size
= le64_to_cpu(sb
->data_size
)/2;
1166 if (le32_to_cpu(sb
->chunksize
))
1167 rdev
->size
&= ~((sector_t
)le32_to_cpu(sb
->chunksize
)/2 - 1);
1169 if (le64_to_cpu(sb
->size
) > rdev
->size
*2)
1174 static int super_1_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1176 struct mdp_superblock_1
*sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1177 __u64 ev1
= le64_to_cpu(sb
->events
);
1179 rdev
->raid_disk
= -1;
1180 clear_bit(Faulty
, &rdev
->flags
);
1181 clear_bit(In_sync
, &rdev
->flags
);
1182 clear_bit(WriteMostly
, &rdev
->flags
);
1183 clear_bit(BarriersNotsupp
, &rdev
->flags
);
1185 if (mddev
->raid_disks
== 0) {
1186 mddev
->major_version
= 1;
1187 mddev
->patch_version
= 0;
1188 mddev
->external
= 0;
1189 mddev
->chunk_size
= le32_to_cpu(sb
->chunksize
) << 9;
1190 mddev
->ctime
= le64_to_cpu(sb
->ctime
) & ((1ULL << 32)-1);
1191 mddev
->utime
= le64_to_cpu(sb
->utime
) & ((1ULL << 32)-1);
1192 mddev
->level
= le32_to_cpu(sb
->level
);
1193 mddev
->clevel
[0] = 0;
1194 mddev
->layout
= le32_to_cpu(sb
->layout
);
1195 mddev
->raid_disks
= le32_to_cpu(sb
->raid_disks
);
1196 mddev
->size
= le64_to_cpu(sb
->size
)/2;
1197 mddev
->events
= ev1
;
1198 mddev
->bitmap_offset
= 0;
1199 mddev
->default_bitmap_offset
= 1024 >> 9;
1201 mddev
->recovery_cp
= le64_to_cpu(sb
->resync_offset
);
1202 memcpy(mddev
->uuid
, sb
->set_uuid
, 16);
1204 mddev
->max_disks
= (4096-256)/2;
1206 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BITMAP_OFFSET
) &&
1207 mddev
->bitmap_file
== NULL
)
1208 mddev
->bitmap_offset
= (__s32
)le32_to_cpu(sb
->bitmap_offset
);
1210 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
1211 mddev
->reshape_position
= le64_to_cpu(sb
->reshape_position
);
1212 mddev
->delta_disks
= le32_to_cpu(sb
->delta_disks
);
1213 mddev
->new_level
= le32_to_cpu(sb
->new_level
);
1214 mddev
->new_layout
= le32_to_cpu(sb
->new_layout
);
1215 mddev
->new_chunk
= le32_to_cpu(sb
->new_chunk
)<<9;
1217 mddev
->reshape_position
= MaxSector
;
1218 mddev
->delta_disks
= 0;
1219 mddev
->new_level
= mddev
->level
;
1220 mddev
->new_layout
= mddev
->layout
;
1221 mddev
->new_chunk
= mddev
->chunk_size
;
1224 } else if (mddev
->pers
== NULL
) {
1225 /* Insist of good event counter while assembling */
1227 if (ev1
< mddev
->events
)
1229 } else if (mddev
->bitmap
) {
1230 /* If adding to array with a bitmap, then we can accept an
1231 * older device, but not too old.
1233 if (ev1
< mddev
->bitmap
->events_cleared
)
1236 if (ev1
< mddev
->events
)
1237 /* just a hot-add of a new device, leave raid_disk at -1 */
1240 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1242 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
1244 case 0xffff: /* spare */
1246 case 0xfffe: /* faulty */
1247 set_bit(Faulty
, &rdev
->flags
);
1250 if ((le32_to_cpu(sb
->feature_map
) &
1251 MD_FEATURE_RECOVERY_OFFSET
))
1252 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
1254 set_bit(In_sync
, &rdev
->flags
);
1255 rdev
->raid_disk
= role
;
1258 if (sb
->devflags
& WriteMostly1
)
1259 set_bit(WriteMostly
, &rdev
->flags
);
1260 } else /* MULTIPATH are always insync */
1261 set_bit(In_sync
, &rdev
->flags
);
1266 static void super_1_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1268 struct mdp_superblock_1
*sb
;
1269 struct list_head
*tmp
;
1272 /* make rdev->sb match mddev and rdev data. */
1274 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1276 sb
->feature_map
= 0;
1278 sb
->recovery_offset
= cpu_to_le64(0);
1279 memset(sb
->pad1
, 0, sizeof(sb
->pad1
));
1280 memset(sb
->pad2
, 0, sizeof(sb
->pad2
));
1281 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
1283 sb
->utime
= cpu_to_le64((__u64
)mddev
->utime
);
1284 sb
->events
= cpu_to_le64(mddev
->events
);
1286 sb
->resync_offset
= cpu_to_le64(mddev
->recovery_cp
);
1288 sb
->resync_offset
= cpu_to_le64(0);
1290 sb
->cnt_corrected_read
= cpu_to_le32(atomic_read(&rdev
->corrected_errors
));
1292 sb
->raid_disks
= cpu_to_le32(mddev
->raid_disks
);
1293 sb
->size
= cpu_to_le64(mddev
->size
<<1);
1295 if (mddev
->bitmap
&& mddev
->bitmap_file
== NULL
) {
1296 sb
->bitmap_offset
= cpu_to_le32((__u32
)mddev
->bitmap_offset
);
1297 sb
->feature_map
= cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
);
1300 if (rdev
->raid_disk
>= 0 &&
1301 !test_bit(In_sync
, &rdev
->flags
) &&
1302 rdev
->recovery_offset
> 0) {
1303 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET
);
1304 sb
->recovery_offset
= cpu_to_le64(rdev
->recovery_offset
);
1307 if (mddev
->reshape_position
!= MaxSector
) {
1308 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE
);
1309 sb
->reshape_position
= cpu_to_le64(mddev
->reshape_position
);
1310 sb
->new_layout
= cpu_to_le32(mddev
->new_layout
);
1311 sb
->delta_disks
= cpu_to_le32(mddev
->delta_disks
);
1312 sb
->new_level
= cpu_to_le32(mddev
->new_level
);
1313 sb
->new_chunk
= cpu_to_le32(mddev
->new_chunk
>>9);
1317 rdev_for_each(rdev2
, tmp
, mddev
)
1318 if (rdev2
->desc_nr
+1 > max_dev
)
1319 max_dev
= rdev2
->desc_nr
+1;
1321 if (max_dev
> le32_to_cpu(sb
->max_dev
))
1322 sb
->max_dev
= cpu_to_le32(max_dev
);
1323 for (i
=0; i
<max_dev
;i
++)
1324 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1326 rdev_for_each(rdev2
, tmp
, mddev
) {
1328 if (test_bit(Faulty
, &rdev2
->flags
))
1329 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1330 else if (test_bit(In_sync
, &rdev2
->flags
))
1331 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1332 else if (rdev2
->raid_disk
>= 0 && rdev2
->recovery_offset
> 0)
1333 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1335 sb
->dev_roles
[i
] = cpu_to_le16(0xffff);
1338 sb
->sb_csum
= calc_sb_1_csum(sb
);
1341 static unsigned long long
1342 super_1_rdev_size_change(mdk_rdev_t
*rdev
, sector_t num_sectors
)
1344 struct mdp_superblock_1
*sb
;
1345 sector_t max_sectors
;
1346 if (num_sectors
&& num_sectors
< rdev
->mddev
->size
* 2)
1347 return 0; /* component must fit device */
1348 if (rdev
->sb_start
< rdev
->data_offset
) {
1349 /* minor versions 1 and 2; superblock before data */
1350 max_sectors
= rdev
->bdev
->bd_inode
->i_size
>> 9;
1351 max_sectors
-= rdev
->data_offset
;
1352 if (!num_sectors
|| num_sectors
> max_sectors
)
1353 num_sectors
= max_sectors
;
1354 } else if (rdev
->mddev
->bitmap_offset
) {
1355 /* minor version 0 with bitmap we can't move */
1358 /* minor version 0; superblock after data */
1360 sb_start
= (rdev
->bdev
->bd_inode
->i_size
>> 9) - 8*2;
1361 sb_start
&= ~(sector_t
)(4*2 - 1);
1362 max_sectors
= rdev
->size
* 2 + sb_start
- rdev
->sb_start
;
1363 if (!num_sectors
|| num_sectors
> max_sectors
)
1364 num_sectors
= max_sectors
;
1365 rdev
->sb_start
= sb_start
;
1367 sb
= (struct mdp_superblock_1
*) page_address(rdev
->sb_page
);
1368 sb
->data_size
= cpu_to_le64(num_sectors
);
1369 sb
->super_offset
= rdev
->sb_start
;
1370 sb
->sb_csum
= calc_sb_1_csum(sb
);
1371 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1373 md_super_wait(rdev
->mddev
);
1374 return num_sectors
/ 2; /* kB for sysfs */
1377 static struct super_type super_types
[] = {
1380 .owner
= THIS_MODULE
,
1381 .load_super
= super_90_load
,
1382 .validate_super
= super_90_validate
,
1383 .sync_super
= super_90_sync
,
1384 .rdev_size_change
= super_90_rdev_size_change
,
1388 .owner
= THIS_MODULE
,
1389 .load_super
= super_1_load
,
1390 .validate_super
= super_1_validate
,
1391 .sync_super
= super_1_sync
,
1392 .rdev_size_change
= super_1_rdev_size_change
,
1396 static int match_mddev_units(mddev_t
*mddev1
, mddev_t
*mddev2
)
1398 mdk_rdev_t
*rdev
, *rdev2
;
1401 rdev_for_each_rcu(rdev
, mddev1
)
1402 rdev_for_each_rcu(rdev2
, mddev2
)
1403 if (rdev
->bdev
->bd_contains
==
1404 rdev2
->bdev
->bd_contains
) {
1412 static LIST_HEAD(pending_raid_disks
);
1414 static int bind_rdev_to_array(mdk_rdev_t
* rdev
, mddev_t
* mddev
)
1416 char b
[BDEVNAME_SIZE
];
1426 /* prevent duplicates */
1427 if (find_rdev(mddev
, rdev
->bdev
->bd_dev
))
1430 /* make sure rdev->size exceeds mddev->size */
1431 if (rdev
->size
&& (mddev
->size
== 0 || rdev
->size
< mddev
->size
)) {
1433 /* Cannot change size, so fail
1434 * If mddev->level <= 0, then we don't care
1435 * about aligning sizes (e.g. linear)
1437 if (mddev
->level
> 0)
1440 mddev
->size
= rdev
->size
;
1443 /* Verify rdev->desc_nr is unique.
1444 * If it is -1, assign a free number, else
1445 * check number is not in use
1447 if (rdev
->desc_nr
< 0) {
1449 if (mddev
->pers
) choice
= mddev
->raid_disks
;
1450 while (find_rdev_nr(mddev
, choice
))
1452 rdev
->desc_nr
= choice
;
1454 if (find_rdev_nr(mddev
, rdev
->desc_nr
))
1457 bdevname(rdev
->bdev
,b
);
1458 while ( (s
=strchr(b
, '/')) != NULL
)
1461 rdev
->mddev
= mddev
;
1462 printk(KERN_INFO
"md: bind<%s>\n", b
);
1464 if ((err
= kobject_add(&rdev
->kobj
, &mddev
->kobj
, "dev-%s", b
)))
1467 if (rdev
->bdev
->bd_part
)
1468 ko
= &rdev
->bdev
->bd_part
->dev
.kobj
;
1470 ko
= &rdev
->bdev
->bd_disk
->dev
.kobj
;
1471 if ((err
= sysfs_create_link(&rdev
->kobj
, ko
, "block"))) {
1472 kobject_del(&rdev
->kobj
);
1475 list_add_rcu(&rdev
->same_set
, &mddev
->disks
);
1476 bd_claim_by_disk(rdev
->bdev
, rdev
->bdev
->bd_holder
, mddev
->gendisk
);
1480 printk(KERN_WARNING
"md: failed to register dev-%s for %s\n",
1485 static void md_delayed_delete(struct work_struct
*ws
)
1487 mdk_rdev_t
*rdev
= container_of(ws
, mdk_rdev_t
, del_work
);
1488 kobject_del(&rdev
->kobj
);
1489 kobject_put(&rdev
->kobj
);
1492 static void unbind_rdev_from_array(mdk_rdev_t
* rdev
)
1494 char b
[BDEVNAME_SIZE
];
1499 bd_release_from_disk(rdev
->bdev
, rdev
->mddev
->gendisk
);
1500 list_del_rcu(&rdev
->same_set
);
1501 printk(KERN_INFO
"md: unbind<%s>\n", bdevname(rdev
->bdev
,b
));
1503 sysfs_remove_link(&rdev
->kobj
, "block");
1505 /* We need to delay this, otherwise we can deadlock when
1506 * writing to 'remove' to "dev/state". We also need
1507 * to delay it due to rcu usage.
1510 INIT_WORK(&rdev
->del_work
, md_delayed_delete
);
1511 kobject_get(&rdev
->kobj
);
1512 schedule_work(&rdev
->del_work
);
1516 * prevent the device from being mounted, repartitioned or
1517 * otherwise reused by a RAID array (or any other kernel
1518 * subsystem), by bd_claiming the device.
1520 static int lock_rdev(mdk_rdev_t
*rdev
, dev_t dev
, int shared
)
1523 struct block_device
*bdev
;
1524 char b
[BDEVNAME_SIZE
];
1526 bdev
= open_by_devnum(dev
, FMODE_READ
|FMODE_WRITE
);
1528 printk(KERN_ERR
"md: could not open %s.\n",
1529 __bdevname(dev
, b
));
1530 return PTR_ERR(bdev
);
1532 err
= bd_claim(bdev
, shared
? (mdk_rdev_t
*)lock_rdev
: rdev
);
1534 printk(KERN_ERR
"md: could not bd_claim %s.\n",
1540 set_bit(AllReserved
, &rdev
->flags
);
1545 static void unlock_rdev(mdk_rdev_t
*rdev
)
1547 struct block_device
*bdev
= rdev
->bdev
;
1555 void md_autodetect_dev(dev_t dev
);
1557 static void export_rdev(mdk_rdev_t
* rdev
)
1559 char b
[BDEVNAME_SIZE
];
1560 printk(KERN_INFO
"md: export_rdev(%s)\n",
1561 bdevname(rdev
->bdev
,b
));
1566 if (test_bit(AutoDetected
, &rdev
->flags
))
1567 md_autodetect_dev(rdev
->bdev
->bd_dev
);
1570 kobject_put(&rdev
->kobj
);
1573 static void kick_rdev_from_array(mdk_rdev_t
* rdev
)
1575 unbind_rdev_from_array(rdev
);
1579 static void export_array(mddev_t
*mddev
)
1581 struct list_head
*tmp
;
1584 rdev_for_each(rdev
, tmp
, mddev
) {
1589 kick_rdev_from_array(rdev
);
1591 if (!list_empty(&mddev
->disks
))
1593 mddev
->raid_disks
= 0;
1594 mddev
->major_version
= 0;
1597 static void print_desc(mdp_disk_t
*desc
)
1599 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc
->number
,
1600 desc
->major
,desc
->minor
,desc
->raid_disk
,desc
->state
);
1603 static void print_sb(mdp_super_t
*sb
)
1608 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1609 sb
->major_version
, sb
->minor_version
, sb
->patch_version
,
1610 sb
->set_uuid0
, sb
->set_uuid1
, sb
->set_uuid2
, sb
->set_uuid3
,
1612 printk(KERN_INFO
"md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1613 sb
->level
, sb
->size
, sb
->nr_disks
, sb
->raid_disks
,
1614 sb
->md_minor
, sb
->layout
, sb
->chunk_size
);
1615 printk(KERN_INFO
"md: UT:%08x ST:%d AD:%d WD:%d"
1616 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1617 sb
->utime
, sb
->state
, sb
->active_disks
, sb
->working_disks
,
1618 sb
->failed_disks
, sb
->spare_disks
,
1619 sb
->sb_csum
, (unsigned long)sb
->events_lo
);
1622 for (i
= 0; i
< MD_SB_DISKS
; i
++) {
1625 desc
= sb
->disks
+ i
;
1626 if (desc
->number
|| desc
->major
|| desc
->minor
||
1627 desc
->raid_disk
|| (desc
->state
&& (desc
->state
!= 4))) {
1628 printk(" D %2d: ", i
);
1632 printk(KERN_INFO
"md: THIS: ");
1633 print_desc(&sb
->this_disk
);
1637 static void print_rdev(mdk_rdev_t
*rdev
)
1639 char b
[BDEVNAME_SIZE
];
1640 printk(KERN_INFO
"md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1641 bdevname(rdev
->bdev
,b
), (unsigned long long)rdev
->size
,
1642 test_bit(Faulty
, &rdev
->flags
), test_bit(In_sync
, &rdev
->flags
),
1644 if (rdev
->sb_loaded
) {
1645 printk(KERN_INFO
"md: rdev superblock:\n");
1646 print_sb((mdp_super_t
*)page_address(rdev
->sb_page
));
1648 printk(KERN_INFO
"md: no rdev superblock!\n");
1651 static void md_print_devices(void)
1653 struct list_head
*tmp
, *tmp2
;
1656 char b
[BDEVNAME_SIZE
];
1659 printk("md: **********************************\n");
1660 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1661 printk("md: **********************************\n");
1662 for_each_mddev(mddev
, tmp
) {
1665 bitmap_print_sb(mddev
->bitmap
);
1667 printk("%s: ", mdname(mddev
));
1668 rdev_for_each(rdev
, tmp2
, mddev
)
1669 printk("<%s>", bdevname(rdev
->bdev
,b
));
1672 rdev_for_each(rdev
, tmp2
, mddev
)
1675 printk("md: **********************************\n");
1680 static void sync_sbs(mddev_t
* mddev
, int nospares
)
1682 /* Update each superblock (in-memory image), but
1683 * if we are allowed to, skip spares which already
1684 * have the right event counter, or have one earlier
1685 * (which would mean they aren't being marked as dirty
1686 * with the rest of the array)
1689 struct list_head
*tmp
;
1691 rdev_for_each(rdev
, tmp
, mddev
) {
1692 if (rdev
->sb_events
== mddev
->events
||
1694 rdev
->raid_disk
< 0 &&
1695 (rdev
->sb_events
&1)==0 &&
1696 rdev
->sb_events
+1 == mddev
->events
)) {
1697 /* Don't update this superblock */
1698 rdev
->sb_loaded
= 2;
1700 super_types
[mddev
->major_version
].
1701 sync_super(mddev
, rdev
);
1702 rdev
->sb_loaded
= 1;
1707 static void md_update_sb(mddev_t
* mddev
, int force_change
)
1709 struct list_head
*tmp
;
1714 if (mddev
->external
)
1717 spin_lock_irq(&mddev
->write_lock
);
1719 set_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
1720 if (test_and_clear_bit(MD_CHANGE_DEVS
, &mddev
->flags
))
1722 if (test_and_clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
))
1723 /* just a clean<-> dirty transition, possibly leave spares alone,
1724 * though if events isn't the right even/odd, we will have to do
1730 if (mddev
->degraded
)
1731 /* If the array is degraded, then skipping spares is both
1732 * dangerous and fairly pointless.
1733 * Dangerous because a device that was removed from the array
1734 * might have a event_count that still looks up-to-date,
1735 * so it can be re-added without a resync.
1736 * Pointless because if there are any spares to skip,
1737 * then a recovery will happen and soon that array won't
1738 * be degraded any more and the spare can go back to sleep then.
1742 sync_req
= mddev
->in_sync
;
1743 mddev
->utime
= get_seconds();
1745 /* If this is just a dirty<->clean transition, and the array is clean
1746 * and 'events' is odd, we can roll back to the previous clean state */
1748 && (mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
1749 && (mddev
->events
& 1)
1750 && mddev
->events
!= 1)
1753 /* otherwise we have to go forward and ... */
1755 if (!mddev
->in_sync
|| mddev
->recovery_cp
!= MaxSector
) { /* not clean */
1756 /* .. if the array isn't clean, insist on an odd 'events' */
1757 if ((mddev
->events
&1)==0) {
1762 /* otherwise insist on an even 'events' (for clean states) */
1763 if ((mddev
->events
&1)) {
1770 if (!mddev
->events
) {
1772 * oops, this 64-bit counter should never wrap.
1773 * Either we are in around ~1 trillion A.C., assuming
1774 * 1 reboot per second, or we have a bug:
1781 * do not write anything to disk if using
1782 * nonpersistent superblocks
1784 if (!mddev
->persistent
) {
1785 if (!mddev
->external
)
1786 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
1788 spin_unlock_irq(&mddev
->write_lock
);
1789 wake_up(&mddev
->sb_wait
);
1792 sync_sbs(mddev
, nospares
);
1793 spin_unlock_irq(&mddev
->write_lock
);
1796 "md: updating %s RAID superblock on device (in sync %d)\n",
1797 mdname(mddev
),mddev
->in_sync
);
1799 bitmap_update_sb(mddev
->bitmap
);
1800 rdev_for_each(rdev
, tmp
, mddev
) {
1801 char b
[BDEVNAME_SIZE
];
1802 dprintk(KERN_INFO
"md: ");
1803 if (rdev
->sb_loaded
!= 1)
1804 continue; /* no noise on spare devices */
1805 if (test_bit(Faulty
, &rdev
->flags
))
1806 dprintk("(skipping faulty ");
1808 dprintk("%s ", bdevname(rdev
->bdev
,b
));
1809 if (!test_bit(Faulty
, &rdev
->flags
)) {
1810 md_super_write(mddev
,rdev
,
1811 rdev
->sb_start
, rdev
->sb_size
,
1813 dprintk(KERN_INFO
"(write) %s's sb offset: %llu\n",
1814 bdevname(rdev
->bdev
,b
),
1815 (unsigned long long)rdev
->sb_start
);
1816 rdev
->sb_events
= mddev
->events
;
1820 if (mddev
->level
== LEVEL_MULTIPATH
)
1821 /* only need to write one superblock... */
1824 md_super_wait(mddev
);
1825 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
1827 spin_lock_irq(&mddev
->write_lock
);
1828 if (mddev
->in_sync
!= sync_req
||
1829 test_bit(MD_CHANGE_DEVS
, &mddev
->flags
)) {
1830 /* have to write it out again */
1831 spin_unlock_irq(&mddev
->write_lock
);
1834 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
1835 spin_unlock_irq(&mddev
->write_lock
);
1836 wake_up(&mddev
->sb_wait
);
1840 /* words written to sysfs files may, or may not, be \n terminated.
1841 * We want to accept with case. For this we use cmd_match.
1843 static int cmd_match(const char *cmd
, const char *str
)
1845 /* See if cmd, written into a sysfs file, matches
1846 * str. They must either be the same, or cmd can
1847 * have a trailing newline
1849 while (*cmd
&& *str
&& *cmd
== *str
) {
1860 struct rdev_sysfs_entry
{
1861 struct attribute attr
;
1862 ssize_t (*show
)(mdk_rdev_t
*, char *);
1863 ssize_t (*store
)(mdk_rdev_t
*, const char *, size_t);
1867 state_show(mdk_rdev_t
*rdev
, char *page
)
1872 if (test_bit(Faulty
, &rdev
->flags
)) {
1873 len
+= sprintf(page
+len
, "%sfaulty",sep
);
1876 if (test_bit(In_sync
, &rdev
->flags
)) {
1877 len
+= sprintf(page
+len
, "%sin_sync",sep
);
1880 if (test_bit(WriteMostly
, &rdev
->flags
)) {
1881 len
+= sprintf(page
+len
, "%swrite_mostly",sep
);
1884 if (test_bit(Blocked
, &rdev
->flags
)) {
1885 len
+= sprintf(page
+len
, "%sblocked", sep
);
1888 if (!test_bit(Faulty
, &rdev
->flags
) &&
1889 !test_bit(In_sync
, &rdev
->flags
)) {
1890 len
+= sprintf(page
+len
, "%sspare", sep
);
1893 return len
+sprintf(page
+len
, "\n");
1897 state_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
1900 * faulty - simulates and error
1901 * remove - disconnects the device
1902 * writemostly - sets write_mostly
1903 * -writemostly - clears write_mostly
1904 * blocked - sets the Blocked flag
1905 * -blocked - clears the Blocked flag
1908 if (cmd_match(buf
, "faulty") && rdev
->mddev
->pers
) {
1909 md_error(rdev
->mddev
, rdev
);
1911 } else if (cmd_match(buf
, "remove")) {
1912 if (rdev
->raid_disk
>= 0)
1915 mddev_t
*mddev
= rdev
->mddev
;
1916 kick_rdev_from_array(rdev
);
1918 md_update_sb(mddev
, 1);
1919 md_new_event(mddev
);
1922 } else if (cmd_match(buf
, "writemostly")) {
1923 set_bit(WriteMostly
, &rdev
->flags
);
1925 } else if (cmd_match(buf
, "-writemostly")) {
1926 clear_bit(WriteMostly
, &rdev
->flags
);
1928 } else if (cmd_match(buf
, "blocked")) {
1929 set_bit(Blocked
, &rdev
->flags
);
1931 } else if (cmd_match(buf
, "-blocked")) {
1932 clear_bit(Blocked
, &rdev
->flags
);
1933 wake_up(&rdev
->blocked_wait
);
1934 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
1935 md_wakeup_thread(rdev
->mddev
->thread
);
1940 sysfs_notify(&rdev
->kobj
, NULL
, "state");
1941 return err
? err
: len
;
1943 static struct rdev_sysfs_entry rdev_state
=
1944 __ATTR(state
, S_IRUGO
|S_IWUSR
, state_show
, state_store
);
1947 errors_show(mdk_rdev_t
*rdev
, char *page
)
1949 return sprintf(page
, "%d\n", atomic_read(&rdev
->corrected_errors
));
1953 errors_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
1956 unsigned long n
= simple_strtoul(buf
, &e
, 10);
1957 if (*buf
&& (*e
== 0 || *e
== '\n')) {
1958 atomic_set(&rdev
->corrected_errors
, n
);
1963 static struct rdev_sysfs_entry rdev_errors
=
1964 __ATTR(errors
, S_IRUGO
|S_IWUSR
, errors_show
, errors_store
);
1967 slot_show(mdk_rdev_t
*rdev
, char *page
)
1969 if (rdev
->raid_disk
< 0)
1970 return sprintf(page
, "none\n");
1972 return sprintf(page
, "%d\n", rdev
->raid_disk
);
1976 slot_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
1981 int slot
= simple_strtoul(buf
, &e
, 10);
1982 if (strncmp(buf
, "none", 4)==0)
1984 else if (e
==buf
|| (*e
&& *e
!= '\n'))
1986 if (rdev
->mddev
->pers
&& slot
== -1) {
1987 /* Setting 'slot' on an active array requires also
1988 * updating the 'rd%d' link, and communicating
1989 * with the personality with ->hot_*_disk.
1990 * For now we only support removing
1991 * failed/spare devices. This normally happens automatically,
1992 * but not when the metadata is externally managed.
1994 if (rdev
->raid_disk
== -1)
1996 /* personality does all needed checks */
1997 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
1999 err
= rdev
->mddev
->pers
->
2000 hot_remove_disk(rdev
->mddev
, rdev
->raid_disk
);
2003 sprintf(nm
, "rd%d", rdev
->raid_disk
);
2004 sysfs_remove_link(&rdev
->mddev
->kobj
, nm
);
2005 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2006 md_wakeup_thread(rdev
->mddev
->thread
);
2007 } else if (rdev
->mddev
->pers
) {
2009 struct list_head
*tmp
;
2010 /* Activating a spare .. or possibly reactivating
2011 * if we every get bitmaps working here.
2014 if (rdev
->raid_disk
!= -1)
2017 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
2020 rdev_for_each(rdev2
, tmp
, rdev
->mddev
)
2021 if (rdev2
->raid_disk
== slot
)
2024 rdev
->raid_disk
= slot
;
2025 if (test_bit(In_sync
, &rdev
->flags
))
2026 rdev
->saved_raid_disk
= slot
;
2028 rdev
->saved_raid_disk
= -1;
2029 err
= rdev
->mddev
->pers
->
2030 hot_add_disk(rdev
->mddev
, rdev
);
2032 rdev
->raid_disk
= -1;
2035 sysfs_notify(&rdev
->kobj
, NULL
, "state");
2036 sprintf(nm
, "rd%d", rdev
->raid_disk
);
2037 if (sysfs_create_link(&rdev
->mddev
->kobj
, &rdev
->kobj
, nm
))
2039 "md: cannot register "
2041 nm
, mdname(rdev
->mddev
));
2043 /* don't wakeup anyone, leave that to userspace. */
2045 if (slot
>= rdev
->mddev
->raid_disks
)
2047 rdev
->raid_disk
= slot
;
2048 /* assume it is working */
2049 clear_bit(Faulty
, &rdev
->flags
);
2050 clear_bit(WriteMostly
, &rdev
->flags
);
2051 set_bit(In_sync
, &rdev
->flags
);
2052 sysfs_notify(&rdev
->kobj
, NULL
, "state");
2058 static struct rdev_sysfs_entry rdev_slot
=
2059 __ATTR(slot
, S_IRUGO
|S_IWUSR
, slot_show
, slot_store
);
2062 offset_show(mdk_rdev_t
*rdev
, char *page
)
2064 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->data_offset
);
2068 offset_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2071 unsigned long long offset
= simple_strtoull(buf
, &e
, 10);
2072 if (e
==buf
|| (*e
&& *e
!= '\n'))
2074 if (rdev
->mddev
->pers
&& rdev
->raid_disk
>= 0)
2076 if (rdev
->size
&& rdev
->mddev
->external
)
2077 /* Must set offset before size, so overlap checks
2080 rdev
->data_offset
= offset
;
2084 static struct rdev_sysfs_entry rdev_offset
=
2085 __ATTR(offset
, S_IRUGO
|S_IWUSR
, offset_show
, offset_store
);
2088 rdev_size_show(mdk_rdev_t
*rdev
, char *page
)
2090 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->size
);
2093 static int overlaps(sector_t s1
, sector_t l1
, sector_t s2
, sector_t l2
)
2095 /* check if two start/length pairs overlap */
2104 rdev_size_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2106 unsigned long long size
;
2107 unsigned long long oldsize
= rdev
->size
;
2108 mddev_t
*my_mddev
= rdev
->mddev
;
2110 if (strict_strtoull(buf
, 10, &size
) < 0)
2112 if (size
< my_mddev
->size
)
2114 if (my_mddev
->pers
&& rdev
->raid_disk
>= 0) {
2115 if (my_mddev
->persistent
) {
2116 size
= super_types
[my_mddev
->major_version
].
2117 rdev_size_change(rdev
, size
* 2);
2121 size
= (rdev
->bdev
->bd_inode
->i_size
>> 10);
2122 size
-= rdev
->data_offset
/2;
2124 if (size
< my_mddev
->size
)
2125 return -EINVAL
; /* component must fit device */
2129 if (size
> oldsize
&& my_mddev
->external
) {
2130 /* need to check that all other rdevs with the same ->bdev
2131 * do not overlap. We need to unlock the mddev to avoid
2132 * a deadlock. We have already changed rdev->size, and if
2133 * we have to change it back, we will have the lock again.
2137 struct list_head
*tmp
, *tmp2
;
2139 mddev_unlock(my_mddev
);
2140 for_each_mddev(mddev
, tmp
) {
2144 rdev_for_each(rdev2
, tmp2
, mddev
)
2145 if (test_bit(AllReserved
, &rdev2
->flags
) ||
2146 (rdev
->bdev
== rdev2
->bdev
&&
2148 overlaps(rdev
->data_offset
, rdev
->size
* 2,
2150 rdev2
->size
* 2))) {
2154 mddev_unlock(mddev
);
2160 mddev_lock(my_mddev
);
2162 /* Someone else could have slipped in a size
2163 * change here, but doing so is just silly.
2164 * We put oldsize back because we *know* it is
2165 * safe, and trust userspace not to race with
2168 rdev
->size
= oldsize
;
2175 static struct rdev_sysfs_entry rdev_size
=
2176 __ATTR(size
, S_IRUGO
|S_IWUSR
, rdev_size_show
, rdev_size_store
);
2178 static struct attribute
*rdev_default_attrs
[] = {
2187 rdev_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
2189 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
2190 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
2191 mddev_t
*mddev
= rdev
->mddev
;
2197 rv
= mddev
? mddev_lock(mddev
) : -EBUSY
;
2199 if (rdev
->mddev
== NULL
)
2202 rv
= entry
->show(rdev
, page
);
2203 mddev_unlock(mddev
);
2209 rdev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
2210 const char *page
, size_t length
)
2212 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
2213 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
2215 mddev_t
*mddev
= rdev
->mddev
;
2219 if (!capable(CAP_SYS_ADMIN
))
2221 rv
= mddev
? mddev_lock(mddev
): -EBUSY
;
2223 if (rdev
->mddev
== NULL
)
2226 rv
= entry
->store(rdev
, page
, length
);
2227 mddev_unlock(mddev
);
2232 static void rdev_free(struct kobject
*ko
)
2234 mdk_rdev_t
*rdev
= container_of(ko
, mdk_rdev_t
, kobj
);
2237 static struct sysfs_ops rdev_sysfs_ops
= {
2238 .show
= rdev_attr_show
,
2239 .store
= rdev_attr_store
,
2241 static struct kobj_type rdev_ktype
= {
2242 .release
= rdev_free
,
2243 .sysfs_ops
= &rdev_sysfs_ops
,
2244 .default_attrs
= rdev_default_attrs
,
2248 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2250 * mark the device faulty if:
2252 * - the device is nonexistent (zero size)
2253 * - the device has no valid superblock
2255 * a faulty rdev _never_ has rdev->sb set.
2257 static mdk_rdev_t
*md_import_device(dev_t newdev
, int super_format
, int super_minor
)
2259 char b
[BDEVNAME_SIZE
];
2264 rdev
= kzalloc(sizeof(*rdev
), GFP_KERNEL
);
2266 printk(KERN_ERR
"md: could not alloc mem for new device!\n");
2267 return ERR_PTR(-ENOMEM
);
2270 if ((err
= alloc_disk_sb(rdev
)))
2273 err
= lock_rdev(rdev
, newdev
, super_format
== -2);
2277 kobject_init(&rdev
->kobj
, &rdev_ktype
);
2280 rdev
->saved_raid_disk
= -1;
2281 rdev
->raid_disk
= -1;
2283 rdev
->data_offset
= 0;
2284 rdev
->sb_events
= 0;
2285 atomic_set(&rdev
->nr_pending
, 0);
2286 atomic_set(&rdev
->read_errors
, 0);
2287 atomic_set(&rdev
->corrected_errors
, 0);
2289 size
= rdev
->bdev
->bd_inode
->i_size
>> BLOCK_SIZE_BITS
;
2292 "md: %s has zero or unknown size, marking faulty!\n",
2293 bdevname(rdev
->bdev
,b
));
2298 if (super_format
>= 0) {
2299 err
= super_types
[super_format
].
2300 load_super(rdev
, NULL
, super_minor
);
2301 if (err
== -EINVAL
) {
2303 "md: %s does not have a valid v%d.%d "
2304 "superblock, not importing!\n",
2305 bdevname(rdev
->bdev
,b
),
2306 super_format
, super_minor
);
2311 "md: could not read %s's sb, not importing!\n",
2312 bdevname(rdev
->bdev
,b
));
2317 INIT_LIST_HEAD(&rdev
->same_set
);
2318 init_waitqueue_head(&rdev
->blocked_wait
);
2323 if (rdev
->sb_page
) {
2329 return ERR_PTR(err
);
2333 * Check a full RAID array for plausibility
2337 static void analyze_sbs(mddev_t
* mddev
)
2340 struct list_head
*tmp
;
2341 mdk_rdev_t
*rdev
, *freshest
;
2342 char b
[BDEVNAME_SIZE
];
2345 rdev_for_each(rdev
, tmp
, mddev
)
2346 switch (super_types
[mddev
->major_version
].
2347 load_super(rdev
, freshest
, mddev
->minor_version
)) {
2355 "md: fatal superblock inconsistency in %s"
2356 " -- removing from array\n",
2357 bdevname(rdev
->bdev
,b
));
2358 kick_rdev_from_array(rdev
);
2362 super_types
[mddev
->major_version
].
2363 validate_super(mddev
, freshest
);
2366 rdev_for_each(rdev
, tmp
, mddev
) {
2367 if (rdev
!= freshest
)
2368 if (super_types
[mddev
->major_version
].
2369 validate_super(mddev
, rdev
)) {
2370 printk(KERN_WARNING
"md: kicking non-fresh %s"
2372 bdevname(rdev
->bdev
,b
));
2373 kick_rdev_from_array(rdev
);
2376 if (mddev
->level
== LEVEL_MULTIPATH
) {
2377 rdev
->desc_nr
= i
++;
2378 rdev
->raid_disk
= rdev
->desc_nr
;
2379 set_bit(In_sync
, &rdev
->flags
);
2380 } else if (rdev
->raid_disk
>= mddev
->raid_disks
) {
2381 rdev
->raid_disk
= -1;
2382 clear_bit(In_sync
, &rdev
->flags
);
2388 if (mddev
->recovery_cp
!= MaxSector
&&
2390 printk(KERN_ERR
"md: %s: raid array is not clean"
2391 " -- starting background reconstruction\n",
2396 static void md_safemode_timeout(unsigned long data
);
2399 safe_delay_show(mddev_t
*mddev
, char *page
)
2401 int msec
= (mddev
->safemode_delay
*1000)/HZ
;
2402 return sprintf(page
, "%d.%03d\n", msec
/1000, msec
%1000);
2405 safe_delay_store(mddev_t
*mddev
, const char *cbuf
, size_t len
)
2413 /* remove a period, and count digits after it */
2414 if (len
>= sizeof(buf
))
2416 strlcpy(buf
, cbuf
, len
);
2418 for (i
=0; i
<len
; i
++) {
2420 if (isdigit(buf
[i
])) {
2425 } else if (buf
[i
] == '.') {
2430 msec
= simple_strtoul(buf
, &e
, 10);
2431 if (e
== buf
|| (*e
&& *e
!= '\n'))
2433 msec
= (msec
* 1000) / scale
;
2435 mddev
->safemode_delay
= 0;
2437 unsigned long old_delay
= mddev
->safemode_delay
;
2438 mddev
->safemode_delay
= (msec
*HZ
)/1000;
2439 if (mddev
->safemode_delay
== 0)
2440 mddev
->safemode_delay
= 1;
2441 if (mddev
->safemode_delay
< old_delay
)
2442 md_safemode_timeout((unsigned long)mddev
);
2446 static struct md_sysfs_entry md_safe_delay
=
2447 __ATTR(safe_mode_delay
, S_IRUGO
|S_IWUSR
,safe_delay_show
, safe_delay_store
);
2450 level_show(mddev_t
*mddev
, char *page
)
2452 struct mdk_personality
*p
= mddev
->pers
;
2454 return sprintf(page
, "%s\n", p
->name
);
2455 else if (mddev
->clevel
[0])
2456 return sprintf(page
, "%s\n", mddev
->clevel
);
2457 else if (mddev
->level
!= LEVEL_NONE
)
2458 return sprintf(page
, "%d\n", mddev
->level
);
2464 level_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2471 if (len
>= sizeof(mddev
->clevel
))
2473 strncpy(mddev
->clevel
, buf
, len
);
2474 if (mddev
->clevel
[len
-1] == '\n')
2476 mddev
->clevel
[len
] = 0;
2477 mddev
->level
= LEVEL_NONE
;
2481 static struct md_sysfs_entry md_level
=
2482 __ATTR(level
, S_IRUGO
|S_IWUSR
, level_show
, level_store
);
2486 layout_show(mddev_t
*mddev
, char *page
)
2488 /* just a number, not meaningful for all levels */
2489 if (mddev
->reshape_position
!= MaxSector
&&
2490 mddev
->layout
!= mddev
->new_layout
)
2491 return sprintf(page
, "%d (%d)\n",
2492 mddev
->new_layout
, mddev
->layout
);
2493 return sprintf(page
, "%d\n", mddev
->layout
);
2497 layout_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2500 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2502 if (!*buf
|| (*e
&& *e
!= '\n'))
2507 if (mddev
->reshape_position
!= MaxSector
)
2508 mddev
->new_layout
= n
;
2513 static struct md_sysfs_entry md_layout
=
2514 __ATTR(layout
, S_IRUGO
|S_IWUSR
, layout_show
, layout_store
);
2518 raid_disks_show(mddev_t
*mddev
, char *page
)
2520 if (mddev
->raid_disks
== 0)
2522 if (mddev
->reshape_position
!= MaxSector
&&
2523 mddev
->delta_disks
!= 0)
2524 return sprintf(page
, "%d (%d)\n", mddev
->raid_disks
,
2525 mddev
->raid_disks
- mddev
->delta_disks
);
2526 return sprintf(page
, "%d\n", mddev
->raid_disks
);
2529 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
);
2532 raid_disks_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2536 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2538 if (!*buf
|| (*e
&& *e
!= '\n'))
2542 rv
= update_raid_disks(mddev
, n
);
2543 else if (mddev
->reshape_position
!= MaxSector
) {
2544 int olddisks
= mddev
->raid_disks
- mddev
->delta_disks
;
2545 mddev
->delta_disks
= n
- olddisks
;
2546 mddev
->raid_disks
= n
;
2548 mddev
->raid_disks
= n
;
2549 return rv
? rv
: len
;
2551 static struct md_sysfs_entry md_raid_disks
=
2552 __ATTR(raid_disks
, S_IRUGO
|S_IWUSR
, raid_disks_show
, raid_disks_store
);
2555 chunk_size_show(mddev_t
*mddev
, char *page
)
2557 if (mddev
->reshape_position
!= MaxSector
&&
2558 mddev
->chunk_size
!= mddev
->new_chunk
)
2559 return sprintf(page
, "%d (%d)\n", mddev
->new_chunk
,
2561 return sprintf(page
, "%d\n", mddev
->chunk_size
);
2565 chunk_size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2567 /* can only set chunk_size if array is not yet active */
2569 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2571 if (!*buf
|| (*e
&& *e
!= '\n'))
2576 else if (mddev
->reshape_position
!= MaxSector
)
2577 mddev
->new_chunk
= n
;
2579 mddev
->chunk_size
= n
;
2582 static struct md_sysfs_entry md_chunk_size
=
2583 __ATTR(chunk_size
, S_IRUGO
|S_IWUSR
, chunk_size_show
, chunk_size_store
);
2586 resync_start_show(mddev_t
*mddev
, char *page
)
2588 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->recovery_cp
);
2592 resync_start_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2595 unsigned long long n
= simple_strtoull(buf
, &e
, 10);
2599 if (!*buf
|| (*e
&& *e
!= '\n'))
2602 mddev
->recovery_cp
= n
;
2605 static struct md_sysfs_entry md_resync_start
=
2606 __ATTR(resync_start
, S_IRUGO
|S_IWUSR
, resync_start_show
, resync_start_store
);
2609 * The array state can be:
2612 * No devices, no size, no level
2613 * Equivalent to STOP_ARRAY ioctl
2615 * May have some settings, but array is not active
2616 * all IO results in error
2617 * When written, doesn't tear down array, but just stops it
2618 * suspended (not supported yet)
2619 * All IO requests will block. The array can be reconfigured.
2620 * Writing this, if accepted, will block until array is quiescent
2622 * no resync can happen. no superblocks get written.
2623 * write requests fail
2625 * like readonly, but behaves like 'clean' on a write request.
2627 * clean - no pending writes, but otherwise active.
2628 * When written to inactive array, starts without resync
2629 * If a write request arrives then
2630 * if metadata is known, mark 'dirty' and switch to 'active'.
2631 * if not known, block and switch to write-pending
2632 * If written to an active array that has pending writes, then fails.
2634 * fully active: IO and resync can be happening.
2635 * When written to inactive array, starts with resync
2638 * clean, but writes are blocked waiting for 'active' to be written.
2641 * like active, but no writes have been seen for a while (100msec).
2644 enum array_state
{ clear
, inactive
, suspended
, readonly
, read_auto
, clean
, active
,
2645 write_pending
, active_idle
, bad_word
};
2646 static char *array_states
[] = {
2647 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2648 "write-pending", "active-idle", NULL
};
2650 static int match_word(const char *word
, char **list
)
2653 for (n
=0; list
[n
]; n
++)
2654 if (cmd_match(word
, list
[n
]))
2660 array_state_show(mddev_t
*mddev
, char *page
)
2662 enum array_state st
= inactive
;
2675 else if (test_bit(MD_CHANGE_CLEAN
, &mddev
->flags
))
2677 else if (mddev
->safemode
)
2683 if (list_empty(&mddev
->disks
) &&
2684 mddev
->raid_disks
== 0 &&
2690 return sprintf(page
, "%s\n", array_states
[st
]);
2693 static int do_md_stop(mddev_t
* mddev
, int ro
, int is_open
);
2694 static int do_md_run(mddev_t
* mddev
);
2695 static int restart_array(mddev_t
*mddev
);
2698 array_state_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2701 enum array_state st
= match_word(buf
, array_states
);
2706 /* stopping an active array */
2707 if (atomic_read(&mddev
->openers
) > 0)
2709 err
= do_md_stop(mddev
, 0, 0);
2712 /* stopping an active array */
2714 if (atomic_read(&mddev
->openers
) > 0)
2716 err
= do_md_stop(mddev
, 2, 0);
2718 err
= 0; /* already inactive */
2721 break; /* not supported yet */
2724 err
= do_md_stop(mddev
, 1, 0);
2727 set_disk_ro(mddev
->gendisk
, 1);
2728 err
= do_md_run(mddev
);
2734 err
= do_md_stop(mddev
, 1, 0);
2736 err
= restart_array(mddev
);
2739 set_disk_ro(mddev
->gendisk
, 0);
2743 err
= do_md_run(mddev
);
2748 restart_array(mddev
);
2749 spin_lock_irq(&mddev
->write_lock
);
2750 if (atomic_read(&mddev
->writes_pending
) == 0) {
2751 if (mddev
->in_sync
== 0) {
2753 if (mddev
->safemode
== 1)
2754 mddev
->safemode
= 0;
2755 if (mddev
->persistent
)
2756 set_bit(MD_CHANGE_CLEAN
,
2762 spin_unlock_irq(&mddev
->write_lock
);
2765 mddev
->recovery_cp
= MaxSector
;
2766 err
= do_md_run(mddev
);
2771 restart_array(mddev
);
2772 if (mddev
->external
)
2773 clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
2774 wake_up(&mddev
->sb_wait
);
2778 set_disk_ro(mddev
->gendisk
, 0);
2779 err
= do_md_run(mddev
);
2784 /* these cannot be set */
2790 sysfs_notify(&mddev
->kobj
, NULL
, "array_state");
2794 static struct md_sysfs_entry md_array_state
=
2795 __ATTR(array_state
, S_IRUGO
|S_IWUSR
, array_state_show
, array_state_store
);
2798 null_show(mddev_t
*mddev
, char *page
)
2804 new_dev_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2806 /* buf must be %d:%d\n? giving major and minor numbers */
2807 /* The new device is added to the array.
2808 * If the array has a persistent superblock, we read the
2809 * superblock to initialise info and check validity.
2810 * Otherwise, only checking done is that in bind_rdev_to_array,
2811 * which mainly checks size.
2814 int major
= simple_strtoul(buf
, &e
, 10);
2820 if (!*buf
|| *e
!= ':' || !e
[1] || e
[1] == '\n')
2822 minor
= simple_strtoul(e
+1, &e
, 10);
2823 if (*e
&& *e
!= '\n')
2825 dev
= MKDEV(major
, minor
);
2826 if (major
!= MAJOR(dev
) ||
2827 minor
!= MINOR(dev
))
2831 if (mddev
->persistent
) {
2832 rdev
= md_import_device(dev
, mddev
->major_version
,
2833 mddev
->minor_version
);
2834 if (!IS_ERR(rdev
) && !list_empty(&mddev
->disks
)) {
2835 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
2836 mdk_rdev_t
, same_set
);
2837 err
= super_types
[mddev
->major_version
]
2838 .load_super(rdev
, rdev0
, mddev
->minor_version
);
2842 } else if (mddev
->external
)
2843 rdev
= md_import_device(dev
, -2, -1);
2845 rdev
= md_import_device(dev
, -1, -1);
2848 return PTR_ERR(rdev
);
2849 err
= bind_rdev_to_array(rdev
, mddev
);
2853 return err
? err
: len
;
2856 static struct md_sysfs_entry md_new_device
=
2857 __ATTR(new_dev
, S_IWUSR
, null_show
, new_dev_store
);
2860 bitmap_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2863 unsigned long chunk
, end_chunk
;
2867 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
2869 chunk
= end_chunk
= simple_strtoul(buf
, &end
, 0);
2870 if (buf
== end
) break;
2871 if (*end
== '-') { /* range */
2873 end_chunk
= simple_strtoul(buf
, &end
, 0);
2874 if (buf
== end
) break;
2876 if (*end
&& !isspace(*end
)) break;
2877 bitmap_dirty_bits(mddev
->bitmap
, chunk
, end_chunk
);
2879 while (isspace(*buf
)) buf
++;
2881 bitmap_unplug(mddev
->bitmap
); /* flush the bits to disk */
2886 static struct md_sysfs_entry md_bitmap
=
2887 __ATTR(bitmap_set_bits
, S_IWUSR
, null_show
, bitmap_store
);
2890 size_show(mddev_t
*mddev
, char *page
)
2892 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->size
);
2895 static int update_size(mddev_t
*mddev
, sector_t num_sectors
);
2898 size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2900 /* If array is inactive, we can reduce the component size, but
2901 * not increase it (except from 0).
2902 * If array is active, we can try an on-line resize
2906 unsigned long long size
= simple_strtoull(buf
, &e
, 10);
2907 if (!*buf
|| *buf
== '\n' ||
2912 err
= update_size(mddev
, size
* 2);
2913 md_update_sb(mddev
, 1);
2915 if (mddev
->size
== 0 ||
2921 return err
? err
: len
;
2924 static struct md_sysfs_entry md_size
=
2925 __ATTR(component_size
, S_IRUGO
|S_IWUSR
, size_show
, size_store
);
2930 * 'none' for arrays with no metadata (good luck...)
2931 * 'external' for arrays with externally managed metadata,
2932 * or N.M for internally known formats
2935 metadata_show(mddev_t
*mddev
, char *page
)
2937 if (mddev
->persistent
)
2938 return sprintf(page
, "%d.%d\n",
2939 mddev
->major_version
, mddev
->minor_version
);
2940 else if (mddev
->external
)
2941 return sprintf(page
, "external:%s\n", mddev
->metadata_type
);
2943 return sprintf(page
, "none\n");
2947 metadata_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2951 if (!list_empty(&mddev
->disks
))
2954 if (cmd_match(buf
, "none")) {
2955 mddev
->persistent
= 0;
2956 mddev
->external
= 0;
2957 mddev
->major_version
= 0;
2958 mddev
->minor_version
= 90;
2961 if (strncmp(buf
, "external:", 9) == 0) {
2962 size_t namelen
= len
-9;
2963 if (namelen
>= sizeof(mddev
->metadata_type
))
2964 namelen
= sizeof(mddev
->metadata_type
)-1;
2965 strncpy(mddev
->metadata_type
, buf
+9, namelen
);
2966 mddev
->metadata_type
[namelen
] = 0;
2967 if (namelen
&& mddev
->metadata_type
[namelen
-1] == '\n')
2968 mddev
->metadata_type
[--namelen
] = 0;
2969 mddev
->persistent
= 0;
2970 mddev
->external
= 1;
2971 mddev
->major_version
= 0;
2972 mddev
->minor_version
= 90;
2975 major
= simple_strtoul(buf
, &e
, 10);
2976 if (e
==buf
|| *e
!= '.')
2979 minor
= simple_strtoul(buf
, &e
, 10);
2980 if (e
==buf
|| (*e
&& *e
!= '\n') )
2982 if (major
>= ARRAY_SIZE(super_types
) || super_types
[major
].name
== NULL
)
2984 mddev
->major_version
= major
;
2985 mddev
->minor_version
= minor
;
2986 mddev
->persistent
= 1;
2987 mddev
->external
= 0;
2991 static struct md_sysfs_entry md_metadata
=
2992 __ATTR(metadata_version
, S_IRUGO
|S_IWUSR
, metadata_show
, metadata_store
);
2995 action_show(mddev_t
*mddev
, char *page
)
2997 char *type
= "idle";
2998 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
2999 (!mddev
->ro
&& test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))) {
3000 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
3002 else if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
3003 if (!test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
3005 else if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
3009 } else if (test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
))
3012 return sprintf(page
, "%s\n", type
);
3016 action_store(mddev_t
*mddev
, const char *page
, size_t len
)
3018 if (!mddev
->pers
|| !mddev
->pers
->sync_request
)
3021 if (cmd_match(page
, "idle")) {
3022 if (mddev
->sync_thread
) {
3023 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
3024 md_unregister_thread(mddev
->sync_thread
);
3025 mddev
->sync_thread
= NULL
;
3026 mddev
->recovery
= 0;
3028 } else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
3029 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
3031 else if (cmd_match(page
, "resync"))
3032 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3033 else if (cmd_match(page
, "recover")) {
3034 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
3035 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3036 } else if (cmd_match(page
, "reshape")) {
3038 if (mddev
->pers
->start_reshape
== NULL
)
3040 err
= mddev
->pers
->start_reshape(mddev
);
3043 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
3045 if (cmd_match(page
, "check"))
3046 set_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
3047 else if (!cmd_match(page
, "repair"))
3049 set_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
3050 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
3052 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3053 md_wakeup_thread(mddev
->thread
);
3054 sysfs_notify(&mddev
->kobj
, NULL
, "sync_action");
3059 mismatch_cnt_show(mddev_t
*mddev
, char *page
)
3061 return sprintf(page
, "%llu\n",
3062 (unsigned long long) mddev
->resync_mismatches
);
3065 static struct md_sysfs_entry md_scan_mode
=
3066 __ATTR(sync_action
, S_IRUGO
|S_IWUSR
, action_show
, action_store
);
3069 static struct md_sysfs_entry md_mismatches
= __ATTR_RO(mismatch_cnt
);
3072 sync_min_show(mddev_t
*mddev
, char *page
)
3074 return sprintf(page
, "%d (%s)\n", speed_min(mddev
),
3075 mddev
->sync_speed_min
? "local": "system");
3079 sync_min_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3083 if (strncmp(buf
, "system", 6)==0) {
3084 mddev
->sync_speed_min
= 0;
3087 min
= simple_strtoul(buf
, &e
, 10);
3088 if (buf
== e
|| (*e
&& *e
!= '\n') || min
<= 0)
3090 mddev
->sync_speed_min
= min
;
3094 static struct md_sysfs_entry md_sync_min
=
3095 __ATTR(sync_speed_min
, S_IRUGO
|S_IWUSR
, sync_min_show
, sync_min_store
);
3098 sync_max_show(mddev_t
*mddev
, char *page
)
3100 return sprintf(page
, "%d (%s)\n", speed_max(mddev
),
3101 mddev
->sync_speed_max
? "local": "system");
3105 sync_max_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3109 if (strncmp(buf
, "system", 6)==0) {
3110 mddev
->sync_speed_max
= 0;
3113 max
= simple_strtoul(buf
, &e
, 10);
3114 if (buf
== e
|| (*e
&& *e
!= '\n') || max
<= 0)
3116 mddev
->sync_speed_max
= max
;
3120 static struct md_sysfs_entry md_sync_max
=
3121 __ATTR(sync_speed_max
, S_IRUGO
|S_IWUSR
, sync_max_show
, sync_max_store
);
3124 degraded_show(mddev_t
*mddev
, char *page
)
3126 return sprintf(page
, "%d\n", mddev
->degraded
);
3128 static struct md_sysfs_entry md_degraded
= __ATTR_RO(degraded
);
3131 sync_force_parallel_show(mddev_t
*mddev
, char *page
)
3133 return sprintf(page
, "%d\n", mddev
->parallel_resync
);
3137 sync_force_parallel_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3141 if (strict_strtol(buf
, 10, &n
))
3144 if (n
!= 0 && n
!= 1)
3147 mddev
->parallel_resync
= n
;
3149 if (mddev
->sync_thread
)
3150 wake_up(&resync_wait
);
3155 /* force parallel resync, even with shared block devices */
3156 static struct md_sysfs_entry md_sync_force_parallel
=
3157 __ATTR(sync_force_parallel
, S_IRUGO
|S_IWUSR
,
3158 sync_force_parallel_show
, sync_force_parallel_store
);
3161 sync_speed_show(mddev_t
*mddev
, char *page
)
3163 unsigned long resync
, dt
, db
;
3164 resync
= mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
);
3165 dt
= (jiffies
- mddev
->resync_mark
) / HZ
;
3167 db
= resync
- mddev
->resync_mark_cnt
;
3168 return sprintf(page
, "%lu\n", db
/dt
/2); /* K/sec */
3171 static struct md_sysfs_entry md_sync_speed
= __ATTR_RO(sync_speed
);
3174 sync_completed_show(mddev_t
*mddev
, char *page
)
3176 unsigned long max_blocks
, resync
;
3178 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
3179 max_blocks
= mddev
->resync_max_sectors
;
3181 max_blocks
= mddev
->size
<< 1;
3183 resync
= (mddev
->curr_resync
- atomic_read(&mddev
->recovery_active
));
3184 return sprintf(page
, "%lu / %lu\n", resync
, max_blocks
);
3187 static struct md_sysfs_entry md_sync_completed
= __ATTR_RO(sync_completed
);
3190 min_sync_show(mddev_t
*mddev
, char *page
)
3192 return sprintf(page
, "%llu\n",
3193 (unsigned long long)mddev
->resync_min
);
3196 min_sync_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3198 unsigned long long min
;
3199 if (strict_strtoull(buf
, 10, &min
))
3201 if (min
> mddev
->resync_max
)
3203 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3206 /* Must be a multiple of chunk_size */
3207 if (mddev
->chunk_size
) {
3208 if (min
& (sector_t
)((mddev
->chunk_size
>>9)-1))
3211 mddev
->resync_min
= min
;
3216 static struct md_sysfs_entry md_min_sync
=
3217 __ATTR(sync_min
, S_IRUGO
|S_IWUSR
, min_sync_show
, min_sync_store
);
3220 max_sync_show(mddev_t
*mddev
, char *page
)
3222 if (mddev
->resync_max
== MaxSector
)
3223 return sprintf(page
, "max\n");
3225 return sprintf(page
, "%llu\n",
3226 (unsigned long long)mddev
->resync_max
);
3229 max_sync_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3231 if (strncmp(buf
, "max", 3) == 0)
3232 mddev
->resync_max
= MaxSector
;
3234 unsigned long long max
;
3235 if (strict_strtoull(buf
, 10, &max
))
3237 if (max
< mddev
->resync_min
)
3239 if (max
< mddev
->resync_max
&&
3240 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3243 /* Must be a multiple of chunk_size */
3244 if (mddev
->chunk_size
) {
3245 if (max
& (sector_t
)((mddev
->chunk_size
>>9)-1))
3248 mddev
->resync_max
= max
;
3250 wake_up(&mddev
->recovery_wait
);
3254 static struct md_sysfs_entry md_max_sync
=
3255 __ATTR(sync_max
, S_IRUGO
|S_IWUSR
, max_sync_show
, max_sync_store
);
3258 suspend_lo_show(mddev_t
*mddev
, char *page
)
3260 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_lo
);
3264 suspend_lo_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3267 unsigned long long new = simple_strtoull(buf
, &e
, 10);
3269 if (mddev
->pers
->quiesce
== NULL
)
3271 if (buf
== e
|| (*e
&& *e
!= '\n'))
3273 if (new >= mddev
->suspend_hi
||
3274 (new > mddev
->suspend_lo
&& new < mddev
->suspend_hi
)) {
3275 mddev
->suspend_lo
= new;
3276 mddev
->pers
->quiesce(mddev
, 2);
3281 static struct md_sysfs_entry md_suspend_lo
=
3282 __ATTR(suspend_lo
, S_IRUGO
|S_IWUSR
, suspend_lo_show
, suspend_lo_store
);
3286 suspend_hi_show(mddev_t
*mddev
, char *page
)
3288 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_hi
);
3292 suspend_hi_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3295 unsigned long long new = simple_strtoull(buf
, &e
, 10);
3297 if (mddev
->pers
->quiesce
== NULL
)
3299 if (buf
== e
|| (*e
&& *e
!= '\n'))
3301 if ((new <= mddev
->suspend_lo
&& mddev
->suspend_lo
>= mddev
->suspend_hi
) ||
3302 (new > mddev
->suspend_lo
&& new > mddev
->suspend_hi
)) {
3303 mddev
->suspend_hi
= new;
3304 mddev
->pers
->quiesce(mddev
, 1);
3305 mddev
->pers
->quiesce(mddev
, 0);
3310 static struct md_sysfs_entry md_suspend_hi
=
3311 __ATTR(suspend_hi
, S_IRUGO
|S_IWUSR
, suspend_hi_show
, suspend_hi_store
);
3314 reshape_position_show(mddev_t
*mddev
, char *page
)
3316 if (mddev
->reshape_position
!= MaxSector
)
3317 return sprintf(page
, "%llu\n",
3318 (unsigned long long)mddev
->reshape_position
);
3319 strcpy(page
, "none\n");
3324 reshape_position_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3327 unsigned long long new = simple_strtoull(buf
, &e
, 10);
3330 if (buf
== e
|| (*e
&& *e
!= '\n'))
3332 mddev
->reshape_position
= new;
3333 mddev
->delta_disks
= 0;
3334 mddev
->new_level
= mddev
->level
;
3335 mddev
->new_layout
= mddev
->layout
;
3336 mddev
->new_chunk
= mddev
->chunk_size
;
3340 static struct md_sysfs_entry md_reshape_position
=
3341 __ATTR(reshape_position
, S_IRUGO
|S_IWUSR
, reshape_position_show
,
3342 reshape_position_store
);
3345 static struct attribute
*md_default_attrs
[] = {
3348 &md_raid_disks
.attr
,
3349 &md_chunk_size
.attr
,
3351 &md_resync_start
.attr
,
3353 &md_new_device
.attr
,
3354 &md_safe_delay
.attr
,
3355 &md_array_state
.attr
,
3356 &md_reshape_position
.attr
,
3360 static struct attribute
*md_redundancy_attrs
[] = {
3362 &md_mismatches
.attr
,
3365 &md_sync_speed
.attr
,
3366 &md_sync_force_parallel
.attr
,
3367 &md_sync_completed
.attr
,
3370 &md_suspend_lo
.attr
,
3371 &md_suspend_hi
.attr
,
3376 static struct attribute_group md_redundancy_group
= {
3378 .attrs
= md_redundancy_attrs
,
3383 md_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
3385 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
3386 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
3391 rv
= mddev_lock(mddev
);
3393 rv
= entry
->show(mddev
, page
);
3394 mddev_unlock(mddev
);
3400 md_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
3401 const char *page
, size_t length
)
3403 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
3404 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
3409 if (!capable(CAP_SYS_ADMIN
))
3411 rv
= mddev_lock(mddev
);
3413 rv
= entry
->store(mddev
, page
, length
);
3414 mddev_unlock(mddev
);
3419 static void md_free(struct kobject
*ko
)
3421 mddev_t
*mddev
= container_of(ko
, mddev_t
, kobj
);
3425 static struct sysfs_ops md_sysfs_ops
= {
3426 .show
= md_attr_show
,
3427 .store
= md_attr_store
,
3429 static struct kobj_type md_ktype
= {
3431 .sysfs_ops
= &md_sysfs_ops
,
3432 .default_attrs
= md_default_attrs
,
3437 static struct kobject
*md_probe(dev_t dev
, int *part
, void *data
)
3439 static DEFINE_MUTEX(disks_mutex
);
3440 mddev_t
*mddev
= mddev_find(dev
);
3441 struct gendisk
*disk
;
3442 int partitioned
= (MAJOR(dev
) != MD_MAJOR
);
3443 int shift
= partitioned
? MdpMinorShift
: 0;
3444 int unit
= MINOR(dev
) >> shift
;
3450 mutex_lock(&disks_mutex
);
3451 if (mddev
->gendisk
) {
3452 mutex_unlock(&disks_mutex
);
3456 disk
= alloc_disk(1 << shift
);
3458 mutex_unlock(&disks_mutex
);
3462 disk
->major
= MAJOR(dev
);
3463 disk
->first_minor
= unit
<< shift
;
3465 sprintf(disk
->disk_name
, "md_d%d", unit
);
3467 sprintf(disk
->disk_name
, "md%d", unit
);
3468 disk
->fops
= &md_fops
;
3469 disk
->private_data
= mddev
;
3470 disk
->queue
= mddev
->queue
;
3472 mddev
->gendisk
= disk
;
3473 error
= kobject_init_and_add(&mddev
->kobj
, &md_ktype
, &disk
->dev
.kobj
,
3475 mutex_unlock(&disks_mutex
);
3477 printk(KERN_WARNING
"md: cannot register %s/md - name in use\n",
3480 kobject_uevent(&mddev
->kobj
, KOBJ_ADD
);
3484 static void md_safemode_timeout(unsigned long data
)
3486 mddev_t
*mddev
= (mddev_t
*) data
;
3488 if (!atomic_read(&mddev
->writes_pending
)) {
3489 mddev
->safemode
= 1;
3490 if (mddev
->external
)
3491 set_bit(MD_NOTIFY_ARRAY_STATE
, &mddev
->flags
);
3493 md_wakeup_thread(mddev
->thread
);
3496 static int start_dirty_degraded
;
3498 static int do_md_run(mddev_t
* mddev
)
3502 struct list_head
*tmp
;
3504 struct gendisk
*disk
;
3505 struct mdk_personality
*pers
;
3506 char b
[BDEVNAME_SIZE
];
3508 if (list_empty(&mddev
->disks
))
3509 /* cannot run an array with no devices.. */
3516 * Analyze all RAID superblock(s)
3518 if (!mddev
->raid_disks
) {
3519 if (!mddev
->persistent
)
3524 chunk_size
= mddev
->chunk_size
;
3527 if (chunk_size
> MAX_CHUNK_SIZE
) {
3528 printk(KERN_ERR
"too big chunk_size: %d > %d\n",
3529 chunk_size
, MAX_CHUNK_SIZE
);
3533 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
3535 if ( (1 << ffz(~chunk_size
)) != chunk_size
) {
3536 printk(KERN_ERR
"chunk_size of %d not valid\n", chunk_size
);
3539 if (chunk_size
< PAGE_SIZE
) {
3540 printk(KERN_ERR
"too small chunk_size: %d < %ld\n",
3541 chunk_size
, PAGE_SIZE
);
3545 /* devices must have minimum size of one chunk */
3546 rdev_for_each(rdev
, tmp
, mddev
) {
3547 if (test_bit(Faulty
, &rdev
->flags
))
3549 if (rdev
->size
< chunk_size
/ 1024) {
3551 "md: Dev %s smaller than chunk_size:"
3553 bdevname(rdev
->bdev
,b
),
3554 (unsigned long long)rdev
->size
,
3562 if (mddev
->level
!= LEVEL_NONE
)
3563 request_module("md-level-%d", mddev
->level
);
3564 else if (mddev
->clevel
[0])
3565 request_module("md-%s", mddev
->clevel
);
3569 * Drop all container device buffers, from now on
3570 * the only valid external interface is through the md
3573 rdev_for_each(rdev
, tmp
, mddev
) {
3574 if (test_bit(Faulty
, &rdev
->flags
))
3576 sync_blockdev(rdev
->bdev
);
3577 invalidate_bdev(rdev
->bdev
);
3579 /* perform some consistency tests on the device.
3580 * We don't want the data to overlap the metadata,
3581 * Internal Bitmap issues has handled elsewhere.
3583 if (rdev
->data_offset
< rdev
->sb_start
) {
3585 rdev
->data_offset
+ mddev
->size
*2
3587 printk("md: %s: data overlaps metadata\n",
3592 if (rdev
->sb_start
+ rdev
->sb_size
/512
3593 > rdev
->data_offset
) {
3594 printk("md: %s: metadata overlaps data\n",
3599 sysfs_notify(&rdev
->kobj
, NULL
, "state");
3602 md_probe(mddev
->unit
, NULL
, NULL
);
3603 disk
= mddev
->gendisk
;
3607 spin_lock(&pers_lock
);
3608 pers
= find_pers(mddev
->level
, mddev
->clevel
);
3609 if (!pers
|| !try_module_get(pers
->owner
)) {
3610 spin_unlock(&pers_lock
);
3611 if (mddev
->level
!= LEVEL_NONE
)
3612 printk(KERN_WARNING
"md: personality for level %d is not loaded!\n",
3615 printk(KERN_WARNING
"md: personality for level %s is not loaded!\n",
3620 spin_unlock(&pers_lock
);
3621 mddev
->level
= pers
->level
;
3622 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
3624 if (mddev
->reshape_position
!= MaxSector
&&
3625 pers
->start_reshape
== NULL
) {
3626 /* This personality cannot handle reshaping... */
3628 module_put(pers
->owner
);
3632 if (pers
->sync_request
) {
3633 /* Warn if this is a potentially silly
3636 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
3638 struct list_head
*tmp2
;
3640 rdev_for_each(rdev
, tmp
, mddev
) {
3641 rdev_for_each(rdev2
, tmp2
, mddev
) {
3643 rdev
->bdev
->bd_contains
==
3644 rdev2
->bdev
->bd_contains
) {
3646 "%s: WARNING: %s appears to be"
3647 " on the same physical disk as"
3650 bdevname(rdev
->bdev
,b
),
3651 bdevname(rdev2
->bdev
,b2
));
3658 "True protection against single-disk"
3659 " failure might be compromised.\n");
3662 mddev
->recovery
= 0;
3663 mddev
->resync_max_sectors
= mddev
->size
<< 1; /* may be over-ridden by personality */
3664 mddev
->barriers_work
= 1;
3665 mddev
->ok_start_degraded
= start_dirty_degraded
;
3668 mddev
->ro
= 2; /* read-only, but switch on first write */
3670 err
= mddev
->pers
->run(mddev
);
3672 printk(KERN_ERR
"md: pers->run() failed ...\n");
3673 else if (mddev
->pers
->sync_request
) {
3674 err
= bitmap_create(mddev
);
3676 printk(KERN_ERR
"%s: failed to create bitmap (%d)\n",
3677 mdname(mddev
), err
);
3678 mddev
->pers
->stop(mddev
);
3682 module_put(mddev
->pers
->owner
);
3684 bitmap_destroy(mddev
);
3687 if (mddev
->pers
->sync_request
) {
3688 if (sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
3690 "md: cannot register extra attributes for %s\n",
3692 } else if (mddev
->ro
== 2) /* auto-readonly not meaningful */
3695 atomic_set(&mddev
->writes_pending
,0);
3696 mddev
->safemode
= 0;
3697 mddev
->safemode_timer
.function
= md_safemode_timeout
;
3698 mddev
->safemode_timer
.data
= (unsigned long) mddev
;
3699 mddev
->safemode_delay
= (200 * HZ
)/1000 +1; /* 200 msec delay */
3702 rdev_for_each(rdev
, tmp
, mddev
)
3703 if (rdev
->raid_disk
>= 0) {
3705 sprintf(nm
, "rd%d", rdev
->raid_disk
);
3706 if (sysfs_create_link(&mddev
->kobj
, &rdev
->kobj
, nm
))
3707 printk("md: cannot register %s for %s\n",
3711 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3714 md_update_sb(mddev
, 0);
3716 set_capacity(disk
, mddev
->array_sectors
);
3718 /* If we call blk_queue_make_request here, it will
3719 * re-initialise max_sectors etc which may have been
3720 * refined inside -> run. So just set the bits we need to set.
3721 * Most initialisation happended when we called
3722 * blk_queue_make_request(..., md_fail_request)
3725 mddev
->queue
->queuedata
= mddev
;
3726 mddev
->queue
->make_request_fn
= mddev
->pers
->make_request
;
3728 /* If there is a partially-recovered drive we need to
3729 * start recovery here. If we leave it to md_check_recovery,
3730 * it will remove the drives and not do the right thing
3732 if (mddev
->degraded
&& !mddev
->sync_thread
) {
3733 struct list_head
*rtmp
;
3735 rdev_for_each(rdev
, rtmp
, mddev
)
3736 if (rdev
->raid_disk
>= 0 &&
3737 !test_bit(In_sync
, &rdev
->flags
) &&
3738 !test_bit(Faulty
, &rdev
->flags
))
3739 /* complete an interrupted recovery */
3741 if (spares
&& mddev
->pers
->sync_request
) {
3742 mddev
->recovery
= 0;
3743 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
3744 mddev
->sync_thread
= md_register_thread(md_do_sync
,
3747 if (!mddev
->sync_thread
) {
3748 printk(KERN_ERR
"%s: could not start resync"
3751 /* leave the spares where they are, it shouldn't hurt */
3752 mddev
->recovery
= 0;
3756 md_wakeup_thread(mddev
->thread
);
3757 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
3760 md_new_event(mddev
);
3761 sysfs_notify(&mddev
->kobj
, NULL
, "array_state");
3762 sysfs_notify(&mddev
->kobj
, NULL
, "sync_action");
3763 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
3764 kobject_uevent(&mddev
->gendisk
->dev
.kobj
, KOBJ_CHANGE
);
3768 static int restart_array(mddev_t
*mddev
)
3770 struct gendisk
*disk
= mddev
->gendisk
;
3772 /* Complain if it has no devices */
3773 if (list_empty(&mddev
->disks
))
3779 mddev
->safemode
= 0;
3781 set_disk_ro(disk
, 0);
3782 printk(KERN_INFO
"md: %s switched to read-write mode.\n",
3784 /* Kick recovery or resync if necessary */
3785 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3786 md_wakeup_thread(mddev
->thread
);
3787 md_wakeup_thread(mddev
->sync_thread
);
3788 sysfs_notify(&mddev
->kobj
, NULL
, "array_state");
3792 /* similar to deny_write_access, but accounts for our holding a reference
3793 * to the file ourselves */
3794 static int deny_bitmap_write_access(struct file
* file
)
3796 struct inode
*inode
= file
->f_mapping
->host
;
3798 spin_lock(&inode
->i_lock
);
3799 if (atomic_read(&inode
->i_writecount
) > 1) {
3800 spin_unlock(&inode
->i_lock
);
3803 atomic_set(&inode
->i_writecount
, -1);
3804 spin_unlock(&inode
->i_lock
);
3809 static void restore_bitmap_write_access(struct file
*file
)
3811 struct inode
*inode
= file
->f_mapping
->host
;
3813 spin_lock(&inode
->i_lock
);
3814 atomic_set(&inode
->i_writecount
, 1);
3815 spin_unlock(&inode
->i_lock
);
3819 * 0 - completely stop and dis-assemble array
3820 * 1 - switch to readonly
3821 * 2 - stop but do not disassemble array
3823 static int do_md_stop(mddev_t
* mddev
, int mode
, int is_open
)
3826 struct gendisk
*disk
= mddev
->gendisk
;
3828 if (atomic_read(&mddev
->openers
) > is_open
) {
3829 printk("md: %s still in use.\n",mdname(mddev
));
3835 if (mddev
->sync_thread
) {
3836 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
3837 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
3838 md_unregister_thread(mddev
->sync_thread
);
3839 mddev
->sync_thread
= NULL
;
3842 del_timer_sync(&mddev
->safemode_timer
);
3845 case 1: /* readonly */
3851 case 0: /* disassemble */
3853 bitmap_flush(mddev
);
3854 md_super_wait(mddev
);
3856 set_disk_ro(disk
, 0);
3857 blk_queue_make_request(mddev
->queue
, md_fail_request
);
3858 mddev
->pers
->stop(mddev
);
3859 mddev
->queue
->merge_bvec_fn
= NULL
;
3860 mddev
->queue
->unplug_fn
= NULL
;
3861 mddev
->queue
->backing_dev_info
.congested_fn
= NULL
;
3862 if (mddev
->pers
->sync_request
)
3863 sysfs_remove_group(&mddev
->kobj
, &md_redundancy_group
);
3865 module_put(mddev
->pers
->owner
);
3867 /* tell userspace to handle 'inactive' */
3868 sysfs_notify(&mddev
->kobj
, NULL
, "array_state");
3870 set_capacity(disk
, 0);
3876 if (!mddev
->in_sync
|| mddev
->flags
) {
3877 /* mark array as shutdown cleanly */
3879 md_update_sb(mddev
, 1);
3882 set_disk_ro(disk
, 1);
3883 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
3887 * Free resources if final stop
3891 struct list_head
*tmp
;
3893 printk(KERN_INFO
"md: %s stopped.\n", mdname(mddev
));
3895 bitmap_destroy(mddev
);
3896 if (mddev
->bitmap_file
) {
3897 restore_bitmap_write_access(mddev
->bitmap_file
);
3898 fput(mddev
->bitmap_file
);
3899 mddev
->bitmap_file
= NULL
;
3901 mddev
->bitmap_offset
= 0;
3903 rdev_for_each(rdev
, tmp
, mddev
)
3904 if (rdev
->raid_disk
>= 0) {
3906 sprintf(nm
, "rd%d", rdev
->raid_disk
);
3907 sysfs_remove_link(&mddev
->kobj
, nm
);
3910 /* make sure all md_delayed_delete calls have finished */
3911 flush_scheduled_work();
3913 export_array(mddev
);
3915 mddev
->array_sectors
= 0;
3917 mddev
->raid_disks
= 0;
3918 mddev
->recovery_cp
= 0;
3919 mddev
->resync_min
= 0;
3920 mddev
->resync_max
= MaxSector
;
3921 mddev
->reshape_position
= MaxSector
;
3922 mddev
->external
= 0;
3923 mddev
->persistent
= 0;
3924 mddev
->level
= LEVEL_NONE
;
3925 mddev
->clevel
[0] = 0;
3928 mddev
->metadata_type
[0] = 0;
3929 mddev
->chunk_size
= 0;
3930 mddev
->ctime
= mddev
->utime
= 0;
3932 mddev
->max_disks
= 0;
3934 mddev
->delta_disks
= 0;
3935 mddev
->new_level
= LEVEL_NONE
;
3936 mddev
->new_layout
= 0;
3937 mddev
->new_chunk
= 0;
3938 mddev
->curr_resync
= 0;
3939 mddev
->resync_mismatches
= 0;
3940 mddev
->suspend_lo
= mddev
->suspend_hi
= 0;
3941 mddev
->sync_speed_min
= mddev
->sync_speed_max
= 0;
3942 mddev
->recovery
= 0;
3945 mddev
->degraded
= 0;
3946 mddev
->barriers_work
= 0;
3947 mddev
->safemode
= 0;
3949 } else if (mddev
->pers
)
3950 printk(KERN_INFO
"md: %s switched to read-only mode.\n",
3953 md_new_event(mddev
);
3954 sysfs_notify(&mddev
->kobj
, NULL
, "array_state");
3960 static void autorun_array(mddev_t
*mddev
)
3963 struct list_head
*tmp
;
3966 if (list_empty(&mddev
->disks
))
3969 printk(KERN_INFO
"md: running: ");
3971 rdev_for_each(rdev
, tmp
, mddev
) {
3972 char b
[BDEVNAME_SIZE
];
3973 printk("<%s>", bdevname(rdev
->bdev
,b
));
3977 err
= do_md_run (mddev
);
3979 printk(KERN_WARNING
"md: do_md_run() returned %d\n", err
);
3980 do_md_stop (mddev
, 0, 0);
3985 * lets try to run arrays based on all disks that have arrived
3986 * until now. (those are in pending_raid_disks)
3988 * the method: pick the first pending disk, collect all disks with
3989 * the same UUID, remove all from the pending list and put them into
3990 * the 'same_array' list. Then order this list based on superblock
3991 * update time (freshest comes first), kick out 'old' disks and
3992 * compare superblocks. If everything's fine then run it.
3994 * If "unit" is allocated, then bump its reference count
3996 static void autorun_devices(int part
)
3998 struct list_head
*tmp
;
3999 mdk_rdev_t
*rdev0
, *rdev
;
4001 char b
[BDEVNAME_SIZE
];
4003 printk(KERN_INFO
"md: autorun ...\n");
4004 while (!list_empty(&pending_raid_disks
)) {
4007 LIST_HEAD(candidates
);
4008 rdev0
= list_entry(pending_raid_disks
.next
,
4009 mdk_rdev_t
, same_set
);
4011 printk(KERN_INFO
"md: considering %s ...\n",
4012 bdevname(rdev0
->bdev
,b
));
4013 INIT_LIST_HEAD(&candidates
);
4014 rdev_for_each_list(rdev
, tmp
, pending_raid_disks
)
4015 if (super_90_load(rdev
, rdev0
, 0) >= 0) {
4016 printk(KERN_INFO
"md: adding %s ...\n",
4017 bdevname(rdev
->bdev
,b
));
4018 list_move(&rdev
->same_set
, &candidates
);
4021 * now we have a set of devices, with all of them having
4022 * mostly sane superblocks. It's time to allocate the
4026 dev
= MKDEV(mdp_major
,
4027 rdev0
->preferred_minor
<< MdpMinorShift
);
4028 unit
= MINOR(dev
) >> MdpMinorShift
;
4030 dev
= MKDEV(MD_MAJOR
, rdev0
->preferred_minor
);
4033 if (rdev0
->preferred_minor
!= unit
) {
4034 printk(KERN_INFO
"md: unit number in %s is bad: %d\n",
4035 bdevname(rdev0
->bdev
, b
), rdev0
->preferred_minor
);
4039 md_probe(dev
, NULL
, NULL
);
4040 mddev
= mddev_find(dev
);
4041 if (!mddev
|| !mddev
->gendisk
) {
4045 "md: cannot allocate memory for md drive.\n");
4048 if (mddev_lock(mddev
))
4049 printk(KERN_WARNING
"md: %s locked, cannot run\n",
4051 else if (mddev
->raid_disks
|| mddev
->major_version
4052 || !list_empty(&mddev
->disks
)) {
4054 "md: %s already running, cannot run %s\n",
4055 mdname(mddev
), bdevname(rdev0
->bdev
,b
));
4056 mddev_unlock(mddev
);
4058 printk(KERN_INFO
"md: created %s\n", mdname(mddev
));
4059 mddev
->persistent
= 1;
4060 rdev_for_each_list(rdev
, tmp
, candidates
) {
4061 list_del_init(&rdev
->same_set
);
4062 if (bind_rdev_to_array(rdev
, mddev
))
4065 autorun_array(mddev
);
4066 mddev_unlock(mddev
);
4068 /* on success, candidates will be empty, on error
4071 rdev_for_each_list(rdev
, tmp
, candidates
) {
4072 list_del_init(&rdev
->same_set
);
4077 printk(KERN_INFO
"md: ... autorun DONE.\n");
4079 #endif /* !MODULE */
4081 static int get_version(void __user
* arg
)
4085 ver
.major
= MD_MAJOR_VERSION
;
4086 ver
.minor
= MD_MINOR_VERSION
;
4087 ver
.patchlevel
= MD_PATCHLEVEL_VERSION
;
4089 if (copy_to_user(arg
, &ver
, sizeof(ver
)))
4095 static int get_array_info(mddev_t
* mddev
, void __user
* arg
)
4097 mdu_array_info_t info
;
4098 int nr
,working
,active
,failed
,spare
;
4100 struct list_head
*tmp
;
4102 nr
=working
=active
=failed
=spare
=0;
4103 rdev_for_each(rdev
, tmp
, mddev
) {
4105 if (test_bit(Faulty
, &rdev
->flags
))
4109 if (test_bit(In_sync
, &rdev
->flags
))
4116 info
.major_version
= mddev
->major_version
;
4117 info
.minor_version
= mddev
->minor_version
;
4118 info
.patch_version
= MD_PATCHLEVEL_VERSION
;
4119 info
.ctime
= mddev
->ctime
;
4120 info
.level
= mddev
->level
;
4121 info
.size
= mddev
->size
;
4122 if (info
.size
!= mddev
->size
) /* overflow */
4125 info
.raid_disks
= mddev
->raid_disks
;
4126 info
.md_minor
= mddev
->md_minor
;
4127 info
.not_persistent
= !mddev
->persistent
;
4129 info
.utime
= mddev
->utime
;
4132 info
.state
= (1<<MD_SB_CLEAN
);
4133 if (mddev
->bitmap
&& mddev
->bitmap_offset
)
4134 info
.state
= (1<<MD_SB_BITMAP_PRESENT
);
4135 info
.active_disks
= active
;
4136 info
.working_disks
= working
;
4137 info
.failed_disks
= failed
;
4138 info
.spare_disks
= spare
;
4140 info
.layout
= mddev
->layout
;
4141 info
.chunk_size
= mddev
->chunk_size
;
4143 if (copy_to_user(arg
, &info
, sizeof(info
)))
4149 static int get_bitmap_file(mddev_t
* mddev
, void __user
* arg
)
4151 mdu_bitmap_file_t
*file
= NULL
; /* too big for stack allocation */
4152 char *ptr
, *buf
= NULL
;
4155 if (md_allow_write(mddev
))
4156 file
= kmalloc(sizeof(*file
), GFP_NOIO
);
4158 file
= kmalloc(sizeof(*file
), GFP_KERNEL
);
4163 /* bitmap disabled, zero the first byte and copy out */
4164 if (!mddev
->bitmap
|| !mddev
->bitmap
->file
) {
4165 file
->pathname
[0] = '\0';
4169 buf
= kmalloc(sizeof(file
->pathname
), GFP_KERNEL
);
4173 ptr
= d_path(&mddev
->bitmap
->file
->f_path
, buf
, sizeof(file
->pathname
));
4177 strcpy(file
->pathname
, ptr
);
4181 if (copy_to_user(arg
, file
, sizeof(*file
)))
4189 static int get_disk_info(mddev_t
* mddev
, void __user
* arg
)
4191 mdu_disk_info_t info
;
4194 if (copy_from_user(&info
, arg
, sizeof(info
)))
4197 rdev
= find_rdev_nr(mddev
, info
.number
);
4199 info
.major
= MAJOR(rdev
->bdev
->bd_dev
);
4200 info
.minor
= MINOR(rdev
->bdev
->bd_dev
);
4201 info
.raid_disk
= rdev
->raid_disk
;
4203 if (test_bit(Faulty
, &rdev
->flags
))
4204 info
.state
|= (1<<MD_DISK_FAULTY
);
4205 else if (test_bit(In_sync
, &rdev
->flags
)) {
4206 info
.state
|= (1<<MD_DISK_ACTIVE
);
4207 info
.state
|= (1<<MD_DISK_SYNC
);
4209 if (test_bit(WriteMostly
, &rdev
->flags
))
4210 info
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
4212 info
.major
= info
.minor
= 0;
4213 info
.raid_disk
= -1;
4214 info
.state
= (1<<MD_DISK_REMOVED
);
4217 if (copy_to_user(arg
, &info
, sizeof(info
)))
4223 static int add_new_disk(mddev_t
* mddev
, mdu_disk_info_t
*info
)
4225 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
4227 dev_t dev
= MKDEV(info
->major
,info
->minor
);
4229 if (info
->major
!= MAJOR(dev
) || info
->minor
!= MINOR(dev
))
4232 if (!mddev
->raid_disks
) {
4234 /* expecting a device which has a superblock */
4235 rdev
= md_import_device(dev
, mddev
->major_version
, mddev
->minor_version
);
4238 "md: md_import_device returned %ld\n",
4240 return PTR_ERR(rdev
);
4242 if (!list_empty(&mddev
->disks
)) {
4243 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
4244 mdk_rdev_t
, same_set
);
4245 int err
= super_types
[mddev
->major_version
]
4246 .load_super(rdev
, rdev0
, mddev
->minor_version
);
4249 "md: %s has different UUID to %s\n",
4250 bdevname(rdev
->bdev
,b
),
4251 bdevname(rdev0
->bdev
,b2
));
4256 err
= bind_rdev_to_array(rdev
, mddev
);
4263 * add_new_disk can be used once the array is assembled
4264 * to add "hot spares". They must already have a superblock
4269 if (!mddev
->pers
->hot_add_disk
) {
4271 "%s: personality does not support diskops!\n",
4275 if (mddev
->persistent
)
4276 rdev
= md_import_device(dev
, mddev
->major_version
,
4277 mddev
->minor_version
);
4279 rdev
= md_import_device(dev
, -1, -1);
4282 "md: md_import_device returned %ld\n",
4284 return PTR_ERR(rdev
);
4286 /* set save_raid_disk if appropriate */
4287 if (!mddev
->persistent
) {
4288 if (info
->state
& (1<<MD_DISK_SYNC
) &&
4289 info
->raid_disk
< mddev
->raid_disks
)
4290 rdev
->raid_disk
= info
->raid_disk
;
4292 rdev
->raid_disk
= -1;
4294 super_types
[mddev
->major_version
].
4295 validate_super(mddev
, rdev
);
4296 rdev
->saved_raid_disk
= rdev
->raid_disk
;
4298 clear_bit(In_sync
, &rdev
->flags
); /* just to be sure */
4299 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
4300 set_bit(WriteMostly
, &rdev
->flags
);
4302 rdev
->raid_disk
= -1;
4303 err
= bind_rdev_to_array(rdev
, mddev
);
4304 if (!err
&& !mddev
->pers
->hot_remove_disk
) {
4305 /* If there is hot_add_disk but no hot_remove_disk
4306 * then added disks for geometry changes,
4307 * and should be added immediately.
4309 super_types
[mddev
->major_version
].
4310 validate_super(mddev
, rdev
);
4311 err
= mddev
->pers
->hot_add_disk(mddev
, rdev
);
4313 unbind_rdev_from_array(rdev
);
4318 sysfs_notify(&rdev
->kobj
, NULL
, "state");
4320 md_update_sb(mddev
, 1);
4321 if (mddev
->degraded
)
4322 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
4323 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4324 md_wakeup_thread(mddev
->thread
);
4328 /* otherwise, add_new_disk is only allowed
4329 * for major_version==0 superblocks
4331 if (mddev
->major_version
!= 0) {
4332 printk(KERN_WARNING
"%s: ADD_NEW_DISK not supported\n",
4337 if (!(info
->state
& (1<<MD_DISK_FAULTY
))) {
4339 rdev
= md_import_device (dev
, -1, 0);
4342 "md: error, md_import_device() returned %ld\n",
4344 return PTR_ERR(rdev
);
4346 rdev
->desc_nr
= info
->number
;
4347 if (info
->raid_disk
< mddev
->raid_disks
)
4348 rdev
->raid_disk
= info
->raid_disk
;
4350 rdev
->raid_disk
= -1;
4352 if (rdev
->raid_disk
< mddev
->raid_disks
)
4353 if (info
->state
& (1<<MD_DISK_SYNC
))
4354 set_bit(In_sync
, &rdev
->flags
);
4356 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
4357 set_bit(WriteMostly
, &rdev
->flags
);
4359 if (!mddev
->persistent
) {
4360 printk(KERN_INFO
"md: nonpersistent superblock ...\n");
4361 rdev
->sb_start
= rdev
->bdev
->bd_inode
->i_size
/ 512;
4363 rdev
->sb_start
= calc_dev_sboffset(rdev
->bdev
);
4364 rdev
->size
= calc_num_sectors(rdev
, mddev
->chunk_size
) / 2;
4366 err
= bind_rdev_to_array(rdev
, mddev
);
4376 static int hot_remove_disk(mddev_t
* mddev
, dev_t dev
)
4378 char b
[BDEVNAME_SIZE
];
4381 rdev
= find_rdev(mddev
, dev
);
4385 if (rdev
->raid_disk
>= 0)
4388 kick_rdev_from_array(rdev
);
4389 md_update_sb(mddev
, 1);
4390 md_new_event(mddev
);
4394 printk(KERN_WARNING
"md: cannot remove active disk %s from %s ...\n",
4395 bdevname(rdev
->bdev
,b
), mdname(mddev
));
4399 static int hot_add_disk(mddev_t
* mddev
, dev_t dev
)
4401 char b
[BDEVNAME_SIZE
];
4408 if (mddev
->major_version
!= 0) {
4409 printk(KERN_WARNING
"%s: HOT_ADD may only be used with"
4410 " version-0 superblocks.\n",
4414 if (!mddev
->pers
->hot_add_disk
) {
4416 "%s: personality does not support diskops!\n",
4421 rdev
= md_import_device (dev
, -1, 0);
4424 "md: error, md_import_device() returned %ld\n",
4429 if (mddev
->persistent
)
4430 rdev
->sb_start
= calc_dev_sboffset(rdev
->bdev
);
4432 rdev
->sb_start
= rdev
->bdev
->bd_inode
->i_size
/ 512;
4434 rdev
->size
= calc_num_sectors(rdev
, mddev
->chunk_size
) / 2;
4436 if (test_bit(Faulty
, &rdev
->flags
)) {
4438 "md: can not hot-add faulty %s disk to %s!\n",
4439 bdevname(rdev
->bdev
,b
), mdname(mddev
));
4443 clear_bit(In_sync
, &rdev
->flags
);
4445 rdev
->saved_raid_disk
= -1;
4446 err
= bind_rdev_to_array(rdev
, mddev
);
4451 * The rest should better be atomic, we can have disk failures
4452 * noticed in interrupt contexts ...
4455 if (rdev
->desc_nr
== mddev
->max_disks
) {
4456 printk(KERN_WARNING
"%s: can not hot-add to full array!\n",
4459 goto abort_unbind_export
;
4462 rdev
->raid_disk
= -1;
4464 md_update_sb(mddev
, 1);
4467 * Kick recovery, maybe this spare has to be added to the
4468 * array immediately.
4470 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4471 md_wakeup_thread(mddev
->thread
);
4472 md_new_event(mddev
);
4475 abort_unbind_export
:
4476 unbind_rdev_from_array(rdev
);
4483 static int set_bitmap_file(mddev_t
*mddev
, int fd
)
4488 if (!mddev
->pers
->quiesce
)
4490 if (mddev
->recovery
|| mddev
->sync_thread
)
4492 /* we should be able to change the bitmap.. */
4498 return -EEXIST
; /* cannot add when bitmap is present */
4499 mddev
->bitmap_file
= fget(fd
);
4501 if (mddev
->bitmap_file
== NULL
) {
4502 printk(KERN_ERR
"%s: error: failed to get bitmap file\n",
4507 err
= deny_bitmap_write_access(mddev
->bitmap_file
);
4509 printk(KERN_ERR
"%s: error: bitmap file is already in use\n",
4511 fput(mddev
->bitmap_file
);
4512 mddev
->bitmap_file
= NULL
;
4515 mddev
->bitmap_offset
= 0; /* file overrides offset */
4516 } else if (mddev
->bitmap
== NULL
)
4517 return -ENOENT
; /* cannot remove what isn't there */
4520 mddev
->pers
->quiesce(mddev
, 1);
4522 err
= bitmap_create(mddev
);
4523 if (fd
< 0 || err
) {
4524 bitmap_destroy(mddev
);
4525 fd
= -1; /* make sure to put the file */
4527 mddev
->pers
->quiesce(mddev
, 0);
4530 if (mddev
->bitmap_file
) {
4531 restore_bitmap_write_access(mddev
->bitmap_file
);
4532 fput(mddev
->bitmap_file
);
4534 mddev
->bitmap_file
= NULL
;
4541 * set_array_info is used two different ways
4542 * The original usage is when creating a new array.
4543 * In this usage, raid_disks is > 0 and it together with
4544 * level, size, not_persistent,layout,chunksize determine the
4545 * shape of the array.
4546 * This will always create an array with a type-0.90.0 superblock.
4547 * The newer usage is when assembling an array.
4548 * In this case raid_disks will be 0, and the major_version field is
4549 * use to determine which style super-blocks are to be found on the devices.
4550 * The minor and patch _version numbers are also kept incase the
4551 * super_block handler wishes to interpret them.
4553 static int set_array_info(mddev_t
* mddev
, mdu_array_info_t
*info
)
4556 if (info
->raid_disks
== 0) {
4557 /* just setting version number for superblock loading */
4558 if (info
->major_version
< 0 ||
4559 info
->major_version
>= ARRAY_SIZE(super_types
) ||
4560 super_types
[info
->major_version
].name
== NULL
) {
4561 /* maybe try to auto-load a module? */
4563 "md: superblock version %d not known\n",
4564 info
->major_version
);
4567 mddev
->major_version
= info
->major_version
;
4568 mddev
->minor_version
= info
->minor_version
;
4569 mddev
->patch_version
= info
->patch_version
;
4570 mddev
->persistent
= !info
->not_persistent
;
4573 mddev
->major_version
= MD_MAJOR_VERSION
;
4574 mddev
->minor_version
= MD_MINOR_VERSION
;
4575 mddev
->patch_version
= MD_PATCHLEVEL_VERSION
;
4576 mddev
->ctime
= get_seconds();
4578 mddev
->level
= info
->level
;
4579 mddev
->clevel
[0] = 0;
4580 mddev
->size
= info
->size
;
4581 mddev
->raid_disks
= info
->raid_disks
;
4582 /* don't set md_minor, it is determined by which /dev/md* was
4585 if (info
->state
& (1<<MD_SB_CLEAN
))
4586 mddev
->recovery_cp
= MaxSector
;
4588 mddev
->recovery_cp
= 0;
4589 mddev
->persistent
= ! info
->not_persistent
;
4590 mddev
->external
= 0;
4592 mddev
->layout
= info
->layout
;
4593 mddev
->chunk_size
= info
->chunk_size
;
4595 mddev
->max_disks
= MD_SB_DISKS
;
4597 if (mddev
->persistent
)
4599 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
4601 mddev
->default_bitmap_offset
= MD_SB_BYTES
>> 9;
4602 mddev
->bitmap_offset
= 0;
4604 mddev
->reshape_position
= MaxSector
;
4607 * Generate a 128 bit UUID
4609 get_random_bytes(mddev
->uuid
, 16);
4611 mddev
->new_level
= mddev
->level
;
4612 mddev
->new_chunk
= mddev
->chunk_size
;
4613 mddev
->new_layout
= mddev
->layout
;
4614 mddev
->delta_disks
= 0;
4619 static int update_size(mddev_t
*mddev
, sector_t num_sectors
)
4623 struct list_head
*tmp
;
4624 int fit
= (num_sectors
== 0);
4626 if (mddev
->pers
->resize
== NULL
)
4628 /* The "num_sectors" is the number of sectors of each device that
4629 * is used. This can only make sense for arrays with redundancy.
4630 * linear and raid0 always use whatever space is available. We can only
4631 * consider changing this number if no resync or reconstruction is
4632 * happening, and if the new size is acceptable. It must fit before the
4633 * sb_start or, if that is <data_offset, it must fit before the size
4634 * of each device. If num_sectors is zero, we find the largest size
4638 if (mddev
->sync_thread
)
4641 /* Sorry, cannot grow a bitmap yet, just remove it,
4645 rdev_for_each(rdev
, tmp
, mddev
) {
4647 avail
= rdev
->size
* 2;
4649 if (fit
&& (num_sectors
== 0 || num_sectors
> avail
))
4650 num_sectors
= avail
;
4651 if (avail
< num_sectors
)
4654 rv
= mddev
->pers
->resize(mddev
, num_sectors
);
4656 struct block_device
*bdev
;
4658 bdev
= bdget_disk(mddev
->gendisk
, 0);
4660 mutex_lock(&bdev
->bd_inode
->i_mutex
);
4661 i_size_write(bdev
->bd_inode
,
4662 (loff_t
)mddev
->array_sectors
<< 9);
4663 mutex_unlock(&bdev
->bd_inode
->i_mutex
);
4670 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
)
4673 /* change the number of raid disks */
4674 if (mddev
->pers
->check_reshape
== NULL
)
4676 if (raid_disks
<= 0 ||
4677 raid_disks
>= mddev
->max_disks
)
4679 if (mddev
->sync_thread
|| mddev
->reshape_position
!= MaxSector
)
4681 mddev
->delta_disks
= raid_disks
- mddev
->raid_disks
;
4683 rv
= mddev
->pers
->check_reshape(mddev
);
4689 * update_array_info is used to change the configuration of an
4691 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
4692 * fields in the info are checked against the array.
4693 * Any differences that cannot be handled will cause an error.
4694 * Normally, only one change can be managed at a time.
4696 static int update_array_info(mddev_t
*mddev
, mdu_array_info_t
*info
)
4702 /* calculate expected state,ignoring low bits */
4703 if (mddev
->bitmap
&& mddev
->bitmap_offset
)
4704 state
|= (1 << MD_SB_BITMAP_PRESENT
);
4706 if (mddev
->major_version
!= info
->major_version
||
4707 mddev
->minor_version
!= info
->minor_version
||
4708 /* mddev->patch_version != info->patch_version || */
4709 mddev
->ctime
!= info
->ctime
||
4710 mddev
->level
!= info
->level
||
4711 /* mddev->layout != info->layout || */
4712 !mddev
->persistent
!= info
->not_persistent
||
4713 mddev
->chunk_size
!= info
->chunk_size
||
4714 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
4715 ((state
^info
->state
) & 0xfffffe00)
4718 /* Check there is only one change */
4719 if (info
->size
>= 0 && mddev
->size
!= info
->size
) cnt
++;
4720 if (mddev
->raid_disks
!= info
->raid_disks
) cnt
++;
4721 if (mddev
->layout
!= info
->layout
) cnt
++;
4722 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) cnt
++;
4723 if (cnt
== 0) return 0;
4724 if (cnt
> 1) return -EINVAL
;
4726 if (mddev
->layout
!= info
->layout
) {
4728 * we don't need to do anything at the md level, the
4729 * personality will take care of it all.
4731 if (mddev
->pers
->reconfig
== NULL
)
4734 return mddev
->pers
->reconfig(mddev
, info
->layout
, -1);
4736 if (info
->size
>= 0 && mddev
->size
!= info
->size
)
4737 rv
= update_size(mddev
, (sector_t
)info
->size
* 2);
4739 if (mddev
->raid_disks
!= info
->raid_disks
)
4740 rv
= update_raid_disks(mddev
, info
->raid_disks
);
4742 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) {
4743 if (mddev
->pers
->quiesce
== NULL
)
4745 if (mddev
->recovery
|| mddev
->sync_thread
)
4747 if (info
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
4748 /* add the bitmap */
4751 if (mddev
->default_bitmap_offset
== 0)
4753 mddev
->bitmap_offset
= mddev
->default_bitmap_offset
;
4754 mddev
->pers
->quiesce(mddev
, 1);
4755 rv
= bitmap_create(mddev
);
4757 bitmap_destroy(mddev
);
4758 mddev
->pers
->quiesce(mddev
, 0);
4760 /* remove the bitmap */
4763 if (mddev
->bitmap
->file
)
4765 mddev
->pers
->quiesce(mddev
, 1);
4766 bitmap_destroy(mddev
);
4767 mddev
->pers
->quiesce(mddev
, 0);
4768 mddev
->bitmap_offset
= 0;
4771 md_update_sb(mddev
, 1);
4775 static int set_disk_faulty(mddev_t
*mddev
, dev_t dev
)
4779 if (mddev
->pers
== NULL
)
4782 rdev
= find_rdev(mddev
, dev
);
4786 md_error(mddev
, rdev
);
4791 * We have a problem here : there is no easy way to give a CHS
4792 * virtual geometry. We currently pretend that we have a 2 heads
4793 * 4 sectors (with a BIG number of cylinders...). This drives
4794 * dosfs just mad... ;-)
4796 static int md_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
4798 mddev_t
*mddev
= bdev
->bd_disk
->private_data
;
4802 geo
->cylinders
= get_capacity(mddev
->gendisk
) / 8;
4806 static int md_ioctl(struct inode
*inode
, struct file
*file
,
4807 unsigned int cmd
, unsigned long arg
)
4810 void __user
*argp
= (void __user
*)arg
;
4811 mddev_t
*mddev
= NULL
;
4813 if (!capable(CAP_SYS_ADMIN
))
4817 * Commands dealing with the RAID driver but not any
4823 err
= get_version(argp
);
4826 case PRINT_RAID_DEBUG
:
4834 autostart_arrays(arg
);
4841 * Commands creating/starting a new array:
4844 mddev
= inode
->i_bdev
->bd_disk
->private_data
;
4851 err
= mddev_lock(mddev
);
4854 "md: ioctl lock interrupted, reason %d, cmd %d\n",
4861 case SET_ARRAY_INFO
:
4863 mdu_array_info_t info
;
4865 memset(&info
, 0, sizeof(info
));
4866 else if (copy_from_user(&info
, argp
, sizeof(info
))) {
4871 err
= update_array_info(mddev
, &info
);
4873 printk(KERN_WARNING
"md: couldn't update"
4874 " array info. %d\n", err
);
4879 if (!list_empty(&mddev
->disks
)) {
4881 "md: array %s already has disks!\n",
4886 if (mddev
->raid_disks
) {
4888 "md: array %s already initialised!\n",
4893 err
= set_array_info(mddev
, &info
);
4895 printk(KERN_WARNING
"md: couldn't set"
4896 " array info. %d\n", err
);
4906 * Commands querying/configuring an existing array:
4908 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
4909 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
4910 if ((!mddev
->raid_disks
&& !mddev
->external
)
4911 && cmd
!= ADD_NEW_DISK
&& cmd
!= STOP_ARRAY
4912 && cmd
!= RUN_ARRAY
&& cmd
!= SET_BITMAP_FILE
4913 && cmd
!= GET_BITMAP_FILE
) {
4919 * Commands even a read-only array can execute:
4923 case GET_ARRAY_INFO
:
4924 err
= get_array_info(mddev
, argp
);
4927 case GET_BITMAP_FILE
:
4928 err
= get_bitmap_file(mddev
, argp
);
4932 err
= get_disk_info(mddev
, argp
);
4935 case RESTART_ARRAY_RW
:
4936 err
= restart_array(mddev
);
4940 err
= do_md_stop (mddev
, 0, 1);
4944 err
= do_md_stop (mddev
, 1, 1);
4950 * The remaining ioctls are changing the state of the
4951 * superblock, so we do not allow them on read-only arrays.
4952 * However non-MD ioctls (e.g. get-size) will still come through
4953 * here and hit the 'default' below, so only disallow
4954 * 'md' ioctls, and switch to rw mode if started auto-readonly.
4956 if (_IOC_TYPE(cmd
) == MD_MAJOR
&& mddev
->ro
&& mddev
->pers
) {
4957 if (mddev
->ro
== 2) {
4959 sysfs_notify(&mddev
->kobj
, NULL
, "array_state");
4960 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4961 md_wakeup_thread(mddev
->thread
);
4972 mdu_disk_info_t info
;
4973 if (copy_from_user(&info
, argp
, sizeof(info
)))
4976 err
= add_new_disk(mddev
, &info
);
4980 case HOT_REMOVE_DISK
:
4981 err
= hot_remove_disk(mddev
, new_decode_dev(arg
));
4985 err
= hot_add_disk(mddev
, new_decode_dev(arg
));
4988 case SET_DISK_FAULTY
:
4989 err
= set_disk_faulty(mddev
, new_decode_dev(arg
));
4993 err
= do_md_run (mddev
);
4996 case SET_BITMAP_FILE
:
4997 err
= set_bitmap_file(mddev
, (int)arg
);
5007 mddev_unlock(mddev
);
5017 static int md_open(struct inode
*inode
, struct file
*file
)
5020 * Succeed if we can lock the mddev, which confirms that
5021 * it isn't being stopped right now.
5023 mddev_t
*mddev
= inode
->i_bdev
->bd_disk
->private_data
;
5026 if ((err
= mutex_lock_interruptible_nested(&mddev
->reconfig_mutex
, 1)))
5031 atomic_inc(&mddev
->openers
);
5032 mddev_unlock(mddev
);
5034 check_disk_change(inode
->i_bdev
);
5039 static int md_release(struct inode
*inode
, struct file
* file
)
5041 mddev_t
*mddev
= inode
->i_bdev
->bd_disk
->private_data
;
5044 atomic_dec(&mddev
->openers
);
5050 static int md_media_changed(struct gendisk
*disk
)
5052 mddev_t
*mddev
= disk
->private_data
;
5054 return mddev
->changed
;
5057 static int md_revalidate(struct gendisk
*disk
)
5059 mddev_t
*mddev
= disk
->private_data
;
5064 static struct block_device_operations md_fops
=
5066 .owner
= THIS_MODULE
,
5068 .release
= md_release
,
5070 .getgeo
= md_getgeo
,
5071 .media_changed
= md_media_changed
,
5072 .revalidate_disk
= md_revalidate
,
5075 static int md_thread(void * arg
)
5077 mdk_thread_t
*thread
= arg
;
5080 * md_thread is a 'system-thread', it's priority should be very
5081 * high. We avoid resource deadlocks individually in each
5082 * raid personality. (RAID5 does preallocation) We also use RR and
5083 * the very same RT priority as kswapd, thus we will never get
5084 * into a priority inversion deadlock.
5086 * we definitely have to have equal or higher priority than
5087 * bdflush, otherwise bdflush will deadlock if there are too
5088 * many dirty RAID5 blocks.
5091 allow_signal(SIGKILL
);
5092 while (!kthread_should_stop()) {
5094 /* We need to wait INTERRUPTIBLE so that
5095 * we don't add to the load-average.
5096 * That means we need to be sure no signals are
5099 if (signal_pending(current
))
5100 flush_signals(current
);
5102 wait_event_interruptible_timeout
5104 test_bit(THREAD_WAKEUP
, &thread
->flags
)
5105 || kthread_should_stop(),
5108 clear_bit(THREAD_WAKEUP
, &thread
->flags
);
5110 thread
->run(thread
->mddev
);
5116 void md_wakeup_thread(mdk_thread_t
*thread
)
5119 dprintk("md: waking up MD thread %s.\n", thread
->tsk
->comm
);
5120 set_bit(THREAD_WAKEUP
, &thread
->flags
);
5121 wake_up(&thread
->wqueue
);
5125 mdk_thread_t
*md_register_thread(void (*run
) (mddev_t
*), mddev_t
*mddev
,
5128 mdk_thread_t
*thread
;
5130 thread
= kzalloc(sizeof(mdk_thread_t
), GFP_KERNEL
);
5134 init_waitqueue_head(&thread
->wqueue
);
5137 thread
->mddev
= mddev
;
5138 thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
5139 thread
->tsk
= kthread_run(md_thread
, thread
, name
, mdname(thread
->mddev
));
5140 if (IS_ERR(thread
->tsk
)) {
5147 void md_unregister_thread(mdk_thread_t
*thread
)
5149 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread
->tsk
));
5151 kthread_stop(thread
->tsk
);
5155 void md_error(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
5162 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
5165 if (mddev
->external
)
5166 set_bit(Blocked
, &rdev
->flags
);
5168 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5170 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5171 __builtin_return_address(0),__builtin_return_address(1),
5172 __builtin_return_address(2),__builtin_return_address(3));
5176 if (!mddev
->pers
->error_handler
)
5178 mddev
->pers
->error_handler(mddev
,rdev
);
5179 if (mddev
->degraded
)
5180 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
5181 set_bit(StateChanged
, &rdev
->flags
);
5182 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5183 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5184 md_wakeup_thread(mddev
->thread
);
5185 md_new_event_inintr(mddev
);
5188 /* seq_file implementation /proc/mdstat */
5190 static void status_unused(struct seq_file
*seq
)
5194 struct list_head
*tmp
;
5196 seq_printf(seq
, "unused devices: ");
5198 rdev_for_each_list(rdev
, tmp
, pending_raid_disks
) {
5199 char b
[BDEVNAME_SIZE
];
5201 seq_printf(seq
, "%s ",
5202 bdevname(rdev
->bdev
,b
));
5205 seq_printf(seq
, "<none>");
5207 seq_printf(seq
, "\n");
5211 static void status_resync(struct seq_file
*seq
, mddev_t
* mddev
)
5213 sector_t max_blocks
, resync
, res
;
5214 unsigned long dt
, db
, rt
;
5216 unsigned int per_milli
;
5218 resync
= (mddev
->curr_resync
- atomic_read(&mddev
->recovery_active
))/2;
5220 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
5221 max_blocks
= mddev
->resync_max_sectors
>> 1;
5223 max_blocks
= mddev
->size
;
5226 * Should not happen.
5232 /* Pick 'scale' such that (resync>>scale)*1000 will fit
5233 * in a sector_t, and (max_blocks>>scale) will fit in a
5234 * u32, as those are the requirements for sector_div.
5235 * Thus 'scale' must be at least 10
5238 if (sizeof(sector_t
) > sizeof(unsigned long)) {
5239 while ( max_blocks
/2 > (1ULL<<(scale
+32)))
5242 res
= (resync
>>scale
)*1000;
5243 sector_div(res
, (u32
)((max_blocks
>>scale
)+1));
5247 int i
, x
= per_milli
/50, y
= 20-x
;
5248 seq_printf(seq
, "[");
5249 for (i
= 0; i
< x
; i
++)
5250 seq_printf(seq
, "=");
5251 seq_printf(seq
, ">");
5252 for (i
= 0; i
< y
; i
++)
5253 seq_printf(seq
, ".");
5254 seq_printf(seq
, "] ");
5256 seq_printf(seq
, " %s =%3u.%u%% (%llu/%llu)",
5257 (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)?
5259 (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)?
5261 (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ?
5262 "resync" : "recovery"))),
5263 per_milli
/10, per_milli
% 10,
5264 (unsigned long long) resync
,
5265 (unsigned long long) max_blocks
);
5268 * We do not want to overflow, so the order of operands and
5269 * the * 100 / 100 trick are important. We do a +1 to be
5270 * safe against division by zero. We only estimate anyway.
5272 * dt: time from mark until now
5273 * db: blocks written from mark until now
5274 * rt: remaining time
5276 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
5278 db
= (mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
))
5279 - mddev
->resync_mark_cnt
;
5280 rt
= (dt
* ((unsigned long)(max_blocks
-resync
) / (db
/2/100+1)))/100;
5282 seq_printf(seq
, " finish=%lu.%lumin", rt
/ 60, (rt
% 60)/6);
5284 seq_printf(seq
, " speed=%ldK/sec", db
/2/dt
);
5287 static void *md_seq_start(struct seq_file
*seq
, loff_t
*pos
)
5289 struct list_head
*tmp
;
5299 spin_lock(&all_mddevs_lock
);
5300 list_for_each(tmp
,&all_mddevs
)
5302 mddev
= list_entry(tmp
, mddev_t
, all_mddevs
);
5304 spin_unlock(&all_mddevs_lock
);
5307 spin_unlock(&all_mddevs_lock
);
5309 return (void*)2;/* tail */
5313 static void *md_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
5315 struct list_head
*tmp
;
5316 mddev_t
*next_mddev
, *mddev
= v
;
5322 spin_lock(&all_mddevs_lock
);
5324 tmp
= all_mddevs
.next
;
5326 tmp
= mddev
->all_mddevs
.next
;
5327 if (tmp
!= &all_mddevs
)
5328 next_mddev
= mddev_get(list_entry(tmp
,mddev_t
,all_mddevs
));
5330 next_mddev
= (void*)2;
5333 spin_unlock(&all_mddevs_lock
);
5341 static void md_seq_stop(struct seq_file
*seq
, void *v
)
5345 if (mddev
&& v
!= (void*)1 && v
!= (void*)2)
5349 struct mdstat_info
{
5353 static int md_seq_show(struct seq_file
*seq
, void *v
)
5357 struct list_head
*tmp2
;
5359 struct mdstat_info
*mi
= seq
->private;
5360 struct bitmap
*bitmap
;
5362 if (v
== (void*)1) {
5363 struct mdk_personality
*pers
;
5364 seq_printf(seq
, "Personalities : ");
5365 spin_lock(&pers_lock
);
5366 list_for_each_entry(pers
, &pers_list
, list
)
5367 seq_printf(seq
, "[%s] ", pers
->name
);
5369 spin_unlock(&pers_lock
);
5370 seq_printf(seq
, "\n");
5371 mi
->event
= atomic_read(&md_event_count
);
5374 if (v
== (void*)2) {
5379 if (mddev_lock(mddev
) < 0)
5382 if (mddev
->pers
|| mddev
->raid_disks
|| !list_empty(&mddev
->disks
)) {
5383 seq_printf(seq
, "%s : %sactive", mdname(mddev
),
5384 mddev
->pers
? "" : "in");
5387 seq_printf(seq
, " (read-only)");
5389 seq_printf(seq
, " (auto-read-only)");
5390 seq_printf(seq
, " %s", mddev
->pers
->name
);
5394 rdev_for_each(rdev
, tmp2
, mddev
) {
5395 char b
[BDEVNAME_SIZE
];
5396 seq_printf(seq
, " %s[%d]",
5397 bdevname(rdev
->bdev
,b
), rdev
->desc_nr
);
5398 if (test_bit(WriteMostly
, &rdev
->flags
))
5399 seq_printf(seq
, "(W)");
5400 if (test_bit(Faulty
, &rdev
->flags
)) {
5401 seq_printf(seq
, "(F)");
5403 } else if (rdev
->raid_disk
< 0)
5404 seq_printf(seq
, "(S)"); /* spare */
5408 if (!list_empty(&mddev
->disks
)) {
5410 seq_printf(seq
, "\n %llu blocks",
5411 (unsigned long long)
5412 mddev
->array_sectors
/ 2);
5414 seq_printf(seq
, "\n %llu blocks",
5415 (unsigned long long)size
);
5417 if (mddev
->persistent
) {
5418 if (mddev
->major_version
!= 0 ||
5419 mddev
->minor_version
!= 90) {
5420 seq_printf(seq
," super %d.%d",
5421 mddev
->major_version
,
5422 mddev
->minor_version
);
5424 } else if (mddev
->external
)
5425 seq_printf(seq
, " super external:%s",
5426 mddev
->metadata_type
);
5428 seq_printf(seq
, " super non-persistent");
5431 mddev
->pers
->status (seq
, mddev
);
5432 seq_printf(seq
, "\n ");
5433 if (mddev
->pers
->sync_request
) {
5434 if (mddev
->curr_resync
> 2) {
5435 status_resync (seq
, mddev
);
5436 seq_printf(seq
, "\n ");
5437 } else if (mddev
->curr_resync
== 1 || mddev
->curr_resync
== 2)
5438 seq_printf(seq
, "\tresync=DELAYED\n ");
5439 else if (mddev
->recovery_cp
< MaxSector
)
5440 seq_printf(seq
, "\tresync=PENDING\n ");
5443 seq_printf(seq
, "\n ");
5445 if ((bitmap
= mddev
->bitmap
)) {
5446 unsigned long chunk_kb
;
5447 unsigned long flags
;
5448 spin_lock_irqsave(&bitmap
->lock
, flags
);
5449 chunk_kb
= bitmap
->chunksize
>> 10;
5450 seq_printf(seq
, "bitmap: %lu/%lu pages [%luKB], "
5452 bitmap
->pages
- bitmap
->missing_pages
,
5454 (bitmap
->pages
- bitmap
->missing_pages
)
5455 << (PAGE_SHIFT
- 10),
5456 chunk_kb
? chunk_kb
: bitmap
->chunksize
,
5457 chunk_kb
? "KB" : "B");
5459 seq_printf(seq
, ", file: ");
5460 seq_path(seq
, &bitmap
->file
->f_path
, " \t\n");
5463 seq_printf(seq
, "\n");
5464 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
5467 seq_printf(seq
, "\n");
5469 mddev_unlock(mddev
);
5474 static struct seq_operations md_seq_ops
= {
5475 .start
= md_seq_start
,
5476 .next
= md_seq_next
,
5477 .stop
= md_seq_stop
,
5478 .show
= md_seq_show
,
5481 static int md_seq_open(struct inode
*inode
, struct file
*file
)
5484 struct mdstat_info
*mi
= kmalloc(sizeof(*mi
), GFP_KERNEL
);
5488 error
= seq_open(file
, &md_seq_ops
);
5492 struct seq_file
*p
= file
->private_data
;
5494 mi
->event
= atomic_read(&md_event_count
);
5499 static unsigned int mdstat_poll(struct file
*filp
, poll_table
*wait
)
5501 struct seq_file
*m
= filp
->private_data
;
5502 struct mdstat_info
*mi
= m
->private;
5505 poll_wait(filp
, &md_event_waiters
, wait
);
5507 /* always allow read */
5508 mask
= POLLIN
| POLLRDNORM
;
5510 if (mi
->event
!= atomic_read(&md_event_count
))
5511 mask
|= POLLERR
| POLLPRI
;
5515 static const struct file_operations md_seq_fops
= {
5516 .owner
= THIS_MODULE
,
5517 .open
= md_seq_open
,
5519 .llseek
= seq_lseek
,
5520 .release
= seq_release_private
,
5521 .poll
= mdstat_poll
,
5524 int register_md_personality(struct mdk_personality
*p
)
5526 spin_lock(&pers_lock
);
5527 list_add_tail(&p
->list
, &pers_list
);
5528 printk(KERN_INFO
"md: %s personality registered for level %d\n", p
->name
, p
->level
);
5529 spin_unlock(&pers_lock
);
5533 int unregister_md_personality(struct mdk_personality
*p
)
5535 printk(KERN_INFO
"md: %s personality unregistered\n", p
->name
);
5536 spin_lock(&pers_lock
);
5537 list_del_init(&p
->list
);
5538 spin_unlock(&pers_lock
);
5542 static int is_mddev_idle(mddev_t
*mddev
)
5550 rdev_for_each_rcu(rdev
, mddev
) {
5551 struct gendisk
*disk
= rdev
->bdev
->bd_contains
->bd_disk
;
5552 curr_events
= disk_stat_read(disk
, sectors
[0]) +
5553 disk_stat_read(disk
, sectors
[1]) -
5554 atomic_read(&disk
->sync_io
);
5555 /* sync IO will cause sync_io to increase before the disk_stats
5556 * as sync_io is counted when a request starts, and
5557 * disk_stats is counted when it completes.
5558 * So resync activity will cause curr_events to be smaller than
5559 * when there was no such activity.
5560 * non-sync IO will cause disk_stat to increase without
5561 * increasing sync_io so curr_events will (eventually)
5562 * be larger than it was before. Once it becomes
5563 * substantially larger, the test below will cause
5564 * the array to appear non-idle, and resync will slow
5566 * If there is a lot of outstanding resync activity when
5567 * we set last_event to curr_events, then all that activity
5568 * completing might cause the array to appear non-idle
5569 * and resync will be slowed down even though there might
5570 * not have been non-resync activity. This will only
5571 * happen once though. 'last_events' will soon reflect
5572 * the state where there is little or no outstanding
5573 * resync requests, and further resync activity will
5574 * always make curr_events less than last_events.
5577 if (curr_events
- rdev
->last_events
> 4096) {
5578 rdev
->last_events
= curr_events
;
5586 void md_done_sync(mddev_t
*mddev
, int blocks
, int ok
)
5588 /* another "blocks" (512byte) blocks have been synced */
5589 atomic_sub(blocks
, &mddev
->recovery_active
);
5590 wake_up(&mddev
->recovery_wait
);
5592 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5593 md_wakeup_thread(mddev
->thread
);
5594 // stop recovery, signal do_sync ....
5599 /* md_write_start(mddev, bi)
5600 * If we need to update some array metadata (e.g. 'active' flag
5601 * in superblock) before writing, schedule a superblock update
5602 * and wait for it to complete.
5604 void md_write_start(mddev_t
*mddev
, struct bio
*bi
)
5607 if (bio_data_dir(bi
) != WRITE
)
5610 BUG_ON(mddev
->ro
== 1);
5611 if (mddev
->ro
== 2) {
5612 /* need to switch to read/write */
5614 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5615 md_wakeup_thread(mddev
->thread
);
5616 md_wakeup_thread(mddev
->sync_thread
);
5619 atomic_inc(&mddev
->writes_pending
);
5620 if (mddev
->safemode
== 1)
5621 mddev
->safemode
= 0;
5622 if (mddev
->in_sync
) {
5623 spin_lock_irq(&mddev
->write_lock
);
5624 if (mddev
->in_sync
) {
5626 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
5627 md_wakeup_thread(mddev
->thread
);
5630 spin_unlock_irq(&mddev
->write_lock
);
5633 sysfs_notify(&mddev
->kobj
, NULL
, "array_state");
5634 wait_event(mddev
->sb_wait
,
5635 !test_bit(MD_CHANGE_CLEAN
, &mddev
->flags
) &&
5636 !test_bit(MD_CHANGE_PENDING
, &mddev
->flags
));
5639 void md_write_end(mddev_t
*mddev
)
5641 if (atomic_dec_and_test(&mddev
->writes_pending
)) {
5642 if (mddev
->safemode
== 2)
5643 md_wakeup_thread(mddev
->thread
);
5644 else if (mddev
->safemode_delay
)
5645 mod_timer(&mddev
->safemode_timer
, jiffies
+ mddev
->safemode_delay
);
5649 /* md_allow_write(mddev)
5650 * Calling this ensures that the array is marked 'active' so that writes
5651 * may proceed without blocking. It is important to call this before
5652 * attempting a GFP_KERNEL allocation while holding the mddev lock.
5653 * Must be called with mddev_lock held.
5655 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
5656 * is dropped, so return -EAGAIN after notifying userspace.
5658 int md_allow_write(mddev_t
*mddev
)
5664 if (!mddev
->pers
->sync_request
)
5667 spin_lock_irq(&mddev
->write_lock
);
5668 if (mddev
->in_sync
) {
5670 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
5671 if (mddev
->safemode_delay
&&
5672 mddev
->safemode
== 0)
5673 mddev
->safemode
= 1;
5674 spin_unlock_irq(&mddev
->write_lock
);
5675 md_update_sb(mddev
, 0);
5676 sysfs_notify(&mddev
->kobj
, NULL
, "array_state");
5678 spin_unlock_irq(&mddev
->write_lock
);
5680 if (test_bit(MD_CHANGE_CLEAN
, &mddev
->flags
))
5685 EXPORT_SYMBOL_GPL(md_allow_write
);
5687 #define SYNC_MARKS 10
5688 #define SYNC_MARK_STEP (3*HZ)
5689 void md_do_sync(mddev_t
*mddev
)
5692 unsigned int currspeed
= 0,
5694 sector_t max_sectors
,j
, io_sectors
;
5695 unsigned long mark
[SYNC_MARKS
];
5696 sector_t mark_cnt
[SYNC_MARKS
];
5698 struct list_head
*tmp
;
5699 sector_t last_check
;
5701 struct list_head
*rtmp
;
5705 /* just incase thread restarts... */
5706 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
5708 if (mddev
->ro
) /* never try to sync a read-only array */
5711 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
5712 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
5713 desc
= "data-check";
5714 else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
5715 desc
= "requested-resync";
5718 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
5723 /* we overload curr_resync somewhat here.
5724 * 0 == not engaged in resync at all
5725 * 2 == checking that there is no conflict with another sync
5726 * 1 == like 2, but have yielded to allow conflicting resync to
5728 * other == active in resync - this many blocks
5730 * Before starting a resync we must have set curr_resync to
5731 * 2, and then checked that every "conflicting" array has curr_resync
5732 * less than ours. When we find one that is the same or higher
5733 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
5734 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
5735 * This will mean we have to start checking from the beginning again.
5740 mddev
->curr_resync
= 2;
5743 if (kthread_should_stop()) {
5744 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5747 for_each_mddev(mddev2
, tmp
) {
5748 if (mddev2
== mddev
)
5750 if (!mddev
->parallel_resync
5751 && mddev2
->curr_resync
5752 && match_mddev_units(mddev
, mddev2
)) {
5754 if (mddev
< mddev2
&& mddev
->curr_resync
== 2) {
5755 /* arbitrarily yield */
5756 mddev
->curr_resync
= 1;
5757 wake_up(&resync_wait
);
5759 if (mddev
> mddev2
&& mddev
->curr_resync
== 1)
5760 /* no need to wait here, we can wait the next
5761 * time 'round when curr_resync == 2
5764 prepare_to_wait(&resync_wait
, &wq
, TASK_UNINTERRUPTIBLE
);
5765 if (!kthread_should_stop() &&
5766 mddev2
->curr_resync
>= mddev
->curr_resync
) {
5767 printk(KERN_INFO
"md: delaying %s of %s"
5768 " until %s has finished (they"
5769 " share one or more physical units)\n",
5770 desc
, mdname(mddev
), mdname(mddev2
));
5773 finish_wait(&resync_wait
, &wq
);
5776 finish_wait(&resync_wait
, &wq
);
5779 } while (mddev
->curr_resync
< 2);
5782 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
5783 /* resync follows the size requested by the personality,
5784 * which defaults to physical size, but can be virtual size
5786 max_sectors
= mddev
->resync_max_sectors
;
5787 mddev
->resync_mismatches
= 0;
5788 /* we don't use the checkpoint if there's a bitmap */
5789 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
5790 j
= mddev
->resync_min
;
5791 else if (!mddev
->bitmap
)
5792 j
= mddev
->recovery_cp
;
5794 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
5795 max_sectors
= mddev
->size
<< 1;
5797 /* recovery follows the physical size of devices */
5798 max_sectors
= mddev
->size
<< 1;
5800 rdev_for_each(rdev
, rtmp
, mddev
)
5801 if (rdev
->raid_disk
>= 0 &&
5802 !test_bit(Faulty
, &rdev
->flags
) &&
5803 !test_bit(In_sync
, &rdev
->flags
) &&
5804 rdev
->recovery_offset
< j
)
5805 j
= rdev
->recovery_offset
;
5808 printk(KERN_INFO
"md: %s of RAID array %s\n", desc
, mdname(mddev
));
5809 printk(KERN_INFO
"md: minimum _guaranteed_ speed:"
5810 " %d KB/sec/disk.\n", speed_min(mddev
));
5811 printk(KERN_INFO
"md: using maximum available idle IO bandwidth "
5812 "(but not more than %d KB/sec) for %s.\n",
5813 speed_max(mddev
), desc
);
5815 is_mddev_idle(mddev
); /* this also initializes IO event counters */
5818 for (m
= 0; m
< SYNC_MARKS
; m
++) {
5820 mark_cnt
[m
] = io_sectors
;
5823 mddev
->resync_mark
= mark
[last_mark
];
5824 mddev
->resync_mark_cnt
= mark_cnt
[last_mark
];
5827 * Tune reconstruction:
5829 window
= 32*(PAGE_SIZE
/512);
5830 printk(KERN_INFO
"md: using %dk window, over a total of %llu blocks.\n",
5831 window
/2,(unsigned long long) max_sectors
/2);
5833 atomic_set(&mddev
->recovery_active
, 0);
5838 "md: resuming %s of %s from checkpoint.\n",
5839 desc
, mdname(mddev
));
5840 mddev
->curr_resync
= j
;
5843 while (j
< max_sectors
) {
5847 if (j
>= mddev
->resync_max
) {
5848 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
5849 wait_event(mddev
->recovery_wait
,
5850 mddev
->resync_max
> j
5851 || kthread_should_stop());
5853 if (kthread_should_stop())
5855 sectors
= mddev
->pers
->sync_request(mddev
, j
, &skipped
,
5856 currspeed
< speed_min(mddev
));
5858 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5862 if (!skipped
) { /* actual IO requested */
5863 io_sectors
+= sectors
;
5864 atomic_add(sectors
, &mddev
->recovery_active
);
5868 if (j
>1) mddev
->curr_resync
= j
;
5869 mddev
->curr_mark_cnt
= io_sectors
;
5870 if (last_check
== 0)
5871 /* this is the earliers that rebuilt will be
5872 * visible in /proc/mdstat
5874 md_new_event(mddev
);
5876 if (last_check
+ window
> io_sectors
|| j
== max_sectors
)
5879 last_check
= io_sectors
;
5881 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
5885 if (time_after_eq(jiffies
, mark
[last_mark
] + SYNC_MARK_STEP
)) {
5887 int next
= (last_mark
+1) % SYNC_MARKS
;
5889 mddev
->resync_mark
= mark
[next
];
5890 mddev
->resync_mark_cnt
= mark_cnt
[next
];
5891 mark
[next
] = jiffies
;
5892 mark_cnt
[next
] = io_sectors
- atomic_read(&mddev
->recovery_active
);
5897 if (kthread_should_stop())
5902 * this loop exits only if either when we are slower than
5903 * the 'hard' speed limit, or the system was IO-idle for
5905 * the system might be non-idle CPU-wise, but we only care
5906 * about not overloading the IO subsystem. (things like an
5907 * e2fsck being done on the RAID array should execute fast)
5909 blk_unplug(mddev
->queue
);
5912 currspeed
= ((unsigned long)(io_sectors
-mddev
->resync_mark_cnt
))/2
5913 /((jiffies
-mddev
->resync_mark
)/HZ
+1) +1;
5915 if (currspeed
> speed_min(mddev
)) {
5916 if ((currspeed
> speed_max(mddev
)) ||
5917 !is_mddev_idle(mddev
)) {
5923 printk(KERN_INFO
"md: %s: %s done.\n",mdname(mddev
), desc
);
5925 * this also signals 'finished resyncing' to md_stop
5928 blk_unplug(mddev
->queue
);
5930 wait_event(mddev
->recovery_wait
, !atomic_read(&mddev
->recovery_active
));
5932 /* tell personality that we are finished */
5933 mddev
->pers
->sync_request(mddev
, max_sectors
, &skipped
, 1);
5935 if (!test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
) &&
5936 mddev
->curr_resync
> 2) {
5937 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
5938 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
5939 if (mddev
->curr_resync
>= mddev
->recovery_cp
) {
5941 "md: checkpointing %s of %s.\n",
5942 desc
, mdname(mddev
));
5943 mddev
->recovery_cp
= mddev
->curr_resync
;
5946 mddev
->recovery_cp
= MaxSector
;
5948 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
5949 mddev
->curr_resync
= MaxSector
;
5950 rdev_for_each(rdev
, rtmp
, mddev
)
5951 if (rdev
->raid_disk
>= 0 &&
5952 !test_bit(Faulty
, &rdev
->flags
) &&
5953 !test_bit(In_sync
, &rdev
->flags
) &&
5954 rdev
->recovery_offset
< mddev
->curr_resync
)
5955 rdev
->recovery_offset
= mddev
->curr_resync
;
5958 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
5961 mddev
->curr_resync
= 0;
5962 mddev
->resync_min
= 0;
5963 mddev
->resync_max
= MaxSector
;
5964 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
5965 wake_up(&resync_wait
);
5966 set_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
5967 md_wakeup_thread(mddev
->thread
);
5972 * got a signal, exit.
5975 "md: md_do_sync() got signal ... exiting\n");
5976 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5980 EXPORT_SYMBOL_GPL(md_do_sync
);
5983 static int remove_and_add_spares(mddev_t
*mddev
)
5986 struct list_head
*rtmp
;
5989 rdev_for_each(rdev
, rtmp
, mddev
)
5990 if (rdev
->raid_disk
>= 0 &&
5991 !test_bit(Blocked
, &rdev
->flags
) &&
5992 (test_bit(Faulty
, &rdev
->flags
) ||
5993 ! test_bit(In_sync
, &rdev
->flags
)) &&
5994 atomic_read(&rdev
->nr_pending
)==0) {
5995 if (mddev
->pers
->hot_remove_disk(
5996 mddev
, rdev
->raid_disk
)==0) {
5998 sprintf(nm
,"rd%d", rdev
->raid_disk
);
5999 sysfs_remove_link(&mddev
->kobj
, nm
);
6000 rdev
->raid_disk
= -1;
6004 if (mddev
->degraded
&& ! mddev
->ro
) {
6005 rdev_for_each(rdev
, rtmp
, mddev
) {
6006 if (rdev
->raid_disk
>= 0 &&
6007 !test_bit(In_sync
, &rdev
->flags
) &&
6008 !test_bit(Blocked
, &rdev
->flags
))
6010 if (rdev
->raid_disk
< 0
6011 && !test_bit(Faulty
, &rdev
->flags
)) {
6012 rdev
->recovery_offset
= 0;
6014 hot_add_disk(mddev
, rdev
) == 0) {
6016 sprintf(nm
, "rd%d", rdev
->raid_disk
);
6017 if (sysfs_create_link(&mddev
->kobj
,
6020 "md: cannot register "
6024 md_new_event(mddev
);
6033 * This routine is regularly called by all per-raid-array threads to
6034 * deal with generic issues like resync and super-block update.
6035 * Raid personalities that don't have a thread (linear/raid0) do not
6036 * need this as they never do any recovery or update the superblock.
6038 * It does not do any resync itself, but rather "forks" off other threads
6039 * to do that as needed.
6040 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6041 * "->recovery" and create a thread at ->sync_thread.
6042 * When the thread finishes it sets MD_RECOVERY_DONE
6043 * and wakeups up this thread which will reap the thread and finish up.
6044 * This thread also removes any faulty devices (with nr_pending == 0).
6046 * The overall approach is:
6047 * 1/ if the superblock needs updating, update it.
6048 * 2/ If a recovery thread is running, don't do anything else.
6049 * 3/ If recovery has finished, clean up, possibly marking spares active.
6050 * 4/ If there are any faulty devices, remove them.
6051 * 5/ If array is degraded, try to add spares devices
6052 * 6/ If array has spares or is not in-sync, start a resync thread.
6054 void md_check_recovery(mddev_t
*mddev
)
6057 struct list_head
*rtmp
;
6061 bitmap_daemon_work(mddev
->bitmap
);
6063 if (test_and_clear_bit(MD_NOTIFY_ARRAY_STATE
, &mddev
->flags
))
6064 sysfs_notify(&mddev
->kobj
, NULL
, "array_state");
6069 if (signal_pending(current
)) {
6070 if (mddev
->pers
->sync_request
&& !mddev
->external
) {
6071 printk(KERN_INFO
"md: %s in immediate safe mode\n",
6073 mddev
->safemode
= 2;
6075 flush_signals(current
);
6078 if (mddev
->ro
&& !test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
6081 (mddev
->flags
&& !mddev
->external
) ||
6082 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
6083 test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
6084 (mddev
->external
== 0 && mddev
->safemode
== 1) ||
6085 (mddev
->safemode
== 2 && ! atomic_read(&mddev
->writes_pending
)
6086 && !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
6090 if (mddev_trylock(mddev
)) {
6094 /* Only thing we do on a ro array is remove
6097 remove_and_add_spares(mddev
);
6098 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6102 if (!mddev
->external
) {
6104 spin_lock_irq(&mddev
->write_lock
);
6105 if (mddev
->safemode
&&
6106 !atomic_read(&mddev
->writes_pending
) &&
6108 mddev
->recovery_cp
== MaxSector
) {
6111 if (mddev
->persistent
)
6112 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6114 if (mddev
->safemode
== 1)
6115 mddev
->safemode
= 0;
6116 spin_unlock_irq(&mddev
->write_lock
);
6118 sysfs_notify(&mddev
->kobj
, NULL
, "array_state");
6122 md_update_sb(mddev
, 0);
6124 rdev_for_each(rdev
, rtmp
, mddev
)
6125 if (test_and_clear_bit(StateChanged
, &rdev
->flags
))
6126 sysfs_notify(&rdev
->kobj
, NULL
, "state");
6129 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
6130 !test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
)) {
6131 /* resync/recovery still happening */
6132 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6135 if (mddev
->sync_thread
) {
6136 /* resync has finished, collect result */
6137 md_unregister_thread(mddev
->sync_thread
);
6138 mddev
->sync_thread
= NULL
;
6139 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
6140 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
6142 /* activate any spares */
6143 if (mddev
->pers
->spare_active(mddev
))
6144 sysfs_notify(&mddev
->kobj
, NULL
,
6147 md_update_sb(mddev
, 1);
6149 /* if array is no-longer degraded, then any saved_raid_disk
6150 * information must be scrapped
6152 if (!mddev
->degraded
)
6153 rdev_for_each(rdev
, rtmp
, mddev
)
6154 rdev
->saved_raid_disk
= -1;
6156 mddev
->recovery
= 0;
6157 /* flag recovery needed just to double check */
6158 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6159 sysfs_notify(&mddev
->kobj
, NULL
, "sync_action");
6160 md_new_event(mddev
);
6163 /* Set RUNNING before clearing NEEDED to avoid
6164 * any transients in the value of "sync_action".
6166 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
6167 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6168 /* Clear some bits that don't mean anything, but
6171 clear_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6172 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
6174 if (test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
6176 /* no recovery is running.
6177 * remove any failed drives, then
6178 * add spares if possible.
6179 * Spare are also removed and re-added, to allow
6180 * the personality to fail the re-add.
6183 if (mddev
->reshape_position
!= MaxSector
) {
6184 if (mddev
->pers
->check_reshape(mddev
) != 0)
6185 /* Cannot proceed */
6187 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
6188 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
6189 } else if ((spares
= remove_and_add_spares(mddev
))) {
6190 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
6191 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
6192 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
6193 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
6194 } else if (mddev
->recovery_cp
< MaxSector
) {
6195 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
6196 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
6197 } else if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
6198 /* nothing to be done ... */
6201 if (mddev
->pers
->sync_request
) {
6202 if (spares
&& mddev
->bitmap
&& ! mddev
->bitmap
->file
) {
6203 /* We are adding a device or devices to an array
6204 * which has the bitmap stored on all devices.
6205 * So make sure all bitmap pages get written
6207 bitmap_write_all(mddev
->bitmap
);
6209 mddev
->sync_thread
= md_register_thread(md_do_sync
,
6212 if (!mddev
->sync_thread
) {
6213 printk(KERN_ERR
"%s: could not start resync"
6216 /* leave the spares where they are, it shouldn't hurt */
6217 mddev
->recovery
= 0;
6219 md_wakeup_thread(mddev
->sync_thread
);
6220 sysfs_notify(&mddev
->kobj
, NULL
, "sync_action");
6221 md_new_event(mddev
);
6224 if (!mddev
->sync_thread
) {
6225 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
6226 if (test_and_clear_bit(MD_RECOVERY_RECOVER
,
6228 sysfs_notify(&mddev
->kobj
, NULL
, "sync_action");
6230 mddev_unlock(mddev
);
6234 void md_wait_for_blocked_rdev(mdk_rdev_t
*rdev
, mddev_t
*mddev
)
6236 sysfs_notify(&rdev
->kobj
, NULL
, "state");
6237 wait_event_timeout(rdev
->blocked_wait
,
6238 !test_bit(Blocked
, &rdev
->flags
),
6239 msecs_to_jiffies(5000));
6240 rdev_dec_pending(rdev
, mddev
);
6242 EXPORT_SYMBOL(md_wait_for_blocked_rdev
);
6244 static int md_notify_reboot(struct notifier_block
*this,
6245 unsigned long code
, void *x
)
6247 struct list_head
*tmp
;
6250 if ((code
== SYS_DOWN
) || (code
== SYS_HALT
) || (code
== SYS_POWER_OFF
)) {
6252 printk(KERN_INFO
"md: stopping all md devices.\n");
6254 for_each_mddev(mddev
, tmp
)
6255 if (mddev_trylock(mddev
)) {
6256 /* Force a switch to readonly even array
6257 * appears to still be in use. Hence
6260 do_md_stop (mddev
, 1, 100);
6261 mddev_unlock(mddev
);
6264 * certain more exotic SCSI devices are known to be
6265 * volatile wrt too early system reboots. While the
6266 * right place to handle this issue is the given
6267 * driver, we do want to have a safe RAID driver ...
6274 static struct notifier_block md_notifier
= {
6275 .notifier_call
= md_notify_reboot
,
6277 .priority
= INT_MAX
, /* before any real devices */
6280 static void md_geninit(void)
6282 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t
));
6284 proc_create("mdstat", S_IRUGO
, NULL
, &md_seq_fops
);
6287 static int __init
md_init(void)
6289 if (register_blkdev(MAJOR_NR
, "md"))
6291 if ((mdp_major
=register_blkdev(0, "mdp"))<=0) {
6292 unregister_blkdev(MAJOR_NR
, "md");
6295 blk_register_region(MKDEV(MAJOR_NR
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
6296 md_probe
, NULL
, NULL
);
6297 blk_register_region(MKDEV(mdp_major
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
6298 md_probe
, NULL
, NULL
);
6300 register_reboot_notifier(&md_notifier
);
6301 raid_table_header
= register_sysctl_table(raid_root_table
);
6311 * Searches all registered partitions for autorun RAID arrays
6315 static LIST_HEAD(all_detected_devices
);
6316 struct detected_devices_node
{
6317 struct list_head list
;
6321 void md_autodetect_dev(dev_t dev
)
6323 struct detected_devices_node
*node_detected_dev
;
6325 node_detected_dev
= kzalloc(sizeof(*node_detected_dev
), GFP_KERNEL
);
6326 if (node_detected_dev
) {
6327 node_detected_dev
->dev
= dev
;
6328 list_add_tail(&node_detected_dev
->list
, &all_detected_devices
);
6330 printk(KERN_CRIT
"md: md_autodetect_dev: kzalloc failed"
6331 ", skipping dev(%d,%d)\n", MAJOR(dev
), MINOR(dev
));
6336 static void autostart_arrays(int part
)
6339 struct detected_devices_node
*node_detected_dev
;
6341 int i_scanned
, i_passed
;
6346 printk(KERN_INFO
"md: Autodetecting RAID arrays.\n");
6348 while (!list_empty(&all_detected_devices
) && i_scanned
< INT_MAX
) {
6350 node_detected_dev
= list_entry(all_detected_devices
.next
,
6351 struct detected_devices_node
, list
);
6352 list_del(&node_detected_dev
->list
);
6353 dev
= node_detected_dev
->dev
;
6354 kfree(node_detected_dev
);
6355 rdev
= md_import_device(dev
,0, 90);
6359 if (test_bit(Faulty
, &rdev
->flags
)) {
6363 set_bit(AutoDetected
, &rdev
->flags
);
6364 list_add(&rdev
->same_set
, &pending_raid_disks
);
6368 printk(KERN_INFO
"md: Scanned %d and added %d devices.\n",
6369 i_scanned
, i_passed
);
6371 autorun_devices(part
);
6374 #endif /* !MODULE */
6376 static __exit
void md_exit(void)
6379 struct list_head
*tmp
;
6381 blk_unregister_region(MKDEV(MAJOR_NR
,0), 1U << MINORBITS
);
6382 blk_unregister_region(MKDEV(mdp_major
,0), 1U << MINORBITS
);
6384 unregister_blkdev(MAJOR_NR
,"md");
6385 unregister_blkdev(mdp_major
, "mdp");
6386 unregister_reboot_notifier(&md_notifier
);
6387 unregister_sysctl_table(raid_table_header
);
6388 remove_proc_entry("mdstat", NULL
);
6389 for_each_mddev(mddev
, tmp
) {
6390 struct gendisk
*disk
= mddev
->gendisk
;
6393 export_array(mddev
);
6396 mddev
->gendisk
= NULL
;
6401 subsys_initcall(md_init
);
6402 module_exit(md_exit
)
6404 static int get_ro(char *buffer
, struct kernel_param
*kp
)
6406 return sprintf(buffer
, "%d", start_readonly
);
6408 static int set_ro(const char *val
, struct kernel_param
*kp
)
6411 int num
= simple_strtoul(val
, &e
, 10);
6412 if (*val
&& (*e
== '\0' || *e
== '\n')) {
6413 start_readonly
= num
;
6419 module_param_call(start_ro
, set_ro
, get_ro
, NULL
, S_IRUSR
|S_IWUSR
);
6420 module_param(start_dirty_degraded
, int, S_IRUGO
|S_IWUSR
);
6423 EXPORT_SYMBOL(register_md_personality
);
6424 EXPORT_SYMBOL(unregister_md_personality
);
6425 EXPORT_SYMBOL(md_error
);
6426 EXPORT_SYMBOL(md_done_sync
);
6427 EXPORT_SYMBOL(md_write_start
);
6428 EXPORT_SYMBOL(md_write_end
);
6429 EXPORT_SYMBOL(md_register_thread
);
6430 EXPORT_SYMBOL(md_unregister_thread
);
6431 EXPORT_SYMBOL(md_wakeup_thread
);
6432 EXPORT_SYMBOL(md_check_recovery
);
6433 MODULE_LICENSE("GPL");
6435 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR
);