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 .procname
= "speed_limit_min",
102 .data
= &sysctl_speed_limit_min
,
103 .maxlen
= sizeof(int),
104 .mode
= S_IRUGO
|S_IWUSR
,
105 .proc_handler
= proc_dointvec
,
108 .procname
= "speed_limit_max",
109 .data
= &sysctl_speed_limit_max
,
110 .maxlen
= sizeof(int),
111 .mode
= S_IRUGO
|S_IWUSR
,
112 .proc_handler
= proc_dointvec
,
117 static ctl_table raid_dir_table
[] = {
121 .mode
= S_IRUGO
|S_IXUGO
,
127 static ctl_table raid_root_table
[] = {
132 .child
= raid_dir_table
,
137 static const struct block_device_operations md_fops
;
139 static int start_readonly
;
142 * We have a system wide 'event count' that is incremented
143 * on any 'interesting' event, and readers of /proc/mdstat
144 * can use 'poll' or 'select' to find out when the event
148 * start array, stop array, error, add device, remove device,
149 * start build, activate spare
151 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters
);
152 static atomic_t md_event_count
;
153 void md_new_event(mddev_t
*mddev
)
155 atomic_inc(&md_event_count
);
156 wake_up(&md_event_waiters
);
158 EXPORT_SYMBOL_GPL(md_new_event
);
160 /* Alternate version that can be called from interrupts
161 * when calling sysfs_notify isn't needed.
163 static void md_new_event_inintr(mddev_t
*mddev
)
165 atomic_inc(&md_event_count
);
166 wake_up(&md_event_waiters
);
170 * Enables to iterate over all existing md arrays
171 * all_mddevs_lock protects this list.
173 static LIST_HEAD(all_mddevs
);
174 static DEFINE_SPINLOCK(all_mddevs_lock
);
178 * iterates through all used mddevs in the system.
179 * We take care to grab the all_mddevs_lock whenever navigating
180 * the list, and to always hold a refcount when unlocked.
181 * Any code which breaks out of this loop while own
182 * a reference to the current mddev and must mddev_put it.
184 #define for_each_mddev(mddev,tmp) \
186 for (({ spin_lock(&all_mddevs_lock); \
187 tmp = all_mddevs.next; \
189 ({ if (tmp != &all_mddevs) \
190 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
191 spin_unlock(&all_mddevs_lock); \
192 if (mddev) mddev_put(mddev); \
193 mddev = list_entry(tmp, mddev_t, all_mddevs); \
194 tmp != &all_mddevs;}); \
195 ({ spin_lock(&all_mddevs_lock); \
200 /* Rather than calling directly into the personality make_request function,
201 * IO requests come here first so that we can check if the device is
202 * being suspended pending a reconfiguration.
203 * We hold a refcount over the call to ->make_request. By the time that
204 * call has finished, the bio has been linked into some internal structure
205 * and so is visible to ->quiesce(), so we don't need the refcount any more.
207 static int md_make_request(struct request_queue
*q
, struct bio
*bio
)
209 mddev_t
*mddev
= q
->queuedata
;
211 if (mddev
== NULL
|| mddev
->pers
== NULL
) {
216 if (mddev
->suspended
) {
219 prepare_to_wait(&mddev
->sb_wait
, &__wait
,
220 TASK_UNINTERRUPTIBLE
);
221 if (!mddev
->suspended
)
227 finish_wait(&mddev
->sb_wait
, &__wait
);
229 atomic_inc(&mddev
->active_io
);
231 rv
= mddev
->pers
->make_request(q
, bio
);
232 if (atomic_dec_and_test(&mddev
->active_io
) && mddev
->suspended
)
233 wake_up(&mddev
->sb_wait
);
238 static void mddev_suspend(mddev_t
*mddev
)
240 BUG_ON(mddev
->suspended
);
241 mddev
->suspended
= 1;
243 wait_event(mddev
->sb_wait
, atomic_read(&mddev
->active_io
) == 0);
244 mddev
->pers
->quiesce(mddev
, 1);
245 md_unregister_thread(mddev
->thread
);
246 mddev
->thread
= NULL
;
247 /* we now know that no code is executing in the personality module,
248 * except possibly the tail end of a ->bi_end_io function, but that
249 * is certain to complete before the module has a chance to get
254 static void mddev_resume(mddev_t
*mddev
)
256 mddev
->suspended
= 0;
257 wake_up(&mddev
->sb_wait
);
258 mddev
->pers
->quiesce(mddev
, 0);
261 int mddev_congested(mddev_t
*mddev
, int bits
)
263 return mddev
->suspended
;
265 EXPORT_SYMBOL(mddev_congested
);
268 static inline mddev_t
*mddev_get(mddev_t
*mddev
)
270 atomic_inc(&mddev
->active
);
274 static void mddev_delayed_delete(struct work_struct
*ws
);
276 static void mddev_put(mddev_t
*mddev
)
278 if (!atomic_dec_and_lock(&mddev
->active
, &all_mddevs_lock
))
280 if (!mddev
->raid_disks
&& list_empty(&mddev
->disks
) &&
281 !mddev
->hold_active
) {
282 list_del(&mddev
->all_mddevs
);
283 if (mddev
->gendisk
) {
284 /* we did a probe so need to clean up.
285 * Call schedule_work inside the spinlock
286 * so that flush_scheduled_work() after
287 * mddev_find will succeed in waiting for the
290 INIT_WORK(&mddev
->del_work
, mddev_delayed_delete
);
291 schedule_work(&mddev
->del_work
);
295 spin_unlock(&all_mddevs_lock
);
298 static mddev_t
* mddev_find(dev_t unit
)
300 mddev_t
*mddev
, *new = NULL
;
303 spin_lock(&all_mddevs_lock
);
306 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
307 if (mddev
->unit
== unit
) {
309 spin_unlock(&all_mddevs_lock
);
315 list_add(&new->all_mddevs
, &all_mddevs
);
316 spin_unlock(&all_mddevs_lock
);
317 new->hold_active
= UNTIL_IOCTL
;
321 /* find an unused unit number */
322 static int next_minor
= 512;
323 int start
= next_minor
;
327 dev
= MKDEV(MD_MAJOR
, next_minor
);
329 if (next_minor
> MINORMASK
)
331 if (next_minor
== start
) {
332 /* Oh dear, all in use. */
333 spin_unlock(&all_mddevs_lock
);
339 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
340 if (mddev
->unit
== dev
) {
346 new->md_minor
= MINOR(dev
);
347 new->hold_active
= UNTIL_STOP
;
348 list_add(&new->all_mddevs
, &all_mddevs
);
349 spin_unlock(&all_mddevs_lock
);
352 spin_unlock(&all_mddevs_lock
);
354 new = kzalloc(sizeof(*new), GFP_KERNEL
);
359 if (MAJOR(unit
) == MD_MAJOR
)
360 new->md_minor
= MINOR(unit
);
362 new->md_minor
= MINOR(unit
) >> MdpMinorShift
;
364 mutex_init(&new->open_mutex
);
365 mutex_init(&new->reconfig_mutex
);
366 INIT_LIST_HEAD(&new->disks
);
367 INIT_LIST_HEAD(&new->all_mddevs
);
368 init_timer(&new->safemode_timer
);
369 atomic_set(&new->active
, 1);
370 atomic_set(&new->openers
, 0);
371 atomic_set(&new->active_io
, 0);
372 spin_lock_init(&new->write_lock
);
373 init_waitqueue_head(&new->sb_wait
);
374 init_waitqueue_head(&new->recovery_wait
);
375 new->reshape_position
= MaxSector
;
377 new->resync_max
= MaxSector
;
378 new->level
= LEVEL_NONE
;
383 static inline int mddev_lock(mddev_t
* mddev
)
385 return mutex_lock_interruptible(&mddev
->reconfig_mutex
);
388 static inline int mddev_is_locked(mddev_t
*mddev
)
390 return mutex_is_locked(&mddev
->reconfig_mutex
);
393 static inline int mddev_trylock(mddev_t
* mddev
)
395 return mutex_trylock(&mddev
->reconfig_mutex
);
398 static inline void mddev_unlock(mddev_t
* mddev
)
400 mutex_unlock(&mddev
->reconfig_mutex
);
402 md_wakeup_thread(mddev
->thread
);
405 static mdk_rdev_t
* find_rdev_nr(mddev_t
*mddev
, int nr
)
409 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
410 if (rdev
->desc_nr
== nr
)
416 static mdk_rdev_t
* find_rdev(mddev_t
* mddev
, dev_t dev
)
420 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
421 if (rdev
->bdev
->bd_dev
== dev
)
427 static struct mdk_personality
*find_pers(int level
, char *clevel
)
429 struct mdk_personality
*pers
;
430 list_for_each_entry(pers
, &pers_list
, list
) {
431 if (level
!= LEVEL_NONE
&& pers
->level
== level
)
433 if (strcmp(pers
->name
, clevel
)==0)
439 /* return the offset of the super block in 512byte sectors */
440 static inline sector_t
calc_dev_sboffset(struct block_device
*bdev
)
442 sector_t num_sectors
= bdev
->bd_inode
->i_size
/ 512;
443 return MD_NEW_SIZE_SECTORS(num_sectors
);
446 static int alloc_disk_sb(mdk_rdev_t
* rdev
)
451 rdev
->sb_page
= alloc_page(GFP_KERNEL
);
452 if (!rdev
->sb_page
) {
453 printk(KERN_ALERT
"md: out of memory.\n");
460 static void free_disk_sb(mdk_rdev_t
* rdev
)
463 put_page(rdev
->sb_page
);
465 rdev
->sb_page
= NULL
;
472 static void super_written(struct bio
*bio
, int error
)
474 mdk_rdev_t
*rdev
= bio
->bi_private
;
475 mddev_t
*mddev
= rdev
->mddev
;
477 if (error
|| !test_bit(BIO_UPTODATE
, &bio
->bi_flags
)) {
478 printk("md: super_written gets error=%d, uptodate=%d\n",
479 error
, test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
480 WARN_ON(test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
481 md_error(mddev
, rdev
);
484 if (atomic_dec_and_test(&mddev
->pending_writes
))
485 wake_up(&mddev
->sb_wait
);
489 static void super_written_barrier(struct bio
*bio
, int error
)
491 struct bio
*bio2
= bio
->bi_private
;
492 mdk_rdev_t
*rdev
= bio2
->bi_private
;
493 mddev_t
*mddev
= rdev
->mddev
;
495 if (!test_bit(BIO_UPTODATE
, &bio
->bi_flags
) &&
496 error
== -EOPNOTSUPP
) {
498 /* barriers don't appear to be supported :-( */
499 set_bit(BarriersNotsupp
, &rdev
->flags
);
500 mddev
->barriers_work
= 0;
501 spin_lock_irqsave(&mddev
->write_lock
, flags
);
502 bio2
->bi_next
= mddev
->biolist
;
503 mddev
->biolist
= bio2
;
504 spin_unlock_irqrestore(&mddev
->write_lock
, flags
);
505 wake_up(&mddev
->sb_wait
);
509 bio
->bi_private
= rdev
;
510 super_written(bio
, error
);
514 void md_super_write(mddev_t
*mddev
, mdk_rdev_t
*rdev
,
515 sector_t sector
, int size
, struct page
*page
)
517 /* write first size bytes of page to sector of rdev
518 * Increment mddev->pending_writes before returning
519 * and decrement it on completion, waking up sb_wait
520 * if zero is reached.
521 * If an error occurred, call md_error
523 * As we might need to resubmit the request if BIO_RW_BARRIER
524 * causes ENOTSUPP, we allocate a spare bio...
526 struct bio
*bio
= bio_alloc(GFP_NOIO
, 1);
527 int rw
= (1<<BIO_RW
) | (1<<BIO_RW_SYNCIO
) | (1<<BIO_RW_UNPLUG
);
529 bio
->bi_bdev
= rdev
->bdev
;
530 bio
->bi_sector
= sector
;
531 bio_add_page(bio
, page
, size
, 0);
532 bio
->bi_private
= rdev
;
533 bio
->bi_end_io
= super_written
;
536 atomic_inc(&mddev
->pending_writes
);
537 if (!test_bit(BarriersNotsupp
, &rdev
->flags
)) {
539 rw
|= (1<<BIO_RW_BARRIER
);
540 rbio
= bio_clone(bio
, GFP_NOIO
);
541 rbio
->bi_private
= bio
;
542 rbio
->bi_end_io
= super_written_barrier
;
543 submit_bio(rw
, rbio
);
548 void md_super_wait(mddev_t
*mddev
)
550 /* wait for all superblock writes that were scheduled to complete.
551 * if any had to be retried (due to BARRIER problems), retry them
555 prepare_to_wait(&mddev
->sb_wait
, &wq
, TASK_UNINTERRUPTIBLE
);
556 if (atomic_read(&mddev
->pending_writes
)==0)
558 while (mddev
->biolist
) {
560 spin_lock_irq(&mddev
->write_lock
);
561 bio
= mddev
->biolist
;
562 mddev
->biolist
= bio
->bi_next
;
564 spin_unlock_irq(&mddev
->write_lock
);
565 submit_bio(bio
->bi_rw
, bio
);
569 finish_wait(&mddev
->sb_wait
, &wq
);
572 static void bi_complete(struct bio
*bio
, int error
)
574 complete((struct completion
*)bio
->bi_private
);
577 int sync_page_io(struct block_device
*bdev
, sector_t sector
, int size
,
578 struct page
*page
, int rw
)
580 struct bio
*bio
= bio_alloc(GFP_NOIO
, 1);
581 struct completion event
;
584 rw
|= (1 << BIO_RW_SYNCIO
) | (1 << BIO_RW_UNPLUG
);
587 bio
->bi_sector
= sector
;
588 bio_add_page(bio
, page
, size
, 0);
589 init_completion(&event
);
590 bio
->bi_private
= &event
;
591 bio
->bi_end_io
= bi_complete
;
593 wait_for_completion(&event
);
595 ret
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
599 EXPORT_SYMBOL_GPL(sync_page_io
);
601 static int read_disk_sb(mdk_rdev_t
* rdev
, int size
)
603 char b
[BDEVNAME_SIZE
];
604 if (!rdev
->sb_page
) {
612 if (!sync_page_io(rdev
->bdev
, rdev
->sb_start
, size
, rdev
->sb_page
, READ
))
618 printk(KERN_WARNING
"md: disabled device %s, could not read superblock.\n",
619 bdevname(rdev
->bdev
,b
));
623 static int uuid_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
625 return sb1
->set_uuid0
== sb2
->set_uuid0
&&
626 sb1
->set_uuid1
== sb2
->set_uuid1
&&
627 sb1
->set_uuid2
== sb2
->set_uuid2
&&
628 sb1
->set_uuid3
== sb2
->set_uuid3
;
631 static int sb_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
634 mdp_super_t
*tmp1
, *tmp2
;
636 tmp1
= kmalloc(sizeof(*tmp1
),GFP_KERNEL
);
637 tmp2
= kmalloc(sizeof(*tmp2
),GFP_KERNEL
);
639 if (!tmp1
|| !tmp2
) {
641 printk(KERN_INFO
"md.c sb_equal(): failed to allocate memory!\n");
649 * nr_disks is not constant
654 ret
= (memcmp(tmp1
, tmp2
, MD_SB_GENERIC_CONSTANT_WORDS
* 4) == 0);
662 static u32
md_csum_fold(u32 csum
)
664 csum
= (csum
& 0xffff) + (csum
>> 16);
665 return (csum
& 0xffff) + (csum
>> 16);
668 static unsigned int calc_sb_csum(mdp_super_t
* sb
)
671 u32
*sb32
= (u32
*)sb
;
673 unsigned int disk_csum
, csum
;
675 disk_csum
= sb
->sb_csum
;
678 for (i
= 0; i
< MD_SB_BYTES
/4 ; i
++)
680 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
684 /* This used to use csum_partial, which was wrong for several
685 * reasons including that different results are returned on
686 * different architectures. It isn't critical that we get exactly
687 * the same return value as before (we always csum_fold before
688 * testing, and that removes any differences). However as we
689 * know that csum_partial always returned a 16bit value on
690 * alphas, do a fold to maximise conformity to previous behaviour.
692 sb
->sb_csum
= md_csum_fold(disk_csum
);
694 sb
->sb_csum
= disk_csum
;
701 * Handle superblock details.
702 * We want to be able to handle multiple superblock formats
703 * so we have a common interface to them all, and an array of
704 * different handlers.
705 * We rely on user-space to write the initial superblock, and support
706 * reading and updating of superblocks.
707 * Interface methods are:
708 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
709 * loads and validates a superblock on dev.
710 * if refdev != NULL, compare superblocks on both devices
712 * 0 - dev has a superblock that is compatible with refdev
713 * 1 - dev has a superblock that is compatible and newer than refdev
714 * so dev should be used as the refdev in future
715 * -EINVAL superblock incompatible or invalid
716 * -othererror e.g. -EIO
718 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
719 * Verify that dev is acceptable into mddev.
720 * The first time, mddev->raid_disks will be 0, and data from
721 * dev should be merged in. Subsequent calls check that dev
722 * is new enough. Return 0 or -EINVAL
724 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
725 * Update the superblock for rdev with data in mddev
726 * This does not write to disc.
732 struct module
*owner
;
733 int (*load_super
)(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
,
735 int (*validate_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
736 void (*sync_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
737 unsigned long long (*rdev_size_change
)(mdk_rdev_t
*rdev
,
738 sector_t num_sectors
);
742 * Check that the given mddev has no bitmap.
744 * This function is called from the run method of all personalities that do not
745 * support bitmaps. It prints an error message and returns non-zero if mddev
746 * has a bitmap. Otherwise, it returns 0.
749 int md_check_no_bitmap(mddev_t
*mddev
)
751 if (!mddev
->bitmap_file
&& !mddev
->bitmap_offset
)
753 printk(KERN_ERR
"%s: bitmaps are not supported for %s\n",
754 mdname(mddev
), mddev
->pers
->name
);
757 EXPORT_SYMBOL(md_check_no_bitmap
);
760 * load_super for 0.90.0
762 static int super_90_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
764 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
769 * Calculate the position of the superblock (512byte sectors),
770 * it's at the end of the disk.
772 * It also happens to be a multiple of 4Kb.
774 rdev
->sb_start
= calc_dev_sboffset(rdev
->bdev
);
776 ret
= read_disk_sb(rdev
, MD_SB_BYTES
);
781 bdevname(rdev
->bdev
, b
);
782 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
784 if (sb
->md_magic
!= MD_SB_MAGIC
) {
785 printk(KERN_ERR
"md: invalid raid superblock magic on %s\n",
790 if (sb
->major_version
!= 0 ||
791 sb
->minor_version
< 90 ||
792 sb
->minor_version
> 91) {
793 printk(KERN_WARNING
"Bad version number %d.%d on %s\n",
794 sb
->major_version
, sb
->minor_version
,
799 if (sb
->raid_disks
<= 0)
802 if (md_csum_fold(calc_sb_csum(sb
)) != md_csum_fold(sb
->sb_csum
)) {
803 printk(KERN_WARNING
"md: invalid superblock checksum on %s\n",
808 rdev
->preferred_minor
= sb
->md_minor
;
809 rdev
->data_offset
= 0;
810 rdev
->sb_size
= MD_SB_BYTES
;
812 if (sb
->level
== LEVEL_MULTIPATH
)
815 rdev
->desc_nr
= sb
->this_disk
.number
;
821 mdp_super_t
*refsb
= (mdp_super_t
*)page_address(refdev
->sb_page
);
822 if (!uuid_equal(refsb
, sb
)) {
823 printk(KERN_WARNING
"md: %s has different UUID to %s\n",
824 b
, bdevname(refdev
->bdev
,b2
));
827 if (!sb_equal(refsb
, sb
)) {
828 printk(KERN_WARNING
"md: %s has same UUID"
829 " but different superblock to %s\n",
830 b
, bdevname(refdev
->bdev
, b2
));
834 ev2
= md_event(refsb
);
840 rdev
->sectors
= rdev
->sb_start
;
842 if (rdev
->sectors
< sb
->size
* 2 && sb
->level
> 1)
843 /* "this cannot possibly happen" ... */
851 * validate_super for 0.90.0
853 static int super_90_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
856 mdp_super_t
*sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
857 __u64 ev1
= md_event(sb
);
859 rdev
->raid_disk
= -1;
860 clear_bit(Faulty
, &rdev
->flags
);
861 clear_bit(In_sync
, &rdev
->flags
);
862 clear_bit(WriteMostly
, &rdev
->flags
);
863 clear_bit(BarriersNotsupp
, &rdev
->flags
);
865 if (mddev
->raid_disks
== 0) {
866 mddev
->major_version
= 0;
867 mddev
->minor_version
= sb
->minor_version
;
868 mddev
->patch_version
= sb
->patch_version
;
870 mddev
->chunk_sectors
= sb
->chunk_size
>> 9;
871 mddev
->ctime
= sb
->ctime
;
872 mddev
->utime
= sb
->utime
;
873 mddev
->level
= sb
->level
;
874 mddev
->clevel
[0] = 0;
875 mddev
->layout
= sb
->layout
;
876 mddev
->raid_disks
= sb
->raid_disks
;
877 mddev
->dev_sectors
= sb
->size
* 2;
879 mddev
->bitmap_offset
= 0;
880 mddev
->default_bitmap_offset
= MD_SB_BYTES
>> 9;
882 if (mddev
->minor_version
>= 91) {
883 mddev
->reshape_position
= sb
->reshape_position
;
884 mddev
->delta_disks
= sb
->delta_disks
;
885 mddev
->new_level
= sb
->new_level
;
886 mddev
->new_layout
= sb
->new_layout
;
887 mddev
->new_chunk_sectors
= sb
->new_chunk
>> 9;
889 mddev
->reshape_position
= MaxSector
;
890 mddev
->delta_disks
= 0;
891 mddev
->new_level
= mddev
->level
;
892 mddev
->new_layout
= mddev
->layout
;
893 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
896 if (sb
->state
& (1<<MD_SB_CLEAN
))
897 mddev
->recovery_cp
= MaxSector
;
899 if (sb
->events_hi
== sb
->cp_events_hi
&&
900 sb
->events_lo
== sb
->cp_events_lo
) {
901 mddev
->recovery_cp
= sb
->recovery_cp
;
903 mddev
->recovery_cp
= 0;
906 memcpy(mddev
->uuid
+0, &sb
->set_uuid0
, 4);
907 memcpy(mddev
->uuid
+4, &sb
->set_uuid1
, 4);
908 memcpy(mddev
->uuid
+8, &sb
->set_uuid2
, 4);
909 memcpy(mddev
->uuid
+12,&sb
->set_uuid3
, 4);
911 mddev
->max_disks
= MD_SB_DISKS
;
913 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
) &&
914 mddev
->bitmap_file
== NULL
)
915 mddev
->bitmap_offset
= mddev
->default_bitmap_offset
;
917 } else if (mddev
->pers
== NULL
) {
918 /* Insist on good event counter while assembling */
920 if (ev1
< mddev
->events
)
922 } else if (mddev
->bitmap
) {
923 /* if adding to array with a bitmap, then we can accept an
924 * older device ... but not too old.
926 if (ev1
< mddev
->bitmap
->events_cleared
)
929 if (ev1
< mddev
->events
)
930 /* just a hot-add of a new device, leave raid_disk at -1 */
934 if (mddev
->level
!= LEVEL_MULTIPATH
) {
935 desc
= sb
->disks
+ rdev
->desc_nr
;
937 if (desc
->state
& (1<<MD_DISK_FAULTY
))
938 set_bit(Faulty
, &rdev
->flags
);
939 else if (desc
->state
& (1<<MD_DISK_SYNC
) /* &&
940 desc->raid_disk < mddev->raid_disks */) {
941 set_bit(In_sync
, &rdev
->flags
);
942 rdev
->raid_disk
= desc
->raid_disk
;
943 } else if (desc
->state
& (1<<MD_DISK_ACTIVE
)) {
944 /* active but not in sync implies recovery up to
945 * reshape position. We don't know exactly where
946 * that is, so set to zero for now */
947 if (mddev
->minor_version
>= 91) {
948 rdev
->recovery_offset
= 0;
949 rdev
->raid_disk
= desc
->raid_disk
;
952 if (desc
->state
& (1<<MD_DISK_WRITEMOSTLY
))
953 set_bit(WriteMostly
, &rdev
->flags
);
954 } else /* MULTIPATH are always insync */
955 set_bit(In_sync
, &rdev
->flags
);
960 * sync_super for 0.90.0
962 static void super_90_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
966 int next_spare
= mddev
->raid_disks
;
969 /* make rdev->sb match mddev data..
972 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
973 * 3/ any empty disks < next_spare become removed
975 * disks[0] gets initialised to REMOVED because
976 * we cannot be sure from other fields if it has
977 * been initialised or not.
980 int active
=0, working
=0,failed
=0,spare
=0,nr_disks
=0;
982 rdev
->sb_size
= MD_SB_BYTES
;
984 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
986 memset(sb
, 0, sizeof(*sb
));
988 sb
->md_magic
= MD_SB_MAGIC
;
989 sb
->major_version
= mddev
->major_version
;
990 sb
->patch_version
= mddev
->patch_version
;
991 sb
->gvalid_words
= 0; /* ignored */
992 memcpy(&sb
->set_uuid0
, mddev
->uuid
+0, 4);
993 memcpy(&sb
->set_uuid1
, mddev
->uuid
+4, 4);
994 memcpy(&sb
->set_uuid2
, mddev
->uuid
+8, 4);
995 memcpy(&sb
->set_uuid3
, mddev
->uuid
+12,4);
997 sb
->ctime
= mddev
->ctime
;
998 sb
->level
= mddev
->level
;
999 sb
->size
= mddev
->dev_sectors
/ 2;
1000 sb
->raid_disks
= mddev
->raid_disks
;
1001 sb
->md_minor
= mddev
->md_minor
;
1002 sb
->not_persistent
= 0;
1003 sb
->utime
= mddev
->utime
;
1005 sb
->events_hi
= (mddev
->events
>>32);
1006 sb
->events_lo
= (u32
)mddev
->events
;
1008 if (mddev
->reshape_position
== MaxSector
)
1009 sb
->minor_version
= 90;
1011 sb
->minor_version
= 91;
1012 sb
->reshape_position
= mddev
->reshape_position
;
1013 sb
->new_level
= mddev
->new_level
;
1014 sb
->delta_disks
= mddev
->delta_disks
;
1015 sb
->new_layout
= mddev
->new_layout
;
1016 sb
->new_chunk
= mddev
->new_chunk_sectors
<< 9;
1018 mddev
->minor_version
= sb
->minor_version
;
1021 sb
->recovery_cp
= mddev
->recovery_cp
;
1022 sb
->cp_events_hi
= (mddev
->events
>>32);
1023 sb
->cp_events_lo
= (u32
)mddev
->events
;
1024 if (mddev
->recovery_cp
== MaxSector
)
1025 sb
->state
= (1<< MD_SB_CLEAN
);
1027 sb
->recovery_cp
= 0;
1029 sb
->layout
= mddev
->layout
;
1030 sb
->chunk_size
= mddev
->chunk_sectors
<< 9;
1032 if (mddev
->bitmap
&& mddev
->bitmap_file
== NULL
)
1033 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
1035 sb
->disks
[0].state
= (1<<MD_DISK_REMOVED
);
1036 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
1039 int is_active
= test_bit(In_sync
, &rdev2
->flags
);
1041 if (rdev2
->raid_disk
>= 0 &&
1042 sb
->minor_version
>= 91)
1043 /* we have nowhere to store the recovery_offset,
1044 * but if it is not below the reshape_position,
1045 * we can piggy-back on that.
1048 if (rdev2
->raid_disk
< 0 ||
1049 test_bit(Faulty
, &rdev2
->flags
))
1052 desc_nr
= rdev2
->raid_disk
;
1054 desc_nr
= next_spare
++;
1055 rdev2
->desc_nr
= desc_nr
;
1056 d
= &sb
->disks
[rdev2
->desc_nr
];
1058 d
->number
= rdev2
->desc_nr
;
1059 d
->major
= MAJOR(rdev2
->bdev
->bd_dev
);
1060 d
->minor
= MINOR(rdev2
->bdev
->bd_dev
);
1062 d
->raid_disk
= rdev2
->raid_disk
;
1064 d
->raid_disk
= rdev2
->desc_nr
; /* compatibility */
1065 if (test_bit(Faulty
, &rdev2
->flags
))
1066 d
->state
= (1<<MD_DISK_FAULTY
);
1067 else if (is_active
) {
1068 d
->state
= (1<<MD_DISK_ACTIVE
);
1069 if (test_bit(In_sync
, &rdev2
->flags
))
1070 d
->state
|= (1<<MD_DISK_SYNC
);
1078 if (test_bit(WriteMostly
, &rdev2
->flags
))
1079 d
->state
|= (1<<MD_DISK_WRITEMOSTLY
);
1081 /* now set the "removed" and "faulty" bits on any missing devices */
1082 for (i
=0 ; i
< mddev
->raid_disks
; i
++) {
1083 mdp_disk_t
*d
= &sb
->disks
[i
];
1084 if (d
->state
== 0 && d
->number
== 0) {
1087 d
->state
= (1<<MD_DISK_REMOVED
);
1088 d
->state
|= (1<<MD_DISK_FAULTY
);
1092 sb
->nr_disks
= nr_disks
;
1093 sb
->active_disks
= active
;
1094 sb
->working_disks
= working
;
1095 sb
->failed_disks
= failed
;
1096 sb
->spare_disks
= spare
;
1098 sb
->this_disk
= sb
->disks
[rdev
->desc_nr
];
1099 sb
->sb_csum
= calc_sb_csum(sb
);
1103 * rdev_size_change for 0.90.0
1105 static unsigned long long
1106 super_90_rdev_size_change(mdk_rdev_t
*rdev
, sector_t num_sectors
)
1108 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1109 return 0; /* component must fit device */
1110 if (rdev
->mddev
->bitmap_offset
)
1111 return 0; /* can't move bitmap */
1112 rdev
->sb_start
= calc_dev_sboffset(rdev
->bdev
);
1113 if (!num_sectors
|| num_sectors
> rdev
->sb_start
)
1114 num_sectors
= rdev
->sb_start
;
1115 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1117 md_super_wait(rdev
->mddev
);
1118 return num_sectors
/ 2; /* kB for sysfs */
1123 * version 1 superblock
1126 static __le32
calc_sb_1_csum(struct mdp_superblock_1
* sb
)
1130 unsigned long long newcsum
;
1131 int size
= 256 + le32_to_cpu(sb
->max_dev
)*2;
1132 __le32
*isuper
= (__le32
*)sb
;
1135 disk_csum
= sb
->sb_csum
;
1138 for (i
=0; size
>=4; size
-= 4 )
1139 newcsum
+= le32_to_cpu(*isuper
++);
1142 newcsum
+= le16_to_cpu(*(__le16
*) isuper
);
1144 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
1145 sb
->sb_csum
= disk_csum
;
1146 return cpu_to_le32(csum
);
1149 static int super_1_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
1151 struct mdp_superblock_1
*sb
;
1154 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1158 * Calculate the position of the superblock in 512byte sectors.
1159 * It is always aligned to a 4K boundary and
1160 * depeding on minor_version, it can be:
1161 * 0: At least 8K, but less than 12K, from end of device
1162 * 1: At start of device
1163 * 2: 4K from start of device.
1165 switch(minor_version
) {
1167 sb_start
= rdev
->bdev
->bd_inode
->i_size
>> 9;
1169 sb_start
&= ~(sector_t
)(4*2-1);
1180 rdev
->sb_start
= sb_start
;
1182 /* superblock is rarely larger than 1K, but it can be larger,
1183 * and it is safe to read 4k, so we do that
1185 ret
= read_disk_sb(rdev
, 4096);
1186 if (ret
) return ret
;
1189 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1191 if (sb
->magic
!= cpu_to_le32(MD_SB_MAGIC
) ||
1192 sb
->major_version
!= cpu_to_le32(1) ||
1193 le32_to_cpu(sb
->max_dev
) > (4096-256)/2 ||
1194 le64_to_cpu(sb
->super_offset
) != rdev
->sb_start
||
1195 (le32_to_cpu(sb
->feature_map
) & ~MD_FEATURE_ALL
) != 0)
1198 if (calc_sb_1_csum(sb
) != sb
->sb_csum
) {
1199 printk("md: invalid superblock checksum on %s\n",
1200 bdevname(rdev
->bdev
,b
));
1203 if (le64_to_cpu(sb
->data_size
) < 10) {
1204 printk("md: data_size too small on %s\n",
1205 bdevname(rdev
->bdev
,b
));
1209 rdev
->preferred_minor
= 0xffff;
1210 rdev
->data_offset
= le64_to_cpu(sb
->data_offset
);
1211 atomic_set(&rdev
->corrected_errors
, le32_to_cpu(sb
->cnt_corrected_read
));
1213 rdev
->sb_size
= le32_to_cpu(sb
->max_dev
) * 2 + 256;
1214 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1215 if (rdev
->sb_size
& bmask
)
1216 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1219 && rdev
->data_offset
< sb_start
+ (rdev
->sb_size
/512))
1222 if (sb
->level
== cpu_to_le32(LEVEL_MULTIPATH
))
1225 rdev
->desc_nr
= le32_to_cpu(sb
->dev_number
);
1231 struct mdp_superblock_1
*refsb
=
1232 (struct mdp_superblock_1
*)page_address(refdev
->sb_page
);
1234 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16) != 0 ||
1235 sb
->level
!= refsb
->level
||
1236 sb
->layout
!= refsb
->layout
||
1237 sb
->chunksize
!= refsb
->chunksize
) {
1238 printk(KERN_WARNING
"md: %s has strangely different"
1239 " superblock to %s\n",
1240 bdevname(rdev
->bdev
,b
),
1241 bdevname(refdev
->bdev
,b2
));
1244 ev1
= le64_to_cpu(sb
->events
);
1245 ev2
= le64_to_cpu(refsb
->events
);
1253 rdev
->sectors
= (rdev
->bdev
->bd_inode
->i_size
>> 9) -
1254 le64_to_cpu(sb
->data_offset
);
1256 rdev
->sectors
= rdev
->sb_start
;
1257 if (rdev
->sectors
< le64_to_cpu(sb
->data_size
))
1259 rdev
->sectors
= le64_to_cpu(sb
->data_size
);
1260 if (le64_to_cpu(sb
->size
) > rdev
->sectors
)
1265 static int super_1_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1267 struct mdp_superblock_1
*sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1268 __u64 ev1
= le64_to_cpu(sb
->events
);
1270 rdev
->raid_disk
= -1;
1271 clear_bit(Faulty
, &rdev
->flags
);
1272 clear_bit(In_sync
, &rdev
->flags
);
1273 clear_bit(WriteMostly
, &rdev
->flags
);
1274 clear_bit(BarriersNotsupp
, &rdev
->flags
);
1276 if (mddev
->raid_disks
== 0) {
1277 mddev
->major_version
= 1;
1278 mddev
->patch_version
= 0;
1279 mddev
->external
= 0;
1280 mddev
->chunk_sectors
= le32_to_cpu(sb
->chunksize
);
1281 mddev
->ctime
= le64_to_cpu(sb
->ctime
) & ((1ULL << 32)-1);
1282 mddev
->utime
= le64_to_cpu(sb
->utime
) & ((1ULL << 32)-1);
1283 mddev
->level
= le32_to_cpu(sb
->level
);
1284 mddev
->clevel
[0] = 0;
1285 mddev
->layout
= le32_to_cpu(sb
->layout
);
1286 mddev
->raid_disks
= le32_to_cpu(sb
->raid_disks
);
1287 mddev
->dev_sectors
= le64_to_cpu(sb
->size
);
1288 mddev
->events
= ev1
;
1289 mddev
->bitmap_offset
= 0;
1290 mddev
->default_bitmap_offset
= 1024 >> 9;
1292 mddev
->recovery_cp
= le64_to_cpu(sb
->resync_offset
);
1293 memcpy(mddev
->uuid
, sb
->set_uuid
, 16);
1295 mddev
->max_disks
= (4096-256)/2;
1297 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BITMAP_OFFSET
) &&
1298 mddev
->bitmap_file
== NULL
)
1299 mddev
->bitmap_offset
= (__s32
)le32_to_cpu(sb
->bitmap_offset
);
1301 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
1302 mddev
->reshape_position
= le64_to_cpu(sb
->reshape_position
);
1303 mddev
->delta_disks
= le32_to_cpu(sb
->delta_disks
);
1304 mddev
->new_level
= le32_to_cpu(sb
->new_level
);
1305 mddev
->new_layout
= le32_to_cpu(sb
->new_layout
);
1306 mddev
->new_chunk_sectors
= le32_to_cpu(sb
->new_chunk
);
1308 mddev
->reshape_position
= MaxSector
;
1309 mddev
->delta_disks
= 0;
1310 mddev
->new_level
= mddev
->level
;
1311 mddev
->new_layout
= mddev
->layout
;
1312 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1315 } else if (mddev
->pers
== NULL
) {
1316 /* Insist of good event counter while assembling */
1318 if (ev1
< mddev
->events
)
1320 } else if (mddev
->bitmap
) {
1321 /* If adding to array with a bitmap, then we can accept an
1322 * older device, but not too old.
1324 if (ev1
< mddev
->bitmap
->events_cleared
)
1327 if (ev1
< mddev
->events
)
1328 /* just a hot-add of a new device, leave raid_disk at -1 */
1331 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1333 if (rdev
->desc_nr
< 0 ||
1334 rdev
->desc_nr
>= le32_to_cpu(sb
->max_dev
)) {
1338 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
1340 case 0xffff: /* spare */
1342 case 0xfffe: /* faulty */
1343 set_bit(Faulty
, &rdev
->flags
);
1346 if ((le32_to_cpu(sb
->feature_map
) &
1347 MD_FEATURE_RECOVERY_OFFSET
))
1348 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
1350 set_bit(In_sync
, &rdev
->flags
);
1351 rdev
->raid_disk
= role
;
1354 if (sb
->devflags
& WriteMostly1
)
1355 set_bit(WriteMostly
, &rdev
->flags
);
1356 } else /* MULTIPATH are always insync */
1357 set_bit(In_sync
, &rdev
->flags
);
1362 static void super_1_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1364 struct mdp_superblock_1
*sb
;
1367 /* make rdev->sb match mddev and rdev data. */
1369 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1371 sb
->feature_map
= 0;
1373 sb
->recovery_offset
= cpu_to_le64(0);
1374 memset(sb
->pad1
, 0, sizeof(sb
->pad1
));
1375 memset(sb
->pad2
, 0, sizeof(sb
->pad2
));
1376 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
1378 sb
->utime
= cpu_to_le64((__u64
)mddev
->utime
);
1379 sb
->events
= cpu_to_le64(mddev
->events
);
1381 sb
->resync_offset
= cpu_to_le64(mddev
->recovery_cp
);
1383 sb
->resync_offset
= cpu_to_le64(0);
1385 sb
->cnt_corrected_read
= cpu_to_le32(atomic_read(&rdev
->corrected_errors
));
1387 sb
->raid_disks
= cpu_to_le32(mddev
->raid_disks
);
1388 sb
->size
= cpu_to_le64(mddev
->dev_sectors
);
1389 sb
->chunksize
= cpu_to_le32(mddev
->chunk_sectors
);
1390 sb
->level
= cpu_to_le32(mddev
->level
);
1391 sb
->layout
= cpu_to_le32(mddev
->layout
);
1393 if (mddev
->bitmap
&& mddev
->bitmap_file
== NULL
) {
1394 sb
->bitmap_offset
= cpu_to_le32((__u32
)mddev
->bitmap_offset
);
1395 sb
->feature_map
= cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
);
1398 if (rdev
->raid_disk
>= 0 &&
1399 !test_bit(In_sync
, &rdev
->flags
)) {
1400 if (rdev
->recovery_offset
> 0) {
1402 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET
);
1403 sb
->recovery_offset
=
1404 cpu_to_le64(rdev
->recovery_offset
);
1408 if (mddev
->reshape_position
!= MaxSector
) {
1409 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE
);
1410 sb
->reshape_position
= cpu_to_le64(mddev
->reshape_position
);
1411 sb
->new_layout
= cpu_to_le32(mddev
->new_layout
);
1412 sb
->delta_disks
= cpu_to_le32(mddev
->delta_disks
);
1413 sb
->new_level
= cpu_to_le32(mddev
->new_level
);
1414 sb
->new_chunk
= cpu_to_le32(mddev
->new_chunk_sectors
);
1418 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
)
1419 if (rdev2
->desc_nr
+1 > max_dev
)
1420 max_dev
= rdev2
->desc_nr
+1;
1422 if (max_dev
> le32_to_cpu(sb
->max_dev
)) {
1424 sb
->max_dev
= cpu_to_le32(max_dev
);
1425 rdev
->sb_size
= max_dev
* 2 + 256;
1426 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1427 if (rdev
->sb_size
& bmask
)
1428 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1430 for (i
=0; i
<max_dev
;i
++)
1431 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1433 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
1435 if (test_bit(Faulty
, &rdev2
->flags
))
1436 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1437 else if (test_bit(In_sync
, &rdev2
->flags
))
1438 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1439 else if (rdev2
->raid_disk
>= 0 && rdev2
->recovery_offset
> 0)
1440 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1442 sb
->dev_roles
[i
] = cpu_to_le16(0xffff);
1445 sb
->sb_csum
= calc_sb_1_csum(sb
);
1448 static unsigned long long
1449 super_1_rdev_size_change(mdk_rdev_t
*rdev
, sector_t num_sectors
)
1451 struct mdp_superblock_1
*sb
;
1452 sector_t max_sectors
;
1453 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1454 return 0; /* component must fit device */
1455 if (rdev
->sb_start
< rdev
->data_offset
) {
1456 /* minor versions 1 and 2; superblock before data */
1457 max_sectors
= rdev
->bdev
->bd_inode
->i_size
>> 9;
1458 max_sectors
-= rdev
->data_offset
;
1459 if (!num_sectors
|| num_sectors
> max_sectors
)
1460 num_sectors
= max_sectors
;
1461 } else if (rdev
->mddev
->bitmap_offset
) {
1462 /* minor version 0 with bitmap we can't move */
1465 /* minor version 0; superblock after data */
1467 sb_start
= (rdev
->bdev
->bd_inode
->i_size
>> 9) - 8*2;
1468 sb_start
&= ~(sector_t
)(4*2 - 1);
1469 max_sectors
= rdev
->sectors
+ sb_start
- rdev
->sb_start
;
1470 if (!num_sectors
|| num_sectors
> max_sectors
)
1471 num_sectors
= max_sectors
;
1472 rdev
->sb_start
= sb_start
;
1474 sb
= (struct mdp_superblock_1
*) page_address(rdev
->sb_page
);
1475 sb
->data_size
= cpu_to_le64(num_sectors
);
1476 sb
->super_offset
= rdev
->sb_start
;
1477 sb
->sb_csum
= calc_sb_1_csum(sb
);
1478 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1480 md_super_wait(rdev
->mddev
);
1481 return num_sectors
/ 2; /* kB for sysfs */
1484 static struct super_type super_types
[] = {
1487 .owner
= THIS_MODULE
,
1488 .load_super
= super_90_load
,
1489 .validate_super
= super_90_validate
,
1490 .sync_super
= super_90_sync
,
1491 .rdev_size_change
= super_90_rdev_size_change
,
1495 .owner
= THIS_MODULE
,
1496 .load_super
= super_1_load
,
1497 .validate_super
= super_1_validate
,
1498 .sync_super
= super_1_sync
,
1499 .rdev_size_change
= super_1_rdev_size_change
,
1503 static int match_mddev_units(mddev_t
*mddev1
, mddev_t
*mddev2
)
1505 mdk_rdev_t
*rdev
, *rdev2
;
1508 rdev_for_each_rcu(rdev
, mddev1
)
1509 rdev_for_each_rcu(rdev2
, mddev2
)
1510 if (rdev
->bdev
->bd_contains
==
1511 rdev2
->bdev
->bd_contains
) {
1519 static LIST_HEAD(pending_raid_disks
);
1522 * Try to register data integrity profile for an mddev
1524 * This is called when an array is started and after a disk has been kicked
1525 * from the array. It only succeeds if all working and active component devices
1526 * are integrity capable with matching profiles.
1528 int md_integrity_register(mddev_t
*mddev
)
1530 mdk_rdev_t
*rdev
, *reference
= NULL
;
1532 if (list_empty(&mddev
->disks
))
1533 return 0; /* nothing to do */
1534 if (blk_get_integrity(mddev
->gendisk
))
1535 return 0; /* already registered */
1536 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
1537 /* skip spares and non-functional disks */
1538 if (test_bit(Faulty
, &rdev
->flags
))
1540 if (rdev
->raid_disk
< 0)
1543 * If at least one rdev is not integrity capable, we can not
1544 * enable data integrity for the md device.
1546 if (!bdev_get_integrity(rdev
->bdev
))
1549 /* Use the first rdev as the reference */
1553 /* does this rdev's profile match the reference profile? */
1554 if (blk_integrity_compare(reference
->bdev
->bd_disk
,
1555 rdev
->bdev
->bd_disk
) < 0)
1559 * All component devices are integrity capable and have matching
1560 * profiles, register the common profile for the md device.
1562 if (blk_integrity_register(mddev
->gendisk
,
1563 bdev_get_integrity(reference
->bdev
)) != 0) {
1564 printk(KERN_ERR
"md: failed to register integrity for %s\n",
1568 printk(KERN_NOTICE
"md: data integrity on %s enabled\n",
1572 EXPORT_SYMBOL(md_integrity_register
);
1574 /* Disable data integrity if non-capable/non-matching disk is being added */
1575 void md_integrity_add_rdev(mdk_rdev_t
*rdev
, mddev_t
*mddev
)
1577 struct blk_integrity
*bi_rdev
= bdev_get_integrity(rdev
->bdev
);
1578 struct blk_integrity
*bi_mddev
= blk_get_integrity(mddev
->gendisk
);
1580 if (!bi_mddev
) /* nothing to do */
1582 if (rdev
->raid_disk
< 0) /* skip spares */
1584 if (bi_rdev
&& blk_integrity_compare(mddev
->gendisk
,
1585 rdev
->bdev
->bd_disk
) >= 0)
1587 printk(KERN_NOTICE
"disabling data integrity on %s\n", mdname(mddev
));
1588 blk_integrity_unregister(mddev
->gendisk
);
1590 EXPORT_SYMBOL(md_integrity_add_rdev
);
1592 static int bind_rdev_to_array(mdk_rdev_t
* rdev
, mddev_t
* mddev
)
1594 char b
[BDEVNAME_SIZE
];
1604 /* prevent duplicates */
1605 if (find_rdev(mddev
, rdev
->bdev
->bd_dev
))
1608 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1609 if (rdev
->sectors
&& (mddev
->dev_sectors
== 0 ||
1610 rdev
->sectors
< mddev
->dev_sectors
)) {
1612 /* Cannot change size, so fail
1613 * If mddev->level <= 0, then we don't care
1614 * about aligning sizes (e.g. linear)
1616 if (mddev
->level
> 0)
1619 mddev
->dev_sectors
= rdev
->sectors
;
1622 /* Verify rdev->desc_nr is unique.
1623 * If it is -1, assign a free number, else
1624 * check number is not in use
1626 if (rdev
->desc_nr
< 0) {
1628 if (mddev
->pers
) choice
= mddev
->raid_disks
;
1629 while (find_rdev_nr(mddev
, choice
))
1631 rdev
->desc_nr
= choice
;
1633 if (find_rdev_nr(mddev
, rdev
->desc_nr
))
1636 if (mddev
->max_disks
&& rdev
->desc_nr
>= mddev
->max_disks
) {
1637 printk(KERN_WARNING
"md: %s: array is limited to %d devices\n",
1638 mdname(mddev
), mddev
->max_disks
);
1641 bdevname(rdev
->bdev
,b
);
1642 while ( (s
=strchr(b
, '/')) != NULL
)
1645 rdev
->mddev
= mddev
;
1646 printk(KERN_INFO
"md: bind<%s>\n", b
);
1648 if ((err
= kobject_add(&rdev
->kobj
, &mddev
->kobj
, "dev-%s", b
)))
1651 ko
= &part_to_dev(rdev
->bdev
->bd_part
)->kobj
;
1652 if ((err
= sysfs_create_link(&rdev
->kobj
, ko
, "block"))) {
1653 kobject_del(&rdev
->kobj
);
1656 rdev
->sysfs_state
= sysfs_get_dirent(rdev
->kobj
.sd
, "state");
1658 list_add_rcu(&rdev
->same_set
, &mddev
->disks
);
1659 bd_claim_by_disk(rdev
->bdev
, rdev
->bdev
->bd_holder
, mddev
->gendisk
);
1661 /* May as well allow recovery to be retried once */
1662 mddev
->recovery_disabled
= 0;
1667 printk(KERN_WARNING
"md: failed to register dev-%s for %s\n",
1672 static void md_delayed_delete(struct work_struct
*ws
)
1674 mdk_rdev_t
*rdev
= container_of(ws
, mdk_rdev_t
, del_work
);
1675 kobject_del(&rdev
->kobj
);
1676 kobject_put(&rdev
->kobj
);
1679 static void unbind_rdev_from_array(mdk_rdev_t
* rdev
)
1681 char b
[BDEVNAME_SIZE
];
1686 bd_release_from_disk(rdev
->bdev
, rdev
->mddev
->gendisk
);
1687 list_del_rcu(&rdev
->same_set
);
1688 printk(KERN_INFO
"md: unbind<%s>\n", bdevname(rdev
->bdev
,b
));
1690 sysfs_remove_link(&rdev
->kobj
, "block");
1691 sysfs_put(rdev
->sysfs_state
);
1692 rdev
->sysfs_state
= NULL
;
1693 /* We need to delay this, otherwise we can deadlock when
1694 * writing to 'remove' to "dev/state". We also need
1695 * to delay it due to rcu usage.
1698 INIT_WORK(&rdev
->del_work
, md_delayed_delete
);
1699 kobject_get(&rdev
->kobj
);
1700 schedule_work(&rdev
->del_work
);
1704 * prevent the device from being mounted, repartitioned or
1705 * otherwise reused by a RAID array (or any other kernel
1706 * subsystem), by bd_claiming the device.
1708 static int lock_rdev(mdk_rdev_t
*rdev
, dev_t dev
, int shared
)
1711 struct block_device
*bdev
;
1712 char b
[BDEVNAME_SIZE
];
1714 bdev
= open_by_devnum(dev
, FMODE_READ
|FMODE_WRITE
);
1716 printk(KERN_ERR
"md: could not open %s.\n",
1717 __bdevname(dev
, b
));
1718 return PTR_ERR(bdev
);
1720 err
= bd_claim(bdev
, shared
? (mdk_rdev_t
*)lock_rdev
: rdev
);
1722 printk(KERN_ERR
"md: could not bd_claim %s.\n",
1724 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
);
1728 set_bit(AllReserved
, &rdev
->flags
);
1733 static void unlock_rdev(mdk_rdev_t
*rdev
)
1735 struct block_device
*bdev
= rdev
->bdev
;
1740 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
);
1743 void md_autodetect_dev(dev_t dev
);
1745 static void export_rdev(mdk_rdev_t
* rdev
)
1747 char b
[BDEVNAME_SIZE
];
1748 printk(KERN_INFO
"md: export_rdev(%s)\n",
1749 bdevname(rdev
->bdev
,b
));
1754 if (test_bit(AutoDetected
, &rdev
->flags
))
1755 md_autodetect_dev(rdev
->bdev
->bd_dev
);
1758 kobject_put(&rdev
->kobj
);
1761 static void kick_rdev_from_array(mdk_rdev_t
* rdev
)
1763 unbind_rdev_from_array(rdev
);
1767 static void export_array(mddev_t
*mddev
)
1769 mdk_rdev_t
*rdev
, *tmp
;
1771 rdev_for_each(rdev
, tmp
, mddev
) {
1776 kick_rdev_from_array(rdev
);
1778 if (!list_empty(&mddev
->disks
))
1780 mddev
->raid_disks
= 0;
1781 mddev
->major_version
= 0;
1784 static void print_desc(mdp_disk_t
*desc
)
1786 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc
->number
,
1787 desc
->major
,desc
->minor
,desc
->raid_disk
,desc
->state
);
1790 static void print_sb_90(mdp_super_t
*sb
)
1795 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1796 sb
->major_version
, sb
->minor_version
, sb
->patch_version
,
1797 sb
->set_uuid0
, sb
->set_uuid1
, sb
->set_uuid2
, sb
->set_uuid3
,
1799 printk(KERN_INFO
"md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1800 sb
->level
, sb
->size
, sb
->nr_disks
, sb
->raid_disks
,
1801 sb
->md_minor
, sb
->layout
, sb
->chunk_size
);
1802 printk(KERN_INFO
"md: UT:%08x ST:%d AD:%d WD:%d"
1803 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1804 sb
->utime
, sb
->state
, sb
->active_disks
, sb
->working_disks
,
1805 sb
->failed_disks
, sb
->spare_disks
,
1806 sb
->sb_csum
, (unsigned long)sb
->events_lo
);
1809 for (i
= 0; i
< MD_SB_DISKS
; i
++) {
1812 desc
= sb
->disks
+ i
;
1813 if (desc
->number
|| desc
->major
|| desc
->minor
||
1814 desc
->raid_disk
|| (desc
->state
&& (desc
->state
!= 4))) {
1815 printk(" D %2d: ", i
);
1819 printk(KERN_INFO
"md: THIS: ");
1820 print_desc(&sb
->this_disk
);
1823 static void print_sb_1(struct mdp_superblock_1
*sb
)
1827 uuid
= sb
->set_uuid
;
1829 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%02x%02x%02x%02x"
1830 ":%02x%02x:%02x%02x:%02x%02x:%02x%02x%02x%02x%02x%02x>\n"
1831 "md: Name: \"%s\" CT:%llu\n",
1832 le32_to_cpu(sb
->major_version
),
1833 le32_to_cpu(sb
->feature_map
),
1834 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
1835 uuid
[4], uuid
[5], uuid
[6], uuid
[7],
1836 uuid
[8], uuid
[9], uuid
[10], uuid
[11],
1837 uuid
[12], uuid
[13], uuid
[14], uuid
[15],
1839 (unsigned long long)le64_to_cpu(sb
->ctime
)
1840 & MD_SUPERBLOCK_1_TIME_SEC_MASK
);
1842 uuid
= sb
->device_uuid
;
1844 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
1846 "md: Dev:%08x UUID: %02x%02x%02x%02x:%02x%02x:%02x%02x:%02x%02x"
1847 ":%02x%02x%02x%02x%02x%02x\n"
1848 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
1849 "md: (MaxDev:%u) \n",
1850 le32_to_cpu(sb
->level
),
1851 (unsigned long long)le64_to_cpu(sb
->size
),
1852 le32_to_cpu(sb
->raid_disks
),
1853 le32_to_cpu(sb
->layout
),
1854 le32_to_cpu(sb
->chunksize
),
1855 (unsigned long long)le64_to_cpu(sb
->data_offset
),
1856 (unsigned long long)le64_to_cpu(sb
->data_size
),
1857 (unsigned long long)le64_to_cpu(sb
->super_offset
),
1858 (unsigned long long)le64_to_cpu(sb
->recovery_offset
),
1859 le32_to_cpu(sb
->dev_number
),
1860 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
1861 uuid
[4], uuid
[5], uuid
[6], uuid
[7],
1862 uuid
[8], uuid
[9], uuid
[10], uuid
[11],
1863 uuid
[12], uuid
[13], uuid
[14], uuid
[15],
1865 (unsigned long long)le64_to_cpu(sb
->utime
) & MD_SUPERBLOCK_1_TIME_SEC_MASK
,
1866 (unsigned long long)le64_to_cpu(sb
->events
),
1867 (unsigned long long)le64_to_cpu(sb
->resync_offset
),
1868 le32_to_cpu(sb
->sb_csum
),
1869 le32_to_cpu(sb
->max_dev
)
1873 static void print_rdev(mdk_rdev_t
*rdev
, int major_version
)
1875 char b
[BDEVNAME_SIZE
];
1876 printk(KERN_INFO
"md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
1877 bdevname(rdev
->bdev
, b
), (unsigned long long)rdev
->sectors
,
1878 test_bit(Faulty
, &rdev
->flags
), test_bit(In_sync
, &rdev
->flags
),
1880 if (rdev
->sb_loaded
) {
1881 printk(KERN_INFO
"md: rdev superblock (MJ:%d):\n", major_version
);
1882 switch (major_version
) {
1884 print_sb_90((mdp_super_t
*)page_address(rdev
->sb_page
));
1887 print_sb_1((struct mdp_superblock_1
*)page_address(rdev
->sb_page
));
1891 printk(KERN_INFO
"md: no rdev superblock!\n");
1894 static void md_print_devices(void)
1896 struct list_head
*tmp
;
1899 char b
[BDEVNAME_SIZE
];
1902 printk("md: **********************************\n");
1903 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1904 printk("md: **********************************\n");
1905 for_each_mddev(mddev
, tmp
) {
1908 bitmap_print_sb(mddev
->bitmap
);
1910 printk("%s: ", mdname(mddev
));
1911 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
1912 printk("<%s>", bdevname(rdev
->bdev
,b
));
1915 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
1916 print_rdev(rdev
, mddev
->major_version
);
1918 printk("md: **********************************\n");
1923 static void sync_sbs(mddev_t
* mddev
, int nospares
)
1925 /* Update each superblock (in-memory image), but
1926 * if we are allowed to, skip spares which already
1927 * have the right event counter, or have one earlier
1928 * (which would mean they aren't being marked as dirty
1929 * with the rest of the array)
1933 /* First make sure individual recovery_offsets are correct */
1934 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
1935 if (rdev
->raid_disk
>= 0 &&
1936 !test_bit(In_sync
, &rdev
->flags
) &&
1937 mddev
->curr_resync_completed
> rdev
->recovery_offset
)
1938 rdev
->recovery_offset
= mddev
->curr_resync_completed
;
1941 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
1942 if (rdev
->sb_events
== mddev
->events
||
1944 rdev
->raid_disk
< 0 &&
1945 (rdev
->sb_events
&1)==0 &&
1946 rdev
->sb_events
+1 == mddev
->events
)) {
1947 /* Don't update this superblock */
1948 rdev
->sb_loaded
= 2;
1950 super_types
[mddev
->major_version
].
1951 sync_super(mddev
, rdev
);
1952 rdev
->sb_loaded
= 1;
1957 static void md_update_sb(mddev_t
* mddev
, int force_change
)
1963 mddev
->utime
= get_seconds();
1964 if (mddev
->external
)
1967 spin_lock_irq(&mddev
->write_lock
);
1969 set_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
1970 if (test_and_clear_bit(MD_CHANGE_DEVS
, &mddev
->flags
))
1972 if (test_and_clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
))
1973 /* just a clean<-> dirty transition, possibly leave spares alone,
1974 * though if events isn't the right even/odd, we will have to do
1980 if (mddev
->degraded
)
1981 /* If the array is degraded, then skipping spares is both
1982 * dangerous and fairly pointless.
1983 * Dangerous because a device that was removed from the array
1984 * might have a event_count that still looks up-to-date,
1985 * so it can be re-added without a resync.
1986 * Pointless because if there are any spares to skip,
1987 * then a recovery will happen and soon that array won't
1988 * be degraded any more and the spare can go back to sleep then.
1992 sync_req
= mddev
->in_sync
;
1994 /* If this is just a dirty<->clean transition, and the array is clean
1995 * and 'events' is odd, we can roll back to the previous clean state */
1997 && (mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
1998 && (mddev
->events
& 1)
1999 && mddev
->events
!= 1)
2002 /* otherwise we have to go forward and ... */
2004 if (!mddev
->in_sync
|| mddev
->recovery_cp
!= MaxSector
) { /* not clean */
2005 /* .. if the array isn't clean, an 'even' event must also go
2007 if ((mddev
->events
&1)==0)
2010 /* otherwise an 'odd' event must go to spares */
2011 if ((mddev
->events
&1))
2016 if (!mddev
->events
) {
2018 * oops, this 64-bit counter should never wrap.
2019 * Either we are in around ~1 trillion A.C., assuming
2020 * 1 reboot per second, or we have a bug:
2027 * do not write anything to disk if using
2028 * nonpersistent superblocks
2030 if (!mddev
->persistent
) {
2031 if (!mddev
->external
)
2032 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
2034 spin_unlock_irq(&mddev
->write_lock
);
2035 wake_up(&mddev
->sb_wait
);
2038 sync_sbs(mddev
, nospares
);
2039 spin_unlock_irq(&mddev
->write_lock
);
2042 "md: updating %s RAID superblock on device (in sync %d)\n",
2043 mdname(mddev
),mddev
->in_sync
);
2045 bitmap_update_sb(mddev
->bitmap
);
2046 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2047 char b
[BDEVNAME_SIZE
];
2048 dprintk(KERN_INFO
"md: ");
2049 if (rdev
->sb_loaded
!= 1)
2050 continue; /* no noise on spare devices */
2051 if (test_bit(Faulty
, &rdev
->flags
))
2052 dprintk("(skipping faulty ");
2054 dprintk("%s ", bdevname(rdev
->bdev
,b
));
2055 if (!test_bit(Faulty
, &rdev
->flags
)) {
2056 md_super_write(mddev
,rdev
,
2057 rdev
->sb_start
, rdev
->sb_size
,
2059 dprintk(KERN_INFO
"(write) %s's sb offset: %llu\n",
2060 bdevname(rdev
->bdev
,b
),
2061 (unsigned long long)rdev
->sb_start
);
2062 rdev
->sb_events
= mddev
->events
;
2066 if (mddev
->level
== LEVEL_MULTIPATH
)
2067 /* only need to write one superblock... */
2070 md_super_wait(mddev
);
2071 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
2073 spin_lock_irq(&mddev
->write_lock
);
2074 if (mddev
->in_sync
!= sync_req
||
2075 test_bit(MD_CHANGE_DEVS
, &mddev
->flags
)) {
2076 /* have to write it out again */
2077 spin_unlock_irq(&mddev
->write_lock
);
2080 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
2081 spin_unlock_irq(&mddev
->write_lock
);
2082 wake_up(&mddev
->sb_wait
);
2083 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
2084 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
2088 /* words written to sysfs files may, or may not, be \n terminated.
2089 * We want to accept with case. For this we use cmd_match.
2091 static int cmd_match(const char *cmd
, const char *str
)
2093 /* See if cmd, written into a sysfs file, matches
2094 * str. They must either be the same, or cmd can
2095 * have a trailing newline
2097 while (*cmd
&& *str
&& *cmd
== *str
) {
2108 struct rdev_sysfs_entry
{
2109 struct attribute attr
;
2110 ssize_t (*show
)(mdk_rdev_t
*, char *);
2111 ssize_t (*store
)(mdk_rdev_t
*, const char *, size_t);
2115 state_show(mdk_rdev_t
*rdev
, char *page
)
2120 if (test_bit(Faulty
, &rdev
->flags
)) {
2121 len
+= sprintf(page
+len
, "%sfaulty",sep
);
2124 if (test_bit(In_sync
, &rdev
->flags
)) {
2125 len
+= sprintf(page
+len
, "%sin_sync",sep
);
2128 if (test_bit(WriteMostly
, &rdev
->flags
)) {
2129 len
+= sprintf(page
+len
, "%swrite_mostly",sep
);
2132 if (test_bit(Blocked
, &rdev
->flags
)) {
2133 len
+= sprintf(page
+len
, "%sblocked", sep
);
2136 if (!test_bit(Faulty
, &rdev
->flags
) &&
2137 !test_bit(In_sync
, &rdev
->flags
)) {
2138 len
+= sprintf(page
+len
, "%sspare", sep
);
2141 return len
+sprintf(page
+len
, "\n");
2145 state_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2148 * faulty - simulates and error
2149 * remove - disconnects the device
2150 * writemostly - sets write_mostly
2151 * -writemostly - clears write_mostly
2152 * blocked - sets the Blocked flag
2153 * -blocked - clears the Blocked flag
2154 * insync - sets Insync providing device isn't active
2157 if (cmd_match(buf
, "faulty") && rdev
->mddev
->pers
) {
2158 md_error(rdev
->mddev
, rdev
);
2160 } else if (cmd_match(buf
, "remove")) {
2161 if (rdev
->raid_disk
>= 0)
2164 mddev_t
*mddev
= rdev
->mddev
;
2165 kick_rdev_from_array(rdev
);
2167 md_update_sb(mddev
, 1);
2168 md_new_event(mddev
);
2171 } else if (cmd_match(buf
, "writemostly")) {
2172 set_bit(WriteMostly
, &rdev
->flags
);
2174 } else if (cmd_match(buf
, "-writemostly")) {
2175 clear_bit(WriteMostly
, &rdev
->flags
);
2177 } else if (cmd_match(buf
, "blocked")) {
2178 set_bit(Blocked
, &rdev
->flags
);
2180 } else if (cmd_match(buf
, "-blocked")) {
2181 clear_bit(Blocked
, &rdev
->flags
);
2182 wake_up(&rdev
->blocked_wait
);
2183 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2184 md_wakeup_thread(rdev
->mddev
->thread
);
2187 } else if (cmd_match(buf
, "insync") && rdev
->raid_disk
== -1) {
2188 set_bit(In_sync
, &rdev
->flags
);
2191 if (!err
&& rdev
->sysfs_state
)
2192 sysfs_notify_dirent(rdev
->sysfs_state
);
2193 return err
? err
: len
;
2195 static struct rdev_sysfs_entry rdev_state
=
2196 __ATTR(state
, S_IRUGO
|S_IWUSR
, state_show
, state_store
);
2199 errors_show(mdk_rdev_t
*rdev
, char *page
)
2201 return sprintf(page
, "%d\n", atomic_read(&rdev
->corrected_errors
));
2205 errors_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2208 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2209 if (*buf
&& (*e
== 0 || *e
== '\n')) {
2210 atomic_set(&rdev
->corrected_errors
, n
);
2215 static struct rdev_sysfs_entry rdev_errors
=
2216 __ATTR(errors
, S_IRUGO
|S_IWUSR
, errors_show
, errors_store
);
2219 slot_show(mdk_rdev_t
*rdev
, char *page
)
2221 if (rdev
->raid_disk
< 0)
2222 return sprintf(page
, "none\n");
2224 return sprintf(page
, "%d\n", rdev
->raid_disk
);
2228 slot_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2233 int slot
= simple_strtoul(buf
, &e
, 10);
2234 if (strncmp(buf
, "none", 4)==0)
2236 else if (e
==buf
|| (*e
&& *e
!= '\n'))
2238 if (rdev
->mddev
->pers
&& slot
== -1) {
2239 /* Setting 'slot' on an active array requires also
2240 * updating the 'rd%d' link, and communicating
2241 * with the personality with ->hot_*_disk.
2242 * For now we only support removing
2243 * failed/spare devices. This normally happens automatically,
2244 * but not when the metadata is externally managed.
2246 if (rdev
->raid_disk
== -1)
2248 /* personality does all needed checks */
2249 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
2251 err
= rdev
->mddev
->pers
->
2252 hot_remove_disk(rdev
->mddev
, rdev
->raid_disk
);
2255 sprintf(nm
, "rd%d", rdev
->raid_disk
);
2256 sysfs_remove_link(&rdev
->mddev
->kobj
, nm
);
2257 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2258 md_wakeup_thread(rdev
->mddev
->thread
);
2259 } else if (rdev
->mddev
->pers
) {
2261 /* Activating a spare .. or possibly reactivating
2262 * if we ever get bitmaps working here.
2265 if (rdev
->raid_disk
!= -1)
2268 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
2271 list_for_each_entry(rdev2
, &rdev
->mddev
->disks
, same_set
)
2272 if (rdev2
->raid_disk
== slot
)
2275 rdev
->raid_disk
= slot
;
2276 if (test_bit(In_sync
, &rdev
->flags
))
2277 rdev
->saved_raid_disk
= slot
;
2279 rdev
->saved_raid_disk
= -1;
2280 err
= rdev
->mddev
->pers
->
2281 hot_add_disk(rdev
->mddev
, rdev
);
2283 rdev
->raid_disk
= -1;
2286 sysfs_notify_dirent(rdev
->sysfs_state
);
2287 sprintf(nm
, "rd%d", rdev
->raid_disk
);
2288 if (sysfs_create_link(&rdev
->mddev
->kobj
, &rdev
->kobj
, nm
))
2290 "md: cannot register "
2292 nm
, mdname(rdev
->mddev
));
2294 /* don't wakeup anyone, leave that to userspace. */
2296 if (slot
>= rdev
->mddev
->raid_disks
)
2298 rdev
->raid_disk
= slot
;
2299 /* assume it is working */
2300 clear_bit(Faulty
, &rdev
->flags
);
2301 clear_bit(WriteMostly
, &rdev
->flags
);
2302 set_bit(In_sync
, &rdev
->flags
);
2303 sysfs_notify_dirent(rdev
->sysfs_state
);
2309 static struct rdev_sysfs_entry rdev_slot
=
2310 __ATTR(slot
, S_IRUGO
|S_IWUSR
, slot_show
, slot_store
);
2313 offset_show(mdk_rdev_t
*rdev
, char *page
)
2315 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->data_offset
);
2319 offset_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2322 unsigned long long offset
= simple_strtoull(buf
, &e
, 10);
2323 if (e
==buf
|| (*e
&& *e
!= '\n'))
2325 if (rdev
->mddev
->pers
&& rdev
->raid_disk
>= 0)
2327 if (rdev
->sectors
&& rdev
->mddev
->external
)
2328 /* Must set offset before size, so overlap checks
2331 rdev
->data_offset
= offset
;
2335 static struct rdev_sysfs_entry rdev_offset
=
2336 __ATTR(offset
, S_IRUGO
|S_IWUSR
, offset_show
, offset_store
);
2339 rdev_size_show(mdk_rdev_t
*rdev
, char *page
)
2341 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->sectors
/ 2);
2344 static int overlaps(sector_t s1
, sector_t l1
, sector_t s2
, sector_t l2
)
2346 /* check if two start/length pairs overlap */
2354 static int strict_blocks_to_sectors(const char *buf
, sector_t
*sectors
)
2356 unsigned long long blocks
;
2359 if (strict_strtoull(buf
, 10, &blocks
) < 0)
2362 if (blocks
& 1ULL << (8 * sizeof(blocks
) - 1))
2363 return -EINVAL
; /* sector conversion overflow */
2366 if (new != blocks
* 2)
2367 return -EINVAL
; /* unsigned long long to sector_t overflow */
2374 rdev_size_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2376 mddev_t
*my_mddev
= rdev
->mddev
;
2377 sector_t oldsectors
= rdev
->sectors
;
2380 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
2382 if (my_mddev
->pers
&& rdev
->raid_disk
>= 0) {
2383 if (my_mddev
->persistent
) {
2384 sectors
= super_types
[my_mddev
->major_version
].
2385 rdev_size_change(rdev
, sectors
);
2388 } else if (!sectors
)
2389 sectors
= (rdev
->bdev
->bd_inode
->i_size
>> 9) -
2392 if (sectors
< my_mddev
->dev_sectors
)
2393 return -EINVAL
; /* component must fit device */
2395 rdev
->sectors
= sectors
;
2396 if (sectors
> oldsectors
&& my_mddev
->external
) {
2397 /* need to check that all other rdevs with the same ->bdev
2398 * do not overlap. We need to unlock the mddev to avoid
2399 * a deadlock. We have already changed rdev->sectors, and if
2400 * we have to change it back, we will have the lock again.
2404 struct list_head
*tmp
;
2406 mddev_unlock(my_mddev
);
2407 for_each_mddev(mddev
, tmp
) {
2411 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
)
2412 if (test_bit(AllReserved
, &rdev2
->flags
) ||
2413 (rdev
->bdev
== rdev2
->bdev
&&
2415 overlaps(rdev
->data_offset
, rdev
->sectors
,
2421 mddev_unlock(mddev
);
2427 mddev_lock(my_mddev
);
2429 /* Someone else could have slipped in a size
2430 * change here, but doing so is just silly.
2431 * We put oldsectors back because we *know* it is
2432 * safe, and trust userspace not to race with
2435 rdev
->sectors
= oldsectors
;
2442 static struct rdev_sysfs_entry rdev_size
=
2443 __ATTR(size
, S_IRUGO
|S_IWUSR
, rdev_size_show
, rdev_size_store
);
2445 static struct attribute
*rdev_default_attrs
[] = {
2454 rdev_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
2456 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
2457 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
2458 mddev_t
*mddev
= rdev
->mddev
;
2464 rv
= mddev
? mddev_lock(mddev
) : -EBUSY
;
2466 if (rdev
->mddev
== NULL
)
2469 rv
= entry
->show(rdev
, page
);
2470 mddev_unlock(mddev
);
2476 rdev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
2477 const char *page
, size_t length
)
2479 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
2480 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
2482 mddev_t
*mddev
= rdev
->mddev
;
2486 if (!capable(CAP_SYS_ADMIN
))
2488 rv
= mddev
? mddev_lock(mddev
): -EBUSY
;
2490 if (rdev
->mddev
== NULL
)
2493 rv
= entry
->store(rdev
, page
, length
);
2494 mddev_unlock(mddev
);
2499 static void rdev_free(struct kobject
*ko
)
2501 mdk_rdev_t
*rdev
= container_of(ko
, mdk_rdev_t
, kobj
);
2504 static struct sysfs_ops rdev_sysfs_ops
= {
2505 .show
= rdev_attr_show
,
2506 .store
= rdev_attr_store
,
2508 static struct kobj_type rdev_ktype
= {
2509 .release
= rdev_free
,
2510 .sysfs_ops
= &rdev_sysfs_ops
,
2511 .default_attrs
= rdev_default_attrs
,
2515 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2517 * mark the device faulty if:
2519 * - the device is nonexistent (zero size)
2520 * - the device has no valid superblock
2522 * a faulty rdev _never_ has rdev->sb set.
2524 static mdk_rdev_t
*md_import_device(dev_t newdev
, int super_format
, int super_minor
)
2526 char b
[BDEVNAME_SIZE
];
2531 rdev
= kzalloc(sizeof(*rdev
), GFP_KERNEL
);
2533 printk(KERN_ERR
"md: could not alloc mem for new device!\n");
2534 return ERR_PTR(-ENOMEM
);
2537 if ((err
= alloc_disk_sb(rdev
)))
2540 err
= lock_rdev(rdev
, newdev
, super_format
== -2);
2544 kobject_init(&rdev
->kobj
, &rdev_ktype
);
2547 rdev
->saved_raid_disk
= -1;
2548 rdev
->raid_disk
= -1;
2550 rdev
->data_offset
= 0;
2551 rdev
->sb_events
= 0;
2552 atomic_set(&rdev
->nr_pending
, 0);
2553 atomic_set(&rdev
->read_errors
, 0);
2554 atomic_set(&rdev
->corrected_errors
, 0);
2556 size
= rdev
->bdev
->bd_inode
->i_size
>> BLOCK_SIZE_BITS
;
2559 "md: %s has zero or unknown size, marking faulty!\n",
2560 bdevname(rdev
->bdev
,b
));
2565 if (super_format
>= 0) {
2566 err
= super_types
[super_format
].
2567 load_super(rdev
, NULL
, super_minor
);
2568 if (err
== -EINVAL
) {
2570 "md: %s does not have a valid v%d.%d "
2571 "superblock, not importing!\n",
2572 bdevname(rdev
->bdev
,b
),
2573 super_format
, super_minor
);
2578 "md: could not read %s's sb, not importing!\n",
2579 bdevname(rdev
->bdev
,b
));
2584 INIT_LIST_HEAD(&rdev
->same_set
);
2585 init_waitqueue_head(&rdev
->blocked_wait
);
2590 if (rdev
->sb_page
) {
2596 return ERR_PTR(err
);
2600 * Check a full RAID array for plausibility
2604 static void analyze_sbs(mddev_t
* mddev
)
2607 mdk_rdev_t
*rdev
, *freshest
, *tmp
;
2608 char b
[BDEVNAME_SIZE
];
2611 rdev_for_each(rdev
, tmp
, mddev
)
2612 switch (super_types
[mddev
->major_version
].
2613 load_super(rdev
, freshest
, mddev
->minor_version
)) {
2621 "md: fatal superblock inconsistency in %s"
2622 " -- removing from array\n",
2623 bdevname(rdev
->bdev
,b
));
2624 kick_rdev_from_array(rdev
);
2628 super_types
[mddev
->major_version
].
2629 validate_super(mddev
, freshest
);
2632 rdev_for_each(rdev
, tmp
, mddev
) {
2633 if (rdev
->desc_nr
>= mddev
->max_disks
||
2634 i
> mddev
->max_disks
) {
2636 "md: %s: %s: only %d devices permitted\n",
2637 mdname(mddev
), bdevname(rdev
->bdev
, b
),
2639 kick_rdev_from_array(rdev
);
2642 if (rdev
!= freshest
)
2643 if (super_types
[mddev
->major_version
].
2644 validate_super(mddev
, rdev
)) {
2645 printk(KERN_WARNING
"md: kicking non-fresh %s"
2647 bdevname(rdev
->bdev
,b
));
2648 kick_rdev_from_array(rdev
);
2651 if (mddev
->level
== LEVEL_MULTIPATH
) {
2652 rdev
->desc_nr
= i
++;
2653 rdev
->raid_disk
= rdev
->desc_nr
;
2654 set_bit(In_sync
, &rdev
->flags
);
2655 } else if (rdev
->raid_disk
>= (mddev
->raid_disks
- min(0, mddev
->delta_disks
))) {
2656 rdev
->raid_disk
= -1;
2657 clear_bit(In_sync
, &rdev
->flags
);
2662 static void md_safemode_timeout(unsigned long data
);
2665 safe_delay_show(mddev_t
*mddev
, char *page
)
2667 int msec
= (mddev
->safemode_delay
*1000)/HZ
;
2668 return sprintf(page
, "%d.%03d\n", msec
/1000, msec
%1000);
2671 safe_delay_store(mddev_t
*mddev
, const char *cbuf
, size_t len
)
2679 /* remove a period, and count digits after it */
2680 if (len
>= sizeof(buf
))
2682 strlcpy(buf
, cbuf
, sizeof(buf
));
2683 for (i
=0; i
<len
; i
++) {
2685 if (isdigit(buf
[i
])) {
2690 } else if (buf
[i
] == '.') {
2695 if (strict_strtoul(buf
, 10, &msec
) < 0)
2697 msec
= (msec
* 1000) / scale
;
2699 mddev
->safemode_delay
= 0;
2701 unsigned long old_delay
= mddev
->safemode_delay
;
2702 mddev
->safemode_delay
= (msec
*HZ
)/1000;
2703 if (mddev
->safemode_delay
== 0)
2704 mddev
->safemode_delay
= 1;
2705 if (mddev
->safemode_delay
< old_delay
)
2706 md_safemode_timeout((unsigned long)mddev
);
2710 static struct md_sysfs_entry md_safe_delay
=
2711 __ATTR(safe_mode_delay
, S_IRUGO
|S_IWUSR
,safe_delay_show
, safe_delay_store
);
2714 level_show(mddev_t
*mddev
, char *page
)
2716 struct mdk_personality
*p
= mddev
->pers
;
2718 return sprintf(page
, "%s\n", p
->name
);
2719 else if (mddev
->clevel
[0])
2720 return sprintf(page
, "%s\n", mddev
->clevel
);
2721 else if (mddev
->level
!= LEVEL_NONE
)
2722 return sprintf(page
, "%d\n", mddev
->level
);
2728 level_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2732 struct mdk_personality
*pers
;
2736 if (mddev
->pers
== NULL
) {
2739 if (len
>= sizeof(mddev
->clevel
))
2741 strncpy(mddev
->clevel
, buf
, len
);
2742 if (mddev
->clevel
[len
-1] == '\n')
2744 mddev
->clevel
[len
] = 0;
2745 mddev
->level
= LEVEL_NONE
;
2749 /* request to change the personality. Need to ensure:
2750 * - array is not engaged in resync/recovery/reshape
2751 * - old personality can be suspended
2752 * - new personality will access other array.
2755 if (mddev
->sync_thread
|| mddev
->reshape_position
!= MaxSector
)
2758 if (!mddev
->pers
->quiesce
) {
2759 printk(KERN_WARNING
"md: %s: %s does not support online personality change\n",
2760 mdname(mddev
), mddev
->pers
->name
);
2764 /* Now find the new personality */
2765 if (len
== 0 || len
>= sizeof(level
))
2767 strncpy(level
, buf
, len
);
2768 if (level
[len
-1] == '\n')
2772 request_module("md-%s", level
);
2773 spin_lock(&pers_lock
);
2774 pers
= find_pers(LEVEL_NONE
, level
);
2775 if (!pers
|| !try_module_get(pers
->owner
)) {
2776 spin_unlock(&pers_lock
);
2777 printk(KERN_WARNING
"md: personality %s not loaded\n", level
);
2780 spin_unlock(&pers_lock
);
2782 if (pers
== mddev
->pers
) {
2783 /* Nothing to do! */
2784 module_put(pers
->owner
);
2787 if (!pers
->takeover
) {
2788 module_put(pers
->owner
);
2789 printk(KERN_WARNING
"md: %s: %s does not support personality takeover\n",
2790 mdname(mddev
), level
);
2794 /* ->takeover must set new_* and/or delta_disks
2795 * if it succeeds, and may set them when it fails.
2797 priv
= pers
->takeover(mddev
);
2799 mddev
->new_level
= mddev
->level
;
2800 mddev
->new_layout
= mddev
->layout
;
2801 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
2802 mddev
->raid_disks
-= mddev
->delta_disks
;
2803 mddev
->delta_disks
= 0;
2804 module_put(pers
->owner
);
2805 printk(KERN_WARNING
"md: %s: %s would not accept array\n",
2806 mdname(mddev
), level
);
2807 return PTR_ERR(priv
);
2810 /* Looks like we have a winner */
2811 mddev_suspend(mddev
);
2812 mddev
->pers
->stop(mddev
);
2813 module_put(mddev
->pers
->owner
);
2814 /* Invalidate devices that are now superfluous */
2815 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
2816 if (rdev
->raid_disk
>= mddev
->raid_disks
) {
2817 rdev
->raid_disk
= -1;
2818 clear_bit(In_sync
, &rdev
->flags
);
2821 mddev
->private = priv
;
2822 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
2823 mddev
->level
= mddev
->new_level
;
2824 mddev
->layout
= mddev
->new_layout
;
2825 mddev
->chunk_sectors
= mddev
->new_chunk_sectors
;
2826 mddev
->delta_disks
= 0;
2828 mddev_resume(mddev
);
2829 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
2830 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
2831 md_wakeup_thread(mddev
->thread
);
2835 static struct md_sysfs_entry md_level
=
2836 __ATTR(level
, S_IRUGO
|S_IWUSR
, level_show
, level_store
);
2840 layout_show(mddev_t
*mddev
, char *page
)
2842 /* just a number, not meaningful for all levels */
2843 if (mddev
->reshape_position
!= MaxSector
&&
2844 mddev
->layout
!= mddev
->new_layout
)
2845 return sprintf(page
, "%d (%d)\n",
2846 mddev
->new_layout
, mddev
->layout
);
2847 return sprintf(page
, "%d\n", mddev
->layout
);
2851 layout_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2854 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2856 if (!*buf
|| (*e
&& *e
!= '\n'))
2861 if (mddev
->pers
->check_reshape
== NULL
)
2863 mddev
->new_layout
= n
;
2864 err
= mddev
->pers
->check_reshape(mddev
);
2866 mddev
->new_layout
= mddev
->layout
;
2870 mddev
->new_layout
= n
;
2871 if (mddev
->reshape_position
== MaxSector
)
2876 static struct md_sysfs_entry md_layout
=
2877 __ATTR(layout
, S_IRUGO
|S_IWUSR
, layout_show
, layout_store
);
2881 raid_disks_show(mddev_t
*mddev
, char *page
)
2883 if (mddev
->raid_disks
== 0)
2885 if (mddev
->reshape_position
!= MaxSector
&&
2886 mddev
->delta_disks
!= 0)
2887 return sprintf(page
, "%d (%d)\n", mddev
->raid_disks
,
2888 mddev
->raid_disks
- mddev
->delta_disks
);
2889 return sprintf(page
, "%d\n", mddev
->raid_disks
);
2892 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
);
2895 raid_disks_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2899 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2901 if (!*buf
|| (*e
&& *e
!= '\n'))
2905 rv
= update_raid_disks(mddev
, n
);
2906 else if (mddev
->reshape_position
!= MaxSector
) {
2907 int olddisks
= mddev
->raid_disks
- mddev
->delta_disks
;
2908 mddev
->delta_disks
= n
- olddisks
;
2909 mddev
->raid_disks
= n
;
2911 mddev
->raid_disks
= n
;
2912 return rv
? rv
: len
;
2914 static struct md_sysfs_entry md_raid_disks
=
2915 __ATTR(raid_disks
, S_IRUGO
|S_IWUSR
, raid_disks_show
, raid_disks_store
);
2918 chunk_size_show(mddev_t
*mddev
, char *page
)
2920 if (mddev
->reshape_position
!= MaxSector
&&
2921 mddev
->chunk_sectors
!= mddev
->new_chunk_sectors
)
2922 return sprintf(page
, "%d (%d)\n",
2923 mddev
->new_chunk_sectors
<< 9,
2924 mddev
->chunk_sectors
<< 9);
2925 return sprintf(page
, "%d\n", mddev
->chunk_sectors
<< 9);
2929 chunk_size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2932 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2934 if (!*buf
|| (*e
&& *e
!= '\n'))
2939 if (mddev
->pers
->check_reshape
== NULL
)
2941 mddev
->new_chunk_sectors
= n
>> 9;
2942 err
= mddev
->pers
->check_reshape(mddev
);
2944 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
2948 mddev
->new_chunk_sectors
= n
>> 9;
2949 if (mddev
->reshape_position
== MaxSector
)
2950 mddev
->chunk_sectors
= n
>> 9;
2954 static struct md_sysfs_entry md_chunk_size
=
2955 __ATTR(chunk_size
, S_IRUGO
|S_IWUSR
, chunk_size_show
, chunk_size_store
);
2958 resync_start_show(mddev_t
*mddev
, char *page
)
2960 if (mddev
->recovery_cp
== MaxSector
)
2961 return sprintf(page
, "none\n");
2962 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->recovery_cp
);
2966 resync_start_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2969 unsigned long long n
= simple_strtoull(buf
, &e
, 10);
2973 if (!*buf
|| (*e
&& *e
!= '\n'))
2976 mddev
->recovery_cp
= n
;
2979 static struct md_sysfs_entry md_resync_start
=
2980 __ATTR(resync_start
, S_IRUGO
|S_IWUSR
, resync_start_show
, resync_start_store
);
2983 * The array state can be:
2986 * No devices, no size, no level
2987 * Equivalent to STOP_ARRAY ioctl
2989 * May have some settings, but array is not active
2990 * all IO results in error
2991 * When written, doesn't tear down array, but just stops it
2992 * suspended (not supported yet)
2993 * All IO requests will block. The array can be reconfigured.
2994 * Writing this, if accepted, will block until array is quiescent
2996 * no resync can happen. no superblocks get written.
2997 * write requests fail
2999 * like readonly, but behaves like 'clean' on a write request.
3001 * clean - no pending writes, but otherwise active.
3002 * When written to inactive array, starts without resync
3003 * If a write request arrives then
3004 * if metadata is known, mark 'dirty' and switch to 'active'.
3005 * if not known, block and switch to write-pending
3006 * If written to an active array that has pending writes, then fails.
3008 * fully active: IO and resync can be happening.
3009 * When written to inactive array, starts with resync
3012 * clean, but writes are blocked waiting for 'active' to be written.
3015 * like active, but no writes have been seen for a while (100msec).
3018 enum array_state
{ clear
, inactive
, suspended
, readonly
, read_auto
, clean
, active
,
3019 write_pending
, active_idle
, bad_word
};
3020 static char *array_states
[] = {
3021 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3022 "write-pending", "active-idle", NULL
};
3024 static int match_word(const char *word
, char **list
)
3027 for (n
=0; list
[n
]; n
++)
3028 if (cmd_match(word
, list
[n
]))
3034 array_state_show(mddev_t
*mddev
, char *page
)
3036 enum array_state st
= inactive
;
3049 else if (test_bit(MD_CHANGE_CLEAN
, &mddev
->flags
))
3051 else if (mddev
->safemode
)
3057 if (list_empty(&mddev
->disks
) &&
3058 mddev
->raid_disks
== 0 &&
3059 mddev
->dev_sectors
== 0)
3064 return sprintf(page
, "%s\n", array_states
[st
]);
3067 static int do_md_stop(mddev_t
* mddev
, int ro
, int is_open
);
3068 static int do_md_run(mddev_t
* mddev
);
3069 static int restart_array(mddev_t
*mddev
);
3072 array_state_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3075 enum array_state st
= match_word(buf
, array_states
);
3080 /* stopping an active array */
3081 if (atomic_read(&mddev
->openers
) > 0)
3083 err
= do_md_stop(mddev
, 0, 0);
3086 /* stopping an active array */
3088 if (atomic_read(&mddev
->openers
) > 0)
3090 err
= do_md_stop(mddev
, 2, 0);
3092 err
= 0; /* already inactive */
3095 break; /* not supported yet */
3098 err
= do_md_stop(mddev
, 1, 0);
3101 set_disk_ro(mddev
->gendisk
, 1);
3102 err
= do_md_run(mddev
);
3108 err
= do_md_stop(mddev
, 1, 0);
3109 else if (mddev
->ro
== 1)
3110 err
= restart_array(mddev
);
3113 set_disk_ro(mddev
->gendisk
, 0);
3117 err
= do_md_run(mddev
);
3122 restart_array(mddev
);
3123 spin_lock_irq(&mddev
->write_lock
);
3124 if (atomic_read(&mddev
->writes_pending
) == 0) {
3125 if (mddev
->in_sync
== 0) {
3127 if (mddev
->safemode
== 1)
3128 mddev
->safemode
= 0;
3129 if (mddev
->persistent
)
3130 set_bit(MD_CHANGE_CLEAN
,
3136 spin_unlock_irq(&mddev
->write_lock
);
3142 restart_array(mddev
);
3143 if (mddev
->external
)
3144 clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
3145 wake_up(&mddev
->sb_wait
);
3149 set_disk_ro(mddev
->gendisk
, 0);
3150 err
= do_md_run(mddev
);
3155 /* these cannot be set */
3161 sysfs_notify_dirent(mddev
->sysfs_state
);
3165 static struct md_sysfs_entry md_array_state
=
3166 __ATTR(array_state
, S_IRUGO
|S_IWUSR
, array_state_show
, array_state_store
);
3169 null_show(mddev_t
*mddev
, char *page
)
3175 new_dev_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3177 /* buf must be %d:%d\n? giving major and minor numbers */
3178 /* The new device is added to the array.
3179 * If the array has a persistent superblock, we read the
3180 * superblock to initialise info and check validity.
3181 * Otherwise, only checking done is that in bind_rdev_to_array,
3182 * which mainly checks size.
3185 int major
= simple_strtoul(buf
, &e
, 10);
3191 if (!*buf
|| *e
!= ':' || !e
[1] || e
[1] == '\n')
3193 minor
= simple_strtoul(e
+1, &e
, 10);
3194 if (*e
&& *e
!= '\n')
3196 dev
= MKDEV(major
, minor
);
3197 if (major
!= MAJOR(dev
) ||
3198 minor
!= MINOR(dev
))
3202 if (mddev
->persistent
) {
3203 rdev
= md_import_device(dev
, mddev
->major_version
,
3204 mddev
->minor_version
);
3205 if (!IS_ERR(rdev
) && !list_empty(&mddev
->disks
)) {
3206 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
3207 mdk_rdev_t
, same_set
);
3208 err
= super_types
[mddev
->major_version
]
3209 .load_super(rdev
, rdev0
, mddev
->minor_version
);
3213 } else if (mddev
->external
)
3214 rdev
= md_import_device(dev
, -2, -1);
3216 rdev
= md_import_device(dev
, -1, -1);
3219 return PTR_ERR(rdev
);
3220 err
= bind_rdev_to_array(rdev
, mddev
);
3224 return err
? err
: len
;
3227 static struct md_sysfs_entry md_new_device
=
3228 __ATTR(new_dev
, S_IWUSR
, null_show
, new_dev_store
);
3231 bitmap_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3234 unsigned long chunk
, end_chunk
;
3238 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3240 chunk
= end_chunk
= simple_strtoul(buf
, &end
, 0);
3241 if (buf
== end
) break;
3242 if (*end
== '-') { /* range */
3244 end_chunk
= simple_strtoul(buf
, &end
, 0);
3245 if (buf
== end
) break;
3247 if (*end
&& !isspace(*end
)) break;
3248 bitmap_dirty_bits(mddev
->bitmap
, chunk
, end_chunk
);
3250 while (isspace(*buf
)) buf
++;
3252 bitmap_unplug(mddev
->bitmap
); /* flush the bits to disk */
3257 static struct md_sysfs_entry md_bitmap
=
3258 __ATTR(bitmap_set_bits
, S_IWUSR
, null_show
, bitmap_store
);
3261 size_show(mddev_t
*mddev
, char *page
)
3263 return sprintf(page
, "%llu\n",
3264 (unsigned long long)mddev
->dev_sectors
/ 2);
3267 static int update_size(mddev_t
*mddev
, sector_t num_sectors
);
3270 size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3272 /* If array is inactive, we can reduce the component size, but
3273 * not increase it (except from 0).
3274 * If array is active, we can try an on-line resize
3277 int err
= strict_blocks_to_sectors(buf
, §ors
);
3282 err
= update_size(mddev
, sectors
);
3283 md_update_sb(mddev
, 1);
3285 if (mddev
->dev_sectors
== 0 ||
3286 mddev
->dev_sectors
> sectors
)
3287 mddev
->dev_sectors
= sectors
;
3291 return err
? err
: len
;
3294 static struct md_sysfs_entry md_size
=
3295 __ATTR(component_size
, S_IRUGO
|S_IWUSR
, size_show
, size_store
);
3300 * 'none' for arrays with no metadata (good luck...)
3301 * 'external' for arrays with externally managed metadata,
3302 * or N.M for internally known formats
3305 metadata_show(mddev_t
*mddev
, char *page
)
3307 if (mddev
->persistent
)
3308 return sprintf(page
, "%d.%d\n",
3309 mddev
->major_version
, mddev
->minor_version
);
3310 else if (mddev
->external
)
3311 return sprintf(page
, "external:%s\n", mddev
->metadata_type
);
3313 return sprintf(page
, "none\n");
3317 metadata_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3321 /* Changing the details of 'external' metadata is
3322 * always permitted. Otherwise there must be
3323 * no devices attached to the array.
3325 if (mddev
->external
&& strncmp(buf
, "external:", 9) == 0)
3327 else if (!list_empty(&mddev
->disks
))
3330 if (cmd_match(buf
, "none")) {
3331 mddev
->persistent
= 0;
3332 mddev
->external
= 0;
3333 mddev
->major_version
= 0;
3334 mddev
->minor_version
= 90;
3337 if (strncmp(buf
, "external:", 9) == 0) {
3338 size_t namelen
= len
-9;
3339 if (namelen
>= sizeof(mddev
->metadata_type
))
3340 namelen
= sizeof(mddev
->metadata_type
)-1;
3341 strncpy(mddev
->metadata_type
, buf
+9, namelen
);
3342 mddev
->metadata_type
[namelen
] = 0;
3343 if (namelen
&& mddev
->metadata_type
[namelen
-1] == '\n')
3344 mddev
->metadata_type
[--namelen
] = 0;
3345 mddev
->persistent
= 0;
3346 mddev
->external
= 1;
3347 mddev
->major_version
= 0;
3348 mddev
->minor_version
= 90;
3351 major
= simple_strtoul(buf
, &e
, 10);
3352 if (e
==buf
|| *e
!= '.')
3355 minor
= simple_strtoul(buf
, &e
, 10);
3356 if (e
==buf
|| (*e
&& *e
!= '\n') )
3358 if (major
>= ARRAY_SIZE(super_types
) || super_types
[major
].name
== NULL
)
3360 mddev
->major_version
= major
;
3361 mddev
->minor_version
= minor
;
3362 mddev
->persistent
= 1;
3363 mddev
->external
= 0;
3367 static struct md_sysfs_entry md_metadata
=
3368 __ATTR(metadata_version
, S_IRUGO
|S_IWUSR
, metadata_show
, metadata_store
);
3371 action_show(mddev_t
*mddev
, char *page
)
3373 char *type
= "idle";
3374 if (test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
3376 else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
3377 (!mddev
->ro
&& test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))) {
3378 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
3380 else if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
3381 if (!test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
3383 else if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
3387 } else if (test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
))
3390 return sprintf(page
, "%s\n", type
);
3394 action_store(mddev_t
*mddev
, const char *page
, size_t len
)
3396 if (!mddev
->pers
|| !mddev
->pers
->sync_request
)
3399 if (cmd_match(page
, "frozen"))
3400 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
3402 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
3404 if (cmd_match(page
, "idle") || cmd_match(page
, "frozen")) {
3405 if (mddev
->sync_thread
) {
3406 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
3407 md_unregister_thread(mddev
->sync_thread
);
3408 mddev
->sync_thread
= NULL
;
3409 mddev
->recovery
= 0;
3411 } else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
3412 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
3414 else if (cmd_match(page
, "resync"))
3415 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3416 else if (cmd_match(page
, "recover")) {
3417 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
3418 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3419 } else if (cmd_match(page
, "reshape")) {
3421 if (mddev
->pers
->start_reshape
== NULL
)
3423 err
= mddev
->pers
->start_reshape(mddev
);
3426 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
3428 if (cmd_match(page
, "check"))
3429 set_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
3430 else if (!cmd_match(page
, "repair"))
3432 set_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
3433 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
3435 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3436 md_wakeup_thread(mddev
->thread
);
3437 sysfs_notify_dirent(mddev
->sysfs_action
);
3442 mismatch_cnt_show(mddev_t
*mddev
, char *page
)
3444 return sprintf(page
, "%llu\n",
3445 (unsigned long long) mddev
->resync_mismatches
);
3448 static struct md_sysfs_entry md_scan_mode
=
3449 __ATTR(sync_action
, S_IRUGO
|S_IWUSR
, action_show
, action_store
);
3452 static struct md_sysfs_entry md_mismatches
= __ATTR_RO(mismatch_cnt
);
3455 sync_min_show(mddev_t
*mddev
, char *page
)
3457 return sprintf(page
, "%d (%s)\n", speed_min(mddev
),
3458 mddev
->sync_speed_min
? "local": "system");
3462 sync_min_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3466 if (strncmp(buf
, "system", 6)==0) {
3467 mddev
->sync_speed_min
= 0;
3470 min
= simple_strtoul(buf
, &e
, 10);
3471 if (buf
== e
|| (*e
&& *e
!= '\n') || min
<= 0)
3473 mddev
->sync_speed_min
= min
;
3477 static struct md_sysfs_entry md_sync_min
=
3478 __ATTR(sync_speed_min
, S_IRUGO
|S_IWUSR
, sync_min_show
, sync_min_store
);
3481 sync_max_show(mddev_t
*mddev
, char *page
)
3483 return sprintf(page
, "%d (%s)\n", speed_max(mddev
),
3484 mddev
->sync_speed_max
? "local": "system");
3488 sync_max_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3492 if (strncmp(buf
, "system", 6)==0) {
3493 mddev
->sync_speed_max
= 0;
3496 max
= simple_strtoul(buf
, &e
, 10);
3497 if (buf
== e
|| (*e
&& *e
!= '\n') || max
<= 0)
3499 mddev
->sync_speed_max
= max
;
3503 static struct md_sysfs_entry md_sync_max
=
3504 __ATTR(sync_speed_max
, S_IRUGO
|S_IWUSR
, sync_max_show
, sync_max_store
);
3507 degraded_show(mddev_t
*mddev
, char *page
)
3509 return sprintf(page
, "%d\n", mddev
->degraded
);
3511 static struct md_sysfs_entry md_degraded
= __ATTR_RO(degraded
);
3514 sync_force_parallel_show(mddev_t
*mddev
, char *page
)
3516 return sprintf(page
, "%d\n", mddev
->parallel_resync
);
3520 sync_force_parallel_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3524 if (strict_strtol(buf
, 10, &n
))
3527 if (n
!= 0 && n
!= 1)
3530 mddev
->parallel_resync
= n
;
3532 if (mddev
->sync_thread
)
3533 wake_up(&resync_wait
);
3538 /* force parallel resync, even with shared block devices */
3539 static struct md_sysfs_entry md_sync_force_parallel
=
3540 __ATTR(sync_force_parallel
, S_IRUGO
|S_IWUSR
,
3541 sync_force_parallel_show
, sync_force_parallel_store
);
3544 sync_speed_show(mddev_t
*mddev
, char *page
)
3546 unsigned long resync
, dt
, db
;
3547 if (mddev
->curr_resync
== 0)
3548 return sprintf(page
, "none\n");
3549 resync
= mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
);
3550 dt
= (jiffies
- mddev
->resync_mark
) / HZ
;
3552 db
= resync
- mddev
->resync_mark_cnt
;
3553 return sprintf(page
, "%lu\n", db
/dt
/2); /* K/sec */
3556 static struct md_sysfs_entry md_sync_speed
= __ATTR_RO(sync_speed
);
3559 sync_completed_show(mddev_t
*mddev
, char *page
)
3561 unsigned long max_sectors
, resync
;
3563 if (!test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3564 return sprintf(page
, "none\n");
3566 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
3567 max_sectors
= mddev
->resync_max_sectors
;
3569 max_sectors
= mddev
->dev_sectors
;
3571 resync
= mddev
->curr_resync_completed
;
3572 return sprintf(page
, "%lu / %lu\n", resync
, max_sectors
);
3575 static struct md_sysfs_entry md_sync_completed
= __ATTR_RO(sync_completed
);
3578 min_sync_show(mddev_t
*mddev
, char *page
)
3580 return sprintf(page
, "%llu\n",
3581 (unsigned long long)mddev
->resync_min
);
3584 min_sync_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3586 unsigned long long min
;
3587 if (strict_strtoull(buf
, 10, &min
))
3589 if (min
> mddev
->resync_max
)
3591 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3594 /* Must be a multiple of chunk_size */
3595 if (mddev
->chunk_sectors
) {
3596 sector_t temp
= min
;
3597 if (sector_div(temp
, mddev
->chunk_sectors
))
3600 mddev
->resync_min
= min
;
3605 static struct md_sysfs_entry md_min_sync
=
3606 __ATTR(sync_min
, S_IRUGO
|S_IWUSR
, min_sync_show
, min_sync_store
);
3609 max_sync_show(mddev_t
*mddev
, char *page
)
3611 if (mddev
->resync_max
== MaxSector
)
3612 return sprintf(page
, "max\n");
3614 return sprintf(page
, "%llu\n",
3615 (unsigned long long)mddev
->resync_max
);
3618 max_sync_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3620 if (strncmp(buf
, "max", 3) == 0)
3621 mddev
->resync_max
= MaxSector
;
3623 unsigned long long max
;
3624 if (strict_strtoull(buf
, 10, &max
))
3626 if (max
< mddev
->resync_min
)
3628 if (max
< mddev
->resync_max
&&
3630 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3633 /* Must be a multiple of chunk_size */
3634 if (mddev
->chunk_sectors
) {
3635 sector_t temp
= max
;
3636 if (sector_div(temp
, mddev
->chunk_sectors
))
3639 mddev
->resync_max
= max
;
3641 wake_up(&mddev
->recovery_wait
);
3645 static struct md_sysfs_entry md_max_sync
=
3646 __ATTR(sync_max
, S_IRUGO
|S_IWUSR
, max_sync_show
, max_sync_store
);
3649 suspend_lo_show(mddev_t
*mddev
, char *page
)
3651 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_lo
);
3655 suspend_lo_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3658 unsigned long long new = simple_strtoull(buf
, &e
, 10);
3660 if (mddev
->pers
== NULL
||
3661 mddev
->pers
->quiesce
== NULL
)
3663 if (buf
== e
|| (*e
&& *e
!= '\n'))
3665 if (new >= mddev
->suspend_hi
||
3666 (new > mddev
->suspend_lo
&& new < mddev
->suspend_hi
)) {
3667 mddev
->suspend_lo
= new;
3668 mddev
->pers
->quiesce(mddev
, 2);
3673 static struct md_sysfs_entry md_suspend_lo
=
3674 __ATTR(suspend_lo
, S_IRUGO
|S_IWUSR
, suspend_lo_show
, suspend_lo_store
);
3678 suspend_hi_show(mddev_t
*mddev
, char *page
)
3680 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_hi
);
3684 suspend_hi_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3687 unsigned long long new = simple_strtoull(buf
, &e
, 10);
3689 if (mddev
->pers
== NULL
||
3690 mddev
->pers
->quiesce
== NULL
)
3692 if (buf
== e
|| (*e
&& *e
!= '\n'))
3694 if ((new <= mddev
->suspend_lo
&& mddev
->suspend_lo
>= mddev
->suspend_hi
) ||
3695 (new > mddev
->suspend_lo
&& new > mddev
->suspend_hi
)) {
3696 mddev
->suspend_hi
= new;
3697 mddev
->pers
->quiesce(mddev
, 1);
3698 mddev
->pers
->quiesce(mddev
, 0);
3703 static struct md_sysfs_entry md_suspend_hi
=
3704 __ATTR(suspend_hi
, S_IRUGO
|S_IWUSR
, suspend_hi_show
, suspend_hi_store
);
3707 reshape_position_show(mddev_t
*mddev
, char *page
)
3709 if (mddev
->reshape_position
!= MaxSector
)
3710 return sprintf(page
, "%llu\n",
3711 (unsigned long long)mddev
->reshape_position
);
3712 strcpy(page
, "none\n");
3717 reshape_position_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3720 unsigned long long new = simple_strtoull(buf
, &e
, 10);
3723 if (buf
== e
|| (*e
&& *e
!= '\n'))
3725 mddev
->reshape_position
= new;
3726 mddev
->delta_disks
= 0;
3727 mddev
->new_level
= mddev
->level
;
3728 mddev
->new_layout
= mddev
->layout
;
3729 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3733 static struct md_sysfs_entry md_reshape_position
=
3734 __ATTR(reshape_position
, S_IRUGO
|S_IWUSR
, reshape_position_show
,
3735 reshape_position_store
);
3738 array_size_show(mddev_t
*mddev
, char *page
)
3740 if (mddev
->external_size
)
3741 return sprintf(page
, "%llu\n",
3742 (unsigned long long)mddev
->array_sectors
/2);
3744 return sprintf(page
, "default\n");
3748 array_size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3752 if (strncmp(buf
, "default", 7) == 0) {
3754 sectors
= mddev
->pers
->size(mddev
, 0, 0);
3756 sectors
= mddev
->array_sectors
;
3758 mddev
->external_size
= 0;
3760 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
3762 if (mddev
->pers
&& mddev
->pers
->size(mddev
, 0, 0) < sectors
)
3765 mddev
->external_size
= 1;
3768 mddev
->array_sectors
= sectors
;
3769 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
3771 revalidate_disk(mddev
->gendisk
);
3776 static struct md_sysfs_entry md_array_size
=
3777 __ATTR(array_size
, S_IRUGO
|S_IWUSR
, array_size_show
,
3780 static struct attribute
*md_default_attrs
[] = {
3783 &md_raid_disks
.attr
,
3784 &md_chunk_size
.attr
,
3786 &md_resync_start
.attr
,
3788 &md_new_device
.attr
,
3789 &md_safe_delay
.attr
,
3790 &md_array_state
.attr
,
3791 &md_reshape_position
.attr
,
3792 &md_array_size
.attr
,
3796 static struct attribute
*md_redundancy_attrs
[] = {
3798 &md_mismatches
.attr
,
3801 &md_sync_speed
.attr
,
3802 &md_sync_force_parallel
.attr
,
3803 &md_sync_completed
.attr
,
3806 &md_suspend_lo
.attr
,
3807 &md_suspend_hi
.attr
,
3812 static struct attribute_group md_redundancy_group
= {
3814 .attrs
= md_redundancy_attrs
,
3819 md_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
3821 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
3822 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
3827 rv
= mddev_lock(mddev
);
3829 rv
= entry
->show(mddev
, page
);
3830 mddev_unlock(mddev
);
3836 md_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
3837 const char *page
, size_t length
)
3839 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
3840 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
3845 if (!capable(CAP_SYS_ADMIN
))
3847 rv
= mddev_lock(mddev
);
3848 if (mddev
->hold_active
== UNTIL_IOCTL
)
3849 mddev
->hold_active
= 0;
3851 rv
= entry
->store(mddev
, page
, length
);
3852 mddev_unlock(mddev
);
3857 static void md_free(struct kobject
*ko
)
3859 mddev_t
*mddev
= container_of(ko
, mddev_t
, kobj
);
3861 if (mddev
->sysfs_state
)
3862 sysfs_put(mddev
->sysfs_state
);
3864 if (mddev
->gendisk
) {
3865 del_gendisk(mddev
->gendisk
);
3866 put_disk(mddev
->gendisk
);
3869 blk_cleanup_queue(mddev
->queue
);
3874 static struct sysfs_ops md_sysfs_ops
= {
3875 .show
= md_attr_show
,
3876 .store
= md_attr_store
,
3878 static struct kobj_type md_ktype
= {
3880 .sysfs_ops
= &md_sysfs_ops
,
3881 .default_attrs
= md_default_attrs
,
3886 static void mddev_delayed_delete(struct work_struct
*ws
)
3888 mddev_t
*mddev
= container_of(ws
, mddev_t
, del_work
);
3890 if (mddev
->private == &md_redundancy_group
) {
3891 sysfs_remove_group(&mddev
->kobj
, &md_redundancy_group
);
3892 if (mddev
->sysfs_action
)
3893 sysfs_put(mddev
->sysfs_action
);
3894 mddev
->sysfs_action
= NULL
;
3895 mddev
->private = NULL
;
3897 kobject_del(&mddev
->kobj
);
3898 kobject_put(&mddev
->kobj
);
3901 static int md_alloc(dev_t dev
, char *name
)
3903 static DEFINE_MUTEX(disks_mutex
);
3904 mddev_t
*mddev
= mddev_find(dev
);
3905 struct gendisk
*disk
;
3914 partitioned
= (MAJOR(mddev
->unit
) != MD_MAJOR
);
3915 shift
= partitioned
? MdpMinorShift
: 0;
3916 unit
= MINOR(mddev
->unit
) >> shift
;
3918 /* wait for any previous instance if this device
3919 * to be completed removed (mddev_delayed_delete).
3921 flush_scheduled_work();
3923 mutex_lock(&disks_mutex
);
3929 /* Need to ensure that 'name' is not a duplicate.
3932 spin_lock(&all_mddevs_lock
);
3934 list_for_each_entry(mddev2
, &all_mddevs
, all_mddevs
)
3935 if (mddev2
->gendisk
&&
3936 strcmp(mddev2
->gendisk
->disk_name
, name
) == 0) {
3937 spin_unlock(&all_mddevs_lock
);
3940 spin_unlock(&all_mddevs_lock
);
3944 mddev
->queue
= blk_alloc_queue(GFP_KERNEL
);
3947 mddev
->queue
->queuedata
= mddev
;
3949 /* Can be unlocked because the queue is new: no concurrency */
3950 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER
, mddev
->queue
);
3952 blk_queue_make_request(mddev
->queue
, md_make_request
);
3954 disk
= alloc_disk(1 << shift
);
3956 blk_cleanup_queue(mddev
->queue
);
3957 mddev
->queue
= NULL
;
3960 disk
->major
= MAJOR(mddev
->unit
);
3961 disk
->first_minor
= unit
<< shift
;
3963 strcpy(disk
->disk_name
, name
);
3964 else if (partitioned
)
3965 sprintf(disk
->disk_name
, "md_d%d", unit
);
3967 sprintf(disk
->disk_name
, "md%d", unit
);
3968 disk
->fops
= &md_fops
;
3969 disk
->private_data
= mddev
;
3970 disk
->queue
= mddev
->queue
;
3971 /* Allow extended partitions. This makes the
3972 * 'mdp' device redundant, but we can't really
3975 disk
->flags
|= GENHD_FL_EXT_DEVT
;
3977 mddev
->gendisk
= disk
;
3978 error
= kobject_init_and_add(&mddev
->kobj
, &md_ktype
,
3979 &disk_to_dev(disk
)->kobj
, "%s", "md");
3981 /* This isn't possible, but as kobject_init_and_add is marked
3982 * __must_check, we must do something with the result
3984 printk(KERN_WARNING
"md: cannot register %s/md - name in use\n",
3989 mutex_unlock(&disks_mutex
);
3991 kobject_uevent(&mddev
->kobj
, KOBJ_ADD
);
3992 mddev
->sysfs_state
= sysfs_get_dirent(mddev
->kobj
.sd
, "array_state");
3998 static struct kobject
*md_probe(dev_t dev
, int *part
, void *data
)
4000 md_alloc(dev
, NULL
);
4004 static int add_named_array(const char *val
, struct kernel_param
*kp
)
4006 /* val must be "md_*" where * is not all digits.
4007 * We allocate an array with a large free minor number, and
4008 * set the name to val. val must not already be an active name.
4010 int len
= strlen(val
);
4011 char buf
[DISK_NAME_LEN
];
4013 while (len
&& val
[len
-1] == '\n')
4015 if (len
>= DISK_NAME_LEN
)
4017 strlcpy(buf
, val
, len
+1);
4018 if (strncmp(buf
, "md_", 3) != 0)
4020 return md_alloc(0, buf
);
4023 static void md_safemode_timeout(unsigned long data
)
4025 mddev_t
*mddev
= (mddev_t
*) data
;
4027 if (!atomic_read(&mddev
->writes_pending
)) {
4028 mddev
->safemode
= 1;
4029 if (mddev
->external
)
4030 sysfs_notify_dirent(mddev
->sysfs_state
);
4032 md_wakeup_thread(mddev
->thread
);
4035 static int start_dirty_degraded
;
4037 static int do_md_run(mddev_t
* mddev
)
4041 struct gendisk
*disk
;
4042 struct mdk_personality
*pers
;
4044 if (list_empty(&mddev
->disks
))
4045 /* cannot run an array with no devices.. */
4052 * Analyze all RAID superblock(s)
4054 if (!mddev
->raid_disks
) {
4055 if (!mddev
->persistent
)
4060 if (mddev
->level
!= LEVEL_NONE
)
4061 request_module("md-level-%d", mddev
->level
);
4062 else if (mddev
->clevel
[0])
4063 request_module("md-%s", mddev
->clevel
);
4066 * Drop all container device buffers, from now on
4067 * the only valid external interface is through the md
4070 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
4071 if (test_bit(Faulty
, &rdev
->flags
))
4073 sync_blockdev(rdev
->bdev
);
4074 invalidate_bdev(rdev
->bdev
);
4076 /* perform some consistency tests on the device.
4077 * We don't want the data to overlap the metadata,
4078 * Internal Bitmap issues have been handled elsewhere.
4080 if (rdev
->data_offset
< rdev
->sb_start
) {
4081 if (mddev
->dev_sectors
&&
4082 rdev
->data_offset
+ mddev
->dev_sectors
4084 printk("md: %s: data overlaps metadata\n",
4089 if (rdev
->sb_start
+ rdev
->sb_size
/512
4090 > rdev
->data_offset
) {
4091 printk("md: %s: metadata overlaps data\n",
4096 sysfs_notify_dirent(rdev
->sysfs_state
);
4099 md_probe(mddev
->unit
, NULL
, NULL
);
4100 disk
= mddev
->gendisk
;
4104 spin_lock(&pers_lock
);
4105 pers
= find_pers(mddev
->level
, mddev
->clevel
);
4106 if (!pers
|| !try_module_get(pers
->owner
)) {
4107 spin_unlock(&pers_lock
);
4108 if (mddev
->level
!= LEVEL_NONE
)
4109 printk(KERN_WARNING
"md: personality for level %d is not loaded!\n",
4112 printk(KERN_WARNING
"md: personality for level %s is not loaded!\n",
4117 spin_unlock(&pers_lock
);
4118 if (mddev
->level
!= pers
->level
) {
4119 mddev
->level
= pers
->level
;
4120 mddev
->new_level
= pers
->level
;
4122 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
4124 if (mddev
->reshape_position
!= MaxSector
&&
4125 pers
->start_reshape
== NULL
) {
4126 /* This personality cannot handle reshaping... */
4128 module_put(pers
->owner
);
4132 if (pers
->sync_request
) {
4133 /* Warn if this is a potentially silly
4136 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
4140 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4141 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
4143 rdev
->bdev
->bd_contains
==
4144 rdev2
->bdev
->bd_contains
) {
4146 "%s: WARNING: %s appears to be"
4147 " on the same physical disk as"
4150 bdevname(rdev
->bdev
,b
),
4151 bdevname(rdev2
->bdev
,b2
));
4158 "True protection against single-disk"
4159 " failure might be compromised.\n");
4162 mddev
->recovery
= 0;
4163 /* may be over-ridden by personality */
4164 mddev
->resync_max_sectors
= mddev
->dev_sectors
;
4166 mddev
->barriers_work
= 1;
4167 mddev
->ok_start_degraded
= start_dirty_degraded
;
4170 mddev
->ro
= 2; /* read-only, but switch on first write */
4172 err
= mddev
->pers
->run(mddev
);
4174 printk(KERN_ERR
"md: pers->run() failed ...\n");
4175 else if (mddev
->pers
->size(mddev
, 0, 0) < mddev
->array_sectors
) {
4176 WARN_ONCE(!mddev
->external_size
, "%s: default size too small,"
4177 " but 'external_size' not in effect?\n", __func__
);
4179 "md: invalid array_size %llu > default size %llu\n",
4180 (unsigned long long)mddev
->array_sectors
/ 2,
4181 (unsigned long long)mddev
->pers
->size(mddev
, 0, 0) / 2);
4183 mddev
->pers
->stop(mddev
);
4185 if (err
== 0 && mddev
->pers
->sync_request
) {
4186 err
= bitmap_create(mddev
);
4188 printk(KERN_ERR
"%s: failed to create bitmap (%d)\n",
4189 mdname(mddev
), err
);
4190 mddev
->pers
->stop(mddev
);
4194 module_put(mddev
->pers
->owner
);
4196 bitmap_destroy(mddev
);
4199 if (mddev
->pers
->sync_request
) {
4200 if (sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
4202 "md: cannot register extra attributes for %s\n",
4204 mddev
->sysfs_action
= sysfs_get_dirent(mddev
->kobj
.sd
, "sync_action");
4205 } else if (mddev
->ro
== 2) /* auto-readonly not meaningful */
4208 atomic_set(&mddev
->writes_pending
,0);
4209 mddev
->safemode
= 0;
4210 mddev
->safemode_timer
.function
= md_safemode_timeout
;
4211 mddev
->safemode_timer
.data
= (unsigned long) mddev
;
4212 mddev
->safemode_delay
= (200 * HZ
)/1000 +1; /* 200 msec delay */
4215 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4216 if (rdev
->raid_disk
>= 0) {
4218 sprintf(nm
, "rd%d", rdev
->raid_disk
);
4219 if (sysfs_create_link(&mddev
->kobj
, &rdev
->kobj
, nm
))
4220 printk("md: cannot register %s for %s\n",
4224 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4227 md_update_sb(mddev
, 0);
4229 set_capacity(disk
, mddev
->array_sectors
);
4231 /* If there is a partially-recovered drive we need to
4232 * start recovery here. If we leave it to md_check_recovery,
4233 * it will remove the drives and not do the right thing
4235 if (mddev
->degraded
&& !mddev
->sync_thread
) {
4237 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4238 if (rdev
->raid_disk
>= 0 &&
4239 !test_bit(In_sync
, &rdev
->flags
) &&
4240 !test_bit(Faulty
, &rdev
->flags
))
4241 /* complete an interrupted recovery */
4243 if (spares
&& mddev
->pers
->sync_request
) {
4244 mddev
->recovery
= 0;
4245 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
4246 mddev
->sync_thread
= md_register_thread(md_do_sync
,
4249 if (!mddev
->sync_thread
) {
4250 printk(KERN_ERR
"%s: could not start resync"
4253 /* leave the spares where they are, it shouldn't hurt */
4254 mddev
->recovery
= 0;
4258 md_wakeup_thread(mddev
->thread
);
4259 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
4261 revalidate_disk(mddev
->gendisk
);
4263 md_new_event(mddev
);
4264 sysfs_notify_dirent(mddev
->sysfs_state
);
4265 if (mddev
->sysfs_action
)
4266 sysfs_notify_dirent(mddev
->sysfs_action
);
4267 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
4268 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
4272 static int restart_array(mddev_t
*mddev
)
4274 struct gendisk
*disk
= mddev
->gendisk
;
4276 /* Complain if it has no devices */
4277 if (list_empty(&mddev
->disks
))
4283 mddev
->safemode
= 0;
4285 set_disk_ro(disk
, 0);
4286 printk(KERN_INFO
"md: %s switched to read-write mode.\n",
4288 /* Kick recovery or resync if necessary */
4289 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4290 md_wakeup_thread(mddev
->thread
);
4291 md_wakeup_thread(mddev
->sync_thread
);
4292 sysfs_notify_dirent(mddev
->sysfs_state
);
4296 /* similar to deny_write_access, but accounts for our holding a reference
4297 * to the file ourselves */
4298 static int deny_bitmap_write_access(struct file
* file
)
4300 struct inode
*inode
= file
->f_mapping
->host
;
4302 spin_lock(&inode
->i_lock
);
4303 if (atomic_read(&inode
->i_writecount
) > 1) {
4304 spin_unlock(&inode
->i_lock
);
4307 atomic_set(&inode
->i_writecount
, -1);
4308 spin_unlock(&inode
->i_lock
);
4313 static void restore_bitmap_write_access(struct file
*file
)
4315 struct inode
*inode
= file
->f_mapping
->host
;
4317 spin_lock(&inode
->i_lock
);
4318 atomic_set(&inode
->i_writecount
, 1);
4319 spin_unlock(&inode
->i_lock
);
4323 * 0 - completely stop and dis-assemble array
4324 * 1 - switch to readonly
4325 * 2 - stop but do not disassemble array
4327 static int do_md_stop(mddev_t
* mddev
, int mode
, int is_open
)
4330 struct gendisk
*disk
= mddev
->gendisk
;
4333 mutex_lock(&mddev
->open_mutex
);
4334 if (atomic_read(&mddev
->openers
) > is_open
) {
4335 printk("md: %s still in use.\n",mdname(mddev
));
4337 } else if (mddev
->pers
) {
4339 if (mddev
->sync_thread
) {
4340 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4341 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4342 md_unregister_thread(mddev
->sync_thread
);
4343 mddev
->sync_thread
= NULL
;
4346 del_timer_sync(&mddev
->safemode_timer
);
4349 case 1: /* readonly */
4355 case 0: /* disassemble */
4357 bitmap_flush(mddev
);
4358 md_super_wait(mddev
);
4360 set_disk_ro(disk
, 0);
4362 mddev
->pers
->stop(mddev
);
4363 mddev
->queue
->merge_bvec_fn
= NULL
;
4364 mddev
->queue
->unplug_fn
= NULL
;
4365 mddev
->queue
->backing_dev_info
.congested_fn
= NULL
;
4366 module_put(mddev
->pers
->owner
);
4367 if (mddev
->pers
->sync_request
)
4368 mddev
->private = &md_redundancy_group
;
4370 /* tell userspace to handle 'inactive' */
4371 sysfs_notify_dirent(mddev
->sysfs_state
);
4373 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4374 if (rdev
->raid_disk
>= 0) {
4376 sprintf(nm
, "rd%d", rdev
->raid_disk
);
4377 sysfs_remove_link(&mddev
->kobj
, nm
);
4380 set_capacity(disk
, 0);
4386 if (!mddev
->in_sync
|| mddev
->flags
) {
4387 /* mark array as shutdown cleanly */
4389 md_update_sb(mddev
, 1);
4392 set_disk_ro(disk
, 1);
4393 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4397 mutex_unlock(&mddev
->open_mutex
);
4401 * Free resources if final stop
4405 printk(KERN_INFO
"md: %s stopped.\n", mdname(mddev
));
4407 bitmap_destroy(mddev
);
4408 if (mddev
->bitmap_file
) {
4409 restore_bitmap_write_access(mddev
->bitmap_file
);
4410 fput(mddev
->bitmap_file
);
4411 mddev
->bitmap_file
= NULL
;
4413 mddev
->bitmap_offset
= 0;
4415 /* make sure all md_delayed_delete calls have finished */
4416 flush_scheduled_work();
4418 export_array(mddev
);
4420 mddev
->array_sectors
= 0;
4421 mddev
->external_size
= 0;
4422 mddev
->dev_sectors
= 0;
4423 mddev
->raid_disks
= 0;
4424 mddev
->recovery_cp
= 0;
4425 mddev
->resync_min
= 0;
4426 mddev
->resync_max
= MaxSector
;
4427 mddev
->reshape_position
= MaxSector
;
4428 mddev
->external
= 0;
4429 mddev
->persistent
= 0;
4430 mddev
->level
= LEVEL_NONE
;
4431 mddev
->clevel
[0] = 0;
4434 mddev
->metadata_type
[0] = 0;
4435 mddev
->chunk_sectors
= 0;
4436 mddev
->ctime
= mddev
->utime
= 0;
4438 mddev
->max_disks
= 0;
4440 mddev
->delta_disks
= 0;
4441 mddev
->new_level
= LEVEL_NONE
;
4442 mddev
->new_layout
= 0;
4443 mddev
->new_chunk_sectors
= 0;
4444 mddev
->curr_resync
= 0;
4445 mddev
->resync_mismatches
= 0;
4446 mddev
->suspend_lo
= mddev
->suspend_hi
= 0;
4447 mddev
->sync_speed_min
= mddev
->sync_speed_max
= 0;
4448 mddev
->recovery
= 0;
4451 mddev
->degraded
= 0;
4452 mddev
->barriers_work
= 0;
4453 mddev
->safemode
= 0;
4454 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
4455 if (mddev
->hold_active
== UNTIL_STOP
)
4456 mddev
->hold_active
= 0;
4458 } else if (mddev
->pers
)
4459 printk(KERN_INFO
"md: %s switched to read-only mode.\n",
4462 blk_integrity_unregister(disk
);
4463 md_new_event(mddev
);
4464 sysfs_notify_dirent(mddev
->sysfs_state
);
4469 static void autorun_array(mddev_t
*mddev
)
4474 if (list_empty(&mddev
->disks
))
4477 printk(KERN_INFO
"md: running: ");
4479 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
4480 char b
[BDEVNAME_SIZE
];
4481 printk("<%s>", bdevname(rdev
->bdev
,b
));
4485 err
= do_md_run(mddev
);
4487 printk(KERN_WARNING
"md: do_md_run() returned %d\n", err
);
4488 do_md_stop(mddev
, 0, 0);
4493 * lets try to run arrays based on all disks that have arrived
4494 * until now. (those are in pending_raid_disks)
4496 * the method: pick the first pending disk, collect all disks with
4497 * the same UUID, remove all from the pending list and put them into
4498 * the 'same_array' list. Then order this list based on superblock
4499 * update time (freshest comes first), kick out 'old' disks and
4500 * compare superblocks. If everything's fine then run it.
4502 * If "unit" is allocated, then bump its reference count
4504 static void autorun_devices(int part
)
4506 mdk_rdev_t
*rdev0
, *rdev
, *tmp
;
4508 char b
[BDEVNAME_SIZE
];
4510 printk(KERN_INFO
"md: autorun ...\n");
4511 while (!list_empty(&pending_raid_disks
)) {
4514 LIST_HEAD(candidates
);
4515 rdev0
= list_entry(pending_raid_disks
.next
,
4516 mdk_rdev_t
, same_set
);
4518 printk(KERN_INFO
"md: considering %s ...\n",
4519 bdevname(rdev0
->bdev
,b
));
4520 INIT_LIST_HEAD(&candidates
);
4521 rdev_for_each_list(rdev
, tmp
, &pending_raid_disks
)
4522 if (super_90_load(rdev
, rdev0
, 0) >= 0) {
4523 printk(KERN_INFO
"md: adding %s ...\n",
4524 bdevname(rdev
->bdev
,b
));
4525 list_move(&rdev
->same_set
, &candidates
);
4528 * now we have a set of devices, with all of them having
4529 * mostly sane superblocks. It's time to allocate the
4533 dev
= MKDEV(mdp_major
,
4534 rdev0
->preferred_minor
<< MdpMinorShift
);
4535 unit
= MINOR(dev
) >> MdpMinorShift
;
4537 dev
= MKDEV(MD_MAJOR
, rdev0
->preferred_minor
);
4540 if (rdev0
->preferred_minor
!= unit
) {
4541 printk(KERN_INFO
"md: unit number in %s is bad: %d\n",
4542 bdevname(rdev0
->bdev
, b
), rdev0
->preferred_minor
);
4546 md_probe(dev
, NULL
, NULL
);
4547 mddev
= mddev_find(dev
);
4548 if (!mddev
|| !mddev
->gendisk
) {
4552 "md: cannot allocate memory for md drive.\n");
4555 if (mddev_lock(mddev
))
4556 printk(KERN_WARNING
"md: %s locked, cannot run\n",
4558 else if (mddev
->raid_disks
|| mddev
->major_version
4559 || !list_empty(&mddev
->disks
)) {
4561 "md: %s already running, cannot run %s\n",
4562 mdname(mddev
), bdevname(rdev0
->bdev
,b
));
4563 mddev_unlock(mddev
);
4565 printk(KERN_INFO
"md: created %s\n", mdname(mddev
));
4566 mddev
->persistent
= 1;
4567 rdev_for_each_list(rdev
, tmp
, &candidates
) {
4568 list_del_init(&rdev
->same_set
);
4569 if (bind_rdev_to_array(rdev
, mddev
))
4572 autorun_array(mddev
);
4573 mddev_unlock(mddev
);
4575 /* on success, candidates will be empty, on error
4578 rdev_for_each_list(rdev
, tmp
, &candidates
) {
4579 list_del_init(&rdev
->same_set
);
4584 printk(KERN_INFO
"md: ... autorun DONE.\n");
4586 #endif /* !MODULE */
4588 static int get_version(void __user
* arg
)
4592 ver
.major
= MD_MAJOR_VERSION
;
4593 ver
.minor
= MD_MINOR_VERSION
;
4594 ver
.patchlevel
= MD_PATCHLEVEL_VERSION
;
4596 if (copy_to_user(arg
, &ver
, sizeof(ver
)))
4602 static int get_array_info(mddev_t
* mddev
, void __user
* arg
)
4604 mdu_array_info_t info
;
4605 int nr
,working
,insync
,failed
,spare
;
4608 nr
=working
=insync
=failed
=spare
=0;
4609 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
4611 if (test_bit(Faulty
, &rdev
->flags
))
4615 if (test_bit(In_sync
, &rdev
->flags
))
4622 info
.major_version
= mddev
->major_version
;
4623 info
.minor_version
= mddev
->minor_version
;
4624 info
.patch_version
= MD_PATCHLEVEL_VERSION
;
4625 info
.ctime
= mddev
->ctime
;
4626 info
.level
= mddev
->level
;
4627 info
.size
= mddev
->dev_sectors
/ 2;
4628 if (info
.size
!= mddev
->dev_sectors
/ 2) /* overflow */
4631 info
.raid_disks
= mddev
->raid_disks
;
4632 info
.md_minor
= mddev
->md_minor
;
4633 info
.not_persistent
= !mddev
->persistent
;
4635 info
.utime
= mddev
->utime
;
4638 info
.state
= (1<<MD_SB_CLEAN
);
4639 if (mddev
->bitmap
&& mddev
->bitmap_offset
)
4640 info
.state
= (1<<MD_SB_BITMAP_PRESENT
);
4641 info
.active_disks
= insync
;
4642 info
.working_disks
= working
;
4643 info
.failed_disks
= failed
;
4644 info
.spare_disks
= spare
;
4646 info
.layout
= mddev
->layout
;
4647 info
.chunk_size
= mddev
->chunk_sectors
<< 9;
4649 if (copy_to_user(arg
, &info
, sizeof(info
)))
4655 static int get_bitmap_file(mddev_t
* mddev
, void __user
* arg
)
4657 mdu_bitmap_file_t
*file
= NULL
; /* too big for stack allocation */
4658 char *ptr
, *buf
= NULL
;
4661 if (md_allow_write(mddev
))
4662 file
= kmalloc(sizeof(*file
), GFP_NOIO
);
4664 file
= kmalloc(sizeof(*file
), GFP_KERNEL
);
4669 /* bitmap disabled, zero the first byte and copy out */
4670 if (!mddev
->bitmap
|| !mddev
->bitmap
->file
) {
4671 file
->pathname
[0] = '\0';
4675 buf
= kmalloc(sizeof(file
->pathname
), GFP_KERNEL
);
4679 ptr
= d_path(&mddev
->bitmap
->file
->f_path
, buf
, sizeof(file
->pathname
));
4683 strcpy(file
->pathname
, ptr
);
4687 if (copy_to_user(arg
, file
, sizeof(*file
)))
4695 static int get_disk_info(mddev_t
* mddev
, void __user
* arg
)
4697 mdu_disk_info_t info
;
4700 if (copy_from_user(&info
, arg
, sizeof(info
)))
4703 rdev
= find_rdev_nr(mddev
, info
.number
);
4705 info
.major
= MAJOR(rdev
->bdev
->bd_dev
);
4706 info
.minor
= MINOR(rdev
->bdev
->bd_dev
);
4707 info
.raid_disk
= rdev
->raid_disk
;
4709 if (test_bit(Faulty
, &rdev
->flags
))
4710 info
.state
|= (1<<MD_DISK_FAULTY
);
4711 else if (test_bit(In_sync
, &rdev
->flags
)) {
4712 info
.state
|= (1<<MD_DISK_ACTIVE
);
4713 info
.state
|= (1<<MD_DISK_SYNC
);
4715 if (test_bit(WriteMostly
, &rdev
->flags
))
4716 info
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
4718 info
.major
= info
.minor
= 0;
4719 info
.raid_disk
= -1;
4720 info
.state
= (1<<MD_DISK_REMOVED
);
4723 if (copy_to_user(arg
, &info
, sizeof(info
)))
4729 static int add_new_disk(mddev_t
* mddev
, mdu_disk_info_t
*info
)
4731 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
4733 dev_t dev
= MKDEV(info
->major
,info
->minor
);
4735 if (info
->major
!= MAJOR(dev
) || info
->minor
!= MINOR(dev
))
4738 if (!mddev
->raid_disks
) {
4740 /* expecting a device which has a superblock */
4741 rdev
= md_import_device(dev
, mddev
->major_version
, mddev
->minor_version
);
4744 "md: md_import_device returned %ld\n",
4746 return PTR_ERR(rdev
);
4748 if (!list_empty(&mddev
->disks
)) {
4749 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
4750 mdk_rdev_t
, same_set
);
4751 err
= super_types
[mddev
->major_version
]
4752 .load_super(rdev
, rdev0
, mddev
->minor_version
);
4755 "md: %s has different UUID to %s\n",
4756 bdevname(rdev
->bdev
,b
),
4757 bdevname(rdev0
->bdev
,b2
));
4762 err
= bind_rdev_to_array(rdev
, mddev
);
4769 * add_new_disk can be used once the array is assembled
4770 * to add "hot spares". They must already have a superblock
4775 if (!mddev
->pers
->hot_add_disk
) {
4777 "%s: personality does not support diskops!\n",
4781 if (mddev
->persistent
)
4782 rdev
= md_import_device(dev
, mddev
->major_version
,
4783 mddev
->minor_version
);
4785 rdev
= md_import_device(dev
, -1, -1);
4788 "md: md_import_device returned %ld\n",
4790 return PTR_ERR(rdev
);
4792 /* set save_raid_disk if appropriate */
4793 if (!mddev
->persistent
) {
4794 if (info
->state
& (1<<MD_DISK_SYNC
) &&
4795 info
->raid_disk
< mddev
->raid_disks
)
4796 rdev
->raid_disk
= info
->raid_disk
;
4798 rdev
->raid_disk
= -1;
4800 super_types
[mddev
->major_version
].
4801 validate_super(mddev
, rdev
);
4802 rdev
->saved_raid_disk
= rdev
->raid_disk
;
4804 clear_bit(In_sync
, &rdev
->flags
); /* just to be sure */
4805 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
4806 set_bit(WriteMostly
, &rdev
->flags
);
4808 clear_bit(WriteMostly
, &rdev
->flags
);
4810 rdev
->raid_disk
= -1;
4811 err
= bind_rdev_to_array(rdev
, mddev
);
4812 if (!err
&& !mddev
->pers
->hot_remove_disk
) {
4813 /* If there is hot_add_disk but no hot_remove_disk
4814 * then added disks for geometry changes,
4815 * and should be added immediately.
4817 super_types
[mddev
->major_version
].
4818 validate_super(mddev
, rdev
);
4819 err
= mddev
->pers
->hot_add_disk(mddev
, rdev
);
4821 unbind_rdev_from_array(rdev
);
4826 sysfs_notify_dirent(rdev
->sysfs_state
);
4828 md_update_sb(mddev
, 1);
4829 if (mddev
->degraded
)
4830 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
4831 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4832 md_wakeup_thread(mddev
->thread
);
4836 /* otherwise, add_new_disk is only allowed
4837 * for major_version==0 superblocks
4839 if (mddev
->major_version
!= 0) {
4840 printk(KERN_WARNING
"%s: ADD_NEW_DISK not supported\n",
4845 if (!(info
->state
& (1<<MD_DISK_FAULTY
))) {
4847 rdev
= md_import_device(dev
, -1, 0);
4850 "md: error, md_import_device() returned %ld\n",
4852 return PTR_ERR(rdev
);
4854 rdev
->desc_nr
= info
->number
;
4855 if (info
->raid_disk
< mddev
->raid_disks
)
4856 rdev
->raid_disk
= info
->raid_disk
;
4858 rdev
->raid_disk
= -1;
4860 if (rdev
->raid_disk
< mddev
->raid_disks
)
4861 if (info
->state
& (1<<MD_DISK_SYNC
))
4862 set_bit(In_sync
, &rdev
->flags
);
4864 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
4865 set_bit(WriteMostly
, &rdev
->flags
);
4867 if (!mddev
->persistent
) {
4868 printk(KERN_INFO
"md: nonpersistent superblock ...\n");
4869 rdev
->sb_start
= rdev
->bdev
->bd_inode
->i_size
/ 512;
4871 rdev
->sb_start
= calc_dev_sboffset(rdev
->bdev
);
4872 rdev
->sectors
= rdev
->sb_start
;
4874 err
= bind_rdev_to_array(rdev
, mddev
);
4884 static int hot_remove_disk(mddev_t
* mddev
, dev_t dev
)
4886 char b
[BDEVNAME_SIZE
];
4889 rdev
= find_rdev(mddev
, dev
);
4893 if (rdev
->raid_disk
>= 0)
4896 kick_rdev_from_array(rdev
);
4897 md_update_sb(mddev
, 1);
4898 md_new_event(mddev
);
4902 printk(KERN_WARNING
"md: cannot remove active disk %s from %s ...\n",
4903 bdevname(rdev
->bdev
,b
), mdname(mddev
));
4907 static int hot_add_disk(mddev_t
* mddev
, dev_t dev
)
4909 char b
[BDEVNAME_SIZE
];
4916 if (mddev
->major_version
!= 0) {
4917 printk(KERN_WARNING
"%s: HOT_ADD may only be used with"
4918 " version-0 superblocks.\n",
4922 if (!mddev
->pers
->hot_add_disk
) {
4924 "%s: personality does not support diskops!\n",
4929 rdev
= md_import_device(dev
, -1, 0);
4932 "md: error, md_import_device() returned %ld\n",
4937 if (mddev
->persistent
)
4938 rdev
->sb_start
= calc_dev_sboffset(rdev
->bdev
);
4940 rdev
->sb_start
= rdev
->bdev
->bd_inode
->i_size
/ 512;
4942 rdev
->sectors
= rdev
->sb_start
;
4944 if (test_bit(Faulty
, &rdev
->flags
)) {
4946 "md: can not hot-add faulty %s disk to %s!\n",
4947 bdevname(rdev
->bdev
,b
), mdname(mddev
));
4951 clear_bit(In_sync
, &rdev
->flags
);
4953 rdev
->saved_raid_disk
= -1;
4954 err
= bind_rdev_to_array(rdev
, mddev
);
4959 * The rest should better be atomic, we can have disk failures
4960 * noticed in interrupt contexts ...
4963 rdev
->raid_disk
= -1;
4965 md_update_sb(mddev
, 1);
4968 * Kick recovery, maybe this spare has to be added to the
4969 * array immediately.
4971 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4972 md_wakeup_thread(mddev
->thread
);
4973 md_new_event(mddev
);
4981 static int set_bitmap_file(mddev_t
*mddev
, int fd
)
4986 if (!mddev
->pers
->quiesce
)
4988 if (mddev
->recovery
|| mddev
->sync_thread
)
4990 /* we should be able to change the bitmap.. */
4996 return -EEXIST
; /* cannot add when bitmap is present */
4997 mddev
->bitmap_file
= fget(fd
);
4999 if (mddev
->bitmap_file
== NULL
) {
5000 printk(KERN_ERR
"%s: error: failed to get bitmap file\n",
5005 err
= deny_bitmap_write_access(mddev
->bitmap_file
);
5007 printk(KERN_ERR
"%s: error: bitmap file is already in use\n",
5009 fput(mddev
->bitmap_file
);
5010 mddev
->bitmap_file
= NULL
;
5013 mddev
->bitmap_offset
= 0; /* file overrides offset */
5014 } else if (mddev
->bitmap
== NULL
)
5015 return -ENOENT
; /* cannot remove what isn't there */
5018 mddev
->pers
->quiesce(mddev
, 1);
5020 err
= bitmap_create(mddev
);
5021 if (fd
< 0 || err
) {
5022 bitmap_destroy(mddev
);
5023 fd
= -1; /* make sure to put the file */
5025 mddev
->pers
->quiesce(mddev
, 0);
5028 if (mddev
->bitmap_file
) {
5029 restore_bitmap_write_access(mddev
->bitmap_file
);
5030 fput(mddev
->bitmap_file
);
5032 mddev
->bitmap_file
= NULL
;
5039 * set_array_info is used two different ways
5040 * The original usage is when creating a new array.
5041 * In this usage, raid_disks is > 0 and it together with
5042 * level, size, not_persistent,layout,chunksize determine the
5043 * shape of the array.
5044 * This will always create an array with a type-0.90.0 superblock.
5045 * The newer usage is when assembling an array.
5046 * In this case raid_disks will be 0, and the major_version field is
5047 * use to determine which style super-blocks are to be found on the devices.
5048 * The minor and patch _version numbers are also kept incase the
5049 * super_block handler wishes to interpret them.
5051 static int set_array_info(mddev_t
* mddev
, mdu_array_info_t
*info
)
5054 if (info
->raid_disks
== 0) {
5055 /* just setting version number for superblock loading */
5056 if (info
->major_version
< 0 ||
5057 info
->major_version
>= ARRAY_SIZE(super_types
) ||
5058 super_types
[info
->major_version
].name
== NULL
) {
5059 /* maybe try to auto-load a module? */
5061 "md: superblock version %d not known\n",
5062 info
->major_version
);
5065 mddev
->major_version
= info
->major_version
;
5066 mddev
->minor_version
= info
->minor_version
;
5067 mddev
->patch_version
= info
->patch_version
;
5068 mddev
->persistent
= !info
->not_persistent
;
5071 mddev
->major_version
= MD_MAJOR_VERSION
;
5072 mddev
->minor_version
= MD_MINOR_VERSION
;
5073 mddev
->patch_version
= MD_PATCHLEVEL_VERSION
;
5074 mddev
->ctime
= get_seconds();
5076 mddev
->level
= info
->level
;
5077 mddev
->clevel
[0] = 0;
5078 mddev
->dev_sectors
= 2 * (sector_t
)info
->size
;
5079 mddev
->raid_disks
= info
->raid_disks
;
5080 /* don't set md_minor, it is determined by which /dev/md* was
5083 if (info
->state
& (1<<MD_SB_CLEAN
))
5084 mddev
->recovery_cp
= MaxSector
;
5086 mddev
->recovery_cp
= 0;
5087 mddev
->persistent
= ! info
->not_persistent
;
5088 mddev
->external
= 0;
5090 mddev
->layout
= info
->layout
;
5091 mddev
->chunk_sectors
= info
->chunk_size
>> 9;
5093 mddev
->max_disks
= MD_SB_DISKS
;
5095 if (mddev
->persistent
)
5097 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
5099 mddev
->default_bitmap_offset
= MD_SB_BYTES
>> 9;
5100 mddev
->bitmap_offset
= 0;
5102 mddev
->reshape_position
= MaxSector
;
5105 * Generate a 128 bit UUID
5107 get_random_bytes(mddev
->uuid
, 16);
5109 mddev
->new_level
= mddev
->level
;
5110 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
5111 mddev
->new_layout
= mddev
->layout
;
5112 mddev
->delta_disks
= 0;
5117 void md_set_array_sectors(mddev_t
*mddev
, sector_t array_sectors
)
5119 WARN(!mddev_is_locked(mddev
), "%s: unlocked mddev!\n", __func__
);
5121 if (mddev
->external_size
)
5124 mddev
->array_sectors
= array_sectors
;
5126 EXPORT_SYMBOL(md_set_array_sectors
);
5128 static int update_size(mddev_t
*mddev
, sector_t num_sectors
)
5132 int fit
= (num_sectors
== 0);
5134 if (mddev
->pers
->resize
== NULL
)
5136 /* The "num_sectors" is the number of sectors of each device that
5137 * is used. This can only make sense for arrays with redundancy.
5138 * linear and raid0 always use whatever space is available. We can only
5139 * consider changing this number if no resync or reconstruction is
5140 * happening, and if the new size is acceptable. It must fit before the
5141 * sb_start or, if that is <data_offset, it must fit before the size
5142 * of each device. If num_sectors is zero, we find the largest size
5146 if (mddev
->sync_thread
)
5149 /* Sorry, cannot grow a bitmap yet, just remove it,
5153 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
5154 sector_t avail
= rdev
->sectors
;
5156 if (fit
&& (num_sectors
== 0 || num_sectors
> avail
))
5157 num_sectors
= avail
;
5158 if (avail
< num_sectors
)
5161 rv
= mddev
->pers
->resize(mddev
, num_sectors
);
5163 revalidate_disk(mddev
->gendisk
);
5167 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
)
5170 /* change the number of raid disks */
5171 if (mddev
->pers
->check_reshape
== NULL
)
5173 if (raid_disks
<= 0 ||
5174 raid_disks
>= mddev
->max_disks
)
5176 if (mddev
->sync_thread
|| mddev
->reshape_position
!= MaxSector
)
5178 mddev
->delta_disks
= raid_disks
- mddev
->raid_disks
;
5180 rv
= mddev
->pers
->check_reshape(mddev
);
5186 * update_array_info is used to change the configuration of an
5188 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5189 * fields in the info are checked against the array.
5190 * Any differences that cannot be handled will cause an error.
5191 * Normally, only one change can be managed at a time.
5193 static int update_array_info(mddev_t
*mddev
, mdu_array_info_t
*info
)
5199 /* calculate expected state,ignoring low bits */
5200 if (mddev
->bitmap
&& mddev
->bitmap_offset
)
5201 state
|= (1 << MD_SB_BITMAP_PRESENT
);
5203 if (mddev
->major_version
!= info
->major_version
||
5204 mddev
->minor_version
!= info
->minor_version
||
5205 /* mddev->patch_version != info->patch_version || */
5206 mddev
->ctime
!= info
->ctime
||
5207 mddev
->level
!= info
->level
||
5208 /* mddev->layout != info->layout || */
5209 !mddev
->persistent
!= info
->not_persistent
||
5210 mddev
->chunk_sectors
!= info
->chunk_size
>> 9 ||
5211 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5212 ((state
^info
->state
) & 0xfffffe00)
5215 /* Check there is only one change */
5216 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
5218 if (mddev
->raid_disks
!= info
->raid_disks
)
5220 if (mddev
->layout
!= info
->layout
)
5222 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
))
5229 if (mddev
->layout
!= info
->layout
) {
5231 * we don't need to do anything at the md level, the
5232 * personality will take care of it all.
5234 if (mddev
->pers
->check_reshape
== NULL
)
5237 mddev
->new_layout
= info
->layout
;
5238 rv
= mddev
->pers
->check_reshape(mddev
);
5240 mddev
->new_layout
= mddev
->layout
;
5244 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
5245 rv
= update_size(mddev
, (sector_t
)info
->size
* 2);
5247 if (mddev
->raid_disks
!= info
->raid_disks
)
5248 rv
= update_raid_disks(mddev
, info
->raid_disks
);
5250 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) {
5251 if (mddev
->pers
->quiesce
== NULL
)
5253 if (mddev
->recovery
|| mddev
->sync_thread
)
5255 if (info
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
5256 /* add the bitmap */
5259 if (mddev
->default_bitmap_offset
== 0)
5261 mddev
->bitmap_offset
= mddev
->default_bitmap_offset
;
5262 mddev
->pers
->quiesce(mddev
, 1);
5263 rv
= bitmap_create(mddev
);
5265 bitmap_destroy(mddev
);
5266 mddev
->pers
->quiesce(mddev
, 0);
5268 /* remove the bitmap */
5271 if (mddev
->bitmap
->file
)
5273 mddev
->pers
->quiesce(mddev
, 1);
5274 bitmap_destroy(mddev
);
5275 mddev
->pers
->quiesce(mddev
, 0);
5276 mddev
->bitmap_offset
= 0;
5279 md_update_sb(mddev
, 1);
5283 static int set_disk_faulty(mddev_t
*mddev
, dev_t dev
)
5287 if (mddev
->pers
== NULL
)
5290 rdev
= find_rdev(mddev
, dev
);
5294 md_error(mddev
, rdev
);
5299 * We have a problem here : there is no easy way to give a CHS
5300 * virtual geometry. We currently pretend that we have a 2 heads
5301 * 4 sectors (with a BIG number of cylinders...). This drives
5302 * dosfs just mad... ;-)
5304 static int md_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
5306 mddev_t
*mddev
= bdev
->bd_disk
->private_data
;
5310 geo
->cylinders
= get_capacity(mddev
->gendisk
) / 8;
5314 static int md_ioctl(struct block_device
*bdev
, fmode_t mode
,
5315 unsigned int cmd
, unsigned long arg
)
5318 void __user
*argp
= (void __user
*)arg
;
5319 mddev_t
*mddev
= NULL
;
5321 if (!capable(CAP_SYS_ADMIN
))
5325 * Commands dealing with the RAID driver but not any
5331 err
= get_version(argp
);
5334 case PRINT_RAID_DEBUG
:
5342 autostart_arrays(arg
);
5349 * Commands creating/starting a new array:
5352 mddev
= bdev
->bd_disk
->private_data
;
5359 err
= mddev_lock(mddev
);
5362 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5369 case SET_ARRAY_INFO
:
5371 mdu_array_info_t info
;
5373 memset(&info
, 0, sizeof(info
));
5374 else if (copy_from_user(&info
, argp
, sizeof(info
))) {
5379 err
= update_array_info(mddev
, &info
);
5381 printk(KERN_WARNING
"md: couldn't update"
5382 " array info. %d\n", err
);
5387 if (!list_empty(&mddev
->disks
)) {
5389 "md: array %s already has disks!\n",
5394 if (mddev
->raid_disks
) {
5396 "md: array %s already initialised!\n",
5401 err
= set_array_info(mddev
, &info
);
5403 printk(KERN_WARNING
"md: couldn't set"
5404 " array info. %d\n", err
);
5414 * Commands querying/configuring an existing array:
5416 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
5417 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
5418 if ((!mddev
->raid_disks
&& !mddev
->external
)
5419 && cmd
!= ADD_NEW_DISK
&& cmd
!= STOP_ARRAY
5420 && cmd
!= RUN_ARRAY
&& cmd
!= SET_BITMAP_FILE
5421 && cmd
!= GET_BITMAP_FILE
) {
5427 * Commands even a read-only array can execute:
5431 case GET_ARRAY_INFO
:
5432 err
= get_array_info(mddev
, argp
);
5435 case GET_BITMAP_FILE
:
5436 err
= get_bitmap_file(mddev
, argp
);
5440 err
= get_disk_info(mddev
, argp
);
5443 case RESTART_ARRAY_RW
:
5444 err
= restart_array(mddev
);
5448 err
= do_md_stop(mddev
, 0, 1);
5452 err
= do_md_stop(mddev
, 1, 1);
5458 * The remaining ioctls are changing the state of the
5459 * superblock, so we do not allow them on read-only arrays.
5460 * However non-MD ioctls (e.g. get-size) will still come through
5461 * here and hit the 'default' below, so only disallow
5462 * 'md' ioctls, and switch to rw mode if started auto-readonly.
5464 if (_IOC_TYPE(cmd
) == MD_MAJOR
&& mddev
->ro
&& mddev
->pers
) {
5465 if (mddev
->ro
== 2) {
5467 sysfs_notify_dirent(mddev
->sysfs_state
);
5468 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5469 md_wakeup_thread(mddev
->thread
);
5480 mdu_disk_info_t info
;
5481 if (copy_from_user(&info
, argp
, sizeof(info
)))
5484 err
= add_new_disk(mddev
, &info
);
5488 case HOT_REMOVE_DISK
:
5489 err
= hot_remove_disk(mddev
, new_decode_dev(arg
));
5493 err
= hot_add_disk(mddev
, new_decode_dev(arg
));
5496 case SET_DISK_FAULTY
:
5497 err
= set_disk_faulty(mddev
, new_decode_dev(arg
));
5501 err
= do_md_run(mddev
);
5504 case SET_BITMAP_FILE
:
5505 err
= set_bitmap_file(mddev
, (int)arg
);
5515 if (mddev
->hold_active
== UNTIL_IOCTL
&&
5517 mddev
->hold_active
= 0;
5518 mddev_unlock(mddev
);
5528 static int md_open(struct block_device
*bdev
, fmode_t mode
)
5531 * Succeed if we can lock the mddev, which confirms that
5532 * it isn't being stopped right now.
5534 mddev_t
*mddev
= mddev_find(bdev
->bd_dev
);
5537 if (mddev
->gendisk
!= bdev
->bd_disk
) {
5538 /* we are racing with mddev_put which is discarding this
5542 /* Wait until bdev->bd_disk is definitely gone */
5543 flush_scheduled_work();
5544 /* Then retry the open from the top */
5545 return -ERESTARTSYS
;
5547 BUG_ON(mddev
!= bdev
->bd_disk
->private_data
);
5549 if ((err
= mutex_lock_interruptible(&mddev
->open_mutex
)))
5553 atomic_inc(&mddev
->openers
);
5554 mutex_unlock(&mddev
->open_mutex
);
5556 check_disk_change(bdev
);
5561 static int md_release(struct gendisk
*disk
, fmode_t mode
)
5563 mddev_t
*mddev
= disk
->private_data
;
5566 atomic_dec(&mddev
->openers
);
5572 static int md_media_changed(struct gendisk
*disk
)
5574 mddev_t
*mddev
= disk
->private_data
;
5576 return mddev
->changed
;
5579 static int md_revalidate(struct gendisk
*disk
)
5581 mddev_t
*mddev
= disk
->private_data
;
5586 static const struct block_device_operations md_fops
=
5588 .owner
= THIS_MODULE
,
5590 .release
= md_release
,
5592 .getgeo
= md_getgeo
,
5593 .media_changed
= md_media_changed
,
5594 .revalidate_disk
= md_revalidate
,
5597 static int md_thread(void * arg
)
5599 mdk_thread_t
*thread
= arg
;
5602 * md_thread is a 'system-thread', it's priority should be very
5603 * high. We avoid resource deadlocks individually in each
5604 * raid personality. (RAID5 does preallocation) We also use RR and
5605 * the very same RT priority as kswapd, thus we will never get
5606 * into a priority inversion deadlock.
5608 * we definitely have to have equal or higher priority than
5609 * bdflush, otherwise bdflush will deadlock if there are too
5610 * many dirty RAID5 blocks.
5613 allow_signal(SIGKILL
);
5614 while (!kthread_should_stop()) {
5616 /* We need to wait INTERRUPTIBLE so that
5617 * we don't add to the load-average.
5618 * That means we need to be sure no signals are
5621 if (signal_pending(current
))
5622 flush_signals(current
);
5624 wait_event_interruptible_timeout
5626 test_bit(THREAD_WAKEUP
, &thread
->flags
)
5627 || kthread_should_stop(),
5630 clear_bit(THREAD_WAKEUP
, &thread
->flags
);
5632 thread
->run(thread
->mddev
);
5638 void md_wakeup_thread(mdk_thread_t
*thread
)
5641 dprintk("md: waking up MD thread %s.\n", thread
->tsk
->comm
);
5642 set_bit(THREAD_WAKEUP
, &thread
->flags
);
5643 wake_up(&thread
->wqueue
);
5647 mdk_thread_t
*md_register_thread(void (*run
) (mddev_t
*), mddev_t
*mddev
,
5650 mdk_thread_t
*thread
;
5652 thread
= kzalloc(sizeof(mdk_thread_t
), GFP_KERNEL
);
5656 init_waitqueue_head(&thread
->wqueue
);
5659 thread
->mddev
= mddev
;
5660 thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
5661 thread
->tsk
= kthread_run(md_thread
, thread
,
5663 mdname(thread
->mddev
),
5664 name
?: mddev
->pers
->name
);
5665 if (IS_ERR(thread
->tsk
)) {
5672 void md_unregister_thread(mdk_thread_t
*thread
)
5676 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread
->tsk
));
5678 kthread_stop(thread
->tsk
);
5682 void md_error(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
5689 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
5692 if (mddev
->external
)
5693 set_bit(Blocked
, &rdev
->flags
);
5695 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5697 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5698 __builtin_return_address(0),__builtin_return_address(1),
5699 __builtin_return_address(2),__builtin_return_address(3));
5703 if (!mddev
->pers
->error_handler
)
5705 mddev
->pers
->error_handler(mddev
,rdev
);
5706 if (mddev
->degraded
)
5707 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
5708 set_bit(StateChanged
, &rdev
->flags
);
5709 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5710 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5711 md_wakeup_thread(mddev
->thread
);
5712 md_new_event_inintr(mddev
);
5715 /* seq_file implementation /proc/mdstat */
5717 static void status_unused(struct seq_file
*seq
)
5722 seq_printf(seq
, "unused devices: ");
5724 list_for_each_entry(rdev
, &pending_raid_disks
, same_set
) {
5725 char b
[BDEVNAME_SIZE
];
5727 seq_printf(seq
, "%s ",
5728 bdevname(rdev
->bdev
,b
));
5731 seq_printf(seq
, "<none>");
5733 seq_printf(seq
, "\n");
5737 static void status_resync(struct seq_file
*seq
, mddev_t
* mddev
)
5739 sector_t max_sectors
, resync
, res
;
5740 unsigned long dt
, db
;
5743 unsigned int per_milli
;
5745 resync
= mddev
->curr_resync
- atomic_read(&mddev
->recovery_active
);
5747 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
5748 max_sectors
= mddev
->resync_max_sectors
;
5750 max_sectors
= mddev
->dev_sectors
;
5753 * Should not happen.
5759 /* Pick 'scale' such that (resync>>scale)*1000 will fit
5760 * in a sector_t, and (max_sectors>>scale) will fit in a
5761 * u32, as those are the requirements for sector_div.
5762 * Thus 'scale' must be at least 10
5765 if (sizeof(sector_t
) > sizeof(unsigned long)) {
5766 while ( max_sectors
/2 > (1ULL<<(scale
+32)))
5769 res
= (resync
>>scale
)*1000;
5770 sector_div(res
, (u32
)((max_sectors
>>scale
)+1));
5774 int i
, x
= per_milli
/50, y
= 20-x
;
5775 seq_printf(seq
, "[");
5776 for (i
= 0; i
< x
; i
++)
5777 seq_printf(seq
, "=");
5778 seq_printf(seq
, ">");
5779 for (i
= 0; i
< y
; i
++)
5780 seq_printf(seq
, ".");
5781 seq_printf(seq
, "] ");
5783 seq_printf(seq
, " %s =%3u.%u%% (%llu/%llu)",
5784 (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)?
5786 (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)?
5788 (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ?
5789 "resync" : "recovery"))),
5790 per_milli
/10, per_milli
% 10,
5791 (unsigned long long) resync
/2,
5792 (unsigned long long) max_sectors
/2);
5795 * dt: time from mark until now
5796 * db: blocks written from mark until now
5797 * rt: remaining time
5799 * rt is a sector_t, so could be 32bit or 64bit.
5800 * So we divide before multiply in case it is 32bit and close
5802 * We scale the divisor (db) by 32 to avoid loosing precision
5803 * near the end of resync when the number of remaining sectors
5805 * We then divide rt by 32 after multiplying by db to compensate.
5806 * The '+1' avoids division by zero if db is very small.
5808 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
5810 db
= (mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
))
5811 - mddev
->resync_mark_cnt
;
5813 rt
= max_sectors
- resync
; /* number of remaining sectors */
5814 sector_div(rt
, db
/32+1);
5818 seq_printf(seq
, " finish=%lu.%lumin", (unsigned long)rt
/ 60,
5819 ((unsigned long)rt
% 60)/6);
5821 seq_printf(seq
, " speed=%ldK/sec", db
/2/dt
);
5824 static void *md_seq_start(struct seq_file
*seq
, loff_t
*pos
)
5826 struct list_head
*tmp
;
5836 spin_lock(&all_mddevs_lock
);
5837 list_for_each(tmp
,&all_mddevs
)
5839 mddev
= list_entry(tmp
, mddev_t
, all_mddevs
);
5841 spin_unlock(&all_mddevs_lock
);
5844 spin_unlock(&all_mddevs_lock
);
5846 return (void*)2;/* tail */
5850 static void *md_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
5852 struct list_head
*tmp
;
5853 mddev_t
*next_mddev
, *mddev
= v
;
5859 spin_lock(&all_mddevs_lock
);
5861 tmp
= all_mddevs
.next
;
5863 tmp
= mddev
->all_mddevs
.next
;
5864 if (tmp
!= &all_mddevs
)
5865 next_mddev
= mddev_get(list_entry(tmp
,mddev_t
,all_mddevs
));
5867 next_mddev
= (void*)2;
5870 spin_unlock(&all_mddevs_lock
);
5878 static void md_seq_stop(struct seq_file
*seq
, void *v
)
5882 if (mddev
&& v
!= (void*)1 && v
!= (void*)2)
5886 struct mdstat_info
{
5890 static int md_seq_show(struct seq_file
*seq
, void *v
)
5895 struct mdstat_info
*mi
= seq
->private;
5896 struct bitmap
*bitmap
;
5898 if (v
== (void*)1) {
5899 struct mdk_personality
*pers
;
5900 seq_printf(seq
, "Personalities : ");
5901 spin_lock(&pers_lock
);
5902 list_for_each_entry(pers
, &pers_list
, list
)
5903 seq_printf(seq
, "[%s] ", pers
->name
);
5905 spin_unlock(&pers_lock
);
5906 seq_printf(seq
, "\n");
5907 mi
->event
= atomic_read(&md_event_count
);
5910 if (v
== (void*)2) {
5915 if (mddev_lock(mddev
) < 0)
5918 if (mddev
->pers
|| mddev
->raid_disks
|| !list_empty(&mddev
->disks
)) {
5919 seq_printf(seq
, "%s : %sactive", mdname(mddev
),
5920 mddev
->pers
? "" : "in");
5923 seq_printf(seq
, " (read-only)");
5925 seq_printf(seq
, " (auto-read-only)");
5926 seq_printf(seq
, " %s", mddev
->pers
->name
);
5930 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
5931 char b
[BDEVNAME_SIZE
];
5932 seq_printf(seq
, " %s[%d]",
5933 bdevname(rdev
->bdev
,b
), rdev
->desc_nr
);
5934 if (test_bit(WriteMostly
, &rdev
->flags
))
5935 seq_printf(seq
, "(W)");
5936 if (test_bit(Faulty
, &rdev
->flags
)) {
5937 seq_printf(seq
, "(F)");
5939 } else if (rdev
->raid_disk
< 0)
5940 seq_printf(seq
, "(S)"); /* spare */
5941 sectors
+= rdev
->sectors
;
5944 if (!list_empty(&mddev
->disks
)) {
5946 seq_printf(seq
, "\n %llu blocks",
5947 (unsigned long long)
5948 mddev
->array_sectors
/ 2);
5950 seq_printf(seq
, "\n %llu blocks",
5951 (unsigned long long)sectors
/ 2);
5953 if (mddev
->persistent
) {
5954 if (mddev
->major_version
!= 0 ||
5955 mddev
->minor_version
!= 90) {
5956 seq_printf(seq
," super %d.%d",
5957 mddev
->major_version
,
5958 mddev
->minor_version
);
5960 } else if (mddev
->external
)
5961 seq_printf(seq
, " super external:%s",
5962 mddev
->metadata_type
);
5964 seq_printf(seq
, " super non-persistent");
5967 mddev
->pers
->status(seq
, mddev
);
5968 seq_printf(seq
, "\n ");
5969 if (mddev
->pers
->sync_request
) {
5970 if (mddev
->curr_resync
> 2) {
5971 status_resync(seq
, mddev
);
5972 seq_printf(seq
, "\n ");
5973 } else if (mddev
->curr_resync
== 1 || mddev
->curr_resync
== 2)
5974 seq_printf(seq
, "\tresync=DELAYED\n ");
5975 else if (mddev
->recovery_cp
< MaxSector
)
5976 seq_printf(seq
, "\tresync=PENDING\n ");
5979 seq_printf(seq
, "\n ");
5981 if ((bitmap
= mddev
->bitmap
)) {
5982 unsigned long chunk_kb
;
5983 unsigned long flags
;
5984 spin_lock_irqsave(&bitmap
->lock
, flags
);
5985 chunk_kb
= bitmap
->chunksize
>> 10;
5986 seq_printf(seq
, "bitmap: %lu/%lu pages [%luKB], "
5988 bitmap
->pages
- bitmap
->missing_pages
,
5990 (bitmap
->pages
- bitmap
->missing_pages
)
5991 << (PAGE_SHIFT
- 10),
5992 chunk_kb
? chunk_kb
: bitmap
->chunksize
,
5993 chunk_kb
? "KB" : "B");
5995 seq_printf(seq
, ", file: ");
5996 seq_path(seq
, &bitmap
->file
->f_path
, " \t\n");
5999 seq_printf(seq
, "\n");
6000 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
6003 seq_printf(seq
, "\n");
6005 mddev_unlock(mddev
);
6010 static const struct seq_operations md_seq_ops
= {
6011 .start
= md_seq_start
,
6012 .next
= md_seq_next
,
6013 .stop
= md_seq_stop
,
6014 .show
= md_seq_show
,
6017 static int md_seq_open(struct inode
*inode
, struct file
*file
)
6020 struct mdstat_info
*mi
= kmalloc(sizeof(*mi
), GFP_KERNEL
);
6024 error
= seq_open(file
, &md_seq_ops
);
6028 struct seq_file
*p
= file
->private_data
;
6030 mi
->event
= atomic_read(&md_event_count
);
6035 static unsigned int mdstat_poll(struct file
*filp
, poll_table
*wait
)
6037 struct seq_file
*m
= filp
->private_data
;
6038 struct mdstat_info
*mi
= m
->private;
6041 poll_wait(filp
, &md_event_waiters
, wait
);
6043 /* always allow read */
6044 mask
= POLLIN
| POLLRDNORM
;
6046 if (mi
->event
!= atomic_read(&md_event_count
))
6047 mask
|= POLLERR
| POLLPRI
;
6051 static const struct file_operations md_seq_fops
= {
6052 .owner
= THIS_MODULE
,
6053 .open
= md_seq_open
,
6055 .llseek
= seq_lseek
,
6056 .release
= seq_release_private
,
6057 .poll
= mdstat_poll
,
6060 int register_md_personality(struct mdk_personality
*p
)
6062 spin_lock(&pers_lock
);
6063 list_add_tail(&p
->list
, &pers_list
);
6064 printk(KERN_INFO
"md: %s personality registered for level %d\n", p
->name
, p
->level
);
6065 spin_unlock(&pers_lock
);
6069 int unregister_md_personality(struct mdk_personality
*p
)
6071 printk(KERN_INFO
"md: %s personality unregistered\n", p
->name
);
6072 spin_lock(&pers_lock
);
6073 list_del_init(&p
->list
);
6074 spin_unlock(&pers_lock
);
6078 static int is_mddev_idle(mddev_t
*mddev
, int init
)
6086 rdev_for_each_rcu(rdev
, mddev
) {
6087 struct gendisk
*disk
= rdev
->bdev
->bd_contains
->bd_disk
;
6088 curr_events
= (int)part_stat_read(&disk
->part0
, sectors
[0]) +
6089 (int)part_stat_read(&disk
->part0
, sectors
[1]) -
6090 atomic_read(&disk
->sync_io
);
6091 /* sync IO will cause sync_io to increase before the disk_stats
6092 * as sync_io is counted when a request starts, and
6093 * disk_stats is counted when it completes.
6094 * So resync activity will cause curr_events to be smaller than
6095 * when there was no such activity.
6096 * non-sync IO will cause disk_stat to increase without
6097 * increasing sync_io so curr_events will (eventually)
6098 * be larger than it was before. Once it becomes
6099 * substantially larger, the test below will cause
6100 * the array to appear non-idle, and resync will slow
6102 * If there is a lot of outstanding resync activity when
6103 * we set last_event to curr_events, then all that activity
6104 * completing might cause the array to appear non-idle
6105 * and resync will be slowed down even though there might
6106 * not have been non-resync activity. This will only
6107 * happen once though. 'last_events' will soon reflect
6108 * the state where there is little or no outstanding
6109 * resync requests, and further resync activity will
6110 * always make curr_events less than last_events.
6113 if (init
|| curr_events
- rdev
->last_events
> 64) {
6114 rdev
->last_events
= curr_events
;
6122 void md_done_sync(mddev_t
*mddev
, int blocks
, int ok
)
6124 /* another "blocks" (512byte) blocks have been synced */
6125 atomic_sub(blocks
, &mddev
->recovery_active
);
6126 wake_up(&mddev
->recovery_wait
);
6128 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6129 md_wakeup_thread(mddev
->thread
);
6130 // stop recovery, signal do_sync ....
6135 /* md_write_start(mddev, bi)
6136 * If we need to update some array metadata (e.g. 'active' flag
6137 * in superblock) before writing, schedule a superblock update
6138 * and wait for it to complete.
6140 void md_write_start(mddev_t
*mddev
, struct bio
*bi
)
6143 if (bio_data_dir(bi
) != WRITE
)
6146 BUG_ON(mddev
->ro
== 1);
6147 if (mddev
->ro
== 2) {
6148 /* need to switch to read/write */
6150 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6151 md_wakeup_thread(mddev
->thread
);
6152 md_wakeup_thread(mddev
->sync_thread
);
6155 atomic_inc(&mddev
->writes_pending
);
6156 if (mddev
->safemode
== 1)
6157 mddev
->safemode
= 0;
6158 if (mddev
->in_sync
) {
6159 spin_lock_irq(&mddev
->write_lock
);
6160 if (mddev
->in_sync
) {
6162 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6163 md_wakeup_thread(mddev
->thread
);
6166 spin_unlock_irq(&mddev
->write_lock
);
6169 sysfs_notify_dirent(mddev
->sysfs_state
);
6170 wait_event(mddev
->sb_wait
,
6171 !test_bit(MD_CHANGE_CLEAN
, &mddev
->flags
) &&
6172 !test_bit(MD_CHANGE_PENDING
, &mddev
->flags
));
6175 void md_write_end(mddev_t
*mddev
)
6177 if (atomic_dec_and_test(&mddev
->writes_pending
)) {
6178 if (mddev
->safemode
== 2)
6179 md_wakeup_thread(mddev
->thread
);
6180 else if (mddev
->safemode_delay
)
6181 mod_timer(&mddev
->safemode_timer
, jiffies
+ mddev
->safemode_delay
);
6185 /* md_allow_write(mddev)
6186 * Calling this ensures that the array is marked 'active' so that writes
6187 * may proceed without blocking. It is important to call this before
6188 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6189 * Must be called with mddev_lock held.
6191 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6192 * is dropped, so return -EAGAIN after notifying userspace.
6194 int md_allow_write(mddev_t
*mddev
)
6200 if (!mddev
->pers
->sync_request
)
6203 spin_lock_irq(&mddev
->write_lock
);
6204 if (mddev
->in_sync
) {
6206 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6207 if (mddev
->safemode_delay
&&
6208 mddev
->safemode
== 0)
6209 mddev
->safemode
= 1;
6210 spin_unlock_irq(&mddev
->write_lock
);
6211 md_update_sb(mddev
, 0);
6212 sysfs_notify_dirent(mddev
->sysfs_state
);
6214 spin_unlock_irq(&mddev
->write_lock
);
6216 if (test_bit(MD_CHANGE_CLEAN
, &mddev
->flags
))
6221 EXPORT_SYMBOL_GPL(md_allow_write
);
6223 #define SYNC_MARKS 10
6224 #define SYNC_MARK_STEP (3*HZ)
6225 void md_do_sync(mddev_t
*mddev
)
6228 unsigned int currspeed
= 0,
6230 sector_t max_sectors
,j
, io_sectors
;
6231 unsigned long mark
[SYNC_MARKS
];
6232 sector_t mark_cnt
[SYNC_MARKS
];
6234 struct list_head
*tmp
;
6235 sector_t last_check
;
6240 /* just incase thread restarts... */
6241 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
6243 if (mddev
->ro
) /* never try to sync a read-only array */
6246 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
6247 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
6248 desc
= "data-check";
6249 else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
6250 desc
= "requested-resync";
6253 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
6258 /* we overload curr_resync somewhat here.
6259 * 0 == not engaged in resync at all
6260 * 2 == checking that there is no conflict with another sync
6261 * 1 == like 2, but have yielded to allow conflicting resync to
6263 * other == active in resync - this many blocks
6265 * Before starting a resync we must have set curr_resync to
6266 * 2, and then checked that every "conflicting" array has curr_resync
6267 * less than ours. When we find one that is the same or higher
6268 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6269 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6270 * This will mean we have to start checking from the beginning again.
6275 mddev
->curr_resync
= 2;
6278 if (kthread_should_stop()) {
6279 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6282 for_each_mddev(mddev2
, tmp
) {
6283 if (mddev2
== mddev
)
6285 if (!mddev
->parallel_resync
6286 && mddev2
->curr_resync
6287 && match_mddev_units(mddev
, mddev2
)) {
6289 if (mddev
< mddev2
&& mddev
->curr_resync
== 2) {
6290 /* arbitrarily yield */
6291 mddev
->curr_resync
= 1;
6292 wake_up(&resync_wait
);
6294 if (mddev
> mddev2
&& mddev
->curr_resync
== 1)
6295 /* no need to wait here, we can wait the next
6296 * time 'round when curr_resync == 2
6299 /* We need to wait 'interruptible' so as not to
6300 * contribute to the load average, and not to
6301 * be caught by 'softlockup'
6303 prepare_to_wait(&resync_wait
, &wq
, TASK_INTERRUPTIBLE
);
6304 if (!kthread_should_stop() &&
6305 mddev2
->curr_resync
>= mddev
->curr_resync
) {
6306 printk(KERN_INFO
"md: delaying %s of %s"
6307 " until %s has finished (they"
6308 " share one or more physical units)\n",
6309 desc
, mdname(mddev
), mdname(mddev2
));
6311 if (signal_pending(current
))
6312 flush_signals(current
);
6314 finish_wait(&resync_wait
, &wq
);
6317 finish_wait(&resync_wait
, &wq
);
6320 } while (mddev
->curr_resync
< 2);
6323 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
6324 /* resync follows the size requested by the personality,
6325 * which defaults to physical size, but can be virtual size
6327 max_sectors
= mddev
->resync_max_sectors
;
6328 mddev
->resync_mismatches
= 0;
6329 /* we don't use the checkpoint if there's a bitmap */
6330 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
6331 j
= mddev
->resync_min
;
6332 else if (!mddev
->bitmap
)
6333 j
= mddev
->recovery_cp
;
6335 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
6336 max_sectors
= mddev
->dev_sectors
;
6338 /* recovery follows the physical size of devices */
6339 max_sectors
= mddev
->dev_sectors
;
6341 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
6342 if (rdev
->raid_disk
>= 0 &&
6343 !test_bit(Faulty
, &rdev
->flags
) &&
6344 !test_bit(In_sync
, &rdev
->flags
) &&
6345 rdev
->recovery_offset
< j
)
6346 j
= rdev
->recovery_offset
;
6349 printk(KERN_INFO
"md: %s of RAID array %s\n", desc
, mdname(mddev
));
6350 printk(KERN_INFO
"md: minimum _guaranteed_ speed:"
6351 " %d KB/sec/disk.\n", speed_min(mddev
));
6352 printk(KERN_INFO
"md: using maximum available idle IO bandwidth "
6353 "(but not more than %d KB/sec) for %s.\n",
6354 speed_max(mddev
), desc
);
6356 is_mddev_idle(mddev
, 1); /* this initializes IO event counters */
6359 for (m
= 0; m
< SYNC_MARKS
; m
++) {
6361 mark_cnt
[m
] = io_sectors
;
6364 mddev
->resync_mark
= mark
[last_mark
];
6365 mddev
->resync_mark_cnt
= mark_cnt
[last_mark
];
6368 * Tune reconstruction:
6370 window
= 32*(PAGE_SIZE
/512);
6371 printk(KERN_INFO
"md: using %dk window, over a total of %llu blocks.\n",
6372 window
/2,(unsigned long long) max_sectors
/2);
6374 atomic_set(&mddev
->recovery_active
, 0);
6379 "md: resuming %s of %s from checkpoint.\n",
6380 desc
, mdname(mddev
));
6381 mddev
->curr_resync
= j
;
6384 while (j
< max_sectors
) {
6389 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
6390 ((mddev
->curr_resync
> mddev
->curr_resync_completed
&&
6391 (mddev
->curr_resync
- mddev
->curr_resync_completed
)
6392 > (max_sectors
>> 4)) ||
6393 (j
- mddev
->curr_resync_completed
)*2
6394 >= mddev
->resync_max
- mddev
->curr_resync_completed
6396 /* time to update curr_resync_completed */
6397 blk_unplug(mddev
->queue
);
6398 wait_event(mddev
->recovery_wait
,
6399 atomic_read(&mddev
->recovery_active
) == 0);
6400 mddev
->curr_resync_completed
=
6402 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6403 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
6406 while (j
>= mddev
->resync_max
&& !kthread_should_stop()) {
6407 /* As this condition is controlled by user-space,
6408 * we can block indefinitely, so use '_interruptible'
6409 * to avoid triggering warnings.
6411 flush_signals(current
); /* just in case */
6412 wait_event_interruptible(mddev
->recovery_wait
,
6413 mddev
->resync_max
> j
6414 || kthread_should_stop());
6417 if (kthread_should_stop())
6420 sectors
= mddev
->pers
->sync_request(mddev
, j
, &skipped
,
6421 currspeed
< speed_min(mddev
));
6423 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6427 if (!skipped
) { /* actual IO requested */
6428 io_sectors
+= sectors
;
6429 atomic_add(sectors
, &mddev
->recovery_active
);
6433 if (j
>1) mddev
->curr_resync
= j
;
6434 mddev
->curr_mark_cnt
= io_sectors
;
6435 if (last_check
== 0)
6436 /* this is the earliers that rebuilt will be
6437 * visible in /proc/mdstat
6439 md_new_event(mddev
);
6441 if (last_check
+ window
> io_sectors
|| j
== max_sectors
)
6444 last_check
= io_sectors
;
6446 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
6450 if (time_after_eq(jiffies
, mark
[last_mark
] + SYNC_MARK_STEP
)) {
6452 int next
= (last_mark
+1) % SYNC_MARKS
;
6454 mddev
->resync_mark
= mark
[next
];
6455 mddev
->resync_mark_cnt
= mark_cnt
[next
];
6456 mark
[next
] = jiffies
;
6457 mark_cnt
[next
] = io_sectors
- atomic_read(&mddev
->recovery_active
);
6462 if (kthread_should_stop())
6467 * this loop exits only if either when we are slower than
6468 * the 'hard' speed limit, or the system was IO-idle for
6470 * the system might be non-idle CPU-wise, but we only care
6471 * about not overloading the IO subsystem. (things like an
6472 * e2fsck being done on the RAID array should execute fast)
6474 blk_unplug(mddev
->queue
);
6477 currspeed
= ((unsigned long)(io_sectors
-mddev
->resync_mark_cnt
))/2
6478 /((jiffies
-mddev
->resync_mark
)/HZ
+1) +1;
6480 if (currspeed
> speed_min(mddev
)) {
6481 if ((currspeed
> speed_max(mddev
)) ||
6482 !is_mddev_idle(mddev
, 0)) {
6488 printk(KERN_INFO
"md: %s: %s done.\n",mdname(mddev
), desc
);
6490 * this also signals 'finished resyncing' to md_stop
6493 blk_unplug(mddev
->queue
);
6495 wait_event(mddev
->recovery_wait
, !atomic_read(&mddev
->recovery_active
));
6497 /* tell personality that we are finished */
6498 mddev
->pers
->sync_request(mddev
, max_sectors
, &skipped
, 1);
6500 if (!test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
) &&
6501 mddev
->curr_resync
> 2) {
6502 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
6503 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
6504 if (mddev
->curr_resync
>= mddev
->recovery_cp
) {
6506 "md: checkpointing %s of %s.\n",
6507 desc
, mdname(mddev
));
6508 mddev
->recovery_cp
= mddev
->curr_resync
;
6511 mddev
->recovery_cp
= MaxSector
;
6513 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
6514 mddev
->curr_resync
= MaxSector
;
6515 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
6516 if (rdev
->raid_disk
>= 0 &&
6517 !test_bit(Faulty
, &rdev
->flags
) &&
6518 !test_bit(In_sync
, &rdev
->flags
) &&
6519 rdev
->recovery_offset
< mddev
->curr_resync
)
6520 rdev
->recovery_offset
= mddev
->curr_resync
;
6523 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
6526 mddev
->curr_resync
= 0;
6527 mddev
->curr_resync_completed
= 0;
6528 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
6529 /* We completed so max setting can be forgotten. */
6530 mddev
->resync_max
= MaxSector
;
6531 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
6532 wake_up(&resync_wait
);
6533 set_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
6534 md_wakeup_thread(mddev
->thread
);
6539 * got a signal, exit.
6542 "md: md_do_sync() got signal ... exiting\n");
6543 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6547 EXPORT_SYMBOL_GPL(md_do_sync
);
6550 static int remove_and_add_spares(mddev_t
*mddev
)
6555 mddev
->curr_resync_completed
= 0;
6557 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
6558 if (rdev
->raid_disk
>= 0 &&
6559 !test_bit(Blocked
, &rdev
->flags
) &&
6560 (test_bit(Faulty
, &rdev
->flags
) ||
6561 ! test_bit(In_sync
, &rdev
->flags
)) &&
6562 atomic_read(&rdev
->nr_pending
)==0) {
6563 if (mddev
->pers
->hot_remove_disk(
6564 mddev
, rdev
->raid_disk
)==0) {
6566 sprintf(nm
,"rd%d", rdev
->raid_disk
);
6567 sysfs_remove_link(&mddev
->kobj
, nm
);
6568 rdev
->raid_disk
= -1;
6572 if (mddev
->degraded
&& ! mddev
->ro
&& !mddev
->recovery_disabled
) {
6573 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
6574 if (rdev
->raid_disk
>= 0 &&
6575 !test_bit(In_sync
, &rdev
->flags
) &&
6576 !test_bit(Blocked
, &rdev
->flags
))
6578 if (rdev
->raid_disk
< 0
6579 && !test_bit(Faulty
, &rdev
->flags
)) {
6580 rdev
->recovery_offset
= 0;
6582 hot_add_disk(mddev
, rdev
) == 0) {
6584 sprintf(nm
, "rd%d", rdev
->raid_disk
);
6585 if (sysfs_create_link(&mddev
->kobj
,
6588 "md: cannot register "
6592 md_new_event(mddev
);
6601 * This routine is regularly called by all per-raid-array threads to
6602 * deal with generic issues like resync and super-block update.
6603 * Raid personalities that don't have a thread (linear/raid0) do not
6604 * need this as they never do any recovery or update the superblock.
6606 * It does not do any resync itself, but rather "forks" off other threads
6607 * to do that as needed.
6608 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6609 * "->recovery" and create a thread at ->sync_thread.
6610 * When the thread finishes it sets MD_RECOVERY_DONE
6611 * and wakeups up this thread which will reap the thread and finish up.
6612 * This thread also removes any faulty devices (with nr_pending == 0).
6614 * The overall approach is:
6615 * 1/ if the superblock needs updating, update it.
6616 * 2/ If a recovery thread is running, don't do anything else.
6617 * 3/ If recovery has finished, clean up, possibly marking spares active.
6618 * 4/ If there are any faulty devices, remove them.
6619 * 5/ If array is degraded, try to add spares devices
6620 * 6/ If array has spares or is not in-sync, start a resync thread.
6622 void md_check_recovery(mddev_t
*mddev
)
6628 bitmap_daemon_work(mddev
->bitmap
);
6633 if (signal_pending(current
)) {
6634 if (mddev
->pers
->sync_request
&& !mddev
->external
) {
6635 printk(KERN_INFO
"md: %s in immediate safe mode\n",
6637 mddev
->safemode
= 2;
6639 flush_signals(current
);
6642 if (mddev
->ro
&& !test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
6645 (mddev
->flags
&& !mddev
->external
) ||
6646 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
6647 test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
6648 (mddev
->external
== 0 && mddev
->safemode
== 1) ||
6649 (mddev
->safemode
== 2 && ! atomic_read(&mddev
->writes_pending
)
6650 && !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
6654 if (mddev_trylock(mddev
)) {
6658 /* Only thing we do on a ro array is remove
6661 remove_and_add_spares(mddev
);
6662 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6666 if (!mddev
->external
) {
6668 spin_lock_irq(&mddev
->write_lock
);
6669 if (mddev
->safemode
&&
6670 !atomic_read(&mddev
->writes_pending
) &&
6672 mddev
->recovery_cp
== MaxSector
) {
6675 if (mddev
->persistent
)
6676 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6678 if (mddev
->safemode
== 1)
6679 mddev
->safemode
= 0;
6680 spin_unlock_irq(&mddev
->write_lock
);
6682 sysfs_notify_dirent(mddev
->sysfs_state
);
6686 md_update_sb(mddev
, 0);
6688 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
6689 if (test_and_clear_bit(StateChanged
, &rdev
->flags
))
6690 sysfs_notify_dirent(rdev
->sysfs_state
);
6693 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
6694 !test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
)) {
6695 /* resync/recovery still happening */
6696 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6699 if (mddev
->sync_thread
) {
6700 /* resync has finished, collect result */
6701 md_unregister_thread(mddev
->sync_thread
);
6702 mddev
->sync_thread
= NULL
;
6703 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
6704 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
6706 /* activate any spares */
6707 if (mddev
->pers
->spare_active(mddev
))
6708 sysfs_notify(&mddev
->kobj
, NULL
,
6711 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
6712 mddev
->pers
->finish_reshape
)
6713 mddev
->pers
->finish_reshape(mddev
);
6714 md_update_sb(mddev
, 1);
6716 /* if array is no-longer degraded, then any saved_raid_disk
6717 * information must be scrapped
6719 if (!mddev
->degraded
)
6720 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
6721 rdev
->saved_raid_disk
= -1;
6723 mddev
->recovery
= 0;
6724 /* flag recovery needed just to double check */
6725 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6726 sysfs_notify_dirent(mddev
->sysfs_action
);
6727 md_new_event(mddev
);
6730 /* Set RUNNING before clearing NEEDED to avoid
6731 * any transients in the value of "sync_action".
6733 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
6734 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6735 /* Clear some bits that don't mean anything, but
6738 clear_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6739 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
6741 if (test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
6743 /* no recovery is running.
6744 * remove any failed drives, then
6745 * add spares if possible.
6746 * Spare are also removed and re-added, to allow
6747 * the personality to fail the re-add.
6750 if (mddev
->reshape_position
!= MaxSector
) {
6751 if (mddev
->pers
->check_reshape
== NULL
||
6752 mddev
->pers
->check_reshape(mddev
) != 0)
6753 /* Cannot proceed */
6755 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
6756 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
6757 } else if ((spares
= remove_and_add_spares(mddev
))) {
6758 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
6759 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
6760 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
6761 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
6762 } else if (mddev
->recovery_cp
< MaxSector
) {
6763 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
6764 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
6765 } else if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
6766 /* nothing to be done ... */
6769 if (mddev
->pers
->sync_request
) {
6770 if (spares
&& mddev
->bitmap
&& ! mddev
->bitmap
->file
) {
6771 /* We are adding a device or devices to an array
6772 * which has the bitmap stored on all devices.
6773 * So make sure all bitmap pages get written
6775 bitmap_write_all(mddev
->bitmap
);
6777 mddev
->sync_thread
= md_register_thread(md_do_sync
,
6780 if (!mddev
->sync_thread
) {
6781 printk(KERN_ERR
"%s: could not start resync"
6784 /* leave the spares where they are, it shouldn't hurt */
6785 mddev
->recovery
= 0;
6787 md_wakeup_thread(mddev
->sync_thread
);
6788 sysfs_notify_dirent(mddev
->sysfs_action
);
6789 md_new_event(mddev
);
6792 if (!mddev
->sync_thread
) {
6793 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
6794 if (test_and_clear_bit(MD_RECOVERY_RECOVER
,
6796 if (mddev
->sysfs_action
)
6797 sysfs_notify_dirent(mddev
->sysfs_action
);
6799 mddev_unlock(mddev
);
6803 void md_wait_for_blocked_rdev(mdk_rdev_t
*rdev
, mddev_t
*mddev
)
6805 sysfs_notify_dirent(rdev
->sysfs_state
);
6806 wait_event_timeout(rdev
->blocked_wait
,
6807 !test_bit(Blocked
, &rdev
->flags
),
6808 msecs_to_jiffies(5000));
6809 rdev_dec_pending(rdev
, mddev
);
6811 EXPORT_SYMBOL(md_wait_for_blocked_rdev
);
6813 static int md_notify_reboot(struct notifier_block
*this,
6814 unsigned long code
, void *x
)
6816 struct list_head
*tmp
;
6819 if ((code
== SYS_DOWN
) || (code
== SYS_HALT
) || (code
== SYS_POWER_OFF
)) {
6821 printk(KERN_INFO
"md: stopping all md devices.\n");
6823 for_each_mddev(mddev
, tmp
)
6824 if (mddev_trylock(mddev
)) {
6825 /* Force a switch to readonly even array
6826 * appears to still be in use. Hence
6829 do_md_stop(mddev
, 1, 100);
6830 mddev_unlock(mddev
);
6833 * certain more exotic SCSI devices are known to be
6834 * volatile wrt too early system reboots. While the
6835 * right place to handle this issue is the given
6836 * driver, we do want to have a safe RAID driver ...
6843 static struct notifier_block md_notifier
= {
6844 .notifier_call
= md_notify_reboot
,
6846 .priority
= INT_MAX
, /* before any real devices */
6849 static void md_geninit(void)
6851 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t
));
6853 proc_create("mdstat", S_IRUGO
, NULL
, &md_seq_fops
);
6856 static int __init
md_init(void)
6858 if (register_blkdev(MD_MAJOR
, "md"))
6860 if ((mdp_major
=register_blkdev(0, "mdp"))<=0) {
6861 unregister_blkdev(MD_MAJOR
, "md");
6864 blk_register_region(MKDEV(MD_MAJOR
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
6865 md_probe
, NULL
, NULL
);
6866 blk_register_region(MKDEV(mdp_major
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
6867 md_probe
, NULL
, NULL
);
6869 register_reboot_notifier(&md_notifier
);
6870 raid_table_header
= register_sysctl_table(raid_root_table
);
6880 * Searches all registered partitions for autorun RAID arrays
6884 static LIST_HEAD(all_detected_devices
);
6885 struct detected_devices_node
{
6886 struct list_head list
;
6890 void md_autodetect_dev(dev_t dev
)
6892 struct detected_devices_node
*node_detected_dev
;
6894 node_detected_dev
= kzalloc(sizeof(*node_detected_dev
), GFP_KERNEL
);
6895 if (node_detected_dev
) {
6896 node_detected_dev
->dev
= dev
;
6897 list_add_tail(&node_detected_dev
->list
, &all_detected_devices
);
6899 printk(KERN_CRIT
"md: md_autodetect_dev: kzalloc failed"
6900 ", skipping dev(%d,%d)\n", MAJOR(dev
), MINOR(dev
));
6905 static void autostart_arrays(int part
)
6908 struct detected_devices_node
*node_detected_dev
;
6910 int i_scanned
, i_passed
;
6915 printk(KERN_INFO
"md: Autodetecting RAID arrays.\n");
6917 while (!list_empty(&all_detected_devices
) && i_scanned
< INT_MAX
) {
6919 node_detected_dev
= list_entry(all_detected_devices
.next
,
6920 struct detected_devices_node
, list
);
6921 list_del(&node_detected_dev
->list
);
6922 dev
= node_detected_dev
->dev
;
6923 kfree(node_detected_dev
);
6924 rdev
= md_import_device(dev
,0, 90);
6928 if (test_bit(Faulty
, &rdev
->flags
)) {
6932 set_bit(AutoDetected
, &rdev
->flags
);
6933 list_add(&rdev
->same_set
, &pending_raid_disks
);
6937 printk(KERN_INFO
"md: Scanned %d and added %d devices.\n",
6938 i_scanned
, i_passed
);
6940 autorun_devices(part
);
6943 #endif /* !MODULE */
6945 static __exit
void md_exit(void)
6948 struct list_head
*tmp
;
6950 blk_unregister_region(MKDEV(MD_MAJOR
,0), 1U << MINORBITS
);
6951 blk_unregister_region(MKDEV(mdp_major
,0), 1U << MINORBITS
);
6953 unregister_blkdev(MD_MAJOR
,"md");
6954 unregister_blkdev(mdp_major
, "mdp");
6955 unregister_reboot_notifier(&md_notifier
);
6956 unregister_sysctl_table(raid_table_header
);
6957 remove_proc_entry("mdstat", NULL
);
6958 for_each_mddev(mddev
, tmp
) {
6959 export_array(mddev
);
6960 mddev
->hold_active
= 0;
6964 subsys_initcall(md_init
);
6965 module_exit(md_exit
)
6967 static int get_ro(char *buffer
, struct kernel_param
*kp
)
6969 return sprintf(buffer
, "%d", start_readonly
);
6971 static int set_ro(const char *val
, struct kernel_param
*kp
)
6974 int num
= simple_strtoul(val
, &e
, 10);
6975 if (*val
&& (*e
== '\0' || *e
== '\n')) {
6976 start_readonly
= num
;
6982 module_param_call(start_ro
, set_ro
, get_ro
, NULL
, S_IRUSR
|S_IWUSR
);
6983 module_param(start_dirty_degraded
, int, S_IRUGO
|S_IWUSR
);
6985 module_param_call(new_array
, add_named_array
, NULL
, NULL
, S_IWUSR
);
6987 EXPORT_SYMBOL(register_md_personality
);
6988 EXPORT_SYMBOL(unregister_md_personality
);
6989 EXPORT_SYMBOL(md_error
);
6990 EXPORT_SYMBOL(md_done_sync
);
6991 EXPORT_SYMBOL(md_write_start
);
6992 EXPORT_SYMBOL(md_write_end
);
6993 EXPORT_SYMBOL(md_register_thread
);
6994 EXPORT_SYMBOL(md_unregister_thread
);
6995 EXPORT_SYMBOL(md_wakeup_thread
);
6996 EXPORT_SYMBOL(md_check_recovery
);
6997 MODULE_LICENSE("GPL");
6999 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR
);