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/module.h>
36 #include <linux/kernel.h>
37 #include <linux/kthread.h>
38 #include <linux/linkage.h>
39 #include <linux/raid/md.h>
40 #include <linux/raid/bitmap.h>
41 #include <linux/sysctl.h>
42 #include <linux/buffer_head.h> /* for invalidate_bdev */
43 #include <linux/poll.h>
44 #include <linux/mutex.h>
45 #include <linux/ctype.h>
46 #include <linux/freezer.h>
48 #include <linux/init.h>
50 #include <linux/file.h>
53 #include <linux/kmod.h>
56 #include <asm/unaligned.h>
58 #define MAJOR_NR MD_MAJOR
61 /* 63 partitions with the alternate major number (mdp) */
62 #define MdpMinorShift 6
65 #define dprintk(x...) ((void)(DEBUG && printk(x)))
69 static void autostart_arrays (int part
);
72 static LIST_HEAD(pers_list
);
73 static DEFINE_SPINLOCK(pers_lock
);
75 static void md_print_devices(void);
77 #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
80 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
81 * is 1000 KB/sec, so the extra system load does not show up that much.
82 * Increase it if you want to have more _guaranteed_ speed. Note that
83 * the RAID driver will use the maximum available bandwidth if the IO
84 * subsystem is idle. There is also an 'absolute maximum' reconstruction
85 * speed limit - in case reconstruction slows down your system despite
88 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
89 * or /sys/block/mdX/md/sync_speed_{min,max}
92 static int sysctl_speed_limit_min
= 1000;
93 static int sysctl_speed_limit_max
= 200000;
94 static inline int speed_min(mddev_t
*mddev
)
96 return mddev
->sync_speed_min
?
97 mddev
->sync_speed_min
: sysctl_speed_limit_min
;
100 static inline int speed_max(mddev_t
*mddev
)
102 return mddev
->sync_speed_max
?
103 mddev
->sync_speed_max
: sysctl_speed_limit_max
;
106 static struct ctl_table_header
*raid_table_header
;
108 static ctl_table raid_table
[] = {
110 .ctl_name
= DEV_RAID_SPEED_LIMIT_MIN
,
111 .procname
= "speed_limit_min",
112 .data
= &sysctl_speed_limit_min
,
113 .maxlen
= sizeof(int),
114 .mode
= S_IRUGO
|S_IWUSR
,
115 .proc_handler
= &proc_dointvec
,
118 .ctl_name
= DEV_RAID_SPEED_LIMIT_MAX
,
119 .procname
= "speed_limit_max",
120 .data
= &sysctl_speed_limit_max
,
121 .maxlen
= sizeof(int),
122 .mode
= S_IRUGO
|S_IWUSR
,
123 .proc_handler
= &proc_dointvec
,
128 static ctl_table raid_dir_table
[] = {
130 .ctl_name
= DEV_RAID
,
133 .mode
= S_IRUGO
|S_IXUGO
,
139 static ctl_table raid_root_table
[] = {
145 .child
= raid_dir_table
,
150 static struct block_device_operations md_fops
;
152 static int start_readonly
;
155 * We have a system wide 'event count' that is incremented
156 * on any 'interesting' event, and readers of /proc/mdstat
157 * can use 'poll' or 'select' to find out when the event
161 * start array, stop array, error, add device, remove device,
162 * start build, activate spare
164 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters
);
165 static atomic_t md_event_count
;
166 void md_new_event(mddev_t
*mddev
)
168 atomic_inc(&md_event_count
);
169 wake_up(&md_event_waiters
);
170 sysfs_notify(&mddev
->kobj
, NULL
, "sync_action");
172 EXPORT_SYMBOL_GPL(md_new_event
);
174 /* Alternate version that can be called from interrupts
175 * when calling sysfs_notify isn't needed.
177 static void md_new_event_inintr(mddev_t
*mddev
)
179 atomic_inc(&md_event_count
);
180 wake_up(&md_event_waiters
);
184 * Enables to iterate over all existing md arrays
185 * all_mddevs_lock protects this list.
187 static LIST_HEAD(all_mddevs
);
188 static DEFINE_SPINLOCK(all_mddevs_lock
);
192 * iterates through all used mddevs in the system.
193 * We take care to grab the all_mddevs_lock whenever navigating
194 * the list, and to always hold a refcount when unlocked.
195 * Any code which breaks out of this loop while own
196 * a reference to the current mddev and must mddev_put it.
198 #define for_each_mddev(mddev,tmp) \
200 for (({ spin_lock(&all_mddevs_lock); \
201 tmp = all_mddevs.next; \
203 ({ if (tmp != &all_mddevs) \
204 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
205 spin_unlock(&all_mddevs_lock); \
206 if (mddev) mddev_put(mddev); \
207 mddev = list_entry(tmp, mddev_t, all_mddevs); \
208 tmp != &all_mddevs;}); \
209 ({ spin_lock(&all_mddevs_lock); \
214 static int md_fail_request (struct request_queue
*q
, struct bio
*bio
)
220 static inline mddev_t
*mddev_get(mddev_t
*mddev
)
222 atomic_inc(&mddev
->active
);
226 static void mddev_put(mddev_t
*mddev
)
228 if (!atomic_dec_and_lock(&mddev
->active
, &all_mddevs_lock
))
230 if (!mddev
->raid_disks
&& list_empty(&mddev
->disks
)) {
231 list_del(&mddev
->all_mddevs
);
232 spin_unlock(&all_mddevs_lock
);
233 blk_cleanup_queue(mddev
->queue
);
234 kobject_put(&mddev
->kobj
);
236 spin_unlock(&all_mddevs_lock
);
239 static mddev_t
* mddev_find(dev_t unit
)
241 mddev_t
*mddev
, *new = NULL
;
244 spin_lock(&all_mddevs_lock
);
245 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
246 if (mddev
->unit
== unit
) {
248 spin_unlock(&all_mddevs_lock
);
254 list_add(&new->all_mddevs
, &all_mddevs
);
255 spin_unlock(&all_mddevs_lock
);
258 spin_unlock(&all_mddevs_lock
);
260 new = kzalloc(sizeof(*new), GFP_KERNEL
);
265 if (MAJOR(unit
) == MD_MAJOR
)
266 new->md_minor
= MINOR(unit
);
268 new->md_minor
= MINOR(unit
) >> MdpMinorShift
;
270 mutex_init(&new->reconfig_mutex
);
271 INIT_LIST_HEAD(&new->disks
);
272 INIT_LIST_HEAD(&new->all_mddevs
);
273 init_timer(&new->safemode_timer
);
274 atomic_set(&new->active
, 1);
275 spin_lock_init(&new->write_lock
);
276 init_waitqueue_head(&new->sb_wait
);
277 new->reshape_position
= MaxSector
;
278 new->resync_max
= MaxSector
;
280 new->queue
= blk_alloc_queue(GFP_KERNEL
);
285 set_bit(QUEUE_FLAG_CLUSTER
, &new->queue
->queue_flags
);
287 blk_queue_make_request(new->queue
, md_fail_request
);
292 static inline int mddev_lock(mddev_t
* mddev
)
294 return mutex_lock_interruptible(&mddev
->reconfig_mutex
);
297 static inline int mddev_trylock(mddev_t
* mddev
)
299 return mutex_trylock(&mddev
->reconfig_mutex
);
302 static inline void mddev_unlock(mddev_t
* mddev
)
304 mutex_unlock(&mddev
->reconfig_mutex
);
306 md_wakeup_thread(mddev
->thread
);
309 static mdk_rdev_t
* find_rdev_nr(mddev_t
*mddev
, int nr
)
312 struct list_head
*tmp
;
314 rdev_for_each(rdev
, tmp
, mddev
) {
315 if (rdev
->desc_nr
== nr
)
321 static mdk_rdev_t
* find_rdev(mddev_t
* mddev
, dev_t dev
)
323 struct list_head
*tmp
;
326 rdev_for_each(rdev
, tmp
, mddev
) {
327 if (rdev
->bdev
->bd_dev
== dev
)
333 static struct mdk_personality
*find_pers(int level
, char *clevel
)
335 struct mdk_personality
*pers
;
336 list_for_each_entry(pers
, &pers_list
, list
) {
337 if (level
!= LEVEL_NONE
&& pers
->level
== level
)
339 if (strcmp(pers
->name
, clevel
)==0)
345 static inline sector_t
calc_dev_sboffset(struct block_device
*bdev
)
347 sector_t size
= bdev
->bd_inode
->i_size
>> BLOCK_SIZE_BITS
;
348 return MD_NEW_SIZE_BLOCKS(size
);
351 static sector_t
calc_dev_size(mdk_rdev_t
*rdev
, unsigned chunk_size
)
355 size
= rdev
->sb_offset
;
358 size
&= ~((sector_t
)chunk_size
/1024 - 1);
362 static int alloc_disk_sb(mdk_rdev_t
* rdev
)
367 rdev
->sb_page
= alloc_page(GFP_KERNEL
);
368 if (!rdev
->sb_page
) {
369 printk(KERN_ALERT
"md: out of memory.\n");
376 static void free_disk_sb(mdk_rdev_t
* rdev
)
379 put_page(rdev
->sb_page
);
381 rdev
->sb_page
= NULL
;
388 static void super_written(struct bio
*bio
, int error
)
390 mdk_rdev_t
*rdev
= bio
->bi_private
;
391 mddev_t
*mddev
= rdev
->mddev
;
393 if (error
|| !test_bit(BIO_UPTODATE
, &bio
->bi_flags
)) {
394 printk("md: super_written gets error=%d, uptodate=%d\n",
395 error
, test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
396 WARN_ON(test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
397 md_error(mddev
, rdev
);
400 if (atomic_dec_and_test(&mddev
->pending_writes
))
401 wake_up(&mddev
->sb_wait
);
405 static void super_written_barrier(struct bio
*bio
, int error
)
407 struct bio
*bio2
= bio
->bi_private
;
408 mdk_rdev_t
*rdev
= bio2
->bi_private
;
409 mddev_t
*mddev
= rdev
->mddev
;
411 if (!test_bit(BIO_UPTODATE
, &bio
->bi_flags
) &&
412 error
== -EOPNOTSUPP
) {
414 /* barriers don't appear to be supported :-( */
415 set_bit(BarriersNotsupp
, &rdev
->flags
);
416 mddev
->barriers_work
= 0;
417 spin_lock_irqsave(&mddev
->write_lock
, flags
);
418 bio2
->bi_next
= mddev
->biolist
;
419 mddev
->biolist
= bio2
;
420 spin_unlock_irqrestore(&mddev
->write_lock
, flags
);
421 wake_up(&mddev
->sb_wait
);
425 bio
->bi_private
= rdev
;
426 super_written(bio
, error
);
430 void md_super_write(mddev_t
*mddev
, mdk_rdev_t
*rdev
,
431 sector_t sector
, int size
, struct page
*page
)
433 /* write first size bytes of page to sector of rdev
434 * Increment mddev->pending_writes before returning
435 * and decrement it on completion, waking up sb_wait
436 * if zero is reached.
437 * If an error occurred, call md_error
439 * As we might need to resubmit the request if BIO_RW_BARRIER
440 * causes ENOTSUPP, we allocate a spare bio...
442 struct bio
*bio
= bio_alloc(GFP_NOIO
, 1);
443 int rw
= (1<<BIO_RW
) | (1<<BIO_RW_SYNC
);
445 bio
->bi_bdev
= rdev
->bdev
;
446 bio
->bi_sector
= sector
;
447 bio_add_page(bio
, page
, size
, 0);
448 bio
->bi_private
= rdev
;
449 bio
->bi_end_io
= super_written
;
452 atomic_inc(&mddev
->pending_writes
);
453 if (!test_bit(BarriersNotsupp
, &rdev
->flags
)) {
455 rw
|= (1<<BIO_RW_BARRIER
);
456 rbio
= bio_clone(bio
, GFP_NOIO
);
457 rbio
->bi_private
= bio
;
458 rbio
->bi_end_io
= super_written_barrier
;
459 submit_bio(rw
, rbio
);
464 void md_super_wait(mddev_t
*mddev
)
466 /* wait for all superblock writes that were scheduled to complete.
467 * if any had to be retried (due to BARRIER problems), retry them
471 prepare_to_wait(&mddev
->sb_wait
, &wq
, TASK_UNINTERRUPTIBLE
);
472 if (atomic_read(&mddev
->pending_writes
)==0)
474 while (mddev
->biolist
) {
476 spin_lock_irq(&mddev
->write_lock
);
477 bio
= mddev
->biolist
;
478 mddev
->biolist
= bio
->bi_next
;
480 spin_unlock_irq(&mddev
->write_lock
);
481 submit_bio(bio
->bi_rw
, bio
);
485 finish_wait(&mddev
->sb_wait
, &wq
);
488 static void bi_complete(struct bio
*bio
, int error
)
490 complete((struct completion
*)bio
->bi_private
);
493 int sync_page_io(struct block_device
*bdev
, sector_t sector
, int size
,
494 struct page
*page
, int rw
)
496 struct bio
*bio
= bio_alloc(GFP_NOIO
, 1);
497 struct completion event
;
500 rw
|= (1 << BIO_RW_SYNC
);
503 bio
->bi_sector
= sector
;
504 bio_add_page(bio
, page
, size
, 0);
505 init_completion(&event
);
506 bio
->bi_private
= &event
;
507 bio
->bi_end_io
= bi_complete
;
509 wait_for_completion(&event
);
511 ret
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
515 EXPORT_SYMBOL_GPL(sync_page_io
);
517 static int read_disk_sb(mdk_rdev_t
* rdev
, int size
)
519 char b
[BDEVNAME_SIZE
];
520 if (!rdev
->sb_page
) {
528 if (!sync_page_io(rdev
->bdev
, rdev
->sb_offset
<<1, size
, rdev
->sb_page
, READ
))
534 printk(KERN_WARNING
"md: disabled device %s, could not read superblock.\n",
535 bdevname(rdev
->bdev
,b
));
539 static int uuid_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
541 if ( (sb1
->set_uuid0
== sb2
->set_uuid0
) &&
542 (sb1
->set_uuid1
== sb2
->set_uuid1
) &&
543 (sb1
->set_uuid2
== sb2
->set_uuid2
) &&
544 (sb1
->set_uuid3
== sb2
->set_uuid3
))
552 static int sb_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
555 mdp_super_t
*tmp1
, *tmp2
;
557 tmp1
= kmalloc(sizeof(*tmp1
),GFP_KERNEL
);
558 tmp2
= kmalloc(sizeof(*tmp2
),GFP_KERNEL
);
560 if (!tmp1
|| !tmp2
) {
562 printk(KERN_INFO
"md.c: sb1 is not equal to sb2!\n");
570 * nr_disks is not constant
575 if (memcmp(tmp1
, tmp2
, MD_SB_GENERIC_CONSTANT_WORDS
* 4))
587 static u32
md_csum_fold(u32 csum
)
589 csum
= (csum
& 0xffff) + (csum
>> 16);
590 return (csum
& 0xffff) + (csum
>> 16);
593 static unsigned int calc_sb_csum(mdp_super_t
* sb
)
596 u32
*sb32
= (u32
*)sb
;
598 unsigned int disk_csum
, csum
;
600 disk_csum
= sb
->sb_csum
;
603 for (i
= 0; i
< MD_SB_BYTES
/4 ; i
++)
605 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
609 /* This used to use csum_partial, which was wrong for several
610 * reasons including that different results are returned on
611 * different architectures. It isn't critical that we get exactly
612 * the same return value as before (we always csum_fold before
613 * testing, and that removes any differences). However as we
614 * know that csum_partial always returned a 16bit value on
615 * alphas, do a fold to maximise conformity to previous behaviour.
617 sb
->sb_csum
= md_csum_fold(disk_csum
);
619 sb
->sb_csum
= disk_csum
;
626 * Handle superblock details.
627 * We want to be able to handle multiple superblock formats
628 * so we have a common interface to them all, and an array of
629 * different handlers.
630 * We rely on user-space to write the initial superblock, and support
631 * reading and updating of superblocks.
632 * Interface methods are:
633 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
634 * loads and validates a superblock on dev.
635 * if refdev != NULL, compare superblocks on both devices
637 * 0 - dev has a superblock that is compatible with refdev
638 * 1 - dev has a superblock that is compatible and newer than refdev
639 * so dev should be used as the refdev in future
640 * -EINVAL superblock incompatible or invalid
641 * -othererror e.g. -EIO
643 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
644 * Verify that dev is acceptable into mddev.
645 * The first time, mddev->raid_disks will be 0, and data from
646 * dev should be merged in. Subsequent calls check that dev
647 * is new enough. Return 0 or -EINVAL
649 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
650 * Update the superblock for rdev with data in mddev
651 * This does not write to disc.
657 struct module
*owner
;
658 int (*load_super
)(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
);
659 int (*validate_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
660 void (*sync_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
664 * load_super for 0.90.0
666 static int super_90_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
668 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
674 * Calculate the position of the superblock,
675 * it's at the end of the disk.
677 * It also happens to be a multiple of 4Kb.
679 sb_offset
= calc_dev_sboffset(rdev
->bdev
);
680 rdev
->sb_offset
= sb_offset
;
682 ret
= read_disk_sb(rdev
, MD_SB_BYTES
);
687 bdevname(rdev
->bdev
, b
);
688 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
690 if (sb
->md_magic
!= MD_SB_MAGIC
) {
691 printk(KERN_ERR
"md: invalid raid superblock magic on %s\n",
696 if (sb
->major_version
!= 0 ||
697 sb
->minor_version
< 90 ||
698 sb
->minor_version
> 91) {
699 printk(KERN_WARNING
"Bad version number %d.%d on %s\n",
700 sb
->major_version
, sb
->minor_version
,
705 if (sb
->raid_disks
<= 0)
708 if (md_csum_fold(calc_sb_csum(sb
)) != md_csum_fold(sb
->sb_csum
)) {
709 printk(KERN_WARNING
"md: invalid superblock checksum on %s\n",
714 rdev
->preferred_minor
= sb
->md_minor
;
715 rdev
->data_offset
= 0;
716 rdev
->sb_size
= MD_SB_BYTES
;
718 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
719 if (sb
->level
!= 1 && sb
->level
!= 4
720 && sb
->level
!= 5 && sb
->level
!= 6
721 && sb
->level
!= 10) {
722 /* FIXME use a better test */
724 "md: bitmaps not supported for this level.\n");
729 if (sb
->level
== LEVEL_MULTIPATH
)
732 rdev
->desc_nr
= sb
->this_disk
.number
;
738 mdp_super_t
*refsb
= (mdp_super_t
*)page_address(refdev
->sb_page
);
739 if (!uuid_equal(refsb
, sb
)) {
740 printk(KERN_WARNING
"md: %s has different UUID to %s\n",
741 b
, bdevname(refdev
->bdev
,b2
));
744 if (!sb_equal(refsb
, sb
)) {
745 printk(KERN_WARNING
"md: %s has same UUID"
746 " but different superblock to %s\n",
747 b
, bdevname(refdev
->bdev
, b2
));
751 ev2
= md_event(refsb
);
757 rdev
->size
= calc_dev_size(rdev
, sb
->chunk_size
);
759 if (rdev
->size
< sb
->size
&& sb
->level
> 1)
760 /* "this cannot possibly happen" ... */
768 * validate_super for 0.90.0
770 static int super_90_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
773 mdp_super_t
*sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
774 __u64 ev1
= md_event(sb
);
776 rdev
->raid_disk
= -1;
777 clear_bit(Faulty
, &rdev
->flags
);
778 clear_bit(In_sync
, &rdev
->flags
);
779 clear_bit(WriteMostly
, &rdev
->flags
);
780 clear_bit(BarriersNotsupp
, &rdev
->flags
);
782 if (mddev
->raid_disks
== 0) {
783 mddev
->major_version
= 0;
784 mddev
->minor_version
= sb
->minor_version
;
785 mddev
->patch_version
= sb
->patch_version
;
787 mddev
->chunk_size
= sb
->chunk_size
;
788 mddev
->ctime
= sb
->ctime
;
789 mddev
->utime
= sb
->utime
;
790 mddev
->level
= sb
->level
;
791 mddev
->clevel
[0] = 0;
792 mddev
->layout
= sb
->layout
;
793 mddev
->raid_disks
= sb
->raid_disks
;
794 mddev
->size
= sb
->size
;
796 mddev
->bitmap_offset
= 0;
797 mddev
->default_bitmap_offset
= MD_SB_BYTES
>> 9;
799 if (mddev
->minor_version
>= 91) {
800 mddev
->reshape_position
= sb
->reshape_position
;
801 mddev
->delta_disks
= sb
->delta_disks
;
802 mddev
->new_level
= sb
->new_level
;
803 mddev
->new_layout
= sb
->new_layout
;
804 mddev
->new_chunk
= sb
->new_chunk
;
806 mddev
->reshape_position
= MaxSector
;
807 mddev
->delta_disks
= 0;
808 mddev
->new_level
= mddev
->level
;
809 mddev
->new_layout
= mddev
->layout
;
810 mddev
->new_chunk
= mddev
->chunk_size
;
813 if (sb
->state
& (1<<MD_SB_CLEAN
))
814 mddev
->recovery_cp
= MaxSector
;
816 if (sb
->events_hi
== sb
->cp_events_hi
&&
817 sb
->events_lo
== sb
->cp_events_lo
) {
818 mddev
->recovery_cp
= sb
->recovery_cp
;
820 mddev
->recovery_cp
= 0;
823 memcpy(mddev
->uuid
+0, &sb
->set_uuid0
, 4);
824 memcpy(mddev
->uuid
+4, &sb
->set_uuid1
, 4);
825 memcpy(mddev
->uuid
+8, &sb
->set_uuid2
, 4);
826 memcpy(mddev
->uuid
+12,&sb
->set_uuid3
, 4);
828 mddev
->max_disks
= MD_SB_DISKS
;
830 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
) &&
831 mddev
->bitmap_file
== NULL
)
832 mddev
->bitmap_offset
= mddev
->default_bitmap_offset
;
834 } else if (mddev
->pers
== NULL
) {
835 /* Insist on good event counter while assembling */
837 if (ev1
< mddev
->events
)
839 } else if (mddev
->bitmap
) {
840 /* if adding to array with a bitmap, then we can accept an
841 * older device ... but not too old.
843 if (ev1
< mddev
->bitmap
->events_cleared
)
846 if (ev1
< mddev
->events
)
847 /* just a hot-add of a new device, leave raid_disk at -1 */
851 if (mddev
->level
!= LEVEL_MULTIPATH
) {
852 desc
= sb
->disks
+ rdev
->desc_nr
;
854 if (desc
->state
& (1<<MD_DISK_FAULTY
))
855 set_bit(Faulty
, &rdev
->flags
);
856 else if (desc
->state
& (1<<MD_DISK_SYNC
) /* &&
857 desc->raid_disk < mddev->raid_disks */) {
858 set_bit(In_sync
, &rdev
->flags
);
859 rdev
->raid_disk
= desc
->raid_disk
;
861 if (desc
->state
& (1<<MD_DISK_WRITEMOSTLY
))
862 set_bit(WriteMostly
, &rdev
->flags
);
863 } else /* MULTIPATH are always insync */
864 set_bit(In_sync
, &rdev
->flags
);
869 * sync_super for 0.90.0
871 static void super_90_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
874 struct list_head
*tmp
;
876 int next_spare
= mddev
->raid_disks
;
879 /* make rdev->sb match mddev data..
882 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
883 * 3/ any empty disks < next_spare become removed
885 * disks[0] gets initialised to REMOVED because
886 * we cannot be sure from other fields if it has
887 * been initialised or not.
890 int active
=0, working
=0,failed
=0,spare
=0,nr_disks
=0;
892 rdev
->sb_size
= MD_SB_BYTES
;
894 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
896 memset(sb
, 0, sizeof(*sb
));
898 sb
->md_magic
= MD_SB_MAGIC
;
899 sb
->major_version
= mddev
->major_version
;
900 sb
->patch_version
= mddev
->patch_version
;
901 sb
->gvalid_words
= 0; /* ignored */
902 memcpy(&sb
->set_uuid0
, mddev
->uuid
+0, 4);
903 memcpy(&sb
->set_uuid1
, mddev
->uuid
+4, 4);
904 memcpy(&sb
->set_uuid2
, mddev
->uuid
+8, 4);
905 memcpy(&sb
->set_uuid3
, mddev
->uuid
+12,4);
907 sb
->ctime
= mddev
->ctime
;
908 sb
->level
= mddev
->level
;
909 sb
->size
= mddev
->size
;
910 sb
->raid_disks
= mddev
->raid_disks
;
911 sb
->md_minor
= mddev
->md_minor
;
912 sb
->not_persistent
= 0;
913 sb
->utime
= mddev
->utime
;
915 sb
->events_hi
= (mddev
->events
>>32);
916 sb
->events_lo
= (u32
)mddev
->events
;
918 if (mddev
->reshape_position
== MaxSector
)
919 sb
->minor_version
= 90;
921 sb
->minor_version
= 91;
922 sb
->reshape_position
= mddev
->reshape_position
;
923 sb
->new_level
= mddev
->new_level
;
924 sb
->delta_disks
= mddev
->delta_disks
;
925 sb
->new_layout
= mddev
->new_layout
;
926 sb
->new_chunk
= mddev
->new_chunk
;
928 mddev
->minor_version
= sb
->minor_version
;
931 sb
->recovery_cp
= mddev
->recovery_cp
;
932 sb
->cp_events_hi
= (mddev
->events
>>32);
933 sb
->cp_events_lo
= (u32
)mddev
->events
;
934 if (mddev
->recovery_cp
== MaxSector
)
935 sb
->state
= (1<< MD_SB_CLEAN
);
939 sb
->layout
= mddev
->layout
;
940 sb
->chunk_size
= mddev
->chunk_size
;
942 if (mddev
->bitmap
&& mddev
->bitmap_file
== NULL
)
943 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
945 sb
->disks
[0].state
= (1<<MD_DISK_REMOVED
);
946 rdev_for_each(rdev2
, tmp
, mddev
) {
949 if (rdev2
->raid_disk
>= 0 && test_bit(In_sync
, &rdev2
->flags
)
950 && !test_bit(Faulty
, &rdev2
->flags
))
951 desc_nr
= rdev2
->raid_disk
;
953 desc_nr
= next_spare
++;
954 rdev2
->desc_nr
= desc_nr
;
955 d
= &sb
->disks
[rdev2
->desc_nr
];
957 d
->number
= rdev2
->desc_nr
;
958 d
->major
= MAJOR(rdev2
->bdev
->bd_dev
);
959 d
->minor
= MINOR(rdev2
->bdev
->bd_dev
);
960 if (rdev2
->raid_disk
>= 0 && test_bit(In_sync
, &rdev2
->flags
)
961 && !test_bit(Faulty
, &rdev2
->flags
))
962 d
->raid_disk
= rdev2
->raid_disk
;
964 d
->raid_disk
= rdev2
->desc_nr
; /* compatibility */
965 if (test_bit(Faulty
, &rdev2
->flags
))
966 d
->state
= (1<<MD_DISK_FAULTY
);
967 else if (test_bit(In_sync
, &rdev2
->flags
)) {
968 d
->state
= (1<<MD_DISK_ACTIVE
);
969 d
->state
|= (1<<MD_DISK_SYNC
);
977 if (test_bit(WriteMostly
, &rdev2
->flags
))
978 d
->state
|= (1<<MD_DISK_WRITEMOSTLY
);
980 /* now set the "removed" and "faulty" bits on any missing devices */
981 for (i
=0 ; i
< mddev
->raid_disks
; i
++) {
982 mdp_disk_t
*d
= &sb
->disks
[i
];
983 if (d
->state
== 0 && d
->number
== 0) {
986 d
->state
= (1<<MD_DISK_REMOVED
);
987 d
->state
|= (1<<MD_DISK_FAULTY
);
991 sb
->nr_disks
= nr_disks
;
992 sb
->active_disks
= active
;
993 sb
->working_disks
= working
;
994 sb
->failed_disks
= failed
;
995 sb
->spare_disks
= spare
;
997 sb
->this_disk
= sb
->disks
[rdev
->desc_nr
];
998 sb
->sb_csum
= calc_sb_csum(sb
);
1002 * version 1 superblock
1005 static __le32
calc_sb_1_csum(struct mdp_superblock_1
* sb
)
1009 unsigned long long newcsum
;
1010 int size
= 256 + le32_to_cpu(sb
->max_dev
)*2;
1011 __le32
*isuper
= (__le32
*)sb
;
1014 disk_csum
= sb
->sb_csum
;
1017 for (i
=0; size
>=4; size
-= 4 )
1018 newcsum
+= le32_to_cpu(*isuper
++);
1021 newcsum
+= le16_to_cpu(*(__le16
*) isuper
);
1023 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
1024 sb
->sb_csum
= disk_csum
;
1025 return cpu_to_le32(csum
);
1028 static int super_1_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
1030 struct mdp_superblock_1
*sb
;
1033 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1037 * Calculate the position of the superblock.
1038 * It is always aligned to a 4K boundary and
1039 * depeding on minor_version, it can be:
1040 * 0: At least 8K, but less than 12K, from end of device
1041 * 1: At start of device
1042 * 2: 4K from start of device.
1044 switch(minor_version
) {
1046 sb_offset
= rdev
->bdev
->bd_inode
->i_size
>> 9;
1048 sb_offset
&= ~(sector_t
)(4*2-1);
1049 /* convert from sectors to K */
1061 rdev
->sb_offset
= sb_offset
;
1063 /* superblock is rarely larger than 1K, but it can be larger,
1064 * and it is safe to read 4k, so we do that
1066 ret
= read_disk_sb(rdev
, 4096);
1067 if (ret
) return ret
;
1070 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1072 if (sb
->magic
!= cpu_to_le32(MD_SB_MAGIC
) ||
1073 sb
->major_version
!= cpu_to_le32(1) ||
1074 le32_to_cpu(sb
->max_dev
) > (4096-256)/2 ||
1075 le64_to_cpu(sb
->super_offset
) != (rdev
->sb_offset
<<1) ||
1076 (le32_to_cpu(sb
->feature_map
) & ~MD_FEATURE_ALL
) != 0)
1079 if (calc_sb_1_csum(sb
) != sb
->sb_csum
) {
1080 printk("md: invalid superblock checksum on %s\n",
1081 bdevname(rdev
->bdev
,b
));
1084 if (le64_to_cpu(sb
->data_size
) < 10) {
1085 printk("md: data_size too small on %s\n",
1086 bdevname(rdev
->bdev
,b
));
1089 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BITMAP_OFFSET
)) {
1090 if (sb
->level
!= cpu_to_le32(1) &&
1091 sb
->level
!= cpu_to_le32(4) &&
1092 sb
->level
!= cpu_to_le32(5) &&
1093 sb
->level
!= cpu_to_le32(6) &&
1094 sb
->level
!= cpu_to_le32(10)) {
1096 "md: bitmaps not supported for this level.\n");
1101 rdev
->preferred_minor
= 0xffff;
1102 rdev
->data_offset
= le64_to_cpu(sb
->data_offset
);
1103 atomic_set(&rdev
->corrected_errors
, le32_to_cpu(sb
->cnt_corrected_read
));
1105 rdev
->sb_size
= le32_to_cpu(sb
->max_dev
) * 2 + 256;
1106 bmask
= queue_hardsect_size(rdev
->bdev
->bd_disk
->queue
)-1;
1107 if (rdev
->sb_size
& bmask
)
1108 rdev
-> sb_size
= (rdev
->sb_size
| bmask
)+1;
1110 if (sb
->level
== cpu_to_le32(LEVEL_MULTIPATH
))
1113 rdev
->desc_nr
= le32_to_cpu(sb
->dev_number
);
1119 struct mdp_superblock_1
*refsb
=
1120 (struct mdp_superblock_1
*)page_address(refdev
->sb_page
);
1122 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16) != 0 ||
1123 sb
->level
!= refsb
->level
||
1124 sb
->layout
!= refsb
->layout
||
1125 sb
->chunksize
!= refsb
->chunksize
) {
1126 printk(KERN_WARNING
"md: %s has strangely different"
1127 " superblock to %s\n",
1128 bdevname(rdev
->bdev
,b
),
1129 bdevname(refdev
->bdev
,b2
));
1132 ev1
= le64_to_cpu(sb
->events
);
1133 ev2
= le64_to_cpu(refsb
->events
);
1141 rdev
->size
= ((rdev
->bdev
->bd_inode
->i_size
>>9) - le64_to_cpu(sb
->data_offset
)) / 2;
1143 rdev
->size
= rdev
->sb_offset
;
1144 if (rdev
->size
< le64_to_cpu(sb
->data_size
)/2)
1146 rdev
->size
= le64_to_cpu(sb
->data_size
)/2;
1147 if (le32_to_cpu(sb
->chunksize
))
1148 rdev
->size
&= ~((sector_t
)le32_to_cpu(sb
->chunksize
)/2 - 1);
1150 if (le64_to_cpu(sb
->size
) > rdev
->size
*2)
1155 static int super_1_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1157 struct mdp_superblock_1
*sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1158 __u64 ev1
= le64_to_cpu(sb
->events
);
1160 rdev
->raid_disk
= -1;
1161 clear_bit(Faulty
, &rdev
->flags
);
1162 clear_bit(In_sync
, &rdev
->flags
);
1163 clear_bit(WriteMostly
, &rdev
->flags
);
1164 clear_bit(BarriersNotsupp
, &rdev
->flags
);
1166 if (mddev
->raid_disks
== 0) {
1167 mddev
->major_version
= 1;
1168 mddev
->patch_version
= 0;
1169 mddev
->external
= 0;
1170 mddev
->chunk_size
= le32_to_cpu(sb
->chunksize
) << 9;
1171 mddev
->ctime
= le64_to_cpu(sb
->ctime
) & ((1ULL << 32)-1);
1172 mddev
->utime
= le64_to_cpu(sb
->utime
) & ((1ULL << 32)-1);
1173 mddev
->level
= le32_to_cpu(sb
->level
);
1174 mddev
->clevel
[0] = 0;
1175 mddev
->layout
= le32_to_cpu(sb
->layout
);
1176 mddev
->raid_disks
= le32_to_cpu(sb
->raid_disks
);
1177 mddev
->size
= le64_to_cpu(sb
->size
)/2;
1178 mddev
->events
= ev1
;
1179 mddev
->bitmap_offset
= 0;
1180 mddev
->default_bitmap_offset
= 1024 >> 9;
1182 mddev
->recovery_cp
= le64_to_cpu(sb
->resync_offset
);
1183 memcpy(mddev
->uuid
, sb
->set_uuid
, 16);
1185 mddev
->max_disks
= (4096-256)/2;
1187 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BITMAP_OFFSET
) &&
1188 mddev
->bitmap_file
== NULL
)
1189 mddev
->bitmap_offset
= (__s32
)le32_to_cpu(sb
->bitmap_offset
);
1191 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
1192 mddev
->reshape_position
= le64_to_cpu(sb
->reshape_position
);
1193 mddev
->delta_disks
= le32_to_cpu(sb
->delta_disks
);
1194 mddev
->new_level
= le32_to_cpu(sb
->new_level
);
1195 mddev
->new_layout
= le32_to_cpu(sb
->new_layout
);
1196 mddev
->new_chunk
= le32_to_cpu(sb
->new_chunk
)<<9;
1198 mddev
->reshape_position
= MaxSector
;
1199 mddev
->delta_disks
= 0;
1200 mddev
->new_level
= mddev
->level
;
1201 mddev
->new_layout
= mddev
->layout
;
1202 mddev
->new_chunk
= mddev
->chunk_size
;
1205 } else if (mddev
->pers
== NULL
) {
1206 /* Insist of good event counter while assembling */
1208 if (ev1
< mddev
->events
)
1210 } else if (mddev
->bitmap
) {
1211 /* If adding to array with a bitmap, then we can accept an
1212 * older device, but not too old.
1214 if (ev1
< mddev
->bitmap
->events_cleared
)
1217 if (ev1
< mddev
->events
)
1218 /* just a hot-add of a new device, leave raid_disk at -1 */
1221 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1223 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
1225 case 0xffff: /* spare */
1227 case 0xfffe: /* faulty */
1228 set_bit(Faulty
, &rdev
->flags
);
1231 if ((le32_to_cpu(sb
->feature_map
) &
1232 MD_FEATURE_RECOVERY_OFFSET
))
1233 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
1235 set_bit(In_sync
, &rdev
->flags
);
1236 rdev
->raid_disk
= role
;
1239 if (sb
->devflags
& WriteMostly1
)
1240 set_bit(WriteMostly
, &rdev
->flags
);
1241 } else /* MULTIPATH are always insync */
1242 set_bit(In_sync
, &rdev
->flags
);
1247 static void super_1_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1249 struct mdp_superblock_1
*sb
;
1250 struct list_head
*tmp
;
1253 /* make rdev->sb match mddev and rdev data. */
1255 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1257 sb
->feature_map
= 0;
1259 sb
->recovery_offset
= cpu_to_le64(0);
1260 memset(sb
->pad1
, 0, sizeof(sb
->pad1
));
1261 memset(sb
->pad2
, 0, sizeof(sb
->pad2
));
1262 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
1264 sb
->utime
= cpu_to_le64((__u64
)mddev
->utime
);
1265 sb
->events
= cpu_to_le64(mddev
->events
);
1267 sb
->resync_offset
= cpu_to_le64(mddev
->recovery_cp
);
1269 sb
->resync_offset
= cpu_to_le64(0);
1271 sb
->cnt_corrected_read
= cpu_to_le32(atomic_read(&rdev
->corrected_errors
));
1273 sb
->raid_disks
= cpu_to_le32(mddev
->raid_disks
);
1274 sb
->size
= cpu_to_le64(mddev
->size
<<1);
1276 if (mddev
->bitmap
&& mddev
->bitmap_file
== NULL
) {
1277 sb
->bitmap_offset
= cpu_to_le32((__u32
)mddev
->bitmap_offset
);
1278 sb
->feature_map
= cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
);
1281 if (rdev
->raid_disk
>= 0 &&
1282 !test_bit(In_sync
, &rdev
->flags
) &&
1283 rdev
->recovery_offset
> 0) {
1284 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET
);
1285 sb
->recovery_offset
= cpu_to_le64(rdev
->recovery_offset
);
1288 if (mddev
->reshape_position
!= MaxSector
) {
1289 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE
);
1290 sb
->reshape_position
= cpu_to_le64(mddev
->reshape_position
);
1291 sb
->new_layout
= cpu_to_le32(mddev
->new_layout
);
1292 sb
->delta_disks
= cpu_to_le32(mddev
->delta_disks
);
1293 sb
->new_level
= cpu_to_le32(mddev
->new_level
);
1294 sb
->new_chunk
= cpu_to_le32(mddev
->new_chunk
>>9);
1298 rdev_for_each(rdev2
, tmp
, mddev
)
1299 if (rdev2
->desc_nr
+1 > max_dev
)
1300 max_dev
= rdev2
->desc_nr
+1;
1302 if (max_dev
> le32_to_cpu(sb
->max_dev
))
1303 sb
->max_dev
= cpu_to_le32(max_dev
);
1304 for (i
=0; i
<max_dev
;i
++)
1305 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1307 rdev_for_each(rdev2
, tmp
, mddev
) {
1309 if (test_bit(Faulty
, &rdev2
->flags
))
1310 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1311 else if (test_bit(In_sync
, &rdev2
->flags
))
1312 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1313 else if (rdev2
->raid_disk
>= 0 && rdev2
->recovery_offset
> 0)
1314 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1316 sb
->dev_roles
[i
] = cpu_to_le16(0xffff);
1319 sb
->sb_csum
= calc_sb_1_csum(sb
);
1323 static struct super_type super_types
[] = {
1326 .owner
= THIS_MODULE
,
1327 .load_super
= super_90_load
,
1328 .validate_super
= super_90_validate
,
1329 .sync_super
= super_90_sync
,
1333 .owner
= THIS_MODULE
,
1334 .load_super
= super_1_load
,
1335 .validate_super
= super_1_validate
,
1336 .sync_super
= super_1_sync
,
1340 static int match_mddev_units(mddev_t
*mddev1
, mddev_t
*mddev2
)
1342 struct list_head
*tmp
, *tmp2
;
1343 mdk_rdev_t
*rdev
, *rdev2
;
1345 rdev_for_each(rdev
, tmp
, mddev1
)
1346 rdev_for_each(rdev2
, tmp2
, mddev2
)
1347 if (rdev
->bdev
->bd_contains
==
1348 rdev2
->bdev
->bd_contains
)
1354 static LIST_HEAD(pending_raid_disks
);
1356 static int bind_rdev_to_array(mdk_rdev_t
* rdev
, mddev_t
* mddev
)
1358 char b
[BDEVNAME_SIZE
];
1367 /* make sure rdev->size exceeds mddev->size */
1368 if (rdev
->size
&& (mddev
->size
== 0 || rdev
->size
< mddev
->size
)) {
1370 /* Cannot change size, so fail
1371 * If mddev->level <= 0, then we don't care
1372 * about aligning sizes (e.g. linear)
1374 if (mddev
->level
> 0)
1377 mddev
->size
= rdev
->size
;
1380 /* Verify rdev->desc_nr is unique.
1381 * If it is -1, assign a free number, else
1382 * check number is not in use
1384 if (rdev
->desc_nr
< 0) {
1386 if (mddev
->pers
) choice
= mddev
->raid_disks
;
1387 while (find_rdev_nr(mddev
, choice
))
1389 rdev
->desc_nr
= choice
;
1391 if (find_rdev_nr(mddev
, rdev
->desc_nr
))
1394 bdevname(rdev
->bdev
,b
);
1395 while ( (s
=strchr(b
, '/')) != NULL
)
1398 rdev
->mddev
= mddev
;
1399 printk(KERN_INFO
"md: bind<%s>\n", b
);
1401 if ((err
= kobject_add(&rdev
->kobj
, &mddev
->kobj
, "dev-%s", b
)))
1404 if (rdev
->bdev
->bd_part
)
1405 ko
= &rdev
->bdev
->bd_part
->dev
.kobj
;
1407 ko
= &rdev
->bdev
->bd_disk
->dev
.kobj
;
1408 if ((err
= sysfs_create_link(&rdev
->kobj
, ko
, "block"))) {
1409 kobject_del(&rdev
->kobj
);
1412 list_add(&rdev
->same_set
, &mddev
->disks
);
1413 bd_claim_by_disk(rdev
->bdev
, rdev
->bdev
->bd_holder
, mddev
->gendisk
);
1417 printk(KERN_WARNING
"md: failed to register dev-%s for %s\n",
1422 static void md_delayed_delete(struct work_struct
*ws
)
1424 mdk_rdev_t
*rdev
= container_of(ws
, mdk_rdev_t
, del_work
);
1425 kobject_del(&rdev
->kobj
);
1426 kobject_put(&rdev
->kobj
);
1429 static void unbind_rdev_from_array(mdk_rdev_t
* rdev
)
1431 char b
[BDEVNAME_SIZE
];
1436 bd_release_from_disk(rdev
->bdev
, rdev
->mddev
->gendisk
);
1437 list_del_init(&rdev
->same_set
);
1438 printk(KERN_INFO
"md: unbind<%s>\n", bdevname(rdev
->bdev
,b
));
1440 sysfs_remove_link(&rdev
->kobj
, "block");
1442 /* We need to delay this, otherwise we can deadlock when
1443 * writing to 'remove' to "dev/state"
1445 INIT_WORK(&rdev
->del_work
, md_delayed_delete
);
1446 kobject_get(&rdev
->kobj
);
1447 schedule_work(&rdev
->del_work
);
1451 * prevent the device from being mounted, repartitioned or
1452 * otherwise reused by a RAID array (or any other kernel
1453 * subsystem), by bd_claiming the device.
1455 static int lock_rdev(mdk_rdev_t
*rdev
, dev_t dev
, int shared
)
1458 struct block_device
*bdev
;
1459 char b
[BDEVNAME_SIZE
];
1461 bdev
= open_by_devnum(dev
, FMODE_READ
|FMODE_WRITE
);
1463 printk(KERN_ERR
"md: could not open %s.\n",
1464 __bdevname(dev
, b
));
1465 return PTR_ERR(bdev
);
1467 err
= bd_claim(bdev
, shared
? (mdk_rdev_t
*)lock_rdev
: rdev
);
1469 printk(KERN_ERR
"md: could not bd_claim %s.\n",
1475 set_bit(AllReserved
, &rdev
->flags
);
1480 static void unlock_rdev(mdk_rdev_t
*rdev
)
1482 struct block_device
*bdev
= rdev
->bdev
;
1490 void md_autodetect_dev(dev_t dev
);
1492 static void export_rdev(mdk_rdev_t
* rdev
)
1494 char b
[BDEVNAME_SIZE
];
1495 printk(KERN_INFO
"md: export_rdev(%s)\n",
1496 bdevname(rdev
->bdev
,b
));
1500 list_del_init(&rdev
->same_set
);
1502 md_autodetect_dev(rdev
->bdev
->bd_dev
);
1505 kobject_put(&rdev
->kobj
);
1508 static void kick_rdev_from_array(mdk_rdev_t
* rdev
)
1510 unbind_rdev_from_array(rdev
);
1514 static void export_array(mddev_t
*mddev
)
1516 struct list_head
*tmp
;
1519 rdev_for_each(rdev
, tmp
, mddev
) {
1524 kick_rdev_from_array(rdev
);
1526 if (!list_empty(&mddev
->disks
))
1528 mddev
->raid_disks
= 0;
1529 mddev
->major_version
= 0;
1532 static void print_desc(mdp_disk_t
*desc
)
1534 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc
->number
,
1535 desc
->major
,desc
->minor
,desc
->raid_disk
,desc
->state
);
1538 static void print_sb(mdp_super_t
*sb
)
1543 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1544 sb
->major_version
, sb
->minor_version
, sb
->patch_version
,
1545 sb
->set_uuid0
, sb
->set_uuid1
, sb
->set_uuid2
, sb
->set_uuid3
,
1547 printk(KERN_INFO
"md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1548 sb
->level
, sb
->size
, sb
->nr_disks
, sb
->raid_disks
,
1549 sb
->md_minor
, sb
->layout
, sb
->chunk_size
);
1550 printk(KERN_INFO
"md: UT:%08x ST:%d AD:%d WD:%d"
1551 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1552 sb
->utime
, sb
->state
, sb
->active_disks
, sb
->working_disks
,
1553 sb
->failed_disks
, sb
->spare_disks
,
1554 sb
->sb_csum
, (unsigned long)sb
->events_lo
);
1557 for (i
= 0; i
< MD_SB_DISKS
; i
++) {
1560 desc
= sb
->disks
+ i
;
1561 if (desc
->number
|| desc
->major
|| desc
->minor
||
1562 desc
->raid_disk
|| (desc
->state
&& (desc
->state
!= 4))) {
1563 printk(" D %2d: ", i
);
1567 printk(KERN_INFO
"md: THIS: ");
1568 print_desc(&sb
->this_disk
);
1572 static void print_rdev(mdk_rdev_t
*rdev
)
1574 char b
[BDEVNAME_SIZE
];
1575 printk(KERN_INFO
"md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1576 bdevname(rdev
->bdev
,b
), (unsigned long long)rdev
->size
,
1577 test_bit(Faulty
, &rdev
->flags
), test_bit(In_sync
, &rdev
->flags
),
1579 if (rdev
->sb_loaded
) {
1580 printk(KERN_INFO
"md: rdev superblock:\n");
1581 print_sb((mdp_super_t
*)page_address(rdev
->sb_page
));
1583 printk(KERN_INFO
"md: no rdev superblock!\n");
1586 static void md_print_devices(void)
1588 struct list_head
*tmp
, *tmp2
;
1591 char b
[BDEVNAME_SIZE
];
1594 printk("md: **********************************\n");
1595 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1596 printk("md: **********************************\n");
1597 for_each_mddev(mddev
, tmp
) {
1600 bitmap_print_sb(mddev
->bitmap
);
1602 printk("%s: ", mdname(mddev
));
1603 rdev_for_each(rdev
, tmp2
, mddev
)
1604 printk("<%s>", bdevname(rdev
->bdev
,b
));
1607 rdev_for_each(rdev
, tmp2
, mddev
)
1610 printk("md: **********************************\n");
1615 static void sync_sbs(mddev_t
* mddev
, int nospares
)
1617 /* Update each superblock (in-memory image), but
1618 * if we are allowed to, skip spares which already
1619 * have the right event counter, or have one earlier
1620 * (which would mean they aren't being marked as dirty
1621 * with the rest of the array)
1624 struct list_head
*tmp
;
1626 rdev_for_each(rdev
, tmp
, mddev
) {
1627 if (rdev
->sb_events
== mddev
->events
||
1629 rdev
->raid_disk
< 0 &&
1630 (rdev
->sb_events
&1)==0 &&
1631 rdev
->sb_events
+1 == mddev
->events
)) {
1632 /* Don't update this superblock */
1633 rdev
->sb_loaded
= 2;
1635 super_types
[mddev
->major_version
].
1636 sync_super(mddev
, rdev
);
1637 rdev
->sb_loaded
= 1;
1642 static void md_update_sb(mddev_t
* mddev
, int force_change
)
1644 struct list_head
*tmp
;
1650 spin_lock_irq(&mddev
->write_lock
);
1652 set_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
1653 if (test_and_clear_bit(MD_CHANGE_DEVS
, &mddev
->flags
))
1655 if (test_and_clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
))
1656 /* just a clean<-> dirty transition, possibly leave spares alone,
1657 * though if events isn't the right even/odd, we will have to do
1663 if (mddev
->degraded
)
1664 /* If the array is degraded, then skipping spares is both
1665 * dangerous and fairly pointless.
1666 * Dangerous because a device that was removed from the array
1667 * might have a event_count that still looks up-to-date,
1668 * so it can be re-added without a resync.
1669 * Pointless because if there are any spares to skip,
1670 * then a recovery will happen and soon that array won't
1671 * be degraded any more and the spare can go back to sleep then.
1675 sync_req
= mddev
->in_sync
;
1676 mddev
->utime
= get_seconds();
1678 /* If this is just a dirty<->clean transition, and the array is clean
1679 * and 'events' is odd, we can roll back to the previous clean state */
1681 && (mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
1682 && (mddev
->events
& 1)
1683 && mddev
->events
!= 1)
1686 /* otherwise we have to go forward and ... */
1688 if (!mddev
->in_sync
|| mddev
->recovery_cp
!= MaxSector
) { /* not clean */
1689 /* .. if the array isn't clean, insist on an odd 'events' */
1690 if ((mddev
->events
&1)==0) {
1695 /* otherwise insist on an even 'events' (for clean states) */
1696 if ((mddev
->events
&1)) {
1703 if (!mddev
->events
) {
1705 * oops, this 64-bit counter should never wrap.
1706 * Either we are in around ~1 trillion A.C., assuming
1707 * 1 reboot per second, or we have a bug:
1714 * do not write anything to disk if using
1715 * nonpersistent superblocks
1717 if (!mddev
->persistent
) {
1718 if (!mddev
->external
)
1719 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
1721 spin_unlock_irq(&mddev
->write_lock
);
1722 wake_up(&mddev
->sb_wait
);
1725 sync_sbs(mddev
, nospares
);
1726 spin_unlock_irq(&mddev
->write_lock
);
1729 "md: updating %s RAID superblock on device (in sync %d)\n",
1730 mdname(mddev
),mddev
->in_sync
);
1732 bitmap_update_sb(mddev
->bitmap
);
1733 rdev_for_each(rdev
, tmp
, mddev
) {
1734 char b
[BDEVNAME_SIZE
];
1735 dprintk(KERN_INFO
"md: ");
1736 if (rdev
->sb_loaded
!= 1)
1737 continue; /* no noise on spare devices */
1738 if (test_bit(Faulty
, &rdev
->flags
))
1739 dprintk("(skipping faulty ");
1741 dprintk("%s ", bdevname(rdev
->bdev
,b
));
1742 if (!test_bit(Faulty
, &rdev
->flags
)) {
1743 md_super_write(mddev
,rdev
,
1744 rdev
->sb_offset
<<1, rdev
->sb_size
,
1746 dprintk(KERN_INFO
"(write) %s's sb offset: %llu\n",
1747 bdevname(rdev
->bdev
,b
),
1748 (unsigned long long)rdev
->sb_offset
);
1749 rdev
->sb_events
= mddev
->events
;
1753 if (mddev
->level
== LEVEL_MULTIPATH
)
1754 /* only need to write one superblock... */
1757 md_super_wait(mddev
);
1758 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
1760 spin_lock_irq(&mddev
->write_lock
);
1761 if (mddev
->in_sync
!= sync_req
||
1762 test_bit(MD_CHANGE_DEVS
, &mddev
->flags
)) {
1763 /* have to write it out again */
1764 spin_unlock_irq(&mddev
->write_lock
);
1767 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
1768 spin_unlock_irq(&mddev
->write_lock
);
1769 wake_up(&mddev
->sb_wait
);
1773 /* words written to sysfs files may, or my not, be \n terminated.
1774 * We want to accept with case. For this we use cmd_match.
1776 static int cmd_match(const char *cmd
, const char *str
)
1778 /* See if cmd, written into a sysfs file, matches
1779 * str. They must either be the same, or cmd can
1780 * have a trailing newline
1782 while (*cmd
&& *str
&& *cmd
== *str
) {
1793 struct rdev_sysfs_entry
{
1794 struct attribute attr
;
1795 ssize_t (*show
)(mdk_rdev_t
*, char *);
1796 ssize_t (*store
)(mdk_rdev_t
*, const char *, size_t);
1800 state_show(mdk_rdev_t
*rdev
, char *page
)
1805 if (test_bit(Faulty
, &rdev
->flags
)) {
1806 len
+= sprintf(page
+len
, "%sfaulty",sep
);
1809 if (test_bit(In_sync
, &rdev
->flags
)) {
1810 len
+= sprintf(page
+len
, "%sin_sync",sep
);
1813 if (test_bit(WriteMostly
, &rdev
->flags
)) {
1814 len
+= sprintf(page
+len
, "%swrite_mostly",sep
);
1817 if (!test_bit(Faulty
, &rdev
->flags
) &&
1818 !test_bit(In_sync
, &rdev
->flags
)) {
1819 len
+= sprintf(page
+len
, "%sspare", sep
);
1822 return len
+sprintf(page
+len
, "\n");
1826 state_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
1829 * faulty - simulates and error
1830 * remove - disconnects the device
1831 * writemostly - sets write_mostly
1832 * -writemostly - clears write_mostly
1835 if (cmd_match(buf
, "faulty") && rdev
->mddev
->pers
) {
1836 md_error(rdev
->mddev
, rdev
);
1838 } else if (cmd_match(buf
, "remove")) {
1839 if (rdev
->raid_disk
>= 0)
1842 mddev_t
*mddev
= rdev
->mddev
;
1843 kick_rdev_from_array(rdev
);
1845 md_update_sb(mddev
, 1);
1846 md_new_event(mddev
);
1849 } else if (cmd_match(buf
, "writemostly")) {
1850 set_bit(WriteMostly
, &rdev
->flags
);
1852 } else if (cmd_match(buf
, "-writemostly")) {
1853 clear_bit(WriteMostly
, &rdev
->flags
);
1856 return err
? err
: len
;
1858 static struct rdev_sysfs_entry rdev_state
=
1859 __ATTR(state
, S_IRUGO
|S_IWUSR
, state_show
, state_store
);
1862 super_show(mdk_rdev_t
*rdev
, char *page
)
1864 if (rdev
->sb_loaded
&& rdev
->sb_size
) {
1865 memcpy(page
, page_address(rdev
->sb_page
), rdev
->sb_size
);
1866 return rdev
->sb_size
;
1870 static struct rdev_sysfs_entry rdev_super
= __ATTR_RO(super
);
1873 errors_show(mdk_rdev_t
*rdev
, char *page
)
1875 return sprintf(page
, "%d\n", atomic_read(&rdev
->corrected_errors
));
1879 errors_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
1882 unsigned long n
= simple_strtoul(buf
, &e
, 10);
1883 if (*buf
&& (*e
== 0 || *e
== '\n')) {
1884 atomic_set(&rdev
->corrected_errors
, n
);
1889 static struct rdev_sysfs_entry rdev_errors
=
1890 __ATTR(errors
, S_IRUGO
|S_IWUSR
, errors_show
, errors_store
);
1893 slot_show(mdk_rdev_t
*rdev
, char *page
)
1895 if (rdev
->raid_disk
< 0)
1896 return sprintf(page
, "none\n");
1898 return sprintf(page
, "%d\n", rdev
->raid_disk
);
1902 slot_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
1907 int slot
= simple_strtoul(buf
, &e
, 10);
1908 if (strncmp(buf
, "none", 4)==0)
1910 else if (e
==buf
|| (*e
&& *e
!= '\n'))
1912 if (rdev
->mddev
->pers
) {
1913 /* Setting 'slot' on an active array requires also
1914 * updating the 'rd%d' link, and communicating
1915 * with the personality with ->hot_*_disk.
1916 * For now we only support removing
1917 * failed/spare devices. This normally happens automatically,
1918 * but not when the metadata is externally managed.
1922 if (rdev
->raid_disk
== -1)
1924 /* personality does all needed checks */
1925 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
1927 err
= rdev
->mddev
->pers
->
1928 hot_remove_disk(rdev
->mddev
, rdev
->raid_disk
);
1931 sprintf(nm
, "rd%d", rdev
->raid_disk
);
1932 sysfs_remove_link(&rdev
->mddev
->kobj
, nm
);
1933 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
1934 md_wakeup_thread(rdev
->mddev
->thread
);
1936 if (slot
>= rdev
->mddev
->raid_disks
)
1938 rdev
->raid_disk
= slot
;
1939 /* assume it is working */
1940 clear_bit(Faulty
, &rdev
->flags
);
1941 clear_bit(WriteMostly
, &rdev
->flags
);
1942 set_bit(In_sync
, &rdev
->flags
);
1948 static struct rdev_sysfs_entry rdev_slot
=
1949 __ATTR(slot
, S_IRUGO
|S_IWUSR
, slot_show
, slot_store
);
1952 offset_show(mdk_rdev_t
*rdev
, char *page
)
1954 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->data_offset
);
1958 offset_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
1961 unsigned long long offset
= simple_strtoull(buf
, &e
, 10);
1962 if (e
==buf
|| (*e
&& *e
!= '\n'))
1964 if (rdev
->mddev
->pers
)
1966 if (rdev
->size
&& rdev
->mddev
->external
)
1967 /* Must set offset before size, so overlap checks
1970 rdev
->data_offset
= offset
;
1974 static struct rdev_sysfs_entry rdev_offset
=
1975 __ATTR(offset
, S_IRUGO
|S_IWUSR
, offset_show
, offset_store
);
1978 rdev_size_show(mdk_rdev_t
*rdev
, char *page
)
1980 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->size
);
1983 static int overlaps(sector_t s1
, sector_t l1
, sector_t s2
, sector_t l2
)
1985 /* check if two start/length pairs overlap */
1994 rdev_size_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
1997 unsigned long long size
= simple_strtoull(buf
, &e
, 10);
1998 unsigned long long oldsize
= rdev
->size
;
1999 if (e
==buf
|| (*e
&& *e
!= '\n'))
2001 if (rdev
->mddev
->pers
)
2004 if (size
> oldsize
&& rdev
->mddev
->external
) {
2005 /* need to check that all other rdevs with the same ->bdev
2006 * do not overlap. We need to unlock the mddev to avoid
2007 * a deadlock. We have already changed rdev->size, and if
2008 * we have to change it back, we will have the lock again.
2012 struct list_head
*tmp
, *tmp2
;
2014 mddev_unlock(rdev
->mddev
);
2015 for_each_mddev(mddev
, tmp
) {
2019 rdev_for_each(rdev2
, tmp2
, mddev
)
2020 if (test_bit(AllReserved
, &rdev2
->flags
) ||
2021 (rdev
->bdev
== rdev2
->bdev
&&
2023 overlaps(rdev
->data_offset
, rdev
->size
,
2024 rdev2
->data_offset
, rdev2
->size
))) {
2028 mddev_unlock(mddev
);
2034 mddev_lock(rdev
->mddev
);
2036 /* Someone else could have slipped in a size
2037 * change here, but doing so is just silly.
2038 * We put oldsize back because we *know* it is
2039 * safe, and trust userspace not to race with
2042 rdev
->size
= oldsize
;
2046 if (size
< rdev
->mddev
->size
|| rdev
->mddev
->size
== 0)
2047 rdev
->mddev
->size
= size
;
2051 static struct rdev_sysfs_entry rdev_size
=
2052 __ATTR(size
, S_IRUGO
|S_IWUSR
, rdev_size_show
, rdev_size_store
);
2054 static struct attribute
*rdev_default_attrs
[] = {
2064 rdev_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
2066 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
2067 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
2071 return entry
->show(rdev
, page
);
2075 rdev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
2076 const char *page
, size_t length
)
2078 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
2079 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
2084 if (!capable(CAP_SYS_ADMIN
))
2086 rv
= mddev_lock(rdev
->mddev
);
2088 rv
= entry
->store(rdev
, page
, length
);
2089 mddev_unlock(rdev
->mddev
);
2094 static void rdev_free(struct kobject
*ko
)
2096 mdk_rdev_t
*rdev
= container_of(ko
, mdk_rdev_t
, kobj
);
2099 static struct sysfs_ops rdev_sysfs_ops
= {
2100 .show
= rdev_attr_show
,
2101 .store
= rdev_attr_store
,
2103 static struct kobj_type rdev_ktype
= {
2104 .release
= rdev_free
,
2105 .sysfs_ops
= &rdev_sysfs_ops
,
2106 .default_attrs
= rdev_default_attrs
,
2110 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2112 * mark the device faulty if:
2114 * - the device is nonexistent (zero size)
2115 * - the device has no valid superblock
2117 * a faulty rdev _never_ has rdev->sb set.
2119 static mdk_rdev_t
*md_import_device(dev_t newdev
, int super_format
, int super_minor
)
2121 char b
[BDEVNAME_SIZE
];
2126 rdev
= kzalloc(sizeof(*rdev
), GFP_KERNEL
);
2128 printk(KERN_ERR
"md: could not alloc mem for new device!\n");
2129 return ERR_PTR(-ENOMEM
);
2132 if ((err
= alloc_disk_sb(rdev
)))
2135 err
= lock_rdev(rdev
, newdev
, super_format
== -2);
2139 kobject_init(&rdev
->kobj
, &rdev_ktype
);
2142 rdev
->saved_raid_disk
= -1;
2143 rdev
->raid_disk
= -1;
2145 rdev
->data_offset
= 0;
2146 rdev
->sb_events
= 0;
2147 atomic_set(&rdev
->nr_pending
, 0);
2148 atomic_set(&rdev
->read_errors
, 0);
2149 atomic_set(&rdev
->corrected_errors
, 0);
2151 size
= rdev
->bdev
->bd_inode
->i_size
>> BLOCK_SIZE_BITS
;
2154 "md: %s has zero or unknown size, marking faulty!\n",
2155 bdevname(rdev
->bdev
,b
));
2160 if (super_format
>= 0) {
2161 err
= super_types
[super_format
].
2162 load_super(rdev
, NULL
, super_minor
);
2163 if (err
== -EINVAL
) {
2165 "md: %s does not have a valid v%d.%d "
2166 "superblock, not importing!\n",
2167 bdevname(rdev
->bdev
,b
),
2168 super_format
, super_minor
);
2173 "md: could not read %s's sb, not importing!\n",
2174 bdevname(rdev
->bdev
,b
));
2178 INIT_LIST_HEAD(&rdev
->same_set
);
2183 if (rdev
->sb_page
) {
2189 return ERR_PTR(err
);
2193 * Check a full RAID array for plausibility
2197 static void analyze_sbs(mddev_t
* mddev
)
2200 struct list_head
*tmp
;
2201 mdk_rdev_t
*rdev
, *freshest
;
2202 char b
[BDEVNAME_SIZE
];
2205 rdev_for_each(rdev
, tmp
, mddev
)
2206 switch (super_types
[mddev
->major_version
].
2207 load_super(rdev
, freshest
, mddev
->minor_version
)) {
2215 "md: fatal superblock inconsistency in %s"
2216 " -- removing from array\n",
2217 bdevname(rdev
->bdev
,b
));
2218 kick_rdev_from_array(rdev
);
2222 super_types
[mddev
->major_version
].
2223 validate_super(mddev
, freshest
);
2226 rdev_for_each(rdev
, tmp
, mddev
) {
2227 if (rdev
!= freshest
)
2228 if (super_types
[mddev
->major_version
].
2229 validate_super(mddev
, rdev
)) {
2230 printk(KERN_WARNING
"md: kicking non-fresh %s"
2232 bdevname(rdev
->bdev
,b
));
2233 kick_rdev_from_array(rdev
);
2236 if (mddev
->level
== LEVEL_MULTIPATH
) {
2237 rdev
->desc_nr
= i
++;
2238 rdev
->raid_disk
= rdev
->desc_nr
;
2239 set_bit(In_sync
, &rdev
->flags
);
2240 } else if (rdev
->raid_disk
>= mddev
->raid_disks
) {
2241 rdev
->raid_disk
= -1;
2242 clear_bit(In_sync
, &rdev
->flags
);
2248 if (mddev
->recovery_cp
!= MaxSector
&&
2250 printk(KERN_ERR
"md: %s: raid array is not clean"
2251 " -- starting background reconstruction\n",
2257 safe_delay_show(mddev_t
*mddev
, char *page
)
2259 int msec
= (mddev
->safemode_delay
*1000)/HZ
;
2260 return sprintf(page
, "%d.%03d\n", msec
/1000, msec
%1000);
2263 safe_delay_store(mddev_t
*mddev
, const char *cbuf
, size_t len
)
2271 /* remove a period, and count digits after it */
2272 if (len
>= sizeof(buf
))
2274 strlcpy(buf
, cbuf
, len
);
2276 for (i
=0; i
<len
; i
++) {
2278 if (isdigit(buf
[i
])) {
2283 } else if (buf
[i
] == '.') {
2288 msec
= simple_strtoul(buf
, &e
, 10);
2289 if (e
== buf
|| (*e
&& *e
!= '\n'))
2291 msec
= (msec
* 1000) / scale
;
2293 mddev
->safemode_delay
= 0;
2295 mddev
->safemode_delay
= (msec
*HZ
)/1000;
2296 if (mddev
->safemode_delay
== 0)
2297 mddev
->safemode_delay
= 1;
2301 static struct md_sysfs_entry md_safe_delay
=
2302 __ATTR(safe_mode_delay
, S_IRUGO
|S_IWUSR
,safe_delay_show
, safe_delay_store
);
2305 level_show(mddev_t
*mddev
, char *page
)
2307 struct mdk_personality
*p
= mddev
->pers
;
2309 return sprintf(page
, "%s\n", p
->name
);
2310 else if (mddev
->clevel
[0])
2311 return sprintf(page
, "%s\n", mddev
->clevel
);
2312 else if (mddev
->level
!= LEVEL_NONE
)
2313 return sprintf(page
, "%d\n", mddev
->level
);
2319 level_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2326 if (len
>= sizeof(mddev
->clevel
))
2328 strncpy(mddev
->clevel
, buf
, len
);
2329 if (mddev
->clevel
[len
-1] == '\n')
2331 mddev
->clevel
[len
] = 0;
2332 mddev
->level
= LEVEL_NONE
;
2336 static struct md_sysfs_entry md_level
=
2337 __ATTR(level
, S_IRUGO
|S_IWUSR
, level_show
, level_store
);
2341 layout_show(mddev_t
*mddev
, char *page
)
2343 /* just a number, not meaningful for all levels */
2344 if (mddev
->reshape_position
!= MaxSector
&&
2345 mddev
->layout
!= mddev
->new_layout
)
2346 return sprintf(page
, "%d (%d)\n",
2347 mddev
->new_layout
, mddev
->layout
);
2348 return sprintf(page
, "%d\n", mddev
->layout
);
2352 layout_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2355 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2357 if (!*buf
|| (*e
&& *e
!= '\n'))
2362 if (mddev
->reshape_position
!= MaxSector
)
2363 mddev
->new_layout
= n
;
2368 static struct md_sysfs_entry md_layout
=
2369 __ATTR(layout
, S_IRUGO
|S_IWUSR
, layout_show
, layout_store
);
2373 raid_disks_show(mddev_t
*mddev
, char *page
)
2375 if (mddev
->raid_disks
== 0)
2377 if (mddev
->reshape_position
!= MaxSector
&&
2378 mddev
->delta_disks
!= 0)
2379 return sprintf(page
, "%d (%d)\n", mddev
->raid_disks
,
2380 mddev
->raid_disks
- mddev
->delta_disks
);
2381 return sprintf(page
, "%d\n", mddev
->raid_disks
);
2384 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
);
2387 raid_disks_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2391 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2393 if (!*buf
|| (*e
&& *e
!= '\n'))
2397 rv
= update_raid_disks(mddev
, n
);
2398 else if (mddev
->reshape_position
!= MaxSector
) {
2399 int olddisks
= mddev
->raid_disks
- mddev
->delta_disks
;
2400 mddev
->delta_disks
= n
- olddisks
;
2401 mddev
->raid_disks
= n
;
2403 mddev
->raid_disks
= n
;
2404 return rv
? rv
: len
;
2406 static struct md_sysfs_entry md_raid_disks
=
2407 __ATTR(raid_disks
, S_IRUGO
|S_IWUSR
, raid_disks_show
, raid_disks_store
);
2410 chunk_size_show(mddev_t
*mddev
, char *page
)
2412 if (mddev
->reshape_position
!= MaxSector
&&
2413 mddev
->chunk_size
!= mddev
->new_chunk
)
2414 return sprintf(page
, "%d (%d)\n", mddev
->new_chunk
,
2416 return sprintf(page
, "%d\n", mddev
->chunk_size
);
2420 chunk_size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2422 /* can only set chunk_size if array is not yet active */
2424 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2426 if (!*buf
|| (*e
&& *e
!= '\n'))
2431 else if (mddev
->reshape_position
!= MaxSector
)
2432 mddev
->new_chunk
= n
;
2434 mddev
->chunk_size
= n
;
2437 static struct md_sysfs_entry md_chunk_size
=
2438 __ATTR(chunk_size
, S_IRUGO
|S_IWUSR
, chunk_size_show
, chunk_size_store
);
2441 resync_start_show(mddev_t
*mddev
, char *page
)
2443 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->recovery_cp
);
2447 resync_start_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2449 /* can only set chunk_size if array is not yet active */
2451 unsigned long long n
= simple_strtoull(buf
, &e
, 10);
2455 if (!*buf
|| (*e
&& *e
!= '\n'))
2458 mddev
->recovery_cp
= n
;
2461 static struct md_sysfs_entry md_resync_start
=
2462 __ATTR(resync_start
, S_IRUGO
|S_IWUSR
, resync_start_show
, resync_start_store
);
2465 * The array state can be:
2468 * No devices, no size, no level
2469 * Equivalent to STOP_ARRAY ioctl
2471 * May have some settings, but array is not active
2472 * all IO results in error
2473 * When written, doesn't tear down array, but just stops it
2474 * suspended (not supported yet)
2475 * All IO requests will block. The array can be reconfigured.
2476 * Writing this, if accepted, will block until array is quiessent
2478 * no resync can happen. no superblocks get written.
2479 * write requests fail
2481 * like readonly, but behaves like 'clean' on a write request.
2483 * clean - no pending writes, but otherwise active.
2484 * When written to inactive array, starts without resync
2485 * If a write request arrives then
2486 * if metadata is known, mark 'dirty' and switch to 'active'.
2487 * if not known, block and switch to write-pending
2488 * If written to an active array that has pending writes, then fails.
2490 * fully active: IO and resync can be happening.
2491 * When written to inactive array, starts with resync
2494 * clean, but writes are blocked waiting for 'active' to be written.
2497 * like active, but no writes have been seen for a while (100msec).
2500 enum array_state
{ clear
, inactive
, suspended
, readonly
, read_auto
, clean
, active
,
2501 write_pending
, active_idle
, bad_word
};
2502 static char *array_states
[] = {
2503 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2504 "write-pending", "active-idle", NULL
};
2506 static int match_word(const char *word
, char **list
)
2509 for (n
=0; list
[n
]; n
++)
2510 if (cmd_match(word
, list
[n
]))
2516 array_state_show(mddev_t
*mddev
, char *page
)
2518 enum array_state st
= inactive
;
2531 else if (test_bit(MD_CHANGE_CLEAN
, &mddev
->flags
))
2533 else if (mddev
->safemode
)
2539 if (list_empty(&mddev
->disks
) &&
2540 mddev
->raid_disks
== 0 &&
2546 return sprintf(page
, "%s\n", array_states
[st
]);
2549 static int do_md_stop(mddev_t
* mddev
, int ro
);
2550 static int do_md_run(mddev_t
* mddev
);
2551 static int restart_array(mddev_t
*mddev
);
2554 array_state_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2557 enum array_state st
= match_word(buf
, array_states
);
2562 /* stopping an active array */
2563 if (atomic_read(&mddev
->active
) > 1)
2565 err
= do_md_stop(mddev
, 0);
2568 /* stopping an active array */
2570 if (atomic_read(&mddev
->active
) > 1)
2572 err
= do_md_stop(mddev
, 2);
2574 err
= 0; /* already inactive */
2577 break; /* not supported yet */
2580 err
= do_md_stop(mddev
, 1);
2583 err
= do_md_run(mddev
);
2587 /* stopping an active array */
2589 err
= do_md_stop(mddev
, 1);
2591 mddev
->ro
= 2; /* FIXME mark devices writable */
2594 err
= do_md_run(mddev
);
2599 restart_array(mddev
);
2600 spin_lock_irq(&mddev
->write_lock
);
2601 if (atomic_read(&mddev
->writes_pending
) == 0) {
2602 if (mddev
->in_sync
== 0) {
2604 if (mddev
->persistent
)
2605 set_bit(MD_CHANGE_CLEAN
,
2611 spin_unlock_irq(&mddev
->write_lock
);
2614 mddev
->recovery_cp
= MaxSector
;
2615 err
= do_md_run(mddev
);
2620 restart_array(mddev
);
2621 if (mddev
->external
)
2622 clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
2623 wake_up(&mddev
->sb_wait
);
2627 err
= do_md_run(mddev
);
2632 /* these cannot be set */
2640 static struct md_sysfs_entry md_array_state
=
2641 __ATTR(array_state
, S_IRUGO
|S_IWUSR
, array_state_show
, array_state_store
);
2644 null_show(mddev_t
*mddev
, char *page
)
2650 new_dev_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2652 /* buf must be %d:%d\n? giving major and minor numbers */
2653 /* The new device is added to the array.
2654 * If the array has a persistent superblock, we read the
2655 * superblock to initialise info and check validity.
2656 * Otherwise, only checking done is that in bind_rdev_to_array,
2657 * which mainly checks size.
2660 int major
= simple_strtoul(buf
, &e
, 10);
2666 if (!*buf
|| *e
!= ':' || !e
[1] || e
[1] == '\n')
2668 minor
= simple_strtoul(e
+1, &e
, 10);
2669 if (*e
&& *e
!= '\n')
2671 dev
= MKDEV(major
, minor
);
2672 if (major
!= MAJOR(dev
) ||
2673 minor
!= MINOR(dev
))
2677 if (mddev
->persistent
) {
2678 rdev
= md_import_device(dev
, mddev
->major_version
,
2679 mddev
->minor_version
);
2680 if (!IS_ERR(rdev
) && !list_empty(&mddev
->disks
)) {
2681 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
2682 mdk_rdev_t
, same_set
);
2683 err
= super_types
[mddev
->major_version
]
2684 .load_super(rdev
, rdev0
, mddev
->minor_version
);
2688 } else if (mddev
->external
)
2689 rdev
= md_import_device(dev
, -2, -1);
2691 rdev
= md_import_device(dev
, -1, -1);
2694 return PTR_ERR(rdev
);
2695 err
= bind_rdev_to_array(rdev
, mddev
);
2699 return err
? err
: len
;
2702 static struct md_sysfs_entry md_new_device
=
2703 __ATTR(new_dev
, S_IWUSR
, null_show
, new_dev_store
);
2706 bitmap_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2709 unsigned long chunk
, end_chunk
;
2713 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
2715 chunk
= end_chunk
= simple_strtoul(buf
, &end
, 0);
2716 if (buf
== end
) break;
2717 if (*end
== '-') { /* range */
2719 end_chunk
= simple_strtoul(buf
, &end
, 0);
2720 if (buf
== end
) break;
2722 if (*end
&& !isspace(*end
)) break;
2723 bitmap_dirty_bits(mddev
->bitmap
, chunk
, end_chunk
);
2725 while (isspace(*buf
)) buf
++;
2727 bitmap_unplug(mddev
->bitmap
); /* flush the bits to disk */
2732 static struct md_sysfs_entry md_bitmap
=
2733 __ATTR(bitmap_set_bits
, S_IWUSR
, null_show
, bitmap_store
);
2736 size_show(mddev_t
*mddev
, char *page
)
2738 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->size
);
2741 static int update_size(mddev_t
*mddev
, unsigned long size
);
2744 size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2746 /* If array is inactive, we can reduce the component size, but
2747 * not increase it (except from 0).
2748 * If array is active, we can try an on-line resize
2752 unsigned long long size
= simple_strtoull(buf
, &e
, 10);
2753 if (!*buf
|| *buf
== '\n' ||
2758 err
= update_size(mddev
, size
);
2759 md_update_sb(mddev
, 1);
2761 if (mddev
->size
== 0 ||
2767 return err
? err
: len
;
2770 static struct md_sysfs_entry md_size
=
2771 __ATTR(component_size
, S_IRUGO
|S_IWUSR
, size_show
, size_store
);
2776 * 'none' for arrays with no metadata (good luck...)
2777 * 'external' for arrays with externally managed metadata,
2778 * or N.M for internally known formats
2781 metadata_show(mddev_t
*mddev
, char *page
)
2783 if (mddev
->persistent
)
2784 return sprintf(page
, "%d.%d\n",
2785 mddev
->major_version
, mddev
->minor_version
);
2786 else if (mddev
->external
)
2787 return sprintf(page
, "external:%s\n", mddev
->metadata_type
);
2789 return sprintf(page
, "none\n");
2793 metadata_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2797 if (!list_empty(&mddev
->disks
))
2800 if (cmd_match(buf
, "none")) {
2801 mddev
->persistent
= 0;
2802 mddev
->external
= 0;
2803 mddev
->major_version
= 0;
2804 mddev
->minor_version
= 90;
2807 if (strncmp(buf
, "external:", 9) == 0) {
2808 size_t namelen
= len
-9;
2809 if (namelen
>= sizeof(mddev
->metadata_type
))
2810 namelen
= sizeof(mddev
->metadata_type
)-1;
2811 strncpy(mddev
->metadata_type
, buf
+9, namelen
);
2812 mddev
->metadata_type
[namelen
] = 0;
2813 if (namelen
&& mddev
->metadata_type
[namelen
-1] == '\n')
2814 mddev
->metadata_type
[--namelen
] = 0;
2815 mddev
->persistent
= 0;
2816 mddev
->external
= 1;
2817 mddev
->major_version
= 0;
2818 mddev
->minor_version
= 90;
2821 major
= simple_strtoul(buf
, &e
, 10);
2822 if (e
==buf
|| *e
!= '.')
2825 minor
= simple_strtoul(buf
, &e
, 10);
2826 if (e
==buf
|| (*e
&& *e
!= '\n') )
2828 if (major
>= ARRAY_SIZE(super_types
) || super_types
[major
].name
== NULL
)
2830 mddev
->major_version
= major
;
2831 mddev
->minor_version
= minor
;
2832 mddev
->persistent
= 1;
2833 mddev
->external
= 0;
2837 static struct md_sysfs_entry md_metadata
=
2838 __ATTR(metadata_version
, S_IRUGO
|S_IWUSR
, metadata_show
, metadata_store
);
2841 action_show(mddev_t
*mddev
, char *page
)
2843 char *type
= "idle";
2844 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
2845 (!mddev
->ro
&& test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))) {
2846 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
2848 else if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
2849 if (!test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
2851 else if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
2858 return sprintf(page
, "%s\n", type
);
2862 action_store(mddev_t
*mddev
, const char *page
, size_t len
)
2864 if (!mddev
->pers
|| !mddev
->pers
->sync_request
)
2867 if (cmd_match(page
, "idle")) {
2868 if (mddev
->sync_thread
) {
2869 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
2870 md_unregister_thread(mddev
->sync_thread
);
2871 mddev
->sync_thread
= NULL
;
2872 mddev
->recovery
= 0;
2874 } else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
2875 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
2877 else if (cmd_match(page
, "resync") || cmd_match(page
, "recover"))
2878 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
2879 else if (cmd_match(page
, "reshape")) {
2881 if (mddev
->pers
->start_reshape
== NULL
)
2883 err
= mddev
->pers
->start_reshape(mddev
);
2887 if (cmd_match(page
, "check"))
2888 set_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
2889 else if (!cmd_match(page
, "repair"))
2891 set_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
2892 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
2894 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
2895 md_wakeup_thread(mddev
->thread
);
2900 mismatch_cnt_show(mddev_t
*mddev
, char *page
)
2902 return sprintf(page
, "%llu\n",
2903 (unsigned long long) mddev
->resync_mismatches
);
2906 static struct md_sysfs_entry md_scan_mode
=
2907 __ATTR(sync_action
, S_IRUGO
|S_IWUSR
, action_show
, action_store
);
2910 static struct md_sysfs_entry md_mismatches
= __ATTR_RO(mismatch_cnt
);
2913 sync_min_show(mddev_t
*mddev
, char *page
)
2915 return sprintf(page
, "%d (%s)\n", speed_min(mddev
),
2916 mddev
->sync_speed_min
? "local": "system");
2920 sync_min_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2924 if (strncmp(buf
, "system", 6)==0) {
2925 mddev
->sync_speed_min
= 0;
2928 min
= simple_strtoul(buf
, &e
, 10);
2929 if (buf
== e
|| (*e
&& *e
!= '\n') || min
<= 0)
2931 mddev
->sync_speed_min
= min
;
2935 static struct md_sysfs_entry md_sync_min
=
2936 __ATTR(sync_speed_min
, S_IRUGO
|S_IWUSR
, sync_min_show
, sync_min_store
);
2939 sync_max_show(mddev_t
*mddev
, char *page
)
2941 return sprintf(page
, "%d (%s)\n", speed_max(mddev
),
2942 mddev
->sync_speed_max
? "local": "system");
2946 sync_max_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2950 if (strncmp(buf
, "system", 6)==0) {
2951 mddev
->sync_speed_max
= 0;
2954 max
= simple_strtoul(buf
, &e
, 10);
2955 if (buf
== e
|| (*e
&& *e
!= '\n') || max
<= 0)
2957 mddev
->sync_speed_max
= max
;
2961 static struct md_sysfs_entry md_sync_max
=
2962 __ATTR(sync_speed_max
, S_IRUGO
|S_IWUSR
, sync_max_show
, sync_max_store
);
2965 degraded_show(mddev_t
*mddev
, char *page
)
2967 return sprintf(page
, "%d\n", mddev
->degraded
);
2969 static struct md_sysfs_entry md_degraded
= __ATTR_RO(degraded
);
2972 sync_speed_show(mddev_t
*mddev
, char *page
)
2974 unsigned long resync
, dt
, db
;
2975 resync
= (mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
));
2976 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
2978 db
= resync
- (mddev
->resync_mark_cnt
);
2979 return sprintf(page
, "%ld\n", db
/dt
/2); /* K/sec */
2982 static struct md_sysfs_entry md_sync_speed
= __ATTR_RO(sync_speed
);
2985 sync_completed_show(mddev_t
*mddev
, char *page
)
2987 unsigned long max_blocks
, resync
;
2989 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
2990 max_blocks
= mddev
->resync_max_sectors
;
2992 max_blocks
= mddev
->size
<< 1;
2994 resync
= (mddev
->curr_resync
- atomic_read(&mddev
->recovery_active
));
2995 return sprintf(page
, "%lu / %lu\n", resync
, max_blocks
);
2998 static struct md_sysfs_entry md_sync_completed
= __ATTR_RO(sync_completed
);
3001 max_sync_show(mddev_t
*mddev
, char *page
)
3003 if (mddev
->resync_max
== MaxSector
)
3004 return sprintf(page
, "max\n");
3006 return sprintf(page
, "%llu\n",
3007 (unsigned long long)mddev
->resync_max
);
3010 max_sync_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3012 if (strncmp(buf
, "max", 3) == 0)
3013 mddev
->resync_max
= MaxSector
;
3016 unsigned long long max
= simple_strtoull(buf
, &ep
, 10);
3017 if (ep
== buf
|| (*ep
!= 0 && *ep
!= '\n'))
3019 if (max
< mddev
->resync_max
&&
3020 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3023 /* Must be a multiple of chunk_size */
3024 if (mddev
->chunk_size
) {
3025 if (max
& (sector_t
)((mddev
->chunk_size
>>9)-1))
3028 mddev
->resync_max
= max
;
3030 wake_up(&mddev
->recovery_wait
);
3034 static struct md_sysfs_entry md_max_sync
=
3035 __ATTR(sync_max
, S_IRUGO
|S_IWUSR
, max_sync_show
, max_sync_store
);
3038 suspend_lo_show(mddev_t
*mddev
, char *page
)
3040 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_lo
);
3044 suspend_lo_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3047 unsigned long long new = simple_strtoull(buf
, &e
, 10);
3049 if (mddev
->pers
->quiesce
== NULL
)
3051 if (buf
== e
|| (*e
&& *e
!= '\n'))
3053 if (new >= mddev
->suspend_hi
||
3054 (new > mddev
->suspend_lo
&& new < mddev
->suspend_hi
)) {
3055 mddev
->suspend_lo
= new;
3056 mddev
->pers
->quiesce(mddev
, 2);
3061 static struct md_sysfs_entry md_suspend_lo
=
3062 __ATTR(suspend_lo
, S_IRUGO
|S_IWUSR
, suspend_lo_show
, suspend_lo_store
);
3066 suspend_hi_show(mddev_t
*mddev
, char *page
)
3068 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_hi
);
3072 suspend_hi_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3075 unsigned long long new = simple_strtoull(buf
, &e
, 10);
3077 if (mddev
->pers
->quiesce
== NULL
)
3079 if (buf
== e
|| (*e
&& *e
!= '\n'))
3081 if ((new <= mddev
->suspend_lo
&& mddev
->suspend_lo
>= mddev
->suspend_hi
) ||
3082 (new > mddev
->suspend_lo
&& new > mddev
->suspend_hi
)) {
3083 mddev
->suspend_hi
= new;
3084 mddev
->pers
->quiesce(mddev
, 1);
3085 mddev
->pers
->quiesce(mddev
, 0);
3090 static struct md_sysfs_entry md_suspend_hi
=
3091 __ATTR(suspend_hi
, S_IRUGO
|S_IWUSR
, suspend_hi_show
, suspend_hi_store
);
3094 reshape_position_show(mddev_t
*mddev
, char *page
)
3096 if (mddev
->reshape_position
!= MaxSector
)
3097 return sprintf(page
, "%llu\n",
3098 (unsigned long long)mddev
->reshape_position
);
3099 strcpy(page
, "none\n");
3104 reshape_position_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3107 unsigned long long new = simple_strtoull(buf
, &e
, 10);
3110 if (buf
== e
|| (*e
&& *e
!= '\n'))
3112 mddev
->reshape_position
= new;
3113 mddev
->delta_disks
= 0;
3114 mddev
->new_level
= mddev
->level
;
3115 mddev
->new_layout
= mddev
->layout
;
3116 mddev
->new_chunk
= mddev
->chunk_size
;
3120 static struct md_sysfs_entry md_reshape_position
=
3121 __ATTR(reshape_position
, S_IRUGO
|S_IWUSR
, reshape_position_show
,
3122 reshape_position_store
);
3125 static struct attribute
*md_default_attrs
[] = {
3128 &md_raid_disks
.attr
,
3129 &md_chunk_size
.attr
,
3131 &md_resync_start
.attr
,
3133 &md_new_device
.attr
,
3134 &md_safe_delay
.attr
,
3135 &md_array_state
.attr
,
3136 &md_reshape_position
.attr
,
3140 static struct attribute
*md_redundancy_attrs
[] = {
3142 &md_mismatches
.attr
,
3145 &md_sync_speed
.attr
,
3146 &md_sync_completed
.attr
,
3148 &md_suspend_lo
.attr
,
3149 &md_suspend_hi
.attr
,
3154 static struct attribute_group md_redundancy_group
= {
3156 .attrs
= md_redundancy_attrs
,
3161 md_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
3163 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
3164 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
3169 rv
= mddev_lock(mddev
);
3171 rv
= entry
->show(mddev
, page
);
3172 mddev_unlock(mddev
);
3178 md_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
3179 const char *page
, size_t length
)
3181 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
3182 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
3187 if (!capable(CAP_SYS_ADMIN
))
3189 rv
= mddev_lock(mddev
);
3191 rv
= entry
->store(mddev
, page
, length
);
3192 mddev_unlock(mddev
);
3197 static void md_free(struct kobject
*ko
)
3199 mddev_t
*mddev
= container_of(ko
, mddev_t
, kobj
);
3203 static struct sysfs_ops md_sysfs_ops
= {
3204 .show
= md_attr_show
,
3205 .store
= md_attr_store
,
3207 static struct kobj_type md_ktype
= {
3209 .sysfs_ops
= &md_sysfs_ops
,
3210 .default_attrs
= md_default_attrs
,
3215 static struct kobject
*md_probe(dev_t dev
, int *part
, void *data
)
3217 static DEFINE_MUTEX(disks_mutex
);
3218 mddev_t
*mddev
= mddev_find(dev
);
3219 struct gendisk
*disk
;
3220 int partitioned
= (MAJOR(dev
) != MD_MAJOR
);
3221 int shift
= partitioned
? MdpMinorShift
: 0;
3222 int unit
= MINOR(dev
) >> shift
;
3228 mutex_lock(&disks_mutex
);
3229 if (mddev
->gendisk
) {
3230 mutex_unlock(&disks_mutex
);
3234 disk
= alloc_disk(1 << shift
);
3236 mutex_unlock(&disks_mutex
);
3240 disk
->major
= MAJOR(dev
);
3241 disk
->first_minor
= unit
<< shift
;
3243 sprintf(disk
->disk_name
, "md_d%d", unit
);
3245 sprintf(disk
->disk_name
, "md%d", unit
);
3246 disk
->fops
= &md_fops
;
3247 disk
->private_data
= mddev
;
3248 disk
->queue
= mddev
->queue
;
3250 mddev
->gendisk
= disk
;
3251 mutex_unlock(&disks_mutex
);
3252 error
= kobject_init_and_add(&mddev
->kobj
, &md_ktype
, &disk
->dev
.kobj
,
3255 printk(KERN_WARNING
"md: cannot register %s/md - name in use\n",
3258 kobject_uevent(&mddev
->kobj
, KOBJ_ADD
);
3262 static void md_safemode_timeout(unsigned long data
)
3264 mddev_t
*mddev
= (mddev_t
*) data
;
3266 mddev
->safemode
= 1;
3267 md_wakeup_thread(mddev
->thread
);
3270 static int start_dirty_degraded
;
3272 static int do_md_run(mddev_t
* mddev
)
3276 struct list_head
*tmp
;
3278 struct gendisk
*disk
;
3279 struct mdk_personality
*pers
;
3280 char b
[BDEVNAME_SIZE
];
3282 if (list_empty(&mddev
->disks
))
3283 /* cannot run an array with no devices.. */
3290 * Analyze all RAID superblock(s)
3292 if (!mddev
->raid_disks
) {
3293 if (!mddev
->persistent
)
3298 chunk_size
= mddev
->chunk_size
;
3301 if (chunk_size
> MAX_CHUNK_SIZE
) {
3302 printk(KERN_ERR
"too big chunk_size: %d > %d\n",
3303 chunk_size
, MAX_CHUNK_SIZE
);
3307 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
3309 if ( (1 << ffz(~chunk_size
)) != chunk_size
) {
3310 printk(KERN_ERR
"chunk_size of %d not valid\n", chunk_size
);
3313 if (chunk_size
< PAGE_SIZE
) {
3314 printk(KERN_ERR
"too small chunk_size: %d < %ld\n",
3315 chunk_size
, PAGE_SIZE
);
3319 /* devices must have minimum size of one chunk */
3320 rdev_for_each(rdev
, tmp
, mddev
) {
3321 if (test_bit(Faulty
, &rdev
->flags
))
3323 if (rdev
->size
< chunk_size
/ 1024) {
3325 "md: Dev %s smaller than chunk_size:"
3327 bdevname(rdev
->bdev
,b
),
3328 (unsigned long long)rdev
->size
,
3336 if (mddev
->level
!= LEVEL_NONE
)
3337 request_module("md-level-%d", mddev
->level
);
3338 else if (mddev
->clevel
[0])
3339 request_module("md-%s", mddev
->clevel
);
3343 * Drop all container device buffers, from now on
3344 * the only valid external interface is through the md
3347 rdev_for_each(rdev
, tmp
, mddev
) {
3348 if (test_bit(Faulty
, &rdev
->flags
))
3350 sync_blockdev(rdev
->bdev
);
3351 invalidate_bdev(rdev
->bdev
);
3353 /* perform some consistency tests on the device.
3354 * We don't want the data to overlap the metadata,
3355 * Internal Bitmap issues has handled elsewhere.
3357 if (rdev
->data_offset
< rdev
->sb_offset
) {
3359 rdev
->data_offset
+ mddev
->size
*2
3360 > rdev
->sb_offset
*2) {
3361 printk("md: %s: data overlaps metadata\n",
3366 if (rdev
->sb_offset
*2 + rdev
->sb_size
/512
3367 > rdev
->data_offset
) {
3368 printk("md: %s: metadata overlaps data\n",
3375 md_probe(mddev
->unit
, NULL
, NULL
);
3376 disk
= mddev
->gendisk
;
3380 spin_lock(&pers_lock
);
3381 pers
= find_pers(mddev
->level
, mddev
->clevel
);
3382 if (!pers
|| !try_module_get(pers
->owner
)) {
3383 spin_unlock(&pers_lock
);
3384 if (mddev
->level
!= LEVEL_NONE
)
3385 printk(KERN_WARNING
"md: personality for level %d is not loaded!\n",
3388 printk(KERN_WARNING
"md: personality for level %s is not loaded!\n",
3393 spin_unlock(&pers_lock
);
3394 mddev
->level
= pers
->level
;
3395 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
3397 if (mddev
->reshape_position
!= MaxSector
&&
3398 pers
->start_reshape
== NULL
) {
3399 /* This personality cannot handle reshaping... */
3401 module_put(pers
->owner
);
3405 if (pers
->sync_request
) {
3406 /* Warn if this is a potentially silly
3409 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
3411 struct list_head
*tmp2
;
3413 rdev_for_each(rdev
, tmp
, mddev
) {
3414 rdev_for_each(rdev2
, tmp2
, mddev
) {
3416 rdev
->bdev
->bd_contains
==
3417 rdev2
->bdev
->bd_contains
) {
3419 "%s: WARNING: %s appears to be"
3420 " on the same physical disk as"
3423 bdevname(rdev
->bdev
,b
),
3424 bdevname(rdev2
->bdev
,b2
));
3431 "True protection against single-disk"
3432 " failure might be compromised.\n");
3435 mddev
->recovery
= 0;
3436 mddev
->resync_max_sectors
= mddev
->size
<< 1; /* may be over-ridden by personality */
3437 mddev
->barriers_work
= 1;
3438 mddev
->ok_start_degraded
= start_dirty_degraded
;
3441 mddev
->ro
= 2; /* read-only, but switch on first write */
3443 err
= mddev
->pers
->run(mddev
);
3444 if (!err
&& mddev
->pers
->sync_request
) {
3445 err
= bitmap_create(mddev
);
3447 printk(KERN_ERR
"%s: failed to create bitmap (%d)\n",
3448 mdname(mddev
), err
);
3449 mddev
->pers
->stop(mddev
);
3453 printk(KERN_ERR
"md: pers->run() failed ...\n");
3454 module_put(mddev
->pers
->owner
);
3456 bitmap_destroy(mddev
);
3459 if (mddev
->pers
->sync_request
) {
3460 if (sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
3462 "md: cannot register extra attributes for %s\n",
3464 } else if (mddev
->ro
== 2) /* auto-readonly not meaningful */
3467 atomic_set(&mddev
->writes_pending
,0);
3468 mddev
->safemode
= 0;
3469 mddev
->safemode_timer
.function
= md_safemode_timeout
;
3470 mddev
->safemode_timer
.data
= (unsigned long) mddev
;
3471 mddev
->safemode_delay
= (200 * HZ
)/1000 +1; /* 200 msec delay */
3474 rdev_for_each(rdev
, tmp
, mddev
)
3475 if (rdev
->raid_disk
>= 0) {
3477 sprintf(nm
, "rd%d", rdev
->raid_disk
);
3478 if (sysfs_create_link(&mddev
->kobj
, &rdev
->kobj
, nm
))
3479 printk("md: cannot register %s for %s\n",
3483 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3486 md_update_sb(mddev
, 0);
3488 set_capacity(disk
, mddev
->array_size
<<1);
3490 /* If we call blk_queue_make_request here, it will
3491 * re-initialise max_sectors etc which may have been
3492 * refined inside -> run. So just set the bits we need to set.
3493 * Most initialisation happended when we called
3494 * blk_queue_make_request(..., md_fail_request)
3497 mddev
->queue
->queuedata
= mddev
;
3498 mddev
->queue
->make_request_fn
= mddev
->pers
->make_request
;
3500 /* If there is a partially-recovered drive we need to
3501 * start recovery here. If we leave it to md_check_recovery,
3502 * it will remove the drives and not do the right thing
3504 if (mddev
->degraded
&& !mddev
->sync_thread
) {
3505 struct list_head
*rtmp
;
3507 rdev_for_each(rdev
, rtmp
, mddev
)
3508 if (rdev
->raid_disk
>= 0 &&
3509 !test_bit(In_sync
, &rdev
->flags
) &&
3510 !test_bit(Faulty
, &rdev
->flags
))
3511 /* complete an interrupted recovery */
3513 if (spares
&& mddev
->pers
->sync_request
) {
3514 mddev
->recovery
= 0;
3515 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
3516 mddev
->sync_thread
= md_register_thread(md_do_sync
,
3519 if (!mddev
->sync_thread
) {
3520 printk(KERN_ERR
"%s: could not start resync"
3523 /* leave the spares where they are, it shouldn't hurt */
3524 mddev
->recovery
= 0;
3528 md_wakeup_thread(mddev
->thread
);
3529 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
3532 md_new_event(mddev
);
3533 kobject_uevent(&mddev
->gendisk
->dev
.kobj
, KOBJ_CHANGE
);
3537 static int restart_array(mddev_t
*mddev
)
3539 struct gendisk
*disk
= mddev
->gendisk
;
3543 * Complain if it has no devices
3546 if (list_empty(&mddev
->disks
))
3554 mddev
->safemode
= 0;
3556 set_disk_ro(disk
, 0);
3558 printk(KERN_INFO
"md: %s switched to read-write mode.\n",
3561 * Kick recovery or resync if necessary
3563 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3564 md_wakeup_thread(mddev
->thread
);
3565 md_wakeup_thread(mddev
->sync_thread
);
3574 /* similar to deny_write_access, but accounts for our holding a reference
3575 * to the file ourselves */
3576 static int deny_bitmap_write_access(struct file
* file
)
3578 struct inode
*inode
= file
->f_mapping
->host
;
3580 spin_lock(&inode
->i_lock
);
3581 if (atomic_read(&inode
->i_writecount
) > 1) {
3582 spin_unlock(&inode
->i_lock
);
3585 atomic_set(&inode
->i_writecount
, -1);
3586 spin_unlock(&inode
->i_lock
);
3591 static void restore_bitmap_write_access(struct file
*file
)
3593 struct inode
*inode
= file
->f_mapping
->host
;
3595 spin_lock(&inode
->i_lock
);
3596 atomic_set(&inode
->i_writecount
, 1);
3597 spin_unlock(&inode
->i_lock
);
3601 * 0 - completely stop and dis-assemble array
3602 * 1 - switch to readonly
3603 * 2 - stop but do not disassemble array
3605 static int do_md_stop(mddev_t
* mddev
, int mode
)
3608 struct gendisk
*disk
= mddev
->gendisk
;
3611 if (atomic_read(&mddev
->active
)>2) {
3612 printk("md: %s still in use.\n",mdname(mddev
));
3616 if (mddev
->sync_thread
) {
3617 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
3618 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
3619 md_unregister_thread(mddev
->sync_thread
);
3620 mddev
->sync_thread
= NULL
;
3623 del_timer_sync(&mddev
->safemode_timer
);
3625 invalidate_partition(disk
, 0);
3628 case 1: /* readonly */
3634 case 0: /* disassemble */
3636 bitmap_flush(mddev
);
3637 md_super_wait(mddev
);
3639 set_disk_ro(disk
, 0);
3640 blk_queue_make_request(mddev
->queue
, md_fail_request
);
3641 mddev
->pers
->stop(mddev
);
3642 mddev
->queue
->merge_bvec_fn
= NULL
;
3643 mddev
->queue
->unplug_fn
= NULL
;
3644 mddev
->queue
->backing_dev_info
.congested_fn
= NULL
;
3645 if (mddev
->pers
->sync_request
)
3646 sysfs_remove_group(&mddev
->kobj
, &md_redundancy_group
);
3648 module_put(mddev
->pers
->owner
);
3651 set_capacity(disk
, 0);
3657 if (!mddev
->in_sync
|| mddev
->flags
) {
3658 /* mark array as shutdown cleanly */
3660 md_update_sb(mddev
, 1);
3663 set_disk_ro(disk
, 1);
3664 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
3668 * Free resources if final stop
3672 struct list_head
*tmp
;
3674 printk(KERN_INFO
"md: %s stopped.\n", mdname(mddev
));
3676 bitmap_destroy(mddev
);
3677 if (mddev
->bitmap_file
) {
3678 restore_bitmap_write_access(mddev
->bitmap_file
);
3679 fput(mddev
->bitmap_file
);
3680 mddev
->bitmap_file
= NULL
;
3682 mddev
->bitmap_offset
= 0;
3684 rdev_for_each(rdev
, tmp
, mddev
)
3685 if (rdev
->raid_disk
>= 0) {
3687 sprintf(nm
, "rd%d", rdev
->raid_disk
);
3688 sysfs_remove_link(&mddev
->kobj
, nm
);
3691 /* make sure all md_delayed_delete calls have finished */
3692 flush_scheduled_work();
3694 export_array(mddev
);
3696 mddev
->array_size
= 0;
3698 mddev
->raid_disks
= 0;
3699 mddev
->recovery_cp
= 0;
3700 mddev
->resync_max
= MaxSector
;
3701 mddev
->reshape_position
= MaxSector
;
3702 mddev
->external
= 0;
3703 mddev
->persistent
= 0;
3705 } else if (mddev
->pers
)
3706 printk(KERN_INFO
"md: %s switched to read-only mode.\n",
3709 md_new_event(mddev
);
3715 static void autorun_array(mddev_t
*mddev
)
3718 struct list_head
*tmp
;
3721 if (list_empty(&mddev
->disks
))
3724 printk(KERN_INFO
"md: running: ");
3726 rdev_for_each(rdev
, tmp
, mddev
) {
3727 char b
[BDEVNAME_SIZE
];
3728 printk("<%s>", bdevname(rdev
->bdev
,b
));
3732 err
= do_md_run (mddev
);
3734 printk(KERN_WARNING
"md: do_md_run() returned %d\n", err
);
3735 do_md_stop (mddev
, 0);
3740 * lets try to run arrays based on all disks that have arrived
3741 * until now. (those are in pending_raid_disks)
3743 * the method: pick the first pending disk, collect all disks with
3744 * the same UUID, remove all from the pending list and put them into
3745 * the 'same_array' list. Then order this list based on superblock
3746 * update time (freshest comes first), kick out 'old' disks and
3747 * compare superblocks. If everything's fine then run it.
3749 * If "unit" is allocated, then bump its reference count
3751 static void autorun_devices(int part
)
3753 struct list_head
*tmp
;
3754 mdk_rdev_t
*rdev0
, *rdev
;
3756 char b
[BDEVNAME_SIZE
];
3758 printk(KERN_INFO
"md: autorun ...\n");
3759 while (!list_empty(&pending_raid_disks
)) {
3762 LIST_HEAD(candidates
);
3763 rdev0
= list_entry(pending_raid_disks
.next
,
3764 mdk_rdev_t
, same_set
);
3766 printk(KERN_INFO
"md: considering %s ...\n",
3767 bdevname(rdev0
->bdev
,b
));
3768 INIT_LIST_HEAD(&candidates
);
3769 rdev_for_each_list(rdev
, tmp
, pending_raid_disks
)
3770 if (super_90_load(rdev
, rdev0
, 0) >= 0) {
3771 printk(KERN_INFO
"md: adding %s ...\n",
3772 bdevname(rdev
->bdev
,b
));
3773 list_move(&rdev
->same_set
, &candidates
);
3776 * now we have a set of devices, with all of them having
3777 * mostly sane superblocks. It's time to allocate the
3781 dev
= MKDEV(mdp_major
,
3782 rdev0
->preferred_minor
<< MdpMinorShift
);
3783 unit
= MINOR(dev
) >> MdpMinorShift
;
3785 dev
= MKDEV(MD_MAJOR
, rdev0
->preferred_minor
);
3788 if (rdev0
->preferred_minor
!= unit
) {
3789 printk(KERN_INFO
"md: unit number in %s is bad: %d\n",
3790 bdevname(rdev0
->bdev
, b
), rdev0
->preferred_minor
);
3794 md_probe(dev
, NULL
, NULL
);
3795 mddev
= mddev_find(dev
);
3798 "md: cannot allocate memory for md drive.\n");
3801 if (mddev_lock(mddev
))
3802 printk(KERN_WARNING
"md: %s locked, cannot run\n",
3804 else if (mddev
->raid_disks
|| mddev
->major_version
3805 || !list_empty(&mddev
->disks
)) {
3807 "md: %s already running, cannot run %s\n",
3808 mdname(mddev
), bdevname(rdev0
->bdev
,b
));
3809 mddev_unlock(mddev
);
3811 printk(KERN_INFO
"md: created %s\n", mdname(mddev
));
3812 mddev
->persistent
= 1;
3813 rdev_for_each_list(rdev
, tmp
, candidates
) {
3814 list_del_init(&rdev
->same_set
);
3815 if (bind_rdev_to_array(rdev
, mddev
))
3818 autorun_array(mddev
);
3819 mddev_unlock(mddev
);
3821 /* on success, candidates will be empty, on error
3824 rdev_for_each_list(rdev
, tmp
, candidates
)
3828 printk(KERN_INFO
"md: ... autorun DONE.\n");
3830 #endif /* !MODULE */
3832 static int get_version(void __user
* arg
)
3836 ver
.major
= MD_MAJOR_VERSION
;
3837 ver
.minor
= MD_MINOR_VERSION
;
3838 ver
.patchlevel
= MD_PATCHLEVEL_VERSION
;
3840 if (copy_to_user(arg
, &ver
, sizeof(ver
)))
3846 static int get_array_info(mddev_t
* mddev
, void __user
* arg
)
3848 mdu_array_info_t info
;
3849 int nr
,working
,active
,failed
,spare
;
3851 struct list_head
*tmp
;
3853 nr
=working
=active
=failed
=spare
=0;
3854 rdev_for_each(rdev
, tmp
, mddev
) {
3856 if (test_bit(Faulty
, &rdev
->flags
))
3860 if (test_bit(In_sync
, &rdev
->flags
))
3867 info
.major_version
= mddev
->major_version
;
3868 info
.minor_version
= mddev
->minor_version
;
3869 info
.patch_version
= MD_PATCHLEVEL_VERSION
;
3870 info
.ctime
= mddev
->ctime
;
3871 info
.level
= mddev
->level
;
3872 info
.size
= mddev
->size
;
3873 if (info
.size
!= mddev
->size
) /* overflow */
3876 info
.raid_disks
= mddev
->raid_disks
;
3877 info
.md_minor
= mddev
->md_minor
;
3878 info
.not_persistent
= !mddev
->persistent
;
3880 info
.utime
= mddev
->utime
;
3883 info
.state
= (1<<MD_SB_CLEAN
);
3884 if (mddev
->bitmap
&& mddev
->bitmap_offset
)
3885 info
.state
= (1<<MD_SB_BITMAP_PRESENT
);
3886 info
.active_disks
= active
;
3887 info
.working_disks
= working
;
3888 info
.failed_disks
= failed
;
3889 info
.spare_disks
= spare
;
3891 info
.layout
= mddev
->layout
;
3892 info
.chunk_size
= mddev
->chunk_size
;
3894 if (copy_to_user(arg
, &info
, sizeof(info
)))
3900 static int get_bitmap_file(mddev_t
* mddev
, void __user
* arg
)
3902 mdu_bitmap_file_t
*file
= NULL
; /* too big for stack allocation */
3903 char *ptr
, *buf
= NULL
;
3906 md_allow_write(mddev
);
3908 file
= kmalloc(sizeof(*file
), GFP_KERNEL
);
3912 /* bitmap disabled, zero the first byte and copy out */
3913 if (!mddev
->bitmap
|| !mddev
->bitmap
->file
) {
3914 file
->pathname
[0] = '\0';
3918 buf
= kmalloc(sizeof(file
->pathname
), GFP_KERNEL
);
3922 ptr
= file_path(mddev
->bitmap
->file
, buf
, sizeof(file
->pathname
));
3926 strcpy(file
->pathname
, ptr
);
3930 if (copy_to_user(arg
, file
, sizeof(*file
)))
3938 static int get_disk_info(mddev_t
* mddev
, void __user
* arg
)
3940 mdu_disk_info_t info
;
3944 if (copy_from_user(&info
, arg
, sizeof(info
)))
3949 rdev
= find_rdev_nr(mddev
, nr
);
3951 info
.major
= MAJOR(rdev
->bdev
->bd_dev
);
3952 info
.minor
= MINOR(rdev
->bdev
->bd_dev
);
3953 info
.raid_disk
= rdev
->raid_disk
;
3955 if (test_bit(Faulty
, &rdev
->flags
))
3956 info
.state
|= (1<<MD_DISK_FAULTY
);
3957 else if (test_bit(In_sync
, &rdev
->flags
)) {
3958 info
.state
|= (1<<MD_DISK_ACTIVE
);
3959 info
.state
|= (1<<MD_DISK_SYNC
);
3961 if (test_bit(WriteMostly
, &rdev
->flags
))
3962 info
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
3964 info
.major
= info
.minor
= 0;
3965 info
.raid_disk
= -1;
3966 info
.state
= (1<<MD_DISK_REMOVED
);
3969 if (copy_to_user(arg
, &info
, sizeof(info
)))
3975 static int add_new_disk(mddev_t
* mddev
, mdu_disk_info_t
*info
)
3977 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
3979 dev_t dev
= MKDEV(info
->major
,info
->minor
);
3981 if (info
->major
!= MAJOR(dev
) || info
->minor
!= MINOR(dev
))
3984 if (!mddev
->raid_disks
) {
3986 /* expecting a device which has a superblock */
3987 rdev
= md_import_device(dev
, mddev
->major_version
, mddev
->minor_version
);
3990 "md: md_import_device returned %ld\n",
3992 return PTR_ERR(rdev
);
3994 if (!list_empty(&mddev
->disks
)) {
3995 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
3996 mdk_rdev_t
, same_set
);
3997 int err
= super_types
[mddev
->major_version
]
3998 .load_super(rdev
, rdev0
, mddev
->minor_version
);
4001 "md: %s has different UUID to %s\n",
4002 bdevname(rdev
->bdev
,b
),
4003 bdevname(rdev0
->bdev
,b2
));
4008 err
= bind_rdev_to_array(rdev
, mddev
);
4015 * add_new_disk can be used once the array is assembled
4016 * to add "hot spares". They must already have a superblock
4021 if (!mddev
->pers
->hot_add_disk
) {
4023 "%s: personality does not support diskops!\n",
4027 if (mddev
->persistent
)
4028 rdev
= md_import_device(dev
, mddev
->major_version
,
4029 mddev
->minor_version
);
4031 rdev
= md_import_device(dev
, -1, -1);
4034 "md: md_import_device returned %ld\n",
4036 return PTR_ERR(rdev
);
4038 /* set save_raid_disk if appropriate */
4039 if (!mddev
->persistent
) {
4040 if (info
->state
& (1<<MD_DISK_SYNC
) &&
4041 info
->raid_disk
< mddev
->raid_disks
)
4042 rdev
->raid_disk
= info
->raid_disk
;
4044 rdev
->raid_disk
= -1;
4046 super_types
[mddev
->major_version
].
4047 validate_super(mddev
, rdev
);
4048 rdev
->saved_raid_disk
= rdev
->raid_disk
;
4050 clear_bit(In_sync
, &rdev
->flags
); /* just to be sure */
4051 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
4052 set_bit(WriteMostly
, &rdev
->flags
);
4054 rdev
->raid_disk
= -1;
4055 err
= bind_rdev_to_array(rdev
, mddev
);
4056 if (!err
&& !mddev
->pers
->hot_remove_disk
) {
4057 /* If there is hot_add_disk but no hot_remove_disk
4058 * then added disks for geometry changes,
4059 * and should be added immediately.
4061 super_types
[mddev
->major_version
].
4062 validate_super(mddev
, rdev
);
4063 err
= mddev
->pers
->hot_add_disk(mddev
, rdev
);
4065 unbind_rdev_from_array(rdev
);
4070 md_update_sb(mddev
, 1);
4071 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4072 md_wakeup_thread(mddev
->thread
);
4076 /* otherwise, add_new_disk is only allowed
4077 * for major_version==0 superblocks
4079 if (mddev
->major_version
!= 0) {
4080 printk(KERN_WARNING
"%s: ADD_NEW_DISK not supported\n",
4085 if (!(info
->state
& (1<<MD_DISK_FAULTY
))) {
4087 rdev
= md_import_device (dev
, -1, 0);
4090 "md: error, md_import_device() returned %ld\n",
4092 return PTR_ERR(rdev
);
4094 rdev
->desc_nr
= info
->number
;
4095 if (info
->raid_disk
< mddev
->raid_disks
)
4096 rdev
->raid_disk
= info
->raid_disk
;
4098 rdev
->raid_disk
= -1;
4100 if (rdev
->raid_disk
< mddev
->raid_disks
)
4101 if (info
->state
& (1<<MD_DISK_SYNC
))
4102 set_bit(In_sync
, &rdev
->flags
);
4104 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
4105 set_bit(WriteMostly
, &rdev
->flags
);
4107 if (!mddev
->persistent
) {
4108 printk(KERN_INFO
"md: nonpersistent superblock ...\n");
4109 rdev
->sb_offset
= rdev
->bdev
->bd_inode
->i_size
>> BLOCK_SIZE_BITS
;
4111 rdev
->sb_offset
= calc_dev_sboffset(rdev
->bdev
);
4112 rdev
->size
= calc_dev_size(rdev
, mddev
->chunk_size
);
4114 err
= bind_rdev_to_array(rdev
, mddev
);
4124 static int hot_remove_disk(mddev_t
* mddev
, dev_t dev
)
4126 char b
[BDEVNAME_SIZE
];
4132 rdev
= find_rdev(mddev
, dev
);
4136 if (rdev
->raid_disk
>= 0)
4139 kick_rdev_from_array(rdev
);
4140 md_update_sb(mddev
, 1);
4141 md_new_event(mddev
);
4145 printk(KERN_WARNING
"md: cannot remove active disk %s from %s ... \n",
4146 bdevname(rdev
->bdev
,b
), mdname(mddev
));
4150 static int hot_add_disk(mddev_t
* mddev
, dev_t dev
)
4152 char b
[BDEVNAME_SIZE
];
4160 if (mddev
->major_version
!= 0) {
4161 printk(KERN_WARNING
"%s: HOT_ADD may only be used with"
4162 " version-0 superblocks.\n",
4166 if (!mddev
->pers
->hot_add_disk
) {
4168 "%s: personality does not support diskops!\n",
4173 rdev
= md_import_device (dev
, -1, 0);
4176 "md: error, md_import_device() returned %ld\n",
4181 if (mddev
->persistent
)
4182 rdev
->sb_offset
= calc_dev_sboffset(rdev
->bdev
);
4185 rdev
->bdev
->bd_inode
->i_size
>> BLOCK_SIZE_BITS
;
4187 size
= calc_dev_size(rdev
, mddev
->chunk_size
);
4190 if (test_bit(Faulty
, &rdev
->flags
)) {
4192 "md: can not hot-add faulty %s disk to %s!\n",
4193 bdevname(rdev
->bdev
,b
), mdname(mddev
));
4197 clear_bit(In_sync
, &rdev
->flags
);
4199 rdev
->saved_raid_disk
= -1;
4200 err
= bind_rdev_to_array(rdev
, mddev
);
4205 * The rest should better be atomic, we can have disk failures
4206 * noticed in interrupt contexts ...
4209 if (rdev
->desc_nr
== mddev
->max_disks
) {
4210 printk(KERN_WARNING
"%s: can not hot-add to full array!\n",
4213 goto abort_unbind_export
;
4216 rdev
->raid_disk
= -1;
4218 md_update_sb(mddev
, 1);
4221 * Kick recovery, maybe this spare has to be added to the
4222 * array immediately.
4224 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4225 md_wakeup_thread(mddev
->thread
);
4226 md_new_event(mddev
);
4229 abort_unbind_export
:
4230 unbind_rdev_from_array(rdev
);
4237 static int set_bitmap_file(mddev_t
*mddev
, int fd
)
4242 if (!mddev
->pers
->quiesce
)
4244 if (mddev
->recovery
|| mddev
->sync_thread
)
4246 /* we should be able to change the bitmap.. */
4252 return -EEXIST
; /* cannot add when bitmap is present */
4253 mddev
->bitmap_file
= fget(fd
);
4255 if (mddev
->bitmap_file
== NULL
) {
4256 printk(KERN_ERR
"%s: error: failed to get bitmap file\n",
4261 err
= deny_bitmap_write_access(mddev
->bitmap_file
);
4263 printk(KERN_ERR
"%s: error: bitmap file is already in use\n",
4265 fput(mddev
->bitmap_file
);
4266 mddev
->bitmap_file
= NULL
;
4269 mddev
->bitmap_offset
= 0; /* file overrides offset */
4270 } else if (mddev
->bitmap
== NULL
)
4271 return -ENOENT
; /* cannot remove what isn't there */
4274 mddev
->pers
->quiesce(mddev
, 1);
4276 err
= bitmap_create(mddev
);
4277 if (fd
< 0 || err
) {
4278 bitmap_destroy(mddev
);
4279 fd
= -1; /* make sure to put the file */
4281 mddev
->pers
->quiesce(mddev
, 0);
4284 if (mddev
->bitmap_file
) {
4285 restore_bitmap_write_access(mddev
->bitmap_file
);
4286 fput(mddev
->bitmap_file
);
4288 mddev
->bitmap_file
= NULL
;
4295 * set_array_info is used two different ways
4296 * The original usage is when creating a new array.
4297 * In this usage, raid_disks is > 0 and it together with
4298 * level, size, not_persistent,layout,chunksize determine the
4299 * shape of the array.
4300 * This will always create an array with a type-0.90.0 superblock.
4301 * The newer usage is when assembling an array.
4302 * In this case raid_disks will be 0, and the major_version field is
4303 * use to determine which style super-blocks are to be found on the devices.
4304 * The minor and patch _version numbers are also kept incase the
4305 * super_block handler wishes to interpret them.
4307 static int set_array_info(mddev_t
* mddev
, mdu_array_info_t
*info
)
4310 if (info
->raid_disks
== 0) {
4311 /* just setting version number for superblock loading */
4312 if (info
->major_version
< 0 ||
4313 info
->major_version
>= ARRAY_SIZE(super_types
) ||
4314 super_types
[info
->major_version
].name
== NULL
) {
4315 /* maybe try to auto-load a module? */
4317 "md: superblock version %d not known\n",
4318 info
->major_version
);
4321 mddev
->major_version
= info
->major_version
;
4322 mddev
->minor_version
= info
->minor_version
;
4323 mddev
->patch_version
= info
->patch_version
;
4324 mddev
->persistent
= !info
->not_persistent
;
4327 mddev
->major_version
= MD_MAJOR_VERSION
;
4328 mddev
->minor_version
= MD_MINOR_VERSION
;
4329 mddev
->patch_version
= MD_PATCHLEVEL_VERSION
;
4330 mddev
->ctime
= get_seconds();
4332 mddev
->level
= info
->level
;
4333 mddev
->clevel
[0] = 0;
4334 mddev
->size
= info
->size
;
4335 mddev
->raid_disks
= info
->raid_disks
;
4336 /* don't set md_minor, it is determined by which /dev/md* was
4339 if (info
->state
& (1<<MD_SB_CLEAN
))
4340 mddev
->recovery_cp
= MaxSector
;
4342 mddev
->recovery_cp
= 0;
4343 mddev
->persistent
= ! info
->not_persistent
;
4344 mddev
->external
= 0;
4346 mddev
->layout
= info
->layout
;
4347 mddev
->chunk_size
= info
->chunk_size
;
4349 mddev
->max_disks
= MD_SB_DISKS
;
4351 if (mddev
->persistent
)
4353 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
4355 mddev
->default_bitmap_offset
= MD_SB_BYTES
>> 9;
4356 mddev
->bitmap_offset
= 0;
4358 mddev
->reshape_position
= MaxSector
;
4361 * Generate a 128 bit UUID
4363 get_random_bytes(mddev
->uuid
, 16);
4365 mddev
->new_level
= mddev
->level
;
4366 mddev
->new_chunk
= mddev
->chunk_size
;
4367 mddev
->new_layout
= mddev
->layout
;
4368 mddev
->delta_disks
= 0;
4373 static int update_size(mddev_t
*mddev
, unsigned long size
)
4377 struct list_head
*tmp
;
4378 int fit
= (size
== 0);
4380 if (mddev
->pers
->resize
== NULL
)
4382 /* The "size" is the amount of each device that is used.
4383 * This can only make sense for arrays with redundancy.
4384 * linear and raid0 always use whatever space is available
4385 * We can only consider changing the size if no resync
4386 * or reconstruction is happening, and if the new size
4387 * is acceptable. It must fit before the sb_offset or,
4388 * if that is <data_offset, it must fit before the
4389 * size of each device.
4390 * If size is zero, we find the largest size that fits.
4392 if (mddev
->sync_thread
)
4394 rdev_for_each(rdev
, tmp
, mddev
) {
4396 avail
= rdev
->size
* 2;
4398 if (fit
&& (size
== 0 || size
> avail
/2))
4400 if (avail
< ((sector_t
)size
<< 1))
4403 rv
= mddev
->pers
->resize(mddev
, (sector_t
)size
*2);
4405 struct block_device
*bdev
;
4407 bdev
= bdget_disk(mddev
->gendisk
, 0);
4409 mutex_lock(&bdev
->bd_inode
->i_mutex
);
4410 i_size_write(bdev
->bd_inode
, (loff_t
)mddev
->array_size
<< 10);
4411 mutex_unlock(&bdev
->bd_inode
->i_mutex
);
4418 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
)
4421 /* change the number of raid disks */
4422 if (mddev
->pers
->check_reshape
== NULL
)
4424 if (raid_disks
<= 0 ||
4425 raid_disks
>= mddev
->max_disks
)
4427 if (mddev
->sync_thread
|| mddev
->reshape_position
!= MaxSector
)
4429 mddev
->delta_disks
= raid_disks
- mddev
->raid_disks
;
4431 rv
= mddev
->pers
->check_reshape(mddev
);
4437 * update_array_info is used to change the configuration of an
4439 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
4440 * fields in the info are checked against the array.
4441 * Any differences that cannot be handled will cause an error.
4442 * Normally, only one change can be managed at a time.
4444 static int update_array_info(mddev_t
*mddev
, mdu_array_info_t
*info
)
4450 /* calculate expected state,ignoring low bits */
4451 if (mddev
->bitmap
&& mddev
->bitmap_offset
)
4452 state
|= (1 << MD_SB_BITMAP_PRESENT
);
4454 if (mddev
->major_version
!= info
->major_version
||
4455 mddev
->minor_version
!= info
->minor_version
||
4456 /* mddev->patch_version != info->patch_version || */
4457 mddev
->ctime
!= info
->ctime
||
4458 mddev
->level
!= info
->level
||
4459 /* mddev->layout != info->layout || */
4460 !mddev
->persistent
!= info
->not_persistent
||
4461 mddev
->chunk_size
!= info
->chunk_size
||
4462 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
4463 ((state
^info
->state
) & 0xfffffe00)
4466 /* Check there is only one change */
4467 if (info
->size
>= 0 && mddev
->size
!= info
->size
) cnt
++;
4468 if (mddev
->raid_disks
!= info
->raid_disks
) cnt
++;
4469 if (mddev
->layout
!= info
->layout
) cnt
++;
4470 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) cnt
++;
4471 if (cnt
== 0) return 0;
4472 if (cnt
> 1) return -EINVAL
;
4474 if (mddev
->layout
!= info
->layout
) {
4476 * we don't need to do anything at the md level, the
4477 * personality will take care of it all.
4479 if (mddev
->pers
->reconfig
== NULL
)
4482 return mddev
->pers
->reconfig(mddev
, info
->layout
, -1);
4484 if (info
->size
>= 0 && mddev
->size
!= info
->size
)
4485 rv
= update_size(mddev
, info
->size
);
4487 if (mddev
->raid_disks
!= info
->raid_disks
)
4488 rv
= update_raid_disks(mddev
, info
->raid_disks
);
4490 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) {
4491 if (mddev
->pers
->quiesce
== NULL
)
4493 if (mddev
->recovery
|| mddev
->sync_thread
)
4495 if (info
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
4496 /* add the bitmap */
4499 if (mddev
->default_bitmap_offset
== 0)
4501 mddev
->bitmap_offset
= mddev
->default_bitmap_offset
;
4502 mddev
->pers
->quiesce(mddev
, 1);
4503 rv
= bitmap_create(mddev
);
4505 bitmap_destroy(mddev
);
4506 mddev
->pers
->quiesce(mddev
, 0);
4508 /* remove the bitmap */
4511 if (mddev
->bitmap
->file
)
4513 mddev
->pers
->quiesce(mddev
, 1);
4514 bitmap_destroy(mddev
);
4515 mddev
->pers
->quiesce(mddev
, 0);
4516 mddev
->bitmap_offset
= 0;
4519 md_update_sb(mddev
, 1);
4523 static int set_disk_faulty(mddev_t
*mddev
, dev_t dev
)
4527 if (mddev
->pers
== NULL
)
4530 rdev
= find_rdev(mddev
, dev
);
4534 md_error(mddev
, rdev
);
4538 static int md_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
4540 mddev_t
*mddev
= bdev
->bd_disk
->private_data
;
4544 geo
->cylinders
= get_capacity(mddev
->gendisk
) / 8;
4548 static int md_ioctl(struct inode
*inode
, struct file
*file
,
4549 unsigned int cmd
, unsigned long arg
)
4552 void __user
*argp
= (void __user
*)arg
;
4553 mddev_t
*mddev
= NULL
;
4555 if (!capable(CAP_SYS_ADMIN
))
4559 * Commands dealing with the RAID driver but not any
4565 err
= get_version(argp
);
4568 case PRINT_RAID_DEBUG
:
4576 autostart_arrays(arg
);
4583 * Commands creating/starting a new array:
4586 mddev
= inode
->i_bdev
->bd_disk
->private_data
;
4593 err
= mddev_lock(mddev
);
4596 "md: ioctl lock interrupted, reason %d, cmd %d\n",
4603 case SET_ARRAY_INFO
:
4605 mdu_array_info_t info
;
4607 memset(&info
, 0, sizeof(info
));
4608 else if (copy_from_user(&info
, argp
, sizeof(info
))) {
4613 err
= update_array_info(mddev
, &info
);
4615 printk(KERN_WARNING
"md: couldn't update"
4616 " array info. %d\n", err
);
4621 if (!list_empty(&mddev
->disks
)) {
4623 "md: array %s already has disks!\n",
4628 if (mddev
->raid_disks
) {
4630 "md: array %s already initialised!\n",
4635 err
= set_array_info(mddev
, &info
);
4637 printk(KERN_WARNING
"md: couldn't set"
4638 " array info. %d\n", err
);
4648 * Commands querying/configuring an existing array:
4650 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
4651 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
4652 if ((!mddev
->raid_disks
&& !mddev
->external
)
4653 && cmd
!= ADD_NEW_DISK
&& cmd
!= STOP_ARRAY
4654 && cmd
!= RUN_ARRAY
&& cmd
!= SET_BITMAP_FILE
4655 && cmd
!= GET_BITMAP_FILE
) {
4661 * Commands even a read-only array can execute:
4665 case GET_ARRAY_INFO
:
4666 err
= get_array_info(mddev
, argp
);
4669 case GET_BITMAP_FILE
:
4670 err
= get_bitmap_file(mddev
, argp
);
4674 err
= get_disk_info(mddev
, argp
);
4677 case RESTART_ARRAY_RW
:
4678 err
= restart_array(mddev
);
4682 err
= do_md_stop (mddev
, 0);
4686 err
= do_md_stop (mddev
, 1);
4690 * We have a problem here : there is no easy way to give a CHS
4691 * virtual geometry. We currently pretend that we have a 2 heads
4692 * 4 sectors (with a BIG number of cylinders...). This drives
4693 * dosfs just mad... ;-)
4698 * The remaining ioctls are changing the state of the
4699 * superblock, so we do not allow them on read-only arrays.
4700 * However non-MD ioctls (e.g. get-size) will still come through
4701 * here and hit the 'default' below, so only disallow
4702 * 'md' ioctls, and switch to rw mode if started auto-readonly.
4704 if (_IOC_TYPE(cmd
) == MD_MAJOR
&&
4705 mddev
->ro
&& mddev
->pers
) {
4706 if (mddev
->ro
== 2) {
4708 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4709 md_wakeup_thread(mddev
->thread
);
4721 mdu_disk_info_t info
;
4722 if (copy_from_user(&info
, argp
, sizeof(info
)))
4725 err
= add_new_disk(mddev
, &info
);
4729 case HOT_REMOVE_DISK
:
4730 err
= hot_remove_disk(mddev
, new_decode_dev(arg
));
4734 err
= hot_add_disk(mddev
, new_decode_dev(arg
));
4737 case SET_DISK_FAULTY
:
4738 err
= set_disk_faulty(mddev
, new_decode_dev(arg
));
4742 err
= do_md_run (mddev
);
4745 case SET_BITMAP_FILE
:
4746 err
= set_bitmap_file(mddev
, (int)arg
);
4756 mddev_unlock(mddev
);
4766 static int md_open(struct inode
*inode
, struct file
*file
)
4769 * Succeed if we can lock the mddev, which confirms that
4770 * it isn't being stopped right now.
4772 mddev_t
*mddev
= inode
->i_bdev
->bd_disk
->private_data
;
4775 if ((err
= mutex_lock_interruptible_nested(&mddev
->reconfig_mutex
, 1)))
4780 mddev_unlock(mddev
);
4782 check_disk_change(inode
->i_bdev
);
4787 static int md_release(struct inode
*inode
, struct file
* file
)
4789 mddev_t
*mddev
= inode
->i_bdev
->bd_disk
->private_data
;
4797 static int md_media_changed(struct gendisk
*disk
)
4799 mddev_t
*mddev
= disk
->private_data
;
4801 return mddev
->changed
;
4804 static int md_revalidate(struct gendisk
*disk
)
4806 mddev_t
*mddev
= disk
->private_data
;
4811 static struct block_device_operations md_fops
=
4813 .owner
= THIS_MODULE
,
4815 .release
= md_release
,
4817 .getgeo
= md_getgeo
,
4818 .media_changed
= md_media_changed
,
4819 .revalidate_disk
= md_revalidate
,
4822 static int md_thread(void * arg
)
4824 mdk_thread_t
*thread
= arg
;
4827 * md_thread is a 'system-thread', it's priority should be very
4828 * high. We avoid resource deadlocks individually in each
4829 * raid personality. (RAID5 does preallocation) We also use RR and
4830 * the very same RT priority as kswapd, thus we will never get
4831 * into a priority inversion deadlock.
4833 * we definitely have to have equal or higher priority than
4834 * bdflush, otherwise bdflush will deadlock if there are too
4835 * many dirty RAID5 blocks.
4838 allow_signal(SIGKILL
);
4839 while (!kthread_should_stop()) {
4841 /* We need to wait INTERRUPTIBLE so that
4842 * we don't add to the load-average.
4843 * That means we need to be sure no signals are
4846 if (signal_pending(current
))
4847 flush_signals(current
);
4849 wait_event_interruptible_timeout
4851 test_bit(THREAD_WAKEUP
, &thread
->flags
)
4852 || kthread_should_stop(),
4855 clear_bit(THREAD_WAKEUP
, &thread
->flags
);
4857 thread
->run(thread
->mddev
);
4863 void md_wakeup_thread(mdk_thread_t
*thread
)
4866 dprintk("md: waking up MD thread %s.\n", thread
->tsk
->comm
);
4867 set_bit(THREAD_WAKEUP
, &thread
->flags
);
4868 wake_up(&thread
->wqueue
);
4872 mdk_thread_t
*md_register_thread(void (*run
) (mddev_t
*), mddev_t
*mddev
,
4875 mdk_thread_t
*thread
;
4877 thread
= kzalloc(sizeof(mdk_thread_t
), GFP_KERNEL
);
4881 init_waitqueue_head(&thread
->wqueue
);
4884 thread
->mddev
= mddev
;
4885 thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
4886 thread
->tsk
= kthread_run(md_thread
, thread
, name
, mdname(thread
->mddev
));
4887 if (IS_ERR(thread
->tsk
)) {
4894 void md_unregister_thread(mdk_thread_t
*thread
)
4896 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread
->tsk
));
4898 kthread_stop(thread
->tsk
);
4902 void md_error(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
4909 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
4912 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
4914 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
4915 __builtin_return_address(0),__builtin_return_address(1),
4916 __builtin_return_address(2),__builtin_return_address(3));
4920 if (!mddev
->pers
->error_handler
)
4922 mddev
->pers
->error_handler(mddev
,rdev
);
4923 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4924 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4925 md_wakeup_thread(mddev
->thread
);
4926 md_new_event_inintr(mddev
);
4929 /* seq_file implementation /proc/mdstat */
4931 static void status_unused(struct seq_file
*seq
)
4935 struct list_head
*tmp
;
4937 seq_printf(seq
, "unused devices: ");
4939 rdev_for_each_list(rdev
, tmp
, pending_raid_disks
) {
4940 char b
[BDEVNAME_SIZE
];
4942 seq_printf(seq
, "%s ",
4943 bdevname(rdev
->bdev
,b
));
4946 seq_printf(seq
, "<none>");
4948 seq_printf(seq
, "\n");
4952 static void status_resync(struct seq_file
*seq
, mddev_t
* mddev
)
4954 sector_t max_blocks
, resync
, res
;
4955 unsigned long dt
, db
, rt
;
4957 unsigned int per_milli
;
4959 resync
= (mddev
->curr_resync
- atomic_read(&mddev
->recovery_active
))/2;
4961 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
4962 max_blocks
= mddev
->resync_max_sectors
>> 1;
4964 max_blocks
= mddev
->size
;
4967 * Should not happen.
4973 /* Pick 'scale' such that (resync>>scale)*1000 will fit
4974 * in a sector_t, and (max_blocks>>scale) will fit in a
4975 * u32, as those are the requirements for sector_div.
4976 * Thus 'scale' must be at least 10
4979 if (sizeof(sector_t
) > sizeof(unsigned long)) {
4980 while ( max_blocks
/2 > (1ULL<<(scale
+32)))
4983 res
= (resync
>>scale
)*1000;
4984 sector_div(res
, (u32
)((max_blocks
>>scale
)+1));
4988 int i
, x
= per_milli
/50, y
= 20-x
;
4989 seq_printf(seq
, "[");
4990 for (i
= 0; i
< x
; i
++)
4991 seq_printf(seq
, "=");
4992 seq_printf(seq
, ">");
4993 for (i
= 0; i
< y
; i
++)
4994 seq_printf(seq
, ".");
4995 seq_printf(seq
, "] ");
4997 seq_printf(seq
, " %s =%3u.%u%% (%llu/%llu)",
4998 (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)?
5000 (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)?
5002 (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ?
5003 "resync" : "recovery"))),
5004 per_milli
/10, per_milli
% 10,
5005 (unsigned long long) resync
,
5006 (unsigned long long) max_blocks
);
5009 * We do not want to overflow, so the order of operands and
5010 * the * 100 / 100 trick are important. We do a +1 to be
5011 * safe against division by zero. We only estimate anyway.
5013 * dt: time from mark until now
5014 * db: blocks written from mark until now
5015 * rt: remaining time
5017 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
5019 db
= (mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
))
5020 - mddev
->resync_mark_cnt
;
5021 rt
= (dt
* ((unsigned long)(max_blocks
-resync
) / (db
/2/100+1)))/100;
5023 seq_printf(seq
, " finish=%lu.%lumin", rt
/ 60, (rt
% 60)/6);
5025 seq_printf(seq
, " speed=%ldK/sec", db
/2/dt
);
5028 static void *md_seq_start(struct seq_file
*seq
, loff_t
*pos
)
5030 struct list_head
*tmp
;
5040 spin_lock(&all_mddevs_lock
);
5041 list_for_each(tmp
,&all_mddevs
)
5043 mddev
= list_entry(tmp
, mddev_t
, all_mddevs
);
5045 spin_unlock(&all_mddevs_lock
);
5048 spin_unlock(&all_mddevs_lock
);
5050 return (void*)2;/* tail */
5054 static void *md_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
5056 struct list_head
*tmp
;
5057 mddev_t
*next_mddev
, *mddev
= v
;
5063 spin_lock(&all_mddevs_lock
);
5065 tmp
= all_mddevs
.next
;
5067 tmp
= mddev
->all_mddevs
.next
;
5068 if (tmp
!= &all_mddevs
)
5069 next_mddev
= mddev_get(list_entry(tmp
,mddev_t
,all_mddevs
));
5071 next_mddev
= (void*)2;
5074 spin_unlock(&all_mddevs_lock
);
5082 static void md_seq_stop(struct seq_file
*seq
, void *v
)
5086 if (mddev
&& v
!= (void*)1 && v
!= (void*)2)
5090 struct mdstat_info
{
5094 static int md_seq_show(struct seq_file
*seq
, void *v
)
5098 struct list_head
*tmp2
;
5100 struct mdstat_info
*mi
= seq
->private;
5101 struct bitmap
*bitmap
;
5103 if (v
== (void*)1) {
5104 struct mdk_personality
*pers
;
5105 seq_printf(seq
, "Personalities : ");
5106 spin_lock(&pers_lock
);
5107 list_for_each_entry(pers
, &pers_list
, list
)
5108 seq_printf(seq
, "[%s] ", pers
->name
);
5110 spin_unlock(&pers_lock
);
5111 seq_printf(seq
, "\n");
5112 mi
->event
= atomic_read(&md_event_count
);
5115 if (v
== (void*)2) {
5120 if (mddev_lock(mddev
) < 0)
5123 if (mddev
->pers
|| mddev
->raid_disks
|| !list_empty(&mddev
->disks
)) {
5124 seq_printf(seq
, "%s : %sactive", mdname(mddev
),
5125 mddev
->pers
? "" : "in");
5128 seq_printf(seq
, " (read-only)");
5130 seq_printf(seq
, "(auto-read-only)");
5131 seq_printf(seq
, " %s", mddev
->pers
->name
);
5135 rdev_for_each(rdev
, tmp2
, mddev
) {
5136 char b
[BDEVNAME_SIZE
];
5137 seq_printf(seq
, " %s[%d]",
5138 bdevname(rdev
->bdev
,b
), rdev
->desc_nr
);
5139 if (test_bit(WriteMostly
, &rdev
->flags
))
5140 seq_printf(seq
, "(W)");
5141 if (test_bit(Faulty
, &rdev
->flags
)) {
5142 seq_printf(seq
, "(F)");
5144 } else if (rdev
->raid_disk
< 0)
5145 seq_printf(seq
, "(S)"); /* spare */
5149 if (!list_empty(&mddev
->disks
)) {
5151 seq_printf(seq
, "\n %llu blocks",
5152 (unsigned long long)mddev
->array_size
);
5154 seq_printf(seq
, "\n %llu blocks",
5155 (unsigned long long)size
);
5157 if (mddev
->persistent
) {
5158 if (mddev
->major_version
!= 0 ||
5159 mddev
->minor_version
!= 90) {
5160 seq_printf(seq
," super %d.%d",
5161 mddev
->major_version
,
5162 mddev
->minor_version
);
5164 } else if (mddev
->external
)
5165 seq_printf(seq
, " super external:%s",
5166 mddev
->metadata_type
);
5168 seq_printf(seq
, " super non-persistent");
5171 mddev
->pers
->status (seq
, mddev
);
5172 seq_printf(seq
, "\n ");
5173 if (mddev
->pers
->sync_request
) {
5174 if (mddev
->curr_resync
> 2) {
5175 status_resync (seq
, mddev
);
5176 seq_printf(seq
, "\n ");
5177 } else if (mddev
->curr_resync
== 1 || mddev
->curr_resync
== 2)
5178 seq_printf(seq
, "\tresync=DELAYED\n ");
5179 else if (mddev
->recovery_cp
< MaxSector
)
5180 seq_printf(seq
, "\tresync=PENDING\n ");
5183 seq_printf(seq
, "\n ");
5185 if ((bitmap
= mddev
->bitmap
)) {
5186 unsigned long chunk_kb
;
5187 unsigned long flags
;
5188 spin_lock_irqsave(&bitmap
->lock
, flags
);
5189 chunk_kb
= bitmap
->chunksize
>> 10;
5190 seq_printf(seq
, "bitmap: %lu/%lu pages [%luKB], "
5192 bitmap
->pages
- bitmap
->missing_pages
,
5194 (bitmap
->pages
- bitmap
->missing_pages
)
5195 << (PAGE_SHIFT
- 10),
5196 chunk_kb
? chunk_kb
: bitmap
->chunksize
,
5197 chunk_kb
? "KB" : "B");
5199 seq_printf(seq
, ", file: ");
5200 seq_path(seq
, &bitmap
->file
->f_path
, " \t\n");
5203 seq_printf(seq
, "\n");
5204 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
5207 seq_printf(seq
, "\n");
5209 mddev_unlock(mddev
);
5214 static struct seq_operations md_seq_ops
= {
5215 .start
= md_seq_start
,
5216 .next
= md_seq_next
,
5217 .stop
= md_seq_stop
,
5218 .show
= md_seq_show
,
5221 static int md_seq_open(struct inode
*inode
, struct file
*file
)
5224 struct mdstat_info
*mi
= kmalloc(sizeof(*mi
), GFP_KERNEL
);
5228 error
= seq_open(file
, &md_seq_ops
);
5232 struct seq_file
*p
= file
->private_data
;
5234 mi
->event
= atomic_read(&md_event_count
);
5239 static unsigned int mdstat_poll(struct file
*filp
, poll_table
*wait
)
5241 struct seq_file
*m
= filp
->private_data
;
5242 struct mdstat_info
*mi
= m
->private;
5245 poll_wait(filp
, &md_event_waiters
, wait
);
5247 /* always allow read */
5248 mask
= POLLIN
| POLLRDNORM
;
5250 if (mi
->event
!= atomic_read(&md_event_count
))
5251 mask
|= POLLERR
| POLLPRI
;
5255 static const struct file_operations md_seq_fops
= {
5256 .owner
= THIS_MODULE
,
5257 .open
= md_seq_open
,
5259 .llseek
= seq_lseek
,
5260 .release
= seq_release_private
,
5261 .poll
= mdstat_poll
,
5264 int register_md_personality(struct mdk_personality
*p
)
5266 spin_lock(&pers_lock
);
5267 list_add_tail(&p
->list
, &pers_list
);
5268 printk(KERN_INFO
"md: %s personality registered for level %d\n", p
->name
, p
->level
);
5269 spin_unlock(&pers_lock
);
5273 int unregister_md_personality(struct mdk_personality
*p
)
5275 printk(KERN_INFO
"md: %s personality unregistered\n", p
->name
);
5276 spin_lock(&pers_lock
);
5277 list_del_init(&p
->list
);
5278 spin_unlock(&pers_lock
);
5282 static int is_mddev_idle(mddev_t
*mddev
)
5285 struct list_head
*tmp
;
5290 rdev_for_each(rdev
, tmp
, mddev
) {
5291 struct gendisk
*disk
= rdev
->bdev
->bd_contains
->bd_disk
;
5292 curr_events
= disk_stat_read(disk
, sectors
[0]) +
5293 disk_stat_read(disk
, sectors
[1]) -
5294 atomic_read(&disk
->sync_io
);
5295 /* sync IO will cause sync_io to increase before the disk_stats
5296 * as sync_io is counted when a request starts, and
5297 * disk_stats is counted when it completes.
5298 * So resync activity will cause curr_events to be smaller than
5299 * when there was no such activity.
5300 * non-sync IO will cause disk_stat to increase without
5301 * increasing sync_io so curr_events will (eventually)
5302 * be larger than it was before. Once it becomes
5303 * substantially larger, the test below will cause
5304 * the array to appear non-idle, and resync will slow
5306 * If there is a lot of outstanding resync activity when
5307 * we set last_event to curr_events, then all that activity
5308 * completing might cause the array to appear non-idle
5309 * and resync will be slowed down even though there might
5310 * not have been non-resync activity. This will only
5311 * happen once though. 'last_events' will soon reflect
5312 * the state where there is little or no outstanding
5313 * resync requests, and further resync activity will
5314 * always make curr_events less than last_events.
5317 if (curr_events
- rdev
->last_events
> 4096) {
5318 rdev
->last_events
= curr_events
;
5325 void md_done_sync(mddev_t
*mddev
, int blocks
, int ok
)
5327 /* another "blocks" (512byte) blocks have been synced */
5328 atomic_sub(blocks
, &mddev
->recovery_active
);
5329 wake_up(&mddev
->recovery_wait
);
5331 set_bit(MD_RECOVERY_ERR
, &mddev
->recovery
);
5332 md_wakeup_thread(mddev
->thread
);
5333 // stop recovery, signal do_sync ....
5338 /* md_write_start(mddev, bi)
5339 * If we need to update some array metadata (e.g. 'active' flag
5340 * in superblock) before writing, schedule a superblock update
5341 * and wait for it to complete.
5343 void md_write_start(mddev_t
*mddev
, struct bio
*bi
)
5345 if (bio_data_dir(bi
) != WRITE
)
5348 BUG_ON(mddev
->ro
== 1);
5349 if (mddev
->ro
== 2) {
5350 /* need to switch to read/write */
5352 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5353 md_wakeup_thread(mddev
->thread
);
5355 atomic_inc(&mddev
->writes_pending
);
5356 if (mddev
->in_sync
) {
5357 spin_lock_irq(&mddev
->write_lock
);
5358 if (mddev
->in_sync
) {
5360 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
5361 md_wakeup_thread(mddev
->thread
);
5363 spin_unlock_irq(&mddev
->write_lock
);
5365 wait_event(mddev
->sb_wait
, mddev
->flags
==0);
5368 void md_write_end(mddev_t
*mddev
)
5370 if (atomic_dec_and_test(&mddev
->writes_pending
)) {
5371 if (mddev
->safemode
== 2)
5372 md_wakeup_thread(mddev
->thread
);
5373 else if (mddev
->safemode_delay
)
5374 mod_timer(&mddev
->safemode_timer
, jiffies
+ mddev
->safemode_delay
);
5378 /* md_allow_write(mddev)
5379 * Calling this ensures that the array is marked 'active' so that writes
5380 * may proceed without blocking. It is important to call this before
5381 * attempting a GFP_KERNEL allocation while holding the mddev lock.
5382 * Must be called with mddev_lock held.
5384 void md_allow_write(mddev_t
*mddev
)
5391 spin_lock_irq(&mddev
->write_lock
);
5392 if (mddev
->in_sync
) {
5394 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
5395 if (mddev
->safemode_delay
&&
5396 mddev
->safemode
== 0)
5397 mddev
->safemode
= 1;
5398 spin_unlock_irq(&mddev
->write_lock
);
5399 md_update_sb(mddev
, 0);
5401 spin_unlock_irq(&mddev
->write_lock
);
5403 EXPORT_SYMBOL_GPL(md_allow_write
);
5405 static DECLARE_WAIT_QUEUE_HEAD(resync_wait
);
5407 #define SYNC_MARKS 10
5408 #define SYNC_MARK_STEP (3*HZ)
5409 void md_do_sync(mddev_t
*mddev
)
5412 unsigned int currspeed
= 0,
5414 sector_t max_sectors
,j
, io_sectors
;
5415 unsigned long mark
[SYNC_MARKS
];
5416 sector_t mark_cnt
[SYNC_MARKS
];
5418 struct list_head
*tmp
;
5419 sector_t last_check
;
5421 struct list_head
*rtmp
;
5425 /* just incase thread restarts... */
5426 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
5428 if (mddev
->ro
) /* never try to sync a read-only array */
5431 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
5432 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
5433 desc
= "data-check";
5434 else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
5435 desc
= "requested-resync";
5438 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
5443 /* we overload curr_resync somewhat here.
5444 * 0 == not engaged in resync at all
5445 * 2 == checking that there is no conflict with another sync
5446 * 1 == like 2, but have yielded to allow conflicting resync to
5448 * other == active in resync - this many blocks
5450 * Before starting a resync we must have set curr_resync to
5451 * 2, and then checked that every "conflicting" array has curr_resync
5452 * less than ours. When we find one that is the same or higher
5453 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
5454 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
5455 * This will mean we have to start checking from the beginning again.
5460 mddev
->curr_resync
= 2;
5463 if (kthread_should_stop()) {
5464 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5467 for_each_mddev(mddev2
, tmp
) {
5468 if (mddev2
== mddev
)
5470 if (mddev2
->curr_resync
&&
5471 match_mddev_units(mddev
,mddev2
)) {
5473 if (mddev
< mddev2
&& mddev
->curr_resync
== 2) {
5474 /* arbitrarily yield */
5475 mddev
->curr_resync
= 1;
5476 wake_up(&resync_wait
);
5478 if (mddev
> mddev2
&& mddev
->curr_resync
== 1)
5479 /* no need to wait here, we can wait the next
5480 * time 'round when curr_resync == 2
5483 prepare_to_wait(&resync_wait
, &wq
, TASK_UNINTERRUPTIBLE
);
5484 if (!kthread_should_stop() &&
5485 mddev2
->curr_resync
>= mddev
->curr_resync
) {
5486 printk(KERN_INFO
"md: delaying %s of %s"
5487 " until %s has finished (they"
5488 " share one or more physical units)\n",
5489 desc
, mdname(mddev
), mdname(mddev2
));
5492 finish_wait(&resync_wait
, &wq
);
5495 finish_wait(&resync_wait
, &wq
);
5498 } while (mddev
->curr_resync
< 2);
5501 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
5502 /* resync follows the size requested by the personality,
5503 * which defaults to physical size, but can be virtual size
5505 max_sectors
= mddev
->resync_max_sectors
;
5506 mddev
->resync_mismatches
= 0;
5507 /* we don't use the checkpoint if there's a bitmap */
5508 if (!mddev
->bitmap
&&
5509 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
5510 j
= mddev
->recovery_cp
;
5511 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
5512 max_sectors
= mddev
->size
<< 1;
5514 /* recovery follows the physical size of devices */
5515 max_sectors
= mddev
->size
<< 1;
5517 rdev_for_each(rdev
, rtmp
, mddev
)
5518 if (rdev
->raid_disk
>= 0 &&
5519 !test_bit(Faulty
, &rdev
->flags
) &&
5520 !test_bit(In_sync
, &rdev
->flags
) &&
5521 rdev
->recovery_offset
< j
)
5522 j
= rdev
->recovery_offset
;
5525 printk(KERN_INFO
"md: %s of RAID array %s\n", desc
, mdname(mddev
));
5526 printk(KERN_INFO
"md: minimum _guaranteed_ speed:"
5527 " %d KB/sec/disk.\n", speed_min(mddev
));
5528 printk(KERN_INFO
"md: using maximum available idle IO bandwidth "
5529 "(but not more than %d KB/sec) for %s.\n",
5530 speed_max(mddev
), desc
);
5532 is_mddev_idle(mddev
); /* this also initializes IO event counters */
5535 for (m
= 0; m
< SYNC_MARKS
; m
++) {
5537 mark_cnt
[m
] = io_sectors
;
5540 mddev
->resync_mark
= mark
[last_mark
];
5541 mddev
->resync_mark_cnt
= mark_cnt
[last_mark
];
5544 * Tune reconstruction:
5546 window
= 32*(PAGE_SIZE
/512);
5547 printk(KERN_INFO
"md: using %dk window, over a total of %llu blocks.\n",
5548 window
/2,(unsigned long long) max_sectors
/2);
5550 atomic_set(&mddev
->recovery_active
, 0);
5551 init_waitqueue_head(&mddev
->recovery_wait
);
5556 "md: resuming %s of %s from checkpoint.\n",
5557 desc
, mdname(mddev
));
5558 mddev
->curr_resync
= j
;
5561 while (j
< max_sectors
) {
5565 if (j
>= mddev
->resync_max
) {
5566 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
5567 wait_event(mddev
->recovery_wait
,
5568 mddev
->resync_max
> j
5569 || kthread_should_stop());
5571 if (kthread_should_stop())
5573 sectors
= mddev
->pers
->sync_request(mddev
, j
, &skipped
,
5574 currspeed
< speed_min(mddev
));
5576 set_bit(MD_RECOVERY_ERR
, &mddev
->recovery
);
5580 if (!skipped
) { /* actual IO requested */
5581 io_sectors
+= sectors
;
5582 atomic_add(sectors
, &mddev
->recovery_active
);
5586 if (j
>1) mddev
->curr_resync
= j
;
5587 mddev
->curr_mark_cnt
= io_sectors
;
5588 if (last_check
== 0)
5589 /* this is the earliers that rebuilt will be
5590 * visible in /proc/mdstat
5592 md_new_event(mddev
);
5594 if (last_check
+ window
> io_sectors
|| j
== max_sectors
)
5597 last_check
= io_sectors
;
5599 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) ||
5600 test_bit(MD_RECOVERY_ERR
, &mddev
->recovery
))
5604 if (time_after_eq(jiffies
, mark
[last_mark
] + SYNC_MARK_STEP
)) {
5606 int next
= (last_mark
+1) % SYNC_MARKS
;
5608 mddev
->resync_mark
= mark
[next
];
5609 mddev
->resync_mark_cnt
= mark_cnt
[next
];
5610 mark
[next
] = jiffies
;
5611 mark_cnt
[next
] = io_sectors
- atomic_read(&mddev
->recovery_active
);
5616 if (kthread_should_stop())
5621 * this loop exits only if either when we are slower than
5622 * the 'hard' speed limit, or the system was IO-idle for
5624 * the system might be non-idle CPU-wise, but we only care
5625 * about not overloading the IO subsystem. (things like an
5626 * e2fsck being done on the RAID array should execute fast)
5628 blk_unplug(mddev
->queue
);
5631 currspeed
= ((unsigned long)(io_sectors
-mddev
->resync_mark_cnt
))/2
5632 /((jiffies
-mddev
->resync_mark
)/HZ
+1) +1;
5634 if (currspeed
> speed_min(mddev
)) {
5635 if ((currspeed
> speed_max(mddev
)) ||
5636 !is_mddev_idle(mddev
)) {
5642 printk(KERN_INFO
"md: %s: %s done.\n",mdname(mddev
), desc
);
5644 * this also signals 'finished resyncing' to md_stop
5647 blk_unplug(mddev
->queue
);
5649 wait_event(mddev
->recovery_wait
, !atomic_read(&mddev
->recovery_active
));
5651 /* tell personality that we are finished */
5652 mddev
->pers
->sync_request(mddev
, max_sectors
, &skipped
, 1);
5654 if (!test_bit(MD_RECOVERY_ERR
, &mddev
->recovery
) &&
5655 !test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
) &&
5656 mddev
->curr_resync
> 2) {
5657 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
5658 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
5659 if (mddev
->curr_resync
>= mddev
->recovery_cp
) {
5661 "md: checkpointing %s of %s.\n",
5662 desc
, mdname(mddev
));
5663 mddev
->recovery_cp
= mddev
->curr_resync
;
5666 mddev
->recovery_cp
= MaxSector
;
5668 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
5669 mddev
->curr_resync
= MaxSector
;
5670 rdev_for_each(rdev
, rtmp
, mddev
)
5671 if (rdev
->raid_disk
>= 0 &&
5672 !test_bit(Faulty
, &rdev
->flags
) &&
5673 !test_bit(In_sync
, &rdev
->flags
) &&
5674 rdev
->recovery_offset
< mddev
->curr_resync
)
5675 rdev
->recovery_offset
= mddev
->curr_resync
;
5678 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
5681 mddev
->curr_resync
= 0;
5682 mddev
->resync_max
= MaxSector
;
5683 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
5684 wake_up(&resync_wait
);
5685 set_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
5686 md_wakeup_thread(mddev
->thread
);
5691 * got a signal, exit.
5694 "md: md_do_sync() got signal ... exiting\n");
5695 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5699 EXPORT_SYMBOL_GPL(md_do_sync
);
5702 static int remove_and_add_spares(mddev_t
*mddev
)
5705 struct list_head
*rtmp
;
5708 rdev_for_each(rdev
, rtmp
, mddev
)
5709 if (rdev
->raid_disk
>= 0 &&
5711 (test_bit(Faulty
, &rdev
->flags
) ||
5712 ! test_bit(In_sync
, &rdev
->flags
)) &&
5713 atomic_read(&rdev
->nr_pending
)==0) {
5714 if (mddev
->pers
->hot_remove_disk(
5715 mddev
, rdev
->raid_disk
)==0) {
5717 sprintf(nm
,"rd%d", rdev
->raid_disk
);
5718 sysfs_remove_link(&mddev
->kobj
, nm
);
5719 rdev
->raid_disk
= -1;
5723 if (mddev
->degraded
) {
5724 rdev_for_each(rdev
, rtmp
, mddev
)
5725 if (rdev
->raid_disk
< 0
5726 && !test_bit(Faulty
, &rdev
->flags
)) {
5727 rdev
->recovery_offset
= 0;
5728 if (mddev
->pers
->hot_add_disk(mddev
,rdev
)) {
5730 sprintf(nm
, "rd%d", rdev
->raid_disk
);
5731 if (sysfs_create_link(&mddev
->kobj
,
5734 "md: cannot register "
5738 md_new_event(mddev
);
5746 * This routine is regularly called by all per-raid-array threads to
5747 * deal with generic issues like resync and super-block update.
5748 * Raid personalities that don't have a thread (linear/raid0) do not
5749 * need this as they never do any recovery or update the superblock.
5751 * It does not do any resync itself, but rather "forks" off other threads
5752 * to do that as needed.
5753 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
5754 * "->recovery" and create a thread at ->sync_thread.
5755 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
5756 * and wakeups up this thread which will reap the thread and finish up.
5757 * This thread also removes any faulty devices (with nr_pending == 0).
5759 * The overall approach is:
5760 * 1/ if the superblock needs updating, update it.
5761 * 2/ If a recovery thread is running, don't do anything else.
5762 * 3/ If recovery has finished, clean up, possibly marking spares active.
5763 * 4/ If there are any faulty devices, remove them.
5764 * 5/ If array is degraded, try to add spares devices
5765 * 6/ If array has spares or is not in-sync, start a resync thread.
5767 void md_check_recovery(mddev_t
*mddev
)
5770 struct list_head
*rtmp
;
5774 bitmap_daemon_work(mddev
->bitmap
);
5779 if (signal_pending(current
)) {
5780 if (mddev
->pers
->sync_request
) {
5781 printk(KERN_INFO
"md: %s in immediate safe mode\n",
5783 mddev
->safemode
= 2;
5785 flush_signals(current
);
5789 (mddev
->flags
&& !mddev
->external
) ||
5790 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
5791 test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
5792 (mddev
->safemode
== 1) ||
5793 (mddev
->safemode
== 2 && ! atomic_read(&mddev
->writes_pending
)
5794 && !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
5798 if (mddev_trylock(mddev
)) {
5801 spin_lock_irq(&mddev
->write_lock
);
5802 if (mddev
->safemode
&& !atomic_read(&mddev
->writes_pending
) &&
5803 !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
) {
5805 if (mddev
->persistent
)
5806 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
5808 if (mddev
->safemode
== 1)
5809 mddev
->safemode
= 0;
5810 spin_unlock_irq(&mddev
->write_lock
);
5813 md_update_sb(mddev
, 0);
5816 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
5817 !test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
)) {
5818 /* resync/recovery still happening */
5819 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5822 if (mddev
->sync_thread
) {
5823 /* resync has finished, collect result */
5824 md_unregister_thread(mddev
->sync_thread
);
5825 mddev
->sync_thread
= NULL
;
5826 if (!test_bit(MD_RECOVERY_ERR
, &mddev
->recovery
) &&
5827 !test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
5829 /* activate any spares */
5830 mddev
->pers
->spare_active(mddev
);
5832 md_update_sb(mddev
, 1);
5834 /* if array is no-longer degraded, then any saved_raid_disk
5835 * information must be scrapped
5837 if (!mddev
->degraded
)
5838 rdev_for_each(rdev
, rtmp
, mddev
)
5839 rdev
->saved_raid_disk
= -1;
5841 mddev
->recovery
= 0;
5842 /* flag recovery needed just to double check */
5843 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5844 md_new_event(mddev
);
5847 /* Clear some bits that don't mean anything, but
5850 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5851 clear_bit(MD_RECOVERY_ERR
, &mddev
->recovery
);
5852 clear_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5853 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
5855 if (test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
5857 /* no recovery is running.
5858 * remove any failed drives, then
5859 * add spares if possible.
5860 * Spare are also removed and re-added, to allow
5861 * the personality to fail the re-add.
5864 if (mddev
->reshape_position
!= MaxSector
) {
5865 if (mddev
->pers
->check_reshape(mddev
) != 0)
5866 /* Cannot proceed */
5868 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
5869 } else if ((spares
= remove_and_add_spares(mddev
))) {
5870 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
5871 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
5872 } else if (mddev
->recovery_cp
< MaxSector
) {
5873 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
5874 } else if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
5875 /* nothing to be done ... */
5878 if (mddev
->pers
->sync_request
) {
5879 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
5880 if (spares
&& mddev
->bitmap
&& ! mddev
->bitmap
->file
) {
5881 /* We are adding a device or devices to an array
5882 * which has the bitmap stored on all devices.
5883 * So make sure all bitmap pages get written
5885 bitmap_write_all(mddev
->bitmap
);
5887 mddev
->sync_thread
= md_register_thread(md_do_sync
,
5890 if (!mddev
->sync_thread
) {
5891 printk(KERN_ERR
"%s: could not start resync"
5894 /* leave the spares where they are, it shouldn't hurt */
5895 mddev
->recovery
= 0;
5897 md_wakeup_thread(mddev
->sync_thread
);
5898 md_new_event(mddev
);
5901 mddev_unlock(mddev
);
5905 static int md_notify_reboot(struct notifier_block
*this,
5906 unsigned long code
, void *x
)
5908 struct list_head
*tmp
;
5911 if ((code
== SYS_DOWN
) || (code
== SYS_HALT
) || (code
== SYS_POWER_OFF
)) {
5913 printk(KERN_INFO
"md: stopping all md devices.\n");
5915 for_each_mddev(mddev
, tmp
)
5916 if (mddev_trylock(mddev
)) {
5917 do_md_stop (mddev
, 1);
5918 mddev_unlock(mddev
);
5921 * certain more exotic SCSI devices are known to be
5922 * volatile wrt too early system reboots. While the
5923 * right place to handle this issue is the given
5924 * driver, we do want to have a safe RAID driver ...
5931 static struct notifier_block md_notifier
= {
5932 .notifier_call
= md_notify_reboot
,
5934 .priority
= INT_MAX
, /* before any real devices */
5937 static void md_geninit(void)
5939 struct proc_dir_entry
*p
;
5941 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t
));
5943 p
= create_proc_entry("mdstat", S_IRUGO
, NULL
);
5945 p
->proc_fops
= &md_seq_fops
;
5948 static int __init
md_init(void)
5950 if (register_blkdev(MAJOR_NR
, "md"))
5952 if ((mdp_major
=register_blkdev(0, "mdp"))<=0) {
5953 unregister_blkdev(MAJOR_NR
, "md");
5956 blk_register_region(MKDEV(MAJOR_NR
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
5957 md_probe
, NULL
, NULL
);
5958 blk_register_region(MKDEV(mdp_major
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
5959 md_probe
, NULL
, NULL
);
5961 register_reboot_notifier(&md_notifier
);
5962 raid_table_header
= register_sysctl_table(raid_root_table
);
5972 * Searches all registered partitions for autorun RAID arrays
5976 static LIST_HEAD(all_detected_devices
);
5977 struct detected_devices_node
{
5978 struct list_head list
;
5982 void md_autodetect_dev(dev_t dev
)
5984 struct detected_devices_node
*node_detected_dev
;
5986 node_detected_dev
= kzalloc(sizeof(*node_detected_dev
), GFP_KERNEL
);
5987 if (node_detected_dev
) {
5988 node_detected_dev
->dev
= dev
;
5989 list_add_tail(&node_detected_dev
->list
, &all_detected_devices
);
5991 printk(KERN_CRIT
"md: md_autodetect_dev: kzalloc failed"
5992 ", skipping dev(%d,%d)\n", MAJOR(dev
), MINOR(dev
));
5997 static void autostart_arrays(int part
)
6000 struct detected_devices_node
*node_detected_dev
;
6002 int i_scanned
, i_passed
;
6007 printk(KERN_INFO
"md: Autodetecting RAID arrays.\n");
6009 while (!list_empty(&all_detected_devices
) && i_scanned
< INT_MAX
) {
6011 node_detected_dev
= list_entry(all_detected_devices
.next
,
6012 struct detected_devices_node
, list
);
6013 list_del(&node_detected_dev
->list
);
6014 dev
= node_detected_dev
->dev
;
6015 kfree(node_detected_dev
);
6016 rdev
= md_import_device(dev
,0, 90);
6020 if (test_bit(Faulty
, &rdev
->flags
)) {
6024 list_add(&rdev
->same_set
, &pending_raid_disks
);
6028 printk(KERN_INFO
"md: Scanned %d and added %d devices.\n",
6029 i_scanned
, i_passed
);
6031 autorun_devices(part
);
6034 #endif /* !MODULE */
6036 static __exit
void md_exit(void)
6039 struct list_head
*tmp
;
6041 blk_unregister_region(MKDEV(MAJOR_NR
,0), 1U << MINORBITS
);
6042 blk_unregister_region(MKDEV(mdp_major
,0), 1U << MINORBITS
);
6044 unregister_blkdev(MAJOR_NR
,"md");
6045 unregister_blkdev(mdp_major
, "mdp");
6046 unregister_reboot_notifier(&md_notifier
);
6047 unregister_sysctl_table(raid_table_header
);
6048 remove_proc_entry("mdstat", NULL
);
6049 for_each_mddev(mddev
, tmp
) {
6050 struct gendisk
*disk
= mddev
->gendisk
;
6053 export_array(mddev
);
6056 mddev
->gendisk
= NULL
;
6061 subsys_initcall(md_init
);
6062 module_exit(md_exit
)
6064 static int get_ro(char *buffer
, struct kernel_param
*kp
)
6066 return sprintf(buffer
, "%d", start_readonly
);
6068 static int set_ro(const char *val
, struct kernel_param
*kp
)
6071 int num
= simple_strtoul(val
, &e
, 10);
6072 if (*val
&& (*e
== '\0' || *e
== '\n')) {
6073 start_readonly
= num
;
6079 module_param_call(start_ro
, set_ro
, get_ro
, NULL
, S_IRUSR
|S_IWUSR
);
6080 module_param(start_dirty_degraded
, int, S_IRUGO
|S_IWUSR
);
6083 EXPORT_SYMBOL(register_md_personality
);
6084 EXPORT_SYMBOL(unregister_md_personality
);
6085 EXPORT_SYMBOL(md_error
);
6086 EXPORT_SYMBOL(md_done_sync
);
6087 EXPORT_SYMBOL(md_write_start
);
6088 EXPORT_SYMBOL(md_write_end
);
6089 EXPORT_SYMBOL(md_register_thread
);
6090 EXPORT_SYMBOL(md_unregister_thread
);
6091 EXPORT_SYMBOL(md_wakeup_thread
);
6092 EXPORT_SYMBOL(md_check_recovery
);
6093 MODULE_LICENSE("GPL");
6095 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR
);