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 static DECLARE_WAIT_QUEUE_HEAD(resync_wait
);
79 #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
82 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
83 * is 1000 KB/sec, so the extra system load does not show up that much.
84 * Increase it if you want to have more _guaranteed_ speed. Note that
85 * the RAID driver will use the maximum available bandwidth if the IO
86 * subsystem is idle. There is also an 'absolute maximum' reconstruction
87 * speed limit - in case reconstruction slows down your system despite
90 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
91 * or /sys/block/mdX/md/sync_speed_{min,max}
94 static int sysctl_speed_limit_min
= 1000;
95 static int sysctl_speed_limit_max
= 200000;
96 static inline int speed_min(mddev_t
*mddev
)
98 return mddev
->sync_speed_min
?
99 mddev
->sync_speed_min
: sysctl_speed_limit_min
;
102 static inline int speed_max(mddev_t
*mddev
)
104 return mddev
->sync_speed_max
?
105 mddev
->sync_speed_max
: sysctl_speed_limit_max
;
108 static struct ctl_table_header
*raid_table_header
;
110 static ctl_table raid_table
[] = {
112 .ctl_name
= DEV_RAID_SPEED_LIMIT_MIN
,
113 .procname
= "speed_limit_min",
114 .data
= &sysctl_speed_limit_min
,
115 .maxlen
= sizeof(int),
116 .mode
= S_IRUGO
|S_IWUSR
,
117 .proc_handler
= &proc_dointvec
,
120 .ctl_name
= DEV_RAID_SPEED_LIMIT_MAX
,
121 .procname
= "speed_limit_max",
122 .data
= &sysctl_speed_limit_max
,
123 .maxlen
= sizeof(int),
124 .mode
= S_IRUGO
|S_IWUSR
,
125 .proc_handler
= &proc_dointvec
,
130 static ctl_table raid_dir_table
[] = {
132 .ctl_name
= DEV_RAID
,
135 .mode
= S_IRUGO
|S_IXUGO
,
141 static ctl_table raid_root_table
[] = {
147 .child
= raid_dir_table
,
152 static struct block_device_operations md_fops
;
154 static int start_readonly
;
157 * We have a system wide 'event count' that is incremented
158 * on any 'interesting' event, and readers of /proc/mdstat
159 * can use 'poll' or 'select' to find out when the event
163 * start array, stop array, error, add device, remove device,
164 * start build, activate spare
166 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters
);
167 static atomic_t md_event_count
;
168 void md_new_event(mddev_t
*mddev
)
170 atomic_inc(&md_event_count
);
171 wake_up(&md_event_waiters
);
172 sysfs_notify(&mddev
->kobj
, NULL
, "sync_action");
174 EXPORT_SYMBOL_GPL(md_new_event
);
176 /* Alternate version that can be called from interrupts
177 * when calling sysfs_notify isn't needed.
179 static void md_new_event_inintr(mddev_t
*mddev
)
181 atomic_inc(&md_event_count
);
182 wake_up(&md_event_waiters
);
186 * Enables to iterate over all existing md arrays
187 * all_mddevs_lock protects this list.
189 static LIST_HEAD(all_mddevs
);
190 static DEFINE_SPINLOCK(all_mddevs_lock
);
194 * iterates through all used mddevs in the system.
195 * We take care to grab the all_mddevs_lock whenever navigating
196 * the list, and to always hold a refcount when unlocked.
197 * Any code which breaks out of this loop while own
198 * a reference to the current mddev and must mddev_put it.
200 #define for_each_mddev(mddev,tmp) \
202 for (({ spin_lock(&all_mddevs_lock); \
203 tmp = all_mddevs.next; \
205 ({ if (tmp != &all_mddevs) \
206 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
207 spin_unlock(&all_mddevs_lock); \
208 if (mddev) mddev_put(mddev); \
209 mddev = list_entry(tmp, mddev_t, all_mddevs); \
210 tmp != &all_mddevs;}); \
211 ({ spin_lock(&all_mddevs_lock); \
216 static int md_fail_request (struct request_queue
*q
, struct bio
*bio
)
222 static inline mddev_t
*mddev_get(mddev_t
*mddev
)
224 atomic_inc(&mddev
->active
);
228 static void mddev_put(mddev_t
*mddev
)
230 if (!atomic_dec_and_lock(&mddev
->active
, &all_mddevs_lock
))
232 if (!mddev
->raid_disks
&& list_empty(&mddev
->disks
)) {
233 list_del(&mddev
->all_mddevs
);
234 spin_unlock(&all_mddevs_lock
);
235 blk_cleanup_queue(mddev
->queue
);
236 kobject_put(&mddev
->kobj
);
238 spin_unlock(&all_mddevs_lock
);
241 static mddev_t
* mddev_find(dev_t unit
)
243 mddev_t
*mddev
, *new = NULL
;
246 spin_lock(&all_mddevs_lock
);
247 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
248 if (mddev
->unit
== unit
) {
250 spin_unlock(&all_mddevs_lock
);
256 list_add(&new->all_mddevs
, &all_mddevs
);
257 spin_unlock(&all_mddevs_lock
);
260 spin_unlock(&all_mddevs_lock
);
262 new = kzalloc(sizeof(*new), GFP_KERNEL
);
267 if (MAJOR(unit
) == MD_MAJOR
)
268 new->md_minor
= MINOR(unit
);
270 new->md_minor
= MINOR(unit
) >> MdpMinorShift
;
272 mutex_init(&new->reconfig_mutex
);
273 INIT_LIST_HEAD(&new->disks
);
274 INIT_LIST_HEAD(&new->all_mddevs
);
275 init_timer(&new->safemode_timer
);
276 atomic_set(&new->active
, 1);
277 spin_lock_init(&new->write_lock
);
278 init_waitqueue_head(&new->sb_wait
);
279 init_waitqueue_head(&new->recovery_wait
);
280 new->reshape_position
= MaxSector
;
281 new->resync_max
= MaxSector
;
282 new->level
= LEVEL_NONE
;
284 new->queue
= blk_alloc_queue(GFP_KERNEL
);
289 /* Can be unlocked because the queue is new: no concurrency */
290 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER
, new->queue
);
292 blk_queue_make_request(new->queue
, md_fail_request
);
297 static inline int mddev_lock(mddev_t
* mddev
)
299 return mutex_lock_interruptible(&mddev
->reconfig_mutex
);
302 static inline int mddev_trylock(mddev_t
* mddev
)
304 return mutex_trylock(&mddev
->reconfig_mutex
);
307 static inline void mddev_unlock(mddev_t
* mddev
)
309 mutex_unlock(&mddev
->reconfig_mutex
);
311 md_wakeup_thread(mddev
->thread
);
314 static mdk_rdev_t
* find_rdev_nr(mddev_t
*mddev
, int nr
)
317 struct list_head
*tmp
;
319 rdev_for_each(rdev
, tmp
, mddev
) {
320 if (rdev
->desc_nr
== nr
)
326 static mdk_rdev_t
* find_rdev(mddev_t
* mddev
, dev_t dev
)
328 struct list_head
*tmp
;
331 rdev_for_each(rdev
, tmp
, mddev
) {
332 if (rdev
->bdev
->bd_dev
== dev
)
338 static struct mdk_personality
*find_pers(int level
, char *clevel
)
340 struct mdk_personality
*pers
;
341 list_for_each_entry(pers
, &pers_list
, list
) {
342 if (level
!= LEVEL_NONE
&& pers
->level
== level
)
344 if (strcmp(pers
->name
, clevel
)==0)
350 static inline sector_t
calc_dev_sboffset(struct block_device
*bdev
)
352 sector_t size
= bdev
->bd_inode
->i_size
>> BLOCK_SIZE_BITS
;
353 return MD_NEW_SIZE_BLOCKS(size
);
356 static sector_t
calc_dev_size(mdk_rdev_t
*rdev
, unsigned chunk_size
)
360 size
= rdev
->sb_offset
;
363 size
&= ~((sector_t
)chunk_size
/1024 - 1);
367 static int alloc_disk_sb(mdk_rdev_t
* rdev
)
372 rdev
->sb_page
= alloc_page(GFP_KERNEL
);
373 if (!rdev
->sb_page
) {
374 printk(KERN_ALERT
"md: out of memory.\n");
381 static void free_disk_sb(mdk_rdev_t
* rdev
)
384 put_page(rdev
->sb_page
);
386 rdev
->sb_page
= NULL
;
393 static void super_written(struct bio
*bio
, int error
)
395 mdk_rdev_t
*rdev
= bio
->bi_private
;
396 mddev_t
*mddev
= rdev
->mddev
;
398 if (error
|| !test_bit(BIO_UPTODATE
, &bio
->bi_flags
)) {
399 printk("md: super_written gets error=%d, uptodate=%d\n",
400 error
, test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
401 WARN_ON(test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
402 md_error(mddev
, rdev
);
405 if (atomic_dec_and_test(&mddev
->pending_writes
))
406 wake_up(&mddev
->sb_wait
);
410 static void super_written_barrier(struct bio
*bio
, int error
)
412 struct bio
*bio2
= bio
->bi_private
;
413 mdk_rdev_t
*rdev
= bio2
->bi_private
;
414 mddev_t
*mddev
= rdev
->mddev
;
416 if (!test_bit(BIO_UPTODATE
, &bio
->bi_flags
) &&
417 error
== -EOPNOTSUPP
) {
419 /* barriers don't appear to be supported :-( */
420 set_bit(BarriersNotsupp
, &rdev
->flags
);
421 mddev
->barriers_work
= 0;
422 spin_lock_irqsave(&mddev
->write_lock
, flags
);
423 bio2
->bi_next
= mddev
->biolist
;
424 mddev
->biolist
= bio2
;
425 spin_unlock_irqrestore(&mddev
->write_lock
, flags
);
426 wake_up(&mddev
->sb_wait
);
430 bio
->bi_private
= rdev
;
431 super_written(bio
, error
);
435 void md_super_write(mddev_t
*mddev
, mdk_rdev_t
*rdev
,
436 sector_t sector
, int size
, struct page
*page
)
438 /* write first size bytes of page to sector of rdev
439 * Increment mddev->pending_writes before returning
440 * and decrement it on completion, waking up sb_wait
441 * if zero is reached.
442 * If an error occurred, call md_error
444 * As we might need to resubmit the request if BIO_RW_BARRIER
445 * causes ENOTSUPP, we allocate a spare bio...
447 struct bio
*bio
= bio_alloc(GFP_NOIO
, 1);
448 int rw
= (1<<BIO_RW
) | (1<<BIO_RW_SYNC
);
450 bio
->bi_bdev
= rdev
->bdev
;
451 bio
->bi_sector
= sector
;
452 bio_add_page(bio
, page
, size
, 0);
453 bio
->bi_private
= rdev
;
454 bio
->bi_end_io
= super_written
;
457 atomic_inc(&mddev
->pending_writes
);
458 if (!test_bit(BarriersNotsupp
, &rdev
->flags
)) {
460 rw
|= (1<<BIO_RW_BARRIER
);
461 rbio
= bio_clone(bio
, GFP_NOIO
);
462 rbio
->bi_private
= bio
;
463 rbio
->bi_end_io
= super_written_barrier
;
464 submit_bio(rw
, rbio
);
469 void md_super_wait(mddev_t
*mddev
)
471 /* wait for all superblock writes that were scheduled to complete.
472 * if any had to be retried (due to BARRIER problems), retry them
476 prepare_to_wait(&mddev
->sb_wait
, &wq
, TASK_UNINTERRUPTIBLE
);
477 if (atomic_read(&mddev
->pending_writes
)==0)
479 while (mddev
->biolist
) {
481 spin_lock_irq(&mddev
->write_lock
);
482 bio
= mddev
->biolist
;
483 mddev
->biolist
= bio
->bi_next
;
485 spin_unlock_irq(&mddev
->write_lock
);
486 submit_bio(bio
->bi_rw
, bio
);
490 finish_wait(&mddev
->sb_wait
, &wq
);
493 static void bi_complete(struct bio
*bio
, int error
)
495 complete((struct completion
*)bio
->bi_private
);
498 int sync_page_io(struct block_device
*bdev
, sector_t sector
, int size
,
499 struct page
*page
, int rw
)
501 struct bio
*bio
= bio_alloc(GFP_NOIO
, 1);
502 struct completion event
;
505 rw
|= (1 << BIO_RW_SYNC
);
508 bio
->bi_sector
= sector
;
509 bio_add_page(bio
, page
, size
, 0);
510 init_completion(&event
);
511 bio
->bi_private
= &event
;
512 bio
->bi_end_io
= bi_complete
;
514 wait_for_completion(&event
);
516 ret
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
520 EXPORT_SYMBOL_GPL(sync_page_io
);
522 static int read_disk_sb(mdk_rdev_t
* rdev
, int size
)
524 char b
[BDEVNAME_SIZE
];
525 if (!rdev
->sb_page
) {
533 if (!sync_page_io(rdev
->bdev
, rdev
->sb_offset
<<1, size
, rdev
->sb_page
, READ
))
539 printk(KERN_WARNING
"md: disabled device %s, could not read superblock.\n",
540 bdevname(rdev
->bdev
,b
));
544 static int uuid_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
546 if ( (sb1
->set_uuid0
== sb2
->set_uuid0
) &&
547 (sb1
->set_uuid1
== sb2
->set_uuid1
) &&
548 (sb1
->set_uuid2
== sb2
->set_uuid2
) &&
549 (sb1
->set_uuid3
== sb2
->set_uuid3
))
557 static int sb_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
560 mdp_super_t
*tmp1
, *tmp2
;
562 tmp1
= kmalloc(sizeof(*tmp1
),GFP_KERNEL
);
563 tmp2
= kmalloc(sizeof(*tmp2
),GFP_KERNEL
);
565 if (!tmp1
|| !tmp2
) {
567 printk(KERN_INFO
"md.c: sb1 is not equal to sb2!\n");
575 * nr_disks is not constant
580 if (memcmp(tmp1
, tmp2
, MD_SB_GENERIC_CONSTANT_WORDS
* 4))
592 static u32
md_csum_fold(u32 csum
)
594 csum
= (csum
& 0xffff) + (csum
>> 16);
595 return (csum
& 0xffff) + (csum
>> 16);
598 static unsigned int calc_sb_csum(mdp_super_t
* sb
)
601 u32
*sb32
= (u32
*)sb
;
603 unsigned int disk_csum
, csum
;
605 disk_csum
= sb
->sb_csum
;
608 for (i
= 0; i
< MD_SB_BYTES
/4 ; i
++)
610 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
614 /* This used to use csum_partial, which was wrong for several
615 * reasons including that different results are returned on
616 * different architectures. It isn't critical that we get exactly
617 * the same return value as before (we always csum_fold before
618 * testing, and that removes any differences). However as we
619 * know that csum_partial always returned a 16bit value on
620 * alphas, do a fold to maximise conformity to previous behaviour.
622 sb
->sb_csum
= md_csum_fold(disk_csum
);
624 sb
->sb_csum
= disk_csum
;
631 * Handle superblock details.
632 * We want to be able to handle multiple superblock formats
633 * so we have a common interface to them all, and an array of
634 * different handlers.
635 * We rely on user-space to write the initial superblock, and support
636 * reading and updating of superblocks.
637 * Interface methods are:
638 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
639 * loads and validates a superblock on dev.
640 * if refdev != NULL, compare superblocks on both devices
642 * 0 - dev has a superblock that is compatible with refdev
643 * 1 - dev has a superblock that is compatible and newer than refdev
644 * so dev should be used as the refdev in future
645 * -EINVAL superblock incompatible or invalid
646 * -othererror e.g. -EIO
648 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
649 * Verify that dev is acceptable into mddev.
650 * The first time, mddev->raid_disks will be 0, and data from
651 * dev should be merged in. Subsequent calls check that dev
652 * is new enough. Return 0 or -EINVAL
654 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
655 * Update the superblock for rdev with data in mddev
656 * This does not write to disc.
662 struct module
*owner
;
663 int (*load_super
)(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
);
664 int (*validate_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
665 void (*sync_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
669 * load_super for 0.90.0
671 static int super_90_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
673 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
679 * Calculate the position of the superblock,
680 * it's at the end of the disk.
682 * It also happens to be a multiple of 4Kb.
684 sb_offset
= calc_dev_sboffset(rdev
->bdev
);
685 rdev
->sb_offset
= sb_offset
;
687 ret
= read_disk_sb(rdev
, MD_SB_BYTES
);
692 bdevname(rdev
->bdev
, b
);
693 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
695 if (sb
->md_magic
!= MD_SB_MAGIC
) {
696 printk(KERN_ERR
"md: invalid raid superblock magic on %s\n",
701 if (sb
->major_version
!= 0 ||
702 sb
->minor_version
< 90 ||
703 sb
->minor_version
> 91) {
704 printk(KERN_WARNING
"Bad version number %d.%d on %s\n",
705 sb
->major_version
, sb
->minor_version
,
710 if (sb
->raid_disks
<= 0)
713 if (md_csum_fold(calc_sb_csum(sb
)) != md_csum_fold(sb
->sb_csum
)) {
714 printk(KERN_WARNING
"md: invalid superblock checksum on %s\n",
719 rdev
->preferred_minor
= sb
->md_minor
;
720 rdev
->data_offset
= 0;
721 rdev
->sb_size
= MD_SB_BYTES
;
723 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
724 if (sb
->level
!= 1 && sb
->level
!= 4
725 && sb
->level
!= 5 && sb
->level
!= 6
726 && sb
->level
!= 10) {
727 /* FIXME use a better test */
729 "md: bitmaps not supported for this level.\n");
734 if (sb
->level
== LEVEL_MULTIPATH
)
737 rdev
->desc_nr
= sb
->this_disk
.number
;
743 mdp_super_t
*refsb
= (mdp_super_t
*)page_address(refdev
->sb_page
);
744 if (!uuid_equal(refsb
, sb
)) {
745 printk(KERN_WARNING
"md: %s has different UUID to %s\n",
746 b
, bdevname(refdev
->bdev
,b2
));
749 if (!sb_equal(refsb
, sb
)) {
750 printk(KERN_WARNING
"md: %s has same UUID"
751 " but different superblock to %s\n",
752 b
, bdevname(refdev
->bdev
, b2
));
756 ev2
= md_event(refsb
);
762 rdev
->size
= calc_dev_size(rdev
, sb
->chunk_size
);
764 if (rdev
->size
< sb
->size
&& sb
->level
> 1)
765 /* "this cannot possibly happen" ... */
773 * validate_super for 0.90.0
775 static int super_90_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
778 mdp_super_t
*sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
779 __u64 ev1
= md_event(sb
);
781 rdev
->raid_disk
= -1;
782 clear_bit(Faulty
, &rdev
->flags
);
783 clear_bit(In_sync
, &rdev
->flags
);
784 clear_bit(WriteMostly
, &rdev
->flags
);
785 clear_bit(BarriersNotsupp
, &rdev
->flags
);
787 if (mddev
->raid_disks
== 0) {
788 mddev
->major_version
= 0;
789 mddev
->minor_version
= sb
->minor_version
;
790 mddev
->patch_version
= sb
->patch_version
;
792 mddev
->chunk_size
= sb
->chunk_size
;
793 mddev
->ctime
= sb
->ctime
;
794 mddev
->utime
= sb
->utime
;
795 mddev
->level
= sb
->level
;
796 mddev
->clevel
[0] = 0;
797 mddev
->layout
= sb
->layout
;
798 mddev
->raid_disks
= sb
->raid_disks
;
799 mddev
->size
= sb
->size
;
801 mddev
->bitmap_offset
= 0;
802 mddev
->default_bitmap_offset
= MD_SB_BYTES
>> 9;
804 if (mddev
->minor_version
>= 91) {
805 mddev
->reshape_position
= sb
->reshape_position
;
806 mddev
->delta_disks
= sb
->delta_disks
;
807 mddev
->new_level
= sb
->new_level
;
808 mddev
->new_layout
= sb
->new_layout
;
809 mddev
->new_chunk
= sb
->new_chunk
;
811 mddev
->reshape_position
= MaxSector
;
812 mddev
->delta_disks
= 0;
813 mddev
->new_level
= mddev
->level
;
814 mddev
->new_layout
= mddev
->layout
;
815 mddev
->new_chunk
= mddev
->chunk_size
;
818 if (sb
->state
& (1<<MD_SB_CLEAN
))
819 mddev
->recovery_cp
= MaxSector
;
821 if (sb
->events_hi
== sb
->cp_events_hi
&&
822 sb
->events_lo
== sb
->cp_events_lo
) {
823 mddev
->recovery_cp
= sb
->recovery_cp
;
825 mddev
->recovery_cp
= 0;
828 memcpy(mddev
->uuid
+0, &sb
->set_uuid0
, 4);
829 memcpy(mddev
->uuid
+4, &sb
->set_uuid1
, 4);
830 memcpy(mddev
->uuid
+8, &sb
->set_uuid2
, 4);
831 memcpy(mddev
->uuid
+12,&sb
->set_uuid3
, 4);
833 mddev
->max_disks
= MD_SB_DISKS
;
835 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
) &&
836 mddev
->bitmap_file
== NULL
)
837 mddev
->bitmap_offset
= mddev
->default_bitmap_offset
;
839 } else if (mddev
->pers
== NULL
) {
840 /* Insist on good event counter while assembling */
842 if (ev1
< mddev
->events
)
844 } else if (mddev
->bitmap
) {
845 /* if adding to array with a bitmap, then we can accept an
846 * older device ... but not too old.
848 if (ev1
< mddev
->bitmap
->events_cleared
)
851 if (ev1
< mddev
->events
)
852 /* just a hot-add of a new device, leave raid_disk at -1 */
856 if (mddev
->level
!= LEVEL_MULTIPATH
) {
857 desc
= sb
->disks
+ rdev
->desc_nr
;
859 if (desc
->state
& (1<<MD_DISK_FAULTY
))
860 set_bit(Faulty
, &rdev
->flags
);
861 else if (desc
->state
& (1<<MD_DISK_SYNC
) /* &&
862 desc->raid_disk < mddev->raid_disks */) {
863 set_bit(In_sync
, &rdev
->flags
);
864 rdev
->raid_disk
= desc
->raid_disk
;
866 if (desc
->state
& (1<<MD_DISK_WRITEMOSTLY
))
867 set_bit(WriteMostly
, &rdev
->flags
);
868 } else /* MULTIPATH are always insync */
869 set_bit(In_sync
, &rdev
->flags
);
874 * sync_super for 0.90.0
876 static void super_90_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
879 struct list_head
*tmp
;
881 int next_spare
= mddev
->raid_disks
;
884 /* make rdev->sb match mddev data..
887 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
888 * 3/ any empty disks < next_spare become removed
890 * disks[0] gets initialised to REMOVED because
891 * we cannot be sure from other fields if it has
892 * been initialised or not.
895 int active
=0, working
=0,failed
=0,spare
=0,nr_disks
=0;
897 rdev
->sb_size
= MD_SB_BYTES
;
899 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
901 memset(sb
, 0, sizeof(*sb
));
903 sb
->md_magic
= MD_SB_MAGIC
;
904 sb
->major_version
= mddev
->major_version
;
905 sb
->patch_version
= mddev
->patch_version
;
906 sb
->gvalid_words
= 0; /* ignored */
907 memcpy(&sb
->set_uuid0
, mddev
->uuid
+0, 4);
908 memcpy(&sb
->set_uuid1
, mddev
->uuid
+4, 4);
909 memcpy(&sb
->set_uuid2
, mddev
->uuid
+8, 4);
910 memcpy(&sb
->set_uuid3
, mddev
->uuid
+12,4);
912 sb
->ctime
= mddev
->ctime
;
913 sb
->level
= mddev
->level
;
914 sb
->size
= mddev
->size
;
915 sb
->raid_disks
= mddev
->raid_disks
;
916 sb
->md_minor
= mddev
->md_minor
;
917 sb
->not_persistent
= 0;
918 sb
->utime
= mddev
->utime
;
920 sb
->events_hi
= (mddev
->events
>>32);
921 sb
->events_lo
= (u32
)mddev
->events
;
923 if (mddev
->reshape_position
== MaxSector
)
924 sb
->minor_version
= 90;
926 sb
->minor_version
= 91;
927 sb
->reshape_position
= mddev
->reshape_position
;
928 sb
->new_level
= mddev
->new_level
;
929 sb
->delta_disks
= mddev
->delta_disks
;
930 sb
->new_layout
= mddev
->new_layout
;
931 sb
->new_chunk
= mddev
->new_chunk
;
933 mddev
->minor_version
= sb
->minor_version
;
936 sb
->recovery_cp
= mddev
->recovery_cp
;
937 sb
->cp_events_hi
= (mddev
->events
>>32);
938 sb
->cp_events_lo
= (u32
)mddev
->events
;
939 if (mddev
->recovery_cp
== MaxSector
)
940 sb
->state
= (1<< MD_SB_CLEAN
);
944 sb
->layout
= mddev
->layout
;
945 sb
->chunk_size
= mddev
->chunk_size
;
947 if (mddev
->bitmap
&& mddev
->bitmap_file
== NULL
)
948 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
950 sb
->disks
[0].state
= (1<<MD_DISK_REMOVED
);
951 rdev_for_each(rdev2
, tmp
, mddev
) {
954 if (rdev2
->raid_disk
>= 0 && test_bit(In_sync
, &rdev2
->flags
)
955 && !test_bit(Faulty
, &rdev2
->flags
))
956 desc_nr
= rdev2
->raid_disk
;
958 desc_nr
= next_spare
++;
959 rdev2
->desc_nr
= desc_nr
;
960 d
= &sb
->disks
[rdev2
->desc_nr
];
962 d
->number
= rdev2
->desc_nr
;
963 d
->major
= MAJOR(rdev2
->bdev
->bd_dev
);
964 d
->minor
= MINOR(rdev2
->bdev
->bd_dev
);
965 if (rdev2
->raid_disk
>= 0 && test_bit(In_sync
, &rdev2
->flags
)
966 && !test_bit(Faulty
, &rdev2
->flags
))
967 d
->raid_disk
= rdev2
->raid_disk
;
969 d
->raid_disk
= rdev2
->desc_nr
; /* compatibility */
970 if (test_bit(Faulty
, &rdev2
->flags
))
971 d
->state
= (1<<MD_DISK_FAULTY
);
972 else if (test_bit(In_sync
, &rdev2
->flags
)) {
973 d
->state
= (1<<MD_DISK_ACTIVE
);
974 d
->state
|= (1<<MD_DISK_SYNC
);
982 if (test_bit(WriteMostly
, &rdev2
->flags
))
983 d
->state
|= (1<<MD_DISK_WRITEMOSTLY
);
985 /* now set the "removed" and "faulty" bits on any missing devices */
986 for (i
=0 ; i
< mddev
->raid_disks
; i
++) {
987 mdp_disk_t
*d
= &sb
->disks
[i
];
988 if (d
->state
== 0 && d
->number
== 0) {
991 d
->state
= (1<<MD_DISK_REMOVED
);
992 d
->state
|= (1<<MD_DISK_FAULTY
);
996 sb
->nr_disks
= nr_disks
;
997 sb
->active_disks
= active
;
998 sb
->working_disks
= working
;
999 sb
->failed_disks
= failed
;
1000 sb
->spare_disks
= spare
;
1002 sb
->this_disk
= sb
->disks
[rdev
->desc_nr
];
1003 sb
->sb_csum
= calc_sb_csum(sb
);
1007 * version 1 superblock
1010 static __le32
calc_sb_1_csum(struct mdp_superblock_1
* sb
)
1014 unsigned long long newcsum
;
1015 int size
= 256 + le32_to_cpu(sb
->max_dev
)*2;
1016 __le32
*isuper
= (__le32
*)sb
;
1019 disk_csum
= sb
->sb_csum
;
1022 for (i
=0; size
>=4; size
-= 4 )
1023 newcsum
+= le32_to_cpu(*isuper
++);
1026 newcsum
+= le16_to_cpu(*(__le16
*) isuper
);
1028 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
1029 sb
->sb_csum
= disk_csum
;
1030 return cpu_to_le32(csum
);
1033 static int super_1_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
1035 struct mdp_superblock_1
*sb
;
1038 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1042 * Calculate the position of the superblock.
1043 * It is always aligned to a 4K boundary and
1044 * depeding on minor_version, it can be:
1045 * 0: At least 8K, but less than 12K, from end of device
1046 * 1: At start of device
1047 * 2: 4K from start of device.
1049 switch(minor_version
) {
1051 sb_offset
= rdev
->bdev
->bd_inode
->i_size
>> 9;
1053 sb_offset
&= ~(sector_t
)(4*2-1);
1054 /* convert from sectors to K */
1066 rdev
->sb_offset
= sb_offset
;
1068 /* superblock is rarely larger than 1K, but it can be larger,
1069 * and it is safe to read 4k, so we do that
1071 ret
= read_disk_sb(rdev
, 4096);
1072 if (ret
) return ret
;
1075 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1077 if (sb
->magic
!= cpu_to_le32(MD_SB_MAGIC
) ||
1078 sb
->major_version
!= cpu_to_le32(1) ||
1079 le32_to_cpu(sb
->max_dev
) > (4096-256)/2 ||
1080 le64_to_cpu(sb
->super_offset
) != (rdev
->sb_offset
<<1) ||
1081 (le32_to_cpu(sb
->feature_map
) & ~MD_FEATURE_ALL
) != 0)
1084 if (calc_sb_1_csum(sb
) != sb
->sb_csum
) {
1085 printk("md: invalid superblock checksum on %s\n",
1086 bdevname(rdev
->bdev
,b
));
1089 if (le64_to_cpu(sb
->data_size
) < 10) {
1090 printk("md: data_size too small on %s\n",
1091 bdevname(rdev
->bdev
,b
));
1094 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BITMAP_OFFSET
)) {
1095 if (sb
->level
!= cpu_to_le32(1) &&
1096 sb
->level
!= cpu_to_le32(4) &&
1097 sb
->level
!= cpu_to_le32(5) &&
1098 sb
->level
!= cpu_to_le32(6) &&
1099 sb
->level
!= cpu_to_le32(10)) {
1101 "md: bitmaps not supported for this level.\n");
1106 rdev
->preferred_minor
= 0xffff;
1107 rdev
->data_offset
= le64_to_cpu(sb
->data_offset
);
1108 atomic_set(&rdev
->corrected_errors
, le32_to_cpu(sb
->cnt_corrected_read
));
1110 rdev
->sb_size
= le32_to_cpu(sb
->max_dev
) * 2 + 256;
1111 bmask
= queue_hardsect_size(rdev
->bdev
->bd_disk
->queue
)-1;
1112 if (rdev
->sb_size
& bmask
)
1113 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1116 && rdev
->data_offset
< sb_offset
+ (rdev
->sb_size
/512))
1119 if (sb
->level
== cpu_to_le32(LEVEL_MULTIPATH
))
1122 rdev
->desc_nr
= le32_to_cpu(sb
->dev_number
);
1128 struct mdp_superblock_1
*refsb
=
1129 (struct mdp_superblock_1
*)page_address(refdev
->sb_page
);
1131 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16) != 0 ||
1132 sb
->level
!= refsb
->level
||
1133 sb
->layout
!= refsb
->layout
||
1134 sb
->chunksize
!= refsb
->chunksize
) {
1135 printk(KERN_WARNING
"md: %s has strangely different"
1136 " superblock to %s\n",
1137 bdevname(rdev
->bdev
,b
),
1138 bdevname(refdev
->bdev
,b2
));
1141 ev1
= le64_to_cpu(sb
->events
);
1142 ev2
= le64_to_cpu(refsb
->events
);
1150 rdev
->size
= ((rdev
->bdev
->bd_inode
->i_size
>>9) - le64_to_cpu(sb
->data_offset
)) / 2;
1152 rdev
->size
= rdev
->sb_offset
;
1153 if (rdev
->size
< le64_to_cpu(sb
->data_size
)/2)
1155 rdev
->size
= le64_to_cpu(sb
->data_size
)/2;
1156 if (le32_to_cpu(sb
->chunksize
))
1157 rdev
->size
&= ~((sector_t
)le32_to_cpu(sb
->chunksize
)/2 - 1);
1159 if (le64_to_cpu(sb
->size
) > rdev
->size
*2)
1164 static int super_1_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1166 struct mdp_superblock_1
*sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1167 __u64 ev1
= le64_to_cpu(sb
->events
);
1169 rdev
->raid_disk
= -1;
1170 clear_bit(Faulty
, &rdev
->flags
);
1171 clear_bit(In_sync
, &rdev
->flags
);
1172 clear_bit(WriteMostly
, &rdev
->flags
);
1173 clear_bit(BarriersNotsupp
, &rdev
->flags
);
1175 if (mddev
->raid_disks
== 0) {
1176 mddev
->major_version
= 1;
1177 mddev
->patch_version
= 0;
1178 mddev
->external
= 0;
1179 mddev
->chunk_size
= le32_to_cpu(sb
->chunksize
) << 9;
1180 mddev
->ctime
= le64_to_cpu(sb
->ctime
) & ((1ULL << 32)-1);
1181 mddev
->utime
= le64_to_cpu(sb
->utime
) & ((1ULL << 32)-1);
1182 mddev
->level
= le32_to_cpu(sb
->level
);
1183 mddev
->clevel
[0] = 0;
1184 mddev
->layout
= le32_to_cpu(sb
->layout
);
1185 mddev
->raid_disks
= le32_to_cpu(sb
->raid_disks
);
1186 mddev
->size
= le64_to_cpu(sb
->size
)/2;
1187 mddev
->events
= ev1
;
1188 mddev
->bitmap_offset
= 0;
1189 mddev
->default_bitmap_offset
= 1024 >> 9;
1191 mddev
->recovery_cp
= le64_to_cpu(sb
->resync_offset
);
1192 memcpy(mddev
->uuid
, sb
->set_uuid
, 16);
1194 mddev
->max_disks
= (4096-256)/2;
1196 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BITMAP_OFFSET
) &&
1197 mddev
->bitmap_file
== NULL
)
1198 mddev
->bitmap_offset
= (__s32
)le32_to_cpu(sb
->bitmap_offset
);
1200 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
1201 mddev
->reshape_position
= le64_to_cpu(sb
->reshape_position
);
1202 mddev
->delta_disks
= le32_to_cpu(sb
->delta_disks
);
1203 mddev
->new_level
= le32_to_cpu(sb
->new_level
);
1204 mddev
->new_layout
= le32_to_cpu(sb
->new_layout
);
1205 mddev
->new_chunk
= le32_to_cpu(sb
->new_chunk
)<<9;
1207 mddev
->reshape_position
= MaxSector
;
1208 mddev
->delta_disks
= 0;
1209 mddev
->new_level
= mddev
->level
;
1210 mddev
->new_layout
= mddev
->layout
;
1211 mddev
->new_chunk
= mddev
->chunk_size
;
1214 } else if (mddev
->pers
== NULL
) {
1215 /* Insist of good event counter while assembling */
1217 if (ev1
< mddev
->events
)
1219 } else if (mddev
->bitmap
) {
1220 /* If adding to array with a bitmap, then we can accept an
1221 * older device, but not too old.
1223 if (ev1
< mddev
->bitmap
->events_cleared
)
1226 if (ev1
< mddev
->events
)
1227 /* just a hot-add of a new device, leave raid_disk at -1 */
1230 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1232 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
1234 case 0xffff: /* spare */
1236 case 0xfffe: /* faulty */
1237 set_bit(Faulty
, &rdev
->flags
);
1240 if ((le32_to_cpu(sb
->feature_map
) &
1241 MD_FEATURE_RECOVERY_OFFSET
))
1242 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
1244 set_bit(In_sync
, &rdev
->flags
);
1245 rdev
->raid_disk
= role
;
1248 if (sb
->devflags
& WriteMostly1
)
1249 set_bit(WriteMostly
, &rdev
->flags
);
1250 } else /* MULTIPATH are always insync */
1251 set_bit(In_sync
, &rdev
->flags
);
1256 static void super_1_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1258 struct mdp_superblock_1
*sb
;
1259 struct list_head
*tmp
;
1262 /* make rdev->sb match mddev and rdev data. */
1264 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1266 sb
->feature_map
= 0;
1268 sb
->recovery_offset
= cpu_to_le64(0);
1269 memset(sb
->pad1
, 0, sizeof(sb
->pad1
));
1270 memset(sb
->pad2
, 0, sizeof(sb
->pad2
));
1271 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
1273 sb
->utime
= cpu_to_le64((__u64
)mddev
->utime
);
1274 sb
->events
= cpu_to_le64(mddev
->events
);
1276 sb
->resync_offset
= cpu_to_le64(mddev
->recovery_cp
);
1278 sb
->resync_offset
= cpu_to_le64(0);
1280 sb
->cnt_corrected_read
= cpu_to_le32(atomic_read(&rdev
->corrected_errors
));
1282 sb
->raid_disks
= cpu_to_le32(mddev
->raid_disks
);
1283 sb
->size
= cpu_to_le64(mddev
->size
<<1);
1285 if (mddev
->bitmap
&& mddev
->bitmap_file
== NULL
) {
1286 sb
->bitmap_offset
= cpu_to_le32((__u32
)mddev
->bitmap_offset
);
1287 sb
->feature_map
= cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
);
1290 if (rdev
->raid_disk
>= 0 &&
1291 !test_bit(In_sync
, &rdev
->flags
) &&
1292 rdev
->recovery_offset
> 0) {
1293 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET
);
1294 sb
->recovery_offset
= cpu_to_le64(rdev
->recovery_offset
);
1297 if (mddev
->reshape_position
!= MaxSector
) {
1298 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE
);
1299 sb
->reshape_position
= cpu_to_le64(mddev
->reshape_position
);
1300 sb
->new_layout
= cpu_to_le32(mddev
->new_layout
);
1301 sb
->delta_disks
= cpu_to_le32(mddev
->delta_disks
);
1302 sb
->new_level
= cpu_to_le32(mddev
->new_level
);
1303 sb
->new_chunk
= cpu_to_le32(mddev
->new_chunk
>>9);
1307 rdev_for_each(rdev2
, tmp
, mddev
)
1308 if (rdev2
->desc_nr
+1 > max_dev
)
1309 max_dev
= rdev2
->desc_nr
+1;
1311 if (max_dev
> le32_to_cpu(sb
->max_dev
))
1312 sb
->max_dev
= cpu_to_le32(max_dev
);
1313 for (i
=0; i
<max_dev
;i
++)
1314 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1316 rdev_for_each(rdev2
, tmp
, mddev
) {
1318 if (test_bit(Faulty
, &rdev2
->flags
))
1319 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1320 else if (test_bit(In_sync
, &rdev2
->flags
))
1321 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1322 else if (rdev2
->raid_disk
>= 0 && rdev2
->recovery_offset
> 0)
1323 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1325 sb
->dev_roles
[i
] = cpu_to_le16(0xffff);
1328 sb
->sb_csum
= calc_sb_1_csum(sb
);
1332 static struct super_type super_types
[] = {
1335 .owner
= THIS_MODULE
,
1336 .load_super
= super_90_load
,
1337 .validate_super
= super_90_validate
,
1338 .sync_super
= super_90_sync
,
1342 .owner
= THIS_MODULE
,
1343 .load_super
= super_1_load
,
1344 .validate_super
= super_1_validate
,
1345 .sync_super
= super_1_sync
,
1349 static int match_mddev_units(mddev_t
*mddev1
, mddev_t
*mddev2
)
1351 struct list_head
*tmp
, *tmp2
;
1352 mdk_rdev_t
*rdev
, *rdev2
;
1354 rdev_for_each(rdev
, tmp
, mddev1
)
1355 rdev_for_each(rdev2
, tmp2
, mddev2
)
1356 if (rdev
->bdev
->bd_contains
==
1357 rdev2
->bdev
->bd_contains
)
1363 static LIST_HEAD(pending_raid_disks
);
1365 static int bind_rdev_to_array(mdk_rdev_t
* rdev
, mddev_t
* mddev
)
1367 char b
[BDEVNAME_SIZE
];
1377 /* prevent duplicates */
1378 if (find_rdev(mddev
, rdev
->bdev
->bd_dev
))
1381 /* make sure rdev->size exceeds mddev->size */
1382 if (rdev
->size
&& (mddev
->size
== 0 || rdev
->size
< mddev
->size
)) {
1384 /* Cannot change size, so fail
1385 * If mddev->level <= 0, then we don't care
1386 * about aligning sizes (e.g. linear)
1388 if (mddev
->level
> 0)
1391 mddev
->size
= rdev
->size
;
1394 /* Verify rdev->desc_nr is unique.
1395 * If it is -1, assign a free number, else
1396 * check number is not in use
1398 if (rdev
->desc_nr
< 0) {
1400 if (mddev
->pers
) choice
= mddev
->raid_disks
;
1401 while (find_rdev_nr(mddev
, choice
))
1403 rdev
->desc_nr
= choice
;
1405 if (find_rdev_nr(mddev
, rdev
->desc_nr
))
1408 bdevname(rdev
->bdev
,b
);
1409 while ( (s
=strchr(b
, '/')) != NULL
)
1412 rdev
->mddev
= mddev
;
1413 printk(KERN_INFO
"md: bind<%s>\n", b
);
1415 if ((err
= kobject_add(&rdev
->kobj
, &mddev
->kobj
, "dev-%s", b
)))
1418 if (rdev
->bdev
->bd_part
)
1419 ko
= &rdev
->bdev
->bd_part
->dev
.kobj
;
1421 ko
= &rdev
->bdev
->bd_disk
->dev
.kobj
;
1422 if ((err
= sysfs_create_link(&rdev
->kobj
, ko
, "block"))) {
1423 kobject_del(&rdev
->kobj
);
1426 list_add(&rdev
->same_set
, &mddev
->disks
);
1427 bd_claim_by_disk(rdev
->bdev
, rdev
->bdev
->bd_holder
, mddev
->gendisk
);
1431 printk(KERN_WARNING
"md: failed to register dev-%s for %s\n",
1436 static void md_delayed_delete(struct work_struct
*ws
)
1438 mdk_rdev_t
*rdev
= container_of(ws
, mdk_rdev_t
, del_work
);
1439 kobject_del(&rdev
->kobj
);
1440 kobject_put(&rdev
->kobj
);
1443 static void unbind_rdev_from_array(mdk_rdev_t
* rdev
)
1445 char b
[BDEVNAME_SIZE
];
1450 bd_release_from_disk(rdev
->bdev
, rdev
->mddev
->gendisk
);
1451 list_del_init(&rdev
->same_set
);
1452 printk(KERN_INFO
"md: unbind<%s>\n", bdevname(rdev
->bdev
,b
));
1454 sysfs_remove_link(&rdev
->kobj
, "block");
1456 /* We need to delay this, otherwise we can deadlock when
1457 * writing to 'remove' to "dev/state"
1459 INIT_WORK(&rdev
->del_work
, md_delayed_delete
);
1460 kobject_get(&rdev
->kobj
);
1461 schedule_work(&rdev
->del_work
);
1465 * prevent the device from being mounted, repartitioned or
1466 * otherwise reused by a RAID array (or any other kernel
1467 * subsystem), by bd_claiming the device.
1469 static int lock_rdev(mdk_rdev_t
*rdev
, dev_t dev
, int shared
)
1472 struct block_device
*bdev
;
1473 char b
[BDEVNAME_SIZE
];
1475 bdev
= open_by_devnum(dev
, FMODE_READ
|FMODE_WRITE
);
1477 printk(KERN_ERR
"md: could not open %s.\n",
1478 __bdevname(dev
, b
));
1479 return PTR_ERR(bdev
);
1481 err
= bd_claim(bdev
, shared
? (mdk_rdev_t
*)lock_rdev
: rdev
);
1483 printk(KERN_ERR
"md: could not bd_claim %s.\n",
1489 set_bit(AllReserved
, &rdev
->flags
);
1494 static void unlock_rdev(mdk_rdev_t
*rdev
)
1496 struct block_device
*bdev
= rdev
->bdev
;
1504 void md_autodetect_dev(dev_t dev
);
1506 static void export_rdev(mdk_rdev_t
* rdev
)
1508 char b
[BDEVNAME_SIZE
];
1509 printk(KERN_INFO
"md: export_rdev(%s)\n",
1510 bdevname(rdev
->bdev
,b
));
1514 list_del_init(&rdev
->same_set
);
1516 if (test_bit(AutoDetected
, &rdev
->flags
))
1517 md_autodetect_dev(rdev
->bdev
->bd_dev
);
1520 kobject_put(&rdev
->kobj
);
1523 static void kick_rdev_from_array(mdk_rdev_t
* rdev
)
1525 unbind_rdev_from_array(rdev
);
1529 static void export_array(mddev_t
*mddev
)
1531 struct list_head
*tmp
;
1534 rdev_for_each(rdev
, tmp
, mddev
) {
1539 kick_rdev_from_array(rdev
);
1541 if (!list_empty(&mddev
->disks
))
1543 mddev
->raid_disks
= 0;
1544 mddev
->major_version
= 0;
1547 static void print_desc(mdp_disk_t
*desc
)
1549 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc
->number
,
1550 desc
->major
,desc
->minor
,desc
->raid_disk
,desc
->state
);
1553 static void print_sb(mdp_super_t
*sb
)
1558 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1559 sb
->major_version
, sb
->minor_version
, sb
->patch_version
,
1560 sb
->set_uuid0
, sb
->set_uuid1
, sb
->set_uuid2
, sb
->set_uuid3
,
1562 printk(KERN_INFO
"md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1563 sb
->level
, sb
->size
, sb
->nr_disks
, sb
->raid_disks
,
1564 sb
->md_minor
, sb
->layout
, sb
->chunk_size
);
1565 printk(KERN_INFO
"md: UT:%08x ST:%d AD:%d WD:%d"
1566 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1567 sb
->utime
, sb
->state
, sb
->active_disks
, sb
->working_disks
,
1568 sb
->failed_disks
, sb
->spare_disks
,
1569 sb
->sb_csum
, (unsigned long)sb
->events_lo
);
1572 for (i
= 0; i
< MD_SB_DISKS
; i
++) {
1575 desc
= sb
->disks
+ i
;
1576 if (desc
->number
|| desc
->major
|| desc
->minor
||
1577 desc
->raid_disk
|| (desc
->state
&& (desc
->state
!= 4))) {
1578 printk(" D %2d: ", i
);
1582 printk(KERN_INFO
"md: THIS: ");
1583 print_desc(&sb
->this_disk
);
1587 static void print_rdev(mdk_rdev_t
*rdev
)
1589 char b
[BDEVNAME_SIZE
];
1590 printk(KERN_INFO
"md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1591 bdevname(rdev
->bdev
,b
), (unsigned long long)rdev
->size
,
1592 test_bit(Faulty
, &rdev
->flags
), test_bit(In_sync
, &rdev
->flags
),
1594 if (rdev
->sb_loaded
) {
1595 printk(KERN_INFO
"md: rdev superblock:\n");
1596 print_sb((mdp_super_t
*)page_address(rdev
->sb_page
));
1598 printk(KERN_INFO
"md: no rdev superblock!\n");
1601 static void md_print_devices(void)
1603 struct list_head
*tmp
, *tmp2
;
1606 char b
[BDEVNAME_SIZE
];
1609 printk("md: **********************************\n");
1610 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1611 printk("md: **********************************\n");
1612 for_each_mddev(mddev
, tmp
) {
1615 bitmap_print_sb(mddev
->bitmap
);
1617 printk("%s: ", mdname(mddev
));
1618 rdev_for_each(rdev
, tmp2
, mddev
)
1619 printk("<%s>", bdevname(rdev
->bdev
,b
));
1622 rdev_for_each(rdev
, tmp2
, mddev
)
1625 printk("md: **********************************\n");
1630 static void sync_sbs(mddev_t
* mddev
, int nospares
)
1632 /* Update each superblock (in-memory image), but
1633 * if we are allowed to, skip spares which already
1634 * have the right event counter, or have one earlier
1635 * (which would mean they aren't being marked as dirty
1636 * with the rest of the array)
1639 struct list_head
*tmp
;
1641 rdev_for_each(rdev
, tmp
, mddev
) {
1642 if (rdev
->sb_events
== mddev
->events
||
1644 rdev
->raid_disk
< 0 &&
1645 (rdev
->sb_events
&1)==0 &&
1646 rdev
->sb_events
+1 == mddev
->events
)) {
1647 /* Don't update this superblock */
1648 rdev
->sb_loaded
= 2;
1650 super_types
[mddev
->major_version
].
1651 sync_super(mddev
, rdev
);
1652 rdev
->sb_loaded
= 1;
1657 static void md_update_sb(mddev_t
* mddev
, int force_change
)
1659 struct list_head
*tmp
;
1664 if (mddev
->external
)
1667 spin_lock_irq(&mddev
->write_lock
);
1669 set_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
1670 if (test_and_clear_bit(MD_CHANGE_DEVS
, &mddev
->flags
))
1672 if (test_and_clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
))
1673 /* just a clean<-> dirty transition, possibly leave spares alone,
1674 * though if events isn't the right even/odd, we will have to do
1680 if (mddev
->degraded
)
1681 /* If the array is degraded, then skipping spares is both
1682 * dangerous and fairly pointless.
1683 * Dangerous because a device that was removed from the array
1684 * might have a event_count that still looks up-to-date,
1685 * so it can be re-added without a resync.
1686 * Pointless because if there are any spares to skip,
1687 * then a recovery will happen and soon that array won't
1688 * be degraded any more and the spare can go back to sleep then.
1692 sync_req
= mddev
->in_sync
;
1693 mddev
->utime
= get_seconds();
1695 /* If this is just a dirty<->clean transition, and the array is clean
1696 * and 'events' is odd, we can roll back to the previous clean state */
1698 && (mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
1699 && (mddev
->events
& 1)
1700 && mddev
->events
!= 1)
1703 /* otherwise we have to go forward and ... */
1705 if (!mddev
->in_sync
|| mddev
->recovery_cp
!= MaxSector
) { /* not clean */
1706 /* .. if the array isn't clean, insist on an odd 'events' */
1707 if ((mddev
->events
&1)==0) {
1712 /* otherwise insist on an even 'events' (for clean states) */
1713 if ((mddev
->events
&1)) {
1720 if (!mddev
->events
) {
1722 * oops, this 64-bit counter should never wrap.
1723 * Either we are in around ~1 trillion A.C., assuming
1724 * 1 reboot per second, or we have a bug:
1731 * do not write anything to disk if using
1732 * nonpersistent superblocks
1734 if (!mddev
->persistent
) {
1735 if (!mddev
->external
)
1736 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
1738 spin_unlock_irq(&mddev
->write_lock
);
1739 wake_up(&mddev
->sb_wait
);
1742 sync_sbs(mddev
, nospares
);
1743 spin_unlock_irq(&mddev
->write_lock
);
1746 "md: updating %s RAID superblock on device (in sync %d)\n",
1747 mdname(mddev
),mddev
->in_sync
);
1749 bitmap_update_sb(mddev
->bitmap
);
1750 rdev_for_each(rdev
, tmp
, mddev
) {
1751 char b
[BDEVNAME_SIZE
];
1752 dprintk(KERN_INFO
"md: ");
1753 if (rdev
->sb_loaded
!= 1)
1754 continue; /* no noise on spare devices */
1755 if (test_bit(Faulty
, &rdev
->flags
))
1756 dprintk("(skipping faulty ");
1758 dprintk("%s ", bdevname(rdev
->bdev
,b
));
1759 if (!test_bit(Faulty
, &rdev
->flags
)) {
1760 md_super_write(mddev
,rdev
,
1761 rdev
->sb_offset
<<1, rdev
->sb_size
,
1763 dprintk(KERN_INFO
"(write) %s's sb offset: %llu\n",
1764 bdevname(rdev
->bdev
,b
),
1765 (unsigned long long)rdev
->sb_offset
);
1766 rdev
->sb_events
= mddev
->events
;
1770 if (mddev
->level
== LEVEL_MULTIPATH
)
1771 /* only need to write one superblock... */
1774 md_super_wait(mddev
);
1775 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
1777 spin_lock_irq(&mddev
->write_lock
);
1778 if (mddev
->in_sync
!= sync_req
||
1779 test_bit(MD_CHANGE_DEVS
, &mddev
->flags
)) {
1780 /* have to write it out again */
1781 spin_unlock_irq(&mddev
->write_lock
);
1784 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
1785 spin_unlock_irq(&mddev
->write_lock
);
1786 wake_up(&mddev
->sb_wait
);
1790 /* words written to sysfs files may, or my not, be \n terminated.
1791 * We want to accept with case. For this we use cmd_match.
1793 static int cmd_match(const char *cmd
, const char *str
)
1795 /* See if cmd, written into a sysfs file, matches
1796 * str. They must either be the same, or cmd can
1797 * have a trailing newline
1799 while (*cmd
&& *str
&& *cmd
== *str
) {
1810 struct rdev_sysfs_entry
{
1811 struct attribute attr
;
1812 ssize_t (*show
)(mdk_rdev_t
*, char *);
1813 ssize_t (*store
)(mdk_rdev_t
*, const char *, size_t);
1817 state_show(mdk_rdev_t
*rdev
, char *page
)
1822 if (test_bit(Faulty
, &rdev
->flags
)) {
1823 len
+= sprintf(page
+len
, "%sfaulty",sep
);
1826 if (test_bit(In_sync
, &rdev
->flags
)) {
1827 len
+= sprintf(page
+len
, "%sin_sync",sep
);
1830 if (test_bit(WriteMostly
, &rdev
->flags
)) {
1831 len
+= sprintf(page
+len
, "%swrite_mostly",sep
);
1834 if (test_bit(Blocked
, &rdev
->flags
)) {
1835 len
+= sprintf(page
+len
, "%sblocked", sep
);
1838 if (!test_bit(Faulty
, &rdev
->flags
) &&
1839 !test_bit(In_sync
, &rdev
->flags
)) {
1840 len
+= sprintf(page
+len
, "%sspare", sep
);
1843 return len
+sprintf(page
+len
, "\n");
1847 state_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
1850 * faulty - simulates and error
1851 * remove - disconnects the device
1852 * writemostly - sets write_mostly
1853 * -writemostly - clears write_mostly
1854 * blocked - sets the Blocked flag
1855 * -blocked - clears the Blocked flag
1858 if (cmd_match(buf
, "faulty") && rdev
->mddev
->pers
) {
1859 md_error(rdev
->mddev
, rdev
);
1861 } else if (cmd_match(buf
, "remove")) {
1862 if (rdev
->raid_disk
>= 0)
1865 mddev_t
*mddev
= rdev
->mddev
;
1866 kick_rdev_from_array(rdev
);
1868 md_update_sb(mddev
, 1);
1869 md_new_event(mddev
);
1872 } else if (cmd_match(buf
, "writemostly")) {
1873 set_bit(WriteMostly
, &rdev
->flags
);
1875 } else if (cmd_match(buf
, "-writemostly")) {
1876 clear_bit(WriteMostly
, &rdev
->flags
);
1878 } else if (cmd_match(buf
, "blocked")) {
1879 set_bit(Blocked
, &rdev
->flags
);
1881 } else if (cmd_match(buf
, "-blocked")) {
1882 clear_bit(Blocked
, &rdev
->flags
);
1883 wake_up(&rdev
->blocked_wait
);
1884 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
1885 md_wakeup_thread(rdev
->mddev
->thread
);
1889 return err
? err
: len
;
1891 static struct rdev_sysfs_entry rdev_state
=
1892 __ATTR(state
, S_IRUGO
|S_IWUSR
, state_show
, state_store
);
1895 errors_show(mdk_rdev_t
*rdev
, char *page
)
1897 return sprintf(page
, "%d\n", atomic_read(&rdev
->corrected_errors
));
1901 errors_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
1904 unsigned long n
= simple_strtoul(buf
, &e
, 10);
1905 if (*buf
&& (*e
== 0 || *e
== '\n')) {
1906 atomic_set(&rdev
->corrected_errors
, n
);
1911 static struct rdev_sysfs_entry rdev_errors
=
1912 __ATTR(errors
, S_IRUGO
|S_IWUSR
, errors_show
, errors_store
);
1915 slot_show(mdk_rdev_t
*rdev
, char *page
)
1917 if (rdev
->raid_disk
< 0)
1918 return sprintf(page
, "none\n");
1920 return sprintf(page
, "%d\n", rdev
->raid_disk
);
1924 slot_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
1929 int slot
= simple_strtoul(buf
, &e
, 10);
1930 if (strncmp(buf
, "none", 4)==0)
1932 else if (e
==buf
|| (*e
&& *e
!= '\n'))
1934 if (rdev
->mddev
->pers
) {
1935 /* Setting 'slot' on an active array requires also
1936 * updating the 'rd%d' link, and communicating
1937 * with the personality with ->hot_*_disk.
1938 * For now we only support removing
1939 * failed/spare devices. This normally happens automatically,
1940 * but not when the metadata is externally managed.
1944 if (rdev
->raid_disk
== -1)
1946 /* personality does all needed checks */
1947 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
1949 err
= rdev
->mddev
->pers
->
1950 hot_remove_disk(rdev
->mddev
, rdev
->raid_disk
);
1953 sprintf(nm
, "rd%d", rdev
->raid_disk
);
1954 sysfs_remove_link(&rdev
->mddev
->kobj
, nm
);
1955 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
1956 md_wakeup_thread(rdev
->mddev
->thread
);
1958 if (slot
>= rdev
->mddev
->raid_disks
)
1960 rdev
->raid_disk
= slot
;
1961 /* assume it is working */
1962 clear_bit(Faulty
, &rdev
->flags
);
1963 clear_bit(WriteMostly
, &rdev
->flags
);
1964 set_bit(In_sync
, &rdev
->flags
);
1970 static struct rdev_sysfs_entry rdev_slot
=
1971 __ATTR(slot
, S_IRUGO
|S_IWUSR
, slot_show
, slot_store
);
1974 offset_show(mdk_rdev_t
*rdev
, char *page
)
1976 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->data_offset
);
1980 offset_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
1983 unsigned long long offset
= simple_strtoull(buf
, &e
, 10);
1984 if (e
==buf
|| (*e
&& *e
!= '\n'))
1986 if (rdev
->mddev
->pers
)
1988 if (rdev
->size
&& rdev
->mddev
->external
)
1989 /* Must set offset before size, so overlap checks
1992 rdev
->data_offset
= offset
;
1996 static struct rdev_sysfs_entry rdev_offset
=
1997 __ATTR(offset
, S_IRUGO
|S_IWUSR
, offset_show
, offset_store
);
2000 rdev_size_show(mdk_rdev_t
*rdev
, char *page
)
2002 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->size
);
2005 static int overlaps(sector_t s1
, sector_t l1
, sector_t s2
, sector_t l2
)
2007 /* check if two start/length pairs overlap */
2016 rdev_size_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2019 unsigned long long size
= simple_strtoull(buf
, &e
, 10);
2020 unsigned long long oldsize
= rdev
->size
;
2021 mddev_t
*my_mddev
= rdev
->mddev
;
2023 if (e
==buf
|| (*e
&& *e
!= '\n'))
2028 if (size
> oldsize
&& rdev
->mddev
->external
) {
2029 /* need to check that all other rdevs with the same ->bdev
2030 * do not overlap. We need to unlock the mddev to avoid
2031 * a deadlock. We have already changed rdev->size, and if
2032 * we have to change it back, we will have the lock again.
2036 struct list_head
*tmp
, *tmp2
;
2038 mddev_unlock(my_mddev
);
2039 for_each_mddev(mddev
, tmp
) {
2043 rdev_for_each(rdev2
, tmp2
, mddev
)
2044 if (test_bit(AllReserved
, &rdev2
->flags
) ||
2045 (rdev
->bdev
== rdev2
->bdev
&&
2047 overlaps(rdev
->data_offset
, rdev
->size
,
2048 rdev2
->data_offset
, rdev2
->size
))) {
2052 mddev_unlock(mddev
);
2058 mddev_lock(my_mddev
);
2060 /* Someone else could have slipped in a size
2061 * change here, but doing so is just silly.
2062 * We put oldsize back because we *know* it is
2063 * safe, and trust userspace not to race with
2066 rdev
->size
= oldsize
;
2070 if (size
< my_mddev
->size
|| my_mddev
->size
== 0)
2071 my_mddev
->size
= size
;
2075 static struct rdev_sysfs_entry rdev_size
=
2076 __ATTR(size
, S_IRUGO
|S_IWUSR
, rdev_size_show
, rdev_size_store
);
2078 static struct attribute
*rdev_default_attrs
[] = {
2087 rdev_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
2089 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
2090 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
2091 mddev_t
*mddev
= rdev
->mddev
;
2097 rv
= mddev
? mddev_lock(mddev
) : -EBUSY
;
2099 if (rdev
->mddev
== NULL
)
2102 rv
= entry
->show(rdev
, page
);
2103 mddev_unlock(mddev
);
2109 rdev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
2110 const char *page
, size_t length
)
2112 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
2113 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
2115 mddev_t
*mddev
= rdev
->mddev
;
2119 if (!capable(CAP_SYS_ADMIN
))
2121 rv
= mddev
? mddev_lock(mddev
): -EBUSY
;
2123 if (rdev
->mddev
== NULL
)
2126 rv
= entry
->store(rdev
, page
, length
);
2127 mddev_unlock(mddev
);
2132 static void rdev_free(struct kobject
*ko
)
2134 mdk_rdev_t
*rdev
= container_of(ko
, mdk_rdev_t
, kobj
);
2137 static struct sysfs_ops rdev_sysfs_ops
= {
2138 .show
= rdev_attr_show
,
2139 .store
= rdev_attr_store
,
2141 static struct kobj_type rdev_ktype
= {
2142 .release
= rdev_free
,
2143 .sysfs_ops
= &rdev_sysfs_ops
,
2144 .default_attrs
= rdev_default_attrs
,
2148 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2150 * mark the device faulty if:
2152 * - the device is nonexistent (zero size)
2153 * - the device has no valid superblock
2155 * a faulty rdev _never_ has rdev->sb set.
2157 static mdk_rdev_t
*md_import_device(dev_t newdev
, int super_format
, int super_minor
)
2159 char b
[BDEVNAME_SIZE
];
2164 rdev
= kzalloc(sizeof(*rdev
), GFP_KERNEL
);
2166 printk(KERN_ERR
"md: could not alloc mem for new device!\n");
2167 return ERR_PTR(-ENOMEM
);
2170 if ((err
= alloc_disk_sb(rdev
)))
2173 err
= lock_rdev(rdev
, newdev
, super_format
== -2);
2177 kobject_init(&rdev
->kobj
, &rdev_ktype
);
2180 rdev
->saved_raid_disk
= -1;
2181 rdev
->raid_disk
= -1;
2183 rdev
->data_offset
= 0;
2184 rdev
->sb_events
= 0;
2185 atomic_set(&rdev
->nr_pending
, 0);
2186 atomic_set(&rdev
->read_errors
, 0);
2187 atomic_set(&rdev
->corrected_errors
, 0);
2189 size
= rdev
->bdev
->bd_inode
->i_size
>> BLOCK_SIZE_BITS
;
2192 "md: %s has zero or unknown size, marking faulty!\n",
2193 bdevname(rdev
->bdev
,b
));
2198 if (super_format
>= 0) {
2199 err
= super_types
[super_format
].
2200 load_super(rdev
, NULL
, super_minor
);
2201 if (err
== -EINVAL
) {
2203 "md: %s does not have a valid v%d.%d "
2204 "superblock, not importing!\n",
2205 bdevname(rdev
->bdev
,b
),
2206 super_format
, super_minor
);
2211 "md: could not read %s's sb, not importing!\n",
2212 bdevname(rdev
->bdev
,b
));
2217 INIT_LIST_HEAD(&rdev
->same_set
);
2218 init_waitqueue_head(&rdev
->blocked_wait
);
2223 if (rdev
->sb_page
) {
2229 return ERR_PTR(err
);
2233 * Check a full RAID array for plausibility
2237 static void analyze_sbs(mddev_t
* mddev
)
2240 struct list_head
*tmp
;
2241 mdk_rdev_t
*rdev
, *freshest
;
2242 char b
[BDEVNAME_SIZE
];
2245 rdev_for_each(rdev
, tmp
, mddev
)
2246 switch (super_types
[mddev
->major_version
].
2247 load_super(rdev
, freshest
, mddev
->minor_version
)) {
2255 "md: fatal superblock inconsistency in %s"
2256 " -- removing from array\n",
2257 bdevname(rdev
->bdev
,b
));
2258 kick_rdev_from_array(rdev
);
2262 super_types
[mddev
->major_version
].
2263 validate_super(mddev
, freshest
);
2266 rdev_for_each(rdev
, tmp
, mddev
) {
2267 if (rdev
!= freshest
)
2268 if (super_types
[mddev
->major_version
].
2269 validate_super(mddev
, rdev
)) {
2270 printk(KERN_WARNING
"md: kicking non-fresh %s"
2272 bdevname(rdev
->bdev
,b
));
2273 kick_rdev_from_array(rdev
);
2276 if (mddev
->level
== LEVEL_MULTIPATH
) {
2277 rdev
->desc_nr
= i
++;
2278 rdev
->raid_disk
= rdev
->desc_nr
;
2279 set_bit(In_sync
, &rdev
->flags
);
2280 } else if (rdev
->raid_disk
>= mddev
->raid_disks
) {
2281 rdev
->raid_disk
= -1;
2282 clear_bit(In_sync
, &rdev
->flags
);
2288 if (mddev
->recovery_cp
!= MaxSector
&&
2290 printk(KERN_ERR
"md: %s: raid array is not clean"
2291 " -- starting background reconstruction\n",
2297 safe_delay_show(mddev_t
*mddev
, char *page
)
2299 int msec
= (mddev
->safemode_delay
*1000)/HZ
;
2300 return sprintf(page
, "%d.%03d\n", msec
/1000, msec
%1000);
2303 safe_delay_store(mddev_t
*mddev
, const char *cbuf
, size_t len
)
2311 /* remove a period, and count digits after it */
2312 if (len
>= sizeof(buf
))
2314 strlcpy(buf
, cbuf
, len
);
2316 for (i
=0; i
<len
; i
++) {
2318 if (isdigit(buf
[i
])) {
2323 } else if (buf
[i
] == '.') {
2328 msec
= simple_strtoul(buf
, &e
, 10);
2329 if (e
== buf
|| (*e
&& *e
!= '\n'))
2331 msec
= (msec
* 1000) / scale
;
2333 mddev
->safemode_delay
= 0;
2335 mddev
->safemode_delay
= (msec
*HZ
)/1000;
2336 if (mddev
->safemode_delay
== 0)
2337 mddev
->safemode_delay
= 1;
2341 static struct md_sysfs_entry md_safe_delay
=
2342 __ATTR(safe_mode_delay
, S_IRUGO
|S_IWUSR
,safe_delay_show
, safe_delay_store
);
2345 level_show(mddev_t
*mddev
, char *page
)
2347 struct mdk_personality
*p
= mddev
->pers
;
2349 return sprintf(page
, "%s\n", p
->name
);
2350 else if (mddev
->clevel
[0])
2351 return sprintf(page
, "%s\n", mddev
->clevel
);
2352 else if (mddev
->level
!= LEVEL_NONE
)
2353 return sprintf(page
, "%d\n", mddev
->level
);
2359 level_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2366 if (len
>= sizeof(mddev
->clevel
))
2368 strncpy(mddev
->clevel
, buf
, len
);
2369 if (mddev
->clevel
[len
-1] == '\n')
2371 mddev
->clevel
[len
] = 0;
2372 mddev
->level
= LEVEL_NONE
;
2376 static struct md_sysfs_entry md_level
=
2377 __ATTR(level
, S_IRUGO
|S_IWUSR
, level_show
, level_store
);
2381 layout_show(mddev_t
*mddev
, char *page
)
2383 /* just a number, not meaningful for all levels */
2384 if (mddev
->reshape_position
!= MaxSector
&&
2385 mddev
->layout
!= mddev
->new_layout
)
2386 return sprintf(page
, "%d (%d)\n",
2387 mddev
->new_layout
, mddev
->layout
);
2388 return sprintf(page
, "%d\n", mddev
->layout
);
2392 layout_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2395 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2397 if (!*buf
|| (*e
&& *e
!= '\n'))
2402 if (mddev
->reshape_position
!= MaxSector
)
2403 mddev
->new_layout
= n
;
2408 static struct md_sysfs_entry md_layout
=
2409 __ATTR(layout
, S_IRUGO
|S_IWUSR
, layout_show
, layout_store
);
2413 raid_disks_show(mddev_t
*mddev
, char *page
)
2415 if (mddev
->raid_disks
== 0)
2417 if (mddev
->reshape_position
!= MaxSector
&&
2418 mddev
->delta_disks
!= 0)
2419 return sprintf(page
, "%d (%d)\n", mddev
->raid_disks
,
2420 mddev
->raid_disks
- mddev
->delta_disks
);
2421 return sprintf(page
, "%d\n", mddev
->raid_disks
);
2424 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
);
2427 raid_disks_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2431 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2433 if (!*buf
|| (*e
&& *e
!= '\n'))
2437 rv
= update_raid_disks(mddev
, n
);
2438 else if (mddev
->reshape_position
!= MaxSector
) {
2439 int olddisks
= mddev
->raid_disks
- mddev
->delta_disks
;
2440 mddev
->delta_disks
= n
- olddisks
;
2441 mddev
->raid_disks
= n
;
2443 mddev
->raid_disks
= n
;
2444 return rv
? rv
: len
;
2446 static struct md_sysfs_entry md_raid_disks
=
2447 __ATTR(raid_disks
, S_IRUGO
|S_IWUSR
, raid_disks_show
, raid_disks_store
);
2450 chunk_size_show(mddev_t
*mddev
, char *page
)
2452 if (mddev
->reshape_position
!= MaxSector
&&
2453 mddev
->chunk_size
!= mddev
->new_chunk
)
2454 return sprintf(page
, "%d (%d)\n", mddev
->new_chunk
,
2456 return sprintf(page
, "%d\n", mddev
->chunk_size
);
2460 chunk_size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2462 /* can only set chunk_size if array is not yet active */
2464 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2466 if (!*buf
|| (*e
&& *e
!= '\n'))
2471 else if (mddev
->reshape_position
!= MaxSector
)
2472 mddev
->new_chunk
= n
;
2474 mddev
->chunk_size
= n
;
2477 static struct md_sysfs_entry md_chunk_size
=
2478 __ATTR(chunk_size
, S_IRUGO
|S_IWUSR
, chunk_size_show
, chunk_size_store
);
2481 resync_start_show(mddev_t
*mddev
, char *page
)
2483 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->recovery_cp
);
2487 resync_start_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2490 unsigned long long n
= simple_strtoull(buf
, &e
, 10);
2494 if (!*buf
|| (*e
&& *e
!= '\n'))
2497 mddev
->recovery_cp
= n
;
2500 static struct md_sysfs_entry md_resync_start
=
2501 __ATTR(resync_start
, S_IRUGO
|S_IWUSR
, resync_start_show
, resync_start_store
);
2504 * The array state can be:
2507 * No devices, no size, no level
2508 * Equivalent to STOP_ARRAY ioctl
2510 * May have some settings, but array is not active
2511 * all IO results in error
2512 * When written, doesn't tear down array, but just stops it
2513 * suspended (not supported yet)
2514 * All IO requests will block. The array can be reconfigured.
2515 * Writing this, if accepted, will block until array is quiessent
2517 * no resync can happen. no superblocks get written.
2518 * write requests fail
2520 * like readonly, but behaves like 'clean' on a write request.
2522 * clean - no pending writes, but otherwise active.
2523 * When written to inactive array, starts without resync
2524 * If a write request arrives then
2525 * if metadata is known, mark 'dirty' and switch to 'active'.
2526 * if not known, block and switch to write-pending
2527 * If written to an active array that has pending writes, then fails.
2529 * fully active: IO and resync can be happening.
2530 * When written to inactive array, starts with resync
2533 * clean, but writes are blocked waiting for 'active' to be written.
2536 * like active, but no writes have been seen for a while (100msec).
2539 enum array_state
{ clear
, inactive
, suspended
, readonly
, read_auto
, clean
, active
,
2540 write_pending
, active_idle
, bad_word
};
2541 static char *array_states
[] = {
2542 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2543 "write-pending", "active-idle", NULL
};
2545 static int match_word(const char *word
, char **list
)
2548 for (n
=0; list
[n
]; n
++)
2549 if (cmd_match(word
, list
[n
]))
2555 array_state_show(mddev_t
*mddev
, char *page
)
2557 enum array_state st
= inactive
;
2570 else if (test_bit(MD_CHANGE_CLEAN
, &mddev
->flags
))
2572 else if (mddev
->safemode
)
2578 if (list_empty(&mddev
->disks
) &&
2579 mddev
->raid_disks
== 0 &&
2585 return sprintf(page
, "%s\n", array_states
[st
]);
2588 static int do_md_stop(mddev_t
* mddev
, int ro
);
2589 static int do_md_run(mddev_t
* mddev
);
2590 static int restart_array(mddev_t
*mddev
);
2593 array_state_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2596 enum array_state st
= match_word(buf
, array_states
);
2601 /* stopping an active array */
2602 if (atomic_read(&mddev
->active
) > 1)
2604 err
= do_md_stop(mddev
, 0);
2607 /* stopping an active array */
2609 if (atomic_read(&mddev
->active
) > 1)
2611 err
= do_md_stop(mddev
, 2);
2613 err
= 0; /* already inactive */
2616 break; /* not supported yet */
2619 err
= do_md_stop(mddev
, 1);
2622 set_disk_ro(mddev
->gendisk
, 1);
2623 err
= do_md_run(mddev
);
2629 err
= do_md_stop(mddev
, 1);
2631 err
= restart_array(mddev
);
2634 set_disk_ro(mddev
->gendisk
, 0);
2638 err
= do_md_run(mddev
);
2643 restart_array(mddev
);
2644 spin_lock_irq(&mddev
->write_lock
);
2645 if (atomic_read(&mddev
->writes_pending
) == 0) {
2646 if (mddev
->in_sync
== 0) {
2648 if (mddev
->safemode
== 1)
2649 mddev
->safemode
= 0;
2650 if (mddev
->persistent
)
2651 set_bit(MD_CHANGE_CLEAN
,
2657 spin_unlock_irq(&mddev
->write_lock
);
2660 mddev
->recovery_cp
= MaxSector
;
2661 err
= do_md_run(mddev
);
2666 restart_array(mddev
);
2667 if (mddev
->external
)
2668 clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
2669 wake_up(&mddev
->sb_wait
);
2673 set_disk_ro(mddev
->gendisk
, 0);
2674 err
= do_md_run(mddev
);
2679 /* these cannot be set */
2687 static struct md_sysfs_entry md_array_state
=
2688 __ATTR(array_state
, S_IRUGO
|S_IWUSR
, array_state_show
, array_state_store
);
2691 null_show(mddev_t
*mddev
, char *page
)
2697 new_dev_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2699 /* buf must be %d:%d\n? giving major and minor numbers */
2700 /* The new device is added to the array.
2701 * If the array has a persistent superblock, we read the
2702 * superblock to initialise info and check validity.
2703 * Otherwise, only checking done is that in bind_rdev_to_array,
2704 * which mainly checks size.
2707 int major
= simple_strtoul(buf
, &e
, 10);
2713 if (!*buf
|| *e
!= ':' || !e
[1] || e
[1] == '\n')
2715 minor
= simple_strtoul(e
+1, &e
, 10);
2716 if (*e
&& *e
!= '\n')
2718 dev
= MKDEV(major
, minor
);
2719 if (major
!= MAJOR(dev
) ||
2720 minor
!= MINOR(dev
))
2724 if (mddev
->persistent
) {
2725 rdev
= md_import_device(dev
, mddev
->major_version
,
2726 mddev
->minor_version
);
2727 if (!IS_ERR(rdev
) && !list_empty(&mddev
->disks
)) {
2728 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
2729 mdk_rdev_t
, same_set
);
2730 err
= super_types
[mddev
->major_version
]
2731 .load_super(rdev
, rdev0
, mddev
->minor_version
);
2735 } else if (mddev
->external
)
2736 rdev
= md_import_device(dev
, -2, -1);
2738 rdev
= md_import_device(dev
, -1, -1);
2741 return PTR_ERR(rdev
);
2742 err
= bind_rdev_to_array(rdev
, mddev
);
2746 return err
? err
: len
;
2749 static struct md_sysfs_entry md_new_device
=
2750 __ATTR(new_dev
, S_IWUSR
, null_show
, new_dev_store
);
2753 bitmap_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2756 unsigned long chunk
, end_chunk
;
2760 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
2762 chunk
= end_chunk
= simple_strtoul(buf
, &end
, 0);
2763 if (buf
== end
) break;
2764 if (*end
== '-') { /* range */
2766 end_chunk
= simple_strtoul(buf
, &end
, 0);
2767 if (buf
== end
) break;
2769 if (*end
&& !isspace(*end
)) break;
2770 bitmap_dirty_bits(mddev
->bitmap
, chunk
, end_chunk
);
2772 while (isspace(*buf
)) buf
++;
2774 bitmap_unplug(mddev
->bitmap
); /* flush the bits to disk */
2779 static struct md_sysfs_entry md_bitmap
=
2780 __ATTR(bitmap_set_bits
, S_IWUSR
, null_show
, bitmap_store
);
2783 size_show(mddev_t
*mddev
, char *page
)
2785 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->size
);
2788 static int update_size(mddev_t
*mddev
, unsigned long size
);
2791 size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2793 /* If array is inactive, we can reduce the component size, but
2794 * not increase it (except from 0).
2795 * If array is active, we can try an on-line resize
2799 unsigned long long size
= simple_strtoull(buf
, &e
, 10);
2800 if (!*buf
|| *buf
== '\n' ||
2805 err
= update_size(mddev
, size
);
2806 md_update_sb(mddev
, 1);
2808 if (mddev
->size
== 0 ||
2814 return err
? err
: len
;
2817 static struct md_sysfs_entry md_size
=
2818 __ATTR(component_size
, S_IRUGO
|S_IWUSR
, size_show
, size_store
);
2823 * 'none' for arrays with no metadata (good luck...)
2824 * 'external' for arrays with externally managed metadata,
2825 * or N.M for internally known formats
2828 metadata_show(mddev_t
*mddev
, char *page
)
2830 if (mddev
->persistent
)
2831 return sprintf(page
, "%d.%d\n",
2832 mddev
->major_version
, mddev
->minor_version
);
2833 else if (mddev
->external
)
2834 return sprintf(page
, "external:%s\n", mddev
->metadata_type
);
2836 return sprintf(page
, "none\n");
2840 metadata_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2844 if (!list_empty(&mddev
->disks
))
2847 if (cmd_match(buf
, "none")) {
2848 mddev
->persistent
= 0;
2849 mddev
->external
= 0;
2850 mddev
->major_version
= 0;
2851 mddev
->minor_version
= 90;
2854 if (strncmp(buf
, "external:", 9) == 0) {
2855 size_t namelen
= len
-9;
2856 if (namelen
>= sizeof(mddev
->metadata_type
))
2857 namelen
= sizeof(mddev
->metadata_type
)-1;
2858 strncpy(mddev
->metadata_type
, buf
+9, namelen
);
2859 mddev
->metadata_type
[namelen
] = 0;
2860 if (namelen
&& mddev
->metadata_type
[namelen
-1] == '\n')
2861 mddev
->metadata_type
[--namelen
] = 0;
2862 mddev
->persistent
= 0;
2863 mddev
->external
= 1;
2864 mddev
->major_version
= 0;
2865 mddev
->minor_version
= 90;
2868 major
= simple_strtoul(buf
, &e
, 10);
2869 if (e
==buf
|| *e
!= '.')
2872 minor
= simple_strtoul(buf
, &e
, 10);
2873 if (e
==buf
|| (*e
&& *e
!= '\n') )
2875 if (major
>= ARRAY_SIZE(super_types
) || super_types
[major
].name
== NULL
)
2877 mddev
->major_version
= major
;
2878 mddev
->minor_version
= minor
;
2879 mddev
->persistent
= 1;
2880 mddev
->external
= 0;
2884 static struct md_sysfs_entry md_metadata
=
2885 __ATTR(metadata_version
, S_IRUGO
|S_IWUSR
, metadata_show
, metadata_store
);
2888 action_show(mddev_t
*mddev
, char *page
)
2890 char *type
= "idle";
2891 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
2892 (!mddev
->ro
&& test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))) {
2893 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
2895 else if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
2896 if (!test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
2898 else if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
2905 return sprintf(page
, "%s\n", type
);
2909 action_store(mddev_t
*mddev
, const char *page
, size_t len
)
2911 if (!mddev
->pers
|| !mddev
->pers
->sync_request
)
2914 if (cmd_match(page
, "idle")) {
2915 if (mddev
->sync_thread
) {
2916 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
2917 md_unregister_thread(mddev
->sync_thread
);
2918 mddev
->sync_thread
= NULL
;
2919 mddev
->recovery
= 0;
2921 } else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
2922 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
2924 else if (cmd_match(page
, "resync") || cmd_match(page
, "recover"))
2925 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
2926 else if (cmd_match(page
, "reshape")) {
2928 if (mddev
->pers
->start_reshape
== NULL
)
2930 err
= mddev
->pers
->start_reshape(mddev
);
2934 if (cmd_match(page
, "check"))
2935 set_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
2936 else if (!cmd_match(page
, "repair"))
2938 set_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
2939 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
2941 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
2942 md_wakeup_thread(mddev
->thread
);
2947 mismatch_cnt_show(mddev_t
*mddev
, char *page
)
2949 return sprintf(page
, "%llu\n",
2950 (unsigned long long) mddev
->resync_mismatches
);
2953 static struct md_sysfs_entry md_scan_mode
=
2954 __ATTR(sync_action
, S_IRUGO
|S_IWUSR
, action_show
, action_store
);
2957 static struct md_sysfs_entry md_mismatches
= __ATTR_RO(mismatch_cnt
);
2960 sync_min_show(mddev_t
*mddev
, char *page
)
2962 return sprintf(page
, "%d (%s)\n", speed_min(mddev
),
2963 mddev
->sync_speed_min
? "local": "system");
2967 sync_min_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2971 if (strncmp(buf
, "system", 6)==0) {
2972 mddev
->sync_speed_min
= 0;
2975 min
= simple_strtoul(buf
, &e
, 10);
2976 if (buf
== e
|| (*e
&& *e
!= '\n') || min
<= 0)
2978 mddev
->sync_speed_min
= min
;
2982 static struct md_sysfs_entry md_sync_min
=
2983 __ATTR(sync_speed_min
, S_IRUGO
|S_IWUSR
, sync_min_show
, sync_min_store
);
2986 sync_max_show(mddev_t
*mddev
, char *page
)
2988 return sprintf(page
, "%d (%s)\n", speed_max(mddev
),
2989 mddev
->sync_speed_max
? "local": "system");
2993 sync_max_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2997 if (strncmp(buf
, "system", 6)==0) {
2998 mddev
->sync_speed_max
= 0;
3001 max
= simple_strtoul(buf
, &e
, 10);
3002 if (buf
== e
|| (*e
&& *e
!= '\n') || max
<= 0)
3004 mddev
->sync_speed_max
= max
;
3008 static struct md_sysfs_entry md_sync_max
=
3009 __ATTR(sync_speed_max
, S_IRUGO
|S_IWUSR
, sync_max_show
, sync_max_store
);
3012 degraded_show(mddev_t
*mddev
, char *page
)
3014 return sprintf(page
, "%d\n", mddev
->degraded
);
3016 static struct md_sysfs_entry md_degraded
= __ATTR_RO(degraded
);
3019 sync_force_parallel_show(mddev_t
*mddev
, char *page
)
3021 return sprintf(page
, "%d\n", mddev
->parallel_resync
);
3025 sync_force_parallel_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3029 if (strict_strtol(buf
, 10, &n
))
3032 if (n
!= 0 && n
!= 1)
3035 mddev
->parallel_resync
= n
;
3037 if (mddev
->sync_thread
)
3038 wake_up(&resync_wait
);
3043 /* force parallel resync, even with shared block devices */
3044 static struct md_sysfs_entry md_sync_force_parallel
=
3045 __ATTR(sync_force_parallel
, S_IRUGO
|S_IWUSR
,
3046 sync_force_parallel_show
, sync_force_parallel_store
);
3049 sync_speed_show(mddev_t
*mddev
, char *page
)
3051 unsigned long resync
, dt
, db
;
3052 resync
= (mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
));
3053 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
3055 db
= resync
- (mddev
->resync_mark_cnt
);
3056 return sprintf(page
, "%ld\n", db
/dt
/2); /* K/sec */
3059 static struct md_sysfs_entry md_sync_speed
= __ATTR_RO(sync_speed
);
3062 sync_completed_show(mddev_t
*mddev
, char *page
)
3064 unsigned long max_blocks
, resync
;
3066 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
3067 max_blocks
= mddev
->resync_max_sectors
;
3069 max_blocks
= mddev
->size
<< 1;
3071 resync
= (mddev
->curr_resync
- atomic_read(&mddev
->recovery_active
));
3072 return sprintf(page
, "%lu / %lu\n", resync
, max_blocks
);
3075 static struct md_sysfs_entry md_sync_completed
= __ATTR_RO(sync_completed
);
3078 max_sync_show(mddev_t
*mddev
, char *page
)
3080 if (mddev
->resync_max
== MaxSector
)
3081 return sprintf(page
, "max\n");
3083 return sprintf(page
, "%llu\n",
3084 (unsigned long long)mddev
->resync_max
);
3087 max_sync_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3089 if (strncmp(buf
, "max", 3) == 0)
3090 mddev
->resync_max
= MaxSector
;
3093 unsigned long long max
= simple_strtoull(buf
, &ep
, 10);
3094 if (ep
== buf
|| (*ep
!= 0 && *ep
!= '\n'))
3096 if (max
< mddev
->resync_max
&&
3097 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3100 /* Must be a multiple of chunk_size */
3101 if (mddev
->chunk_size
) {
3102 if (max
& (sector_t
)((mddev
->chunk_size
>>9)-1))
3105 mddev
->resync_max
= max
;
3107 wake_up(&mddev
->recovery_wait
);
3111 static struct md_sysfs_entry md_max_sync
=
3112 __ATTR(sync_max
, S_IRUGO
|S_IWUSR
, max_sync_show
, max_sync_store
);
3115 suspend_lo_show(mddev_t
*mddev
, char *page
)
3117 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_lo
);
3121 suspend_lo_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3124 unsigned long long new = simple_strtoull(buf
, &e
, 10);
3126 if (mddev
->pers
->quiesce
== NULL
)
3128 if (buf
== e
|| (*e
&& *e
!= '\n'))
3130 if (new >= mddev
->suspend_hi
||
3131 (new > mddev
->suspend_lo
&& new < mddev
->suspend_hi
)) {
3132 mddev
->suspend_lo
= new;
3133 mddev
->pers
->quiesce(mddev
, 2);
3138 static struct md_sysfs_entry md_suspend_lo
=
3139 __ATTR(suspend_lo
, S_IRUGO
|S_IWUSR
, suspend_lo_show
, suspend_lo_store
);
3143 suspend_hi_show(mddev_t
*mddev
, char *page
)
3145 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_hi
);
3149 suspend_hi_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3152 unsigned long long new = simple_strtoull(buf
, &e
, 10);
3154 if (mddev
->pers
->quiesce
== NULL
)
3156 if (buf
== e
|| (*e
&& *e
!= '\n'))
3158 if ((new <= mddev
->suspend_lo
&& mddev
->suspend_lo
>= mddev
->suspend_hi
) ||
3159 (new > mddev
->suspend_lo
&& new > mddev
->suspend_hi
)) {
3160 mddev
->suspend_hi
= new;
3161 mddev
->pers
->quiesce(mddev
, 1);
3162 mddev
->pers
->quiesce(mddev
, 0);
3167 static struct md_sysfs_entry md_suspend_hi
=
3168 __ATTR(suspend_hi
, S_IRUGO
|S_IWUSR
, suspend_hi_show
, suspend_hi_store
);
3171 reshape_position_show(mddev_t
*mddev
, char *page
)
3173 if (mddev
->reshape_position
!= MaxSector
)
3174 return sprintf(page
, "%llu\n",
3175 (unsigned long long)mddev
->reshape_position
);
3176 strcpy(page
, "none\n");
3181 reshape_position_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3184 unsigned long long new = simple_strtoull(buf
, &e
, 10);
3187 if (buf
== e
|| (*e
&& *e
!= '\n'))
3189 mddev
->reshape_position
= new;
3190 mddev
->delta_disks
= 0;
3191 mddev
->new_level
= mddev
->level
;
3192 mddev
->new_layout
= mddev
->layout
;
3193 mddev
->new_chunk
= mddev
->chunk_size
;
3197 static struct md_sysfs_entry md_reshape_position
=
3198 __ATTR(reshape_position
, S_IRUGO
|S_IWUSR
, reshape_position_show
,
3199 reshape_position_store
);
3202 static struct attribute
*md_default_attrs
[] = {
3205 &md_raid_disks
.attr
,
3206 &md_chunk_size
.attr
,
3208 &md_resync_start
.attr
,
3210 &md_new_device
.attr
,
3211 &md_safe_delay
.attr
,
3212 &md_array_state
.attr
,
3213 &md_reshape_position
.attr
,
3217 static struct attribute
*md_redundancy_attrs
[] = {
3219 &md_mismatches
.attr
,
3222 &md_sync_speed
.attr
,
3223 &md_sync_force_parallel
.attr
,
3224 &md_sync_completed
.attr
,
3226 &md_suspend_lo
.attr
,
3227 &md_suspend_hi
.attr
,
3232 static struct attribute_group md_redundancy_group
= {
3234 .attrs
= md_redundancy_attrs
,
3239 md_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
3241 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
3242 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
3247 rv
= mddev_lock(mddev
);
3249 rv
= entry
->show(mddev
, page
);
3250 mddev_unlock(mddev
);
3256 md_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
3257 const char *page
, size_t length
)
3259 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
3260 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
3265 if (!capable(CAP_SYS_ADMIN
))
3267 rv
= mddev_lock(mddev
);
3269 rv
= entry
->store(mddev
, page
, length
);
3270 mddev_unlock(mddev
);
3275 static void md_free(struct kobject
*ko
)
3277 mddev_t
*mddev
= container_of(ko
, mddev_t
, kobj
);
3281 static struct sysfs_ops md_sysfs_ops
= {
3282 .show
= md_attr_show
,
3283 .store
= md_attr_store
,
3285 static struct kobj_type md_ktype
= {
3287 .sysfs_ops
= &md_sysfs_ops
,
3288 .default_attrs
= md_default_attrs
,
3293 static struct kobject
*md_probe(dev_t dev
, int *part
, void *data
)
3295 static DEFINE_MUTEX(disks_mutex
);
3296 mddev_t
*mddev
= mddev_find(dev
);
3297 struct gendisk
*disk
;
3298 int partitioned
= (MAJOR(dev
) != MD_MAJOR
);
3299 int shift
= partitioned
? MdpMinorShift
: 0;
3300 int unit
= MINOR(dev
) >> shift
;
3306 mutex_lock(&disks_mutex
);
3307 if (mddev
->gendisk
) {
3308 mutex_unlock(&disks_mutex
);
3312 disk
= alloc_disk(1 << shift
);
3314 mutex_unlock(&disks_mutex
);
3318 disk
->major
= MAJOR(dev
);
3319 disk
->first_minor
= unit
<< shift
;
3321 sprintf(disk
->disk_name
, "md_d%d", unit
);
3323 sprintf(disk
->disk_name
, "md%d", unit
);
3324 disk
->fops
= &md_fops
;
3325 disk
->private_data
= mddev
;
3326 disk
->queue
= mddev
->queue
;
3328 mddev
->gendisk
= disk
;
3329 mutex_unlock(&disks_mutex
);
3330 error
= kobject_init_and_add(&mddev
->kobj
, &md_ktype
, &disk
->dev
.kobj
,
3333 printk(KERN_WARNING
"md: cannot register %s/md - name in use\n",
3336 kobject_uevent(&mddev
->kobj
, KOBJ_ADD
);
3340 static void md_safemode_timeout(unsigned long data
)
3342 mddev_t
*mddev
= (mddev_t
*) data
;
3344 mddev
->safemode
= 1;
3345 md_wakeup_thread(mddev
->thread
);
3348 static int start_dirty_degraded
;
3350 static int do_md_run(mddev_t
* mddev
)
3354 struct list_head
*tmp
;
3356 struct gendisk
*disk
;
3357 struct mdk_personality
*pers
;
3358 char b
[BDEVNAME_SIZE
];
3360 if (list_empty(&mddev
->disks
))
3361 /* cannot run an array with no devices.. */
3368 * Analyze all RAID superblock(s)
3370 if (!mddev
->raid_disks
) {
3371 if (!mddev
->persistent
)
3376 chunk_size
= mddev
->chunk_size
;
3379 if (chunk_size
> MAX_CHUNK_SIZE
) {
3380 printk(KERN_ERR
"too big chunk_size: %d > %d\n",
3381 chunk_size
, MAX_CHUNK_SIZE
);
3385 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
3387 if ( (1 << ffz(~chunk_size
)) != chunk_size
) {
3388 printk(KERN_ERR
"chunk_size of %d not valid\n", chunk_size
);
3391 if (chunk_size
< PAGE_SIZE
) {
3392 printk(KERN_ERR
"too small chunk_size: %d < %ld\n",
3393 chunk_size
, PAGE_SIZE
);
3397 /* devices must have minimum size of one chunk */
3398 rdev_for_each(rdev
, tmp
, mddev
) {
3399 if (test_bit(Faulty
, &rdev
->flags
))
3401 if (rdev
->size
< chunk_size
/ 1024) {
3403 "md: Dev %s smaller than chunk_size:"
3405 bdevname(rdev
->bdev
,b
),
3406 (unsigned long long)rdev
->size
,
3414 if (mddev
->level
!= LEVEL_NONE
)
3415 request_module("md-level-%d", mddev
->level
);
3416 else if (mddev
->clevel
[0])
3417 request_module("md-%s", mddev
->clevel
);
3421 * Drop all container device buffers, from now on
3422 * the only valid external interface is through the md
3425 rdev_for_each(rdev
, tmp
, mddev
) {
3426 if (test_bit(Faulty
, &rdev
->flags
))
3428 sync_blockdev(rdev
->bdev
);
3429 invalidate_bdev(rdev
->bdev
);
3431 /* perform some consistency tests on the device.
3432 * We don't want the data to overlap the metadata,
3433 * Internal Bitmap issues has handled elsewhere.
3435 if (rdev
->data_offset
< rdev
->sb_offset
) {
3437 rdev
->data_offset
+ mddev
->size
*2
3438 > rdev
->sb_offset
*2) {
3439 printk("md: %s: data overlaps metadata\n",
3444 if (rdev
->sb_offset
*2 + rdev
->sb_size
/512
3445 > rdev
->data_offset
) {
3446 printk("md: %s: metadata overlaps data\n",
3453 md_probe(mddev
->unit
, NULL
, NULL
);
3454 disk
= mddev
->gendisk
;
3458 spin_lock(&pers_lock
);
3459 pers
= find_pers(mddev
->level
, mddev
->clevel
);
3460 if (!pers
|| !try_module_get(pers
->owner
)) {
3461 spin_unlock(&pers_lock
);
3462 if (mddev
->level
!= LEVEL_NONE
)
3463 printk(KERN_WARNING
"md: personality for level %d is not loaded!\n",
3466 printk(KERN_WARNING
"md: personality for level %s is not loaded!\n",
3471 spin_unlock(&pers_lock
);
3472 mddev
->level
= pers
->level
;
3473 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
3475 if (mddev
->reshape_position
!= MaxSector
&&
3476 pers
->start_reshape
== NULL
) {
3477 /* This personality cannot handle reshaping... */
3479 module_put(pers
->owner
);
3483 if (pers
->sync_request
) {
3484 /* Warn if this is a potentially silly
3487 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
3489 struct list_head
*tmp2
;
3491 rdev_for_each(rdev
, tmp
, mddev
) {
3492 rdev_for_each(rdev2
, tmp2
, mddev
) {
3494 rdev
->bdev
->bd_contains
==
3495 rdev2
->bdev
->bd_contains
) {
3497 "%s: WARNING: %s appears to be"
3498 " on the same physical disk as"
3501 bdevname(rdev
->bdev
,b
),
3502 bdevname(rdev2
->bdev
,b2
));
3509 "True protection against single-disk"
3510 " failure might be compromised.\n");
3513 mddev
->recovery
= 0;
3514 mddev
->resync_max_sectors
= mddev
->size
<< 1; /* may be over-ridden by personality */
3515 mddev
->barriers_work
= 1;
3516 mddev
->ok_start_degraded
= start_dirty_degraded
;
3519 mddev
->ro
= 2; /* read-only, but switch on first write */
3521 err
= mddev
->pers
->run(mddev
);
3522 if (!err
&& mddev
->pers
->sync_request
) {
3523 err
= bitmap_create(mddev
);
3525 printk(KERN_ERR
"%s: failed to create bitmap (%d)\n",
3526 mdname(mddev
), err
);
3527 mddev
->pers
->stop(mddev
);
3531 printk(KERN_ERR
"md: pers->run() failed ...\n");
3532 module_put(mddev
->pers
->owner
);
3534 bitmap_destroy(mddev
);
3537 if (mddev
->pers
->sync_request
) {
3538 if (sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
3540 "md: cannot register extra attributes for %s\n",
3542 } else if (mddev
->ro
== 2) /* auto-readonly not meaningful */
3545 atomic_set(&mddev
->writes_pending
,0);
3546 mddev
->safemode
= 0;
3547 mddev
->safemode_timer
.function
= md_safemode_timeout
;
3548 mddev
->safemode_timer
.data
= (unsigned long) mddev
;
3549 mddev
->safemode_delay
= (200 * HZ
)/1000 +1; /* 200 msec delay */
3552 rdev_for_each(rdev
, tmp
, mddev
)
3553 if (rdev
->raid_disk
>= 0) {
3555 sprintf(nm
, "rd%d", rdev
->raid_disk
);
3556 if (sysfs_create_link(&mddev
->kobj
, &rdev
->kobj
, nm
))
3557 printk("md: cannot register %s for %s\n",
3561 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3564 md_update_sb(mddev
, 0);
3566 set_capacity(disk
, mddev
->array_size
<<1);
3568 /* If we call blk_queue_make_request here, it will
3569 * re-initialise max_sectors etc which may have been
3570 * refined inside -> run. So just set the bits we need to set.
3571 * Most initialisation happended when we called
3572 * blk_queue_make_request(..., md_fail_request)
3575 mddev
->queue
->queuedata
= mddev
;
3576 mddev
->queue
->make_request_fn
= mddev
->pers
->make_request
;
3578 /* If there is a partially-recovered drive we need to
3579 * start recovery here. If we leave it to md_check_recovery,
3580 * it will remove the drives and not do the right thing
3582 if (mddev
->degraded
&& !mddev
->sync_thread
) {
3583 struct list_head
*rtmp
;
3585 rdev_for_each(rdev
, rtmp
, mddev
)
3586 if (rdev
->raid_disk
>= 0 &&
3587 !test_bit(In_sync
, &rdev
->flags
) &&
3588 !test_bit(Faulty
, &rdev
->flags
))
3589 /* complete an interrupted recovery */
3591 if (spares
&& mddev
->pers
->sync_request
) {
3592 mddev
->recovery
= 0;
3593 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
3594 mddev
->sync_thread
= md_register_thread(md_do_sync
,
3597 if (!mddev
->sync_thread
) {
3598 printk(KERN_ERR
"%s: could not start resync"
3601 /* leave the spares where they are, it shouldn't hurt */
3602 mddev
->recovery
= 0;
3606 md_wakeup_thread(mddev
->thread
);
3607 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
3610 md_new_event(mddev
);
3611 kobject_uevent(&mddev
->gendisk
->dev
.kobj
, KOBJ_CHANGE
);
3615 static int restart_array(mddev_t
*mddev
)
3617 struct gendisk
*disk
= mddev
->gendisk
;
3621 * Complain if it has no devices
3624 if (list_empty(&mddev
->disks
))
3632 mddev
->safemode
= 0;
3634 set_disk_ro(disk
, 0);
3636 printk(KERN_INFO
"md: %s switched to read-write mode.\n",
3639 * Kick recovery or resync if necessary
3641 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3642 md_wakeup_thread(mddev
->thread
);
3643 md_wakeup_thread(mddev
->sync_thread
);
3652 /* similar to deny_write_access, but accounts for our holding a reference
3653 * to the file ourselves */
3654 static int deny_bitmap_write_access(struct file
* file
)
3656 struct inode
*inode
= file
->f_mapping
->host
;
3658 spin_lock(&inode
->i_lock
);
3659 if (atomic_read(&inode
->i_writecount
) > 1) {
3660 spin_unlock(&inode
->i_lock
);
3663 atomic_set(&inode
->i_writecount
, -1);
3664 spin_unlock(&inode
->i_lock
);
3669 static void restore_bitmap_write_access(struct file
*file
)
3671 struct inode
*inode
= file
->f_mapping
->host
;
3673 spin_lock(&inode
->i_lock
);
3674 atomic_set(&inode
->i_writecount
, 1);
3675 spin_unlock(&inode
->i_lock
);
3679 * 0 - completely stop and dis-assemble array
3680 * 1 - switch to readonly
3681 * 2 - stop but do not disassemble array
3683 static int do_md_stop(mddev_t
* mddev
, int mode
)
3686 struct gendisk
*disk
= mddev
->gendisk
;
3689 if (atomic_read(&mddev
->active
)>2) {
3690 printk("md: %s still in use.\n",mdname(mddev
));
3694 if (mddev
->sync_thread
) {
3695 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
3696 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
3697 md_unregister_thread(mddev
->sync_thread
);
3698 mddev
->sync_thread
= NULL
;
3701 del_timer_sync(&mddev
->safemode_timer
);
3703 invalidate_partition(disk
, 0);
3706 case 1: /* readonly */
3712 case 0: /* disassemble */
3714 bitmap_flush(mddev
);
3715 md_super_wait(mddev
);
3717 set_disk_ro(disk
, 0);
3718 blk_queue_make_request(mddev
->queue
, md_fail_request
);
3719 mddev
->pers
->stop(mddev
);
3720 mddev
->queue
->merge_bvec_fn
= NULL
;
3721 mddev
->queue
->unplug_fn
= NULL
;
3722 mddev
->queue
->backing_dev_info
.congested_fn
= NULL
;
3723 if (mddev
->pers
->sync_request
)
3724 sysfs_remove_group(&mddev
->kobj
, &md_redundancy_group
);
3726 module_put(mddev
->pers
->owner
);
3728 /* tell userspace to handle 'inactive' */
3729 sysfs_notify(&mddev
->kobj
, NULL
, "array_state");
3731 set_capacity(disk
, 0);
3737 if (!mddev
->in_sync
|| mddev
->flags
) {
3738 /* mark array as shutdown cleanly */
3740 md_update_sb(mddev
, 1);
3743 set_disk_ro(disk
, 1);
3744 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
3748 * Free resources if final stop
3752 struct list_head
*tmp
;
3754 printk(KERN_INFO
"md: %s stopped.\n", mdname(mddev
));
3756 bitmap_destroy(mddev
);
3757 if (mddev
->bitmap_file
) {
3758 restore_bitmap_write_access(mddev
->bitmap_file
);
3759 fput(mddev
->bitmap_file
);
3760 mddev
->bitmap_file
= NULL
;
3762 mddev
->bitmap_offset
= 0;
3764 rdev_for_each(rdev
, tmp
, mddev
)
3765 if (rdev
->raid_disk
>= 0) {
3767 sprintf(nm
, "rd%d", rdev
->raid_disk
);
3768 sysfs_remove_link(&mddev
->kobj
, nm
);
3771 /* make sure all md_delayed_delete calls have finished */
3772 flush_scheduled_work();
3774 export_array(mddev
);
3776 mddev
->array_size
= 0;
3778 mddev
->raid_disks
= 0;
3779 mddev
->recovery_cp
= 0;
3780 mddev
->resync_max
= MaxSector
;
3781 mddev
->reshape_position
= MaxSector
;
3782 mddev
->external
= 0;
3783 mddev
->persistent
= 0;
3784 mddev
->level
= LEVEL_NONE
;
3785 mddev
->clevel
[0] = 0;
3788 mddev
->metadata_type
[0] = 0;
3789 mddev
->chunk_size
= 0;
3790 mddev
->ctime
= mddev
->utime
= 0;
3792 mddev
->max_disks
= 0;
3794 mddev
->delta_disks
= 0;
3795 mddev
->new_level
= LEVEL_NONE
;
3796 mddev
->new_layout
= 0;
3797 mddev
->new_chunk
= 0;
3798 mddev
->curr_resync
= 0;
3799 mddev
->resync_mismatches
= 0;
3800 mddev
->suspend_lo
= mddev
->suspend_hi
= 0;
3801 mddev
->sync_speed_min
= mddev
->sync_speed_max
= 0;
3802 mddev
->recovery
= 0;
3805 mddev
->degraded
= 0;
3806 mddev
->barriers_work
= 0;
3807 mddev
->safemode
= 0;
3809 } else if (mddev
->pers
)
3810 printk(KERN_INFO
"md: %s switched to read-only mode.\n",
3813 md_new_event(mddev
);
3819 static void autorun_array(mddev_t
*mddev
)
3822 struct list_head
*tmp
;
3825 if (list_empty(&mddev
->disks
))
3828 printk(KERN_INFO
"md: running: ");
3830 rdev_for_each(rdev
, tmp
, mddev
) {
3831 char b
[BDEVNAME_SIZE
];
3832 printk("<%s>", bdevname(rdev
->bdev
,b
));
3836 err
= do_md_run (mddev
);
3838 printk(KERN_WARNING
"md: do_md_run() returned %d\n", err
);
3839 do_md_stop (mddev
, 0);
3844 * lets try to run arrays based on all disks that have arrived
3845 * until now. (those are in pending_raid_disks)
3847 * the method: pick the first pending disk, collect all disks with
3848 * the same UUID, remove all from the pending list and put them into
3849 * the 'same_array' list. Then order this list based on superblock
3850 * update time (freshest comes first), kick out 'old' disks and
3851 * compare superblocks. If everything's fine then run it.
3853 * If "unit" is allocated, then bump its reference count
3855 static void autorun_devices(int part
)
3857 struct list_head
*tmp
;
3858 mdk_rdev_t
*rdev0
, *rdev
;
3860 char b
[BDEVNAME_SIZE
];
3862 printk(KERN_INFO
"md: autorun ...\n");
3863 while (!list_empty(&pending_raid_disks
)) {
3866 LIST_HEAD(candidates
);
3867 rdev0
= list_entry(pending_raid_disks
.next
,
3868 mdk_rdev_t
, same_set
);
3870 printk(KERN_INFO
"md: considering %s ...\n",
3871 bdevname(rdev0
->bdev
,b
));
3872 INIT_LIST_HEAD(&candidates
);
3873 rdev_for_each_list(rdev
, tmp
, pending_raid_disks
)
3874 if (super_90_load(rdev
, rdev0
, 0) >= 0) {
3875 printk(KERN_INFO
"md: adding %s ...\n",
3876 bdevname(rdev
->bdev
,b
));
3877 list_move(&rdev
->same_set
, &candidates
);
3880 * now we have a set of devices, with all of them having
3881 * mostly sane superblocks. It's time to allocate the
3885 dev
= MKDEV(mdp_major
,
3886 rdev0
->preferred_minor
<< MdpMinorShift
);
3887 unit
= MINOR(dev
) >> MdpMinorShift
;
3889 dev
= MKDEV(MD_MAJOR
, rdev0
->preferred_minor
);
3892 if (rdev0
->preferred_minor
!= unit
) {
3893 printk(KERN_INFO
"md: unit number in %s is bad: %d\n",
3894 bdevname(rdev0
->bdev
, b
), rdev0
->preferred_minor
);
3898 md_probe(dev
, NULL
, NULL
);
3899 mddev
= mddev_find(dev
);
3900 if (!mddev
|| !mddev
->gendisk
) {
3904 "md: cannot allocate memory for md drive.\n");
3907 if (mddev_lock(mddev
))
3908 printk(KERN_WARNING
"md: %s locked, cannot run\n",
3910 else if (mddev
->raid_disks
|| mddev
->major_version
3911 || !list_empty(&mddev
->disks
)) {
3913 "md: %s already running, cannot run %s\n",
3914 mdname(mddev
), bdevname(rdev0
->bdev
,b
));
3915 mddev_unlock(mddev
);
3917 printk(KERN_INFO
"md: created %s\n", mdname(mddev
));
3918 mddev
->persistent
= 1;
3919 rdev_for_each_list(rdev
, tmp
, candidates
) {
3920 list_del_init(&rdev
->same_set
);
3921 if (bind_rdev_to_array(rdev
, mddev
))
3924 autorun_array(mddev
);
3925 mddev_unlock(mddev
);
3927 /* on success, candidates will be empty, on error
3930 rdev_for_each_list(rdev
, tmp
, candidates
)
3934 printk(KERN_INFO
"md: ... autorun DONE.\n");
3936 #endif /* !MODULE */
3938 static int get_version(void __user
* arg
)
3942 ver
.major
= MD_MAJOR_VERSION
;
3943 ver
.minor
= MD_MINOR_VERSION
;
3944 ver
.patchlevel
= MD_PATCHLEVEL_VERSION
;
3946 if (copy_to_user(arg
, &ver
, sizeof(ver
)))
3952 static int get_array_info(mddev_t
* mddev
, void __user
* arg
)
3954 mdu_array_info_t info
;
3955 int nr
,working
,active
,failed
,spare
;
3957 struct list_head
*tmp
;
3959 nr
=working
=active
=failed
=spare
=0;
3960 rdev_for_each(rdev
, tmp
, mddev
) {
3962 if (test_bit(Faulty
, &rdev
->flags
))
3966 if (test_bit(In_sync
, &rdev
->flags
))
3973 info
.major_version
= mddev
->major_version
;
3974 info
.minor_version
= mddev
->minor_version
;
3975 info
.patch_version
= MD_PATCHLEVEL_VERSION
;
3976 info
.ctime
= mddev
->ctime
;
3977 info
.level
= mddev
->level
;
3978 info
.size
= mddev
->size
;
3979 if (info
.size
!= mddev
->size
) /* overflow */
3982 info
.raid_disks
= mddev
->raid_disks
;
3983 info
.md_minor
= mddev
->md_minor
;
3984 info
.not_persistent
= !mddev
->persistent
;
3986 info
.utime
= mddev
->utime
;
3989 info
.state
= (1<<MD_SB_CLEAN
);
3990 if (mddev
->bitmap
&& mddev
->bitmap_offset
)
3991 info
.state
= (1<<MD_SB_BITMAP_PRESENT
);
3992 info
.active_disks
= active
;
3993 info
.working_disks
= working
;
3994 info
.failed_disks
= failed
;
3995 info
.spare_disks
= spare
;
3997 info
.layout
= mddev
->layout
;
3998 info
.chunk_size
= mddev
->chunk_size
;
4000 if (copy_to_user(arg
, &info
, sizeof(info
)))
4006 static int get_bitmap_file(mddev_t
* mddev
, void __user
* arg
)
4008 mdu_bitmap_file_t
*file
= NULL
; /* too big for stack allocation */
4009 char *ptr
, *buf
= NULL
;
4012 md_allow_write(mddev
);
4014 file
= kmalloc(sizeof(*file
), GFP_KERNEL
);
4018 /* bitmap disabled, zero the first byte and copy out */
4019 if (!mddev
->bitmap
|| !mddev
->bitmap
->file
) {
4020 file
->pathname
[0] = '\0';
4024 buf
= kmalloc(sizeof(file
->pathname
), GFP_KERNEL
);
4028 ptr
= d_path(&mddev
->bitmap
->file
->f_path
, buf
, sizeof(file
->pathname
));
4032 strcpy(file
->pathname
, ptr
);
4036 if (copy_to_user(arg
, file
, sizeof(*file
)))
4044 static int get_disk_info(mddev_t
* mddev
, void __user
* arg
)
4046 mdu_disk_info_t info
;
4050 if (copy_from_user(&info
, arg
, sizeof(info
)))
4055 rdev
= find_rdev_nr(mddev
, nr
);
4057 info
.major
= MAJOR(rdev
->bdev
->bd_dev
);
4058 info
.minor
= MINOR(rdev
->bdev
->bd_dev
);
4059 info
.raid_disk
= rdev
->raid_disk
;
4061 if (test_bit(Faulty
, &rdev
->flags
))
4062 info
.state
|= (1<<MD_DISK_FAULTY
);
4063 else if (test_bit(In_sync
, &rdev
->flags
)) {
4064 info
.state
|= (1<<MD_DISK_ACTIVE
);
4065 info
.state
|= (1<<MD_DISK_SYNC
);
4067 if (test_bit(WriteMostly
, &rdev
->flags
))
4068 info
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
4070 info
.major
= info
.minor
= 0;
4071 info
.raid_disk
= -1;
4072 info
.state
= (1<<MD_DISK_REMOVED
);
4075 if (copy_to_user(arg
, &info
, sizeof(info
)))
4081 static int add_new_disk(mddev_t
* mddev
, mdu_disk_info_t
*info
)
4083 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
4085 dev_t dev
= MKDEV(info
->major
,info
->minor
);
4087 if (info
->major
!= MAJOR(dev
) || info
->minor
!= MINOR(dev
))
4090 if (!mddev
->raid_disks
) {
4092 /* expecting a device which has a superblock */
4093 rdev
= md_import_device(dev
, mddev
->major_version
, mddev
->minor_version
);
4096 "md: md_import_device returned %ld\n",
4098 return PTR_ERR(rdev
);
4100 if (!list_empty(&mddev
->disks
)) {
4101 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
4102 mdk_rdev_t
, same_set
);
4103 int err
= super_types
[mddev
->major_version
]
4104 .load_super(rdev
, rdev0
, mddev
->minor_version
);
4107 "md: %s has different UUID to %s\n",
4108 bdevname(rdev
->bdev
,b
),
4109 bdevname(rdev0
->bdev
,b2
));
4114 err
= bind_rdev_to_array(rdev
, mddev
);
4121 * add_new_disk can be used once the array is assembled
4122 * to add "hot spares". They must already have a superblock
4127 if (!mddev
->pers
->hot_add_disk
) {
4129 "%s: personality does not support diskops!\n",
4133 if (mddev
->persistent
)
4134 rdev
= md_import_device(dev
, mddev
->major_version
,
4135 mddev
->minor_version
);
4137 rdev
= md_import_device(dev
, -1, -1);
4140 "md: md_import_device returned %ld\n",
4142 return PTR_ERR(rdev
);
4144 /* set save_raid_disk if appropriate */
4145 if (!mddev
->persistent
) {
4146 if (info
->state
& (1<<MD_DISK_SYNC
) &&
4147 info
->raid_disk
< mddev
->raid_disks
)
4148 rdev
->raid_disk
= info
->raid_disk
;
4150 rdev
->raid_disk
= -1;
4152 super_types
[mddev
->major_version
].
4153 validate_super(mddev
, rdev
);
4154 rdev
->saved_raid_disk
= rdev
->raid_disk
;
4156 clear_bit(In_sync
, &rdev
->flags
); /* just to be sure */
4157 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
4158 set_bit(WriteMostly
, &rdev
->flags
);
4160 rdev
->raid_disk
= -1;
4161 err
= bind_rdev_to_array(rdev
, mddev
);
4162 if (!err
&& !mddev
->pers
->hot_remove_disk
) {
4163 /* If there is hot_add_disk but no hot_remove_disk
4164 * then added disks for geometry changes,
4165 * and should be added immediately.
4167 super_types
[mddev
->major_version
].
4168 validate_super(mddev
, rdev
);
4169 err
= mddev
->pers
->hot_add_disk(mddev
, rdev
);
4171 unbind_rdev_from_array(rdev
);
4176 md_update_sb(mddev
, 1);
4177 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4178 md_wakeup_thread(mddev
->thread
);
4182 /* otherwise, add_new_disk is only allowed
4183 * for major_version==0 superblocks
4185 if (mddev
->major_version
!= 0) {
4186 printk(KERN_WARNING
"%s: ADD_NEW_DISK not supported\n",
4191 if (!(info
->state
& (1<<MD_DISK_FAULTY
))) {
4193 rdev
= md_import_device (dev
, -1, 0);
4196 "md: error, md_import_device() returned %ld\n",
4198 return PTR_ERR(rdev
);
4200 rdev
->desc_nr
= info
->number
;
4201 if (info
->raid_disk
< mddev
->raid_disks
)
4202 rdev
->raid_disk
= info
->raid_disk
;
4204 rdev
->raid_disk
= -1;
4206 if (rdev
->raid_disk
< mddev
->raid_disks
)
4207 if (info
->state
& (1<<MD_DISK_SYNC
))
4208 set_bit(In_sync
, &rdev
->flags
);
4210 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
4211 set_bit(WriteMostly
, &rdev
->flags
);
4213 if (!mddev
->persistent
) {
4214 printk(KERN_INFO
"md: nonpersistent superblock ...\n");
4215 rdev
->sb_offset
= rdev
->bdev
->bd_inode
->i_size
>> BLOCK_SIZE_BITS
;
4217 rdev
->sb_offset
= calc_dev_sboffset(rdev
->bdev
);
4218 rdev
->size
= calc_dev_size(rdev
, mddev
->chunk_size
);
4220 err
= bind_rdev_to_array(rdev
, mddev
);
4230 static int hot_remove_disk(mddev_t
* mddev
, dev_t dev
)
4232 char b
[BDEVNAME_SIZE
];
4238 rdev
= find_rdev(mddev
, dev
);
4242 if (rdev
->raid_disk
>= 0)
4245 kick_rdev_from_array(rdev
);
4246 md_update_sb(mddev
, 1);
4247 md_new_event(mddev
);
4251 printk(KERN_WARNING
"md: cannot remove active disk %s from %s ...\n",
4252 bdevname(rdev
->bdev
,b
), mdname(mddev
));
4256 static int hot_add_disk(mddev_t
* mddev
, dev_t dev
)
4258 char b
[BDEVNAME_SIZE
];
4266 if (mddev
->major_version
!= 0) {
4267 printk(KERN_WARNING
"%s: HOT_ADD may only be used with"
4268 " version-0 superblocks.\n",
4272 if (!mddev
->pers
->hot_add_disk
) {
4274 "%s: personality does not support diskops!\n",
4279 rdev
= md_import_device (dev
, -1, 0);
4282 "md: error, md_import_device() returned %ld\n",
4287 if (mddev
->persistent
)
4288 rdev
->sb_offset
= calc_dev_sboffset(rdev
->bdev
);
4291 rdev
->bdev
->bd_inode
->i_size
>> BLOCK_SIZE_BITS
;
4293 size
= calc_dev_size(rdev
, mddev
->chunk_size
);
4296 if (test_bit(Faulty
, &rdev
->flags
)) {
4298 "md: can not hot-add faulty %s disk to %s!\n",
4299 bdevname(rdev
->bdev
,b
), mdname(mddev
));
4303 clear_bit(In_sync
, &rdev
->flags
);
4305 rdev
->saved_raid_disk
= -1;
4306 err
= bind_rdev_to_array(rdev
, mddev
);
4311 * The rest should better be atomic, we can have disk failures
4312 * noticed in interrupt contexts ...
4315 if (rdev
->desc_nr
== mddev
->max_disks
) {
4316 printk(KERN_WARNING
"%s: can not hot-add to full array!\n",
4319 goto abort_unbind_export
;
4322 rdev
->raid_disk
= -1;
4324 md_update_sb(mddev
, 1);
4327 * Kick recovery, maybe this spare has to be added to the
4328 * array immediately.
4330 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4331 md_wakeup_thread(mddev
->thread
);
4332 md_new_event(mddev
);
4335 abort_unbind_export
:
4336 unbind_rdev_from_array(rdev
);
4343 static int set_bitmap_file(mddev_t
*mddev
, int fd
)
4348 if (!mddev
->pers
->quiesce
)
4350 if (mddev
->recovery
|| mddev
->sync_thread
)
4352 /* we should be able to change the bitmap.. */
4358 return -EEXIST
; /* cannot add when bitmap is present */
4359 mddev
->bitmap_file
= fget(fd
);
4361 if (mddev
->bitmap_file
== NULL
) {
4362 printk(KERN_ERR
"%s: error: failed to get bitmap file\n",
4367 err
= deny_bitmap_write_access(mddev
->bitmap_file
);
4369 printk(KERN_ERR
"%s: error: bitmap file is already in use\n",
4371 fput(mddev
->bitmap_file
);
4372 mddev
->bitmap_file
= NULL
;
4375 mddev
->bitmap_offset
= 0; /* file overrides offset */
4376 } else if (mddev
->bitmap
== NULL
)
4377 return -ENOENT
; /* cannot remove what isn't there */
4380 mddev
->pers
->quiesce(mddev
, 1);
4382 err
= bitmap_create(mddev
);
4383 if (fd
< 0 || err
) {
4384 bitmap_destroy(mddev
);
4385 fd
= -1; /* make sure to put the file */
4387 mddev
->pers
->quiesce(mddev
, 0);
4390 if (mddev
->bitmap_file
) {
4391 restore_bitmap_write_access(mddev
->bitmap_file
);
4392 fput(mddev
->bitmap_file
);
4394 mddev
->bitmap_file
= NULL
;
4401 * set_array_info is used two different ways
4402 * The original usage is when creating a new array.
4403 * In this usage, raid_disks is > 0 and it together with
4404 * level, size, not_persistent,layout,chunksize determine the
4405 * shape of the array.
4406 * This will always create an array with a type-0.90.0 superblock.
4407 * The newer usage is when assembling an array.
4408 * In this case raid_disks will be 0, and the major_version field is
4409 * use to determine which style super-blocks are to be found on the devices.
4410 * The minor and patch _version numbers are also kept incase the
4411 * super_block handler wishes to interpret them.
4413 static int set_array_info(mddev_t
* mddev
, mdu_array_info_t
*info
)
4416 if (info
->raid_disks
== 0) {
4417 /* just setting version number for superblock loading */
4418 if (info
->major_version
< 0 ||
4419 info
->major_version
>= ARRAY_SIZE(super_types
) ||
4420 super_types
[info
->major_version
].name
== NULL
) {
4421 /* maybe try to auto-load a module? */
4423 "md: superblock version %d not known\n",
4424 info
->major_version
);
4427 mddev
->major_version
= info
->major_version
;
4428 mddev
->minor_version
= info
->minor_version
;
4429 mddev
->patch_version
= info
->patch_version
;
4430 mddev
->persistent
= !info
->not_persistent
;
4433 mddev
->major_version
= MD_MAJOR_VERSION
;
4434 mddev
->minor_version
= MD_MINOR_VERSION
;
4435 mddev
->patch_version
= MD_PATCHLEVEL_VERSION
;
4436 mddev
->ctime
= get_seconds();
4438 mddev
->level
= info
->level
;
4439 mddev
->clevel
[0] = 0;
4440 mddev
->size
= info
->size
;
4441 mddev
->raid_disks
= info
->raid_disks
;
4442 /* don't set md_minor, it is determined by which /dev/md* was
4445 if (info
->state
& (1<<MD_SB_CLEAN
))
4446 mddev
->recovery_cp
= MaxSector
;
4448 mddev
->recovery_cp
= 0;
4449 mddev
->persistent
= ! info
->not_persistent
;
4450 mddev
->external
= 0;
4452 mddev
->layout
= info
->layout
;
4453 mddev
->chunk_size
= info
->chunk_size
;
4455 mddev
->max_disks
= MD_SB_DISKS
;
4457 if (mddev
->persistent
)
4459 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
4461 mddev
->default_bitmap_offset
= MD_SB_BYTES
>> 9;
4462 mddev
->bitmap_offset
= 0;
4464 mddev
->reshape_position
= MaxSector
;
4467 * Generate a 128 bit UUID
4469 get_random_bytes(mddev
->uuid
, 16);
4471 mddev
->new_level
= mddev
->level
;
4472 mddev
->new_chunk
= mddev
->chunk_size
;
4473 mddev
->new_layout
= mddev
->layout
;
4474 mddev
->delta_disks
= 0;
4479 static int update_size(mddev_t
*mddev
, unsigned long size
)
4483 struct list_head
*tmp
;
4484 int fit
= (size
== 0);
4486 if (mddev
->pers
->resize
== NULL
)
4488 /* The "size" is the amount of each device that is used.
4489 * This can only make sense for arrays with redundancy.
4490 * linear and raid0 always use whatever space is available
4491 * We can only consider changing the size if no resync
4492 * or reconstruction is happening, and if the new size
4493 * is acceptable. It must fit before the sb_offset or,
4494 * if that is <data_offset, it must fit before the
4495 * size of each device.
4496 * If size is zero, we find the largest size that fits.
4498 if (mddev
->sync_thread
)
4500 rdev_for_each(rdev
, tmp
, mddev
) {
4502 avail
= rdev
->size
* 2;
4504 if (fit
&& (size
== 0 || size
> avail
/2))
4506 if (avail
< ((sector_t
)size
<< 1))
4509 rv
= mddev
->pers
->resize(mddev
, (sector_t
)size
*2);
4511 struct block_device
*bdev
;
4513 bdev
= bdget_disk(mddev
->gendisk
, 0);
4515 mutex_lock(&bdev
->bd_inode
->i_mutex
);
4516 i_size_write(bdev
->bd_inode
, (loff_t
)mddev
->array_size
<< 10);
4517 mutex_unlock(&bdev
->bd_inode
->i_mutex
);
4524 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
)
4527 /* change the number of raid disks */
4528 if (mddev
->pers
->check_reshape
== NULL
)
4530 if (raid_disks
<= 0 ||
4531 raid_disks
>= mddev
->max_disks
)
4533 if (mddev
->sync_thread
|| mddev
->reshape_position
!= MaxSector
)
4535 mddev
->delta_disks
= raid_disks
- mddev
->raid_disks
;
4537 rv
= mddev
->pers
->check_reshape(mddev
);
4543 * update_array_info is used to change the configuration of an
4545 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
4546 * fields in the info are checked against the array.
4547 * Any differences that cannot be handled will cause an error.
4548 * Normally, only one change can be managed at a time.
4550 static int update_array_info(mddev_t
*mddev
, mdu_array_info_t
*info
)
4556 /* calculate expected state,ignoring low bits */
4557 if (mddev
->bitmap
&& mddev
->bitmap_offset
)
4558 state
|= (1 << MD_SB_BITMAP_PRESENT
);
4560 if (mddev
->major_version
!= info
->major_version
||
4561 mddev
->minor_version
!= info
->minor_version
||
4562 /* mddev->patch_version != info->patch_version || */
4563 mddev
->ctime
!= info
->ctime
||
4564 mddev
->level
!= info
->level
||
4565 /* mddev->layout != info->layout || */
4566 !mddev
->persistent
!= info
->not_persistent
||
4567 mddev
->chunk_size
!= info
->chunk_size
||
4568 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
4569 ((state
^info
->state
) & 0xfffffe00)
4572 /* Check there is only one change */
4573 if (info
->size
>= 0 && mddev
->size
!= info
->size
) cnt
++;
4574 if (mddev
->raid_disks
!= info
->raid_disks
) cnt
++;
4575 if (mddev
->layout
!= info
->layout
) cnt
++;
4576 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) cnt
++;
4577 if (cnt
== 0) return 0;
4578 if (cnt
> 1) return -EINVAL
;
4580 if (mddev
->layout
!= info
->layout
) {
4582 * we don't need to do anything at the md level, the
4583 * personality will take care of it all.
4585 if (mddev
->pers
->reconfig
== NULL
)
4588 return mddev
->pers
->reconfig(mddev
, info
->layout
, -1);
4590 if (info
->size
>= 0 && mddev
->size
!= info
->size
)
4591 rv
= update_size(mddev
, info
->size
);
4593 if (mddev
->raid_disks
!= info
->raid_disks
)
4594 rv
= update_raid_disks(mddev
, info
->raid_disks
);
4596 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) {
4597 if (mddev
->pers
->quiesce
== NULL
)
4599 if (mddev
->recovery
|| mddev
->sync_thread
)
4601 if (info
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
4602 /* add the bitmap */
4605 if (mddev
->default_bitmap_offset
== 0)
4607 mddev
->bitmap_offset
= mddev
->default_bitmap_offset
;
4608 mddev
->pers
->quiesce(mddev
, 1);
4609 rv
= bitmap_create(mddev
);
4611 bitmap_destroy(mddev
);
4612 mddev
->pers
->quiesce(mddev
, 0);
4614 /* remove the bitmap */
4617 if (mddev
->bitmap
->file
)
4619 mddev
->pers
->quiesce(mddev
, 1);
4620 bitmap_destroy(mddev
);
4621 mddev
->pers
->quiesce(mddev
, 0);
4622 mddev
->bitmap_offset
= 0;
4625 md_update_sb(mddev
, 1);
4629 static int set_disk_faulty(mddev_t
*mddev
, dev_t dev
)
4633 if (mddev
->pers
== NULL
)
4636 rdev
= find_rdev(mddev
, dev
);
4640 md_error(mddev
, rdev
);
4644 static int md_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
4646 mddev_t
*mddev
= bdev
->bd_disk
->private_data
;
4650 geo
->cylinders
= get_capacity(mddev
->gendisk
) / 8;
4654 static int md_ioctl(struct inode
*inode
, struct file
*file
,
4655 unsigned int cmd
, unsigned long arg
)
4658 void __user
*argp
= (void __user
*)arg
;
4659 mddev_t
*mddev
= NULL
;
4661 if (!capable(CAP_SYS_ADMIN
))
4665 * Commands dealing with the RAID driver but not any
4671 err
= get_version(argp
);
4674 case PRINT_RAID_DEBUG
:
4682 autostart_arrays(arg
);
4689 * Commands creating/starting a new array:
4692 mddev
= inode
->i_bdev
->bd_disk
->private_data
;
4699 err
= mddev_lock(mddev
);
4702 "md: ioctl lock interrupted, reason %d, cmd %d\n",
4709 case SET_ARRAY_INFO
:
4711 mdu_array_info_t info
;
4713 memset(&info
, 0, sizeof(info
));
4714 else if (copy_from_user(&info
, argp
, sizeof(info
))) {
4719 err
= update_array_info(mddev
, &info
);
4721 printk(KERN_WARNING
"md: couldn't update"
4722 " array info. %d\n", err
);
4727 if (!list_empty(&mddev
->disks
)) {
4729 "md: array %s already has disks!\n",
4734 if (mddev
->raid_disks
) {
4736 "md: array %s already initialised!\n",
4741 err
= set_array_info(mddev
, &info
);
4743 printk(KERN_WARNING
"md: couldn't set"
4744 " array info. %d\n", err
);
4754 * Commands querying/configuring an existing array:
4756 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
4757 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
4758 if ((!mddev
->raid_disks
&& !mddev
->external
)
4759 && cmd
!= ADD_NEW_DISK
&& cmd
!= STOP_ARRAY
4760 && cmd
!= RUN_ARRAY
&& cmd
!= SET_BITMAP_FILE
4761 && cmd
!= GET_BITMAP_FILE
) {
4767 * Commands even a read-only array can execute:
4771 case GET_ARRAY_INFO
:
4772 err
= get_array_info(mddev
, argp
);
4775 case GET_BITMAP_FILE
:
4776 err
= get_bitmap_file(mddev
, argp
);
4780 err
= get_disk_info(mddev
, argp
);
4783 case RESTART_ARRAY_RW
:
4784 err
= restart_array(mddev
);
4788 err
= do_md_stop (mddev
, 0);
4792 err
= do_md_stop (mddev
, 1);
4796 * We have a problem here : there is no easy way to give a CHS
4797 * virtual geometry. We currently pretend that we have a 2 heads
4798 * 4 sectors (with a BIG number of cylinders...). This drives
4799 * dosfs just mad... ;-)
4804 * The remaining ioctls are changing the state of the
4805 * superblock, so we do not allow them on read-only arrays.
4806 * However non-MD ioctls (e.g. get-size) will still come through
4807 * here and hit the 'default' below, so only disallow
4808 * 'md' ioctls, and switch to rw mode if started auto-readonly.
4810 if (_IOC_TYPE(cmd
) == MD_MAJOR
&&
4811 mddev
->ro
&& mddev
->pers
) {
4812 if (mddev
->ro
== 2) {
4814 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4815 md_wakeup_thread(mddev
->thread
);
4827 mdu_disk_info_t info
;
4828 if (copy_from_user(&info
, argp
, sizeof(info
)))
4831 err
= add_new_disk(mddev
, &info
);
4835 case HOT_REMOVE_DISK
:
4836 err
= hot_remove_disk(mddev
, new_decode_dev(arg
));
4840 err
= hot_add_disk(mddev
, new_decode_dev(arg
));
4843 case SET_DISK_FAULTY
:
4844 err
= set_disk_faulty(mddev
, new_decode_dev(arg
));
4848 err
= do_md_run (mddev
);
4851 case SET_BITMAP_FILE
:
4852 err
= set_bitmap_file(mddev
, (int)arg
);
4862 mddev_unlock(mddev
);
4872 static int md_open(struct inode
*inode
, struct file
*file
)
4875 * Succeed if we can lock the mddev, which confirms that
4876 * it isn't being stopped right now.
4878 mddev_t
*mddev
= inode
->i_bdev
->bd_disk
->private_data
;
4881 if ((err
= mutex_lock_interruptible_nested(&mddev
->reconfig_mutex
, 1)))
4886 mddev_unlock(mddev
);
4888 check_disk_change(inode
->i_bdev
);
4893 static int md_release(struct inode
*inode
, struct file
* file
)
4895 mddev_t
*mddev
= inode
->i_bdev
->bd_disk
->private_data
;
4903 static int md_media_changed(struct gendisk
*disk
)
4905 mddev_t
*mddev
= disk
->private_data
;
4907 return mddev
->changed
;
4910 static int md_revalidate(struct gendisk
*disk
)
4912 mddev_t
*mddev
= disk
->private_data
;
4917 static struct block_device_operations md_fops
=
4919 .owner
= THIS_MODULE
,
4921 .release
= md_release
,
4923 .getgeo
= md_getgeo
,
4924 .media_changed
= md_media_changed
,
4925 .revalidate_disk
= md_revalidate
,
4928 static int md_thread(void * arg
)
4930 mdk_thread_t
*thread
= arg
;
4933 * md_thread is a 'system-thread', it's priority should be very
4934 * high. We avoid resource deadlocks individually in each
4935 * raid personality. (RAID5 does preallocation) We also use RR and
4936 * the very same RT priority as kswapd, thus we will never get
4937 * into a priority inversion deadlock.
4939 * we definitely have to have equal or higher priority than
4940 * bdflush, otherwise bdflush will deadlock if there are too
4941 * many dirty RAID5 blocks.
4944 allow_signal(SIGKILL
);
4945 while (!kthread_should_stop()) {
4947 /* We need to wait INTERRUPTIBLE so that
4948 * we don't add to the load-average.
4949 * That means we need to be sure no signals are
4952 if (signal_pending(current
))
4953 flush_signals(current
);
4955 wait_event_interruptible_timeout
4957 test_bit(THREAD_WAKEUP
, &thread
->flags
)
4958 || kthread_should_stop(),
4961 clear_bit(THREAD_WAKEUP
, &thread
->flags
);
4963 thread
->run(thread
->mddev
);
4969 void md_wakeup_thread(mdk_thread_t
*thread
)
4972 dprintk("md: waking up MD thread %s.\n", thread
->tsk
->comm
);
4973 set_bit(THREAD_WAKEUP
, &thread
->flags
);
4974 wake_up(&thread
->wqueue
);
4978 mdk_thread_t
*md_register_thread(void (*run
) (mddev_t
*), mddev_t
*mddev
,
4981 mdk_thread_t
*thread
;
4983 thread
= kzalloc(sizeof(mdk_thread_t
), GFP_KERNEL
);
4987 init_waitqueue_head(&thread
->wqueue
);
4990 thread
->mddev
= mddev
;
4991 thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
4992 thread
->tsk
= kthread_run(md_thread
, thread
, name
, mdname(thread
->mddev
));
4993 if (IS_ERR(thread
->tsk
)) {
5000 void md_unregister_thread(mdk_thread_t
*thread
)
5002 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread
->tsk
));
5004 kthread_stop(thread
->tsk
);
5008 void md_error(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
5015 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
5018 if (mddev
->external
)
5019 set_bit(Blocked
, &rdev
->flags
);
5021 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5023 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5024 __builtin_return_address(0),__builtin_return_address(1),
5025 __builtin_return_address(2),__builtin_return_address(3));
5029 if (!mddev
->pers
->error_handler
)
5031 mddev
->pers
->error_handler(mddev
,rdev
);
5032 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5033 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5034 md_wakeup_thread(mddev
->thread
);
5035 md_new_event_inintr(mddev
);
5038 /* seq_file implementation /proc/mdstat */
5040 static void status_unused(struct seq_file
*seq
)
5044 struct list_head
*tmp
;
5046 seq_printf(seq
, "unused devices: ");
5048 rdev_for_each_list(rdev
, tmp
, pending_raid_disks
) {
5049 char b
[BDEVNAME_SIZE
];
5051 seq_printf(seq
, "%s ",
5052 bdevname(rdev
->bdev
,b
));
5055 seq_printf(seq
, "<none>");
5057 seq_printf(seq
, "\n");
5061 static void status_resync(struct seq_file
*seq
, mddev_t
* mddev
)
5063 sector_t max_blocks
, resync
, res
;
5064 unsigned long dt
, db
, rt
;
5066 unsigned int per_milli
;
5068 resync
= (mddev
->curr_resync
- atomic_read(&mddev
->recovery_active
))/2;
5070 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
5071 max_blocks
= mddev
->resync_max_sectors
>> 1;
5073 max_blocks
= mddev
->size
;
5076 * Should not happen.
5082 /* Pick 'scale' such that (resync>>scale)*1000 will fit
5083 * in a sector_t, and (max_blocks>>scale) will fit in a
5084 * u32, as those are the requirements for sector_div.
5085 * Thus 'scale' must be at least 10
5088 if (sizeof(sector_t
) > sizeof(unsigned long)) {
5089 while ( max_blocks
/2 > (1ULL<<(scale
+32)))
5092 res
= (resync
>>scale
)*1000;
5093 sector_div(res
, (u32
)((max_blocks
>>scale
)+1));
5097 int i
, x
= per_milli
/50, y
= 20-x
;
5098 seq_printf(seq
, "[");
5099 for (i
= 0; i
< x
; i
++)
5100 seq_printf(seq
, "=");
5101 seq_printf(seq
, ">");
5102 for (i
= 0; i
< y
; i
++)
5103 seq_printf(seq
, ".");
5104 seq_printf(seq
, "] ");
5106 seq_printf(seq
, " %s =%3u.%u%% (%llu/%llu)",
5107 (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)?
5109 (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)?
5111 (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ?
5112 "resync" : "recovery"))),
5113 per_milli
/10, per_milli
% 10,
5114 (unsigned long long) resync
,
5115 (unsigned long long) max_blocks
);
5118 * We do not want to overflow, so the order of operands and
5119 * the * 100 / 100 trick are important. We do a +1 to be
5120 * safe against division by zero. We only estimate anyway.
5122 * dt: time from mark until now
5123 * db: blocks written from mark until now
5124 * rt: remaining time
5126 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
5128 db
= (mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
))
5129 - mddev
->resync_mark_cnt
;
5130 rt
= (dt
* ((unsigned long)(max_blocks
-resync
) / (db
/2/100+1)))/100;
5132 seq_printf(seq
, " finish=%lu.%lumin", rt
/ 60, (rt
% 60)/6);
5134 seq_printf(seq
, " speed=%ldK/sec", db
/2/dt
);
5137 static void *md_seq_start(struct seq_file
*seq
, loff_t
*pos
)
5139 struct list_head
*tmp
;
5149 spin_lock(&all_mddevs_lock
);
5150 list_for_each(tmp
,&all_mddevs
)
5152 mddev
= list_entry(tmp
, mddev_t
, all_mddevs
);
5154 spin_unlock(&all_mddevs_lock
);
5157 spin_unlock(&all_mddevs_lock
);
5159 return (void*)2;/* tail */
5163 static void *md_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
5165 struct list_head
*tmp
;
5166 mddev_t
*next_mddev
, *mddev
= v
;
5172 spin_lock(&all_mddevs_lock
);
5174 tmp
= all_mddevs
.next
;
5176 tmp
= mddev
->all_mddevs
.next
;
5177 if (tmp
!= &all_mddevs
)
5178 next_mddev
= mddev_get(list_entry(tmp
,mddev_t
,all_mddevs
));
5180 next_mddev
= (void*)2;
5183 spin_unlock(&all_mddevs_lock
);
5191 static void md_seq_stop(struct seq_file
*seq
, void *v
)
5195 if (mddev
&& v
!= (void*)1 && v
!= (void*)2)
5199 struct mdstat_info
{
5203 static int md_seq_show(struct seq_file
*seq
, void *v
)
5207 struct list_head
*tmp2
;
5209 struct mdstat_info
*mi
= seq
->private;
5210 struct bitmap
*bitmap
;
5212 if (v
== (void*)1) {
5213 struct mdk_personality
*pers
;
5214 seq_printf(seq
, "Personalities : ");
5215 spin_lock(&pers_lock
);
5216 list_for_each_entry(pers
, &pers_list
, list
)
5217 seq_printf(seq
, "[%s] ", pers
->name
);
5219 spin_unlock(&pers_lock
);
5220 seq_printf(seq
, "\n");
5221 mi
->event
= atomic_read(&md_event_count
);
5224 if (v
== (void*)2) {
5229 if (mddev_lock(mddev
) < 0)
5232 if (mddev
->pers
|| mddev
->raid_disks
|| !list_empty(&mddev
->disks
)) {
5233 seq_printf(seq
, "%s : %sactive", mdname(mddev
),
5234 mddev
->pers
? "" : "in");
5237 seq_printf(seq
, " (read-only)");
5239 seq_printf(seq
, " (auto-read-only)");
5240 seq_printf(seq
, " %s", mddev
->pers
->name
);
5244 rdev_for_each(rdev
, tmp2
, mddev
) {
5245 char b
[BDEVNAME_SIZE
];
5246 seq_printf(seq
, " %s[%d]",
5247 bdevname(rdev
->bdev
,b
), rdev
->desc_nr
);
5248 if (test_bit(WriteMostly
, &rdev
->flags
))
5249 seq_printf(seq
, "(W)");
5250 if (test_bit(Faulty
, &rdev
->flags
)) {
5251 seq_printf(seq
, "(F)");
5253 } else if (rdev
->raid_disk
< 0)
5254 seq_printf(seq
, "(S)"); /* spare */
5258 if (!list_empty(&mddev
->disks
)) {
5260 seq_printf(seq
, "\n %llu blocks",
5261 (unsigned long long)mddev
->array_size
);
5263 seq_printf(seq
, "\n %llu blocks",
5264 (unsigned long long)size
);
5266 if (mddev
->persistent
) {
5267 if (mddev
->major_version
!= 0 ||
5268 mddev
->minor_version
!= 90) {
5269 seq_printf(seq
," super %d.%d",
5270 mddev
->major_version
,
5271 mddev
->minor_version
);
5273 } else if (mddev
->external
)
5274 seq_printf(seq
, " super external:%s",
5275 mddev
->metadata_type
);
5277 seq_printf(seq
, " super non-persistent");
5280 mddev
->pers
->status (seq
, mddev
);
5281 seq_printf(seq
, "\n ");
5282 if (mddev
->pers
->sync_request
) {
5283 if (mddev
->curr_resync
> 2) {
5284 status_resync (seq
, mddev
);
5285 seq_printf(seq
, "\n ");
5286 } else if (mddev
->curr_resync
== 1 || mddev
->curr_resync
== 2)
5287 seq_printf(seq
, "\tresync=DELAYED\n ");
5288 else if (mddev
->recovery_cp
< MaxSector
)
5289 seq_printf(seq
, "\tresync=PENDING\n ");
5292 seq_printf(seq
, "\n ");
5294 if ((bitmap
= mddev
->bitmap
)) {
5295 unsigned long chunk_kb
;
5296 unsigned long flags
;
5297 spin_lock_irqsave(&bitmap
->lock
, flags
);
5298 chunk_kb
= bitmap
->chunksize
>> 10;
5299 seq_printf(seq
, "bitmap: %lu/%lu pages [%luKB], "
5301 bitmap
->pages
- bitmap
->missing_pages
,
5303 (bitmap
->pages
- bitmap
->missing_pages
)
5304 << (PAGE_SHIFT
- 10),
5305 chunk_kb
? chunk_kb
: bitmap
->chunksize
,
5306 chunk_kb
? "KB" : "B");
5308 seq_printf(seq
, ", file: ");
5309 seq_path(seq
, &bitmap
->file
->f_path
, " \t\n");
5312 seq_printf(seq
, "\n");
5313 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
5316 seq_printf(seq
, "\n");
5318 mddev_unlock(mddev
);
5323 static struct seq_operations md_seq_ops
= {
5324 .start
= md_seq_start
,
5325 .next
= md_seq_next
,
5326 .stop
= md_seq_stop
,
5327 .show
= md_seq_show
,
5330 static int md_seq_open(struct inode
*inode
, struct file
*file
)
5333 struct mdstat_info
*mi
= kmalloc(sizeof(*mi
), GFP_KERNEL
);
5337 error
= seq_open(file
, &md_seq_ops
);
5341 struct seq_file
*p
= file
->private_data
;
5343 mi
->event
= atomic_read(&md_event_count
);
5348 static unsigned int mdstat_poll(struct file
*filp
, poll_table
*wait
)
5350 struct seq_file
*m
= filp
->private_data
;
5351 struct mdstat_info
*mi
= m
->private;
5354 poll_wait(filp
, &md_event_waiters
, wait
);
5356 /* always allow read */
5357 mask
= POLLIN
| POLLRDNORM
;
5359 if (mi
->event
!= atomic_read(&md_event_count
))
5360 mask
|= POLLERR
| POLLPRI
;
5364 static const struct file_operations md_seq_fops
= {
5365 .owner
= THIS_MODULE
,
5366 .open
= md_seq_open
,
5368 .llseek
= seq_lseek
,
5369 .release
= seq_release_private
,
5370 .poll
= mdstat_poll
,
5373 int register_md_personality(struct mdk_personality
*p
)
5375 spin_lock(&pers_lock
);
5376 list_add_tail(&p
->list
, &pers_list
);
5377 printk(KERN_INFO
"md: %s personality registered for level %d\n", p
->name
, p
->level
);
5378 spin_unlock(&pers_lock
);
5382 int unregister_md_personality(struct mdk_personality
*p
)
5384 printk(KERN_INFO
"md: %s personality unregistered\n", p
->name
);
5385 spin_lock(&pers_lock
);
5386 list_del_init(&p
->list
);
5387 spin_unlock(&pers_lock
);
5391 static int is_mddev_idle(mddev_t
*mddev
)
5394 struct list_head
*tmp
;
5399 rdev_for_each(rdev
, tmp
, mddev
) {
5400 struct gendisk
*disk
= rdev
->bdev
->bd_contains
->bd_disk
;
5401 curr_events
= disk_stat_read(disk
, sectors
[0]) +
5402 disk_stat_read(disk
, sectors
[1]) -
5403 atomic_read(&disk
->sync_io
);
5404 /* sync IO will cause sync_io to increase before the disk_stats
5405 * as sync_io is counted when a request starts, and
5406 * disk_stats is counted when it completes.
5407 * So resync activity will cause curr_events to be smaller than
5408 * when there was no such activity.
5409 * non-sync IO will cause disk_stat to increase without
5410 * increasing sync_io so curr_events will (eventually)
5411 * be larger than it was before. Once it becomes
5412 * substantially larger, the test below will cause
5413 * the array to appear non-idle, and resync will slow
5415 * If there is a lot of outstanding resync activity when
5416 * we set last_event to curr_events, then all that activity
5417 * completing might cause the array to appear non-idle
5418 * and resync will be slowed down even though there might
5419 * not have been non-resync activity. This will only
5420 * happen once though. 'last_events' will soon reflect
5421 * the state where there is little or no outstanding
5422 * resync requests, and further resync activity will
5423 * always make curr_events less than last_events.
5426 if (curr_events
- rdev
->last_events
> 4096) {
5427 rdev
->last_events
= curr_events
;
5434 void md_done_sync(mddev_t
*mddev
, int blocks
, int ok
)
5436 /* another "blocks" (512byte) blocks have been synced */
5437 atomic_sub(blocks
, &mddev
->recovery_active
);
5438 wake_up(&mddev
->recovery_wait
);
5440 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5441 md_wakeup_thread(mddev
->thread
);
5442 // stop recovery, signal do_sync ....
5447 /* md_write_start(mddev, bi)
5448 * If we need to update some array metadata (e.g. 'active' flag
5449 * in superblock) before writing, schedule a superblock update
5450 * and wait for it to complete.
5452 void md_write_start(mddev_t
*mddev
, struct bio
*bi
)
5454 if (bio_data_dir(bi
) != WRITE
)
5457 BUG_ON(mddev
->ro
== 1);
5458 if (mddev
->ro
== 2) {
5459 /* need to switch to read/write */
5461 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5462 md_wakeup_thread(mddev
->thread
);
5463 md_wakeup_thread(mddev
->sync_thread
);
5465 atomic_inc(&mddev
->writes_pending
);
5466 if (mddev
->safemode
== 1)
5467 mddev
->safemode
= 0;
5468 if (mddev
->in_sync
) {
5469 spin_lock_irq(&mddev
->write_lock
);
5470 if (mddev
->in_sync
) {
5472 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
5473 md_wakeup_thread(mddev
->thread
);
5475 spin_unlock_irq(&mddev
->write_lock
);
5476 sysfs_notify(&mddev
->kobj
, NULL
, "array_state");
5478 wait_event(mddev
->sb_wait
,
5479 !test_bit(MD_CHANGE_CLEAN
, &mddev
->flags
) &&
5480 !test_bit(MD_CHANGE_PENDING
, &mddev
->flags
));
5483 void md_write_end(mddev_t
*mddev
)
5485 if (atomic_dec_and_test(&mddev
->writes_pending
)) {
5486 if (mddev
->safemode
== 2)
5487 md_wakeup_thread(mddev
->thread
);
5488 else if (mddev
->safemode_delay
)
5489 mod_timer(&mddev
->safemode_timer
, jiffies
+ mddev
->safemode_delay
);
5493 /* md_allow_write(mddev)
5494 * Calling this ensures that the array is marked 'active' so that writes
5495 * may proceed without blocking. It is important to call this before
5496 * attempting a GFP_KERNEL allocation while holding the mddev lock.
5497 * Must be called with mddev_lock held.
5499 void md_allow_write(mddev_t
*mddev
)
5506 spin_lock_irq(&mddev
->write_lock
);
5507 if (mddev
->in_sync
) {
5509 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
5510 if (mddev
->safemode_delay
&&
5511 mddev
->safemode
== 0)
5512 mddev
->safemode
= 1;
5513 spin_unlock_irq(&mddev
->write_lock
);
5514 md_update_sb(mddev
, 0);
5516 sysfs_notify(&mddev
->kobj
, NULL
, "array_state");
5517 /* wait for the dirty state to be recorded in the metadata */
5518 wait_event(mddev
->sb_wait
,
5519 !test_bit(MD_CHANGE_CLEAN
, &mddev
->flags
) &&
5520 !test_bit(MD_CHANGE_PENDING
, &mddev
->flags
));
5522 spin_unlock_irq(&mddev
->write_lock
);
5524 EXPORT_SYMBOL_GPL(md_allow_write
);
5526 #define SYNC_MARKS 10
5527 #define SYNC_MARK_STEP (3*HZ)
5528 void md_do_sync(mddev_t
*mddev
)
5531 unsigned int currspeed
= 0,
5533 sector_t max_sectors
,j
, io_sectors
;
5534 unsigned long mark
[SYNC_MARKS
];
5535 sector_t mark_cnt
[SYNC_MARKS
];
5537 struct list_head
*tmp
;
5538 sector_t last_check
;
5540 struct list_head
*rtmp
;
5544 /* just incase thread restarts... */
5545 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
5547 if (mddev
->ro
) /* never try to sync a read-only array */
5550 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
5551 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
5552 desc
= "data-check";
5553 else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
5554 desc
= "requested-resync";
5557 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
5562 /* we overload curr_resync somewhat here.
5563 * 0 == not engaged in resync at all
5564 * 2 == checking that there is no conflict with another sync
5565 * 1 == like 2, but have yielded to allow conflicting resync to
5567 * other == active in resync - this many blocks
5569 * Before starting a resync we must have set curr_resync to
5570 * 2, and then checked that every "conflicting" array has curr_resync
5571 * less than ours. When we find one that is the same or higher
5572 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
5573 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
5574 * This will mean we have to start checking from the beginning again.
5579 mddev
->curr_resync
= 2;
5582 if (kthread_should_stop()) {
5583 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5586 for_each_mddev(mddev2
, tmp
) {
5587 if (mddev2
== mddev
)
5589 if (!mddev
->parallel_resync
5590 && mddev2
->curr_resync
5591 && match_mddev_units(mddev
, mddev2
)) {
5593 if (mddev
< mddev2
&& mddev
->curr_resync
== 2) {
5594 /* arbitrarily yield */
5595 mddev
->curr_resync
= 1;
5596 wake_up(&resync_wait
);
5598 if (mddev
> mddev2
&& mddev
->curr_resync
== 1)
5599 /* no need to wait here, we can wait the next
5600 * time 'round when curr_resync == 2
5603 prepare_to_wait(&resync_wait
, &wq
, TASK_UNINTERRUPTIBLE
);
5604 if (!kthread_should_stop() &&
5605 mddev2
->curr_resync
>= mddev
->curr_resync
) {
5606 printk(KERN_INFO
"md: delaying %s of %s"
5607 " until %s has finished (they"
5608 " share one or more physical units)\n",
5609 desc
, mdname(mddev
), mdname(mddev2
));
5612 finish_wait(&resync_wait
, &wq
);
5615 finish_wait(&resync_wait
, &wq
);
5618 } while (mddev
->curr_resync
< 2);
5621 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
5622 /* resync follows the size requested by the personality,
5623 * which defaults to physical size, but can be virtual size
5625 max_sectors
= mddev
->resync_max_sectors
;
5626 mddev
->resync_mismatches
= 0;
5627 /* we don't use the checkpoint if there's a bitmap */
5628 if (!mddev
->bitmap
&&
5629 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
5630 j
= mddev
->recovery_cp
;
5631 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
5632 max_sectors
= mddev
->size
<< 1;
5634 /* recovery follows the physical size of devices */
5635 max_sectors
= mddev
->size
<< 1;
5637 rdev_for_each(rdev
, rtmp
, mddev
)
5638 if (rdev
->raid_disk
>= 0 &&
5639 !test_bit(Faulty
, &rdev
->flags
) &&
5640 !test_bit(In_sync
, &rdev
->flags
) &&
5641 rdev
->recovery_offset
< j
)
5642 j
= rdev
->recovery_offset
;
5645 printk(KERN_INFO
"md: %s of RAID array %s\n", desc
, mdname(mddev
));
5646 printk(KERN_INFO
"md: minimum _guaranteed_ speed:"
5647 " %d KB/sec/disk.\n", speed_min(mddev
));
5648 printk(KERN_INFO
"md: using maximum available idle IO bandwidth "
5649 "(but not more than %d KB/sec) for %s.\n",
5650 speed_max(mddev
), desc
);
5652 is_mddev_idle(mddev
); /* this also initializes IO event counters */
5655 for (m
= 0; m
< SYNC_MARKS
; m
++) {
5657 mark_cnt
[m
] = io_sectors
;
5660 mddev
->resync_mark
= mark
[last_mark
];
5661 mddev
->resync_mark_cnt
= mark_cnt
[last_mark
];
5664 * Tune reconstruction:
5666 window
= 32*(PAGE_SIZE
/512);
5667 printk(KERN_INFO
"md: using %dk window, over a total of %llu blocks.\n",
5668 window
/2,(unsigned long long) max_sectors
/2);
5670 atomic_set(&mddev
->recovery_active
, 0);
5675 "md: resuming %s of %s from checkpoint.\n",
5676 desc
, mdname(mddev
));
5677 mddev
->curr_resync
= j
;
5680 while (j
< max_sectors
) {
5684 if (j
>= mddev
->resync_max
) {
5685 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
5686 wait_event(mddev
->recovery_wait
,
5687 mddev
->resync_max
> j
5688 || kthread_should_stop());
5690 if (kthread_should_stop())
5692 sectors
= mddev
->pers
->sync_request(mddev
, j
, &skipped
,
5693 currspeed
< speed_min(mddev
));
5695 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5699 if (!skipped
) { /* actual IO requested */
5700 io_sectors
+= sectors
;
5701 atomic_add(sectors
, &mddev
->recovery_active
);
5705 if (j
>1) mddev
->curr_resync
= j
;
5706 mddev
->curr_mark_cnt
= io_sectors
;
5707 if (last_check
== 0)
5708 /* this is the earliers that rebuilt will be
5709 * visible in /proc/mdstat
5711 md_new_event(mddev
);
5713 if (last_check
+ window
> io_sectors
|| j
== max_sectors
)
5716 last_check
= io_sectors
;
5718 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
5722 if (time_after_eq(jiffies
, mark
[last_mark
] + SYNC_MARK_STEP
)) {
5724 int next
= (last_mark
+1) % SYNC_MARKS
;
5726 mddev
->resync_mark
= mark
[next
];
5727 mddev
->resync_mark_cnt
= mark_cnt
[next
];
5728 mark
[next
] = jiffies
;
5729 mark_cnt
[next
] = io_sectors
- atomic_read(&mddev
->recovery_active
);
5734 if (kthread_should_stop())
5739 * this loop exits only if either when we are slower than
5740 * the 'hard' speed limit, or the system was IO-idle for
5742 * the system might be non-idle CPU-wise, but we only care
5743 * about not overloading the IO subsystem. (things like an
5744 * e2fsck being done on the RAID array should execute fast)
5746 blk_unplug(mddev
->queue
);
5749 currspeed
= ((unsigned long)(io_sectors
-mddev
->resync_mark_cnt
))/2
5750 /((jiffies
-mddev
->resync_mark
)/HZ
+1) +1;
5752 if (currspeed
> speed_min(mddev
)) {
5753 if ((currspeed
> speed_max(mddev
)) ||
5754 !is_mddev_idle(mddev
)) {
5760 printk(KERN_INFO
"md: %s: %s done.\n",mdname(mddev
), desc
);
5762 * this also signals 'finished resyncing' to md_stop
5765 blk_unplug(mddev
->queue
);
5767 wait_event(mddev
->recovery_wait
, !atomic_read(&mddev
->recovery_active
));
5769 /* tell personality that we are finished */
5770 mddev
->pers
->sync_request(mddev
, max_sectors
, &skipped
, 1);
5772 if (!test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
) &&
5773 mddev
->curr_resync
> 2) {
5774 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
5775 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
5776 if (mddev
->curr_resync
>= mddev
->recovery_cp
) {
5778 "md: checkpointing %s of %s.\n",
5779 desc
, mdname(mddev
));
5780 mddev
->recovery_cp
= mddev
->curr_resync
;
5783 mddev
->recovery_cp
= MaxSector
;
5785 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
5786 mddev
->curr_resync
= MaxSector
;
5787 rdev_for_each(rdev
, rtmp
, mddev
)
5788 if (rdev
->raid_disk
>= 0 &&
5789 !test_bit(Faulty
, &rdev
->flags
) &&
5790 !test_bit(In_sync
, &rdev
->flags
) &&
5791 rdev
->recovery_offset
< mddev
->curr_resync
)
5792 rdev
->recovery_offset
= mddev
->curr_resync
;
5795 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
5798 mddev
->curr_resync
= 0;
5799 mddev
->resync_max
= MaxSector
;
5800 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
5801 wake_up(&resync_wait
);
5802 set_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
5803 md_wakeup_thread(mddev
->thread
);
5808 * got a signal, exit.
5811 "md: md_do_sync() got signal ... exiting\n");
5812 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5816 EXPORT_SYMBOL_GPL(md_do_sync
);
5819 static int remove_and_add_spares(mddev_t
*mddev
)
5822 struct list_head
*rtmp
;
5825 rdev_for_each(rdev
, rtmp
, mddev
)
5826 if (rdev
->raid_disk
>= 0 &&
5827 !test_bit(Blocked
, &rdev
->flags
) &&
5828 (test_bit(Faulty
, &rdev
->flags
) ||
5829 ! test_bit(In_sync
, &rdev
->flags
)) &&
5830 atomic_read(&rdev
->nr_pending
)==0) {
5831 if (mddev
->pers
->hot_remove_disk(
5832 mddev
, rdev
->raid_disk
)==0) {
5834 sprintf(nm
,"rd%d", rdev
->raid_disk
);
5835 sysfs_remove_link(&mddev
->kobj
, nm
);
5836 rdev
->raid_disk
= -1;
5840 if (mddev
->degraded
) {
5841 rdev_for_each(rdev
, rtmp
, mddev
) {
5842 if (rdev
->raid_disk
>= 0 &&
5843 !test_bit(In_sync
, &rdev
->flags
))
5845 if (rdev
->raid_disk
< 0
5846 && !test_bit(Faulty
, &rdev
->flags
)) {
5847 rdev
->recovery_offset
= 0;
5848 if (mddev
->pers
->hot_add_disk(mddev
,rdev
)) {
5850 sprintf(nm
, "rd%d", rdev
->raid_disk
);
5851 if (sysfs_create_link(&mddev
->kobj
,
5854 "md: cannot register "
5858 md_new_event(mddev
);
5867 * This routine is regularly called by all per-raid-array threads to
5868 * deal with generic issues like resync and super-block update.
5869 * Raid personalities that don't have a thread (linear/raid0) do not
5870 * need this as they never do any recovery or update the superblock.
5872 * It does not do any resync itself, but rather "forks" off other threads
5873 * to do that as needed.
5874 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
5875 * "->recovery" and create a thread at ->sync_thread.
5876 * When the thread finishes it sets MD_RECOVERY_DONE
5877 * and wakeups up this thread which will reap the thread and finish up.
5878 * This thread also removes any faulty devices (with nr_pending == 0).
5880 * The overall approach is:
5881 * 1/ if the superblock needs updating, update it.
5882 * 2/ If a recovery thread is running, don't do anything else.
5883 * 3/ If recovery has finished, clean up, possibly marking spares active.
5884 * 4/ If there are any faulty devices, remove them.
5885 * 5/ If array is degraded, try to add spares devices
5886 * 6/ If array has spares or is not in-sync, start a resync thread.
5888 void md_check_recovery(mddev_t
*mddev
)
5891 struct list_head
*rtmp
;
5895 bitmap_daemon_work(mddev
->bitmap
);
5900 if (signal_pending(current
)) {
5901 if (mddev
->pers
->sync_request
&& !mddev
->external
) {
5902 printk(KERN_INFO
"md: %s in immediate safe mode\n",
5904 mddev
->safemode
= 2;
5906 flush_signals(current
);
5910 (mddev
->flags
&& !mddev
->external
) ||
5911 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
5912 test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
5913 (mddev
->external
== 0 && mddev
->safemode
== 1) ||
5914 (mddev
->safemode
== 2 && ! atomic_read(&mddev
->writes_pending
)
5915 && !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
5919 if (mddev_trylock(mddev
)) {
5922 if (!mddev
->external
) {
5923 spin_lock_irq(&mddev
->write_lock
);
5924 if (mddev
->safemode
&&
5925 !atomic_read(&mddev
->writes_pending
) &&
5927 mddev
->recovery_cp
== MaxSector
) {
5929 if (mddev
->persistent
)
5930 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
5932 if (mddev
->safemode
== 1)
5933 mddev
->safemode
= 0;
5934 spin_unlock_irq(&mddev
->write_lock
);
5938 md_update_sb(mddev
, 0);
5941 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
5942 !test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
)) {
5943 /* resync/recovery still happening */
5944 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5947 if (mddev
->sync_thread
) {
5948 /* resync has finished, collect result */
5949 md_unregister_thread(mddev
->sync_thread
);
5950 mddev
->sync_thread
= NULL
;
5951 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
5953 /* activate any spares */
5954 mddev
->pers
->spare_active(mddev
);
5956 md_update_sb(mddev
, 1);
5958 /* if array is no-longer degraded, then any saved_raid_disk
5959 * information must be scrapped
5961 if (!mddev
->degraded
)
5962 rdev_for_each(rdev
, rtmp
, mddev
)
5963 rdev
->saved_raid_disk
= -1;
5965 mddev
->recovery
= 0;
5966 /* flag recovery needed just to double check */
5967 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5968 md_new_event(mddev
);
5971 /* Clear some bits that don't mean anything, but
5974 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5975 clear_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5976 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
5978 if (test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
5980 /* no recovery is running.
5981 * remove any failed drives, then
5982 * add spares if possible.
5983 * Spare are also removed and re-added, to allow
5984 * the personality to fail the re-add.
5987 if (mddev
->reshape_position
!= MaxSector
) {
5988 if (mddev
->pers
->check_reshape(mddev
) != 0)
5989 /* Cannot proceed */
5991 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
5992 } else if ((spares
= remove_and_add_spares(mddev
))) {
5993 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
5994 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
5995 } else if (mddev
->recovery_cp
< MaxSector
) {
5996 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
5997 } else if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
5998 /* nothing to be done ... */
6001 if (mddev
->pers
->sync_request
) {
6002 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
6003 if (spares
&& mddev
->bitmap
&& ! mddev
->bitmap
->file
) {
6004 /* We are adding a device or devices to an array
6005 * which has the bitmap stored on all devices.
6006 * So make sure all bitmap pages get written
6008 bitmap_write_all(mddev
->bitmap
);
6010 mddev
->sync_thread
= md_register_thread(md_do_sync
,
6013 if (!mddev
->sync_thread
) {
6014 printk(KERN_ERR
"%s: could not start resync"
6017 /* leave the spares where they are, it shouldn't hurt */
6018 mddev
->recovery
= 0;
6020 md_wakeup_thread(mddev
->sync_thread
);
6021 md_new_event(mddev
);
6024 mddev_unlock(mddev
);
6028 void md_wait_for_blocked_rdev(mdk_rdev_t
*rdev
, mddev_t
*mddev
)
6030 sysfs_notify(&rdev
->kobj
, NULL
, "state");
6031 wait_event_timeout(rdev
->blocked_wait
,
6032 !test_bit(Blocked
, &rdev
->flags
),
6033 msecs_to_jiffies(5000));
6034 rdev_dec_pending(rdev
, mddev
);
6036 EXPORT_SYMBOL(md_wait_for_blocked_rdev
);
6038 static int md_notify_reboot(struct notifier_block
*this,
6039 unsigned long code
, void *x
)
6041 struct list_head
*tmp
;
6044 if ((code
== SYS_DOWN
) || (code
== SYS_HALT
) || (code
== SYS_POWER_OFF
)) {
6046 printk(KERN_INFO
"md: stopping all md devices.\n");
6048 for_each_mddev(mddev
, tmp
)
6049 if (mddev_trylock(mddev
)) {
6050 do_md_stop (mddev
, 1);
6051 mddev_unlock(mddev
);
6054 * certain more exotic SCSI devices are known to be
6055 * volatile wrt too early system reboots. While the
6056 * right place to handle this issue is the given
6057 * driver, we do want to have a safe RAID driver ...
6064 static struct notifier_block md_notifier
= {
6065 .notifier_call
= md_notify_reboot
,
6067 .priority
= INT_MAX
, /* before any real devices */
6070 static void md_geninit(void)
6072 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t
));
6074 proc_create("mdstat", S_IRUGO
, NULL
, &md_seq_fops
);
6077 static int __init
md_init(void)
6079 if (register_blkdev(MAJOR_NR
, "md"))
6081 if ((mdp_major
=register_blkdev(0, "mdp"))<=0) {
6082 unregister_blkdev(MAJOR_NR
, "md");
6085 blk_register_region(MKDEV(MAJOR_NR
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
6086 md_probe
, NULL
, NULL
);
6087 blk_register_region(MKDEV(mdp_major
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
6088 md_probe
, NULL
, NULL
);
6090 register_reboot_notifier(&md_notifier
);
6091 raid_table_header
= register_sysctl_table(raid_root_table
);
6101 * Searches all registered partitions for autorun RAID arrays
6105 static LIST_HEAD(all_detected_devices
);
6106 struct detected_devices_node
{
6107 struct list_head list
;
6111 void md_autodetect_dev(dev_t dev
)
6113 struct detected_devices_node
*node_detected_dev
;
6115 node_detected_dev
= kzalloc(sizeof(*node_detected_dev
), GFP_KERNEL
);
6116 if (node_detected_dev
) {
6117 node_detected_dev
->dev
= dev
;
6118 list_add_tail(&node_detected_dev
->list
, &all_detected_devices
);
6120 printk(KERN_CRIT
"md: md_autodetect_dev: kzalloc failed"
6121 ", skipping dev(%d,%d)\n", MAJOR(dev
), MINOR(dev
));
6126 static void autostart_arrays(int part
)
6129 struct detected_devices_node
*node_detected_dev
;
6131 int i_scanned
, i_passed
;
6136 printk(KERN_INFO
"md: Autodetecting RAID arrays.\n");
6138 while (!list_empty(&all_detected_devices
) && i_scanned
< INT_MAX
) {
6140 node_detected_dev
= list_entry(all_detected_devices
.next
,
6141 struct detected_devices_node
, list
);
6142 list_del(&node_detected_dev
->list
);
6143 dev
= node_detected_dev
->dev
;
6144 kfree(node_detected_dev
);
6145 rdev
= md_import_device(dev
,0, 90);
6149 if (test_bit(Faulty
, &rdev
->flags
)) {
6153 set_bit(AutoDetected
, &rdev
->flags
);
6154 list_add(&rdev
->same_set
, &pending_raid_disks
);
6158 printk(KERN_INFO
"md: Scanned %d and added %d devices.\n",
6159 i_scanned
, i_passed
);
6161 autorun_devices(part
);
6164 #endif /* !MODULE */
6166 static __exit
void md_exit(void)
6169 struct list_head
*tmp
;
6171 blk_unregister_region(MKDEV(MAJOR_NR
,0), 1U << MINORBITS
);
6172 blk_unregister_region(MKDEV(mdp_major
,0), 1U << MINORBITS
);
6174 unregister_blkdev(MAJOR_NR
,"md");
6175 unregister_blkdev(mdp_major
, "mdp");
6176 unregister_reboot_notifier(&md_notifier
);
6177 unregister_sysctl_table(raid_table_header
);
6178 remove_proc_entry("mdstat", NULL
);
6179 for_each_mddev(mddev
, tmp
) {
6180 struct gendisk
*disk
= mddev
->gendisk
;
6183 export_array(mddev
);
6186 mddev
->gendisk
= NULL
;
6191 subsys_initcall(md_init
);
6192 module_exit(md_exit
)
6194 static int get_ro(char *buffer
, struct kernel_param
*kp
)
6196 return sprintf(buffer
, "%d", start_readonly
);
6198 static int set_ro(const char *val
, struct kernel_param
*kp
)
6201 int num
= simple_strtoul(val
, &e
, 10);
6202 if (*val
&& (*e
== '\0' || *e
== '\n')) {
6203 start_readonly
= num
;
6209 module_param_call(start_ro
, set_ro
, get_ro
, NULL
, S_IRUSR
|S_IWUSR
);
6210 module_param(start_dirty_degraded
, int, S_IRUGO
|S_IWUSR
);
6213 EXPORT_SYMBOL(register_md_personality
);
6214 EXPORT_SYMBOL(unregister_md_personality
);
6215 EXPORT_SYMBOL(md_error
);
6216 EXPORT_SYMBOL(md_done_sync
);
6217 EXPORT_SYMBOL(md_write_start
);
6218 EXPORT_SYMBOL(md_write_end
);
6219 EXPORT_SYMBOL(md_register_thread
);
6220 EXPORT_SYMBOL(md_unregister_thread
);
6221 EXPORT_SYMBOL(md_wakeup_thread
);
6222 EXPORT_SYMBOL(md_check_recovery
);
6223 MODULE_LICENSE("GPL");
6225 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR
);