2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
5 completely rewritten, based on the MD driver code from Marc Zyngier
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
20 Neil Brown <neilb@cse.unsw.edu.au>.
22 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
25 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 #include <linux/kthread.h>
36 #include <linux/blkdev.h>
37 #include <linux/sysctl.h>
38 #include <linux/seq_file.h>
39 #include <linux/buffer_head.h> /* for invalidate_bdev */
40 #include <linux/poll.h>
41 #include <linux/ctype.h>
42 #include <linux/hdreg.h>
43 #include <linux/proc_fs.h>
44 #include <linux/random.h>
45 #include <linux/reboot.h>
46 #include <linux/file.h>
47 #include <linux/delay.h>
48 #include <linux/raid/md_p.h>
49 #include <linux/raid/md_u.h>
54 #define dprintk(x...) ((void)(DEBUG && printk(x)))
58 static void autostart_arrays(int part
);
61 static LIST_HEAD(pers_list
);
62 static DEFINE_SPINLOCK(pers_lock
);
64 static void md_print_devices(void);
66 static DECLARE_WAIT_QUEUE_HEAD(resync_wait
);
68 #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
71 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
72 * is 1000 KB/sec, so the extra system load does not show up that much.
73 * Increase it if you want to have more _guaranteed_ speed. Note that
74 * the RAID driver will use the maximum available bandwidth if the IO
75 * subsystem is idle. There is also an 'absolute maximum' reconstruction
76 * speed limit - in case reconstruction slows down your system despite
79 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
80 * or /sys/block/mdX/md/sync_speed_{min,max}
83 static int sysctl_speed_limit_min
= 1000;
84 static int sysctl_speed_limit_max
= 200000;
85 static inline int speed_min(mddev_t
*mddev
)
87 return mddev
->sync_speed_min
?
88 mddev
->sync_speed_min
: sysctl_speed_limit_min
;
91 static inline int speed_max(mddev_t
*mddev
)
93 return mddev
->sync_speed_max
?
94 mddev
->sync_speed_max
: sysctl_speed_limit_max
;
97 static struct ctl_table_header
*raid_table_header
;
99 static ctl_table raid_table
[] = {
101 .ctl_name
= DEV_RAID_SPEED_LIMIT_MIN
,
102 .procname
= "speed_limit_min",
103 .data
= &sysctl_speed_limit_min
,
104 .maxlen
= sizeof(int),
105 .mode
= S_IRUGO
|S_IWUSR
,
106 .proc_handler
= &proc_dointvec
,
109 .ctl_name
= DEV_RAID_SPEED_LIMIT_MAX
,
110 .procname
= "speed_limit_max",
111 .data
= &sysctl_speed_limit_max
,
112 .maxlen
= sizeof(int),
113 .mode
= S_IRUGO
|S_IWUSR
,
114 .proc_handler
= &proc_dointvec
,
119 static ctl_table raid_dir_table
[] = {
121 .ctl_name
= DEV_RAID
,
124 .mode
= S_IRUGO
|S_IXUGO
,
130 static ctl_table raid_root_table
[] = {
136 .child
= raid_dir_table
,
141 static const struct block_device_operations md_fops
;
143 static int start_readonly
;
146 * We have a system wide 'event count' that is incremented
147 * on any 'interesting' event, and readers of /proc/mdstat
148 * can use 'poll' or 'select' to find out when the event
152 * start array, stop array, error, add device, remove device,
153 * start build, activate spare
155 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters
);
156 static atomic_t md_event_count
;
157 void md_new_event(mddev_t
*mddev
)
159 atomic_inc(&md_event_count
);
160 wake_up(&md_event_waiters
);
162 EXPORT_SYMBOL_GPL(md_new_event
);
164 /* Alternate version that can be called from interrupts
165 * when calling sysfs_notify isn't needed.
167 static void md_new_event_inintr(mddev_t
*mddev
)
169 atomic_inc(&md_event_count
);
170 wake_up(&md_event_waiters
);
174 * Enables to iterate over all existing md arrays
175 * all_mddevs_lock protects this list.
177 static LIST_HEAD(all_mddevs
);
178 static DEFINE_SPINLOCK(all_mddevs_lock
);
182 * iterates through all used mddevs in the system.
183 * We take care to grab the all_mddevs_lock whenever navigating
184 * the list, and to always hold a refcount when unlocked.
185 * Any code which breaks out of this loop while own
186 * a reference to the current mddev and must mddev_put it.
188 #define for_each_mddev(mddev,tmp) \
190 for (({ spin_lock(&all_mddevs_lock); \
191 tmp = all_mddevs.next; \
193 ({ if (tmp != &all_mddevs) \
194 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
195 spin_unlock(&all_mddevs_lock); \
196 if (mddev) mddev_put(mddev); \
197 mddev = list_entry(tmp, mddev_t, all_mddevs); \
198 tmp != &all_mddevs;}); \
199 ({ spin_lock(&all_mddevs_lock); \
204 /* Rather than calling directly into the personality make_request function,
205 * IO requests come here first so that we can check if the device is
206 * being suspended pending a reconfiguration.
207 * We hold a refcount over the call to ->make_request. By the time that
208 * call has finished, the bio has been linked into some internal structure
209 * and so is visible to ->quiesce(), so we don't need the refcount any more.
211 static int md_make_request(struct request_queue
*q
, struct bio
*bio
)
213 mddev_t
*mddev
= q
->queuedata
;
215 if (mddev
== NULL
|| mddev
->pers
== NULL
) {
220 if (mddev
->suspended
) {
223 prepare_to_wait(&mddev
->sb_wait
, &__wait
,
224 TASK_UNINTERRUPTIBLE
);
225 if (!mddev
->suspended
)
231 finish_wait(&mddev
->sb_wait
, &__wait
);
233 atomic_inc(&mddev
->active_io
);
235 rv
= mddev
->pers
->make_request(q
, bio
);
236 if (atomic_dec_and_test(&mddev
->active_io
) && mddev
->suspended
)
237 wake_up(&mddev
->sb_wait
);
242 static void mddev_suspend(mddev_t
*mddev
)
244 BUG_ON(mddev
->suspended
);
245 mddev
->suspended
= 1;
247 wait_event(mddev
->sb_wait
, atomic_read(&mddev
->active_io
) == 0);
248 mddev
->pers
->quiesce(mddev
, 1);
249 md_unregister_thread(mddev
->thread
);
250 mddev
->thread
= NULL
;
251 /* we now know that no code is executing in the personality module,
252 * except possibly the tail end of a ->bi_end_io function, but that
253 * is certain to complete before the module has a chance to get
258 static void mddev_resume(mddev_t
*mddev
)
260 mddev
->suspended
= 0;
261 wake_up(&mddev
->sb_wait
);
262 mddev
->pers
->quiesce(mddev
, 0);
265 int mddev_congested(mddev_t
*mddev
, int bits
)
267 return mddev
->suspended
;
269 EXPORT_SYMBOL(mddev_congested
);
272 static inline mddev_t
*mddev_get(mddev_t
*mddev
)
274 atomic_inc(&mddev
->active
);
278 static void mddev_delayed_delete(struct work_struct
*ws
);
280 static void mddev_put(mddev_t
*mddev
)
282 if (!atomic_dec_and_lock(&mddev
->active
, &all_mddevs_lock
))
284 if (!mddev
->raid_disks
&& list_empty(&mddev
->disks
) &&
285 !mddev
->hold_active
) {
286 list_del(&mddev
->all_mddevs
);
287 if (mddev
->gendisk
) {
288 /* we did a probe so need to clean up.
289 * Call schedule_work inside the spinlock
290 * so that flush_scheduled_work() after
291 * mddev_find will succeed in waiting for the
294 INIT_WORK(&mddev
->del_work
, mddev_delayed_delete
);
295 schedule_work(&mddev
->del_work
);
299 spin_unlock(&all_mddevs_lock
);
302 static mddev_t
* mddev_find(dev_t unit
)
304 mddev_t
*mddev
, *new = NULL
;
307 spin_lock(&all_mddevs_lock
);
310 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
311 if (mddev
->unit
== unit
) {
313 spin_unlock(&all_mddevs_lock
);
319 list_add(&new->all_mddevs
, &all_mddevs
);
320 spin_unlock(&all_mddevs_lock
);
321 new->hold_active
= UNTIL_IOCTL
;
325 /* find an unused unit number */
326 static int next_minor
= 512;
327 int start
= next_minor
;
331 dev
= MKDEV(MD_MAJOR
, next_minor
);
333 if (next_minor
> MINORMASK
)
335 if (next_minor
== start
) {
336 /* Oh dear, all in use. */
337 spin_unlock(&all_mddevs_lock
);
343 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
344 if (mddev
->unit
== dev
) {
350 new->md_minor
= MINOR(dev
);
351 new->hold_active
= UNTIL_STOP
;
352 list_add(&new->all_mddevs
, &all_mddevs
);
353 spin_unlock(&all_mddevs_lock
);
356 spin_unlock(&all_mddevs_lock
);
358 new = kzalloc(sizeof(*new), GFP_KERNEL
);
363 if (MAJOR(unit
) == MD_MAJOR
)
364 new->md_minor
= MINOR(unit
);
366 new->md_minor
= MINOR(unit
) >> MdpMinorShift
;
368 mutex_init(&new->open_mutex
);
369 mutex_init(&new->reconfig_mutex
);
370 INIT_LIST_HEAD(&new->disks
);
371 INIT_LIST_HEAD(&new->all_mddevs
);
372 init_timer(&new->safemode_timer
);
373 atomic_set(&new->active
, 1);
374 atomic_set(&new->openers
, 0);
375 atomic_set(&new->active_io
, 0);
376 spin_lock_init(&new->write_lock
);
377 init_waitqueue_head(&new->sb_wait
);
378 init_waitqueue_head(&new->recovery_wait
);
379 new->reshape_position
= MaxSector
;
381 new->resync_max
= MaxSector
;
382 new->level
= LEVEL_NONE
;
387 static inline int mddev_lock(mddev_t
* mddev
)
389 return mutex_lock_interruptible(&mddev
->reconfig_mutex
);
392 static inline int mddev_is_locked(mddev_t
*mddev
)
394 return mutex_is_locked(&mddev
->reconfig_mutex
);
397 static inline int mddev_trylock(mddev_t
* mddev
)
399 return mutex_trylock(&mddev
->reconfig_mutex
);
402 static inline void mddev_unlock(mddev_t
* mddev
)
404 mutex_unlock(&mddev
->reconfig_mutex
);
406 md_wakeup_thread(mddev
->thread
);
409 static mdk_rdev_t
* find_rdev_nr(mddev_t
*mddev
, int nr
)
413 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
414 if (rdev
->desc_nr
== nr
)
420 static mdk_rdev_t
* find_rdev(mddev_t
* mddev
, dev_t dev
)
424 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
425 if (rdev
->bdev
->bd_dev
== dev
)
431 static struct mdk_personality
*find_pers(int level
, char *clevel
)
433 struct mdk_personality
*pers
;
434 list_for_each_entry(pers
, &pers_list
, list
) {
435 if (level
!= LEVEL_NONE
&& pers
->level
== level
)
437 if (strcmp(pers
->name
, clevel
)==0)
443 /* return the offset of the super block in 512byte sectors */
444 static inline sector_t
calc_dev_sboffset(struct block_device
*bdev
)
446 sector_t num_sectors
= bdev
->bd_inode
->i_size
/ 512;
447 return MD_NEW_SIZE_SECTORS(num_sectors
);
450 static int alloc_disk_sb(mdk_rdev_t
* rdev
)
455 rdev
->sb_page
= alloc_page(GFP_KERNEL
);
456 if (!rdev
->sb_page
) {
457 printk(KERN_ALERT
"md: out of memory.\n");
464 static void free_disk_sb(mdk_rdev_t
* rdev
)
467 put_page(rdev
->sb_page
);
469 rdev
->sb_page
= NULL
;
476 static void super_written(struct bio
*bio
, int error
)
478 mdk_rdev_t
*rdev
= bio
->bi_private
;
479 mddev_t
*mddev
= rdev
->mddev
;
481 if (error
|| !test_bit(BIO_UPTODATE
, &bio
->bi_flags
)) {
482 printk("md: super_written gets error=%d, uptodate=%d\n",
483 error
, test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
484 WARN_ON(test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
485 md_error(mddev
, rdev
);
488 if (atomic_dec_and_test(&mddev
->pending_writes
))
489 wake_up(&mddev
->sb_wait
);
493 static void super_written_barrier(struct bio
*bio
, int error
)
495 struct bio
*bio2
= bio
->bi_private
;
496 mdk_rdev_t
*rdev
= bio2
->bi_private
;
497 mddev_t
*mddev
= rdev
->mddev
;
499 if (!test_bit(BIO_UPTODATE
, &bio
->bi_flags
) &&
500 error
== -EOPNOTSUPP
) {
502 /* barriers don't appear to be supported :-( */
503 set_bit(BarriersNotsupp
, &rdev
->flags
);
504 mddev
->barriers_work
= 0;
505 spin_lock_irqsave(&mddev
->write_lock
, flags
);
506 bio2
->bi_next
= mddev
->biolist
;
507 mddev
->biolist
= bio2
;
508 spin_unlock_irqrestore(&mddev
->write_lock
, flags
);
509 wake_up(&mddev
->sb_wait
);
513 bio
->bi_private
= rdev
;
514 super_written(bio
, error
);
518 void md_super_write(mddev_t
*mddev
, mdk_rdev_t
*rdev
,
519 sector_t sector
, int size
, struct page
*page
)
521 /* write first size bytes of page to sector of rdev
522 * Increment mddev->pending_writes before returning
523 * and decrement it on completion, waking up sb_wait
524 * if zero is reached.
525 * If an error occurred, call md_error
527 * As we might need to resubmit the request if BIO_RW_BARRIER
528 * causes ENOTSUPP, we allocate a spare bio...
530 struct bio
*bio
= bio_alloc(GFP_NOIO
, 1);
531 int rw
= (1<<BIO_RW
) | (1<<BIO_RW_SYNCIO
) | (1<<BIO_RW_UNPLUG
);
533 bio
->bi_bdev
= rdev
->bdev
;
534 bio
->bi_sector
= sector
;
535 bio_add_page(bio
, page
, size
, 0);
536 bio
->bi_private
= rdev
;
537 bio
->bi_end_io
= super_written
;
540 atomic_inc(&mddev
->pending_writes
);
541 if (!test_bit(BarriersNotsupp
, &rdev
->flags
)) {
543 rw
|= (1<<BIO_RW_BARRIER
);
544 rbio
= bio_clone(bio
, GFP_NOIO
);
545 rbio
->bi_private
= bio
;
546 rbio
->bi_end_io
= super_written_barrier
;
547 submit_bio(rw
, rbio
);
552 void md_super_wait(mddev_t
*mddev
)
554 /* wait for all superblock writes that were scheduled to complete.
555 * if any had to be retried (due to BARRIER problems), retry them
559 prepare_to_wait(&mddev
->sb_wait
, &wq
, TASK_UNINTERRUPTIBLE
);
560 if (atomic_read(&mddev
->pending_writes
)==0)
562 while (mddev
->biolist
) {
564 spin_lock_irq(&mddev
->write_lock
);
565 bio
= mddev
->biolist
;
566 mddev
->biolist
= bio
->bi_next
;
568 spin_unlock_irq(&mddev
->write_lock
);
569 submit_bio(bio
->bi_rw
, bio
);
573 finish_wait(&mddev
->sb_wait
, &wq
);
576 static void bi_complete(struct bio
*bio
, int error
)
578 complete((struct completion
*)bio
->bi_private
);
581 int sync_page_io(struct block_device
*bdev
, sector_t sector
, int size
,
582 struct page
*page
, int rw
)
584 struct bio
*bio
= bio_alloc(GFP_NOIO
, 1);
585 struct completion event
;
588 rw
|= (1 << BIO_RW_SYNCIO
) | (1 << BIO_RW_UNPLUG
);
591 bio
->bi_sector
= sector
;
592 bio_add_page(bio
, page
, size
, 0);
593 init_completion(&event
);
594 bio
->bi_private
= &event
;
595 bio
->bi_end_io
= bi_complete
;
597 wait_for_completion(&event
);
599 ret
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
603 EXPORT_SYMBOL_GPL(sync_page_io
);
605 static int read_disk_sb(mdk_rdev_t
* rdev
, int size
)
607 char b
[BDEVNAME_SIZE
];
608 if (!rdev
->sb_page
) {
616 if (!sync_page_io(rdev
->bdev
, rdev
->sb_start
, size
, rdev
->sb_page
, READ
))
622 printk(KERN_WARNING
"md: disabled device %s, could not read superblock.\n",
623 bdevname(rdev
->bdev
,b
));
627 static int uuid_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
629 return sb1
->set_uuid0
== sb2
->set_uuid0
&&
630 sb1
->set_uuid1
== sb2
->set_uuid1
&&
631 sb1
->set_uuid2
== sb2
->set_uuid2
&&
632 sb1
->set_uuid3
== sb2
->set_uuid3
;
635 static int sb_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
638 mdp_super_t
*tmp1
, *tmp2
;
640 tmp1
= kmalloc(sizeof(*tmp1
),GFP_KERNEL
);
641 tmp2
= kmalloc(sizeof(*tmp2
),GFP_KERNEL
);
643 if (!tmp1
|| !tmp2
) {
645 printk(KERN_INFO
"md.c sb_equal(): failed to allocate memory!\n");
653 * nr_disks is not constant
658 ret
= (memcmp(tmp1
, tmp2
, MD_SB_GENERIC_CONSTANT_WORDS
* 4) == 0);
666 static u32
md_csum_fold(u32 csum
)
668 csum
= (csum
& 0xffff) + (csum
>> 16);
669 return (csum
& 0xffff) + (csum
>> 16);
672 static unsigned int calc_sb_csum(mdp_super_t
* sb
)
675 u32
*sb32
= (u32
*)sb
;
677 unsigned int disk_csum
, csum
;
679 disk_csum
= sb
->sb_csum
;
682 for (i
= 0; i
< MD_SB_BYTES
/4 ; i
++)
684 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
688 /* This used to use csum_partial, which was wrong for several
689 * reasons including that different results are returned on
690 * different architectures. It isn't critical that we get exactly
691 * the same return value as before (we always csum_fold before
692 * testing, and that removes any differences). However as we
693 * know that csum_partial always returned a 16bit value on
694 * alphas, do a fold to maximise conformity to previous behaviour.
696 sb
->sb_csum
= md_csum_fold(disk_csum
);
698 sb
->sb_csum
= disk_csum
;
705 * Handle superblock details.
706 * We want to be able to handle multiple superblock formats
707 * so we have a common interface to them all, and an array of
708 * different handlers.
709 * We rely on user-space to write the initial superblock, and support
710 * reading and updating of superblocks.
711 * Interface methods are:
712 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
713 * loads and validates a superblock on dev.
714 * if refdev != NULL, compare superblocks on both devices
716 * 0 - dev has a superblock that is compatible with refdev
717 * 1 - dev has a superblock that is compatible and newer than refdev
718 * so dev should be used as the refdev in future
719 * -EINVAL superblock incompatible or invalid
720 * -othererror e.g. -EIO
722 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
723 * Verify that dev is acceptable into mddev.
724 * The first time, mddev->raid_disks will be 0, and data from
725 * dev should be merged in. Subsequent calls check that dev
726 * is new enough. Return 0 or -EINVAL
728 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
729 * Update the superblock for rdev with data in mddev
730 * This does not write to disc.
736 struct module
*owner
;
737 int (*load_super
)(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
,
739 int (*validate_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
740 void (*sync_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
741 unsigned long long (*rdev_size_change
)(mdk_rdev_t
*rdev
,
742 sector_t num_sectors
);
746 * Check that the given mddev has no bitmap.
748 * This function is called from the run method of all personalities that do not
749 * support bitmaps. It prints an error message and returns non-zero if mddev
750 * has a bitmap. Otherwise, it returns 0.
753 int md_check_no_bitmap(mddev_t
*mddev
)
755 if (!mddev
->bitmap_file
&& !mddev
->bitmap_offset
)
757 printk(KERN_ERR
"%s: bitmaps are not supported for %s\n",
758 mdname(mddev
), mddev
->pers
->name
);
761 EXPORT_SYMBOL(md_check_no_bitmap
);
764 * load_super for 0.90.0
766 static int super_90_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
768 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
773 * Calculate the position of the superblock (512byte sectors),
774 * it's at the end of the disk.
776 * It also happens to be a multiple of 4Kb.
778 rdev
->sb_start
= calc_dev_sboffset(rdev
->bdev
);
780 ret
= read_disk_sb(rdev
, MD_SB_BYTES
);
785 bdevname(rdev
->bdev
, b
);
786 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
788 if (sb
->md_magic
!= MD_SB_MAGIC
) {
789 printk(KERN_ERR
"md: invalid raid superblock magic on %s\n",
794 if (sb
->major_version
!= 0 ||
795 sb
->minor_version
< 90 ||
796 sb
->minor_version
> 91) {
797 printk(KERN_WARNING
"Bad version number %d.%d on %s\n",
798 sb
->major_version
, sb
->minor_version
,
803 if (sb
->raid_disks
<= 0)
806 if (md_csum_fold(calc_sb_csum(sb
)) != md_csum_fold(sb
->sb_csum
)) {
807 printk(KERN_WARNING
"md: invalid superblock checksum on %s\n",
812 rdev
->preferred_minor
= sb
->md_minor
;
813 rdev
->data_offset
= 0;
814 rdev
->sb_size
= MD_SB_BYTES
;
816 if (sb
->level
== LEVEL_MULTIPATH
)
819 rdev
->desc_nr
= sb
->this_disk
.number
;
825 mdp_super_t
*refsb
= (mdp_super_t
*)page_address(refdev
->sb_page
);
826 if (!uuid_equal(refsb
, sb
)) {
827 printk(KERN_WARNING
"md: %s has different UUID to %s\n",
828 b
, bdevname(refdev
->bdev
,b2
));
831 if (!sb_equal(refsb
, sb
)) {
832 printk(KERN_WARNING
"md: %s has same UUID"
833 " but different superblock to %s\n",
834 b
, bdevname(refdev
->bdev
, b2
));
838 ev2
= md_event(refsb
);
844 rdev
->sectors
= rdev
->sb_start
;
846 if (rdev
->sectors
< sb
->size
* 2 && sb
->level
> 1)
847 /* "this cannot possibly happen" ... */
855 * validate_super for 0.90.0
857 static int super_90_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
860 mdp_super_t
*sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
861 __u64 ev1
= md_event(sb
);
863 rdev
->raid_disk
= -1;
864 clear_bit(Faulty
, &rdev
->flags
);
865 clear_bit(In_sync
, &rdev
->flags
);
866 clear_bit(WriteMostly
, &rdev
->flags
);
867 clear_bit(BarriersNotsupp
, &rdev
->flags
);
869 if (mddev
->raid_disks
== 0) {
870 mddev
->major_version
= 0;
871 mddev
->minor_version
= sb
->minor_version
;
872 mddev
->patch_version
= sb
->patch_version
;
874 mddev
->chunk_sectors
= sb
->chunk_size
>> 9;
875 mddev
->ctime
= sb
->ctime
;
876 mddev
->utime
= sb
->utime
;
877 mddev
->level
= sb
->level
;
878 mddev
->clevel
[0] = 0;
879 mddev
->layout
= sb
->layout
;
880 mddev
->raid_disks
= sb
->raid_disks
;
881 mddev
->dev_sectors
= sb
->size
* 2;
883 mddev
->bitmap_offset
= 0;
884 mddev
->default_bitmap_offset
= MD_SB_BYTES
>> 9;
886 if (mddev
->minor_version
>= 91) {
887 mddev
->reshape_position
= sb
->reshape_position
;
888 mddev
->delta_disks
= sb
->delta_disks
;
889 mddev
->new_level
= sb
->new_level
;
890 mddev
->new_layout
= sb
->new_layout
;
891 mddev
->new_chunk_sectors
= sb
->new_chunk
>> 9;
893 mddev
->reshape_position
= MaxSector
;
894 mddev
->delta_disks
= 0;
895 mddev
->new_level
= mddev
->level
;
896 mddev
->new_layout
= mddev
->layout
;
897 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
900 if (sb
->state
& (1<<MD_SB_CLEAN
))
901 mddev
->recovery_cp
= MaxSector
;
903 if (sb
->events_hi
== sb
->cp_events_hi
&&
904 sb
->events_lo
== sb
->cp_events_lo
) {
905 mddev
->recovery_cp
= sb
->recovery_cp
;
907 mddev
->recovery_cp
= 0;
910 memcpy(mddev
->uuid
+0, &sb
->set_uuid0
, 4);
911 memcpy(mddev
->uuid
+4, &sb
->set_uuid1
, 4);
912 memcpy(mddev
->uuid
+8, &sb
->set_uuid2
, 4);
913 memcpy(mddev
->uuid
+12,&sb
->set_uuid3
, 4);
915 mddev
->max_disks
= MD_SB_DISKS
;
917 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
) &&
918 mddev
->bitmap_file
== NULL
)
919 mddev
->bitmap_offset
= mddev
->default_bitmap_offset
;
921 } else if (mddev
->pers
== NULL
) {
922 /* Insist on good event counter while assembling */
924 if (ev1
< mddev
->events
)
926 } else if (mddev
->bitmap
) {
927 /* if adding to array with a bitmap, then we can accept an
928 * older device ... but not too old.
930 if (ev1
< mddev
->bitmap
->events_cleared
)
933 if (ev1
< mddev
->events
)
934 /* just a hot-add of a new device, leave raid_disk at -1 */
938 if (mddev
->level
!= LEVEL_MULTIPATH
) {
939 desc
= sb
->disks
+ rdev
->desc_nr
;
941 if (desc
->state
& (1<<MD_DISK_FAULTY
))
942 set_bit(Faulty
, &rdev
->flags
);
943 else if (desc
->state
& (1<<MD_DISK_SYNC
) /* &&
944 desc->raid_disk < mddev->raid_disks */) {
945 set_bit(In_sync
, &rdev
->flags
);
946 rdev
->raid_disk
= desc
->raid_disk
;
948 if (desc
->state
& (1<<MD_DISK_WRITEMOSTLY
))
949 set_bit(WriteMostly
, &rdev
->flags
);
950 } else /* MULTIPATH are always insync */
951 set_bit(In_sync
, &rdev
->flags
);
956 * sync_super for 0.90.0
958 static void super_90_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
962 int next_spare
= mddev
->raid_disks
;
965 /* make rdev->sb match mddev data..
968 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
969 * 3/ any empty disks < next_spare become removed
971 * disks[0] gets initialised to REMOVED because
972 * we cannot be sure from other fields if it has
973 * been initialised or not.
976 int active
=0, working
=0,failed
=0,spare
=0,nr_disks
=0;
978 rdev
->sb_size
= MD_SB_BYTES
;
980 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
982 memset(sb
, 0, sizeof(*sb
));
984 sb
->md_magic
= MD_SB_MAGIC
;
985 sb
->major_version
= mddev
->major_version
;
986 sb
->patch_version
= mddev
->patch_version
;
987 sb
->gvalid_words
= 0; /* ignored */
988 memcpy(&sb
->set_uuid0
, mddev
->uuid
+0, 4);
989 memcpy(&sb
->set_uuid1
, mddev
->uuid
+4, 4);
990 memcpy(&sb
->set_uuid2
, mddev
->uuid
+8, 4);
991 memcpy(&sb
->set_uuid3
, mddev
->uuid
+12,4);
993 sb
->ctime
= mddev
->ctime
;
994 sb
->level
= mddev
->level
;
995 sb
->size
= mddev
->dev_sectors
/ 2;
996 sb
->raid_disks
= mddev
->raid_disks
;
997 sb
->md_minor
= mddev
->md_minor
;
998 sb
->not_persistent
= 0;
999 sb
->utime
= mddev
->utime
;
1001 sb
->events_hi
= (mddev
->events
>>32);
1002 sb
->events_lo
= (u32
)mddev
->events
;
1004 if (mddev
->reshape_position
== MaxSector
)
1005 sb
->minor_version
= 90;
1007 sb
->minor_version
= 91;
1008 sb
->reshape_position
= mddev
->reshape_position
;
1009 sb
->new_level
= mddev
->new_level
;
1010 sb
->delta_disks
= mddev
->delta_disks
;
1011 sb
->new_layout
= mddev
->new_layout
;
1012 sb
->new_chunk
= mddev
->new_chunk_sectors
<< 9;
1014 mddev
->minor_version
= sb
->minor_version
;
1017 sb
->recovery_cp
= mddev
->recovery_cp
;
1018 sb
->cp_events_hi
= (mddev
->events
>>32);
1019 sb
->cp_events_lo
= (u32
)mddev
->events
;
1020 if (mddev
->recovery_cp
== MaxSector
)
1021 sb
->state
= (1<< MD_SB_CLEAN
);
1023 sb
->recovery_cp
= 0;
1025 sb
->layout
= mddev
->layout
;
1026 sb
->chunk_size
= mddev
->chunk_sectors
<< 9;
1028 if (mddev
->bitmap
&& mddev
->bitmap_file
== NULL
)
1029 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
1031 sb
->disks
[0].state
= (1<<MD_DISK_REMOVED
);
1032 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
1035 if (rdev2
->raid_disk
>= 0 && test_bit(In_sync
, &rdev2
->flags
)
1036 && !test_bit(Faulty
, &rdev2
->flags
))
1037 desc_nr
= rdev2
->raid_disk
;
1039 desc_nr
= next_spare
++;
1040 rdev2
->desc_nr
= desc_nr
;
1041 d
= &sb
->disks
[rdev2
->desc_nr
];
1043 d
->number
= rdev2
->desc_nr
;
1044 d
->major
= MAJOR(rdev2
->bdev
->bd_dev
);
1045 d
->minor
= MINOR(rdev2
->bdev
->bd_dev
);
1046 if (rdev2
->raid_disk
>= 0 && test_bit(In_sync
, &rdev2
->flags
)
1047 && !test_bit(Faulty
, &rdev2
->flags
))
1048 d
->raid_disk
= rdev2
->raid_disk
;
1050 d
->raid_disk
= rdev2
->desc_nr
; /* compatibility */
1051 if (test_bit(Faulty
, &rdev2
->flags
))
1052 d
->state
= (1<<MD_DISK_FAULTY
);
1053 else if (test_bit(In_sync
, &rdev2
->flags
)) {
1054 d
->state
= (1<<MD_DISK_ACTIVE
);
1055 d
->state
|= (1<<MD_DISK_SYNC
);
1063 if (test_bit(WriteMostly
, &rdev2
->flags
))
1064 d
->state
|= (1<<MD_DISK_WRITEMOSTLY
);
1066 /* now set the "removed" and "faulty" bits on any missing devices */
1067 for (i
=0 ; i
< mddev
->raid_disks
; i
++) {
1068 mdp_disk_t
*d
= &sb
->disks
[i
];
1069 if (d
->state
== 0 && d
->number
== 0) {
1072 d
->state
= (1<<MD_DISK_REMOVED
);
1073 d
->state
|= (1<<MD_DISK_FAULTY
);
1077 sb
->nr_disks
= nr_disks
;
1078 sb
->active_disks
= active
;
1079 sb
->working_disks
= working
;
1080 sb
->failed_disks
= failed
;
1081 sb
->spare_disks
= spare
;
1083 sb
->this_disk
= sb
->disks
[rdev
->desc_nr
];
1084 sb
->sb_csum
= calc_sb_csum(sb
);
1088 * rdev_size_change for 0.90.0
1090 static unsigned long long
1091 super_90_rdev_size_change(mdk_rdev_t
*rdev
, sector_t num_sectors
)
1093 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1094 return 0; /* component must fit device */
1095 if (rdev
->mddev
->bitmap_offset
)
1096 return 0; /* can't move bitmap */
1097 rdev
->sb_start
= calc_dev_sboffset(rdev
->bdev
);
1098 if (!num_sectors
|| num_sectors
> rdev
->sb_start
)
1099 num_sectors
= rdev
->sb_start
;
1100 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1102 md_super_wait(rdev
->mddev
);
1103 return num_sectors
/ 2; /* kB for sysfs */
1108 * version 1 superblock
1111 static __le32
calc_sb_1_csum(struct mdp_superblock_1
* sb
)
1115 unsigned long long newcsum
;
1116 int size
= 256 + le32_to_cpu(sb
->max_dev
)*2;
1117 __le32
*isuper
= (__le32
*)sb
;
1120 disk_csum
= sb
->sb_csum
;
1123 for (i
=0; size
>=4; size
-= 4 )
1124 newcsum
+= le32_to_cpu(*isuper
++);
1127 newcsum
+= le16_to_cpu(*(__le16
*) isuper
);
1129 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
1130 sb
->sb_csum
= disk_csum
;
1131 return cpu_to_le32(csum
);
1134 static int super_1_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
1136 struct mdp_superblock_1
*sb
;
1139 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1143 * Calculate the position of the superblock in 512byte sectors.
1144 * It is always aligned to a 4K boundary and
1145 * depeding on minor_version, it can be:
1146 * 0: At least 8K, but less than 12K, from end of device
1147 * 1: At start of device
1148 * 2: 4K from start of device.
1150 switch(minor_version
) {
1152 sb_start
= rdev
->bdev
->bd_inode
->i_size
>> 9;
1154 sb_start
&= ~(sector_t
)(4*2-1);
1165 rdev
->sb_start
= sb_start
;
1167 /* superblock is rarely larger than 1K, but it can be larger,
1168 * and it is safe to read 4k, so we do that
1170 ret
= read_disk_sb(rdev
, 4096);
1171 if (ret
) return ret
;
1174 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1176 if (sb
->magic
!= cpu_to_le32(MD_SB_MAGIC
) ||
1177 sb
->major_version
!= cpu_to_le32(1) ||
1178 le32_to_cpu(sb
->max_dev
) > (4096-256)/2 ||
1179 le64_to_cpu(sb
->super_offset
) != rdev
->sb_start
||
1180 (le32_to_cpu(sb
->feature_map
) & ~MD_FEATURE_ALL
) != 0)
1183 if (calc_sb_1_csum(sb
) != sb
->sb_csum
) {
1184 printk("md: invalid superblock checksum on %s\n",
1185 bdevname(rdev
->bdev
,b
));
1188 if (le64_to_cpu(sb
->data_size
) < 10) {
1189 printk("md: data_size too small on %s\n",
1190 bdevname(rdev
->bdev
,b
));
1194 rdev
->preferred_minor
= 0xffff;
1195 rdev
->data_offset
= le64_to_cpu(sb
->data_offset
);
1196 atomic_set(&rdev
->corrected_errors
, le32_to_cpu(sb
->cnt_corrected_read
));
1198 rdev
->sb_size
= le32_to_cpu(sb
->max_dev
) * 2 + 256;
1199 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1200 if (rdev
->sb_size
& bmask
)
1201 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1204 && rdev
->data_offset
< sb_start
+ (rdev
->sb_size
/512))
1207 if (sb
->level
== cpu_to_le32(LEVEL_MULTIPATH
))
1210 rdev
->desc_nr
= le32_to_cpu(sb
->dev_number
);
1216 struct mdp_superblock_1
*refsb
=
1217 (struct mdp_superblock_1
*)page_address(refdev
->sb_page
);
1219 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16) != 0 ||
1220 sb
->level
!= refsb
->level
||
1221 sb
->layout
!= refsb
->layout
||
1222 sb
->chunksize
!= refsb
->chunksize
) {
1223 printk(KERN_WARNING
"md: %s has strangely different"
1224 " superblock to %s\n",
1225 bdevname(rdev
->bdev
,b
),
1226 bdevname(refdev
->bdev
,b2
));
1229 ev1
= le64_to_cpu(sb
->events
);
1230 ev2
= le64_to_cpu(refsb
->events
);
1238 rdev
->sectors
= (rdev
->bdev
->bd_inode
->i_size
>> 9) -
1239 le64_to_cpu(sb
->data_offset
);
1241 rdev
->sectors
= rdev
->sb_start
;
1242 if (rdev
->sectors
< le64_to_cpu(sb
->data_size
))
1244 rdev
->sectors
= le64_to_cpu(sb
->data_size
);
1245 if (le64_to_cpu(sb
->size
) > rdev
->sectors
)
1250 static int super_1_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1252 struct mdp_superblock_1
*sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1253 __u64 ev1
= le64_to_cpu(sb
->events
);
1255 rdev
->raid_disk
= -1;
1256 clear_bit(Faulty
, &rdev
->flags
);
1257 clear_bit(In_sync
, &rdev
->flags
);
1258 clear_bit(WriteMostly
, &rdev
->flags
);
1259 clear_bit(BarriersNotsupp
, &rdev
->flags
);
1261 if (mddev
->raid_disks
== 0) {
1262 mddev
->major_version
= 1;
1263 mddev
->patch_version
= 0;
1264 mddev
->external
= 0;
1265 mddev
->chunk_sectors
= le32_to_cpu(sb
->chunksize
);
1266 mddev
->ctime
= le64_to_cpu(sb
->ctime
) & ((1ULL << 32)-1);
1267 mddev
->utime
= le64_to_cpu(sb
->utime
) & ((1ULL << 32)-1);
1268 mddev
->level
= le32_to_cpu(sb
->level
);
1269 mddev
->clevel
[0] = 0;
1270 mddev
->layout
= le32_to_cpu(sb
->layout
);
1271 mddev
->raid_disks
= le32_to_cpu(sb
->raid_disks
);
1272 mddev
->dev_sectors
= le64_to_cpu(sb
->size
);
1273 mddev
->events
= ev1
;
1274 mddev
->bitmap_offset
= 0;
1275 mddev
->default_bitmap_offset
= 1024 >> 9;
1277 mddev
->recovery_cp
= le64_to_cpu(sb
->resync_offset
);
1278 memcpy(mddev
->uuid
, sb
->set_uuid
, 16);
1280 mddev
->max_disks
= (4096-256)/2;
1282 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BITMAP_OFFSET
) &&
1283 mddev
->bitmap_file
== NULL
)
1284 mddev
->bitmap_offset
= (__s32
)le32_to_cpu(sb
->bitmap_offset
);
1286 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
1287 mddev
->reshape_position
= le64_to_cpu(sb
->reshape_position
);
1288 mddev
->delta_disks
= le32_to_cpu(sb
->delta_disks
);
1289 mddev
->new_level
= le32_to_cpu(sb
->new_level
);
1290 mddev
->new_layout
= le32_to_cpu(sb
->new_layout
);
1291 mddev
->new_chunk_sectors
= le32_to_cpu(sb
->new_chunk
);
1293 mddev
->reshape_position
= MaxSector
;
1294 mddev
->delta_disks
= 0;
1295 mddev
->new_level
= mddev
->level
;
1296 mddev
->new_layout
= mddev
->layout
;
1297 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1300 } else if (mddev
->pers
== NULL
) {
1301 /* Insist of good event counter while assembling */
1303 if (ev1
< mddev
->events
)
1305 } else if (mddev
->bitmap
) {
1306 /* If adding to array with a bitmap, then we can accept an
1307 * older device, but not too old.
1309 if (ev1
< mddev
->bitmap
->events_cleared
)
1312 if (ev1
< mddev
->events
)
1313 /* just a hot-add of a new device, leave raid_disk at -1 */
1316 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1318 if (rdev
->desc_nr
< 0 ||
1319 rdev
->desc_nr
>= le32_to_cpu(sb
->max_dev
)) {
1323 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
1325 case 0xffff: /* spare */
1327 case 0xfffe: /* faulty */
1328 set_bit(Faulty
, &rdev
->flags
);
1331 if ((le32_to_cpu(sb
->feature_map
) &
1332 MD_FEATURE_RECOVERY_OFFSET
))
1333 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
1335 set_bit(In_sync
, &rdev
->flags
);
1336 rdev
->raid_disk
= role
;
1339 if (sb
->devflags
& WriteMostly1
)
1340 set_bit(WriteMostly
, &rdev
->flags
);
1341 } else /* MULTIPATH are always insync */
1342 set_bit(In_sync
, &rdev
->flags
);
1347 static void super_1_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1349 struct mdp_superblock_1
*sb
;
1352 /* make rdev->sb match mddev and rdev data. */
1354 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1356 sb
->feature_map
= 0;
1358 sb
->recovery_offset
= cpu_to_le64(0);
1359 memset(sb
->pad1
, 0, sizeof(sb
->pad1
));
1360 memset(sb
->pad2
, 0, sizeof(sb
->pad2
));
1361 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
1363 sb
->utime
= cpu_to_le64((__u64
)mddev
->utime
);
1364 sb
->events
= cpu_to_le64(mddev
->events
);
1366 sb
->resync_offset
= cpu_to_le64(mddev
->recovery_cp
);
1368 sb
->resync_offset
= cpu_to_le64(0);
1370 sb
->cnt_corrected_read
= cpu_to_le32(atomic_read(&rdev
->corrected_errors
));
1372 sb
->raid_disks
= cpu_to_le32(mddev
->raid_disks
);
1373 sb
->size
= cpu_to_le64(mddev
->dev_sectors
);
1374 sb
->chunksize
= cpu_to_le32(mddev
->chunk_sectors
);
1375 sb
->level
= cpu_to_le32(mddev
->level
);
1376 sb
->layout
= cpu_to_le32(mddev
->layout
);
1378 if (mddev
->bitmap
&& mddev
->bitmap_file
== NULL
) {
1379 sb
->bitmap_offset
= cpu_to_le32((__u32
)mddev
->bitmap_offset
);
1380 sb
->feature_map
= cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
);
1383 if (rdev
->raid_disk
>= 0 &&
1384 !test_bit(In_sync
, &rdev
->flags
)) {
1385 if (mddev
->curr_resync_completed
> rdev
->recovery_offset
)
1386 rdev
->recovery_offset
= mddev
->curr_resync_completed
;
1387 if (rdev
->recovery_offset
> 0) {
1389 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET
);
1390 sb
->recovery_offset
=
1391 cpu_to_le64(rdev
->recovery_offset
);
1395 if (mddev
->reshape_position
!= MaxSector
) {
1396 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE
);
1397 sb
->reshape_position
= cpu_to_le64(mddev
->reshape_position
);
1398 sb
->new_layout
= cpu_to_le32(mddev
->new_layout
);
1399 sb
->delta_disks
= cpu_to_le32(mddev
->delta_disks
);
1400 sb
->new_level
= cpu_to_le32(mddev
->new_level
);
1401 sb
->new_chunk
= cpu_to_le32(mddev
->new_chunk_sectors
);
1405 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
)
1406 if (rdev2
->desc_nr
+1 > max_dev
)
1407 max_dev
= rdev2
->desc_nr
+1;
1409 if (max_dev
> le32_to_cpu(sb
->max_dev
)) {
1411 sb
->max_dev
= cpu_to_le32(max_dev
);
1412 rdev
->sb_size
= max_dev
* 2 + 256;
1413 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1414 if (rdev
->sb_size
& bmask
)
1415 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1417 for (i
=0; i
<max_dev
;i
++)
1418 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1420 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
1422 if (test_bit(Faulty
, &rdev2
->flags
))
1423 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1424 else if (test_bit(In_sync
, &rdev2
->flags
))
1425 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1426 else if (rdev2
->raid_disk
>= 0 && rdev2
->recovery_offset
> 0)
1427 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1429 sb
->dev_roles
[i
] = cpu_to_le16(0xffff);
1432 sb
->sb_csum
= calc_sb_1_csum(sb
);
1435 static unsigned long long
1436 super_1_rdev_size_change(mdk_rdev_t
*rdev
, sector_t num_sectors
)
1438 struct mdp_superblock_1
*sb
;
1439 sector_t max_sectors
;
1440 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1441 return 0; /* component must fit device */
1442 if (rdev
->sb_start
< rdev
->data_offset
) {
1443 /* minor versions 1 and 2; superblock before data */
1444 max_sectors
= rdev
->bdev
->bd_inode
->i_size
>> 9;
1445 max_sectors
-= rdev
->data_offset
;
1446 if (!num_sectors
|| num_sectors
> max_sectors
)
1447 num_sectors
= max_sectors
;
1448 } else if (rdev
->mddev
->bitmap_offset
) {
1449 /* minor version 0 with bitmap we can't move */
1452 /* minor version 0; superblock after data */
1454 sb_start
= (rdev
->bdev
->bd_inode
->i_size
>> 9) - 8*2;
1455 sb_start
&= ~(sector_t
)(4*2 - 1);
1456 max_sectors
= rdev
->sectors
+ sb_start
- rdev
->sb_start
;
1457 if (!num_sectors
|| num_sectors
> max_sectors
)
1458 num_sectors
= max_sectors
;
1459 rdev
->sb_start
= sb_start
;
1461 sb
= (struct mdp_superblock_1
*) page_address(rdev
->sb_page
);
1462 sb
->data_size
= cpu_to_le64(num_sectors
);
1463 sb
->super_offset
= rdev
->sb_start
;
1464 sb
->sb_csum
= calc_sb_1_csum(sb
);
1465 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1467 md_super_wait(rdev
->mddev
);
1468 return num_sectors
/ 2; /* kB for sysfs */
1471 static struct super_type super_types
[] = {
1474 .owner
= THIS_MODULE
,
1475 .load_super
= super_90_load
,
1476 .validate_super
= super_90_validate
,
1477 .sync_super
= super_90_sync
,
1478 .rdev_size_change
= super_90_rdev_size_change
,
1482 .owner
= THIS_MODULE
,
1483 .load_super
= super_1_load
,
1484 .validate_super
= super_1_validate
,
1485 .sync_super
= super_1_sync
,
1486 .rdev_size_change
= super_1_rdev_size_change
,
1490 static int match_mddev_units(mddev_t
*mddev1
, mddev_t
*mddev2
)
1492 mdk_rdev_t
*rdev
, *rdev2
;
1495 rdev_for_each_rcu(rdev
, mddev1
)
1496 rdev_for_each_rcu(rdev2
, mddev2
)
1497 if (rdev
->bdev
->bd_contains
==
1498 rdev2
->bdev
->bd_contains
) {
1506 static LIST_HEAD(pending_raid_disks
);
1509 * Try to register data integrity profile for an mddev
1511 * This is called when an array is started and after a disk has been kicked
1512 * from the array. It only succeeds if all working and active component devices
1513 * are integrity capable with matching profiles.
1515 int md_integrity_register(mddev_t
*mddev
)
1517 mdk_rdev_t
*rdev
, *reference
= NULL
;
1519 if (list_empty(&mddev
->disks
))
1520 return 0; /* nothing to do */
1521 if (blk_get_integrity(mddev
->gendisk
))
1522 return 0; /* already registered */
1523 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
1524 /* skip spares and non-functional disks */
1525 if (test_bit(Faulty
, &rdev
->flags
))
1527 if (rdev
->raid_disk
< 0)
1530 * If at least one rdev is not integrity capable, we can not
1531 * enable data integrity for the md device.
1533 if (!bdev_get_integrity(rdev
->bdev
))
1536 /* Use the first rdev as the reference */
1540 /* does this rdev's profile match the reference profile? */
1541 if (blk_integrity_compare(reference
->bdev
->bd_disk
,
1542 rdev
->bdev
->bd_disk
) < 0)
1546 * All component devices are integrity capable and have matching
1547 * profiles, register the common profile for the md device.
1549 if (blk_integrity_register(mddev
->gendisk
,
1550 bdev_get_integrity(reference
->bdev
)) != 0) {
1551 printk(KERN_ERR
"md: failed to register integrity for %s\n",
1555 printk(KERN_NOTICE
"md: data integrity on %s enabled\n",
1559 EXPORT_SYMBOL(md_integrity_register
);
1561 /* Disable data integrity if non-capable/non-matching disk is being added */
1562 void md_integrity_add_rdev(mdk_rdev_t
*rdev
, mddev_t
*mddev
)
1564 struct blk_integrity
*bi_rdev
= bdev_get_integrity(rdev
->bdev
);
1565 struct blk_integrity
*bi_mddev
= blk_get_integrity(mddev
->gendisk
);
1567 if (!bi_mddev
) /* nothing to do */
1569 if (rdev
->raid_disk
< 0) /* skip spares */
1571 if (bi_rdev
&& blk_integrity_compare(mddev
->gendisk
,
1572 rdev
->bdev
->bd_disk
) >= 0)
1574 printk(KERN_NOTICE
"disabling data integrity on %s\n", mdname(mddev
));
1575 blk_integrity_unregister(mddev
->gendisk
);
1577 EXPORT_SYMBOL(md_integrity_add_rdev
);
1579 static int bind_rdev_to_array(mdk_rdev_t
* rdev
, mddev_t
* mddev
)
1581 char b
[BDEVNAME_SIZE
];
1591 /* prevent duplicates */
1592 if (find_rdev(mddev
, rdev
->bdev
->bd_dev
))
1595 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1596 if (rdev
->sectors
&& (mddev
->dev_sectors
== 0 ||
1597 rdev
->sectors
< mddev
->dev_sectors
)) {
1599 /* Cannot change size, so fail
1600 * If mddev->level <= 0, then we don't care
1601 * about aligning sizes (e.g. linear)
1603 if (mddev
->level
> 0)
1606 mddev
->dev_sectors
= rdev
->sectors
;
1609 /* Verify rdev->desc_nr is unique.
1610 * If it is -1, assign a free number, else
1611 * check number is not in use
1613 if (rdev
->desc_nr
< 0) {
1615 if (mddev
->pers
) choice
= mddev
->raid_disks
;
1616 while (find_rdev_nr(mddev
, choice
))
1618 rdev
->desc_nr
= choice
;
1620 if (find_rdev_nr(mddev
, rdev
->desc_nr
))
1623 if (mddev
->max_disks
&& rdev
->desc_nr
>= mddev
->max_disks
) {
1624 printk(KERN_WARNING
"md: %s: array is limited to %d devices\n",
1625 mdname(mddev
), mddev
->max_disks
);
1628 bdevname(rdev
->bdev
,b
);
1629 while ( (s
=strchr(b
, '/')) != NULL
)
1632 rdev
->mddev
= mddev
;
1633 printk(KERN_INFO
"md: bind<%s>\n", b
);
1635 if ((err
= kobject_add(&rdev
->kobj
, &mddev
->kobj
, "dev-%s", b
)))
1638 ko
= &part_to_dev(rdev
->bdev
->bd_part
)->kobj
;
1639 if ((err
= sysfs_create_link(&rdev
->kobj
, ko
, "block"))) {
1640 kobject_del(&rdev
->kobj
);
1643 rdev
->sysfs_state
= sysfs_get_dirent(rdev
->kobj
.sd
, "state");
1645 list_add_rcu(&rdev
->same_set
, &mddev
->disks
);
1646 bd_claim_by_disk(rdev
->bdev
, rdev
->bdev
->bd_holder
, mddev
->gendisk
);
1648 /* May as well allow recovery to be retried once */
1649 mddev
->recovery_disabled
= 0;
1654 printk(KERN_WARNING
"md: failed to register dev-%s for %s\n",
1659 static void md_delayed_delete(struct work_struct
*ws
)
1661 mdk_rdev_t
*rdev
= container_of(ws
, mdk_rdev_t
, del_work
);
1662 kobject_del(&rdev
->kobj
);
1663 kobject_put(&rdev
->kobj
);
1666 static void unbind_rdev_from_array(mdk_rdev_t
* rdev
)
1668 char b
[BDEVNAME_SIZE
];
1673 bd_release_from_disk(rdev
->bdev
, rdev
->mddev
->gendisk
);
1674 list_del_rcu(&rdev
->same_set
);
1675 printk(KERN_INFO
"md: unbind<%s>\n", bdevname(rdev
->bdev
,b
));
1677 sysfs_remove_link(&rdev
->kobj
, "block");
1678 sysfs_put(rdev
->sysfs_state
);
1679 rdev
->sysfs_state
= NULL
;
1680 /* We need to delay this, otherwise we can deadlock when
1681 * writing to 'remove' to "dev/state". We also need
1682 * to delay it due to rcu usage.
1685 INIT_WORK(&rdev
->del_work
, md_delayed_delete
);
1686 kobject_get(&rdev
->kobj
);
1687 schedule_work(&rdev
->del_work
);
1691 * prevent the device from being mounted, repartitioned or
1692 * otherwise reused by a RAID array (or any other kernel
1693 * subsystem), by bd_claiming the device.
1695 static int lock_rdev(mdk_rdev_t
*rdev
, dev_t dev
, int shared
)
1698 struct block_device
*bdev
;
1699 char b
[BDEVNAME_SIZE
];
1701 bdev
= open_by_devnum(dev
, FMODE_READ
|FMODE_WRITE
);
1703 printk(KERN_ERR
"md: could not open %s.\n",
1704 __bdevname(dev
, b
));
1705 return PTR_ERR(bdev
);
1707 err
= bd_claim(bdev
, shared
? (mdk_rdev_t
*)lock_rdev
: rdev
);
1709 printk(KERN_ERR
"md: could not bd_claim %s.\n",
1711 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
);
1715 set_bit(AllReserved
, &rdev
->flags
);
1720 static void unlock_rdev(mdk_rdev_t
*rdev
)
1722 struct block_device
*bdev
= rdev
->bdev
;
1727 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
);
1730 void md_autodetect_dev(dev_t dev
);
1732 static void export_rdev(mdk_rdev_t
* rdev
)
1734 char b
[BDEVNAME_SIZE
];
1735 printk(KERN_INFO
"md: export_rdev(%s)\n",
1736 bdevname(rdev
->bdev
,b
));
1741 if (test_bit(AutoDetected
, &rdev
->flags
))
1742 md_autodetect_dev(rdev
->bdev
->bd_dev
);
1745 kobject_put(&rdev
->kobj
);
1748 static void kick_rdev_from_array(mdk_rdev_t
* rdev
)
1750 unbind_rdev_from_array(rdev
);
1754 static void export_array(mddev_t
*mddev
)
1756 mdk_rdev_t
*rdev
, *tmp
;
1758 rdev_for_each(rdev
, tmp
, mddev
) {
1763 kick_rdev_from_array(rdev
);
1765 if (!list_empty(&mddev
->disks
))
1767 mddev
->raid_disks
= 0;
1768 mddev
->major_version
= 0;
1771 static void print_desc(mdp_disk_t
*desc
)
1773 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc
->number
,
1774 desc
->major
,desc
->minor
,desc
->raid_disk
,desc
->state
);
1777 static void print_sb_90(mdp_super_t
*sb
)
1782 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1783 sb
->major_version
, sb
->minor_version
, sb
->patch_version
,
1784 sb
->set_uuid0
, sb
->set_uuid1
, sb
->set_uuid2
, sb
->set_uuid3
,
1786 printk(KERN_INFO
"md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1787 sb
->level
, sb
->size
, sb
->nr_disks
, sb
->raid_disks
,
1788 sb
->md_minor
, sb
->layout
, sb
->chunk_size
);
1789 printk(KERN_INFO
"md: UT:%08x ST:%d AD:%d WD:%d"
1790 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1791 sb
->utime
, sb
->state
, sb
->active_disks
, sb
->working_disks
,
1792 sb
->failed_disks
, sb
->spare_disks
,
1793 sb
->sb_csum
, (unsigned long)sb
->events_lo
);
1796 for (i
= 0; i
< MD_SB_DISKS
; i
++) {
1799 desc
= sb
->disks
+ i
;
1800 if (desc
->number
|| desc
->major
|| desc
->minor
||
1801 desc
->raid_disk
|| (desc
->state
&& (desc
->state
!= 4))) {
1802 printk(" D %2d: ", i
);
1806 printk(KERN_INFO
"md: THIS: ");
1807 print_desc(&sb
->this_disk
);
1810 static void print_sb_1(struct mdp_superblock_1
*sb
)
1814 uuid
= sb
->set_uuid
;
1816 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%02x%02x%02x%02x"
1817 ":%02x%02x:%02x%02x:%02x%02x:%02x%02x%02x%02x%02x%02x>\n"
1818 "md: Name: \"%s\" CT:%llu\n",
1819 le32_to_cpu(sb
->major_version
),
1820 le32_to_cpu(sb
->feature_map
),
1821 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
1822 uuid
[4], uuid
[5], uuid
[6], uuid
[7],
1823 uuid
[8], uuid
[9], uuid
[10], uuid
[11],
1824 uuid
[12], uuid
[13], uuid
[14], uuid
[15],
1826 (unsigned long long)le64_to_cpu(sb
->ctime
)
1827 & MD_SUPERBLOCK_1_TIME_SEC_MASK
);
1829 uuid
= sb
->device_uuid
;
1831 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
1833 "md: Dev:%08x UUID: %02x%02x%02x%02x:%02x%02x:%02x%02x:%02x%02x"
1834 ":%02x%02x%02x%02x%02x%02x\n"
1835 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
1836 "md: (MaxDev:%u) \n",
1837 le32_to_cpu(sb
->level
),
1838 (unsigned long long)le64_to_cpu(sb
->size
),
1839 le32_to_cpu(sb
->raid_disks
),
1840 le32_to_cpu(sb
->layout
),
1841 le32_to_cpu(sb
->chunksize
),
1842 (unsigned long long)le64_to_cpu(sb
->data_offset
),
1843 (unsigned long long)le64_to_cpu(sb
->data_size
),
1844 (unsigned long long)le64_to_cpu(sb
->super_offset
),
1845 (unsigned long long)le64_to_cpu(sb
->recovery_offset
),
1846 le32_to_cpu(sb
->dev_number
),
1847 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
1848 uuid
[4], uuid
[5], uuid
[6], uuid
[7],
1849 uuid
[8], uuid
[9], uuid
[10], uuid
[11],
1850 uuid
[12], uuid
[13], uuid
[14], uuid
[15],
1852 (unsigned long long)le64_to_cpu(sb
->utime
) & MD_SUPERBLOCK_1_TIME_SEC_MASK
,
1853 (unsigned long long)le64_to_cpu(sb
->events
),
1854 (unsigned long long)le64_to_cpu(sb
->resync_offset
),
1855 le32_to_cpu(sb
->sb_csum
),
1856 le32_to_cpu(sb
->max_dev
)
1860 static void print_rdev(mdk_rdev_t
*rdev
, int major_version
)
1862 char b
[BDEVNAME_SIZE
];
1863 printk(KERN_INFO
"md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
1864 bdevname(rdev
->bdev
, b
), (unsigned long long)rdev
->sectors
,
1865 test_bit(Faulty
, &rdev
->flags
), test_bit(In_sync
, &rdev
->flags
),
1867 if (rdev
->sb_loaded
) {
1868 printk(KERN_INFO
"md: rdev superblock (MJ:%d):\n", major_version
);
1869 switch (major_version
) {
1871 print_sb_90((mdp_super_t
*)page_address(rdev
->sb_page
));
1874 print_sb_1((struct mdp_superblock_1
*)page_address(rdev
->sb_page
));
1878 printk(KERN_INFO
"md: no rdev superblock!\n");
1881 static void md_print_devices(void)
1883 struct list_head
*tmp
;
1886 char b
[BDEVNAME_SIZE
];
1889 printk("md: **********************************\n");
1890 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1891 printk("md: **********************************\n");
1892 for_each_mddev(mddev
, tmp
) {
1895 bitmap_print_sb(mddev
->bitmap
);
1897 printk("%s: ", mdname(mddev
));
1898 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
1899 printk("<%s>", bdevname(rdev
->bdev
,b
));
1902 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
1903 print_rdev(rdev
, mddev
->major_version
);
1905 printk("md: **********************************\n");
1910 static void sync_sbs(mddev_t
* mddev
, int nospares
)
1912 /* Update each superblock (in-memory image), but
1913 * if we are allowed to, skip spares which already
1914 * have the right event counter, or have one earlier
1915 * (which would mean they aren't being marked as dirty
1916 * with the rest of the array)
1920 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
1921 if (rdev
->sb_events
== mddev
->events
||
1923 rdev
->raid_disk
< 0 &&
1924 (rdev
->sb_events
&1)==0 &&
1925 rdev
->sb_events
+1 == mddev
->events
)) {
1926 /* Don't update this superblock */
1927 rdev
->sb_loaded
= 2;
1929 super_types
[mddev
->major_version
].
1930 sync_super(mddev
, rdev
);
1931 rdev
->sb_loaded
= 1;
1936 static void md_update_sb(mddev_t
* mddev
, int force_change
)
1942 mddev
->utime
= get_seconds();
1943 if (mddev
->external
)
1946 spin_lock_irq(&mddev
->write_lock
);
1948 set_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
1949 if (test_and_clear_bit(MD_CHANGE_DEVS
, &mddev
->flags
))
1951 if (test_and_clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
))
1952 /* just a clean<-> dirty transition, possibly leave spares alone,
1953 * though if events isn't the right even/odd, we will have to do
1959 if (mddev
->degraded
)
1960 /* If the array is degraded, then skipping spares is both
1961 * dangerous and fairly pointless.
1962 * Dangerous because a device that was removed from the array
1963 * might have a event_count that still looks up-to-date,
1964 * so it can be re-added without a resync.
1965 * Pointless because if there are any spares to skip,
1966 * then a recovery will happen and soon that array won't
1967 * be degraded any more and the spare can go back to sleep then.
1971 sync_req
= mddev
->in_sync
;
1973 /* If this is just a dirty<->clean transition, and the array is clean
1974 * and 'events' is odd, we can roll back to the previous clean state */
1976 && (mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
1977 && (mddev
->events
& 1)
1978 && mddev
->events
!= 1)
1981 /* otherwise we have to go forward and ... */
1983 if (!mddev
->in_sync
|| mddev
->recovery_cp
!= MaxSector
) { /* not clean */
1984 /* .. if the array isn't clean, an 'even' event must also go
1986 if ((mddev
->events
&1)==0)
1989 /* otherwise an 'odd' event must go to spares */
1990 if ((mddev
->events
&1))
1995 if (!mddev
->events
) {
1997 * oops, this 64-bit counter should never wrap.
1998 * Either we are in around ~1 trillion A.C., assuming
1999 * 1 reboot per second, or we have a bug:
2006 * do not write anything to disk if using
2007 * nonpersistent superblocks
2009 if (!mddev
->persistent
) {
2010 if (!mddev
->external
)
2011 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
2013 spin_unlock_irq(&mddev
->write_lock
);
2014 wake_up(&mddev
->sb_wait
);
2017 sync_sbs(mddev
, nospares
);
2018 spin_unlock_irq(&mddev
->write_lock
);
2021 "md: updating %s RAID superblock on device (in sync %d)\n",
2022 mdname(mddev
),mddev
->in_sync
);
2024 bitmap_update_sb(mddev
->bitmap
);
2025 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2026 char b
[BDEVNAME_SIZE
];
2027 dprintk(KERN_INFO
"md: ");
2028 if (rdev
->sb_loaded
!= 1)
2029 continue; /* no noise on spare devices */
2030 if (test_bit(Faulty
, &rdev
->flags
))
2031 dprintk("(skipping faulty ");
2033 dprintk("%s ", bdevname(rdev
->bdev
,b
));
2034 if (!test_bit(Faulty
, &rdev
->flags
)) {
2035 md_super_write(mddev
,rdev
,
2036 rdev
->sb_start
, rdev
->sb_size
,
2038 dprintk(KERN_INFO
"(write) %s's sb offset: %llu\n",
2039 bdevname(rdev
->bdev
,b
),
2040 (unsigned long long)rdev
->sb_start
);
2041 rdev
->sb_events
= mddev
->events
;
2045 if (mddev
->level
== LEVEL_MULTIPATH
)
2046 /* only need to write one superblock... */
2049 md_super_wait(mddev
);
2050 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
2052 spin_lock_irq(&mddev
->write_lock
);
2053 if (mddev
->in_sync
!= sync_req
||
2054 test_bit(MD_CHANGE_DEVS
, &mddev
->flags
)) {
2055 /* have to write it out again */
2056 spin_unlock_irq(&mddev
->write_lock
);
2059 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
2060 spin_unlock_irq(&mddev
->write_lock
);
2061 wake_up(&mddev
->sb_wait
);
2062 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
2063 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
2067 /* words written to sysfs files may, or may not, be \n terminated.
2068 * We want to accept with case. For this we use cmd_match.
2070 static int cmd_match(const char *cmd
, const char *str
)
2072 /* See if cmd, written into a sysfs file, matches
2073 * str. They must either be the same, or cmd can
2074 * have a trailing newline
2076 while (*cmd
&& *str
&& *cmd
== *str
) {
2087 struct rdev_sysfs_entry
{
2088 struct attribute attr
;
2089 ssize_t (*show
)(mdk_rdev_t
*, char *);
2090 ssize_t (*store
)(mdk_rdev_t
*, const char *, size_t);
2094 state_show(mdk_rdev_t
*rdev
, char *page
)
2099 if (test_bit(Faulty
, &rdev
->flags
)) {
2100 len
+= sprintf(page
+len
, "%sfaulty",sep
);
2103 if (test_bit(In_sync
, &rdev
->flags
)) {
2104 len
+= sprintf(page
+len
, "%sin_sync",sep
);
2107 if (test_bit(WriteMostly
, &rdev
->flags
)) {
2108 len
+= sprintf(page
+len
, "%swrite_mostly",sep
);
2111 if (test_bit(Blocked
, &rdev
->flags
)) {
2112 len
+= sprintf(page
+len
, "%sblocked", sep
);
2115 if (!test_bit(Faulty
, &rdev
->flags
) &&
2116 !test_bit(In_sync
, &rdev
->flags
)) {
2117 len
+= sprintf(page
+len
, "%sspare", sep
);
2120 return len
+sprintf(page
+len
, "\n");
2124 state_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2127 * faulty - simulates and error
2128 * remove - disconnects the device
2129 * writemostly - sets write_mostly
2130 * -writemostly - clears write_mostly
2131 * blocked - sets the Blocked flag
2132 * -blocked - clears the Blocked flag
2133 * insync - sets Insync providing device isn't active
2136 if (cmd_match(buf
, "faulty") && rdev
->mddev
->pers
) {
2137 md_error(rdev
->mddev
, rdev
);
2139 } else if (cmd_match(buf
, "remove")) {
2140 if (rdev
->raid_disk
>= 0)
2143 mddev_t
*mddev
= rdev
->mddev
;
2144 kick_rdev_from_array(rdev
);
2146 md_update_sb(mddev
, 1);
2147 md_new_event(mddev
);
2150 } else if (cmd_match(buf
, "writemostly")) {
2151 set_bit(WriteMostly
, &rdev
->flags
);
2153 } else if (cmd_match(buf
, "-writemostly")) {
2154 clear_bit(WriteMostly
, &rdev
->flags
);
2156 } else if (cmd_match(buf
, "blocked")) {
2157 set_bit(Blocked
, &rdev
->flags
);
2159 } else if (cmd_match(buf
, "-blocked")) {
2160 clear_bit(Blocked
, &rdev
->flags
);
2161 wake_up(&rdev
->blocked_wait
);
2162 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2163 md_wakeup_thread(rdev
->mddev
->thread
);
2166 } else if (cmd_match(buf
, "insync") && rdev
->raid_disk
== -1) {
2167 set_bit(In_sync
, &rdev
->flags
);
2170 if (!err
&& rdev
->sysfs_state
)
2171 sysfs_notify_dirent(rdev
->sysfs_state
);
2172 return err
? err
: len
;
2174 static struct rdev_sysfs_entry rdev_state
=
2175 __ATTR(state
, S_IRUGO
|S_IWUSR
, state_show
, state_store
);
2178 errors_show(mdk_rdev_t
*rdev
, char *page
)
2180 return sprintf(page
, "%d\n", atomic_read(&rdev
->corrected_errors
));
2184 errors_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2187 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2188 if (*buf
&& (*e
== 0 || *e
== '\n')) {
2189 atomic_set(&rdev
->corrected_errors
, n
);
2194 static struct rdev_sysfs_entry rdev_errors
=
2195 __ATTR(errors
, S_IRUGO
|S_IWUSR
, errors_show
, errors_store
);
2198 slot_show(mdk_rdev_t
*rdev
, char *page
)
2200 if (rdev
->raid_disk
< 0)
2201 return sprintf(page
, "none\n");
2203 return sprintf(page
, "%d\n", rdev
->raid_disk
);
2207 slot_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2212 int slot
= simple_strtoul(buf
, &e
, 10);
2213 if (strncmp(buf
, "none", 4)==0)
2215 else if (e
==buf
|| (*e
&& *e
!= '\n'))
2217 if (rdev
->mddev
->pers
&& slot
== -1) {
2218 /* Setting 'slot' on an active array requires also
2219 * updating the 'rd%d' link, and communicating
2220 * with the personality with ->hot_*_disk.
2221 * For now we only support removing
2222 * failed/spare devices. This normally happens automatically,
2223 * but not when the metadata is externally managed.
2225 if (rdev
->raid_disk
== -1)
2227 /* personality does all needed checks */
2228 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
2230 err
= rdev
->mddev
->pers
->
2231 hot_remove_disk(rdev
->mddev
, rdev
->raid_disk
);
2234 sprintf(nm
, "rd%d", rdev
->raid_disk
);
2235 sysfs_remove_link(&rdev
->mddev
->kobj
, nm
);
2236 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2237 md_wakeup_thread(rdev
->mddev
->thread
);
2238 } else if (rdev
->mddev
->pers
) {
2240 /* Activating a spare .. or possibly reactivating
2241 * if we ever get bitmaps working here.
2244 if (rdev
->raid_disk
!= -1)
2247 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
2250 list_for_each_entry(rdev2
, &rdev
->mddev
->disks
, same_set
)
2251 if (rdev2
->raid_disk
== slot
)
2254 rdev
->raid_disk
= slot
;
2255 if (test_bit(In_sync
, &rdev
->flags
))
2256 rdev
->saved_raid_disk
= slot
;
2258 rdev
->saved_raid_disk
= -1;
2259 err
= rdev
->mddev
->pers
->
2260 hot_add_disk(rdev
->mddev
, rdev
);
2262 rdev
->raid_disk
= -1;
2265 sysfs_notify_dirent(rdev
->sysfs_state
);
2266 sprintf(nm
, "rd%d", rdev
->raid_disk
);
2267 if (sysfs_create_link(&rdev
->mddev
->kobj
, &rdev
->kobj
, nm
))
2269 "md: cannot register "
2271 nm
, mdname(rdev
->mddev
));
2273 /* don't wakeup anyone, leave that to userspace. */
2275 if (slot
>= rdev
->mddev
->raid_disks
)
2277 rdev
->raid_disk
= slot
;
2278 /* assume it is working */
2279 clear_bit(Faulty
, &rdev
->flags
);
2280 clear_bit(WriteMostly
, &rdev
->flags
);
2281 set_bit(In_sync
, &rdev
->flags
);
2282 sysfs_notify_dirent(rdev
->sysfs_state
);
2288 static struct rdev_sysfs_entry rdev_slot
=
2289 __ATTR(slot
, S_IRUGO
|S_IWUSR
, slot_show
, slot_store
);
2292 offset_show(mdk_rdev_t
*rdev
, char *page
)
2294 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->data_offset
);
2298 offset_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2301 unsigned long long offset
= simple_strtoull(buf
, &e
, 10);
2302 if (e
==buf
|| (*e
&& *e
!= '\n'))
2304 if (rdev
->mddev
->pers
&& rdev
->raid_disk
>= 0)
2306 if (rdev
->sectors
&& rdev
->mddev
->external
)
2307 /* Must set offset before size, so overlap checks
2310 rdev
->data_offset
= offset
;
2314 static struct rdev_sysfs_entry rdev_offset
=
2315 __ATTR(offset
, S_IRUGO
|S_IWUSR
, offset_show
, offset_store
);
2318 rdev_size_show(mdk_rdev_t
*rdev
, char *page
)
2320 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->sectors
/ 2);
2323 static int overlaps(sector_t s1
, sector_t l1
, sector_t s2
, sector_t l2
)
2325 /* check if two start/length pairs overlap */
2333 static int strict_blocks_to_sectors(const char *buf
, sector_t
*sectors
)
2335 unsigned long long blocks
;
2338 if (strict_strtoull(buf
, 10, &blocks
) < 0)
2341 if (blocks
& 1ULL << (8 * sizeof(blocks
) - 1))
2342 return -EINVAL
; /* sector conversion overflow */
2345 if (new != blocks
* 2)
2346 return -EINVAL
; /* unsigned long long to sector_t overflow */
2353 rdev_size_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2355 mddev_t
*my_mddev
= rdev
->mddev
;
2356 sector_t oldsectors
= rdev
->sectors
;
2359 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
2361 if (my_mddev
->pers
&& rdev
->raid_disk
>= 0) {
2362 if (my_mddev
->persistent
) {
2363 sectors
= super_types
[my_mddev
->major_version
].
2364 rdev_size_change(rdev
, sectors
);
2367 } else if (!sectors
)
2368 sectors
= (rdev
->bdev
->bd_inode
->i_size
>> 9) -
2371 if (sectors
< my_mddev
->dev_sectors
)
2372 return -EINVAL
; /* component must fit device */
2374 rdev
->sectors
= sectors
;
2375 if (sectors
> oldsectors
&& my_mddev
->external
) {
2376 /* need to check that all other rdevs with the same ->bdev
2377 * do not overlap. We need to unlock the mddev to avoid
2378 * a deadlock. We have already changed rdev->sectors, and if
2379 * we have to change it back, we will have the lock again.
2383 struct list_head
*tmp
;
2385 mddev_unlock(my_mddev
);
2386 for_each_mddev(mddev
, tmp
) {
2390 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
)
2391 if (test_bit(AllReserved
, &rdev2
->flags
) ||
2392 (rdev
->bdev
== rdev2
->bdev
&&
2394 overlaps(rdev
->data_offset
, rdev
->sectors
,
2400 mddev_unlock(mddev
);
2406 mddev_lock(my_mddev
);
2408 /* Someone else could have slipped in a size
2409 * change here, but doing so is just silly.
2410 * We put oldsectors back because we *know* it is
2411 * safe, and trust userspace not to race with
2414 rdev
->sectors
= oldsectors
;
2421 static struct rdev_sysfs_entry rdev_size
=
2422 __ATTR(size
, S_IRUGO
|S_IWUSR
, rdev_size_show
, rdev_size_store
);
2424 static struct attribute
*rdev_default_attrs
[] = {
2433 rdev_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
2435 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
2436 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
2437 mddev_t
*mddev
= rdev
->mddev
;
2443 rv
= mddev
? mddev_lock(mddev
) : -EBUSY
;
2445 if (rdev
->mddev
== NULL
)
2448 rv
= entry
->show(rdev
, page
);
2449 mddev_unlock(mddev
);
2455 rdev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
2456 const char *page
, size_t length
)
2458 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
2459 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
2461 mddev_t
*mddev
= rdev
->mddev
;
2465 if (!capable(CAP_SYS_ADMIN
))
2467 rv
= mddev
? mddev_lock(mddev
): -EBUSY
;
2469 if (rdev
->mddev
== NULL
)
2472 rv
= entry
->store(rdev
, page
, length
);
2473 mddev_unlock(mddev
);
2478 static void rdev_free(struct kobject
*ko
)
2480 mdk_rdev_t
*rdev
= container_of(ko
, mdk_rdev_t
, kobj
);
2483 static struct sysfs_ops rdev_sysfs_ops
= {
2484 .show
= rdev_attr_show
,
2485 .store
= rdev_attr_store
,
2487 static struct kobj_type rdev_ktype
= {
2488 .release
= rdev_free
,
2489 .sysfs_ops
= &rdev_sysfs_ops
,
2490 .default_attrs
= rdev_default_attrs
,
2494 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2496 * mark the device faulty if:
2498 * - the device is nonexistent (zero size)
2499 * - the device has no valid superblock
2501 * a faulty rdev _never_ has rdev->sb set.
2503 static mdk_rdev_t
*md_import_device(dev_t newdev
, int super_format
, int super_minor
)
2505 char b
[BDEVNAME_SIZE
];
2510 rdev
= kzalloc(sizeof(*rdev
), GFP_KERNEL
);
2512 printk(KERN_ERR
"md: could not alloc mem for new device!\n");
2513 return ERR_PTR(-ENOMEM
);
2516 if ((err
= alloc_disk_sb(rdev
)))
2519 err
= lock_rdev(rdev
, newdev
, super_format
== -2);
2523 kobject_init(&rdev
->kobj
, &rdev_ktype
);
2526 rdev
->saved_raid_disk
= -1;
2527 rdev
->raid_disk
= -1;
2529 rdev
->data_offset
= 0;
2530 rdev
->sb_events
= 0;
2531 atomic_set(&rdev
->nr_pending
, 0);
2532 atomic_set(&rdev
->read_errors
, 0);
2533 atomic_set(&rdev
->corrected_errors
, 0);
2535 size
= rdev
->bdev
->bd_inode
->i_size
>> BLOCK_SIZE_BITS
;
2538 "md: %s has zero or unknown size, marking faulty!\n",
2539 bdevname(rdev
->bdev
,b
));
2544 if (super_format
>= 0) {
2545 err
= super_types
[super_format
].
2546 load_super(rdev
, NULL
, super_minor
);
2547 if (err
== -EINVAL
) {
2549 "md: %s does not have a valid v%d.%d "
2550 "superblock, not importing!\n",
2551 bdevname(rdev
->bdev
,b
),
2552 super_format
, super_minor
);
2557 "md: could not read %s's sb, not importing!\n",
2558 bdevname(rdev
->bdev
,b
));
2563 INIT_LIST_HEAD(&rdev
->same_set
);
2564 init_waitqueue_head(&rdev
->blocked_wait
);
2569 if (rdev
->sb_page
) {
2575 return ERR_PTR(err
);
2579 * Check a full RAID array for plausibility
2583 static void analyze_sbs(mddev_t
* mddev
)
2586 mdk_rdev_t
*rdev
, *freshest
, *tmp
;
2587 char b
[BDEVNAME_SIZE
];
2590 rdev_for_each(rdev
, tmp
, mddev
)
2591 switch (super_types
[mddev
->major_version
].
2592 load_super(rdev
, freshest
, mddev
->minor_version
)) {
2600 "md: fatal superblock inconsistency in %s"
2601 " -- removing from array\n",
2602 bdevname(rdev
->bdev
,b
));
2603 kick_rdev_from_array(rdev
);
2607 super_types
[mddev
->major_version
].
2608 validate_super(mddev
, freshest
);
2611 rdev_for_each(rdev
, tmp
, mddev
) {
2612 if (rdev
->desc_nr
>= mddev
->max_disks
||
2613 i
> mddev
->max_disks
) {
2615 "md: %s: %s: only %d devices permitted\n",
2616 mdname(mddev
), bdevname(rdev
->bdev
, b
),
2618 kick_rdev_from_array(rdev
);
2621 if (rdev
!= freshest
)
2622 if (super_types
[mddev
->major_version
].
2623 validate_super(mddev
, rdev
)) {
2624 printk(KERN_WARNING
"md: kicking non-fresh %s"
2626 bdevname(rdev
->bdev
,b
));
2627 kick_rdev_from_array(rdev
);
2630 if (mddev
->level
== LEVEL_MULTIPATH
) {
2631 rdev
->desc_nr
= i
++;
2632 rdev
->raid_disk
= rdev
->desc_nr
;
2633 set_bit(In_sync
, &rdev
->flags
);
2634 } else if (rdev
->raid_disk
>= (mddev
->raid_disks
- min(0, mddev
->delta_disks
))) {
2635 rdev
->raid_disk
= -1;
2636 clear_bit(In_sync
, &rdev
->flags
);
2641 static void md_safemode_timeout(unsigned long data
);
2644 safe_delay_show(mddev_t
*mddev
, char *page
)
2646 int msec
= (mddev
->safemode_delay
*1000)/HZ
;
2647 return sprintf(page
, "%d.%03d\n", msec
/1000, msec
%1000);
2650 safe_delay_store(mddev_t
*mddev
, const char *cbuf
, size_t len
)
2658 /* remove a period, and count digits after it */
2659 if (len
>= sizeof(buf
))
2661 strlcpy(buf
, cbuf
, sizeof(buf
));
2662 for (i
=0; i
<len
; i
++) {
2664 if (isdigit(buf
[i
])) {
2669 } else if (buf
[i
] == '.') {
2674 if (strict_strtoul(buf
, 10, &msec
) < 0)
2676 msec
= (msec
* 1000) / scale
;
2678 mddev
->safemode_delay
= 0;
2680 unsigned long old_delay
= mddev
->safemode_delay
;
2681 mddev
->safemode_delay
= (msec
*HZ
)/1000;
2682 if (mddev
->safemode_delay
== 0)
2683 mddev
->safemode_delay
= 1;
2684 if (mddev
->safemode_delay
< old_delay
)
2685 md_safemode_timeout((unsigned long)mddev
);
2689 static struct md_sysfs_entry md_safe_delay
=
2690 __ATTR(safe_mode_delay
, S_IRUGO
|S_IWUSR
,safe_delay_show
, safe_delay_store
);
2693 level_show(mddev_t
*mddev
, char *page
)
2695 struct mdk_personality
*p
= mddev
->pers
;
2697 return sprintf(page
, "%s\n", p
->name
);
2698 else if (mddev
->clevel
[0])
2699 return sprintf(page
, "%s\n", mddev
->clevel
);
2700 else if (mddev
->level
!= LEVEL_NONE
)
2701 return sprintf(page
, "%d\n", mddev
->level
);
2707 level_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2711 struct mdk_personality
*pers
;
2715 if (mddev
->pers
== NULL
) {
2718 if (len
>= sizeof(mddev
->clevel
))
2720 strncpy(mddev
->clevel
, buf
, len
);
2721 if (mddev
->clevel
[len
-1] == '\n')
2723 mddev
->clevel
[len
] = 0;
2724 mddev
->level
= LEVEL_NONE
;
2728 /* request to change the personality. Need to ensure:
2729 * - array is not engaged in resync/recovery/reshape
2730 * - old personality can be suspended
2731 * - new personality will access other array.
2734 if (mddev
->sync_thread
|| mddev
->reshape_position
!= MaxSector
)
2737 if (!mddev
->pers
->quiesce
) {
2738 printk(KERN_WARNING
"md: %s: %s does not support online personality change\n",
2739 mdname(mddev
), mddev
->pers
->name
);
2743 /* Now find the new personality */
2744 if (len
== 0 || len
>= sizeof(level
))
2746 strncpy(level
, buf
, len
);
2747 if (level
[len
-1] == '\n')
2751 request_module("md-%s", level
);
2752 spin_lock(&pers_lock
);
2753 pers
= find_pers(LEVEL_NONE
, level
);
2754 if (!pers
|| !try_module_get(pers
->owner
)) {
2755 spin_unlock(&pers_lock
);
2756 printk(KERN_WARNING
"md: personality %s not loaded\n", level
);
2759 spin_unlock(&pers_lock
);
2761 if (pers
== mddev
->pers
) {
2762 /* Nothing to do! */
2763 module_put(pers
->owner
);
2766 if (!pers
->takeover
) {
2767 module_put(pers
->owner
);
2768 printk(KERN_WARNING
"md: %s: %s does not support personality takeover\n",
2769 mdname(mddev
), level
);
2773 /* ->takeover must set new_* and/or delta_disks
2774 * if it succeeds, and may set them when it fails.
2776 priv
= pers
->takeover(mddev
);
2778 mddev
->new_level
= mddev
->level
;
2779 mddev
->new_layout
= mddev
->layout
;
2780 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
2781 mddev
->raid_disks
-= mddev
->delta_disks
;
2782 mddev
->delta_disks
= 0;
2783 module_put(pers
->owner
);
2784 printk(KERN_WARNING
"md: %s: %s would not accept array\n",
2785 mdname(mddev
), level
);
2786 return PTR_ERR(priv
);
2789 /* Looks like we have a winner */
2790 mddev_suspend(mddev
);
2791 mddev
->pers
->stop(mddev
);
2792 module_put(mddev
->pers
->owner
);
2793 /* Invalidate devices that are now superfluous */
2794 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
2795 if (rdev
->raid_disk
>= mddev
->raid_disks
) {
2796 rdev
->raid_disk
= -1;
2797 clear_bit(In_sync
, &rdev
->flags
);
2800 mddev
->private = priv
;
2801 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
2802 mddev
->level
= mddev
->new_level
;
2803 mddev
->layout
= mddev
->new_layout
;
2804 mddev
->chunk_sectors
= mddev
->new_chunk_sectors
;
2805 mddev
->delta_disks
= 0;
2807 mddev_resume(mddev
);
2808 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
2809 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
2810 md_wakeup_thread(mddev
->thread
);
2814 static struct md_sysfs_entry md_level
=
2815 __ATTR(level
, S_IRUGO
|S_IWUSR
, level_show
, level_store
);
2819 layout_show(mddev_t
*mddev
, char *page
)
2821 /* just a number, not meaningful for all levels */
2822 if (mddev
->reshape_position
!= MaxSector
&&
2823 mddev
->layout
!= mddev
->new_layout
)
2824 return sprintf(page
, "%d (%d)\n",
2825 mddev
->new_layout
, mddev
->layout
);
2826 return sprintf(page
, "%d\n", mddev
->layout
);
2830 layout_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2833 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2835 if (!*buf
|| (*e
&& *e
!= '\n'))
2840 if (mddev
->pers
->check_reshape
== NULL
)
2842 mddev
->new_layout
= n
;
2843 err
= mddev
->pers
->check_reshape(mddev
);
2845 mddev
->new_layout
= mddev
->layout
;
2849 mddev
->new_layout
= n
;
2850 if (mddev
->reshape_position
== MaxSector
)
2855 static struct md_sysfs_entry md_layout
=
2856 __ATTR(layout
, S_IRUGO
|S_IWUSR
, layout_show
, layout_store
);
2860 raid_disks_show(mddev_t
*mddev
, char *page
)
2862 if (mddev
->raid_disks
== 0)
2864 if (mddev
->reshape_position
!= MaxSector
&&
2865 mddev
->delta_disks
!= 0)
2866 return sprintf(page
, "%d (%d)\n", mddev
->raid_disks
,
2867 mddev
->raid_disks
- mddev
->delta_disks
);
2868 return sprintf(page
, "%d\n", mddev
->raid_disks
);
2871 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
);
2874 raid_disks_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2878 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2880 if (!*buf
|| (*e
&& *e
!= '\n'))
2884 rv
= update_raid_disks(mddev
, n
);
2885 else if (mddev
->reshape_position
!= MaxSector
) {
2886 int olddisks
= mddev
->raid_disks
- mddev
->delta_disks
;
2887 mddev
->delta_disks
= n
- olddisks
;
2888 mddev
->raid_disks
= n
;
2890 mddev
->raid_disks
= n
;
2891 return rv
? rv
: len
;
2893 static struct md_sysfs_entry md_raid_disks
=
2894 __ATTR(raid_disks
, S_IRUGO
|S_IWUSR
, raid_disks_show
, raid_disks_store
);
2897 chunk_size_show(mddev_t
*mddev
, char *page
)
2899 if (mddev
->reshape_position
!= MaxSector
&&
2900 mddev
->chunk_sectors
!= mddev
->new_chunk_sectors
)
2901 return sprintf(page
, "%d (%d)\n",
2902 mddev
->new_chunk_sectors
<< 9,
2903 mddev
->chunk_sectors
<< 9);
2904 return sprintf(page
, "%d\n", mddev
->chunk_sectors
<< 9);
2908 chunk_size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2911 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2913 if (!*buf
|| (*e
&& *e
!= '\n'))
2918 if (mddev
->pers
->check_reshape
== NULL
)
2920 mddev
->new_chunk_sectors
= n
>> 9;
2921 err
= mddev
->pers
->check_reshape(mddev
);
2923 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
2927 mddev
->new_chunk_sectors
= n
>> 9;
2928 if (mddev
->reshape_position
== MaxSector
)
2929 mddev
->chunk_sectors
= n
>> 9;
2933 static struct md_sysfs_entry md_chunk_size
=
2934 __ATTR(chunk_size
, S_IRUGO
|S_IWUSR
, chunk_size_show
, chunk_size_store
);
2937 resync_start_show(mddev_t
*mddev
, char *page
)
2939 if (mddev
->recovery_cp
== MaxSector
)
2940 return sprintf(page
, "none\n");
2941 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->recovery_cp
);
2945 resync_start_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2948 unsigned long long n
= simple_strtoull(buf
, &e
, 10);
2952 if (!*buf
|| (*e
&& *e
!= '\n'))
2955 mddev
->recovery_cp
= n
;
2958 static struct md_sysfs_entry md_resync_start
=
2959 __ATTR(resync_start
, S_IRUGO
|S_IWUSR
, resync_start_show
, resync_start_store
);
2962 * The array state can be:
2965 * No devices, no size, no level
2966 * Equivalent to STOP_ARRAY ioctl
2968 * May have some settings, but array is not active
2969 * all IO results in error
2970 * When written, doesn't tear down array, but just stops it
2971 * suspended (not supported yet)
2972 * All IO requests will block. The array can be reconfigured.
2973 * Writing this, if accepted, will block until array is quiescent
2975 * no resync can happen. no superblocks get written.
2976 * write requests fail
2978 * like readonly, but behaves like 'clean' on a write request.
2980 * clean - no pending writes, but otherwise active.
2981 * When written to inactive array, starts without resync
2982 * If a write request arrives then
2983 * if metadata is known, mark 'dirty' and switch to 'active'.
2984 * if not known, block and switch to write-pending
2985 * If written to an active array that has pending writes, then fails.
2987 * fully active: IO and resync can be happening.
2988 * When written to inactive array, starts with resync
2991 * clean, but writes are blocked waiting for 'active' to be written.
2994 * like active, but no writes have been seen for a while (100msec).
2997 enum array_state
{ clear
, inactive
, suspended
, readonly
, read_auto
, clean
, active
,
2998 write_pending
, active_idle
, bad_word
};
2999 static char *array_states
[] = {
3000 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3001 "write-pending", "active-idle", NULL
};
3003 static int match_word(const char *word
, char **list
)
3006 for (n
=0; list
[n
]; n
++)
3007 if (cmd_match(word
, list
[n
]))
3013 array_state_show(mddev_t
*mddev
, char *page
)
3015 enum array_state st
= inactive
;
3028 else if (test_bit(MD_CHANGE_CLEAN
, &mddev
->flags
))
3030 else if (mddev
->safemode
)
3036 if (list_empty(&mddev
->disks
) &&
3037 mddev
->raid_disks
== 0 &&
3038 mddev
->dev_sectors
== 0)
3043 return sprintf(page
, "%s\n", array_states
[st
]);
3046 static int do_md_stop(mddev_t
* mddev
, int ro
, int is_open
);
3047 static int do_md_run(mddev_t
* mddev
);
3048 static int restart_array(mddev_t
*mddev
);
3051 array_state_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3054 enum array_state st
= match_word(buf
, array_states
);
3059 /* stopping an active array */
3060 if (atomic_read(&mddev
->openers
) > 0)
3062 err
= do_md_stop(mddev
, 0, 0);
3065 /* stopping an active array */
3067 if (atomic_read(&mddev
->openers
) > 0)
3069 err
= do_md_stop(mddev
, 2, 0);
3071 err
= 0; /* already inactive */
3074 break; /* not supported yet */
3077 err
= do_md_stop(mddev
, 1, 0);
3080 set_disk_ro(mddev
->gendisk
, 1);
3081 err
= do_md_run(mddev
);
3087 err
= do_md_stop(mddev
, 1, 0);
3088 else if (mddev
->ro
== 1)
3089 err
= restart_array(mddev
);
3092 set_disk_ro(mddev
->gendisk
, 0);
3096 err
= do_md_run(mddev
);
3101 restart_array(mddev
);
3102 spin_lock_irq(&mddev
->write_lock
);
3103 if (atomic_read(&mddev
->writes_pending
) == 0) {
3104 if (mddev
->in_sync
== 0) {
3106 if (mddev
->safemode
== 1)
3107 mddev
->safemode
= 0;
3108 if (mddev
->persistent
)
3109 set_bit(MD_CHANGE_CLEAN
,
3115 spin_unlock_irq(&mddev
->write_lock
);
3121 restart_array(mddev
);
3122 if (mddev
->external
)
3123 clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
3124 wake_up(&mddev
->sb_wait
);
3128 set_disk_ro(mddev
->gendisk
, 0);
3129 err
= do_md_run(mddev
);
3134 /* these cannot be set */
3140 sysfs_notify_dirent(mddev
->sysfs_state
);
3144 static struct md_sysfs_entry md_array_state
=
3145 __ATTR(array_state
, S_IRUGO
|S_IWUSR
, array_state_show
, array_state_store
);
3148 null_show(mddev_t
*mddev
, char *page
)
3154 new_dev_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3156 /* buf must be %d:%d\n? giving major and minor numbers */
3157 /* The new device is added to the array.
3158 * If the array has a persistent superblock, we read the
3159 * superblock to initialise info and check validity.
3160 * Otherwise, only checking done is that in bind_rdev_to_array,
3161 * which mainly checks size.
3164 int major
= simple_strtoul(buf
, &e
, 10);
3170 if (!*buf
|| *e
!= ':' || !e
[1] || e
[1] == '\n')
3172 minor
= simple_strtoul(e
+1, &e
, 10);
3173 if (*e
&& *e
!= '\n')
3175 dev
= MKDEV(major
, minor
);
3176 if (major
!= MAJOR(dev
) ||
3177 minor
!= MINOR(dev
))
3181 if (mddev
->persistent
) {
3182 rdev
= md_import_device(dev
, mddev
->major_version
,
3183 mddev
->minor_version
);
3184 if (!IS_ERR(rdev
) && !list_empty(&mddev
->disks
)) {
3185 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
3186 mdk_rdev_t
, same_set
);
3187 err
= super_types
[mddev
->major_version
]
3188 .load_super(rdev
, rdev0
, mddev
->minor_version
);
3192 } else if (mddev
->external
)
3193 rdev
= md_import_device(dev
, -2, -1);
3195 rdev
= md_import_device(dev
, -1, -1);
3198 return PTR_ERR(rdev
);
3199 err
= bind_rdev_to_array(rdev
, mddev
);
3203 return err
? err
: len
;
3206 static struct md_sysfs_entry md_new_device
=
3207 __ATTR(new_dev
, S_IWUSR
, null_show
, new_dev_store
);
3210 bitmap_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3213 unsigned long chunk
, end_chunk
;
3217 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3219 chunk
= end_chunk
= simple_strtoul(buf
, &end
, 0);
3220 if (buf
== end
) break;
3221 if (*end
== '-') { /* range */
3223 end_chunk
= simple_strtoul(buf
, &end
, 0);
3224 if (buf
== end
) break;
3226 if (*end
&& !isspace(*end
)) break;
3227 bitmap_dirty_bits(mddev
->bitmap
, chunk
, end_chunk
);
3229 while (isspace(*buf
)) buf
++;
3231 bitmap_unplug(mddev
->bitmap
); /* flush the bits to disk */
3236 static struct md_sysfs_entry md_bitmap
=
3237 __ATTR(bitmap_set_bits
, S_IWUSR
, null_show
, bitmap_store
);
3240 size_show(mddev_t
*mddev
, char *page
)
3242 return sprintf(page
, "%llu\n",
3243 (unsigned long long)mddev
->dev_sectors
/ 2);
3246 static int update_size(mddev_t
*mddev
, sector_t num_sectors
);
3249 size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3251 /* If array is inactive, we can reduce the component size, but
3252 * not increase it (except from 0).
3253 * If array is active, we can try an on-line resize
3256 int err
= strict_blocks_to_sectors(buf
, §ors
);
3261 err
= update_size(mddev
, sectors
);
3262 md_update_sb(mddev
, 1);
3264 if (mddev
->dev_sectors
== 0 ||
3265 mddev
->dev_sectors
> sectors
)
3266 mddev
->dev_sectors
= sectors
;
3270 return err
? err
: len
;
3273 static struct md_sysfs_entry md_size
=
3274 __ATTR(component_size
, S_IRUGO
|S_IWUSR
, size_show
, size_store
);
3279 * 'none' for arrays with no metadata (good luck...)
3280 * 'external' for arrays with externally managed metadata,
3281 * or N.M for internally known formats
3284 metadata_show(mddev_t
*mddev
, char *page
)
3286 if (mddev
->persistent
)
3287 return sprintf(page
, "%d.%d\n",
3288 mddev
->major_version
, mddev
->minor_version
);
3289 else if (mddev
->external
)
3290 return sprintf(page
, "external:%s\n", mddev
->metadata_type
);
3292 return sprintf(page
, "none\n");
3296 metadata_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3300 /* Changing the details of 'external' metadata is
3301 * always permitted. Otherwise there must be
3302 * no devices attached to the array.
3304 if (mddev
->external
&& strncmp(buf
, "external:", 9) == 0)
3306 else if (!list_empty(&mddev
->disks
))
3309 if (cmd_match(buf
, "none")) {
3310 mddev
->persistent
= 0;
3311 mddev
->external
= 0;
3312 mddev
->major_version
= 0;
3313 mddev
->minor_version
= 90;
3316 if (strncmp(buf
, "external:", 9) == 0) {
3317 size_t namelen
= len
-9;
3318 if (namelen
>= sizeof(mddev
->metadata_type
))
3319 namelen
= sizeof(mddev
->metadata_type
)-1;
3320 strncpy(mddev
->metadata_type
, buf
+9, namelen
);
3321 mddev
->metadata_type
[namelen
] = 0;
3322 if (namelen
&& mddev
->metadata_type
[namelen
-1] == '\n')
3323 mddev
->metadata_type
[--namelen
] = 0;
3324 mddev
->persistent
= 0;
3325 mddev
->external
= 1;
3326 mddev
->major_version
= 0;
3327 mddev
->minor_version
= 90;
3330 major
= simple_strtoul(buf
, &e
, 10);
3331 if (e
==buf
|| *e
!= '.')
3334 minor
= simple_strtoul(buf
, &e
, 10);
3335 if (e
==buf
|| (*e
&& *e
!= '\n') )
3337 if (major
>= ARRAY_SIZE(super_types
) || super_types
[major
].name
== NULL
)
3339 mddev
->major_version
= major
;
3340 mddev
->minor_version
= minor
;
3341 mddev
->persistent
= 1;
3342 mddev
->external
= 0;
3346 static struct md_sysfs_entry md_metadata
=
3347 __ATTR(metadata_version
, S_IRUGO
|S_IWUSR
, metadata_show
, metadata_store
);
3350 action_show(mddev_t
*mddev
, char *page
)
3352 char *type
= "idle";
3353 if (test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
3355 else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
3356 (!mddev
->ro
&& test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))) {
3357 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
3359 else if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
3360 if (!test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
3362 else if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
3366 } else if (test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
))
3369 return sprintf(page
, "%s\n", type
);
3373 action_store(mddev_t
*mddev
, const char *page
, size_t len
)
3375 if (!mddev
->pers
|| !mddev
->pers
->sync_request
)
3378 if (cmd_match(page
, "frozen"))
3379 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
3381 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
3383 if (cmd_match(page
, "idle") || cmd_match(page
, "frozen")) {
3384 if (mddev
->sync_thread
) {
3385 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
3386 md_unregister_thread(mddev
->sync_thread
);
3387 mddev
->sync_thread
= NULL
;
3388 mddev
->recovery
= 0;
3390 } else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
3391 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
3393 else if (cmd_match(page
, "resync"))
3394 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3395 else if (cmd_match(page
, "recover")) {
3396 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
3397 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3398 } else if (cmd_match(page
, "reshape")) {
3400 if (mddev
->pers
->start_reshape
== NULL
)
3402 err
= mddev
->pers
->start_reshape(mddev
);
3405 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
3407 if (cmd_match(page
, "check"))
3408 set_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
3409 else if (!cmd_match(page
, "repair"))
3411 set_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
3412 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
3414 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3415 md_wakeup_thread(mddev
->thread
);
3416 sysfs_notify_dirent(mddev
->sysfs_action
);
3421 mismatch_cnt_show(mddev_t
*mddev
, char *page
)
3423 return sprintf(page
, "%llu\n",
3424 (unsigned long long) mddev
->resync_mismatches
);
3427 static struct md_sysfs_entry md_scan_mode
=
3428 __ATTR(sync_action
, S_IRUGO
|S_IWUSR
, action_show
, action_store
);
3431 static struct md_sysfs_entry md_mismatches
= __ATTR_RO(mismatch_cnt
);
3434 sync_min_show(mddev_t
*mddev
, char *page
)
3436 return sprintf(page
, "%d (%s)\n", speed_min(mddev
),
3437 mddev
->sync_speed_min
? "local": "system");
3441 sync_min_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3445 if (strncmp(buf
, "system", 6)==0) {
3446 mddev
->sync_speed_min
= 0;
3449 min
= simple_strtoul(buf
, &e
, 10);
3450 if (buf
== e
|| (*e
&& *e
!= '\n') || min
<= 0)
3452 mddev
->sync_speed_min
= min
;
3456 static struct md_sysfs_entry md_sync_min
=
3457 __ATTR(sync_speed_min
, S_IRUGO
|S_IWUSR
, sync_min_show
, sync_min_store
);
3460 sync_max_show(mddev_t
*mddev
, char *page
)
3462 return sprintf(page
, "%d (%s)\n", speed_max(mddev
),
3463 mddev
->sync_speed_max
? "local": "system");
3467 sync_max_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3471 if (strncmp(buf
, "system", 6)==0) {
3472 mddev
->sync_speed_max
= 0;
3475 max
= simple_strtoul(buf
, &e
, 10);
3476 if (buf
== e
|| (*e
&& *e
!= '\n') || max
<= 0)
3478 mddev
->sync_speed_max
= max
;
3482 static struct md_sysfs_entry md_sync_max
=
3483 __ATTR(sync_speed_max
, S_IRUGO
|S_IWUSR
, sync_max_show
, sync_max_store
);
3486 degraded_show(mddev_t
*mddev
, char *page
)
3488 return sprintf(page
, "%d\n", mddev
->degraded
);
3490 static struct md_sysfs_entry md_degraded
= __ATTR_RO(degraded
);
3493 sync_force_parallel_show(mddev_t
*mddev
, char *page
)
3495 return sprintf(page
, "%d\n", mddev
->parallel_resync
);
3499 sync_force_parallel_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3503 if (strict_strtol(buf
, 10, &n
))
3506 if (n
!= 0 && n
!= 1)
3509 mddev
->parallel_resync
= n
;
3511 if (mddev
->sync_thread
)
3512 wake_up(&resync_wait
);
3517 /* force parallel resync, even with shared block devices */
3518 static struct md_sysfs_entry md_sync_force_parallel
=
3519 __ATTR(sync_force_parallel
, S_IRUGO
|S_IWUSR
,
3520 sync_force_parallel_show
, sync_force_parallel_store
);
3523 sync_speed_show(mddev_t
*mddev
, char *page
)
3525 unsigned long resync
, dt
, db
;
3526 if (mddev
->curr_resync
== 0)
3527 return sprintf(page
, "none\n");
3528 resync
= mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
);
3529 dt
= (jiffies
- mddev
->resync_mark
) / HZ
;
3531 db
= resync
- mddev
->resync_mark_cnt
;
3532 return sprintf(page
, "%lu\n", db
/dt
/2); /* K/sec */
3535 static struct md_sysfs_entry md_sync_speed
= __ATTR_RO(sync_speed
);
3538 sync_completed_show(mddev_t
*mddev
, char *page
)
3540 unsigned long max_sectors
, resync
;
3542 if (!test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3543 return sprintf(page
, "none\n");
3545 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
3546 max_sectors
= mddev
->resync_max_sectors
;
3548 max_sectors
= mddev
->dev_sectors
;
3550 resync
= mddev
->curr_resync_completed
;
3551 return sprintf(page
, "%lu / %lu\n", resync
, max_sectors
);
3554 static struct md_sysfs_entry md_sync_completed
= __ATTR_RO(sync_completed
);
3557 min_sync_show(mddev_t
*mddev
, char *page
)
3559 return sprintf(page
, "%llu\n",
3560 (unsigned long long)mddev
->resync_min
);
3563 min_sync_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3565 unsigned long long min
;
3566 if (strict_strtoull(buf
, 10, &min
))
3568 if (min
> mddev
->resync_max
)
3570 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3573 /* Must be a multiple of chunk_size */
3574 if (mddev
->chunk_sectors
) {
3575 sector_t temp
= min
;
3576 if (sector_div(temp
, mddev
->chunk_sectors
))
3579 mddev
->resync_min
= min
;
3584 static struct md_sysfs_entry md_min_sync
=
3585 __ATTR(sync_min
, S_IRUGO
|S_IWUSR
, min_sync_show
, min_sync_store
);
3588 max_sync_show(mddev_t
*mddev
, char *page
)
3590 if (mddev
->resync_max
== MaxSector
)
3591 return sprintf(page
, "max\n");
3593 return sprintf(page
, "%llu\n",
3594 (unsigned long long)mddev
->resync_max
);
3597 max_sync_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3599 if (strncmp(buf
, "max", 3) == 0)
3600 mddev
->resync_max
= MaxSector
;
3602 unsigned long long max
;
3603 if (strict_strtoull(buf
, 10, &max
))
3605 if (max
< mddev
->resync_min
)
3607 if (max
< mddev
->resync_max
&&
3609 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3612 /* Must be a multiple of chunk_size */
3613 if (mddev
->chunk_sectors
) {
3614 sector_t temp
= max
;
3615 if (sector_div(temp
, mddev
->chunk_sectors
))
3618 mddev
->resync_max
= max
;
3620 wake_up(&mddev
->recovery_wait
);
3624 static struct md_sysfs_entry md_max_sync
=
3625 __ATTR(sync_max
, S_IRUGO
|S_IWUSR
, max_sync_show
, max_sync_store
);
3628 suspend_lo_show(mddev_t
*mddev
, char *page
)
3630 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_lo
);
3634 suspend_lo_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3637 unsigned long long new = simple_strtoull(buf
, &e
, 10);
3639 if (mddev
->pers
== NULL
||
3640 mddev
->pers
->quiesce
== NULL
)
3642 if (buf
== e
|| (*e
&& *e
!= '\n'))
3644 if (new >= mddev
->suspend_hi
||
3645 (new > mddev
->suspend_lo
&& new < mddev
->suspend_hi
)) {
3646 mddev
->suspend_lo
= new;
3647 mddev
->pers
->quiesce(mddev
, 2);
3652 static struct md_sysfs_entry md_suspend_lo
=
3653 __ATTR(suspend_lo
, S_IRUGO
|S_IWUSR
, suspend_lo_show
, suspend_lo_store
);
3657 suspend_hi_show(mddev_t
*mddev
, char *page
)
3659 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_hi
);
3663 suspend_hi_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3666 unsigned long long new = simple_strtoull(buf
, &e
, 10);
3668 if (mddev
->pers
== NULL
||
3669 mddev
->pers
->quiesce
== NULL
)
3671 if (buf
== e
|| (*e
&& *e
!= '\n'))
3673 if ((new <= mddev
->suspend_lo
&& mddev
->suspend_lo
>= mddev
->suspend_hi
) ||
3674 (new > mddev
->suspend_lo
&& new > mddev
->suspend_hi
)) {
3675 mddev
->suspend_hi
= new;
3676 mddev
->pers
->quiesce(mddev
, 1);
3677 mddev
->pers
->quiesce(mddev
, 0);
3682 static struct md_sysfs_entry md_suspend_hi
=
3683 __ATTR(suspend_hi
, S_IRUGO
|S_IWUSR
, suspend_hi_show
, suspend_hi_store
);
3686 reshape_position_show(mddev_t
*mddev
, char *page
)
3688 if (mddev
->reshape_position
!= MaxSector
)
3689 return sprintf(page
, "%llu\n",
3690 (unsigned long long)mddev
->reshape_position
);
3691 strcpy(page
, "none\n");
3696 reshape_position_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3699 unsigned long long new = simple_strtoull(buf
, &e
, 10);
3702 if (buf
== e
|| (*e
&& *e
!= '\n'))
3704 mddev
->reshape_position
= new;
3705 mddev
->delta_disks
= 0;
3706 mddev
->new_level
= mddev
->level
;
3707 mddev
->new_layout
= mddev
->layout
;
3708 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3712 static struct md_sysfs_entry md_reshape_position
=
3713 __ATTR(reshape_position
, S_IRUGO
|S_IWUSR
, reshape_position_show
,
3714 reshape_position_store
);
3717 array_size_show(mddev_t
*mddev
, char *page
)
3719 if (mddev
->external_size
)
3720 return sprintf(page
, "%llu\n",
3721 (unsigned long long)mddev
->array_sectors
/2);
3723 return sprintf(page
, "default\n");
3727 array_size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3731 if (strncmp(buf
, "default", 7) == 0) {
3733 sectors
= mddev
->pers
->size(mddev
, 0, 0);
3735 sectors
= mddev
->array_sectors
;
3737 mddev
->external_size
= 0;
3739 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
3741 if (mddev
->pers
&& mddev
->pers
->size(mddev
, 0, 0) < sectors
)
3744 mddev
->external_size
= 1;
3747 mddev
->array_sectors
= sectors
;
3748 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
3750 revalidate_disk(mddev
->gendisk
);
3755 static struct md_sysfs_entry md_array_size
=
3756 __ATTR(array_size
, S_IRUGO
|S_IWUSR
, array_size_show
,
3759 static struct attribute
*md_default_attrs
[] = {
3762 &md_raid_disks
.attr
,
3763 &md_chunk_size
.attr
,
3765 &md_resync_start
.attr
,
3767 &md_new_device
.attr
,
3768 &md_safe_delay
.attr
,
3769 &md_array_state
.attr
,
3770 &md_reshape_position
.attr
,
3771 &md_array_size
.attr
,
3775 static struct attribute
*md_redundancy_attrs
[] = {
3777 &md_mismatches
.attr
,
3780 &md_sync_speed
.attr
,
3781 &md_sync_force_parallel
.attr
,
3782 &md_sync_completed
.attr
,
3785 &md_suspend_lo
.attr
,
3786 &md_suspend_hi
.attr
,
3791 static struct attribute_group md_redundancy_group
= {
3793 .attrs
= md_redundancy_attrs
,
3798 md_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
3800 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
3801 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
3806 rv
= mddev_lock(mddev
);
3808 rv
= entry
->show(mddev
, page
);
3809 mddev_unlock(mddev
);
3815 md_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
3816 const char *page
, size_t length
)
3818 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
3819 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
3824 if (!capable(CAP_SYS_ADMIN
))
3826 rv
= mddev_lock(mddev
);
3827 if (mddev
->hold_active
== UNTIL_IOCTL
)
3828 mddev
->hold_active
= 0;
3830 rv
= entry
->store(mddev
, page
, length
);
3831 mddev_unlock(mddev
);
3836 static void md_free(struct kobject
*ko
)
3838 mddev_t
*mddev
= container_of(ko
, mddev_t
, kobj
);
3840 if (mddev
->sysfs_state
)
3841 sysfs_put(mddev
->sysfs_state
);
3843 if (mddev
->gendisk
) {
3844 del_gendisk(mddev
->gendisk
);
3845 put_disk(mddev
->gendisk
);
3848 blk_cleanup_queue(mddev
->queue
);
3853 static struct sysfs_ops md_sysfs_ops
= {
3854 .show
= md_attr_show
,
3855 .store
= md_attr_store
,
3857 static struct kobj_type md_ktype
= {
3859 .sysfs_ops
= &md_sysfs_ops
,
3860 .default_attrs
= md_default_attrs
,
3865 static void mddev_delayed_delete(struct work_struct
*ws
)
3867 mddev_t
*mddev
= container_of(ws
, mddev_t
, del_work
);
3869 if (mddev
->private == &md_redundancy_group
) {
3870 sysfs_remove_group(&mddev
->kobj
, &md_redundancy_group
);
3871 if (mddev
->sysfs_action
)
3872 sysfs_put(mddev
->sysfs_action
);
3873 mddev
->sysfs_action
= NULL
;
3874 mddev
->private = NULL
;
3876 kobject_del(&mddev
->kobj
);
3877 kobject_put(&mddev
->kobj
);
3880 static int md_alloc(dev_t dev
, char *name
)
3882 static DEFINE_MUTEX(disks_mutex
);
3883 mddev_t
*mddev
= mddev_find(dev
);
3884 struct gendisk
*disk
;
3893 partitioned
= (MAJOR(mddev
->unit
) != MD_MAJOR
);
3894 shift
= partitioned
? MdpMinorShift
: 0;
3895 unit
= MINOR(mddev
->unit
) >> shift
;
3897 /* wait for any previous instance if this device
3898 * to be completed removed (mddev_delayed_delete).
3900 flush_scheduled_work();
3902 mutex_lock(&disks_mutex
);
3908 /* Need to ensure that 'name' is not a duplicate.
3911 spin_lock(&all_mddevs_lock
);
3913 list_for_each_entry(mddev2
, &all_mddevs
, all_mddevs
)
3914 if (mddev2
->gendisk
&&
3915 strcmp(mddev2
->gendisk
->disk_name
, name
) == 0) {
3916 spin_unlock(&all_mddevs_lock
);
3919 spin_unlock(&all_mddevs_lock
);
3923 mddev
->queue
= blk_alloc_queue(GFP_KERNEL
);
3926 mddev
->queue
->queuedata
= mddev
;
3928 /* Can be unlocked because the queue is new: no concurrency */
3929 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER
, mddev
->queue
);
3931 blk_queue_make_request(mddev
->queue
, md_make_request
);
3933 disk
= alloc_disk(1 << shift
);
3935 blk_cleanup_queue(mddev
->queue
);
3936 mddev
->queue
= NULL
;
3939 disk
->major
= MAJOR(mddev
->unit
);
3940 disk
->first_minor
= unit
<< shift
;
3942 strcpy(disk
->disk_name
, name
);
3943 else if (partitioned
)
3944 sprintf(disk
->disk_name
, "md_d%d", unit
);
3946 sprintf(disk
->disk_name
, "md%d", unit
);
3947 disk
->fops
= &md_fops
;
3948 disk
->private_data
= mddev
;
3949 disk
->queue
= mddev
->queue
;
3950 /* Allow extended partitions. This makes the
3951 * 'mdp' device redundant, but we can't really
3954 disk
->flags
|= GENHD_FL_EXT_DEVT
;
3956 mddev
->gendisk
= disk
;
3957 error
= kobject_init_and_add(&mddev
->kobj
, &md_ktype
,
3958 &disk_to_dev(disk
)->kobj
, "%s", "md");
3960 /* This isn't possible, but as kobject_init_and_add is marked
3961 * __must_check, we must do something with the result
3963 printk(KERN_WARNING
"md: cannot register %s/md - name in use\n",
3968 mutex_unlock(&disks_mutex
);
3970 kobject_uevent(&mddev
->kobj
, KOBJ_ADD
);
3971 mddev
->sysfs_state
= sysfs_get_dirent(mddev
->kobj
.sd
, "array_state");
3977 static struct kobject
*md_probe(dev_t dev
, int *part
, void *data
)
3979 md_alloc(dev
, NULL
);
3983 static int add_named_array(const char *val
, struct kernel_param
*kp
)
3985 /* val must be "md_*" where * is not all digits.
3986 * We allocate an array with a large free minor number, and
3987 * set the name to val. val must not already be an active name.
3989 int len
= strlen(val
);
3990 char buf
[DISK_NAME_LEN
];
3992 while (len
&& val
[len
-1] == '\n')
3994 if (len
>= DISK_NAME_LEN
)
3996 strlcpy(buf
, val
, len
+1);
3997 if (strncmp(buf
, "md_", 3) != 0)
3999 return md_alloc(0, buf
);
4002 static void md_safemode_timeout(unsigned long data
)
4004 mddev_t
*mddev
= (mddev_t
*) data
;
4006 if (!atomic_read(&mddev
->writes_pending
)) {
4007 mddev
->safemode
= 1;
4008 if (mddev
->external
)
4009 sysfs_notify_dirent(mddev
->sysfs_state
);
4011 md_wakeup_thread(mddev
->thread
);
4014 static int start_dirty_degraded
;
4016 static int do_md_run(mddev_t
* mddev
)
4020 struct gendisk
*disk
;
4021 struct mdk_personality
*pers
;
4023 if (list_empty(&mddev
->disks
))
4024 /* cannot run an array with no devices.. */
4031 * Analyze all RAID superblock(s)
4033 if (!mddev
->raid_disks
) {
4034 if (!mddev
->persistent
)
4039 if (mddev
->level
!= LEVEL_NONE
)
4040 request_module("md-level-%d", mddev
->level
);
4041 else if (mddev
->clevel
[0])
4042 request_module("md-%s", mddev
->clevel
);
4045 * Drop all container device buffers, from now on
4046 * the only valid external interface is through the md
4049 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
4050 if (test_bit(Faulty
, &rdev
->flags
))
4052 sync_blockdev(rdev
->bdev
);
4053 invalidate_bdev(rdev
->bdev
);
4055 /* perform some consistency tests on the device.
4056 * We don't want the data to overlap the metadata,
4057 * Internal Bitmap issues have been handled elsewhere.
4059 if (rdev
->data_offset
< rdev
->sb_start
) {
4060 if (mddev
->dev_sectors
&&
4061 rdev
->data_offset
+ mddev
->dev_sectors
4063 printk("md: %s: data overlaps metadata\n",
4068 if (rdev
->sb_start
+ rdev
->sb_size
/512
4069 > rdev
->data_offset
) {
4070 printk("md: %s: metadata overlaps data\n",
4075 sysfs_notify_dirent(rdev
->sysfs_state
);
4078 md_probe(mddev
->unit
, NULL
, NULL
);
4079 disk
= mddev
->gendisk
;
4083 spin_lock(&pers_lock
);
4084 pers
= find_pers(mddev
->level
, mddev
->clevel
);
4085 if (!pers
|| !try_module_get(pers
->owner
)) {
4086 spin_unlock(&pers_lock
);
4087 if (mddev
->level
!= LEVEL_NONE
)
4088 printk(KERN_WARNING
"md: personality for level %d is not loaded!\n",
4091 printk(KERN_WARNING
"md: personality for level %s is not loaded!\n",
4096 spin_unlock(&pers_lock
);
4097 if (mddev
->level
!= pers
->level
) {
4098 mddev
->level
= pers
->level
;
4099 mddev
->new_level
= pers
->level
;
4101 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
4103 if (mddev
->reshape_position
!= MaxSector
&&
4104 pers
->start_reshape
== NULL
) {
4105 /* This personality cannot handle reshaping... */
4107 module_put(pers
->owner
);
4111 if (pers
->sync_request
) {
4112 /* Warn if this is a potentially silly
4115 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
4119 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4120 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
4122 rdev
->bdev
->bd_contains
==
4123 rdev2
->bdev
->bd_contains
) {
4125 "%s: WARNING: %s appears to be"
4126 " on the same physical disk as"
4129 bdevname(rdev
->bdev
,b
),
4130 bdevname(rdev2
->bdev
,b2
));
4137 "True protection against single-disk"
4138 " failure might be compromised.\n");
4141 mddev
->recovery
= 0;
4142 /* may be over-ridden by personality */
4143 mddev
->resync_max_sectors
= mddev
->dev_sectors
;
4145 mddev
->barriers_work
= 1;
4146 mddev
->ok_start_degraded
= start_dirty_degraded
;
4149 mddev
->ro
= 2; /* read-only, but switch on first write */
4151 err
= mddev
->pers
->run(mddev
);
4153 printk(KERN_ERR
"md: pers->run() failed ...\n");
4154 else if (mddev
->pers
->size(mddev
, 0, 0) < mddev
->array_sectors
) {
4155 WARN_ONCE(!mddev
->external_size
, "%s: default size too small,"
4156 " but 'external_size' not in effect?\n", __func__
);
4158 "md: invalid array_size %llu > default size %llu\n",
4159 (unsigned long long)mddev
->array_sectors
/ 2,
4160 (unsigned long long)mddev
->pers
->size(mddev
, 0, 0) / 2);
4162 mddev
->pers
->stop(mddev
);
4164 if (err
== 0 && mddev
->pers
->sync_request
) {
4165 err
= bitmap_create(mddev
);
4167 printk(KERN_ERR
"%s: failed to create bitmap (%d)\n",
4168 mdname(mddev
), err
);
4169 mddev
->pers
->stop(mddev
);
4173 module_put(mddev
->pers
->owner
);
4175 bitmap_destroy(mddev
);
4178 if (mddev
->pers
->sync_request
) {
4179 if (sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
4181 "md: cannot register extra attributes for %s\n",
4183 mddev
->sysfs_action
= sysfs_get_dirent(mddev
->kobj
.sd
, "sync_action");
4184 } else if (mddev
->ro
== 2) /* auto-readonly not meaningful */
4187 atomic_set(&mddev
->writes_pending
,0);
4188 mddev
->safemode
= 0;
4189 mddev
->safemode_timer
.function
= md_safemode_timeout
;
4190 mddev
->safemode_timer
.data
= (unsigned long) mddev
;
4191 mddev
->safemode_delay
= (200 * HZ
)/1000 +1; /* 200 msec delay */
4194 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4195 if (rdev
->raid_disk
>= 0) {
4197 sprintf(nm
, "rd%d", rdev
->raid_disk
);
4198 if (sysfs_create_link(&mddev
->kobj
, &rdev
->kobj
, nm
))
4199 printk("md: cannot register %s for %s\n",
4203 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4206 md_update_sb(mddev
, 0);
4208 set_capacity(disk
, mddev
->array_sectors
);
4210 /* If there is a partially-recovered drive we need to
4211 * start recovery here. If we leave it to md_check_recovery,
4212 * it will remove the drives and not do the right thing
4214 if (mddev
->degraded
&& !mddev
->sync_thread
) {
4216 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4217 if (rdev
->raid_disk
>= 0 &&
4218 !test_bit(In_sync
, &rdev
->flags
) &&
4219 !test_bit(Faulty
, &rdev
->flags
))
4220 /* complete an interrupted recovery */
4222 if (spares
&& mddev
->pers
->sync_request
) {
4223 mddev
->recovery
= 0;
4224 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
4225 mddev
->sync_thread
= md_register_thread(md_do_sync
,
4228 if (!mddev
->sync_thread
) {
4229 printk(KERN_ERR
"%s: could not start resync"
4232 /* leave the spares where they are, it shouldn't hurt */
4233 mddev
->recovery
= 0;
4237 md_wakeup_thread(mddev
->thread
);
4238 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
4240 revalidate_disk(mddev
->gendisk
);
4242 md_new_event(mddev
);
4243 sysfs_notify_dirent(mddev
->sysfs_state
);
4244 if (mddev
->sysfs_action
)
4245 sysfs_notify_dirent(mddev
->sysfs_action
);
4246 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
4247 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
4251 static int restart_array(mddev_t
*mddev
)
4253 struct gendisk
*disk
= mddev
->gendisk
;
4255 /* Complain if it has no devices */
4256 if (list_empty(&mddev
->disks
))
4262 mddev
->safemode
= 0;
4264 set_disk_ro(disk
, 0);
4265 printk(KERN_INFO
"md: %s switched to read-write mode.\n",
4267 /* Kick recovery or resync if necessary */
4268 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4269 md_wakeup_thread(mddev
->thread
);
4270 md_wakeup_thread(mddev
->sync_thread
);
4271 sysfs_notify_dirent(mddev
->sysfs_state
);
4275 /* similar to deny_write_access, but accounts for our holding a reference
4276 * to the file ourselves */
4277 static int deny_bitmap_write_access(struct file
* file
)
4279 struct inode
*inode
= file
->f_mapping
->host
;
4281 spin_lock(&inode
->i_lock
);
4282 if (atomic_read(&inode
->i_writecount
) > 1) {
4283 spin_unlock(&inode
->i_lock
);
4286 atomic_set(&inode
->i_writecount
, -1);
4287 spin_unlock(&inode
->i_lock
);
4292 static void restore_bitmap_write_access(struct file
*file
)
4294 struct inode
*inode
= file
->f_mapping
->host
;
4296 spin_lock(&inode
->i_lock
);
4297 atomic_set(&inode
->i_writecount
, 1);
4298 spin_unlock(&inode
->i_lock
);
4302 * 0 - completely stop and dis-assemble array
4303 * 1 - switch to readonly
4304 * 2 - stop but do not disassemble array
4306 static int do_md_stop(mddev_t
* mddev
, int mode
, int is_open
)
4309 struct gendisk
*disk
= mddev
->gendisk
;
4312 mutex_lock(&mddev
->open_mutex
);
4313 if (atomic_read(&mddev
->openers
) > is_open
) {
4314 printk("md: %s still in use.\n",mdname(mddev
));
4316 } else if (mddev
->pers
) {
4318 if (mddev
->sync_thread
) {
4319 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4320 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4321 md_unregister_thread(mddev
->sync_thread
);
4322 mddev
->sync_thread
= NULL
;
4325 del_timer_sync(&mddev
->safemode_timer
);
4328 case 1: /* readonly */
4334 case 0: /* disassemble */
4336 bitmap_flush(mddev
);
4337 md_super_wait(mddev
);
4339 set_disk_ro(disk
, 0);
4341 mddev
->pers
->stop(mddev
);
4342 mddev
->queue
->merge_bvec_fn
= NULL
;
4343 mddev
->queue
->unplug_fn
= NULL
;
4344 mddev
->queue
->backing_dev_info
.congested_fn
= NULL
;
4345 module_put(mddev
->pers
->owner
);
4346 if (mddev
->pers
->sync_request
)
4347 mddev
->private = &md_redundancy_group
;
4349 /* tell userspace to handle 'inactive' */
4350 sysfs_notify_dirent(mddev
->sysfs_state
);
4352 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4353 if (rdev
->raid_disk
>= 0) {
4355 sprintf(nm
, "rd%d", rdev
->raid_disk
);
4356 sysfs_remove_link(&mddev
->kobj
, nm
);
4359 set_capacity(disk
, 0);
4365 if (!mddev
->in_sync
|| mddev
->flags
) {
4366 /* mark array as shutdown cleanly */
4368 md_update_sb(mddev
, 1);
4371 set_disk_ro(disk
, 1);
4372 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4376 mutex_unlock(&mddev
->open_mutex
);
4380 * Free resources if final stop
4384 printk(KERN_INFO
"md: %s stopped.\n", mdname(mddev
));
4386 bitmap_destroy(mddev
);
4387 if (mddev
->bitmap_file
) {
4388 restore_bitmap_write_access(mddev
->bitmap_file
);
4389 fput(mddev
->bitmap_file
);
4390 mddev
->bitmap_file
= NULL
;
4392 mddev
->bitmap_offset
= 0;
4394 /* make sure all md_delayed_delete calls have finished */
4395 flush_scheduled_work();
4397 export_array(mddev
);
4399 mddev
->array_sectors
= 0;
4400 mddev
->external_size
= 0;
4401 mddev
->dev_sectors
= 0;
4402 mddev
->raid_disks
= 0;
4403 mddev
->recovery_cp
= 0;
4404 mddev
->resync_min
= 0;
4405 mddev
->resync_max
= MaxSector
;
4406 mddev
->reshape_position
= MaxSector
;
4407 mddev
->external
= 0;
4408 mddev
->persistent
= 0;
4409 mddev
->level
= LEVEL_NONE
;
4410 mddev
->clevel
[0] = 0;
4413 mddev
->metadata_type
[0] = 0;
4414 mddev
->chunk_sectors
= 0;
4415 mddev
->ctime
= mddev
->utime
= 0;
4417 mddev
->max_disks
= 0;
4419 mddev
->delta_disks
= 0;
4420 mddev
->new_level
= LEVEL_NONE
;
4421 mddev
->new_layout
= 0;
4422 mddev
->new_chunk_sectors
= 0;
4423 mddev
->curr_resync
= 0;
4424 mddev
->resync_mismatches
= 0;
4425 mddev
->suspend_lo
= mddev
->suspend_hi
= 0;
4426 mddev
->sync_speed_min
= mddev
->sync_speed_max
= 0;
4427 mddev
->recovery
= 0;
4430 mddev
->degraded
= 0;
4431 mddev
->barriers_work
= 0;
4432 mddev
->safemode
= 0;
4433 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
4434 if (mddev
->hold_active
== UNTIL_STOP
)
4435 mddev
->hold_active
= 0;
4437 } else if (mddev
->pers
)
4438 printk(KERN_INFO
"md: %s switched to read-only mode.\n",
4441 blk_integrity_unregister(disk
);
4442 md_new_event(mddev
);
4443 sysfs_notify_dirent(mddev
->sysfs_state
);
4448 static void autorun_array(mddev_t
*mddev
)
4453 if (list_empty(&mddev
->disks
))
4456 printk(KERN_INFO
"md: running: ");
4458 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
4459 char b
[BDEVNAME_SIZE
];
4460 printk("<%s>", bdevname(rdev
->bdev
,b
));
4464 err
= do_md_run(mddev
);
4466 printk(KERN_WARNING
"md: do_md_run() returned %d\n", err
);
4467 do_md_stop(mddev
, 0, 0);
4472 * lets try to run arrays based on all disks that have arrived
4473 * until now. (those are in pending_raid_disks)
4475 * the method: pick the first pending disk, collect all disks with
4476 * the same UUID, remove all from the pending list and put them into
4477 * the 'same_array' list. Then order this list based on superblock
4478 * update time (freshest comes first), kick out 'old' disks and
4479 * compare superblocks. If everything's fine then run it.
4481 * If "unit" is allocated, then bump its reference count
4483 static void autorun_devices(int part
)
4485 mdk_rdev_t
*rdev0
, *rdev
, *tmp
;
4487 char b
[BDEVNAME_SIZE
];
4489 printk(KERN_INFO
"md: autorun ...\n");
4490 while (!list_empty(&pending_raid_disks
)) {
4493 LIST_HEAD(candidates
);
4494 rdev0
= list_entry(pending_raid_disks
.next
,
4495 mdk_rdev_t
, same_set
);
4497 printk(KERN_INFO
"md: considering %s ...\n",
4498 bdevname(rdev0
->bdev
,b
));
4499 INIT_LIST_HEAD(&candidates
);
4500 rdev_for_each_list(rdev
, tmp
, &pending_raid_disks
)
4501 if (super_90_load(rdev
, rdev0
, 0) >= 0) {
4502 printk(KERN_INFO
"md: adding %s ...\n",
4503 bdevname(rdev
->bdev
,b
));
4504 list_move(&rdev
->same_set
, &candidates
);
4507 * now we have a set of devices, with all of them having
4508 * mostly sane superblocks. It's time to allocate the
4512 dev
= MKDEV(mdp_major
,
4513 rdev0
->preferred_minor
<< MdpMinorShift
);
4514 unit
= MINOR(dev
) >> MdpMinorShift
;
4516 dev
= MKDEV(MD_MAJOR
, rdev0
->preferred_minor
);
4519 if (rdev0
->preferred_minor
!= unit
) {
4520 printk(KERN_INFO
"md: unit number in %s is bad: %d\n",
4521 bdevname(rdev0
->bdev
, b
), rdev0
->preferred_minor
);
4525 md_probe(dev
, NULL
, NULL
);
4526 mddev
= mddev_find(dev
);
4527 if (!mddev
|| !mddev
->gendisk
) {
4531 "md: cannot allocate memory for md drive.\n");
4534 if (mddev_lock(mddev
))
4535 printk(KERN_WARNING
"md: %s locked, cannot run\n",
4537 else if (mddev
->raid_disks
|| mddev
->major_version
4538 || !list_empty(&mddev
->disks
)) {
4540 "md: %s already running, cannot run %s\n",
4541 mdname(mddev
), bdevname(rdev0
->bdev
,b
));
4542 mddev_unlock(mddev
);
4544 printk(KERN_INFO
"md: created %s\n", mdname(mddev
));
4545 mddev
->persistent
= 1;
4546 rdev_for_each_list(rdev
, tmp
, &candidates
) {
4547 list_del_init(&rdev
->same_set
);
4548 if (bind_rdev_to_array(rdev
, mddev
))
4551 autorun_array(mddev
);
4552 mddev_unlock(mddev
);
4554 /* on success, candidates will be empty, on error
4557 rdev_for_each_list(rdev
, tmp
, &candidates
) {
4558 list_del_init(&rdev
->same_set
);
4563 printk(KERN_INFO
"md: ... autorun DONE.\n");
4565 #endif /* !MODULE */
4567 static int get_version(void __user
* arg
)
4571 ver
.major
= MD_MAJOR_VERSION
;
4572 ver
.minor
= MD_MINOR_VERSION
;
4573 ver
.patchlevel
= MD_PATCHLEVEL_VERSION
;
4575 if (copy_to_user(arg
, &ver
, sizeof(ver
)))
4581 static int get_array_info(mddev_t
* mddev
, void __user
* arg
)
4583 mdu_array_info_t info
;
4584 int nr
,working
,insync
,failed
,spare
;
4587 nr
=working
=insync
=failed
=spare
=0;
4588 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
4590 if (test_bit(Faulty
, &rdev
->flags
))
4594 if (test_bit(In_sync
, &rdev
->flags
))
4601 info
.major_version
= mddev
->major_version
;
4602 info
.minor_version
= mddev
->minor_version
;
4603 info
.patch_version
= MD_PATCHLEVEL_VERSION
;
4604 info
.ctime
= mddev
->ctime
;
4605 info
.level
= mddev
->level
;
4606 info
.size
= mddev
->dev_sectors
/ 2;
4607 if (info
.size
!= mddev
->dev_sectors
/ 2) /* overflow */
4610 info
.raid_disks
= mddev
->raid_disks
;
4611 info
.md_minor
= mddev
->md_minor
;
4612 info
.not_persistent
= !mddev
->persistent
;
4614 info
.utime
= mddev
->utime
;
4617 info
.state
= (1<<MD_SB_CLEAN
);
4618 if (mddev
->bitmap
&& mddev
->bitmap_offset
)
4619 info
.state
= (1<<MD_SB_BITMAP_PRESENT
);
4620 info
.active_disks
= insync
;
4621 info
.working_disks
= working
;
4622 info
.failed_disks
= failed
;
4623 info
.spare_disks
= spare
;
4625 info
.layout
= mddev
->layout
;
4626 info
.chunk_size
= mddev
->chunk_sectors
<< 9;
4628 if (copy_to_user(arg
, &info
, sizeof(info
)))
4634 static int get_bitmap_file(mddev_t
* mddev
, void __user
* arg
)
4636 mdu_bitmap_file_t
*file
= NULL
; /* too big for stack allocation */
4637 char *ptr
, *buf
= NULL
;
4640 if (md_allow_write(mddev
))
4641 file
= kmalloc(sizeof(*file
), GFP_NOIO
);
4643 file
= kmalloc(sizeof(*file
), GFP_KERNEL
);
4648 /* bitmap disabled, zero the first byte and copy out */
4649 if (!mddev
->bitmap
|| !mddev
->bitmap
->file
) {
4650 file
->pathname
[0] = '\0';
4654 buf
= kmalloc(sizeof(file
->pathname
), GFP_KERNEL
);
4658 ptr
= d_path(&mddev
->bitmap
->file
->f_path
, buf
, sizeof(file
->pathname
));
4662 strcpy(file
->pathname
, ptr
);
4666 if (copy_to_user(arg
, file
, sizeof(*file
)))
4674 static int get_disk_info(mddev_t
* mddev
, void __user
* arg
)
4676 mdu_disk_info_t info
;
4679 if (copy_from_user(&info
, arg
, sizeof(info
)))
4682 rdev
= find_rdev_nr(mddev
, info
.number
);
4684 info
.major
= MAJOR(rdev
->bdev
->bd_dev
);
4685 info
.minor
= MINOR(rdev
->bdev
->bd_dev
);
4686 info
.raid_disk
= rdev
->raid_disk
;
4688 if (test_bit(Faulty
, &rdev
->flags
))
4689 info
.state
|= (1<<MD_DISK_FAULTY
);
4690 else if (test_bit(In_sync
, &rdev
->flags
)) {
4691 info
.state
|= (1<<MD_DISK_ACTIVE
);
4692 info
.state
|= (1<<MD_DISK_SYNC
);
4694 if (test_bit(WriteMostly
, &rdev
->flags
))
4695 info
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
4697 info
.major
= info
.minor
= 0;
4698 info
.raid_disk
= -1;
4699 info
.state
= (1<<MD_DISK_REMOVED
);
4702 if (copy_to_user(arg
, &info
, sizeof(info
)))
4708 static int add_new_disk(mddev_t
* mddev
, mdu_disk_info_t
*info
)
4710 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
4712 dev_t dev
= MKDEV(info
->major
,info
->minor
);
4714 if (info
->major
!= MAJOR(dev
) || info
->minor
!= MINOR(dev
))
4717 if (!mddev
->raid_disks
) {
4719 /* expecting a device which has a superblock */
4720 rdev
= md_import_device(dev
, mddev
->major_version
, mddev
->minor_version
);
4723 "md: md_import_device returned %ld\n",
4725 return PTR_ERR(rdev
);
4727 if (!list_empty(&mddev
->disks
)) {
4728 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
4729 mdk_rdev_t
, same_set
);
4730 err
= super_types
[mddev
->major_version
]
4731 .load_super(rdev
, rdev0
, mddev
->minor_version
);
4734 "md: %s has different UUID to %s\n",
4735 bdevname(rdev
->bdev
,b
),
4736 bdevname(rdev0
->bdev
,b2
));
4741 err
= bind_rdev_to_array(rdev
, mddev
);
4748 * add_new_disk can be used once the array is assembled
4749 * to add "hot spares". They must already have a superblock
4754 if (!mddev
->pers
->hot_add_disk
) {
4756 "%s: personality does not support diskops!\n",
4760 if (mddev
->persistent
)
4761 rdev
= md_import_device(dev
, mddev
->major_version
,
4762 mddev
->minor_version
);
4764 rdev
= md_import_device(dev
, -1, -1);
4767 "md: md_import_device returned %ld\n",
4769 return PTR_ERR(rdev
);
4771 /* set save_raid_disk if appropriate */
4772 if (!mddev
->persistent
) {
4773 if (info
->state
& (1<<MD_DISK_SYNC
) &&
4774 info
->raid_disk
< mddev
->raid_disks
)
4775 rdev
->raid_disk
= info
->raid_disk
;
4777 rdev
->raid_disk
= -1;
4779 super_types
[mddev
->major_version
].
4780 validate_super(mddev
, rdev
);
4781 rdev
->saved_raid_disk
= rdev
->raid_disk
;
4783 clear_bit(In_sync
, &rdev
->flags
); /* just to be sure */
4784 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
4785 set_bit(WriteMostly
, &rdev
->flags
);
4787 clear_bit(WriteMostly
, &rdev
->flags
);
4789 rdev
->raid_disk
= -1;
4790 err
= bind_rdev_to_array(rdev
, mddev
);
4791 if (!err
&& !mddev
->pers
->hot_remove_disk
) {
4792 /* If there is hot_add_disk but no hot_remove_disk
4793 * then added disks for geometry changes,
4794 * and should be added immediately.
4796 super_types
[mddev
->major_version
].
4797 validate_super(mddev
, rdev
);
4798 err
= mddev
->pers
->hot_add_disk(mddev
, rdev
);
4800 unbind_rdev_from_array(rdev
);
4805 sysfs_notify_dirent(rdev
->sysfs_state
);
4807 md_update_sb(mddev
, 1);
4808 if (mddev
->degraded
)
4809 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
4810 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4811 md_wakeup_thread(mddev
->thread
);
4815 /* otherwise, add_new_disk is only allowed
4816 * for major_version==0 superblocks
4818 if (mddev
->major_version
!= 0) {
4819 printk(KERN_WARNING
"%s: ADD_NEW_DISK not supported\n",
4824 if (!(info
->state
& (1<<MD_DISK_FAULTY
))) {
4826 rdev
= md_import_device(dev
, -1, 0);
4829 "md: error, md_import_device() returned %ld\n",
4831 return PTR_ERR(rdev
);
4833 rdev
->desc_nr
= info
->number
;
4834 if (info
->raid_disk
< mddev
->raid_disks
)
4835 rdev
->raid_disk
= info
->raid_disk
;
4837 rdev
->raid_disk
= -1;
4839 if (rdev
->raid_disk
< mddev
->raid_disks
)
4840 if (info
->state
& (1<<MD_DISK_SYNC
))
4841 set_bit(In_sync
, &rdev
->flags
);
4843 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
4844 set_bit(WriteMostly
, &rdev
->flags
);
4846 if (!mddev
->persistent
) {
4847 printk(KERN_INFO
"md: nonpersistent superblock ...\n");
4848 rdev
->sb_start
= rdev
->bdev
->bd_inode
->i_size
/ 512;
4850 rdev
->sb_start
= calc_dev_sboffset(rdev
->bdev
);
4851 rdev
->sectors
= rdev
->sb_start
;
4853 err
= bind_rdev_to_array(rdev
, mddev
);
4863 static int hot_remove_disk(mddev_t
* mddev
, dev_t dev
)
4865 char b
[BDEVNAME_SIZE
];
4868 rdev
= find_rdev(mddev
, dev
);
4872 if (rdev
->raid_disk
>= 0)
4875 kick_rdev_from_array(rdev
);
4876 md_update_sb(mddev
, 1);
4877 md_new_event(mddev
);
4881 printk(KERN_WARNING
"md: cannot remove active disk %s from %s ...\n",
4882 bdevname(rdev
->bdev
,b
), mdname(mddev
));
4886 static int hot_add_disk(mddev_t
* mddev
, dev_t dev
)
4888 char b
[BDEVNAME_SIZE
];
4895 if (mddev
->major_version
!= 0) {
4896 printk(KERN_WARNING
"%s: HOT_ADD may only be used with"
4897 " version-0 superblocks.\n",
4901 if (!mddev
->pers
->hot_add_disk
) {
4903 "%s: personality does not support diskops!\n",
4908 rdev
= md_import_device(dev
, -1, 0);
4911 "md: error, md_import_device() returned %ld\n",
4916 if (mddev
->persistent
)
4917 rdev
->sb_start
= calc_dev_sboffset(rdev
->bdev
);
4919 rdev
->sb_start
= rdev
->bdev
->bd_inode
->i_size
/ 512;
4921 rdev
->sectors
= rdev
->sb_start
;
4923 if (test_bit(Faulty
, &rdev
->flags
)) {
4925 "md: can not hot-add faulty %s disk to %s!\n",
4926 bdevname(rdev
->bdev
,b
), mdname(mddev
));
4930 clear_bit(In_sync
, &rdev
->flags
);
4932 rdev
->saved_raid_disk
= -1;
4933 err
= bind_rdev_to_array(rdev
, mddev
);
4938 * The rest should better be atomic, we can have disk failures
4939 * noticed in interrupt contexts ...
4942 rdev
->raid_disk
= -1;
4944 md_update_sb(mddev
, 1);
4947 * Kick recovery, maybe this spare has to be added to the
4948 * array immediately.
4950 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4951 md_wakeup_thread(mddev
->thread
);
4952 md_new_event(mddev
);
4960 static int set_bitmap_file(mddev_t
*mddev
, int fd
)
4965 if (!mddev
->pers
->quiesce
)
4967 if (mddev
->recovery
|| mddev
->sync_thread
)
4969 /* we should be able to change the bitmap.. */
4975 return -EEXIST
; /* cannot add when bitmap is present */
4976 mddev
->bitmap_file
= fget(fd
);
4978 if (mddev
->bitmap_file
== NULL
) {
4979 printk(KERN_ERR
"%s: error: failed to get bitmap file\n",
4984 err
= deny_bitmap_write_access(mddev
->bitmap_file
);
4986 printk(KERN_ERR
"%s: error: bitmap file is already in use\n",
4988 fput(mddev
->bitmap_file
);
4989 mddev
->bitmap_file
= NULL
;
4992 mddev
->bitmap_offset
= 0; /* file overrides offset */
4993 } else if (mddev
->bitmap
== NULL
)
4994 return -ENOENT
; /* cannot remove what isn't there */
4997 mddev
->pers
->quiesce(mddev
, 1);
4999 err
= bitmap_create(mddev
);
5000 if (fd
< 0 || err
) {
5001 bitmap_destroy(mddev
);
5002 fd
= -1; /* make sure to put the file */
5004 mddev
->pers
->quiesce(mddev
, 0);
5007 if (mddev
->bitmap_file
) {
5008 restore_bitmap_write_access(mddev
->bitmap_file
);
5009 fput(mddev
->bitmap_file
);
5011 mddev
->bitmap_file
= NULL
;
5018 * set_array_info is used two different ways
5019 * The original usage is when creating a new array.
5020 * In this usage, raid_disks is > 0 and it together with
5021 * level, size, not_persistent,layout,chunksize determine the
5022 * shape of the array.
5023 * This will always create an array with a type-0.90.0 superblock.
5024 * The newer usage is when assembling an array.
5025 * In this case raid_disks will be 0, and the major_version field is
5026 * use to determine which style super-blocks are to be found on the devices.
5027 * The minor and patch _version numbers are also kept incase the
5028 * super_block handler wishes to interpret them.
5030 static int set_array_info(mddev_t
* mddev
, mdu_array_info_t
*info
)
5033 if (info
->raid_disks
== 0) {
5034 /* just setting version number for superblock loading */
5035 if (info
->major_version
< 0 ||
5036 info
->major_version
>= ARRAY_SIZE(super_types
) ||
5037 super_types
[info
->major_version
].name
== NULL
) {
5038 /* maybe try to auto-load a module? */
5040 "md: superblock version %d not known\n",
5041 info
->major_version
);
5044 mddev
->major_version
= info
->major_version
;
5045 mddev
->minor_version
= info
->minor_version
;
5046 mddev
->patch_version
= info
->patch_version
;
5047 mddev
->persistent
= !info
->not_persistent
;
5050 mddev
->major_version
= MD_MAJOR_VERSION
;
5051 mddev
->minor_version
= MD_MINOR_VERSION
;
5052 mddev
->patch_version
= MD_PATCHLEVEL_VERSION
;
5053 mddev
->ctime
= get_seconds();
5055 mddev
->level
= info
->level
;
5056 mddev
->clevel
[0] = 0;
5057 mddev
->dev_sectors
= 2 * (sector_t
)info
->size
;
5058 mddev
->raid_disks
= info
->raid_disks
;
5059 /* don't set md_minor, it is determined by which /dev/md* was
5062 if (info
->state
& (1<<MD_SB_CLEAN
))
5063 mddev
->recovery_cp
= MaxSector
;
5065 mddev
->recovery_cp
= 0;
5066 mddev
->persistent
= ! info
->not_persistent
;
5067 mddev
->external
= 0;
5069 mddev
->layout
= info
->layout
;
5070 mddev
->chunk_sectors
= info
->chunk_size
>> 9;
5072 mddev
->max_disks
= MD_SB_DISKS
;
5074 if (mddev
->persistent
)
5076 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
5078 mddev
->default_bitmap_offset
= MD_SB_BYTES
>> 9;
5079 mddev
->bitmap_offset
= 0;
5081 mddev
->reshape_position
= MaxSector
;
5084 * Generate a 128 bit UUID
5086 get_random_bytes(mddev
->uuid
, 16);
5088 mddev
->new_level
= mddev
->level
;
5089 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
5090 mddev
->new_layout
= mddev
->layout
;
5091 mddev
->delta_disks
= 0;
5096 void md_set_array_sectors(mddev_t
*mddev
, sector_t array_sectors
)
5098 WARN(!mddev_is_locked(mddev
), "%s: unlocked mddev!\n", __func__
);
5100 if (mddev
->external_size
)
5103 mddev
->array_sectors
= array_sectors
;
5105 EXPORT_SYMBOL(md_set_array_sectors
);
5107 static int update_size(mddev_t
*mddev
, sector_t num_sectors
)
5111 int fit
= (num_sectors
== 0);
5113 if (mddev
->pers
->resize
== NULL
)
5115 /* The "num_sectors" is the number of sectors of each device that
5116 * is used. This can only make sense for arrays with redundancy.
5117 * linear and raid0 always use whatever space is available. We can only
5118 * consider changing this number if no resync or reconstruction is
5119 * happening, and if the new size is acceptable. It must fit before the
5120 * sb_start or, if that is <data_offset, it must fit before the size
5121 * of each device. If num_sectors is zero, we find the largest size
5125 if (mddev
->sync_thread
)
5128 /* Sorry, cannot grow a bitmap yet, just remove it,
5132 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
5133 sector_t avail
= rdev
->sectors
;
5135 if (fit
&& (num_sectors
== 0 || num_sectors
> avail
))
5136 num_sectors
= avail
;
5137 if (avail
< num_sectors
)
5140 rv
= mddev
->pers
->resize(mddev
, num_sectors
);
5142 revalidate_disk(mddev
->gendisk
);
5146 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
)
5149 /* change the number of raid disks */
5150 if (mddev
->pers
->check_reshape
== NULL
)
5152 if (raid_disks
<= 0 ||
5153 raid_disks
>= mddev
->max_disks
)
5155 if (mddev
->sync_thread
|| mddev
->reshape_position
!= MaxSector
)
5157 mddev
->delta_disks
= raid_disks
- mddev
->raid_disks
;
5159 rv
= mddev
->pers
->check_reshape(mddev
);
5165 * update_array_info is used to change the configuration of an
5167 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5168 * fields in the info are checked against the array.
5169 * Any differences that cannot be handled will cause an error.
5170 * Normally, only one change can be managed at a time.
5172 static int update_array_info(mddev_t
*mddev
, mdu_array_info_t
*info
)
5178 /* calculate expected state,ignoring low bits */
5179 if (mddev
->bitmap
&& mddev
->bitmap_offset
)
5180 state
|= (1 << MD_SB_BITMAP_PRESENT
);
5182 if (mddev
->major_version
!= info
->major_version
||
5183 mddev
->minor_version
!= info
->minor_version
||
5184 /* mddev->patch_version != info->patch_version || */
5185 mddev
->ctime
!= info
->ctime
||
5186 mddev
->level
!= info
->level
||
5187 /* mddev->layout != info->layout || */
5188 !mddev
->persistent
!= info
->not_persistent
||
5189 mddev
->chunk_sectors
!= info
->chunk_size
>> 9 ||
5190 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5191 ((state
^info
->state
) & 0xfffffe00)
5194 /* Check there is only one change */
5195 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
5197 if (mddev
->raid_disks
!= info
->raid_disks
)
5199 if (mddev
->layout
!= info
->layout
)
5201 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
))
5208 if (mddev
->layout
!= info
->layout
) {
5210 * we don't need to do anything at the md level, the
5211 * personality will take care of it all.
5213 if (mddev
->pers
->check_reshape
== NULL
)
5216 mddev
->new_layout
= info
->layout
;
5217 rv
= mddev
->pers
->check_reshape(mddev
);
5219 mddev
->new_layout
= mddev
->layout
;
5223 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
5224 rv
= update_size(mddev
, (sector_t
)info
->size
* 2);
5226 if (mddev
->raid_disks
!= info
->raid_disks
)
5227 rv
= update_raid_disks(mddev
, info
->raid_disks
);
5229 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) {
5230 if (mddev
->pers
->quiesce
== NULL
)
5232 if (mddev
->recovery
|| mddev
->sync_thread
)
5234 if (info
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
5235 /* add the bitmap */
5238 if (mddev
->default_bitmap_offset
== 0)
5240 mddev
->bitmap_offset
= mddev
->default_bitmap_offset
;
5241 mddev
->pers
->quiesce(mddev
, 1);
5242 rv
= bitmap_create(mddev
);
5244 bitmap_destroy(mddev
);
5245 mddev
->pers
->quiesce(mddev
, 0);
5247 /* remove the bitmap */
5250 if (mddev
->bitmap
->file
)
5252 mddev
->pers
->quiesce(mddev
, 1);
5253 bitmap_destroy(mddev
);
5254 mddev
->pers
->quiesce(mddev
, 0);
5255 mddev
->bitmap_offset
= 0;
5258 md_update_sb(mddev
, 1);
5262 static int set_disk_faulty(mddev_t
*mddev
, dev_t dev
)
5266 if (mddev
->pers
== NULL
)
5269 rdev
= find_rdev(mddev
, dev
);
5273 md_error(mddev
, rdev
);
5278 * We have a problem here : there is no easy way to give a CHS
5279 * virtual geometry. We currently pretend that we have a 2 heads
5280 * 4 sectors (with a BIG number of cylinders...). This drives
5281 * dosfs just mad... ;-)
5283 static int md_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
5285 mddev_t
*mddev
= bdev
->bd_disk
->private_data
;
5289 geo
->cylinders
= get_capacity(mddev
->gendisk
) / 8;
5293 static int md_ioctl(struct block_device
*bdev
, fmode_t mode
,
5294 unsigned int cmd
, unsigned long arg
)
5297 void __user
*argp
= (void __user
*)arg
;
5298 mddev_t
*mddev
= NULL
;
5300 if (!capable(CAP_SYS_ADMIN
))
5304 * Commands dealing with the RAID driver but not any
5310 err
= get_version(argp
);
5313 case PRINT_RAID_DEBUG
:
5321 autostart_arrays(arg
);
5328 * Commands creating/starting a new array:
5331 mddev
= bdev
->bd_disk
->private_data
;
5338 err
= mddev_lock(mddev
);
5341 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5348 case SET_ARRAY_INFO
:
5350 mdu_array_info_t info
;
5352 memset(&info
, 0, sizeof(info
));
5353 else if (copy_from_user(&info
, argp
, sizeof(info
))) {
5358 err
= update_array_info(mddev
, &info
);
5360 printk(KERN_WARNING
"md: couldn't update"
5361 " array info. %d\n", err
);
5366 if (!list_empty(&mddev
->disks
)) {
5368 "md: array %s already has disks!\n",
5373 if (mddev
->raid_disks
) {
5375 "md: array %s already initialised!\n",
5380 err
= set_array_info(mddev
, &info
);
5382 printk(KERN_WARNING
"md: couldn't set"
5383 " array info. %d\n", err
);
5393 * Commands querying/configuring an existing array:
5395 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
5396 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
5397 if ((!mddev
->raid_disks
&& !mddev
->external
)
5398 && cmd
!= ADD_NEW_DISK
&& cmd
!= STOP_ARRAY
5399 && cmd
!= RUN_ARRAY
&& cmd
!= SET_BITMAP_FILE
5400 && cmd
!= GET_BITMAP_FILE
) {
5406 * Commands even a read-only array can execute:
5410 case GET_ARRAY_INFO
:
5411 err
= get_array_info(mddev
, argp
);
5414 case GET_BITMAP_FILE
:
5415 err
= get_bitmap_file(mddev
, argp
);
5419 err
= get_disk_info(mddev
, argp
);
5422 case RESTART_ARRAY_RW
:
5423 err
= restart_array(mddev
);
5427 err
= do_md_stop(mddev
, 0, 1);
5431 err
= do_md_stop(mddev
, 1, 1);
5437 * The remaining ioctls are changing the state of the
5438 * superblock, so we do not allow them on read-only arrays.
5439 * However non-MD ioctls (e.g. get-size) will still come through
5440 * here and hit the 'default' below, so only disallow
5441 * 'md' ioctls, and switch to rw mode if started auto-readonly.
5443 if (_IOC_TYPE(cmd
) == MD_MAJOR
&& mddev
->ro
&& mddev
->pers
) {
5444 if (mddev
->ro
== 2) {
5446 sysfs_notify_dirent(mddev
->sysfs_state
);
5447 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5448 md_wakeup_thread(mddev
->thread
);
5459 mdu_disk_info_t info
;
5460 if (copy_from_user(&info
, argp
, sizeof(info
)))
5463 err
= add_new_disk(mddev
, &info
);
5467 case HOT_REMOVE_DISK
:
5468 err
= hot_remove_disk(mddev
, new_decode_dev(arg
));
5472 err
= hot_add_disk(mddev
, new_decode_dev(arg
));
5475 case SET_DISK_FAULTY
:
5476 err
= set_disk_faulty(mddev
, new_decode_dev(arg
));
5480 err
= do_md_run(mddev
);
5483 case SET_BITMAP_FILE
:
5484 err
= set_bitmap_file(mddev
, (int)arg
);
5494 if (mddev
->hold_active
== UNTIL_IOCTL
&&
5496 mddev
->hold_active
= 0;
5497 mddev_unlock(mddev
);
5507 static int md_open(struct block_device
*bdev
, fmode_t mode
)
5510 * Succeed if we can lock the mddev, which confirms that
5511 * it isn't being stopped right now.
5513 mddev_t
*mddev
= mddev_find(bdev
->bd_dev
);
5516 if (mddev
->gendisk
!= bdev
->bd_disk
) {
5517 /* we are racing with mddev_put which is discarding this
5521 /* Wait until bdev->bd_disk is definitely gone */
5522 flush_scheduled_work();
5523 /* Then retry the open from the top */
5524 return -ERESTARTSYS
;
5526 BUG_ON(mddev
!= bdev
->bd_disk
->private_data
);
5528 if ((err
= mutex_lock_interruptible(&mddev
->open_mutex
)))
5532 atomic_inc(&mddev
->openers
);
5533 mutex_unlock(&mddev
->open_mutex
);
5535 check_disk_change(bdev
);
5540 static int md_release(struct gendisk
*disk
, fmode_t mode
)
5542 mddev_t
*mddev
= disk
->private_data
;
5545 atomic_dec(&mddev
->openers
);
5551 static int md_media_changed(struct gendisk
*disk
)
5553 mddev_t
*mddev
= disk
->private_data
;
5555 return mddev
->changed
;
5558 static int md_revalidate(struct gendisk
*disk
)
5560 mddev_t
*mddev
= disk
->private_data
;
5565 static const struct block_device_operations md_fops
=
5567 .owner
= THIS_MODULE
,
5569 .release
= md_release
,
5571 .getgeo
= md_getgeo
,
5572 .media_changed
= md_media_changed
,
5573 .revalidate_disk
= md_revalidate
,
5576 static int md_thread(void * arg
)
5578 mdk_thread_t
*thread
= arg
;
5581 * md_thread is a 'system-thread', it's priority should be very
5582 * high. We avoid resource deadlocks individually in each
5583 * raid personality. (RAID5 does preallocation) We also use RR and
5584 * the very same RT priority as kswapd, thus we will never get
5585 * into a priority inversion deadlock.
5587 * we definitely have to have equal or higher priority than
5588 * bdflush, otherwise bdflush will deadlock if there are too
5589 * many dirty RAID5 blocks.
5592 allow_signal(SIGKILL
);
5593 while (!kthread_should_stop()) {
5595 /* We need to wait INTERRUPTIBLE so that
5596 * we don't add to the load-average.
5597 * That means we need to be sure no signals are
5600 if (signal_pending(current
))
5601 flush_signals(current
);
5603 wait_event_interruptible_timeout
5605 test_bit(THREAD_WAKEUP
, &thread
->flags
)
5606 || kthread_should_stop(),
5609 clear_bit(THREAD_WAKEUP
, &thread
->flags
);
5611 thread
->run(thread
->mddev
);
5617 void md_wakeup_thread(mdk_thread_t
*thread
)
5620 dprintk("md: waking up MD thread %s.\n", thread
->tsk
->comm
);
5621 set_bit(THREAD_WAKEUP
, &thread
->flags
);
5622 wake_up(&thread
->wqueue
);
5626 mdk_thread_t
*md_register_thread(void (*run
) (mddev_t
*), mddev_t
*mddev
,
5629 mdk_thread_t
*thread
;
5631 thread
= kzalloc(sizeof(mdk_thread_t
), GFP_KERNEL
);
5635 init_waitqueue_head(&thread
->wqueue
);
5638 thread
->mddev
= mddev
;
5639 thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
5640 thread
->tsk
= kthread_run(md_thread
, thread
,
5642 mdname(thread
->mddev
),
5643 name
?: mddev
->pers
->name
);
5644 if (IS_ERR(thread
->tsk
)) {
5651 void md_unregister_thread(mdk_thread_t
*thread
)
5655 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread
->tsk
));
5657 kthread_stop(thread
->tsk
);
5661 void md_error(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
5668 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
5671 if (mddev
->external
)
5672 set_bit(Blocked
, &rdev
->flags
);
5674 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5676 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5677 __builtin_return_address(0),__builtin_return_address(1),
5678 __builtin_return_address(2),__builtin_return_address(3));
5682 if (!mddev
->pers
->error_handler
)
5684 mddev
->pers
->error_handler(mddev
,rdev
);
5685 if (mddev
->degraded
)
5686 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
5687 set_bit(StateChanged
, &rdev
->flags
);
5688 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5689 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5690 md_wakeup_thread(mddev
->thread
);
5691 md_new_event_inintr(mddev
);
5694 /* seq_file implementation /proc/mdstat */
5696 static void status_unused(struct seq_file
*seq
)
5701 seq_printf(seq
, "unused devices: ");
5703 list_for_each_entry(rdev
, &pending_raid_disks
, same_set
) {
5704 char b
[BDEVNAME_SIZE
];
5706 seq_printf(seq
, "%s ",
5707 bdevname(rdev
->bdev
,b
));
5710 seq_printf(seq
, "<none>");
5712 seq_printf(seq
, "\n");
5716 static void status_resync(struct seq_file
*seq
, mddev_t
* mddev
)
5718 sector_t max_sectors
, resync
, res
;
5719 unsigned long dt
, db
;
5722 unsigned int per_milli
;
5724 resync
= mddev
->curr_resync
- atomic_read(&mddev
->recovery_active
);
5726 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
5727 max_sectors
= mddev
->resync_max_sectors
;
5729 max_sectors
= mddev
->dev_sectors
;
5732 * Should not happen.
5738 /* Pick 'scale' such that (resync>>scale)*1000 will fit
5739 * in a sector_t, and (max_sectors>>scale) will fit in a
5740 * u32, as those are the requirements for sector_div.
5741 * Thus 'scale' must be at least 10
5744 if (sizeof(sector_t
) > sizeof(unsigned long)) {
5745 while ( max_sectors
/2 > (1ULL<<(scale
+32)))
5748 res
= (resync
>>scale
)*1000;
5749 sector_div(res
, (u32
)((max_sectors
>>scale
)+1));
5753 int i
, x
= per_milli
/50, y
= 20-x
;
5754 seq_printf(seq
, "[");
5755 for (i
= 0; i
< x
; i
++)
5756 seq_printf(seq
, "=");
5757 seq_printf(seq
, ">");
5758 for (i
= 0; i
< y
; i
++)
5759 seq_printf(seq
, ".");
5760 seq_printf(seq
, "] ");
5762 seq_printf(seq
, " %s =%3u.%u%% (%llu/%llu)",
5763 (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)?
5765 (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)?
5767 (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ?
5768 "resync" : "recovery"))),
5769 per_milli
/10, per_milli
% 10,
5770 (unsigned long long) resync
/2,
5771 (unsigned long long) max_sectors
/2);
5774 * dt: time from mark until now
5775 * db: blocks written from mark until now
5776 * rt: remaining time
5778 * rt is a sector_t, so could be 32bit or 64bit.
5779 * So we divide before multiply in case it is 32bit and close
5781 * We scale the divisor (db) by 32 to avoid loosing precision
5782 * near the end of resync when the number of remaining sectors
5784 * We then divide rt by 32 after multiplying by db to compensate.
5785 * The '+1' avoids division by zero if db is very small.
5787 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
5789 db
= (mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
))
5790 - mddev
->resync_mark_cnt
;
5792 rt
= max_sectors
- resync
; /* number of remaining sectors */
5793 sector_div(rt
, db
/32+1);
5797 seq_printf(seq
, " finish=%lu.%lumin", (unsigned long)rt
/ 60,
5798 ((unsigned long)rt
% 60)/6);
5800 seq_printf(seq
, " speed=%ldK/sec", db
/2/dt
);
5803 static void *md_seq_start(struct seq_file
*seq
, loff_t
*pos
)
5805 struct list_head
*tmp
;
5815 spin_lock(&all_mddevs_lock
);
5816 list_for_each(tmp
,&all_mddevs
)
5818 mddev
= list_entry(tmp
, mddev_t
, all_mddevs
);
5820 spin_unlock(&all_mddevs_lock
);
5823 spin_unlock(&all_mddevs_lock
);
5825 return (void*)2;/* tail */
5829 static void *md_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
5831 struct list_head
*tmp
;
5832 mddev_t
*next_mddev
, *mddev
= v
;
5838 spin_lock(&all_mddevs_lock
);
5840 tmp
= all_mddevs
.next
;
5842 tmp
= mddev
->all_mddevs
.next
;
5843 if (tmp
!= &all_mddevs
)
5844 next_mddev
= mddev_get(list_entry(tmp
,mddev_t
,all_mddevs
));
5846 next_mddev
= (void*)2;
5849 spin_unlock(&all_mddevs_lock
);
5857 static void md_seq_stop(struct seq_file
*seq
, void *v
)
5861 if (mddev
&& v
!= (void*)1 && v
!= (void*)2)
5865 struct mdstat_info
{
5869 static int md_seq_show(struct seq_file
*seq
, void *v
)
5874 struct mdstat_info
*mi
= seq
->private;
5875 struct bitmap
*bitmap
;
5877 if (v
== (void*)1) {
5878 struct mdk_personality
*pers
;
5879 seq_printf(seq
, "Personalities : ");
5880 spin_lock(&pers_lock
);
5881 list_for_each_entry(pers
, &pers_list
, list
)
5882 seq_printf(seq
, "[%s] ", pers
->name
);
5884 spin_unlock(&pers_lock
);
5885 seq_printf(seq
, "\n");
5886 mi
->event
= atomic_read(&md_event_count
);
5889 if (v
== (void*)2) {
5894 if (mddev_lock(mddev
) < 0)
5897 if (mddev
->pers
|| mddev
->raid_disks
|| !list_empty(&mddev
->disks
)) {
5898 seq_printf(seq
, "%s : %sactive", mdname(mddev
),
5899 mddev
->pers
? "" : "in");
5902 seq_printf(seq
, " (read-only)");
5904 seq_printf(seq
, " (auto-read-only)");
5905 seq_printf(seq
, " %s", mddev
->pers
->name
);
5909 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
5910 char b
[BDEVNAME_SIZE
];
5911 seq_printf(seq
, " %s[%d]",
5912 bdevname(rdev
->bdev
,b
), rdev
->desc_nr
);
5913 if (test_bit(WriteMostly
, &rdev
->flags
))
5914 seq_printf(seq
, "(W)");
5915 if (test_bit(Faulty
, &rdev
->flags
)) {
5916 seq_printf(seq
, "(F)");
5918 } else if (rdev
->raid_disk
< 0)
5919 seq_printf(seq
, "(S)"); /* spare */
5920 sectors
+= rdev
->sectors
;
5923 if (!list_empty(&mddev
->disks
)) {
5925 seq_printf(seq
, "\n %llu blocks",
5926 (unsigned long long)
5927 mddev
->array_sectors
/ 2);
5929 seq_printf(seq
, "\n %llu blocks",
5930 (unsigned long long)sectors
/ 2);
5932 if (mddev
->persistent
) {
5933 if (mddev
->major_version
!= 0 ||
5934 mddev
->minor_version
!= 90) {
5935 seq_printf(seq
," super %d.%d",
5936 mddev
->major_version
,
5937 mddev
->minor_version
);
5939 } else if (mddev
->external
)
5940 seq_printf(seq
, " super external:%s",
5941 mddev
->metadata_type
);
5943 seq_printf(seq
, " super non-persistent");
5946 mddev
->pers
->status(seq
, mddev
);
5947 seq_printf(seq
, "\n ");
5948 if (mddev
->pers
->sync_request
) {
5949 if (mddev
->curr_resync
> 2) {
5950 status_resync(seq
, mddev
);
5951 seq_printf(seq
, "\n ");
5952 } else if (mddev
->curr_resync
== 1 || mddev
->curr_resync
== 2)
5953 seq_printf(seq
, "\tresync=DELAYED\n ");
5954 else if (mddev
->recovery_cp
< MaxSector
)
5955 seq_printf(seq
, "\tresync=PENDING\n ");
5958 seq_printf(seq
, "\n ");
5960 if ((bitmap
= mddev
->bitmap
)) {
5961 unsigned long chunk_kb
;
5962 unsigned long flags
;
5963 spin_lock_irqsave(&bitmap
->lock
, flags
);
5964 chunk_kb
= bitmap
->chunksize
>> 10;
5965 seq_printf(seq
, "bitmap: %lu/%lu pages [%luKB], "
5967 bitmap
->pages
- bitmap
->missing_pages
,
5969 (bitmap
->pages
- bitmap
->missing_pages
)
5970 << (PAGE_SHIFT
- 10),
5971 chunk_kb
? chunk_kb
: bitmap
->chunksize
,
5972 chunk_kb
? "KB" : "B");
5974 seq_printf(seq
, ", file: ");
5975 seq_path(seq
, &bitmap
->file
->f_path
, " \t\n");
5978 seq_printf(seq
, "\n");
5979 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
5982 seq_printf(seq
, "\n");
5984 mddev_unlock(mddev
);
5989 static const struct seq_operations md_seq_ops
= {
5990 .start
= md_seq_start
,
5991 .next
= md_seq_next
,
5992 .stop
= md_seq_stop
,
5993 .show
= md_seq_show
,
5996 static int md_seq_open(struct inode
*inode
, struct file
*file
)
5999 struct mdstat_info
*mi
= kmalloc(sizeof(*mi
), GFP_KERNEL
);
6003 error
= seq_open(file
, &md_seq_ops
);
6007 struct seq_file
*p
= file
->private_data
;
6009 mi
->event
= atomic_read(&md_event_count
);
6014 static unsigned int mdstat_poll(struct file
*filp
, poll_table
*wait
)
6016 struct seq_file
*m
= filp
->private_data
;
6017 struct mdstat_info
*mi
= m
->private;
6020 poll_wait(filp
, &md_event_waiters
, wait
);
6022 /* always allow read */
6023 mask
= POLLIN
| POLLRDNORM
;
6025 if (mi
->event
!= atomic_read(&md_event_count
))
6026 mask
|= POLLERR
| POLLPRI
;
6030 static const struct file_operations md_seq_fops
= {
6031 .owner
= THIS_MODULE
,
6032 .open
= md_seq_open
,
6034 .llseek
= seq_lseek
,
6035 .release
= seq_release_private
,
6036 .poll
= mdstat_poll
,
6039 int register_md_personality(struct mdk_personality
*p
)
6041 spin_lock(&pers_lock
);
6042 list_add_tail(&p
->list
, &pers_list
);
6043 printk(KERN_INFO
"md: %s personality registered for level %d\n", p
->name
, p
->level
);
6044 spin_unlock(&pers_lock
);
6048 int unregister_md_personality(struct mdk_personality
*p
)
6050 printk(KERN_INFO
"md: %s personality unregistered\n", p
->name
);
6051 spin_lock(&pers_lock
);
6052 list_del_init(&p
->list
);
6053 spin_unlock(&pers_lock
);
6057 static int is_mddev_idle(mddev_t
*mddev
, int init
)
6065 rdev_for_each_rcu(rdev
, mddev
) {
6066 struct gendisk
*disk
= rdev
->bdev
->bd_contains
->bd_disk
;
6067 curr_events
= (int)part_stat_read(&disk
->part0
, sectors
[0]) +
6068 (int)part_stat_read(&disk
->part0
, sectors
[1]) -
6069 atomic_read(&disk
->sync_io
);
6070 /* sync IO will cause sync_io to increase before the disk_stats
6071 * as sync_io is counted when a request starts, and
6072 * disk_stats is counted when it completes.
6073 * So resync activity will cause curr_events to be smaller than
6074 * when there was no such activity.
6075 * non-sync IO will cause disk_stat to increase without
6076 * increasing sync_io so curr_events will (eventually)
6077 * be larger than it was before. Once it becomes
6078 * substantially larger, the test below will cause
6079 * the array to appear non-idle, and resync will slow
6081 * If there is a lot of outstanding resync activity when
6082 * we set last_event to curr_events, then all that activity
6083 * completing might cause the array to appear non-idle
6084 * and resync will be slowed down even though there might
6085 * not have been non-resync activity. This will only
6086 * happen once though. 'last_events' will soon reflect
6087 * the state where there is little or no outstanding
6088 * resync requests, and further resync activity will
6089 * always make curr_events less than last_events.
6092 if (init
|| curr_events
- rdev
->last_events
> 64) {
6093 rdev
->last_events
= curr_events
;
6101 void md_done_sync(mddev_t
*mddev
, int blocks
, int ok
)
6103 /* another "blocks" (512byte) blocks have been synced */
6104 atomic_sub(blocks
, &mddev
->recovery_active
);
6105 wake_up(&mddev
->recovery_wait
);
6107 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6108 md_wakeup_thread(mddev
->thread
);
6109 // stop recovery, signal do_sync ....
6114 /* md_write_start(mddev, bi)
6115 * If we need to update some array metadata (e.g. 'active' flag
6116 * in superblock) before writing, schedule a superblock update
6117 * and wait for it to complete.
6119 void md_write_start(mddev_t
*mddev
, struct bio
*bi
)
6122 if (bio_data_dir(bi
) != WRITE
)
6125 BUG_ON(mddev
->ro
== 1);
6126 if (mddev
->ro
== 2) {
6127 /* need to switch to read/write */
6129 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6130 md_wakeup_thread(mddev
->thread
);
6131 md_wakeup_thread(mddev
->sync_thread
);
6134 atomic_inc(&mddev
->writes_pending
);
6135 if (mddev
->safemode
== 1)
6136 mddev
->safemode
= 0;
6137 if (mddev
->in_sync
) {
6138 spin_lock_irq(&mddev
->write_lock
);
6139 if (mddev
->in_sync
) {
6141 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6142 md_wakeup_thread(mddev
->thread
);
6145 spin_unlock_irq(&mddev
->write_lock
);
6148 sysfs_notify_dirent(mddev
->sysfs_state
);
6149 wait_event(mddev
->sb_wait
,
6150 !test_bit(MD_CHANGE_CLEAN
, &mddev
->flags
) &&
6151 !test_bit(MD_CHANGE_PENDING
, &mddev
->flags
));
6154 void md_write_end(mddev_t
*mddev
)
6156 if (atomic_dec_and_test(&mddev
->writes_pending
)) {
6157 if (mddev
->safemode
== 2)
6158 md_wakeup_thread(mddev
->thread
);
6159 else if (mddev
->safemode_delay
)
6160 mod_timer(&mddev
->safemode_timer
, jiffies
+ mddev
->safemode_delay
);
6164 /* md_allow_write(mddev)
6165 * Calling this ensures that the array is marked 'active' so that writes
6166 * may proceed without blocking. It is important to call this before
6167 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6168 * Must be called with mddev_lock held.
6170 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6171 * is dropped, so return -EAGAIN after notifying userspace.
6173 int md_allow_write(mddev_t
*mddev
)
6179 if (!mddev
->pers
->sync_request
)
6182 spin_lock_irq(&mddev
->write_lock
);
6183 if (mddev
->in_sync
) {
6185 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6186 if (mddev
->safemode_delay
&&
6187 mddev
->safemode
== 0)
6188 mddev
->safemode
= 1;
6189 spin_unlock_irq(&mddev
->write_lock
);
6190 md_update_sb(mddev
, 0);
6191 sysfs_notify_dirent(mddev
->sysfs_state
);
6193 spin_unlock_irq(&mddev
->write_lock
);
6195 if (test_bit(MD_CHANGE_CLEAN
, &mddev
->flags
))
6200 EXPORT_SYMBOL_GPL(md_allow_write
);
6202 #define SYNC_MARKS 10
6203 #define SYNC_MARK_STEP (3*HZ)
6204 void md_do_sync(mddev_t
*mddev
)
6207 unsigned int currspeed
= 0,
6209 sector_t max_sectors
,j
, io_sectors
;
6210 unsigned long mark
[SYNC_MARKS
];
6211 sector_t mark_cnt
[SYNC_MARKS
];
6213 struct list_head
*tmp
;
6214 sector_t last_check
;
6219 /* just incase thread restarts... */
6220 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
6222 if (mddev
->ro
) /* never try to sync a read-only array */
6225 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
6226 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
6227 desc
= "data-check";
6228 else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
6229 desc
= "requested-resync";
6232 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
6237 /* we overload curr_resync somewhat here.
6238 * 0 == not engaged in resync at all
6239 * 2 == checking that there is no conflict with another sync
6240 * 1 == like 2, but have yielded to allow conflicting resync to
6242 * other == active in resync - this many blocks
6244 * Before starting a resync we must have set curr_resync to
6245 * 2, and then checked that every "conflicting" array has curr_resync
6246 * less than ours. When we find one that is the same or higher
6247 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6248 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6249 * This will mean we have to start checking from the beginning again.
6254 mddev
->curr_resync
= 2;
6257 if (kthread_should_stop()) {
6258 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6261 for_each_mddev(mddev2
, tmp
) {
6262 if (mddev2
== mddev
)
6264 if (!mddev
->parallel_resync
6265 && mddev2
->curr_resync
6266 && match_mddev_units(mddev
, mddev2
)) {
6268 if (mddev
< mddev2
&& mddev
->curr_resync
== 2) {
6269 /* arbitrarily yield */
6270 mddev
->curr_resync
= 1;
6271 wake_up(&resync_wait
);
6273 if (mddev
> mddev2
&& mddev
->curr_resync
== 1)
6274 /* no need to wait here, we can wait the next
6275 * time 'round when curr_resync == 2
6278 /* We need to wait 'interruptible' so as not to
6279 * contribute to the load average, and not to
6280 * be caught by 'softlockup'
6282 prepare_to_wait(&resync_wait
, &wq
, TASK_INTERRUPTIBLE
);
6283 if (!kthread_should_stop() &&
6284 mddev2
->curr_resync
>= mddev
->curr_resync
) {
6285 printk(KERN_INFO
"md: delaying %s of %s"
6286 " until %s has finished (they"
6287 " share one or more physical units)\n",
6288 desc
, mdname(mddev
), mdname(mddev2
));
6290 if (signal_pending(current
))
6291 flush_signals(current
);
6293 finish_wait(&resync_wait
, &wq
);
6296 finish_wait(&resync_wait
, &wq
);
6299 } while (mddev
->curr_resync
< 2);
6302 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
6303 /* resync follows the size requested by the personality,
6304 * which defaults to physical size, but can be virtual size
6306 max_sectors
= mddev
->resync_max_sectors
;
6307 mddev
->resync_mismatches
= 0;
6308 /* we don't use the checkpoint if there's a bitmap */
6309 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
6310 j
= mddev
->resync_min
;
6311 else if (!mddev
->bitmap
)
6312 j
= mddev
->recovery_cp
;
6314 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
6315 max_sectors
= mddev
->dev_sectors
;
6317 /* recovery follows the physical size of devices */
6318 max_sectors
= mddev
->dev_sectors
;
6320 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
6321 if (rdev
->raid_disk
>= 0 &&
6322 !test_bit(Faulty
, &rdev
->flags
) &&
6323 !test_bit(In_sync
, &rdev
->flags
) &&
6324 rdev
->recovery_offset
< j
)
6325 j
= rdev
->recovery_offset
;
6328 printk(KERN_INFO
"md: %s of RAID array %s\n", desc
, mdname(mddev
));
6329 printk(KERN_INFO
"md: minimum _guaranteed_ speed:"
6330 " %d KB/sec/disk.\n", speed_min(mddev
));
6331 printk(KERN_INFO
"md: using maximum available idle IO bandwidth "
6332 "(but not more than %d KB/sec) for %s.\n",
6333 speed_max(mddev
), desc
);
6335 is_mddev_idle(mddev
, 1); /* this initializes IO event counters */
6338 for (m
= 0; m
< SYNC_MARKS
; m
++) {
6340 mark_cnt
[m
] = io_sectors
;
6343 mddev
->resync_mark
= mark
[last_mark
];
6344 mddev
->resync_mark_cnt
= mark_cnt
[last_mark
];
6347 * Tune reconstruction:
6349 window
= 32*(PAGE_SIZE
/512);
6350 printk(KERN_INFO
"md: using %dk window, over a total of %llu blocks.\n",
6351 window
/2,(unsigned long long) max_sectors
/2);
6353 atomic_set(&mddev
->recovery_active
, 0);
6358 "md: resuming %s of %s from checkpoint.\n",
6359 desc
, mdname(mddev
));
6360 mddev
->curr_resync
= j
;
6363 while (j
< max_sectors
) {
6368 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
6369 ((mddev
->curr_resync
> mddev
->curr_resync_completed
&&
6370 (mddev
->curr_resync
- mddev
->curr_resync_completed
)
6371 > (max_sectors
>> 4)) ||
6372 (j
- mddev
->curr_resync_completed
)*2
6373 >= mddev
->resync_max
- mddev
->curr_resync_completed
6375 /* time to update curr_resync_completed */
6376 blk_unplug(mddev
->queue
);
6377 wait_event(mddev
->recovery_wait
,
6378 atomic_read(&mddev
->recovery_active
) == 0);
6379 mddev
->curr_resync_completed
=
6381 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6382 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
6385 while (j
>= mddev
->resync_max
&& !kthread_should_stop()) {
6386 /* As this condition is controlled by user-space,
6387 * we can block indefinitely, so use '_interruptible'
6388 * to avoid triggering warnings.
6390 flush_signals(current
); /* just in case */
6391 wait_event_interruptible(mddev
->recovery_wait
,
6392 mddev
->resync_max
> j
6393 || kthread_should_stop());
6396 if (kthread_should_stop())
6399 sectors
= mddev
->pers
->sync_request(mddev
, j
, &skipped
,
6400 currspeed
< speed_min(mddev
));
6402 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6406 if (!skipped
) { /* actual IO requested */
6407 io_sectors
+= sectors
;
6408 atomic_add(sectors
, &mddev
->recovery_active
);
6412 if (j
>1) mddev
->curr_resync
= j
;
6413 mddev
->curr_mark_cnt
= io_sectors
;
6414 if (last_check
== 0)
6415 /* this is the earliers that rebuilt will be
6416 * visible in /proc/mdstat
6418 md_new_event(mddev
);
6420 if (last_check
+ window
> io_sectors
|| j
== max_sectors
)
6423 last_check
= io_sectors
;
6425 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
6429 if (time_after_eq(jiffies
, mark
[last_mark
] + SYNC_MARK_STEP
)) {
6431 int next
= (last_mark
+1) % SYNC_MARKS
;
6433 mddev
->resync_mark
= mark
[next
];
6434 mddev
->resync_mark_cnt
= mark_cnt
[next
];
6435 mark
[next
] = jiffies
;
6436 mark_cnt
[next
] = io_sectors
- atomic_read(&mddev
->recovery_active
);
6441 if (kthread_should_stop())
6446 * this loop exits only if either when we are slower than
6447 * the 'hard' speed limit, or the system was IO-idle for
6449 * the system might be non-idle CPU-wise, but we only care
6450 * about not overloading the IO subsystem. (things like an
6451 * e2fsck being done on the RAID array should execute fast)
6453 blk_unplug(mddev
->queue
);
6456 currspeed
= ((unsigned long)(io_sectors
-mddev
->resync_mark_cnt
))/2
6457 /((jiffies
-mddev
->resync_mark
)/HZ
+1) +1;
6459 if (currspeed
> speed_min(mddev
)) {
6460 if ((currspeed
> speed_max(mddev
)) ||
6461 !is_mddev_idle(mddev
, 0)) {
6467 printk(KERN_INFO
"md: %s: %s done.\n",mdname(mddev
), desc
);
6469 * this also signals 'finished resyncing' to md_stop
6472 blk_unplug(mddev
->queue
);
6474 wait_event(mddev
->recovery_wait
, !atomic_read(&mddev
->recovery_active
));
6476 /* tell personality that we are finished */
6477 mddev
->pers
->sync_request(mddev
, max_sectors
, &skipped
, 1);
6479 if (!test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
) &&
6480 mddev
->curr_resync
> 2) {
6481 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
6482 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
6483 if (mddev
->curr_resync
>= mddev
->recovery_cp
) {
6485 "md: checkpointing %s of %s.\n",
6486 desc
, mdname(mddev
));
6487 mddev
->recovery_cp
= mddev
->curr_resync
;
6490 mddev
->recovery_cp
= MaxSector
;
6492 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
6493 mddev
->curr_resync
= MaxSector
;
6494 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
6495 if (rdev
->raid_disk
>= 0 &&
6496 !test_bit(Faulty
, &rdev
->flags
) &&
6497 !test_bit(In_sync
, &rdev
->flags
) &&
6498 rdev
->recovery_offset
< mddev
->curr_resync
)
6499 rdev
->recovery_offset
= mddev
->curr_resync
;
6502 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
6505 mddev
->curr_resync
= 0;
6506 mddev
->curr_resync_completed
= 0;
6507 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
6508 /* We completed so max setting can be forgotten. */
6509 mddev
->resync_max
= MaxSector
;
6510 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
6511 wake_up(&resync_wait
);
6512 set_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
6513 md_wakeup_thread(mddev
->thread
);
6518 * got a signal, exit.
6521 "md: md_do_sync() got signal ... exiting\n");
6522 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6526 EXPORT_SYMBOL_GPL(md_do_sync
);
6529 static int remove_and_add_spares(mddev_t
*mddev
)
6534 mddev
->curr_resync_completed
= 0;
6536 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
6537 if (rdev
->raid_disk
>= 0 &&
6538 !test_bit(Blocked
, &rdev
->flags
) &&
6539 (test_bit(Faulty
, &rdev
->flags
) ||
6540 ! test_bit(In_sync
, &rdev
->flags
)) &&
6541 atomic_read(&rdev
->nr_pending
)==0) {
6542 if (mddev
->pers
->hot_remove_disk(
6543 mddev
, rdev
->raid_disk
)==0) {
6545 sprintf(nm
,"rd%d", rdev
->raid_disk
);
6546 sysfs_remove_link(&mddev
->kobj
, nm
);
6547 rdev
->raid_disk
= -1;
6551 if (mddev
->degraded
&& ! mddev
->ro
&& !mddev
->recovery_disabled
) {
6552 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
6553 if (rdev
->raid_disk
>= 0 &&
6554 !test_bit(In_sync
, &rdev
->flags
) &&
6555 !test_bit(Blocked
, &rdev
->flags
))
6557 if (rdev
->raid_disk
< 0
6558 && !test_bit(Faulty
, &rdev
->flags
)) {
6559 rdev
->recovery_offset
= 0;
6561 hot_add_disk(mddev
, rdev
) == 0) {
6563 sprintf(nm
, "rd%d", rdev
->raid_disk
);
6564 if (sysfs_create_link(&mddev
->kobj
,
6567 "md: cannot register "
6571 md_new_event(mddev
);
6580 * This routine is regularly called by all per-raid-array threads to
6581 * deal with generic issues like resync and super-block update.
6582 * Raid personalities that don't have a thread (linear/raid0) do not
6583 * need this as they never do any recovery or update the superblock.
6585 * It does not do any resync itself, but rather "forks" off other threads
6586 * to do that as needed.
6587 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6588 * "->recovery" and create a thread at ->sync_thread.
6589 * When the thread finishes it sets MD_RECOVERY_DONE
6590 * and wakeups up this thread which will reap the thread and finish up.
6591 * This thread also removes any faulty devices (with nr_pending == 0).
6593 * The overall approach is:
6594 * 1/ if the superblock needs updating, update it.
6595 * 2/ If a recovery thread is running, don't do anything else.
6596 * 3/ If recovery has finished, clean up, possibly marking spares active.
6597 * 4/ If there are any faulty devices, remove them.
6598 * 5/ If array is degraded, try to add spares devices
6599 * 6/ If array has spares or is not in-sync, start a resync thread.
6601 void md_check_recovery(mddev_t
*mddev
)
6607 bitmap_daemon_work(mddev
->bitmap
);
6612 if (signal_pending(current
)) {
6613 if (mddev
->pers
->sync_request
&& !mddev
->external
) {
6614 printk(KERN_INFO
"md: %s in immediate safe mode\n",
6616 mddev
->safemode
= 2;
6618 flush_signals(current
);
6621 if (mddev
->ro
&& !test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
6624 (mddev
->flags
&& !mddev
->external
) ||
6625 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
6626 test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
6627 (mddev
->external
== 0 && mddev
->safemode
== 1) ||
6628 (mddev
->safemode
== 2 && ! atomic_read(&mddev
->writes_pending
)
6629 && !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
6633 if (mddev_trylock(mddev
)) {
6637 /* Only thing we do on a ro array is remove
6640 remove_and_add_spares(mddev
);
6641 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6645 if (!mddev
->external
) {
6647 spin_lock_irq(&mddev
->write_lock
);
6648 if (mddev
->safemode
&&
6649 !atomic_read(&mddev
->writes_pending
) &&
6651 mddev
->recovery_cp
== MaxSector
) {
6654 if (mddev
->persistent
)
6655 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6657 if (mddev
->safemode
== 1)
6658 mddev
->safemode
= 0;
6659 spin_unlock_irq(&mddev
->write_lock
);
6661 sysfs_notify_dirent(mddev
->sysfs_state
);
6665 md_update_sb(mddev
, 0);
6667 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
6668 if (test_and_clear_bit(StateChanged
, &rdev
->flags
))
6669 sysfs_notify_dirent(rdev
->sysfs_state
);
6672 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
6673 !test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
)) {
6674 /* resync/recovery still happening */
6675 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6678 if (mddev
->sync_thread
) {
6679 /* resync has finished, collect result */
6680 md_unregister_thread(mddev
->sync_thread
);
6681 mddev
->sync_thread
= NULL
;
6682 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
6683 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
6685 /* activate any spares */
6686 if (mddev
->pers
->spare_active(mddev
))
6687 sysfs_notify(&mddev
->kobj
, NULL
,
6690 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
6691 mddev
->pers
->finish_reshape
)
6692 mddev
->pers
->finish_reshape(mddev
);
6693 md_update_sb(mddev
, 1);
6695 /* if array is no-longer degraded, then any saved_raid_disk
6696 * information must be scrapped
6698 if (!mddev
->degraded
)
6699 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
6700 rdev
->saved_raid_disk
= -1;
6702 mddev
->recovery
= 0;
6703 /* flag recovery needed just to double check */
6704 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6705 sysfs_notify_dirent(mddev
->sysfs_action
);
6706 md_new_event(mddev
);
6709 /* Set RUNNING before clearing NEEDED to avoid
6710 * any transients in the value of "sync_action".
6712 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
6713 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6714 /* Clear some bits that don't mean anything, but
6717 clear_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6718 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
6720 if (test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
6722 /* no recovery is running.
6723 * remove any failed drives, then
6724 * add spares if possible.
6725 * Spare are also removed and re-added, to allow
6726 * the personality to fail the re-add.
6729 if (mddev
->reshape_position
!= MaxSector
) {
6730 if (mddev
->pers
->check_reshape
== NULL
||
6731 mddev
->pers
->check_reshape(mddev
) != 0)
6732 /* Cannot proceed */
6734 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
6735 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
6736 } else if ((spares
= remove_and_add_spares(mddev
))) {
6737 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
6738 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
6739 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
6740 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
6741 } else if (mddev
->recovery_cp
< MaxSector
) {
6742 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
6743 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
6744 } else if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
6745 /* nothing to be done ... */
6748 if (mddev
->pers
->sync_request
) {
6749 if (spares
&& mddev
->bitmap
&& ! mddev
->bitmap
->file
) {
6750 /* We are adding a device or devices to an array
6751 * which has the bitmap stored on all devices.
6752 * So make sure all bitmap pages get written
6754 bitmap_write_all(mddev
->bitmap
);
6756 mddev
->sync_thread
= md_register_thread(md_do_sync
,
6759 if (!mddev
->sync_thread
) {
6760 printk(KERN_ERR
"%s: could not start resync"
6763 /* leave the spares where they are, it shouldn't hurt */
6764 mddev
->recovery
= 0;
6766 md_wakeup_thread(mddev
->sync_thread
);
6767 sysfs_notify_dirent(mddev
->sysfs_action
);
6768 md_new_event(mddev
);
6771 if (!mddev
->sync_thread
) {
6772 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
6773 if (test_and_clear_bit(MD_RECOVERY_RECOVER
,
6775 if (mddev
->sysfs_action
)
6776 sysfs_notify_dirent(mddev
->sysfs_action
);
6778 mddev_unlock(mddev
);
6782 void md_wait_for_blocked_rdev(mdk_rdev_t
*rdev
, mddev_t
*mddev
)
6784 sysfs_notify_dirent(rdev
->sysfs_state
);
6785 wait_event_timeout(rdev
->blocked_wait
,
6786 !test_bit(Blocked
, &rdev
->flags
),
6787 msecs_to_jiffies(5000));
6788 rdev_dec_pending(rdev
, mddev
);
6790 EXPORT_SYMBOL(md_wait_for_blocked_rdev
);
6792 static int md_notify_reboot(struct notifier_block
*this,
6793 unsigned long code
, void *x
)
6795 struct list_head
*tmp
;
6798 if ((code
== SYS_DOWN
) || (code
== SYS_HALT
) || (code
== SYS_POWER_OFF
)) {
6800 printk(KERN_INFO
"md: stopping all md devices.\n");
6802 for_each_mddev(mddev
, tmp
)
6803 if (mddev_trylock(mddev
)) {
6804 /* Force a switch to readonly even array
6805 * appears to still be in use. Hence
6808 do_md_stop(mddev
, 1, 100);
6809 mddev_unlock(mddev
);
6812 * certain more exotic SCSI devices are known to be
6813 * volatile wrt too early system reboots. While the
6814 * right place to handle this issue is the given
6815 * driver, we do want to have a safe RAID driver ...
6822 static struct notifier_block md_notifier
= {
6823 .notifier_call
= md_notify_reboot
,
6825 .priority
= INT_MAX
, /* before any real devices */
6828 static void md_geninit(void)
6830 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t
));
6832 proc_create("mdstat", S_IRUGO
, NULL
, &md_seq_fops
);
6835 static int __init
md_init(void)
6837 if (register_blkdev(MD_MAJOR
, "md"))
6839 if ((mdp_major
=register_blkdev(0, "mdp"))<=0) {
6840 unregister_blkdev(MD_MAJOR
, "md");
6843 blk_register_region(MKDEV(MD_MAJOR
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
6844 md_probe
, NULL
, NULL
);
6845 blk_register_region(MKDEV(mdp_major
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
6846 md_probe
, NULL
, NULL
);
6848 register_reboot_notifier(&md_notifier
);
6849 raid_table_header
= register_sysctl_table(raid_root_table
);
6859 * Searches all registered partitions for autorun RAID arrays
6863 static LIST_HEAD(all_detected_devices
);
6864 struct detected_devices_node
{
6865 struct list_head list
;
6869 void md_autodetect_dev(dev_t dev
)
6871 struct detected_devices_node
*node_detected_dev
;
6873 node_detected_dev
= kzalloc(sizeof(*node_detected_dev
), GFP_KERNEL
);
6874 if (node_detected_dev
) {
6875 node_detected_dev
->dev
= dev
;
6876 list_add_tail(&node_detected_dev
->list
, &all_detected_devices
);
6878 printk(KERN_CRIT
"md: md_autodetect_dev: kzalloc failed"
6879 ", skipping dev(%d,%d)\n", MAJOR(dev
), MINOR(dev
));
6884 static void autostart_arrays(int part
)
6887 struct detected_devices_node
*node_detected_dev
;
6889 int i_scanned
, i_passed
;
6894 printk(KERN_INFO
"md: Autodetecting RAID arrays.\n");
6896 while (!list_empty(&all_detected_devices
) && i_scanned
< INT_MAX
) {
6898 node_detected_dev
= list_entry(all_detected_devices
.next
,
6899 struct detected_devices_node
, list
);
6900 list_del(&node_detected_dev
->list
);
6901 dev
= node_detected_dev
->dev
;
6902 kfree(node_detected_dev
);
6903 rdev
= md_import_device(dev
,0, 90);
6907 if (test_bit(Faulty
, &rdev
->flags
)) {
6911 set_bit(AutoDetected
, &rdev
->flags
);
6912 list_add(&rdev
->same_set
, &pending_raid_disks
);
6916 printk(KERN_INFO
"md: Scanned %d and added %d devices.\n",
6917 i_scanned
, i_passed
);
6919 autorun_devices(part
);
6922 #endif /* !MODULE */
6924 static __exit
void md_exit(void)
6927 struct list_head
*tmp
;
6929 blk_unregister_region(MKDEV(MD_MAJOR
,0), 1U << MINORBITS
);
6930 blk_unregister_region(MKDEV(mdp_major
,0), 1U << MINORBITS
);
6932 unregister_blkdev(MD_MAJOR
,"md");
6933 unregister_blkdev(mdp_major
, "mdp");
6934 unregister_reboot_notifier(&md_notifier
);
6935 unregister_sysctl_table(raid_table_header
);
6936 remove_proc_entry("mdstat", NULL
);
6937 for_each_mddev(mddev
, tmp
) {
6938 export_array(mddev
);
6939 mddev
->hold_active
= 0;
6943 subsys_initcall(md_init
);
6944 module_exit(md_exit
)
6946 static int get_ro(char *buffer
, struct kernel_param
*kp
)
6948 return sprintf(buffer
, "%d", start_readonly
);
6950 static int set_ro(const char *val
, struct kernel_param
*kp
)
6953 int num
= simple_strtoul(val
, &e
, 10);
6954 if (*val
&& (*e
== '\0' || *e
== '\n')) {
6955 start_readonly
= num
;
6961 module_param_call(start_ro
, set_ro
, get_ro
, NULL
, S_IRUSR
|S_IWUSR
);
6962 module_param(start_dirty_degraded
, int, S_IRUGO
|S_IWUSR
);
6964 module_param_call(new_array
, add_named_array
, NULL
, NULL
, S_IWUSR
);
6966 EXPORT_SYMBOL(register_md_personality
);
6967 EXPORT_SYMBOL(unregister_md_personality
);
6968 EXPORT_SYMBOL(md_error
);
6969 EXPORT_SYMBOL(md_done_sync
);
6970 EXPORT_SYMBOL(md_write_start
);
6971 EXPORT_SYMBOL(md_write_end
);
6972 EXPORT_SYMBOL(md_register_thread
);
6973 EXPORT_SYMBOL(md_unregister_thread
);
6974 EXPORT_SYMBOL(md_wakeup_thread
);
6975 EXPORT_SYMBOL(md_check_recovery
);
6976 MODULE_LICENSE("GPL");
6978 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR
);