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
;
309 spin_lock(&all_mddevs_lock
);
312 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
313 if (mddev
->unit
== unit
) {
315 spin_unlock(&all_mddevs_lock
);
321 list_add(&new->all_mddevs
, &all_mddevs
);
322 spin_unlock(&all_mddevs_lock
);
323 new->hold_active
= UNTIL_IOCTL
;
327 /* find an unused unit number */
328 static int next_minor
= 512;
329 int start
= next_minor
;
333 dev
= MKDEV(MD_MAJOR
, next_minor
);
335 if (next_minor
> MINORMASK
)
337 if (next_minor
== start
) {
338 /* Oh dear, all in use. */
339 spin_unlock(&all_mddevs_lock
);
345 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
346 if (mddev
->unit
== dev
) {
352 new->md_minor
= MINOR(dev
);
353 new->hold_active
= UNTIL_STOP
;
354 list_add(&new->all_mddevs
, &all_mddevs
);
355 spin_unlock(&all_mddevs_lock
);
358 spin_unlock(&all_mddevs_lock
);
360 new = kzalloc(sizeof(*new), GFP_KERNEL
);
365 if (MAJOR(unit
) == MD_MAJOR
)
366 new->md_minor
= MINOR(unit
);
368 new->md_minor
= MINOR(unit
) >> MdpMinorShift
;
370 mutex_init(&new->open_mutex
);
371 mutex_init(&new->reconfig_mutex
);
372 mutex_init(&new->bitmap_mutex
);
373 INIT_LIST_HEAD(&new->disks
);
374 INIT_LIST_HEAD(&new->all_mddevs
);
375 init_timer(&new->safemode_timer
);
376 atomic_set(&new->active
, 1);
377 atomic_set(&new->openers
, 0);
378 atomic_set(&new->active_io
, 0);
379 spin_lock_init(&new->write_lock
);
380 init_waitqueue_head(&new->sb_wait
);
381 init_waitqueue_head(&new->recovery_wait
);
382 new->reshape_position
= MaxSector
;
384 new->resync_max
= MaxSector
;
385 new->level
= LEVEL_NONE
;
390 static inline int mddev_lock(mddev_t
* mddev
)
392 return mutex_lock_interruptible(&mddev
->reconfig_mutex
);
395 static inline int mddev_is_locked(mddev_t
*mddev
)
397 return mutex_is_locked(&mddev
->reconfig_mutex
);
400 static inline int mddev_trylock(mddev_t
* mddev
)
402 return mutex_trylock(&mddev
->reconfig_mutex
);
405 static inline void mddev_unlock(mddev_t
* mddev
)
407 mutex_unlock(&mddev
->reconfig_mutex
);
409 md_wakeup_thread(mddev
->thread
);
412 static mdk_rdev_t
* find_rdev_nr(mddev_t
*mddev
, int nr
)
416 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
417 if (rdev
->desc_nr
== nr
)
423 static mdk_rdev_t
* find_rdev(mddev_t
* mddev
, dev_t dev
)
427 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
428 if (rdev
->bdev
->bd_dev
== dev
)
434 static struct mdk_personality
*find_pers(int level
, char *clevel
)
436 struct mdk_personality
*pers
;
437 list_for_each_entry(pers
, &pers_list
, list
) {
438 if (level
!= LEVEL_NONE
&& pers
->level
== level
)
440 if (strcmp(pers
->name
, clevel
)==0)
446 /* return the offset of the super block in 512byte sectors */
447 static inline sector_t
calc_dev_sboffset(struct block_device
*bdev
)
449 sector_t num_sectors
= bdev
->bd_inode
->i_size
/ 512;
450 return MD_NEW_SIZE_SECTORS(num_sectors
);
453 static int alloc_disk_sb(mdk_rdev_t
* rdev
)
458 rdev
->sb_page
= alloc_page(GFP_KERNEL
);
459 if (!rdev
->sb_page
) {
460 printk(KERN_ALERT
"md: out of memory.\n");
467 static void free_disk_sb(mdk_rdev_t
* rdev
)
470 put_page(rdev
->sb_page
);
472 rdev
->sb_page
= NULL
;
479 static void super_written(struct bio
*bio
, int error
)
481 mdk_rdev_t
*rdev
= bio
->bi_private
;
482 mddev_t
*mddev
= rdev
->mddev
;
484 if (error
|| !test_bit(BIO_UPTODATE
, &bio
->bi_flags
)) {
485 printk("md: super_written gets error=%d, uptodate=%d\n",
486 error
, test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
487 WARN_ON(test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
488 md_error(mddev
, rdev
);
491 if (atomic_dec_and_test(&mddev
->pending_writes
))
492 wake_up(&mddev
->sb_wait
);
496 static void super_written_barrier(struct bio
*bio
, int error
)
498 struct bio
*bio2
= bio
->bi_private
;
499 mdk_rdev_t
*rdev
= bio2
->bi_private
;
500 mddev_t
*mddev
= rdev
->mddev
;
502 if (!test_bit(BIO_UPTODATE
, &bio
->bi_flags
) &&
503 error
== -EOPNOTSUPP
) {
505 /* barriers don't appear to be supported :-( */
506 set_bit(BarriersNotsupp
, &rdev
->flags
);
507 mddev
->barriers_work
= 0;
508 spin_lock_irqsave(&mddev
->write_lock
, flags
);
509 bio2
->bi_next
= mddev
->biolist
;
510 mddev
->biolist
= bio2
;
511 spin_unlock_irqrestore(&mddev
->write_lock
, flags
);
512 wake_up(&mddev
->sb_wait
);
516 bio
->bi_private
= rdev
;
517 super_written(bio
, error
);
521 void md_super_write(mddev_t
*mddev
, mdk_rdev_t
*rdev
,
522 sector_t sector
, int size
, struct page
*page
)
524 /* write first size bytes of page to sector of rdev
525 * Increment mddev->pending_writes before returning
526 * and decrement it on completion, waking up sb_wait
527 * if zero is reached.
528 * If an error occurred, call md_error
530 * As we might need to resubmit the request if BIO_RW_BARRIER
531 * causes ENOTSUPP, we allocate a spare bio...
533 struct bio
*bio
= bio_alloc(GFP_NOIO
, 1);
534 int rw
= (1<<BIO_RW
) | (1<<BIO_RW_SYNCIO
) | (1<<BIO_RW_UNPLUG
);
536 bio
->bi_bdev
= rdev
->bdev
;
537 bio
->bi_sector
= sector
;
538 bio_add_page(bio
, page
, size
, 0);
539 bio
->bi_private
= rdev
;
540 bio
->bi_end_io
= super_written
;
543 atomic_inc(&mddev
->pending_writes
);
544 if (!test_bit(BarriersNotsupp
, &rdev
->flags
)) {
546 rw
|= (1<<BIO_RW_BARRIER
);
547 rbio
= bio_clone(bio
, GFP_NOIO
);
548 rbio
->bi_private
= bio
;
549 rbio
->bi_end_io
= super_written_barrier
;
550 submit_bio(rw
, rbio
);
555 void md_super_wait(mddev_t
*mddev
)
557 /* wait for all superblock writes that were scheduled to complete.
558 * if any had to be retried (due to BARRIER problems), retry them
562 prepare_to_wait(&mddev
->sb_wait
, &wq
, TASK_UNINTERRUPTIBLE
);
563 if (atomic_read(&mddev
->pending_writes
)==0)
565 while (mddev
->biolist
) {
567 spin_lock_irq(&mddev
->write_lock
);
568 bio
= mddev
->biolist
;
569 mddev
->biolist
= bio
->bi_next
;
571 spin_unlock_irq(&mddev
->write_lock
);
572 submit_bio(bio
->bi_rw
, bio
);
576 finish_wait(&mddev
->sb_wait
, &wq
);
579 static void bi_complete(struct bio
*bio
, int error
)
581 complete((struct completion
*)bio
->bi_private
);
584 int sync_page_io(struct block_device
*bdev
, sector_t sector
, int size
,
585 struct page
*page
, int rw
)
587 struct bio
*bio
= bio_alloc(GFP_NOIO
, 1);
588 struct completion event
;
591 rw
|= (1 << BIO_RW_SYNCIO
) | (1 << BIO_RW_UNPLUG
);
594 bio
->bi_sector
= sector
;
595 bio_add_page(bio
, page
, size
, 0);
596 init_completion(&event
);
597 bio
->bi_private
= &event
;
598 bio
->bi_end_io
= bi_complete
;
600 wait_for_completion(&event
);
602 ret
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
606 EXPORT_SYMBOL_GPL(sync_page_io
);
608 static int read_disk_sb(mdk_rdev_t
* rdev
, int size
)
610 char b
[BDEVNAME_SIZE
];
611 if (!rdev
->sb_page
) {
619 if (!sync_page_io(rdev
->bdev
, rdev
->sb_start
, size
, rdev
->sb_page
, READ
))
625 printk(KERN_WARNING
"md: disabled device %s, could not read superblock.\n",
626 bdevname(rdev
->bdev
,b
));
630 static int uuid_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
632 return sb1
->set_uuid0
== sb2
->set_uuid0
&&
633 sb1
->set_uuid1
== sb2
->set_uuid1
&&
634 sb1
->set_uuid2
== sb2
->set_uuid2
&&
635 sb1
->set_uuid3
== sb2
->set_uuid3
;
638 static int sb_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
641 mdp_super_t
*tmp1
, *tmp2
;
643 tmp1
= kmalloc(sizeof(*tmp1
),GFP_KERNEL
);
644 tmp2
= kmalloc(sizeof(*tmp2
),GFP_KERNEL
);
646 if (!tmp1
|| !tmp2
) {
648 printk(KERN_INFO
"md.c sb_equal(): failed to allocate memory!\n");
656 * nr_disks is not constant
661 ret
= (memcmp(tmp1
, tmp2
, MD_SB_GENERIC_CONSTANT_WORDS
* 4) == 0);
669 static u32
md_csum_fold(u32 csum
)
671 csum
= (csum
& 0xffff) + (csum
>> 16);
672 return (csum
& 0xffff) + (csum
>> 16);
675 static unsigned int calc_sb_csum(mdp_super_t
* sb
)
678 u32
*sb32
= (u32
*)sb
;
680 unsigned int disk_csum
, csum
;
682 disk_csum
= sb
->sb_csum
;
685 for (i
= 0; i
< MD_SB_BYTES
/4 ; i
++)
687 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
691 /* This used to use csum_partial, which was wrong for several
692 * reasons including that different results are returned on
693 * different architectures. It isn't critical that we get exactly
694 * the same return value as before (we always csum_fold before
695 * testing, and that removes any differences). However as we
696 * know that csum_partial always returned a 16bit value on
697 * alphas, do a fold to maximise conformity to previous behaviour.
699 sb
->sb_csum
= md_csum_fold(disk_csum
);
701 sb
->sb_csum
= disk_csum
;
708 * Handle superblock details.
709 * We want to be able to handle multiple superblock formats
710 * so we have a common interface to them all, and an array of
711 * different handlers.
712 * We rely on user-space to write the initial superblock, and support
713 * reading and updating of superblocks.
714 * Interface methods are:
715 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
716 * loads and validates a superblock on dev.
717 * if refdev != NULL, compare superblocks on both devices
719 * 0 - dev has a superblock that is compatible with refdev
720 * 1 - dev has a superblock that is compatible and newer than refdev
721 * so dev should be used as the refdev in future
722 * -EINVAL superblock incompatible or invalid
723 * -othererror e.g. -EIO
725 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
726 * Verify that dev is acceptable into mddev.
727 * The first time, mddev->raid_disks will be 0, and data from
728 * dev should be merged in. Subsequent calls check that dev
729 * is new enough. Return 0 or -EINVAL
731 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
732 * Update the superblock for rdev with data in mddev
733 * This does not write to disc.
739 struct module
*owner
;
740 int (*load_super
)(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
,
742 int (*validate_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
743 void (*sync_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
744 unsigned long long (*rdev_size_change
)(mdk_rdev_t
*rdev
,
745 sector_t num_sectors
);
749 * Check that the given mddev has no bitmap.
751 * This function is called from the run method of all personalities that do not
752 * support bitmaps. It prints an error message and returns non-zero if mddev
753 * has a bitmap. Otherwise, it returns 0.
756 int md_check_no_bitmap(mddev_t
*mddev
)
758 if (!mddev
->bitmap_file
&& !mddev
->bitmap_offset
)
760 printk(KERN_ERR
"%s: bitmaps are not supported for %s\n",
761 mdname(mddev
), mddev
->pers
->name
);
764 EXPORT_SYMBOL(md_check_no_bitmap
);
767 * load_super for 0.90.0
769 static int super_90_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
771 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
776 * Calculate the position of the superblock (512byte sectors),
777 * it's at the end of the disk.
779 * It also happens to be a multiple of 4Kb.
781 rdev
->sb_start
= calc_dev_sboffset(rdev
->bdev
);
783 ret
= read_disk_sb(rdev
, MD_SB_BYTES
);
788 bdevname(rdev
->bdev
, b
);
789 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
791 if (sb
->md_magic
!= MD_SB_MAGIC
) {
792 printk(KERN_ERR
"md: invalid raid superblock magic on %s\n",
797 if (sb
->major_version
!= 0 ||
798 sb
->minor_version
< 90 ||
799 sb
->minor_version
> 91) {
800 printk(KERN_WARNING
"Bad version number %d.%d on %s\n",
801 sb
->major_version
, sb
->minor_version
,
806 if (sb
->raid_disks
<= 0)
809 if (md_csum_fold(calc_sb_csum(sb
)) != md_csum_fold(sb
->sb_csum
)) {
810 printk(KERN_WARNING
"md: invalid superblock checksum on %s\n",
815 rdev
->preferred_minor
= sb
->md_minor
;
816 rdev
->data_offset
= 0;
817 rdev
->sb_size
= MD_SB_BYTES
;
819 if (sb
->level
== LEVEL_MULTIPATH
)
822 rdev
->desc_nr
= sb
->this_disk
.number
;
828 mdp_super_t
*refsb
= (mdp_super_t
*)page_address(refdev
->sb_page
);
829 if (!uuid_equal(refsb
, sb
)) {
830 printk(KERN_WARNING
"md: %s has different UUID to %s\n",
831 b
, bdevname(refdev
->bdev
,b2
));
834 if (!sb_equal(refsb
, sb
)) {
835 printk(KERN_WARNING
"md: %s has same UUID"
836 " but different superblock to %s\n",
837 b
, bdevname(refdev
->bdev
, b2
));
841 ev2
= md_event(refsb
);
847 rdev
->sectors
= rdev
->sb_start
;
849 if (rdev
->sectors
< sb
->size
* 2 && sb
->level
> 1)
850 /* "this cannot possibly happen" ... */
858 * validate_super for 0.90.0
860 static int super_90_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
863 mdp_super_t
*sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
864 __u64 ev1
= md_event(sb
);
866 rdev
->raid_disk
= -1;
867 clear_bit(Faulty
, &rdev
->flags
);
868 clear_bit(In_sync
, &rdev
->flags
);
869 clear_bit(WriteMostly
, &rdev
->flags
);
870 clear_bit(BarriersNotsupp
, &rdev
->flags
);
872 if (mddev
->raid_disks
== 0) {
873 mddev
->major_version
= 0;
874 mddev
->minor_version
= sb
->minor_version
;
875 mddev
->patch_version
= sb
->patch_version
;
877 mddev
->chunk_sectors
= sb
->chunk_size
>> 9;
878 mddev
->ctime
= sb
->ctime
;
879 mddev
->utime
= sb
->utime
;
880 mddev
->level
= sb
->level
;
881 mddev
->clevel
[0] = 0;
882 mddev
->layout
= sb
->layout
;
883 mddev
->raid_disks
= sb
->raid_disks
;
884 mddev
->dev_sectors
= sb
->size
* 2;
886 mddev
->bitmap_offset
= 0;
887 mddev
->default_bitmap_offset
= MD_SB_BYTES
>> 9;
889 if (mddev
->minor_version
>= 91) {
890 mddev
->reshape_position
= sb
->reshape_position
;
891 mddev
->delta_disks
= sb
->delta_disks
;
892 mddev
->new_level
= sb
->new_level
;
893 mddev
->new_layout
= sb
->new_layout
;
894 mddev
->new_chunk_sectors
= sb
->new_chunk
>> 9;
896 mddev
->reshape_position
= MaxSector
;
897 mddev
->delta_disks
= 0;
898 mddev
->new_level
= mddev
->level
;
899 mddev
->new_layout
= mddev
->layout
;
900 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
903 if (sb
->state
& (1<<MD_SB_CLEAN
))
904 mddev
->recovery_cp
= MaxSector
;
906 if (sb
->events_hi
== sb
->cp_events_hi
&&
907 sb
->events_lo
== sb
->cp_events_lo
) {
908 mddev
->recovery_cp
= sb
->recovery_cp
;
910 mddev
->recovery_cp
= 0;
913 memcpy(mddev
->uuid
+0, &sb
->set_uuid0
, 4);
914 memcpy(mddev
->uuid
+4, &sb
->set_uuid1
, 4);
915 memcpy(mddev
->uuid
+8, &sb
->set_uuid2
, 4);
916 memcpy(mddev
->uuid
+12,&sb
->set_uuid3
, 4);
918 mddev
->max_disks
= MD_SB_DISKS
;
920 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
) &&
921 mddev
->bitmap_file
== NULL
)
922 mddev
->bitmap_offset
= mddev
->default_bitmap_offset
;
924 } else if (mddev
->pers
== NULL
) {
925 /* Insist on good event counter while assembling */
927 if (ev1
< mddev
->events
)
929 } else if (mddev
->bitmap
) {
930 /* if adding to array with a bitmap, then we can accept an
931 * older device ... but not too old.
933 if (ev1
< mddev
->bitmap
->events_cleared
)
936 if (ev1
< mddev
->events
)
937 /* just a hot-add of a new device, leave raid_disk at -1 */
941 if (mddev
->level
!= LEVEL_MULTIPATH
) {
942 desc
= sb
->disks
+ rdev
->desc_nr
;
944 if (desc
->state
& (1<<MD_DISK_FAULTY
))
945 set_bit(Faulty
, &rdev
->flags
);
946 else if (desc
->state
& (1<<MD_DISK_SYNC
) /* &&
947 desc->raid_disk < mddev->raid_disks */) {
948 set_bit(In_sync
, &rdev
->flags
);
949 rdev
->raid_disk
= desc
->raid_disk
;
950 } else if (desc
->state
& (1<<MD_DISK_ACTIVE
)) {
951 /* active but not in sync implies recovery up to
952 * reshape position. We don't know exactly where
953 * that is, so set to zero for now */
954 if (mddev
->minor_version
>= 91) {
955 rdev
->recovery_offset
= 0;
956 rdev
->raid_disk
= desc
->raid_disk
;
959 if (desc
->state
& (1<<MD_DISK_WRITEMOSTLY
))
960 set_bit(WriteMostly
, &rdev
->flags
);
961 } else /* MULTIPATH are always insync */
962 set_bit(In_sync
, &rdev
->flags
);
967 * sync_super for 0.90.0
969 static void super_90_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
973 int next_spare
= mddev
->raid_disks
;
976 /* make rdev->sb match mddev data..
979 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
980 * 3/ any empty disks < next_spare become removed
982 * disks[0] gets initialised to REMOVED because
983 * we cannot be sure from other fields if it has
984 * been initialised or not.
987 int active
=0, working
=0,failed
=0,spare
=0,nr_disks
=0;
989 rdev
->sb_size
= MD_SB_BYTES
;
991 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
993 memset(sb
, 0, sizeof(*sb
));
995 sb
->md_magic
= MD_SB_MAGIC
;
996 sb
->major_version
= mddev
->major_version
;
997 sb
->patch_version
= mddev
->patch_version
;
998 sb
->gvalid_words
= 0; /* ignored */
999 memcpy(&sb
->set_uuid0
, mddev
->uuid
+0, 4);
1000 memcpy(&sb
->set_uuid1
, mddev
->uuid
+4, 4);
1001 memcpy(&sb
->set_uuid2
, mddev
->uuid
+8, 4);
1002 memcpy(&sb
->set_uuid3
, mddev
->uuid
+12,4);
1004 sb
->ctime
= mddev
->ctime
;
1005 sb
->level
= mddev
->level
;
1006 sb
->size
= mddev
->dev_sectors
/ 2;
1007 sb
->raid_disks
= mddev
->raid_disks
;
1008 sb
->md_minor
= mddev
->md_minor
;
1009 sb
->not_persistent
= 0;
1010 sb
->utime
= mddev
->utime
;
1012 sb
->events_hi
= (mddev
->events
>>32);
1013 sb
->events_lo
= (u32
)mddev
->events
;
1015 if (mddev
->reshape_position
== MaxSector
)
1016 sb
->minor_version
= 90;
1018 sb
->minor_version
= 91;
1019 sb
->reshape_position
= mddev
->reshape_position
;
1020 sb
->new_level
= mddev
->new_level
;
1021 sb
->delta_disks
= mddev
->delta_disks
;
1022 sb
->new_layout
= mddev
->new_layout
;
1023 sb
->new_chunk
= mddev
->new_chunk_sectors
<< 9;
1025 mddev
->minor_version
= sb
->minor_version
;
1028 sb
->recovery_cp
= mddev
->recovery_cp
;
1029 sb
->cp_events_hi
= (mddev
->events
>>32);
1030 sb
->cp_events_lo
= (u32
)mddev
->events
;
1031 if (mddev
->recovery_cp
== MaxSector
)
1032 sb
->state
= (1<< MD_SB_CLEAN
);
1034 sb
->recovery_cp
= 0;
1036 sb
->layout
= mddev
->layout
;
1037 sb
->chunk_size
= mddev
->chunk_sectors
<< 9;
1039 if (mddev
->bitmap
&& mddev
->bitmap_file
== NULL
)
1040 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
1042 sb
->disks
[0].state
= (1<<MD_DISK_REMOVED
);
1043 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
1046 int is_active
= test_bit(In_sync
, &rdev2
->flags
);
1048 if (rdev2
->raid_disk
>= 0 &&
1049 sb
->minor_version
>= 91)
1050 /* we have nowhere to store the recovery_offset,
1051 * but if it is not below the reshape_position,
1052 * we can piggy-back on that.
1055 if (rdev2
->raid_disk
< 0 ||
1056 test_bit(Faulty
, &rdev2
->flags
))
1059 desc_nr
= rdev2
->raid_disk
;
1061 desc_nr
= next_spare
++;
1062 rdev2
->desc_nr
= desc_nr
;
1063 d
= &sb
->disks
[rdev2
->desc_nr
];
1065 d
->number
= rdev2
->desc_nr
;
1066 d
->major
= MAJOR(rdev2
->bdev
->bd_dev
);
1067 d
->minor
= MINOR(rdev2
->bdev
->bd_dev
);
1069 d
->raid_disk
= rdev2
->raid_disk
;
1071 d
->raid_disk
= rdev2
->desc_nr
; /* compatibility */
1072 if (test_bit(Faulty
, &rdev2
->flags
))
1073 d
->state
= (1<<MD_DISK_FAULTY
);
1074 else if (is_active
) {
1075 d
->state
= (1<<MD_DISK_ACTIVE
);
1076 if (test_bit(In_sync
, &rdev2
->flags
))
1077 d
->state
|= (1<<MD_DISK_SYNC
);
1085 if (test_bit(WriteMostly
, &rdev2
->flags
))
1086 d
->state
|= (1<<MD_DISK_WRITEMOSTLY
);
1088 /* now set the "removed" and "faulty" bits on any missing devices */
1089 for (i
=0 ; i
< mddev
->raid_disks
; i
++) {
1090 mdp_disk_t
*d
= &sb
->disks
[i
];
1091 if (d
->state
== 0 && d
->number
== 0) {
1094 d
->state
= (1<<MD_DISK_REMOVED
);
1095 d
->state
|= (1<<MD_DISK_FAULTY
);
1099 sb
->nr_disks
= nr_disks
;
1100 sb
->active_disks
= active
;
1101 sb
->working_disks
= working
;
1102 sb
->failed_disks
= failed
;
1103 sb
->spare_disks
= spare
;
1105 sb
->this_disk
= sb
->disks
[rdev
->desc_nr
];
1106 sb
->sb_csum
= calc_sb_csum(sb
);
1110 * rdev_size_change for 0.90.0
1112 static unsigned long long
1113 super_90_rdev_size_change(mdk_rdev_t
*rdev
, sector_t num_sectors
)
1115 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1116 return 0; /* component must fit device */
1117 if (rdev
->mddev
->bitmap_offset
)
1118 return 0; /* can't move bitmap */
1119 rdev
->sb_start
= calc_dev_sboffset(rdev
->bdev
);
1120 if (!num_sectors
|| num_sectors
> rdev
->sb_start
)
1121 num_sectors
= rdev
->sb_start
;
1122 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1124 md_super_wait(rdev
->mddev
);
1125 return num_sectors
/ 2; /* kB for sysfs */
1130 * version 1 superblock
1133 static __le32
calc_sb_1_csum(struct mdp_superblock_1
* sb
)
1137 unsigned long long newcsum
;
1138 int size
= 256 + le32_to_cpu(sb
->max_dev
)*2;
1139 __le32
*isuper
= (__le32
*)sb
;
1142 disk_csum
= sb
->sb_csum
;
1145 for (i
=0; size
>=4; size
-= 4 )
1146 newcsum
+= le32_to_cpu(*isuper
++);
1149 newcsum
+= le16_to_cpu(*(__le16
*) isuper
);
1151 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
1152 sb
->sb_csum
= disk_csum
;
1153 return cpu_to_le32(csum
);
1156 static int super_1_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
1158 struct mdp_superblock_1
*sb
;
1161 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1165 * Calculate the position of the superblock in 512byte sectors.
1166 * It is always aligned to a 4K boundary and
1167 * depeding on minor_version, it can be:
1168 * 0: At least 8K, but less than 12K, from end of device
1169 * 1: At start of device
1170 * 2: 4K from start of device.
1172 switch(minor_version
) {
1174 sb_start
= rdev
->bdev
->bd_inode
->i_size
>> 9;
1176 sb_start
&= ~(sector_t
)(4*2-1);
1187 rdev
->sb_start
= sb_start
;
1189 /* superblock is rarely larger than 1K, but it can be larger,
1190 * and it is safe to read 4k, so we do that
1192 ret
= read_disk_sb(rdev
, 4096);
1193 if (ret
) return ret
;
1196 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1198 if (sb
->magic
!= cpu_to_le32(MD_SB_MAGIC
) ||
1199 sb
->major_version
!= cpu_to_le32(1) ||
1200 le32_to_cpu(sb
->max_dev
) > (4096-256)/2 ||
1201 le64_to_cpu(sb
->super_offset
) != rdev
->sb_start
||
1202 (le32_to_cpu(sb
->feature_map
) & ~MD_FEATURE_ALL
) != 0)
1205 if (calc_sb_1_csum(sb
) != sb
->sb_csum
) {
1206 printk("md: invalid superblock checksum on %s\n",
1207 bdevname(rdev
->bdev
,b
));
1210 if (le64_to_cpu(sb
->data_size
) < 10) {
1211 printk("md: data_size too small on %s\n",
1212 bdevname(rdev
->bdev
,b
));
1216 rdev
->preferred_minor
= 0xffff;
1217 rdev
->data_offset
= le64_to_cpu(sb
->data_offset
);
1218 atomic_set(&rdev
->corrected_errors
, le32_to_cpu(sb
->cnt_corrected_read
));
1220 rdev
->sb_size
= le32_to_cpu(sb
->max_dev
) * 2 + 256;
1221 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1222 if (rdev
->sb_size
& bmask
)
1223 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1226 && rdev
->data_offset
< sb_start
+ (rdev
->sb_size
/512))
1229 if (sb
->level
== cpu_to_le32(LEVEL_MULTIPATH
))
1232 rdev
->desc_nr
= le32_to_cpu(sb
->dev_number
);
1238 struct mdp_superblock_1
*refsb
=
1239 (struct mdp_superblock_1
*)page_address(refdev
->sb_page
);
1241 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16) != 0 ||
1242 sb
->level
!= refsb
->level
||
1243 sb
->layout
!= refsb
->layout
||
1244 sb
->chunksize
!= refsb
->chunksize
) {
1245 printk(KERN_WARNING
"md: %s has strangely different"
1246 " superblock to %s\n",
1247 bdevname(rdev
->bdev
,b
),
1248 bdevname(refdev
->bdev
,b2
));
1251 ev1
= le64_to_cpu(sb
->events
);
1252 ev2
= le64_to_cpu(refsb
->events
);
1260 rdev
->sectors
= (rdev
->bdev
->bd_inode
->i_size
>> 9) -
1261 le64_to_cpu(sb
->data_offset
);
1263 rdev
->sectors
= rdev
->sb_start
;
1264 if (rdev
->sectors
< le64_to_cpu(sb
->data_size
))
1266 rdev
->sectors
= le64_to_cpu(sb
->data_size
);
1267 if (le64_to_cpu(sb
->size
) > rdev
->sectors
)
1272 static int super_1_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1274 struct mdp_superblock_1
*sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1275 __u64 ev1
= le64_to_cpu(sb
->events
);
1277 rdev
->raid_disk
= -1;
1278 clear_bit(Faulty
, &rdev
->flags
);
1279 clear_bit(In_sync
, &rdev
->flags
);
1280 clear_bit(WriteMostly
, &rdev
->flags
);
1281 clear_bit(BarriersNotsupp
, &rdev
->flags
);
1283 if (mddev
->raid_disks
== 0) {
1284 mddev
->major_version
= 1;
1285 mddev
->patch_version
= 0;
1286 mddev
->external
= 0;
1287 mddev
->chunk_sectors
= le32_to_cpu(sb
->chunksize
);
1288 mddev
->ctime
= le64_to_cpu(sb
->ctime
) & ((1ULL << 32)-1);
1289 mddev
->utime
= le64_to_cpu(sb
->utime
) & ((1ULL << 32)-1);
1290 mddev
->level
= le32_to_cpu(sb
->level
);
1291 mddev
->clevel
[0] = 0;
1292 mddev
->layout
= le32_to_cpu(sb
->layout
);
1293 mddev
->raid_disks
= le32_to_cpu(sb
->raid_disks
);
1294 mddev
->dev_sectors
= le64_to_cpu(sb
->size
);
1295 mddev
->events
= ev1
;
1296 mddev
->bitmap_offset
= 0;
1297 mddev
->default_bitmap_offset
= 1024 >> 9;
1299 mddev
->recovery_cp
= le64_to_cpu(sb
->resync_offset
);
1300 memcpy(mddev
->uuid
, sb
->set_uuid
, 16);
1302 mddev
->max_disks
= (4096-256)/2;
1304 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BITMAP_OFFSET
) &&
1305 mddev
->bitmap_file
== NULL
)
1306 mddev
->bitmap_offset
= (__s32
)le32_to_cpu(sb
->bitmap_offset
);
1308 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
1309 mddev
->reshape_position
= le64_to_cpu(sb
->reshape_position
);
1310 mddev
->delta_disks
= le32_to_cpu(sb
->delta_disks
);
1311 mddev
->new_level
= le32_to_cpu(sb
->new_level
);
1312 mddev
->new_layout
= le32_to_cpu(sb
->new_layout
);
1313 mddev
->new_chunk_sectors
= le32_to_cpu(sb
->new_chunk
);
1315 mddev
->reshape_position
= MaxSector
;
1316 mddev
->delta_disks
= 0;
1317 mddev
->new_level
= mddev
->level
;
1318 mddev
->new_layout
= mddev
->layout
;
1319 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1322 } else if (mddev
->pers
== NULL
) {
1323 /* Insist of good event counter while assembling */
1325 if (ev1
< mddev
->events
)
1327 } else if (mddev
->bitmap
) {
1328 /* If adding to array with a bitmap, then we can accept an
1329 * older device, but not too old.
1331 if (ev1
< mddev
->bitmap
->events_cleared
)
1334 if (ev1
< mddev
->events
)
1335 /* just a hot-add of a new device, leave raid_disk at -1 */
1338 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1340 if (rdev
->desc_nr
< 0 ||
1341 rdev
->desc_nr
>= le32_to_cpu(sb
->max_dev
)) {
1345 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
1347 case 0xffff: /* spare */
1349 case 0xfffe: /* faulty */
1350 set_bit(Faulty
, &rdev
->flags
);
1353 if ((le32_to_cpu(sb
->feature_map
) &
1354 MD_FEATURE_RECOVERY_OFFSET
))
1355 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
1357 set_bit(In_sync
, &rdev
->flags
);
1358 rdev
->raid_disk
= role
;
1361 if (sb
->devflags
& WriteMostly1
)
1362 set_bit(WriteMostly
, &rdev
->flags
);
1363 } else /* MULTIPATH are always insync */
1364 set_bit(In_sync
, &rdev
->flags
);
1369 static void super_1_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1371 struct mdp_superblock_1
*sb
;
1374 /* make rdev->sb match mddev and rdev data. */
1376 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1378 sb
->feature_map
= 0;
1380 sb
->recovery_offset
= cpu_to_le64(0);
1381 memset(sb
->pad1
, 0, sizeof(sb
->pad1
));
1382 memset(sb
->pad2
, 0, sizeof(sb
->pad2
));
1383 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
1385 sb
->utime
= cpu_to_le64((__u64
)mddev
->utime
);
1386 sb
->events
= cpu_to_le64(mddev
->events
);
1388 sb
->resync_offset
= cpu_to_le64(mddev
->recovery_cp
);
1390 sb
->resync_offset
= cpu_to_le64(0);
1392 sb
->cnt_corrected_read
= cpu_to_le32(atomic_read(&rdev
->corrected_errors
));
1394 sb
->raid_disks
= cpu_to_le32(mddev
->raid_disks
);
1395 sb
->size
= cpu_to_le64(mddev
->dev_sectors
);
1396 sb
->chunksize
= cpu_to_le32(mddev
->chunk_sectors
);
1397 sb
->level
= cpu_to_le32(mddev
->level
);
1398 sb
->layout
= cpu_to_le32(mddev
->layout
);
1400 if (mddev
->bitmap
&& mddev
->bitmap_file
== NULL
) {
1401 sb
->bitmap_offset
= cpu_to_le32((__u32
)mddev
->bitmap_offset
);
1402 sb
->feature_map
= cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
);
1405 if (rdev
->raid_disk
>= 0 &&
1406 !test_bit(In_sync
, &rdev
->flags
)) {
1407 if (rdev
->recovery_offset
> 0) {
1409 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET
);
1410 sb
->recovery_offset
=
1411 cpu_to_le64(rdev
->recovery_offset
);
1415 if (mddev
->reshape_position
!= MaxSector
) {
1416 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE
);
1417 sb
->reshape_position
= cpu_to_le64(mddev
->reshape_position
);
1418 sb
->new_layout
= cpu_to_le32(mddev
->new_layout
);
1419 sb
->delta_disks
= cpu_to_le32(mddev
->delta_disks
);
1420 sb
->new_level
= cpu_to_le32(mddev
->new_level
);
1421 sb
->new_chunk
= cpu_to_le32(mddev
->new_chunk_sectors
);
1425 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
)
1426 if (rdev2
->desc_nr
+1 > max_dev
)
1427 max_dev
= rdev2
->desc_nr
+1;
1429 if (max_dev
> le32_to_cpu(sb
->max_dev
)) {
1431 sb
->max_dev
= cpu_to_le32(max_dev
);
1432 rdev
->sb_size
= max_dev
* 2 + 256;
1433 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1434 if (rdev
->sb_size
& bmask
)
1435 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1437 for (i
=0; i
<max_dev
;i
++)
1438 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1440 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
1442 if (test_bit(Faulty
, &rdev2
->flags
))
1443 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1444 else if (test_bit(In_sync
, &rdev2
->flags
))
1445 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1446 else if (rdev2
->raid_disk
>= 0 && rdev2
->recovery_offset
> 0)
1447 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1449 sb
->dev_roles
[i
] = cpu_to_le16(0xffff);
1452 sb
->sb_csum
= calc_sb_1_csum(sb
);
1455 static unsigned long long
1456 super_1_rdev_size_change(mdk_rdev_t
*rdev
, sector_t num_sectors
)
1458 struct mdp_superblock_1
*sb
;
1459 sector_t max_sectors
;
1460 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1461 return 0; /* component must fit device */
1462 if (rdev
->sb_start
< rdev
->data_offset
) {
1463 /* minor versions 1 and 2; superblock before data */
1464 max_sectors
= rdev
->bdev
->bd_inode
->i_size
>> 9;
1465 max_sectors
-= rdev
->data_offset
;
1466 if (!num_sectors
|| num_sectors
> max_sectors
)
1467 num_sectors
= max_sectors
;
1468 } else if (rdev
->mddev
->bitmap_offset
) {
1469 /* minor version 0 with bitmap we can't move */
1472 /* minor version 0; superblock after data */
1474 sb_start
= (rdev
->bdev
->bd_inode
->i_size
>> 9) - 8*2;
1475 sb_start
&= ~(sector_t
)(4*2 - 1);
1476 max_sectors
= rdev
->sectors
+ sb_start
- rdev
->sb_start
;
1477 if (!num_sectors
|| num_sectors
> max_sectors
)
1478 num_sectors
= max_sectors
;
1479 rdev
->sb_start
= sb_start
;
1481 sb
= (struct mdp_superblock_1
*) page_address(rdev
->sb_page
);
1482 sb
->data_size
= cpu_to_le64(num_sectors
);
1483 sb
->super_offset
= rdev
->sb_start
;
1484 sb
->sb_csum
= calc_sb_1_csum(sb
);
1485 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1487 md_super_wait(rdev
->mddev
);
1488 return num_sectors
/ 2; /* kB for sysfs */
1491 static struct super_type super_types
[] = {
1494 .owner
= THIS_MODULE
,
1495 .load_super
= super_90_load
,
1496 .validate_super
= super_90_validate
,
1497 .sync_super
= super_90_sync
,
1498 .rdev_size_change
= super_90_rdev_size_change
,
1502 .owner
= THIS_MODULE
,
1503 .load_super
= super_1_load
,
1504 .validate_super
= super_1_validate
,
1505 .sync_super
= super_1_sync
,
1506 .rdev_size_change
= super_1_rdev_size_change
,
1510 static int match_mddev_units(mddev_t
*mddev1
, mddev_t
*mddev2
)
1512 mdk_rdev_t
*rdev
, *rdev2
;
1515 rdev_for_each_rcu(rdev
, mddev1
)
1516 rdev_for_each_rcu(rdev2
, mddev2
)
1517 if (rdev
->bdev
->bd_contains
==
1518 rdev2
->bdev
->bd_contains
) {
1526 static LIST_HEAD(pending_raid_disks
);
1529 * Try to register data integrity profile for an mddev
1531 * This is called when an array is started and after a disk has been kicked
1532 * from the array. It only succeeds if all working and active component devices
1533 * are integrity capable with matching profiles.
1535 int md_integrity_register(mddev_t
*mddev
)
1537 mdk_rdev_t
*rdev
, *reference
= NULL
;
1539 if (list_empty(&mddev
->disks
))
1540 return 0; /* nothing to do */
1541 if (blk_get_integrity(mddev
->gendisk
))
1542 return 0; /* already registered */
1543 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
1544 /* skip spares and non-functional disks */
1545 if (test_bit(Faulty
, &rdev
->flags
))
1547 if (rdev
->raid_disk
< 0)
1550 * If at least one rdev is not integrity capable, we can not
1551 * enable data integrity for the md device.
1553 if (!bdev_get_integrity(rdev
->bdev
))
1556 /* Use the first rdev as the reference */
1560 /* does this rdev's profile match the reference profile? */
1561 if (blk_integrity_compare(reference
->bdev
->bd_disk
,
1562 rdev
->bdev
->bd_disk
) < 0)
1566 * All component devices are integrity capable and have matching
1567 * profiles, register the common profile for the md device.
1569 if (blk_integrity_register(mddev
->gendisk
,
1570 bdev_get_integrity(reference
->bdev
)) != 0) {
1571 printk(KERN_ERR
"md: failed to register integrity for %s\n",
1575 printk(KERN_NOTICE
"md: data integrity on %s enabled\n",
1579 EXPORT_SYMBOL(md_integrity_register
);
1581 /* Disable data integrity if non-capable/non-matching disk is being added */
1582 void md_integrity_add_rdev(mdk_rdev_t
*rdev
, mddev_t
*mddev
)
1584 struct blk_integrity
*bi_rdev
= bdev_get_integrity(rdev
->bdev
);
1585 struct blk_integrity
*bi_mddev
= blk_get_integrity(mddev
->gendisk
);
1587 if (!bi_mddev
) /* nothing to do */
1589 if (rdev
->raid_disk
< 0) /* skip spares */
1591 if (bi_rdev
&& blk_integrity_compare(mddev
->gendisk
,
1592 rdev
->bdev
->bd_disk
) >= 0)
1594 printk(KERN_NOTICE
"disabling data integrity on %s\n", mdname(mddev
));
1595 blk_integrity_unregister(mddev
->gendisk
);
1597 EXPORT_SYMBOL(md_integrity_add_rdev
);
1599 static int bind_rdev_to_array(mdk_rdev_t
* rdev
, mddev_t
* mddev
)
1601 char b
[BDEVNAME_SIZE
];
1611 /* prevent duplicates */
1612 if (find_rdev(mddev
, rdev
->bdev
->bd_dev
))
1615 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1616 if (rdev
->sectors
&& (mddev
->dev_sectors
== 0 ||
1617 rdev
->sectors
< mddev
->dev_sectors
)) {
1619 /* Cannot change size, so fail
1620 * If mddev->level <= 0, then we don't care
1621 * about aligning sizes (e.g. linear)
1623 if (mddev
->level
> 0)
1626 mddev
->dev_sectors
= rdev
->sectors
;
1629 /* Verify rdev->desc_nr is unique.
1630 * If it is -1, assign a free number, else
1631 * check number is not in use
1633 if (rdev
->desc_nr
< 0) {
1635 if (mddev
->pers
) choice
= mddev
->raid_disks
;
1636 while (find_rdev_nr(mddev
, choice
))
1638 rdev
->desc_nr
= choice
;
1640 if (find_rdev_nr(mddev
, rdev
->desc_nr
))
1643 if (mddev
->max_disks
&& rdev
->desc_nr
>= mddev
->max_disks
) {
1644 printk(KERN_WARNING
"md: %s: array is limited to %d devices\n",
1645 mdname(mddev
), mddev
->max_disks
);
1648 bdevname(rdev
->bdev
,b
);
1649 while ( (s
=strchr(b
, '/')) != NULL
)
1652 rdev
->mddev
= mddev
;
1653 printk(KERN_INFO
"md: bind<%s>\n", b
);
1655 if ((err
= kobject_add(&rdev
->kobj
, &mddev
->kobj
, "dev-%s", b
)))
1658 ko
= &part_to_dev(rdev
->bdev
->bd_part
)->kobj
;
1659 if ((err
= sysfs_create_link(&rdev
->kobj
, ko
, "block"))) {
1660 kobject_del(&rdev
->kobj
);
1663 rdev
->sysfs_state
= sysfs_get_dirent(rdev
->kobj
.sd
, "state");
1665 list_add_rcu(&rdev
->same_set
, &mddev
->disks
);
1666 bd_claim_by_disk(rdev
->bdev
, rdev
->bdev
->bd_holder
, mddev
->gendisk
);
1668 /* May as well allow recovery to be retried once */
1669 mddev
->recovery_disabled
= 0;
1674 printk(KERN_WARNING
"md: failed to register dev-%s for %s\n",
1679 static void md_delayed_delete(struct work_struct
*ws
)
1681 mdk_rdev_t
*rdev
= container_of(ws
, mdk_rdev_t
, del_work
);
1682 kobject_del(&rdev
->kobj
);
1683 kobject_put(&rdev
->kobj
);
1686 static void unbind_rdev_from_array(mdk_rdev_t
* rdev
)
1688 char b
[BDEVNAME_SIZE
];
1693 bd_release_from_disk(rdev
->bdev
, rdev
->mddev
->gendisk
);
1694 list_del_rcu(&rdev
->same_set
);
1695 printk(KERN_INFO
"md: unbind<%s>\n", bdevname(rdev
->bdev
,b
));
1697 sysfs_remove_link(&rdev
->kobj
, "block");
1698 sysfs_put(rdev
->sysfs_state
);
1699 rdev
->sysfs_state
= NULL
;
1700 /* We need to delay this, otherwise we can deadlock when
1701 * writing to 'remove' to "dev/state". We also need
1702 * to delay it due to rcu usage.
1705 INIT_WORK(&rdev
->del_work
, md_delayed_delete
);
1706 kobject_get(&rdev
->kobj
);
1707 schedule_work(&rdev
->del_work
);
1711 * prevent the device from being mounted, repartitioned or
1712 * otherwise reused by a RAID array (or any other kernel
1713 * subsystem), by bd_claiming the device.
1715 static int lock_rdev(mdk_rdev_t
*rdev
, dev_t dev
, int shared
)
1718 struct block_device
*bdev
;
1719 char b
[BDEVNAME_SIZE
];
1721 bdev
= open_by_devnum(dev
, FMODE_READ
|FMODE_WRITE
);
1723 printk(KERN_ERR
"md: could not open %s.\n",
1724 __bdevname(dev
, b
));
1725 return PTR_ERR(bdev
);
1727 err
= bd_claim(bdev
, shared
? (mdk_rdev_t
*)lock_rdev
: rdev
);
1729 printk(KERN_ERR
"md: could not bd_claim %s.\n",
1731 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
);
1735 set_bit(AllReserved
, &rdev
->flags
);
1740 static void unlock_rdev(mdk_rdev_t
*rdev
)
1742 struct block_device
*bdev
= rdev
->bdev
;
1747 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
);
1750 void md_autodetect_dev(dev_t dev
);
1752 static void export_rdev(mdk_rdev_t
* rdev
)
1754 char b
[BDEVNAME_SIZE
];
1755 printk(KERN_INFO
"md: export_rdev(%s)\n",
1756 bdevname(rdev
->bdev
,b
));
1761 if (test_bit(AutoDetected
, &rdev
->flags
))
1762 md_autodetect_dev(rdev
->bdev
->bd_dev
);
1765 kobject_put(&rdev
->kobj
);
1768 static void kick_rdev_from_array(mdk_rdev_t
* rdev
)
1770 unbind_rdev_from_array(rdev
);
1774 static void export_array(mddev_t
*mddev
)
1776 mdk_rdev_t
*rdev
, *tmp
;
1778 rdev_for_each(rdev
, tmp
, mddev
) {
1783 kick_rdev_from_array(rdev
);
1785 if (!list_empty(&mddev
->disks
))
1787 mddev
->raid_disks
= 0;
1788 mddev
->major_version
= 0;
1791 static void print_desc(mdp_disk_t
*desc
)
1793 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc
->number
,
1794 desc
->major
,desc
->minor
,desc
->raid_disk
,desc
->state
);
1797 static void print_sb_90(mdp_super_t
*sb
)
1802 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1803 sb
->major_version
, sb
->minor_version
, sb
->patch_version
,
1804 sb
->set_uuid0
, sb
->set_uuid1
, sb
->set_uuid2
, sb
->set_uuid3
,
1806 printk(KERN_INFO
"md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1807 sb
->level
, sb
->size
, sb
->nr_disks
, sb
->raid_disks
,
1808 sb
->md_minor
, sb
->layout
, sb
->chunk_size
);
1809 printk(KERN_INFO
"md: UT:%08x ST:%d AD:%d WD:%d"
1810 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1811 sb
->utime
, sb
->state
, sb
->active_disks
, sb
->working_disks
,
1812 sb
->failed_disks
, sb
->spare_disks
,
1813 sb
->sb_csum
, (unsigned long)sb
->events_lo
);
1816 for (i
= 0; i
< MD_SB_DISKS
; i
++) {
1819 desc
= sb
->disks
+ i
;
1820 if (desc
->number
|| desc
->major
|| desc
->minor
||
1821 desc
->raid_disk
|| (desc
->state
&& (desc
->state
!= 4))) {
1822 printk(" D %2d: ", i
);
1826 printk(KERN_INFO
"md: THIS: ");
1827 print_desc(&sb
->this_disk
);
1830 static void print_sb_1(struct mdp_superblock_1
*sb
)
1834 uuid
= sb
->set_uuid
;
1836 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%02x%02x%02x%02x"
1837 ":%02x%02x:%02x%02x:%02x%02x:%02x%02x%02x%02x%02x%02x>\n"
1838 "md: Name: \"%s\" CT:%llu\n",
1839 le32_to_cpu(sb
->major_version
),
1840 le32_to_cpu(sb
->feature_map
),
1841 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
1842 uuid
[4], uuid
[5], uuid
[6], uuid
[7],
1843 uuid
[8], uuid
[9], uuid
[10], uuid
[11],
1844 uuid
[12], uuid
[13], uuid
[14], uuid
[15],
1846 (unsigned long long)le64_to_cpu(sb
->ctime
)
1847 & MD_SUPERBLOCK_1_TIME_SEC_MASK
);
1849 uuid
= sb
->device_uuid
;
1851 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
1853 "md: Dev:%08x UUID: %02x%02x%02x%02x:%02x%02x:%02x%02x:%02x%02x"
1854 ":%02x%02x%02x%02x%02x%02x\n"
1855 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
1856 "md: (MaxDev:%u) \n",
1857 le32_to_cpu(sb
->level
),
1858 (unsigned long long)le64_to_cpu(sb
->size
),
1859 le32_to_cpu(sb
->raid_disks
),
1860 le32_to_cpu(sb
->layout
),
1861 le32_to_cpu(sb
->chunksize
),
1862 (unsigned long long)le64_to_cpu(sb
->data_offset
),
1863 (unsigned long long)le64_to_cpu(sb
->data_size
),
1864 (unsigned long long)le64_to_cpu(sb
->super_offset
),
1865 (unsigned long long)le64_to_cpu(sb
->recovery_offset
),
1866 le32_to_cpu(sb
->dev_number
),
1867 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
1868 uuid
[4], uuid
[5], uuid
[6], uuid
[7],
1869 uuid
[8], uuid
[9], uuid
[10], uuid
[11],
1870 uuid
[12], uuid
[13], uuid
[14], uuid
[15],
1872 (unsigned long long)le64_to_cpu(sb
->utime
) & MD_SUPERBLOCK_1_TIME_SEC_MASK
,
1873 (unsigned long long)le64_to_cpu(sb
->events
),
1874 (unsigned long long)le64_to_cpu(sb
->resync_offset
),
1875 le32_to_cpu(sb
->sb_csum
),
1876 le32_to_cpu(sb
->max_dev
)
1880 static void print_rdev(mdk_rdev_t
*rdev
, int major_version
)
1882 char b
[BDEVNAME_SIZE
];
1883 printk(KERN_INFO
"md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
1884 bdevname(rdev
->bdev
, b
), (unsigned long long)rdev
->sectors
,
1885 test_bit(Faulty
, &rdev
->flags
), test_bit(In_sync
, &rdev
->flags
),
1887 if (rdev
->sb_loaded
) {
1888 printk(KERN_INFO
"md: rdev superblock (MJ:%d):\n", major_version
);
1889 switch (major_version
) {
1891 print_sb_90((mdp_super_t
*)page_address(rdev
->sb_page
));
1894 print_sb_1((struct mdp_superblock_1
*)page_address(rdev
->sb_page
));
1898 printk(KERN_INFO
"md: no rdev superblock!\n");
1901 static void md_print_devices(void)
1903 struct list_head
*tmp
;
1906 char b
[BDEVNAME_SIZE
];
1909 printk("md: **********************************\n");
1910 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1911 printk("md: **********************************\n");
1912 for_each_mddev(mddev
, tmp
) {
1915 bitmap_print_sb(mddev
->bitmap
);
1917 printk("%s: ", mdname(mddev
));
1918 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
1919 printk("<%s>", bdevname(rdev
->bdev
,b
));
1922 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
1923 print_rdev(rdev
, mddev
->major_version
);
1925 printk("md: **********************************\n");
1930 static void sync_sbs(mddev_t
* mddev
, int nospares
)
1932 /* Update each superblock (in-memory image), but
1933 * if we are allowed to, skip spares which already
1934 * have the right event counter, or have one earlier
1935 * (which would mean they aren't being marked as dirty
1936 * with the rest of the array)
1940 /* First make sure individual recovery_offsets are correct */
1941 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
1942 if (rdev
->raid_disk
>= 0 &&
1943 !test_bit(In_sync
, &rdev
->flags
) &&
1944 mddev
->curr_resync_completed
> rdev
->recovery_offset
)
1945 rdev
->recovery_offset
= mddev
->curr_resync_completed
;
1948 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
1949 if (rdev
->sb_events
== mddev
->events
||
1951 rdev
->raid_disk
< 0 &&
1952 (rdev
->sb_events
&1)==0 &&
1953 rdev
->sb_events
+1 == mddev
->events
)) {
1954 /* Don't update this superblock */
1955 rdev
->sb_loaded
= 2;
1957 super_types
[mddev
->major_version
].
1958 sync_super(mddev
, rdev
);
1959 rdev
->sb_loaded
= 1;
1964 static void md_update_sb(mddev_t
* mddev
, int force_change
)
1970 mddev
->utime
= get_seconds();
1971 if (mddev
->external
)
1974 spin_lock_irq(&mddev
->write_lock
);
1976 set_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
1977 if (test_and_clear_bit(MD_CHANGE_DEVS
, &mddev
->flags
))
1979 if (test_and_clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
))
1980 /* just a clean<-> dirty transition, possibly leave spares alone,
1981 * though if events isn't the right even/odd, we will have to do
1987 if (mddev
->degraded
)
1988 /* If the array is degraded, then skipping spares is both
1989 * dangerous and fairly pointless.
1990 * Dangerous because a device that was removed from the array
1991 * might have a event_count that still looks up-to-date,
1992 * so it can be re-added without a resync.
1993 * Pointless because if there are any spares to skip,
1994 * then a recovery will happen and soon that array won't
1995 * be degraded any more and the spare can go back to sleep then.
1999 sync_req
= mddev
->in_sync
;
2001 /* If this is just a dirty<->clean transition, and the array is clean
2002 * and 'events' is odd, we can roll back to the previous clean state */
2004 && (mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
2005 && (mddev
->events
& 1)
2006 && mddev
->events
!= 1)
2009 /* otherwise we have to go forward and ... */
2011 if (!mddev
->in_sync
|| mddev
->recovery_cp
!= MaxSector
) { /* not clean */
2012 /* .. if the array isn't clean, an 'even' event must also go
2014 if ((mddev
->events
&1)==0)
2017 /* otherwise an 'odd' event must go to spares */
2018 if ((mddev
->events
&1))
2023 if (!mddev
->events
) {
2025 * oops, this 64-bit counter should never wrap.
2026 * Either we are in around ~1 trillion A.C., assuming
2027 * 1 reboot per second, or we have a bug:
2034 * do not write anything to disk if using
2035 * nonpersistent superblocks
2037 if (!mddev
->persistent
) {
2038 if (!mddev
->external
)
2039 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
2041 spin_unlock_irq(&mddev
->write_lock
);
2042 wake_up(&mddev
->sb_wait
);
2045 sync_sbs(mddev
, nospares
);
2046 spin_unlock_irq(&mddev
->write_lock
);
2049 "md: updating %s RAID superblock on device (in sync %d)\n",
2050 mdname(mddev
),mddev
->in_sync
);
2052 bitmap_update_sb(mddev
->bitmap
);
2053 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2054 char b
[BDEVNAME_SIZE
];
2055 dprintk(KERN_INFO
"md: ");
2056 if (rdev
->sb_loaded
!= 1)
2057 continue; /* no noise on spare devices */
2058 if (test_bit(Faulty
, &rdev
->flags
))
2059 dprintk("(skipping faulty ");
2061 dprintk("%s ", bdevname(rdev
->bdev
,b
));
2062 if (!test_bit(Faulty
, &rdev
->flags
)) {
2063 md_super_write(mddev
,rdev
,
2064 rdev
->sb_start
, rdev
->sb_size
,
2066 dprintk(KERN_INFO
"(write) %s's sb offset: %llu\n",
2067 bdevname(rdev
->bdev
,b
),
2068 (unsigned long long)rdev
->sb_start
);
2069 rdev
->sb_events
= mddev
->events
;
2073 if (mddev
->level
== LEVEL_MULTIPATH
)
2074 /* only need to write one superblock... */
2077 md_super_wait(mddev
);
2078 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
2080 spin_lock_irq(&mddev
->write_lock
);
2081 if (mddev
->in_sync
!= sync_req
||
2082 test_bit(MD_CHANGE_DEVS
, &mddev
->flags
)) {
2083 /* have to write it out again */
2084 spin_unlock_irq(&mddev
->write_lock
);
2087 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
2088 spin_unlock_irq(&mddev
->write_lock
);
2089 wake_up(&mddev
->sb_wait
);
2090 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
2091 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
2095 /* words written to sysfs files may, or may not, be \n terminated.
2096 * We want to accept with case. For this we use cmd_match.
2098 static int cmd_match(const char *cmd
, const char *str
)
2100 /* See if cmd, written into a sysfs file, matches
2101 * str. They must either be the same, or cmd can
2102 * have a trailing newline
2104 while (*cmd
&& *str
&& *cmd
== *str
) {
2115 struct rdev_sysfs_entry
{
2116 struct attribute attr
;
2117 ssize_t (*show
)(mdk_rdev_t
*, char *);
2118 ssize_t (*store
)(mdk_rdev_t
*, const char *, size_t);
2122 state_show(mdk_rdev_t
*rdev
, char *page
)
2127 if (test_bit(Faulty
, &rdev
->flags
)) {
2128 len
+= sprintf(page
+len
, "%sfaulty",sep
);
2131 if (test_bit(In_sync
, &rdev
->flags
)) {
2132 len
+= sprintf(page
+len
, "%sin_sync",sep
);
2135 if (test_bit(WriteMostly
, &rdev
->flags
)) {
2136 len
+= sprintf(page
+len
, "%swrite_mostly",sep
);
2139 if (test_bit(Blocked
, &rdev
->flags
)) {
2140 len
+= sprintf(page
+len
, "%sblocked", sep
);
2143 if (!test_bit(Faulty
, &rdev
->flags
) &&
2144 !test_bit(In_sync
, &rdev
->flags
)) {
2145 len
+= sprintf(page
+len
, "%sspare", sep
);
2148 return len
+sprintf(page
+len
, "\n");
2152 state_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2155 * faulty - simulates and error
2156 * remove - disconnects the device
2157 * writemostly - sets write_mostly
2158 * -writemostly - clears write_mostly
2159 * blocked - sets the Blocked flag
2160 * -blocked - clears the Blocked flag
2161 * insync - sets Insync providing device isn't active
2164 if (cmd_match(buf
, "faulty") && rdev
->mddev
->pers
) {
2165 md_error(rdev
->mddev
, rdev
);
2167 } else if (cmd_match(buf
, "remove")) {
2168 if (rdev
->raid_disk
>= 0)
2171 mddev_t
*mddev
= rdev
->mddev
;
2172 kick_rdev_from_array(rdev
);
2174 md_update_sb(mddev
, 1);
2175 md_new_event(mddev
);
2178 } else if (cmd_match(buf
, "writemostly")) {
2179 set_bit(WriteMostly
, &rdev
->flags
);
2181 } else if (cmd_match(buf
, "-writemostly")) {
2182 clear_bit(WriteMostly
, &rdev
->flags
);
2184 } else if (cmd_match(buf
, "blocked")) {
2185 set_bit(Blocked
, &rdev
->flags
);
2187 } else if (cmd_match(buf
, "-blocked")) {
2188 clear_bit(Blocked
, &rdev
->flags
);
2189 wake_up(&rdev
->blocked_wait
);
2190 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2191 md_wakeup_thread(rdev
->mddev
->thread
);
2194 } else if (cmd_match(buf
, "insync") && rdev
->raid_disk
== -1) {
2195 set_bit(In_sync
, &rdev
->flags
);
2198 if (!err
&& rdev
->sysfs_state
)
2199 sysfs_notify_dirent(rdev
->sysfs_state
);
2200 return err
? err
: len
;
2202 static struct rdev_sysfs_entry rdev_state
=
2203 __ATTR(state
, S_IRUGO
|S_IWUSR
, state_show
, state_store
);
2206 errors_show(mdk_rdev_t
*rdev
, char *page
)
2208 return sprintf(page
, "%d\n", atomic_read(&rdev
->corrected_errors
));
2212 errors_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2215 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2216 if (*buf
&& (*e
== 0 || *e
== '\n')) {
2217 atomic_set(&rdev
->corrected_errors
, n
);
2222 static struct rdev_sysfs_entry rdev_errors
=
2223 __ATTR(errors
, S_IRUGO
|S_IWUSR
, errors_show
, errors_store
);
2226 slot_show(mdk_rdev_t
*rdev
, char *page
)
2228 if (rdev
->raid_disk
< 0)
2229 return sprintf(page
, "none\n");
2231 return sprintf(page
, "%d\n", rdev
->raid_disk
);
2235 slot_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2240 int slot
= simple_strtoul(buf
, &e
, 10);
2241 if (strncmp(buf
, "none", 4)==0)
2243 else if (e
==buf
|| (*e
&& *e
!= '\n'))
2245 if (rdev
->mddev
->pers
&& slot
== -1) {
2246 /* Setting 'slot' on an active array requires also
2247 * updating the 'rd%d' link, and communicating
2248 * with the personality with ->hot_*_disk.
2249 * For now we only support removing
2250 * failed/spare devices. This normally happens automatically,
2251 * but not when the metadata is externally managed.
2253 if (rdev
->raid_disk
== -1)
2255 /* personality does all needed checks */
2256 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
2258 err
= rdev
->mddev
->pers
->
2259 hot_remove_disk(rdev
->mddev
, rdev
->raid_disk
);
2262 sprintf(nm
, "rd%d", rdev
->raid_disk
);
2263 sysfs_remove_link(&rdev
->mddev
->kobj
, nm
);
2264 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2265 md_wakeup_thread(rdev
->mddev
->thread
);
2266 } else if (rdev
->mddev
->pers
) {
2268 /* Activating a spare .. or possibly reactivating
2269 * if we ever get bitmaps working here.
2272 if (rdev
->raid_disk
!= -1)
2275 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
2278 list_for_each_entry(rdev2
, &rdev
->mddev
->disks
, same_set
)
2279 if (rdev2
->raid_disk
== slot
)
2282 rdev
->raid_disk
= slot
;
2283 if (test_bit(In_sync
, &rdev
->flags
))
2284 rdev
->saved_raid_disk
= slot
;
2286 rdev
->saved_raid_disk
= -1;
2287 err
= rdev
->mddev
->pers
->
2288 hot_add_disk(rdev
->mddev
, rdev
);
2290 rdev
->raid_disk
= -1;
2293 sysfs_notify_dirent(rdev
->sysfs_state
);
2294 sprintf(nm
, "rd%d", rdev
->raid_disk
);
2295 if (sysfs_create_link(&rdev
->mddev
->kobj
, &rdev
->kobj
, nm
))
2297 "md: cannot register "
2299 nm
, mdname(rdev
->mddev
));
2301 /* don't wakeup anyone, leave that to userspace. */
2303 if (slot
>= rdev
->mddev
->raid_disks
)
2305 rdev
->raid_disk
= slot
;
2306 /* assume it is working */
2307 clear_bit(Faulty
, &rdev
->flags
);
2308 clear_bit(WriteMostly
, &rdev
->flags
);
2309 set_bit(In_sync
, &rdev
->flags
);
2310 sysfs_notify_dirent(rdev
->sysfs_state
);
2316 static struct rdev_sysfs_entry rdev_slot
=
2317 __ATTR(slot
, S_IRUGO
|S_IWUSR
, slot_show
, slot_store
);
2320 offset_show(mdk_rdev_t
*rdev
, char *page
)
2322 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->data_offset
);
2326 offset_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2329 unsigned long long offset
= simple_strtoull(buf
, &e
, 10);
2330 if (e
==buf
|| (*e
&& *e
!= '\n'))
2332 if (rdev
->mddev
->pers
&& rdev
->raid_disk
>= 0)
2334 if (rdev
->sectors
&& rdev
->mddev
->external
)
2335 /* Must set offset before size, so overlap checks
2338 rdev
->data_offset
= offset
;
2342 static struct rdev_sysfs_entry rdev_offset
=
2343 __ATTR(offset
, S_IRUGO
|S_IWUSR
, offset_show
, offset_store
);
2346 rdev_size_show(mdk_rdev_t
*rdev
, char *page
)
2348 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->sectors
/ 2);
2351 static int overlaps(sector_t s1
, sector_t l1
, sector_t s2
, sector_t l2
)
2353 /* check if two start/length pairs overlap */
2361 static int strict_blocks_to_sectors(const char *buf
, sector_t
*sectors
)
2363 unsigned long long blocks
;
2366 if (strict_strtoull(buf
, 10, &blocks
) < 0)
2369 if (blocks
& 1ULL << (8 * sizeof(blocks
) - 1))
2370 return -EINVAL
; /* sector conversion overflow */
2373 if (new != blocks
* 2)
2374 return -EINVAL
; /* unsigned long long to sector_t overflow */
2381 rdev_size_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2383 mddev_t
*my_mddev
= rdev
->mddev
;
2384 sector_t oldsectors
= rdev
->sectors
;
2387 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
2389 if (my_mddev
->pers
&& rdev
->raid_disk
>= 0) {
2390 if (my_mddev
->persistent
) {
2391 sectors
= super_types
[my_mddev
->major_version
].
2392 rdev_size_change(rdev
, sectors
);
2395 } else if (!sectors
)
2396 sectors
= (rdev
->bdev
->bd_inode
->i_size
>> 9) -
2399 if (sectors
< my_mddev
->dev_sectors
)
2400 return -EINVAL
; /* component must fit device */
2402 rdev
->sectors
= sectors
;
2403 if (sectors
> oldsectors
&& my_mddev
->external
) {
2404 /* need to check that all other rdevs with the same ->bdev
2405 * do not overlap. We need to unlock the mddev to avoid
2406 * a deadlock. We have already changed rdev->sectors, and if
2407 * we have to change it back, we will have the lock again.
2411 struct list_head
*tmp
;
2413 mddev_unlock(my_mddev
);
2414 for_each_mddev(mddev
, tmp
) {
2418 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
)
2419 if (test_bit(AllReserved
, &rdev2
->flags
) ||
2420 (rdev
->bdev
== rdev2
->bdev
&&
2422 overlaps(rdev
->data_offset
, rdev
->sectors
,
2428 mddev_unlock(mddev
);
2434 mddev_lock(my_mddev
);
2436 /* Someone else could have slipped in a size
2437 * change here, but doing so is just silly.
2438 * We put oldsectors back because we *know* it is
2439 * safe, and trust userspace not to race with
2442 rdev
->sectors
= oldsectors
;
2449 static struct rdev_sysfs_entry rdev_size
=
2450 __ATTR(size
, S_IRUGO
|S_IWUSR
, rdev_size_show
, rdev_size_store
);
2452 static struct attribute
*rdev_default_attrs
[] = {
2461 rdev_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
2463 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
2464 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
2465 mddev_t
*mddev
= rdev
->mddev
;
2471 rv
= mddev
? mddev_lock(mddev
) : -EBUSY
;
2473 if (rdev
->mddev
== NULL
)
2476 rv
= entry
->show(rdev
, page
);
2477 mddev_unlock(mddev
);
2483 rdev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
2484 const char *page
, size_t length
)
2486 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
2487 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
2489 mddev_t
*mddev
= rdev
->mddev
;
2493 if (!capable(CAP_SYS_ADMIN
))
2495 rv
= mddev
? mddev_lock(mddev
): -EBUSY
;
2497 if (rdev
->mddev
== NULL
)
2500 rv
= entry
->store(rdev
, page
, length
);
2501 mddev_unlock(mddev
);
2506 static void rdev_free(struct kobject
*ko
)
2508 mdk_rdev_t
*rdev
= container_of(ko
, mdk_rdev_t
, kobj
);
2511 static struct sysfs_ops rdev_sysfs_ops
= {
2512 .show
= rdev_attr_show
,
2513 .store
= rdev_attr_store
,
2515 static struct kobj_type rdev_ktype
= {
2516 .release
= rdev_free
,
2517 .sysfs_ops
= &rdev_sysfs_ops
,
2518 .default_attrs
= rdev_default_attrs
,
2522 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2524 * mark the device faulty if:
2526 * - the device is nonexistent (zero size)
2527 * - the device has no valid superblock
2529 * a faulty rdev _never_ has rdev->sb set.
2531 static mdk_rdev_t
*md_import_device(dev_t newdev
, int super_format
, int super_minor
)
2533 char b
[BDEVNAME_SIZE
];
2538 rdev
= kzalloc(sizeof(*rdev
), GFP_KERNEL
);
2540 printk(KERN_ERR
"md: could not alloc mem for new device!\n");
2541 return ERR_PTR(-ENOMEM
);
2544 if ((err
= alloc_disk_sb(rdev
)))
2547 err
= lock_rdev(rdev
, newdev
, super_format
== -2);
2551 kobject_init(&rdev
->kobj
, &rdev_ktype
);
2554 rdev
->saved_raid_disk
= -1;
2555 rdev
->raid_disk
= -1;
2557 rdev
->data_offset
= 0;
2558 rdev
->sb_events
= 0;
2559 atomic_set(&rdev
->nr_pending
, 0);
2560 atomic_set(&rdev
->read_errors
, 0);
2561 atomic_set(&rdev
->corrected_errors
, 0);
2563 size
= rdev
->bdev
->bd_inode
->i_size
>> BLOCK_SIZE_BITS
;
2566 "md: %s has zero or unknown size, marking faulty!\n",
2567 bdevname(rdev
->bdev
,b
));
2572 if (super_format
>= 0) {
2573 err
= super_types
[super_format
].
2574 load_super(rdev
, NULL
, super_minor
);
2575 if (err
== -EINVAL
) {
2577 "md: %s does not have a valid v%d.%d "
2578 "superblock, not importing!\n",
2579 bdevname(rdev
->bdev
,b
),
2580 super_format
, super_minor
);
2585 "md: could not read %s's sb, not importing!\n",
2586 bdevname(rdev
->bdev
,b
));
2591 INIT_LIST_HEAD(&rdev
->same_set
);
2592 init_waitqueue_head(&rdev
->blocked_wait
);
2597 if (rdev
->sb_page
) {
2603 return ERR_PTR(err
);
2607 * Check a full RAID array for plausibility
2611 static void analyze_sbs(mddev_t
* mddev
)
2614 mdk_rdev_t
*rdev
, *freshest
, *tmp
;
2615 char b
[BDEVNAME_SIZE
];
2618 rdev_for_each(rdev
, tmp
, mddev
)
2619 switch (super_types
[mddev
->major_version
].
2620 load_super(rdev
, freshest
, mddev
->minor_version
)) {
2628 "md: fatal superblock inconsistency in %s"
2629 " -- removing from array\n",
2630 bdevname(rdev
->bdev
,b
));
2631 kick_rdev_from_array(rdev
);
2635 super_types
[mddev
->major_version
].
2636 validate_super(mddev
, freshest
);
2639 rdev_for_each(rdev
, tmp
, mddev
) {
2640 if (rdev
->desc_nr
>= mddev
->max_disks
||
2641 i
> mddev
->max_disks
) {
2643 "md: %s: %s: only %d devices permitted\n",
2644 mdname(mddev
), bdevname(rdev
->bdev
, b
),
2646 kick_rdev_from_array(rdev
);
2649 if (rdev
!= freshest
)
2650 if (super_types
[mddev
->major_version
].
2651 validate_super(mddev
, rdev
)) {
2652 printk(KERN_WARNING
"md: kicking non-fresh %s"
2654 bdevname(rdev
->bdev
,b
));
2655 kick_rdev_from_array(rdev
);
2658 if (mddev
->level
== LEVEL_MULTIPATH
) {
2659 rdev
->desc_nr
= i
++;
2660 rdev
->raid_disk
= rdev
->desc_nr
;
2661 set_bit(In_sync
, &rdev
->flags
);
2662 } else if (rdev
->raid_disk
>= (mddev
->raid_disks
- min(0, mddev
->delta_disks
))) {
2663 rdev
->raid_disk
= -1;
2664 clear_bit(In_sync
, &rdev
->flags
);
2669 static void md_safemode_timeout(unsigned long data
);
2672 safe_delay_show(mddev_t
*mddev
, char *page
)
2674 int msec
= (mddev
->safemode_delay
*1000)/HZ
;
2675 return sprintf(page
, "%d.%03d\n", msec
/1000, msec
%1000);
2678 safe_delay_store(mddev_t
*mddev
, const char *cbuf
, size_t len
)
2686 /* remove a period, and count digits after it */
2687 if (len
>= sizeof(buf
))
2689 strlcpy(buf
, cbuf
, sizeof(buf
));
2690 for (i
=0; i
<len
; i
++) {
2692 if (isdigit(buf
[i
])) {
2697 } else if (buf
[i
] == '.') {
2702 if (strict_strtoul(buf
, 10, &msec
) < 0)
2704 msec
= (msec
* 1000) / scale
;
2706 mddev
->safemode_delay
= 0;
2708 unsigned long old_delay
= mddev
->safemode_delay
;
2709 mddev
->safemode_delay
= (msec
*HZ
)/1000;
2710 if (mddev
->safemode_delay
== 0)
2711 mddev
->safemode_delay
= 1;
2712 if (mddev
->safemode_delay
< old_delay
)
2713 md_safemode_timeout((unsigned long)mddev
);
2717 static struct md_sysfs_entry md_safe_delay
=
2718 __ATTR(safe_mode_delay
, S_IRUGO
|S_IWUSR
,safe_delay_show
, safe_delay_store
);
2721 level_show(mddev_t
*mddev
, char *page
)
2723 struct mdk_personality
*p
= mddev
->pers
;
2725 return sprintf(page
, "%s\n", p
->name
);
2726 else if (mddev
->clevel
[0])
2727 return sprintf(page
, "%s\n", mddev
->clevel
);
2728 else if (mddev
->level
!= LEVEL_NONE
)
2729 return sprintf(page
, "%d\n", mddev
->level
);
2735 level_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2739 struct mdk_personality
*pers
;
2743 if (mddev
->pers
== NULL
) {
2746 if (len
>= sizeof(mddev
->clevel
))
2748 strncpy(mddev
->clevel
, buf
, len
);
2749 if (mddev
->clevel
[len
-1] == '\n')
2751 mddev
->clevel
[len
] = 0;
2752 mddev
->level
= LEVEL_NONE
;
2756 /* request to change the personality. Need to ensure:
2757 * - array is not engaged in resync/recovery/reshape
2758 * - old personality can be suspended
2759 * - new personality will access other array.
2762 if (mddev
->sync_thread
|| mddev
->reshape_position
!= MaxSector
)
2765 if (!mddev
->pers
->quiesce
) {
2766 printk(KERN_WARNING
"md: %s: %s does not support online personality change\n",
2767 mdname(mddev
), mddev
->pers
->name
);
2771 /* Now find the new personality */
2772 if (len
== 0 || len
>= sizeof(level
))
2774 strncpy(level
, buf
, len
);
2775 if (level
[len
-1] == '\n')
2779 request_module("md-%s", level
);
2780 spin_lock(&pers_lock
);
2781 pers
= find_pers(LEVEL_NONE
, level
);
2782 if (!pers
|| !try_module_get(pers
->owner
)) {
2783 spin_unlock(&pers_lock
);
2784 printk(KERN_WARNING
"md: personality %s not loaded\n", level
);
2787 spin_unlock(&pers_lock
);
2789 if (pers
== mddev
->pers
) {
2790 /* Nothing to do! */
2791 module_put(pers
->owner
);
2794 if (!pers
->takeover
) {
2795 module_put(pers
->owner
);
2796 printk(KERN_WARNING
"md: %s: %s does not support personality takeover\n",
2797 mdname(mddev
), level
);
2801 /* ->takeover must set new_* and/or delta_disks
2802 * if it succeeds, and may set them when it fails.
2804 priv
= pers
->takeover(mddev
);
2806 mddev
->new_level
= mddev
->level
;
2807 mddev
->new_layout
= mddev
->layout
;
2808 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
2809 mddev
->raid_disks
-= mddev
->delta_disks
;
2810 mddev
->delta_disks
= 0;
2811 module_put(pers
->owner
);
2812 printk(KERN_WARNING
"md: %s: %s would not accept array\n",
2813 mdname(mddev
), level
);
2814 return PTR_ERR(priv
);
2817 /* Looks like we have a winner */
2818 mddev_suspend(mddev
);
2819 mddev
->pers
->stop(mddev
);
2820 module_put(mddev
->pers
->owner
);
2821 /* Invalidate devices that are now superfluous */
2822 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
2823 if (rdev
->raid_disk
>= mddev
->raid_disks
) {
2824 rdev
->raid_disk
= -1;
2825 clear_bit(In_sync
, &rdev
->flags
);
2828 mddev
->private = priv
;
2829 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
2830 mddev
->level
= mddev
->new_level
;
2831 mddev
->layout
= mddev
->new_layout
;
2832 mddev
->chunk_sectors
= mddev
->new_chunk_sectors
;
2833 mddev
->delta_disks
= 0;
2835 mddev_resume(mddev
);
2836 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
2837 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
2838 md_wakeup_thread(mddev
->thread
);
2842 static struct md_sysfs_entry md_level
=
2843 __ATTR(level
, S_IRUGO
|S_IWUSR
, level_show
, level_store
);
2847 layout_show(mddev_t
*mddev
, char *page
)
2849 /* just a number, not meaningful for all levels */
2850 if (mddev
->reshape_position
!= MaxSector
&&
2851 mddev
->layout
!= mddev
->new_layout
)
2852 return sprintf(page
, "%d (%d)\n",
2853 mddev
->new_layout
, mddev
->layout
);
2854 return sprintf(page
, "%d\n", mddev
->layout
);
2858 layout_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2861 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2863 if (!*buf
|| (*e
&& *e
!= '\n'))
2868 if (mddev
->pers
->check_reshape
== NULL
)
2870 mddev
->new_layout
= n
;
2871 err
= mddev
->pers
->check_reshape(mddev
);
2873 mddev
->new_layout
= mddev
->layout
;
2877 mddev
->new_layout
= n
;
2878 if (mddev
->reshape_position
== MaxSector
)
2883 static struct md_sysfs_entry md_layout
=
2884 __ATTR(layout
, S_IRUGO
|S_IWUSR
, layout_show
, layout_store
);
2888 raid_disks_show(mddev_t
*mddev
, char *page
)
2890 if (mddev
->raid_disks
== 0)
2892 if (mddev
->reshape_position
!= MaxSector
&&
2893 mddev
->delta_disks
!= 0)
2894 return sprintf(page
, "%d (%d)\n", mddev
->raid_disks
,
2895 mddev
->raid_disks
- mddev
->delta_disks
);
2896 return sprintf(page
, "%d\n", mddev
->raid_disks
);
2899 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
);
2902 raid_disks_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2906 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2908 if (!*buf
|| (*e
&& *e
!= '\n'))
2912 rv
= update_raid_disks(mddev
, n
);
2913 else if (mddev
->reshape_position
!= MaxSector
) {
2914 int olddisks
= mddev
->raid_disks
- mddev
->delta_disks
;
2915 mddev
->delta_disks
= n
- olddisks
;
2916 mddev
->raid_disks
= n
;
2918 mddev
->raid_disks
= n
;
2919 return rv
? rv
: len
;
2921 static struct md_sysfs_entry md_raid_disks
=
2922 __ATTR(raid_disks
, S_IRUGO
|S_IWUSR
, raid_disks_show
, raid_disks_store
);
2925 chunk_size_show(mddev_t
*mddev
, char *page
)
2927 if (mddev
->reshape_position
!= MaxSector
&&
2928 mddev
->chunk_sectors
!= mddev
->new_chunk_sectors
)
2929 return sprintf(page
, "%d (%d)\n",
2930 mddev
->new_chunk_sectors
<< 9,
2931 mddev
->chunk_sectors
<< 9);
2932 return sprintf(page
, "%d\n", mddev
->chunk_sectors
<< 9);
2936 chunk_size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2939 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2941 if (!*buf
|| (*e
&& *e
!= '\n'))
2946 if (mddev
->pers
->check_reshape
== NULL
)
2948 mddev
->new_chunk_sectors
= n
>> 9;
2949 err
= mddev
->pers
->check_reshape(mddev
);
2951 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
2955 mddev
->new_chunk_sectors
= n
>> 9;
2956 if (mddev
->reshape_position
== MaxSector
)
2957 mddev
->chunk_sectors
= n
>> 9;
2961 static struct md_sysfs_entry md_chunk_size
=
2962 __ATTR(chunk_size
, S_IRUGO
|S_IWUSR
, chunk_size_show
, chunk_size_store
);
2965 resync_start_show(mddev_t
*mddev
, char *page
)
2967 if (mddev
->recovery_cp
== MaxSector
)
2968 return sprintf(page
, "none\n");
2969 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->recovery_cp
);
2973 resync_start_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2976 unsigned long long n
= simple_strtoull(buf
, &e
, 10);
2980 if (!*buf
|| (*e
&& *e
!= '\n'))
2983 mddev
->recovery_cp
= n
;
2986 static struct md_sysfs_entry md_resync_start
=
2987 __ATTR(resync_start
, S_IRUGO
|S_IWUSR
, resync_start_show
, resync_start_store
);
2990 * The array state can be:
2993 * No devices, no size, no level
2994 * Equivalent to STOP_ARRAY ioctl
2996 * May have some settings, but array is not active
2997 * all IO results in error
2998 * When written, doesn't tear down array, but just stops it
2999 * suspended (not supported yet)
3000 * All IO requests will block. The array can be reconfigured.
3001 * Writing this, if accepted, will block until array is quiescent
3003 * no resync can happen. no superblocks get written.
3004 * write requests fail
3006 * like readonly, but behaves like 'clean' on a write request.
3008 * clean - no pending writes, but otherwise active.
3009 * When written to inactive array, starts without resync
3010 * If a write request arrives then
3011 * if metadata is known, mark 'dirty' and switch to 'active'.
3012 * if not known, block and switch to write-pending
3013 * If written to an active array that has pending writes, then fails.
3015 * fully active: IO and resync can be happening.
3016 * When written to inactive array, starts with resync
3019 * clean, but writes are blocked waiting for 'active' to be written.
3022 * like active, but no writes have been seen for a while (100msec).
3025 enum array_state
{ clear
, inactive
, suspended
, readonly
, read_auto
, clean
, active
,
3026 write_pending
, active_idle
, bad_word
};
3027 static char *array_states
[] = {
3028 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3029 "write-pending", "active-idle", NULL
};
3031 static int match_word(const char *word
, char **list
)
3034 for (n
=0; list
[n
]; n
++)
3035 if (cmd_match(word
, list
[n
]))
3041 array_state_show(mddev_t
*mddev
, char *page
)
3043 enum array_state st
= inactive
;
3056 else if (test_bit(MD_CHANGE_CLEAN
, &mddev
->flags
))
3058 else if (mddev
->safemode
)
3064 if (list_empty(&mddev
->disks
) &&
3065 mddev
->raid_disks
== 0 &&
3066 mddev
->dev_sectors
== 0)
3071 return sprintf(page
, "%s\n", array_states
[st
]);
3074 static int do_md_stop(mddev_t
* mddev
, int ro
, int is_open
);
3075 static int do_md_run(mddev_t
* mddev
);
3076 static int restart_array(mddev_t
*mddev
);
3079 array_state_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3082 enum array_state st
= match_word(buf
, array_states
);
3087 /* stopping an active array */
3088 if (atomic_read(&mddev
->openers
) > 0)
3090 err
= do_md_stop(mddev
, 0, 0);
3093 /* stopping an active array */
3095 if (atomic_read(&mddev
->openers
) > 0)
3097 err
= do_md_stop(mddev
, 2, 0);
3099 err
= 0; /* already inactive */
3102 break; /* not supported yet */
3105 err
= do_md_stop(mddev
, 1, 0);
3108 set_disk_ro(mddev
->gendisk
, 1);
3109 err
= do_md_run(mddev
);
3115 err
= do_md_stop(mddev
, 1, 0);
3116 else if (mddev
->ro
== 1)
3117 err
= restart_array(mddev
);
3120 set_disk_ro(mddev
->gendisk
, 0);
3124 err
= do_md_run(mddev
);
3129 restart_array(mddev
);
3130 spin_lock_irq(&mddev
->write_lock
);
3131 if (atomic_read(&mddev
->writes_pending
) == 0) {
3132 if (mddev
->in_sync
== 0) {
3134 if (mddev
->safemode
== 1)
3135 mddev
->safemode
= 0;
3136 if (mddev
->persistent
)
3137 set_bit(MD_CHANGE_CLEAN
,
3143 spin_unlock_irq(&mddev
->write_lock
);
3149 restart_array(mddev
);
3150 if (mddev
->external
)
3151 clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
3152 wake_up(&mddev
->sb_wait
);
3156 set_disk_ro(mddev
->gendisk
, 0);
3157 err
= do_md_run(mddev
);
3162 /* these cannot be set */
3168 sysfs_notify_dirent(mddev
->sysfs_state
);
3172 static struct md_sysfs_entry md_array_state
=
3173 __ATTR(array_state
, S_IRUGO
|S_IWUSR
, array_state_show
, array_state_store
);
3176 null_show(mddev_t
*mddev
, char *page
)
3182 new_dev_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3184 /* buf must be %d:%d\n? giving major and minor numbers */
3185 /* The new device is added to the array.
3186 * If the array has a persistent superblock, we read the
3187 * superblock to initialise info and check validity.
3188 * Otherwise, only checking done is that in bind_rdev_to_array,
3189 * which mainly checks size.
3192 int major
= simple_strtoul(buf
, &e
, 10);
3198 if (!*buf
|| *e
!= ':' || !e
[1] || e
[1] == '\n')
3200 minor
= simple_strtoul(e
+1, &e
, 10);
3201 if (*e
&& *e
!= '\n')
3203 dev
= MKDEV(major
, minor
);
3204 if (major
!= MAJOR(dev
) ||
3205 minor
!= MINOR(dev
))
3209 if (mddev
->persistent
) {
3210 rdev
= md_import_device(dev
, mddev
->major_version
,
3211 mddev
->minor_version
);
3212 if (!IS_ERR(rdev
) && !list_empty(&mddev
->disks
)) {
3213 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
3214 mdk_rdev_t
, same_set
);
3215 err
= super_types
[mddev
->major_version
]
3216 .load_super(rdev
, rdev0
, mddev
->minor_version
);
3220 } else if (mddev
->external
)
3221 rdev
= md_import_device(dev
, -2, -1);
3223 rdev
= md_import_device(dev
, -1, -1);
3226 return PTR_ERR(rdev
);
3227 err
= bind_rdev_to_array(rdev
, mddev
);
3231 return err
? err
: len
;
3234 static struct md_sysfs_entry md_new_device
=
3235 __ATTR(new_dev
, S_IWUSR
, null_show
, new_dev_store
);
3238 bitmap_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3241 unsigned long chunk
, end_chunk
;
3245 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3247 chunk
= end_chunk
= simple_strtoul(buf
, &end
, 0);
3248 if (buf
== end
) break;
3249 if (*end
== '-') { /* range */
3251 end_chunk
= simple_strtoul(buf
, &end
, 0);
3252 if (buf
== end
) break;
3254 if (*end
&& !isspace(*end
)) break;
3255 bitmap_dirty_bits(mddev
->bitmap
, chunk
, end_chunk
);
3257 while (isspace(*buf
)) buf
++;
3259 bitmap_unplug(mddev
->bitmap
); /* flush the bits to disk */
3264 static struct md_sysfs_entry md_bitmap
=
3265 __ATTR(bitmap_set_bits
, S_IWUSR
, null_show
, bitmap_store
);
3268 size_show(mddev_t
*mddev
, char *page
)
3270 return sprintf(page
, "%llu\n",
3271 (unsigned long long)mddev
->dev_sectors
/ 2);
3274 static int update_size(mddev_t
*mddev
, sector_t num_sectors
);
3277 size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3279 /* If array is inactive, we can reduce the component size, but
3280 * not increase it (except from 0).
3281 * If array is active, we can try an on-line resize
3284 int err
= strict_blocks_to_sectors(buf
, §ors
);
3289 err
= update_size(mddev
, sectors
);
3290 md_update_sb(mddev
, 1);
3292 if (mddev
->dev_sectors
== 0 ||
3293 mddev
->dev_sectors
> sectors
)
3294 mddev
->dev_sectors
= sectors
;
3298 return err
? err
: len
;
3301 static struct md_sysfs_entry md_size
=
3302 __ATTR(component_size
, S_IRUGO
|S_IWUSR
, size_show
, size_store
);
3307 * 'none' for arrays with no metadata (good luck...)
3308 * 'external' for arrays with externally managed metadata,
3309 * or N.M for internally known formats
3312 metadata_show(mddev_t
*mddev
, char *page
)
3314 if (mddev
->persistent
)
3315 return sprintf(page
, "%d.%d\n",
3316 mddev
->major_version
, mddev
->minor_version
);
3317 else if (mddev
->external
)
3318 return sprintf(page
, "external:%s\n", mddev
->metadata_type
);
3320 return sprintf(page
, "none\n");
3324 metadata_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3328 /* Changing the details of 'external' metadata is
3329 * always permitted. Otherwise there must be
3330 * no devices attached to the array.
3332 if (mddev
->external
&& strncmp(buf
, "external:", 9) == 0)
3334 else if (!list_empty(&mddev
->disks
))
3337 if (cmd_match(buf
, "none")) {
3338 mddev
->persistent
= 0;
3339 mddev
->external
= 0;
3340 mddev
->major_version
= 0;
3341 mddev
->minor_version
= 90;
3344 if (strncmp(buf
, "external:", 9) == 0) {
3345 size_t namelen
= len
-9;
3346 if (namelen
>= sizeof(mddev
->metadata_type
))
3347 namelen
= sizeof(mddev
->metadata_type
)-1;
3348 strncpy(mddev
->metadata_type
, buf
+9, namelen
);
3349 mddev
->metadata_type
[namelen
] = 0;
3350 if (namelen
&& mddev
->metadata_type
[namelen
-1] == '\n')
3351 mddev
->metadata_type
[--namelen
] = 0;
3352 mddev
->persistent
= 0;
3353 mddev
->external
= 1;
3354 mddev
->major_version
= 0;
3355 mddev
->minor_version
= 90;
3358 major
= simple_strtoul(buf
, &e
, 10);
3359 if (e
==buf
|| *e
!= '.')
3362 minor
= simple_strtoul(buf
, &e
, 10);
3363 if (e
==buf
|| (*e
&& *e
!= '\n') )
3365 if (major
>= ARRAY_SIZE(super_types
) || super_types
[major
].name
== NULL
)
3367 mddev
->major_version
= major
;
3368 mddev
->minor_version
= minor
;
3369 mddev
->persistent
= 1;
3370 mddev
->external
= 0;
3374 static struct md_sysfs_entry md_metadata
=
3375 __ATTR(metadata_version
, S_IRUGO
|S_IWUSR
, metadata_show
, metadata_store
);
3378 action_show(mddev_t
*mddev
, char *page
)
3380 char *type
= "idle";
3381 if (test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
3383 else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
3384 (!mddev
->ro
&& test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))) {
3385 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
3387 else if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
3388 if (!test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
3390 else if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
3394 } else if (test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
))
3397 return sprintf(page
, "%s\n", type
);
3401 action_store(mddev_t
*mddev
, const char *page
, size_t len
)
3403 if (!mddev
->pers
|| !mddev
->pers
->sync_request
)
3406 if (cmd_match(page
, "frozen"))
3407 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
3409 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
3411 if (cmd_match(page
, "idle") || cmd_match(page
, "frozen")) {
3412 if (mddev
->sync_thread
) {
3413 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
3414 md_unregister_thread(mddev
->sync_thread
);
3415 mddev
->sync_thread
= NULL
;
3416 mddev
->recovery
= 0;
3418 } else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
3419 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
3421 else if (cmd_match(page
, "resync"))
3422 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3423 else if (cmd_match(page
, "recover")) {
3424 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
3425 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3426 } else if (cmd_match(page
, "reshape")) {
3428 if (mddev
->pers
->start_reshape
== NULL
)
3430 err
= mddev
->pers
->start_reshape(mddev
);
3433 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
3435 if (cmd_match(page
, "check"))
3436 set_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
3437 else if (!cmd_match(page
, "repair"))
3439 set_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
3440 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
3442 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3443 md_wakeup_thread(mddev
->thread
);
3444 sysfs_notify_dirent(mddev
->sysfs_action
);
3449 mismatch_cnt_show(mddev_t
*mddev
, char *page
)
3451 return sprintf(page
, "%llu\n",
3452 (unsigned long long) mddev
->resync_mismatches
);
3455 static struct md_sysfs_entry md_scan_mode
=
3456 __ATTR(sync_action
, S_IRUGO
|S_IWUSR
, action_show
, action_store
);
3459 static struct md_sysfs_entry md_mismatches
= __ATTR_RO(mismatch_cnt
);
3462 sync_min_show(mddev_t
*mddev
, char *page
)
3464 return sprintf(page
, "%d (%s)\n", speed_min(mddev
),
3465 mddev
->sync_speed_min
? "local": "system");
3469 sync_min_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3473 if (strncmp(buf
, "system", 6)==0) {
3474 mddev
->sync_speed_min
= 0;
3477 min
= simple_strtoul(buf
, &e
, 10);
3478 if (buf
== e
|| (*e
&& *e
!= '\n') || min
<= 0)
3480 mddev
->sync_speed_min
= min
;
3484 static struct md_sysfs_entry md_sync_min
=
3485 __ATTR(sync_speed_min
, S_IRUGO
|S_IWUSR
, sync_min_show
, sync_min_store
);
3488 sync_max_show(mddev_t
*mddev
, char *page
)
3490 return sprintf(page
, "%d (%s)\n", speed_max(mddev
),
3491 mddev
->sync_speed_max
? "local": "system");
3495 sync_max_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3499 if (strncmp(buf
, "system", 6)==0) {
3500 mddev
->sync_speed_max
= 0;
3503 max
= simple_strtoul(buf
, &e
, 10);
3504 if (buf
== e
|| (*e
&& *e
!= '\n') || max
<= 0)
3506 mddev
->sync_speed_max
= max
;
3510 static struct md_sysfs_entry md_sync_max
=
3511 __ATTR(sync_speed_max
, S_IRUGO
|S_IWUSR
, sync_max_show
, sync_max_store
);
3514 degraded_show(mddev_t
*mddev
, char *page
)
3516 return sprintf(page
, "%d\n", mddev
->degraded
);
3518 static struct md_sysfs_entry md_degraded
= __ATTR_RO(degraded
);
3521 sync_force_parallel_show(mddev_t
*mddev
, char *page
)
3523 return sprintf(page
, "%d\n", mddev
->parallel_resync
);
3527 sync_force_parallel_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3531 if (strict_strtol(buf
, 10, &n
))
3534 if (n
!= 0 && n
!= 1)
3537 mddev
->parallel_resync
= n
;
3539 if (mddev
->sync_thread
)
3540 wake_up(&resync_wait
);
3545 /* force parallel resync, even with shared block devices */
3546 static struct md_sysfs_entry md_sync_force_parallel
=
3547 __ATTR(sync_force_parallel
, S_IRUGO
|S_IWUSR
,
3548 sync_force_parallel_show
, sync_force_parallel_store
);
3551 sync_speed_show(mddev_t
*mddev
, char *page
)
3553 unsigned long resync
, dt
, db
;
3554 if (mddev
->curr_resync
== 0)
3555 return sprintf(page
, "none\n");
3556 resync
= mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
);
3557 dt
= (jiffies
- mddev
->resync_mark
) / HZ
;
3559 db
= resync
- mddev
->resync_mark_cnt
;
3560 return sprintf(page
, "%lu\n", db
/dt
/2); /* K/sec */
3563 static struct md_sysfs_entry md_sync_speed
= __ATTR_RO(sync_speed
);
3566 sync_completed_show(mddev_t
*mddev
, char *page
)
3568 unsigned long max_sectors
, resync
;
3570 if (!test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3571 return sprintf(page
, "none\n");
3573 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
3574 max_sectors
= mddev
->resync_max_sectors
;
3576 max_sectors
= mddev
->dev_sectors
;
3578 resync
= mddev
->curr_resync_completed
;
3579 return sprintf(page
, "%lu / %lu\n", resync
, max_sectors
);
3582 static struct md_sysfs_entry md_sync_completed
= __ATTR_RO(sync_completed
);
3585 min_sync_show(mddev_t
*mddev
, char *page
)
3587 return sprintf(page
, "%llu\n",
3588 (unsigned long long)mddev
->resync_min
);
3591 min_sync_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3593 unsigned long long min
;
3594 if (strict_strtoull(buf
, 10, &min
))
3596 if (min
> mddev
->resync_max
)
3598 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3601 /* Must be a multiple of chunk_size */
3602 if (mddev
->chunk_sectors
) {
3603 sector_t temp
= min
;
3604 if (sector_div(temp
, mddev
->chunk_sectors
))
3607 mddev
->resync_min
= min
;
3612 static struct md_sysfs_entry md_min_sync
=
3613 __ATTR(sync_min
, S_IRUGO
|S_IWUSR
, min_sync_show
, min_sync_store
);
3616 max_sync_show(mddev_t
*mddev
, char *page
)
3618 if (mddev
->resync_max
== MaxSector
)
3619 return sprintf(page
, "max\n");
3621 return sprintf(page
, "%llu\n",
3622 (unsigned long long)mddev
->resync_max
);
3625 max_sync_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3627 if (strncmp(buf
, "max", 3) == 0)
3628 mddev
->resync_max
= MaxSector
;
3630 unsigned long long max
;
3631 if (strict_strtoull(buf
, 10, &max
))
3633 if (max
< mddev
->resync_min
)
3635 if (max
< mddev
->resync_max
&&
3637 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3640 /* Must be a multiple of chunk_size */
3641 if (mddev
->chunk_sectors
) {
3642 sector_t temp
= max
;
3643 if (sector_div(temp
, mddev
->chunk_sectors
))
3646 mddev
->resync_max
= max
;
3648 wake_up(&mddev
->recovery_wait
);
3652 static struct md_sysfs_entry md_max_sync
=
3653 __ATTR(sync_max
, S_IRUGO
|S_IWUSR
, max_sync_show
, max_sync_store
);
3656 suspend_lo_show(mddev_t
*mddev
, char *page
)
3658 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_lo
);
3662 suspend_lo_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3665 unsigned long long new = simple_strtoull(buf
, &e
, 10);
3667 if (mddev
->pers
== NULL
||
3668 mddev
->pers
->quiesce
== NULL
)
3670 if (buf
== e
|| (*e
&& *e
!= '\n'))
3672 if (new >= mddev
->suspend_hi
||
3673 (new > mddev
->suspend_lo
&& new < mddev
->suspend_hi
)) {
3674 mddev
->suspend_lo
= new;
3675 mddev
->pers
->quiesce(mddev
, 2);
3680 static struct md_sysfs_entry md_suspend_lo
=
3681 __ATTR(suspend_lo
, S_IRUGO
|S_IWUSR
, suspend_lo_show
, suspend_lo_store
);
3685 suspend_hi_show(mddev_t
*mddev
, char *page
)
3687 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_hi
);
3691 suspend_hi_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3694 unsigned long long new = simple_strtoull(buf
, &e
, 10);
3696 if (mddev
->pers
== NULL
||
3697 mddev
->pers
->quiesce
== NULL
)
3699 if (buf
== e
|| (*e
&& *e
!= '\n'))
3701 if ((new <= mddev
->suspend_lo
&& mddev
->suspend_lo
>= mddev
->suspend_hi
) ||
3702 (new > mddev
->suspend_lo
&& new > mddev
->suspend_hi
)) {
3703 mddev
->suspend_hi
= new;
3704 mddev
->pers
->quiesce(mddev
, 1);
3705 mddev
->pers
->quiesce(mddev
, 0);
3710 static struct md_sysfs_entry md_suspend_hi
=
3711 __ATTR(suspend_hi
, S_IRUGO
|S_IWUSR
, suspend_hi_show
, suspend_hi_store
);
3714 reshape_position_show(mddev_t
*mddev
, char *page
)
3716 if (mddev
->reshape_position
!= MaxSector
)
3717 return sprintf(page
, "%llu\n",
3718 (unsigned long long)mddev
->reshape_position
);
3719 strcpy(page
, "none\n");
3724 reshape_position_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3727 unsigned long long new = simple_strtoull(buf
, &e
, 10);
3730 if (buf
== e
|| (*e
&& *e
!= '\n'))
3732 mddev
->reshape_position
= new;
3733 mddev
->delta_disks
= 0;
3734 mddev
->new_level
= mddev
->level
;
3735 mddev
->new_layout
= mddev
->layout
;
3736 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3740 static struct md_sysfs_entry md_reshape_position
=
3741 __ATTR(reshape_position
, S_IRUGO
|S_IWUSR
, reshape_position_show
,
3742 reshape_position_store
);
3745 array_size_show(mddev_t
*mddev
, char *page
)
3747 if (mddev
->external_size
)
3748 return sprintf(page
, "%llu\n",
3749 (unsigned long long)mddev
->array_sectors
/2);
3751 return sprintf(page
, "default\n");
3755 array_size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3759 if (strncmp(buf
, "default", 7) == 0) {
3761 sectors
= mddev
->pers
->size(mddev
, 0, 0);
3763 sectors
= mddev
->array_sectors
;
3765 mddev
->external_size
= 0;
3767 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
3769 if (mddev
->pers
&& mddev
->pers
->size(mddev
, 0, 0) < sectors
)
3772 mddev
->external_size
= 1;
3775 mddev
->array_sectors
= sectors
;
3776 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
3778 revalidate_disk(mddev
->gendisk
);
3783 static struct md_sysfs_entry md_array_size
=
3784 __ATTR(array_size
, S_IRUGO
|S_IWUSR
, array_size_show
,
3787 static struct attribute
*md_default_attrs
[] = {
3790 &md_raid_disks
.attr
,
3791 &md_chunk_size
.attr
,
3793 &md_resync_start
.attr
,
3795 &md_new_device
.attr
,
3796 &md_safe_delay
.attr
,
3797 &md_array_state
.attr
,
3798 &md_reshape_position
.attr
,
3799 &md_array_size
.attr
,
3803 static struct attribute
*md_redundancy_attrs
[] = {
3805 &md_mismatches
.attr
,
3808 &md_sync_speed
.attr
,
3809 &md_sync_force_parallel
.attr
,
3810 &md_sync_completed
.attr
,
3813 &md_suspend_lo
.attr
,
3814 &md_suspend_hi
.attr
,
3819 static struct attribute_group md_redundancy_group
= {
3821 .attrs
= md_redundancy_attrs
,
3826 md_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
3828 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
3829 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
3834 rv
= mddev_lock(mddev
);
3836 rv
= entry
->show(mddev
, page
);
3837 mddev_unlock(mddev
);
3843 md_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
3844 const char *page
, size_t length
)
3846 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
3847 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
3852 if (!capable(CAP_SYS_ADMIN
))
3854 rv
= mddev_lock(mddev
);
3855 if (mddev
->hold_active
== UNTIL_IOCTL
)
3856 mddev
->hold_active
= 0;
3858 rv
= entry
->store(mddev
, page
, length
);
3859 mddev_unlock(mddev
);
3864 static void md_free(struct kobject
*ko
)
3866 mddev_t
*mddev
= container_of(ko
, mddev_t
, kobj
);
3868 if (mddev
->sysfs_state
)
3869 sysfs_put(mddev
->sysfs_state
);
3871 if (mddev
->gendisk
) {
3872 del_gendisk(mddev
->gendisk
);
3873 put_disk(mddev
->gendisk
);
3876 blk_cleanup_queue(mddev
->queue
);
3881 static struct sysfs_ops md_sysfs_ops
= {
3882 .show
= md_attr_show
,
3883 .store
= md_attr_store
,
3885 static struct kobj_type md_ktype
= {
3887 .sysfs_ops
= &md_sysfs_ops
,
3888 .default_attrs
= md_default_attrs
,
3893 static void mddev_delayed_delete(struct work_struct
*ws
)
3895 mddev_t
*mddev
= container_of(ws
, mddev_t
, del_work
);
3897 if (mddev
->private == &md_redundancy_group
) {
3898 sysfs_remove_group(&mddev
->kobj
, &md_redundancy_group
);
3899 if (mddev
->sysfs_action
)
3900 sysfs_put(mddev
->sysfs_action
);
3901 mddev
->sysfs_action
= NULL
;
3902 mddev
->private = NULL
;
3904 kobject_del(&mddev
->kobj
);
3905 kobject_put(&mddev
->kobj
);
3908 static int md_alloc(dev_t dev
, char *name
)
3910 static DEFINE_MUTEX(disks_mutex
);
3911 mddev_t
*mddev
= mddev_find(dev
);
3912 struct gendisk
*disk
;
3921 partitioned
= (MAJOR(mddev
->unit
) != MD_MAJOR
);
3922 shift
= partitioned
? MdpMinorShift
: 0;
3923 unit
= MINOR(mddev
->unit
) >> shift
;
3925 /* wait for any previous instance if this device
3926 * to be completed removed (mddev_delayed_delete).
3928 flush_scheduled_work();
3930 mutex_lock(&disks_mutex
);
3936 /* Need to ensure that 'name' is not a duplicate.
3939 spin_lock(&all_mddevs_lock
);
3941 list_for_each_entry(mddev2
, &all_mddevs
, all_mddevs
)
3942 if (mddev2
->gendisk
&&
3943 strcmp(mddev2
->gendisk
->disk_name
, name
) == 0) {
3944 spin_unlock(&all_mddevs_lock
);
3947 spin_unlock(&all_mddevs_lock
);
3951 mddev
->queue
= blk_alloc_queue(GFP_KERNEL
);
3954 mddev
->queue
->queuedata
= mddev
;
3956 /* Can be unlocked because the queue is new: no concurrency */
3957 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER
, mddev
->queue
);
3959 blk_queue_make_request(mddev
->queue
, md_make_request
);
3961 disk
= alloc_disk(1 << shift
);
3963 blk_cleanup_queue(mddev
->queue
);
3964 mddev
->queue
= NULL
;
3967 disk
->major
= MAJOR(mddev
->unit
);
3968 disk
->first_minor
= unit
<< shift
;
3970 strcpy(disk
->disk_name
, name
);
3971 else if (partitioned
)
3972 sprintf(disk
->disk_name
, "md_d%d", unit
);
3974 sprintf(disk
->disk_name
, "md%d", unit
);
3975 disk
->fops
= &md_fops
;
3976 disk
->private_data
= mddev
;
3977 disk
->queue
= mddev
->queue
;
3978 /* Allow extended partitions. This makes the
3979 * 'mdp' device redundant, but we can't really
3982 disk
->flags
|= GENHD_FL_EXT_DEVT
;
3984 mddev
->gendisk
= disk
;
3985 error
= kobject_init_and_add(&mddev
->kobj
, &md_ktype
,
3986 &disk_to_dev(disk
)->kobj
, "%s", "md");
3988 /* This isn't possible, but as kobject_init_and_add is marked
3989 * __must_check, we must do something with the result
3991 printk(KERN_WARNING
"md: cannot register %s/md - name in use\n",
3996 mutex_unlock(&disks_mutex
);
3998 kobject_uevent(&mddev
->kobj
, KOBJ_ADD
);
3999 mddev
->sysfs_state
= sysfs_get_dirent(mddev
->kobj
.sd
, "array_state");
4005 static struct kobject
*md_probe(dev_t dev
, int *part
, void *data
)
4007 md_alloc(dev
, NULL
);
4011 static int add_named_array(const char *val
, struct kernel_param
*kp
)
4013 /* val must be "md_*" where * is not all digits.
4014 * We allocate an array with a large free minor number, and
4015 * set the name to val. val must not already be an active name.
4017 int len
= strlen(val
);
4018 char buf
[DISK_NAME_LEN
];
4020 while (len
&& val
[len
-1] == '\n')
4022 if (len
>= DISK_NAME_LEN
)
4024 strlcpy(buf
, val
, len
+1);
4025 if (strncmp(buf
, "md_", 3) != 0)
4027 return md_alloc(0, buf
);
4030 static void md_safemode_timeout(unsigned long data
)
4032 mddev_t
*mddev
= (mddev_t
*) data
;
4034 if (!atomic_read(&mddev
->writes_pending
)) {
4035 mddev
->safemode
= 1;
4036 if (mddev
->external
)
4037 sysfs_notify_dirent(mddev
->sysfs_state
);
4039 md_wakeup_thread(mddev
->thread
);
4042 static int start_dirty_degraded
;
4044 static int do_md_run(mddev_t
* mddev
)
4048 struct gendisk
*disk
;
4049 struct mdk_personality
*pers
;
4051 if (list_empty(&mddev
->disks
))
4052 /* cannot run an array with no devices.. */
4059 * Analyze all RAID superblock(s)
4061 if (!mddev
->raid_disks
) {
4062 if (!mddev
->persistent
)
4067 if (mddev
->level
!= LEVEL_NONE
)
4068 request_module("md-level-%d", mddev
->level
);
4069 else if (mddev
->clevel
[0])
4070 request_module("md-%s", mddev
->clevel
);
4073 * Drop all container device buffers, from now on
4074 * the only valid external interface is through the md
4077 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
4078 if (test_bit(Faulty
, &rdev
->flags
))
4080 sync_blockdev(rdev
->bdev
);
4081 invalidate_bdev(rdev
->bdev
);
4083 /* perform some consistency tests on the device.
4084 * We don't want the data to overlap the metadata,
4085 * Internal Bitmap issues have been handled elsewhere.
4087 if (rdev
->data_offset
< rdev
->sb_start
) {
4088 if (mddev
->dev_sectors
&&
4089 rdev
->data_offset
+ mddev
->dev_sectors
4091 printk("md: %s: data overlaps metadata\n",
4096 if (rdev
->sb_start
+ rdev
->sb_size
/512
4097 > rdev
->data_offset
) {
4098 printk("md: %s: metadata overlaps data\n",
4103 sysfs_notify_dirent(rdev
->sysfs_state
);
4106 md_probe(mddev
->unit
, NULL
, NULL
);
4107 disk
= mddev
->gendisk
;
4111 spin_lock(&pers_lock
);
4112 pers
= find_pers(mddev
->level
, mddev
->clevel
);
4113 if (!pers
|| !try_module_get(pers
->owner
)) {
4114 spin_unlock(&pers_lock
);
4115 if (mddev
->level
!= LEVEL_NONE
)
4116 printk(KERN_WARNING
"md: personality for level %d is not loaded!\n",
4119 printk(KERN_WARNING
"md: personality for level %s is not loaded!\n",
4124 spin_unlock(&pers_lock
);
4125 if (mddev
->level
!= pers
->level
) {
4126 mddev
->level
= pers
->level
;
4127 mddev
->new_level
= pers
->level
;
4129 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
4131 if (mddev
->reshape_position
!= MaxSector
&&
4132 pers
->start_reshape
== NULL
) {
4133 /* This personality cannot handle reshaping... */
4135 module_put(pers
->owner
);
4139 if (pers
->sync_request
) {
4140 /* Warn if this is a potentially silly
4143 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
4147 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4148 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
4150 rdev
->bdev
->bd_contains
==
4151 rdev2
->bdev
->bd_contains
) {
4153 "%s: WARNING: %s appears to be"
4154 " on the same physical disk as"
4157 bdevname(rdev
->bdev
,b
),
4158 bdevname(rdev2
->bdev
,b2
));
4165 "True protection against single-disk"
4166 " failure might be compromised.\n");
4169 mddev
->recovery
= 0;
4170 /* may be over-ridden by personality */
4171 mddev
->resync_max_sectors
= mddev
->dev_sectors
;
4173 mddev
->barriers_work
= 1;
4174 mddev
->ok_start_degraded
= start_dirty_degraded
;
4176 if (start_readonly
&& mddev
->ro
== 0)
4177 mddev
->ro
= 2; /* read-only, but switch on first write */
4179 err
= mddev
->pers
->run(mddev
);
4181 printk(KERN_ERR
"md: pers->run() failed ...\n");
4182 else if (mddev
->pers
->size(mddev
, 0, 0) < mddev
->array_sectors
) {
4183 WARN_ONCE(!mddev
->external_size
, "%s: default size too small,"
4184 " but 'external_size' not in effect?\n", __func__
);
4186 "md: invalid array_size %llu > default size %llu\n",
4187 (unsigned long long)mddev
->array_sectors
/ 2,
4188 (unsigned long long)mddev
->pers
->size(mddev
, 0, 0) / 2);
4190 mddev
->pers
->stop(mddev
);
4192 if (err
== 0 && mddev
->pers
->sync_request
) {
4193 err
= bitmap_create(mddev
);
4195 printk(KERN_ERR
"%s: failed to create bitmap (%d)\n",
4196 mdname(mddev
), err
);
4197 mddev
->pers
->stop(mddev
);
4201 module_put(mddev
->pers
->owner
);
4203 bitmap_destroy(mddev
);
4206 if (mddev
->pers
->sync_request
) {
4207 if (sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
4209 "md: cannot register extra attributes for %s\n",
4211 mddev
->sysfs_action
= sysfs_get_dirent(mddev
->kobj
.sd
, "sync_action");
4212 } else if (mddev
->ro
== 2) /* auto-readonly not meaningful */
4215 atomic_set(&mddev
->writes_pending
,0);
4216 mddev
->safemode
= 0;
4217 mddev
->safemode_timer
.function
= md_safemode_timeout
;
4218 mddev
->safemode_timer
.data
= (unsigned long) mddev
;
4219 mddev
->safemode_delay
= (200 * HZ
)/1000 +1; /* 200 msec delay */
4222 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4223 if (rdev
->raid_disk
>= 0) {
4225 sprintf(nm
, "rd%d", rdev
->raid_disk
);
4226 if (sysfs_create_link(&mddev
->kobj
, &rdev
->kobj
, nm
))
4227 printk("md: cannot register %s for %s\n",
4231 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4234 md_update_sb(mddev
, 0);
4236 set_capacity(disk
, mddev
->array_sectors
);
4238 /* If there is a partially-recovered drive we need to
4239 * start recovery here. If we leave it to md_check_recovery,
4240 * it will remove the drives and not do the right thing
4242 if (mddev
->degraded
&& !mddev
->sync_thread
) {
4244 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4245 if (rdev
->raid_disk
>= 0 &&
4246 !test_bit(In_sync
, &rdev
->flags
) &&
4247 !test_bit(Faulty
, &rdev
->flags
))
4248 /* complete an interrupted recovery */
4250 if (spares
&& mddev
->pers
->sync_request
) {
4251 mddev
->recovery
= 0;
4252 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
4253 mddev
->sync_thread
= md_register_thread(md_do_sync
,
4256 if (!mddev
->sync_thread
) {
4257 printk(KERN_ERR
"%s: could not start resync"
4260 /* leave the spares where they are, it shouldn't hurt */
4261 mddev
->recovery
= 0;
4265 md_wakeup_thread(mddev
->thread
);
4266 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
4268 revalidate_disk(mddev
->gendisk
);
4270 md_new_event(mddev
);
4271 sysfs_notify_dirent(mddev
->sysfs_state
);
4272 if (mddev
->sysfs_action
)
4273 sysfs_notify_dirent(mddev
->sysfs_action
);
4274 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
4275 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
4279 static int restart_array(mddev_t
*mddev
)
4281 struct gendisk
*disk
= mddev
->gendisk
;
4283 /* Complain if it has no devices */
4284 if (list_empty(&mddev
->disks
))
4290 mddev
->safemode
= 0;
4292 set_disk_ro(disk
, 0);
4293 printk(KERN_INFO
"md: %s switched to read-write mode.\n",
4295 /* Kick recovery or resync if necessary */
4296 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4297 md_wakeup_thread(mddev
->thread
);
4298 md_wakeup_thread(mddev
->sync_thread
);
4299 sysfs_notify_dirent(mddev
->sysfs_state
);
4303 /* similar to deny_write_access, but accounts for our holding a reference
4304 * to the file ourselves */
4305 static int deny_bitmap_write_access(struct file
* file
)
4307 struct inode
*inode
= file
->f_mapping
->host
;
4309 spin_lock(&inode
->i_lock
);
4310 if (atomic_read(&inode
->i_writecount
) > 1) {
4311 spin_unlock(&inode
->i_lock
);
4314 atomic_set(&inode
->i_writecount
, -1);
4315 spin_unlock(&inode
->i_lock
);
4320 static void restore_bitmap_write_access(struct file
*file
)
4322 struct inode
*inode
= file
->f_mapping
->host
;
4324 spin_lock(&inode
->i_lock
);
4325 atomic_set(&inode
->i_writecount
, 1);
4326 spin_unlock(&inode
->i_lock
);
4330 * 0 - completely stop and dis-assemble array
4331 * 1 - switch to readonly
4332 * 2 - stop but do not disassemble array
4334 static int do_md_stop(mddev_t
* mddev
, int mode
, int is_open
)
4337 struct gendisk
*disk
= mddev
->gendisk
;
4340 mutex_lock(&mddev
->open_mutex
);
4341 if (atomic_read(&mddev
->openers
) > is_open
) {
4342 printk("md: %s still in use.\n",mdname(mddev
));
4344 } else if (mddev
->pers
) {
4346 if (mddev
->sync_thread
) {
4347 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4348 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4349 md_unregister_thread(mddev
->sync_thread
);
4350 mddev
->sync_thread
= NULL
;
4353 del_timer_sync(&mddev
->safemode_timer
);
4356 case 1: /* readonly */
4362 case 0: /* disassemble */
4364 bitmap_flush(mddev
);
4365 md_super_wait(mddev
);
4367 set_disk_ro(disk
, 0);
4369 mddev
->pers
->stop(mddev
);
4370 mddev
->queue
->merge_bvec_fn
= NULL
;
4371 mddev
->queue
->unplug_fn
= NULL
;
4372 mddev
->queue
->backing_dev_info
.congested_fn
= NULL
;
4373 module_put(mddev
->pers
->owner
);
4374 if (mddev
->pers
->sync_request
)
4375 mddev
->private = &md_redundancy_group
;
4377 /* tell userspace to handle 'inactive' */
4378 sysfs_notify_dirent(mddev
->sysfs_state
);
4380 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4381 if (rdev
->raid_disk
>= 0) {
4383 sprintf(nm
, "rd%d", rdev
->raid_disk
);
4384 sysfs_remove_link(&mddev
->kobj
, nm
);
4387 set_capacity(disk
, 0);
4393 if (!mddev
->in_sync
|| mddev
->flags
) {
4394 /* mark array as shutdown cleanly */
4396 md_update_sb(mddev
, 1);
4399 set_disk_ro(disk
, 1);
4400 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4404 mutex_unlock(&mddev
->open_mutex
);
4408 * Free resources if final stop
4412 printk(KERN_INFO
"md: %s stopped.\n", mdname(mddev
));
4414 bitmap_destroy(mddev
);
4415 if (mddev
->bitmap_file
) {
4416 restore_bitmap_write_access(mddev
->bitmap_file
);
4417 fput(mddev
->bitmap_file
);
4418 mddev
->bitmap_file
= NULL
;
4420 mddev
->bitmap_offset
= 0;
4422 /* make sure all md_delayed_delete calls have finished */
4423 flush_scheduled_work();
4425 export_array(mddev
);
4427 mddev
->array_sectors
= 0;
4428 mddev
->external_size
= 0;
4429 mddev
->dev_sectors
= 0;
4430 mddev
->raid_disks
= 0;
4431 mddev
->recovery_cp
= 0;
4432 mddev
->resync_min
= 0;
4433 mddev
->resync_max
= MaxSector
;
4434 mddev
->reshape_position
= MaxSector
;
4435 mddev
->external
= 0;
4436 mddev
->persistent
= 0;
4437 mddev
->level
= LEVEL_NONE
;
4438 mddev
->clevel
[0] = 0;
4441 mddev
->metadata_type
[0] = 0;
4442 mddev
->chunk_sectors
= 0;
4443 mddev
->ctime
= mddev
->utime
= 0;
4445 mddev
->max_disks
= 0;
4447 mddev
->delta_disks
= 0;
4448 mddev
->new_level
= LEVEL_NONE
;
4449 mddev
->new_layout
= 0;
4450 mddev
->new_chunk_sectors
= 0;
4451 mddev
->curr_resync
= 0;
4452 mddev
->resync_mismatches
= 0;
4453 mddev
->suspend_lo
= mddev
->suspend_hi
= 0;
4454 mddev
->sync_speed_min
= mddev
->sync_speed_max
= 0;
4455 mddev
->recovery
= 0;
4458 mddev
->degraded
= 0;
4459 mddev
->barriers_work
= 0;
4460 mddev
->safemode
= 0;
4461 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
4462 if (mddev
->hold_active
== UNTIL_STOP
)
4463 mddev
->hold_active
= 0;
4465 } else if (mddev
->pers
)
4466 printk(KERN_INFO
"md: %s switched to read-only mode.\n",
4469 blk_integrity_unregister(disk
);
4470 md_new_event(mddev
);
4471 sysfs_notify_dirent(mddev
->sysfs_state
);
4476 static void autorun_array(mddev_t
*mddev
)
4481 if (list_empty(&mddev
->disks
))
4484 printk(KERN_INFO
"md: running: ");
4486 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
4487 char b
[BDEVNAME_SIZE
];
4488 printk("<%s>", bdevname(rdev
->bdev
,b
));
4492 err
= do_md_run(mddev
);
4494 printk(KERN_WARNING
"md: do_md_run() returned %d\n", err
);
4495 do_md_stop(mddev
, 0, 0);
4500 * lets try to run arrays based on all disks that have arrived
4501 * until now. (those are in pending_raid_disks)
4503 * the method: pick the first pending disk, collect all disks with
4504 * the same UUID, remove all from the pending list and put them into
4505 * the 'same_array' list. Then order this list based on superblock
4506 * update time (freshest comes first), kick out 'old' disks and
4507 * compare superblocks. If everything's fine then run it.
4509 * If "unit" is allocated, then bump its reference count
4511 static void autorun_devices(int part
)
4513 mdk_rdev_t
*rdev0
, *rdev
, *tmp
;
4515 char b
[BDEVNAME_SIZE
];
4517 printk(KERN_INFO
"md: autorun ...\n");
4518 while (!list_empty(&pending_raid_disks
)) {
4521 LIST_HEAD(candidates
);
4522 rdev0
= list_entry(pending_raid_disks
.next
,
4523 mdk_rdev_t
, same_set
);
4525 printk(KERN_INFO
"md: considering %s ...\n",
4526 bdevname(rdev0
->bdev
,b
));
4527 INIT_LIST_HEAD(&candidates
);
4528 rdev_for_each_list(rdev
, tmp
, &pending_raid_disks
)
4529 if (super_90_load(rdev
, rdev0
, 0) >= 0) {
4530 printk(KERN_INFO
"md: adding %s ...\n",
4531 bdevname(rdev
->bdev
,b
));
4532 list_move(&rdev
->same_set
, &candidates
);
4535 * now we have a set of devices, with all of them having
4536 * mostly sane superblocks. It's time to allocate the
4540 dev
= MKDEV(mdp_major
,
4541 rdev0
->preferred_minor
<< MdpMinorShift
);
4542 unit
= MINOR(dev
) >> MdpMinorShift
;
4544 dev
= MKDEV(MD_MAJOR
, rdev0
->preferred_minor
);
4547 if (rdev0
->preferred_minor
!= unit
) {
4548 printk(KERN_INFO
"md: unit number in %s is bad: %d\n",
4549 bdevname(rdev0
->bdev
, b
), rdev0
->preferred_minor
);
4553 md_probe(dev
, NULL
, NULL
);
4554 mddev
= mddev_find(dev
);
4555 if (!mddev
|| !mddev
->gendisk
) {
4559 "md: cannot allocate memory for md drive.\n");
4562 if (mddev_lock(mddev
))
4563 printk(KERN_WARNING
"md: %s locked, cannot run\n",
4565 else if (mddev
->raid_disks
|| mddev
->major_version
4566 || !list_empty(&mddev
->disks
)) {
4568 "md: %s already running, cannot run %s\n",
4569 mdname(mddev
), bdevname(rdev0
->bdev
,b
));
4570 mddev_unlock(mddev
);
4572 printk(KERN_INFO
"md: created %s\n", mdname(mddev
));
4573 mddev
->persistent
= 1;
4574 rdev_for_each_list(rdev
, tmp
, &candidates
) {
4575 list_del_init(&rdev
->same_set
);
4576 if (bind_rdev_to_array(rdev
, mddev
))
4579 autorun_array(mddev
);
4580 mddev_unlock(mddev
);
4582 /* on success, candidates will be empty, on error
4585 rdev_for_each_list(rdev
, tmp
, &candidates
) {
4586 list_del_init(&rdev
->same_set
);
4591 printk(KERN_INFO
"md: ... autorun DONE.\n");
4593 #endif /* !MODULE */
4595 static int get_version(void __user
* arg
)
4599 ver
.major
= MD_MAJOR_VERSION
;
4600 ver
.minor
= MD_MINOR_VERSION
;
4601 ver
.patchlevel
= MD_PATCHLEVEL_VERSION
;
4603 if (copy_to_user(arg
, &ver
, sizeof(ver
)))
4609 static int get_array_info(mddev_t
* mddev
, void __user
* arg
)
4611 mdu_array_info_t info
;
4612 int nr
,working
,insync
,failed
,spare
;
4615 nr
=working
=insync
=failed
=spare
=0;
4616 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
4618 if (test_bit(Faulty
, &rdev
->flags
))
4622 if (test_bit(In_sync
, &rdev
->flags
))
4629 info
.major_version
= mddev
->major_version
;
4630 info
.minor_version
= mddev
->minor_version
;
4631 info
.patch_version
= MD_PATCHLEVEL_VERSION
;
4632 info
.ctime
= mddev
->ctime
;
4633 info
.level
= mddev
->level
;
4634 info
.size
= mddev
->dev_sectors
/ 2;
4635 if (info
.size
!= mddev
->dev_sectors
/ 2) /* overflow */
4638 info
.raid_disks
= mddev
->raid_disks
;
4639 info
.md_minor
= mddev
->md_minor
;
4640 info
.not_persistent
= !mddev
->persistent
;
4642 info
.utime
= mddev
->utime
;
4645 info
.state
= (1<<MD_SB_CLEAN
);
4646 if (mddev
->bitmap
&& mddev
->bitmap_offset
)
4647 info
.state
= (1<<MD_SB_BITMAP_PRESENT
);
4648 info
.active_disks
= insync
;
4649 info
.working_disks
= working
;
4650 info
.failed_disks
= failed
;
4651 info
.spare_disks
= spare
;
4653 info
.layout
= mddev
->layout
;
4654 info
.chunk_size
= mddev
->chunk_sectors
<< 9;
4656 if (copy_to_user(arg
, &info
, sizeof(info
)))
4662 static int get_bitmap_file(mddev_t
* mddev
, void __user
* arg
)
4664 mdu_bitmap_file_t
*file
= NULL
; /* too big for stack allocation */
4665 char *ptr
, *buf
= NULL
;
4668 if (md_allow_write(mddev
))
4669 file
= kmalloc(sizeof(*file
), GFP_NOIO
);
4671 file
= kmalloc(sizeof(*file
), GFP_KERNEL
);
4676 /* bitmap disabled, zero the first byte and copy out */
4677 if (!mddev
->bitmap
|| !mddev
->bitmap
->file
) {
4678 file
->pathname
[0] = '\0';
4682 buf
= kmalloc(sizeof(file
->pathname
), GFP_KERNEL
);
4686 ptr
= d_path(&mddev
->bitmap
->file
->f_path
, buf
, sizeof(file
->pathname
));
4690 strcpy(file
->pathname
, ptr
);
4694 if (copy_to_user(arg
, file
, sizeof(*file
)))
4702 static int get_disk_info(mddev_t
* mddev
, void __user
* arg
)
4704 mdu_disk_info_t info
;
4707 if (copy_from_user(&info
, arg
, sizeof(info
)))
4710 rdev
= find_rdev_nr(mddev
, info
.number
);
4712 info
.major
= MAJOR(rdev
->bdev
->bd_dev
);
4713 info
.minor
= MINOR(rdev
->bdev
->bd_dev
);
4714 info
.raid_disk
= rdev
->raid_disk
;
4716 if (test_bit(Faulty
, &rdev
->flags
))
4717 info
.state
|= (1<<MD_DISK_FAULTY
);
4718 else if (test_bit(In_sync
, &rdev
->flags
)) {
4719 info
.state
|= (1<<MD_DISK_ACTIVE
);
4720 info
.state
|= (1<<MD_DISK_SYNC
);
4722 if (test_bit(WriteMostly
, &rdev
->flags
))
4723 info
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
4725 info
.major
= info
.minor
= 0;
4726 info
.raid_disk
= -1;
4727 info
.state
= (1<<MD_DISK_REMOVED
);
4730 if (copy_to_user(arg
, &info
, sizeof(info
)))
4736 static int add_new_disk(mddev_t
* mddev
, mdu_disk_info_t
*info
)
4738 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
4740 dev_t dev
= MKDEV(info
->major
,info
->minor
);
4742 if (info
->major
!= MAJOR(dev
) || info
->minor
!= MINOR(dev
))
4745 if (!mddev
->raid_disks
) {
4747 /* expecting a device which has a superblock */
4748 rdev
= md_import_device(dev
, mddev
->major_version
, mddev
->minor_version
);
4751 "md: md_import_device returned %ld\n",
4753 return PTR_ERR(rdev
);
4755 if (!list_empty(&mddev
->disks
)) {
4756 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
4757 mdk_rdev_t
, same_set
);
4758 err
= super_types
[mddev
->major_version
]
4759 .load_super(rdev
, rdev0
, mddev
->minor_version
);
4762 "md: %s has different UUID to %s\n",
4763 bdevname(rdev
->bdev
,b
),
4764 bdevname(rdev0
->bdev
,b2
));
4769 err
= bind_rdev_to_array(rdev
, mddev
);
4776 * add_new_disk can be used once the array is assembled
4777 * to add "hot spares". They must already have a superblock
4782 if (!mddev
->pers
->hot_add_disk
) {
4784 "%s: personality does not support diskops!\n",
4788 if (mddev
->persistent
)
4789 rdev
= md_import_device(dev
, mddev
->major_version
,
4790 mddev
->minor_version
);
4792 rdev
= md_import_device(dev
, -1, -1);
4795 "md: md_import_device returned %ld\n",
4797 return PTR_ERR(rdev
);
4799 /* set save_raid_disk if appropriate */
4800 if (!mddev
->persistent
) {
4801 if (info
->state
& (1<<MD_DISK_SYNC
) &&
4802 info
->raid_disk
< mddev
->raid_disks
)
4803 rdev
->raid_disk
= info
->raid_disk
;
4805 rdev
->raid_disk
= -1;
4807 super_types
[mddev
->major_version
].
4808 validate_super(mddev
, rdev
);
4809 rdev
->saved_raid_disk
= rdev
->raid_disk
;
4811 clear_bit(In_sync
, &rdev
->flags
); /* just to be sure */
4812 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
4813 set_bit(WriteMostly
, &rdev
->flags
);
4815 clear_bit(WriteMostly
, &rdev
->flags
);
4817 rdev
->raid_disk
= -1;
4818 err
= bind_rdev_to_array(rdev
, mddev
);
4819 if (!err
&& !mddev
->pers
->hot_remove_disk
) {
4820 /* If there is hot_add_disk but no hot_remove_disk
4821 * then added disks for geometry changes,
4822 * and should be added immediately.
4824 super_types
[mddev
->major_version
].
4825 validate_super(mddev
, rdev
);
4826 err
= mddev
->pers
->hot_add_disk(mddev
, rdev
);
4828 unbind_rdev_from_array(rdev
);
4833 sysfs_notify_dirent(rdev
->sysfs_state
);
4835 md_update_sb(mddev
, 1);
4836 if (mddev
->degraded
)
4837 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
4838 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4839 md_wakeup_thread(mddev
->thread
);
4843 /* otherwise, add_new_disk is only allowed
4844 * for major_version==0 superblocks
4846 if (mddev
->major_version
!= 0) {
4847 printk(KERN_WARNING
"%s: ADD_NEW_DISK not supported\n",
4852 if (!(info
->state
& (1<<MD_DISK_FAULTY
))) {
4854 rdev
= md_import_device(dev
, -1, 0);
4857 "md: error, md_import_device() returned %ld\n",
4859 return PTR_ERR(rdev
);
4861 rdev
->desc_nr
= info
->number
;
4862 if (info
->raid_disk
< mddev
->raid_disks
)
4863 rdev
->raid_disk
= info
->raid_disk
;
4865 rdev
->raid_disk
= -1;
4867 if (rdev
->raid_disk
< mddev
->raid_disks
)
4868 if (info
->state
& (1<<MD_DISK_SYNC
))
4869 set_bit(In_sync
, &rdev
->flags
);
4871 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
4872 set_bit(WriteMostly
, &rdev
->flags
);
4874 if (!mddev
->persistent
) {
4875 printk(KERN_INFO
"md: nonpersistent superblock ...\n");
4876 rdev
->sb_start
= rdev
->bdev
->bd_inode
->i_size
/ 512;
4878 rdev
->sb_start
= calc_dev_sboffset(rdev
->bdev
);
4879 rdev
->sectors
= rdev
->sb_start
;
4881 err
= bind_rdev_to_array(rdev
, mddev
);
4891 static int hot_remove_disk(mddev_t
* mddev
, dev_t dev
)
4893 char b
[BDEVNAME_SIZE
];
4896 rdev
= find_rdev(mddev
, dev
);
4900 if (rdev
->raid_disk
>= 0)
4903 kick_rdev_from_array(rdev
);
4904 md_update_sb(mddev
, 1);
4905 md_new_event(mddev
);
4909 printk(KERN_WARNING
"md: cannot remove active disk %s from %s ...\n",
4910 bdevname(rdev
->bdev
,b
), mdname(mddev
));
4914 static int hot_add_disk(mddev_t
* mddev
, dev_t dev
)
4916 char b
[BDEVNAME_SIZE
];
4923 if (mddev
->major_version
!= 0) {
4924 printk(KERN_WARNING
"%s: HOT_ADD may only be used with"
4925 " version-0 superblocks.\n",
4929 if (!mddev
->pers
->hot_add_disk
) {
4931 "%s: personality does not support diskops!\n",
4936 rdev
= md_import_device(dev
, -1, 0);
4939 "md: error, md_import_device() returned %ld\n",
4944 if (mddev
->persistent
)
4945 rdev
->sb_start
= calc_dev_sboffset(rdev
->bdev
);
4947 rdev
->sb_start
= rdev
->bdev
->bd_inode
->i_size
/ 512;
4949 rdev
->sectors
= rdev
->sb_start
;
4951 if (test_bit(Faulty
, &rdev
->flags
)) {
4953 "md: can not hot-add faulty %s disk to %s!\n",
4954 bdevname(rdev
->bdev
,b
), mdname(mddev
));
4958 clear_bit(In_sync
, &rdev
->flags
);
4960 rdev
->saved_raid_disk
= -1;
4961 err
= bind_rdev_to_array(rdev
, mddev
);
4966 * The rest should better be atomic, we can have disk failures
4967 * noticed in interrupt contexts ...
4970 rdev
->raid_disk
= -1;
4972 md_update_sb(mddev
, 1);
4975 * Kick recovery, maybe this spare has to be added to the
4976 * array immediately.
4978 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4979 md_wakeup_thread(mddev
->thread
);
4980 md_new_event(mddev
);
4988 static int set_bitmap_file(mddev_t
*mddev
, int fd
)
4993 if (!mddev
->pers
->quiesce
)
4995 if (mddev
->recovery
|| mddev
->sync_thread
)
4997 /* we should be able to change the bitmap.. */
5003 return -EEXIST
; /* cannot add when bitmap is present */
5004 mddev
->bitmap_file
= fget(fd
);
5006 if (mddev
->bitmap_file
== NULL
) {
5007 printk(KERN_ERR
"%s: error: failed to get bitmap file\n",
5012 err
= deny_bitmap_write_access(mddev
->bitmap_file
);
5014 printk(KERN_ERR
"%s: error: bitmap file is already in use\n",
5016 fput(mddev
->bitmap_file
);
5017 mddev
->bitmap_file
= NULL
;
5020 mddev
->bitmap_offset
= 0; /* file overrides offset */
5021 } else if (mddev
->bitmap
== NULL
)
5022 return -ENOENT
; /* cannot remove what isn't there */
5025 mddev
->pers
->quiesce(mddev
, 1);
5027 err
= bitmap_create(mddev
);
5028 if (fd
< 0 || err
) {
5029 bitmap_destroy(mddev
);
5030 fd
= -1; /* make sure to put the file */
5032 mddev
->pers
->quiesce(mddev
, 0);
5035 if (mddev
->bitmap_file
) {
5036 restore_bitmap_write_access(mddev
->bitmap_file
);
5037 fput(mddev
->bitmap_file
);
5039 mddev
->bitmap_file
= NULL
;
5046 * set_array_info is used two different ways
5047 * The original usage is when creating a new array.
5048 * In this usage, raid_disks is > 0 and it together with
5049 * level, size, not_persistent,layout,chunksize determine the
5050 * shape of the array.
5051 * This will always create an array with a type-0.90.0 superblock.
5052 * The newer usage is when assembling an array.
5053 * In this case raid_disks will be 0, and the major_version field is
5054 * use to determine which style super-blocks are to be found on the devices.
5055 * The minor and patch _version numbers are also kept incase the
5056 * super_block handler wishes to interpret them.
5058 static int set_array_info(mddev_t
* mddev
, mdu_array_info_t
*info
)
5061 if (info
->raid_disks
== 0) {
5062 /* just setting version number for superblock loading */
5063 if (info
->major_version
< 0 ||
5064 info
->major_version
>= ARRAY_SIZE(super_types
) ||
5065 super_types
[info
->major_version
].name
== NULL
) {
5066 /* maybe try to auto-load a module? */
5068 "md: superblock version %d not known\n",
5069 info
->major_version
);
5072 mddev
->major_version
= info
->major_version
;
5073 mddev
->minor_version
= info
->minor_version
;
5074 mddev
->patch_version
= info
->patch_version
;
5075 mddev
->persistent
= !info
->not_persistent
;
5076 /* ensure mddev_put doesn't delete this now that there
5077 * is some minimal configuration.
5079 mddev
->ctime
= get_seconds();
5082 mddev
->major_version
= MD_MAJOR_VERSION
;
5083 mddev
->minor_version
= MD_MINOR_VERSION
;
5084 mddev
->patch_version
= MD_PATCHLEVEL_VERSION
;
5085 mddev
->ctime
= get_seconds();
5087 mddev
->level
= info
->level
;
5088 mddev
->clevel
[0] = 0;
5089 mddev
->dev_sectors
= 2 * (sector_t
)info
->size
;
5090 mddev
->raid_disks
= info
->raid_disks
;
5091 /* don't set md_minor, it is determined by which /dev/md* was
5094 if (info
->state
& (1<<MD_SB_CLEAN
))
5095 mddev
->recovery_cp
= MaxSector
;
5097 mddev
->recovery_cp
= 0;
5098 mddev
->persistent
= ! info
->not_persistent
;
5099 mddev
->external
= 0;
5101 mddev
->layout
= info
->layout
;
5102 mddev
->chunk_sectors
= info
->chunk_size
>> 9;
5104 mddev
->max_disks
= MD_SB_DISKS
;
5106 if (mddev
->persistent
)
5108 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
5110 mddev
->default_bitmap_offset
= MD_SB_BYTES
>> 9;
5111 mddev
->bitmap_offset
= 0;
5113 mddev
->reshape_position
= MaxSector
;
5116 * Generate a 128 bit UUID
5118 get_random_bytes(mddev
->uuid
, 16);
5120 mddev
->new_level
= mddev
->level
;
5121 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
5122 mddev
->new_layout
= mddev
->layout
;
5123 mddev
->delta_disks
= 0;
5128 void md_set_array_sectors(mddev_t
*mddev
, sector_t array_sectors
)
5130 WARN(!mddev_is_locked(mddev
), "%s: unlocked mddev!\n", __func__
);
5132 if (mddev
->external_size
)
5135 mddev
->array_sectors
= array_sectors
;
5137 EXPORT_SYMBOL(md_set_array_sectors
);
5139 static int update_size(mddev_t
*mddev
, sector_t num_sectors
)
5143 int fit
= (num_sectors
== 0);
5145 if (mddev
->pers
->resize
== NULL
)
5147 /* The "num_sectors" is the number of sectors of each device that
5148 * is used. This can only make sense for arrays with redundancy.
5149 * linear and raid0 always use whatever space is available. We can only
5150 * consider changing this number if no resync or reconstruction is
5151 * happening, and if the new size is acceptable. It must fit before the
5152 * sb_start or, if that is <data_offset, it must fit before the size
5153 * of each device. If num_sectors is zero, we find the largest size
5157 if (mddev
->sync_thread
)
5160 /* Sorry, cannot grow a bitmap yet, just remove it,
5164 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
5165 sector_t avail
= rdev
->sectors
;
5167 if (fit
&& (num_sectors
== 0 || num_sectors
> avail
))
5168 num_sectors
= avail
;
5169 if (avail
< num_sectors
)
5172 rv
= mddev
->pers
->resize(mddev
, num_sectors
);
5174 revalidate_disk(mddev
->gendisk
);
5178 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
)
5181 /* change the number of raid disks */
5182 if (mddev
->pers
->check_reshape
== NULL
)
5184 if (raid_disks
<= 0 ||
5185 raid_disks
>= mddev
->max_disks
)
5187 if (mddev
->sync_thread
|| mddev
->reshape_position
!= MaxSector
)
5189 mddev
->delta_disks
= raid_disks
- mddev
->raid_disks
;
5191 rv
= mddev
->pers
->check_reshape(mddev
);
5197 * update_array_info is used to change the configuration of an
5199 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5200 * fields in the info are checked against the array.
5201 * Any differences that cannot be handled will cause an error.
5202 * Normally, only one change can be managed at a time.
5204 static int update_array_info(mddev_t
*mddev
, mdu_array_info_t
*info
)
5210 /* calculate expected state,ignoring low bits */
5211 if (mddev
->bitmap
&& mddev
->bitmap_offset
)
5212 state
|= (1 << MD_SB_BITMAP_PRESENT
);
5214 if (mddev
->major_version
!= info
->major_version
||
5215 mddev
->minor_version
!= info
->minor_version
||
5216 /* mddev->patch_version != info->patch_version || */
5217 mddev
->ctime
!= info
->ctime
||
5218 mddev
->level
!= info
->level
||
5219 /* mddev->layout != info->layout || */
5220 !mddev
->persistent
!= info
->not_persistent
||
5221 mddev
->chunk_sectors
!= info
->chunk_size
>> 9 ||
5222 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5223 ((state
^info
->state
) & 0xfffffe00)
5226 /* Check there is only one change */
5227 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
5229 if (mddev
->raid_disks
!= info
->raid_disks
)
5231 if (mddev
->layout
!= info
->layout
)
5233 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
))
5240 if (mddev
->layout
!= info
->layout
) {
5242 * we don't need to do anything at the md level, the
5243 * personality will take care of it all.
5245 if (mddev
->pers
->check_reshape
== NULL
)
5248 mddev
->new_layout
= info
->layout
;
5249 rv
= mddev
->pers
->check_reshape(mddev
);
5251 mddev
->new_layout
= mddev
->layout
;
5255 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
5256 rv
= update_size(mddev
, (sector_t
)info
->size
* 2);
5258 if (mddev
->raid_disks
!= info
->raid_disks
)
5259 rv
= update_raid_disks(mddev
, info
->raid_disks
);
5261 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) {
5262 if (mddev
->pers
->quiesce
== NULL
)
5264 if (mddev
->recovery
|| mddev
->sync_thread
)
5266 if (info
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
5267 /* add the bitmap */
5270 if (mddev
->default_bitmap_offset
== 0)
5272 mddev
->bitmap_offset
= mddev
->default_bitmap_offset
;
5273 mddev
->pers
->quiesce(mddev
, 1);
5274 rv
= bitmap_create(mddev
);
5276 bitmap_destroy(mddev
);
5277 mddev
->pers
->quiesce(mddev
, 0);
5279 /* remove the bitmap */
5282 if (mddev
->bitmap
->file
)
5284 mddev
->pers
->quiesce(mddev
, 1);
5285 bitmap_destroy(mddev
);
5286 mddev
->pers
->quiesce(mddev
, 0);
5287 mddev
->bitmap_offset
= 0;
5290 md_update_sb(mddev
, 1);
5294 static int set_disk_faulty(mddev_t
*mddev
, dev_t dev
)
5298 if (mddev
->pers
== NULL
)
5301 rdev
= find_rdev(mddev
, dev
);
5305 md_error(mddev
, rdev
);
5310 * We have a problem here : there is no easy way to give a CHS
5311 * virtual geometry. We currently pretend that we have a 2 heads
5312 * 4 sectors (with a BIG number of cylinders...). This drives
5313 * dosfs just mad... ;-)
5315 static int md_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
5317 mddev_t
*mddev
= bdev
->bd_disk
->private_data
;
5321 geo
->cylinders
= get_capacity(mddev
->gendisk
) / 8;
5325 static int md_ioctl(struct block_device
*bdev
, fmode_t mode
,
5326 unsigned int cmd
, unsigned long arg
)
5329 void __user
*argp
= (void __user
*)arg
;
5330 mddev_t
*mddev
= NULL
;
5332 if (!capable(CAP_SYS_ADMIN
))
5336 * Commands dealing with the RAID driver but not any
5342 err
= get_version(argp
);
5345 case PRINT_RAID_DEBUG
:
5353 autostart_arrays(arg
);
5360 * Commands creating/starting a new array:
5363 mddev
= bdev
->bd_disk
->private_data
;
5370 err
= mddev_lock(mddev
);
5373 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5380 case SET_ARRAY_INFO
:
5382 mdu_array_info_t info
;
5384 memset(&info
, 0, sizeof(info
));
5385 else if (copy_from_user(&info
, argp
, sizeof(info
))) {
5390 err
= update_array_info(mddev
, &info
);
5392 printk(KERN_WARNING
"md: couldn't update"
5393 " array info. %d\n", err
);
5398 if (!list_empty(&mddev
->disks
)) {
5400 "md: array %s already has disks!\n",
5405 if (mddev
->raid_disks
) {
5407 "md: array %s already initialised!\n",
5412 err
= set_array_info(mddev
, &info
);
5414 printk(KERN_WARNING
"md: couldn't set"
5415 " array info. %d\n", err
);
5425 * Commands querying/configuring an existing array:
5427 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
5428 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
5429 if ((!mddev
->raid_disks
&& !mddev
->external
)
5430 && cmd
!= ADD_NEW_DISK
&& cmd
!= STOP_ARRAY
5431 && cmd
!= RUN_ARRAY
&& cmd
!= SET_BITMAP_FILE
5432 && cmd
!= GET_BITMAP_FILE
) {
5438 * Commands even a read-only array can execute:
5442 case GET_ARRAY_INFO
:
5443 err
= get_array_info(mddev
, argp
);
5446 case GET_BITMAP_FILE
:
5447 err
= get_bitmap_file(mddev
, argp
);
5451 err
= get_disk_info(mddev
, argp
);
5454 case RESTART_ARRAY_RW
:
5455 err
= restart_array(mddev
);
5459 err
= do_md_stop(mddev
, 0, 1);
5463 err
= do_md_stop(mddev
, 1, 1);
5469 * The remaining ioctls are changing the state of the
5470 * superblock, so we do not allow them on read-only arrays.
5471 * However non-MD ioctls (e.g. get-size) will still come through
5472 * here and hit the 'default' below, so only disallow
5473 * 'md' ioctls, and switch to rw mode if started auto-readonly.
5475 if (_IOC_TYPE(cmd
) == MD_MAJOR
&& mddev
->ro
&& mddev
->pers
) {
5476 if (mddev
->ro
== 2) {
5478 sysfs_notify_dirent(mddev
->sysfs_state
);
5479 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5480 md_wakeup_thread(mddev
->thread
);
5491 mdu_disk_info_t info
;
5492 if (copy_from_user(&info
, argp
, sizeof(info
)))
5495 err
= add_new_disk(mddev
, &info
);
5499 case HOT_REMOVE_DISK
:
5500 err
= hot_remove_disk(mddev
, new_decode_dev(arg
));
5504 err
= hot_add_disk(mddev
, new_decode_dev(arg
));
5507 case SET_DISK_FAULTY
:
5508 err
= set_disk_faulty(mddev
, new_decode_dev(arg
));
5512 err
= do_md_run(mddev
);
5515 case SET_BITMAP_FILE
:
5516 err
= set_bitmap_file(mddev
, (int)arg
);
5526 if (mddev
->hold_active
== UNTIL_IOCTL
&&
5528 mddev
->hold_active
= 0;
5529 mddev_unlock(mddev
);
5539 static int md_open(struct block_device
*bdev
, fmode_t mode
)
5542 * Succeed if we can lock the mddev, which confirms that
5543 * it isn't being stopped right now.
5545 mddev_t
*mddev
= mddev_find(bdev
->bd_dev
);
5548 if (mddev
->gendisk
!= bdev
->bd_disk
) {
5549 /* we are racing with mddev_put which is discarding this
5553 /* Wait until bdev->bd_disk is definitely gone */
5554 flush_scheduled_work();
5555 /* Then retry the open from the top */
5556 return -ERESTARTSYS
;
5558 BUG_ON(mddev
!= bdev
->bd_disk
->private_data
);
5560 if ((err
= mutex_lock_interruptible(&mddev
->open_mutex
)))
5564 atomic_inc(&mddev
->openers
);
5565 mutex_unlock(&mddev
->open_mutex
);
5567 check_disk_change(bdev
);
5572 static int md_release(struct gendisk
*disk
, fmode_t mode
)
5574 mddev_t
*mddev
= disk
->private_data
;
5577 atomic_dec(&mddev
->openers
);
5583 static int md_media_changed(struct gendisk
*disk
)
5585 mddev_t
*mddev
= disk
->private_data
;
5587 return mddev
->changed
;
5590 static int md_revalidate(struct gendisk
*disk
)
5592 mddev_t
*mddev
= disk
->private_data
;
5597 static const struct block_device_operations md_fops
=
5599 .owner
= THIS_MODULE
,
5601 .release
= md_release
,
5603 .getgeo
= md_getgeo
,
5604 .media_changed
= md_media_changed
,
5605 .revalidate_disk
= md_revalidate
,
5608 static int md_thread(void * arg
)
5610 mdk_thread_t
*thread
= arg
;
5613 * md_thread is a 'system-thread', it's priority should be very
5614 * high. We avoid resource deadlocks individually in each
5615 * raid personality. (RAID5 does preallocation) We also use RR and
5616 * the very same RT priority as kswapd, thus we will never get
5617 * into a priority inversion deadlock.
5619 * we definitely have to have equal or higher priority than
5620 * bdflush, otherwise bdflush will deadlock if there are too
5621 * many dirty RAID5 blocks.
5624 allow_signal(SIGKILL
);
5625 while (!kthread_should_stop()) {
5627 /* We need to wait INTERRUPTIBLE so that
5628 * we don't add to the load-average.
5629 * That means we need to be sure no signals are
5632 if (signal_pending(current
))
5633 flush_signals(current
);
5635 wait_event_interruptible_timeout
5637 test_bit(THREAD_WAKEUP
, &thread
->flags
)
5638 || kthread_should_stop(),
5641 clear_bit(THREAD_WAKEUP
, &thread
->flags
);
5643 thread
->run(thread
->mddev
);
5649 void md_wakeup_thread(mdk_thread_t
*thread
)
5652 dprintk("md: waking up MD thread %s.\n", thread
->tsk
->comm
);
5653 set_bit(THREAD_WAKEUP
, &thread
->flags
);
5654 wake_up(&thread
->wqueue
);
5658 mdk_thread_t
*md_register_thread(void (*run
) (mddev_t
*), mddev_t
*mddev
,
5661 mdk_thread_t
*thread
;
5663 thread
= kzalloc(sizeof(mdk_thread_t
), GFP_KERNEL
);
5667 init_waitqueue_head(&thread
->wqueue
);
5670 thread
->mddev
= mddev
;
5671 thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
5672 thread
->tsk
= kthread_run(md_thread
, thread
,
5674 mdname(thread
->mddev
),
5675 name
?: mddev
->pers
->name
);
5676 if (IS_ERR(thread
->tsk
)) {
5683 void md_unregister_thread(mdk_thread_t
*thread
)
5687 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread
->tsk
));
5689 kthread_stop(thread
->tsk
);
5693 void md_error(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
5700 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
5703 if (mddev
->external
)
5704 set_bit(Blocked
, &rdev
->flags
);
5706 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5708 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5709 __builtin_return_address(0),__builtin_return_address(1),
5710 __builtin_return_address(2),__builtin_return_address(3));
5714 if (!mddev
->pers
->error_handler
)
5716 mddev
->pers
->error_handler(mddev
,rdev
);
5717 if (mddev
->degraded
)
5718 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
5719 set_bit(StateChanged
, &rdev
->flags
);
5720 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5721 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5722 md_wakeup_thread(mddev
->thread
);
5723 md_new_event_inintr(mddev
);
5726 /* seq_file implementation /proc/mdstat */
5728 static void status_unused(struct seq_file
*seq
)
5733 seq_printf(seq
, "unused devices: ");
5735 list_for_each_entry(rdev
, &pending_raid_disks
, same_set
) {
5736 char b
[BDEVNAME_SIZE
];
5738 seq_printf(seq
, "%s ",
5739 bdevname(rdev
->bdev
,b
));
5742 seq_printf(seq
, "<none>");
5744 seq_printf(seq
, "\n");
5748 static void status_resync(struct seq_file
*seq
, mddev_t
* mddev
)
5750 sector_t max_sectors
, resync
, res
;
5751 unsigned long dt
, db
;
5754 unsigned int per_milli
;
5756 resync
= mddev
->curr_resync
- atomic_read(&mddev
->recovery_active
);
5758 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
5759 max_sectors
= mddev
->resync_max_sectors
;
5761 max_sectors
= mddev
->dev_sectors
;
5764 * Should not happen.
5770 /* Pick 'scale' such that (resync>>scale)*1000 will fit
5771 * in a sector_t, and (max_sectors>>scale) will fit in a
5772 * u32, as those are the requirements for sector_div.
5773 * Thus 'scale' must be at least 10
5776 if (sizeof(sector_t
) > sizeof(unsigned long)) {
5777 while ( max_sectors
/2 > (1ULL<<(scale
+32)))
5780 res
= (resync
>>scale
)*1000;
5781 sector_div(res
, (u32
)((max_sectors
>>scale
)+1));
5785 int i
, x
= per_milli
/50, y
= 20-x
;
5786 seq_printf(seq
, "[");
5787 for (i
= 0; i
< x
; i
++)
5788 seq_printf(seq
, "=");
5789 seq_printf(seq
, ">");
5790 for (i
= 0; i
< y
; i
++)
5791 seq_printf(seq
, ".");
5792 seq_printf(seq
, "] ");
5794 seq_printf(seq
, " %s =%3u.%u%% (%llu/%llu)",
5795 (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)?
5797 (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)?
5799 (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ?
5800 "resync" : "recovery"))),
5801 per_milli
/10, per_milli
% 10,
5802 (unsigned long long) resync
/2,
5803 (unsigned long long) max_sectors
/2);
5806 * dt: time from mark until now
5807 * db: blocks written from mark until now
5808 * rt: remaining time
5810 * rt is a sector_t, so could be 32bit or 64bit.
5811 * So we divide before multiply in case it is 32bit and close
5813 * We scale the divisor (db) by 32 to avoid loosing precision
5814 * near the end of resync when the number of remaining sectors
5816 * We then divide rt by 32 after multiplying by db to compensate.
5817 * The '+1' avoids division by zero if db is very small.
5819 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
5821 db
= (mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
))
5822 - mddev
->resync_mark_cnt
;
5824 rt
= max_sectors
- resync
; /* number of remaining sectors */
5825 sector_div(rt
, db
/32+1);
5829 seq_printf(seq
, " finish=%lu.%lumin", (unsigned long)rt
/ 60,
5830 ((unsigned long)rt
% 60)/6);
5832 seq_printf(seq
, " speed=%ldK/sec", db
/2/dt
);
5835 static void *md_seq_start(struct seq_file
*seq
, loff_t
*pos
)
5837 struct list_head
*tmp
;
5847 spin_lock(&all_mddevs_lock
);
5848 list_for_each(tmp
,&all_mddevs
)
5850 mddev
= list_entry(tmp
, mddev_t
, all_mddevs
);
5852 spin_unlock(&all_mddevs_lock
);
5855 spin_unlock(&all_mddevs_lock
);
5857 return (void*)2;/* tail */
5861 static void *md_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
5863 struct list_head
*tmp
;
5864 mddev_t
*next_mddev
, *mddev
= v
;
5870 spin_lock(&all_mddevs_lock
);
5872 tmp
= all_mddevs
.next
;
5874 tmp
= mddev
->all_mddevs
.next
;
5875 if (tmp
!= &all_mddevs
)
5876 next_mddev
= mddev_get(list_entry(tmp
,mddev_t
,all_mddevs
));
5878 next_mddev
= (void*)2;
5881 spin_unlock(&all_mddevs_lock
);
5889 static void md_seq_stop(struct seq_file
*seq
, void *v
)
5893 if (mddev
&& v
!= (void*)1 && v
!= (void*)2)
5897 struct mdstat_info
{
5901 static int md_seq_show(struct seq_file
*seq
, void *v
)
5906 struct mdstat_info
*mi
= seq
->private;
5907 struct bitmap
*bitmap
;
5909 if (v
== (void*)1) {
5910 struct mdk_personality
*pers
;
5911 seq_printf(seq
, "Personalities : ");
5912 spin_lock(&pers_lock
);
5913 list_for_each_entry(pers
, &pers_list
, list
)
5914 seq_printf(seq
, "[%s] ", pers
->name
);
5916 spin_unlock(&pers_lock
);
5917 seq_printf(seq
, "\n");
5918 mi
->event
= atomic_read(&md_event_count
);
5921 if (v
== (void*)2) {
5926 if (mddev_lock(mddev
) < 0)
5929 if (mddev
->pers
|| mddev
->raid_disks
|| !list_empty(&mddev
->disks
)) {
5930 seq_printf(seq
, "%s : %sactive", mdname(mddev
),
5931 mddev
->pers
? "" : "in");
5934 seq_printf(seq
, " (read-only)");
5936 seq_printf(seq
, " (auto-read-only)");
5937 seq_printf(seq
, " %s", mddev
->pers
->name
);
5941 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
5942 char b
[BDEVNAME_SIZE
];
5943 seq_printf(seq
, " %s[%d]",
5944 bdevname(rdev
->bdev
,b
), rdev
->desc_nr
);
5945 if (test_bit(WriteMostly
, &rdev
->flags
))
5946 seq_printf(seq
, "(W)");
5947 if (test_bit(Faulty
, &rdev
->flags
)) {
5948 seq_printf(seq
, "(F)");
5950 } else if (rdev
->raid_disk
< 0)
5951 seq_printf(seq
, "(S)"); /* spare */
5952 sectors
+= rdev
->sectors
;
5955 if (!list_empty(&mddev
->disks
)) {
5957 seq_printf(seq
, "\n %llu blocks",
5958 (unsigned long long)
5959 mddev
->array_sectors
/ 2);
5961 seq_printf(seq
, "\n %llu blocks",
5962 (unsigned long long)sectors
/ 2);
5964 if (mddev
->persistent
) {
5965 if (mddev
->major_version
!= 0 ||
5966 mddev
->minor_version
!= 90) {
5967 seq_printf(seq
," super %d.%d",
5968 mddev
->major_version
,
5969 mddev
->minor_version
);
5971 } else if (mddev
->external
)
5972 seq_printf(seq
, " super external:%s",
5973 mddev
->metadata_type
);
5975 seq_printf(seq
, " super non-persistent");
5978 mddev
->pers
->status(seq
, mddev
);
5979 seq_printf(seq
, "\n ");
5980 if (mddev
->pers
->sync_request
) {
5981 if (mddev
->curr_resync
> 2) {
5982 status_resync(seq
, mddev
);
5983 seq_printf(seq
, "\n ");
5984 } else if (mddev
->curr_resync
== 1 || mddev
->curr_resync
== 2)
5985 seq_printf(seq
, "\tresync=DELAYED\n ");
5986 else if (mddev
->recovery_cp
< MaxSector
)
5987 seq_printf(seq
, "\tresync=PENDING\n ");
5990 seq_printf(seq
, "\n ");
5992 if ((bitmap
= mddev
->bitmap
)) {
5993 unsigned long chunk_kb
;
5994 unsigned long flags
;
5995 spin_lock_irqsave(&bitmap
->lock
, flags
);
5996 chunk_kb
= bitmap
->chunksize
>> 10;
5997 seq_printf(seq
, "bitmap: %lu/%lu pages [%luKB], "
5999 bitmap
->pages
- bitmap
->missing_pages
,
6001 (bitmap
->pages
- bitmap
->missing_pages
)
6002 << (PAGE_SHIFT
- 10),
6003 chunk_kb
? chunk_kb
: bitmap
->chunksize
,
6004 chunk_kb
? "KB" : "B");
6006 seq_printf(seq
, ", file: ");
6007 seq_path(seq
, &bitmap
->file
->f_path
, " \t\n");
6010 seq_printf(seq
, "\n");
6011 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
6014 seq_printf(seq
, "\n");
6016 mddev_unlock(mddev
);
6021 static const struct seq_operations md_seq_ops
= {
6022 .start
= md_seq_start
,
6023 .next
= md_seq_next
,
6024 .stop
= md_seq_stop
,
6025 .show
= md_seq_show
,
6028 static int md_seq_open(struct inode
*inode
, struct file
*file
)
6031 struct mdstat_info
*mi
= kmalloc(sizeof(*mi
), GFP_KERNEL
);
6035 error
= seq_open(file
, &md_seq_ops
);
6039 struct seq_file
*p
= file
->private_data
;
6041 mi
->event
= atomic_read(&md_event_count
);
6046 static unsigned int mdstat_poll(struct file
*filp
, poll_table
*wait
)
6048 struct seq_file
*m
= filp
->private_data
;
6049 struct mdstat_info
*mi
= m
->private;
6052 poll_wait(filp
, &md_event_waiters
, wait
);
6054 /* always allow read */
6055 mask
= POLLIN
| POLLRDNORM
;
6057 if (mi
->event
!= atomic_read(&md_event_count
))
6058 mask
|= POLLERR
| POLLPRI
;
6062 static const struct file_operations md_seq_fops
= {
6063 .owner
= THIS_MODULE
,
6064 .open
= md_seq_open
,
6066 .llseek
= seq_lseek
,
6067 .release
= seq_release_private
,
6068 .poll
= mdstat_poll
,
6071 int register_md_personality(struct mdk_personality
*p
)
6073 spin_lock(&pers_lock
);
6074 list_add_tail(&p
->list
, &pers_list
);
6075 printk(KERN_INFO
"md: %s personality registered for level %d\n", p
->name
, p
->level
);
6076 spin_unlock(&pers_lock
);
6080 int unregister_md_personality(struct mdk_personality
*p
)
6082 printk(KERN_INFO
"md: %s personality unregistered\n", p
->name
);
6083 spin_lock(&pers_lock
);
6084 list_del_init(&p
->list
);
6085 spin_unlock(&pers_lock
);
6089 static int is_mddev_idle(mddev_t
*mddev
, int init
)
6097 rdev_for_each_rcu(rdev
, mddev
) {
6098 struct gendisk
*disk
= rdev
->bdev
->bd_contains
->bd_disk
;
6099 curr_events
= (int)part_stat_read(&disk
->part0
, sectors
[0]) +
6100 (int)part_stat_read(&disk
->part0
, sectors
[1]) -
6101 atomic_read(&disk
->sync_io
);
6102 /* sync IO will cause sync_io to increase before the disk_stats
6103 * as sync_io is counted when a request starts, and
6104 * disk_stats is counted when it completes.
6105 * So resync activity will cause curr_events to be smaller than
6106 * when there was no such activity.
6107 * non-sync IO will cause disk_stat to increase without
6108 * increasing sync_io so curr_events will (eventually)
6109 * be larger than it was before. Once it becomes
6110 * substantially larger, the test below will cause
6111 * the array to appear non-idle, and resync will slow
6113 * If there is a lot of outstanding resync activity when
6114 * we set last_event to curr_events, then all that activity
6115 * completing might cause the array to appear non-idle
6116 * and resync will be slowed down even though there might
6117 * not have been non-resync activity. This will only
6118 * happen once though. 'last_events' will soon reflect
6119 * the state where there is little or no outstanding
6120 * resync requests, and further resync activity will
6121 * always make curr_events less than last_events.
6124 if (init
|| curr_events
- rdev
->last_events
> 64) {
6125 rdev
->last_events
= curr_events
;
6133 void md_done_sync(mddev_t
*mddev
, int blocks
, int ok
)
6135 /* another "blocks" (512byte) blocks have been synced */
6136 atomic_sub(blocks
, &mddev
->recovery_active
);
6137 wake_up(&mddev
->recovery_wait
);
6139 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6140 md_wakeup_thread(mddev
->thread
);
6141 // stop recovery, signal do_sync ....
6146 /* md_write_start(mddev, bi)
6147 * If we need to update some array metadata (e.g. 'active' flag
6148 * in superblock) before writing, schedule a superblock update
6149 * and wait for it to complete.
6151 void md_write_start(mddev_t
*mddev
, struct bio
*bi
)
6154 if (bio_data_dir(bi
) != WRITE
)
6157 BUG_ON(mddev
->ro
== 1);
6158 if (mddev
->ro
== 2) {
6159 /* need to switch to read/write */
6161 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6162 md_wakeup_thread(mddev
->thread
);
6163 md_wakeup_thread(mddev
->sync_thread
);
6166 atomic_inc(&mddev
->writes_pending
);
6167 if (mddev
->safemode
== 1)
6168 mddev
->safemode
= 0;
6169 if (mddev
->in_sync
) {
6170 spin_lock_irq(&mddev
->write_lock
);
6171 if (mddev
->in_sync
) {
6173 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6174 md_wakeup_thread(mddev
->thread
);
6177 spin_unlock_irq(&mddev
->write_lock
);
6180 sysfs_notify_dirent(mddev
->sysfs_state
);
6181 wait_event(mddev
->sb_wait
,
6182 !test_bit(MD_CHANGE_CLEAN
, &mddev
->flags
) &&
6183 !test_bit(MD_CHANGE_PENDING
, &mddev
->flags
));
6186 void md_write_end(mddev_t
*mddev
)
6188 if (atomic_dec_and_test(&mddev
->writes_pending
)) {
6189 if (mddev
->safemode
== 2)
6190 md_wakeup_thread(mddev
->thread
);
6191 else if (mddev
->safemode_delay
)
6192 mod_timer(&mddev
->safemode_timer
, jiffies
+ mddev
->safemode_delay
);
6196 /* md_allow_write(mddev)
6197 * Calling this ensures that the array is marked 'active' so that writes
6198 * may proceed without blocking. It is important to call this before
6199 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6200 * Must be called with mddev_lock held.
6202 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6203 * is dropped, so return -EAGAIN after notifying userspace.
6205 int md_allow_write(mddev_t
*mddev
)
6211 if (!mddev
->pers
->sync_request
)
6214 spin_lock_irq(&mddev
->write_lock
);
6215 if (mddev
->in_sync
) {
6217 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6218 if (mddev
->safemode_delay
&&
6219 mddev
->safemode
== 0)
6220 mddev
->safemode
= 1;
6221 spin_unlock_irq(&mddev
->write_lock
);
6222 md_update_sb(mddev
, 0);
6223 sysfs_notify_dirent(mddev
->sysfs_state
);
6225 spin_unlock_irq(&mddev
->write_lock
);
6227 if (test_bit(MD_CHANGE_CLEAN
, &mddev
->flags
))
6232 EXPORT_SYMBOL_GPL(md_allow_write
);
6234 #define SYNC_MARKS 10
6235 #define SYNC_MARK_STEP (3*HZ)
6236 void md_do_sync(mddev_t
*mddev
)
6239 unsigned int currspeed
= 0,
6241 sector_t max_sectors
,j
, io_sectors
;
6242 unsigned long mark
[SYNC_MARKS
];
6243 sector_t mark_cnt
[SYNC_MARKS
];
6245 struct list_head
*tmp
;
6246 sector_t last_check
;
6251 /* just incase thread restarts... */
6252 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
6254 if (mddev
->ro
) /* never try to sync a read-only array */
6257 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
6258 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
6259 desc
= "data-check";
6260 else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
6261 desc
= "requested-resync";
6264 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
6269 /* we overload curr_resync somewhat here.
6270 * 0 == not engaged in resync at all
6271 * 2 == checking that there is no conflict with another sync
6272 * 1 == like 2, but have yielded to allow conflicting resync to
6274 * other == active in resync - this many blocks
6276 * Before starting a resync we must have set curr_resync to
6277 * 2, and then checked that every "conflicting" array has curr_resync
6278 * less than ours. When we find one that is the same or higher
6279 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6280 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6281 * This will mean we have to start checking from the beginning again.
6286 mddev
->curr_resync
= 2;
6289 if (kthread_should_stop()) {
6290 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6293 for_each_mddev(mddev2
, tmp
) {
6294 if (mddev2
== mddev
)
6296 if (!mddev
->parallel_resync
6297 && mddev2
->curr_resync
6298 && match_mddev_units(mddev
, mddev2
)) {
6300 if (mddev
< mddev2
&& mddev
->curr_resync
== 2) {
6301 /* arbitrarily yield */
6302 mddev
->curr_resync
= 1;
6303 wake_up(&resync_wait
);
6305 if (mddev
> mddev2
&& mddev
->curr_resync
== 1)
6306 /* no need to wait here, we can wait the next
6307 * time 'round when curr_resync == 2
6310 /* We need to wait 'interruptible' so as not to
6311 * contribute to the load average, and not to
6312 * be caught by 'softlockup'
6314 prepare_to_wait(&resync_wait
, &wq
, TASK_INTERRUPTIBLE
);
6315 if (!kthread_should_stop() &&
6316 mddev2
->curr_resync
>= mddev
->curr_resync
) {
6317 printk(KERN_INFO
"md: delaying %s of %s"
6318 " until %s has finished (they"
6319 " share one or more physical units)\n",
6320 desc
, mdname(mddev
), mdname(mddev2
));
6322 if (signal_pending(current
))
6323 flush_signals(current
);
6325 finish_wait(&resync_wait
, &wq
);
6328 finish_wait(&resync_wait
, &wq
);
6331 } while (mddev
->curr_resync
< 2);
6334 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
6335 /* resync follows the size requested by the personality,
6336 * which defaults to physical size, but can be virtual size
6338 max_sectors
= mddev
->resync_max_sectors
;
6339 mddev
->resync_mismatches
= 0;
6340 /* we don't use the checkpoint if there's a bitmap */
6341 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
6342 j
= mddev
->resync_min
;
6343 else if (!mddev
->bitmap
)
6344 j
= mddev
->recovery_cp
;
6346 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
6347 max_sectors
= mddev
->dev_sectors
;
6349 /* recovery follows the physical size of devices */
6350 max_sectors
= mddev
->dev_sectors
;
6352 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
6353 if (rdev
->raid_disk
>= 0 &&
6354 !test_bit(Faulty
, &rdev
->flags
) &&
6355 !test_bit(In_sync
, &rdev
->flags
) &&
6356 rdev
->recovery_offset
< j
)
6357 j
= rdev
->recovery_offset
;
6360 printk(KERN_INFO
"md: %s of RAID array %s\n", desc
, mdname(mddev
));
6361 printk(KERN_INFO
"md: minimum _guaranteed_ speed:"
6362 " %d KB/sec/disk.\n", speed_min(mddev
));
6363 printk(KERN_INFO
"md: using maximum available idle IO bandwidth "
6364 "(but not more than %d KB/sec) for %s.\n",
6365 speed_max(mddev
), desc
);
6367 is_mddev_idle(mddev
, 1); /* this initializes IO event counters */
6370 for (m
= 0; m
< SYNC_MARKS
; m
++) {
6372 mark_cnt
[m
] = io_sectors
;
6375 mddev
->resync_mark
= mark
[last_mark
];
6376 mddev
->resync_mark_cnt
= mark_cnt
[last_mark
];
6379 * Tune reconstruction:
6381 window
= 32*(PAGE_SIZE
/512);
6382 printk(KERN_INFO
"md: using %dk window, over a total of %llu blocks.\n",
6383 window
/2,(unsigned long long) max_sectors
/2);
6385 atomic_set(&mddev
->recovery_active
, 0);
6390 "md: resuming %s of %s from checkpoint.\n",
6391 desc
, mdname(mddev
));
6392 mddev
->curr_resync
= j
;
6395 while (j
< max_sectors
) {
6400 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
6401 ((mddev
->curr_resync
> mddev
->curr_resync_completed
&&
6402 (mddev
->curr_resync
- mddev
->curr_resync_completed
)
6403 > (max_sectors
>> 4)) ||
6404 (j
- mddev
->curr_resync_completed
)*2
6405 >= mddev
->resync_max
- mddev
->curr_resync_completed
6407 /* time to update curr_resync_completed */
6408 blk_unplug(mddev
->queue
);
6409 wait_event(mddev
->recovery_wait
,
6410 atomic_read(&mddev
->recovery_active
) == 0);
6411 mddev
->curr_resync_completed
=
6413 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6414 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
6417 while (j
>= mddev
->resync_max
&& !kthread_should_stop()) {
6418 /* As this condition is controlled by user-space,
6419 * we can block indefinitely, so use '_interruptible'
6420 * to avoid triggering warnings.
6422 flush_signals(current
); /* just in case */
6423 wait_event_interruptible(mddev
->recovery_wait
,
6424 mddev
->resync_max
> j
6425 || kthread_should_stop());
6428 if (kthread_should_stop())
6431 sectors
= mddev
->pers
->sync_request(mddev
, j
, &skipped
,
6432 currspeed
< speed_min(mddev
));
6434 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6438 if (!skipped
) { /* actual IO requested */
6439 io_sectors
+= sectors
;
6440 atomic_add(sectors
, &mddev
->recovery_active
);
6444 if (j
>1) mddev
->curr_resync
= j
;
6445 mddev
->curr_mark_cnt
= io_sectors
;
6446 if (last_check
== 0)
6447 /* this is the earliers that rebuilt will be
6448 * visible in /proc/mdstat
6450 md_new_event(mddev
);
6452 if (last_check
+ window
> io_sectors
|| j
== max_sectors
)
6455 last_check
= io_sectors
;
6457 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
6461 if (time_after_eq(jiffies
, mark
[last_mark
] + SYNC_MARK_STEP
)) {
6463 int next
= (last_mark
+1) % SYNC_MARKS
;
6465 mddev
->resync_mark
= mark
[next
];
6466 mddev
->resync_mark_cnt
= mark_cnt
[next
];
6467 mark
[next
] = jiffies
;
6468 mark_cnt
[next
] = io_sectors
- atomic_read(&mddev
->recovery_active
);
6473 if (kthread_should_stop())
6478 * this loop exits only if either when we are slower than
6479 * the 'hard' speed limit, or the system was IO-idle for
6481 * the system might be non-idle CPU-wise, but we only care
6482 * about not overloading the IO subsystem. (things like an
6483 * e2fsck being done on the RAID array should execute fast)
6485 blk_unplug(mddev
->queue
);
6488 currspeed
= ((unsigned long)(io_sectors
-mddev
->resync_mark_cnt
))/2
6489 /((jiffies
-mddev
->resync_mark
)/HZ
+1) +1;
6491 if (currspeed
> speed_min(mddev
)) {
6492 if ((currspeed
> speed_max(mddev
)) ||
6493 !is_mddev_idle(mddev
, 0)) {
6499 printk(KERN_INFO
"md: %s: %s done.\n",mdname(mddev
), desc
);
6501 * this also signals 'finished resyncing' to md_stop
6504 blk_unplug(mddev
->queue
);
6506 wait_event(mddev
->recovery_wait
, !atomic_read(&mddev
->recovery_active
));
6508 /* tell personality that we are finished */
6509 mddev
->pers
->sync_request(mddev
, max_sectors
, &skipped
, 1);
6511 if (!test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
) &&
6512 mddev
->curr_resync
> 2) {
6513 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
6514 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
6515 if (mddev
->curr_resync
>= mddev
->recovery_cp
) {
6517 "md: checkpointing %s of %s.\n",
6518 desc
, mdname(mddev
));
6519 mddev
->recovery_cp
= mddev
->curr_resync
;
6522 mddev
->recovery_cp
= MaxSector
;
6524 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
6525 mddev
->curr_resync
= MaxSector
;
6526 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
6527 if (rdev
->raid_disk
>= 0 &&
6528 !test_bit(Faulty
, &rdev
->flags
) &&
6529 !test_bit(In_sync
, &rdev
->flags
) &&
6530 rdev
->recovery_offset
< mddev
->curr_resync
)
6531 rdev
->recovery_offset
= mddev
->curr_resync
;
6534 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
6537 mddev
->curr_resync
= 0;
6538 mddev
->curr_resync_completed
= 0;
6539 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
6540 /* We completed so max setting can be forgotten. */
6541 mddev
->resync_max
= MaxSector
;
6542 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
6543 wake_up(&resync_wait
);
6544 set_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
6545 md_wakeup_thread(mddev
->thread
);
6550 * got a signal, exit.
6553 "md: md_do_sync() got signal ... exiting\n");
6554 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6558 EXPORT_SYMBOL_GPL(md_do_sync
);
6561 static int remove_and_add_spares(mddev_t
*mddev
)
6566 mddev
->curr_resync_completed
= 0;
6568 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
6569 if (rdev
->raid_disk
>= 0 &&
6570 !test_bit(Blocked
, &rdev
->flags
) &&
6571 (test_bit(Faulty
, &rdev
->flags
) ||
6572 ! test_bit(In_sync
, &rdev
->flags
)) &&
6573 atomic_read(&rdev
->nr_pending
)==0) {
6574 if (mddev
->pers
->hot_remove_disk(
6575 mddev
, rdev
->raid_disk
)==0) {
6577 sprintf(nm
,"rd%d", rdev
->raid_disk
);
6578 sysfs_remove_link(&mddev
->kobj
, nm
);
6579 rdev
->raid_disk
= -1;
6583 if (mddev
->degraded
&& ! mddev
->ro
&& !mddev
->recovery_disabled
) {
6584 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
6585 if (rdev
->raid_disk
>= 0 &&
6586 !test_bit(In_sync
, &rdev
->flags
) &&
6587 !test_bit(Blocked
, &rdev
->flags
))
6589 if (rdev
->raid_disk
< 0
6590 && !test_bit(Faulty
, &rdev
->flags
)) {
6591 rdev
->recovery_offset
= 0;
6593 hot_add_disk(mddev
, rdev
) == 0) {
6595 sprintf(nm
, "rd%d", rdev
->raid_disk
);
6596 if (sysfs_create_link(&mddev
->kobj
,
6599 "md: cannot register "
6603 md_new_event(mddev
);
6612 * This routine is regularly called by all per-raid-array threads to
6613 * deal with generic issues like resync and super-block update.
6614 * Raid personalities that don't have a thread (linear/raid0) do not
6615 * need this as they never do any recovery or update the superblock.
6617 * It does not do any resync itself, but rather "forks" off other threads
6618 * to do that as needed.
6619 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6620 * "->recovery" and create a thread at ->sync_thread.
6621 * When the thread finishes it sets MD_RECOVERY_DONE
6622 * and wakeups up this thread which will reap the thread and finish up.
6623 * This thread also removes any faulty devices (with nr_pending == 0).
6625 * The overall approach is:
6626 * 1/ if the superblock needs updating, update it.
6627 * 2/ If a recovery thread is running, don't do anything else.
6628 * 3/ If recovery has finished, clean up, possibly marking spares active.
6629 * 4/ If there are any faulty devices, remove them.
6630 * 5/ If array is degraded, try to add spares devices
6631 * 6/ If array has spares or is not in-sync, start a resync thread.
6633 void md_check_recovery(mddev_t
*mddev
)
6639 bitmap_daemon_work(mddev
);
6644 if (signal_pending(current
)) {
6645 if (mddev
->pers
->sync_request
&& !mddev
->external
) {
6646 printk(KERN_INFO
"md: %s in immediate safe mode\n",
6648 mddev
->safemode
= 2;
6650 flush_signals(current
);
6653 if (mddev
->ro
&& !test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
6656 (mddev
->flags
&& !mddev
->external
) ||
6657 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
6658 test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
6659 (mddev
->external
== 0 && mddev
->safemode
== 1) ||
6660 (mddev
->safemode
== 2 && ! atomic_read(&mddev
->writes_pending
)
6661 && !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
6665 if (mddev_trylock(mddev
)) {
6669 /* Only thing we do on a ro array is remove
6672 remove_and_add_spares(mddev
);
6673 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6677 if (!mddev
->external
) {
6679 spin_lock_irq(&mddev
->write_lock
);
6680 if (mddev
->safemode
&&
6681 !atomic_read(&mddev
->writes_pending
) &&
6683 mddev
->recovery_cp
== MaxSector
) {
6686 if (mddev
->persistent
)
6687 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6689 if (mddev
->safemode
== 1)
6690 mddev
->safemode
= 0;
6691 spin_unlock_irq(&mddev
->write_lock
);
6693 sysfs_notify_dirent(mddev
->sysfs_state
);
6697 md_update_sb(mddev
, 0);
6699 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
6700 if (test_and_clear_bit(StateChanged
, &rdev
->flags
))
6701 sysfs_notify_dirent(rdev
->sysfs_state
);
6704 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
6705 !test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
)) {
6706 /* resync/recovery still happening */
6707 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6710 if (mddev
->sync_thread
) {
6711 /* resync has finished, collect result */
6712 md_unregister_thread(mddev
->sync_thread
);
6713 mddev
->sync_thread
= NULL
;
6714 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
6715 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
6717 /* activate any spares */
6718 if (mddev
->pers
->spare_active(mddev
))
6719 sysfs_notify(&mddev
->kobj
, NULL
,
6722 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
6723 mddev
->pers
->finish_reshape
)
6724 mddev
->pers
->finish_reshape(mddev
);
6725 md_update_sb(mddev
, 1);
6727 /* if array is no-longer degraded, then any saved_raid_disk
6728 * information must be scrapped
6730 if (!mddev
->degraded
)
6731 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
6732 rdev
->saved_raid_disk
= -1;
6734 mddev
->recovery
= 0;
6735 /* flag recovery needed just to double check */
6736 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6737 sysfs_notify_dirent(mddev
->sysfs_action
);
6738 md_new_event(mddev
);
6741 /* Set RUNNING before clearing NEEDED to avoid
6742 * any transients in the value of "sync_action".
6744 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
6745 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6746 /* Clear some bits that don't mean anything, but
6749 clear_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6750 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
6752 if (test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
6754 /* no recovery is running.
6755 * remove any failed drives, then
6756 * add spares if possible.
6757 * Spare are also removed and re-added, to allow
6758 * the personality to fail the re-add.
6761 if (mddev
->reshape_position
!= MaxSector
) {
6762 if (mddev
->pers
->check_reshape
== NULL
||
6763 mddev
->pers
->check_reshape(mddev
) != 0)
6764 /* Cannot proceed */
6766 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
6767 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
6768 } else if ((spares
= remove_and_add_spares(mddev
))) {
6769 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
6770 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
6771 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
6772 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
6773 } else if (mddev
->recovery_cp
< MaxSector
) {
6774 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
6775 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
6776 } else if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
6777 /* nothing to be done ... */
6780 if (mddev
->pers
->sync_request
) {
6781 if (spares
&& mddev
->bitmap
&& ! mddev
->bitmap
->file
) {
6782 /* We are adding a device or devices to an array
6783 * which has the bitmap stored on all devices.
6784 * So make sure all bitmap pages get written
6786 bitmap_write_all(mddev
->bitmap
);
6788 mddev
->sync_thread
= md_register_thread(md_do_sync
,
6791 if (!mddev
->sync_thread
) {
6792 printk(KERN_ERR
"%s: could not start resync"
6795 /* leave the spares where they are, it shouldn't hurt */
6796 mddev
->recovery
= 0;
6798 md_wakeup_thread(mddev
->sync_thread
);
6799 sysfs_notify_dirent(mddev
->sysfs_action
);
6800 md_new_event(mddev
);
6803 if (!mddev
->sync_thread
) {
6804 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
6805 if (test_and_clear_bit(MD_RECOVERY_RECOVER
,
6807 if (mddev
->sysfs_action
)
6808 sysfs_notify_dirent(mddev
->sysfs_action
);
6810 mddev_unlock(mddev
);
6814 void md_wait_for_blocked_rdev(mdk_rdev_t
*rdev
, mddev_t
*mddev
)
6816 sysfs_notify_dirent(rdev
->sysfs_state
);
6817 wait_event_timeout(rdev
->blocked_wait
,
6818 !test_bit(Blocked
, &rdev
->flags
),
6819 msecs_to_jiffies(5000));
6820 rdev_dec_pending(rdev
, mddev
);
6822 EXPORT_SYMBOL(md_wait_for_blocked_rdev
);
6824 static int md_notify_reboot(struct notifier_block
*this,
6825 unsigned long code
, void *x
)
6827 struct list_head
*tmp
;
6830 if ((code
== SYS_DOWN
) || (code
== SYS_HALT
) || (code
== SYS_POWER_OFF
)) {
6832 printk(KERN_INFO
"md: stopping all md devices.\n");
6834 for_each_mddev(mddev
, tmp
)
6835 if (mddev_trylock(mddev
)) {
6836 /* Force a switch to readonly even array
6837 * appears to still be in use. Hence
6840 do_md_stop(mddev
, 1, 100);
6841 mddev_unlock(mddev
);
6844 * certain more exotic SCSI devices are known to be
6845 * volatile wrt too early system reboots. While the
6846 * right place to handle this issue is the given
6847 * driver, we do want to have a safe RAID driver ...
6854 static struct notifier_block md_notifier
= {
6855 .notifier_call
= md_notify_reboot
,
6857 .priority
= INT_MAX
, /* before any real devices */
6860 static void md_geninit(void)
6862 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t
));
6864 proc_create("mdstat", S_IRUGO
, NULL
, &md_seq_fops
);
6867 static int __init
md_init(void)
6869 if (register_blkdev(MD_MAJOR
, "md"))
6871 if ((mdp_major
=register_blkdev(0, "mdp"))<=0) {
6872 unregister_blkdev(MD_MAJOR
, "md");
6875 blk_register_region(MKDEV(MD_MAJOR
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
6876 md_probe
, NULL
, NULL
);
6877 blk_register_region(MKDEV(mdp_major
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
6878 md_probe
, NULL
, NULL
);
6880 register_reboot_notifier(&md_notifier
);
6881 raid_table_header
= register_sysctl_table(raid_root_table
);
6891 * Searches all registered partitions for autorun RAID arrays
6895 static LIST_HEAD(all_detected_devices
);
6896 struct detected_devices_node
{
6897 struct list_head list
;
6901 void md_autodetect_dev(dev_t dev
)
6903 struct detected_devices_node
*node_detected_dev
;
6905 node_detected_dev
= kzalloc(sizeof(*node_detected_dev
), GFP_KERNEL
);
6906 if (node_detected_dev
) {
6907 node_detected_dev
->dev
= dev
;
6908 list_add_tail(&node_detected_dev
->list
, &all_detected_devices
);
6910 printk(KERN_CRIT
"md: md_autodetect_dev: kzalloc failed"
6911 ", skipping dev(%d,%d)\n", MAJOR(dev
), MINOR(dev
));
6916 static void autostart_arrays(int part
)
6919 struct detected_devices_node
*node_detected_dev
;
6921 int i_scanned
, i_passed
;
6926 printk(KERN_INFO
"md: Autodetecting RAID arrays.\n");
6928 while (!list_empty(&all_detected_devices
) && i_scanned
< INT_MAX
) {
6930 node_detected_dev
= list_entry(all_detected_devices
.next
,
6931 struct detected_devices_node
, list
);
6932 list_del(&node_detected_dev
->list
);
6933 dev
= node_detected_dev
->dev
;
6934 kfree(node_detected_dev
);
6935 rdev
= md_import_device(dev
,0, 90);
6939 if (test_bit(Faulty
, &rdev
->flags
)) {
6943 set_bit(AutoDetected
, &rdev
->flags
);
6944 list_add(&rdev
->same_set
, &pending_raid_disks
);
6948 printk(KERN_INFO
"md: Scanned %d and added %d devices.\n",
6949 i_scanned
, i_passed
);
6951 autorun_devices(part
);
6954 #endif /* !MODULE */
6956 static __exit
void md_exit(void)
6959 struct list_head
*tmp
;
6961 blk_unregister_region(MKDEV(MD_MAJOR
,0), 1U << MINORBITS
);
6962 blk_unregister_region(MKDEV(mdp_major
,0), 1U << MINORBITS
);
6964 unregister_blkdev(MD_MAJOR
,"md");
6965 unregister_blkdev(mdp_major
, "mdp");
6966 unregister_reboot_notifier(&md_notifier
);
6967 unregister_sysctl_table(raid_table_header
);
6968 remove_proc_entry("mdstat", NULL
);
6969 for_each_mddev(mddev
, tmp
) {
6970 export_array(mddev
);
6971 mddev
->hold_active
= 0;
6975 subsys_initcall(md_init
);
6976 module_exit(md_exit
)
6978 static int get_ro(char *buffer
, struct kernel_param
*kp
)
6980 return sprintf(buffer
, "%d", start_readonly
);
6982 static int set_ro(const char *val
, struct kernel_param
*kp
)
6985 int num
= simple_strtoul(val
, &e
, 10);
6986 if (*val
&& (*e
== '\0' || *e
== '\n')) {
6987 start_readonly
= num
;
6993 module_param_call(start_ro
, set_ro
, get_ro
, NULL
, S_IRUSR
|S_IWUSR
);
6994 module_param(start_dirty_degraded
, int, S_IRUGO
|S_IWUSR
);
6996 module_param_call(new_array
, add_named_array
, NULL
, NULL
, S_IWUSR
);
6998 EXPORT_SYMBOL(register_md_personality
);
6999 EXPORT_SYMBOL(unregister_md_personality
);
7000 EXPORT_SYMBOL(md_error
);
7001 EXPORT_SYMBOL(md_done_sync
);
7002 EXPORT_SYMBOL(md_write_start
);
7003 EXPORT_SYMBOL(md_write_end
);
7004 EXPORT_SYMBOL(md_register_thread
);
7005 EXPORT_SYMBOL(md_unregister_thread
);
7006 EXPORT_SYMBOL(md_wakeup_thread
);
7007 EXPORT_SYMBOL(md_check_recovery
);
7008 MODULE_LICENSE("GPL");
7010 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR
);