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 ITERATE_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
;
279 new->queue
= blk_alloc_queue(GFP_KERNEL
);
284 set_bit(QUEUE_FLAG_CLUSTER
, &new->queue
->queue_flags
);
286 blk_queue_make_request(new->queue
, md_fail_request
);
291 static inline int mddev_lock(mddev_t
* mddev
)
293 return mutex_lock_interruptible(&mddev
->reconfig_mutex
);
296 static inline int mddev_trylock(mddev_t
* mddev
)
298 return mutex_trylock(&mddev
->reconfig_mutex
);
301 static inline void mddev_unlock(mddev_t
* mddev
)
303 mutex_unlock(&mddev
->reconfig_mutex
);
305 md_wakeup_thread(mddev
->thread
);
308 static mdk_rdev_t
* find_rdev_nr(mddev_t
*mddev
, int nr
)
311 struct list_head
*tmp
;
313 ITERATE_RDEV(mddev
,rdev
,tmp
) {
314 if (rdev
->desc_nr
== nr
)
320 static mdk_rdev_t
* find_rdev(mddev_t
* mddev
, dev_t dev
)
322 struct list_head
*tmp
;
325 ITERATE_RDEV(mddev
,rdev
,tmp
) {
326 if (rdev
->bdev
->bd_dev
== dev
)
332 static struct mdk_personality
*find_pers(int level
, char *clevel
)
334 struct mdk_personality
*pers
;
335 list_for_each_entry(pers
, &pers_list
, list
) {
336 if (level
!= LEVEL_NONE
&& pers
->level
== level
)
338 if (strcmp(pers
->name
, clevel
)==0)
344 static inline sector_t
calc_dev_sboffset(struct block_device
*bdev
)
346 sector_t size
= bdev
->bd_inode
->i_size
>> BLOCK_SIZE_BITS
;
347 return MD_NEW_SIZE_BLOCKS(size
);
350 static sector_t
calc_dev_size(mdk_rdev_t
*rdev
, unsigned chunk_size
)
354 size
= rdev
->sb_offset
;
357 size
&= ~((sector_t
)chunk_size
/1024 - 1);
361 static int alloc_disk_sb(mdk_rdev_t
* rdev
)
366 rdev
->sb_page
= alloc_page(GFP_KERNEL
);
367 if (!rdev
->sb_page
) {
368 printk(KERN_ALERT
"md: out of memory.\n");
375 static void free_disk_sb(mdk_rdev_t
* rdev
)
378 put_page(rdev
->sb_page
);
380 rdev
->sb_page
= NULL
;
387 static void super_written(struct bio
*bio
, int error
)
389 mdk_rdev_t
*rdev
= bio
->bi_private
;
390 mddev_t
*mddev
= rdev
->mddev
;
392 if (error
|| !test_bit(BIO_UPTODATE
, &bio
->bi_flags
)) {
393 printk("md: super_written gets error=%d, uptodate=%d\n",
394 error
, test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
395 WARN_ON(test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
396 md_error(mddev
, rdev
);
399 if (atomic_dec_and_test(&mddev
->pending_writes
))
400 wake_up(&mddev
->sb_wait
);
404 static void super_written_barrier(struct bio
*bio
, int error
)
406 struct bio
*bio2
= bio
->bi_private
;
407 mdk_rdev_t
*rdev
= bio2
->bi_private
;
408 mddev_t
*mddev
= rdev
->mddev
;
410 if (!test_bit(BIO_UPTODATE
, &bio
->bi_flags
) &&
411 error
== -EOPNOTSUPP
) {
413 /* barriers don't appear to be supported :-( */
414 set_bit(BarriersNotsupp
, &rdev
->flags
);
415 mddev
->barriers_work
= 0;
416 spin_lock_irqsave(&mddev
->write_lock
, flags
);
417 bio2
->bi_next
= mddev
->biolist
;
418 mddev
->biolist
= bio2
;
419 spin_unlock_irqrestore(&mddev
->write_lock
, flags
);
420 wake_up(&mddev
->sb_wait
);
424 bio
->bi_private
= rdev
;
425 super_written(bio
, error
);
429 void md_super_write(mddev_t
*mddev
, mdk_rdev_t
*rdev
,
430 sector_t sector
, int size
, struct page
*page
)
432 /* write first size bytes of page to sector of rdev
433 * Increment mddev->pending_writes before returning
434 * and decrement it on completion, waking up sb_wait
435 * if zero is reached.
436 * If an error occurred, call md_error
438 * As we might need to resubmit the request if BIO_RW_BARRIER
439 * causes ENOTSUPP, we allocate a spare bio...
441 struct bio
*bio
= bio_alloc(GFP_NOIO
, 1);
442 int rw
= (1<<BIO_RW
) | (1<<BIO_RW_SYNC
);
444 bio
->bi_bdev
= rdev
->bdev
;
445 bio
->bi_sector
= sector
;
446 bio_add_page(bio
, page
, size
, 0);
447 bio
->bi_private
= rdev
;
448 bio
->bi_end_io
= super_written
;
451 atomic_inc(&mddev
->pending_writes
);
452 if (!test_bit(BarriersNotsupp
, &rdev
->flags
)) {
454 rw
|= (1<<BIO_RW_BARRIER
);
455 rbio
= bio_clone(bio
, GFP_NOIO
);
456 rbio
->bi_private
= bio
;
457 rbio
->bi_end_io
= super_written_barrier
;
458 submit_bio(rw
, rbio
);
463 void md_super_wait(mddev_t
*mddev
)
465 /* wait for all superblock writes that were scheduled to complete.
466 * if any had to be retried (due to BARRIER problems), retry them
470 prepare_to_wait(&mddev
->sb_wait
, &wq
, TASK_UNINTERRUPTIBLE
);
471 if (atomic_read(&mddev
->pending_writes
)==0)
473 while (mddev
->biolist
) {
475 spin_lock_irq(&mddev
->write_lock
);
476 bio
= mddev
->biolist
;
477 mddev
->biolist
= bio
->bi_next
;
479 spin_unlock_irq(&mddev
->write_lock
);
480 submit_bio(bio
->bi_rw
, bio
);
484 finish_wait(&mddev
->sb_wait
, &wq
);
487 static void bi_complete(struct bio
*bio
, int error
)
489 complete((struct completion
*)bio
->bi_private
);
492 int sync_page_io(struct block_device
*bdev
, sector_t sector
, int size
,
493 struct page
*page
, int rw
)
495 struct bio
*bio
= bio_alloc(GFP_NOIO
, 1);
496 struct completion event
;
499 rw
|= (1 << BIO_RW_SYNC
);
502 bio
->bi_sector
= sector
;
503 bio_add_page(bio
, page
, size
, 0);
504 init_completion(&event
);
505 bio
->bi_private
= &event
;
506 bio
->bi_end_io
= bi_complete
;
508 wait_for_completion(&event
);
510 ret
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
514 EXPORT_SYMBOL_GPL(sync_page_io
);
516 static int read_disk_sb(mdk_rdev_t
* rdev
, int size
)
518 char b
[BDEVNAME_SIZE
];
519 if (!rdev
->sb_page
) {
527 if (!sync_page_io(rdev
->bdev
, rdev
->sb_offset
<<1, size
, rdev
->sb_page
, READ
))
533 printk(KERN_WARNING
"md: disabled device %s, could not read superblock.\n",
534 bdevname(rdev
->bdev
,b
));
538 static int uuid_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
540 if ( (sb1
->set_uuid0
== sb2
->set_uuid0
) &&
541 (sb1
->set_uuid1
== sb2
->set_uuid1
) &&
542 (sb1
->set_uuid2
== sb2
->set_uuid2
) &&
543 (sb1
->set_uuid3
== sb2
->set_uuid3
))
551 static int sb_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
554 mdp_super_t
*tmp1
, *tmp2
;
556 tmp1
= kmalloc(sizeof(*tmp1
),GFP_KERNEL
);
557 tmp2
= kmalloc(sizeof(*tmp2
),GFP_KERNEL
);
559 if (!tmp1
|| !tmp2
) {
561 printk(KERN_INFO
"md.c: sb1 is not equal to sb2!\n");
569 * nr_disks is not constant
574 if (memcmp(tmp1
, tmp2
, MD_SB_GENERIC_CONSTANT_WORDS
* 4))
586 static u32
md_csum_fold(u32 csum
)
588 csum
= (csum
& 0xffff) + (csum
>> 16);
589 return (csum
& 0xffff) + (csum
>> 16);
592 static unsigned int calc_sb_csum(mdp_super_t
* sb
)
595 u32
*sb32
= (u32
*)sb
;
597 unsigned int disk_csum
, csum
;
599 disk_csum
= sb
->sb_csum
;
602 for (i
= 0; i
< MD_SB_BYTES
/4 ; i
++)
604 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
608 /* This used to use csum_partial, which was wrong for several
609 * reasons including that different results are returned on
610 * different architectures. It isn't critical that we get exactly
611 * the same return value as before (we always csum_fold before
612 * testing, and that removes any differences). However as we
613 * know that csum_partial always returned a 16bit value on
614 * alphas, do a fold to maximise conformity to previous behaviour.
616 sb
->sb_csum
= md_csum_fold(disk_csum
);
618 sb
->sb_csum
= disk_csum
;
625 * Handle superblock details.
626 * We want to be able to handle multiple superblock formats
627 * so we have a common interface to them all, and an array of
628 * different handlers.
629 * We rely on user-space to write the initial superblock, and support
630 * reading and updating of superblocks.
631 * Interface methods are:
632 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
633 * loads and validates a superblock on dev.
634 * if refdev != NULL, compare superblocks on both devices
636 * 0 - dev has a superblock that is compatible with refdev
637 * 1 - dev has a superblock that is compatible and newer than refdev
638 * so dev should be used as the refdev in future
639 * -EINVAL superblock incompatible or invalid
640 * -othererror e.g. -EIO
642 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
643 * Verify that dev is acceptable into mddev.
644 * The first time, mddev->raid_disks will be 0, and data from
645 * dev should be merged in. Subsequent calls check that dev
646 * is new enough. Return 0 or -EINVAL
648 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
649 * Update the superblock for rdev with data in mddev
650 * This does not write to disc.
656 struct module
*owner
;
657 int (*load_super
)(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
);
658 int (*validate_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
659 void (*sync_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
663 * load_super for 0.90.0
665 static int super_90_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
667 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
673 * Calculate the position of the superblock,
674 * it's at the end of the disk.
676 * It also happens to be a multiple of 4Kb.
678 sb_offset
= calc_dev_sboffset(rdev
->bdev
);
679 rdev
->sb_offset
= sb_offset
;
681 ret
= read_disk_sb(rdev
, MD_SB_BYTES
);
686 bdevname(rdev
->bdev
, b
);
687 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
689 if (sb
->md_magic
!= MD_SB_MAGIC
) {
690 printk(KERN_ERR
"md: invalid raid superblock magic on %s\n",
695 if (sb
->major_version
!= 0 ||
696 sb
->minor_version
< 90 ||
697 sb
->minor_version
> 91) {
698 printk(KERN_WARNING
"Bad version number %d.%d on %s\n",
699 sb
->major_version
, sb
->minor_version
,
704 if (sb
->raid_disks
<= 0)
707 if (md_csum_fold(calc_sb_csum(sb
)) != md_csum_fold(sb
->sb_csum
)) {
708 printk(KERN_WARNING
"md: invalid superblock checksum on %s\n",
713 rdev
->preferred_minor
= sb
->md_minor
;
714 rdev
->data_offset
= 0;
715 rdev
->sb_size
= MD_SB_BYTES
;
717 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
718 if (sb
->level
!= 1 && sb
->level
!= 4
719 && sb
->level
!= 5 && sb
->level
!= 6
720 && sb
->level
!= 10) {
721 /* FIXME use a better test */
723 "md: bitmaps not supported for this level.\n");
728 if (sb
->level
== LEVEL_MULTIPATH
)
731 rdev
->desc_nr
= sb
->this_disk
.number
;
737 mdp_super_t
*refsb
= (mdp_super_t
*)page_address(refdev
->sb_page
);
738 if (!uuid_equal(refsb
, sb
)) {
739 printk(KERN_WARNING
"md: %s has different UUID to %s\n",
740 b
, bdevname(refdev
->bdev
,b2
));
743 if (!sb_equal(refsb
, sb
)) {
744 printk(KERN_WARNING
"md: %s has same UUID"
745 " but different superblock to %s\n",
746 b
, bdevname(refdev
->bdev
, b2
));
750 ev2
= md_event(refsb
);
756 rdev
->size
= calc_dev_size(rdev
, sb
->chunk_size
);
758 if (rdev
->size
< sb
->size
&& sb
->level
> 1)
759 /* "this cannot possibly happen" ... */
767 * validate_super for 0.90.0
769 static int super_90_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
772 mdp_super_t
*sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
773 __u64 ev1
= md_event(sb
);
775 rdev
->raid_disk
= -1;
777 if (mddev
->raid_disks
== 0) {
778 mddev
->major_version
= 0;
779 mddev
->minor_version
= sb
->minor_version
;
780 mddev
->patch_version
= sb
->patch_version
;
781 mddev
->persistent
= ! sb
->not_persistent
;
782 mddev
->chunk_size
= sb
->chunk_size
;
783 mddev
->ctime
= sb
->ctime
;
784 mddev
->utime
= sb
->utime
;
785 mddev
->level
= sb
->level
;
786 mddev
->clevel
[0] = 0;
787 mddev
->layout
= sb
->layout
;
788 mddev
->raid_disks
= sb
->raid_disks
;
789 mddev
->size
= sb
->size
;
791 mddev
->bitmap_offset
= 0;
792 mddev
->default_bitmap_offset
= MD_SB_BYTES
>> 9;
794 if (mddev
->minor_version
>= 91) {
795 mddev
->reshape_position
= sb
->reshape_position
;
796 mddev
->delta_disks
= sb
->delta_disks
;
797 mddev
->new_level
= sb
->new_level
;
798 mddev
->new_layout
= sb
->new_layout
;
799 mddev
->new_chunk
= sb
->new_chunk
;
801 mddev
->reshape_position
= MaxSector
;
802 mddev
->delta_disks
= 0;
803 mddev
->new_level
= mddev
->level
;
804 mddev
->new_layout
= mddev
->layout
;
805 mddev
->new_chunk
= mddev
->chunk_size
;
808 if (sb
->state
& (1<<MD_SB_CLEAN
))
809 mddev
->recovery_cp
= MaxSector
;
811 if (sb
->events_hi
== sb
->cp_events_hi
&&
812 sb
->events_lo
== sb
->cp_events_lo
) {
813 mddev
->recovery_cp
= sb
->recovery_cp
;
815 mddev
->recovery_cp
= 0;
818 memcpy(mddev
->uuid
+0, &sb
->set_uuid0
, 4);
819 memcpy(mddev
->uuid
+4, &sb
->set_uuid1
, 4);
820 memcpy(mddev
->uuid
+8, &sb
->set_uuid2
, 4);
821 memcpy(mddev
->uuid
+12,&sb
->set_uuid3
, 4);
823 mddev
->max_disks
= MD_SB_DISKS
;
825 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
) &&
826 mddev
->bitmap_file
== NULL
)
827 mddev
->bitmap_offset
= mddev
->default_bitmap_offset
;
829 } else if (mddev
->pers
== NULL
) {
830 /* Insist on good event counter while assembling */
832 if (ev1
< mddev
->events
)
834 } else if (mddev
->bitmap
) {
835 /* if adding to array with a bitmap, then we can accept an
836 * older device ... but not too old.
838 if (ev1
< mddev
->bitmap
->events_cleared
)
841 if (ev1
< mddev
->events
)
842 /* just a hot-add of a new device, leave raid_disk at -1 */
846 if (mddev
->level
!= LEVEL_MULTIPATH
) {
847 desc
= sb
->disks
+ rdev
->desc_nr
;
849 if (desc
->state
& (1<<MD_DISK_FAULTY
))
850 set_bit(Faulty
, &rdev
->flags
);
851 else if (desc
->state
& (1<<MD_DISK_SYNC
) /* &&
852 desc->raid_disk < mddev->raid_disks */) {
853 set_bit(In_sync
, &rdev
->flags
);
854 rdev
->raid_disk
= desc
->raid_disk
;
856 if (desc
->state
& (1<<MD_DISK_WRITEMOSTLY
))
857 set_bit(WriteMostly
, &rdev
->flags
);
858 } else /* MULTIPATH are always insync */
859 set_bit(In_sync
, &rdev
->flags
);
864 * sync_super for 0.90.0
866 static void super_90_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
869 struct list_head
*tmp
;
871 int next_spare
= mddev
->raid_disks
;
874 /* make rdev->sb match mddev data..
877 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
878 * 3/ any empty disks < next_spare become removed
880 * disks[0] gets initialised to REMOVED because
881 * we cannot be sure from other fields if it has
882 * been initialised or not.
885 int active
=0, working
=0,failed
=0,spare
=0,nr_disks
=0;
887 rdev
->sb_size
= MD_SB_BYTES
;
889 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
891 memset(sb
, 0, sizeof(*sb
));
893 sb
->md_magic
= MD_SB_MAGIC
;
894 sb
->major_version
= mddev
->major_version
;
895 sb
->patch_version
= mddev
->patch_version
;
896 sb
->gvalid_words
= 0; /* ignored */
897 memcpy(&sb
->set_uuid0
, mddev
->uuid
+0, 4);
898 memcpy(&sb
->set_uuid1
, mddev
->uuid
+4, 4);
899 memcpy(&sb
->set_uuid2
, mddev
->uuid
+8, 4);
900 memcpy(&sb
->set_uuid3
, mddev
->uuid
+12,4);
902 sb
->ctime
= mddev
->ctime
;
903 sb
->level
= mddev
->level
;
904 sb
->size
= mddev
->size
;
905 sb
->raid_disks
= mddev
->raid_disks
;
906 sb
->md_minor
= mddev
->md_minor
;
907 sb
->not_persistent
= !mddev
->persistent
;
908 sb
->utime
= mddev
->utime
;
910 sb
->events_hi
= (mddev
->events
>>32);
911 sb
->events_lo
= (u32
)mddev
->events
;
913 if (mddev
->reshape_position
== MaxSector
)
914 sb
->minor_version
= 90;
916 sb
->minor_version
= 91;
917 sb
->reshape_position
= mddev
->reshape_position
;
918 sb
->new_level
= mddev
->new_level
;
919 sb
->delta_disks
= mddev
->delta_disks
;
920 sb
->new_layout
= mddev
->new_layout
;
921 sb
->new_chunk
= mddev
->new_chunk
;
923 mddev
->minor_version
= sb
->minor_version
;
926 sb
->recovery_cp
= mddev
->recovery_cp
;
927 sb
->cp_events_hi
= (mddev
->events
>>32);
928 sb
->cp_events_lo
= (u32
)mddev
->events
;
929 if (mddev
->recovery_cp
== MaxSector
)
930 sb
->state
= (1<< MD_SB_CLEAN
);
934 sb
->layout
= mddev
->layout
;
935 sb
->chunk_size
= mddev
->chunk_size
;
937 if (mddev
->bitmap
&& mddev
->bitmap_file
== NULL
)
938 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
940 sb
->disks
[0].state
= (1<<MD_DISK_REMOVED
);
941 ITERATE_RDEV(mddev
,rdev2
,tmp
) {
944 if (rdev2
->raid_disk
>= 0 && test_bit(In_sync
, &rdev2
->flags
)
945 && !test_bit(Faulty
, &rdev2
->flags
))
946 desc_nr
= rdev2
->raid_disk
;
948 desc_nr
= next_spare
++;
949 rdev2
->desc_nr
= desc_nr
;
950 d
= &sb
->disks
[rdev2
->desc_nr
];
952 d
->number
= rdev2
->desc_nr
;
953 d
->major
= MAJOR(rdev2
->bdev
->bd_dev
);
954 d
->minor
= MINOR(rdev2
->bdev
->bd_dev
);
955 if (rdev2
->raid_disk
>= 0 && test_bit(In_sync
, &rdev2
->flags
)
956 && !test_bit(Faulty
, &rdev2
->flags
))
957 d
->raid_disk
= rdev2
->raid_disk
;
959 d
->raid_disk
= rdev2
->desc_nr
; /* compatibility */
960 if (test_bit(Faulty
, &rdev2
->flags
))
961 d
->state
= (1<<MD_DISK_FAULTY
);
962 else if (test_bit(In_sync
, &rdev2
->flags
)) {
963 d
->state
= (1<<MD_DISK_ACTIVE
);
964 d
->state
|= (1<<MD_DISK_SYNC
);
972 if (test_bit(WriteMostly
, &rdev2
->flags
))
973 d
->state
|= (1<<MD_DISK_WRITEMOSTLY
);
975 /* now set the "removed" and "faulty" bits on any missing devices */
976 for (i
=0 ; i
< mddev
->raid_disks
; i
++) {
977 mdp_disk_t
*d
= &sb
->disks
[i
];
978 if (d
->state
== 0 && d
->number
== 0) {
981 d
->state
= (1<<MD_DISK_REMOVED
);
982 d
->state
|= (1<<MD_DISK_FAULTY
);
986 sb
->nr_disks
= nr_disks
;
987 sb
->active_disks
= active
;
988 sb
->working_disks
= working
;
989 sb
->failed_disks
= failed
;
990 sb
->spare_disks
= spare
;
992 sb
->this_disk
= sb
->disks
[rdev
->desc_nr
];
993 sb
->sb_csum
= calc_sb_csum(sb
);
997 * version 1 superblock
1000 static __le32
calc_sb_1_csum(struct mdp_superblock_1
* sb
)
1004 unsigned long long newcsum
;
1005 int size
= 256 + le32_to_cpu(sb
->max_dev
)*2;
1006 __le32
*isuper
= (__le32
*)sb
;
1009 disk_csum
= sb
->sb_csum
;
1012 for (i
=0; size
>=4; size
-= 4 )
1013 newcsum
+= le32_to_cpu(*isuper
++);
1016 newcsum
+= le16_to_cpu(*(__le16
*) isuper
);
1018 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
1019 sb
->sb_csum
= disk_csum
;
1020 return cpu_to_le32(csum
);
1023 static int super_1_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
1025 struct mdp_superblock_1
*sb
;
1028 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1032 * Calculate the position of the superblock.
1033 * It is always aligned to a 4K boundary and
1034 * depeding on minor_version, it can be:
1035 * 0: At least 8K, but less than 12K, from end of device
1036 * 1: At start of device
1037 * 2: 4K from start of device.
1039 switch(minor_version
) {
1041 sb_offset
= rdev
->bdev
->bd_inode
->i_size
>> 9;
1043 sb_offset
&= ~(sector_t
)(4*2-1);
1044 /* convert from sectors to K */
1056 rdev
->sb_offset
= sb_offset
;
1058 /* superblock is rarely larger than 1K, but it can be larger,
1059 * and it is safe to read 4k, so we do that
1061 ret
= read_disk_sb(rdev
, 4096);
1062 if (ret
) return ret
;
1065 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1067 if (sb
->magic
!= cpu_to_le32(MD_SB_MAGIC
) ||
1068 sb
->major_version
!= cpu_to_le32(1) ||
1069 le32_to_cpu(sb
->max_dev
) > (4096-256)/2 ||
1070 le64_to_cpu(sb
->super_offset
) != (rdev
->sb_offset
<<1) ||
1071 (le32_to_cpu(sb
->feature_map
) & ~MD_FEATURE_ALL
) != 0)
1074 if (calc_sb_1_csum(sb
) != sb
->sb_csum
) {
1075 printk("md: invalid superblock checksum on %s\n",
1076 bdevname(rdev
->bdev
,b
));
1079 if (le64_to_cpu(sb
->data_size
) < 10) {
1080 printk("md: data_size too small on %s\n",
1081 bdevname(rdev
->bdev
,b
));
1084 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BITMAP_OFFSET
)) {
1085 if (sb
->level
!= cpu_to_le32(1) &&
1086 sb
->level
!= cpu_to_le32(4) &&
1087 sb
->level
!= cpu_to_le32(5) &&
1088 sb
->level
!= cpu_to_le32(6) &&
1089 sb
->level
!= cpu_to_le32(10)) {
1091 "md: bitmaps not supported for this level.\n");
1096 rdev
->preferred_minor
= 0xffff;
1097 rdev
->data_offset
= le64_to_cpu(sb
->data_offset
);
1098 atomic_set(&rdev
->corrected_errors
, le32_to_cpu(sb
->cnt_corrected_read
));
1100 rdev
->sb_size
= le32_to_cpu(sb
->max_dev
) * 2 + 256;
1101 bmask
= queue_hardsect_size(rdev
->bdev
->bd_disk
->queue
)-1;
1102 if (rdev
->sb_size
& bmask
)
1103 rdev
-> sb_size
= (rdev
->sb_size
| bmask
)+1;
1105 if (sb
->level
== cpu_to_le32(LEVEL_MULTIPATH
))
1108 rdev
->desc_nr
= le32_to_cpu(sb
->dev_number
);
1114 struct mdp_superblock_1
*refsb
=
1115 (struct mdp_superblock_1
*)page_address(refdev
->sb_page
);
1117 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16) != 0 ||
1118 sb
->level
!= refsb
->level
||
1119 sb
->layout
!= refsb
->layout
||
1120 sb
->chunksize
!= refsb
->chunksize
) {
1121 printk(KERN_WARNING
"md: %s has strangely different"
1122 " superblock to %s\n",
1123 bdevname(rdev
->bdev
,b
),
1124 bdevname(refdev
->bdev
,b2
));
1127 ev1
= le64_to_cpu(sb
->events
);
1128 ev2
= le64_to_cpu(refsb
->events
);
1136 rdev
->size
= ((rdev
->bdev
->bd_inode
->i_size
>>9) - le64_to_cpu(sb
->data_offset
)) / 2;
1138 rdev
->size
= rdev
->sb_offset
;
1139 if (rdev
->size
< le64_to_cpu(sb
->data_size
)/2)
1141 rdev
->size
= le64_to_cpu(sb
->data_size
)/2;
1142 if (le32_to_cpu(sb
->chunksize
))
1143 rdev
->size
&= ~((sector_t
)le32_to_cpu(sb
->chunksize
)/2 - 1);
1145 if (le64_to_cpu(sb
->size
) > rdev
->size
*2)
1150 static int super_1_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1152 struct mdp_superblock_1
*sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1153 __u64 ev1
= le64_to_cpu(sb
->events
);
1155 rdev
->raid_disk
= -1;
1157 if (mddev
->raid_disks
== 0) {
1158 mddev
->major_version
= 1;
1159 mddev
->patch_version
= 0;
1160 mddev
->persistent
= 1;
1161 mddev
->chunk_size
= le32_to_cpu(sb
->chunksize
) << 9;
1162 mddev
->ctime
= le64_to_cpu(sb
->ctime
) & ((1ULL << 32)-1);
1163 mddev
->utime
= le64_to_cpu(sb
->utime
) & ((1ULL << 32)-1);
1164 mddev
->level
= le32_to_cpu(sb
->level
);
1165 mddev
->clevel
[0] = 0;
1166 mddev
->layout
= le32_to_cpu(sb
->layout
);
1167 mddev
->raid_disks
= le32_to_cpu(sb
->raid_disks
);
1168 mddev
->size
= le64_to_cpu(sb
->size
)/2;
1169 mddev
->events
= ev1
;
1170 mddev
->bitmap_offset
= 0;
1171 mddev
->default_bitmap_offset
= 1024 >> 9;
1173 mddev
->recovery_cp
= le64_to_cpu(sb
->resync_offset
);
1174 memcpy(mddev
->uuid
, sb
->set_uuid
, 16);
1176 mddev
->max_disks
= (4096-256)/2;
1178 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BITMAP_OFFSET
) &&
1179 mddev
->bitmap_file
== NULL
)
1180 mddev
->bitmap_offset
= (__s32
)le32_to_cpu(sb
->bitmap_offset
);
1182 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
1183 mddev
->reshape_position
= le64_to_cpu(sb
->reshape_position
);
1184 mddev
->delta_disks
= le32_to_cpu(sb
->delta_disks
);
1185 mddev
->new_level
= le32_to_cpu(sb
->new_level
);
1186 mddev
->new_layout
= le32_to_cpu(sb
->new_layout
);
1187 mddev
->new_chunk
= le32_to_cpu(sb
->new_chunk
)<<9;
1189 mddev
->reshape_position
= MaxSector
;
1190 mddev
->delta_disks
= 0;
1191 mddev
->new_level
= mddev
->level
;
1192 mddev
->new_layout
= mddev
->layout
;
1193 mddev
->new_chunk
= mddev
->chunk_size
;
1196 } else if (mddev
->pers
== NULL
) {
1197 /* Insist of good event counter while assembling */
1199 if (ev1
< mddev
->events
)
1201 } else if (mddev
->bitmap
) {
1202 /* If adding to array with a bitmap, then we can accept an
1203 * older device, but not too old.
1205 if (ev1
< mddev
->bitmap
->events_cleared
)
1208 if (ev1
< mddev
->events
)
1209 /* just a hot-add of a new device, leave raid_disk at -1 */
1212 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1214 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
1216 case 0xffff: /* spare */
1218 case 0xfffe: /* faulty */
1219 set_bit(Faulty
, &rdev
->flags
);
1222 if ((le32_to_cpu(sb
->feature_map
) &
1223 MD_FEATURE_RECOVERY_OFFSET
))
1224 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
1226 set_bit(In_sync
, &rdev
->flags
);
1227 rdev
->raid_disk
= role
;
1230 if (sb
->devflags
& WriteMostly1
)
1231 set_bit(WriteMostly
, &rdev
->flags
);
1232 } else /* MULTIPATH are always insync */
1233 set_bit(In_sync
, &rdev
->flags
);
1238 static void super_1_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1240 struct mdp_superblock_1
*sb
;
1241 struct list_head
*tmp
;
1244 /* make rdev->sb match mddev and rdev data. */
1246 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1248 sb
->feature_map
= 0;
1250 sb
->recovery_offset
= cpu_to_le64(0);
1251 memset(sb
->pad1
, 0, sizeof(sb
->pad1
));
1252 memset(sb
->pad2
, 0, sizeof(sb
->pad2
));
1253 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
1255 sb
->utime
= cpu_to_le64((__u64
)mddev
->utime
);
1256 sb
->events
= cpu_to_le64(mddev
->events
);
1258 sb
->resync_offset
= cpu_to_le64(mddev
->recovery_cp
);
1260 sb
->resync_offset
= cpu_to_le64(0);
1262 sb
->cnt_corrected_read
= cpu_to_le32(atomic_read(&rdev
->corrected_errors
));
1264 sb
->raid_disks
= cpu_to_le32(mddev
->raid_disks
);
1265 sb
->size
= cpu_to_le64(mddev
->size
<<1);
1267 if (mddev
->bitmap
&& mddev
->bitmap_file
== NULL
) {
1268 sb
->bitmap_offset
= cpu_to_le32((__u32
)mddev
->bitmap_offset
);
1269 sb
->feature_map
= cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
);
1272 if (rdev
->raid_disk
>= 0 &&
1273 !test_bit(In_sync
, &rdev
->flags
) &&
1274 rdev
->recovery_offset
> 0) {
1275 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET
);
1276 sb
->recovery_offset
= cpu_to_le64(rdev
->recovery_offset
);
1279 if (mddev
->reshape_position
!= MaxSector
) {
1280 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE
);
1281 sb
->reshape_position
= cpu_to_le64(mddev
->reshape_position
);
1282 sb
->new_layout
= cpu_to_le32(mddev
->new_layout
);
1283 sb
->delta_disks
= cpu_to_le32(mddev
->delta_disks
);
1284 sb
->new_level
= cpu_to_le32(mddev
->new_level
);
1285 sb
->new_chunk
= cpu_to_le32(mddev
->new_chunk
>>9);
1289 ITERATE_RDEV(mddev
,rdev2
,tmp
)
1290 if (rdev2
->desc_nr
+1 > max_dev
)
1291 max_dev
= rdev2
->desc_nr
+1;
1293 if (max_dev
> le32_to_cpu(sb
->max_dev
))
1294 sb
->max_dev
= cpu_to_le32(max_dev
);
1295 for (i
=0; i
<max_dev
;i
++)
1296 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1298 ITERATE_RDEV(mddev
,rdev2
,tmp
) {
1300 if (test_bit(Faulty
, &rdev2
->flags
))
1301 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1302 else if (test_bit(In_sync
, &rdev2
->flags
))
1303 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1304 else if (rdev2
->raid_disk
>= 0 && rdev2
->recovery_offset
> 0)
1305 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1307 sb
->dev_roles
[i
] = cpu_to_le16(0xffff);
1310 sb
->sb_csum
= calc_sb_1_csum(sb
);
1314 static struct super_type super_types
[] = {
1317 .owner
= THIS_MODULE
,
1318 .load_super
= super_90_load
,
1319 .validate_super
= super_90_validate
,
1320 .sync_super
= super_90_sync
,
1324 .owner
= THIS_MODULE
,
1325 .load_super
= super_1_load
,
1326 .validate_super
= super_1_validate
,
1327 .sync_super
= super_1_sync
,
1331 static int match_mddev_units(mddev_t
*mddev1
, mddev_t
*mddev2
)
1333 struct list_head
*tmp
, *tmp2
;
1334 mdk_rdev_t
*rdev
, *rdev2
;
1336 ITERATE_RDEV(mddev1
,rdev
,tmp
)
1337 ITERATE_RDEV(mddev2
, rdev2
, tmp2
)
1338 if (rdev
->bdev
->bd_contains
==
1339 rdev2
->bdev
->bd_contains
)
1345 static LIST_HEAD(pending_raid_disks
);
1347 static int bind_rdev_to_array(mdk_rdev_t
* rdev
, mddev_t
* mddev
)
1349 char b
[BDEVNAME_SIZE
];
1358 /* make sure rdev->size exceeds mddev->size */
1359 if (rdev
->size
&& (mddev
->size
== 0 || rdev
->size
< mddev
->size
)) {
1361 /* Cannot change size, so fail
1362 * If mddev->level <= 0, then we don't care
1363 * about aligning sizes (e.g. linear)
1365 if (mddev
->level
> 0)
1368 mddev
->size
= rdev
->size
;
1371 /* Verify rdev->desc_nr is unique.
1372 * If it is -1, assign a free number, else
1373 * check number is not in use
1375 if (rdev
->desc_nr
< 0) {
1377 if (mddev
->pers
) choice
= mddev
->raid_disks
;
1378 while (find_rdev_nr(mddev
, choice
))
1380 rdev
->desc_nr
= choice
;
1382 if (find_rdev_nr(mddev
, rdev
->desc_nr
))
1385 bdevname(rdev
->bdev
,b
);
1386 while ( (s
=strchr(b
, '/')) != NULL
)
1389 rdev
->mddev
= mddev
;
1390 printk(KERN_INFO
"md: bind<%s>\n", b
);
1392 if ((err
= kobject_add(&rdev
->kobj
, &mddev
->kobj
, "dev-%s", b
)))
1395 if (rdev
->bdev
->bd_part
)
1396 ko
= &rdev
->bdev
->bd_part
->dev
.kobj
;
1398 ko
= &rdev
->bdev
->bd_disk
->dev
.kobj
;
1399 if ((err
= sysfs_create_link(&rdev
->kobj
, ko
, "block"))) {
1400 kobject_del(&rdev
->kobj
);
1403 list_add(&rdev
->same_set
, &mddev
->disks
);
1404 bd_claim_by_disk(rdev
->bdev
, rdev
, mddev
->gendisk
);
1408 printk(KERN_WARNING
"md: failed to register dev-%s for %s\n",
1413 static void delayed_delete(struct work_struct
*ws
)
1415 mdk_rdev_t
*rdev
= container_of(ws
, mdk_rdev_t
, del_work
);
1416 kobject_del(&rdev
->kobj
);
1419 static void unbind_rdev_from_array(mdk_rdev_t
* rdev
)
1421 char b
[BDEVNAME_SIZE
];
1426 bd_release_from_disk(rdev
->bdev
, rdev
->mddev
->gendisk
);
1427 list_del_init(&rdev
->same_set
);
1428 printk(KERN_INFO
"md: unbind<%s>\n", bdevname(rdev
->bdev
,b
));
1430 sysfs_remove_link(&rdev
->kobj
, "block");
1432 /* We need to delay this, otherwise we can deadlock when
1433 * writing to 'remove' to "dev/state"
1435 INIT_WORK(&rdev
->del_work
, delayed_delete
);
1436 schedule_work(&rdev
->del_work
);
1440 * prevent the device from being mounted, repartitioned or
1441 * otherwise reused by a RAID array (or any other kernel
1442 * subsystem), by bd_claiming the device.
1444 static int lock_rdev(mdk_rdev_t
*rdev
, dev_t dev
)
1447 struct block_device
*bdev
;
1448 char b
[BDEVNAME_SIZE
];
1450 bdev
= open_by_devnum(dev
, FMODE_READ
|FMODE_WRITE
);
1452 printk(KERN_ERR
"md: could not open %s.\n",
1453 __bdevname(dev
, b
));
1454 return PTR_ERR(bdev
);
1456 err
= bd_claim(bdev
, rdev
);
1458 printk(KERN_ERR
"md: could not bd_claim %s.\n",
1467 static void unlock_rdev(mdk_rdev_t
*rdev
)
1469 struct block_device
*bdev
= rdev
->bdev
;
1477 void md_autodetect_dev(dev_t dev
);
1479 static void export_rdev(mdk_rdev_t
* rdev
)
1481 char b
[BDEVNAME_SIZE
];
1482 printk(KERN_INFO
"md: export_rdev(%s)\n",
1483 bdevname(rdev
->bdev
,b
));
1487 list_del_init(&rdev
->same_set
);
1489 md_autodetect_dev(rdev
->bdev
->bd_dev
);
1492 kobject_put(&rdev
->kobj
);
1495 static void kick_rdev_from_array(mdk_rdev_t
* rdev
)
1497 unbind_rdev_from_array(rdev
);
1501 static void export_array(mddev_t
*mddev
)
1503 struct list_head
*tmp
;
1506 ITERATE_RDEV(mddev
,rdev
,tmp
) {
1511 kick_rdev_from_array(rdev
);
1513 if (!list_empty(&mddev
->disks
))
1515 mddev
->raid_disks
= 0;
1516 mddev
->major_version
= 0;
1519 static void print_desc(mdp_disk_t
*desc
)
1521 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc
->number
,
1522 desc
->major
,desc
->minor
,desc
->raid_disk
,desc
->state
);
1525 static void print_sb(mdp_super_t
*sb
)
1530 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1531 sb
->major_version
, sb
->minor_version
, sb
->patch_version
,
1532 sb
->set_uuid0
, sb
->set_uuid1
, sb
->set_uuid2
, sb
->set_uuid3
,
1534 printk(KERN_INFO
"md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1535 sb
->level
, sb
->size
, sb
->nr_disks
, sb
->raid_disks
,
1536 sb
->md_minor
, sb
->layout
, sb
->chunk_size
);
1537 printk(KERN_INFO
"md: UT:%08x ST:%d AD:%d WD:%d"
1538 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1539 sb
->utime
, sb
->state
, sb
->active_disks
, sb
->working_disks
,
1540 sb
->failed_disks
, sb
->spare_disks
,
1541 sb
->sb_csum
, (unsigned long)sb
->events_lo
);
1544 for (i
= 0; i
< MD_SB_DISKS
; i
++) {
1547 desc
= sb
->disks
+ i
;
1548 if (desc
->number
|| desc
->major
|| desc
->minor
||
1549 desc
->raid_disk
|| (desc
->state
&& (desc
->state
!= 4))) {
1550 printk(" D %2d: ", i
);
1554 printk(KERN_INFO
"md: THIS: ");
1555 print_desc(&sb
->this_disk
);
1559 static void print_rdev(mdk_rdev_t
*rdev
)
1561 char b
[BDEVNAME_SIZE
];
1562 printk(KERN_INFO
"md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1563 bdevname(rdev
->bdev
,b
), (unsigned long long)rdev
->size
,
1564 test_bit(Faulty
, &rdev
->flags
), test_bit(In_sync
, &rdev
->flags
),
1566 if (rdev
->sb_loaded
) {
1567 printk(KERN_INFO
"md: rdev superblock:\n");
1568 print_sb((mdp_super_t
*)page_address(rdev
->sb_page
));
1570 printk(KERN_INFO
"md: no rdev superblock!\n");
1573 static void md_print_devices(void)
1575 struct list_head
*tmp
, *tmp2
;
1578 char b
[BDEVNAME_SIZE
];
1581 printk("md: **********************************\n");
1582 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1583 printk("md: **********************************\n");
1584 ITERATE_MDDEV(mddev
,tmp
) {
1587 bitmap_print_sb(mddev
->bitmap
);
1589 printk("%s: ", mdname(mddev
));
1590 ITERATE_RDEV(mddev
,rdev
,tmp2
)
1591 printk("<%s>", bdevname(rdev
->bdev
,b
));
1594 ITERATE_RDEV(mddev
,rdev
,tmp2
)
1597 printk("md: **********************************\n");
1602 static void sync_sbs(mddev_t
* mddev
, int nospares
)
1604 /* Update each superblock (in-memory image), but
1605 * if we are allowed to, skip spares which already
1606 * have the right event counter, or have one earlier
1607 * (which would mean they aren't being marked as dirty
1608 * with the rest of the array)
1611 struct list_head
*tmp
;
1613 ITERATE_RDEV(mddev
,rdev
,tmp
) {
1614 if (rdev
->sb_events
== mddev
->events
||
1616 rdev
->raid_disk
< 0 &&
1617 (rdev
->sb_events
&1)==0 &&
1618 rdev
->sb_events
+1 == mddev
->events
)) {
1619 /* Don't update this superblock */
1620 rdev
->sb_loaded
= 2;
1622 super_types
[mddev
->major_version
].
1623 sync_super(mddev
, rdev
);
1624 rdev
->sb_loaded
= 1;
1629 static void md_update_sb(mddev_t
* mddev
, int force_change
)
1631 struct list_head
*tmp
;
1637 spin_lock_irq(&mddev
->write_lock
);
1639 set_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
1640 if (test_and_clear_bit(MD_CHANGE_DEVS
, &mddev
->flags
))
1642 if (test_and_clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
))
1643 /* just a clean<-> dirty transition, possibly leave spares alone,
1644 * though if events isn't the right even/odd, we will have to do
1650 if (mddev
->degraded
)
1651 /* If the array is degraded, then skipping spares is both
1652 * dangerous and fairly pointless.
1653 * Dangerous because a device that was removed from the array
1654 * might have a event_count that still looks up-to-date,
1655 * so it can be re-added without a resync.
1656 * Pointless because if there are any spares to skip,
1657 * then a recovery will happen and soon that array won't
1658 * be degraded any more and the spare can go back to sleep then.
1662 sync_req
= mddev
->in_sync
;
1663 mddev
->utime
= get_seconds();
1665 /* If this is just a dirty<->clean transition, and the array is clean
1666 * and 'events' is odd, we can roll back to the previous clean state */
1668 && (mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
1669 && (mddev
->events
& 1)
1670 && mddev
->events
!= 1)
1673 /* otherwise we have to go forward and ... */
1675 if (!mddev
->in_sync
|| mddev
->recovery_cp
!= MaxSector
) { /* not clean */
1676 /* .. if the array isn't clean, insist on an odd 'events' */
1677 if ((mddev
->events
&1)==0) {
1682 /* otherwise insist on an even 'events' (for clean states) */
1683 if ((mddev
->events
&1)) {
1690 if (!mddev
->events
) {
1692 * oops, this 64-bit counter should never wrap.
1693 * Either we are in around ~1 trillion A.C., assuming
1694 * 1 reboot per second, or we have a bug:
1699 sync_sbs(mddev
, nospares
);
1702 * do not write anything to disk if using
1703 * nonpersistent superblocks
1705 if (!mddev
->persistent
) {
1706 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
1707 spin_unlock_irq(&mddev
->write_lock
);
1708 wake_up(&mddev
->sb_wait
);
1711 spin_unlock_irq(&mddev
->write_lock
);
1714 "md: updating %s RAID superblock on device (in sync %d)\n",
1715 mdname(mddev
),mddev
->in_sync
);
1717 bitmap_update_sb(mddev
->bitmap
);
1718 ITERATE_RDEV(mddev
,rdev
,tmp
) {
1719 char b
[BDEVNAME_SIZE
];
1720 dprintk(KERN_INFO
"md: ");
1721 if (rdev
->sb_loaded
!= 1)
1722 continue; /* no noise on spare devices */
1723 if (test_bit(Faulty
, &rdev
->flags
))
1724 dprintk("(skipping faulty ");
1726 dprintk("%s ", bdevname(rdev
->bdev
,b
));
1727 if (!test_bit(Faulty
, &rdev
->flags
)) {
1728 md_super_write(mddev
,rdev
,
1729 rdev
->sb_offset
<<1, rdev
->sb_size
,
1731 dprintk(KERN_INFO
"(write) %s's sb offset: %llu\n",
1732 bdevname(rdev
->bdev
,b
),
1733 (unsigned long long)rdev
->sb_offset
);
1734 rdev
->sb_events
= mddev
->events
;
1738 if (mddev
->level
== LEVEL_MULTIPATH
)
1739 /* only need to write one superblock... */
1742 md_super_wait(mddev
);
1743 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
1745 spin_lock_irq(&mddev
->write_lock
);
1746 if (mddev
->in_sync
!= sync_req
||
1747 test_bit(MD_CHANGE_DEVS
, &mddev
->flags
)) {
1748 /* have to write it out again */
1749 spin_unlock_irq(&mddev
->write_lock
);
1752 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
1753 spin_unlock_irq(&mddev
->write_lock
);
1754 wake_up(&mddev
->sb_wait
);
1758 /* words written to sysfs files may, or my not, be \n terminated.
1759 * We want to accept with case. For this we use cmd_match.
1761 static int cmd_match(const char *cmd
, const char *str
)
1763 /* See if cmd, written into a sysfs file, matches
1764 * str. They must either be the same, or cmd can
1765 * have a trailing newline
1767 while (*cmd
&& *str
&& *cmd
== *str
) {
1778 struct rdev_sysfs_entry
{
1779 struct attribute attr
;
1780 ssize_t (*show
)(mdk_rdev_t
*, char *);
1781 ssize_t (*store
)(mdk_rdev_t
*, const char *, size_t);
1785 state_show(mdk_rdev_t
*rdev
, char *page
)
1790 if (test_bit(Faulty
, &rdev
->flags
)) {
1791 len
+= sprintf(page
+len
, "%sfaulty",sep
);
1794 if (test_bit(In_sync
, &rdev
->flags
)) {
1795 len
+= sprintf(page
+len
, "%sin_sync",sep
);
1798 if (test_bit(WriteMostly
, &rdev
->flags
)) {
1799 len
+= sprintf(page
+len
, "%swrite_mostly",sep
);
1802 if (!test_bit(Faulty
, &rdev
->flags
) &&
1803 !test_bit(In_sync
, &rdev
->flags
)) {
1804 len
+= sprintf(page
+len
, "%sspare", sep
);
1807 return len
+sprintf(page
+len
, "\n");
1811 state_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
1814 * faulty - simulates and error
1815 * remove - disconnects the device
1816 * writemostly - sets write_mostly
1817 * -writemostly - clears write_mostly
1820 if (cmd_match(buf
, "faulty") && rdev
->mddev
->pers
) {
1821 md_error(rdev
->mddev
, rdev
);
1823 } else if (cmd_match(buf
, "remove")) {
1824 if (rdev
->raid_disk
>= 0)
1827 mddev_t
*mddev
= rdev
->mddev
;
1828 kick_rdev_from_array(rdev
);
1830 md_update_sb(mddev
, 1);
1831 md_new_event(mddev
);
1834 } else if (cmd_match(buf
, "writemostly")) {
1835 set_bit(WriteMostly
, &rdev
->flags
);
1837 } else if (cmd_match(buf
, "-writemostly")) {
1838 clear_bit(WriteMostly
, &rdev
->flags
);
1841 return err
? err
: len
;
1843 static struct rdev_sysfs_entry rdev_state
=
1844 __ATTR(state
, S_IRUGO
|S_IWUSR
, state_show
, state_store
);
1847 super_show(mdk_rdev_t
*rdev
, char *page
)
1849 if (rdev
->sb_loaded
&& rdev
->sb_size
) {
1850 memcpy(page
, page_address(rdev
->sb_page
), rdev
->sb_size
);
1851 return rdev
->sb_size
;
1855 static struct rdev_sysfs_entry rdev_super
= __ATTR_RO(super
);
1858 errors_show(mdk_rdev_t
*rdev
, char *page
)
1860 return sprintf(page
, "%d\n", atomic_read(&rdev
->corrected_errors
));
1864 errors_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
1867 unsigned long n
= simple_strtoul(buf
, &e
, 10);
1868 if (*buf
&& (*e
== 0 || *e
== '\n')) {
1869 atomic_set(&rdev
->corrected_errors
, n
);
1874 static struct rdev_sysfs_entry rdev_errors
=
1875 __ATTR(errors
, S_IRUGO
|S_IWUSR
, errors_show
, errors_store
);
1878 slot_show(mdk_rdev_t
*rdev
, char *page
)
1880 if (rdev
->raid_disk
< 0)
1881 return sprintf(page
, "none\n");
1883 return sprintf(page
, "%d\n", rdev
->raid_disk
);
1887 slot_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
1890 int slot
= simple_strtoul(buf
, &e
, 10);
1891 if (strncmp(buf
, "none", 4)==0)
1893 else if (e
==buf
|| (*e
&& *e
!= '\n'))
1895 if (rdev
->mddev
->pers
)
1896 /* Cannot set slot in active array (yet) */
1898 if (slot
>= rdev
->mddev
->raid_disks
)
1900 rdev
->raid_disk
= slot
;
1901 /* assume it is working */
1903 set_bit(In_sync
, &rdev
->flags
);
1908 static struct rdev_sysfs_entry rdev_slot
=
1909 __ATTR(slot
, S_IRUGO
|S_IWUSR
, slot_show
, slot_store
);
1912 offset_show(mdk_rdev_t
*rdev
, char *page
)
1914 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->data_offset
);
1918 offset_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
1921 unsigned long long offset
= simple_strtoull(buf
, &e
, 10);
1922 if (e
==buf
|| (*e
&& *e
!= '\n'))
1924 if (rdev
->mddev
->pers
)
1926 rdev
->data_offset
= offset
;
1930 static struct rdev_sysfs_entry rdev_offset
=
1931 __ATTR(offset
, S_IRUGO
|S_IWUSR
, offset_show
, offset_store
);
1934 rdev_size_show(mdk_rdev_t
*rdev
, char *page
)
1936 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->size
);
1940 rdev_size_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
1943 unsigned long long size
= simple_strtoull(buf
, &e
, 10);
1944 if (e
==buf
|| (*e
&& *e
!= '\n'))
1946 if (rdev
->mddev
->pers
)
1949 if (size
< rdev
->mddev
->size
|| rdev
->mddev
->size
== 0)
1950 rdev
->mddev
->size
= size
;
1954 static struct rdev_sysfs_entry rdev_size
=
1955 __ATTR(size
, S_IRUGO
|S_IWUSR
, rdev_size_show
, rdev_size_store
);
1957 static struct attribute
*rdev_default_attrs
[] = {
1967 rdev_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
1969 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
1970 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
1974 return entry
->show(rdev
, page
);
1978 rdev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
1979 const char *page
, size_t length
)
1981 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
1982 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
1986 if (!capable(CAP_SYS_ADMIN
))
1988 return entry
->store(rdev
, page
, length
);
1991 static void rdev_free(struct kobject
*ko
)
1993 mdk_rdev_t
*rdev
= container_of(ko
, mdk_rdev_t
, kobj
);
1996 static struct sysfs_ops rdev_sysfs_ops
= {
1997 .show
= rdev_attr_show
,
1998 .store
= rdev_attr_store
,
2000 static struct kobj_type rdev_ktype
= {
2001 .release
= rdev_free
,
2002 .sysfs_ops
= &rdev_sysfs_ops
,
2003 .default_attrs
= rdev_default_attrs
,
2007 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2009 * mark the device faulty if:
2011 * - the device is nonexistent (zero size)
2012 * - the device has no valid superblock
2014 * a faulty rdev _never_ has rdev->sb set.
2016 static mdk_rdev_t
*md_import_device(dev_t newdev
, int super_format
, int super_minor
)
2018 char b
[BDEVNAME_SIZE
];
2023 rdev
= kzalloc(sizeof(*rdev
), GFP_KERNEL
);
2025 printk(KERN_ERR
"md: could not alloc mem for new device!\n");
2026 return ERR_PTR(-ENOMEM
);
2029 if ((err
= alloc_disk_sb(rdev
)))
2032 err
= lock_rdev(rdev
, newdev
);
2036 kobject_init(&rdev
->kobj
, &rdev_ktype
);
2039 rdev
->saved_raid_disk
= -1;
2040 rdev
->raid_disk
= -1;
2042 rdev
->data_offset
= 0;
2043 rdev
->sb_events
= 0;
2044 atomic_set(&rdev
->nr_pending
, 0);
2045 atomic_set(&rdev
->read_errors
, 0);
2046 atomic_set(&rdev
->corrected_errors
, 0);
2048 size
= rdev
->bdev
->bd_inode
->i_size
>> BLOCK_SIZE_BITS
;
2051 "md: %s has zero or unknown size, marking faulty!\n",
2052 bdevname(rdev
->bdev
,b
));
2057 if (super_format
>= 0) {
2058 err
= super_types
[super_format
].
2059 load_super(rdev
, NULL
, super_minor
);
2060 if (err
== -EINVAL
) {
2062 "md: %s does not have a valid v%d.%d "
2063 "superblock, not importing!\n",
2064 bdevname(rdev
->bdev
,b
),
2065 super_format
, super_minor
);
2070 "md: could not read %s's sb, not importing!\n",
2071 bdevname(rdev
->bdev
,b
));
2075 INIT_LIST_HEAD(&rdev
->same_set
);
2080 if (rdev
->sb_page
) {
2086 return ERR_PTR(err
);
2090 * Check a full RAID array for plausibility
2094 static void analyze_sbs(mddev_t
* mddev
)
2097 struct list_head
*tmp
;
2098 mdk_rdev_t
*rdev
, *freshest
;
2099 char b
[BDEVNAME_SIZE
];
2102 ITERATE_RDEV(mddev
,rdev
,tmp
)
2103 switch (super_types
[mddev
->major_version
].
2104 load_super(rdev
, freshest
, mddev
->minor_version
)) {
2112 "md: fatal superblock inconsistency in %s"
2113 " -- removing from array\n",
2114 bdevname(rdev
->bdev
,b
));
2115 kick_rdev_from_array(rdev
);
2119 super_types
[mddev
->major_version
].
2120 validate_super(mddev
, freshest
);
2123 ITERATE_RDEV(mddev
,rdev
,tmp
) {
2124 if (rdev
!= freshest
)
2125 if (super_types
[mddev
->major_version
].
2126 validate_super(mddev
, rdev
)) {
2127 printk(KERN_WARNING
"md: kicking non-fresh %s"
2129 bdevname(rdev
->bdev
,b
));
2130 kick_rdev_from_array(rdev
);
2133 if (mddev
->level
== LEVEL_MULTIPATH
) {
2134 rdev
->desc_nr
= i
++;
2135 rdev
->raid_disk
= rdev
->desc_nr
;
2136 set_bit(In_sync
, &rdev
->flags
);
2137 } else if (rdev
->raid_disk
>= mddev
->raid_disks
) {
2138 rdev
->raid_disk
= -1;
2139 clear_bit(In_sync
, &rdev
->flags
);
2145 if (mddev
->recovery_cp
!= MaxSector
&&
2147 printk(KERN_ERR
"md: %s: raid array is not clean"
2148 " -- starting background reconstruction\n",
2154 safe_delay_show(mddev_t
*mddev
, char *page
)
2156 int msec
= (mddev
->safemode_delay
*1000)/HZ
;
2157 return sprintf(page
, "%d.%03d\n", msec
/1000, msec
%1000);
2160 safe_delay_store(mddev_t
*mddev
, const char *cbuf
, size_t len
)
2168 /* remove a period, and count digits after it */
2169 if (len
>= sizeof(buf
))
2171 strlcpy(buf
, cbuf
, len
);
2173 for (i
=0; i
<len
; i
++) {
2175 if (isdigit(buf
[i
])) {
2180 } else if (buf
[i
] == '.') {
2185 msec
= simple_strtoul(buf
, &e
, 10);
2186 if (e
== buf
|| (*e
&& *e
!= '\n'))
2188 msec
= (msec
* 1000) / scale
;
2190 mddev
->safemode_delay
= 0;
2192 mddev
->safemode_delay
= (msec
*HZ
)/1000;
2193 if (mddev
->safemode_delay
== 0)
2194 mddev
->safemode_delay
= 1;
2198 static struct md_sysfs_entry md_safe_delay
=
2199 __ATTR(safe_mode_delay
, S_IRUGO
|S_IWUSR
,safe_delay_show
, safe_delay_store
);
2202 level_show(mddev_t
*mddev
, char *page
)
2204 struct mdk_personality
*p
= mddev
->pers
;
2206 return sprintf(page
, "%s\n", p
->name
);
2207 else if (mddev
->clevel
[0])
2208 return sprintf(page
, "%s\n", mddev
->clevel
);
2209 else if (mddev
->level
!= LEVEL_NONE
)
2210 return sprintf(page
, "%d\n", mddev
->level
);
2216 level_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2223 if (len
>= sizeof(mddev
->clevel
))
2225 strncpy(mddev
->clevel
, buf
, len
);
2226 if (mddev
->clevel
[len
-1] == '\n')
2228 mddev
->clevel
[len
] = 0;
2229 mddev
->level
= LEVEL_NONE
;
2233 static struct md_sysfs_entry md_level
=
2234 __ATTR(level
, S_IRUGO
|S_IWUSR
, level_show
, level_store
);
2238 layout_show(mddev_t
*mddev
, char *page
)
2240 /* just a number, not meaningful for all levels */
2241 if (mddev
->reshape_position
!= MaxSector
&&
2242 mddev
->layout
!= mddev
->new_layout
)
2243 return sprintf(page
, "%d (%d)\n",
2244 mddev
->new_layout
, mddev
->layout
);
2245 return sprintf(page
, "%d\n", mddev
->layout
);
2249 layout_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2252 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2254 if (!*buf
|| (*e
&& *e
!= '\n'))
2259 if (mddev
->reshape_position
!= MaxSector
)
2260 mddev
->new_layout
= n
;
2265 static struct md_sysfs_entry md_layout
=
2266 __ATTR(layout
, S_IRUGO
|S_IWUSR
, layout_show
, layout_store
);
2270 raid_disks_show(mddev_t
*mddev
, char *page
)
2272 if (mddev
->raid_disks
== 0)
2274 if (mddev
->reshape_position
!= MaxSector
&&
2275 mddev
->delta_disks
!= 0)
2276 return sprintf(page
, "%d (%d)\n", mddev
->raid_disks
,
2277 mddev
->raid_disks
- mddev
->delta_disks
);
2278 return sprintf(page
, "%d\n", mddev
->raid_disks
);
2281 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
);
2284 raid_disks_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2288 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2290 if (!*buf
|| (*e
&& *e
!= '\n'))
2294 rv
= update_raid_disks(mddev
, n
);
2295 else if (mddev
->reshape_position
!= MaxSector
) {
2296 int olddisks
= mddev
->raid_disks
- mddev
->delta_disks
;
2297 mddev
->delta_disks
= n
- olddisks
;
2298 mddev
->raid_disks
= n
;
2300 mddev
->raid_disks
= n
;
2301 return rv
? rv
: len
;
2303 static struct md_sysfs_entry md_raid_disks
=
2304 __ATTR(raid_disks
, S_IRUGO
|S_IWUSR
, raid_disks_show
, raid_disks_store
);
2307 chunk_size_show(mddev_t
*mddev
, char *page
)
2309 if (mddev
->reshape_position
!= MaxSector
&&
2310 mddev
->chunk_size
!= mddev
->new_chunk
)
2311 return sprintf(page
, "%d (%d)\n", mddev
->new_chunk
,
2313 return sprintf(page
, "%d\n", mddev
->chunk_size
);
2317 chunk_size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2319 /* can only set chunk_size if array is not yet active */
2321 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2323 if (!*buf
|| (*e
&& *e
!= '\n'))
2328 else if (mddev
->reshape_position
!= MaxSector
)
2329 mddev
->new_chunk
= n
;
2331 mddev
->chunk_size
= n
;
2334 static struct md_sysfs_entry md_chunk_size
=
2335 __ATTR(chunk_size
, S_IRUGO
|S_IWUSR
, chunk_size_show
, chunk_size_store
);
2338 resync_start_show(mddev_t
*mddev
, char *page
)
2340 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->recovery_cp
);
2344 resync_start_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2346 /* can only set chunk_size if array is not yet active */
2348 unsigned long long n
= simple_strtoull(buf
, &e
, 10);
2352 if (!*buf
|| (*e
&& *e
!= '\n'))
2355 mddev
->recovery_cp
= n
;
2358 static struct md_sysfs_entry md_resync_start
=
2359 __ATTR(resync_start
, S_IRUGO
|S_IWUSR
, resync_start_show
, resync_start_store
);
2362 * The array state can be:
2365 * No devices, no size, no level
2366 * Equivalent to STOP_ARRAY ioctl
2368 * May have some settings, but array is not active
2369 * all IO results in error
2370 * When written, doesn't tear down array, but just stops it
2371 * suspended (not supported yet)
2372 * All IO requests will block. The array can be reconfigured.
2373 * Writing this, if accepted, will block until array is quiessent
2375 * no resync can happen. no superblocks get written.
2376 * write requests fail
2378 * like readonly, but behaves like 'clean' on a write request.
2380 * clean - no pending writes, but otherwise active.
2381 * When written to inactive array, starts without resync
2382 * If a write request arrives then
2383 * if metadata is known, mark 'dirty' and switch to 'active'.
2384 * if not known, block and switch to write-pending
2385 * If written to an active array that has pending writes, then fails.
2387 * fully active: IO and resync can be happening.
2388 * When written to inactive array, starts with resync
2391 * clean, but writes are blocked waiting for 'active' to be written.
2394 * like active, but no writes have been seen for a while (100msec).
2397 enum array_state
{ clear
, inactive
, suspended
, readonly
, read_auto
, clean
, active
,
2398 write_pending
, active_idle
, bad_word
};
2399 static char *array_states
[] = {
2400 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2401 "write-pending", "active-idle", NULL
};
2403 static int match_word(const char *word
, char **list
)
2406 for (n
=0; list
[n
]; n
++)
2407 if (cmd_match(word
, list
[n
]))
2413 array_state_show(mddev_t
*mddev
, char *page
)
2415 enum array_state st
= inactive
;
2428 else if (mddev
->safemode
)
2434 if (list_empty(&mddev
->disks
) &&
2435 mddev
->raid_disks
== 0 &&
2441 return sprintf(page
, "%s\n", array_states
[st
]);
2444 static int do_md_stop(mddev_t
* mddev
, int ro
);
2445 static int do_md_run(mddev_t
* mddev
);
2446 static int restart_array(mddev_t
*mddev
);
2449 array_state_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2452 enum array_state st
= match_word(buf
, array_states
);
2457 /* stopping an active array */
2459 if (atomic_read(&mddev
->active
) > 1)
2461 err
= do_md_stop(mddev
, 0);
2465 /* stopping an active array */
2467 if (atomic_read(&mddev
->active
) > 1)
2469 err
= do_md_stop(mddev
, 2);
2473 break; /* not supported yet */
2476 err
= do_md_stop(mddev
, 1);
2479 err
= do_md_run(mddev
);
2483 /* stopping an active array */
2485 err
= do_md_stop(mddev
, 1);
2487 mddev
->ro
= 2; /* FIXME mark devices writable */
2490 err
= do_md_run(mddev
);
2495 restart_array(mddev
);
2496 spin_lock_irq(&mddev
->write_lock
);
2497 if (atomic_read(&mddev
->writes_pending
) == 0) {
2499 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
2501 spin_unlock_irq(&mddev
->write_lock
);
2504 mddev
->recovery_cp
= MaxSector
;
2505 err
= do_md_run(mddev
);
2510 restart_array(mddev
);
2511 clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
2512 wake_up(&mddev
->sb_wait
);
2516 err
= do_md_run(mddev
);
2521 /* these cannot be set */
2529 static struct md_sysfs_entry md_array_state
=
2530 __ATTR(array_state
, S_IRUGO
|S_IWUSR
, array_state_show
, array_state_store
);
2533 null_show(mddev_t
*mddev
, char *page
)
2539 new_dev_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2541 /* buf must be %d:%d\n? giving major and minor numbers */
2542 /* The new device is added to the array.
2543 * If the array has a persistent superblock, we read the
2544 * superblock to initialise info and check validity.
2545 * Otherwise, only checking done is that in bind_rdev_to_array,
2546 * which mainly checks size.
2549 int major
= simple_strtoul(buf
, &e
, 10);
2555 if (!*buf
|| *e
!= ':' || !e
[1] || e
[1] == '\n')
2557 minor
= simple_strtoul(e
+1, &e
, 10);
2558 if (*e
&& *e
!= '\n')
2560 dev
= MKDEV(major
, minor
);
2561 if (major
!= MAJOR(dev
) ||
2562 minor
!= MINOR(dev
))
2566 if (mddev
->persistent
) {
2567 rdev
= md_import_device(dev
, mddev
->major_version
,
2568 mddev
->minor_version
);
2569 if (!IS_ERR(rdev
) && !list_empty(&mddev
->disks
)) {
2570 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
2571 mdk_rdev_t
, same_set
);
2572 err
= super_types
[mddev
->major_version
]
2573 .load_super(rdev
, rdev0
, mddev
->minor_version
);
2578 rdev
= md_import_device(dev
, -1, -1);
2581 return PTR_ERR(rdev
);
2582 err
= bind_rdev_to_array(rdev
, mddev
);
2586 return err
? err
: len
;
2589 static struct md_sysfs_entry md_new_device
=
2590 __ATTR(new_dev
, S_IWUSR
, null_show
, new_dev_store
);
2593 bitmap_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2596 unsigned long chunk
, end_chunk
;
2600 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
2602 chunk
= end_chunk
= simple_strtoul(buf
, &end
, 0);
2603 if (buf
== end
) break;
2604 if (*end
== '-') { /* range */
2606 end_chunk
= simple_strtoul(buf
, &end
, 0);
2607 if (buf
== end
) break;
2609 if (*end
&& !isspace(*end
)) break;
2610 bitmap_dirty_bits(mddev
->bitmap
, chunk
, end_chunk
);
2612 while (isspace(*buf
)) buf
++;
2614 bitmap_unplug(mddev
->bitmap
); /* flush the bits to disk */
2619 static struct md_sysfs_entry md_bitmap
=
2620 __ATTR(bitmap_set_bits
, S_IWUSR
, null_show
, bitmap_store
);
2623 size_show(mddev_t
*mddev
, char *page
)
2625 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->size
);
2628 static int update_size(mddev_t
*mddev
, unsigned long size
);
2631 size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2633 /* If array is inactive, we can reduce the component size, but
2634 * not increase it (except from 0).
2635 * If array is active, we can try an on-line resize
2639 unsigned long long size
= simple_strtoull(buf
, &e
, 10);
2640 if (!*buf
|| *buf
== '\n' ||
2645 err
= update_size(mddev
, size
);
2646 md_update_sb(mddev
, 1);
2648 if (mddev
->size
== 0 ||
2654 return err
? err
: len
;
2657 static struct md_sysfs_entry md_size
=
2658 __ATTR(component_size
, S_IRUGO
|S_IWUSR
, size_show
, size_store
);
2662 * This is either 'none' for arrays with externally managed metadata,
2663 * or N.M for internally known formats
2666 metadata_show(mddev_t
*mddev
, char *page
)
2668 if (mddev
->persistent
)
2669 return sprintf(page
, "%d.%d\n",
2670 mddev
->major_version
, mddev
->minor_version
);
2672 return sprintf(page
, "none\n");
2676 metadata_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2680 if (!list_empty(&mddev
->disks
))
2683 if (cmd_match(buf
, "none")) {
2684 mddev
->persistent
= 0;
2685 mddev
->major_version
= 0;
2686 mddev
->minor_version
= 90;
2689 major
= simple_strtoul(buf
, &e
, 10);
2690 if (e
==buf
|| *e
!= '.')
2693 minor
= simple_strtoul(buf
, &e
, 10);
2694 if (e
==buf
|| (*e
&& *e
!= '\n') )
2696 if (major
>= ARRAY_SIZE(super_types
) || super_types
[major
].name
== NULL
)
2698 mddev
->major_version
= major
;
2699 mddev
->minor_version
= minor
;
2700 mddev
->persistent
= 1;
2704 static struct md_sysfs_entry md_metadata
=
2705 __ATTR(metadata_version
, S_IRUGO
|S_IWUSR
, metadata_show
, metadata_store
);
2708 action_show(mddev_t
*mddev
, char *page
)
2710 char *type
= "idle";
2711 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
2712 (!mddev
->ro
&& test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))) {
2713 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
2715 else if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
2716 if (!test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
2718 else if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
2725 return sprintf(page
, "%s\n", type
);
2729 action_store(mddev_t
*mddev
, const char *page
, size_t len
)
2731 if (!mddev
->pers
|| !mddev
->pers
->sync_request
)
2734 if (cmd_match(page
, "idle")) {
2735 if (mddev
->sync_thread
) {
2736 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
2737 md_unregister_thread(mddev
->sync_thread
);
2738 mddev
->sync_thread
= NULL
;
2739 mddev
->recovery
= 0;
2741 } else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
2742 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
2744 else if (cmd_match(page
, "resync") || cmd_match(page
, "recover"))
2745 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
2746 else if (cmd_match(page
, "reshape")) {
2748 if (mddev
->pers
->start_reshape
== NULL
)
2750 err
= mddev
->pers
->start_reshape(mddev
);
2754 if (cmd_match(page
, "check"))
2755 set_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
2756 else if (!cmd_match(page
, "repair"))
2758 set_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
2759 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
2761 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
2762 md_wakeup_thread(mddev
->thread
);
2767 mismatch_cnt_show(mddev_t
*mddev
, char *page
)
2769 return sprintf(page
, "%llu\n",
2770 (unsigned long long) mddev
->resync_mismatches
);
2773 static struct md_sysfs_entry md_scan_mode
=
2774 __ATTR(sync_action
, S_IRUGO
|S_IWUSR
, action_show
, action_store
);
2777 static struct md_sysfs_entry md_mismatches
= __ATTR_RO(mismatch_cnt
);
2780 sync_min_show(mddev_t
*mddev
, char *page
)
2782 return sprintf(page
, "%d (%s)\n", speed_min(mddev
),
2783 mddev
->sync_speed_min
? "local": "system");
2787 sync_min_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2791 if (strncmp(buf
, "system", 6)==0) {
2792 mddev
->sync_speed_min
= 0;
2795 min
= simple_strtoul(buf
, &e
, 10);
2796 if (buf
== e
|| (*e
&& *e
!= '\n') || min
<= 0)
2798 mddev
->sync_speed_min
= min
;
2802 static struct md_sysfs_entry md_sync_min
=
2803 __ATTR(sync_speed_min
, S_IRUGO
|S_IWUSR
, sync_min_show
, sync_min_store
);
2806 sync_max_show(mddev_t
*mddev
, char *page
)
2808 return sprintf(page
, "%d (%s)\n", speed_max(mddev
),
2809 mddev
->sync_speed_max
? "local": "system");
2813 sync_max_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2817 if (strncmp(buf
, "system", 6)==0) {
2818 mddev
->sync_speed_max
= 0;
2821 max
= simple_strtoul(buf
, &e
, 10);
2822 if (buf
== e
|| (*e
&& *e
!= '\n') || max
<= 0)
2824 mddev
->sync_speed_max
= max
;
2828 static struct md_sysfs_entry md_sync_max
=
2829 __ATTR(sync_speed_max
, S_IRUGO
|S_IWUSR
, sync_max_show
, sync_max_store
);
2832 degraded_show(mddev_t
*mddev
, char *page
)
2834 return sprintf(page
, "%d\n", mddev
->degraded
);
2836 static struct md_sysfs_entry md_degraded
= __ATTR_RO(degraded
);
2839 sync_speed_show(mddev_t
*mddev
, char *page
)
2841 unsigned long resync
, dt
, db
;
2842 resync
= (mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
));
2843 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
2845 db
= resync
- (mddev
->resync_mark_cnt
);
2846 return sprintf(page
, "%ld\n", db
/dt
/2); /* K/sec */
2849 static struct md_sysfs_entry md_sync_speed
= __ATTR_RO(sync_speed
);
2852 sync_completed_show(mddev_t
*mddev
, char *page
)
2854 unsigned long max_blocks
, resync
;
2856 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
2857 max_blocks
= mddev
->resync_max_sectors
;
2859 max_blocks
= mddev
->size
<< 1;
2861 resync
= (mddev
->curr_resync
- atomic_read(&mddev
->recovery_active
));
2862 return sprintf(page
, "%lu / %lu\n", resync
, max_blocks
);
2865 static struct md_sysfs_entry md_sync_completed
= __ATTR_RO(sync_completed
);
2868 suspend_lo_show(mddev_t
*mddev
, char *page
)
2870 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_lo
);
2874 suspend_lo_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2877 unsigned long long new = simple_strtoull(buf
, &e
, 10);
2879 if (mddev
->pers
->quiesce
== NULL
)
2881 if (buf
== e
|| (*e
&& *e
!= '\n'))
2883 if (new >= mddev
->suspend_hi
||
2884 (new > mddev
->suspend_lo
&& new < mddev
->suspend_hi
)) {
2885 mddev
->suspend_lo
= new;
2886 mddev
->pers
->quiesce(mddev
, 2);
2891 static struct md_sysfs_entry md_suspend_lo
=
2892 __ATTR(suspend_lo
, S_IRUGO
|S_IWUSR
, suspend_lo_show
, suspend_lo_store
);
2896 suspend_hi_show(mddev_t
*mddev
, char *page
)
2898 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_hi
);
2902 suspend_hi_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2905 unsigned long long new = simple_strtoull(buf
, &e
, 10);
2907 if (mddev
->pers
->quiesce
== NULL
)
2909 if (buf
== e
|| (*e
&& *e
!= '\n'))
2911 if ((new <= mddev
->suspend_lo
&& mddev
->suspend_lo
>= mddev
->suspend_hi
) ||
2912 (new > mddev
->suspend_lo
&& new > mddev
->suspend_hi
)) {
2913 mddev
->suspend_hi
= new;
2914 mddev
->pers
->quiesce(mddev
, 1);
2915 mddev
->pers
->quiesce(mddev
, 0);
2920 static struct md_sysfs_entry md_suspend_hi
=
2921 __ATTR(suspend_hi
, S_IRUGO
|S_IWUSR
, suspend_hi_show
, suspend_hi_store
);
2924 reshape_position_show(mddev_t
*mddev
, char *page
)
2926 if (mddev
->reshape_position
!= MaxSector
)
2927 return sprintf(page
, "%llu\n",
2928 (unsigned long long)mddev
->reshape_position
);
2929 strcpy(page
, "none\n");
2934 reshape_position_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2937 unsigned long long new = simple_strtoull(buf
, &e
, 10);
2940 if (buf
== e
|| (*e
&& *e
!= '\n'))
2942 mddev
->reshape_position
= new;
2943 mddev
->delta_disks
= 0;
2944 mddev
->new_level
= mddev
->level
;
2945 mddev
->new_layout
= mddev
->layout
;
2946 mddev
->new_chunk
= mddev
->chunk_size
;
2950 static struct md_sysfs_entry md_reshape_position
=
2951 __ATTR(reshape_position
, S_IRUGO
|S_IWUSR
, reshape_position_show
,
2952 reshape_position_store
);
2955 static struct attribute
*md_default_attrs
[] = {
2958 &md_raid_disks
.attr
,
2959 &md_chunk_size
.attr
,
2961 &md_resync_start
.attr
,
2963 &md_new_device
.attr
,
2964 &md_safe_delay
.attr
,
2965 &md_array_state
.attr
,
2966 &md_reshape_position
.attr
,
2970 static struct attribute
*md_redundancy_attrs
[] = {
2972 &md_mismatches
.attr
,
2975 &md_sync_speed
.attr
,
2976 &md_sync_completed
.attr
,
2977 &md_suspend_lo
.attr
,
2978 &md_suspend_hi
.attr
,
2983 static struct attribute_group md_redundancy_group
= {
2985 .attrs
= md_redundancy_attrs
,
2990 md_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
2992 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
2993 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
2998 rv
= mddev_lock(mddev
);
3000 rv
= entry
->show(mddev
, page
);
3001 mddev_unlock(mddev
);
3007 md_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
3008 const char *page
, size_t length
)
3010 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
3011 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
3016 if (!capable(CAP_SYS_ADMIN
))
3018 rv
= mddev_lock(mddev
);
3020 rv
= entry
->store(mddev
, page
, length
);
3021 mddev_unlock(mddev
);
3026 static void md_free(struct kobject
*ko
)
3028 mddev_t
*mddev
= container_of(ko
, mddev_t
, kobj
);
3032 static struct sysfs_ops md_sysfs_ops
= {
3033 .show
= md_attr_show
,
3034 .store
= md_attr_store
,
3036 static struct kobj_type md_ktype
= {
3038 .sysfs_ops
= &md_sysfs_ops
,
3039 .default_attrs
= md_default_attrs
,
3044 static struct kobject
*md_probe(dev_t dev
, int *part
, void *data
)
3046 static DEFINE_MUTEX(disks_mutex
);
3047 mddev_t
*mddev
= mddev_find(dev
);
3048 struct gendisk
*disk
;
3049 int partitioned
= (MAJOR(dev
) != MD_MAJOR
);
3050 int shift
= partitioned
? MdpMinorShift
: 0;
3051 int unit
= MINOR(dev
) >> shift
;
3057 mutex_lock(&disks_mutex
);
3058 if (mddev
->gendisk
) {
3059 mutex_unlock(&disks_mutex
);
3063 disk
= alloc_disk(1 << shift
);
3065 mutex_unlock(&disks_mutex
);
3069 disk
->major
= MAJOR(dev
);
3070 disk
->first_minor
= unit
<< shift
;
3072 sprintf(disk
->disk_name
, "md_d%d", unit
);
3074 sprintf(disk
->disk_name
, "md%d", unit
);
3075 disk
->fops
= &md_fops
;
3076 disk
->private_data
= mddev
;
3077 disk
->queue
= mddev
->queue
;
3079 mddev
->gendisk
= disk
;
3080 mutex_unlock(&disks_mutex
);
3081 error
= kobject_init_and_add(&mddev
->kobj
, &md_ktype
, &disk
->dev
.kobj
,
3084 printk(KERN_WARNING
"md: cannot register %s/md - name in use\n",
3087 kobject_uevent(&mddev
->kobj
, KOBJ_ADD
);
3091 static void md_safemode_timeout(unsigned long data
)
3093 mddev_t
*mddev
= (mddev_t
*) data
;
3095 mddev
->safemode
= 1;
3096 md_wakeup_thread(mddev
->thread
);
3099 static int start_dirty_degraded
;
3101 static int do_md_run(mddev_t
* mddev
)
3105 struct list_head
*tmp
;
3107 struct gendisk
*disk
;
3108 struct mdk_personality
*pers
;
3109 char b
[BDEVNAME_SIZE
];
3111 if (list_empty(&mddev
->disks
))
3112 /* cannot run an array with no devices.. */
3119 * Analyze all RAID superblock(s)
3121 if (!mddev
->raid_disks
)
3124 chunk_size
= mddev
->chunk_size
;
3127 if (chunk_size
> MAX_CHUNK_SIZE
) {
3128 printk(KERN_ERR
"too big chunk_size: %d > %d\n",
3129 chunk_size
, MAX_CHUNK_SIZE
);
3133 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
3135 if ( (1 << ffz(~chunk_size
)) != chunk_size
) {
3136 printk(KERN_ERR
"chunk_size of %d not valid\n", chunk_size
);
3139 if (chunk_size
< PAGE_SIZE
) {
3140 printk(KERN_ERR
"too small chunk_size: %d < %ld\n",
3141 chunk_size
, PAGE_SIZE
);
3145 /* devices must have minimum size of one chunk */
3146 ITERATE_RDEV(mddev
,rdev
,tmp
) {
3147 if (test_bit(Faulty
, &rdev
->flags
))
3149 if (rdev
->size
< chunk_size
/ 1024) {
3151 "md: Dev %s smaller than chunk_size:"
3153 bdevname(rdev
->bdev
,b
),
3154 (unsigned long long)rdev
->size
,
3162 if (mddev
->level
!= LEVEL_NONE
)
3163 request_module("md-level-%d", mddev
->level
);
3164 else if (mddev
->clevel
[0])
3165 request_module("md-%s", mddev
->clevel
);
3169 * Drop all container device buffers, from now on
3170 * the only valid external interface is through the md
3173 ITERATE_RDEV(mddev
,rdev
,tmp
) {
3174 if (test_bit(Faulty
, &rdev
->flags
))
3176 sync_blockdev(rdev
->bdev
);
3177 invalidate_bdev(rdev
->bdev
);
3179 /* perform some consistency tests on the device.
3180 * We don't want the data to overlap the metadata,
3181 * Internal Bitmap issues has handled elsewhere.
3183 if (rdev
->data_offset
< rdev
->sb_offset
) {
3185 rdev
->data_offset
+ mddev
->size
*2
3186 > rdev
->sb_offset
*2) {
3187 printk("md: %s: data overlaps metadata\n",
3192 if (rdev
->sb_offset
*2 + rdev
->sb_size
/512
3193 > rdev
->data_offset
) {
3194 printk("md: %s: metadata overlaps data\n",
3201 md_probe(mddev
->unit
, NULL
, NULL
);
3202 disk
= mddev
->gendisk
;
3206 spin_lock(&pers_lock
);
3207 pers
= find_pers(mddev
->level
, mddev
->clevel
);
3208 if (!pers
|| !try_module_get(pers
->owner
)) {
3209 spin_unlock(&pers_lock
);
3210 if (mddev
->level
!= LEVEL_NONE
)
3211 printk(KERN_WARNING
"md: personality for level %d is not loaded!\n",
3214 printk(KERN_WARNING
"md: personality for level %s is not loaded!\n",
3219 spin_unlock(&pers_lock
);
3220 mddev
->level
= pers
->level
;
3221 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
3223 if (mddev
->reshape_position
!= MaxSector
&&
3224 pers
->start_reshape
== NULL
) {
3225 /* This personality cannot handle reshaping... */
3227 module_put(pers
->owner
);
3231 if (pers
->sync_request
) {
3232 /* Warn if this is a potentially silly
3235 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
3237 struct list_head
*tmp2
;
3239 ITERATE_RDEV(mddev
, rdev
, tmp
) {
3240 ITERATE_RDEV(mddev
, rdev2
, tmp2
) {
3242 rdev
->bdev
->bd_contains
==
3243 rdev2
->bdev
->bd_contains
) {
3245 "%s: WARNING: %s appears to be"
3246 " on the same physical disk as"
3249 bdevname(rdev
->bdev
,b
),
3250 bdevname(rdev2
->bdev
,b2
));
3257 "True protection against single-disk"
3258 " failure might be compromised.\n");
3261 mddev
->recovery
= 0;
3262 mddev
->resync_max_sectors
= mddev
->size
<< 1; /* may be over-ridden by personality */
3263 mddev
->barriers_work
= 1;
3264 mddev
->ok_start_degraded
= start_dirty_degraded
;
3267 mddev
->ro
= 2; /* read-only, but switch on first write */
3269 err
= mddev
->pers
->run(mddev
);
3270 if (!err
&& mddev
->pers
->sync_request
) {
3271 err
= bitmap_create(mddev
);
3273 printk(KERN_ERR
"%s: failed to create bitmap (%d)\n",
3274 mdname(mddev
), err
);
3275 mddev
->pers
->stop(mddev
);
3279 printk(KERN_ERR
"md: pers->run() failed ...\n");
3280 module_put(mddev
->pers
->owner
);
3282 bitmap_destroy(mddev
);
3285 if (mddev
->pers
->sync_request
) {
3286 if (sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
3288 "md: cannot register extra attributes for %s\n",
3290 } else if (mddev
->ro
== 2) /* auto-readonly not meaningful */
3293 atomic_set(&mddev
->writes_pending
,0);
3294 mddev
->safemode
= 0;
3295 mddev
->safemode_timer
.function
= md_safemode_timeout
;
3296 mddev
->safemode_timer
.data
= (unsigned long) mddev
;
3297 mddev
->safemode_delay
= (200 * HZ
)/1000 +1; /* 200 msec delay */
3300 ITERATE_RDEV(mddev
,rdev
,tmp
)
3301 if (rdev
->raid_disk
>= 0) {
3303 sprintf(nm
, "rd%d", rdev
->raid_disk
);
3304 if (sysfs_create_link(&mddev
->kobj
, &rdev
->kobj
, nm
))
3305 printk("md: cannot register %s for %s\n",
3309 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3312 md_update_sb(mddev
, 0);
3314 set_capacity(disk
, mddev
->array_size
<<1);
3316 /* If we call blk_queue_make_request here, it will
3317 * re-initialise max_sectors etc which may have been
3318 * refined inside -> run. So just set the bits we need to set.
3319 * Most initialisation happended when we called
3320 * blk_queue_make_request(..., md_fail_request)
3323 mddev
->queue
->queuedata
= mddev
;
3324 mddev
->queue
->make_request_fn
= mddev
->pers
->make_request
;
3326 /* If there is a partially-recovered drive we need to
3327 * start recovery here. If we leave it to md_check_recovery,
3328 * it will remove the drives and not do the right thing
3330 if (mddev
->degraded
&& !mddev
->sync_thread
) {
3331 struct list_head
*rtmp
;
3333 ITERATE_RDEV(mddev
,rdev
,rtmp
)
3334 if (rdev
->raid_disk
>= 0 &&
3335 !test_bit(In_sync
, &rdev
->flags
) &&
3336 !test_bit(Faulty
, &rdev
->flags
))
3337 /* complete an interrupted recovery */
3339 if (spares
&& mddev
->pers
->sync_request
) {
3340 mddev
->recovery
= 0;
3341 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
3342 mddev
->sync_thread
= md_register_thread(md_do_sync
,
3345 if (!mddev
->sync_thread
) {
3346 printk(KERN_ERR
"%s: could not start resync"
3349 /* leave the spares where they are, it shouldn't hurt */
3350 mddev
->recovery
= 0;
3354 md_wakeup_thread(mddev
->thread
);
3355 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
3358 md_new_event(mddev
);
3359 kobject_uevent(&mddev
->gendisk
->dev
.kobj
, KOBJ_CHANGE
);
3363 static int restart_array(mddev_t
*mddev
)
3365 struct gendisk
*disk
= mddev
->gendisk
;
3369 * Complain if it has no devices
3372 if (list_empty(&mddev
->disks
))
3380 mddev
->safemode
= 0;
3382 set_disk_ro(disk
, 0);
3384 printk(KERN_INFO
"md: %s switched to read-write mode.\n",
3387 * Kick recovery or resync if necessary
3389 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3390 md_wakeup_thread(mddev
->thread
);
3391 md_wakeup_thread(mddev
->sync_thread
);
3400 /* similar to deny_write_access, but accounts for our holding a reference
3401 * to the file ourselves */
3402 static int deny_bitmap_write_access(struct file
* file
)
3404 struct inode
*inode
= file
->f_mapping
->host
;
3406 spin_lock(&inode
->i_lock
);
3407 if (atomic_read(&inode
->i_writecount
) > 1) {
3408 spin_unlock(&inode
->i_lock
);
3411 atomic_set(&inode
->i_writecount
, -1);
3412 spin_unlock(&inode
->i_lock
);
3417 static void restore_bitmap_write_access(struct file
*file
)
3419 struct inode
*inode
= file
->f_mapping
->host
;
3421 spin_lock(&inode
->i_lock
);
3422 atomic_set(&inode
->i_writecount
, 1);
3423 spin_unlock(&inode
->i_lock
);
3427 * 0 - completely stop and dis-assemble array
3428 * 1 - switch to readonly
3429 * 2 - stop but do not disassemble array
3431 static int do_md_stop(mddev_t
* mddev
, int mode
)
3434 struct gendisk
*disk
= mddev
->gendisk
;
3437 if (atomic_read(&mddev
->active
)>2) {
3438 printk("md: %s still in use.\n",mdname(mddev
));
3442 if (mddev
->sync_thread
) {
3443 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
3444 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
3445 md_unregister_thread(mddev
->sync_thread
);
3446 mddev
->sync_thread
= NULL
;
3449 del_timer_sync(&mddev
->safemode_timer
);
3451 invalidate_partition(disk
, 0);
3454 case 1: /* readonly */
3460 case 0: /* disassemble */
3462 bitmap_flush(mddev
);
3463 md_super_wait(mddev
);
3465 set_disk_ro(disk
, 0);
3466 blk_queue_make_request(mddev
->queue
, md_fail_request
);
3467 mddev
->pers
->stop(mddev
);
3468 mddev
->queue
->merge_bvec_fn
= NULL
;
3469 mddev
->queue
->unplug_fn
= NULL
;
3470 mddev
->queue
->backing_dev_info
.congested_fn
= NULL
;
3471 if (mddev
->pers
->sync_request
)
3472 sysfs_remove_group(&mddev
->kobj
, &md_redundancy_group
);
3474 module_put(mddev
->pers
->owner
);
3477 set_capacity(disk
, 0);
3483 if (!mddev
->in_sync
|| mddev
->flags
) {
3484 /* mark array as shutdown cleanly */
3486 md_update_sb(mddev
, 1);
3489 set_disk_ro(disk
, 1);
3490 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
3494 * Free resources if final stop
3498 struct list_head
*tmp
;
3500 printk(KERN_INFO
"md: %s stopped.\n", mdname(mddev
));
3502 bitmap_destroy(mddev
);
3503 if (mddev
->bitmap_file
) {
3504 restore_bitmap_write_access(mddev
->bitmap_file
);
3505 fput(mddev
->bitmap_file
);
3506 mddev
->bitmap_file
= NULL
;
3508 mddev
->bitmap_offset
= 0;
3510 ITERATE_RDEV(mddev
,rdev
,tmp
)
3511 if (rdev
->raid_disk
>= 0) {
3513 sprintf(nm
, "rd%d", rdev
->raid_disk
);
3514 sysfs_remove_link(&mddev
->kobj
, nm
);
3517 /* make sure all delayed_delete calls have finished */
3518 flush_scheduled_work();
3520 export_array(mddev
);
3522 mddev
->array_size
= 0;
3524 mddev
->raid_disks
= 0;
3525 mddev
->recovery_cp
= 0;
3526 mddev
->reshape_position
= MaxSector
;
3528 } else if (mddev
->pers
)
3529 printk(KERN_INFO
"md: %s switched to read-only mode.\n",
3532 md_new_event(mddev
);
3538 static void autorun_array(mddev_t
*mddev
)
3541 struct list_head
*tmp
;
3544 if (list_empty(&mddev
->disks
))
3547 printk(KERN_INFO
"md: running: ");
3549 ITERATE_RDEV(mddev
,rdev
,tmp
) {
3550 char b
[BDEVNAME_SIZE
];
3551 printk("<%s>", bdevname(rdev
->bdev
,b
));
3555 err
= do_md_run (mddev
);
3557 printk(KERN_WARNING
"md: do_md_run() returned %d\n", err
);
3558 do_md_stop (mddev
, 0);
3563 * lets try to run arrays based on all disks that have arrived
3564 * until now. (those are in pending_raid_disks)
3566 * the method: pick the first pending disk, collect all disks with
3567 * the same UUID, remove all from the pending list and put them into
3568 * the 'same_array' list. Then order this list based on superblock
3569 * update time (freshest comes first), kick out 'old' disks and
3570 * compare superblocks. If everything's fine then run it.
3572 * If "unit" is allocated, then bump its reference count
3574 static void autorun_devices(int part
)
3576 struct list_head
*tmp
;
3577 mdk_rdev_t
*rdev0
, *rdev
;
3579 char b
[BDEVNAME_SIZE
];
3581 printk(KERN_INFO
"md: autorun ...\n");
3582 while (!list_empty(&pending_raid_disks
)) {
3585 LIST_HEAD(candidates
);
3586 rdev0
= list_entry(pending_raid_disks
.next
,
3587 mdk_rdev_t
, same_set
);
3589 printk(KERN_INFO
"md: considering %s ...\n",
3590 bdevname(rdev0
->bdev
,b
));
3591 INIT_LIST_HEAD(&candidates
);
3592 ITERATE_RDEV_PENDING(rdev
,tmp
)
3593 if (super_90_load(rdev
, rdev0
, 0) >= 0) {
3594 printk(KERN_INFO
"md: adding %s ...\n",
3595 bdevname(rdev
->bdev
,b
));
3596 list_move(&rdev
->same_set
, &candidates
);
3599 * now we have a set of devices, with all of them having
3600 * mostly sane superblocks. It's time to allocate the
3604 dev
= MKDEV(mdp_major
,
3605 rdev0
->preferred_minor
<< MdpMinorShift
);
3606 unit
= MINOR(dev
) >> MdpMinorShift
;
3608 dev
= MKDEV(MD_MAJOR
, rdev0
->preferred_minor
);
3611 if (rdev0
->preferred_minor
!= unit
) {
3612 printk(KERN_INFO
"md: unit number in %s is bad: %d\n",
3613 bdevname(rdev0
->bdev
, b
), rdev0
->preferred_minor
);
3617 md_probe(dev
, NULL
, NULL
);
3618 mddev
= mddev_find(dev
);
3621 "md: cannot allocate memory for md drive.\n");
3624 if (mddev_lock(mddev
))
3625 printk(KERN_WARNING
"md: %s locked, cannot run\n",
3627 else if (mddev
->raid_disks
|| mddev
->major_version
3628 || !list_empty(&mddev
->disks
)) {
3630 "md: %s already running, cannot run %s\n",
3631 mdname(mddev
), bdevname(rdev0
->bdev
,b
));
3632 mddev_unlock(mddev
);
3634 printk(KERN_INFO
"md: created %s\n", mdname(mddev
));
3635 ITERATE_RDEV_GENERIC(candidates
,rdev
,tmp
) {
3636 list_del_init(&rdev
->same_set
);
3637 if (bind_rdev_to_array(rdev
, mddev
))
3640 autorun_array(mddev
);
3641 mddev_unlock(mddev
);
3643 /* on success, candidates will be empty, on error
3646 ITERATE_RDEV_GENERIC(candidates
,rdev
,tmp
)
3650 printk(KERN_INFO
"md: ... autorun DONE.\n");
3652 #endif /* !MODULE */
3654 static int get_version(void __user
* arg
)
3658 ver
.major
= MD_MAJOR_VERSION
;
3659 ver
.minor
= MD_MINOR_VERSION
;
3660 ver
.patchlevel
= MD_PATCHLEVEL_VERSION
;
3662 if (copy_to_user(arg
, &ver
, sizeof(ver
)))
3668 static int get_array_info(mddev_t
* mddev
, void __user
* arg
)
3670 mdu_array_info_t info
;
3671 int nr
,working
,active
,failed
,spare
;
3673 struct list_head
*tmp
;
3675 nr
=working
=active
=failed
=spare
=0;
3676 ITERATE_RDEV(mddev
,rdev
,tmp
) {
3678 if (test_bit(Faulty
, &rdev
->flags
))
3682 if (test_bit(In_sync
, &rdev
->flags
))
3689 info
.major_version
= mddev
->major_version
;
3690 info
.minor_version
= mddev
->minor_version
;
3691 info
.patch_version
= MD_PATCHLEVEL_VERSION
;
3692 info
.ctime
= mddev
->ctime
;
3693 info
.level
= mddev
->level
;
3694 info
.size
= mddev
->size
;
3695 if (info
.size
!= mddev
->size
) /* overflow */
3698 info
.raid_disks
= mddev
->raid_disks
;
3699 info
.md_minor
= mddev
->md_minor
;
3700 info
.not_persistent
= !mddev
->persistent
;
3702 info
.utime
= mddev
->utime
;
3705 info
.state
= (1<<MD_SB_CLEAN
);
3706 if (mddev
->bitmap
&& mddev
->bitmap_offset
)
3707 info
.state
= (1<<MD_SB_BITMAP_PRESENT
);
3708 info
.active_disks
= active
;
3709 info
.working_disks
= working
;
3710 info
.failed_disks
= failed
;
3711 info
.spare_disks
= spare
;
3713 info
.layout
= mddev
->layout
;
3714 info
.chunk_size
= mddev
->chunk_size
;
3716 if (copy_to_user(arg
, &info
, sizeof(info
)))
3722 static int get_bitmap_file(mddev_t
* mddev
, void __user
* arg
)
3724 mdu_bitmap_file_t
*file
= NULL
; /* too big for stack allocation */
3725 char *ptr
, *buf
= NULL
;
3728 md_allow_write(mddev
);
3730 file
= kmalloc(sizeof(*file
), GFP_KERNEL
);
3734 /* bitmap disabled, zero the first byte and copy out */
3735 if (!mddev
->bitmap
|| !mddev
->bitmap
->file
) {
3736 file
->pathname
[0] = '\0';
3740 buf
= kmalloc(sizeof(file
->pathname
), GFP_KERNEL
);
3744 ptr
= file_path(mddev
->bitmap
->file
, buf
, sizeof(file
->pathname
));
3748 strcpy(file
->pathname
, ptr
);
3752 if (copy_to_user(arg
, file
, sizeof(*file
)))
3760 static int get_disk_info(mddev_t
* mddev
, void __user
* arg
)
3762 mdu_disk_info_t info
;
3766 if (copy_from_user(&info
, arg
, sizeof(info
)))
3771 rdev
= find_rdev_nr(mddev
, nr
);
3773 info
.major
= MAJOR(rdev
->bdev
->bd_dev
);
3774 info
.minor
= MINOR(rdev
->bdev
->bd_dev
);
3775 info
.raid_disk
= rdev
->raid_disk
;
3777 if (test_bit(Faulty
, &rdev
->flags
))
3778 info
.state
|= (1<<MD_DISK_FAULTY
);
3779 else if (test_bit(In_sync
, &rdev
->flags
)) {
3780 info
.state
|= (1<<MD_DISK_ACTIVE
);
3781 info
.state
|= (1<<MD_DISK_SYNC
);
3783 if (test_bit(WriteMostly
, &rdev
->flags
))
3784 info
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
3786 info
.major
= info
.minor
= 0;
3787 info
.raid_disk
= -1;
3788 info
.state
= (1<<MD_DISK_REMOVED
);
3791 if (copy_to_user(arg
, &info
, sizeof(info
)))
3797 static int add_new_disk(mddev_t
* mddev
, mdu_disk_info_t
*info
)
3799 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
3801 dev_t dev
= MKDEV(info
->major
,info
->minor
);
3803 if (info
->major
!= MAJOR(dev
) || info
->minor
!= MINOR(dev
))
3806 if (!mddev
->raid_disks
) {
3808 /* expecting a device which has a superblock */
3809 rdev
= md_import_device(dev
, mddev
->major_version
, mddev
->minor_version
);
3812 "md: md_import_device returned %ld\n",
3814 return PTR_ERR(rdev
);
3816 if (!list_empty(&mddev
->disks
)) {
3817 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
3818 mdk_rdev_t
, same_set
);
3819 int err
= super_types
[mddev
->major_version
]
3820 .load_super(rdev
, rdev0
, mddev
->minor_version
);
3823 "md: %s has different UUID to %s\n",
3824 bdevname(rdev
->bdev
,b
),
3825 bdevname(rdev0
->bdev
,b2
));
3830 err
= bind_rdev_to_array(rdev
, mddev
);
3837 * add_new_disk can be used once the array is assembled
3838 * to add "hot spares". They must already have a superblock
3843 if (!mddev
->pers
->hot_add_disk
) {
3845 "%s: personality does not support diskops!\n",
3849 if (mddev
->persistent
)
3850 rdev
= md_import_device(dev
, mddev
->major_version
,
3851 mddev
->minor_version
);
3853 rdev
= md_import_device(dev
, -1, -1);
3856 "md: md_import_device returned %ld\n",
3858 return PTR_ERR(rdev
);
3860 /* set save_raid_disk if appropriate */
3861 if (!mddev
->persistent
) {
3862 if (info
->state
& (1<<MD_DISK_SYNC
) &&
3863 info
->raid_disk
< mddev
->raid_disks
)
3864 rdev
->raid_disk
= info
->raid_disk
;
3866 rdev
->raid_disk
= -1;
3868 super_types
[mddev
->major_version
].
3869 validate_super(mddev
, rdev
);
3870 rdev
->saved_raid_disk
= rdev
->raid_disk
;
3872 clear_bit(In_sync
, &rdev
->flags
); /* just to be sure */
3873 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
3874 set_bit(WriteMostly
, &rdev
->flags
);
3876 rdev
->raid_disk
= -1;
3877 err
= bind_rdev_to_array(rdev
, mddev
);
3878 if (!err
&& !mddev
->pers
->hot_remove_disk
) {
3879 /* If there is hot_add_disk but no hot_remove_disk
3880 * then added disks for geometry changes,
3881 * and should be added immediately.
3883 super_types
[mddev
->major_version
].
3884 validate_super(mddev
, rdev
);
3885 err
= mddev
->pers
->hot_add_disk(mddev
, rdev
);
3887 unbind_rdev_from_array(rdev
);
3892 md_update_sb(mddev
, 1);
3893 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3894 md_wakeup_thread(mddev
->thread
);
3898 /* otherwise, add_new_disk is only allowed
3899 * for major_version==0 superblocks
3901 if (mddev
->major_version
!= 0) {
3902 printk(KERN_WARNING
"%s: ADD_NEW_DISK not supported\n",
3907 if (!(info
->state
& (1<<MD_DISK_FAULTY
))) {
3909 rdev
= md_import_device (dev
, -1, 0);
3912 "md: error, md_import_device() returned %ld\n",
3914 return PTR_ERR(rdev
);
3916 rdev
->desc_nr
= info
->number
;
3917 if (info
->raid_disk
< mddev
->raid_disks
)
3918 rdev
->raid_disk
= info
->raid_disk
;
3920 rdev
->raid_disk
= -1;
3924 if (rdev
->raid_disk
< mddev
->raid_disks
)
3925 if (info
->state
& (1<<MD_DISK_SYNC
))
3926 set_bit(In_sync
, &rdev
->flags
);
3928 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
3929 set_bit(WriteMostly
, &rdev
->flags
);
3931 if (!mddev
->persistent
) {
3932 printk(KERN_INFO
"md: nonpersistent superblock ...\n");
3933 rdev
->sb_offset
= rdev
->bdev
->bd_inode
->i_size
>> BLOCK_SIZE_BITS
;
3935 rdev
->sb_offset
= calc_dev_sboffset(rdev
->bdev
);
3936 rdev
->size
= calc_dev_size(rdev
, mddev
->chunk_size
);
3938 err
= bind_rdev_to_array(rdev
, mddev
);
3948 static int hot_remove_disk(mddev_t
* mddev
, dev_t dev
)
3950 char b
[BDEVNAME_SIZE
];
3956 rdev
= find_rdev(mddev
, dev
);
3960 if (rdev
->raid_disk
>= 0)
3963 kick_rdev_from_array(rdev
);
3964 md_update_sb(mddev
, 1);
3965 md_new_event(mddev
);
3969 printk(KERN_WARNING
"md: cannot remove active disk %s from %s ... \n",
3970 bdevname(rdev
->bdev
,b
), mdname(mddev
));
3974 static int hot_add_disk(mddev_t
* mddev
, dev_t dev
)
3976 char b
[BDEVNAME_SIZE
];
3984 if (mddev
->major_version
!= 0) {
3985 printk(KERN_WARNING
"%s: HOT_ADD may only be used with"
3986 " version-0 superblocks.\n",
3990 if (!mddev
->pers
->hot_add_disk
) {
3992 "%s: personality does not support diskops!\n",
3997 rdev
= md_import_device (dev
, -1, 0);
4000 "md: error, md_import_device() returned %ld\n",
4005 if (mddev
->persistent
)
4006 rdev
->sb_offset
= calc_dev_sboffset(rdev
->bdev
);
4009 rdev
->bdev
->bd_inode
->i_size
>> BLOCK_SIZE_BITS
;
4011 size
= calc_dev_size(rdev
, mddev
->chunk_size
);
4014 if (test_bit(Faulty
, &rdev
->flags
)) {
4016 "md: can not hot-add faulty %s disk to %s!\n",
4017 bdevname(rdev
->bdev
,b
), mdname(mddev
));
4021 clear_bit(In_sync
, &rdev
->flags
);
4023 rdev
->saved_raid_disk
= -1;
4024 err
= bind_rdev_to_array(rdev
, mddev
);
4029 * The rest should better be atomic, we can have disk failures
4030 * noticed in interrupt contexts ...
4033 if (rdev
->desc_nr
== mddev
->max_disks
) {
4034 printk(KERN_WARNING
"%s: can not hot-add to full array!\n",
4037 goto abort_unbind_export
;
4040 rdev
->raid_disk
= -1;
4042 md_update_sb(mddev
, 1);
4045 * Kick recovery, maybe this spare has to be added to the
4046 * array immediately.
4048 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4049 md_wakeup_thread(mddev
->thread
);
4050 md_new_event(mddev
);
4053 abort_unbind_export
:
4054 unbind_rdev_from_array(rdev
);
4061 static int set_bitmap_file(mddev_t
*mddev
, int fd
)
4066 if (!mddev
->pers
->quiesce
)
4068 if (mddev
->recovery
|| mddev
->sync_thread
)
4070 /* we should be able to change the bitmap.. */
4076 return -EEXIST
; /* cannot add when bitmap is present */
4077 mddev
->bitmap_file
= fget(fd
);
4079 if (mddev
->bitmap_file
== NULL
) {
4080 printk(KERN_ERR
"%s: error: failed to get bitmap file\n",
4085 err
= deny_bitmap_write_access(mddev
->bitmap_file
);
4087 printk(KERN_ERR
"%s: error: bitmap file is already in use\n",
4089 fput(mddev
->bitmap_file
);
4090 mddev
->bitmap_file
= NULL
;
4093 mddev
->bitmap_offset
= 0; /* file overrides offset */
4094 } else if (mddev
->bitmap
== NULL
)
4095 return -ENOENT
; /* cannot remove what isn't there */
4098 mddev
->pers
->quiesce(mddev
, 1);
4100 err
= bitmap_create(mddev
);
4101 if (fd
< 0 || err
) {
4102 bitmap_destroy(mddev
);
4103 fd
= -1; /* make sure to put the file */
4105 mddev
->pers
->quiesce(mddev
, 0);
4108 if (mddev
->bitmap_file
) {
4109 restore_bitmap_write_access(mddev
->bitmap_file
);
4110 fput(mddev
->bitmap_file
);
4112 mddev
->bitmap_file
= NULL
;
4119 * set_array_info is used two different ways
4120 * The original usage is when creating a new array.
4121 * In this usage, raid_disks is > 0 and it together with
4122 * level, size, not_persistent,layout,chunksize determine the
4123 * shape of the array.
4124 * This will always create an array with a type-0.90.0 superblock.
4125 * The newer usage is when assembling an array.
4126 * In this case raid_disks will be 0, and the major_version field is
4127 * use to determine which style super-blocks are to be found on the devices.
4128 * The minor and patch _version numbers are also kept incase the
4129 * super_block handler wishes to interpret them.
4131 static int set_array_info(mddev_t
* mddev
, mdu_array_info_t
*info
)
4134 if (info
->raid_disks
== 0) {
4135 /* just setting version number for superblock loading */
4136 if (info
->major_version
< 0 ||
4137 info
->major_version
>= ARRAY_SIZE(super_types
) ||
4138 super_types
[info
->major_version
].name
== NULL
) {
4139 /* maybe try to auto-load a module? */
4141 "md: superblock version %d not known\n",
4142 info
->major_version
);
4145 mddev
->major_version
= info
->major_version
;
4146 mddev
->minor_version
= info
->minor_version
;
4147 mddev
->patch_version
= info
->patch_version
;
4148 mddev
->persistent
= !info
->not_persistent
;
4151 mddev
->major_version
= MD_MAJOR_VERSION
;
4152 mddev
->minor_version
= MD_MINOR_VERSION
;
4153 mddev
->patch_version
= MD_PATCHLEVEL_VERSION
;
4154 mddev
->ctime
= get_seconds();
4156 mddev
->level
= info
->level
;
4157 mddev
->clevel
[0] = 0;
4158 mddev
->size
= info
->size
;
4159 mddev
->raid_disks
= info
->raid_disks
;
4160 /* don't set md_minor, it is determined by which /dev/md* was
4163 if (info
->state
& (1<<MD_SB_CLEAN
))
4164 mddev
->recovery_cp
= MaxSector
;
4166 mddev
->recovery_cp
= 0;
4167 mddev
->persistent
= ! info
->not_persistent
;
4169 mddev
->layout
= info
->layout
;
4170 mddev
->chunk_size
= info
->chunk_size
;
4172 mddev
->max_disks
= MD_SB_DISKS
;
4175 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
4177 mddev
->default_bitmap_offset
= MD_SB_BYTES
>> 9;
4178 mddev
->bitmap_offset
= 0;
4180 mddev
->reshape_position
= MaxSector
;
4183 * Generate a 128 bit UUID
4185 get_random_bytes(mddev
->uuid
, 16);
4187 mddev
->new_level
= mddev
->level
;
4188 mddev
->new_chunk
= mddev
->chunk_size
;
4189 mddev
->new_layout
= mddev
->layout
;
4190 mddev
->delta_disks
= 0;
4195 static int update_size(mddev_t
*mddev
, unsigned long size
)
4199 struct list_head
*tmp
;
4200 int fit
= (size
== 0);
4202 if (mddev
->pers
->resize
== NULL
)
4204 /* The "size" is the amount of each device that is used.
4205 * This can only make sense for arrays with redundancy.
4206 * linear and raid0 always use whatever space is available
4207 * We can only consider changing the size if no resync
4208 * or reconstruction is happening, and if the new size
4209 * is acceptable. It must fit before the sb_offset or,
4210 * if that is <data_offset, it must fit before the
4211 * size of each device.
4212 * If size is zero, we find the largest size that fits.
4214 if (mddev
->sync_thread
)
4216 ITERATE_RDEV(mddev
,rdev
,tmp
) {
4218 avail
= rdev
->size
* 2;
4220 if (fit
&& (size
== 0 || size
> avail
/2))
4222 if (avail
< ((sector_t
)size
<< 1))
4225 rv
= mddev
->pers
->resize(mddev
, (sector_t
)size
*2);
4227 struct block_device
*bdev
;
4229 bdev
= bdget_disk(mddev
->gendisk
, 0);
4231 mutex_lock(&bdev
->bd_inode
->i_mutex
);
4232 i_size_write(bdev
->bd_inode
, (loff_t
)mddev
->array_size
<< 10);
4233 mutex_unlock(&bdev
->bd_inode
->i_mutex
);
4240 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
)
4243 /* change the number of raid disks */
4244 if (mddev
->pers
->check_reshape
== NULL
)
4246 if (raid_disks
<= 0 ||
4247 raid_disks
>= mddev
->max_disks
)
4249 if (mddev
->sync_thread
|| mddev
->reshape_position
!= MaxSector
)
4251 mddev
->delta_disks
= raid_disks
- mddev
->raid_disks
;
4253 rv
= mddev
->pers
->check_reshape(mddev
);
4259 * update_array_info is used to change the configuration of an
4261 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
4262 * fields in the info are checked against the array.
4263 * Any differences that cannot be handled will cause an error.
4264 * Normally, only one change can be managed at a time.
4266 static int update_array_info(mddev_t
*mddev
, mdu_array_info_t
*info
)
4272 /* calculate expected state,ignoring low bits */
4273 if (mddev
->bitmap
&& mddev
->bitmap_offset
)
4274 state
|= (1 << MD_SB_BITMAP_PRESENT
);
4276 if (mddev
->major_version
!= info
->major_version
||
4277 mddev
->minor_version
!= info
->minor_version
||
4278 /* mddev->patch_version != info->patch_version || */
4279 mddev
->ctime
!= info
->ctime
||
4280 mddev
->level
!= info
->level
||
4281 /* mddev->layout != info->layout || */
4282 !mddev
->persistent
!= info
->not_persistent
||
4283 mddev
->chunk_size
!= info
->chunk_size
||
4284 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
4285 ((state
^info
->state
) & 0xfffffe00)
4288 /* Check there is only one change */
4289 if (info
->size
>= 0 && mddev
->size
!= info
->size
) cnt
++;
4290 if (mddev
->raid_disks
!= info
->raid_disks
) cnt
++;
4291 if (mddev
->layout
!= info
->layout
) cnt
++;
4292 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) cnt
++;
4293 if (cnt
== 0) return 0;
4294 if (cnt
> 1) return -EINVAL
;
4296 if (mddev
->layout
!= info
->layout
) {
4298 * we don't need to do anything at the md level, the
4299 * personality will take care of it all.
4301 if (mddev
->pers
->reconfig
== NULL
)
4304 return mddev
->pers
->reconfig(mddev
, info
->layout
, -1);
4306 if (info
->size
>= 0 && mddev
->size
!= info
->size
)
4307 rv
= update_size(mddev
, info
->size
);
4309 if (mddev
->raid_disks
!= info
->raid_disks
)
4310 rv
= update_raid_disks(mddev
, info
->raid_disks
);
4312 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) {
4313 if (mddev
->pers
->quiesce
== NULL
)
4315 if (mddev
->recovery
|| mddev
->sync_thread
)
4317 if (info
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
4318 /* add the bitmap */
4321 if (mddev
->default_bitmap_offset
== 0)
4323 mddev
->bitmap_offset
= mddev
->default_bitmap_offset
;
4324 mddev
->pers
->quiesce(mddev
, 1);
4325 rv
= bitmap_create(mddev
);
4327 bitmap_destroy(mddev
);
4328 mddev
->pers
->quiesce(mddev
, 0);
4330 /* remove the bitmap */
4333 if (mddev
->bitmap
->file
)
4335 mddev
->pers
->quiesce(mddev
, 1);
4336 bitmap_destroy(mddev
);
4337 mddev
->pers
->quiesce(mddev
, 0);
4338 mddev
->bitmap_offset
= 0;
4341 md_update_sb(mddev
, 1);
4345 static int set_disk_faulty(mddev_t
*mddev
, dev_t dev
)
4349 if (mddev
->pers
== NULL
)
4352 rdev
= find_rdev(mddev
, dev
);
4356 md_error(mddev
, rdev
);
4360 static int md_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
4362 mddev_t
*mddev
= bdev
->bd_disk
->private_data
;
4366 geo
->cylinders
= get_capacity(mddev
->gendisk
) / 8;
4370 static int md_ioctl(struct inode
*inode
, struct file
*file
,
4371 unsigned int cmd
, unsigned long arg
)
4374 void __user
*argp
= (void __user
*)arg
;
4375 mddev_t
*mddev
= NULL
;
4377 if (!capable(CAP_SYS_ADMIN
))
4381 * Commands dealing with the RAID driver but not any
4387 err
= get_version(argp
);
4390 case PRINT_RAID_DEBUG
:
4398 autostart_arrays(arg
);
4405 * Commands creating/starting a new array:
4408 mddev
= inode
->i_bdev
->bd_disk
->private_data
;
4415 err
= mddev_lock(mddev
);
4418 "md: ioctl lock interrupted, reason %d, cmd %d\n",
4425 case SET_ARRAY_INFO
:
4427 mdu_array_info_t info
;
4429 memset(&info
, 0, sizeof(info
));
4430 else if (copy_from_user(&info
, argp
, sizeof(info
))) {
4435 err
= update_array_info(mddev
, &info
);
4437 printk(KERN_WARNING
"md: couldn't update"
4438 " array info. %d\n", err
);
4443 if (!list_empty(&mddev
->disks
)) {
4445 "md: array %s already has disks!\n",
4450 if (mddev
->raid_disks
) {
4452 "md: array %s already initialised!\n",
4457 err
= set_array_info(mddev
, &info
);
4459 printk(KERN_WARNING
"md: couldn't set"
4460 " array info. %d\n", err
);
4470 * Commands querying/configuring an existing array:
4472 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
4473 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
4474 if (!mddev
->raid_disks
&& cmd
!= ADD_NEW_DISK
&& cmd
!= STOP_ARRAY
4475 && cmd
!= RUN_ARRAY
&& cmd
!= SET_BITMAP_FILE
4476 && cmd
!= GET_BITMAP_FILE
) {
4482 * Commands even a read-only array can execute:
4486 case GET_ARRAY_INFO
:
4487 err
= get_array_info(mddev
, argp
);
4490 case GET_BITMAP_FILE
:
4491 err
= get_bitmap_file(mddev
, argp
);
4495 err
= get_disk_info(mddev
, argp
);
4498 case RESTART_ARRAY_RW
:
4499 err
= restart_array(mddev
);
4503 err
= do_md_stop (mddev
, 0);
4507 err
= do_md_stop (mddev
, 1);
4511 * We have a problem here : there is no easy way to give a CHS
4512 * virtual geometry. We currently pretend that we have a 2 heads
4513 * 4 sectors (with a BIG number of cylinders...). This drives
4514 * dosfs just mad... ;-)
4519 * The remaining ioctls are changing the state of the
4520 * superblock, so we do not allow them on read-only arrays.
4521 * However non-MD ioctls (e.g. get-size) will still come through
4522 * here and hit the 'default' below, so only disallow
4523 * 'md' ioctls, and switch to rw mode if started auto-readonly.
4525 if (_IOC_TYPE(cmd
) == MD_MAJOR
&&
4526 mddev
->ro
&& mddev
->pers
) {
4527 if (mddev
->ro
== 2) {
4529 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4530 md_wakeup_thread(mddev
->thread
);
4542 mdu_disk_info_t info
;
4543 if (copy_from_user(&info
, argp
, sizeof(info
)))
4546 err
= add_new_disk(mddev
, &info
);
4550 case HOT_REMOVE_DISK
:
4551 err
= hot_remove_disk(mddev
, new_decode_dev(arg
));
4555 err
= hot_add_disk(mddev
, new_decode_dev(arg
));
4558 case SET_DISK_FAULTY
:
4559 err
= set_disk_faulty(mddev
, new_decode_dev(arg
));
4563 err
= do_md_run (mddev
);
4566 case SET_BITMAP_FILE
:
4567 err
= set_bitmap_file(mddev
, (int)arg
);
4577 mddev_unlock(mddev
);
4587 static int md_open(struct inode
*inode
, struct file
*file
)
4590 * Succeed if we can lock the mddev, which confirms that
4591 * it isn't being stopped right now.
4593 mddev_t
*mddev
= inode
->i_bdev
->bd_disk
->private_data
;
4596 if ((err
= mutex_lock_interruptible_nested(&mddev
->reconfig_mutex
, 1)))
4601 mddev_unlock(mddev
);
4603 check_disk_change(inode
->i_bdev
);
4608 static int md_release(struct inode
*inode
, struct file
* file
)
4610 mddev_t
*mddev
= inode
->i_bdev
->bd_disk
->private_data
;
4618 static int md_media_changed(struct gendisk
*disk
)
4620 mddev_t
*mddev
= disk
->private_data
;
4622 return mddev
->changed
;
4625 static int md_revalidate(struct gendisk
*disk
)
4627 mddev_t
*mddev
= disk
->private_data
;
4632 static struct block_device_operations md_fops
=
4634 .owner
= THIS_MODULE
,
4636 .release
= md_release
,
4638 .getgeo
= md_getgeo
,
4639 .media_changed
= md_media_changed
,
4640 .revalidate_disk
= md_revalidate
,
4643 static int md_thread(void * arg
)
4645 mdk_thread_t
*thread
= arg
;
4648 * md_thread is a 'system-thread', it's priority should be very
4649 * high. We avoid resource deadlocks individually in each
4650 * raid personality. (RAID5 does preallocation) We also use RR and
4651 * the very same RT priority as kswapd, thus we will never get
4652 * into a priority inversion deadlock.
4654 * we definitely have to have equal or higher priority than
4655 * bdflush, otherwise bdflush will deadlock if there are too
4656 * many dirty RAID5 blocks.
4659 allow_signal(SIGKILL
);
4660 while (!kthread_should_stop()) {
4662 /* We need to wait INTERRUPTIBLE so that
4663 * we don't add to the load-average.
4664 * That means we need to be sure no signals are
4667 if (signal_pending(current
))
4668 flush_signals(current
);
4670 wait_event_interruptible_timeout
4672 test_bit(THREAD_WAKEUP
, &thread
->flags
)
4673 || kthread_should_stop(),
4676 clear_bit(THREAD_WAKEUP
, &thread
->flags
);
4678 thread
->run(thread
->mddev
);
4684 void md_wakeup_thread(mdk_thread_t
*thread
)
4687 dprintk("md: waking up MD thread %s.\n", thread
->tsk
->comm
);
4688 set_bit(THREAD_WAKEUP
, &thread
->flags
);
4689 wake_up(&thread
->wqueue
);
4693 mdk_thread_t
*md_register_thread(void (*run
) (mddev_t
*), mddev_t
*mddev
,
4696 mdk_thread_t
*thread
;
4698 thread
= kzalloc(sizeof(mdk_thread_t
), GFP_KERNEL
);
4702 init_waitqueue_head(&thread
->wqueue
);
4705 thread
->mddev
= mddev
;
4706 thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
4707 thread
->tsk
= kthread_run(md_thread
, thread
, name
, mdname(thread
->mddev
));
4708 if (IS_ERR(thread
->tsk
)) {
4715 void md_unregister_thread(mdk_thread_t
*thread
)
4717 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread
->tsk
));
4719 kthread_stop(thread
->tsk
);
4723 void md_error(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
4730 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
4733 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
4735 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
4736 __builtin_return_address(0),__builtin_return_address(1),
4737 __builtin_return_address(2),__builtin_return_address(3));
4741 if (!mddev
->pers
->error_handler
)
4743 mddev
->pers
->error_handler(mddev
,rdev
);
4744 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4745 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4746 md_wakeup_thread(mddev
->thread
);
4747 md_new_event_inintr(mddev
);
4750 /* seq_file implementation /proc/mdstat */
4752 static void status_unused(struct seq_file
*seq
)
4756 struct list_head
*tmp
;
4758 seq_printf(seq
, "unused devices: ");
4760 ITERATE_RDEV_PENDING(rdev
,tmp
) {
4761 char b
[BDEVNAME_SIZE
];
4763 seq_printf(seq
, "%s ",
4764 bdevname(rdev
->bdev
,b
));
4767 seq_printf(seq
, "<none>");
4769 seq_printf(seq
, "\n");
4773 static void status_resync(struct seq_file
*seq
, mddev_t
* mddev
)
4775 sector_t max_blocks
, resync
, res
;
4776 unsigned long dt
, db
, rt
;
4778 unsigned int per_milli
;
4780 resync
= (mddev
->curr_resync
- atomic_read(&mddev
->recovery_active
))/2;
4782 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
4783 max_blocks
= mddev
->resync_max_sectors
>> 1;
4785 max_blocks
= mddev
->size
;
4788 * Should not happen.
4794 /* Pick 'scale' such that (resync>>scale)*1000 will fit
4795 * in a sector_t, and (max_blocks>>scale) will fit in a
4796 * u32, as those are the requirements for sector_div.
4797 * Thus 'scale' must be at least 10
4800 if (sizeof(sector_t
) > sizeof(unsigned long)) {
4801 while ( max_blocks
/2 > (1ULL<<(scale
+32)))
4804 res
= (resync
>>scale
)*1000;
4805 sector_div(res
, (u32
)((max_blocks
>>scale
)+1));
4809 int i
, x
= per_milli
/50, y
= 20-x
;
4810 seq_printf(seq
, "[");
4811 for (i
= 0; i
< x
; i
++)
4812 seq_printf(seq
, "=");
4813 seq_printf(seq
, ">");
4814 for (i
= 0; i
< y
; i
++)
4815 seq_printf(seq
, ".");
4816 seq_printf(seq
, "] ");
4818 seq_printf(seq
, " %s =%3u.%u%% (%llu/%llu)",
4819 (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)?
4821 (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)?
4823 (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ?
4824 "resync" : "recovery"))),
4825 per_milli
/10, per_milli
% 10,
4826 (unsigned long long) resync
,
4827 (unsigned long long) max_blocks
);
4830 * We do not want to overflow, so the order of operands and
4831 * the * 100 / 100 trick are important. We do a +1 to be
4832 * safe against division by zero. We only estimate anyway.
4834 * dt: time from mark until now
4835 * db: blocks written from mark until now
4836 * rt: remaining time
4838 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
4840 db
= (mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
))
4841 - mddev
->resync_mark_cnt
;
4842 rt
= (dt
* ((unsigned long)(max_blocks
-resync
) / (db
/2/100+1)))/100;
4844 seq_printf(seq
, " finish=%lu.%lumin", rt
/ 60, (rt
% 60)/6);
4846 seq_printf(seq
, " speed=%ldK/sec", db
/2/dt
);
4849 static void *md_seq_start(struct seq_file
*seq
, loff_t
*pos
)
4851 struct list_head
*tmp
;
4861 spin_lock(&all_mddevs_lock
);
4862 list_for_each(tmp
,&all_mddevs
)
4864 mddev
= list_entry(tmp
, mddev_t
, all_mddevs
);
4866 spin_unlock(&all_mddevs_lock
);
4869 spin_unlock(&all_mddevs_lock
);
4871 return (void*)2;/* tail */
4875 static void *md_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
4877 struct list_head
*tmp
;
4878 mddev_t
*next_mddev
, *mddev
= v
;
4884 spin_lock(&all_mddevs_lock
);
4886 tmp
= all_mddevs
.next
;
4888 tmp
= mddev
->all_mddevs
.next
;
4889 if (tmp
!= &all_mddevs
)
4890 next_mddev
= mddev_get(list_entry(tmp
,mddev_t
,all_mddevs
));
4892 next_mddev
= (void*)2;
4895 spin_unlock(&all_mddevs_lock
);
4903 static void md_seq_stop(struct seq_file
*seq
, void *v
)
4907 if (mddev
&& v
!= (void*)1 && v
!= (void*)2)
4911 struct mdstat_info
{
4915 static int md_seq_show(struct seq_file
*seq
, void *v
)
4919 struct list_head
*tmp2
;
4921 struct mdstat_info
*mi
= seq
->private;
4922 struct bitmap
*bitmap
;
4924 if (v
== (void*)1) {
4925 struct mdk_personality
*pers
;
4926 seq_printf(seq
, "Personalities : ");
4927 spin_lock(&pers_lock
);
4928 list_for_each_entry(pers
, &pers_list
, list
)
4929 seq_printf(seq
, "[%s] ", pers
->name
);
4931 spin_unlock(&pers_lock
);
4932 seq_printf(seq
, "\n");
4933 mi
->event
= atomic_read(&md_event_count
);
4936 if (v
== (void*)2) {
4941 if (mddev_lock(mddev
) < 0)
4944 if (mddev
->pers
|| mddev
->raid_disks
|| !list_empty(&mddev
->disks
)) {
4945 seq_printf(seq
, "%s : %sactive", mdname(mddev
),
4946 mddev
->pers
? "" : "in");
4949 seq_printf(seq
, " (read-only)");
4951 seq_printf(seq
, "(auto-read-only)");
4952 seq_printf(seq
, " %s", mddev
->pers
->name
);
4956 ITERATE_RDEV(mddev
,rdev
,tmp2
) {
4957 char b
[BDEVNAME_SIZE
];
4958 seq_printf(seq
, " %s[%d]",
4959 bdevname(rdev
->bdev
,b
), rdev
->desc_nr
);
4960 if (test_bit(WriteMostly
, &rdev
->flags
))
4961 seq_printf(seq
, "(W)");
4962 if (test_bit(Faulty
, &rdev
->flags
)) {
4963 seq_printf(seq
, "(F)");
4965 } else if (rdev
->raid_disk
< 0)
4966 seq_printf(seq
, "(S)"); /* spare */
4970 if (!list_empty(&mddev
->disks
)) {
4972 seq_printf(seq
, "\n %llu blocks",
4973 (unsigned long long)mddev
->array_size
);
4975 seq_printf(seq
, "\n %llu blocks",
4976 (unsigned long long)size
);
4978 if (mddev
->persistent
) {
4979 if (mddev
->major_version
!= 0 ||
4980 mddev
->minor_version
!= 90) {
4981 seq_printf(seq
," super %d.%d",
4982 mddev
->major_version
,
4983 mddev
->minor_version
);
4986 seq_printf(seq
, " super non-persistent");
4989 mddev
->pers
->status (seq
, mddev
);
4990 seq_printf(seq
, "\n ");
4991 if (mddev
->pers
->sync_request
) {
4992 if (mddev
->curr_resync
> 2) {
4993 status_resync (seq
, mddev
);
4994 seq_printf(seq
, "\n ");
4995 } else if (mddev
->curr_resync
== 1 || mddev
->curr_resync
== 2)
4996 seq_printf(seq
, "\tresync=DELAYED\n ");
4997 else if (mddev
->recovery_cp
< MaxSector
)
4998 seq_printf(seq
, "\tresync=PENDING\n ");
5001 seq_printf(seq
, "\n ");
5003 if ((bitmap
= mddev
->bitmap
)) {
5004 unsigned long chunk_kb
;
5005 unsigned long flags
;
5006 spin_lock_irqsave(&bitmap
->lock
, flags
);
5007 chunk_kb
= bitmap
->chunksize
>> 10;
5008 seq_printf(seq
, "bitmap: %lu/%lu pages [%luKB], "
5010 bitmap
->pages
- bitmap
->missing_pages
,
5012 (bitmap
->pages
- bitmap
->missing_pages
)
5013 << (PAGE_SHIFT
- 10),
5014 chunk_kb
? chunk_kb
: bitmap
->chunksize
,
5015 chunk_kb
? "KB" : "B");
5017 seq_printf(seq
, ", file: ");
5018 seq_path(seq
, bitmap
->file
->f_path
.mnt
,
5019 bitmap
->file
->f_path
.dentry
," \t\n");
5022 seq_printf(seq
, "\n");
5023 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
5026 seq_printf(seq
, "\n");
5028 mddev_unlock(mddev
);
5033 static struct seq_operations md_seq_ops
= {
5034 .start
= md_seq_start
,
5035 .next
= md_seq_next
,
5036 .stop
= md_seq_stop
,
5037 .show
= md_seq_show
,
5040 static int md_seq_open(struct inode
*inode
, struct file
*file
)
5043 struct mdstat_info
*mi
= kmalloc(sizeof(*mi
), GFP_KERNEL
);
5047 error
= seq_open(file
, &md_seq_ops
);
5051 struct seq_file
*p
= file
->private_data
;
5053 mi
->event
= atomic_read(&md_event_count
);
5058 static unsigned int mdstat_poll(struct file
*filp
, poll_table
*wait
)
5060 struct seq_file
*m
= filp
->private_data
;
5061 struct mdstat_info
*mi
= m
->private;
5064 poll_wait(filp
, &md_event_waiters
, wait
);
5066 /* always allow read */
5067 mask
= POLLIN
| POLLRDNORM
;
5069 if (mi
->event
!= atomic_read(&md_event_count
))
5070 mask
|= POLLERR
| POLLPRI
;
5074 static const struct file_operations md_seq_fops
= {
5075 .owner
= THIS_MODULE
,
5076 .open
= md_seq_open
,
5078 .llseek
= seq_lseek
,
5079 .release
= seq_release_private
,
5080 .poll
= mdstat_poll
,
5083 int register_md_personality(struct mdk_personality
*p
)
5085 spin_lock(&pers_lock
);
5086 list_add_tail(&p
->list
, &pers_list
);
5087 printk(KERN_INFO
"md: %s personality registered for level %d\n", p
->name
, p
->level
);
5088 spin_unlock(&pers_lock
);
5092 int unregister_md_personality(struct mdk_personality
*p
)
5094 printk(KERN_INFO
"md: %s personality unregistered\n", p
->name
);
5095 spin_lock(&pers_lock
);
5096 list_del_init(&p
->list
);
5097 spin_unlock(&pers_lock
);
5101 static int is_mddev_idle(mddev_t
*mddev
)
5104 struct list_head
*tmp
;
5109 ITERATE_RDEV(mddev
,rdev
,tmp
) {
5110 struct gendisk
*disk
= rdev
->bdev
->bd_contains
->bd_disk
;
5111 curr_events
= disk_stat_read(disk
, sectors
[0]) +
5112 disk_stat_read(disk
, sectors
[1]) -
5113 atomic_read(&disk
->sync_io
);
5114 /* sync IO will cause sync_io to increase before the disk_stats
5115 * as sync_io is counted when a request starts, and
5116 * disk_stats is counted when it completes.
5117 * So resync activity will cause curr_events to be smaller than
5118 * when there was no such activity.
5119 * non-sync IO will cause disk_stat to increase without
5120 * increasing sync_io so curr_events will (eventually)
5121 * be larger than it was before. Once it becomes
5122 * substantially larger, the test below will cause
5123 * the array to appear non-idle, and resync will slow
5125 * If there is a lot of outstanding resync activity when
5126 * we set last_event to curr_events, then all that activity
5127 * completing might cause the array to appear non-idle
5128 * and resync will be slowed down even though there might
5129 * not have been non-resync activity. This will only
5130 * happen once though. 'last_events' will soon reflect
5131 * the state where there is little or no outstanding
5132 * resync requests, and further resync activity will
5133 * always make curr_events less than last_events.
5136 if (curr_events
- rdev
->last_events
> 4096) {
5137 rdev
->last_events
= curr_events
;
5144 void md_done_sync(mddev_t
*mddev
, int blocks
, int ok
)
5146 /* another "blocks" (512byte) blocks have been synced */
5147 atomic_sub(blocks
, &mddev
->recovery_active
);
5148 wake_up(&mddev
->recovery_wait
);
5150 set_bit(MD_RECOVERY_ERR
, &mddev
->recovery
);
5151 md_wakeup_thread(mddev
->thread
);
5152 // stop recovery, signal do_sync ....
5157 /* md_write_start(mddev, bi)
5158 * If we need to update some array metadata (e.g. 'active' flag
5159 * in superblock) before writing, schedule a superblock update
5160 * and wait for it to complete.
5162 void md_write_start(mddev_t
*mddev
, struct bio
*bi
)
5164 if (bio_data_dir(bi
) != WRITE
)
5167 BUG_ON(mddev
->ro
== 1);
5168 if (mddev
->ro
== 2) {
5169 /* need to switch to read/write */
5171 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5172 md_wakeup_thread(mddev
->thread
);
5174 atomic_inc(&mddev
->writes_pending
);
5175 if (mddev
->in_sync
) {
5176 spin_lock_irq(&mddev
->write_lock
);
5177 if (mddev
->in_sync
) {
5179 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
5180 md_wakeup_thread(mddev
->thread
);
5182 spin_unlock_irq(&mddev
->write_lock
);
5184 wait_event(mddev
->sb_wait
, mddev
->flags
==0);
5187 void md_write_end(mddev_t
*mddev
)
5189 if (atomic_dec_and_test(&mddev
->writes_pending
)) {
5190 if (mddev
->safemode
== 2)
5191 md_wakeup_thread(mddev
->thread
);
5192 else if (mddev
->safemode_delay
)
5193 mod_timer(&mddev
->safemode_timer
, jiffies
+ mddev
->safemode_delay
);
5197 /* md_allow_write(mddev)
5198 * Calling this ensures that the array is marked 'active' so that writes
5199 * may proceed without blocking. It is important to call this before
5200 * attempting a GFP_KERNEL allocation while holding the mddev lock.
5201 * Must be called with mddev_lock held.
5203 void md_allow_write(mddev_t
*mddev
)
5210 spin_lock_irq(&mddev
->write_lock
);
5211 if (mddev
->in_sync
) {
5213 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
5214 if (mddev
->safemode_delay
&&
5215 mddev
->safemode
== 0)
5216 mddev
->safemode
= 1;
5217 spin_unlock_irq(&mddev
->write_lock
);
5218 md_update_sb(mddev
, 0);
5220 spin_unlock_irq(&mddev
->write_lock
);
5222 EXPORT_SYMBOL_GPL(md_allow_write
);
5224 static DECLARE_WAIT_QUEUE_HEAD(resync_wait
);
5226 #define SYNC_MARKS 10
5227 #define SYNC_MARK_STEP (3*HZ)
5228 void md_do_sync(mddev_t
*mddev
)
5231 unsigned int currspeed
= 0,
5233 sector_t max_sectors
,j
, io_sectors
;
5234 unsigned long mark
[SYNC_MARKS
];
5235 sector_t mark_cnt
[SYNC_MARKS
];
5237 struct list_head
*tmp
;
5238 sector_t last_check
;
5240 struct list_head
*rtmp
;
5244 /* just incase thread restarts... */
5245 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
5247 if (mddev
->ro
) /* never try to sync a read-only array */
5250 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
5251 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
5252 desc
= "data-check";
5253 else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
5254 desc
= "requested-resync";
5257 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
5262 /* we overload curr_resync somewhat here.
5263 * 0 == not engaged in resync at all
5264 * 2 == checking that there is no conflict with another sync
5265 * 1 == like 2, but have yielded to allow conflicting resync to
5267 * other == active in resync - this many blocks
5269 * Before starting a resync we must have set curr_resync to
5270 * 2, and then checked that every "conflicting" array has curr_resync
5271 * less than ours. When we find one that is the same or higher
5272 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
5273 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
5274 * This will mean we have to start checking from the beginning again.
5279 mddev
->curr_resync
= 2;
5282 if (kthread_should_stop()) {
5283 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5286 ITERATE_MDDEV(mddev2
,tmp
) {
5287 if (mddev2
== mddev
)
5289 if (mddev2
->curr_resync
&&
5290 match_mddev_units(mddev
,mddev2
)) {
5292 if (mddev
< mddev2
&& mddev
->curr_resync
== 2) {
5293 /* arbitrarily yield */
5294 mddev
->curr_resync
= 1;
5295 wake_up(&resync_wait
);
5297 if (mddev
> mddev2
&& mddev
->curr_resync
== 1)
5298 /* no need to wait here, we can wait the next
5299 * time 'round when curr_resync == 2
5302 prepare_to_wait(&resync_wait
, &wq
, TASK_UNINTERRUPTIBLE
);
5303 if (!kthread_should_stop() &&
5304 mddev2
->curr_resync
>= mddev
->curr_resync
) {
5305 printk(KERN_INFO
"md: delaying %s of %s"
5306 " until %s has finished (they"
5307 " share one or more physical units)\n",
5308 desc
, mdname(mddev
), mdname(mddev2
));
5311 finish_wait(&resync_wait
, &wq
);
5314 finish_wait(&resync_wait
, &wq
);
5317 } while (mddev
->curr_resync
< 2);
5320 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
5321 /* resync follows the size requested by the personality,
5322 * which defaults to physical size, but can be virtual size
5324 max_sectors
= mddev
->resync_max_sectors
;
5325 mddev
->resync_mismatches
= 0;
5326 /* we don't use the checkpoint if there's a bitmap */
5327 if (!mddev
->bitmap
&&
5328 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
5329 j
= mddev
->recovery_cp
;
5330 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
5331 max_sectors
= mddev
->size
<< 1;
5333 /* recovery follows the physical size of devices */
5334 max_sectors
= mddev
->size
<< 1;
5336 ITERATE_RDEV(mddev
,rdev
,rtmp
)
5337 if (rdev
->raid_disk
>= 0 &&
5338 !test_bit(Faulty
, &rdev
->flags
) &&
5339 !test_bit(In_sync
, &rdev
->flags
) &&
5340 rdev
->recovery_offset
< j
)
5341 j
= rdev
->recovery_offset
;
5344 printk(KERN_INFO
"md: %s of RAID array %s\n", desc
, mdname(mddev
));
5345 printk(KERN_INFO
"md: minimum _guaranteed_ speed:"
5346 " %d KB/sec/disk.\n", speed_min(mddev
));
5347 printk(KERN_INFO
"md: using maximum available idle IO bandwidth "
5348 "(but not more than %d KB/sec) for %s.\n",
5349 speed_max(mddev
), desc
);
5351 is_mddev_idle(mddev
); /* this also initializes IO event counters */
5354 for (m
= 0; m
< SYNC_MARKS
; m
++) {
5356 mark_cnt
[m
] = io_sectors
;
5359 mddev
->resync_mark
= mark
[last_mark
];
5360 mddev
->resync_mark_cnt
= mark_cnt
[last_mark
];
5363 * Tune reconstruction:
5365 window
= 32*(PAGE_SIZE
/512);
5366 printk(KERN_INFO
"md: using %dk window, over a total of %llu blocks.\n",
5367 window
/2,(unsigned long long) max_sectors
/2);
5369 atomic_set(&mddev
->recovery_active
, 0);
5370 init_waitqueue_head(&mddev
->recovery_wait
);
5375 "md: resuming %s of %s from checkpoint.\n",
5376 desc
, mdname(mddev
));
5377 mddev
->curr_resync
= j
;
5380 while (j
< max_sectors
) {
5384 sectors
= mddev
->pers
->sync_request(mddev
, j
, &skipped
,
5385 currspeed
< speed_min(mddev
));
5387 set_bit(MD_RECOVERY_ERR
, &mddev
->recovery
);
5391 if (!skipped
) { /* actual IO requested */
5392 io_sectors
+= sectors
;
5393 atomic_add(sectors
, &mddev
->recovery_active
);
5397 if (j
>1) mddev
->curr_resync
= j
;
5398 mddev
->curr_mark_cnt
= io_sectors
;
5399 if (last_check
== 0)
5400 /* this is the earliers that rebuilt will be
5401 * visible in /proc/mdstat
5403 md_new_event(mddev
);
5405 if (last_check
+ window
> io_sectors
|| j
== max_sectors
)
5408 last_check
= io_sectors
;
5410 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) ||
5411 test_bit(MD_RECOVERY_ERR
, &mddev
->recovery
))
5415 if (time_after_eq(jiffies
, mark
[last_mark
] + SYNC_MARK_STEP
)) {
5417 int next
= (last_mark
+1) % SYNC_MARKS
;
5419 mddev
->resync_mark
= mark
[next
];
5420 mddev
->resync_mark_cnt
= mark_cnt
[next
];
5421 mark
[next
] = jiffies
;
5422 mark_cnt
[next
] = io_sectors
- atomic_read(&mddev
->recovery_active
);
5427 if (kthread_should_stop()) {
5429 * got a signal, exit.
5432 "md: md_do_sync() got signal ... exiting\n");
5433 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5438 * this loop exits only if either when we are slower than
5439 * the 'hard' speed limit, or the system was IO-idle for
5441 * the system might be non-idle CPU-wise, but we only care
5442 * about not overloading the IO subsystem. (things like an
5443 * e2fsck being done on the RAID array should execute fast)
5445 blk_unplug(mddev
->queue
);
5448 currspeed
= ((unsigned long)(io_sectors
-mddev
->resync_mark_cnt
))/2
5449 /((jiffies
-mddev
->resync_mark
)/HZ
+1) +1;
5451 if (currspeed
> speed_min(mddev
)) {
5452 if ((currspeed
> speed_max(mddev
)) ||
5453 !is_mddev_idle(mddev
)) {
5459 printk(KERN_INFO
"md: %s: %s done.\n",mdname(mddev
), desc
);
5461 * this also signals 'finished resyncing' to md_stop
5464 blk_unplug(mddev
->queue
);
5466 wait_event(mddev
->recovery_wait
, !atomic_read(&mddev
->recovery_active
));
5468 /* tell personality that we are finished */
5469 mddev
->pers
->sync_request(mddev
, max_sectors
, &skipped
, 1);
5471 if (!test_bit(MD_RECOVERY_ERR
, &mddev
->recovery
) &&
5472 !test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
) &&
5473 mddev
->curr_resync
> 2) {
5474 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
5475 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
5476 if (mddev
->curr_resync
>= mddev
->recovery_cp
) {
5478 "md: checkpointing %s of %s.\n",
5479 desc
, mdname(mddev
));
5480 mddev
->recovery_cp
= mddev
->curr_resync
;
5483 mddev
->recovery_cp
= MaxSector
;
5485 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
5486 mddev
->curr_resync
= MaxSector
;
5487 ITERATE_RDEV(mddev
,rdev
,rtmp
)
5488 if (rdev
->raid_disk
>= 0 &&
5489 !test_bit(Faulty
, &rdev
->flags
) &&
5490 !test_bit(In_sync
, &rdev
->flags
) &&
5491 rdev
->recovery_offset
< mddev
->curr_resync
)
5492 rdev
->recovery_offset
= mddev
->curr_resync
;
5495 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
5498 mddev
->curr_resync
= 0;
5499 wake_up(&resync_wait
);
5500 set_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
5501 md_wakeup_thread(mddev
->thread
);
5503 EXPORT_SYMBOL_GPL(md_do_sync
);
5506 static int remove_and_add_spares(mddev_t
*mddev
)
5509 struct list_head
*rtmp
;
5512 ITERATE_RDEV(mddev
,rdev
,rtmp
)
5513 if (rdev
->raid_disk
>= 0 &&
5514 (test_bit(Faulty
, &rdev
->flags
) ||
5515 ! test_bit(In_sync
, &rdev
->flags
)) &&
5516 atomic_read(&rdev
->nr_pending
)==0) {
5517 if (mddev
->pers
->hot_remove_disk(
5518 mddev
, rdev
->raid_disk
)==0) {
5520 sprintf(nm
,"rd%d", rdev
->raid_disk
);
5521 sysfs_remove_link(&mddev
->kobj
, nm
);
5522 rdev
->raid_disk
= -1;
5526 if (mddev
->degraded
) {
5527 ITERATE_RDEV(mddev
,rdev
,rtmp
)
5528 if (rdev
->raid_disk
< 0
5529 && !test_bit(Faulty
, &rdev
->flags
)) {
5530 rdev
->recovery_offset
= 0;
5531 if (mddev
->pers
->hot_add_disk(mddev
,rdev
)) {
5533 sprintf(nm
, "rd%d", rdev
->raid_disk
);
5534 if (sysfs_create_link(&mddev
->kobj
,
5537 "md: cannot register "
5541 md_new_event(mddev
);
5549 * This routine is regularly called by all per-raid-array threads to
5550 * deal with generic issues like resync and super-block update.
5551 * Raid personalities that don't have a thread (linear/raid0) do not
5552 * need this as they never do any recovery or update the superblock.
5554 * It does not do any resync itself, but rather "forks" off other threads
5555 * to do that as needed.
5556 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
5557 * "->recovery" and create a thread at ->sync_thread.
5558 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
5559 * and wakeups up this thread which will reap the thread and finish up.
5560 * This thread also removes any faulty devices (with nr_pending == 0).
5562 * The overall approach is:
5563 * 1/ if the superblock needs updating, update it.
5564 * 2/ If a recovery thread is running, don't do anything else.
5565 * 3/ If recovery has finished, clean up, possibly marking spares active.
5566 * 4/ If there are any faulty devices, remove them.
5567 * 5/ If array is degraded, try to add spares devices
5568 * 6/ If array has spares or is not in-sync, start a resync thread.
5570 void md_check_recovery(mddev_t
*mddev
)
5573 struct list_head
*rtmp
;
5577 bitmap_daemon_work(mddev
->bitmap
);
5582 if (signal_pending(current
)) {
5583 if (mddev
->pers
->sync_request
) {
5584 printk(KERN_INFO
"md: %s in immediate safe mode\n",
5586 mddev
->safemode
= 2;
5588 flush_signals(current
);
5593 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
5594 test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
5595 (mddev
->safemode
== 1) ||
5596 (mddev
->safemode
== 2 && ! atomic_read(&mddev
->writes_pending
)
5597 && !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
5601 if (mddev_trylock(mddev
)) {
5604 spin_lock_irq(&mddev
->write_lock
);
5605 if (mddev
->safemode
&& !atomic_read(&mddev
->writes_pending
) &&
5606 !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
) {
5608 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
5610 if (mddev
->safemode
== 1)
5611 mddev
->safemode
= 0;
5612 spin_unlock_irq(&mddev
->write_lock
);
5615 md_update_sb(mddev
, 0);
5618 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
5619 !test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
)) {
5620 /* resync/recovery still happening */
5621 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5624 if (mddev
->sync_thread
) {
5625 /* resync has finished, collect result */
5626 md_unregister_thread(mddev
->sync_thread
);
5627 mddev
->sync_thread
= NULL
;
5628 if (!test_bit(MD_RECOVERY_ERR
, &mddev
->recovery
) &&
5629 !test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
5631 /* activate any spares */
5632 mddev
->pers
->spare_active(mddev
);
5634 md_update_sb(mddev
, 1);
5636 /* if array is no-longer degraded, then any saved_raid_disk
5637 * information must be scrapped
5639 if (!mddev
->degraded
)
5640 ITERATE_RDEV(mddev
,rdev
,rtmp
)
5641 rdev
->saved_raid_disk
= -1;
5643 mddev
->recovery
= 0;
5644 /* flag recovery needed just to double check */
5645 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5646 md_new_event(mddev
);
5649 /* Clear some bits that don't mean anything, but
5652 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5653 clear_bit(MD_RECOVERY_ERR
, &mddev
->recovery
);
5654 clear_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5655 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
5657 if (test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
5659 /* no recovery is running.
5660 * remove any failed drives, then
5661 * add spares if possible.
5662 * Spare are also removed and re-added, to allow
5663 * the personality to fail the re-add.
5666 if (mddev
->reshape_position
!= MaxSector
) {
5667 if (mddev
->pers
->check_reshape(mddev
) != 0)
5668 /* Cannot proceed */
5670 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
5671 } else if ((spares
= remove_and_add_spares(mddev
))) {
5672 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
5673 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
5674 } else if (mddev
->recovery_cp
< MaxSector
) {
5675 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
5676 } else if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
5677 /* nothing to be done ... */
5680 if (mddev
->pers
->sync_request
) {
5681 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
5682 if (spares
&& mddev
->bitmap
&& ! mddev
->bitmap
->file
) {
5683 /* We are adding a device or devices to an array
5684 * which has the bitmap stored on all devices.
5685 * So make sure all bitmap pages get written
5687 bitmap_write_all(mddev
->bitmap
);
5689 mddev
->sync_thread
= md_register_thread(md_do_sync
,
5692 if (!mddev
->sync_thread
) {
5693 printk(KERN_ERR
"%s: could not start resync"
5696 /* leave the spares where they are, it shouldn't hurt */
5697 mddev
->recovery
= 0;
5699 md_wakeup_thread(mddev
->sync_thread
);
5700 md_new_event(mddev
);
5703 mddev_unlock(mddev
);
5707 static int md_notify_reboot(struct notifier_block
*this,
5708 unsigned long code
, void *x
)
5710 struct list_head
*tmp
;
5713 if ((code
== SYS_DOWN
) || (code
== SYS_HALT
) || (code
== SYS_POWER_OFF
)) {
5715 printk(KERN_INFO
"md: stopping all md devices.\n");
5717 ITERATE_MDDEV(mddev
,tmp
)
5718 if (mddev_trylock(mddev
)) {
5719 do_md_stop (mddev
, 1);
5720 mddev_unlock(mddev
);
5723 * certain more exotic SCSI devices are known to be
5724 * volatile wrt too early system reboots. While the
5725 * right place to handle this issue is the given
5726 * driver, we do want to have a safe RAID driver ...
5733 static struct notifier_block md_notifier
= {
5734 .notifier_call
= md_notify_reboot
,
5736 .priority
= INT_MAX
, /* before any real devices */
5739 static void md_geninit(void)
5741 struct proc_dir_entry
*p
;
5743 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t
));
5745 p
= create_proc_entry("mdstat", S_IRUGO
, NULL
);
5747 p
->proc_fops
= &md_seq_fops
;
5750 static int __init
md_init(void)
5752 if (register_blkdev(MAJOR_NR
, "md"))
5754 if ((mdp_major
=register_blkdev(0, "mdp"))<=0) {
5755 unregister_blkdev(MAJOR_NR
, "md");
5758 blk_register_region(MKDEV(MAJOR_NR
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
5759 md_probe
, NULL
, NULL
);
5760 blk_register_region(MKDEV(mdp_major
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
5761 md_probe
, NULL
, NULL
);
5763 register_reboot_notifier(&md_notifier
);
5764 raid_table_header
= register_sysctl_table(raid_root_table
);
5774 * Searches all registered partitions for autorun RAID arrays
5778 static LIST_HEAD(all_detected_devices
);
5779 struct detected_devices_node
{
5780 struct list_head list
;
5784 void md_autodetect_dev(dev_t dev
)
5786 struct detected_devices_node
*node_detected_dev
;
5788 node_detected_dev
= kzalloc(sizeof(*node_detected_dev
), GFP_KERNEL
);
5789 if (node_detected_dev
) {
5790 node_detected_dev
->dev
= dev
;
5791 list_add_tail(&node_detected_dev
->list
, &all_detected_devices
);
5793 printk(KERN_CRIT
"md: md_autodetect_dev: kzalloc failed"
5794 ", skipping dev(%d,%d)\n", MAJOR(dev
), MINOR(dev
));
5799 static void autostart_arrays(int part
)
5802 struct detected_devices_node
*node_detected_dev
;
5804 int i_scanned
, i_passed
;
5809 printk(KERN_INFO
"md: Autodetecting RAID arrays.\n");
5811 while (!list_empty(&all_detected_devices
) && i_scanned
< INT_MAX
) {
5813 node_detected_dev
= list_entry(all_detected_devices
.next
,
5814 struct detected_devices_node
, list
);
5815 list_del(&node_detected_dev
->list
);
5816 dev
= node_detected_dev
->dev
;
5817 kfree(node_detected_dev
);
5818 rdev
= md_import_device(dev
,0, 90);
5822 if (test_bit(Faulty
, &rdev
->flags
)) {
5826 list_add(&rdev
->same_set
, &pending_raid_disks
);
5830 printk(KERN_INFO
"md: Scanned %d and added %d devices.\n",
5831 i_scanned
, i_passed
);
5833 autorun_devices(part
);
5836 #endif /* !MODULE */
5838 static __exit
void md_exit(void)
5841 struct list_head
*tmp
;
5843 blk_unregister_region(MKDEV(MAJOR_NR
,0), 1U << MINORBITS
);
5844 blk_unregister_region(MKDEV(mdp_major
,0), 1U << MINORBITS
);
5846 unregister_blkdev(MAJOR_NR
,"md");
5847 unregister_blkdev(mdp_major
, "mdp");
5848 unregister_reboot_notifier(&md_notifier
);
5849 unregister_sysctl_table(raid_table_header
);
5850 remove_proc_entry("mdstat", NULL
);
5851 ITERATE_MDDEV(mddev
,tmp
) {
5852 struct gendisk
*disk
= mddev
->gendisk
;
5855 export_array(mddev
);
5858 mddev
->gendisk
= NULL
;
5863 subsys_initcall(md_init
);
5864 module_exit(md_exit
)
5866 static int get_ro(char *buffer
, struct kernel_param
*kp
)
5868 return sprintf(buffer
, "%d", start_readonly
);
5870 static int set_ro(const char *val
, struct kernel_param
*kp
)
5873 int num
= simple_strtoul(val
, &e
, 10);
5874 if (*val
&& (*e
== '\0' || *e
== '\n')) {
5875 start_readonly
= num
;
5881 module_param_call(start_ro
, set_ro
, get_ro
, NULL
, S_IRUSR
|S_IWUSR
);
5882 module_param(start_dirty_degraded
, int, S_IRUGO
|S_IWUSR
);
5885 EXPORT_SYMBOL(register_md_personality
);
5886 EXPORT_SYMBOL(unregister_md_personality
);
5887 EXPORT_SYMBOL(md_error
);
5888 EXPORT_SYMBOL(md_done_sync
);
5889 EXPORT_SYMBOL(md_write_start
);
5890 EXPORT_SYMBOL(md_write_end
);
5891 EXPORT_SYMBOL(md_register_thread
);
5892 EXPORT_SYMBOL(md_unregister_thread
);
5893 EXPORT_SYMBOL(md_wakeup_thread
);
5894 EXPORT_SYMBOL(md_check_recovery
);
5895 MODULE_LICENSE("GPL");
5897 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR
);