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 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);
266 static inline mddev_t
*mddev_get(mddev_t
*mddev
)
268 atomic_inc(&mddev
->active
);
272 static void mddev_delayed_delete(struct work_struct
*ws
);
274 static void mddev_put(mddev_t
*mddev
)
276 if (!atomic_dec_and_lock(&mddev
->active
, &all_mddevs_lock
))
278 if (!mddev
->raid_disks
&& list_empty(&mddev
->disks
) &&
279 !mddev
->hold_active
) {
280 list_del(&mddev
->all_mddevs
);
281 if (mddev
->gendisk
) {
282 /* we did a probe so need to clean up.
283 * Call schedule_work inside the spinlock
284 * so that flush_scheduled_work() after
285 * mddev_find will succeed in waiting for the
288 INIT_WORK(&mddev
->del_work
, mddev_delayed_delete
);
289 schedule_work(&mddev
->del_work
);
293 spin_unlock(&all_mddevs_lock
);
296 static mddev_t
* mddev_find(dev_t unit
)
298 mddev_t
*mddev
, *new = NULL
;
301 spin_lock(&all_mddevs_lock
);
304 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
305 if (mddev
->unit
== unit
) {
307 spin_unlock(&all_mddevs_lock
);
313 list_add(&new->all_mddevs
, &all_mddevs
);
314 spin_unlock(&all_mddevs_lock
);
315 new->hold_active
= UNTIL_IOCTL
;
319 /* find an unused unit number */
320 static int next_minor
= 512;
321 int start
= next_minor
;
325 dev
= MKDEV(MD_MAJOR
, next_minor
);
327 if (next_minor
> MINORMASK
)
329 if (next_minor
== start
) {
330 /* Oh dear, all in use. */
331 spin_unlock(&all_mddevs_lock
);
337 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
338 if (mddev
->unit
== dev
) {
344 new->md_minor
= MINOR(dev
);
345 new->hold_active
= UNTIL_STOP
;
346 list_add(&new->all_mddevs
, &all_mddevs
);
347 spin_unlock(&all_mddevs_lock
);
350 spin_unlock(&all_mddevs_lock
);
352 new = kzalloc(sizeof(*new), GFP_KERNEL
);
357 if (MAJOR(unit
) == MD_MAJOR
)
358 new->md_minor
= MINOR(unit
);
360 new->md_minor
= MINOR(unit
) >> MdpMinorShift
;
362 mutex_init(&new->reconfig_mutex
);
363 INIT_LIST_HEAD(&new->disks
);
364 INIT_LIST_HEAD(&new->all_mddevs
);
365 init_timer(&new->safemode_timer
);
366 atomic_set(&new->active
, 1);
367 atomic_set(&new->openers
, 0);
368 atomic_set(&new->active_io
, 0);
369 spin_lock_init(&new->write_lock
);
370 init_waitqueue_head(&new->sb_wait
);
371 init_waitqueue_head(&new->recovery_wait
);
372 new->reshape_position
= MaxSector
;
374 new->resync_max
= MaxSector
;
375 new->level
= LEVEL_NONE
;
380 static inline int mddev_lock(mddev_t
* mddev
)
382 return mutex_lock_interruptible(&mddev
->reconfig_mutex
);
385 static inline int mddev_is_locked(mddev_t
*mddev
)
387 return mutex_is_locked(&mddev
->reconfig_mutex
);
390 static inline int mddev_trylock(mddev_t
* mddev
)
392 return mutex_trylock(&mddev
->reconfig_mutex
);
395 static inline void mddev_unlock(mddev_t
* mddev
)
397 mutex_unlock(&mddev
->reconfig_mutex
);
399 md_wakeup_thread(mddev
->thread
);
402 static mdk_rdev_t
* find_rdev_nr(mddev_t
*mddev
, int nr
)
406 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
407 if (rdev
->desc_nr
== nr
)
413 static mdk_rdev_t
* find_rdev(mddev_t
* mddev
, dev_t dev
)
417 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
418 if (rdev
->bdev
->bd_dev
== dev
)
424 static struct mdk_personality
*find_pers(int level
, char *clevel
)
426 struct mdk_personality
*pers
;
427 list_for_each_entry(pers
, &pers_list
, list
) {
428 if (level
!= LEVEL_NONE
&& pers
->level
== level
)
430 if (strcmp(pers
->name
, clevel
)==0)
436 /* return the offset of the super block in 512byte sectors */
437 static inline sector_t
calc_dev_sboffset(struct block_device
*bdev
)
439 sector_t num_sectors
= bdev
->bd_inode
->i_size
/ 512;
440 return MD_NEW_SIZE_SECTORS(num_sectors
);
443 static int alloc_disk_sb(mdk_rdev_t
* rdev
)
448 rdev
->sb_page
= alloc_page(GFP_KERNEL
);
449 if (!rdev
->sb_page
) {
450 printk(KERN_ALERT
"md: out of memory.\n");
457 static void free_disk_sb(mdk_rdev_t
* rdev
)
460 put_page(rdev
->sb_page
);
462 rdev
->sb_page
= NULL
;
469 static void super_written(struct bio
*bio
, int error
)
471 mdk_rdev_t
*rdev
= bio
->bi_private
;
472 mddev_t
*mddev
= rdev
->mddev
;
474 if (error
|| !test_bit(BIO_UPTODATE
, &bio
->bi_flags
)) {
475 printk("md: super_written gets error=%d, uptodate=%d\n",
476 error
, test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
477 WARN_ON(test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
478 md_error(mddev
, rdev
);
481 if (atomic_dec_and_test(&mddev
->pending_writes
))
482 wake_up(&mddev
->sb_wait
);
486 static void super_written_barrier(struct bio
*bio
, int error
)
488 struct bio
*bio2
= bio
->bi_private
;
489 mdk_rdev_t
*rdev
= bio2
->bi_private
;
490 mddev_t
*mddev
= rdev
->mddev
;
492 if (!test_bit(BIO_UPTODATE
, &bio
->bi_flags
) &&
493 error
== -EOPNOTSUPP
) {
495 /* barriers don't appear to be supported :-( */
496 set_bit(BarriersNotsupp
, &rdev
->flags
);
497 mddev
->barriers_work
= 0;
498 spin_lock_irqsave(&mddev
->write_lock
, flags
);
499 bio2
->bi_next
= mddev
->biolist
;
500 mddev
->biolist
= bio2
;
501 spin_unlock_irqrestore(&mddev
->write_lock
, flags
);
502 wake_up(&mddev
->sb_wait
);
506 bio
->bi_private
= rdev
;
507 super_written(bio
, error
);
511 void md_super_write(mddev_t
*mddev
, mdk_rdev_t
*rdev
,
512 sector_t sector
, int size
, struct page
*page
)
514 /* write first size bytes of page to sector of rdev
515 * Increment mddev->pending_writes before returning
516 * and decrement it on completion, waking up sb_wait
517 * if zero is reached.
518 * If an error occurred, call md_error
520 * As we might need to resubmit the request if BIO_RW_BARRIER
521 * causes ENOTSUPP, we allocate a spare bio...
523 struct bio
*bio
= bio_alloc(GFP_NOIO
, 1);
524 int rw
= (1<<BIO_RW
) | (1<<BIO_RW_SYNCIO
) | (1<<BIO_RW_UNPLUG
);
526 bio
->bi_bdev
= rdev
->bdev
;
527 bio
->bi_sector
= sector
;
528 bio_add_page(bio
, page
, size
, 0);
529 bio
->bi_private
= rdev
;
530 bio
->bi_end_io
= super_written
;
533 atomic_inc(&mddev
->pending_writes
);
534 if (!test_bit(BarriersNotsupp
, &rdev
->flags
)) {
536 rw
|= (1<<BIO_RW_BARRIER
);
537 rbio
= bio_clone(bio
, GFP_NOIO
);
538 rbio
->bi_private
= bio
;
539 rbio
->bi_end_io
= super_written_barrier
;
540 submit_bio(rw
, rbio
);
545 void md_super_wait(mddev_t
*mddev
)
547 /* wait for all superblock writes that were scheduled to complete.
548 * if any had to be retried (due to BARRIER problems), retry them
552 prepare_to_wait(&mddev
->sb_wait
, &wq
, TASK_UNINTERRUPTIBLE
);
553 if (atomic_read(&mddev
->pending_writes
)==0)
555 while (mddev
->biolist
) {
557 spin_lock_irq(&mddev
->write_lock
);
558 bio
= mddev
->biolist
;
559 mddev
->biolist
= bio
->bi_next
;
561 spin_unlock_irq(&mddev
->write_lock
);
562 submit_bio(bio
->bi_rw
, bio
);
566 finish_wait(&mddev
->sb_wait
, &wq
);
569 static void bi_complete(struct bio
*bio
, int error
)
571 complete((struct completion
*)bio
->bi_private
);
574 int sync_page_io(struct block_device
*bdev
, sector_t sector
, int size
,
575 struct page
*page
, int rw
)
577 struct bio
*bio
= bio_alloc(GFP_NOIO
, 1);
578 struct completion event
;
581 rw
|= (1 << BIO_RW_SYNCIO
) | (1 << BIO_RW_UNPLUG
);
584 bio
->bi_sector
= sector
;
585 bio_add_page(bio
, page
, size
, 0);
586 init_completion(&event
);
587 bio
->bi_private
= &event
;
588 bio
->bi_end_io
= bi_complete
;
590 wait_for_completion(&event
);
592 ret
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
596 EXPORT_SYMBOL_GPL(sync_page_io
);
598 static int read_disk_sb(mdk_rdev_t
* rdev
, int size
)
600 char b
[BDEVNAME_SIZE
];
601 if (!rdev
->sb_page
) {
609 if (!sync_page_io(rdev
->bdev
, rdev
->sb_start
, size
, rdev
->sb_page
, READ
))
615 printk(KERN_WARNING
"md: disabled device %s, could not read superblock.\n",
616 bdevname(rdev
->bdev
,b
));
620 static int uuid_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
622 return sb1
->set_uuid0
== sb2
->set_uuid0
&&
623 sb1
->set_uuid1
== sb2
->set_uuid1
&&
624 sb1
->set_uuid2
== sb2
->set_uuid2
&&
625 sb1
->set_uuid3
== sb2
->set_uuid3
;
628 static int sb_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
631 mdp_super_t
*tmp1
, *tmp2
;
633 tmp1
= kmalloc(sizeof(*tmp1
),GFP_KERNEL
);
634 tmp2
= kmalloc(sizeof(*tmp2
),GFP_KERNEL
);
636 if (!tmp1
|| !tmp2
) {
638 printk(KERN_INFO
"md.c sb_equal(): failed to allocate memory!\n");
646 * nr_disks is not constant
651 ret
= (memcmp(tmp1
, tmp2
, MD_SB_GENERIC_CONSTANT_WORDS
* 4) == 0);
659 static u32
md_csum_fold(u32 csum
)
661 csum
= (csum
& 0xffff) + (csum
>> 16);
662 return (csum
& 0xffff) + (csum
>> 16);
665 static unsigned int calc_sb_csum(mdp_super_t
* sb
)
668 u32
*sb32
= (u32
*)sb
;
670 unsigned int disk_csum
, csum
;
672 disk_csum
= sb
->sb_csum
;
675 for (i
= 0; i
< MD_SB_BYTES
/4 ; i
++)
677 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
681 /* This used to use csum_partial, which was wrong for several
682 * reasons including that different results are returned on
683 * different architectures. It isn't critical that we get exactly
684 * the same return value as before (we always csum_fold before
685 * testing, and that removes any differences). However as we
686 * know that csum_partial always returned a 16bit value on
687 * alphas, do a fold to maximise conformity to previous behaviour.
689 sb
->sb_csum
= md_csum_fold(disk_csum
);
691 sb
->sb_csum
= disk_csum
;
698 * Handle superblock details.
699 * We want to be able to handle multiple superblock formats
700 * so we have a common interface to them all, and an array of
701 * different handlers.
702 * We rely on user-space to write the initial superblock, and support
703 * reading and updating of superblocks.
704 * Interface methods are:
705 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
706 * loads and validates a superblock on dev.
707 * if refdev != NULL, compare superblocks on both devices
709 * 0 - dev has a superblock that is compatible with refdev
710 * 1 - dev has a superblock that is compatible and newer than refdev
711 * so dev should be used as the refdev in future
712 * -EINVAL superblock incompatible or invalid
713 * -othererror e.g. -EIO
715 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
716 * Verify that dev is acceptable into mddev.
717 * The first time, mddev->raid_disks will be 0, and data from
718 * dev should be merged in. Subsequent calls check that dev
719 * is new enough. Return 0 or -EINVAL
721 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
722 * Update the superblock for rdev with data in mddev
723 * This does not write to disc.
729 struct module
*owner
;
730 int (*load_super
)(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
,
732 int (*validate_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
733 void (*sync_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
734 unsigned long long (*rdev_size_change
)(mdk_rdev_t
*rdev
,
735 sector_t num_sectors
);
739 * Check that the given mddev has no bitmap.
741 * This function is called from the run method of all personalities that do not
742 * support bitmaps. It prints an error message and returns non-zero if mddev
743 * has a bitmap. Otherwise, it returns 0.
746 int md_check_no_bitmap(mddev_t
*mddev
)
748 if (!mddev
->bitmap_file
&& !mddev
->bitmap_offset
)
750 printk(KERN_ERR
"%s: bitmaps are not supported for %s\n",
751 mdname(mddev
), mddev
->pers
->name
);
754 EXPORT_SYMBOL(md_check_no_bitmap
);
757 * load_super for 0.90.0
759 static int super_90_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
761 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
766 * Calculate the position of the superblock (512byte sectors),
767 * it's at the end of the disk.
769 * It also happens to be a multiple of 4Kb.
771 rdev
->sb_start
= calc_dev_sboffset(rdev
->bdev
);
773 ret
= read_disk_sb(rdev
, MD_SB_BYTES
);
778 bdevname(rdev
->bdev
, b
);
779 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
781 if (sb
->md_magic
!= MD_SB_MAGIC
) {
782 printk(KERN_ERR
"md: invalid raid superblock magic on %s\n",
787 if (sb
->major_version
!= 0 ||
788 sb
->minor_version
< 90 ||
789 sb
->minor_version
> 91) {
790 printk(KERN_WARNING
"Bad version number %d.%d on %s\n",
791 sb
->major_version
, sb
->minor_version
,
796 if (sb
->raid_disks
<= 0)
799 if (md_csum_fold(calc_sb_csum(sb
)) != md_csum_fold(sb
->sb_csum
)) {
800 printk(KERN_WARNING
"md: invalid superblock checksum on %s\n",
805 rdev
->preferred_minor
= sb
->md_minor
;
806 rdev
->data_offset
= 0;
807 rdev
->sb_size
= MD_SB_BYTES
;
809 if (sb
->level
== LEVEL_MULTIPATH
)
812 rdev
->desc_nr
= sb
->this_disk
.number
;
818 mdp_super_t
*refsb
= (mdp_super_t
*)page_address(refdev
->sb_page
);
819 if (!uuid_equal(refsb
, sb
)) {
820 printk(KERN_WARNING
"md: %s has different UUID to %s\n",
821 b
, bdevname(refdev
->bdev
,b2
));
824 if (!sb_equal(refsb
, sb
)) {
825 printk(KERN_WARNING
"md: %s has same UUID"
826 " but different superblock to %s\n",
827 b
, bdevname(refdev
->bdev
, b2
));
831 ev2
= md_event(refsb
);
837 rdev
->sectors
= rdev
->sb_start
;
839 if (rdev
->sectors
< sb
->size
* 2 && sb
->level
> 1)
840 /* "this cannot possibly happen" ... */
848 * validate_super for 0.90.0
850 static int super_90_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
853 mdp_super_t
*sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
854 __u64 ev1
= md_event(sb
);
856 rdev
->raid_disk
= -1;
857 clear_bit(Faulty
, &rdev
->flags
);
858 clear_bit(In_sync
, &rdev
->flags
);
859 clear_bit(WriteMostly
, &rdev
->flags
);
860 clear_bit(BarriersNotsupp
, &rdev
->flags
);
862 if (mddev
->raid_disks
== 0) {
863 mddev
->major_version
= 0;
864 mddev
->minor_version
= sb
->minor_version
;
865 mddev
->patch_version
= sb
->patch_version
;
867 mddev
->chunk_sectors
= sb
->chunk_size
>> 9;
868 mddev
->ctime
= sb
->ctime
;
869 mddev
->utime
= sb
->utime
;
870 mddev
->level
= sb
->level
;
871 mddev
->clevel
[0] = 0;
872 mddev
->layout
= sb
->layout
;
873 mddev
->raid_disks
= sb
->raid_disks
;
874 mddev
->dev_sectors
= sb
->size
* 2;
876 mddev
->bitmap_offset
= 0;
877 mddev
->default_bitmap_offset
= MD_SB_BYTES
>> 9;
879 if (mddev
->minor_version
>= 91) {
880 mddev
->reshape_position
= sb
->reshape_position
;
881 mddev
->delta_disks
= sb
->delta_disks
;
882 mddev
->new_level
= sb
->new_level
;
883 mddev
->new_layout
= sb
->new_layout
;
884 mddev
->new_chunk_sectors
= sb
->new_chunk
>> 9;
886 mddev
->reshape_position
= MaxSector
;
887 mddev
->delta_disks
= 0;
888 mddev
->new_level
= mddev
->level
;
889 mddev
->new_layout
= mddev
->layout
;
890 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
893 if (sb
->state
& (1<<MD_SB_CLEAN
))
894 mddev
->recovery_cp
= MaxSector
;
896 if (sb
->events_hi
== sb
->cp_events_hi
&&
897 sb
->events_lo
== sb
->cp_events_lo
) {
898 mddev
->recovery_cp
= sb
->recovery_cp
;
900 mddev
->recovery_cp
= 0;
903 memcpy(mddev
->uuid
+0, &sb
->set_uuid0
, 4);
904 memcpy(mddev
->uuid
+4, &sb
->set_uuid1
, 4);
905 memcpy(mddev
->uuid
+8, &sb
->set_uuid2
, 4);
906 memcpy(mddev
->uuid
+12,&sb
->set_uuid3
, 4);
908 mddev
->max_disks
= MD_SB_DISKS
;
910 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
) &&
911 mddev
->bitmap_file
== NULL
)
912 mddev
->bitmap_offset
= mddev
->default_bitmap_offset
;
914 } else if (mddev
->pers
== NULL
) {
915 /* Insist on good event counter while assembling */
917 if (ev1
< mddev
->events
)
919 } else if (mddev
->bitmap
) {
920 /* if adding to array with a bitmap, then we can accept an
921 * older device ... but not too old.
923 if (ev1
< mddev
->bitmap
->events_cleared
)
926 if (ev1
< mddev
->events
)
927 /* just a hot-add of a new device, leave raid_disk at -1 */
931 if (mddev
->level
!= LEVEL_MULTIPATH
) {
932 desc
= sb
->disks
+ rdev
->desc_nr
;
934 if (desc
->state
& (1<<MD_DISK_FAULTY
))
935 set_bit(Faulty
, &rdev
->flags
);
936 else if (desc
->state
& (1<<MD_DISK_SYNC
) /* &&
937 desc->raid_disk < mddev->raid_disks */) {
938 set_bit(In_sync
, &rdev
->flags
);
939 rdev
->raid_disk
= desc
->raid_disk
;
941 if (desc
->state
& (1<<MD_DISK_WRITEMOSTLY
))
942 set_bit(WriteMostly
, &rdev
->flags
);
943 } else /* MULTIPATH are always insync */
944 set_bit(In_sync
, &rdev
->flags
);
949 * sync_super for 0.90.0
951 static void super_90_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
955 int next_spare
= mddev
->raid_disks
;
958 /* make rdev->sb match mddev data..
961 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
962 * 3/ any empty disks < next_spare become removed
964 * disks[0] gets initialised to REMOVED because
965 * we cannot be sure from other fields if it has
966 * been initialised or not.
969 int active
=0, working
=0,failed
=0,spare
=0,nr_disks
=0;
971 rdev
->sb_size
= MD_SB_BYTES
;
973 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
975 memset(sb
, 0, sizeof(*sb
));
977 sb
->md_magic
= MD_SB_MAGIC
;
978 sb
->major_version
= mddev
->major_version
;
979 sb
->patch_version
= mddev
->patch_version
;
980 sb
->gvalid_words
= 0; /* ignored */
981 memcpy(&sb
->set_uuid0
, mddev
->uuid
+0, 4);
982 memcpy(&sb
->set_uuid1
, mddev
->uuid
+4, 4);
983 memcpy(&sb
->set_uuid2
, mddev
->uuid
+8, 4);
984 memcpy(&sb
->set_uuid3
, mddev
->uuid
+12,4);
986 sb
->ctime
= mddev
->ctime
;
987 sb
->level
= mddev
->level
;
988 sb
->size
= mddev
->dev_sectors
/ 2;
989 sb
->raid_disks
= mddev
->raid_disks
;
990 sb
->md_minor
= mddev
->md_minor
;
991 sb
->not_persistent
= 0;
992 sb
->utime
= mddev
->utime
;
994 sb
->events_hi
= (mddev
->events
>>32);
995 sb
->events_lo
= (u32
)mddev
->events
;
997 if (mddev
->reshape_position
== MaxSector
)
998 sb
->minor_version
= 90;
1000 sb
->minor_version
= 91;
1001 sb
->reshape_position
= mddev
->reshape_position
;
1002 sb
->new_level
= mddev
->new_level
;
1003 sb
->delta_disks
= mddev
->delta_disks
;
1004 sb
->new_layout
= mddev
->new_layout
;
1005 sb
->new_chunk
= mddev
->new_chunk_sectors
<< 9;
1007 mddev
->minor_version
= sb
->minor_version
;
1010 sb
->recovery_cp
= mddev
->recovery_cp
;
1011 sb
->cp_events_hi
= (mddev
->events
>>32);
1012 sb
->cp_events_lo
= (u32
)mddev
->events
;
1013 if (mddev
->recovery_cp
== MaxSector
)
1014 sb
->state
= (1<< MD_SB_CLEAN
);
1016 sb
->recovery_cp
= 0;
1018 sb
->layout
= mddev
->layout
;
1019 sb
->chunk_size
= mddev
->chunk_sectors
<< 9;
1021 if (mddev
->bitmap
&& mddev
->bitmap_file
== NULL
)
1022 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
1024 sb
->disks
[0].state
= (1<<MD_DISK_REMOVED
);
1025 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
1028 if (rdev2
->raid_disk
>= 0 && test_bit(In_sync
, &rdev2
->flags
)
1029 && !test_bit(Faulty
, &rdev2
->flags
))
1030 desc_nr
= rdev2
->raid_disk
;
1032 desc_nr
= next_spare
++;
1033 rdev2
->desc_nr
= desc_nr
;
1034 d
= &sb
->disks
[rdev2
->desc_nr
];
1036 d
->number
= rdev2
->desc_nr
;
1037 d
->major
= MAJOR(rdev2
->bdev
->bd_dev
);
1038 d
->minor
= MINOR(rdev2
->bdev
->bd_dev
);
1039 if (rdev2
->raid_disk
>= 0 && test_bit(In_sync
, &rdev2
->flags
)
1040 && !test_bit(Faulty
, &rdev2
->flags
))
1041 d
->raid_disk
= rdev2
->raid_disk
;
1043 d
->raid_disk
= rdev2
->desc_nr
; /* compatibility */
1044 if (test_bit(Faulty
, &rdev2
->flags
))
1045 d
->state
= (1<<MD_DISK_FAULTY
);
1046 else if (test_bit(In_sync
, &rdev2
->flags
)) {
1047 d
->state
= (1<<MD_DISK_ACTIVE
);
1048 d
->state
|= (1<<MD_DISK_SYNC
);
1056 if (test_bit(WriteMostly
, &rdev2
->flags
))
1057 d
->state
|= (1<<MD_DISK_WRITEMOSTLY
);
1059 /* now set the "removed" and "faulty" bits on any missing devices */
1060 for (i
=0 ; i
< mddev
->raid_disks
; i
++) {
1061 mdp_disk_t
*d
= &sb
->disks
[i
];
1062 if (d
->state
== 0 && d
->number
== 0) {
1065 d
->state
= (1<<MD_DISK_REMOVED
);
1066 d
->state
|= (1<<MD_DISK_FAULTY
);
1070 sb
->nr_disks
= nr_disks
;
1071 sb
->active_disks
= active
;
1072 sb
->working_disks
= working
;
1073 sb
->failed_disks
= failed
;
1074 sb
->spare_disks
= spare
;
1076 sb
->this_disk
= sb
->disks
[rdev
->desc_nr
];
1077 sb
->sb_csum
= calc_sb_csum(sb
);
1081 * rdev_size_change for 0.90.0
1083 static unsigned long long
1084 super_90_rdev_size_change(mdk_rdev_t
*rdev
, sector_t num_sectors
)
1086 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1087 return 0; /* component must fit device */
1088 if (rdev
->mddev
->bitmap_offset
)
1089 return 0; /* can't move bitmap */
1090 rdev
->sb_start
= calc_dev_sboffset(rdev
->bdev
);
1091 if (!num_sectors
|| num_sectors
> rdev
->sb_start
)
1092 num_sectors
= rdev
->sb_start
;
1093 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1095 md_super_wait(rdev
->mddev
);
1096 return num_sectors
/ 2; /* kB for sysfs */
1101 * version 1 superblock
1104 static __le32
calc_sb_1_csum(struct mdp_superblock_1
* sb
)
1108 unsigned long long newcsum
;
1109 int size
= 256 + le32_to_cpu(sb
->max_dev
)*2;
1110 __le32
*isuper
= (__le32
*)sb
;
1113 disk_csum
= sb
->sb_csum
;
1116 for (i
=0; size
>=4; size
-= 4 )
1117 newcsum
+= le32_to_cpu(*isuper
++);
1120 newcsum
+= le16_to_cpu(*(__le16
*) isuper
);
1122 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
1123 sb
->sb_csum
= disk_csum
;
1124 return cpu_to_le32(csum
);
1127 static int super_1_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
1129 struct mdp_superblock_1
*sb
;
1132 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1136 * Calculate the position of the superblock in 512byte sectors.
1137 * It is always aligned to a 4K boundary and
1138 * depeding on minor_version, it can be:
1139 * 0: At least 8K, but less than 12K, from end of device
1140 * 1: At start of device
1141 * 2: 4K from start of device.
1143 switch(minor_version
) {
1145 sb_start
= rdev
->bdev
->bd_inode
->i_size
>> 9;
1147 sb_start
&= ~(sector_t
)(4*2-1);
1158 rdev
->sb_start
= sb_start
;
1160 /* superblock is rarely larger than 1K, but it can be larger,
1161 * and it is safe to read 4k, so we do that
1163 ret
= read_disk_sb(rdev
, 4096);
1164 if (ret
) return ret
;
1167 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1169 if (sb
->magic
!= cpu_to_le32(MD_SB_MAGIC
) ||
1170 sb
->major_version
!= cpu_to_le32(1) ||
1171 le32_to_cpu(sb
->max_dev
) > (4096-256)/2 ||
1172 le64_to_cpu(sb
->super_offset
) != rdev
->sb_start
||
1173 (le32_to_cpu(sb
->feature_map
) & ~MD_FEATURE_ALL
) != 0)
1176 if (calc_sb_1_csum(sb
) != sb
->sb_csum
) {
1177 printk("md: invalid superblock checksum on %s\n",
1178 bdevname(rdev
->bdev
,b
));
1181 if (le64_to_cpu(sb
->data_size
) < 10) {
1182 printk("md: data_size too small on %s\n",
1183 bdevname(rdev
->bdev
,b
));
1187 rdev
->preferred_minor
= 0xffff;
1188 rdev
->data_offset
= le64_to_cpu(sb
->data_offset
);
1189 atomic_set(&rdev
->corrected_errors
, le32_to_cpu(sb
->cnt_corrected_read
));
1191 rdev
->sb_size
= le32_to_cpu(sb
->max_dev
) * 2 + 256;
1192 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1193 if (rdev
->sb_size
& bmask
)
1194 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1197 && rdev
->data_offset
< sb_start
+ (rdev
->sb_size
/512))
1200 if (sb
->level
== cpu_to_le32(LEVEL_MULTIPATH
))
1203 rdev
->desc_nr
= le32_to_cpu(sb
->dev_number
);
1209 struct mdp_superblock_1
*refsb
=
1210 (struct mdp_superblock_1
*)page_address(refdev
->sb_page
);
1212 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16) != 0 ||
1213 sb
->level
!= refsb
->level
||
1214 sb
->layout
!= refsb
->layout
||
1215 sb
->chunksize
!= refsb
->chunksize
) {
1216 printk(KERN_WARNING
"md: %s has strangely different"
1217 " superblock to %s\n",
1218 bdevname(rdev
->bdev
,b
),
1219 bdevname(refdev
->bdev
,b2
));
1222 ev1
= le64_to_cpu(sb
->events
);
1223 ev2
= le64_to_cpu(refsb
->events
);
1231 rdev
->sectors
= (rdev
->bdev
->bd_inode
->i_size
>> 9) -
1232 le64_to_cpu(sb
->data_offset
);
1234 rdev
->sectors
= rdev
->sb_start
;
1235 if (rdev
->sectors
< le64_to_cpu(sb
->data_size
))
1237 rdev
->sectors
= le64_to_cpu(sb
->data_size
);
1238 if (le64_to_cpu(sb
->size
) > rdev
->sectors
)
1243 static int super_1_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1245 struct mdp_superblock_1
*sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1246 __u64 ev1
= le64_to_cpu(sb
->events
);
1248 rdev
->raid_disk
= -1;
1249 clear_bit(Faulty
, &rdev
->flags
);
1250 clear_bit(In_sync
, &rdev
->flags
);
1251 clear_bit(WriteMostly
, &rdev
->flags
);
1252 clear_bit(BarriersNotsupp
, &rdev
->flags
);
1254 if (mddev
->raid_disks
== 0) {
1255 mddev
->major_version
= 1;
1256 mddev
->patch_version
= 0;
1257 mddev
->external
= 0;
1258 mddev
->chunk_sectors
= le32_to_cpu(sb
->chunksize
);
1259 mddev
->ctime
= le64_to_cpu(sb
->ctime
) & ((1ULL << 32)-1);
1260 mddev
->utime
= le64_to_cpu(sb
->utime
) & ((1ULL << 32)-1);
1261 mddev
->level
= le32_to_cpu(sb
->level
);
1262 mddev
->clevel
[0] = 0;
1263 mddev
->layout
= le32_to_cpu(sb
->layout
);
1264 mddev
->raid_disks
= le32_to_cpu(sb
->raid_disks
);
1265 mddev
->dev_sectors
= le64_to_cpu(sb
->size
);
1266 mddev
->events
= ev1
;
1267 mddev
->bitmap_offset
= 0;
1268 mddev
->default_bitmap_offset
= 1024 >> 9;
1270 mddev
->recovery_cp
= le64_to_cpu(sb
->resync_offset
);
1271 memcpy(mddev
->uuid
, sb
->set_uuid
, 16);
1273 mddev
->max_disks
= (4096-256)/2;
1275 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BITMAP_OFFSET
) &&
1276 mddev
->bitmap_file
== NULL
)
1277 mddev
->bitmap_offset
= (__s32
)le32_to_cpu(sb
->bitmap_offset
);
1279 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
1280 mddev
->reshape_position
= le64_to_cpu(sb
->reshape_position
);
1281 mddev
->delta_disks
= le32_to_cpu(sb
->delta_disks
);
1282 mddev
->new_level
= le32_to_cpu(sb
->new_level
);
1283 mddev
->new_layout
= le32_to_cpu(sb
->new_layout
);
1284 mddev
->new_chunk_sectors
= le32_to_cpu(sb
->new_chunk
);
1286 mddev
->reshape_position
= MaxSector
;
1287 mddev
->delta_disks
= 0;
1288 mddev
->new_level
= mddev
->level
;
1289 mddev
->new_layout
= mddev
->layout
;
1290 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1293 } else if (mddev
->pers
== NULL
) {
1294 /* Insist of good event counter while assembling */
1296 if (ev1
< mddev
->events
)
1298 } else if (mddev
->bitmap
) {
1299 /* If adding to array with a bitmap, then we can accept an
1300 * older device, but not too old.
1302 if (ev1
< mddev
->bitmap
->events_cleared
)
1305 if (ev1
< mddev
->events
)
1306 /* just a hot-add of a new device, leave raid_disk at -1 */
1309 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1311 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
1313 case 0xffff: /* spare */
1315 case 0xfffe: /* faulty */
1316 set_bit(Faulty
, &rdev
->flags
);
1319 if ((le32_to_cpu(sb
->feature_map
) &
1320 MD_FEATURE_RECOVERY_OFFSET
))
1321 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
1323 set_bit(In_sync
, &rdev
->flags
);
1324 rdev
->raid_disk
= role
;
1327 if (sb
->devflags
& WriteMostly1
)
1328 set_bit(WriteMostly
, &rdev
->flags
);
1329 } else /* MULTIPATH are always insync */
1330 set_bit(In_sync
, &rdev
->flags
);
1335 static void super_1_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1337 struct mdp_superblock_1
*sb
;
1340 /* make rdev->sb match mddev and rdev data. */
1342 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1344 sb
->feature_map
= 0;
1346 sb
->recovery_offset
= cpu_to_le64(0);
1347 memset(sb
->pad1
, 0, sizeof(sb
->pad1
));
1348 memset(sb
->pad2
, 0, sizeof(sb
->pad2
));
1349 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
1351 sb
->utime
= cpu_to_le64((__u64
)mddev
->utime
);
1352 sb
->events
= cpu_to_le64(mddev
->events
);
1354 sb
->resync_offset
= cpu_to_le64(mddev
->recovery_cp
);
1356 sb
->resync_offset
= cpu_to_le64(0);
1358 sb
->cnt_corrected_read
= cpu_to_le32(atomic_read(&rdev
->corrected_errors
));
1360 sb
->raid_disks
= cpu_to_le32(mddev
->raid_disks
);
1361 sb
->size
= cpu_to_le64(mddev
->dev_sectors
);
1362 sb
->chunksize
= cpu_to_le32(mddev
->chunk_sectors
);
1363 sb
->level
= cpu_to_le32(mddev
->level
);
1364 sb
->layout
= cpu_to_le32(mddev
->layout
);
1366 if (mddev
->bitmap
&& mddev
->bitmap_file
== NULL
) {
1367 sb
->bitmap_offset
= cpu_to_le32((__u32
)mddev
->bitmap_offset
);
1368 sb
->feature_map
= cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
);
1371 if (rdev
->raid_disk
>= 0 &&
1372 !test_bit(In_sync
, &rdev
->flags
)) {
1373 if (mddev
->curr_resync_completed
> rdev
->recovery_offset
)
1374 rdev
->recovery_offset
= mddev
->curr_resync_completed
;
1375 if (rdev
->recovery_offset
> 0) {
1377 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET
);
1378 sb
->recovery_offset
=
1379 cpu_to_le64(rdev
->recovery_offset
);
1383 if (mddev
->reshape_position
!= MaxSector
) {
1384 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE
);
1385 sb
->reshape_position
= cpu_to_le64(mddev
->reshape_position
);
1386 sb
->new_layout
= cpu_to_le32(mddev
->new_layout
);
1387 sb
->delta_disks
= cpu_to_le32(mddev
->delta_disks
);
1388 sb
->new_level
= cpu_to_le32(mddev
->new_level
);
1389 sb
->new_chunk
= cpu_to_le32(mddev
->new_chunk_sectors
);
1393 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
)
1394 if (rdev2
->desc_nr
+1 > max_dev
)
1395 max_dev
= rdev2
->desc_nr
+1;
1397 if (max_dev
> le32_to_cpu(sb
->max_dev
))
1398 sb
->max_dev
= cpu_to_le32(max_dev
);
1399 for (i
=0; i
<max_dev
;i
++)
1400 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1402 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
1404 if (test_bit(Faulty
, &rdev2
->flags
))
1405 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1406 else if (test_bit(In_sync
, &rdev2
->flags
))
1407 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1408 else if (rdev2
->raid_disk
>= 0 && rdev2
->recovery_offset
> 0)
1409 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1411 sb
->dev_roles
[i
] = cpu_to_le16(0xffff);
1414 sb
->sb_csum
= calc_sb_1_csum(sb
);
1417 static unsigned long long
1418 super_1_rdev_size_change(mdk_rdev_t
*rdev
, sector_t num_sectors
)
1420 struct mdp_superblock_1
*sb
;
1421 sector_t max_sectors
;
1422 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1423 return 0; /* component must fit device */
1424 if (rdev
->sb_start
< rdev
->data_offset
) {
1425 /* minor versions 1 and 2; superblock before data */
1426 max_sectors
= rdev
->bdev
->bd_inode
->i_size
>> 9;
1427 max_sectors
-= rdev
->data_offset
;
1428 if (!num_sectors
|| num_sectors
> max_sectors
)
1429 num_sectors
= max_sectors
;
1430 } else if (rdev
->mddev
->bitmap_offset
) {
1431 /* minor version 0 with bitmap we can't move */
1434 /* minor version 0; superblock after data */
1436 sb_start
= (rdev
->bdev
->bd_inode
->i_size
>> 9) - 8*2;
1437 sb_start
&= ~(sector_t
)(4*2 - 1);
1438 max_sectors
= rdev
->sectors
+ sb_start
- rdev
->sb_start
;
1439 if (!num_sectors
|| num_sectors
> max_sectors
)
1440 num_sectors
= max_sectors
;
1441 rdev
->sb_start
= sb_start
;
1443 sb
= (struct mdp_superblock_1
*) page_address(rdev
->sb_page
);
1444 sb
->data_size
= cpu_to_le64(num_sectors
);
1445 sb
->super_offset
= rdev
->sb_start
;
1446 sb
->sb_csum
= calc_sb_1_csum(sb
);
1447 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1449 md_super_wait(rdev
->mddev
);
1450 return num_sectors
/ 2; /* kB for sysfs */
1453 static struct super_type super_types
[] = {
1456 .owner
= THIS_MODULE
,
1457 .load_super
= super_90_load
,
1458 .validate_super
= super_90_validate
,
1459 .sync_super
= super_90_sync
,
1460 .rdev_size_change
= super_90_rdev_size_change
,
1464 .owner
= THIS_MODULE
,
1465 .load_super
= super_1_load
,
1466 .validate_super
= super_1_validate
,
1467 .sync_super
= super_1_sync
,
1468 .rdev_size_change
= super_1_rdev_size_change
,
1472 static int match_mddev_units(mddev_t
*mddev1
, mddev_t
*mddev2
)
1474 mdk_rdev_t
*rdev
, *rdev2
;
1477 rdev_for_each_rcu(rdev
, mddev1
)
1478 rdev_for_each_rcu(rdev2
, mddev2
)
1479 if (rdev
->bdev
->bd_contains
==
1480 rdev2
->bdev
->bd_contains
) {
1488 static LIST_HEAD(pending_raid_disks
);
1490 static void md_integrity_check(mdk_rdev_t
*rdev
, mddev_t
*mddev
)
1492 struct mdk_personality
*pers
= mddev
->pers
;
1493 struct gendisk
*disk
= mddev
->gendisk
;
1494 struct blk_integrity
*bi_rdev
= bdev_get_integrity(rdev
->bdev
);
1495 struct blk_integrity
*bi_mddev
= blk_get_integrity(disk
);
1497 /* Data integrity passthrough not supported on RAID 4, 5 and 6 */
1498 if (pers
&& pers
->level
>= 4 && pers
->level
<= 6)
1501 /* If rdev is integrity capable, register profile for mddev */
1502 if (!bi_mddev
&& bi_rdev
) {
1503 if (blk_integrity_register(disk
, bi_rdev
))
1504 printk(KERN_ERR
"%s: %s Could not register integrity!\n",
1505 __func__
, disk
->disk_name
);
1507 printk(KERN_NOTICE
"Enabling data integrity on %s\n",
1512 /* Check that mddev and rdev have matching profiles */
1513 if (blk_integrity_compare(disk
, rdev
->bdev
->bd_disk
) < 0) {
1514 printk(KERN_ERR
"%s: %s/%s integrity mismatch!\n", __func__
,
1515 disk
->disk_name
, rdev
->bdev
->bd_disk
->disk_name
);
1516 printk(KERN_NOTICE
"Disabling data integrity on %s\n",
1518 blk_integrity_unregister(disk
);
1522 static int bind_rdev_to_array(mdk_rdev_t
* rdev
, mddev_t
* mddev
)
1524 char b
[BDEVNAME_SIZE
];
1534 /* prevent duplicates */
1535 if (find_rdev(mddev
, rdev
->bdev
->bd_dev
))
1538 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1539 if (rdev
->sectors
&& (mddev
->dev_sectors
== 0 ||
1540 rdev
->sectors
< mddev
->dev_sectors
)) {
1542 /* Cannot change size, so fail
1543 * If mddev->level <= 0, then we don't care
1544 * about aligning sizes (e.g. linear)
1546 if (mddev
->level
> 0)
1549 mddev
->dev_sectors
= rdev
->sectors
;
1552 /* Verify rdev->desc_nr is unique.
1553 * If it is -1, assign a free number, else
1554 * check number is not in use
1556 if (rdev
->desc_nr
< 0) {
1558 if (mddev
->pers
) choice
= mddev
->raid_disks
;
1559 while (find_rdev_nr(mddev
, choice
))
1561 rdev
->desc_nr
= choice
;
1563 if (find_rdev_nr(mddev
, rdev
->desc_nr
))
1566 if (mddev
->max_disks
&& rdev
->desc_nr
>= mddev
->max_disks
) {
1567 printk(KERN_WARNING
"md: %s: array is limited to %d devices\n",
1568 mdname(mddev
), mddev
->max_disks
);
1571 bdevname(rdev
->bdev
,b
);
1572 while ( (s
=strchr(b
, '/')) != NULL
)
1575 rdev
->mddev
= mddev
;
1576 printk(KERN_INFO
"md: bind<%s>\n", b
);
1578 if ((err
= kobject_add(&rdev
->kobj
, &mddev
->kobj
, "dev-%s", b
)))
1581 ko
= &part_to_dev(rdev
->bdev
->bd_part
)->kobj
;
1582 if ((err
= sysfs_create_link(&rdev
->kobj
, ko
, "block"))) {
1583 kobject_del(&rdev
->kobj
);
1586 rdev
->sysfs_state
= sysfs_get_dirent(rdev
->kobj
.sd
, "state");
1588 list_add_rcu(&rdev
->same_set
, &mddev
->disks
);
1589 bd_claim_by_disk(rdev
->bdev
, rdev
->bdev
->bd_holder
, mddev
->gendisk
);
1591 /* May as well allow recovery to be retried once */
1592 mddev
->recovery_disabled
= 0;
1594 md_integrity_check(rdev
, mddev
);
1598 printk(KERN_WARNING
"md: failed to register dev-%s for %s\n",
1603 static void md_delayed_delete(struct work_struct
*ws
)
1605 mdk_rdev_t
*rdev
= container_of(ws
, mdk_rdev_t
, del_work
);
1606 kobject_del(&rdev
->kobj
);
1607 kobject_put(&rdev
->kobj
);
1610 static void unbind_rdev_from_array(mdk_rdev_t
* rdev
)
1612 char b
[BDEVNAME_SIZE
];
1617 bd_release_from_disk(rdev
->bdev
, rdev
->mddev
->gendisk
);
1618 list_del_rcu(&rdev
->same_set
);
1619 printk(KERN_INFO
"md: unbind<%s>\n", bdevname(rdev
->bdev
,b
));
1621 sysfs_remove_link(&rdev
->kobj
, "block");
1622 sysfs_put(rdev
->sysfs_state
);
1623 rdev
->sysfs_state
= NULL
;
1624 /* We need to delay this, otherwise we can deadlock when
1625 * writing to 'remove' to "dev/state". We also need
1626 * to delay it due to rcu usage.
1629 INIT_WORK(&rdev
->del_work
, md_delayed_delete
);
1630 kobject_get(&rdev
->kobj
);
1631 schedule_work(&rdev
->del_work
);
1635 * prevent the device from being mounted, repartitioned or
1636 * otherwise reused by a RAID array (or any other kernel
1637 * subsystem), by bd_claiming the device.
1639 static int lock_rdev(mdk_rdev_t
*rdev
, dev_t dev
, int shared
)
1642 struct block_device
*bdev
;
1643 char b
[BDEVNAME_SIZE
];
1645 bdev
= open_by_devnum(dev
, FMODE_READ
|FMODE_WRITE
);
1647 printk(KERN_ERR
"md: could not open %s.\n",
1648 __bdevname(dev
, b
));
1649 return PTR_ERR(bdev
);
1651 err
= bd_claim(bdev
, shared
? (mdk_rdev_t
*)lock_rdev
: rdev
);
1653 printk(KERN_ERR
"md: could not bd_claim %s.\n",
1655 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
);
1659 set_bit(AllReserved
, &rdev
->flags
);
1664 static void unlock_rdev(mdk_rdev_t
*rdev
)
1666 struct block_device
*bdev
= rdev
->bdev
;
1671 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
);
1674 void md_autodetect_dev(dev_t dev
);
1676 static void export_rdev(mdk_rdev_t
* rdev
)
1678 char b
[BDEVNAME_SIZE
];
1679 printk(KERN_INFO
"md: export_rdev(%s)\n",
1680 bdevname(rdev
->bdev
,b
));
1685 if (test_bit(AutoDetected
, &rdev
->flags
))
1686 md_autodetect_dev(rdev
->bdev
->bd_dev
);
1689 kobject_put(&rdev
->kobj
);
1692 static void kick_rdev_from_array(mdk_rdev_t
* rdev
)
1694 unbind_rdev_from_array(rdev
);
1698 static void export_array(mddev_t
*mddev
)
1700 mdk_rdev_t
*rdev
, *tmp
;
1702 rdev_for_each(rdev
, tmp
, mddev
) {
1707 kick_rdev_from_array(rdev
);
1709 if (!list_empty(&mddev
->disks
))
1711 mddev
->raid_disks
= 0;
1712 mddev
->major_version
= 0;
1715 static void print_desc(mdp_disk_t
*desc
)
1717 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc
->number
,
1718 desc
->major
,desc
->minor
,desc
->raid_disk
,desc
->state
);
1721 static void print_sb_90(mdp_super_t
*sb
)
1726 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1727 sb
->major_version
, sb
->minor_version
, sb
->patch_version
,
1728 sb
->set_uuid0
, sb
->set_uuid1
, sb
->set_uuid2
, sb
->set_uuid3
,
1730 printk(KERN_INFO
"md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1731 sb
->level
, sb
->size
, sb
->nr_disks
, sb
->raid_disks
,
1732 sb
->md_minor
, sb
->layout
, sb
->chunk_size
);
1733 printk(KERN_INFO
"md: UT:%08x ST:%d AD:%d WD:%d"
1734 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1735 sb
->utime
, sb
->state
, sb
->active_disks
, sb
->working_disks
,
1736 sb
->failed_disks
, sb
->spare_disks
,
1737 sb
->sb_csum
, (unsigned long)sb
->events_lo
);
1740 for (i
= 0; i
< MD_SB_DISKS
; i
++) {
1743 desc
= sb
->disks
+ i
;
1744 if (desc
->number
|| desc
->major
|| desc
->minor
||
1745 desc
->raid_disk
|| (desc
->state
&& (desc
->state
!= 4))) {
1746 printk(" D %2d: ", i
);
1750 printk(KERN_INFO
"md: THIS: ");
1751 print_desc(&sb
->this_disk
);
1754 static void print_sb_1(struct mdp_superblock_1
*sb
)
1758 uuid
= sb
->set_uuid
;
1760 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%02x%02x%02x%02x"
1761 ":%02x%02x:%02x%02x:%02x%02x:%02x%02x%02x%02x%02x%02x>\n"
1762 "md: Name: \"%s\" CT:%llu\n",
1763 le32_to_cpu(sb
->major_version
),
1764 le32_to_cpu(sb
->feature_map
),
1765 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
1766 uuid
[4], uuid
[5], uuid
[6], uuid
[7],
1767 uuid
[8], uuid
[9], uuid
[10], uuid
[11],
1768 uuid
[12], uuid
[13], uuid
[14], uuid
[15],
1770 (unsigned long long)le64_to_cpu(sb
->ctime
)
1771 & MD_SUPERBLOCK_1_TIME_SEC_MASK
);
1773 uuid
= sb
->device_uuid
;
1775 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
1777 "md: Dev:%08x UUID: %02x%02x%02x%02x:%02x%02x:%02x%02x:%02x%02x"
1778 ":%02x%02x%02x%02x%02x%02x\n"
1779 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
1780 "md: (MaxDev:%u) \n",
1781 le32_to_cpu(sb
->level
),
1782 (unsigned long long)le64_to_cpu(sb
->size
),
1783 le32_to_cpu(sb
->raid_disks
),
1784 le32_to_cpu(sb
->layout
),
1785 le32_to_cpu(sb
->chunksize
),
1786 (unsigned long long)le64_to_cpu(sb
->data_offset
),
1787 (unsigned long long)le64_to_cpu(sb
->data_size
),
1788 (unsigned long long)le64_to_cpu(sb
->super_offset
),
1789 (unsigned long long)le64_to_cpu(sb
->recovery_offset
),
1790 le32_to_cpu(sb
->dev_number
),
1791 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
1792 uuid
[4], uuid
[5], uuid
[6], uuid
[7],
1793 uuid
[8], uuid
[9], uuid
[10], uuid
[11],
1794 uuid
[12], uuid
[13], uuid
[14], uuid
[15],
1796 (unsigned long long)le64_to_cpu(sb
->utime
) & MD_SUPERBLOCK_1_TIME_SEC_MASK
,
1797 (unsigned long long)le64_to_cpu(sb
->events
),
1798 (unsigned long long)le64_to_cpu(sb
->resync_offset
),
1799 le32_to_cpu(sb
->sb_csum
),
1800 le32_to_cpu(sb
->max_dev
)
1804 static void print_rdev(mdk_rdev_t
*rdev
, int major_version
)
1806 char b
[BDEVNAME_SIZE
];
1807 printk(KERN_INFO
"md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
1808 bdevname(rdev
->bdev
, b
), (unsigned long long)rdev
->sectors
,
1809 test_bit(Faulty
, &rdev
->flags
), test_bit(In_sync
, &rdev
->flags
),
1811 if (rdev
->sb_loaded
) {
1812 printk(KERN_INFO
"md: rdev superblock (MJ:%d):\n", major_version
);
1813 switch (major_version
) {
1815 print_sb_90((mdp_super_t
*)page_address(rdev
->sb_page
));
1818 print_sb_1((struct mdp_superblock_1
*)page_address(rdev
->sb_page
));
1822 printk(KERN_INFO
"md: no rdev superblock!\n");
1825 static void md_print_devices(void)
1827 struct list_head
*tmp
;
1830 char b
[BDEVNAME_SIZE
];
1833 printk("md: **********************************\n");
1834 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1835 printk("md: **********************************\n");
1836 for_each_mddev(mddev
, tmp
) {
1839 bitmap_print_sb(mddev
->bitmap
);
1841 printk("%s: ", mdname(mddev
));
1842 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
1843 printk("<%s>", bdevname(rdev
->bdev
,b
));
1846 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
1847 print_rdev(rdev
, mddev
->major_version
);
1849 printk("md: **********************************\n");
1854 static void sync_sbs(mddev_t
* mddev
, int nospares
)
1856 /* Update each superblock (in-memory image), but
1857 * if we are allowed to, skip spares which already
1858 * have the right event counter, or have one earlier
1859 * (which would mean they aren't being marked as dirty
1860 * with the rest of the array)
1864 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
1865 if (rdev
->sb_events
== mddev
->events
||
1867 rdev
->raid_disk
< 0 &&
1868 (rdev
->sb_events
&1)==0 &&
1869 rdev
->sb_events
+1 == mddev
->events
)) {
1870 /* Don't update this superblock */
1871 rdev
->sb_loaded
= 2;
1873 super_types
[mddev
->major_version
].
1874 sync_super(mddev
, rdev
);
1875 rdev
->sb_loaded
= 1;
1880 static void md_update_sb(mddev_t
* mddev
, int force_change
)
1886 mddev
->utime
= get_seconds();
1887 if (mddev
->external
)
1890 spin_lock_irq(&mddev
->write_lock
);
1892 set_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
1893 if (test_and_clear_bit(MD_CHANGE_DEVS
, &mddev
->flags
))
1895 if (test_and_clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
))
1896 /* just a clean<-> dirty transition, possibly leave spares alone,
1897 * though if events isn't the right even/odd, we will have to do
1903 if (mddev
->degraded
)
1904 /* If the array is degraded, then skipping spares is both
1905 * dangerous and fairly pointless.
1906 * Dangerous because a device that was removed from the array
1907 * might have a event_count that still looks up-to-date,
1908 * so it can be re-added without a resync.
1909 * Pointless because if there are any spares to skip,
1910 * then a recovery will happen and soon that array won't
1911 * be degraded any more and the spare can go back to sleep then.
1915 sync_req
= mddev
->in_sync
;
1917 /* If this is just a dirty<->clean transition, and the array is clean
1918 * and 'events' is odd, we can roll back to the previous clean state */
1920 && (mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
1921 && (mddev
->events
& 1)
1922 && mddev
->events
!= 1)
1925 /* otherwise we have to go forward and ... */
1927 if (!mddev
->in_sync
|| mddev
->recovery_cp
!= MaxSector
) { /* not clean */
1928 /* .. if the array isn't clean, insist on an odd 'events' */
1929 if ((mddev
->events
&1)==0) {
1934 /* otherwise insist on an even 'events' (for clean states) */
1935 if ((mddev
->events
&1)) {
1942 if (!mddev
->events
) {
1944 * oops, this 64-bit counter should never wrap.
1945 * Either we are in around ~1 trillion A.C., assuming
1946 * 1 reboot per second, or we have a bug:
1953 * do not write anything to disk if using
1954 * nonpersistent superblocks
1956 if (!mddev
->persistent
) {
1957 if (!mddev
->external
)
1958 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
1960 spin_unlock_irq(&mddev
->write_lock
);
1961 wake_up(&mddev
->sb_wait
);
1964 sync_sbs(mddev
, nospares
);
1965 spin_unlock_irq(&mddev
->write_lock
);
1968 "md: updating %s RAID superblock on device (in sync %d)\n",
1969 mdname(mddev
),mddev
->in_sync
);
1971 bitmap_update_sb(mddev
->bitmap
);
1972 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
1973 char b
[BDEVNAME_SIZE
];
1974 dprintk(KERN_INFO
"md: ");
1975 if (rdev
->sb_loaded
!= 1)
1976 continue; /* no noise on spare devices */
1977 if (test_bit(Faulty
, &rdev
->flags
))
1978 dprintk("(skipping faulty ");
1980 dprintk("%s ", bdevname(rdev
->bdev
,b
));
1981 if (!test_bit(Faulty
, &rdev
->flags
)) {
1982 md_super_write(mddev
,rdev
,
1983 rdev
->sb_start
, rdev
->sb_size
,
1985 dprintk(KERN_INFO
"(write) %s's sb offset: %llu\n",
1986 bdevname(rdev
->bdev
,b
),
1987 (unsigned long long)rdev
->sb_start
);
1988 rdev
->sb_events
= mddev
->events
;
1992 if (mddev
->level
== LEVEL_MULTIPATH
)
1993 /* only need to write one superblock... */
1996 md_super_wait(mddev
);
1997 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
1999 spin_lock_irq(&mddev
->write_lock
);
2000 if (mddev
->in_sync
!= sync_req
||
2001 test_bit(MD_CHANGE_DEVS
, &mddev
->flags
)) {
2002 /* have to write it out again */
2003 spin_unlock_irq(&mddev
->write_lock
);
2006 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
2007 spin_unlock_irq(&mddev
->write_lock
);
2008 wake_up(&mddev
->sb_wait
);
2009 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
2010 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
2014 /* words written to sysfs files may, or may not, be \n terminated.
2015 * We want to accept with case. For this we use cmd_match.
2017 static int cmd_match(const char *cmd
, const char *str
)
2019 /* See if cmd, written into a sysfs file, matches
2020 * str. They must either be the same, or cmd can
2021 * have a trailing newline
2023 while (*cmd
&& *str
&& *cmd
== *str
) {
2034 struct rdev_sysfs_entry
{
2035 struct attribute attr
;
2036 ssize_t (*show
)(mdk_rdev_t
*, char *);
2037 ssize_t (*store
)(mdk_rdev_t
*, const char *, size_t);
2041 state_show(mdk_rdev_t
*rdev
, char *page
)
2046 if (test_bit(Faulty
, &rdev
->flags
)) {
2047 len
+= sprintf(page
+len
, "%sfaulty",sep
);
2050 if (test_bit(In_sync
, &rdev
->flags
)) {
2051 len
+= sprintf(page
+len
, "%sin_sync",sep
);
2054 if (test_bit(WriteMostly
, &rdev
->flags
)) {
2055 len
+= sprintf(page
+len
, "%swrite_mostly",sep
);
2058 if (test_bit(Blocked
, &rdev
->flags
)) {
2059 len
+= sprintf(page
+len
, "%sblocked", sep
);
2062 if (!test_bit(Faulty
, &rdev
->flags
) &&
2063 !test_bit(In_sync
, &rdev
->flags
)) {
2064 len
+= sprintf(page
+len
, "%sspare", sep
);
2067 return len
+sprintf(page
+len
, "\n");
2071 state_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2074 * faulty - simulates and error
2075 * remove - disconnects the device
2076 * writemostly - sets write_mostly
2077 * -writemostly - clears write_mostly
2078 * blocked - sets the Blocked flag
2079 * -blocked - clears the Blocked flag
2080 * insync - sets Insync providing device isn't active
2083 if (cmd_match(buf
, "faulty") && rdev
->mddev
->pers
) {
2084 md_error(rdev
->mddev
, rdev
);
2086 } else if (cmd_match(buf
, "remove")) {
2087 if (rdev
->raid_disk
>= 0)
2090 mddev_t
*mddev
= rdev
->mddev
;
2091 kick_rdev_from_array(rdev
);
2093 md_update_sb(mddev
, 1);
2094 md_new_event(mddev
);
2097 } else if (cmd_match(buf
, "writemostly")) {
2098 set_bit(WriteMostly
, &rdev
->flags
);
2100 } else if (cmd_match(buf
, "-writemostly")) {
2101 clear_bit(WriteMostly
, &rdev
->flags
);
2103 } else if (cmd_match(buf
, "blocked")) {
2104 set_bit(Blocked
, &rdev
->flags
);
2106 } else if (cmd_match(buf
, "-blocked")) {
2107 clear_bit(Blocked
, &rdev
->flags
);
2108 wake_up(&rdev
->blocked_wait
);
2109 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2110 md_wakeup_thread(rdev
->mddev
->thread
);
2113 } else if (cmd_match(buf
, "insync") && rdev
->raid_disk
== -1) {
2114 set_bit(In_sync
, &rdev
->flags
);
2117 if (!err
&& rdev
->sysfs_state
)
2118 sysfs_notify_dirent(rdev
->sysfs_state
);
2119 return err
? err
: len
;
2121 static struct rdev_sysfs_entry rdev_state
=
2122 __ATTR(state
, S_IRUGO
|S_IWUSR
, state_show
, state_store
);
2125 errors_show(mdk_rdev_t
*rdev
, char *page
)
2127 return sprintf(page
, "%d\n", atomic_read(&rdev
->corrected_errors
));
2131 errors_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2134 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2135 if (*buf
&& (*e
== 0 || *e
== '\n')) {
2136 atomic_set(&rdev
->corrected_errors
, n
);
2141 static struct rdev_sysfs_entry rdev_errors
=
2142 __ATTR(errors
, S_IRUGO
|S_IWUSR
, errors_show
, errors_store
);
2145 slot_show(mdk_rdev_t
*rdev
, char *page
)
2147 if (rdev
->raid_disk
< 0)
2148 return sprintf(page
, "none\n");
2150 return sprintf(page
, "%d\n", rdev
->raid_disk
);
2154 slot_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2159 int slot
= simple_strtoul(buf
, &e
, 10);
2160 if (strncmp(buf
, "none", 4)==0)
2162 else if (e
==buf
|| (*e
&& *e
!= '\n'))
2164 if (rdev
->mddev
->pers
&& slot
== -1) {
2165 /* Setting 'slot' on an active array requires also
2166 * updating the 'rd%d' link, and communicating
2167 * with the personality with ->hot_*_disk.
2168 * For now we only support removing
2169 * failed/spare devices. This normally happens automatically,
2170 * but not when the metadata is externally managed.
2172 if (rdev
->raid_disk
== -1)
2174 /* personality does all needed checks */
2175 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
2177 err
= rdev
->mddev
->pers
->
2178 hot_remove_disk(rdev
->mddev
, rdev
->raid_disk
);
2181 sprintf(nm
, "rd%d", rdev
->raid_disk
);
2182 sysfs_remove_link(&rdev
->mddev
->kobj
, nm
);
2183 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2184 md_wakeup_thread(rdev
->mddev
->thread
);
2185 } else if (rdev
->mddev
->pers
) {
2187 /* Activating a spare .. or possibly reactivating
2188 * if we ever get bitmaps working here.
2191 if (rdev
->raid_disk
!= -1)
2194 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
2197 list_for_each_entry(rdev2
, &rdev
->mddev
->disks
, same_set
)
2198 if (rdev2
->raid_disk
== slot
)
2201 rdev
->raid_disk
= slot
;
2202 if (test_bit(In_sync
, &rdev
->flags
))
2203 rdev
->saved_raid_disk
= slot
;
2205 rdev
->saved_raid_disk
= -1;
2206 err
= rdev
->mddev
->pers
->
2207 hot_add_disk(rdev
->mddev
, rdev
);
2209 rdev
->raid_disk
= -1;
2212 sysfs_notify_dirent(rdev
->sysfs_state
);
2213 sprintf(nm
, "rd%d", rdev
->raid_disk
);
2214 if (sysfs_create_link(&rdev
->mddev
->kobj
, &rdev
->kobj
, nm
))
2216 "md: cannot register "
2218 nm
, mdname(rdev
->mddev
));
2220 /* don't wakeup anyone, leave that to userspace. */
2222 if (slot
>= rdev
->mddev
->raid_disks
)
2224 rdev
->raid_disk
= slot
;
2225 /* assume it is working */
2226 clear_bit(Faulty
, &rdev
->flags
);
2227 clear_bit(WriteMostly
, &rdev
->flags
);
2228 set_bit(In_sync
, &rdev
->flags
);
2229 sysfs_notify_dirent(rdev
->sysfs_state
);
2235 static struct rdev_sysfs_entry rdev_slot
=
2236 __ATTR(slot
, S_IRUGO
|S_IWUSR
, slot_show
, slot_store
);
2239 offset_show(mdk_rdev_t
*rdev
, char *page
)
2241 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->data_offset
);
2245 offset_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2248 unsigned long long offset
= simple_strtoull(buf
, &e
, 10);
2249 if (e
==buf
|| (*e
&& *e
!= '\n'))
2251 if (rdev
->mddev
->pers
&& rdev
->raid_disk
>= 0)
2253 if (rdev
->sectors
&& rdev
->mddev
->external
)
2254 /* Must set offset before size, so overlap checks
2257 rdev
->data_offset
= offset
;
2261 static struct rdev_sysfs_entry rdev_offset
=
2262 __ATTR(offset
, S_IRUGO
|S_IWUSR
, offset_show
, offset_store
);
2265 rdev_size_show(mdk_rdev_t
*rdev
, char *page
)
2267 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->sectors
/ 2);
2270 static int overlaps(sector_t s1
, sector_t l1
, sector_t s2
, sector_t l2
)
2272 /* check if two start/length pairs overlap */
2280 static int strict_blocks_to_sectors(const char *buf
, sector_t
*sectors
)
2282 unsigned long long blocks
;
2285 if (strict_strtoull(buf
, 10, &blocks
) < 0)
2288 if (blocks
& 1ULL << (8 * sizeof(blocks
) - 1))
2289 return -EINVAL
; /* sector conversion overflow */
2292 if (new != blocks
* 2)
2293 return -EINVAL
; /* unsigned long long to sector_t overflow */
2300 rdev_size_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2302 mddev_t
*my_mddev
= rdev
->mddev
;
2303 sector_t oldsectors
= rdev
->sectors
;
2306 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
2308 if (my_mddev
->pers
&& rdev
->raid_disk
>= 0) {
2309 if (my_mddev
->persistent
) {
2310 sectors
= super_types
[my_mddev
->major_version
].
2311 rdev_size_change(rdev
, sectors
);
2314 } else if (!sectors
)
2315 sectors
= (rdev
->bdev
->bd_inode
->i_size
>> 9) -
2318 if (sectors
< my_mddev
->dev_sectors
)
2319 return -EINVAL
; /* component must fit device */
2321 rdev
->sectors
= sectors
;
2322 if (sectors
> oldsectors
&& my_mddev
->external
) {
2323 /* need to check that all other rdevs with the same ->bdev
2324 * do not overlap. We need to unlock the mddev to avoid
2325 * a deadlock. We have already changed rdev->sectors, and if
2326 * we have to change it back, we will have the lock again.
2330 struct list_head
*tmp
;
2332 mddev_unlock(my_mddev
);
2333 for_each_mddev(mddev
, tmp
) {
2337 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
)
2338 if (test_bit(AllReserved
, &rdev2
->flags
) ||
2339 (rdev
->bdev
== rdev2
->bdev
&&
2341 overlaps(rdev
->data_offset
, rdev
->sectors
,
2347 mddev_unlock(mddev
);
2353 mddev_lock(my_mddev
);
2355 /* Someone else could have slipped in a size
2356 * change here, but doing so is just silly.
2357 * We put oldsectors back because we *know* it is
2358 * safe, and trust userspace not to race with
2361 rdev
->sectors
= oldsectors
;
2368 static struct rdev_sysfs_entry rdev_size
=
2369 __ATTR(size
, S_IRUGO
|S_IWUSR
, rdev_size_show
, rdev_size_store
);
2371 static struct attribute
*rdev_default_attrs
[] = {
2380 rdev_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
2382 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
2383 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
2384 mddev_t
*mddev
= rdev
->mddev
;
2390 rv
= mddev
? mddev_lock(mddev
) : -EBUSY
;
2392 if (rdev
->mddev
== NULL
)
2395 rv
= entry
->show(rdev
, page
);
2396 mddev_unlock(mddev
);
2402 rdev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
2403 const char *page
, size_t length
)
2405 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
2406 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
2408 mddev_t
*mddev
= rdev
->mddev
;
2412 if (!capable(CAP_SYS_ADMIN
))
2414 rv
= mddev
? mddev_lock(mddev
): -EBUSY
;
2416 if (rdev
->mddev
== NULL
)
2419 rv
= entry
->store(rdev
, page
, length
);
2420 mddev_unlock(mddev
);
2425 static void rdev_free(struct kobject
*ko
)
2427 mdk_rdev_t
*rdev
= container_of(ko
, mdk_rdev_t
, kobj
);
2430 static struct sysfs_ops rdev_sysfs_ops
= {
2431 .show
= rdev_attr_show
,
2432 .store
= rdev_attr_store
,
2434 static struct kobj_type rdev_ktype
= {
2435 .release
= rdev_free
,
2436 .sysfs_ops
= &rdev_sysfs_ops
,
2437 .default_attrs
= rdev_default_attrs
,
2441 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2443 * mark the device faulty if:
2445 * - the device is nonexistent (zero size)
2446 * - the device has no valid superblock
2448 * a faulty rdev _never_ has rdev->sb set.
2450 static mdk_rdev_t
*md_import_device(dev_t newdev
, int super_format
, int super_minor
)
2452 char b
[BDEVNAME_SIZE
];
2457 rdev
= kzalloc(sizeof(*rdev
), GFP_KERNEL
);
2459 printk(KERN_ERR
"md: could not alloc mem for new device!\n");
2460 return ERR_PTR(-ENOMEM
);
2463 if ((err
= alloc_disk_sb(rdev
)))
2466 err
= lock_rdev(rdev
, newdev
, super_format
== -2);
2470 kobject_init(&rdev
->kobj
, &rdev_ktype
);
2473 rdev
->saved_raid_disk
= -1;
2474 rdev
->raid_disk
= -1;
2476 rdev
->data_offset
= 0;
2477 rdev
->sb_events
= 0;
2478 atomic_set(&rdev
->nr_pending
, 0);
2479 atomic_set(&rdev
->read_errors
, 0);
2480 atomic_set(&rdev
->corrected_errors
, 0);
2482 size
= rdev
->bdev
->bd_inode
->i_size
>> BLOCK_SIZE_BITS
;
2485 "md: %s has zero or unknown size, marking faulty!\n",
2486 bdevname(rdev
->bdev
,b
));
2491 if (super_format
>= 0) {
2492 err
= super_types
[super_format
].
2493 load_super(rdev
, NULL
, super_minor
);
2494 if (err
== -EINVAL
) {
2496 "md: %s does not have a valid v%d.%d "
2497 "superblock, not importing!\n",
2498 bdevname(rdev
->bdev
,b
),
2499 super_format
, super_minor
);
2504 "md: could not read %s's sb, not importing!\n",
2505 bdevname(rdev
->bdev
,b
));
2510 INIT_LIST_HEAD(&rdev
->same_set
);
2511 init_waitqueue_head(&rdev
->blocked_wait
);
2516 if (rdev
->sb_page
) {
2522 return ERR_PTR(err
);
2526 * Check a full RAID array for plausibility
2530 static void analyze_sbs(mddev_t
* mddev
)
2533 mdk_rdev_t
*rdev
, *freshest
, *tmp
;
2534 char b
[BDEVNAME_SIZE
];
2537 rdev_for_each(rdev
, tmp
, mddev
)
2538 switch (super_types
[mddev
->major_version
].
2539 load_super(rdev
, freshest
, mddev
->minor_version
)) {
2547 "md: fatal superblock inconsistency in %s"
2548 " -- removing from array\n",
2549 bdevname(rdev
->bdev
,b
));
2550 kick_rdev_from_array(rdev
);
2554 super_types
[mddev
->major_version
].
2555 validate_super(mddev
, freshest
);
2558 rdev_for_each(rdev
, tmp
, mddev
) {
2559 if (rdev
->desc_nr
>= mddev
->max_disks
||
2560 i
> mddev
->max_disks
) {
2562 "md: %s: %s: only %d devices permitted\n",
2563 mdname(mddev
), bdevname(rdev
->bdev
, b
),
2565 kick_rdev_from_array(rdev
);
2568 if (rdev
!= freshest
)
2569 if (super_types
[mddev
->major_version
].
2570 validate_super(mddev
, rdev
)) {
2571 printk(KERN_WARNING
"md: kicking non-fresh %s"
2573 bdevname(rdev
->bdev
,b
));
2574 kick_rdev_from_array(rdev
);
2577 if (mddev
->level
== LEVEL_MULTIPATH
) {
2578 rdev
->desc_nr
= i
++;
2579 rdev
->raid_disk
= rdev
->desc_nr
;
2580 set_bit(In_sync
, &rdev
->flags
);
2581 } else if (rdev
->raid_disk
>= mddev
->raid_disks
) {
2582 rdev
->raid_disk
= -1;
2583 clear_bit(In_sync
, &rdev
->flags
);
2588 static void md_safemode_timeout(unsigned long data
);
2591 safe_delay_show(mddev_t
*mddev
, char *page
)
2593 int msec
= (mddev
->safemode_delay
*1000)/HZ
;
2594 return sprintf(page
, "%d.%03d\n", msec
/1000, msec
%1000);
2597 safe_delay_store(mddev_t
*mddev
, const char *cbuf
, size_t len
)
2605 /* remove a period, and count digits after it */
2606 if (len
>= sizeof(buf
))
2608 strlcpy(buf
, cbuf
, sizeof(buf
));
2609 for (i
=0; i
<len
; i
++) {
2611 if (isdigit(buf
[i
])) {
2616 } else if (buf
[i
] == '.') {
2621 if (strict_strtoul(buf
, 10, &msec
) < 0)
2623 msec
= (msec
* 1000) / scale
;
2625 mddev
->safemode_delay
= 0;
2627 unsigned long old_delay
= mddev
->safemode_delay
;
2628 mddev
->safemode_delay
= (msec
*HZ
)/1000;
2629 if (mddev
->safemode_delay
== 0)
2630 mddev
->safemode_delay
= 1;
2631 if (mddev
->safemode_delay
< old_delay
)
2632 md_safemode_timeout((unsigned long)mddev
);
2636 static struct md_sysfs_entry md_safe_delay
=
2637 __ATTR(safe_mode_delay
, S_IRUGO
|S_IWUSR
,safe_delay_show
, safe_delay_store
);
2640 level_show(mddev_t
*mddev
, char *page
)
2642 struct mdk_personality
*p
= mddev
->pers
;
2644 return sprintf(page
, "%s\n", p
->name
);
2645 else if (mddev
->clevel
[0])
2646 return sprintf(page
, "%s\n", mddev
->clevel
);
2647 else if (mddev
->level
!= LEVEL_NONE
)
2648 return sprintf(page
, "%d\n", mddev
->level
);
2654 level_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2658 struct mdk_personality
*pers
;
2661 if (mddev
->pers
== NULL
) {
2664 if (len
>= sizeof(mddev
->clevel
))
2666 strncpy(mddev
->clevel
, buf
, len
);
2667 if (mddev
->clevel
[len
-1] == '\n')
2669 mddev
->clevel
[len
] = 0;
2670 mddev
->level
= LEVEL_NONE
;
2674 /* request to change the personality. Need to ensure:
2675 * - array is not engaged in resync/recovery/reshape
2676 * - old personality can be suspended
2677 * - new personality will access other array.
2680 if (mddev
->sync_thread
|| mddev
->reshape_position
!= MaxSector
)
2683 if (!mddev
->pers
->quiesce
) {
2684 printk(KERN_WARNING
"md: %s: %s does not support online personality change\n",
2685 mdname(mddev
), mddev
->pers
->name
);
2689 /* Now find the new personality */
2690 if (len
== 0 || len
>= sizeof(level
))
2692 strncpy(level
, buf
, len
);
2693 if (level
[len
-1] == '\n')
2697 request_module("md-%s", level
);
2698 spin_lock(&pers_lock
);
2699 pers
= find_pers(LEVEL_NONE
, level
);
2700 if (!pers
|| !try_module_get(pers
->owner
)) {
2701 spin_unlock(&pers_lock
);
2702 printk(KERN_WARNING
"md: personality %s not loaded\n", level
);
2705 spin_unlock(&pers_lock
);
2707 if (pers
== mddev
->pers
) {
2708 /* Nothing to do! */
2709 module_put(pers
->owner
);
2712 if (!pers
->takeover
) {
2713 module_put(pers
->owner
);
2714 printk(KERN_WARNING
"md: %s: %s does not support personality takeover\n",
2715 mdname(mddev
), level
);
2719 /* ->takeover must set new_* and/or delta_disks
2720 * if it succeeds, and may set them when it fails.
2722 priv
= pers
->takeover(mddev
);
2724 mddev
->new_level
= mddev
->level
;
2725 mddev
->new_layout
= mddev
->layout
;
2726 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
2727 mddev
->raid_disks
-= mddev
->delta_disks
;
2728 mddev
->delta_disks
= 0;
2729 module_put(pers
->owner
);
2730 printk(KERN_WARNING
"md: %s: %s would not accept array\n",
2731 mdname(mddev
), level
);
2732 return PTR_ERR(priv
);
2735 /* Looks like we have a winner */
2736 mddev_suspend(mddev
);
2737 mddev
->pers
->stop(mddev
);
2738 module_put(mddev
->pers
->owner
);
2740 mddev
->private = priv
;
2741 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
2742 mddev
->level
= mddev
->new_level
;
2743 mddev
->layout
= mddev
->new_layout
;
2744 mddev
->chunk_sectors
= mddev
->new_chunk_sectors
;
2745 mddev
->delta_disks
= 0;
2747 mddev_resume(mddev
);
2748 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
2749 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
2750 md_wakeup_thread(mddev
->thread
);
2754 static struct md_sysfs_entry md_level
=
2755 __ATTR(level
, S_IRUGO
|S_IWUSR
, level_show
, level_store
);
2759 layout_show(mddev_t
*mddev
, char *page
)
2761 /* just a number, not meaningful for all levels */
2762 if (mddev
->reshape_position
!= MaxSector
&&
2763 mddev
->layout
!= mddev
->new_layout
)
2764 return sprintf(page
, "%d (%d)\n",
2765 mddev
->new_layout
, mddev
->layout
);
2766 return sprintf(page
, "%d\n", mddev
->layout
);
2770 layout_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2773 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2775 if (!*buf
|| (*e
&& *e
!= '\n'))
2780 if (mddev
->pers
->check_reshape
== NULL
)
2782 mddev
->new_layout
= n
;
2783 err
= mddev
->pers
->check_reshape(mddev
);
2785 mddev
->new_layout
= mddev
->layout
;
2789 mddev
->new_layout
= n
;
2790 if (mddev
->reshape_position
== MaxSector
)
2795 static struct md_sysfs_entry md_layout
=
2796 __ATTR(layout
, S_IRUGO
|S_IWUSR
, layout_show
, layout_store
);
2800 raid_disks_show(mddev_t
*mddev
, char *page
)
2802 if (mddev
->raid_disks
== 0)
2804 if (mddev
->reshape_position
!= MaxSector
&&
2805 mddev
->delta_disks
!= 0)
2806 return sprintf(page
, "%d (%d)\n", mddev
->raid_disks
,
2807 mddev
->raid_disks
- mddev
->delta_disks
);
2808 return sprintf(page
, "%d\n", mddev
->raid_disks
);
2811 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
);
2814 raid_disks_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2818 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2820 if (!*buf
|| (*e
&& *e
!= '\n'))
2824 rv
= update_raid_disks(mddev
, n
);
2825 else if (mddev
->reshape_position
!= MaxSector
) {
2826 int olddisks
= mddev
->raid_disks
- mddev
->delta_disks
;
2827 mddev
->delta_disks
= n
- olddisks
;
2828 mddev
->raid_disks
= n
;
2830 mddev
->raid_disks
= n
;
2831 return rv
? rv
: len
;
2833 static struct md_sysfs_entry md_raid_disks
=
2834 __ATTR(raid_disks
, S_IRUGO
|S_IWUSR
, raid_disks_show
, raid_disks_store
);
2837 chunk_size_show(mddev_t
*mddev
, char *page
)
2839 if (mddev
->reshape_position
!= MaxSector
&&
2840 mddev
->chunk_sectors
!= mddev
->new_chunk_sectors
)
2841 return sprintf(page
, "%d (%d)\n",
2842 mddev
->new_chunk_sectors
<< 9,
2843 mddev
->chunk_sectors
<< 9);
2844 return sprintf(page
, "%d\n", mddev
->chunk_sectors
<< 9);
2848 chunk_size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2851 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2853 if (!*buf
|| (*e
&& *e
!= '\n'))
2858 if (mddev
->pers
->check_reshape
== NULL
)
2860 mddev
->new_chunk_sectors
= n
>> 9;
2861 err
= mddev
->pers
->check_reshape(mddev
);
2863 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
2867 mddev
->new_chunk_sectors
= n
>> 9;
2868 if (mddev
->reshape_position
== MaxSector
)
2869 mddev
->chunk_sectors
= n
>> 9;
2873 static struct md_sysfs_entry md_chunk_size
=
2874 __ATTR(chunk_size
, S_IRUGO
|S_IWUSR
, chunk_size_show
, chunk_size_store
);
2877 resync_start_show(mddev_t
*mddev
, char *page
)
2879 if (mddev
->recovery_cp
== MaxSector
)
2880 return sprintf(page
, "none\n");
2881 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->recovery_cp
);
2885 resync_start_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2888 unsigned long long n
= simple_strtoull(buf
, &e
, 10);
2892 if (!*buf
|| (*e
&& *e
!= '\n'))
2895 mddev
->recovery_cp
= n
;
2898 static struct md_sysfs_entry md_resync_start
=
2899 __ATTR(resync_start
, S_IRUGO
|S_IWUSR
, resync_start_show
, resync_start_store
);
2902 * The array state can be:
2905 * No devices, no size, no level
2906 * Equivalent to STOP_ARRAY ioctl
2908 * May have some settings, but array is not active
2909 * all IO results in error
2910 * When written, doesn't tear down array, but just stops it
2911 * suspended (not supported yet)
2912 * All IO requests will block. The array can be reconfigured.
2913 * Writing this, if accepted, will block until array is quiescent
2915 * no resync can happen. no superblocks get written.
2916 * write requests fail
2918 * like readonly, but behaves like 'clean' on a write request.
2920 * clean - no pending writes, but otherwise active.
2921 * When written to inactive array, starts without resync
2922 * If a write request arrives then
2923 * if metadata is known, mark 'dirty' and switch to 'active'.
2924 * if not known, block and switch to write-pending
2925 * If written to an active array that has pending writes, then fails.
2927 * fully active: IO and resync can be happening.
2928 * When written to inactive array, starts with resync
2931 * clean, but writes are blocked waiting for 'active' to be written.
2934 * like active, but no writes have been seen for a while (100msec).
2937 enum array_state
{ clear
, inactive
, suspended
, readonly
, read_auto
, clean
, active
,
2938 write_pending
, active_idle
, bad_word
};
2939 static char *array_states
[] = {
2940 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2941 "write-pending", "active-idle", NULL
};
2943 static int match_word(const char *word
, char **list
)
2946 for (n
=0; list
[n
]; n
++)
2947 if (cmd_match(word
, list
[n
]))
2953 array_state_show(mddev_t
*mddev
, char *page
)
2955 enum array_state st
= inactive
;
2968 else if (test_bit(MD_CHANGE_CLEAN
, &mddev
->flags
))
2970 else if (mddev
->safemode
)
2976 if (list_empty(&mddev
->disks
) &&
2977 mddev
->raid_disks
== 0 &&
2978 mddev
->dev_sectors
== 0)
2983 return sprintf(page
, "%s\n", array_states
[st
]);
2986 static int do_md_stop(mddev_t
* mddev
, int ro
, int is_open
);
2987 static int do_md_run(mddev_t
* mddev
);
2988 static int restart_array(mddev_t
*mddev
);
2991 array_state_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2994 enum array_state st
= match_word(buf
, array_states
);
2999 /* stopping an active array */
3000 if (atomic_read(&mddev
->openers
) > 0)
3002 err
= do_md_stop(mddev
, 0, 0);
3005 /* stopping an active array */
3007 if (atomic_read(&mddev
->openers
) > 0)
3009 err
= do_md_stop(mddev
, 2, 0);
3011 err
= 0; /* already inactive */
3014 break; /* not supported yet */
3017 err
= do_md_stop(mddev
, 1, 0);
3020 set_disk_ro(mddev
->gendisk
, 1);
3021 err
= do_md_run(mddev
);
3027 err
= do_md_stop(mddev
, 1, 0);
3028 else if (mddev
->ro
== 1)
3029 err
= restart_array(mddev
);
3032 set_disk_ro(mddev
->gendisk
, 0);
3036 err
= do_md_run(mddev
);
3041 restart_array(mddev
);
3042 spin_lock_irq(&mddev
->write_lock
);
3043 if (atomic_read(&mddev
->writes_pending
) == 0) {
3044 if (mddev
->in_sync
== 0) {
3046 if (mddev
->safemode
== 1)
3047 mddev
->safemode
= 0;
3048 if (mddev
->persistent
)
3049 set_bit(MD_CHANGE_CLEAN
,
3055 spin_unlock_irq(&mddev
->write_lock
);
3061 restart_array(mddev
);
3062 if (mddev
->external
)
3063 clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
3064 wake_up(&mddev
->sb_wait
);
3068 set_disk_ro(mddev
->gendisk
, 0);
3069 err
= do_md_run(mddev
);
3074 /* these cannot be set */
3080 sysfs_notify_dirent(mddev
->sysfs_state
);
3084 static struct md_sysfs_entry md_array_state
=
3085 __ATTR(array_state
, S_IRUGO
|S_IWUSR
, array_state_show
, array_state_store
);
3088 null_show(mddev_t
*mddev
, char *page
)
3094 new_dev_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3096 /* buf must be %d:%d\n? giving major and minor numbers */
3097 /* The new device is added to the array.
3098 * If the array has a persistent superblock, we read the
3099 * superblock to initialise info and check validity.
3100 * Otherwise, only checking done is that in bind_rdev_to_array,
3101 * which mainly checks size.
3104 int major
= simple_strtoul(buf
, &e
, 10);
3110 if (!*buf
|| *e
!= ':' || !e
[1] || e
[1] == '\n')
3112 minor
= simple_strtoul(e
+1, &e
, 10);
3113 if (*e
&& *e
!= '\n')
3115 dev
= MKDEV(major
, minor
);
3116 if (major
!= MAJOR(dev
) ||
3117 minor
!= MINOR(dev
))
3121 if (mddev
->persistent
) {
3122 rdev
= md_import_device(dev
, mddev
->major_version
,
3123 mddev
->minor_version
);
3124 if (!IS_ERR(rdev
) && !list_empty(&mddev
->disks
)) {
3125 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
3126 mdk_rdev_t
, same_set
);
3127 err
= super_types
[mddev
->major_version
]
3128 .load_super(rdev
, rdev0
, mddev
->minor_version
);
3132 } else if (mddev
->external
)
3133 rdev
= md_import_device(dev
, -2, -1);
3135 rdev
= md_import_device(dev
, -1, -1);
3138 return PTR_ERR(rdev
);
3139 err
= bind_rdev_to_array(rdev
, mddev
);
3143 return err
? err
: len
;
3146 static struct md_sysfs_entry md_new_device
=
3147 __ATTR(new_dev
, S_IWUSR
, null_show
, new_dev_store
);
3150 bitmap_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3153 unsigned long chunk
, end_chunk
;
3157 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3159 chunk
= end_chunk
= simple_strtoul(buf
, &end
, 0);
3160 if (buf
== end
) break;
3161 if (*end
== '-') { /* range */
3163 end_chunk
= simple_strtoul(buf
, &end
, 0);
3164 if (buf
== end
) break;
3166 if (*end
&& !isspace(*end
)) break;
3167 bitmap_dirty_bits(mddev
->bitmap
, chunk
, end_chunk
);
3169 while (isspace(*buf
)) buf
++;
3171 bitmap_unplug(mddev
->bitmap
); /* flush the bits to disk */
3176 static struct md_sysfs_entry md_bitmap
=
3177 __ATTR(bitmap_set_bits
, S_IWUSR
, null_show
, bitmap_store
);
3180 size_show(mddev_t
*mddev
, char *page
)
3182 return sprintf(page
, "%llu\n",
3183 (unsigned long long)mddev
->dev_sectors
/ 2);
3186 static int update_size(mddev_t
*mddev
, sector_t num_sectors
);
3189 size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3191 /* If array is inactive, we can reduce the component size, but
3192 * not increase it (except from 0).
3193 * If array is active, we can try an on-line resize
3196 int err
= strict_blocks_to_sectors(buf
, §ors
);
3201 err
= update_size(mddev
, sectors
);
3202 md_update_sb(mddev
, 1);
3204 if (mddev
->dev_sectors
== 0 ||
3205 mddev
->dev_sectors
> sectors
)
3206 mddev
->dev_sectors
= sectors
;
3210 return err
? err
: len
;
3213 static struct md_sysfs_entry md_size
=
3214 __ATTR(component_size
, S_IRUGO
|S_IWUSR
, size_show
, size_store
);
3219 * 'none' for arrays with no metadata (good luck...)
3220 * 'external' for arrays with externally managed metadata,
3221 * or N.M for internally known formats
3224 metadata_show(mddev_t
*mddev
, char *page
)
3226 if (mddev
->persistent
)
3227 return sprintf(page
, "%d.%d\n",
3228 mddev
->major_version
, mddev
->minor_version
);
3229 else if (mddev
->external
)
3230 return sprintf(page
, "external:%s\n", mddev
->metadata_type
);
3232 return sprintf(page
, "none\n");
3236 metadata_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3240 /* Changing the details of 'external' metadata is
3241 * always permitted. Otherwise there must be
3242 * no devices attached to the array.
3244 if (mddev
->external
&& strncmp(buf
, "external:", 9) == 0)
3246 else if (!list_empty(&mddev
->disks
))
3249 if (cmd_match(buf
, "none")) {
3250 mddev
->persistent
= 0;
3251 mddev
->external
= 0;
3252 mddev
->major_version
= 0;
3253 mddev
->minor_version
= 90;
3256 if (strncmp(buf
, "external:", 9) == 0) {
3257 size_t namelen
= len
-9;
3258 if (namelen
>= sizeof(mddev
->metadata_type
))
3259 namelen
= sizeof(mddev
->metadata_type
)-1;
3260 strncpy(mddev
->metadata_type
, buf
+9, namelen
);
3261 mddev
->metadata_type
[namelen
] = 0;
3262 if (namelen
&& mddev
->metadata_type
[namelen
-1] == '\n')
3263 mddev
->metadata_type
[--namelen
] = 0;
3264 mddev
->persistent
= 0;
3265 mddev
->external
= 1;
3266 mddev
->major_version
= 0;
3267 mddev
->minor_version
= 90;
3270 major
= simple_strtoul(buf
, &e
, 10);
3271 if (e
==buf
|| *e
!= '.')
3274 minor
= simple_strtoul(buf
, &e
, 10);
3275 if (e
==buf
|| (*e
&& *e
!= '\n') )
3277 if (major
>= ARRAY_SIZE(super_types
) || super_types
[major
].name
== NULL
)
3279 mddev
->major_version
= major
;
3280 mddev
->minor_version
= minor
;
3281 mddev
->persistent
= 1;
3282 mddev
->external
= 0;
3286 static struct md_sysfs_entry md_metadata
=
3287 __ATTR(metadata_version
, S_IRUGO
|S_IWUSR
, metadata_show
, metadata_store
);
3290 action_show(mddev_t
*mddev
, char *page
)
3292 char *type
= "idle";
3293 if (test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
3295 else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
3296 (!mddev
->ro
&& test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))) {
3297 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
3299 else if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
3300 if (!test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
3302 else if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
3306 } else if (test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
))
3309 return sprintf(page
, "%s\n", type
);
3313 action_store(mddev_t
*mddev
, const char *page
, size_t len
)
3315 if (!mddev
->pers
|| !mddev
->pers
->sync_request
)
3318 if (cmd_match(page
, "frozen"))
3319 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
3321 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
3323 if (cmd_match(page
, "idle") || cmd_match(page
, "frozen")) {
3324 if (mddev
->sync_thread
) {
3325 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
3326 md_unregister_thread(mddev
->sync_thread
);
3327 mddev
->sync_thread
= NULL
;
3328 mddev
->recovery
= 0;
3330 } else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
3331 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
3333 else if (cmd_match(page
, "resync"))
3334 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3335 else if (cmd_match(page
, "recover")) {
3336 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
3337 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3338 } else if (cmd_match(page
, "reshape")) {
3340 if (mddev
->pers
->start_reshape
== NULL
)
3342 err
= mddev
->pers
->start_reshape(mddev
);
3345 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
3347 if (cmd_match(page
, "check"))
3348 set_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
3349 else if (!cmd_match(page
, "repair"))
3351 set_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
3352 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
3354 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3355 md_wakeup_thread(mddev
->thread
);
3356 sysfs_notify_dirent(mddev
->sysfs_action
);
3361 mismatch_cnt_show(mddev_t
*mddev
, char *page
)
3363 return sprintf(page
, "%llu\n",
3364 (unsigned long long) mddev
->resync_mismatches
);
3367 static struct md_sysfs_entry md_scan_mode
=
3368 __ATTR(sync_action
, S_IRUGO
|S_IWUSR
, action_show
, action_store
);
3371 static struct md_sysfs_entry md_mismatches
= __ATTR_RO(mismatch_cnt
);
3374 sync_min_show(mddev_t
*mddev
, char *page
)
3376 return sprintf(page
, "%d (%s)\n", speed_min(mddev
),
3377 mddev
->sync_speed_min
? "local": "system");
3381 sync_min_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3385 if (strncmp(buf
, "system", 6)==0) {
3386 mddev
->sync_speed_min
= 0;
3389 min
= simple_strtoul(buf
, &e
, 10);
3390 if (buf
== e
|| (*e
&& *e
!= '\n') || min
<= 0)
3392 mddev
->sync_speed_min
= min
;
3396 static struct md_sysfs_entry md_sync_min
=
3397 __ATTR(sync_speed_min
, S_IRUGO
|S_IWUSR
, sync_min_show
, sync_min_store
);
3400 sync_max_show(mddev_t
*mddev
, char *page
)
3402 return sprintf(page
, "%d (%s)\n", speed_max(mddev
),
3403 mddev
->sync_speed_max
? "local": "system");
3407 sync_max_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3411 if (strncmp(buf
, "system", 6)==0) {
3412 mddev
->sync_speed_max
= 0;
3415 max
= simple_strtoul(buf
, &e
, 10);
3416 if (buf
== e
|| (*e
&& *e
!= '\n') || max
<= 0)
3418 mddev
->sync_speed_max
= max
;
3422 static struct md_sysfs_entry md_sync_max
=
3423 __ATTR(sync_speed_max
, S_IRUGO
|S_IWUSR
, sync_max_show
, sync_max_store
);
3426 degraded_show(mddev_t
*mddev
, char *page
)
3428 return sprintf(page
, "%d\n", mddev
->degraded
);
3430 static struct md_sysfs_entry md_degraded
= __ATTR_RO(degraded
);
3433 sync_force_parallel_show(mddev_t
*mddev
, char *page
)
3435 return sprintf(page
, "%d\n", mddev
->parallel_resync
);
3439 sync_force_parallel_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3443 if (strict_strtol(buf
, 10, &n
))
3446 if (n
!= 0 && n
!= 1)
3449 mddev
->parallel_resync
= n
;
3451 if (mddev
->sync_thread
)
3452 wake_up(&resync_wait
);
3457 /* force parallel resync, even with shared block devices */
3458 static struct md_sysfs_entry md_sync_force_parallel
=
3459 __ATTR(sync_force_parallel
, S_IRUGO
|S_IWUSR
,
3460 sync_force_parallel_show
, sync_force_parallel_store
);
3463 sync_speed_show(mddev_t
*mddev
, char *page
)
3465 unsigned long resync
, dt
, db
;
3466 if (mddev
->curr_resync
== 0)
3467 return sprintf(page
, "none\n");
3468 resync
= mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
);
3469 dt
= (jiffies
- mddev
->resync_mark
) / HZ
;
3471 db
= resync
- mddev
->resync_mark_cnt
;
3472 return sprintf(page
, "%lu\n", db
/dt
/2); /* K/sec */
3475 static struct md_sysfs_entry md_sync_speed
= __ATTR_RO(sync_speed
);
3478 sync_completed_show(mddev_t
*mddev
, char *page
)
3480 unsigned long max_sectors
, resync
;
3482 if (!test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3483 return sprintf(page
, "none\n");
3485 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
3486 max_sectors
= mddev
->resync_max_sectors
;
3488 max_sectors
= mddev
->dev_sectors
;
3490 resync
= mddev
->curr_resync_completed
;
3491 return sprintf(page
, "%lu / %lu\n", resync
, max_sectors
);
3494 static struct md_sysfs_entry md_sync_completed
= __ATTR_RO(sync_completed
);
3497 min_sync_show(mddev_t
*mddev
, char *page
)
3499 return sprintf(page
, "%llu\n",
3500 (unsigned long long)mddev
->resync_min
);
3503 min_sync_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3505 unsigned long long min
;
3506 if (strict_strtoull(buf
, 10, &min
))
3508 if (min
> mddev
->resync_max
)
3510 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3513 /* Must be a multiple of chunk_size */
3514 if (mddev
->chunk_sectors
) {
3515 sector_t temp
= min
;
3516 if (sector_div(temp
, mddev
->chunk_sectors
))
3519 mddev
->resync_min
= min
;
3524 static struct md_sysfs_entry md_min_sync
=
3525 __ATTR(sync_min
, S_IRUGO
|S_IWUSR
, min_sync_show
, min_sync_store
);
3528 max_sync_show(mddev_t
*mddev
, char *page
)
3530 if (mddev
->resync_max
== MaxSector
)
3531 return sprintf(page
, "max\n");
3533 return sprintf(page
, "%llu\n",
3534 (unsigned long long)mddev
->resync_max
);
3537 max_sync_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3539 if (strncmp(buf
, "max", 3) == 0)
3540 mddev
->resync_max
= MaxSector
;
3542 unsigned long long max
;
3543 if (strict_strtoull(buf
, 10, &max
))
3545 if (max
< mddev
->resync_min
)
3547 if (max
< mddev
->resync_max
&&
3548 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3551 /* Must be a multiple of chunk_size */
3552 if (mddev
->chunk_sectors
) {
3553 sector_t temp
= max
;
3554 if (sector_div(temp
, mddev
->chunk_sectors
))
3557 mddev
->resync_max
= max
;
3559 wake_up(&mddev
->recovery_wait
);
3563 static struct md_sysfs_entry md_max_sync
=
3564 __ATTR(sync_max
, S_IRUGO
|S_IWUSR
, max_sync_show
, max_sync_store
);
3567 suspend_lo_show(mddev_t
*mddev
, char *page
)
3569 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_lo
);
3573 suspend_lo_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3576 unsigned long long new = simple_strtoull(buf
, &e
, 10);
3578 if (mddev
->pers
== NULL
||
3579 mddev
->pers
->quiesce
== NULL
)
3581 if (buf
== e
|| (*e
&& *e
!= '\n'))
3583 if (new >= mddev
->suspend_hi
||
3584 (new > mddev
->suspend_lo
&& new < mddev
->suspend_hi
)) {
3585 mddev
->suspend_lo
= new;
3586 mddev
->pers
->quiesce(mddev
, 2);
3591 static struct md_sysfs_entry md_suspend_lo
=
3592 __ATTR(suspend_lo
, S_IRUGO
|S_IWUSR
, suspend_lo_show
, suspend_lo_store
);
3596 suspend_hi_show(mddev_t
*mddev
, char *page
)
3598 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_hi
);
3602 suspend_hi_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3605 unsigned long long new = simple_strtoull(buf
, &e
, 10);
3607 if (mddev
->pers
== NULL
||
3608 mddev
->pers
->quiesce
== NULL
)
3610 if (buf
== e
|| (*e
&& *e
!= '\n'))
3612 if ((new <= mddev
->suspend_lo
&& mddev
->suspend_lo
>= mddev
->suspend_hi
) ||
3613 (new > mddev
->suspend_lo
&& new > mddev
->suspend_hi
)) {
3614 mddev
->suspend_hi
= new;
3615 mddev
->pers
->quiesce(mddev
, 1);
3616 mddev
->pers
->quiesce(mddev
, 0);
3621 static struct md_sysfs_entry md_suspend_hi
=
3622 __ATTR(suspend_hi
, S_IRUGO
|S_IWUSR
, suspend_hi_show
, suspend_hi_store
);
3625 reshape_position_show(mddev_t
*mddev
, char *page
)
3627 if (mddev
->reshape_position
!= MaxSector
)
3628 return sprintf(page
, "%llu\n",
3629 (unsigned long long)mddev
->reshape_position
);
3630 strcpy(page
, "none\n");
3635 reshape_position_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3638 unsigned long long new = simple_strtoull(buf
, &e
, 10);
3641 if (buf
== e
|| (*e
&& *e
!= '\n'))
3643 mddev
->reshape_position
= new;
3644 mddev
->delta_disks
= 0;
3645 mddev
->new_level
= mddev
->level
;
3646 mddev
->new_layout
= mddev
->layout
;
3647 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3651 static struct md_sysfs_entry md_reshape_position
=
3652 __ATTR(reshape_position
, S_IRUGO
|S_IWUSR
, reshape_position_show
,
3653 reshape_position_store
);
3656 array_size_show(mddev_t
*mddev
, char *page
)
3658 if (mddev
->external_size
)
3659 return sprintf(page
, "%llu\n",
3660 (unsigned long long)mddev
->array_sectors
/2);
3662 return sprintf(page
, "default\n");
3666 array_size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3670 if (strncmp(buf
, "default", 7) == 0) {
3672 sectors
= mddev
->pers
->size(mddev
, 0, 0);
3674 sectors
= mddev
->array_sectors
;
3676 mddev
->external_size
= 0;
3678 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
3680 if (mddev
->pers
&& mddev
->pers
->size(mddev
, 0, 0) < sectors
)
3683 mddev
->external_size
= 1;
3686 mddev
->array_sectors
= sectors
;
3687 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
3689 struct block_device
*bdev
= bdget_disk(mddev
->gendisk
, 0);
3692 mutex_lock(&bdev
->bd_inode
->i_mutex
);
3693 i_size_write(bdev
->bd_inode
,
3694 (loff_t
)mddev
->array_sectors
<< 9);
3695 mutex_unlock(&bdev
->bd_inode
->i_mutex
);
3703 static struct md_sysfs_entry md_array_size
=
3704 __ATTR(array_size
, S_IRUGO
|S_IWUSR
, array_size_show
,
3707 static struct attribute
*md_default_attrs
[] = {
3710 &md_raid_disks
.attr
,
3711 &md_chunk_size
.attr
,
3713 &md_resync_start
.attr
,
3715 &md_new_device
.attr
,
3716 &md_safe_delay
.attr
,
3717 &md_array_state
.attr
,
3718 &md_reshape_position
.attr
,
3719 &md_array_size
.attr
,
3723 static struct attribute
*md_redundancy_attrs
[] = {
3725 &md_mismatches
.attr
,
3728 &md_sync_speed
.attr
,
3729 &md_sync_force_parallel
.attr
,
3730 &md_sync_completed
.attr
,
3733 &md_suspend_lo
.attr
,
3734 &md_suspend_hi
.attr
,
3739 static struct attribute_group md_redundancy_group
= {
3741 .attrs
= md_redundancy_attrs
,
3746 md_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
3748 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
3749 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
3754 rv
= mddev_lock(mddev
);
3756 rv
= entry
->show(mddev
, page
);
3757 mddev_unlock(mddev
);
3763 md_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
3764 const char *page
, size_t length
)
3766 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
3767 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
3772 if (!capable(CAP_SYS_ADMIN
))
3774 rv
= mddev_lock(mddev
);
3775 if (mddev
->hold_active
== UNTIL_IOCTL
)
3776 mddev
->hold_active
= 0;
3778 rv
= entry
->store(mddev
, page
, length
);
3779 mddev_unlock(mddev
);
3784 static void md_free(struct kobject
*ko
)
3786 mddev_t
*mddev
= container_of(ko
, mddev_t
, kobj
);
3788 if (mddev
->sysfs_state
)
3789 sysfs_put(mddev
->sysfs_state
);
3791 if (mddev
->gendisk
) {
3792 del_gendisk(mddev
->gendisk
);
3793 put_disk(mddev
->gendisk
);
3796 blk_cleanup_queue(mddev
->queue
);
3801 static struct sysfs_ops md_sysfs_ops
= {
3802 .show
= md_attr_show
,
3803 .store
= md_attr_store
,
3805 static struct kobj_type md_ktype
= {
3807 .sysfs_ops
= &md_sysfs_ops
,
3808 .default_attrs
= md_default_attrs
,
3813 static void mddev_delayed_delete(struct work_struct
*ws
)
3815 mddev_t
*mddev
= container_of(ws
, mddev_t
, del_work
);
3817 if (mddev
->private == &md_redundancy_group
) {
3818 sysfs_remove_group(&mddev
->kobj
, &md_redundancy_group
);
3819 if (mddev
->sysfs_action
)
3820 sysfs_put(mddev
->sysfs_action
);
3821 mddev
->sysfs_action
= NULL
;
3822 mddev
->private = NULL
;
3824 kobject_del(&mddev
->kobj
);
3825 kobject_put(&mddev
->kobj
);
3828 static int md_alloc(dev_t dev
, char *name
)
3830 static DEFINE_MUTEX(disks_mutex
);
3831 mddev_t
*mddev
= mddev_find(dev
);
3832 struct gendisk
*disk
;
3841 partitioned
= (MAJOR(mddev
->unit
) != MD_MAJOR
);
3842 shift
= partitioned
? MdpMinorShift
: 0;
3843 unit
= MINOR(mddev
->unit
) >> shift
;
3845 /* wait for any previous instance if this device
3846 * to be completed removed (mddev_delayed_delete).
3848 flush_scheduled_work();
3850 mutex_lock(&disks_mutex
);
3856 /* Need to ensure that 'name' is not a duplicate.
3859 spin_lock(&all_mddevs_lock
);
3861 list_for_each_entry(mddev2
, &all_mddevs
, all_mddevs
)
3862 if (mddev2
->gendisk
&&
3863 strcmp(mddev2
->gendisk
->disk_name
, name
) == 0) {
3864 spin_unlock(&all_mddevs_lock
);
3867 spin_unlock(&all_mddevs_lock
);
3871 mddev
->queue
= blk_alloc_queue(GFP_KERNEL
);
3874 mddev
->queue
->queuedata
= mddev
;
3876 /* Can be unlocked because the queue is new: no concurrency */
3877 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER
, mddev
->queue
);
3879 blk_queue_make_request(mddev
->queue
, md_make_request
);
3881 disk
= alloc_disk(1 << shift
);
3883 blk_cleanup_queue(mddev
->queue
);
3884 mddev
->queue
= NULL
;
3887 disk
->major
= MAJOR(mddev
->unit
);
3888 disk
->first_minor
= unit
<< shift
;
3890 strcpy(disk
->disk_name
, name
);
3891 else if (partitioned
)
3892 sprintf(disk
->disk_name
, "md_d%d", unit
);
3894 sprintf(disk
->disk_name
, "md%d", unit
);
3895 disk
->fops
= &md_fops
;
3896 disk
->private_data
= mddev
;
3897 disk
->queue
= mddev
->queue
;
3898 /* Allow extended partitions. This makes the
3899 * 'mdp' device redundant, but we can't really
3902 disk
->flags
|= GENHD_FL_EXT_DEVT
;
3904 mddev
->gendisk
= disk
;
3905 error
= kobject_init_and_add(&mddev
->kobj
, &md_ktype
,
3906 &disk_to_dev(disk
)->kobj
, "%s", "md");
3908 /* This isn't possible, but as kobject_init_and_add is marked
3909 * __must_check, we must do something with the result
3911 printk(KERN_WARNING
"md: cannot register %s/md - name in use\n",
3916 mutex_unlock(&disks_mutex
);
3918 kobject_uevent(&mddev
->kobj
, KOBJ_ADD
);
3919 mddev
->sysfs_state
= sysfs_get_dirent(mddev
->kobj
.sd
, "array_state");
3925 static struct kobject
*md_probe(dev_t dev
, int *part
, void *data
)
3927 md_alloc(dev
, NULL
);
3931 static int add_named_array(const char *val
, struct kernel_param
*kp
)
3933 /* val must be "md_*" where * is not all digits.
3934 * We allocate an array with a large free minor number, and
3935 * set the name to val. val must not already be an active name.
3937 int len
= strlen(val
);
3938 char buf
[DISK_NAME_LEN
];
3940 while (len
&& val
[len
-1] == '\n')
3942 if (len
>= DISK_NAME_LEN
)
3944 strlcpy(buf
, val
, len
+1);
3945 if (strncmp(buf
, "md_", 3) != 0)
3947 return md_alloc(0, buf
);
3950 static void md_safemode_timeout(unsigned long data
)
3952 mddev_t
*mddev
= (mddev_t
*) data
;
3954 if (!atomic_read(&mddev
->writes_pending
)) {
3955 mddev
->safemode
= 1;
3956 if (mddev
->external
)
3957 sysfs_notify_dirent(mddev
->sysfs_state
);
3959 md_wakeup_thread(mddev
->thread
);
3962 static int start_dirty_degraded
;
3964 static int do_md_run(mddev_t
* mddev
)
3968 struct gendisk
*disk
;
3969 struct mdk_personality
*pers
;
3971 if (list_empty(&mddev
->disks
))
3972 /* cannot run an array with no devices.. */
3979 * Analyze all RAID superblock(s)
3981 if (!mddev
->raid_disks
) {
3982 if (!mddev
->persistent
)
3987 if (mddev
->level
!= LEVEL_NONE
)
3988 request_module("md-level-%d", mddev
->level
);
3989 else if (mddev
->clevel
[0])
3990 request_module("md-%s", mddev
->clevel
);
3993 * Drop all container device buffers, from now on
3994 * the only valid external interface is through the md
3997 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
3998 if (test_bit(Faulty
, &rdev
->flags
))
4000 sync_blockdev(rdev
->bdev
);
4001 invalidate_bdev(rdev
->bdev
);
4003 /* perform some consistency tests on the device.
4004 * We don't want the data to overlap the metadata,
4005 * Internal Bitmap issues have been handled elsewhere.
4007 if (rdev
->data_offset
< rdev
->sb_start
) {
4008 if (mddev
->dev_sectors
&&
4009 rdev
->data_offset
+ mddev
->dev_sectors
4011 printk("md: %s: data overlaps metadata\n",
4016 if (rdev
->sb_start
+ rdev
->sb_size
/512
4017 > rdev
->data_offset
) {
4018 printk("md: %s: metadata overlaps data\n",
4023 sysfs_notify_dirent(rdev
->sysfs_state
);
4026 md_probe(mddev
->unit
, NULL
, NULL
);
4027 disk
= mddev
->gendisk
;
4031 spin_lock(&pers_lock
);
4032 pers
= find_pers(mddev
->level
, mddev
->clevel
);
4033 if (!pers
|| !try_module_get(pers
->owner
)) {
4034 spin_unlock(&pers_lock
);
4035 if (mddev
->level
!= LEVEL_NONE
)
4036 printk(KERN_WARNING
"md: personality for level %d is not loaded!\n",
4039 printk(KERN_WARNING
"md: personality for level %s is not loaded!\n",
4044 spin_unlock(&pers_lock
);
4045 if (mddev
->level
!= pers
->level
) {
4046 mddev
->level
= pers
->level
;
4047 mddev
->new_level
= pers
->level
;
4049 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
4051 if (pers
->level
>= 4 && pers
->level
<= 6)
4052 /* Cannot support integrity (yet) */
4053 blk_integrity_unregister(mddev
->gendisk
);
4055 if (mddev
->reshape_position
!= MaxSector
&&
4056 pers
->start_reshape
== NULL
) {
4057 /* This personality cannot handle reshaping... */
4059 module_put(pers
->owner
);
4063 if (pers
->sync_request
) {
4064 /* Warn if this is a potentially silly
4067 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
4071 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4072 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
4074 rdev
->bdev
->bd_contains
==
4075 rdev2
->bdev
->bd_contains
) {
4077 "%s: WARNING: %s appears to be"
4078 " on the same physical disk as"
4081 bdevname(rdev
->bdev
,b
),
4082 bdevname(rdev2
->bdev
,b2
));
4089 "True protection against single-disk"
4090 " failure might be compromised.\n");
4093 mddev
->recovery
= 0;
4094 /* may be over-ridden by personality */
4095 mddev
->resync_max_sectors
= mddev
->dev_sectors
;
4097 mddev
->barriers_work
= 1;
4098 mddev
->ok_start_degraded
= start_dirty_degraded
;
4101 mddev
->ro
= 2; /* read-only, but switch on first write */
4103 err
= mddev
->pers
->run(mddev
);
4105 printk(KERN_ERR
"md: pers->run() failed ...\n");
4106 else if (mddev
->pers
->size(mddev
, 0, 0) < mddev
->array_sectors
) {
4107 WARN_ONCE(!mddev
->external_size
, "%s: default size too small,"
4108 " but 'external_size' not in effect?\n", __func__
);
4110 "md: invalid array_size %llu > default size %llu\n",
4111 (unsigned long long)mddev
->array_sectors
/ 2,
4112 (unsigned long long)mddev
->pers
->size(mddev
, 0, 0) / 2);
4114 mddev
->pers
->stop(mddev
);
4116 if (err
== 0 && mddev
->pers
->sync_request
) {
4117 err
= bitmap_create(mddev
);
4119 printk(KERN_ERR
"%s: failed to create bitmap (%d)\n",
4120 mdname(mddev
), err
);
4121 mddev
->pers
->stop(mddev
);
4125 module_put(mddev
->pers
->owner
);
4127 bitmap_destroy(mddev
);
4130 if (mddev
->pers
->sync_request
) {
4131 if (sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
4133 "md: cannot register extra attributes for %s\n",
4135 mddev
->sysfs_action
= sysfs_get_dirent(mddev
->kobj
.sd
, "sync_action");
4136 } else if (mddev
->ro
== 2) /* auto-readonly not meaningful */
4139 atomic_set(&mddev
->writes_pending
,0);
4140 mddev
->safemode
= 0;
4141 mddev
->safemode_timer
.function
= md_safemode_timeout
;
4142 mddev
->safemode_timer
.data
= (unsigned long) mddev
;
4143 mddev
->safemode_delay
= (200 * HZ
)/1000 +1; /* 200 msec delay */
4146 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4147 if (rdev
->raid_disk
>= 0) {
4149 sprintf(nm
, "rd%d", rdev
->raid_disk
);
4150 if (sysfs_create_link(&mddev
->kobj
, &rdev
->kobj
, nm
))
4151 printk("md: cannot register %s for %s\n",
4155 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4158 md_update_sb(mddev
, 0);
4160 set_capacity(disk
, mddev
->array_sectors
);
4162 /* If there is a partially-recovered drive we need to
4163 * start recovery here. If we leave it to md_check_recovery,
4164 * it will remove the drives and not do the right thing
4166 if (mddev
->degraded
&& !mddev
->sync_thread
) {
4168 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4169 if (rdev
->raid_disk
>= 0 &&
4170 !test_bit(In_sync
, &rdev
->flags
) &&
4171 !test_bit(Faulty
, &rdev
->flags
))
4172 /* complete an interrupted recovery */
4174 if (spares
&& mddev
->pers
->sync_request
) {
4175 mddev
->recovery
= 0;
4176 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
4177 mddev
->sync_thread
= md_register_thread(md_do_sync
,
4180 if (!mddev
->sync_thread
) {
4181 printk(KERN_ERR
"%s: could not start resync"
4184 /* leave the spares where they are, it shouldn't hurt */
4185 mddev
->recovery
= 0;
4189 md_wakeup_thread(mddev
->thread
);
4190 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
4193 md_new_event(mddev
);
4194 sysfs_notify_dirent(mddev
->sysfs_state
);
4195 if (mddev
->sysfs_action
)
4196 sysfs_notify_dirent(mddev
->sysfs_action
);
4197 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
4198 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
4202 static int restart_array(mddev_t
*mddev
)
4204 struct gendisk
*disk
= mddev
->gendisk
;
4206 /* Complain if it has no devices */
4207 if (list_empty(&mddev
->disks
))
4213 mddev
->safemode
= 0;
4215 set_disk_ro(disk
, 0);
4216 printk(KERN_INFO
"md: %s switched to read-write mode.\n",
4218 /* Kick recovery or resync if necessary */
4219 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4220 md_wakeup_thread(mddev
->thread
);
4221 md_wakeup_thread(mddev
->sync_thread
);
4222 sysfs_notify_dirent(mddev
->sysfs_state
);
4226 /* similar to deny_write_access, but accounts for our holding a reference
4227 * to the file ourselves */
4228 static int deny_bitmap_write_access(struct file
* file
)
4230 struct inode
*inode
= file
->f_mapping
->host
;
4232 spin_lock(&inode
->i_lock
);
4233 if (atomic_read(&inode
->i_writecount
) > 1) {
4234 spin_unlock(&inode
->i_lock
);
4237 atomic_set(&inode
->i_writecount
, -1);
4238 spin_unlock(&inode
->i_lock
);
4243 static void restore_bitmap_write_access(struct file
*file
)
4245 struct inode
*inode
= file
->f_mapping
->host
;
4247 spin_lock(&inode
->i_lock
);
4248 atomic_set(&inode
->i_writecount
, 1);
4249 spin_unlock(&inode
->i_lock
);
4253 * 0 - completely stop and dis-assemble array
4254 * 1 - switch to readonly
4255 * 2 - stop but do not disassemble array
4257 static int do_md_stop(mddev_t
* mddev
, int mode
, int is_open
)
4260 struct gendisk
*disk
= mddev
->gendisk
;
4263 if (atomic_read(&mddev
->openers
) > is_open
) {
4264 printk("md: %s still in use.\n",mdname(mddev
));
4270 if (mddev
->sync_thread
) {
4271 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4272 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4273 md_unregister_thread(mddev
->sync_thread
);
4274 mddev
->sync_thread
= NULL
;
4277 del_timer_sync(&mddev
->safemode_timer
);
4280 case 1: /* readonly */
4286 case 0: /* disassemble */
4288 bitmap_flush(mddev
);
4289 md_super_wait(mddev
);
4291 set_disk_ro(disk
, 0);
4293 mddev
->pers
->stop(mddev
);
4294 mddev
->queue
->merge_bvec_fn
= NULL
;
4295 mddev
->queue
->unplug_fn
= NULL
;
4296 mddev
->queue
->backing_dev_info
.congested_fn
= NULL
;
4297 module_put(mddev
->pers
->owner
);
4298 if (mddev
->pers
->sync_request
)
4299 mddev
->private = &md_redundancy_group
;
4301 /* tell userspace to handle 'inactive' */
4302 sysfs_notify_dirent(mddev
->sysfs_state
);
4304 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4305 if (rdev
->raid_disk
>= 0) {
4307 sprintf(nm
, "rd%d", rdev
->raid_disk
);
4308 sysfs_remove_link(&mddev
->kobj
, nm
);
4311 set_capacity(disk
, 0);
4317 if (!mddev
->in_sync
|| mddev
->flags
) {
4318 /* mark array as shutdown cleanly */
4320 md_update_sb(mddev
, 1);
4323 set_disk_ro(disk
, 1);
4324 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4328 * Free resources if final stop
4332 printk(KERN_INFO
"md: %s stopped.\n", mdname(mddev
));
4334 bitmap_destroy(mddev
);
4335 if (mddev
->bitmap_file
) {
4336 restore_bitmap_write_access(mddev
->bitmap_file
);
4337 fput(mddev
->bitmap_file
);
4338 mddev
->bitmap_file
= NULL
;
4340 mddev
->bitmap_offset
= 0;
4342 /* make sure all md_delayed_delete calls have finished */
4343 flush_scheduled_work();
4345 export_array(mddev
);
4347 mddev
->array_sectors
= 0;
4348 mddev
->external_size
= 0;
4349 mddev
->dev_sectors
= 0;
4350 mddev
->raid_disks
= 0;
4351 mddev
->recovery_cp
= 0;
4352 mddev
->resync_min
= 0;
4353 mddev
->resync_max
= MaxSector
;
4354 mddev
->reshape_position
= MaxSector
;
4355 mddev
->external
= 0;
4356 mddev
->persistent
= 0;
4357 mddev
->level
= LEVEL_NONE
;
4358 mddev
->clevel
[0] = 0;
4361 mddev
->metadata_type
[0] = 0;
4362 mddev
->chunk_sectors
= 0;
4363 mddev
->ctime
= mddev
->utime
= 0;
4365 mddev
->max_disks
= 0;
4367 mddev
->delta_disks
= 0;
4368 mddev
->new_level
= LEVEL_NONE
;
4369 mddev
->new_layout
= 0;
4370 mddev
->new_chunk_sectors
= 0;
4371 mddev
->curr_resync
= 0;
4372 mddev
->resync_mismatches
= 0;
4373 mddev
->suspend_lo
= mddev
->suspend_hi
= 0;
4374 mddev
->sync_speed_min
= mddev
->sync_speed_max
= 0;
4375 mddev
->recovery
= 0;
4378 mddev
->degraded
= 0;
4379 mddev
->barriers_work
= 0;
4380 mddev
->safemode
= 0;
4381 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
4382 if (mddev
->hold_active
== UNTIL_STOP
)
4383 mddev
->hold_active
= 0;
4385 } else if (mddev
->pers
)
4386 printk(KERN_INFO
"md: %s switched to read-only mode.\n",
4389 blk_integrity_unregister(disk
);
4390 md_new_event(mddev
);
4391 sysfs_notify_dirent(mddev
->sysfs_state
);
4397 static void autorun_array(mddev_t
*mddev
)
4402 if (list_empty(&mddev
->disks
))
4405 printk(KERN_INFO
"md: running: ");
4407 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
4408 char b
[BDEVNAME_SIZE
];
4409 printk("<%s>", bdevname(rdev
->bdev
,b
));
4413 err
= do_md_run(mddev
);
4415 printk(KERN_WARNING
"md: do_md_run() returned %d\n", err
);
4416 do_md_stop(mddev
, 0, 0);
4421 * lets try to run arrays based on all disks that have arrived
4422 * until now. (those are in pending_raid_disks)
4424 * the method: pick the first pending disk, collect all disks with
4425 * the same UUID, remove all from the pending list and put them into
4426 * the 'same_array' list. Then order this list based on superblock
4427 * update time (freshest comes first), kick out 'old' disks and
4428 * compare superblocks. If everything's fine then run it.
4430 * If "unit" is allocated, then bump its reference count
4432 static void autorun_devices(int part
)
4434 mdk_rdev_t
*rdev0
, *rdev
, *tmp
;
4436 char b
[BDEVNAME_SIZE
];
4438 printk(KERN_INFO
"md: autorun ...\n");
4439 while (!list_empty(&pending_raid_disks
)) {
4442 LIST_HEAD(candidates
);
4443 rdev0
= list_entry(pending_raid_disks
.next
,
4444 mdk_rdev_t
, same_set
);
4446 printk(KERN_INFO
"md: considering %s ...\n",
4447 bdevname(rdev0
->bdev
,b
));
4448 INIT_LIST_HEAD(&candidates
);
4449 rdev_for_each_list(rdev
, tmp
, &pending_raid_disks
)
4450 if (super_90_load(rdev
, rdev0
, 0) >= 0) {
4451 printk(KERN_INFO
"md: adding %s ...\n",
4452 bdevname(rdev
->bdev
,b
));
4453 list_move(&rdev
->same_set
, &candidates
);
4456 * now we have a set of devices, with all of them having
4457 * mostly sane superblocks. It's time to allocate the
4461 dev
= MKDEV(mdp_major
,
4462 rdev0
->preferred_minor
<< MdpMinorShift
);
4463 unit
= MINOR(dev
) >> MdpMinorShift
;
4465 dev
= MKDEV(MD_MAJOR
, rdev0
->preferred_minor
);
4468 if (rdev0
->preferred_minor
!= unit
) {
4469 printk(KERN_INFO
"md: unit number in %s is bad: %d\n",
4470 bdevname(rdev0
->bdev
, b
), rdev0
->preferred_minor
);
4474 md_probe(dev
, NULL
, NULL
);
4475 mddev
= mddev_find(dev
);
4476 if (!mddev
|| !mddev
->gendisk
) {
4480 "md: cannot allocate memory for md drive.\n");
4483 if (mddev_lock(mddev
))
4484 printk(KERN_WARNING
"md: %s locked, cannot run\n",
4486 else if (mddev
->raid_disks
|| mddev
->major_version
4487 || !list_empty(&mddev
->disks
)) {
4489 "md: %s already running, cannot run %s\n",
4490 mdname(mddev
), bdevname(rdev0
->bdev
,b
));
4491 mddev_unlock(mddev
);
4493 printk(KERN_INFO
"md: created %s\n", mdname(mddev
));
4494 mddev
->persistent
= 1;
4495 rdev_for_each_list(rdev
, tmp
, &candidates
) {
4496 list_del_init(&rdev
->same_set
);
4497 if (bind_rdev_to_array(rdev
, mddev
))
4500 autorun_array(mddev
);
4501 mddev_unlock(mddev
);
4503 /* on success, candidates will be empty, on error
4506 rdev_for_each_list(rdev
, tmp
, &candidates
) {
4507 list_del_init(&rdev
->same_set
);
4512 printk(KERN_INFO
"md: ... autorun DONE.\n");
4514 #endif /* !MODULE */
4516 static int get_version(void __user
* arg
)
4520 ver
.major
= MD_MAJOR_VERSION
;
4521 ver
.minor
= MD_MINOR_VERSION
;
4522 ver
.patchlevel
= MD_PATCHLEVEL_VERSION
;
4524 if (copy_to_user(arg
, &ver
, sizeof(ver
)))
4530 static int get_array_info(mddev_t
* mddev
, void __user
* arg
)
4532 mdu_array_info_t info
;
4533 int nr
,working
,active
,failed
,spare
;
4536 nr
=working
=active
=failed
=spare
=0;
4537 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
4539 if (test_bit(Faulty
, &rdev
->flags
))
4543 if (test_bit(In_sync
, &rdev
->flags
))
4550 info
.major_version
= mddev
->major_version
;
4551 info
.minor_version
= mddev
->minor_version
;
4552 info
.patch_version
= MD_PATCHLEVEL_VERSION
;
4553 info
.ctime
= mddev
->ctime
;
4554 info
.level
= mddev
->level
;
4555 info
.size
= mddev
->dev_sectors
/ 2;
4556 if (info
.size
!= mddev
->dev_sectors
/ 2) /* overflow */
4559 info
.raid_disks
= mddev
->raid_disks
;
4560 info
.md_minor
= mddev
->md_minor
;
4561 info
.not_persistent
= !mddev
->persistent
;
4563 info
.utime
= mddev
->utime
;
4566 info
.state
= (1<<MD_SB_CLEAN
);
4567 if (mddev
->bitmap
&& mddev
->bitmap_offset
)
4568 info
.state
= (1<<MD_SB_BITMAP_PRESENT
);
4569 info
.active_disks
= active
;
4570 info
.working_disks
= working
;
4571 info
.failed_disks
= failed
;
4572 info
.spare_disks
= spare
;
4574 info
.layout
= mddev
->layout
;
4575 info
.chunk_size
= mddev
->chunk_sectors
<< 9;
4577 if (copy_to_user(arg
, &info
, sizeof(info
)))
4583 static int get_bitmap_file(mddev_t
* mddev
, void __user
* arg
)
4585 mdu_bitmap_file_t
*file
= NULL
; /* too big for stack allocation */
4586 char *ptr
, *buf
= NULL
;
4589 if (md_allow_write(mddev
))
4590 file
= kmalloc(sizeof(*file
), GFP_NOIO
);
4592 file
= kmalloc(sizeof(*file
), GFP_KERNEL
);
4597 /* bitmap disabled, zero the first byte and copy out */
4598 if (!mddev
->bitmap
|| !mddev
->bitmap
->file
) {
4599 file
->pathname
[0] = '\0';
4603 buf
= kmalloc(sizeof(file
->pathname
), GFP_KERNEL
);
4607 ptr
= d_path(&mddev
->bitmap
->file
->f_path
, buf
, sizeof(file
->pathname
));
4611 strcpy(file
->pathname
, ptr
);
4615 if (copy_to_user(arg
, file
, sizeof(*file
)))
4623 static int get_disk_info(mddev_t
* mddev
, void __user
* arg
)
4625 mdu_disk_info_t info
;
4628 if (copy_from_user(&info
, arg
, sizeof(info
)))
4631 rdev
= find_rdev_nr(mddev
, info
.number
);
4633 info
.major
= MAJOR(rdev
->bdev
->bd_dev
);
4634 info
.minor
= MINOR(rdev
->bdev
->bd_dev
);
4635 info
.raid_disk
= rdev
->raid_disk
;
4637 if (test_bit(Faulty
, &rdev
->flags
))
4638 info
.state
|= (1<<MD_DISK_FAULTY
);
4639 else if (test_bit(In_sync
, &rdev
->flags
)) {
4640 info
.state
|= (1<<MD_DISK_ACTIVE
);
4641 info
.state
|= (1<<MD_DISK_SYNC
);
4643 if (test_bit(WriteMostly
, &rdev
->flags
))
4644 info
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
4646 info
.major
= info
.minor
= 0;
4647 info
.raid_disk
= -1;
4648 info
.state
= (1<<MD_DISK_REMOVED
);
4651 if (copy_to_user(arg
, &info
, sizeof(info
)))
4657 static int add_new_disk(mddev_t
* mddev
, mdu_disk_info_t
*info
)
4659 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
4661 dev_t dev
= MKDEV(info
->major
,info
->minor
);
4663 if (info
->major
!= MAJOR(dev
) || info
->minor
!= MINOR(dev
))
4666 if (!mddev
->raid_disks
) {
4668 /* expecting a device which has a superblock */
4669 rdev
= md_import_device(dev
, mddev
->major_version
, mddev
->minor_version
);
4672 "md: md_import_device returned %ld\n",
4674 return PTR_ERR(rdev
);
4676 if (!list_empty(&mddev
->disks
)) {
4677 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
4678 mdk_rdev_t
, same_set
);
4679 int err
= super_types
[mddev
->major_version
]
4680 .load_super(rdev
, rdev0
, mddev
->minor_version
);
4683 "md: %s has different UUID to %s\n",
4684 bdevname(rdev
->bdev
,b
),
4685 bdevname(rdev0
->bdev
,b2
));
4690 err
= bind_rdev_to_array(rdev
, mddev
);
4697 * add_new_disk can be used once the array is assembled
4698 * to add "hot spares". They must already have a superblock
4703 if (!mddev
->pers
->hot_add_disk
) {
4705 "%s: personality does not support diskops!\n",
4709 if (mddev
->persistent
)
4710 rdev
= md_import_device(dev
, mddev
->major_version
,
4711 mddev
->minor_version
);
4713 rdev
= md_import_device(dev
, -1, -1);
4716 "md: md_import_device returned %ld\n",
4718 return PTR_ERR(rdev
);
4720 /* set save_raid_disk if appropriate */
4721 if (!mddev
->persistent
) {
4722 if (info
->state
& (1<<MD_DISK_SYNC
) &&
4723 info
->raid_disk
< mddev
->raid_disks
)
4724 rdev
->raid_disk
= info
->raid_disk
;
4726 rdev
->raid_disk
= -1;
4728 super_types
[mddev
->major_version
].
4729 validate_super(mddev
, rdev
);
4730 rdev
->saved_raid_disk
= rdev
->raid_disk
;
4732 clear_bit(In_sync
, &rdev
->flags
); /* just to be sure */
4733 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
4734 set_bit(WriteMostly
, &rdev
->flags
);
4736 clear_bit(WriteMostly
, &rdev
->flags
);
4738 rdev
->raid_disk
= -1;
4739 err
= bind_rdev_to_array(rdev
, mddev
);
4740 if (!err
&& !mddev
->pers
->hot_remove_disk
) {
4741 /* If there is hot_add_disk but no hot_remove_disk
4742 * then added disks for geometry changes,
4743 * and should be added immediately.
4745 super_types
[mddev
->major_version
].
4746 validate_super(mddev
, rdev
);
4747 err
= mddev
->pers
->hot_add_disk(mddev
, rdev
);
4749 unbind_rdev_from_array(rdev
);
4754 sysfs_notify_dirent(rdev
->sysfs_state
);
4756 md_update_sb(mddev
, 1);
4757 if (mddev
->degraded
)
4758 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
4759 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4760 md_wakeup_thread(mddev
->thread
);
4764 /* otherwise, add_new_disk is only allowed
4765 * for major_version==0 superblocks
4767 if (mddev
->major_version
!= 0) {
4768 printk(KERN_WARNING
"%s: ADD_NEW_DISK not supported\n",
4773 if (!(info
->state
& (1<<MD_DISK_FAULTY
))) {
4775 rdev
= md_import_device(dev
, -1, 0);
4778 "md: error, md_import_device() returned %ld\n",
4780 return PTR_ERR(rdev
);
4782 rdev
->desc_nr
= info
->number
;
4783 if (info
->raid_disk
< mddev
->raid_disks
)
4784 rdev
->raid_disk
= info
->raid_disk
;
4786 rdev
->raid_disk
= -1;
4788 if (rdev
->raid_disk
< mddev
->raid_disks
)
4789 if (info
->state
& (1<<MD_DISK_SYNC
))
4790 set_bit(In_sync
, &rdev
->flags
);
4792 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
4793 set_bit(WriteMostly
, &rdev
->flags
);
4795 if (!mddev
->persistent
) {
4796 printk(KERN_INFO
"md: nonpersistent superblock ...\n");
4797 rdev
->sb_start
= rdev
->bdev
->bd_inode
->i_size
/ 512;
4799 rdev
->sb_start
= calc_dev_sboffset(rdev
->bdev
);
4800 rdev
->sectors
= rdev
->sb_start
;
4802 err
= bind_rdev_to_array(rdev
, mddev
);
4812 static int hot_remove_disk(mddev_t
* mddev
, dev_t dev
)
4814 char b
[BDEVNAME_SIZE
];
4817 rdev
= find_rdev(mddev
, dev
);
4821 if (rdev
->raid_disk
>= 0)
4824 kick_rdev_from_array(rdev
);
4825 md_update_sb(mddev
, 1);
4826 md_new_event(mddev
);
4830 printk(KERN_WARNING
"md: cannot remove active disk %s from %s ...\n",
4831 bdevname(rdev
->bdev
,b
), mdname(mddev
));
4835 static int hot_add_disk(mddev_t
* mddev
, dev_t dev
)
4837 char b
[BDEVNAME_SIZE
];
4844 if (mddev
->major_version
!= 0) {
4845 printk(KERN_WARNING
"%s: HOT_ADD may only be used with"
4846 " version-0 superblocks.\n",
4850 if (!mddev
->pers
->hot_add_disk
) {
4852 "%s: personality does not support diskops!\n",
4857 rdev
= md_import_device(dev
, -1, 0);
4860 "md: error, md_import_device() returned %ld\n",
4865 if (mddev
->persistent
)
4866 rdev
->sb_start
= calc_dev_sboffset(rdev
->bdev
);
4868 rdev
->sb_start
= rdev
->bdev
->bd_inode
->i_size
/ 512;
4870 rdev
->sectors
= rdev
->sb_start
;
4872 if (test_bit(Faulty
, &rdev
->flags
)) {
4874 "md: can not hot-add faulty %s disk to %s!\n",
4875 bdevname(rdev
->bdev
,b
), mdname(mddev
));
4879 clear_bit(In_sync
, &rdev
->flags
);
4881 rdev
->saved_raid_disk
= -1;
4882 err
= bind_rdev_to_array(rdev
, mddev
);
4887 * The rest should better be atomic, we can have disk failures
4888 * noticed in interrupt contexts ...
4891 rdev
->raid_disk
= -1;
4893 md_update_sb(mddev
, 1);
4896 * Kick recovery, maybe this spare has to be added to the
4897 * array immediately.
4899 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4900 md_wakeup_thread(mddev
->thread
);
4901 md_new_event(mddev
);
4909 static int set_bitmap_file(mddev_t
*mddev
, int fd
)
4914 if (!mddev
->pers
->quiesce
)
4916 if (mddev
->recovery
|| mddev
->sync_thread
)
4918 /* we should be able to change the bitmap.. */
4924 return -EEXIST
; /* cannot add when bitmap is present */
4925 mddev
->bitmap_file
= fget(fd
);
4927 if (mddev
->bitmap_file
== NULL
) {
4928 printk(KERN_ERR
"%s: error: failed to get bitmap file\n",
4933 err
= deny_bitmap_write_access(mddev
->bitmap_file
);
4935 printk(KERN_ERR
"%s: error: bitmap file is already in use\n",
4937 fput(mddev
->bitmap_file
);
4938 mddev
->bitmap_file
= NULL
;
4941 mddev
->bitmap_offset
= 0; /* file overrides offset */
4942 } else if (mddev
->bitmap
== NULL
)
4943 return -ENOENT
; /* cannot remove what isn't there */
4946 mddev
->pers
->quiesce(mddev
, 1);
4948 err
= bitmap_create(mddev
);
4949 if (fd
< 0 || err
) {
4950 bitmap_destroy(mddev
);
4951 fd
= -1; /* make sure to put the file */
4953 mddev
->pers
->quiesce(mddev
, 0);
4956 if (mddev
->bitmap_file
) {
4957 restore_bitmap_write_access(mddev
->bitmap_file
);
4958 fput(mddev
->bitmap_file
);
4960 mddev
->bitmap_file
= NULL
;
4967 * set_array_info is used two different ways
4968 * The original usage is when creating a new array.
4969 * In this usage, raid_disks is > 0 and it together with
4970 * level, size, not_persistent,layout,chunksize determine the
4971 * shape of the array.
4972 * This will always create an array with a type-0.90.0 superblock.
4973 * The newer usage is when assembling an array.
4974 * In this case raid_disks will be 0, and the major_version field is
4975 * use to determine which style super-blocks are to be found on the devices.
4976 * The minor and patch _version numbers are also kept incase the
4977 * super_block handler wishes to interpret them.
4979 static int set_array_info(mddev_t
* mddev
, mdu_array_info_t
*info
)
4982 if (info
->raid_disks
== 0) {
4983 /* just setting version number for superblock loading */
4984 if (info
->major_version
< 0 ||
4985 info
->major_version
>= ARRAY_SIZE(super_types
) ||
4986 super_types
[info
->major_version
].name
== NULL
) {
4987 /* maybe try to auto-load a module? */
4989 "md: superblock version %d not known\n",
4990 info
->major_version
);
4993 mddev
->major_version
= info
->major_version
;
4994 mddev
->minor_version
= info
->minor_version
;
4995 mddev
->patch_version
= info
->patch_version
;
4996 mddev
->persistent
= !info
->not_persistent
;
4999 mddev
->major_version
= MD_MAJOR_VERSION
;
5000 mddev
->minor_version
= MD_MINOR_VERSION
;
5001 mddev
->patch_version
= MD_PATCHLEVEL_VERSION
;
5002 mddev
->ctime
= get_seconds();
5004 mddev
->level
= info
->level
;
5005 mddev
->clevel
[0] = 0;
5006 mddev
->dev_sectors
= 2 * (sector_t
)info
->size
;
5007 mddev
->raid_disks
= info
->raid_disks
;
5008 /* don't set md_minor, it is determined by which /dev/md* was
5011 if (info
->state
& (1<<MD_SB_CLEAN
))
5012 mddev
->recovery_cp
= MaxSector
;
5014 mddev
->recovery_cp
= 0;
5015 mddev
->persistent
= ! info
->not_persistent
;
5016 mddev
->external
= 0;
5018 mddev
->layout
= info
->layout
;
5019 mddev
->chunk_sectors
= info
->chunk_size
>> 9;
5021 mddev
->max_disks
= MD_SB_DISKS
;
5023 if (mddev
->persistent
)
5025 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
5027 mddev
->default_bitmap_offset
= MD_SB_BYTES
>> 9;
5028 mddev
->bitmap_offset
= 0;
5030 mddev
->reshape_position
= MaxSector
;
5033 * Generate a 128 bit UUID
5035 get_random_bytes(mddev
->uuid
, 16);
5037 mddev
->new_level
= mddev
->level
;
5038 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
5039 mddev
->new_layout
= mddev
->layout
;
5040 mddev
->delta_disks
= 0;
5045 void md_set_array_sectors(mddev_t
*mddev
, sector_t array_sectors
)
5047 WARN(!mddev_is_locked(mddev
), "%s: unlocked mddev!\n", __func__
);
5049 if (mddev
->external_size
)
5052 mddev
->array_sectors
= array_sectors
;
5054 EXPORT_SYMBOL(md_set_array_sectors
);
5056 static int update_size(mddev_t
*mddev
, sector_t num_sectors
)
5060 int fit
= (num_sectors
== 0);
5062 if (mddev
->pers
->resize
== NULL
)
5064 /* The "num_sectors" is the number of sectors of each device that
5065 * is used. This can only make sense for arrays with redundancy.
5066 * linear and raid0 always use whatever space is available. We can only
5067 * consider changing this number if no resync or reconstruction is
5068 * happening, and if the new size is acceptable. It must fit before the
5069 * sb_start or, if that is <data_offset, it must fit before the size
5070 * of each device. If num_sectors is zero, we find the largest size
5074 if (mddev
->sync_thread
)
5077 /* Sorry, cannot grow a bitmap yet, just remove it,
5081 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
5082 sector_t avail
= rdev
->sectors
;
5084 if (fit
&& (num_sectors
== 0 || num_sectors
> avail
))
5085 num_sectors
= avail
;
5086 if (avail
< num_sectors
)
5089 rv
= mddev
->pers
->resize(mddev
, num_sectors
);
5091 struct block_device
*bdev
;
5093 bdev
= bdget_disk(mddev
->gendisk
, 0);
5095 mutex_lock(&bdev
->bd_inode
->i_mutex
);
5096 i_size_write(bdev
->bd_inode
,
5097 (loff_t
)mddev
->array_sectors
<< 9);
5098 mutex_unlock(&bdev
->bd_inode
->i_mutex
);
5105 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
)
5108 /* change the number of raid disks */
5109 if (mddev
->pers
->check_reshape
== NULL
)
5111 if (raid_disks
<= 0 ||
5112 raid_disks
>= mddev
->max_disks
)
5114 if (mddev
->sync_thread
|| mddev
->reshape_position
!= MaxSector
)
5116 mddev
->delta_disks
= raid_disks
- mddev
->raid_disks
;
5118 rv
= mddev
->pers
->check_reshape(mddev
);
5124 * update_array_info is used to change the configuration of an
5126 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5127 * fields in the info are checked against the array.
5128 * Any differences that cannot be handled will cause an error.
5129 * Normally, only one change can be managed at a time.
5131 static int update_array_info(mddev_t
*mddev
, mdu_array_info_t
*info
)
5137 /* calculate expected state,ignoring low bits */
5138 if (mddev
->bitmap
&& mddev
->bitmap_offset
)
5139 state
|= (1 << MD_SB_BITMAP_PRESENT
);
5141 if (mddev
->major_version
!= info
->major_version
||
5142 mddev
->minor_version
!= info
->minor_version
||
5143 /* mddev->patch_version != info->patch_version || */
5144 mddev
->ctime
!= info
->ctime
||
5145 mddev
->level
!= info
->level
||
5146 /* mddev->layout != info->layout || */
5147 !mddev
->persistent
!= info
->not_persistent
||
5148 mddev
->chunk_sectors
!= info
->chunk_size
>> 9 ||
5149 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5150 ((state
^info
->state
) & 0xfffffe00)
5153 /* Check there is only one change */
5154 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
5156 if (mddev
->raid_disks
!= info
->raid_disks
)
5158 if (mddev
->layout
!= info
->layout
)
5160 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
))
5167 if (mddev
->layout
!= info
->layout
) {
5169 * we don't need to do anything at the md level, the
5170 * personality will take care of it all.
5172 if (mddev
->pers
->check_reshape
== NULL
)
5175 mddev
->new_layout
= info
->layout
;
5176 rv
= mddev
->pers
->check_reshape(mddev
);
5178 mddev
->new_layout
= mddev
->layout
;
5182 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
5183 rv
= update_size(mddev
, (sector_t
)info
->size
* 2);
5185 if (mddev
->raid_disks
!= info
->raid_disks
)
5186 rv
= update_raid_disks(mddev
, info
->raid_disks
);
5188 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) {
5189 if (mddev
->pers
->quiesce
== NULL
)
5191 if (mddev
->recovery
|| mddev
->sync_thread
)
5193 if (info
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
5194 /* add the bitmap */
5197 if (mddev
->default_bitmap_offset
== 0)
5199 mddev
->bitmap_offset
= mddev
->default_bitmap_offset
;
5200 mddev
->pers
->quiesce(mddev
, 1);
5201 rv
= bitmap_create(mddev
);
5203 bitmap_destroy(mddev
);
5204 mddev
->pers
->quiesce(mddev
, 0);
5206 /* remove the bitmap */
5209 if (mddev
->bitmap
->file
)
5211 mddev
->pers
->quiesce(mddev
, 1);
5212 bitmap_destroy(mddev
);
5213 mddev
->pers
->quiesce(mddev
, 0);
5214 mddev
->bitmap_offset
= 0;
5217 md_update_sb(mddev
, 1);
5221 static int set_disk_faulty(mddev_t
*mddev
, dev_t dev
)
5225 if (mddev
->pers
== NULL
)
5228 rdev
= find_rdev(mddev
, dev
);
5232 md_error(mddev
, rdev
);
5237 * We have a problem here : there is no easy way to give a CHS
5238 * virtual geometry. We currently pretend that we have a 2 heads
5239 * 4 sectors (with a BIG number of cylinders...). This drives
5240 * dosfs just mad... ;-)
5242 static int md_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
5244 mddev_t
*mddev
= bdev
->bd_disk
->private_data
;
5248 geo
->cylinders
= get_capacity(mddev
->gendisk
) / 8;
5252 static int md_ioctl(struct block_device
*bdev
, fmode_t mode
,
5253 unsigned int cmd
, unsigned long arg
)
5256 void __user
*argp
= (void __user
*)arg
;
5257 mddev_t
*mddev
= NULL
;
5259 if (!capable(CAP_SYS_ADMIN
))
5263 * Commands dealing with the RAID driver but not any
5269 err
= get_version(argp
);
5272 case PRINT_RAID_DEBUG
:
5280 autostart_arrays(arg
);
5287 * Commands creating/starting a new array:
5290 mddev
= bdev
->bd_disk
->private_data
;
5297 err
= mddev_lock(mddev
);
5300 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5307 case SET_ARRAY_INFO
:
5309 mdu_array_info_t info
;
5311 memset(&info
, 0, sizeof(info
));
5312 else if (copy_from_user(&info
, argp
, sizeof(info
))) {
5317 err
= update_array_info(mddev
, &info
);
5319 printk(KERN_WARNING
"md: couldn't update"
5320 " array info. %d\n", err
);
5325 if (!list_empty(&mddev
->disks
)) {
5327 "md: array %s already has disks!\n",
5332 if (mddev
->raid_disks
) {
5334 "md: array %s already initialised!\n",
5339 err
= set_array_info(mddev
, &info
);
5341 printk(KERN_WARNING
"md: couldn't set"
5342 " array info. %d\n", err
);
5352 * Commands querying/configuring an existing array:
5354 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
5355 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
5356 if ((!mddev
->raid_disks
&& !mddev
->external
)
5357 && cmd
!= ADD_NEW_DISK
&& cmd
!= STOP_ARRAY
5358 && cmd
!= RUN_ARRAY
&& cmd
!= SET_BITMAP_FILE
5359 && cmd
!= GET_BITMAP_FILE
) {
5365 * Commands even a read-only array can execute:
5369 case GET_ARRAY_INFO
:
5370 err
= get_array_info(mddev
, argp
);
5373 case GET_BITMAP_FILE
:
5374 err
= get_bitmap_file(mddev
, argp
);
5378 err
= get_disk_info(mddev
, argp
);
5381 case RESTART_ARRAY_RW
:
5382 err
= restart_array(mddev
);
5386 err
= do_md_stop(mddev
, 0, 1);
5390 err
= do_md_stop(mddev
, 1, 1);
5396 * The remaining ioctls are changing the state of the
5397 * superblock, so we do not allow them on read-only arrays.
5398 * However non-MD ioctls (e.g. get-size) will still come through
5399 * here and hit the 'default' below, so only disallow
5400 * 'md' ioctls, and switch to rw mode if started auto-readonly.
5402 if (_IOC_TYPE(cmd
) == MD_MAJOR
&& mddev
->ro
&& mddev
->pers
) {
5403 if (mddev
->ro
== 2) {
5405 sysfs_notify_dirent(mddev
->sysfs_state
);
5406 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5407 md_wakeup_thread(mddev
->thread
);
5418 mdu_disk_info_t info
;
5419 if (copy_from_user(&info
, argp
, sizeof(info
)))
5422 err
= add_new_disk(mddev
, &info
);
5426 case HOT_REMOVE_DISK
:
5427 err
= hot_remove_disk(mddev
, new_decode_dev(arg
));
5431 err
= hot_add_disk(mddev
, new_decode_dev(arg
));
5434 case SET_DISK_FAULTY
:
5435 err
= set_disk_faulty(mddev
, new_decode_dev(arg
));
5439 err
= do_md_run(mddev
);
5442 case SET_BITMAP_FILE
:
5443 err
= set_bitmap_file(mddev
, (int)arg
);
5453 if (mddev
->hold_active
== UNTIL_IOCTL
&&
5455 mddev
->hold_active
= 0;
5456 mddev_unlock(mddev
);
5466 static int md_open(struct block_device
*bdev
, fmode_t mode
)
5469 * Succeed if we can lock the mddev, which confirms that
5470 * it isn't being stopped right now.
5472 mddev_t
*mddev
= mddev_find(bdev
->bd_dev
);
5475 if (mddev
->gendisk
!= bdev
->bd_disk
) {
5476 /* we are racing with mddev_put which is discarding this
5480 /* Wait until bdev->bd_disk is definitely gone */
5481 flush_scheduled_work();
5482 /* Then retry the open from the top */
5483 return -ERESTARTSYS
;
5485 BUG_ON(mddev
!= bdev
->bd_disk
->private_data
);
5487 if ((err
= mutex_lock_interruptible_nested(&mddev
->reconfig_mutex
, 1)))
5491 atomic_inc(&mddev
->openers
);
5492 mddev_unlock(mddev
);
5494 check_disk_change(bdev
);
5499 static int md_release(struct gendisk
*disk
, fmode_t mode
)
5501 mddev_t
*mddev
= disk
->private_data
;
5504 atomic_dec(&mddev
->openers
);
5510 static int md_media_changed(struct gendisk
*disk
)
5512 mddev_t
*mddev
= disk
->private_data
;
5514 return mddev
->changed
;
5517 static int md_revalidate(struct gendisk
*disk
)
5519 mddev_t
*mddev
= disk
->private_data
;
5524 static struct block_device_operations md_fops
=
5526 .owner
= THIS_MODULE
,
5528 .release
= md_release
,
5530 .getgeo
= md_getgeo
,
5531 .media_changed
= md_media_changed
,
5532 .revalidate_disk
= md_revalidate
,
5535 static int md_thread(void * arg
)
5537 mdk_thread_t
*thread
= arg
;
5540 * md_thread is a 'system-thread', it's priority should be very
5541 * high. We avoid resource deadlocks individually in each
5542 * raid personality. (RAID5 does preallocation) We also use RR and
5543 * the very same RT priority as kswapd, thus we will never get
5544 * into a priority inversion deadlock.
5546 * we definitely have to have equal or higher priority than
5547 * bdflush, otherwise bdflush will deadlock if there are too
5548 * many dirty RAID5 blocks.
5551 allow_signal(SIGKILL
);
5552 while (!kthread_should_stop()) {
5554 /* We need to wait INTERRUPTIBLE so that
5555 * we don't add to the load-average.
5556 * That means we need to be sure no signals are
5559 if (signal_pending(current
))
5560 flush_signals(current
);
5562 wait_event_interruptible_timeout
5564 test_bit(THREAD_WAKEUP
, &thread
->flags
)
5565 || kthread_should_stop(),
5568 clear_bit(THREAD_WAKEUP
, &thread
->flags
);
5570 thread
->run(thread
->mddev
);
5576 void md_wakeup_thread(mdk_thread_t
*thread
)
5579 dprintk("md: waking up MD thread %s.\n", thread
->tsk
->comm
);
5580 set_bit(THREAD_WAKEUP
, &thread
->flags
);
5581 wake_up(&thread
->wqueue
);
5585 mdk_thread_t
*md_register_thread(void (*run
) (mddev_t
*), mddev_t
*mddev
,
5588 mdk_thread_t
*thread
;
5590 thread
= kzalloc(sizeof(mdk_thread_t
), GFP_KERNEL
);
5594 init_waitqueue_head(&thread
->wqueue
);
5597 thread
->mddev
= mddev
;
5598 thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
5599 thread
->tsk
= kthread_run(md_thread
, thread
, name
, mdname(thread
->mddev
));
5600 if (IS_ERR(thread
->tsk
)) {
5607 void md_unregister_thread(mdk_thread_t
*thread
)
5611 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread
->tsk
));
5613 kthread_stop(thread
->tsk
);
5617 void md_error(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
5624 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
5627 if (mddev
->external
)
5628 set_bit(Blocked
, &rdev
->flags
);
5630 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5632 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5633 __builtin_return_address(0),__builtin_return_address(1),
5634 __builtin_return_address(2),__builtin_return_address(3));
5638 if (!mddev
->pers
->error_handler
)
5640 mddev
->pers
->error_handler(mddev
,rdev
);
5641 if (mddev
->degraded
)
5642 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
5643 set_bit(StateChanged
, &rdev
->flags
);
5644 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5645 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5646 md_wakeup_thread(mddev
->thread
);
5647 md_new_event_inintr(mddev
);
5650 /* seq_file implementation /proc/mdstat */
5652 static void status_unused(struct seq_file
*seq
)
5657 seq_printf(seq
, "unused devices: ");
5659 list_for_each_entry(rdev
, &pending_raid_disks
, same_set
) {
5660 char b
[BDEVNAME_SIZE
];
5662 seq_printf(seq
, "%s ",
5663 bdevname(rdev
->bdev
,b
));
5666 seq_printf(seq
, "<none>");
5668 seq_printf(seq
, "\n");
5672 static void status_resync(struct seq_file
*seq
, mddev_t
* mddev
)
5674 sector_t max_sectors
, resync
, res
;
5675 unsigned long dt
, db
;
5678 unsigned int per_milli
;
5680 resync
= mddev
->curr_resync
- atomic_read(&mddev
->recovery_active
);
5682 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
5683 max_sectors
= mddev
->resync_max_sectors
;
5685 max_sectors
= mddev
->dev_sectors
;
5688 * Should not happen.
5694 /* Pick 'scale' such that (resync>>scale)*1000 will fit
5695 * in a sector_t, and (max_sectors>>scale) will fit in a
5696 * u32, as those are the requirements for sector_div.
5697 * Thus 'scale' must be at least 10
5700 if (sizeof(sector_t
) > sizeof(unsigned long)) {
5701 while ( max_sectors
/2 > (1ULL<<(scale
+32)))
5704 res
= (resync
>>scale
)*1000;
5705 sector_div(res
, (u32
)((max_sectors
>>scale
)+1));
5709 int i
, x
= per_milli
/50, y
= 20-x
;
5710 seq_printf(seq
, "[");
5711 for (i
= 0; i
< x
; i
++)
5712 seq_printf(seq
, "=");
5713 seq_printf(seq
, ">");
5714 for (i
= 0; i
< y
; i
++)
5715 seq_printf(seq
, ".");
5716 seq_printf(seq
, "] ");
5718 seq_printf(seq
, " %s =%3u.%u%% (%llu/%llu)",
5719 (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)?
5721 (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)?
5723 (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ?
5724 "resync" : "recovery"))),
5725 per_milli
/10, per_milli
% 10,
5726 (unsigned long long) resync
/2,
5727 (unsigned long long) max_sectors
/2);
5730 * dt: time from mark until now
5731 * db: blocks written from mark until now
5732 * rt: remaining time
5734 * rt is a sector_t, so could be 32bit or 64bit.
5735 * So we divide before multiply in case it is 32bit and close
5737 * We scale the divisor (db) by 32 to avoid loosing precision
5738 * near the end of resync when the number of remaining sectors
5740 * We then divide rt by 32 after multiplying by db to compensate.
5741 * The '+1' avoids division by zero if db is very small.
5743 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
5745 db
= (mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
))
5746 - mddev
->resync_mark_cnt
;
5748 rt
= max_sectors
- resync
; /* number of remaining sectors */
5749 sector_div(rt
, db
/32+1);
5753 seq_printf(seq
, " finish=%lu.%lumin", (unsigned long)rt
/ 60,
5754 ((unsigned long)rt
% 60)/6);
5756 seq_printf(seq
, " speed=%ldK/sec", db
/2/dt
);
5759 static void *md_seq_start(struct seq_file
*seq
, loff_t
*pos
)
5761 struct list_head
*tmp
;
5771 spin_lock(&all_mddevs_lock
);
5772 list_for_each(tmp
,&all_mddevs
)
5774 mddev
= list_entry(tmp
, mddev_t
, all_mddevs
);
5776 spin_unlock(&all_mddevs_lock
);
5779 spin_unlock(&all_mddevs_lock
);
5781 return (void*)2;/* tail */
5785 static void *md_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
5787 struct list_head
*tmp
;
5788 mddev_t
*next_mddev
, *mddev
= v
;
5794 spin_lock(&all_mddevs_lock
);
5796 tmp
= all_mddevs
.next
;
5798 tmp
= mddev
->all_mddevs
.next
;
5799 if (tmp
!= &all_mddevs
)
5800 next_mddev
= mddev_get(list_entry(tmp
,mddev_t
,all_mddevs
));
5802 next_mddev
= (void*)2;
5805 spin_unlock(&all_mddevs_lock
);
5813 static void md_seq_stop(struct seq_file
*seq
, void *v
)
5817 if (mddev
&& v
!= (void*)1 && v
!= (void*)2)
5821 struct mdstat_info
{
5825 static int md_seq_show(struct seq_file
*seq
, void *v
)
5830 struct mdstat_info
*mi
= seq
->private;
5831 struct bitmap
*bitmap
;
5833 if (v
== (void*)1) {
5834 struct mdk_personality
*pers
;
5835 seq_printf(seq
, "Personalities : ");
5836 spin_lock(&pers_lock
);
5837 list_for_each_entry(pers
, &pers_list
, list
)
5838 seq_printf(seq
, "[%s] ", pers
->name
);
5840 spin_unlock(&pers_lock
);
5841 seq_printf(seq
, "\n");
5842 mi
->event
= atomic_read(&md_event_count
);
5845 if (v
== (void*)2) {
5850 if (mddev_lock(mddev
) < 0)
5853 if (mddev
->pers
|| mddev
->raid_disks
|| !list_empty(&mddev
->disks
)) {
5854 seq_printf(seq
, "%s : %sactive", mdname(mddev
),
5855 mddev
->pers
? "" : "in");
5858 seq_printf(seq
, " (read-only)");
5860 seq_printf(seq
, " (auto-read-only)");
5861 seq_printf(seq
, " %s", mddev
->pers
->name
);
5865 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
5866 char b
[BDEVNAME_SIZE
];
5867 seq_printf(seq
, " %s[%d]",
5868 bdevname(rdev
->bdev
,b
), rdev
->desc_nr
);
5869 if (test_bit(WriteMostly
, &rdev
->flags
))
5870 seq_printf(seq
, "(W)");
5871 if (test_bit(Faulty
, &rdev
->flags
)) {
5872 seq_printf(seq
, "(F)");
5874 } else if (rdev
->raid_disk
< 0)
5875 seq_printf(seq
, "(S)"); /* spare */
5876 sectors
+= rdev
->sectors
;
5879 if (!list_empty(&mddev
->disks
)) {
5881 seq_printf(seq
, "\n %llu blocks",
5882 (unsigned long long)
5883 mddev
->array_sectors
/ 2);
5885 seq_printf(seq
, "\n %llu blocks",
5886 (unsigned long long)sectors
/ 2);
5888 if (mddev
->persistent
) {
5889 if (mddev
->major_version
!= 0 ||
5890 mddev
->minor_version
!= 90) {
5891 seq_printf(seq
," super %d.%d",
5892 mddev
->major_version
,
5893 mddev
->minor_version
);
5895 } else if (mddev
->external
)
5896 seq_printf(seq
, " super external:%s",
5897 mddev
->metadata_type
);
5899 seq_printf(seq
, " super non-persistent");
5902 mddev
->pers
->status(seq
, mddev
);
5903 seq_printf(seq
, "\n ");
5904 if (mddev
->pers
->sync_request
) {
5905 if (mddev
->curr_resync
> 2) {
5906 status_resync(seq
, mddev
);
5907 seq_printf(seq
, "\n ");
5908 } else if (mddev
->curr_resync
== 1 || mddev
->curr_resync
== 2)
5909 seq_printf(seq
, "\tresync=DELAYED\n ");
5910 else if (mddev
->recovery_cp
< MaxSector
)
5911 seq_printf(seq
, "\tresync=PENDING\n ");
5914 seq_printf(seq
, "\n ");
5916 if ((bitmap
= mddev
->bitmap
)) {
5917 unsigned long chunk_kb
;
5918 unsigned long flags
;
5919 spin_lock_irqsave(&bitmap
->lock
, flags
);
5920 chunk_kb
= bitmap
->chunksize
>> 10;
5921 seq_printf(seq
, "bitmap: %lu/%lu pages [%luKB], "
5923 bitmap
->pages
- bitmap
->missing_pages
,
5925 (bitmap
->pages
- bitmap
->missing_pages
)
5926 << (PAGE_SHIFT
- 10),
5927 chunk_kb
? chunk_kb
: bitmap
->chunksize
,
5928 chunk_kb
? "KB" : "B");
5930 seq_printf(seq
, ", file: ");
5931 seq_path(seq
, &bitmap
->file
->f_path
, " \t\n");
5934 seq_printf(seq
, "\n");
5935 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
5938 seq_printf(seq
, "\n");
5940 mddev_unlock(mddev
);
5945 static const struct seq_operations md_seq_ops
= {
5946 .start
= md_seq_start
,
5947 .next
= md_seq_next
,
5948 .stop
= md_seq_stop
,
5949 .show
= md_seq_show
,
5952 static int md_seq_open(struct inode
*inode
, struct file
*file
)
5955 struct mdstat_info
*mi
= kmalloc(sizeof(*mi
), GFP_KERNEL
);
5959 error
= seq_open(file
, &md_seq_ops
);
5963 struct seq_file
*p
= file
->private_data
;
5965 mi
->event
= atomic_read(&md_event_count
);
5970 static unsigned int mdstat_poll(struct file
*filp
, poll_table
*wait
)
5972 struct seq_file
*m
= filp
->private_data
;
5973 struct mdstat_info
*mi
= m
->private;
5976 poll_wait(filp
, &md_event_waiters
, wait
);
5978 /* always allow read */
5979 mask
= POLLIN
| POLLRDNORM
;
5981 if (mi
->event
!= atomic_read(&md_event_count
))
5982 mask
|= POLLERR
| POLLPRI
;
5986 static const struct file_operations md_seq_fops
= {
5987 .owner
= THIS_MODULE
,
5988 .open
= md_seq_open
,
5990 .llseek
= seq_lseek
,
5991 .release
= seq_release_private
,
5992 .poll
= mdstat_poll
,
5995 int register_md_personality(struct mdk_personality
*p
)
5997 spin_lock(&pers_lock
);
5998 list_add_tail(&p
->list
, &pers_list
);
5999 printk(KERN_INFO
"md: %s personality registered for level %d\n", p
->name
, p
->level
);
6000 spin_unlock(&pers_lock
);
6004 int unregister_md_personality(struct mdk_personality
*p
)
6006 printk(KERN_INFO
"md: %s personality unregistered\n", p
->name
);
6007 spin_lock(&pers_lock
);
6008 list_del_init(&p
->list
);
6009 spin_unlock(&pers_lock
);
6013 static int is_mddev_idle(mddev_t
*mddev
, int init
)
6021 rdev_for_each_rcu(rdev
, mddev
) {
6022 struct gendisk
*disk
= rdev
->bdev
->bd_contains
->bd_disk
;
6023 curr_events
= (int)part_stat_read(&disk
->part0
, sectors
[0]) +
6024 (int)part_stat_read(&disk
->part0
, sectors
[1]) -
6025 atomic_read(&disk
->sync_io
);
6026 /* sync IO will cause sync_io to increase before the disk_stats
6027 * as sync_io is counted when a request starts, and
6028 * disk_stats is counted when it completes.
6029 * So resync activity will cause curr_events to be smaller than
6030 * when there was no such activity.
6031 * non-sync IO will cause disk_stat to increase without
6032 * increasing sync_io so curr_events will (eventually)
6033 * be larger than it was before. Once it becomes
6034 * substantially larger, the test below will cause
6035 * the array to appear non-idle, and resync will slow
6037 * If there is a lot of outstanding resync activity when
6038 * we set last_event to curr_events, then all that activity
6039 * completing might cause the array to appear non-idle
6040 * and resync will be slowed down even though there might
6041 * not have been non-resync activity. This will only
6042 * happen once though. 'last_events' will soon reflect
6043 * the state where there is little or no outstanding
6044 * resync requests, and further resync activity will
6045 * always make curr_events less than last_events.
6048 if (init
|| curr_events
- rdev
->last_events
> 64) {
6049 rdev
->last_events
= curr_events
;
6057 void md_done_sync(mddev_t
*mddev
, int blocks
, int ok
)
6059 /* another "blocks" (512byte) blocks have been synced */
6060 atomic_sub(blocks
, &mddev
->recovery_active
);
6061 wake_up(&mddev
->recovery_wait
);
6063 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6064 md_wakeup_thread(mddev
->thread
);
6065 // stop recovery, signal do_sync ....
6070 /* md_write_start(mddev, bi)
6071 * If we need to update some array metadata (e.g. 'active' flag
6072 * in superblock) before writing, schedule a superblock update
6073 * and wait for it to complete.
6075 void md_write_start(mddev_t
*mddev
, struct bio
*bi
)
6078 if (bio_data_dir(bi
) != WRITE
)
6081 BUG_ON(mddev
->ro
== 1);
6082 if (mddev
->ro
== 2) {
6083 /* need to switch to read/write */
6085 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6086 md_wakeup_thread(mddev
->thread
);
6087 md_wakeup_thread(mddev
->sync_thread
);
6090 atomic_inc(&mddev
->writes_pending
);
6091 if (mddev
->safemode
== 1)
6092 mddev
->safemode
= 0;
6093 if (mddev
->in_sync
) {
6094 spin_lock_irq(&mddev
->write_lock
);
6095 if (mddev
->in_sync
) {
6097 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6098 md_wakeup_thread(mddev
->thread
);
6101 spin_unlock_irq(&mddev
->write_lock
);
6104 sysfs_notify_dirent(mddev
->sysfs_state
);
6105 wait_event(mddev
->sb_wait
,
6106 !test_bit(MD_CHANGE_CLEAN
, &mddev
->flags
) &&
6107 !test_bit(MD_CHANGE_PENDING
, &mddev
->flags
));
6110 void md_write_end(mddev_t
*mddev
)
6112 if (atomic_dec_and_test(&mddev
->writes_pending
)) {
6113 if (mddev
->safemode
== 2)
6114 md_wakeup_thread(mddev
->thread
);
6115 else if (mddev
->safemode_delay
)
6116 mod_timer(&mddev
->safemode_timer
, jiffies
+ mddev
->safemode_delay
);
6120 /* md_allow_write(mddev)
6121 * Calling this ensures that the array is marked 'active' so that writes
6122 * may proceed without blocking. It is important to call this before
6123 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6124 * Must be called with mddev_lock held.
6126 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6127 * is dropped, so return -EAGAIN after notifying userspace.
6129 int md_allow_write(mddev_t
*mddev
)
6135 if (!mddev
->pers
->sync_request
)
6138 spin_lock_irq(&mddev
->write_lock
);
6139 if (mddev
->in_sync
) {
6141 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6142 if (mddev
->safemode_delay
&&
6143 mddev
->safemode
== 0)
6144 mddev
->safemode
= 1;
6145 spin_unlock_irq(&mddev
->write_lock
);
6146 md_update_sb(mddev
, 0);
6147 sysfs_notify_dirent(mddev
->sysfs_state
);
6149 spin_unlock_irq(&mddev
->write_lock
);
6151 if (test_bit(MD_CHANGE_CLEAN
, &mddev
->flags
))
6156 EXPORT_SYMBOL_GPL(md_allow_write
);
6158 #define SYNC_MARKS 10
6159 #define SYNC_MARK_STEP (3*HZ)
6160 void md_do_sync(mddev_t
*mddev
)
6163 unsigned int currspeed
= 0,
6165 sector_t max_sectors
,j
, io_sectors
;
6166 unsigned long mark
[SYNC_MARKS
];
6167 sector_t mark_cnt
[SYNC_MARKS
];
6169 struct list_head
*tmp
;
6170 sector_t last_check
;
6175 /* just incase thread restarts... */
6176 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
6178 if (mddev
->ro
) /* never try to sync a read-only array */
6181 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
6182 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
6183 desc
= "data-check";
6184 else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
6185 desc
= "requested-resync";
6188 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
6193 /* we overload curr_resync somewhat here.
6194 * 0 == not engaged in resync at all
6195 * 2 == checking that there is no conflict with another sync
6196 * 1 == like 2, but have yielded to allow conflicting resync to
6198 * other == active in resync - this many blocks
6200 * Before starting a resync we must have set curr_resync to
6201 * 2, and then checked that every "conflicting" array has curr_resync
6202 * less than ours. When we find one that is the same or higher
6203 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6204 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6205 * This will mean we have to start checking from the beginning again.
6210 mddev
->curr_resync
= 2;
6213 if (kthread_should_stop()) {
6214 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6217 for_each_mddev(mddev2
, tmp
) {
6218 if (mddev2
== mddev
)
6220 if (!mddev
->parallel_resync
6221 && mddev2
->curr_resync
6222 && match_mddev_units(mddev
, mddev2
)) {
6224 if (mddev
< mddev2
&& mddev
->curr_resync
== 2) {
6225 /* arbitrarily yield */
6226 mddev
->curr_resync
= 1;
6227 wake_up(&resync_wait
);
6229 if (mddev
> mddev2
&& mddev
->curr_resync
== 1)
6230 /* no need to wait here, we can wait the next
6231 * time 'round when curr_resync == 2
6234 /* We need to wait 'interruptible' so as not to
6235 * contribute to the load average, and not to
6236 * be caught by 'softlockup'
6238 prepare_to_wait(&resync_wait
, &wq
, TASK_INTERRUPTIBLE
);
6239 if (!kthread_should_stop() &&
6240 mddev2
->curr_resync
>= mddev
->curr_resync
) {
6241 printk(KERN_INFO
"md: delaying %s of %s"
6242 " until %s has finished (they"
6243 " share one or more physical units)\n",
6244 desc
, mdname(mddev
), mdname(mddev2
));
6246 if (signal_pending(current
))
6247 flush_signals(current
);
6249 finish_wait(&resync_wait
, &wq
);
6252 finish_wait(&resync_wait
, &wq
);
6255 } while (mddev
->curr_resync
< 2);
6258 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
6259 /* resync follows the size requested by the personality,
6260 * which defaults to physical size, but can be virtual size
6262 max_sectors
= mddev
->resync_max_sectors
;
6263 mddev
->resync_mismatches
= 0;
6264 /* we don't use the checkpoint if there's a bitmap */
6265 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
6266 j
= mddev
->resync_min
;
6267 else if (!mddev
->bitmap
)
6268 j
= mddev
->recovery_cp
;
6270 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
6271 max_sectors
= mddev
->dev_sectors
;
6273 /* recovery follows the physical size of devices */
6274 max_sectors
= mddev
->dev_sectors
;
6276 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
6277 if (rdev
->raid_disk
>= 0 &&
6278 !test_bit(Faulty
, &rdev
->flags
) &&
6279 !test_bit(In_sync
, &rdev
->flags
) &&
6280 rdev
->recovery_offset
< j
)
6281 j
= rdev
->recovery_offset
;
6284 printk(KERN_INFO
"md: %s of RAID array %s\n", desc
, mdname(mddev
));
6285 printk(KERN_INFO
"md: minimum _guaranteed_ speed:"
6286 " %d KB/sec/disk.\n", speed_min(mddev
));
6287 printk(KERN_INFO
"md: using maximum available idle IO bandwidth "
6288 "(but not more than %d KB/sec) for %s.\n",
6289 speed_max(mddev
), desc
);
6291 is_mddev_idle(mddev
, 1); /* this initializes IO event counters */
6294 for (m
= 0; m
< SYNC_MARKS
; m
++) {
6296 mark_cnt
[m
] = io_sectors
;
6299 mddev
->resync_mark
= mark
[last_mark
];
6300 mddev
->resync_mark_cnt
= mark_cnt
[last_mark
];
6303 * Tune reconstruction:
6305 window
= 32*(PAGE_SIZE
/512);
6306 printk(KERN_INFO
"md: using %dk window, over a total of %llu blocks.\n",
6307 window
/2,(unsigned long long) max_sectors
/2);
6309 atomic_set(&mddev
->recovery_active
, 0);
6314 "md: resuming %s of %s from checkpoint.\n",
6315 desc
, mdname(mddev
));
6316 mddev
->curr_resync
= j
;
6319 while (j
< max_sectors
) {
6324 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
6325 ((mddev
->curr_resync
> mddev
->curr_resync_completed
&&
6326 (mddev
->curr_resync
- mddev
->curr_resync_completed
)
6327 > (max_sectors
>> 4)) ||
6328 (j
- mddev
->curr_resync_completed
)*2
6329 >= mddev
->resync_max
- mddev
->curr_resync_completed
6331 /* time to update curr_resync_completed */
6332 blk_unplug(mddev
->queue
);
6333 wait_event(mddev
->recovery_wait
,
6334 atomic_read(&mddev
->recovery_active
) == 0);
6335 mddev
->curr_resync_completed
=
6337 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6338 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
6341 while (j
>= mddev
->resync_max
&& !kthread_should_stop()) {
6342 /* As this condition is controlled by user-space,
6343 * we can block indefinitely, so use '_interruptible'
6344 * to avoid triggering warnings.
6346 flush_signals(current
); /* just in case */
6347 wait_event_interruptible(mddev
->recovery_wait
,
6348 mddev
->resync_max
> j
6349 || kthread_should_stop());
6352 if (kthread_should_stop())
6355 sectors
= mddev
->pers
->sync_request(mddev
, j
, &skipped
,
6356 currspeed
< speed_min(mddev
));
6358 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6362 if (!skipped
) { /* actual IO requested */
6363 io_sectors
+= sectors
;
6364 atomic_add(sectors
, &mddev
->recovery_active
);
6368 if (j
>1) mddev
->curr_resync
= j
;
6369 mddev
->curr_mark_cnt
= io_sectors
;
6370 if (last_check
== 0)
6371 /* this is the earliers that rebuilt will be
6372 * visible in /proc/mdstat
6374 md_new_event(mddev
);
6376 if (last_check
+ window
> io_sectors
|| j
== max_sectors
)
6379 last_check
= io_sectors
;
6381 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
6385 if (time_after_eq(jiffies
, mark
[last_mark
] + SYNC_MARK_STEP
)) {
6387 int next
= (last_mark
+1) % SYNC_MARKS
;
6389 mddev
->resync_mark
= mark
[next
];
6390 mddev
->resync_mark_cnt
= mark_cnt
[next
];
6391 mark
[next
] = jiffies
;
6392 mark_cnt
[next
] = io_sectors
- atomic_read(&mddev
->recovery_active
);
6397 if (kthread_should_stop())
6402 * this loop exits only if either when we are slower than
6403 * the 'hard' speed limit, or the system was IO-idle for
6405 * the system might be non-idle CPU-wise, but we only care
6406 * about not overloading the IO subsystem. (things like an
6407 * e2fsck being done on the RAID array should execute fast)
6409 blk_unplug(mddev
->queue
);
6412 currspeed
= ((unsigned long)(io_sectors
-mddev
->resync_mark_cnt
))/2
6413 /((jiffies
-mddev
->resync_mark
)/HZ
+1) +1;
6415 if (currspeed
> speed_min(mddev
)) {
6416 if ((currspeed
> speed_max(mddev
)) ||
6417 !is_mddev_idle(mddev
, 0)) {
6423 printk(KERN_INFO
"md: %s: %s done.\n",mdname(mddev
), desc
);
6425 * this also signals 'finished resyncing' to md_stop
6428 blk_unplug(mddev
->queue
);
6430 wait_event(mddev
->recovery_wait
, !atomic_read(&mddev
->recovery_active
));
6432 /* tell personality that we are finished */
6433 mddev
->pers
->sync_request(mddev
, max_sectors
, &skipped
, 1);
6435 if (!test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
) &&
6436 mddev
->curr_resync
> 2) {
6437 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
6438 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
6439 if (mddev
->curr_resync
>= mddev
->recovery_cp
) {
6441 "md: checkpointing %s of %s.\n",
6442 desc
, mdname(mddev
));
6443 mddev
->recovery_cp
= mddev
->curr_resync
;
6446 mddev
->recovery_cp
= MaxSector
;
6448 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
6449 mddev
->curr_resync
= MaxSector
;
6450 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
6451 if (rdev
->raid_disk
>= 0 &&
6452 !test_bit(Faulty
, &rdev
->flags
) &&
6453 !test_bit(In_sync
, &rdev
->flags
) &&
6454 rdev
->recovery_offset
< mddev
->curr_resync
)
6455 rdev
->recovery_offset
= mddev
->curr_resync
;
6458 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
6461 mddev
->curr_resync
= 0;
6462 mddev
->curr_resync_completed
= 0;
6463 mddev
->resync_min
= 0;
6464 mddev
->resync_max
= MaxSector
;
6465 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
6466 wake_up(&resync_wait
);
6467 set_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
6468 md_wakeup_thread(mddev
->thread
);
6473 * got a signal, exit.
6476 "md: md_do_sync() got signal ... exiting\n");
6477 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6481 EXPORT_SYMBOL_GPL(md_do_sync
);
6484 static int remove_and_add_spares(mddev_t
*mddev
)
6489 mddev
->curr_resync_completed
= 0;
6491 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
6492 if (rdev
->raid_disk
>= 0 &&
6493 !test_bit(Blocked
, &rdev
->flags
) &&
6494 (test_bit(Faulty
, &rdev
->flags
) ||
6495 ! test_bit(In_sync
, &rdev
->flags
)) &&
6496 atomic_read(&rdev
->nr_pending
)==0) {
6497 if (mddev
->pers
->hot_remove_disk(
6498 mddev
, rdev
->raid_disk
)==0) {
6500 sprintf(nm
,"rd%d", rdev
->raid_disk
);
6501 sysfs_remove_link(&mddev
->kobj
, nm
);
6502 rdev
->raid_disk
= -1;
6506 if (mddev
->degraded
&& ! mddev
->ro
&& !mddev
->recovery_disabled
) {
6507 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
6508 if (rdev
->raid_disk
>= 0 &&
6509 !test_bit(In_sync
, &rdev
->flags
) &&
6510 !test_bit(Blocked
, &rdev
->flags
))
6512 if (rdev
->raid_disk
< 0
6513 && !test_bit(Faulty
, &rdev
->flags
)) {
6514 rdev
->recovery_offset
= 0;
6516 hot_add_disk(mddev
, rdev
) == 0) {
6518 sprintf(nm
, "rd%d", rdev
->raid_disk
);
6519 if (sysfs_create_link(&mddev
->kobj
,
6522 "md: cannot register "
6526 md_new_event(mddev
);
6535 * This routine is regularly called by all per-raid-array threads to
6536 * deal with generic issues like resync and super-block update.
6537 * Raid personalities that don't have a thread (linear/raid0) do not
6538 * need this as they never do any recovery or update the superblock.
6540 * It does not do any resync itself, but rather "forks" off other threads
6541 * to do that as needed.
6542 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6543 * "->recovery" and create a thread at ->sync_thread.
6544 * When the thread finishes it sets MD_RECOVERY_DONE
6545 * and wakeups up this thread which will reap the thread and finish up.
6546 * This thread also removes any faulty devices (with nr_pending == 0).
6548 * The overall approach is:
6549 * 1/ if the superblock needs updating, update it.
6550 * 2/ If a recovery thread is running, don't do anything else.
6551 * 3/ If recovery has finished, clean up, possibly marking spares active.
6552 * 4/ If there are any faulty devices, remove them.
6553 * 5/ If array is degraded, try to add spares devices
6554 * 6/ If array has spares or is not in-sync, start a resync thread.
6556 void md_check_recovery(mddev_t
*mddev
)
6562 bitmap_daemon_work(mddev
->bitmap
);
6567 if (signal_pending(current
)) {
6568 if (mddev
->pers
->sync_request
&& !mddev
->external
) {
6569 printk(KERN_INFO
"md: %s in immediate safe mode\n",
6571 mddev
->safemode
= 2;
6573 flush_signals(current
);
6576 if (mddev
->ro
&& !test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
6579 (mddev
->flags
&& !mddev
->external
) ||
6580 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
6581 test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
6582 (mddev
->external
== 0 && mddev
->safemode
== 1) ||
6583 (mddev
->safemode
== 2 && ! atomic_read(&mddev
->writes_pending
)
6584 && !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
6588 if (mddev_trylock(mddev
)) {
6592 /* Only thing we do on a ro array is remove
6595 remove_and_add_spares(mddev
);
6596 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6600 if (!mddev
->external
) {
6602 spin_lock_irq(&mddev
->write_lock
);
6603 if (mddev
->safemode
&&
6604 !atomic_read(&mddev
->writes_pending
) &&
6606 mddev
->recovery_cp
== MaxSector
) {
6609 if (mddev
->persistent
)
6610 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6612 if (mddev
->safemode
== 1)
6613 mddev
->safemode
= 0;
6614 spin_unlock_irq(&mddev
->write_lock
);
6616 sysfs_notify_dirent(mddev
->sysfs_state
);
6620 md_update_sb(mddev
, 0);
6622 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
6623 if (test_and_clear_bit(StateChanged
, &rdev
->flags
))
6624 sysfs_notify_dirent(rdev
->sysfs_state
);
6627 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
6628 !test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
)) {
6629 /* resync/recovery still happening */
6630 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6633 if (mddev
->sync_thread
) {
6634 /* resync has finished, collect result */
6635 md_unregister_thread(mddev
->sync_thread
);
6636 mddev
->sync_thread
= NULL
;
6637 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
6638 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
6640 /* activate any spares */
6641 if (mddev
->pers
->spare_active(mddev
))
6642 sysfs_notify(&mddev
->kobj
, NULL
,
6645 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
6646 mddev
->pers
->finish_reshape
)
6647 mddev
->pers
->finish_reshape(mddev
);
6648 md_update_sb(mddev
, 1);
6650 /* if array is no-longer degraded, then any saved_raid_disk
6651 * information must be scrapped
6653 if (!mddev
->degraded
)
6654 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
6655 rdev
->saved_raid_disk
= -1;
6657 mddev
->recovery
= 0;
6658 /* flag recovery needed just to double check */
6659 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6660 sysfs_notify_dirent(mddev
->sysfs_action
);
6661 md_new_event(mddev
);
6664 /* Set RUNNING before clearing NEEDED to avoid
6665 * any transients in the value of "sync_action".
6667 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
6668 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6669 /* Clear some bits that don't mean anything, but
6672 clear_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6673 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
6675 if (test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
6677 /* no recovery is running.
6678 * remove any failed drives, then
6679 * add spares if possible.
6680 * Spare are also removed and re-added, to allow
6681 * the personality to fail the re-add.
6684 if (mddev
->reshape_position
!= MaxSector
) {
6685 if (mddev
->pers
->check_reshape
== NULL
||
6686 mddev
->pers
->check_reshape(mddev
) != 0)
6687 /* Cannot proceed */
6689 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
6690 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
6691 } else if ((spares
= remove_and_add_spares(mddev
))) {
6692 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
6693 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
6694 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
6695 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
6696 } else if (mddev
->recovery_cp
< MaxSector
) {
6697 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
6698 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
6699 } else if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
6700 /* nothing to be done ... */
6703 if (mddev
->pers
->sync_request
) {
6704 if (spares
&& mddev
->bitmap
&& ! mddev
->bitmap
->file
) {
6705 /* We are adding a device or devices to an array
6706 * which has the bitmap stored on all devices.
6707 * So make sure all bitmap pages get written
6709 bitmap_write_all(mddev
->bitmap
);
6711 mddev
->sync_thread
= md_register_thread(md_do_sync
,
6714 if (!mddev
->sync_thread
) {
6715 printk(KERN_ERR
"%s: could not start resync"
6718 /* leave the spares where they are, it shouldn't hurt */
6719 mddev
->recovery
= 0;
6721 md_wakeup_thread(mddev
->sync_thread
);
6722 sysfs_notify_dirent(mddev
->sysfs_action
);
6723 md_new_event(mddev
);
6726 if (!mddev
->sync_thread
) {
6727 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
6728 if (test_and_clear_bit(MD_RECOVERY_RECOVER
,
6730 if (mddev
->sysfs_action
)
6731 sysfs_notify_dirent(mddev
->sysfs_action
);
6733 mddev_unlock(mddev
);
6737 void md_wait_for_blocked_rdev(mdk_rdev_t
*rdev
, mddev_t
*mddev
)
6739 sysfs_notify_dirent(rdev
->sysfs_state
);
6740 wait_event_timeout(rdev
->blocked_wait
,
6741 !test_bit(Blocked
, &rdev
->flags
),
6742 msecs_to_jiffies(5000));
6743 rdev_dec_pending(rdev
, mddev
);
6745 EXPORT_SYMBOL(md_wait_for_blocked_rdev
);
6747 static int md_notify_reboot(struct notifier_block
*this,
6748 unsigned long code
, void *x
)
6750 struct list_head
*tmp
;
6753 if ((code
== SYS_DOWN
) || (code
== SYS_HALT
) || (code
== SYS_POWER_OFF
)) {
6755 printk(KERN_INFO
"md: stopping all md devices.\n");
6757 for_each_mddev(mddev
, tmp
)
6758 if (mddev_trylock(mddev
)) {
6759 /* Force a switch to readonly even array
6760 * appears to still be in use. Hence
6763 do_md_stop(mddev
, 1, 100);
6764 mddev_unlock(mddev
);
6767 * certain more exotic SCSI devices are known to be
6768 * volatile wrt too early system reboots. While the
6769 * right place to handle this issue is the given
6770 * driver, we do want to have a safe RAID driver ...
6777 static struct notifier_block md_notifier
= {
6778 .notifier_call
= md_notify_reboot
,
6780 .priority
= INT_MAX
, /* before any real devices */
6783 static void md_geninit(void)
6785 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t
));
6787 proc_create("mdstat", S_IRUGO
, NULL
, &md_seq_fops
);
6790 static int __init
md_init(void)
6792 if (register_blkdev(MD_MAJOR
, "md"))
6794 if ((mdp_major
=register_blkdev(0, "mdp"))<=0) {
6795 unregister_blkdev(MD_MAJOR
, "md");
6798 blk_register_region(MKDEV(MD_MAJOR
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
6799 md_probe
, NULL
, NULL
);
6800 blk_register_region(MKDEV(mdp_major
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
6801 md_probe
, NULL
, NULL
);
6803 register_reboot_notifier(&md_notifier
);
6804 raid_table_header
= register_sysctl_table(raid_root_table
);
6814 * Searches all registered partitions for autorun RAID arrays
6818 static LIST_HEAD(all_detected_devices
);
6819 struct detected_devices_node
{
6820 struct list_head list
;
6824 void md_autodetect_dev(dev_t dev
)
6826 struct detected_devices_node
*node_detected_dev
;
6828 node_detected_dev
= kzalloc(sizeof(*node_detected_dev
), GFP_KERNEL
);
6829 if (node_detected_dev
) {
6830 node_detected_dev
->dev
= dev
;
6831 list_add_tail(&node_detected_dev
->list
, &all_detected_devices
);
6833 printk(KERN_CRIT
"md: md_autodetect_dev: kzalloc failed"
6834 ", skipping dev(%d,%d)\n", MAJOR(dev
), MINOR(dev
));
6839 static void autostart_arrays(int part
)
6842 struct detected_devices_node
*node_detected_dev
;
6844 int i_scanned
, i_passed
;
6849 printk(KERN_INFO
"md: Autodetecting RAID arrays.\n");
6851 while (!list_empty(&all_detected_devices
) && i_scanned
< INT_MAX
) {
6853 node_detected_dev
= list_entry(all_detected_devices
.next
,
6854 struct detected_devices_node
, list
);
6855 list_del(&node_detected_dev
->list
);
6856 dev
= node_detected_dev
->dev
;
6857 kfree(node_detected_dev
);
6858 rdev
= md_import_device(dev
,0, 90);
6862 if (test_bit(Faulty
, &rdev
->flags
)) {
6866 set_bit(AutoDetected
, &rdev
->flags
);
6867 list_add(&rdev
->same_set
, &pending_raid_disks
);
6871 printk(KERN_INFO
"md: Scanned %d and added %d devices.\n",
6872 i_scanned
, i_passed
);
6874 autorun_devices(part
);
6877 #endif /* !MODULE */
6879 static __exit
void md_exit(void)
6882 struct list_head
*tmp
;
6884 blk_unregister_region(MKDEV(MD_MAJOR
,0), 1U << MINORBITS
);
6885 blk_unregister_region(MKDEV(mdp_major
,0), 1U << MINORBITS
);
6887 unregister_blkdev(MD_MAJOR
,"md");
6888 unregister_blkdev(mdp_major
, "mdp");
6889 unregister_reboot_notifier(&md_notifier
);
6890 unregister_sysctl_table(raid_table_header
);
6891 remove_proc_entry("mdstat", NULL
);
6892 for_each_mddev(mddev
, tmp
) {
6893 export_array(mddev
);
6894 mddev
->hold_active
= 0;
6898 subsys_initcall(md_init
);
6899 module_exit(md_exit
)
6901 static int get_ro(char *buffer
, struct kernel_param
*kp
)
6903 return sprintf(buffer
, "%d", start_readonly
);
6905 static int set_ro(const char *val
, struct kernel_param
*kp
)
6908 int num
= simple_strtoul(val
, &e
, 10);
6909 if (*val
&& (*e
== '\0' || *e
== '\n')) {
6910 start_readonly
= num
;
6916 module_param_call(start_ro
, set_ro
, get_ro
, NULL
, S_IRUSR
|S_IWUSR
);
6917 module_param(start_dirty_degraded
, int, S_IRUGO
|S_IWUSR
);
6919 module_param_call(new_array
, add_named_array
, NULL
, NULL
, S_IWUSR
);
6921 EXPORT_SYMBOL(register_md_personality
);
6922 EXPORT_SYMBOL(unregister_md_personality
);
6923 EXPORT_SYMBOL(md_error
);
6924 EXPORT_SYMBOL(md_done_sync
);
6925 EXPORT_SYMBOL(md_write_start
);
6926 EXPORT_SYMBOL(md_write_end
);
6927 EXPORT_SYMBOL(md_register_thread
);
6928 EXPORT_SYMBOL(md_unregister_thread
);
6929 EXPORT_SYMBOL(md_wakeup_thread
);
6930 EXPORT_SYMBOL(md_check_recovery
);
6931 MODULE_LICENSE("GPL");
6933 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR
);