2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
5 completely rewritten, based on the MD driver code from Marc Zyngier
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
20 Neil Brown <neilb@cse.unsw.edu.au>.
22 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
25 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 #include <linux/kthread.h>
36 #include <linux/blkdev.h>
37 #include <linux/sysctl.h>
38 #include <linux/seq_file.h>
39 #include <linux/buffer_head.h> /* for invalidate_bdev */
40 #include <linux/poll.h>
41 #include <linux/ctype.h>
42 #include <linux/hdreg.h>
43 #include <linux/proc_fs.h>
44 #include <linux/random.h>
45 #include <linux/reboot.h>
46 #include <linux/file.h>
47 #include <linux/delay.h>
48 #include <linux/raid/md_p.h>
49 #include <linux/raid/md_u.h>
54 #define dprintk(x...) ((void)(DEBUG && printk(x)))
58 static void autostart_arrays(int part
);
61 static LIST_HEAD(pers_list
);
62 static DEFINE_SPINLOCK(pers_lock
);
64 static void md_print_devices(void);
66 static DECLARE_WAIT_QUEUE_HEAD(resync_wait
);
68 #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
71 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
72 * is 1000 KB/sec, so the extra system load does not show up that much.
73 * Increase it if you want to have more _guaranteed_ speed. Note that
74 * the RAID driver will use the maximum available bandwidth if the IO
75 * subsystem is idle. There is also an 'absolute maximum' reconstruction
76 * speed limit - in case reconstruction slows down your system despite
79 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
80 * or /sys/block/mdX/md/sync_speed_{min,max}
83 static int sysctl_speed_limit_min
= 1000;
84 static int sysctl_speed_limit_max
= 200000;
85 static inline int speed_min(mddev_t
*mddev
)
87 return mddev
->sync_speed_min
?
88 mddev
->sync_speed_min
: sysctl_speed_limit_min
;
91 static inline int speed_max(mddev_t
*mddev
)
93 return mddev
->sync_speed_max
?
94 mddev
->sync_speed_max
: sysctl_speed_limit_max
;
97 static struct ctl_table_header
*raid_table_header
;
99 static ctl_table raid_table
[] = {
101 .ctl_name
= DEV_RAID_SPEED_LIMIT_MIN
,
102 .procname
= "speed_limit_min",
103 .data
= &sysctl_speed_limit_min
,
104 .maxlen
= sizeof(int),
105 .mode
= S_IRUGO
|S_IWUSR
,
106 .proc_handler
= &proc_dointvec
,
109 .ctl_name
= DEV_RAID_SPEED_LIMIT_MAX
,
110 .procname
= "speed_limit_max",
111 .data
= &sysctl_speed_limit_max
,
112 .maxlen
= sizeof(int),
113 .mode
= S_IRUGO
|S_IWUSR
,
114 .proc_handler
= &proc_dointvec
,
119 static ctl_table raid_dir_table
[] = {
121 .ctl_name
= DEV_RAID
,
124 .mode
= S_IRUGO
|S_IXUGO
,
130 static ctl_table raid_root_table
[] = {
136 .child
= raid_dir_table
,
141 static const struct block_device_operations md_fops
;
143 static int start_readonly
;
146 * We have a system wide 'event count' that is incremented
147 * on any 'interesting' event, and readers of /proc/mdstat
148 * can use 'poll' or 'select' to find out when the event
152 * start array, stop array, error, add device, remove device,
153 * start build, activate spare
155 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters
);
156 static atomic_t md_event_count
;
157 void md_new_event(mddev_t
*mddev
)
159 atomic_inc(&md_event_count
);
160 wake_up(&md_event_waiters
);
162 EXPORT_SYMBOL_GPL(md_new_event
);
164 /* Alternate version that can be called from interrupts
165 * when calling sysfs_notify isn't needed.
167 static void md_new_event_inintr(mddev_t
*mddev
)
169 atomic_inc(&md_event_count
);
170 wake_up(&md_event_waiters
);
174 * Enables to iterate over all existing md arrays
175 * all_mddevs_lock protects this list.
177 static LIST_HEAD(all_mddevs
);
178 static DEFINE_SPINLOCK(all_mddevs_lock
);
182 * iterates through all used mddevs in the system.
183 * We take care to grab the all_mddevs_lock whenever navigating
184 * the list, and to always hold a refcount when unlocked.
185 * Any code which breaks out of this loop while own
186 * a reference to the current mddev and must mddev_put it.
188 #define for_each_mddev(mddev,tmp) \
190 for (({ spin_lock(&all_mddevs_lock); \
191 tmp = all_mddevs.next; \
193 ({ if (tmp != &all_mddevs) \
194 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
195 spin_unlock(&all_mddevs_lock); \
196 if (mddev) mddev_put(mddev); \
197 mddev = list_entry(tmp, mddev_t, all_mddevs); \
198 tmp != &all_mddevs;}); \
199 ({ spin_lock(&all_mddevs_lock); \
204 /* Rather than calling directly into the personality make_request function,
205 * IO requests come here first so that we can check if the device is
206 * being suspended pending a reconfiguration.
207 * We hold a refcount over the call to ->make_request. By the time that
208 * call has finished, the bio has been linked into some internal structure
209 * and so is visible to ->quiesce(), so we don't need the refcount any more.
211 static int md_make_request(struct request_queue
*q
, struct bio
*bio
)
213 mddev_t
*mddev
= q
->queuedata
;
215 if (mddev
== NULL
|| mddev
->pers
== NULL
) {
220 if (mddev
->suspended
) {
223 prepare_to_wait(&mddev
->sb_wait
, &__wait
,
224 TASK_UNINTERRUPTIBLE
);
225 if (!mddev
->suspended
)
231 finish_wait(&mddev
->sb_wait
, &__wait
);
233 atomic_inc(&mddev
->active_io
);
235 rv
= mddev
->pers
->make_request(q
, bio
);
236 if (atomic_dec_and_test(&mddev
->active_io
) && mddev
->suspended
)
237 wake_up(&mddev
->sb_wait
);
242 static void mddev_suspend(mddev_t
*mddev
)
244 BUG_ON(mddev
->suspended
);
245 mddev
->suspended
= 1;
247 wait_event(mddev
->sb_wait
, atomic_read(&mddev
->active_io
) == 0);
248 mddev
->pers
->quiesce(mddev
, 1);
249 md_unregister_thread(mddev
->thread
);
250 mddev
->thread
= NULL
;
251 /* we now know that no code is executing in the personality module,
252 * except possibly the tail end of a ->bi_end_io function, but that
253 * is certain to complete before the module has a chance to get
258 static void mddev_resume(mddev_t
*mddev
)
260 mddev
->suspended
= 0;
261 wake_up(&mddev
->sb_wait
);
262 mddev
->pers
->quiesce(mddev
, 0);
265 int mddev_congested(mddev_t
*mddev
, int bits
)
267 return mddev
->suspended
;
269 EXPORT_SYMBOL(mddev_congested
);
272 static inline mddev_t
*mddev_get(mddev_t
*mddev
)
274 atomic_inc(&mddev
->active
);
278 static void mddev_delayed_delete(struct work_struct
*ws
);
280 static void mddev_put(mddev_t
*mddev
)
282 if (!atomic_dec_and_lock(&mddev
->active
, &all_mddevs_lock
))
284 if (!mddev
->raid_disks
&& list_empty(&mddev
->disks
) &&
285 mddev
->ctime
== 0 && !mddev
->hold_active
) {
286 /* Array is not configured at all, and not held active,
288 list_del(&mddev
->all_mddevs
);
289 if (mddev
->gendisk
) {
290 /* we did a probe so need to clean up.
291 * Call schedule_work inside the spinlock
292 * so that flush_scheduled_work() after
293 * mddev_find will succeed in waiting for the
296 INIT_WORK(&mddev
->del_work
, mddev_delayed_delete
);
297 schedule_work(&mddev
->del_work
);
301 spin_unlock(&all_mddevs_lock
);
304 static mddev_t
* mddev_find(dev_t unit
)
306 mddev_t
*mddev
, *new = NULL
;
308 if (unit
&& MAJOR(unit
) != MD_MAJOR
)
309 unit
&= ~((1<<MdpMinorShift
)-1);
312 spin_lock(&all_mddevs_lock
);
315 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
316 if (mddev
->unit
== unit
) {
318 spin_unlock(&all_mddevs_lock
);
324 list_add(&new->all_mddevs
, &all_mddevs
);
325 spin_unlock(&all_mddevs_lock
);
326 new->hold_active
= UNTIL_IOCTL
;
330 /* find an unused unit number */
331 static int next_minor
= 512;
332 int start
= next_minor
;
336 dev
= MKDEV(MD_MAJOR
, next_minor
);
338 if (next_minor
> MINORMASK
)
340 if (next_minor
== start
) {
341 /* Oh dear, all in use. */
342 spin_unlock(&all_mddevs_lock
);
348 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
349 if (mddev
->unit
== dev
) {
355 new->md_minor
= MINOR(dev
);
356 new->hold_active
= UNTIL_STOP
;
357 list_add(&new->all_mddevs
, &all_mddevs
);
358 spin_unlock(&all_mddevs_lock
);
361 spin_unlock(&all_mddevs_lock
);
363 new = kzalloc(sizeof(*new), GFP_KERNEL
);
368 if (MAJOR(unit
) == MD_MAJOR
)
369 new->md_minor
= MINOR(unit
);
371 new->md_minor
= MINOR(unit
) >> MdpMinorShift
;
373 mutex_init(&new->open_mutex
);
374 mutex_init(&new->reconfig_mutex
);
375 mutex_init(&new->bitmap_mutex
);
376 INIT_LIST_HEAD(&new->disks
);
377 INIT_LIST_HEAD(&new->all_mddevs
);
378 init_timer(&new->safemode_timer
);
379 atomic_set(&new->active
, 1);
380 atomic_set(&new->openers
, 0);
381 atomic_set(&new->active_io
, 0);
382 spin_lock_init(&new->write_lock
);
383 init_waitqueue_head(&new->sb_wait
);
384 init_waitqueue_head(&new->recovery_wait
);
385 new->reshape_position
= MaxSector
;
387 new->resync_max
= MaxSector
;
388 new->level
= LEVEL_NONE
;
393 static inline int mddev_lock(mddev_t
* mddev
)
395 return mutex_lock_interruptible(&mddev
->reconfig_mutex
);
398 static inline int mddev_is_locked(mddev_t
*mddev
)
400 return mutex_is_locked(&mddev
->reconfig_mutex
);
403 static inline int mddev_trylock(mddev_t
* mddev
)
405 return mutex_trylock(&mddev
->reconfig_mutex
);
408 static inline void mddev_unlock(mddev_t
* mddev
)
410 mutex_unlock(&mddev
->reconfig_mutex
);
412 md_wakeup_thread(mddev
->thread
);
415 static mdk_rdev_t
* find_rdev_nr(mddev_t
*mddev
, int nr
)
419 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
420 if (rdev
->desc_nr
== nr
)
426 static mdk_rdev_t
* find_rdev(mddev_t
* mddev
, dev_t dev
)
430 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
431 if (rdev
->bdev
->bd_dev
== dev
)
437 static struct mdk_personality
*find_pers(int level
, char *clevel
)
439 struct mdk_personality
*pers
;
440 list_for_each_entry(pers
, &pers_list
, list
) {
441 if (level
!= LEVEL_NONE
&& pers
->level
== level
)
443 if (strcmp(pers
->name
, clevel
)==0)
449 /* return the offset of the super block in 512byte sectors */
450 static inline sector_t
calc_dev_sboffset(struct block_device
*bdev
)
452 sector_t num_sectors
= bdev
->bd_inode
->i_size
/ 512;
453 return MD_NEW_SIZE_SECTORS(num_sectors
);
456 static int alloc_disk_sb(mdk_rdev_t
* rdev
)
461 rdev
->sb_page
= alloc_page(GFP_KERNEL
);
462 if (!rdev
->sb_page
) {
463 printk(KERN_ALERT
"md: out of memory.\n");
470 static void free_disk_sb(mdk_rdev_t
* rdev
)
473 put_page(rdev
->sb_page
);
475 rdev
->sb_page
= NULL
;
482 static void super_written(struct bio
*bio
, int error
)
484 mdk_rdev_t
*rdev
= bio
->bi_private
;
485 mddev_t
*mddev
= rdev
->mddev
;
487 if (error
|| !test_bit(BIO_UPTODATE
, &bio
->bi_flags
)) {
488 printk("md: super_written gets error=%d, uptodate=%d\n",
489 error
, test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
490 WARN_ON(test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
491 md_error(mddev
, rdev
);
494 if (atomic_dec_and_test(&mddev
->pending_writes
))
495 wake_up(&mddev
->sb_wait
);
499 static void super_written_barrier(struct bio
*bio
, int error
)
501 struct bio
*bio2
= bio
->bi_private
;
502 mdk_rdev_t
*rdev
= bio2
->bi_private
;
503 mddev_t
*mddev
= rdev
->mddev
;
505 if (!test_bit(BIO_UPTODATE
, &bio
->bi_flags
) &&
506 error
== -EOPNOTSUPP
) {
508 /* barriers don't appear to be supported :-( */
509 set_bit(BarriersNotsupp
, &rdev
->flags
);
510 mddev
->barriers_work
= 0;
511 spin_lock_irqsave(&mddev
->write_lock
, flags
);
512 bio2
->bi_next
= mddev
->biolist
;
513 mddev
->biolist
= bio2
;
514 spin_unlock_irqrestore(&mddev
->write_lock
, flags
);
515 wake_up(&mddev
->sb_wait
);
519 bio
->bi_private
= rdev
;
520 super_written(bio
, error
);
524 void md_super_write(mddev_t
*mddev
, mdk_rdev_t
*rdev
,
525 sector_t sector
, int size
, struct page
*page
)
527 /* write first size bytes of page to sector of rdev
528 * Increment mddev->pending_writes before returning
529 * and decrement it on completion, waking up sb_wait
530 * if zero is reached.
531 * If an error occurred, call md_error
533 * As we might need to resubmit the request if BIO_RW_BARRIER
534 * causes ENOTSUPP, we allocate a spare bio...
536 struct bio
*bio
= bio_alloc(GFP_NOIO
, 1);
537 int rw
= (1<<BIO_RW
) | (1<<BIO_RW_SYNCIO
) | (1<<BIO_RW_UNPLUG
);
539 bio
->bi_bdev
= rdev
->bdev
;
540 bio
->bi_sector
= sector
;
541 bio_add_page(bio
, page
, size
, 0);
542 bio
->bi_private
= rdev
;
543 bio
->bi_end_io
= super_written
;
546 atomic_inc(&mddev
->pending_writes
);
547 if (!test_bit(BarriersNotsupp
, &rdev
->flags
)) {
549 rw
|= (1<<BIO_RW_BARRIER
);
550 rbio
= bio_clone(bio
, GFP_NOIO
);
551 rbio
->bi_private
= bio
;
552 rbio
->bi_end_io
= super_written_barrier
;
553 submit_bio(rw
, rbio
);
558 void md_super_wait(mddev_t
*mddev
)
560 /* wait for all superblock writes that were scheduled to complete.
561 * if any had to be retried (due to BARRIER problems), retry them
565 prepare_to_wait(&mddev
->sb_wait
, &wq
, TASK_UNINTERRUPTIBLE
);
566 if (atomic_read(&mddev
->pending_writes
)==0)
568 while (mddev
->biolist
) {
570 spin_lock_irq(&mddev
->write_lock
);
571 bio
= mddev
->biolist
;
572 mddev
->biolist
= bio
->bi_next
;
574 spin_unlock_irq(&mddev
->write_lock
);
575 submit_bio(bio
->bi_rw
, bio
);
579 finish_wait(&mddev
->sb_wait
, &wq
);
582 static void bi_complete(struct bio
*bio
, int error
)
584 complete((struct completion
*)bio
->bi_private
);
587 int sync_page_io(struct block_device
*bdev
, sector_t sector
, int size
,
588 struct page
*page
, int rw
)
590 struct bio
*bio
= bio_alloc(GFP_NOIO
, 1);
591 struct completion event
;
594 rw
|= (1 << BIO_RW_SYNCIO
) | (1 << BIO_RW_UNPLUG
);
597 bio
->bi_sector
= sector
;
598 bio_add_page(bio
, page
, size
, 0);
599 init_completion(&event
);
600 bio
->bi_private
= &event
;
601 bio
->bi_end_io
= bi_complete
;
603 wait_for_completion(&event
);
605 ret
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
609 EXPORT_SYMBOL_GPL(sync_page_io
);
611 static int read_disk_sb(mdk_rdev_t
* rdev
, int size
)
613 char b
[BDEVNAME_SIZE
];
614 if (!rdev
->sb_page
) {
622 if (!sync_page_io(rdev
->bdev
, rdev
->sb_start
, size
, rdev
->sb_page
, READ
))
628 printk(KERN_WARNING
"md: disabled device %s, could not read superblock.\n",
629 bdevname(rdev
->bdev
,b
));
633 static int uuid_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
635 return sb1
->set_uuid0
== sb2
->set_uuid0
&&
636 sb1
->set_uuid1
== sb2
->set_uuid1
&&
637 sb1
->set_uuid2
== sb2
->set_uuid2
&&
638 sb1
->set_uuid3
== sb2
->set_uuid3
;
641 static int sb_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
644 mdp_super_t
*tmp1
, *tmp2
;
646 tmp1
= kmalloc(sizeof(*tmp1
),GFP_KERNEL
);
647 tmp2
= kmalloc(sizeof(*tmp2
),GFP_KERNEL
);
649 if (!tmp1
|| !tmp2
) {
651 printk(KERN_INFO
"md.c sb_equal(): failed to allocate memory!\n");
659 * nr_disks is not constant
664 ret
= (memcmp(tmp1
, tmp2
, MD_SB_GENERIC_CONSTANT_WORDS
* 4) == 0);
672 static u32
md_csum_fold(u32 csum
)
674 csum
= (csum
& 0xffff) + (csum
>> 16);
675 return (csum
& 0xffff) + (csum
>> 16);
678 static unsigned int calc_sb_csum(mdp_super_t
* sb
)
681 u32
*sb32
= (u32
*)sb
;
683 unsigned int disk_csum
, csum
;
685 disk_csum
= sb
->sb_csum
;
688 for (i
= 0; i
< MD_SB_BYTES
/4 ; i
++)
690 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
694 /* This used to use csum_partial, which was wrong for several
695 * reasons including that different results are returned on
696 * different architectures. It isn't critical that we get exactly
697 * the same return value as before (we always csum_fold before
698 * testing, and that removes any differences). However as we
699 * know that csum_partial always returned a 16bit value on
700 * alphas, do a fold to maximise conformity to previous behaviour.
702 sb
->sb_csum
= md_csum_fold(disk_csum
);
704 sb
->sb_csum
= disk_csum
;
711 * Handle superblock details.
712 * We want to be able to handle multiple superblock formats
713 * so we have a common interface to them all, and an array of
714 * different handlers.
715 * We rely on user-space to write the initial superblock, and support
716 * reading and updating of superblocks.
717 * Interface methods are:
718 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
719 * loads and validates a superblock on dev.
720 * if refdev != NULL, compare superblocks on both devices
722 * 0 - dev has a superblock that is compatible with refdev
723 * 1 - dev has a superblock that is compatible and newer than refdev
724 * so dev should be used as the refdev in future
725 * -EINVAL superblock incompatible or invalid
726 * -othererror e.g. -EIO
728 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
729 * Verify that dev is acceptable into mddev.
730 * The first time, mddev->raid_disks will be 0, and data from
731 * dev should be merged in. Subsequent calls check that dev
732 * is new enough. Return 0 or -EINVAL
734 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
735 * Update the superblock for rdev with data in mddev
736 * This does not write to disc.
742 struct module
*owner
;
743 int (*load_super
)(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
,
745 int (*validate_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
746 void (*sync_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
747 unsigned long long (*rdev_size_change
)(mdk_rdev_t
*rdev
,
748 sector_t num_sectors
);
752 * Check that the given mddev has no bitmap.
754 * This function is called from the run method of all personalities that do not
755 * support bitmaps. It prints an error message and returns non-zero if mddev
756 * has a bitmap. Otherwise, it returns 0.
759 int md_check_no_bitmap(mddev_t
*mddev
)
761 if (!mddev
->bitmap_file
&& !mddev
->bitmap_offset
)
763 printk(KERN_ERR
"%s: bitmaps are not supported for %s\n",
764 mdname(mddev
), mddev
->pers
->name
);
767 EXPORT_SYMBOL(md_check_no_bitmap
);
770 * load_super for 0.90.0
772 static int super_90_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
774 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
779 * Calculate the position of the superblock (512byte sectors),
780 * it's at the end of the disk.
782 * It also happens to be a multiple of 4Kb.
784 rdev
->sb_start
= calc_dev_sboffset(rdev
->bdev
);
786 ret
= read_disk_sb(rdev
, MD_SB_BYTES
);
791 bdevname(rdev
->bdev
, b
);
792 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
794 if (sb
->md_magic
!= MD_SB_MAGIC
) {
795 printk(KERN_ERR
"md: invalid raid superblock magic on %s\n",
800 if (sb
->major_version
!= 0 ||
801 sb
->minor_version
< 90 ||
802 sb
->minor_version
> 91) {
803 printk(KERN_WARNING
"Bad version number %d.%d on %s\n",
804 sb
->major_version
, sb
->minor_version
,
809 if (sb
->raid_disks
<= 0)
812 if (md_csum_fold(calc_sb_csum(sb
)) != md_csum_fold(sb
->sb_csum
)) {
813 printk(KERN_WARNING
"md: invalid superblock checksum on %s\n",
818 rdev
->preferred_minor
= sb
->md_minor
;
819 rdev
->data_offset
= 0;
820 rdev
->sb_size
= MD_SB_BYTES
;
822 if (sb
->level
== LEVEL_MULTIPATH
)
825 rdev
->desc_nr
= sb
->this_disk
.number
;
831 mdp_super_t
*refsb
= (mdp_super_t
*)page_address(refdev
->sb_page
);
832 if (!uuid_equal(refsb
, sb
)) {
833 printk(KERN_WARNING
"md: %s has different UUID to %s\n",
834 b
, bdevname(refdev
->bdev
,b2
));
837 if (!sb_equal(refsb
, sb
)) {
838 printk(KERN_WARNING
"md: %s has same UUID"
839 " but different superblock to %s\n",
840 b
, bdevname(refdev
->bdev
, b2
));
844 ev2
= md_event(refsb
);
850 rdev
->sectors
= rdev
->sb_start
;
852 if (rdev
->sectors
< sb
->size
* 2 && sb
->level
> 1)
853 /* "this cannot possibly happen" ... */
861 * validate_super for 0.90.0
863 static int super_90_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
866 mdp_super_t
*sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
867 __u64 ev1
= md_event(sb
);
869 rdev
->raid_disk
= -1;
870 clear_bit(Faulty
, &rdev
->flags
);
871 clear_bit(In_sync
, &rdev
->flags
);
872 clear_bit(WriteMostly
, &rdev
->flags
);
873 clear_bit(BarriersNotsupp
, &rdev
->flags
);
875 if (mddev
->raid_disks
== 0) {
876 mddev
->major_version
= 0;
877 mddev
->minor_version
= sb
->minor_version
;
878 mddev
->patch_version
= sb
->patch_version
;
880 mddev
->chunk_sectors
= sb
->chunk_size
>> 9;
881 mddev
->ctime
= sb
->ctime
;
882 mddev
->utime
= sb
->utime
;
883 mddev
->level
= sb
->level
;
884 mddev
->clevel
[0] = 0;
885 mddev
->layout
= sb
->layout
;
886 mddev
->raid_disks
= sb
->raid_disks
;
887 mddev
->dev_sectors
= sb
->size
* 2;
889 mddev
->bitmap_offset
= 0;
890 mddev
->default_bitmap_offset
= MD_SB_BYTES
>> 9;
892 if (mddev
->minor_version
>= 91) {
893 mddev
->reshape_position
= sb
->reshape_position
;
894 mddev
->delta_disks
= sb
->delta_disks
;
895 mddev
->new_level
= sb
->new_level
;
896 mddev
->new_layout
= sb
->new_layout
;
897 mddev
->new_chunk_sectors
= sb
->new_chunk
>> 9;
899 mddev
->reshape_position
= MaxSector
;
900 mddev
->delta_disks
= 0;
901 mddev
->new_level
= mddev
->level
;
902 mddev
->new_layout
= mddev
->layout
;
903 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
906 if (sb
->state
& (1<<MD_SB_CLEAN
))
907 mddev
->recovery_cp
= MaxSector
;
909 if (sb
->events_hi
== sb
->cp_events_hi
&&
910 sb
->events_lo
== sb
->cp_events_lo
) {
911 mddev
->recovery_cp
= sb
->recovery_cp
;
913 mddev
->recovery_cp
= 0;
916 memcpy(mddev
->uuid
+0, &sb
->set_uuid0
, 4);
917 memcpy(mddev
->uuid
+4, &sb
->set_uuid1
, 4);
918 memcpy(mddev
->uuid
+8, &sb
->set_uuid2
, 4);
919 memcpy(mddev
->uuid
+12,&sb
->set_uuid3
, 4);
921 mddev
->max_disks
= MD_SB_DISKS
;
923 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
) &&
924 mddev
->bitmap_file
== NULL
)
925 mddev
->bitmap_offset
= mddev
->default_bitmap_offset
;
927 } else if (mddev
->pers
== NULL
) {
928 /* Insist on good event counter while assembling */
930 if (ev1
< mddev
->events
)
932 } else if (mddev
->bitmap
) {
933 /* if adding to array with a bitmap, then we can accept an
934 * older device ... but not too old.
936 if (ev1
< mddev
->bitmap
->events_cleared
)
939 if (ev1
< mddev
->events
)
940 /* just a hot-add of a new device, leave raid_disk at -1 */
944 if (mddev
->level
!= LEVEL_MULTIPATH
) {
945 desc
= sb
->disks
+ rdev
->desc_nr
;
947 if (desc
->state
& (1<<MD_DISK_FAULTY
))
948 set_bit(Faulty
, &rdev
->flags
);
949 else if (desc
->state
& (1<<MD_DISK_SYNC
) /* &&
950 desc->raid_disk < mddev->raid_disks */) {
951 set_bit(In_sync
, &rdev
->flags
);
952 rdev
->raid_disk
= desc
->raid_disk
;
953 } else if (desc
->state
& (1<<MD_DISK_ACTIVE
)) {
954 /* active but not in sync implies recovery up to
955 * reshape position. We don't know exactly where
956 * that is, so set to zero for now */
957 if (mddev
->minor_version
>= 91) {
958 rdev
->recovery_offset
= 0;
959 rdev
->raid_disk
= desc
->raid_disk
;
962 if (desc
->state
& (1<<MD_DISK_WRITEMOSTLY
))
963 set_bit(WriteMostly
, &rdev
->flags
);
964 } else /* MULTIPATH are always insync */
965 set_bit(In_sync
, &rdev
->flags
);
970 * sync_super for 0.90.0
972 static void super_90_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
976 int next_spare
= mddev
->raid_disks
;
979 /* make rdev->sb match mddev data..
982 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
983 * 3/ any empty disks < next_spare become removed
985 * disks[0] gets initialised to REMOVED because
986 * we cannot be sure from other fields if it has
987 * been initialised or not.
990 int active
=0, working
=0,failed
=0,spare
=0,nr_disks
=0;
992 rdev
->sb_size
= MD_SB_BYTES
;
994 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
996 memset(sb
, 0, sizeof(*sb
));
998 sb
->md_magic
= MD_SB_MAGIC
;
999 sb
->major_version
= mddev
->major_version
;
1000 sb
->patch_version
= mddev
->patch_version
;
1001 sb
->gvalid_words
= 0; /* ignored */
1002 memcpy(&sb
->set_uuid0
, mddev
->uuid
+0, 4);
1003 memcpy(&sb
->set_uuid1
, mddev
->uuid
+4, 4);
1004 memcpy(&sb
->set_uuid2
, mddev
->uuid
+8, 4);
1005 memcpy(&sb
->set_uuid3
, mddev
->uuid
+12,4);
1007 sb
->ctime
= mddev
->ctime
;
1008 sb
->level
= mddev
->level
;
1009 sb
->size
= mddev
->dev_sectors
/ 2;
1010 sb
->raid_disks
= mddev
->raid_disks
;
1011 sb
->md_minor
= mddev
->md_minor
;
1012 sb
->not_persistent
= 0;
1013 sb
->utime
= mddev
->utime
;
1015 sb
->events_hi
= (mddev
->events
>>32);
1016 sb
->events_lo
= (u32
)mddev
->events
;
1018 if (mddev
->reshape_position
== MaxSector
)
1019 sb
->minor_version
= 90;
1021 sb
->minor_version
= 91;
1022 sb
->reshape_position
= mddev
->reshape_position
;
1023 sb
->new_level
= mddev
->new_level
;
1024 sb
->delta_disks
= mddev
->delta_disks
;
1025 sb
->new_layout
= mddev
->new_layout
;
1026 sb
->new_chunk
= mddev
->new_chunk_sectors
<< 9;
1028 mddev
->minor_version
= sb
->minor_version
;
1031 sb
->recovery_cp
= mddev
->recovery_cp
;
1032 sb
->cp_events_hi
= (mddev
->events
>>32);
1033 sb
->cp_events_lo
= (u32
)mddev
->events
;
1034 if (mddev
->recovery_cp
== MaxSector
)
1035 sb
->state
= (1<< MD_SB_CLEAN
);
1037 sb
->recovery_cp
= 0;
1039 sb
->layout
= mddev
->layout
;
1040 sb
->chunk_size
= mddev
->chunk_sectors
<< 9;
1042 if (mddev
->bitmap
&& mddev
->bitmap_file
== NULL
)
1043 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
1045 sb
->disks
[0].state
= (1<<MD_DISK_REMOVED
);
1046 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
1049 int is_active
= test_bit(In_sync
, &rdev2
->flags
);
1051 if (rdev2
->raid_disk
>= 0 &&
1052 sb
->minor_version
>= 91)
1053 /* we have nowhere to store the recovery_offset,
1054 * but if it is not below the reshape_position,
1055 * we can piggy-back on that.
1058 if (rdev2
->raid_disk
< 0 ||
1059 test_bit(Faulty
, &rdev2
->flags
))
1062 desc_nr
= rdev2
->raid_disk
;
1064 desc_nr
= next_spare
++;
1065 rdev2
->desc_nr
= desc_nr
;
1066 d
= &sb
->disks
[rdev2
->desc_nr
];
1068 d
->number
= rdev2
->desc_nr
;
1069 d
->major
= MAJOR(rdev2
->bdev
->bd_dev
);
1070 d
->minor
= MINOR(rdev2
->bdev
->bd_dev
);
1072 d
->raid_disk
= rdev2
->raid_disk
;
1074 d
->raid_disk
= rdev2
->desc_nr
; /* compatibility */
1075 if (test_bit(Faulty
, &rdev2
->flags
))
1076 d
->state
= (1<<MD_DISK_FAULTY
);
1077 else if (is_active
) {
1078 d
->state
= (1<<MD_DISK_ACTIVE
);
1079 if (test_bit(In_sync
, &rdev2
->flags
))
1080 d
->state
|= (1<<MD_DISK_SYNC
);
1088 if (test_bit(WriteMostly
, &rdev2
->flags
))
1089 d
->state
|= (1<<MD_DISK_WRITEMOSTLY
);
1091 /* now set the "removed" and "faulty" bits on any missing devices */
1092 for (i
=0 ; i
< mddev
->raid_disks
; i
++) {
1093 mdp_disk_t
*d
= &sb
->disks
[i
];
1094 if (d
->state
== 0 && d
->number
== 0) {
1097 d
->state
= (1<<MD_DISK_REMOVED
);
1098 d
->state
|= (1<<MD_DISK_FAULTY
);
1102 sb
->nr_disks
= nr_disks
;
1103 sb
->active_disks
= active
;
1104 sb
->working_disks
= working
;
1105 sb
->failed_disks
= failed
;
1106 sb
->spare_disks
= spare
;
1108 sb
->this_disk
= sb
->disks
[rdev
->desc_nr
];
1109 sb
->sb_csum
= calc_sb_csum(sb
);
1113 * rdev_size_change for 0.90.0
1115 static unsigned long long
1116 super_90_rdev_size_change(mdk_rdev_t
*rdev
, sector_t num_sectors
)
1118 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1119 return 0; /* component must fit device */
1120 if (rdev
->mddev
->bitmap_offset
)
1121 return 0; /* can't move bitmap */
1122 rdev
->sb_start
= calc_dev_sboffset(rdev
->bdev
);
1123 if (!num_sectors
|| num_sectors
> rdev
->sb_start
)
1124 num_sectors
= rdev
->sb_start
;
1125 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1127 md_super_wait(rdev
->mddev
);
1133 * version 1 superblock
1136 static __le32
calc_sb_1_csum(struct mdp_superblock_1
* sb
)
1140 unsigned long long newcsum
;
1141 int size
= 256 + le32_to_cpu(sb
->max_dev
)*2;
1142 __le32
*isuper
= (__le32
*)sb
;
1145 disk_csum
= sb
->sb_csum
;
1148 for (i
=0; size
>=4; size
-= 4 )
1149 newcsum
+= le32_to_cpu(*isuper
++);
1152 newcsum
+= le16_to_cpu(*(__le16
*) isuper
);
1154 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
1155 sb
->sb_csum
= disk_csum
;
1156 return cpu_to_le32(csum
);
1159 static int super_1_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
1161 struct mdp_superblock_1
*sb
;
1164 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1168 * Calculate the position of the superblock in 512byte sectors.
1169 * It is always aligned to a 4K boundary and
1170 * depeding on minor_version, it can be:
1171 * 0: At least 8K, but less than 12K, from end of device
1172 * 1: At start of device
1173 * 2: 4K from start of device.
1175 switch(minor_version
) {
1177 sb_start
= rdev
->bdev
->bd_inode
->i_size
>> 9;
1179 sb_start
&= ~(sector_t
)(4*2-1);
1190 rdev
->sb_start
= sb_start
;
1192 /* superblock is rarely larger than 1K, but it can be larger,
1193 * and it is safe to read 4k, so we do that
1195 ret
= read_disk_sb(rdev
, 4096);
1196 if (ret
) return ret
;
1199 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1201 if (sb
->magic
!= cpu_to_le32(MD_SB_MAGIC
) ||
1202 sb
->major_version
!= cpu_to_le32(1) ||
1203 le32_to_cpu(sb
->max_dev
) > (4096-256)/2 ||
1204 le64_to_cpu(sb
->super_offset
) != rdev
->sb_start
||
1205 (le32_to_cpu(sb
->feature_map
) & ~MD_FEATURE_ALL
) != 0)
1208 if (calc_sb_1_csum(sb
) != sb
->sb_csum
) {
1209 printk("md: invalid superblock checksum on %s\n",
1210 bdevname(rdev
->bdev
,b
));
1213 if (le64_to_cpu(sb
->data_size
) < 10) {
1214 printk("md: data_size too small on %s\n",
1215 bdevname(rdev
->bdev
,b
));
1219 rdev
->preferred_minor
= 0xffff;
1220 rdev
->data_offset
= le64_to_cpu(sb
->data_offset
);
1221 atomic_set(&rdev
->corrected_errors
, le32_to_cpu(sb
->cnt_corrected_read
));
1223 rdev
->sb_size
= le32_to_cpu(sb
->max_dev
) * 2 + 256;
1224 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1225 if (rdev
->sb_size
& bmask
)
1226 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1229 && rdev
->data_offset
< sb_start
+ (rdev
->sb_size
/512))
1232 if (sb
->level
== cpu_to_le32(LEVEL_MULTIPATH
))
1235 rdev
->desc_nr
= le32_to_cpu(sb
->dev_number
);
1241 struct mdp_superblock_1
*refsb
=
1242 (struct mdp_superblock_1
*)page_address(refdev
->sb_page
);
1244 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16) != 0 ||
1245 sb
->level
!= refsb
->level
||
1246 sb
->layout
!= refsb
->layout
||
1247 sb
->chunksize
!= refsb
->chunksize
) {
1248 printk(KERN_WARNING
"md: %s has strangely different"
1249 " superblock to %s\n",
1250 bdevname(rdev
->bdev
,b
),
1251 bdevname(refdev
->bdev
,b2
));
1254 ev1
= le64_to_cpu(sb
->events
);
1255 ev2
= le64_to_cpu(refsb
->events
);
1263 rdev
->sectors
= (rdev
->bdev
->bd_inode
->i_size
>> 9) -
1264 le64_to_cpu(sb
->data_offset
);
1266 rdev
->sectors
= rdev
->sb_start
;
1267 if (rdev
->sectors
< le64_to_cpu(sb
->data_size
))
1269 rdev
->sectors
= le64_to_cpu(sb
->data_size
);
1270 if (le64_to_cpu(sb
->size
) > rdev
->sectors
)
1275 static int super_1_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1277 struct mdp_superblock_1
*sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1278 __u64 ev1
= le64_to_cpu(sb
->events
);
1280 rdev
->raid_disk
= -1;
1281 clear_bit(Faulty
, &rdev
->flags
);
1282 clear_bit(In_sync
, &rdev
->flags
);
1283 clear_bit(WriteMostly
, &rdev
->flags
);
1284 clear_bit(BarriersNotsupp
, &rdev
->flags
);
1286 if (mddev
->raid_disks
== 0) {
1287 mddev
->major_version
= 1;
1288 mddev
->patch_version
= 0;
1289 mddev
->external
= 0;
1290 mddev
->chunk_sectors
= le32_to_cpu(sb
->chunksize
);
1291 mddev
->ctime
= le64_to_cpu(sb
->ctime
) & ((1ULL << 32)-1);
1292 mddev
->utime
= le64_to_cpu(sb
->utime
) & ((1ULL << 32)-1);
1293 mddev
->level
= le32_to_cpu(sb
->level
);
1294 mddev
->clevel
[0] = 0;
1295 mddev
->layout
= le32_to_cpu(sb
->layout
);
1296 mddev
->raid_disks
= le32_to_cpu(sb
->raid_disks
);
1297 mddev
->dev_sectors
= le64_to_cpu(sb
->size
);
1298 mddev
->events
= ev1
;
1299 mddev
->bitmap_offset
= 0;
1300 mddev
->default_bitmap_offset
= 1024 >> 9;
1302 mddev
->recovery_cp
= le64_to_cpu(sb
->resync_offset
);
1303 memcpy(mddev
->uuid
, sb
->set_uuid
, 16);
1305 mddev
->max_disks
= (4096-256)/2;
1307 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BITMAP_OFFSET
) &&
1308 mddev
->bitmap_file
== NULL
)
1309 mddev
->bitmap_offset
= (__s32
)le32_to_cpu(sb
->bitmap_offset
);
1311 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
1312 mddev
->reshape_position
= le64_to_cpu(sb
->reshape_position
);
1313 mddev
->delta_disks
= le32_to_cpu(sb
->delta_disks
);
1314 mddev
->new_level
= le32_to_cpu(sb
->new_level
);
1315 mddev
->new_layout
= le32_to_cpu(sb
->new_layout
);
1316 mddev
->new_chunk_sectors
= le32_to_cpu(sb
->new_chunk
);
1318 mddev
->reshape_position
= MaxSector
;
1319 mddev
->delta_disks
= 0;
1320 mddev
->new_level
= mddev
->level
;
1321 mddev
->new_layout
= mddev
->layout
;
1322 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1325 } else if (mddev
->pers
== NULL
) {
1326 /* Insist of good event counter while assembling */
1328 if (ev1
< mddev
->events
)
1330 } else if (mddev
->bitmap
) {
1331 /* If adding to array with a bitmap, then we can accept an
1332 * older device, but not too old.
1334 if (ev1
< mddev
->bitmap
->events_cleared
)
1337 if (ev1
< mddev
->events
)
1338 /* just a hot-add of a new device, leave raid_disk at -1 */
1341 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1343 if (rdev
->desc_nr
< 0 ||
1344 rdev
->desc_nr
>= le32_to_cpu(sb
->max_dev
)) {
1348 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
1350 case 0xffff: /* spare */
1352 case 0xfffe: /* faulty */
1353 set_bit(Faulty
, &rdev
->flags
);
1356 if ((le32_to_cpu(sb
->feature_map
) &
1357 MD_FEATURE_RECOVERY_OFFSET
))
1358 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
1360 set_bit(In_sync
, &rdev
->flags
);
1361 rdev
->raid_disk
= role
;
1364 if (sb
->devflags
& WriteMostly1
)
1365 set_bit(WriteMostly
, &rdev
->flags
);
1366 } else /* MULTIPATH are always insync */
1367 set_bit(In_sync
, &rdev
->flags
);
1372 static void super_1_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1374 struct mdp_superblock_1
*sb
;
1377 /* make rdev->sb match mddev and rdev data. */
1379 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1381 sb
->feature_map
= 0;
1383 sb
->recovery_offset
= cpu_to_le64(0);
1384 memset(sb
->pad1
, 0, sizeof(sb
->pad1
));
1385 memset(sb
->pad2
, 0, sizeof(sb
->pad2
));
1386 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
1388 sb
->utime
= cpu_to_le64((__u64
)mddev
->utime
);
1389 sb
->events
= cpu_to_le64(mddev
->events
);
1391 sb
->resync_offset
= cpu_to_le64(mddev
->recovery_cp
);
1393 sb
->resync_offset
= cpu_to_le64(0);
1395 sb
->cnt_corrected_read
= cpu_to_le32(atomic_read(&rdev
->corrected_errors
));
1397 sb
->raid_disks
= cpu_to_le32(mddev
->raid_disks
);
1398 sb
->size
= cpu_to_le64(mddev
->dev_sectors
);
1399 sb
->chunksize
= cpu_to_le32(mddev
->chunk_sectors
);
1400 sb
->level
= cpu_to_le32(mddev
->level
);
1401 sb
->layout
= cpu_to_le32(mddev
->layout
);
1403 if (mddev
->bitmap
&& mddev
->bitmap_file
== NULL
) {
1404 sb
->bitmap_offset
= cpu_to_le32((__u32
)mddev
->bitmap_offset
);
1405 sb
->feature_map
= cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
);
1408 if (rdev
->raid_disk
>= 0 &&
1409 !test_bit(In_sync
, &rdev
->flags
)) {
1410 if (rdev
->recovery_offset
> 0) {
1412 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET
);
1413 sb
->recovery_offset
=
1414 cpu_to_le64(rdev
->recovery_offset
);
1418 if (mddev
->reshape_position
!= MaxSector
) {
1419 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE
);
1420 sb
->reshape_position
= cpu_to_le64(mddev
->reshape_position
);
1421 sb
->new_layout
= cpu_to_le32(mddev
->new_layout
);
1422 sb
->delta_disks
= cpu_to_le32(mddev
->delta_disks
);
1423 sb
->new_level
= cpu_to_le32(mddev
->new_level
);
1424 sb
->new_chunk
= cpu_to_le32(mddev
->new_chunk_sectors
);
1428 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
)
1429 if (rdev2
->desc_nr
+1 > max_dev
)
1430 max_dev
= rdev2
->desc_nr
+1;
1432 if (max_dev
> le32_to_cpu(sb
->max_dev
)) {
1434 sb
->max_dev
= cpu_to_le32(max_dev
);
1435 rdev
->sb_size
= max_dev
* 2 + 256;
1436 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1437 if (rdev
->sb_size
& bmask
)
1438 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1440 for (i
=0; i
<max_dev
;i
++)
1441 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1443 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
1445 if (test_bit(Faulty
, &rdev2
->flags
))
1446 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1447 else if (test_bit(In_sync
, &rdev2
->flags
))
1448 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1449 else if (rdev2
->raid_disk
>= 0 && rdev2
->recovery_offset
> 0)
1450 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1452 sb
->dev_roles
[i
] = cpu_to_le16(0xffff);
1455 sb
->sb_csum
= calc_sb_1_csum(sb
);
1458 static unsigned long long
1459 super_1_rdev_size_change(mdk_rdev_t
*rdev
, sector_t num_sectors
)
1461 struct mdp_superblock_1
*sb
;
1462 sector_t max_sectors
;
1463 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1464 return 0; /* component must fit device */
1465 if (rdev
->sb_start
< rdev
->data_offset
) {
1466 /* minor versions 1 and 2; superblock before data */
1467 max_sectors
= rdev
->bdev
->bd_inode
->i_size
>> 9;
1468 max_sectors
-= rdev
->data_offset
;
1469 if (!num_sectors
|| num_sectors
> max_sectors
)
1470 num_sectors
= max_sectors
;
1471 } else if (rdev
->mddev
->bitmap_offset
) {
1472 /* minor version 0 with bitmap we can't move */
1475 /* minor version 0; superblock after data */
1477 sb_start
= (rdev
->bdev
->bd_inode
->i_size
>> 9) - 8*2;
1478 sb_start
&= ~(sector_t
)(4*2 - 1);
1479 max_sectors
= rdev
->sectors
+ sb_start
- rdev
->sb_start
;
1480 if (!num_sectors
|| num_sectors
> max_sectors
)
1481 num_sectors
= max_sectors
;
1482 rdev
->sb_start
= sb_start
;
1484 sb
= (struct mdp_superblock_1
*) page_address(rdev
->sb_page
);
1485 sb
->data_size
= cpu_to_le64(num_sectors
);
1486 sb
->super_offset
= rdev
->sb_start
;
1487 sb
->sb_csum
= calc_sb_1_csum(sb
);
1488 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1490 md_super_wait(rdev
->mddev
);
1494 static struct super_type super_types
[] = {
1497 .owner
= THIS_MODULE
,
1498 .load_super
= super_90_load
,
1499 .validate_super
= super_90_validate
,
1500 .sync_super
= super_90_sync
,
1501 .rdev_size_change
= super_90_rdev_size_change
,
1505 .owner
= THIS_MODULE
,
1506 .load_super
= super_1_load
,
1507 .validate_super
= super_1_validate
,
1508 .sync_super
= super_1_sync
,
1509 .rdev_size_change
= super_1_rdev_size_change
,
1513 static int match_mddev_units(mddev_t
*mddev1
, mddev_t
*mddev2
)
1515 mdk_rdev_t
*rdev
, *rdev2
;
1518 rdev_for_each_rcu(rdev
, mddev1
)
1519 rdev_for_each_rcu(rdev2
, mddev2
)
1520 if (rdev
->bdev
->bd_contains
==
1521 rdev2
->bdev
->bd_contains
) {
1529 static LIST_HEAD(pending_raid_disks
);
1532 * Try to register data integrity profile for an mddev
1534 * This is called when an array is started and after a disk has been kicked
1535 * from the array. It only succeeds if all working and active component devices
1536 * are integrity capable with matching profiles.
1538 int md_integrity_register(mddev_t
*mddev
)
1540 mdk_rdev_t
*rdev
, *reference
= NULL
;
1542 if (list_empty(&mddev
->disks
))
1543 return 0; /* nothing to do */
1544 if (blk_get_integrity(mddev
->gendisk
))
1545 return 0; /* already registered */
1546 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
1547 /* skip spares and non-functional disks */
1548 if (test_bit(Faulty
, &rdev
->flags
))
1550 if (rdev
->raid_disk
< 0)
1553 * If at least one rdev is not integrity capable, we can not
1554 * enable data integrity for the md device.
1556 if (!bdev_get_integrity(rdev
->bdev
))
1559 /* Use the first rdev as the reference */
1563 /* does this rdev's profile match the reference profile? */
1564 if (blk_integrity_compare(reference
->bdev
->bd_disk
,
1565 rdev
->bdev
->bd_disk
) < 0)
1569 * All component devices are integrity capable and have matching
1570 * profiles, register the common profile for the md device.
1572 if (blk_integrity_register(mddev
->gendisk
,
1573 bdev_get_integrity(reference
->bdev
)) != 0) {
1574 printk(KERN_ERR
"md: failed to register integrity for %s\n",
1578 printk(KERN_NOTICE
"md: data integrity on %s enabled\n",
1582 EXPORT_SYMBOL(md_integrity_register
);
1584 /* Disable data integrity if non-capable/non-matching disk is being added */
1585 void md_integrity_add_rdev(mdk_rdev_t
*rdev
, mddev_t
*mddev
)
1587 struct blk_integrity
*bi_rdev
= bdev_get_integrity(rdev
->bdev
);
1588 struct blk_integrity
*bi_mddev
= blk_get_integrity(mddev
->gendisk
);
1590 if (!bi_mddev
) /* nothing to do */
1592 if (rdev
->raid_disk
< 0) /* skip spares */
1594 if (bi_rdev
&& blk_integrity_compare(mddev
->gendisk
,
1595 rdev
->bdev
->bd_disk
) >= 0)
1597 printk(KERN_NOTICE
"disabling data integrity on %s\n", mdname(mddev
));
1598 blk_integrity_unregister(mddev
->gendisk
);
1600 EXPORT_SYMBOL(md_integrity_add_rdev
);
1602 static int bind_rdev_to_array(mdk_rdev_t
* rdev
, mddev_t
* mddev
)
1604 char b
[BDEVNAME_SIZE
];
1614 /* prevent duplicates */
1615 if (find_rdev(mddev
, rdev
->bdev
->bd_dev
))
1618 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1619 if (rdev
->sectors
&& (mddev
->dev_sectors
== 0 ||
1620 rdev
->sectors
< mddev
->dev_sectors
)) {
1622 /* Cannot change size, so fail
1623 * If mddev->level <= 0, then we don't care
1624 * about aligning sizes (e.g. linear)
1626 if (mddev
->level
> 0)
1629 mddev
->dev_sectors
= rdev
->sectors
;
1632 /* Verify rdev->desc_nr is unique.
1633 * If it is -1, assign a free number, else
1634 * check number is not in use
1636 if (rdev
->desc_nr
< 0) {
1638 if (mddev
->pers
) choice
= mddev
->raid_disks
;
1639 while (find_rdev_nr(mddev
, choice
))
1641 rdev
->desc_nr
= choice
;
1643 if (find_rdev_nr(mddev
, rdev
->desc_nr
))
1646 if (mddev
->max_disks
&& rdev
->desc_nr
>= mddev
->max_disks
) {
1647 printk(KERN_WARNING
"md: %s: array is limited to %d devices\n",
1648 mdname(mddev
), mddev
->max_disks
);
1651 bdevname(rdev
->bdev
,b
);
1652 while ( (s
=strchr(b
, '/')) != NULL
)
1655 rdev
->mddev
= mddev
;
1656 printk(KERN_INFO
"md: bind<%s>\n", b
);
1658 if ((err
= kobject_add(&rdev
->kobj
, &mddev
->kobj
, "dev-%s", b
)))
1661 ko
= &part_to_dev(rdev
->bdev
->bd_part
)->kobj
;
1662 if ((err
= sysfs_create_link(&rdev
->kobj
, ko
, "block"))) {
1663 kobject_del(&rdev
->kobj
);
1666 rdev
->sysfs_state
= sysfs_get_dirent(rdev
->kobj
.sd
, "state");
1668 list_add_rcu(&rdev
->same_set
, &mddev
->disks
);
1669 bd_claim_by_disk(rdev
->bdev
, rdev
->bdev
->bd_holder
, mddev
->gendisk
);
1671 /* May as well allow recovery to be retried once */
1672 mddev
->recovery_disabled
= 0;
1677 printk(KERN_WARNING
"md: failed to register dev-%s for %s\n",
1682 static void md_delayed_delete(struct work_struct
*ws
)
1684 mdk_rdev_t
*rdev
= container_of(ws
, mdk_rdev_t
, del_work
);
1685 kobject_del(&rdev
->kobj
);
1686 kobject_put(&rdev
->kobj
);
1689 static void unbind_rdev_from_array(mdk_rdev_t
* rdev
)
1691 char b
[BDEVNAME_SIZE
];
1696 bd_release_from_disk(rdev
->bdev
, rdev
->mddev
->gendisk
);
1697 list_del_rcu(&rdev
->same_set
);
1698 printk(KERN_INFO
"md: unbind<%s>\n", bdevname(rdev
->bdev
,b
));
1700 sysfs_remove_link(&rdev
->kobj
, "block");
1701 sysfs_put(rdev
->sysfs_state
);
1702 rdev
->sysfs_state
= NULL
;
1703 /* We need to delay this, otherwise we can deadlock when
1704 * writing to 'remove' to "dev/state". We also need
1705 * to delay it due to rcu usage.
1708 INIT_WORK(&rdev
->del_work
, md_delayed_delete
);
1709 kobject_get(&rdev
->kobj
);
1710 schedule_work(&rdev
->del_work
);
1714 * prevent the device from being mounted, repartitioned or
1715 * otherwise reused by a RAID array (or any other kernel
1716 * subsystem), by bd_claiming the device.
1718 static int lock_rdev(mdk_rdev_t
*rdev
, dev_t dev
, int shared
)
1721 struct block_device
*bdev
;
1722 char b
[BDEVNAME_SIZE
];
1724 bdev
= open_by_devnum(dev
, FMODE_READ
|FMODE_WRITE
);
1726 printk(KERN_ERR
"md: could not open %s.\n",
1727 __bdevname(dev
, b
));
1728 return PTR_ERR(bdev
);
1730 err
= bd_claim(bdev
, shared
? (mdk_rdev_t
*)lock_rdev
: rdev
);
1732 printk(KERN_ERR
"md: could not bd_claim %s.\n",
1734 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
);
1738 set_bit(AllReserved
, &rdev
->flags
);
1743 static void unlock_rdev(mdk_rdev_t
*rdev
)
1745 struct block_device
*bdev
= rdev
->bdev
;
1750 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
);
1753 void md_autodetect_dev(dev_t dev
);
1755 static void export_rdev(mdk_rdev_t
* rdev
)
1757 char b
[BDEVNAME_SIZE
];
1758 printk(KERN_INFO
"md: export_rdev(%s)\n",
1759 bdevname(rdev
->bdev
,b
));
1764 if (test_bit(AutoDetected
, &rdev
->flags
))
1765 md_autodetect_dev(rdev
->bdev
->bd_dev
);
1768 kobject_put(&rdev
->kobj
);
1771 static void kick_rdev_from_array(mdk_rdev_t
* rdev
)
1773 unbind_rdev_from_array(rdev
);
1777 static void export_array(mddev_t
*mddev
)
1779 mdk_rdev_t
*rdev
, *tmp
;
1781 rdev_for_each(rdev
, tmp
, mddev
) {
1786 kick_rdev_from_array(rdev
);
1788 if (!list_empty(&mddev
->disks
))
1790 mddev
->raid_disks
= 0;
1791 mddev
->major_version
= 0;
1794 static void print_desc(mdp_disk_t
*desc
)
1796 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc
->number
,
1797 desc
->major
,desc
->minor
,desc
->raid_disk
,desc
->state
);
1800 static void print_sb_90(mdp_super_t
*sb
)
1805 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1806 sb
->major_version
, sb
->minor_version
, sb
->patch_version
,
1807 sb
->set_uuid0
, sb
->set_uuid1
, sb
->set_uuid2
, sb
->set_uuid3
,
1809 printk(KERN_INFO
"md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1810 sb
->level
, sb
->size
, sb
->nr_disks
, sb
->raid_disks
,
1811 sb
->md_minor
, sb
->layout
, sb
->chunk_size
);
1812 printk(KERN_INFO
"md: UT:%08x ST:%d AD:%d WD:%d"
1813 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1814 sb
->utime
, sb
->state
, sb
->active_disks
, sb
->working_disks
,
1815 sb
->failed_disks
, sb
->spare_disks
,
1816 sb
->sb_csum
, (unsigned long)sb
->events_lo
);
1819 for (i
= 0; i
< MD_SB_DISKS
; i
++) {
1822 desc
= sb
->disks
+ i
;
1823 if (desc
->number
|| desc
->major
|| desc
->minor
||
1824 desc
->raid_disk
|| (desc
->state
&& (desc
->state
!= 4))) {
1825 printk(" D %2d: ", i
);
1829 printk(KERN_INFO
"md: THIS: ");
1830 print_desc(&sb
->this_disk
);
1833 static void print_sb_1(struct mdp_superblock_1
*sb
)
1837 uuid
= sb
->set_uuid
;
1839 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%02x%02x%02x%02x"
1840 ":%02x%02x:%02x%02x:%02x%02x:%02x%02x%02x%02x%02x%02x>\n"
1841 "md: Name: \"%s\" CT:%llu\n",
1842 le32_to_cpu(sb
->major_version
),
1843 le32_to_cpu(sb
->feature_map
),
1844 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
1845 uuid
[4], uuid
[5], uuid
[6], uuid
[7],
1846 uuid
[8], uuid
[9], uuid
[10], uuid
[11],
1847 uuid
[12], uuid
[13], uuid
[14], uuid
[15],
1849 (unsigned long long)le64_to_cpu(sb
->ctime
)
1850 & MD_SUPERBLOCK_1_TIME_SEC_MASK
);
1852 uuid
= sb
->device_uuid
;
1854 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
1856 "md: Dev:%08x UUID: %02x%02x%02x%02x:%02x%02x:%02x%02x:%02x%02x"
1857 ":%02x%02x%02x%02x%02x%02x\n"
1858 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
1859 "md: (MaxDev:%u) \n",
1860 le32_to_cpu(sb
->level
),
1861 (unsigned long long)le64_to_cpu(sb
->size
),
1862 le32_to_cpu(sb
->raid_disks
),
1863 le32_to_cpu(sb
->layout
),
1864 le32_to_cpu(sb
->chunksize
),
1865 (unsigned long long)le64_to_cpu(sb
->data_offset
),
1866 (unsigned long long)le64_to_cpu(sb
->data_size
),
1867 (unsigned long long)le64_to_cpu(sb
->super_offset
),
1868 (unsigned long long)le64_to_cpu(sb
->recovery_offset
),
1869 le32_to_cpu(sb
->dev_number
),
1870 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
1871 uuid
[4], uuid
[5], uuid
[6], uuid
[7],
1872 uuid
[8], uuid
[9], uuid
[10], uuid
[11],
1873 uuid
[12], uuid
[13], uuid
[14], uuid
[15],
1875 (unsigned long long)le64_to_cpu(sb
->utime
) & MD_SUPERBLOCK_1_TIME_SEC_MASK
,
1876 (unsigned long long)le64_to_cpu(sb
->events
),
1877 (unsigned long long)le64_to_cpu(sb
->resync_offset
),
1878 le32_to_cpu(sb
->sb_csum
),
1879 le32_to_cpu(sb
->max_dev
)
1883 static void print_rdev(mdk_rdev_t
*rdev
, int major_version
)
1885 char b
[BDEVNAME_SIZE
];
1886 printk(KERN_INFO
"md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
1887 bdevname(rdev
->bdev
, b
), (unsigned long long)rdev
->sectors
,
1888 test_bit(Faulty
, &rdev
->flags
), test_bit(In_sync
, &rdev
->flags
),
1890 if (rdev
->sb_loaded
) {
1891 printk(KERN_INFO
"md: rdev superblock (MJ:%d):\n", major_version
);
1892 switch (major_version
) {
1894 print_sb_90((mdp_super_t
*)page_address(rdev
->sb_page
));
1897 print_sb_1((struct mdp_superblock_1
*)page_address(rdev
->sb_page
));
1901 printk(KERN_INFO
"md: no rdev superblock!\n");
1904 static void md_print_devices(void)
1906 struct list_head
*tmp
;
1909 char b
[BDEVNAME_SIZE
];
1912 printk("md: **********************************\n");
1913 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1914 printk("md: **********************************\n");
1915 for_each_mddev(mddev
, tmp
) {
1918 bitmap_print_sb(mddev
->bitmap
);
1920 printk("%s: ", mdname(mddev
));
1921 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
1922 printk("<%s>", bdevname(rdev
->bdev
,b
));
1925 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
1926 print_rdev(rdev
, mddev
->major_version
);
1928 printk("md: **********************************\n");
1933 static void sync_sbs(mddev_t
* mddev
, int nospares
)
1935 /* Update each superblock (in-memory image), but
1936 * if we are allowed to, skip spares which already
1937 * have the right event counter, or have one earlier
1938 * (which would mean they aren't being marked as dirty
1939 * with the rest of the array)
1943 /* First make sure individual recovery_offsets are correct */
1944 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
1945 if (rdev
->raid_disk
>= 0 &&
1946 !test_bit(In_sync
, &rdev
->flags
) &&
1947 mddev
->curr_resync_completed
> rdev
->recovery_offset
)
1948 rdev
->recovery_offset
= mddev
->curr_resync_completed
;
1951 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
1952 if (rdev
->sb_events
== mddev
->events
||
1954 rdev
->raid_disk
< 0 &&
1955 (rdev
->sb_events
&1)==0 &&
1956 rdev
->sb_events
+1 == mddev
->events
)) {
1957 /* Don't update this superblock */
1958 rdev
->sb_loaded
= 2;
1960 super_types
[mddev
->major_version
].
1961 sync_super(mddev
, rdev
);
1962 rdev
->sb_loaded
= 1;
1967 static void md_update_sb(mddev_t
* mddev
, int force_change
)
1973 mddev
->utime
= get_seconds();
1974 if (mddev
->external
)
1977 spin_lock_irq(&mddev
->write_lock
);
1979 set_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
1980 if (test_and_clear_bit(MD_CHANGE_DEVS
, &mddev
->flags
))
1982 if (test_and_clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
))
1983 /* just a clean<-> dirty transition, possibly leave spares alone,
1984 * though if events isn't the right even/odd, we will have to do
1990 if (mddev
->degraded
)
1991 /* If the array is degraded, then skipping spares is both
1992 * dangerous and fairly pointless.
1993 * Dangerous because a device that was removed from the array
1994 * might have a event_count that still looks up-to-date,
1995 * so it can be re-added without a resync.
1996 * Pointless because if there are any spares to skip,
1997 * then a recovery will happen and soon that array won't
1998 * be degraded any more and the spare can go back to sleep then.
2002 sync_req
= mddev
->in_sync
;
2004 /* If this is just a dirty<->clean transition, and the array is clean
2005 * and 'events' is odd, we can roll back to the previous clean state */
2007 && (mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
2008 && (mddev
->events
& 1)
2009 && mddev
->events
!= 1)
2012 /* otherwise we have to go forward and ... */
2014 if (!mddev
->in_sync
|| mddev
->recovery_cp
!= MaxSector
) { /* not clean */
2015 /* .. if the array isn't clean, an 'even' event must also go
2017 if ((mddev
->events
&1)==0) {
2019 sync_req
= 2; /* force a second update to get the
2020 * even/odd in sync */
2023 /* otherwise an 'odd' event must go to spares */
2024 if ((mddev
->events
&1)) {
2026 sync_req
= 2; /* force a second update to get the
2027 * even/odd in sync */
2032 if (!mddev
->events
) {
2034 * oops, this 64-bit counter should never wrap.
2035 * Either we are in around ~1 trillion A.C., assuming
2036 * 1 reboot per second, or we have a bug:
2043 * do not write anything to disk if using
2044 * nonpersistent superblocks
2046 if (!mddev
->persistent
) {
2047 if (!mddev
->external
)
2048 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
2050 spin_unlock_irq(&mddev
->write_lock
);
2051 wake_up(&mddev
->sb_wait
);
2054 sync_sbs(mddev
, nospares
);
2055 spin_unlock_irq(&mddev
->write_lock
);
2058 "md: updating %s RAID superblock on device (in sync %d)\n",
2059 mdname(mddev
),mddev
->in_sync
);
2061 bitmap_update_sb(mddev
->bitmap
);
2062 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2063 char b
[BDEVNAME_SIZE
];
2064 dprintk(KERN_INFO
"md: ");
2065 if (rdev
->sb_loaded
!= 1)
2066 continue; /* no noise on spare devices */
2067 if (test_bit(Faulty
, &rdev
->flags
))
2068 dprintk("(skipping faulty ");
2070 dprintk("%s ", bdevname(rdev
->bdev
,b
));
2071 if (!test_bit(Faulty
, &rdev
->flags
)) {
2072 md_super_write(mddev
,rdev
,
2073 rdev
->sb_start
, rdev
->sb_size
,
2075 dprintk(KERN_INFO
"(write) %s's sb offset: %llu\n",
2076 bdevname(rdev
->bdev
,b
),
2077 (unsigned long long)rdev
->sb_start
);
2078 rdev
->sb_events
= mddev
->events
;
2082 if (mddev
->level
== LEVEL_MULTIPATH
)
2083 /* only need to write one superblock... */
2086 md_super_wait(mddev
);
2087 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
2089 spin_lock_irq(&mddev
->write_lock
);
2090 if (mddev
->in_sync
!= sync_req
||
2091 test_bit(MD_CHANGE_DEVS
, &mddev
->flags
)) {
2092 /* have to write it out again */
2093 spin_unlock_irq(&mddev
->write_lock
);
2096 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
2097 spin_unlock_irq(&mddev
->write_lock
);
2098 wake_up(&mddev
->sb_wait
);
2099 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
2100 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
2104 /* words written to sysfs files may, or may not, be \n terminated.
2105 * We want to accept with case. For this we use cmd_match.
2107 static int cmd_match(const char *cmd
, const char *str
)
2109 /* See if cmd, written into a sysfs file, matches
2110 * str. They must either be the same, or cmd can
2111 * have a trailing newline
2113 while (*cmd
&& *str
&& *cmd
== *str
) {
2124 struct rdev_sysfs_entry
{
2125 struct attribute attr
;
2126 ssize_t (*show
)(mdk_rdev_t
*, char *);
2127 ssize_t (*store
)(mdk_rdev_t
*, const char *, size_t);
2131 state_show(mdk_rdev_t
*rdev
, char *page
)
2136 if (test_bit(Faulty
, &rdev
->flags
)) {
2137 len
+= sprintf(page
+len
, "%sfaulty",sep
);
2140 if (test_bit(In_sync
, &rdev
->flags
)) {
2141 len
+= sprintf(page
+len
, "%sin_sync",sep
);
2144 if (test_bit(WriteMostly
, &rdev
->flags
)) {
2145 len
+= sprintf(page
+len
, "%swrite_mostly",sep
);
2148 if (test_bit(Blocked
, &rdev
->flags
)) {
2149 len
+= sprintf(page
+len
, "%sblocked", sep
);
2152 if (!test_bit(Faulty
, &rdev
->flags
) &&
2153 !test_bit(In_sync
, &rdev
->flags
)) {
2154 len
+= sprintf(page
+len
, "%sspare", sep
);
2157 return len
+sprintf(page
+len
, "\n");
2161 state_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2164 * faulty - simulates and error
2165 * remove - disconnects the device
2166 * writemostly - sets write_mostly
2167 * -writemostly - clears write_mostly
2168 * blocked - sets the Blocked flag
2169 * -blocked - clears the Blocked flag
2170 * insync - sets Insync providing device isn't active
2173 if (cmd_match(buf
, "faulty") && rdev
->mddev
->pers
) {
2174 md_error(rdev
->mddev
, rdev
);
2176 } else if (cmd_match(buf
, "remove")) {
2177 if (rdev
->raid_disk
>= 0)
2180 mddev_t
*mddev
= rdev
->mddev
;
2181 kick_rdev_from_array(rdev
);
2183 md_update_sb(mddev
, 1);
2184 md_new_event(mddev
);
2187 } else if (cmd_match(buf
, "writemostly")) {
2188 set_bit(WriteMostly
, &rdev
->flags
);
2190 } else if (cmd_match(buf
, "-writemostly")) {
2191 clear_bit(WriteMostly
, &rdev
->flags
);
2193 } else if (cmd_match(buf
, "blocked")) {
2194 set_bit(Blocked
, &rdev
->flags
);
2196 } else if (cmd_match(buf
, "-blocked")) {
2197 clear_bit(Blocked
, &rdev
->flags
);
2198 wake_up(&rdev
->blocked_wait
);
2199 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2200 md_wakeup_thread(rdev
->mddev
->thread
);
2203 } else if (cmd_match(buf
, "insync") && rdev
->raid_disk
== -1) {
2204 set_bit(In_sync
, &rdev
->flags
);
2207 if (!err
&& rdev
->sysfs_state
)
2208 sysfs_notify_dirent(rdev
->sysfs_state
);
2209 return err
? err
: len
;
2211 static struct rdev_sysfs_entry rdev_state
=
2212 __ATTR(state
, S_IRUGO
|S_IWUSR
, state_show
, state_store
);
2215 errors_show(mdk_rdev_t
*rdev
, char *page
)
2217 return sprintf(page
, "%d\n", atomic_read(&rdev
->corrected_errors
));
2221 errors_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2224 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2225 if (*buf
&& (*e
== 0 || *e
== '\n')) {
2226 atomic_set(&rdev
->corrected_errors
, n
);
2231 static struct rdev_sysfs_entry rdev_errors
=
2232 __ATTR(errors
, S_IRUGO
|S_IWUSR
, errors_show
, errors_store
);
2235 slot_show(mdk_rdev_t
*rdev
, char *page
)
2237 if (rdev
->raid_disk
< 0)
2238 return sprintf(page
, "none\n");
2240 return sprintf(page
, "%d\n", rdev
->raid_disk
);
2244 slot_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2249 int slot
= simple_strtoul(buf
, &e
, 10);
2250 if (strncmp(buf
, "none", 4)==0)
2252 else if (e
==buf
|| (*e
&& *e
!= '\n'))
2254 if (rdev
->mddev
->pers
&& slot
== -1) {
2255 /* Setting 'slot' on an active array requires also
2256 * updating the 'rd%d' link, and communicating
2257 * with the personality with ->hot_*_disk.
2258 * For now we only support removing
2259 * failed/spare devices. This normally happens automatically,
2260 * but not when the metadata is externally managed.
2262 if (rdev
->raid_disk
== -1)
2264 /* personality does all needed checks */
2265 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
2267 err
= rdev
->mddev
->pers
->
2268 hot_remove_disk(rdev
->mddev
, rdev
->raid_disk
);
2271 sprintf(nm
, "rd%d", rdev
->raid_disk
);
2272 sysfs_remove_link(&rdev
->mddev
->kobj
, nm
);
2273 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2274 md_wakeup_thread(rdev
->mddev
->thread
);
2275 } else if (rdev
->mddev
->pers
) {
2277 /* Activating a spare .. or possibly reactivating
2278 * if we ever get bitmaps working here.
2281 if (rdev
->raid_disk
!= -1)
2284 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
2287 list_for_each_entry(rdev2
, &rdev
->mddev
->disks
, same_set
)
2288 if (rdev2
->raid_disk
== slot
)
2291 rdev
->raid_disk
= slot
;
2292 if (test_bit(In_sync
, &rdev
->flags
))
2293 rdev
->saved_raid_disk
= slot
;
2295 rdev
->saved_raid_disk
= -1;
2296 err
= rdev
->mddev
->pers
->
2297 hot_add_disk(rdev
->mddev
, rdev
);
2299 rdev
->raid_disk
= -1;
2302 sysfs_notify_dirent(rdev
->sysfs_state
);
2303 sprintf(nm
, "rd%d", rdev
->raid_disk
);
2304 if (sysfs_create_link(&rdev
->mddev
->kobj
, &rdev
->kobj
, nm
))
2306 "md: cannot register "
2308 nm
, mdname(rdev
->mddev
));
2310 /* don't wakeup anyone, leave that to userspace. */
2312 if (slot
>= rdev
->mddev
->raid_disks
)
2314 rdev
->raid_disk
= slot
;
2315 /* assume it is working */
2316 clear_bit(Faulty
, &rdev
->flags
);
2317 clear_bit(WriteMostly
, &rdev
->flags
);
2318 set_bit(In_sync
, &rdev
->flags
);
2319 sysfs_notify_dirent(rdev
->sysfs_state
);
2325 static struct rdev_sysfs_entry rdev_slot
=
2326 __ATTR(slot
, S_IRUGO
|S_IWUSR
, slot_show
, slot_store
);
2329 offset_show(mdk_rdev_t
*rdev
, char *page
)
2331 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->data_offset
);
2335 offset_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2338 unsigned long long offset
= simple_strtoull(buf
, &e
, 10);
2339 if (e
==buf
|| (*e
&& *e
!= '\n'))
2341 if (rdev
->mddev
->pers
&& rdev
->raid_disk
>= 0)
2343 if (rdev
->sectors
&& rdev
->mddev
->external
)
2344 /* Must set offset before size, so overlap checks
2347 rdev
->data_offset
= offset
;
2351 static struct rdev_sysfs_entry rdev_offset
=
2352 __ATTR(offset
, S_IRUGO
|S_IWUSR
, offset_show
, offset_store
);
2355 rdev_size_show(mdk_rdev_t
*rdev
, char *page
)
2357 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->sectors
/ 2);
2360 static int overlaps(sector_t s1
, sector_t l1
, sector_t s2
, sector_t l2
)
2362 /* check if two start/length pairs overlap */
2370 static int strict_blocks_to_sectors(const char *buf
, sector_t
*sectors
)
2372 unsigned long long blocks
;
2375 if (strict_strtoull(buf
, 10, &blocks
) < 0)
2378 if (blocks
& 1ULL << (8 * sizeof(blocks
) - 1))
2379 return -EINVAL
; /* sector conversion overflow */
2382 if (new != blocks
* 2)
2383 return -EINVAL
; /* unsigned long long to sector_t overflow */
2390 rdev_size_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2392 mddev_t
*my_mddev
= rdev
->mddev
;
2393 sector_t oldsectors
= rdev
->sectors
;
2396 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
2398 if (my_mddev
->pers
&& rdev
->raid_disk
>= 0) {
2399 if (my_mddev
->persistent
) {
2400 sectors
= super_types
[my_mddev
->major_version
].
2401 rdev_size_change(rdev
, sectors
);
2404 } else if (!sectors
)
2405 sectors
= (rdev
->bdev
->bd_inode
->i_size
>> 9) -
2408 if (sectors
< my_mddev
->dev_sectors
)
2409 return -EINVAL
; /* component must fit device */
2411 rdev
->sectors
= sectors
;
2412 if (sectors
> oldsectors
&& my_mddev
->external
) {
2413 /* need to check that all other rdevs with the same ->bdev
2414 * do not overlap. We need to unlock the mddev to avoid
2415 * a deadlock. We have already changed rdev->sectors, and if
2416 * we have to change it back, we will have the lock again.
2420 struct list_head
*tmp
;
2422 mddev_unlock(my_mddev
);
2423 for_each_mddev(mddev
, tmp
) {
2427 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
)
2428 if (test_bit(AllReserved
, &rdev2
->flags
) ||
2429 (rdev
->bdev
== rdev2
->bdev
&&
2431 overlaps(rdev
->data_offset
, rdev
->sectors
,
2437 mddev_unlock(mddev
);
2443 mddev_lock(my_mddev
);
2445 /* Someone else could have slipped in a size
2446 * change here, but doing so is just silly.
2447 * We put oldsectors back because we *know* it is
2448 * safe, and trust userspace not to race with
2451 rdev
->sectors
= oldsectors
;
2458 static struct rdev_sysfs_entry rdev_size
=
2459 __ATTR(size
, S_IRUGO
|S_IWUSR
, rdev_size_show
, rdev_size_store
);
2461 static struct attribute
*rdev_default_attrs
[] = {
2470 rdev_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
2472 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
2473 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
2474 mddev_t
*mddev
= rdev
->mddev
;
2480 rv
= mddev
? mddev_lock(mddev
) : -EBUSY
;
2482 if (rdev
->mddev
== NULL
)
2485 rv
= entry
->show(rdev
, page
);
2486 mddev_unlock(mddev
);
2492 rdev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
2493 const char *page
, size_t length
)
2495 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
2496 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
2498 mddev_t
*mddev
= rdev
->mddev
;
2502 if (!capable(CAP_SYS_ADMIN
))
2504 rv
= mddev
? mddev_lock(mddev
): -EBUSY
;
2506 if (rdev
->mddev
== NULL
)
2509 rv
= entry
->store(rdev
, page
, length
);
2510 mddev_unlock(mddev
);
2515 static void rdev_free(struct kobject
*ko
)
2517 mdk_rdev_t
*rdev
= container_of(ko
, mdk_rdev_t
, kobj
);
2520 static struct sysfs_ops rdev_sysfs_ops
= {
2521 .show
= rdev_attr_show
,
2522 .store
= rdev_attr_store
,
2524 static struct kobj_type rdev_ktype
= {
2525 .release
= rdev_free
,
2526 .sysfs_ops
= &rdev_sysfs_ops
,
2527 .default_attrs
= rdev_default_attrs
,
2531 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2533 * mark the device faulty if:
2535 * - the device is nonexistent (zero size)
2536 * - the device has no valid superblock
2538 * a faulty rdev _never_ has rdev->sb set.
2540 static mdk_rdev_t
*md_import_device(dev_t newdev
, int super_format
, int super_minor
)
2542 char b
[BDEVNAME_SIZE
];
2547 rdev
= kzalloc(sizeof(*rdev
), GFP_KERNEL
);
2549 printk(KERN_ERR
"md: could not alloc mem for new device!\n");
2550 return ERR_PTR(-ENOMEM
);
2553 if ((err
= alloc_disk_sb(rdev
)))
2556 err
= lock_rdev(rdev
, newdev
, super_format
== -2);
2560 kobject_init(&rdev
->kobj
, &rdev_ktype
);
2563 rdev
->saved_raid_disk
= -1;
2564 rdev
->raid_disk
= -1;
2566 rdev
->data_offset
= 0;
2567 rdev
->sb_events
= 0;
2568 atomic_set(&rdev
->nr_pending
, 0);
2569 atomic_set(&rdev
->read_errors
, 0);
2570 atomic_set(&rdev
->corrected_errors
, 0);
2572 size
= rdev
->bdev
->bd_inode
->i_size
>> BLOCK_SIZE_BITS
;
2575 "md: %s has zero or unknown size, marking faulty!\n",
2576 bdevname(rdev
->bdev
,b
));
2581 if (super_format
>= 0) {
2582 err
= super_types
[super_format
].
2583 load_super(rdev
, NULL
, super_minor
);
2584 if (err
== -EINVAL
) {
2586 "md: %s does not have a valid v%d.%d "
2587 "superblock, not importing!\n",
2588 bdevname(rdev
->bdev
,b
),
2589 super_format
, super_minor
);
2594 "md: could not read %s's sb, not importing!\n",
2595 bdevname(rdev
->bdev
,b
));
2600 INIT_LIST_HEAD(&rdev
->same_set
);
2601 init_waitqueue_head(&rdev
->blocked_wait
);
2606 if (rdev
->sb_page
) {
2612 return ERR_PTR(err
);
2616 * Check a full RAID array for plausibility
2620 static void analyze_sbs(mddev_t
* mddev
)
2623 mdk_rdev_t
*rdev
, *freshest
, *tmp
;
2624 char b
[BDEVNAME_SIZE
];
2627 rdev_for_each(rdev
, tmp
, mddev
)
2628 switch (super_types
[mddev
->major_version
].
2629 load_super(rdev
, freshest
, mddev
->minor_version
)) {
2637 "md: fatal superblock inconsistency in %s"
2638 " -- removing from array\n",
2639 bdevname(rdev
->bdev
,b
));
2640 kick_rdev_from_array(rdev
);
2644 super_types
[mddev
->major_version
].
2645 validate_super(mddev
, freshest
);
2648 rdev_for_each(rdev
, tmp
, mddev
) {
2649 if (rdev
->desc_nr
>= mddev
->max_disks
||
2650 i
> mddev
->max_disks
) {
2652 "md: %s: %s: only %d devices permitted\n",
2653 mdname(mddev
), bdevname(rdev
->bdev
, b
),
2655 kick_rdev_from_array(rdev
);
2658 if (rdev
!= freshest
)
2659 if (super_types
[mddev
->major_version
].
2660 validate_super(mddev
, rdev
)) {
2661 printk(KERN_WARNING
"md: kicking non-fresh %s"
2663 bdevname(rdev
->bdev
,b
));
2664 kick_rdev_from_array(rdev
);
2667 if (mddev
->level
== LEVEL_MULTIPATH
) {
2668 rdev
->desc_nr
= i
++;
2669 rdev
->raid_disk
= rdev
->desc_nr
;
2670 set_bit(In_sync
, &rdev
->flags
);
2671 } else if (rdev
->raid_disk
>= (mddev
->raid_disks
- min(0, mddev
->delta_disks
))) {
2672 rdev
->raid_disk
= -1;
2673 clear_bit(In_sync
, &rdev
->flags
);
2678 static void md_safemode_timeout(unsigned long data
);
2681 safe_delay_show(mddev_t
*mddev
, char *page
)
2683 int msec
= (mddev
->safemode_delay
*1000)/HZ
;
2684 return sprintf(page
, "%d.%03d\n", msec
/1000, msec
%1000);
2687 safe_delay_store(mddev_t
*mddev
, const char *cbuf
, size_t len
)
2695 /* remove a period, and count digits after it */
2696 if (len
>= sizeof(buf
))
2698 strlcpy(buf
, cbuf
, sizeof(buf
));
2699 for (i
=0; i
<len
; i
++) {
2701 if (isdigit(buf
[i
])) {
2706 } else if (buf
[i
] == '.') {
2711 if (strict_strtoul(buf
, 10, &msec
) < 0)
2713 msec
= (msec
* 1000) / scale
;
2715 mddev
->safemode_delay
= 0;
2717 unsigned long old_delay
= mddev
->safemode_delay
;
2718 mddev
->safemode_delay
= (msec
*HZ
)/1000;
2719 if (mddev
->safemode_delay
== 0)
2720 mddev
->safemode_delay
= 1;
2721 if (mddev
->safemode_delay
< old_delay
)
2722 md_safemode_timeout((unsigned long)mddev
);
2726 static struct md_sysfs_entry md_safe_delay
=
2727 __ATTR(safe_mode_delay
, S_IRUGO
|S_IWUSR
,safe_delay_show
, safe_delay_store
);
2730 level_show(mddev_t
*mddev
, char *page
)
2732 struct mdk_personality
*p
= mddev
->pers
;
2734 return sprintf(page
, "%s\n", p
->name
);
2735 else if (mddev
->clevel
[0])
2736 return sprintf(page
, "%s\n", mddev
->clevel
);
2737 else if (mddev
->level
!= LEVEL_NONE
)
2738 return sprintf(page
, "%d\n", mddev
->level
);
2744 level_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2748 struct mdk_personality
*pers
;
2752 if (mddev
->pers
== NULL
) {
2755 if (len
>= sizeof(mddev
->clevel
))
2757 strncpy(mddev
->clevel
, buf
, len
);
2758 if (mddev
->clevel
[len
-1] == '\n')
2760 mddev
->clevel
[len
] = 0;
2761 mddev
->level
= LEVEL_NONE
;
2765 /* request to change the personality. Need to ensure:
2766 * - array is not engaged in resync/recovery/reshape
2767 * - old personality can be suspended
2768 * - new personality will access other array.
2771 if (mddev
->sync_thread
|| mddev
->reshape_position
!= MaxSector
)
2774 if (!mddev
->pers
->quiesce
) {
2775 printk(KERN_WARNING
"md: %s: %s does not support online personality change\n",
2776 mdname(mddev
), mddev
->pers
->name
);
2780 /* Now find the new personality */
2781 if (len
== 0 || len
>= sizeof(level
))
2783 strncpy(level
, buf
, len
);
2784 if (level
[len
-1] == '\n')
2788 request_module("md-%s", level
);
2789 spin_lock(&pers_lock
);
2790 pers
= find_pers(LEVEL_NONE
, level
);
2791 if (!pers
|| !try_module_get(pers
->owner
)) {
2792 spin_unlock(&pers_lock
);
2793 printk(KERN_WARNING
"md: personality %s not loaded\n", level
);
2796 spin_unlock(&pers_lock
);
2798 if (pers
== mddev
->pers
) {
2799 /* Nothing to do! */
2800 module_put(pers
->owner
);
2803 if (!pers
->takeover
) {
2804 module_put(pers
->owner
);
2805 printk(KERN_WARNING
"md: %s: %s does not support personality takeover\n",
2806 mdname(mddev
), level
);
2810 /* ->takeover must set new_* and/or delta_disks
2811 * if it succeeds, and may set them when it fails.
2813 priv
= pers
->takeover(mddev
);
2815 mddev
->new_level
= mddev
->level
;
2816 mddev
->new_layout
= mddev
->layout
;
2817 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
2818 mddev
->raid_disks
-= mddev
->delta_disks
;
2819 mddev
->delta_disks
= 0;
2820 module_put(pers
->owner
);
2821 printk(KERN_WARNING
"md: %s: %s would not accept array\n",
2822 mdname(mddev
), level
);
2823 return PTR_ERR(priv
);
2826 /* Looks like we have a winner */
2827 mddev_suspend(mddev
);
2828 mddev
->pers
->stop(mddev
);
2829 module_put(mddev
->pers
->owner
);
2830 /* Invalidate devices that are now superfluous */
2831 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
2832 if (rdev
->raid_disk
>= mddev
->raid_disks
) {
2833 rdev
->raid_disk
= -1;
2834 clear_bit(In_sync
, &rdev
->flags
);
2837 mddev
->private = priv
;
2838 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
2839 mddev
->level
= mddev
->new_level
;
2840 mddev
->layout
= mddev
->new_layout
;
2841 mddev
->chunk_sectors
= mddev
->new_chunk_sectors
;
2842 mddev
->delta_disks
= 0;
2844 mddev_resume(mddev
);
2845 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
2846 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
2847 md_wakeup_thread(mddev
->thread
);
2851 static struct md_sysfs_entry md_level
=
2852 __ATTR(level
, S_IRUGO
|S_IWUSR
, level_show
, level_store
);
2856 layout_show(mddev_t
*mddev
, char *page
)
2858 /* just a number, not meaningful for all levels */
2859 if (mddev
->reshape_position
!= MaxSector
&&
2860 mddev
->layout
!= mddev
->new_layout
)
2861 return sprintf(page
, "%d (%d)\n",
2862 mddev
->new_layout
, mddev
->layout
);
2863 return sprintf(page
, "%d\n", mddev
->layout
);
2867 layout_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2870 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2872 if (!*buf
|| (*e
&& *e
!= '\n'))
2877 if (mddev
->pers
->check_reshape
== NULL
)
2879 mddev
->new_layout
= n
;
2880 err
= mddev
->pers
->check_reshape(mddev
);
2882 mddev
->new_layout
= mddev
->layout
;
2886 mddev
->new_layout
= n
;
2887 if (mddev
->reshape_position
== MaxSector
)
2892 static struct md_sysfs_entry md_layout
=
2893 __ATTR(layout
, S_IRUGO
|S_IWUSR
, layout_show
, layout_store
);
2897 raid_disks_show(mddev_t
*mddev
, char *page
)
2899 if (mddev
->raid_disks
== 0)
2901 if (mddev
->reshape_position
!= MaxSector
&&
2902 mddev
->delta_disks
!= 0)
2903 return sprintf(page
, "%d (%d)\n", mddev
->raid_disks
,
2904 mddev
->raid_disks
- mddev
->delta_disks
);
2905 return sprintf(page
, "%d\n", mddev
->raid_disks
);
2908 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
);
2911 raid_disks_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2915 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2917 if (!*buf
|| (*e
&& *e
!= '\n'))
2921 rv
= update_raid_disks(mddev
, n
);
2922 else if (mddev
->reshape_position
!= MaxSector
) {
2923 int olddisks
= mddev
->raid_disks
- mddev
->delta_disks
;
2924 mddev
->delta_disks
= n
- olddisks
;
2925 mddev
->raid_disks
= n
;
2927 mddev
->raid_disks
= n
;
2928 return rv
? rv
: len
;
2930 static struct md_sysfs_entry md_raid_disks
=
2931 __ATTR(raid_disks
, S_IRUGO
|S_IWUSR
, raid_disks_show
, raid_disks_store
);
2934 chunk_size_show(mddev_t
*mddev
, char *page
)
2936 if (mddev
->reshape_position
!= MaxSector
&&
2937 mddev
->chunk_sectors
!= mddev
->new_chunk_sectors
)
2938 return sprintf(page
, "%d (%d)\n",
2939 mddev
->new_chunk_sectors
<< 9,
2940 mddev
->chunk_sectors
<< 9);
2941 return sprintf(page
, "%d\n", mddev
->chunk_sectors
<< 9);
2945 chunk_size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2948 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2950 if (!*buf
|| (*e
&& *e
!= '\n'))
2955 if (mddev
->pers
->check_reshape
== NULL
)
2957 mddev
->new_chunk_sectors
= n
>> 9;
2958 err
= mddev
->pers
->check_reshape(mddev
);
2960 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
2964 mddev
->new_chunk_sectors
= n
>> 9;
2965 if (mddev
->reshape_position
== MaxSector
)
2966 mddev
->chunk_sectors
= n
>> 9;
2970 static struct md_sysfs_entry md_chunk_size
=
2971 __ATTR(chunk_size
, S_IRUGO
|S_IWUSR
, chunk_size_show
, chunk_size_store
);
2974 resync_start_show(mddev_t
*mddev
, char *page
)
2976 if (mddev
->recovery_cp
== MaxSector
)
2977 return sprintf(page
, "none\n");
2978 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->recovery_cp
);
2982 resync_start_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2985 unsigned long long n
= simple_strtoull(buf
, &e
, 10);
2989 if (!*buf
|| (*e
&& *e
!= '\n'))
2992 mddev
->recovery_cp
= n
;
2995 static struct md_sysfs_entry md_resync_start
=
2996 __ATTR(resync_start
, S_IRUGO
|S_IWUSR
, resync_start_show
, resync_start_store
);
2999 * The array state can be:
3002 * No devices, no size, no level
3003 * Equivalent to STOP_ARRAY ioctl
3005 * May have some settings, but array is not active
3006 * all IO results in error
3007 * When written, doesn't tear down array, but just stops it
3008 * suspended (not supported yet)
3009 * All IO requests will block. The array can be reconfigured.
3010 * Writing this, if accepted, will block until array is quiescent
3012 * no resync can happen. no superblocks get written.
3013 * write requests fail
3015 * like readonly, but behaves like 'clean' on a write request.
3017 * clean - no pending writes, but otherwise active.
3018 * When written to inactive array, starts without resync
3019 * If a write request arrives then
3020 * if metadata is known, mark 'dirty' and switch to 'active'.
3021 * if not known, block and switch to write-pending
3022 * If written to an active array that has pending writes, then fails.
3024 * fully active: IO and resync can be happening.
3025 * When written to inactive array, starts with resync
3028 * clean, but writes are blocked waiting for 'active' to be written.
3031 * like active, but no writes have been seen for a while (100msec).
3034 enum array_state
{ clear
, inactive
, suspended
, readonly
, read_auto
, clean
, active
,
3035 write_pending
, active_idle
, bad_word
};
3036 static char *array_states
[] = {
3037 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3038 "write-pending", "active-idle", NULL
};
3040 static int match_word(const char *word
, char **list
)
3043 for (n
=0; list
[n
]; n
++)
3044 if (cmd_match(word
, list
[n
]))
3050 array_state_show(mddev_t
*mddev
, char *page
)
3052 enum array_state st
= inactive
;
3065 else if (test_bit(MD_CHANGE_CLEAN
, &mddev
->flags
))
3067 else if (mddev
->safemode
)
3073 if (list_empty(&mddev
->disks
) &&
3074 mddev
->raid_disks
== 0 &&
3075 mddev
->dev_sectors
== 0)
3080 return sprintf(page
, "%s\n", array_states
[st
]);
3083 static int do_md_stop(mddev_t
* mddev
, int ro
, int is_open
);
3084 static int do_md_run(mddev_t
* mddev
);
3085 static int restart_array(mddev_t
*mddev
);
3088 array_state_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3091 enum array_state st
= match_word(buf
, array_states
);
3096 /* stopping an active array */
3097 if (atomic_read(&mddev
->openers
) > 0)
3099 err
= do_md_stop(mddev
, 0, 0);
3102 /* stopping an active array */
3104 if (atomic_read(&mddev
->openers
) > 0)
3106 err
= do_md_stop(mddev
, 2, 0);
3108 err
= 0; /* already inactive */
3111 break; /* not supported yet */
3114 err
= do_md_stop(mddev
, 1, 0);
3117 set_disk_ro(mddev
->gendisk
, 1);
3118 err
= do_md_run(mddev
);
3124 err
= do_md_stop(mddev
, 1, 0);
3125 else if (mddev
->ro
== 1)
3126 err
= restart_array(mddev
);
3129 set_disk_ro(mddev
->gendisk
, 0);
3133 err
= do_md_run(mddev
);
3138 restart_array(mddev
);
3139 spin_lock_irq(&mddev
->write_lock
);
3140 if (atomic_read(&mddev
->writes_pending
) == 0) {
3141 if (mddev
->in_sync
== 0) {
3143 if (mddev
->safemode
== 1)
3144 mddev
->safemode
= 0;
3145 if (mddev
->persistent
)
3146 set_bit(MD_CHANGE_CLEAN
,
3152 spin_unlock_irq(&mddev
->write_lock
);
3158 restart_array(mddev
);
3159 if (mddev
->external
)
3160 clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
3161 wake_up(&mddev
->sb_wait
);
3165 set_disk_ro(mddev
->gendisk
, 0);
3166 err
= do_md_run(mddev
);
3171 /* these cannot be set */
3177 sysfs_notify_dirent(mddev
->sysfs_state
);
3181 static struct md_sysfs_entry md_array_state
=
3182 __ATTR(array_state
, S_IRUGO
|S_IWUSR
, array_state_show
, array_state_store
);
3185 null_show(mddev_t
*mddev
, char *page
)
3191 new_dev_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3193 /* buf must be %d:%d\n? giving major and minor numbers */
3194 /* The new device is added to the array.
3195 * If the array has a persistent superblock, we read the
3196 * superblock to initialise info and check validity.
3197 * Otherwise, only checking done is that in bind_rdev_to_array,
3198 * which mainly checks size.
3201 int major
= simple_strtoul(buf
, &e
, 10);
3207 if (!*buf
|| *e
!= ':' || !e
[1] || e
[1] == '\n')
3209 minor
= simple_strtoul(e
+1, &e
, 10);
3210 if (*e
&& *e
!= '\n')
3212 dev
= MKDEV(major
, minor
);
3213 if (major
!= MAJOR(dev
) ||
3214 minor
!= MINOR(dev
))
3218 if (mddev
->persistent
) {
3219 rdev
= md_import_device(dev
, mddev
->major_version
,
3220 mddev
->minor_version
);
3221 if (!IS_ERR(rdev
) && !list_empty(&mddev
->disks
)) {
3222 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
3223 mdk_rdev_t
, same_set
);
3224 err
= super_types
[mddev
->major_version
]
3225 .load_super(rdev
, rdev0
, mddev
->minor_version
);
3229 } else if (mddev
->external
)
3230 rdev
= md_import_device(dev
, -2, -1);
3232 rdev
= md_import_device(dev
, -1, -1);
3235 return PTR_ERR(rdev
);
3236 err
= bind_rdev_to_array(rdev
, mddev
);
3240 return err
? err
: len
;
3243 static struct md_sysfs_entry md_new_device
=
3244 __ATTR(new_dev
, S_IWUSR
, null_show
, new_dev_store
);
3247 bitmap_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3250 unsigned long chunk
, end_chunk
;
3254 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3256 chunk
= end_chunk
= simple_strtoul(buf
, &end
, 0);
3257 if (buf
== end
) break;
3258 if (*end
== '-') { /* range */
3260 end_chunk
= simple_strtoul(buf
, &end
, 0);
3261 if (buf
== end
) break;
3263 if (*end
&& !isspace(*end
)) break;
3264 bitmap_dirty_bits(mddev
->bitmap
, chunk
, end_chunk
);
3266 while (isspace(*buf
)) buf
++;
3268 bitmap_unplug(mddev
->bitmap
); /* flush the bits to disk */
3273 static struct md_sysfs_entry md_bitmap
=
3274 __ATTR(bitmap_set_bits
, S_IWUSR
, null_show
, bitmap_store
);
3277 size_show(mddev_t
*mddev
, char *page
)
3279 return sprintf(page
, "%llu\n",
3280 (unsigned long long)mddev
->dev_sectors
/ 2);
3283 static int update_size(mddev_t
*mddev
, sector_t num_sectors
);
3286 size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3288 /* If array is inactive, we can reduce the component size, but
3289 * not increase it (except from 0).
3290 * If array is active, we can try an on-line resize
3293 int err
= strict_blocks_to_sectors(buf
, §ors
);
3298 err
= update_size(mddev
, sectors
);
3299 md_update_sb(mddev
, 1);
3301 if (mddev
->dev_sectors
== 0 ||
3302 mddev
->dev_sectors
> sectors
)
3303 mddev
->dev_sectors
= sectors
;
3307 return err
? err
: len
;
3310 static struct md_sysfs_entry md_size
=
3311 __ATTR(component_size
, S_IRUGO
|S_IWUSR
, size_show
, size_store
);
3316 * 'none' for arrays with no metadata (good luck...)
3317 * 'external' for arrays with externally managed metadata,
3318 * or N.M for internally known formats
3321 metadata_show(mddev_t
*mddev
, char *page
)
3323 if (mddev
->persistent
)
3324 return sprintf(page
, "%d.%d\n",
3325 mddev
->major_version
, mddev
->minor_version
);
3326 else if (mddev
->external
)
3327 return sprintf(page
, "external:%s\n", mddev
->metadata_type
);
3329 return sprintf(page
, "none\n");
3333 metadata_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3337 /* Changing the details of 'external' metadata is
3338 * always permitted. Otherwise there must be
3339 * no devices attached to the array.
3341 if (mddev
->external
&& strncmp(buf
, "external:", 9) == 0)
3343 else if (!list_empty(&mddev
->disks
))
3346 if (cmd_match(buf
, "none")) {
3347 mddev
->persistent
= 0;
3348 mddev
->external
= 0;
3349 mddev
->major_version
= 0;
3350 mddev
->minor_version
= 90;
3353 if (strncmp(buf
, "external:", 9) == 0) {
3354 size_t namelen
= len
-9;
3355 if (namelen
>= sizeof(mddev
->metadata_type
))
3356 namelen
= sizeof(mddev
->metadata_type
)-1;
3357 strncpy(mddev
->metadata_type
, buf
+9, namelen
);
3358 mddev
->metadata_type
[namelen
] = 0;
3359 if (namelen
&& mddev
->metadata_type
[namelen
-1] == '\n')
3360 mddev
->metadata_type
[--namelen
] = 0;
3361 mddev
->persistent
= 0;
3362 mddev
->external
= 1;
3363 mddev
->major_version
= 0;
3364 mddev
->minor_version
= 90;
3367 major
= simple_strtoul(buf
, &e
, 10);
3368 if (e
==buf
|| *e
!= '.')
3371 minor
= simple_strtoul(buf
, &e
, 10);
3372 if (e
==buf
|| (*e
&& *e
!= '\n') )
3374 if (major
>= ARRAY_SIZE(super_types
) || super_types
[major
].name
== NULL
)
3376 mddev
->major_version
= major
;
3377 mddev
->minor_version
= minor
;
3378 mddev
->persistent
= 1;
3379 mddev
->external
= 0;
3383 static struct md_sysfs_entry md_metadata
=
3384 __ATTR(metadata_version
, S_IRUGO
|S_IWUSR
, metadata_show
, metadata_store
);
3387 action_show(mddev_t
*mddev
, char *page
)
3389 char *type
= "idle";
3390 if (test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
3392 else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
3393 (!mddev
->ro
&& test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))) {
3394 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
3396 else if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
3397 if (!test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
3399 else if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
3403 } else if (test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
))
3406 return sprintf(page
, "%s\n", type
);
3410 action_store(mddev_t
*mddev
, const char *page
, size_t len
)
3412 if (!mddev
->pers
|| !mddev
->pers
->sync_request
)
3415 if (cmd_match(page
, "frozen"))
3416 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
3418 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
3420 if (cmd_match(page
, "idle") || cmd_match(page
, "frozen")) {
3421 if (mddev
->sync_thread
) {
3422 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
3423 md_unregister_thread(mddev
->sync_thread
);
3424 mddev
->sync_thread
= NULL
;
3425 mddev
->recovery
= 0;
3427 } else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
3428 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
3430 else if (cmd_match(page
, "resync"))
3431 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3432 else if (cmd_match(page
, "recover")) {
3433 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
3434 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3435 } else if (cmd_match(page
, "reshape")) {
3437 if (mddev
->pers
->start_reshape
== NULL
)
3439 err
= mddev
->pers
->start_reshape(mddev
);
3442 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
3444 if (cmd_match(page
, "check"))
3445 set_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
3446 else if (!cmd_match(page
, "repair"))
3448 set_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
3449 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
3451 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3452 md_wakeup_thread(mddev
->thread
);
3453 sysfs_notify_dirent(mddev
->sysfs_action
);
3458 mismatch_cnt_show(mddev_t
*mddev
, char *page
)
3460 return sprintf(page
, "%llu\n",
3461 (unsigned long long) mddev
->resync_mismatches
);
3464 static struct md_sysfs_entry md_scan_mode
=
3465 __ATTR(sync_action
, S_IRUGO
|S_IWUSR
, action_show
, action_store
);
3468 static struct md_sysfs_entry md_mismatches
= __ATTR_RO(mismatch_cnt
);
3471 sync_min_show(mddev_t
*mddev
, char *page
)
3473 return sprintf(page
, "%d (%s)\n", speed_min(mddev
),
3474 mddev
->sync_speed_min
? "local": "system");
3478 sync_min_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3482 if (strncmp(buf
, "system", 6)==0) {
3483 mddev
->sync_speed_min
= 0;
3486 min
= simple_strtoul(buf
, &e
, 10);
3487 if (buf
== e
|| (*e
&& *e
!= '\n') || min
<= 0)
3489 mddev
->sync_speed_min
= min
;
3493 static struct md_sysfs_entry md_sync_min
=
3494 __ATTR(sync_speed_min
, S_IRUGO
|S_IWUSR
, sync_min_show
, sync_min_store
);
3497 sync_max_show(mddev_t
*mddev
, char *page
)
3499 return sprintf(page
, "%d (%s)\n", speed_max(mddev
),
3500 mddev
->sync_speed_max
? "local": "system");
3504 sync_max_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3508 if (strncmp(buf
, "system", 6)==0) {
3509 mddev
->sync_speed_max
= 0;
3512 max
= simple_strtoul(buf
, &e
, 10);
3513 if (buf
== e
|| (*e
&& *e
!= '\n') || max
<= 0)
3515 mddev
->sync_speed_max
= max
;
3519 static struct md_sysfs_entry md_sync_max
=
3520 __ATTR(sync_speed_max
, S_IRUGO
|S_IWUSR
, sync_max_show
, sync_max_store
);
3523 degraded_show(mddev_t
*mddev
, char *page
)
3525 return sprintf(page
, "%d\n", mddev
->degraded
);
3527 static struct md_sysfs_entry md_degraded
= __ATTR_RO(degraded
);
3530 sync_force_parallel_show(mddev_t
*mddev
, char *page
)
3532 return sprintf(page
, "%d\n", mddev
->parallel_resync
);
3536 sync_force_parallel_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3540 if (strict_strtol(buf
, 10, &n
))
3543 if (n
!= 0 && n
!= 1)
3546 mddev
->parallel_resync
= n
;
3548 if (mddev
->sync_thread
)
3549 wake_up(&resync_wait
);
3554 /* force parallel resync, even with shared block devices */
3555 static struct md_sysfs_entry md_sync_force_parallel
=
3556 __ATTR(sync_force_parallel
, S_IRUGO
|S_IWUSR
,
3557 sync_force_parallel_show
, sync_force_parallel_store
);
3560 sync_speed_show(mddev_t
*mddev
, char *page
)
3562 unsigned long resync
, dt
, db
;
3563 if (mddev
->curr_resync
== 0)
3564 return sprintf(page
, "none\n");
3565 resync
= mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
);
3566 dt
= (jiffies
- mddev
->resync_mark
) / HZ
;
3568 db
= resync
- mddev
->resync_mark_cnt
;
3569 return sprintf(page
, "%lu\n", db
/dt
/2); /* K/sec */
3572 static struct md_sysfs_entry md_sync_speed
= __ATTR_RO(sync_speed
);
3575 sync_completed_show(mddev_t
*mddev
, char *page
)
3577 unsigned long max_sectors
, resync
;
3579 if (!test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3580 return sprintf(page
, "none\n");
3582 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
3583 max_sectors
= mddev
->resync_max_sectors
;
3585 max_sectors
= mddev
->dev_sectors
;
3587 resync
= mddev
->curr_resync_completed
;
3588 return sprintf(page
, "%lu / %lu\n", resync
, max_sectors
);
3591 static struct md_sysfs_entry md_sync_completed
= __ATTR_RO(sync_completed
);
3594 min_sync_show(mddev_t
*mddev
, char *page
)
3596 return sprintf(page
, "%llu\n",
3597 (unsigned long long)mddev
->resync_min
);
3600 min_sync_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3602 unsigned long long min
;
3603 if (strict_strtoull(buf
, 10, &min
))
3605 if (min
> mddev
->resync_max
)
3607 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3610 /* Must be a multiple of chunk_size */
3611 if (mddev
->chunk_sectors
) {
3612 sector_t temp
= min
;
3613 if (sector_div(temp
, mddev
->chunk_sectors
))
3616 mddev
->resync_min
= min
;
3621 static struct md_sysfs_entry md_min_sync
=
3622 __ATTR(sync_min
, S_IRUGO
|S_IWUSR
, min_sync_show
, min_sync_store
);
3625 max_sync_show(mddev_t
*mddev
, char *page
)
3627 if (mddev
->resync_max
== MaxSector
)
3628 return sprintf(page
, "max\n");
3630 return sprintf(page
, "%llu\n",
3631 (unsigned long long)mddev
->resync_max
);
3634 max_sync_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3636 if (strncmp(buf
, "max", 3) == 0)
3637 mddev
->resync_max
= MaxSector
;
3639 unsigned long long max
;
3640 if (strict_strtoull(buf
, 10, &max
))
3642 if (max
< mddev
->resync_min
)
3644 if (max
< mddev
->resync_max
&&
3646 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3649 /* Must be a multiple of chunk_size */
3650 if (mddev
->chunk_sectors
) {
3651 sector_t temp
= max
;
3652 if (sector_div(temp
, mddev
->chunk_sectors
))
3655 mddev
->resync_max
= max
;
3657 wake_up(&mddev
->recovery_wait
);
3661 static struct md_sysfs_entry md_max_sync
=
3662 __ATTR(sync_max
, S_IRUGO
|S_IWUSR
, max_sync_show
, max_sync_store
);
3665 suspend_lo_show(mddev_t
*mddev
, char *page
)
3667 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_lo
);
3671 suspend_lo_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3674 unsigned long long new = simple_strtoull(buf
, &e
, 10);
3676 if (mddev
->pers
== NULL
||
3677 mddev
->pers
->quiesce
== NULL
)
3679 if (buf
== e
|| (*e
&& *e
!= '\n'))
3681 if (new >= mddev
->suspend_hi
||
3682 (new > mddev
->suspend_lo
&& new < mddev
->suspend_hi
)) {
3683 mddev
->suspend_lo
= new;
3684 mddev
->pers
->quiesce(mddev
, 2);
3689 static struct md_sysfs_entry md_suspend_lo
=
3690 __ATTR(suspend_lo
, S_IRUGO
|S_IWUSR
, suspend_lo_show
, suspend_lo_store
);
3694 suspend_hi_show(mddev_t
*mddev
, char *page
)
3696 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_hi
);
3700 suspend_hi_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3703 unsigned long long new = simple_strtoull(buf
, &e
, 10);
3705 if (mddev
->pers
== NULL
||
3706 mddev
->pers
->quiesce
== NULL
)
3708 if (buf
== e
|| (*e
&& *e
!= '\n'))
3710 if ((new <= mddev
->suspend_lo
&& mddev
->suspend_lo
>= mddev
->suspend_hi
) ||
3711 (new > mddev
->suspend_lo
&& new > mddev
->suspend_hi
)) {
3712 mddev
->suspend_hi
= new;
3713 mddev
->pers
->quiesce(mddev
, 1);
3714 mddev
->pers
->quiesce(mddev
, 0);
3719 static struct md_sysfs_entry md_suspend_hi
=
3720 __ATTR(suspend_hi
, S_IRUGO
|S_IWUSR
, suspend_hi_show
, suspend_hi_store
);
3723 reshape_position_show(mddev_t
*mddev
, char *page
)
3725 if (mddev
->reshape_position
!= MaxSector
)
3726 return sprintf(page
, "%llu\n",
3727 (unsigned long long)mddev
->reshape_position
);
3728 strcpy(page
, "none\n");
3733 reshape_position_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3736 unsigned long long new = simple_strtoull(buf
, &e
, 10);
3739 if (buf
== e
|| (*e
&& *e
!= '\n'))
3741 mddev
->reshape_position
= new;
3742 mddev
->delta_disks
= 0;
3743 mddev
->new_level
= mddev
->level
;
3744 mddev
->new_layout
= mddev
->layout
;
3745 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3749 static struct md_sysfs_entry md_reshape_position
=
3750 __ATTR(reshape_position
, S_IRUGO
|S_IWUSR
, reshape_position_show
,
3751 reshape_position_store
);
3754 array_size_show(mddev_t
*mddev
, char *page
)
3756 if (mddev
->external_size
)
3757 return sprintf(page
, "%llu\n",
3758 (unsigned long long)mddev
->array_sectors
/2);
3760 return sprintf(page
, "default\n");
3764 array_size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3768 if (strncmp(buf
, "default", 7) == 0) {
3770 sectors
= mddev
->pers
->size(mddev
, 0, 0);
3772 sectors
= mddev
->array_sectors
;
3774 mddev
->external_size
= 0;
3776 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
3778 if (mddev
->pers
&& mddev
->pers
->size(mddev
, 0, 0) < sectors
)
3781 mddev
->external_size
= 1;
3784 mddev
->array_sectors
= sectors
;
3785 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
3787 revalidate_disk(mddev
->gendisk
);
3792 static struct md_sysfs_entry md_array_size
=
3793 __ATTR(array_size
, S_IRUGO
|S_IWUSR
, array_size_show
,
3796 static struct attribute
*md_default_attrs
[] = {
3799 &md_raid_disks
.attr
,
3800 &md_chunk_size
.attr
,
3802 &md_resync_start
.attr
,
3804 &md_new_device
.attr
,
3805 &md_safe_delay
.attr
,
3806 &md_array_state
.attr
,
3807 &md_reshape_position
.attr
,
3808 &md_array_size
.attr
,
3812 static struct attribute
*md_redundancy_attrs
[] = {
3814 &md_mismatches
.attr
,
3817 &md_sync_speed
.attr
,
3818 &md_sync_force_parallel
.attr
,
3819 &md_sync_completed
.attr
,
3822 &md_suspend_lo
.attr
,
3823 &md_suspend_hi
.attr
,
3828 static struct attribute_group md_redundancy_group
= {
3830 .attrs
= md_redundancy_attrs
,
3835 md_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
3837 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
3838 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
3843 rv
= mddev_lock(mddev
);
3845 rv
= entry
->show(mddev
, page
);
3846 mddev_unlock(mddev
);
3852 md_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
3853 const char *page
, size_t length
)
3855 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
3856 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
3861 if (!capable(CAP_SYS_ADMIN
))
3863 rv
= mddev_lock(mddev
);
3864 if (mddev
->hold_active
== UNTIL_IOCTL
)
3865 mddev
->hold_active
= 0;
3867 rv
= entry
->store(mddev
, page
, length
);
3868 mddev_unlock(mddev
);
3873 static void md_free(struct kobject
*ko
)
3875 mddev_t
*mddev
= container_of(ko
, mddev_t
, kobj
);
3877 if (mddev
->sysfs_state
)
3878 sysfs_put(mddev
->sysfs_state
);
3880 if (mddev
->gendisk
) {
3881 del_gendisk(mddev
->gendisk
);
3882 put_disk(mddev
->gendisk
);
3885 blk_cleanup_queue(mddev
->queue
);
3890 static struct sysfs_ops md_sysfs_ops
= {
3891 .show
= md_attr_show
,
3892 .store
= md_attr_store
,
3894 static struct kobj_type md_ktype
= {
3896 .sysfs_ops
= &md_sysfs_ops
,
3897 .default_attrs
= md_default_attrs
,
3902 static void mddev_delayed_delete(struct work_struct
*ws
)
3904 mddev_t
*mddev
= container_of(ws
, mddev_t
, del_work
);
3906 if (mddev
->private == &md_redundancy_group
) {
3907 sysfs_remove_group(&mddev
->kobj
, &md_redundancy_group
);
3908 if (mddev
->sysfs_action
)
3909 sysfs_put(mddev
->sysfs_action
);
3910 mddev
->sysfs_action
= NULL
;
3911 mddev
->private = NULL
;
3913 kobject_del(&mddev
->kobj
);
3914 kobject_put(&mddev
->kobj
);
3917 static int md_alloc(dev_t dev
, char *name
)
3919 static DEFINE_MUTEX(disks_mutex
);
3920 mddev_t
*mddev
= mddev_find(dev
);
3921 struct gendisk
*disk
;
3930 partitioned
= (MAJOR(mddev
->unit
) != MD_MAJOR
);
3931 shift
= partitioned
? MdpMinorShift
: 0;
3932 unit
= MINOR(mddev
->unit
) >> shift
;
3934 /* wait for any previous instance if this device
3935 * to be completed removed (mddev_delayed_delete).
3937 flush_scheduled_work();
3939 mutex_lock(&disks_mutex
);
3945 /* Need to ensure that 'name' is not a duplicate.
3948 spin_lock(&all_mddevs_lock
);
3950 list_for_each_entry(mddev2
, &all_mddevs
, all_mddevs
)
3951 if (mddev2
->gendisk
&&
3952 strcmp(mddev2
->gendisk
->disk_name
, name
) == 0) {
3953 spin_unlock(&all_mddevs_lock
);
3956 spin_unlock(&all_mddevs_lock
);
3960 mddev
->queue
= blk_alloc_queue(GFP_KERNEL
);
3963 mddev
->queue
->queuedata
= mddev
;
3965 blk_queue_make_request(mddev
->queue
, md_make_request
);
3967 disk
= alloc_disk(1 << shift
);
3969 blk_cleanup_queue(mddev
->queue
);
3970 mddev
->queue
= NULL
;
3973 disk
->major
= MAJOR(mddev
->unit
);
3974 disk
->first_minor
= unit
<< shift
;
3976 strcpy(disk
->disk_name
, name
);
3977 else if (partitioned
)
3978 sprintf(disk
->disk_name
, "md_d%d", unit
);
3980 sprintf(disk
->disk_name
, "md%d", unit
);
3981 disk
->fops
= &md_fops
;
3982 disk
->private_data
= mddev
;
3983 disk
->queue
= mddev
->queue
;
3984 /* Allow extended partitions. This makes the
3985 * 'mdp' device redundant, but we can't really
3988 disk
->flags
|= GENHD_FL_EXT_DEVT
;
3990 mddev
->gendisk
= disk
;
3991 error
= kobject_init_and_add(&mddev
->kobj
, &md_ktype
,
3992 &disk_to_dev(disk
)->kobj
, "%s", "md");
3994 /* This isn't possible, but as kobject_init_and_add is marked
3995 * __must_check, we must do something with the result
3997 printk(KERN_WARNING
"md: cannot register %s/md - name in use\n",
4002 mutex_unlock(&disks_mutex
);
4004 kobject_uevent(&mddev
->kobj
, KOBJ_ADD
);
4005 mddev
->sysfs_state
= sysfs_get_dirent(mddev
->kobj
.sd
, "array_state");
4011 static struct kobject
*md_probe(dev_t dev
, int *part
, void *data
)
4013 md_alloc(dev
, NULL
);
4017 static int add_named_array(const char *val
, struct kernel_param
*kp
)
4019 /* val must be "md_*" where * is not all digits.
4020 * We allocate an array with a large free minor number, and
4021 * set the name to val. val must not already be an active name.
4023 int len
= strlen(val
);
4024 char buf
[DISK_NAME_LEN
];
4026 while (len
&& val
[len
-1] == '\n')
4028 if (len
>= DISK_NAME_LEN
)
4030 strlcpy(buf
, val
, len
+1);
4031 if (strncmp(buf
, "md_", 3) != 0)
4033 return md_alloc(0, buf
);
4036 static void md_safemode_timeout(unsigned long data
)
4038 mddev_t
*mddev
= (mddev_t
*) data
;
4040 if (!atomic_read(&mddev
->writes_pending
)) {
4041 mddev
->safemode
= 1;
4042 if (mddev
->external
)
4043 sysfs_notify_dirent(mddev
->sysfs_state
);
4045 md_wakeup_thread(mddev
->thread
);
4048 static int start_dirty_degraded
;
4050 static int do_md_run(mddev_t
* mddev
)
4054 struct gendisk
*disk
;
4055 struct mdk_personality
*pers
;
4057 if (list_empty(&mddev
->disks
))
4058 /* cannot run an array with no devices.. */
4065 * Analyze all RAID superblock(s)
4067 if (!mddev
->raid_disks
) {
4068 if (!mddev
->persistent
)
4073 if (mddev
->level
!= LEVEL_NONE
)
4074 request_module("md-level-%d", mddev
->level
);
4075 else if (mddev
->clevel
[0])
4076 request_module("md-%s", mddev
->clevel
);
4079 * Drop all container device buffers, from now on
4080 * the only valid external interface is through the md
4083 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
4084 if (test_bit(Faulty
, &rdev
->flags
))
4086 sync_blockdev(rdev
->bdev
);
4087 invalidate_bdev(rdev
->bdev
);
4089 /* perform some consistency tests on the device.
4090 * We don't want the data to overlap the metadata,
4091 * Internal Bitmap issues have been handled elsewhere.
4093 if (rdev
->data_offset
< rdev
->sb_start
) {
4094 if (mddev
->dev_sectors
&&
4095 rdev
->data_offset
+ mddev
->dev_sectors
4097 printk("md: %s: data overlaps metadata\n",
4102 if (rdev
->sb_start
+ rdev
->sb_size
/512
4103 > rdev
->data_offset
) {
4104 printk("md: %s: metadata overlaps data\n",
4109 sysfs_notify_dirent(rdev
->sysfs_state
);
4112 md_probe(mddev
->unit
, NULL
, NULL
);
4113 disk
= mddev
->gendisk
;
4117 spin_lock(&pers_lock
);
4118 pers
= find_pers(mddev
->level
, mddev
->clevel
);
4119 if (!pers
|| !try_module_get(pers
->owner
)) {
4120 spin_unlock(&pers_lock
);
4121 if (mddev
->level
!= LEVEL_NONE
)
4122 printk(KERN_WARNING
"md: personality for level %d is not loaded!\n",
4125 printk(KERN_WARNING
"md: personality for level %s is not loaded!\n",
4130 spin_unlock(&pers_lock
);
4131 if (mddev
->level
!= pers
->level
) {
4132 mddev
->level
= pers
->level
;
4133 mddev
->new_level
= pers
->level
;
4135 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
4137 if (mddev
->reshape_position
!= MaxSector
&&
4138 pers
->start_reshape
== NULL
) {
4139 /* This personality cannot handle reshaping... */
4141 module_put(pers
->owner
);
4145 if (pers
->sync_request
) {
4146 /* Warn if this is a potentially silly
4149 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
4153 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4154 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
4156 rdev
->bdev
->bd_contains
==
4157 rdev2
->bdev
->bd_contains
) {
4159 "%s: WARNING: %s appears to be"
4160 " on the same physical disk as"
4163 bdevname(rdev
->bdev
,b
),
4164 bdevname(rdev2
->bdev
,b2
));
4171 "True protection against single-disk"
4172 " failure might be compromised.\n");
4175 mddev
->recovery
= 0;
4176 /* may be over-ridden by personality */
4177 mddev
->resync_max_sectors
= mddev
->dev_sectors
;
4179 mddev
->barriers_work
= 1;
4180 mddev
->ok_start_degraded
= start_dirty_degraded
;
4182 if (start_readonly
&& mddev
->ro
== 0)
4183 mddev
->ro
= 2; /* read-only, but switch on first write */
4185 err
= mddev
->pers
->run(mddev
);
4187 printk(KERN_ERR
"md: pers->run() failed ...\n");
4188 else if (mddev
->pers
->size(mddev
, 0, 0) < mddev
->array_sectors
) {
4189 WARN_ONCE(!mddev
->external_size
, "%s: default size too small,"
4190 " but 'external_size' not in effect?\n", __func__
);
4192 "md: invalid array_size %llu > default size %llu\n",
4193 (unsigned long long)mddev
->array_sectors
/ 2,
4194 (unsigned long long)mddev
->pers
->size(mddev
, 0, 0) / 2);
4196 mddev
->pers
->stop(mddev
);
4198 if (err
== 0 && mddev
->pers
->sync_request
) {
4199 err
= bitmap_create(mddev
);
4201 printk(KERN_ERR
"%s: failed to create bitmap (%d)\n",
4202 mdname(mddev
), err
);
4203 mddev
->pers
->stop(mddev
);
4207 module_put(mddev
->pers
->owner
);
4209 bitmap_destroy(mddev
);
4212 if (mddev
->pers
->sync_request
) {
4213 if (sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
4215 "md: cannot register extra attributes for %s\n",
4217 mddev
->sysfs_action
= sysfs_get_dirent(mddev
->kobj
.sd
, "sync_action");
4218 } else if (mddev
->ro
== 2) /* auto-readonly not meaningful */
4221 atomic_set(&mddev
->writes_pending
,0);
4222 mddev
->safemode
= 0;
4223 mddev
->safemode_timer
.function
= md_safemode_timeout
;
4224 mddev
->safemode_timer
.data
= (unsigned long) mddev
;
4225 mddev
->safemode_delay
= (200 * HZ
)/1000 +1; /* 200 msec delay */
4228 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4229 if (rdev
->raid_disk
>= 0) {
4231 sprintf(nm
, "rd%d", rdev
->raid_disk
);
4232 if (sysfs_create_link(&mddev
->kobj
, &rdev
->kobj
, nm
))
4233 printk("md: cannot register %s for %s\n",
4237 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4240 md_update_sb(mddev
, 0);
4242 set_capacity(disk
, mddev
->array_sectors
);
4244 /* If there is a partially-recovered drive we need to
4245 * start recovery here. If we leave it to md_check_recovery,
4246 * it will remove the drives and not do the right thing
4248 if (mddev
->degraded
&& !mddev
->sync_thread
) {
4250 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4251 if (rdev
->raid_disk
>= 0 &&
4252 !test_bit(In_sync
, &rdev
->flags
) &&
4253 !test_bit(Faulty
, &rdev
->flags
))
4254 /* complete an interrupted recovery */
4256 if (spares
&& mddev
->pers
->sync_request
) {
4257 mddev
->recovery
= 0;
4258 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
4259 mddev
->sync_thread
= md_register_thread(md_do_sync
,
4262 if (!mddev
->sync_thread
) {
4263 printk(KERN_ERR
"%s: could not start resync"
4266 /* leave the spares where they are, it shouldn't hurt */
4267 mddev
->recovery
= 0;
4271 md_wakeup_thread(mddev
->thread
);
4272 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
4274 revalidate_disk(mddev
->gendisk
);
4276 md_new_event(mddev
);
4277 sysfs_notify_dirent(mddev
->sysfs_state
);
4278 if (mddev
->sysfs_action
)
4279 sysfs_notify_dirent(mddev
->sysfs_action
);
4280 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
4281 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
4285 static int restart_array(mddev_t
*mddev
)
4287 struct gendisk
*disk
= mddev
->gendisk
;
4289 /* Complain if it has no devices */
4290 if (list_empty(&mddev
->disks
))
4296 mddev
->safemode
= 0;
4298 set_disk_ro(disk
, 0);
4299 printk(KERN_INFO
"md: %s switched to read-write mode.\n",
4301 /* Kick recovery or resync if necessary */
4302 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4303 md_wakeup_thread(mddev
->thread
);
4304 md_wakeup_thread(mddev
->sync_thread
);
4305 sysfs_notify_dirent(mddev
->sysfs_state
);
4309 /* similar to deny_write_access, but accounts for our holding a reference
4310 * to the file ourselves */
4311 static int deny_bitmap_write_access(struct file
* file
)
4313 struct inode
*inode
= file
->f_mapping
->host
;
4315 spin_lock(&inode
->i_lock
);
4316 if (atomic_read(&inode
->i_writecount
) > 1) {
4317 spin_unlock(&inode
->i_lock
);
4320 atomic_set(&inode
->i_writecount
, -1);
4321 spin_unlock(&inode
->i_lock
);
4326 static void restore_bitmap_write_access(struct file
*file
)
4328 struct inode
*inode
= file
->f_mapping
->host
;
4330 spin_lock(&inode
->i_lock
);
4331 atomic_set(&inode
->i_writecount
, 1);
4332 spin_unlock(&inode
->i_lock
);
4336 * 0 - completely stop and dis-assemble array
4337 * 1 - switch to readonly
4338 * 2 - stop but do not disassemble array
4340 static int do_md_stop(mddev_t
* mddev
, int mode
, int is_open
)
4343 struct gendisk
*disk
= mddev
->gendisk
;
4346 mutex_lock(&mddev
->open_mutex
);
4347 if (atomic_read(&mddev
->openers
) > is_open
) {
4348 printk("md: %s still in use.\n",mdname(mddev
));
4350 } else if (mddev
->pers
) {
4352 if (mddev
->sync_thread
) {
4353 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4354 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4355 md_unregister_thread(mddev
->sync_thread
);
4356 mddev
->sync_thread
= NULL
;
4359 del_timer_sync(&mddev
->safemode_timer
);
4362 case 1: /* readonly */
4368 case 0: /* disassemble */
4370 bitmap_flush(mddev
);
4371 md_super_wait(mddev
);
4373 set_disk_ro(disk
, 0);
4375 mddev
->pers
->stop(mddev
);
4376 mddev
->queue
->merge_bvec_fn
= NULL
;
4377 mddev
->queue
->unplug_fn
= NULL
;
4378 mddev
->queue
->backing_dev_info
.congested_fn
= NULL
;
4379 module_put(mddev
->pers
->owner
);
4380 if (mddev
->pers
->sync_request
)
4381 mddev
->private = &md_redundancy_group
;
4383 /* tell userspace to handle 'inactive' */
4384 sysfs_notify_dirent(mddev
->sysfs_state
);
4386 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4387 if (rdev
->raid_disk
>= 0) {
4389 sprintf(nm
, "rd%d", rdev
->raid_disk
);
4390 sysfs_remove_link(&mddev
->kobj
, nm
);
4393 set_capacity(disk
, 0);
4399 if (!mddev
->in_sync
|| mddev
->flags
) {
4400 /* mark array as shutdown cleanly */
4402 md_update_sb(mddev
, 1);
4405 set_disk_ro(disk
, 1);
4406 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4410 mutex_unlock(&mddev
->open_mutex
);
4414 * Free resources if final stop
4418 printk(KERN_INFO
"md: %s stopped.\n", mdname(mddev
));
4420 bitmap_destroy(mddev
);
4421 if (mddev
->bitmap_file
) {
4422 restore_bitmap_write_access(mddev
->bitmap_file
);
4423 fput(mddev
->bitmap_file
);
4424 mddev
->bitmap_file
= NULL
;
4426 mddev
->bitmap_offset
= 0;
4428 /* make sure all md_delayed_delete calls have finished */
4429 flush_scheduled_work();
4431 export_array(mddev
);
4433 mddev
->array_sectors
= 0;
4434 mddev
->external_size
= 0;
4435 mddev
->dev_sectors
= 0;
4436 mddev
->raid_disks
= 0;
4437 mddev
->recovery_cp
= 0;
4438 mddev
->resync_min
= 0;
4439 mddev
->resync_max
= MaxSector
;
4440 mddev
->reshape_position
= MaxSector
;
4441 mddev
->external
= 0;
4442 mddev
->persistent
= 0;
4443 mddev
->level
= LEVEL_NONE
;
4444 mddev
->clevel
[0] = 0;
4447 mddev
->metadata_type
[0] = 0;
4448 mddev
->chunk_sectors
= 0;
4449 mddev
->ctime
= mddev
->utime
= 0;
4451 mddev
->max_disks
= 0;
4453 mddev
->delta_disks
= 0;
4454 mddev
->new_level
= LEVEL_NONE
;
4455 mddev
->new_layout
= 0;
4456 mddev
->new_chunk_sectors
= 0;
4457 mddev
->curr_resync
= 0;
4458 mddev
->resync_mismatches
= 0;
4459 mddev
->suspend_lo
= mddev
->suspend_hi
= 0;
4460 mddev
->sync_speed_min
= mddev
->sync_speed_max
= 0;
4461 mddev
->recovery
= 0;
4464 mddev
->degraded
= 0;
4465 mddev
->barriers_work
= 0;
4466 mddev
->safemode
= 0;
4467 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
4468 if (mddev
->hold_active
== UNTIL_STOP
)
4469 mddev
->hold_active
= 0;
4471 } else if (mddev
->pers
)
4472 printk(KERN_INFO
"md: %s switched to read-only mode.\n",
4475 blk_integrity_unregister(disk
);
4476 md_new_event(mddev
);
4477 sysfs_notify_dirent(mddev
->sysfs_state
);
4482 static void autorun_array(mddev_t
*mddev
)
4487 if (list_empty(&mddev
->disks
))
4490 printk(KERN_INFO
"md: running: ");
4492 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
4493 char b
[BDEVNAME_SIZE
];
4494 printk("<%s>", bdevname(rdev
->bdev
,b
));
4498 err
= do_md_run(mddev
);
4500 printk(KERN_WARNING
"md: do_md_run() returned %d\n", err
);
4501 do_md_stop(mddev
, 0, 0);
4506 * lets try to run arrays based on all disks that have arrived
4507 * until now. (those are in pending_raid_disks)
4509 * the method: pick the first pending disk, collect all disks with
4510 * the same UUID, remove all from the pending list and put them into
4511 * the 'same_array' list. Then order this list based on superblock
4512 * update time (freshest comes first), kick out 'old' disks and
4513 * compare superblocks. If everything's fine then run it.
4515 * If "unit" is allocated, then bump its reference count
4517 static void autorun_devices(int part
)
4519 mdk_rdev_t
*rdev0
, *rdev
, *tmp
;
4521 char b
[BDEVNAME_SIZE
];
4523 printk(KERN_INFO
"md: autorun ...\n");
4524 while (!list_empty(&pending_raid_disks
)) {
4527 LIST_HEAD(candidates
);
4528 rdev0
= list_entry(pending_raid_disks
.next
,
4529 mdk_rdev_t
, same_set
);
4531 printk(KERN_INFO
"md: considering %s ...\n",
4532 bdevname(rdev0
->bdev
,b
));
4533 INIT_LIST_HEAD(&candidates
);
4534 rdev_for_each_list(rdev
, tmp
, &pending_raid_disks
)
4535 if (super_90_load(rdev
, rdev0
, 0) >= 0) {
4536 printk(KERN_INFO
"md: adding %s ...\n",
4537 bdevname(rdev
->bdev
,b
));
4538 list_move(&rdev
->same_set
, &candidates
);
4541 * now we have a set of devices, with all of them having
4542 * mostly sane superblocks. It's time to allocate the
4546 dev
= MKDEV(mdp_major
,
4547 rdev0
->preferred_minor
<< MdpMinorShift
);
4548 unit
= MINOR(dev
) >> MdpMinorShift
;
4550 dev
= MKDEV(MD_MAJOR
, rdev0
->preferred_minor
);
4553 if (rdev0
->preferred_minor
!= unit
) {
4554 printk(KERN_INFO
"md: unit number in %s is bad: %d\n",
4555 bdevname(rdev0
->bdev
, b
), rdev0
->preferred_minor
);
4559 md_probe(dev
, NULL
, NULL
);
4560 mddev
= mddev_find(dev
);
4561 if (!mddev
|| !mddev
->gendisk
) {
4565 "md: cannot allocate memory for md drive.\n");
4568 if (mddev_lock(mddev
))
4569 printk(KERN_WARNING
"md: %s locked, cannot run\n",
4571 else if (mddev
->raid_disks
|| mddev
->major_version
4572 || !list_empty(&mddev
->disks
)) {
4574 "md: %s already running, cannot run %s\n",
4575 mdname(mddev
), bdevname(rdev0
->bdev
,b
));
4576 mddev_unlock(mddev
);
4578 printk(KERN_INFO
"md: created %s\n", mdname(mddev
));
4579 mddev
->persistent
= 1;
4580 rdev_for_each_list(rdev
, tmp
, &candidates
) {
4581 list_del_init(&rdev
->same_set
);
4582 if (bind_rdev_to_array(rdev
, mddev
))
4585 autorun_array(mddev
);
4586 mddev_unlock(mddev
);
4588 /* on success, candidates will be empty, on error
4591 rdev_for_each_list(rdev
, tmp
, &candidates
) {
4592 list_del_init(&rdev
->same_set
);
4597 printk(KERN_INFO
"md: ... autorun DONE.\n");
4599 #endif /* !MODULE */
4601 static int get_version(void __user
* arg
)
4605 ver
.major
= MD_MAJOR_VERSION
;
4606 ver
.minor
= MD_MINOR_VERSION
;
4607 ver
.patchlevel
= MD_PATCHLEVEL_VERSION
;
4609 if (copy_to_user(arg
, &ver
, sizeof(ver
)))
4615 static int get_array_info(mddev_t
* mddev
, void __user
* arg
)
4617 mdu_array_info_t info
;
4618 int nr
,working
,insync
,failed
,spare
;
4621 nr
=working
=insync
=failed
=spare
=0;
4622 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
4624 if (test_bit(Faulty
, &rdev
->flags
))
4628 if (test_bit(In_sync
, &rdev
->flags
))
4635 info
.major_version
= mddev
->major_version
;
4636 info
.minor_version
= mddev
->minor_version
;
4637 info
.patch_version
= MD_PATCHLEVEL_VERSION
;
4638 info
.ctime
= mddev
->ctime
;
4639 info
.level
= mddev
->level
;
4640 info
.size
= mddev
->dev_sectors
/ 2;
4641 if (info
.size
!= mddev
->dev_sectors
/ 2) /* overflow */
4644 info
.raid_disks
= mddev
->raid_disks
;
4645 info
.md_minor
= mddev
->md_minor
;
4646 info
.not_persistent
= !mddev
->persistent
;
4648 info
.utime
= mddev
->utime
;
4651 info
.state
= (1<<MD_SB_CLEAN
);
4652 if (mddev
->bitmap
&& mddev
->bitmap_offset
)
4653 info
.state
= (1<<MD_SB_BITMAP_PRESENT
);
4654 info
.active_disks
= insync
;
4655 info
.working_disks
= working
;
4656 info
.failed_disks
= failed
;
4657 info
.spare_disks
= spare
;
4659 info
.layout
= mddev
->layout
;
4660 info
.chunk_size
= mddev
->chunk_sectors
<< 9;
4662 if (copy_to_user(arg
, &info
, sizeof(info
)))
4668 static int get_bitmap_file(mddev_t
* mddev
, void __user
* arg
)
4670 mdu_bitmap_file_t
*file
= NULL
; /* too big for stack allocation */
4671 char *ptr
, *buf
= NULL
;
4674 if (md_allow_write(mddev
))
4675 file
= kmalloc(sizeof(*file
), GFP_NOIO
);
4677 file
= kmalloc(sizeof(*file
), GFP_KERNEL
);
4682 /* bitmap disabled, zero the first byte and copy out */
4683 if (!mddev
->bitmap
|| !mddev
->bitmap
->file
) {
4684 file
->pathname
[0] = '\0';
4688 buf
= kmalloc(sizeof(file
->pathname
), GFP_KERNEL
);
4692 ptr
= d_path(&mddev
->bitmap
->file
->f_path
, buf
, sizeof(file
->pathname
));
4696 strcpy(file
->pathname
, ptr
);
4700 if (copy_to_user(arg
, file
, sizeof(*file
)))
4708 static int get_disk_info(mddev_t
* mddev
, void __user
* arg
)
4710 mdu_disk_info_t info
;
4713 if (copy_from_user(&info
, arg
, sizeof(info
)))
4716 rdev
= find_rdev_nr(mddev
, info
.number
);
4718 info
.major
= MAJOR(rdev
->bdev
->bd_dev
);
4719 info
.minor
= MINOR(rdev
->bdev
->bd_dev
);
4720 info
.raid_disk
= rdev
->raid_disk
;
4722 if (test_bit(Faulty
, &rdev
->flags
))
4723 info
.state
|= (1<<MD_DISK_FAULTY
);
4724 else if (test_bit(In_sync
, &rdev
->flags
)) {
4725 info
.state
|= (1<<MD_DISK_ACTIVE
);
4726 info
.state
|= (1<<MD_DISK_SYNC
);
4728 if (test_bit(WriteMostly
, &rdev
->flags
))
4729 info
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
4731 info
.major
= info
.minor
= 0;
4732 info
.raid_disk
= -1;
4733 info
.state
= (1<<MD_DISK_REMOVED
);
4736 if (copy_to_user(arg
, &info
, sizeof(info
)))
4742 static int add_new_disk(mddev_t
* mddev
, mdu_disk_info_t
*info
)
4744 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
4746 dev_t dev
= MKDEV(info
->major
,info
->minor
);
4748 if (info
->major
!= MAJOR(dev
) || info
->minor
!= MINOR(dev
))
4751 if (!mddev
->raid_disks
) {
4753 /* expecting a device which has a superblock */
4754 rdev
= md_import_device(dev
, mddev
->major_version
, mddev
->minor_version
);
4757 "md: md_import_device returned %ld\n",
4759 return PTR_ERR(rdev
);
4761 if (!list_empty(&mddev
->disks
)) {
4762 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
4763 mdk_rdev_t
, same_set
);
4764 err
= super_types
[mddev
->major_version
]
4765 .load_super(rdev
, rdev0
, mddev
->minor_version
);
4768 "md: %s has different UUID to %s\n",
4769 bdevname(rdev
->bdev
,b
),
4770 bdevname(rdev0
->bdev
,b2
));
4775 err
= bind_rdev_to_array(rdev
, mddev
);
4782 * add_new_disk can be used once the array is assembled
4783 * to add "hot spares". They must already have a superblock
4788 if (!mddev
->pers
->hot_add_disk
) {
4790 "%s: personality does not support diskops!\n",
4794 if (mddev
->persistent
)
4795 rdev
= md_import_device(dev
, mddev
->major_version
,
4796 mddev
->minor_version
);
4798 rdev
= md_import_device(dev
, -1, -1);
4801 "md: md_import_device returned %ld\n",
4803 return PTR_ERR(rdev
);
4805 /* set saved_raid_disk if appropriate */
4806 if (!mddev
->persistent
) {
4807 if (info
->state
& (1<<MD_DISK_SYNC
) &&
4808 info
->raid_disk
< mddev
->raid_disks
) {
4809 rdev
->raid_disk
= info
->raid_disk
;
4810 set_bit(In_sync
, &rdev
->flags
);
4812 rdev
->raid_disk
= -1;
4814 super_types
[mddev
->major_version
].
4815 validate_super(mddev
, rdev
);
4816 if (test_bit(In_sync
, &rdev
->flags
))
4817 rdev
->saved_raid_disk
= rdev
->raid_disk
;
4819 rdev
->saved_raid_disk
= -1;
4821 clear_bit(In_sync
, &rdev
->flags
); /* just to be sure */
4822 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
4823 set_bit(WriteMostly
, &rdev
->flags
);
4825 clear_bit(WriteMostly
, &rdev
->flags
);
4827 rdev
->raid_disk
= -1;
4828 err
= bind_rdev_to_array(rdev
, mddev
);
4829 if (!err
&& !mddev
->pers
->hot_remove_disk
) {
4830 /* If there is hot_add_disk but no hot_remove_disk
4831 * then added disks for geometry changes,
4832 * and should be added immediately.
4834 super_types
[mddev
->major_version
].
4835 validate_super(mddev
, rdev
);
4836 err
= mddev
->pers
->hot_add_disk(mddev
, rdev
);
4838 unbind_rdev_from_array(rdev
);
4843 sysfs_notify_dirent(rdev
->sysfs_state
);
4845 md_update_sb(mddev
, 1);
4846 if (mddev
->degraded
)
4847 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
4848 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4849 md_wakeup_thread(mddev
->thread
);
4853 /* otherwise, add_new_disk is only allowed
4854 * for major_version==0 superblocks
4856 if (mddev
->major_version
!= 0) {
4857 printk(KERN_WARNING
"%s: ADD_NEW_DISK not supported\n",
4862 if (!(info
->state
& (1<<MD_DISK_FAULTY
))) {
4864 rdev
= md_import_device(dev
, -1, 0);
4867 "md: error, md_import_device() returned %ld\n",
4869 return PTR_ERR(rdev
);
4871 rdev
->desc_nr
= info
->number
;
4872 if (info
->raid_disk
< mddev
->raid_disks
)
4873 rdev
->raid_disk
= info
->raid_disk
;
4875 rdev
->raid_disk
= -1;
4877 if (rdev
->raid_disk
< mddev
->raid_disks
)
4878 if (info
->state
& (1<<MD_DISK_SYNC
))
4879 set_bit(In_sync
, &rdev
->flags
);
4881 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
4882 set_bit(WriteMostly
, &rdev
->flags
);
4884 if (!mddev
->persistent
) {
4885 printk(KERN_INFO
"md: nonpersistent superblock ...\n");
4886 rdev
->sb_start
= rdev
->bdev
->bd_inode
->i_size
/ 512;
4888 rdev
->sb_start
= calc_dev_sboffset(rdev
->bdev
);
4889 rdev
->sectors
= rdev
->sb_start
;
4891 err
= bind_rdev_to_array(rdev
, mddev
);
4901 static int hot_remove_disk(mddev_t
* mddev
, dev_t dev
)
4903 char b
[BDEVNAME_SIZE
];
4906 rdev
= find_rdev(mddev
, dev
);
4910 if (rdev
->raid_disk
>= 0)
4913 kick_rdev_from_array(rdev
);
4914 md_update_sb(mddev
, 1);
4915 md_new_event(mddev
);
4919 printk(KERN_WARNING
"md: cannot remove active disk %s from %s ...\n",
4920 bdevname(rdev
->bdev
,b
), mdname(mddev
));
4924 static int hot_add_disk(mddev_t
* mddev
, dev_t dev
)
4926 char b
[BDEVNAME_SIZE
];
4933 if (mddev
->major_version
!= 0) {
4934 printk(KERN_WARNING
"%s: HOT_ADD may only be used with"
4935 " version-0 superblocks.\n",
4939 if (!mddev
->pers
->hot_add_disk
) {
4941 "%s: personality does not support diskops!\n",
4946 rdev
= md_import_device(dev
, -1, 0);
4949 "md: error, md_import_device() returned %ld\n",
4954 if (mddev
->persistent
)
4955 rdev
->sb_start
= calc_dev_sboffset(rdev
->bdev
);
4957 rdev
->sb_start
= rdev
->bdev
->bd_inode
->i_size
/ 512;
4959 rdev
->sectors
= rdev
->sb_start
;
4961 if (test_bit(Faulty
, &rdev
->flags
)) {
4963 "md: can not hot-add faulty %s disk to %s!\n",
4964 bdevname(rdev
->bdev
,b
), mdname(mddev
));
4968 clear_bit(In_sync
, &rdev
->flags
);
4970 rdev
->saved_raid_disk
= -1;
4971 err
= bind_rdev_to_array(rdev
, mddev
);
4976 * The rest should better be atomic, we can have disk failures
4977 * noticed in interrupt contexts ...
4980 rdev
->raid_disk
= -1;
4982 md_update_sb(mddev
, 1);
4985 * Kick recovery, maybe this spare has to be added to the
4986 * array immediately.
4988 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4989 md_wakeup_thread(mddev
->thread
);
4990 md_new_event(mddev
);
4998 static int set_bitmap_file(mddev_t
*mddev
, int fd
)
5003 if (!mddev
->pers
->quiesce
)
5005 if (mddev
->recovery
|| mddev
->sync_thread
)
5007 /* we should be able to change the bitmap.. */
5013 return -EEXIST
; /* cannot add when bitmap is present */
5014 mddev
->bitmap_file
= fget(fd
);
5016 if (mddev
->bitmap_file
== NULL
) {
5017 printk(KERN_ERR
"%s: error: failed to get bitmap file\n",
5022 err
= deny_bitmap_write_access(mddev
->bitmap_file
);
5024 printk(KERN_ERR
"%s: error: bitmap file is already in use\n",
5026 fput(mddev
->bitmap_file
);
5027 mddev
->bitmap_file
= NULL
;
5030 mddev
->bitmap_offset
= 0; /* file overrides offset */
5031 } else if (mddev
->bitmap
== NULL
)
5032 return -ENOENT
; /* cannot remove what isn't there */
5035 mddev
->pers
->quiesce(mddev
, 1);
5037 err
= bitmap_create(mddev
);
5038 if (fd
< 0 || err
) {
5039 bitmap_destroy(mddev
);
5040 fd
= -1; /* make sure to put the file */
5042 mddev
->pers
->quiesce(mddev
, 0);
5045 if (mddev
->bitmap_file
) {
5046 restore_bitmap_write_access(mddev
->bitmap_file
);
5047 fput(mddev
->bitmap_file
);
5049 mddev
->bitmap_file
= NULL
;
5056 * set_array_info is used two different ways
5057 * The original usage is when creating a new array.
5058 * In this usage, raid_disks is > 0 and it together with
5059 * level, size, not_persistent,layout,chunksize determine the
5060 * shape of the array.
5061 * This will always create an array with a type-0.90.0 superblock.
5062 * The newer usage is when assembling an array.
5063 * In this case raid_disks will be 0, and the major_version field is
5064 * use to determine which style super-blocks are to be found on the devices.
5065 * The minor and patch _version numbers are also kept incase the
5066 * super_block handler wishes to interpret them.
5068 static int set_array_info(mddev_t
* mddev
, mdu_array_info_t
*info
)
5071 if (info
->raid_disks
== 0) {
5072 /* just setting version number for superblock loading */
5073 if (info
->major_version
< 0 ||
5074 info
->major_version
>= ARRAY_SIZE(super_types
) ||
5075 super_types
[info
->major_version
].name
== NULL
) {
5076 /* maybe try to auto-load a module? */
5078 "md: superblock version %d not known\n",
5079 info
->major_version
);
5082 mddev
->major_version
= info
->major_version
;
5083 mddev
->minor_version
= info
->minor_version
;
5084 mddev
->patch_version
= info
->patch_version
;
5085 mddev
->persistent
= !info
->not_persistent
;
5086 /* ensure mddev_put doesn't delete this now that there
5087 * is some minimal configuration.
5089 mddev
->ctime
= get_seconds();
5092 mddev
->major_version
= MD_MAJOR_VERSION
;
5093 mddev
->minor_version
= MD_MINOR_VERSION
;
5094 mddev
->patch_version
= MD_PATCHLEVEL_VERSION
;
5095 mddev
->ctime
= get_seconds();
5097 mddev
->level
= info
->level
;
5098 mddev
->clevel
[0] = 0;
5099 mddev
->dev_sectors
= 2 * (sector_t
)info
->size
;
5100 mddev
->raid_disks
= info
->raid_disks
;
5101 /* don't set md_minor, it is determined by which /dev/md* was
5104 if (info
->state
& (1<<MD_SB_CLEAN
))
5105 mddev
->recovery_cp
= MaxSector
;
5107 mddev
->recovery_cp
= 0;
5108 mddev
->persistent
= ! info
->not_persistent
;
5109 mddev
->external
= 0;
5111 mddev
->layout
= info
->layout
;
5112 mddev
->chunk_sectors
= info
->chunk_size
>> 9;
5114 mddev
->max_disks
= MD_SB_DISKS
;
5116 if (mddev
->persistent
)
5118 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
5120 mddev
->default_bitmap_offset
= MD_SB_BYTES
>> 9;
5121 mddev
->bitmap_offset
= 0;
5123 mddev
->reshape_position
= MaxSector
;
5126 * Generate a 128 bit UUID
5128 get_random_bytes(mddev
->uuid
, 16);
5130 mddev
->new_level
= mddev
->level
;
5131 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
5132 mddev
->new_layout
= mddev
->layout
;
5133 mddev
->delta_disks
= 0;
5138 void md_set_array_sectors(mddev_t
*mddev
, sector_t array_sectors
)
5140 WARN(!mddev_is_locked(mddev
), "%s: unlocked mddev!\n", __func__
);
5142 if (mddev
->external_size
)
5145 mddev
->array_sectors
= array_sectors
;
5147 EXPORT_SYMBOL(md_set_array_sectors
);
5149 static int update_size(mddev_t
*mddev
, sector_t num_sectors
)
5153 int fit
= (num_sectors
== 0);
5155 if (mddev
->pers
->resize
== NULL
)
5157 /* The "num_sectors" is the number of sectors of each device that
5158 * is used. This can only make sense for arrays with redundancy.
5159 * linear and raid0 always use whatever space is available. We can only
5160 * consider changing this number if no resync or reconstruction is
5161 * happening, and if the new size is acceptable. It must fit before the
5162 * sb_start or, if that is <data_offset, it must fit before the size
5163 * of each device. If num_sectors is zero, we find the largest size
5167 if (mddev
->sync_thread
)
5170 /* Sorry, cannot grow a bitmap yet, just remove it,
5174 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
5175 sector_t avail
= rdev
->sectors
;
5177 if (fit
&& (num_sectors
== 0 || num_sectors
> avail
))
5178 num_sectors
= avail
;
5179 if (avail
< num_sectors
)
5182 rv
= mddev
->pers
->resize(mddev
, num_sectors
);
5184 revalidate_disk(mddev
->gendisk
);
5188 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
)
5191 /* change the number of raid disks */
5192 if (mddev
->pers
->check_reshape
== NULL
)
5194 if (raid_disks
<= 0 ||
5195 raid_disks
>= mddev
->max_disks
)
5197 if (mddev
->sync_thread
|| mddev
->reshape_position
!= MaxSector
)
5199 mddev
->delta_disks
= raid_disks
- mddev
->raid_disks
;
5201 rv
= mddev
->pers
->check_reshape(mddev
);
5207 * update_array_info is used to change the configuration of an
5209 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5210 * fields in the info are checked against the array.
5211 * Any differences that cannot be handled will cause an error.
5212 * Normally, only one change can be managed at a time.
5214 static int update_array_info(mddev_t
*mddev
, mdu_array_info_t
*info
)
5220 /* calculate expected state,ignoring low bits */
5221 if (mddev
->bitmap
&& mddev
->bitmap_offset
)
5222 state
|= (1 << MD_SB_BITMAP_PRESENT
);
5224 if (mddev
->major_version
!= info
->major_version
||
5225 mddev
->minor_version
!= info
->minor_version
||
5226 /* mddev->patch_version != info->patch_version || */
5227 mddev
->ctime
!= info
->ctime
||
5228 mddev
->level
!= info
->level
||
5229 /* mddev->layout != info->layout || */
5230 !mddev
->persistent
!= info
->not_persistent
||
5231 mddev
->chunk_sectors
!= info
->chunk_size
>> 9 ||
5232 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5233 ((state
^info
->state
) & 0xfffffe00)
5236 /* Check there is only one change */
5237 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
5239 if (mddev
->raid_disks
!= info
->raid_disks
)
5241 if (mddev
->layout
!= info
->layout
)
5243 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
))
5250 if (mddev
->layout
!= info
->layout
) {
5252 * we don't need to do anything at the md level, the
5253 * personality will take care of it all.
5255 if (mddev
->pers
->check_reshape
== NULL
)
5258 mddev
->new_layout
= info
->layout
;
5259 rv
= mddev
->pers
->check_reshape(mddev
);
5261 mddev
->new_layout
= mddev
->layout
;
5265 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
5266 rv
= update_size(mddev
, (sector_t
)info
->size
* 2);
5268 if (mddev
->raid_disks
!= info
->raid_disks
)
5269 rv
= update_raid_disks(mddev
, info
->raid_disks
);
5271 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) {
5272 if (mddev
->pers
->quiesce
== NULL
)
5274 if (mddev
->recovery
|| mddev
->sync_thread
)
5276 if (info
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
5277 /* add the bitmap */
5280 if (mddev
->default_bitmap_offset
== 0)
5282 mddev
->bitmap_offset
= mddev
->default_bitmap_offset
;
5283 mddev
->pers
->quiesce(mddev
, 1);
5284 rv
= bitmap_create(mddev
);
5286 bitmap_destroy(mddev
);
5287 mddev
->pers
->quiesce(mddev
, 0);
5289 /* remove the bitmap */
5292 if (mddev
->bitmap
->file
)
5294 mddev
->pers
->quiesce(mddev
, 1);
5295 bitmap_destroy(mddev
);
5296 mddev
->pers
->quiesce(mddev
, 0);
5297 mddev
->bitmap_offset
= 0;
5300 md_update_sb(mddev
, 1);
5304 static int set_disk_faulty(mddev_t
*mddev
, dev_t dev
)
5308 if (mddev
->pers
== NULL
)
5311 rdev
= find_rdev(mddev
, dev
);
5315 md_error(mddev
, rdev
);
5320 * We have a problem here : there is no easy way to give a CHS
5321 * virtual geometry. We currently pretend that we have a 2 heads
5322 * 4 sectors (with a BIG number of cylinders...). This drives
5323 * dosfs just mad... ;-)
5325 static int md_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
5327 mddev_t
*mddev
= bdev
->bd_disk
->private_data
;
5331 geo
->cylinders
= get_capacity(mddev
->gendisk
) / 8;
5335 static int md_ioctl(struct block_device
*bdev
, fmode_t mode
,
5336 unsigned int cmd
, unsigned long arg
)
5339 void __user
*argp
= (void __user
*)arg
;
5340 mddev_t
*mddev
= NULL
;
5343 if (!capable(CAP_SYS_ADMIN
))
5347 * Commands dealing with the RAID driver but not any
5353 err
= get_version(argp
);
5356 case PRINT_RAID_DEBUG
:
5364 autostart_arrays(arg
);
5371 * Commands creating/starting a new array:
5374 mddev
= bdev
->bd_disk
->private_data
;
5381 err
= mddev_lock(mddev
);
5384 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5391 case SET_ARRAY_INFO
:
5393 mdu_array_info_t info
;
5395 memset(&info
, 0, sizeof(info
));
5396 else if (copy_from_user(&info
, argp
, sizeof(info
))) {
5401 err
= update_array_info(mddev
, &info
);
5403 printk(KERN_WARNING
"md: couldn't update"
5404 " array info. %d\n", err
);
5409 if (!list_empty(&mddev
->disks
)) {
5411 "md: array %s already has disks!\n",
5416 if (mddev
->raid_disks
) {
5418 "md: array %s already initialised!\n",
5423 err
= set_array_info(mddev
, &info
);
5425 printk(KERN_WARNING
"md: couldn't set"
5426 " array info. %d\n", err
);
5436 * Commands querying/configuring an existing array:
5438 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
5439 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
5440 if ((!mddev
->raid_disks
&& !mddev
->external
)
5441 && cmd
!= ADD_NEW_DISK
&& cmd
!= STOP_ARRAY
5442 && cmd
!= RUN_ARRAY
&& cmd
!= SET_BITMAP_FILE
5443 && cmd
!= GET_BITMAP_FILE
) {
5449 * Commands even a read-only array can execute:
5453 case GET_ARRAY_INFO
:
5454 err
= get_array_info(mddev
, argp
);
5457 case GET_BITMAP_FILE
:
5458 err
= get_bitmap_file(mddev
, argp
);
5462 err
= get_disk_info(mddev
, argp
);
5465 case RESTART_ARRAY_RW
:
5466 err
= restart_array(mddev
);
5470 err
= do_md_stop(mddev
, 0, 1);
5474 err
= do_md_stop(mddev
, 1, 1);
5478 if (get_user(ro
, (int __user
*)(arg
))) {
5484 /* if the bdev is going readonly the value of mddev->ro
5485 * does not matter, no writes are coming
5490 /* are we are already prepared for writes? */
5494 /* transitioning to readauto need only happen for
5495 * arrays that call md_write_start
5498 err
= restart_array(mddev
);
5501 set_disk_ro(mddev
->gendisk
, 0);
5508 * The remaining ioctls are changing the state of the
5509 * superblock, so we do not allow them on read-only arrays.
5510 * However non-MD ioctls (e.g. get-size) will still come through
5511 * here and hit the 'default' below, so only disallow
5512 * 'md' ioctls, and switch to rw mode if started auto-readonly.
5514 if (_IOC_TYPE(cmd
) == MD_MAJOR
&& mddev
->ro
&& mddev
->pers
) {
5515 if (mddev
->ro
== 2) {
5517 sysfs_notify_dirent(mddev
->sysfs_state
);
5518 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5519 md_wakeup_thread(mddev
->thread
);
5530 mdu_disk_info_t info
;
5531 if (copy_from_user(&info
, argp
, sizeof(info
)))
5534 err
= add_new_disk(mddev
, &info
);
5538 case HOT_REMOVE_DISK
:
5539 err
= hot_remove_disk(mddev
, new_decode_dev(arg
));
5543 err
= hot_add_disk(mddev
, new_decode_dev(arg
));
5546 case SET_DISK_FAULTY
:
5547 err
= set_disk_faulty(mddev
, new_decode_dev(arg
));
5551 err
= do_md_run(mddev
);
5554 case SET_BITMAP_FILE
:
5555 err
= set_bitmap_file(mddev
, (int)arg
);
5565 if (mddev
->hold_active
== UNTIL_IOCTL
&&
5567 mddev
->hold_active
= 0;
5568 mddev_unlock(mddev
);
5578 static int md_open(struct block_device
*bdev
, fmode_t mode
)
5581 * Succeed if we can lock the mddev, which confirms that
5582 * it isn't being stopped right now.
5584 mddev_t
*mddev
= mddev_find(bdev
->bd_dev
);
5587 if (mddev
->gendisk
!= bdev
->bd_disk
) {
5588 /* we are racing with mddev_put which is discarding this
5592 /* Wait until bdev->bd_disk is definitely gone */
5593 flush_scheduled_work();
5594 /* Then retry the open from the top */
5595 return -ERESTARTSYS
;
5597 BUG_ON(mddev
!= bdev
->bd_disk
->private_data
);
5599 if ((err
= mutex_lock_interruptible(&mddev
->open_mutex
)))
5603 atomic_inc(&mddev
->openers
);
5604 mutex_unlock(&mddev
->open_mutex
);
5606 check_disk_change(bdev
);
5611 static int md_release(struct gendisk
*disk
, fmode_t mode
)
5613 mddev_t
*mddev
= disk
->private_data
;
5616 atomic_dec(&mddev
->openers
);
5622 static int md_media_changed(struct gendisk
*disk
)
5624 mddev_t
*mddev
= disk
->private_data
;
5626 return mddev
->changed
;
5629 static int md_revalidate(struct gendisk
*disk
)
5631 mddev_t
*mddev
= disk
->private_data
;
5636 static const struct block_device_operations md_fops
=
5638 .owner
= THIS_MODULE
,
5640 .release
= md_release
,
5642 .getgeo
= md_getgeo
,
5643 .media_changed
= md_media_changed
,
5644 .revalidate_disk
= md_revalidate
,
5647 static int md_thread(void * arg
)
5649 mdk_thread_t
*thread
= arg
;
5652 * md_thread is a 'system-thread', it's priority should be very
5653 * high. We avoid resource deadlocks individually in each
5654 * raid personality. (RAID5 does preallocation) We also use RR and
5655 * the very same RT priority as kswapd, thus we will never get
5656 * into a priority inversion deadlock.
5658 * we definitely have to have equal or higher priority than
5659 * bdflush, otherwise bdflush will deadlock if there are too
5660 * many dirty RAID5 blocks.
5663 allow_signal(SIGKILL
);
5664 while (!kthread_should_stop()) {
5666 /* We need to wait INTERRUPTIBLE so that
5667 * we don't add to the load-average.
5668 * That means we need to be sure no signals are
5671 if (signal_pending(current
))
5672 flush_signals(current
);
5674 wait_event_interruptible_timeout
5676 test_bit(THREAD_WAKEUP
, &thread
->flags
)
5677 || kthread_should_stop(),
5680 clear_bit(THREAD_WAKEUP
, &thread
->flags
);
5682 thread
->run(thread
->mddev
);
5688 void md_wakeup_thread(mdk_thread_t
*thread
)
5691 dprintk("md: waking up MD thread %s.\n", thread
->tsk
->comm
);
5692 set_bit(THREAD_WAKEUP
, &thread
->flags
);
5693 wake_up(&thread
->wqueue
);
5697 mdk_thread_t
*md_register_thread(void (*run
) (mddev_t
*), mddev_t
*mddev
,
5700 mdk_thread_t
*thread
;
5702 thread
= kzalloc(sizeof(mdk_thread_t
), GFP_KERNEL
);
5706 init_waitqueue_head(&thread
->wqueue
);
5709 thread
->mddev
= mddev
;
5710 thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
5711 thread
->tsk
= kthread_run(md_thread
, thread
,
5713 mdname(thread
->mddev
),
5714 name
?: mddev
->pers
->name
);
5715 if (IS_ERR(thread
->tsk
)) {
5722 void md_unregister_thread(mdk_thread_t
*thread
)
5726 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread
->tsk
));
5728 kthread_stop(thread
->tsk
);
5732 void md_error(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
5739 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
5742 if (mddev
->external
)
5743 set_bit(Blocked
, &rdev
->flags
);
5745 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5747 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5748 __builtin_return_address(0),__builtin_return_address(1),
5749 __builtin_return_address(2),__builtin_return_address(3));
5753 if (!mddev
->pers
->error_handler
)
5755 mddev
->pers
->error_handler(mddev
,rdev
);
5756 if (mddev
->degraded
)
5757 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
5758 set_bit(StateChanged
, &rdev
->flags
);
5759 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5760 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5761 md_wakeup_thread(mddev
->thread
);
5762 md_new_event_inintr(mddev
);
5765 /* seq_file implementation /proc/mdstat */
5767 static void status_unused(struct seq_file
*seq
)
5772 seq_printf(seq
, "unused devices: ");
5774 list_for_each_entry(rdev
, &pending_raid_disks
, same_set
) {
5775 char b
[BDEVNAME_SIZE
];
5777 seq_printf(seq
, "%s ",
5778 bdevname(rdev
->bdev
,b
));
5781 seq_printf(seq
, "<none>");
5783 seq_printf(seq
, "\n");
5787 static void status_resync(struct seq_file
*seq
, mddev_t
* mddev
)
5789 sector_t max_sectors
, resync
, res
;
5790 unsigned long dt
, db
;
5793 unsigned int per_milli
;
5795 resync
= mddev
->curr_resync
- atomic_read(&mddev
->recovery_active
);
5797 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
5798 max_sectors
= mddev
->resync_max_sectors
;
5800 max_sectors
= mddev
->dev_sectors
;
5803 * Should not happen.
5809 /* Pick 'scale' such that (resync>>scale)*1000 will fit
5810 * in a sector_t, and (max_sectors>>scale) will fit in a
5811 * u32, as those are the requirements for sector_div.
5812 * Thus 'scale' must be at least 10
5815 if (sizeof(sector_t
) > sizeof(unsigned long)) {
5816 while ( max_sectors
/2 > (1ULL<<(scale
+32)))
5819 res
= (resync
>>scale
)*1000;
5820 sector_div(res
, (u32
)((max_sectors
>>scale
)+1));
5824 int i
, x
= per_milli
/50, y
= 20-x
;
5825 seq_printf(seq
, "[");
5826 for (i
= 0; i
< x
; i
++)
5827 seq_printf(seq
, "=");
5828 seq_printf(seq
, ">");
5829 for (i
= 0; i
< y
; i
++)
5830 seq_printf(seq
, ".");
5831 seq_printf(seq
, "] ");
5833 seq_printf(seq
, " %s =%3u.%u%% (%llu/%llu)",
5834 (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)?
5836 (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)?
5838 (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ?
5839 "resync" : "recovery"))),
5840 per_milli
/10, per_milli
% 10,
5841 (unsigned long long) resync
/2,
5842 (unsigned long long) max_sectors
/2);
5845 * dt: time from mark until now
5846 * db: blocks written from mark until now
5847 * rt: remaining time
5849 * rt is a sector_t, so could be 32bit or 64bit.
5850 * So we divide before multiply in case it is 32bit and close
5852 * We scale the divisor (db) by 32 to avoid loosing precision
5853 * near the end of resync when the number of remaining sectors
5855 * We then divide rt by 32 after multiplying by db to compensate.
5856 * The '+1' avoids division by zero if db is very small.
5858 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
5860 db
= (mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
))
5861 - mddev
->resync_mark_cnt
;
5863 rt
= max_sectors
- resync
; /* number of remaining sectors */
5864 sector_div(rt
, db
/32+1);
5868 seq_printf(seq
, " finish=%lu.%lumin", (unsigned long)rt
/ 60,
5869 ((unsigned long)rt
% 60)/6);
5871 seq_printf(seq
, " speed=%ldK/sec", db
/2/dt
);
5874 static void *md_seq_start(struct seq_file
*seq
, loff_t
*pos
)
5876 struct list_head
*tmp
;
5886 spin_lock(&all_mddevs_lock
);
5887 list_for_each(tmp
,&all_mddevs
)
5889 mddev
= list_entry(tmp
, mddev_t
, all_mddevs
);
5891 spin_unlock(&all_mddevs_lock
);
5894 spin_unlock(&all_mddevs_lock
);
5896 return (void*)2;/* tail */
5900 static void *md_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
5902 struct list_head
*tmp
;
5903 mddev_t
*next_mddev
, *mddev
= v
;
5909 spin_lock(&all_mddevs_lock
);
5911 tmp
= all_mddevs
.next
;
5913 tmp
= mddev
->all_mddevs
.next
;
5914 if (tmp
!= &all_mddevs
)
5915 next_mddev
= mddev_get(list_entry(tmp
,mddev_t
,all_mddevs
));
5917 next_mddev
= (void*)2;
5920 spin_unlock(&all_mddevs_lock
);
5928 static void md_seq_stop(struct seq_file
*seq
, void *v
)
5932 if (mddev
&& v
!= (void*)1 && v
!= (void*)2)
5936 struct mdstat_info
{
5940 static int md_seq_show(struct seq_file
*seq
, void *v
)
5945 struct mdstat_info
*mi
= seq
->private;
5946 struct bitmap
*bitmap
;
5948 if (v
== (void*)1) {
5949 struct mdk_personality
*pers
;
5950 seq_printf(seq
, "Personalities : ");
5951 spin_lock(&pers_lock
);
5952 list_for_each_entry(pers
, &pers_list
, list
)
5953 seq_printf(seq
, "[%s] ", pers
->name
);
5955 spin_unlock(&pers_lock
);
5956 seq_printf(seq
, "\n");
5957 mi
->event
= atomic_read(&md_event_count
);
5960 if (v
== (void*)2) {
5965 if (mddev_lock(mddev
) < 0)
5968 if (mddev
->pers
|| mddev
->raid_disks
|| !list_empty(&mddev
->disks
)) {
5969 seq_printf(seq
, "%s : %sactive", mdname(mddev
),
5970 mddev
->pers
? "" : "in");
5973 seq_printf(seq
, " (read-only)");
5975 seq_printf(seq
, " (auto-read-only)");
5976 seq_printf(seq
, " %s", mddev
->pers
->name
);
5980 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
5981 char b
[BDEVNAME_SIZE
];
5982 seq_printf(seq
, " %s[%d]",
5983 bdevname(rdev
->bdev
,b
), rdev
->desc_nr
);
5984 if (test_bit(WriteMostly
, &rdev
->flags
))
5985 seq_printf(seq
, "(W)");
5986 if (test_bit(Faulty
, &rdev
->flags
)) {
5987 seq_printf(seq
, "(F)");
5989 } else if (rdev
->raid_disk
< 0)
5990 seq_printf(seq
, "(S)"); /* spare */
5991 sectors
+= rdev
->sectors
;
5994 if (!list_empty(&mddev
->disks
)) {
5996 seq_printf(seq
, "\n %llu blocks",
5997 (unsigned long long)
5998 mddev
->array_sectors
/ 2);
6000 seq_printf(seq
, "\n %llu blocks",
6001 (unsigned long long)sectors
/ 2);
6003 if (mddev
->persistent
) {
6004 if (mddev
->major_version
!= 0 ||
6005 mddev
->minor_version
!= 90) {
6006 seq_printf(seq
," super %d.%d",
6007 mddev
->major_version
,
6008 mddev
->minor_version
);
6010 } else if (mddev
->external
)
6011 seq_printf(seq
, " super external:%s",
6012 mddev
->metadata_type
);
6014 seq_printf(seq
, " super non-persistent");
6017 mddev
->pers
->status(seq
, mddev
);
6018 seq_printf(seq
, "\n ");
6019 if (mddev
->pers
->sync_request
) {
6020 if (mddev
->curr_resync
> 2) {
6021 status_resync(seq
, mddev
);
6022 seq_printf(seq
, "\n ");
6023 } else if (mddev
->curr_resync
== 1 || mddev
->curr_resync
== 2)
6024 seq_printf(seq
, "\tresync=DELAYED\n ");
6025 else if (mddev
->recovery_cp
< MaxSector
)
6026 seq_printf(seq
, "\tresync=PENDING\n ");
6029 seq_printf(seq
, "\n ");
6031 if ((bitmap
= mddev
->bitmap
)) {
6032 unsigned long chunk_kb
;
6033 unsigned long flags
;
6034 spin_lock_irqsave(&bitmap
->lock
, flags
);
6035 chunk_kb
= bitmap
->chunksize
>> 10;
6036 seq_printf(seq
, "bitmap: %lu/%lu pages [%luKB], "
6038 bitmap
->pages
- bitmap
->missing_pages
,
6040 (bitmap
->pages
- bitmap
->missing_pages
)
6041 << (PAGE_SHIFT
- 10),
6042 chunk_kb
? chunk_kb
: bitmap
->chunksize
,
6043 chunk_kb
? "KB" : "B");
6045 seq_printf(seq
, ", file: ");
6046 seq_path(seq
, &bitmap
->file
->f_path
, " \t\n");
6049 seq_printf(seq
, "\n");
6050 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
6053 seq_printf(seq
, "\n");
6055 mddev_unlock(mddev
);
6060 static const struct seq_operations md_seq_ops
= {
6061 .start
= md_seq_start
,
6062 .next
= md_seq_next
,
6063 .stop
= md_seq_stop
,
6064 .show
= md_seq_show
,
6067 static int md_seq_open(struct inode
*inode
, struct file
*file
)
6070 struct mdstat_info
*mi
= kmalloc(sizeof(*mi
), GFP_KERNEL
);
6074 error
= seq_open(file
, &md_seq_ops
);
6078 struct seq_file
*p
= file
->private_data
;
6080 mi
->event
= atomic_read(&md_event_count
);
6085 static unsigned int mdstat_poll(struct file
*filp
, poll_table
*wait
)
6087 struct seq_file
*m
= filp
->private_data
;
6088 struct mdstat_info
*mi
= m
->private;
6091 poll_wait(filp
, &md_event_waiters
, wait
);
6093 /* always allow read */
6094 mask
= POLLIN
| POLLRDNORM
;
6096 if (mi
->event
!= atomic_read(&md_event_count
))
6097 mask
|= POLLERR
| POLLPRI
;
6101 static const struct file_operations md_seq_fops
= {
6102 .owner
= THIS_MODULE
,
6103 .open
= md_seq_open
,
6105 .llseek
= seq_lseek
,
6106 .release
= seq_release_private
,
6107 .poll
= mdstat_poll
,
6110 int register_md_personality(struct mdk_personality
*p
)
6112 spin_lock(&pers_lock
);
6113 list_add_tail(&p
->list
, &pers_list
);
6114 printk(KERN_INFO
"md: %s personality registered for level %d\n", p
->name
, p
->level
);
6115 spin_unlock(&pers_lock
);
6119 int unregister_md_personality(struct mdk_personality
*p
)
6121 printk(KERN_INFO
"md: %s personality unregistered\n", p
->name
);
6122 spin_lock(&pers_lock
);
6123 list_del_init(&p
->list
);
6124 spin_unlock(&pers_lock
);
6128 static int is_mddev_idle(mddev_t
*mddev
, int init
)
6136 rdev_for_each_rcu(rdev
, mddev
) {
6137 struct gendisk
*disk
= rdev
->bdev
->bd_contains
->bd_disk
;
6138 curr_events
= (int)part_stat_read(&disk
->part0
, sectors
[0]) +
6139 (int)part_stat_read(&disk
->part0
, sectors
[1]) -
6140 atomic_read(&disk
->sync_io
);
6141 /* sync IO will cause sync_io to increase before the disk_stats
6142 * as sync_io is counted when a request starts, and
6143 * disk_stats is counted when it completes.
6144 * So resync activity will cause curr_events to be smaller than
6145 * when there was no such activity.
6146 * non-sync IO will cause disk_stat to increase without
6147 * increasing sync_io so curr_events will (eventually)
6148 * be larger than it was before. Once it becomes
6149 * substantially larger, the test below will cause
6150 * the array to appear non-idle, and resync will slow
6152 * If there is a lot of outstanding resync activity when
6153 * we set last_event to curr_events, then all that activity
6154 * completing might cause the array to appear non-idle
6155 * and resync will be slowed down even though there might
6156 * not have been non-resync activity. This will only
6157 * happen once though. 'last_events' will soon reflect
6158 * the state where there is little or no outstanding
6159 * resync requests, and further resync activity will
6160 * always make curr_events less than last_events.
6163 if (init
|| curr_events
- rdev
->last_events
> 64) {
6164 rdev
->last_events
= curr_events
;
6172 void md_done_sync(mddev_t
*mddev
, int blocks
, int ok
)
6174 /* another "blocks" (512byte) blocks have been synced */
6175 atomic_sub(blocks
, &mddev
->recovery_active
);
6176 wake_up(&mddev
->recovery_wait
);
6178 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6179 md_wakeup_thread(mddev
->thread
);
6180 // stop recovery, signal do_sync ....
6185 /* md_write_start(mddev, bi)
6186 * If we need to update some array metadata (e.g. 'active' flag
6187 * in superblock) before writing, schedule a superblock update
6188 * and wait for it to complete.
6190 void md_write_start(mddev_t
*mddev
, struct bio
*bi
)
6193 if (bio_data_dir(bi
) != WRITE
)
6196 BUG_ON(mddev
->ro
== 1);
6197 if (mddev
->ro
== 2) {
6198 /* need to switch to read/write */
6200 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6201 md_wakeup_thread(mddev
->thread
);
6202 md_wakeup_thread(mddev
->sync_thread
);
6205 atomic_inc(&mddev
->writes_pending
);
6206 if (mddev
->safemode
== 1)
6207 mddev
->safemode
= 0;
6208 if (mddev
->in_sync
) {
6209 spin_lock_irq(&mddev
->write_lock
);
6210 if (mddev
->in_sync
) {
6212 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6213 md_wakeup_thread(mddev
->thread
);
6216 spin_unlock_irq(&mddev
->write_lock
);
6219 sysfs_notify_dirent(mddev
->sysfs_state
);
6220 wait_event(mddev
->sb_wait
,
6221 !test_bit(MD_CHANGE_CLEAN
, &mddev
->flags
) &&
6222 !test_bit(MD_CHANGE_PENDING
, &mddev
->flags
));
6225 void md_write_end(mddev_t
*mddev
)
6227 if (atomic_dec_and_test(&mddev
->writes_pending
)) {
6228 if (mddev
->safemode
== 2)
6229 md_wakeup_thread(mddev
->thread
);
6230 else if (mddev
->safemode_delay
)
6231 mod_timer(&mddev
->safemode_timer
, jiffies
+ mddev
->safemode_delay
);
6235 /* md_allow_write(mddev)
6236 * Calling this ensures that the array is marked 'active' so that writes
6237 * may proceed without blocking. It is important to call this before
6238 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6239 * Must be called with mddev_lock held.
6241 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6242 * is dropped, so return -EAGAIN after notifying userspace.
6244 int md_allow_write(mddev_t
*mddev
)
6250 if (!mddev
->pers
->sync_request
)
6253 spin_lock_irq(&mddev
->write_lock
);
6254 if (mddev
->in_sync
) {
6256 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6257 if (mddev
->safemode_delay
&&
6258 mddev
->safemode
== 0)
6259 mddev
->safemode
= 1;
6260 spin_unlock_irq(&mddev
->write_lock
);
6261 md_update_sb(mddev
, 0);
6262 sysfs_notify_dirent(mddev
->sysfs_state
);
6264 spin_unlock_irq(&mddev
->write_lock
);
6266 if (test_bit(MD_CHANGE_CLEAN
, &mddev
->flags
))
6271 EXPORT_SYMBOL_GPL(md_allow_write
);
6273 #define SYNC_MARKS 10
6274 #define SYNC_MARK_STEP (3*HZ)
6275 void md_do_sync(mddev_t
*mddev
)
6278 unsigned int currspeed
= 0,
6280 sector_t max_sectors
,j
, io_sectors
;
6281 unsigned long mark
[SYNC_MARKS
];
6282 sector_t mark_cnt
[SYNC_MARKS
];
6284 struct list_head
*tmp
;
6285 sector_t last_check
;
6290 /* just incase thread restarts... */
6291 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
6293 if (mddev
->ro
) /* never try to sync a read-only array */
6296 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
6297 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
6298 desc
= "data-check";
6299 else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
6300 desc
= "requested-resync";
6303 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
6308 /* we overload curr_resync somewhat here.
6309 * 0 == not engaged in resync at all
6310 * 2 == checking that there is no conflict with another sync
6311 * 1 == like 2, but have yielded to allow conflicting resync to
6313 * other == active in resync - this many blocks
6315 * Before starting a resync we must have set curr_resync to
6316 * 2, and then checked that every "conflicting" array has curr_resync
6317 * less than ours. When we find one that is the same or higher
6318 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6319 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6320 * This will mean we have to start checking from the beginning again.
6325 mddev
->curr_resync
= 2;
6328 if (kthread_should_stop()) {
6329 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6332 for_each_mddev(mddev2
, tmp
) {
6333 if (mddev2
== mddev
)
6335 if (!mddev
->parallel_resync
6336 && mddev2
->curr_resync
6337 && match_mddev_units(mddev
, mddev2
)) {
6339 if (mddev
< mddev2
&& mddev
->curr_resync
== 2) {
6340 /* arbitrarily yield */
6341 mddev
->curr_resync
= 1;
6342 wake_up(&resync_wait
);
6344 if (mddev
> mddev2
&& mddev
->curr_resync
== 1)
6345 /* no need to wait here, we can wait the next
6346 * time 'round when curr_resync == 2
6349 /* We need to wait 'interruptible' so as not to
6350 * contribute to the load average, and not to
6351 * be caught by 'softlockup'
6353 prepare_to_wait(&resync_wait
, &wq
, TASK_INTERRUPTIBLE
);
6354 if (!kthread_should_stop() &&
6355 mddev2
->curr_resync
>= mddev
->curr_resync
) {
6356 printk(KERN_INFO
"md: delaying %s of %s"
6357 " until %s has finished (they"
6358 " share one or more physical units)\n",
6359 desc
, mdname(mddev
), mdname(mddev2
));
6361 if (signal_pending(current
))
6362 flush_signals(current
);
6364 finish_wait(&resync_wait
, &wq
);
6367 finish_wait(&resync_wait
, &wq
);
6370 } while (mddev
->curr_resync
< 2);
6373 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
6374 /* resync follows the size requested by the personality,
6375 * which defaults to physical size, but can be virtual size
6377 max_sectors
= mddev
->resync_max_sectors
;
6378 mddev
->resync_mismatches
= 0;
6379 /* we don't use the checkpoint if there's a bitmap */
6380 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
6381 j
= mddev
->resync_min
;
6382 else if (!mddev
->bitmap
)
6383 j
= mddev
->recovery_cp
;
6385 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
6386 max_sectors
= mddev
->dev_sectors
;
6388 /* recovery follows the physical size of devices */
6389 max_sectors
= mddev
->dev_sectors
;
6391 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
6392 if (rdev
->raid_disk
>= 0 &&
6393 !test_bit(Faulty
, &rdev
->flags
) &&
6394 !test_bit(In_sync
, &rdev
->flags
) &&
6395 rdev
->recovery_offset
< j
)
6396 j
= rdev
->recovery_offset
;
6399 printk(KERN_INFO
"md: %s of RAID array %s\n", desc
, mdname(mddev
));
6400 printk(KERN_INFO
"md: minimum _guaranteed_ speed:"
6401 " %d KB/sec/disk.\n", speed_min(mddev
));
6402 printk(KERN_INFO
"md: using maximum available idle IO bandwidth "
6403 "(but not more than %d KB/sec) for %s.\n",
6404 speed_max(mddev
), desc
);
6406 is_mddev_idle(mddev
, 1); /* this initializes IO event counters */
6409 for (m
= 0; m
< SYNC_MARKS
; m
++) {
6411 mark_cnt
[m
] = io_sectors
;
6414 mddev
->resync_mark
= mark
[last_mark
];
6415 mddev
->resync_mark_cnt
= mark_cnt
[last_mark
];
6418 * Tune reconstruction:
6420 window
= 32*(PAGE_SIZE
/512);
6421 printk(KERN_INFO
"md: using %dk window, over a total of %llu blocks.\n",
6422 window
/2,(unsigned long long) max_sectors
/2);
6424 atomic_set(&mddev
->recovery_active
, 0);
6429 "md: resuming %s of %s from checkpoint.\n",
6430 desc
, mdname(mddev
));
6431 mddev
->curr_resync
= j
;
6434 while (j
< max_sectors
) {
6439 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
6440 ((mddev
->curr_resync
> mddev
->curr_resync_completed
&&
6441 (mddev
->curr_resync
- mddev
->curr_resync_completed
)
6442 > (max_sectors
>> 4)) ||
6443 (j
- mddev
->curr_resync_completed
)*2
6444 >= mddev
->resync_max
- mddev
->curr_resync_completed
6446 /* time to update curr_resync_completed */
6447 blk_unplug(mddev
->queue
);
6448 wait_event(mddev
->recovery_wait
,
6449 atomic_read(&mddev
->recovery_active
) == 0);
6450 mddev
->curr_resync_completed
=
6452 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6453 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
6456 while (j
>= mddev
->resync_max
&& !kthread_should_stop()) {
6457 /* As this condition is controlled by user-space,
6458 * we can block indefinitely, so use '_interruptible'
6459 * to avoid triggering warnings.
6461 flush_signals(current
); /* just in case */
6462 wait_event_interruptible(mddev
->recovery_wait
,
6463 mddev
->resync_max
> j
6464 || kthread_should_stop());
6467 if (kthread_should_stop())
6470 sectors
= mddev
->pers
->sync_request(mddev
, j
, &skipped
,
6471 currspeed
< speed_min(mddev
));
6473 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6477 if (!skipped
) { /* actual IO requested */
6478 io_sectors
+= sectors
;
6479 atomic_add(sectors
, &mddev
->recovery_active
);
6483 if (j
>1) mddev
->curr_resync
= j
;
6484 mddev
->curr_mark_cnt
= io_sectors
;
6485 if (last_check
== 0)
6486 /* this is the earliers that rebuilt will be
6487 * visible in /proc/mdstat
6489 md_new_event(mddev
);
6491 if (last_check
+ window
> io_sectors
|| j
== max_sectors
)
6494 last_check
= io_sectors
;
6496 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
6500 if (time_after_eq(jiffies
, mark
[last_mark
] + SYNC_MARK_STEP
)) {
6502 int next
= (last_mark
+1) % SYNC_MARKS
;
6504 mddev
->resync_mark
= mark
[next
];
6505 mddev
->resync_mark_cnt
= mark_cnt
[next
];
6506 mark
[next
] = jiffies
;
6507 mark_cnt
[next
] = io_sectors
- atomic_read(&mddev
->recovery_active
);
6512 if (kthread_should_stop())
6517 * this loop exits only if either when we are slower than
6518 * the 'hard' speed limit, or the system was IO-idle for
6520 * the system might be non-idle CPU-wise, but we only care
6521 * about not overloading the IO subsystem. (things like an
6522 * e2fsck being done on the RAID array should execute fast)
6524 blk_unplug(mddev
->queue
);
6527 currspeed
= ((unsigned long)(io_sectors
-mddev
->resync_mark_cnt
))/2
6528 /((jiffies
-mddev
->resync_mark
)/HZ
+1) +1;
6530 if (currspeed
> speed_min(mddev
)) {
6531 if ((currspeed
> speed_max(mddev
)) ||
6532 !is_mddev_idle(mddev
, 0)) {
6538 printk(KERN_INFO
"md: %s: %s done.\n",mdname(mddev
), desc
);
6540 * this also signals 'finished resyncing' to md_stop
6543 blk_unplug(mddev
->queue
);
6545 wait_event(mddev
->recovery_wait
, !atomic_read(&mddev
->recovery_active
));
6547 /* tell personality that we are finished */
6548 mddev
->pers
->sync_request(mddev
, max_sectors
, &skipped
, 1);
6550 if (!test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
) &&
6551 mddev
->curr_resync
> 2) {
6552 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
6553 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
6554 if (mddev
->curr_resync
>= mddev
->recovery_cp
) {
6556 "md: checkpointing %s of %s.\n",
6557 desc
, mdname(mddev
));
6558 mddev
->recovery_cp
= mddev
->curr_resync
;
6561 mddev
->recovery_cp
= MaxSector
;
6563 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
6564 mddev
->curr_resync
= MaxSector
;
6565 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
6566 if (rdev
->raid_disk
>= 0 &&
6567 !test_bit(Faulty
, &rdev
->flags
) &&
6568 !test_bit(In_sync
, &rdev
->flags
) &&
6569 rdev
->recovery_offset
< mddev
->curr_resync
)
6570 rdev
->recovery_offset
= mddev
->curr_resync
;
6573 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
6576 mddev
->curr_resync
= 0;
6577 mddev
->curr_resync_completed
= 0;
6578 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
6579 /* We completed so max setting can be forgotten. */
6580 mddev
->resync_max
= MaxSector
;
6581 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
6582 wake_up(&resync_wait
);
6583 set_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
6584 md_wakeup_thread(mddev
->thread
);
6589 * got a signal, exit.
6592 "md: md_do_sync() got signal ... exiting\n");
6593 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6597 EXPORT_SYMBOL_GPL(md_do_sync
);
6600 static int remove_and_add_spares(mddev_t
*mddev
)
6605 mddev
->curr_resync_completed
= 0;
6607 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
6608 if (rdev
->raid_disk
>= 0 &&
6609 !test_bit(Blocked
, &rdev
->flags
) &&
6610 (test_bit(Faulty
, &rdev
->flags
) ||
6611 ! test_bit(In_sync
, &rdev
->flags
)) &&
6612 atomic_read(&rdev
->nr_pending
)==0) {
6613 if (mddev
->pers
->hot_remove_disk(
6614 mddev
, rdev
->raid_disk
)==0) {
6616 sprintf(nm
,"rd%d", rdev
->raid_disk
);
6617 sysfs_remove_link(&mddev
->kobj
, nm
);
6618 rdev
->raid_disk
= -1;
6622 if (mddev
->degraded
&& ! mddev
->ro
&& !mddev
->recovery_disabled
) {
6623 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
6624 if (rdev
->raid_disk
>= 0 &&
6625 !test_bit(In_sync
, &rdev
->flags
) &&
6626 !test_bit(Blocked
, &rdev
->flags
))
6628 if (rdev
->raid_disk
< 0
6629 && !test_bit(Faulty
, &rdev
->flags
)) {
6630 rdev
->recovery_offset
= 0;
6632 hot_add_disk(mddev
, rdev
) == 0) {
6634 sprintf(nm
, "rd%d", rdev
->raid_disk
);
6635 if (sysfs_create_link(&mddev
->kobj
,
6638 "md: cannot register "
6642 md_new_event(mddev
);
6651 * This routine is regularly called by all per-raid-array threads to
6652 * deal with generic issues like resync and super-block update.
6653 * Raid personalities that don't have a thread (linear/raid0) do not
6654 * need this as they never do any recovery or update the superblock.
6656 * It does not do any resync itself, but rather "forks" off other threads
6657 * to do that as needed.
6658 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6659 * "->recovery" and create a thread at ->sync_thread.
6660 * When the thread finishes it sets MD_RECOVERY_DONE
6661 * and wakeups up this thread which will reap the thread and finish up.
6662 * This thread also removes any faulty devices (with nr_pending == 0).
6664 * The overall approach is:
6665 * 1/ if the superblock needs updating, update it.
6666 * 2/ If a recovery thread is running, don't do anything else.
6667 * 3/ If recovery has finished, clean up, possibly marking spares active.
6668 * 4/ If there are any faulty devices, remove them.
6669 * 5/ If array is degraded, try to add spares devices
6670 * 6/ If array has spares or is not in-sync, start a resync thread.
6672 void md_check_recovery(mddev_t
*mddev
)
6678 bitmap_daemon_work(mddev
);
6683 if (signal_pending(current
)) {
6684 if (mddev
->pers
->sync_request
&& !mddev
->external
) {
6685 printk(KERN_INFO
"md: %s in immediate safe mode\n",
6687 mddev
->safemode
= 2;
6689 flush_signals(current
);
6692 if (mddev
->ro
&& !test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
6695 (mddev
->flags
&& !mddev
->external
) ||
6696 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
6697 test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
6698 (mddev
->external
== 0 && mddev
->safemode
== 1) ||
6699 (mddev
->safemode
== 2 && ! atomic_read(&mddev
->writes_pending
)
6700 && !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
6704 if (mddev_trylock(mddev
)) {
6708 /* Only thing we do on a ro array is remove
6711 remove_and_add_spares(mddev
);
6712 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6716 if (!mddev
->external
) {
6718 spin_lock_irq(&mddev
->write_lock
);
6719 if (mddev
->safemode
&&
6720 !atomic_read(&mddev
->writes_pending
) &&
6722 mddev
->recovery_cp
== MaxSector
) {
6725 if (mddev
->persistent
)
6726 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6728 if (mddev
->safemode
== 1)
6729 mddev
->safemode
= 0;
6730 spin_unlock_irq(&mddev
->write_lock
);
6732 sysfs_notify_dirent(mddev
->sysfs_state
);
6736 md_update_sb(mddev
, 0);
6738 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
6739 if (test_and_clear_bit(StateChanged
, &rdev
->flags
))
6740 sysfs_notify_dirent(rdev
->sysfs_state
);
6743 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
6744 !test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
)) {
6745 /* resync/recovery still happening */
6746 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6749 if (mddev
->sync_thread
) {
6750 /* resync has finished, collect result */
6751 md_unregister_thread(mddev
->sync_thread
);
6752 mddev
->sync_thread
= NULL
;
6753 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
6754 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
6756 /* activate any spares */
6757 if (mddev
->pers
->spare_active(mddev
))
6758 sysfs_notify(&mddev
->kobj
, NULL
,
6761 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
6762 mddev
->pers
->finish_reshape
)
6763 mddev
->pers
->finish_reshape(mddev
);
6764 md_update_sb(mddev
, 1);
6766 /* if array is no-longer degraded, then any saved_raid_disk
6767 * information must be scrapped
6769 if (!mddev
->degraded
)
6770 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
6771 rdev
->saved_raid_disk
= -1;
6773 mddev
->recovery
= 0;
6774 /* flag recovery needed just to double check */
6775 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6776 sysfs_notify_dirent(mddev
->sysfs_action
);
6777 md_new_event(mddev
);
6780 /* Set RUNNING before clearing NEEDED to avoid
6781 * any transients in the value of "sync_action".
6783 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
6784 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6785 /* Clear some bits that don't mean anything, but
6788 clear_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6789 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
6791 if (test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
6793 /* no recovery is running.
6794 * remove any failed drives, then
6795 * add spares if possible.
6796 * Spare are also removed and re-added, to allow
6797 * the personality to fail the re-add.
6800 if (mddev
->reshape_position
!= MaxSector
) {
6801 if (mddev
->pers
->check_reshape
== NULL
||
6802 mddev
->pers
->check_reshape(mddev
) != 0)
6803 /* Cannot proceed */
6805 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
6806 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
6807 } else if ((spares
= remove_and_add_spares(mddev
))) {
6808 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
6809 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
6810 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
6811 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
6812 } else if (mddev
->recovery_cp
< MaxSector
) {
6813 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
6814 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
6815 } else if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
6816 /* nothing to be done ... */
6819 if (mddev
->pers
->sync_request
) {
6820 if (spares
&& mddev
->bitmap
&& ! mddev
->bitmap
->file
) {
6821 /* We are adding a device or devices to an array
6822 * which has the bitmap stored on all devices.
6823 * So make sure all bitmap pages get written
6825 bitmap_write_all(mddev
->bitmap
);
6827 mddev
->sync_thread
= md_register_thread(md_do_sync
,
6830 if (!mddev
->sync_thread
) {
6831 printk(KERN_ERR
"%s: could not start resync"
6834 /* leave the spares where they are, it shouldn't hurt */
6835 mddev
->recovery
= 0;
6837 md_wakeup_thread(mddev
->sync_thread
);
6838 sysfs_notify_dirent(mddev
->sysfs_action
);
6839 md_new_event(mddev
);
6842 if (!mddev
->sync_thread
) {
6843 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
6844 if (test_and_clear_bit(MD_RECOVERY_RECOVER
,
6846 if (mddev
->sysfs_action
)
6847 sysfs_notify_dirent(mddev
->sysfs_action
);
6849 mddev_unlock(mddev
);
6853 void md_wait_for_blocked_rdev(mdk_rdev_t
*rdev
, mddev_t
*mddev
)
6855 sysfs_notify_dirent(rdev
->sysfs_state
);
6856 wait_event_timeout(rdev
->blocked_wait
,
6857 !test_bit(Blocked
, &rdev
->flags
),
6858 msecs_to_jiffies(5000));
6859 rdev_dec_pending(rdev
, mddev
);
6861 EXPORT_SYMBOL(md_wait_for_blocked_rdev
);
6863 static int md_notify_reboot(struct notifier_block
*this,
6864 unsigned long code
, void *x
)
6866 struct list_head
*tmp
;
6869 if ((code
== SYS_DOWN
) || (code
== SYS_HALT
) || (code
== SYS_POWER_OFF
)) {
6871 printk(KERN_INFO
"md: stopping all md devices.\n");
6873 for_each_mddev(mddev
, tmp
)
6874 if (mddev_trylock(mddev
)) {
6875 /* Force a switch to readonly even array
6876 * appears to still be in use. Hence
6879 do_md_stop(mddev
, 1, 100);
6880 mddev_unlock(mddev
);
6883 * certain more exotic SCSI devices are known to be
6884 * volatile wrt too early system reboots. While the
6885 * right place to handle this issue is the given
6886 * driver, we do want to have a safe RAID driver ...
6893 static struct notifier_block md_notifier
= {
6894 .notifier_call
= md_notify_reboot
,
6896 .priority
= INT_MAX
, /* before any real devices */
6899 static void md_geninit(void)
6901 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t
));
6903 proc_create("mdstat", S_IRUGO
, NULL
, &md_seq_fops
);
6906 static int __init
md_init(void)
6908 if (register_blkdev(MD_MAJOR
, "md"))
6910 if ((mdp_major
=register_blkdev(0, "mdp"))<=0) {
6911 unregister_blkdev(MD_MAJOR
, "md");
6914 blk_register_region(MKDEV(MD_MAJOR
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
6915 md_probe
, NULL
, NULL
);
6916 blk_register_region(MKDEV(mdp_major
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
6917 md_probe
, NULL
, NULL
);
6919 register_reboot_notifier(&md_notifier
);
6920 raid_table_header
= register_sysctl_table(raid_root_table
);
6930 * Searches all registered partitions for autorun RAID arrays
6934 static LIST_HEAD(all_detected_devices
);
6935 struct detected_devices_node
{
6936 struct list_head list
;
6940 void md_autodetect_dev(dev_t dev
)
6942 struct detected_devices_node
*node_detected_dev
;
6944 node_detected_dev
= kzalloc(sizeof(*node_detected_dev
), GFP_KERNEL
);
6945 if (node_detected_dev
) {
6946 node_detected_dev
->dev
= dev
;
6947 list_add_tail(&node_detected_dev
->list
, &all_detected_devices
);
6949 printk(KERN_CRIT
"md: md_autodetect_dev: kzalloc failed"
6950 ", skipping dev(%d,%d)\n", MAJOR(dev
), MINOR(dev
));
6955 static void autostart_arrays(int part
)
6958 struct detected_devices_node
*node_detected_dev
;
6960 int i_scanned
, i_passed
;
6965 printk(KERN_INFO
"md: Autodetecting RAID arrays.\n");
6967 while (!list_empty(&all_detected_devices
) && i_scanned
< INT_MAX
) {
6969 node_detected_dev
= list_entry(all_detected_devices
.next
,
6970 struct detected_devices_node
, list
);
6971 list_del(&node_detected_dev
->list
);
6972 dev
= node_detected_dev
->dev
;
6973 kfree(node_detected_dev
);
6974 rdev
= md_import_device(dev
,0, 90);
6978 if (test_bit(Faulty
, &rdev
->flags
)) {
6982 set_bit(AutoDetected
, &rdev
->flags
);
6983 list_add(&rdev
->same_set
, &pending_raid_disks
);
6987 printk(KERN_INFO
"md: Scanned %d and added %d devices.\n",
6988 i_scanned
, i_passed
);
6990 autorun_devices(part
);
6993 #endif /* !MODULE */
6995 static __exit
void md_exit(void)
6998 struct list_head
*tmp
;
7000 blk_unregister_region(MKDEV(MD_MAJOR
,0), 1U << MINORBITS
);
7001 blk_unregister_region(MKDEV(mdp_major
,0), 1U << MINORBITS
);
7003 unregister_blkdev(MD_MAJOR
,"md");
7004 unregister_blkdev(mdp_major
, "mdp");
7005 unregister_reboot_notifier(&md_notifier
);
7006 unregister_sysctl_table(raid_table_header
);
7007 remove_proc_entry("mdstat", NULL
);
7008 for_each_mddev(mddev
, tmp
) {
7009 export_array(mddev
);
7010 mddev
->hold_active
= 0;
7014 subsys_initcall(md_init
);
7015 module_exit(md_exit
)
7017 static int get_ro(char *buffer
, struct kernel_param
*kp
)
7019 return sprintf(buffer
, "%d", start_readonly
);
7021 static int set_ro(const char *val
, struct kernel_param
*kp
)
7024 int num
= simple_strtoul(val
, &e
, 10);
7025 if (*val
&& (*e
== '\0' || *e
== '\n')) {
7026 start_readonly
= num
;
7032 module_param_call(start_ro
, set_ro
, get_ro
, NULL
, S_IRUSR
|S_IWUSR
);
7033 module_param(start_dirty_degraded
, int, S_IRUGO
|S_IWUSR
);
7035 module_param_call(new_array
, add_named_array
, NULL
, NULL
, S_IWUSR
);
7037 EXPORT_SYMBOL(register_md_personality
);
7038 EXPORT_SYMBOL(unregister_md_personality
);
7039 EXPORT_SYMBOL(md_error
);
7040 EXPORT_SYMBOL(md_done_sync
);
7041 EXPORT_SYMBOL(md_write_start
);
7042 EXPORT_SYMBOL(md_write_end
);
7043 EXPORT_SYMBOL(md_register_thread
);
7044 EXPORT_SYMBOL(md_unregister_thread
);
7045 EXPORT_SYMBOL(md_wakeup_thread
);
7046 EXPORT_SYMBOL(md_check_recovery
);
7047 MODULE_LICENSE("GPL");
7049 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR
);