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/config.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/devfs_fs_kernel.h>
43 #include <linux/buffer_head.h> /* for invalidate_bdev */
44 #include <linux/suspend.h>
45 #include <linux/poll.h>
46 #include <linux/mutex.h>
47 #include <linux/ctype.h>
49 #include <linux/init.h>
51 #include <linux/file.h>
54 #include <linux/kmod.h>
57 #include <asm/unaligned.h>
59 #define MAJOR_NR MD_MAJOR
62 /* 63 partitions with the alternate major number (mdp) */
63 #define MdpMinorShift 6
66 #define dprintk(x...) ((void)(DEBUG && printk(x)))
70 static void autostart_arrays (int part
);
73 static LIST_HEAD(pers_list
);
74 static DEFINE_SPINLOCK(pers_lock
);
76 static void md_print_devices(void);
78 #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
81 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
82 * is 1000 KB/sec, so the extra system load does not show up that much.
83 * Increase it if you want to have more _guaranteed_ speed. Note that
84 * the RAID driver will use the maximum available bandwidth if the IO
85 * subsystem is idle. There is also an 'absolute maximum' reconstruction
86 * speed limit - in case reconstruction slows down your system despite
89 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
90 * or /sys/block/mdX/md/sync_speed_{min,max}
93 static int sysctl_speed_limit_min
= 1000;
94 static int sysctl_speed_limit_max
= 200000;
95 static inline int speed_min(mddev_t
*mddev
)
97 return mddev
->sync_speed_min
?
98 mddev
->sync_speed_min
: sysctl_speed_limit_min
;
101 static inline int speed_max(mddev_t
*mddev
)
103 return mddev
->sync_speed_max
?
104 mddev
->sync_speed_max
: sysctl_speed_limit_max
;
107 static struct ctl_table_header
*raid_table_header
;
109 static ctl_table raid_table
[] = {
111 .ctl_name
= DEV_RAID_SPEED_LIMIT_MIN
,
112 .procname
= "speed_limit_min",
113 .data
= &sysctl_speed_limit_min
,
114 .maxlen
= sizeof(int),
115 .mode
= S_IRUGO
|S_IWUSR
,
116 .proc_handler
= &proc_dointvec
,
119 .ctl_name
= DEV_RAID_SPEED_LIMIT_MAX
,
120 .procname
= "speed_limit_max",
121 .data
= &sysctl_speed_limit_max
,
122 .maxlen
= sizeof(int),
123 .mode
= S_IRUGO
|S_IWUSR
,
124 .proc_handler
= &proc_dointvec
,
129 static ctl_table raid_dir_table
[] = {
131 .ctl_name
= DEV_RAID
,
134 .mode
= S_IRUGO
|S_IWUGO
,
140 static ctl_table raid_root_table
[] = {
146 .child
= raid_dir_table
,
151 static struct block_device_operations md_fops
;
153 static int start_readonly
;
156 * We have a system wide 'event count' that is incremented
157 * on any 'interesting' event, and readers of /proc/mdstat
158 * can use 'poll' or 'select' to find out when the event
162 * start array, stop array, error, add device, remove device,
163 * start build, activate spare
165 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters
);
166 static atomic_t md_event_count
;
167 void md_new_event(mddev_t
*mddev
)
169 atomic_inc(&md_event_count
);
170 wake_up(&md_event_waiters
);
171 sysfs_notify(&mddev
->kobj
, NULL
, "sync_action");
173 EXPORT_SYMBOL_GPL(md_new_event
);
175 /* Alternate version that can be called from interrupts
176 * when calling sysfs_notify isn't needed.
178 void md_new_event_inintr(mddev_t
*mddev
)
180 atomic_inc(&md_event_count
);
181 wake_up(&md_event_waiters
);
185 * Enables to iterate over all existing md arrays
186 * all_mddevs_lock protects this list.
188 static LIST_HEAD(all_mddevs
);
189 static DEFINE_SPINLOCK(all_mddevs_lock
);
193 * iterates through all used mddevs in the system.
194 * We take care to grab the all_mddevs_lock whenever navigating
195 * the list, and to always hold a refcount when unlocked.
196 * Any code which breaks out of this loop while own
197 * a reference to the current mddev and must mddev_put it.
199 #define ITERATE_MDDEV(mddev,tmp) \
201 for (({ spin_lock(&all_mddevs_lock); \
202 tmp = all_mddevs.next; \
204 ({ if (tmp != &all_mddevs) \
205 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
206 spin_unlock(&all_mddevs_lock); \
207 if (mddev) mddev_put(mddev); \
208 mddev = list_entry(tmp, mddev_t, all_mddevs); \
209 tmp != &all_mddevs;}); \
210 ({ spin_lock(&all_mddevs_lock); \
215 static int md_fail_request (request_queue_t
*q
, struct bio
*bio
)
217 bio_io_error(bio
, bio
->bi_size
);
221 static inline mddev_t
*mddev_get(mddev_t
*mddev
)
223 atomic_inc(&mddev
->active
);
227 static void mddev_put(mddev_t
*mddev
)
229 if (!atomic_dec_and_lock(&mddev
->active
, &all_mddevs_lock
))
231 if (!mddev
->raid_disks
&& list_empty(&mddev
->disks
)) {
232 list_del(&mddev
->all_mddevs
);
233 spin_unlock(&all_mddevs_lock
);
234 blk_cleanup_queue(mddev
->queue
);
235 kobject_unregister(&mddev
->kobj
);
237 spin_unlock(&all_mddevs_lock
);
240 static mddev_t
* mddev_find(dev_t unit
)
242 mddev_t
*mddev
, *new = NULL
;
245 spin_lock(&all_mddevs_lock
);
246 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
247 if (mddev
->unit
== unit
) {
249 spin_unlock(&all_mddevs_lock
);
255 list_add(&new->all_mddevs
, &all_mddevs
);
256 spin_unlock(&all_mddevs_lock
);
259 spin_unlock(&all_mddevs_lock
);
261 new = kzalloc(sizeof(*new), GFP_KERNEL
);
266 if (MAJOR(unit
) == MD_MAJOR
)
267 new->md_minor
= MINOR(unit
);
269 new->md_minor
= MINOR(unit
) >> MdpMinorShift
;
271 mutex_init(&new->reconfig_mutex
);
272 INIT_LIST_HEAD(&new->disks
);
273 INIT_LIST_HEAD(&new->all_mddevs
);
274 init_timer(&new->safemode_timer
);
275 atomic_set(&new->active
, 1);
276 spin_lock_init(&new->write_lock
);
277 init_waitqueue_head(&new->sb_wait
);
278 new->resync_max
= MaxSector
;
280 new->queue
= blk_alloc_queue(GFP_KERNEL
);
285 set_bit(QUEUE_FLAG_CLUSTER
, &new->queue
->queue_flags
);
287 blk_queue_make_request(new->queue
, md_fail_request
);
292 static inline int mddev_lock(mddev_t
* mddev
)
294 return mutex_lock_interruptible(&mddev
->reconfig_mutex
);
297 static inline int mddev_trylock(mddev_t
* mddev
)
299 return mutex_trylock(&mddev
->reconfig_mutex
);
302 static inline void mddev_unlock(mddev_t
* mddev
)
304 mutex_unlock(&mddev
->reconfig_mutex
);
306 md_wakeup_thread(mddev
->thread
);
309 static mdk_rdev_t
* find_rdev_nr(mddev_t
*mddev
, int nr
)
312 struct list_head
*tmp
;
314 ITERATE_RDEV(mddev
,rdev
,tmp
) {
315 if (rdev
->desc_nr
== nr
)
321 static mdk_rdev_t
* find_rdev(mddev_t
* mddev
, dev_t dev
)
323 struct list_head
*tmp
;
326 ITERATE_RDEV(mddev
,rdev
,tmp
) {
327 if (rdev
->bdev
->bd_dev
== dev
)
333 static struct mdk_personality
*find_pers(int level
, char *clevel
)
335 struct mdk_personality
*pers
;
336 list_for_each_entry(pers
, &pers_list
, list
) {
337 if (level
!= LEVEL_NONE
&& pers
->level
== level
)
339 if (strcmp(pers
->name
, clevel
)==0)
345 static inline sector_t
calc_dev_sboffset(struct block_device
*bdev
)
347 sector_t size
= bdev
->bd_inode
->i_size
>> BLOCK_SIZE_BITS
;
348 return MD_NEW_SIZE_BLOCKS(size
);
351 static sector_t
calc_dev_size(mdk_rdev_t
*rdev
, unsigned chunk_size
)
355 size
= rdev
->sb_offset
;
358 size
&= ~((sector_t
)chunk_size
/1024 - 1);
362 static int alloc_disk_sb(mdk_rdev_t
* rdev
)
367 rdev
->sb_page
= alloc_page(GFP_KERNEL
);
368 if (!rdev
->sb_page
) {
369 printk(KERN_ALERT
"md: out of memory.\n");
376 static void free_disk_sb(mdk_rdev_t
* rdev
)
379 put_page(rdev
->sb_page
);
381 rdev
->sb_page
= NULL
;
388 static int super_written(struct bio
*bio
, unsigned int bytes_done
, int error
)
390 mdk_rdev_t
*rdev
= bio
->bi_private
;
391 mddev_t
*mddev
= rdev
->mddev
;
395 if (error
|| !test_bit(BIO_UPTODATE
, &bio
->bi_flags
))
396 md_error(mddev
, rdev
);
398 if (atomic_dec_and_test(&mddev
->pending_writes
))
399 wake_up(&mddev
->sb_wait
);
404 static int super_written_barrier(struct bio
*bio
, unsigned int bytes_done
, int error
)
406 struct bio
*bio2
= bio
->bi_private
;
407 mdk_rdev_t
*rdev
= bio2
->bi_private
;
408 mddev_t
*mddev
= rdev
->mddev
;
412 if (!test_bit(BIO_UPTODATE
, &bio
->bi_flags
) &&
413 error
== -EOPNOTSUPP
) {
415 /* barriers don't appear to be supported :-( */
416 set_bit(BarriersNotsupp
, &rdev
->flags
);
417 mddev
->barriers_work
= 0;
418 spin_lock_irqsave(&mddev
->write_lock
, flags
);
419 bio2
->bi_next
= mddev
->biolist
;
420 mddev
->biolist
= bio2
;
421 spin_unlock_irqrestore(&mddev
->write_lock
, flags
);
422 wake_up(&mddev
->sb_wait
);
427 bio
->bi_private
= rdev
;
428 return super_written(bio
, bytes_done
, error
);
431 void md_super_write(mddev_t
*mddev
, mdk_rdev_t
*rdev
,
432 sector_t sector
, int size
, struct page
*page
)
434 /* write first size bytes of page to sector of rdev
435 * Increment mddev->pending_writes before returning
436 * and decrement it on completion, waking up sb_wait
437 * if zero is reached.
438 * If an error occurred, call md_error
440 * As we might need to resubmit the request if BIO_RW_BARRIER
441 * causes ENOTSUPP, we allocate a spare bio...
443 struct bio
*bio
= bio_alloc(GFP_NOIO
, 1);
444 int rw
= (1<<BIO_RW
) | (1<<BIO_RW_SYNC
);
446 bio
->bi_bdev
= rdev
->bdev
;
447 bio
->bi_sector
= sector
;
448 bio_add_page(bio
, page
, size
, 0);
449 bio
->bi_private
= rdev
;
450 bio
->bi_end_io
= super_written
;
453 atomic_inc(&mddev
->pending_writes
);
454 if (!test_bit(BarriersNotsupp
, &rdev
->flags
)) {
456 rw
|= (1<<BIO_RW_BARRIER
);
457 rbio
= bio_clone(bio
, GFP_NOIO
);
458 rbio
->bi_private
= bio
;
459 rbio
->bi_end_io
= super_written_barrier
;
460 submit_bio(rw
, rbio
);
465 void md_super_wait(mddev_t
*mddev
)
467 /* wait for all superblock writes that were scheduled to complete.
468 * if any had to be retried (due to BARRIER problems), retry them
472 prepare_to_wait(&mddev
->sb_wait
, &wq
, TASK_UNINTERRUPTIBLE
);
473 if (atomic_read(&mddev
->pending_writes
)==0)
475 while (mddev
->biolist
) {
477 spin_lock_irq(&mddev
->write_lock
);
478 bio
= mddev
->biolist
;
479 mddev
->biolist
= bio
->bi_next
;
481 spin_unlock_irq(&mddev
->write_lock
);
482 submit_bio(bio
->bi_rw
, bio
);
486 finish_wait(&mddev
->sb_wait
, &wq
);
489 static int bi_complete(struct bio
*bio
, unsigned int bytes_done
, int error
)
494 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))
591 static unsigned int calc_sb_csum(mdp_super_t
* sb
)
593 unsigned int disk_csum
, csum
;
595 disk_csum
= sb
->sb_csum
;
597 csum
= csum_partial((void *)sb
, MD_SB_BYTES
, 0);
598 sb
->sb_csum
= disk_csum
;
604 * Handle superblock details.
605 * We want to be able to handle multiple superblock formats
606 * so we have a common interface to them all, and an array of
607 * different handlers.
608 * We rely on user-space to write the initial superblock, and support
609 * reading and updating of superblocks.
610 * Interface methods are:
611 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
612 * loads and validates a superblock on dev.
613 * if refdev != NULL, compare superblocks on both devices
615 * 0 - dev has a superblock that is compatible with refdev
616 * 1 - dev has a superblock that is compatible and newer than refdev
617 * so dev should be used as the refdev in future
618 * -EINVAL superblock incompatible or invalid
619 * -othererror e.g. -EIO
621 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
622 * Verify that dev is acceptable into mddev.
623 * The first time, mddev->raid_disks will be 0, and data from
624 * dev should be merged in. Subsequent calls check that dev
625 * is new enough. Return 0 or -EINVAL
627 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
628 * Update the superblock for rdev with data in mddev
629 * This does not write to disc.
635 struct module
*owner
;
636 int (*load_super
)(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
);
637 int (*validate_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
638 void (*sync_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
642 * load_super for 0.90.0
644 static int super_90_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
646 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
652 * Calculate the position of the superblock,
653 * it's at the end of the disk.
655 * It also happens to be a multiple of 4Kb.
657 sb_offset
= calc_dev_sboffset(rdev
->bdev
);
658 rdev
->sb_offset
= sb_offset
;
660 ret
= read_disk_sb(rdev
, MD_SB_BYTES
);
665 bdevname(rdev
->bdev
, b
);
666 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
668 if (sb
->md_magic
!= MD_SB_MAGIC
) {
669 printk(KERN_ERR
"md: invalid raid superblock magic on %s\n",
674 if (sb
->major_version
!= 0 ||
675 sb
->minor_version
< 90 ||
676 sb
->minor_version
> 91) {
677 printk(KERN_WARNING
"Bad version number %d.%d on %s\n",
678 sb
->major_version
, sb
->minor_version
,
683 if (sb
->raid_disks
<= 0)
686 if (csum_fold(calc_sb_csum(sb
)) != csum_fold(sb
->sb_csum
)) {
687 printk(KERN_WARNING
"md: invalid superblock checksum on %s\n",
692 rdev
->preferred_minor
= sb
->md_minor
;
693 rdev
->data_offset
= 0;
694 rdev
->sb_size
= MD_SB_BYTES
;
696 if (sb
->level
== LEVEL_MULTIPATH
)
699 rdev
->desc_nr
= sb
->this_disk
.number
;
705 mdp_super_t
*refsb
= (mdp_super_t
*)page_address(refdev
->sb_page
);
706 if (!uuid_equal(refsb
, sb
)) {
707 printk(KERN_WARNING
"md: %s has different UUID to %s\n",
708 b
, bdevname(refdev
->bdev
,b2
));
711 if (!sb_equal(refsb
, sb
)) {
712 printk(KERN_WARNING
"md: %s has same UUID"
713 " but different superblock to %s\n",
714 b
, bdevname(refdev
->bdev
, b2
));
718 ev2
= md_event(refsb
);
724 rdev
->size
= calc_dev_size(rdev
, sb
->chunk_size
);
726 if (rdev
->size
< sb
->size
&& sb
->level
> 1)
727 /* "this cannot possibly happen" ... */
735 * validate_super for 0.90.0
737 static int super_90_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
740 mdp_super_t
*sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
741 __u64 ev1
= md_event(sb
);
743 rdev
->raid_disk
= -1;
745 if (mddev
->raid_disks
== 0) {
746 mddev
->major_version
= 0;
747 mddev
->minor_version
= sb
->minor_version
;
748 mddev
->patch_version
= sb
->patch_version
;
749 mddev
->persistent
= 1;
751 mddev
->chunk_size
= sb
->chunk_size
;
752 mddev
->ctime
= sb
->ctime
;
753 mddev
->utime
= sb
->utime
;
754 mddev
->level
= sb
->level
;
755 mddev
->clevel
[0] = 0;
756 mddev
->layout
= sb
->layout
;
757 mddev
->raid_disks
= sb
->raid_disks
;
758 mddev
->size
= sb
->size
;
760 mddev
->bitmap_offset
= 0;
761 mddev
->default_bitmap_offset
= MD_SB_BYTES
>> 9;
763 if (mddev
->minor_version
>= 91) {
764 mddev
->reshape_position
= sb
->reshape_position
;
765 mddev
->delta_disks
= sb
->delta_disks
;
766 mddev
->new_level
= sb
->new_level
;
767 mddev
->new_layout
= sb
->new_layout
;
768 mddev
->new_chunk
= sb
->new_chunk
;
770 mddev
->reshape_position
= MaxSector
;
771 mddev
->delta_disks
= 0;
772 mddev
->new_level
= mddev
->level
;
773 mddev
->new_layout
= mddev
->layout
;
774 mddev
->new_chunk
= mddev
->chunk_size
;
777 if (sb
->state
& (1<<MD_SB_CLEAN
))
778 mddev
->recovery_cp
= MaxSector
;
780 if (sb
->events_hi
== sb
->cp_events_hi
&&
781 sb
->events_lo
== sb
->cp_events_lo
) {
782 mddev
->recovery_cp
= sb
->recovery_cp
;
784 mddev
->recovery_cp
= 0;
787 memcpy(mddev
->uuid
+0, &sb
->set_uuid0
, 4);
788 memcpy(mddev
->uuid
+4, &sb
->set_uuid1
, 4);
789 memcpy(mddev
->uuid
+8, &sb
->set_uuid2
, 4);
790 memcpy(mddev
->uuid
+12,&sb
->set_uuid3
, 4);
792 mddev
->max_disks
= MD_SB_DISKS
;
794 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
) &&
795 mddev
->bitmap_file
== NULL
) {
796 if (mddev
->level
!= 1 && mddev
->level
!= 4
797 && mddev
->level
!= 5 && mddev
->level
!= 6
798 && mddev
->level
!= 10) {
799 /* FIXME use a better test */
800 printk(KERN_WARNING
"md: bitmaps not supported for this level.\n");
803 mddev
->bitmap_offset
= mddev
->default_bitmap_offset
;
806 } else if (mddev
->pers
== NULL
) {
807 /* Insist on good event counter while assembling */
809 if (ev1
< mddev
->events
)
811 } else if (mddev
->bitmap
) {
812 /* if adding to array with a bitmap, then we can accept an
813 * older device ... but not too old.
815 if (ev1
< mddev
->bitmap
->events_cleared
)
818 if (ev1
< mddev
->events
)
819 /* just a hot-add of a new device, leave raid_disk at -1 */
823 if (mddev
->level
!= LEVEL_MULTIPATH
) {
824 desc
= sb
->disks
+ rdev
->desc_nr
;
826 if (desc
->state
& (1<<MD_DISK_FAULTY
))
827 set_bit(Faulty
, &rdev
->flags
);
828 else if (desc
->state
& (1<<MD_DISK_SYNC
) /* &&
829 desc->raid_disk < mddev->raid_disks */) {
830 set_bit(In_sync
, &rdev
->flags
);
831 rdev
->raid_disk
= desc
->raid_disk
;
833 if (desc
->state
& (1<<MD_DISK_WRITEMOSTLY
))
834 set_bit(WriteMostly
, &rdev
->flags
);
835 } else /* MULTIPATH are always insync */
836 set_bit(In_sync
, &rdev
->flags
);
841 * sync_super for 0.90.0
843 static void super_90_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
846 struct list_head
*tmp
;
848 int next_spare
= mddev
->raid_disks
;
851 /* make rdev->sb match mddev data..
854 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
855 * 3/ any empty disks < next_spare become removed
857 * disks[0] gets initialised to REMOVED because
858 * we cannot be sure from other fields if it has
859 * been initialised or not.
862 int active
=0, working
=0,failed
=0,spare
=0,nr_disks
=0;
864 rdev
->sb_size
= MD_SB_BYTES
;
866 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
868 memset(sb
, 0, sizeof(*sb
));
870 sb
->md_magic
= MD_SB_MAGIC
;
871 sb
->major_version
= mddev
->major_version
;
872 sb
->patch_version
= mddev
->patch_version
;
873 sb
->gvalid_words
= 0; /* ignored */
874 memcpy(&sb
->set_uuid0
, mddev
->uuid
+0, 4);
875 memcpy(&sb
->set_uuid1
, mddev
->uuid
+4, 4);
876 memcpy(&sb
->set_uuid2
, mddev
->uuid
+8, 4);
877 memcpy(&sb
->set_uuid3
, mddev
->uuid
+12,4);
879 sb
->ctime
= mddev
->ctime
;
880 sb
->level
= mddev
->level
;
881 sb
->size
= mddev
->size
;
882 sb
->raid_disks
= mddev
->raid_disks
;
883 sb
->md_minor
= mddev
->md_minor
;
884 sb
->not_persistent
= 0;
885 sb
->utime
= mddev
->utime
;
887 sb
->events_hi
= (mddev
->events
>>32);
888 sb
->events_lo
= (u32
)mddev
->events
;
890 if (mddev
->reshape_position
== MaxSector
)
891 sb
->minor_version
= 90;
893 sb
->minor_version
= 91;
894 sb
->reshape_position
= mddev
->reshape_position
;
895 sb
->new_level
= mddev
->new_level
;
896 sb
->delta_disks
= mddev
->delta_disks
;
897 sb
->new_layout
= mddev
->new_layout
;
898 sb
->new_chunk
= mddev
->new_chunk
;
900 mddev
->minor_version
= sb
->minor_version
;
903 sb
->recovery_cp
= mddev
->recovery_cp
;
904 sb
->cp_events_hi
= (mddev
->events
>>32);
905 sb
->cp_events_lo
= (u32
)mddev
->events
;
906 if (mddev
->recovery_cp
== MaxSector
)
907 sb
->state
= (1<< MD_SB_CLEAN
);
911 sb
->layout
= mddev
->layout
;
912 sb
->chunk_size
= mddev
->chunk_size
;
914 if (mddev
->bitmap
&& mddev
->bitmap_file
== NULL
)
915 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
917 sb
->disks
[0].state
= (1<<MD_DISK_REMOVED
);
918 ITERATE_RDEV(mddev
,rdev2
,tmp
) {
921 if (rdev2
->raid_disk
>= 0 && test_bit(In_sync
, &rdev2
->flags
)
922 && !test_bit(Faulty
, &rdev2
->flags
))
923 desc_nr
= rdev2
->raid_disk
;
925 desc_nr
= next_spare
++;
926 rdev2
->desc_nr
= desc_nr
;
927 d
= &sb
->disks
[rdev2
->desc_nr
];
929 d
->number
= rdev2
->desc_nr
;
930 d
->major
= MAJOR(rdev2
->bdev
->bd_dev
);
931 d
->minor
= MINOR(rdev2
->bdev
->bd_dev
);
932 if (rdev2
->raid_disk
>= 0 && test_bit(In_sync
, &rdev2
->flags
)
933 && !test_bit(Faulty
, &rdev2
->flags
))
934 d
->raid_disk
= rdev2
->raid_disk
;
936 d
->raid_disk
= rdev2
->desc_nr
; /* compatibility */
937 if (test_bit(Faulty
, &rdev2
->flags
))
938 d
->state
= (1<<MD_DISK_FAULTY
);
939 else if (test_bit(In_sync
, &rdev2
->flags
)) {
940 d
->state
= (1<<MD_DISK_ACTIVE
);
941 d
->state
|= (1<<MD_DISK_SYNC
);
949 if (test_bit(WriteMostly
, &rdev2
->flags
))
950 d
->state
|= (1<<MD_DISK_WRITEMOSTLY
);
952 /* now set the "removed" and "faulty" bits on any missing devices */
953 for (i
=0 ; i
< mddev
->raid_disks
; i
++) {
954 mdp_disk_t
*d
= &sb
->disks
[i
];
955 if (d
->state
== 0 && d
->number
== 0) {
958 d
->state
= (1<<MD_DISK_REMOVED
);
959 d
->state
|= (1<<MD_DISK_FAULTY
);
963 sb
->nr_disks
= nr_disks
;
964 sb
->active_disks
= active
;
965 sb
->working_disks
= working
;
966 sb
->failed_disks
= failed
;
967 sb
->spare_disks
= spare
;
969 sb
->this_disk
= sb
->disks
[rdev
->desc_nr
];
970 sb
->sb_csum
= calc_sb_csum(sb
);
974 * version 1 superblock
977 static unsigned int calc_sb_1_csum(struct mdp_superblock_1
* sb
)
979 unsigned int disk_csum
, csum
;
980 unsigned long long newcsum
;
981 int size
= 256 + le32_to_cpu(sb
->max_dev
)*2;
982 unsigned int *isuper
= (unsigned int*)sb
;
985 disk_csum
= sb
->sb_csum
;
988 for (i
=0; size
>=4; size
-= 4 )
989 newcsum
+= le32_to_cpu(*isuper
++);
992 newcsum
+= le16_to_cpu(*(unsigned short*) isuper
);
994 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
995 sb
->sb_csum
= disk_csum
;
996 return cpu_to_le32(csum
);
999 static int super_1_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
1001 struct mdp_superblock_1
*sb
;
1004 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1008 * Calculate the position of the superblock.
1009 * It is always aligned to a 4K boundary and
1010 * depeding on minor_version, it can be:
1011 * 0: At least 8K, but less than 12K, from end of device
1012 * 1: At start of device
1013 * 2: 4K from start of device.
1015 switch(minor_version
) {
1017 sb_offset
= rdev
->bdev
->bd_inode
->i_size
>> 9;
1019 sb_offset
&= ~(sector_t
)(4*2-1);
1020 /* convert from sectors to K */
1032 rdev
->sb_offset
= sb_offset
;
1034 /* superblock is rarely larger than 1K, but it can be larger,
1035 * and it is safe to read 4k, so we do that
1037 ret
= read_disk_sb(rdev
, 4096);
1038 if (ret
) return ret
;
1041 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1043 if (sb
->magic
!= cpu_to_le32(MD_SB_MAGIC
) ||
1044 sb
->major_version
!= cpu_to_le32(1) ||
1045 le32_to_cpu(sb
->max_dev
) > (4096-256)/2 ||
1046 le64_to_cpu(sb
->super_offset
) != (rdev
->sb_offset
<<1) ||
1047 (le32_to_cpu(sb
->feature_map
) & ~MD_FEATURE_ALL
) != 0)
1050 if (calc_sb_1_csum(sb
) != sb
->sb_csum
) {
1051 printk("md: invalid superblock checksum on %s\n",
1052 bdevname(rdev
->bdev
,b
));
1055 if (le64_to_cpu(sb
->data_size
) < 10) {
1056 printk("md: data_size too small on %s\n",
1057 bdevname(rdev
->bdev
,b
));
1060 rdev
->preferred_minor
= 0xffff;
1061 rdev
->data_offset
= le64_to_cpu(sb
->data_offset
);
1062 atomic_set(&rdev
->corrected_errors
, le32_to_cpu(sb
->cnt_corrected_read
));
1064 rdev
->sb_size
= le32_to_cpu(sb
->max_dev
) * 2 + 256;
1065 bmask
= queue_hardsect_size(rdev
->bdev
->bd_disk
->queue
)-1;
1066 if (rdev
->sb_size
& bmask
)
1067 rdev
-> sb_size
= (rdev
->sb_size
| bmask
)+1;
1073 struct mdp_superblock_1
*refsb
=
1074 (struct mdp_superblock_1
*)page_address(refdev
->sb_page
);
1076 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16) != 0 ||
1077 sb
->level
!= refsb
->level
||
1078 sb
->layout
!= refsb
->layout
||
1079 sb
->chunksize
!= refsb
->chunksize
) {
1080 printk(KERN_WARNING
"md: %s has strangely different"
1081 " superblock to %s\n",
1082 bdevname(rdev
->bdev
,b
),
1083 bdevname(refdev
->bdev
,b2
));
1086 ev1
= le64_to_cpu(sb
->events
);
1087 ev2
= le64_to_cpu(refsb
->events
);
1095 rdev
->size
= ((rdev
->bdev
->bd_inode
->i_size
>>9) - le64_to_cpu(sb
->data_offset
)) / 2;
1097 rdev
->size
= rdev
->sb_offset
;
1098 if (rdev
->size
< le64_to_cpu(sb
->data_size
)/2)
1100 rdev
->size
= le64_to_cpu(sb
->data_size
)/2;
1101 if (le32_to_cpu(sb
->chunksize
))
1102 rdev
->size
&= ~((sector_t
)le32_to_cpu(sb
->chunksize
)/2 - 1);
1104 if (le32_to_cpu(sb
->size
) > rdev
->size
*2)
1109 static int super_1_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1111 struct mdp_superblock_1
*sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1112 __u64 ev1
= le64_to_cpu(sb
->events
);
1114 rdev
->raid_disk
= -1;
1116 if (mddev
->raid_disks
== 0) {
1117 mddev
->major_version
= 1;
1118 mddev
->patch_version
= 0;
1119 mddev
->persistent
= 1;
1120 mddev
->external
= 0;
1121 mddev
->chunk_size
= le32_to_cpu(sb
->chunksize
) << 9;
1122 mddev
->ctime
= le64_to_cpu(sb
->ctime
) & ((1ULL << 32)-1);
1123 mddev
->utime
= le64_to_cpu(sb
->utime
) & ((1ULL << 32)-1);
1124 mddev
->level
= le32_to_cpu(sb
->level
);
1125 mddev
->clevel
[0] = 0;
1126 mddev
->layout
= le32_to_cpu(sb
->layout
);
1127 mddev
->raid_disks
= le32_to_cpu(sb
->raid_disks
);
1128 mddev
->size
= le64_to_cpu(sb
->size
)/2;
1129 mddev
->events
= ev1
;
1130 mddev
->bitmap_offset
= 0;
1131 mddev
->default_bitmap_offset
= 1024 >> 9;
1133 mddev
->recovery_cp
= le64_to_cpu(sb
->resync_offset
);
1134 memcpy(mddev
->uuid
, sb
->set_uuid
, 16);
1136 mddev
->max_disks
= (4096-256)/2;
1138 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BITMAP_OFFSET
) &&
1139 mddev
->bitmap_file
== NULL
) {
1140 if (mddev
->level
!= 1 && mddev
->level
!= 5 && mddev
->level
!= 6
1141 && mddev
->level
!= 10) {
1142 printk(KERN_WARNING
"md: bitmaps not supported for this level.\n");
1145 mddev
->bitmap_offset
= (__s32
)le32_to_cpu(sb
->bitmap_offset
);
1147 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
1148 mddev
->reshape_position
= le64_to_cpu(sb
->reshape_position
);
1149 mddev
->delta_disks
= le32_to_cpu(sb
->delta_disks
);
1150 mddev
->new_level
= le32_to_cpu(sb
->new_level
);
1151 mddev
->new_layout
= le32_to_cpu(sb
->new_layout
);
1152 mddev
->new_chunk
= le32_to_cpu(sb
->new_chunk
)<<9;
1154 mddev
->reshape_position
= MaxSector
;
1155 mddev
->delta_disks
= 0;
1156 mddev
->new_level
= mddev
->level
;
1157 mddev
->new_layout
= mddev
->layout
;
1158 mddev
->new_chunk
= mddev
->chunk_size
;
1161 } else if (mddev
->pers
== NULL
) {
1162 /* Insist of good event counter while assembling */
1164 if (ev1
< mddev
->events
)
1166 } else if (mddev
->bitmap
) {
1167 /* If adding to array with a bitmap, then we can accept an
1168 * older device, but not too old.
1170 if (ev1
< mddev
->bitmap
->events_cleared
)
1173 if (ev1
< mddev
->events
)
1174 /* just a hot-add of a new device, leave raid_disk at -1 */
1177 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1179 rdev
->desc_nr
= le32_to_cpu(sb
->dev_number
);
1180 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
1182 case 0xffff: /* spare */
1184 case 0xfffe: /* faulty */
1185 set_bit(Faulty
, &rdev
->flags
);
1188 if ((le32_to_cpu(sb
->feature_map
) &
1189 MD_FEATURE_RECOVERY_OFFSET
))
1190 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
1192 set_bit(In_sync
, &rdev
->flags
);
1193 rdev
->raid_disk
= role
;
1196 if (sb
->devflags
& WriteMostly1
)
1197 set_bit(WriteMostly
, &rdev
->flags
);
1198 } else /* MULTIPATH are always insync */
1199 set_bit(In_sync
, &rdev
->flags
);
1204 static void super_1_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1206 struct mdp_superblock_1
*sb
;
1207 struct list_head
*tmp
;
1210 /* make rdev->sb match mddev and rdev data. */
1212 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1214 sb
->feature_map
= 0;
1216 sb
->recovery_offset
= cpu_to_le64(0);
1217 memset(sb
->pad1
, 0, sizeof(sb
->pad1
));
1218 memset(sb
->pad2
, 0, sizeof(sb
->pad2
));
1219 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
1221 sb
->utime
= cpu_to_le64((__u64
)mddev
->utime
);
1222 sb
->events
= cpu_to_le64(mddev
->events
);
1224 sb
->resync_offset
= cpu_to_le64(mddev
->recovery_cp
);
1226 sb
->resync_offset
= cpu_to_le64(0);
1228 sb
->cnt_corrected_read
= atomic_read(&rdev
->corrected_errors
);
1230 sb
->raid_disks
= cpu_to_le32(mddev
->raid_disks
);
1231 sb
->size
= cpu_to_le64(mddev
->size
<<1);
1233 if (mddev
->bitmap
&& mddev
->bitmap_file
== NULL
) {
1234 sb
->bitmap_offset
= cpu_to_le32((__u32
)mddev
->bitmap_offset
);
1235 sb
->feature_map
= cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
);
1238 if (rdev
->raid_disk
>= 0 &&
1239 !test_bit(In_sync
, &rdev
->flags
) &&
1240 rdev
->recovery_offset
> 0) {
1241 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET
);
1242 sb
->recovery_offset
= cpu_to_le64(rdev
->recovery_offset
);
1245 if (mddev
->reshape_position
!= MaxSector
) {
1246 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE
);
1247 sb
->reshape_position
= cpu_to_le64(mddev
->reshape_position
);
1248 sb
->new_layout
= cpu_to_le32(mddev
->new_layout
);
1249 sb
->delta_disks
= cpu_to_le32(mddev
->delta_disks
);
1250 sb
->new_level
= cpu_to_le32(mddev
->new_level
);
1251 sb
->new_chunk
= cpu_to_le32(mddev
->new_chunk
>>9);
1255 ITERATE_RDEV(mddev
,rdev2
,tmp
)
1256 if (rdev2
->desc_nr
+1 > max_dev
)
1257 max_dev
= rdev2
->desc_nr
+1;
1259 sb
->max_dev
= cpu_to_le32(max_dev
);
1260 for (i
=0; i
<max_dev
;i
++)
1261 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1263 ITERATE_RDEV(mddev
,rdev2
,tmp
) {
1265 if (test_bit(Faulty
, &rdev2
->flags
))
1266 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1267 else if (test_bit(In_sync
, &rdev2
->flags
))
1268 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1269 else if (rdev2
->raid_disk
>= 0 && rdev2
->recovery_offset
> 0)
1270 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1272 sb
->dev_roles
[i
] = cpu_to_le16(0xffff);
1275 sb
->sb_csum
= calc_sb_1_csum(sb
);
1279 static struct super_type super_types
[] = {
1282 .owner
= THIS_MODULE
,
1283 .load_super
= super_90_load
,
1284 .validate_super
= super_90_validate
,
1285 .sync_super
= super_90_sync
,
1289 .owner
= THIS_MODULE
,
1290 .load_super
= super_1_load
,
1291 .validate_super
= super_1_validate
,
1292 .sync_super
= super_1_sync
,
1296 static mdk_rdev_t
* match_dev_unit(mddev_t
*mddev
, mdk_rdev_t
*dev
)
1298 struct list_head
*tmp
;
1301 ITERATE_RDEV(mddev
,rdev
,tmp
)
1302 if (rdev
->bdev
->bd_contains
== dev
->bdev
->bd_contains
)
1308 static int match_mddev_units(mddev_t
*mddev1
, mddev_t
*mddev2
)
1310 struct list_head
*tmp
;
1313 ITERATE_RDEV(mddev1
,rdev
,tmp
)
1314 if (match_dev_unit(mddev2
, rdev
))
1320 static LIST_HEAD(pending_raid_disks
);
1322 static int bind_rdev_to_array(mdk_rdev_t
* rdev
, mddev_t
* mddev
)
1324 mdk_rdev_t
*same_pdev
;
1325 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1333 /* make sure rdev->size exceeds mddev->size */
1334 if (rdev
->size
&& (mddev
->size
== 0 || rdev
->size
< mddev
->size
)) {
1336 /* Cannot change size, so fail */
1339 mddev
->size
= rdev
->size
;
1341 same_pdev
= match_dev_unit(mddev
, rdev
);
1344 "%s: WARNING: %s appears to be on the same physical"
1345 " disk as %s. True\n protection against single-disk"
1346 " failure might be compromised.\n",
1347 mdname(mddev
), bdevname(rdev
->bdev
,b
),
1348 bdevname(same_pdev
->bdev
,b2
));
1350 /* Verify rdev->desc_nr is unique.
1351 * If it is -1, assign a free number, else
1352 * check number is not in use
1354 if (rdev
->desc_nr
< 0) {
1356 if (mddev
->pers
) choice
= mddev
->raid_disks
;
1357 while (find_rdev_nr(mddev
, choice
))
1359 rdev
->desc_nr
= choice
;
1361 if (find_rdev_nr(mddev
, rdev
->desc_nr
))
1364 bdevname(rdev
->bdev
,b
);
1365 if (kobject_set_name(&rdev
->kobj
, "dev-%s", b
) < 0)
1367 while ( (s
=strchr(rdev
->kobj
.k_name
, '/')) != NULL
)
1370 list_add(&rdev
->same_set
, &mddev
->disks
);
1371 rdev
->mddev
= mddev
;
1372 printk(KERN_INFO
"md: bind<%s>\n", b
);
1374 rdev
->kobj
.parent
= &mddev
->kobj
;
1375 kobject_add(&rdev
->kobj
);
1377 if (rdev
->bdev
->bd_part
)
1378 ko
= &rdev
->bdev
->bd_part
->kobj
;
1380 ko
= &rdev
->bdev
->bd_disk
->kobj
;
1381 sysfs_create_link(&rdev
->kobj
, ko
, "block");
1382 bd_claim_by_disk(rdev
->bdev
, rdev
, mddev
->gendisk
);
1386 static void unbind_rdev_from_array(mdk_rdev_t
* rdev
)
1388 char b
[BDEVNAME_SIZE
];
1393 bd_release_from_disk(rdev
->bdev
, rdev
->mddev
->gendisk
);
1394 list_del_init(&rdev
->same_set
);
1395 printk(KERN_INFO
"md: unbind<%s>\n", bdevname(rdev
->bdev
,b
));
1397 sysfs_remove_link(&rdev
->kobj
, "block");
1398 kobject_del(&rdev
->kobj
);
1402 * prevent the device from being mounted, repartitioned or
1403 * otherwise reused by a RAID array (or any other kernel
1404 * subsystem), by bd_claiming the device.
1406 static int lock_rdev(mdk_rdev_t
*rdev
, dev_t dev
)
1409 struct block_device
*bdev
;
1410 char b
[BDEVNAME_SIZE
];
1412 bdev
= open_partition_by_devnum(dev
, FMODE_READ
|FMODE_WRITE
);
1414 printk(KERN_ERR
"md: could not open %s.\n",
1415 __bdevname(dev
, b
));
1416 return PTR_ERR(bdev
);
1418 err
= bd_claim(bdev
, rdev
);
1420 printk(KERN_ERR
"md: could not bd_claim %s.\n",
1422 blkdev_put_partition(bdev
);
1429 static void unlock_rdev(mdk_rdev_t
*rdev
)
1431 struct block_device
*bdev
= rdev
->bdev
;
1436 blkdev_put_partition(bdev
);
1439 void md_autodetect_dev(dev_t dev
);
1441 static void export_rdev(mdk_rdev_t
* rdev
)
1443 char b
[BDEVNAME_SIZE
];
1444 printk(KERN_INFO
"md: export_rdev(%s)\n",
1445 bdevname(rdev
->bdev
,b
));
1449 list_del_init(&rdev
->same_set
);
1451 md_autodetect_dev(rdev
->bdev
->bd_dev
);
1454 kobject_put(&rdev
->kobj
);
1457 static void kick_rdev_from_array(mdk_rdev_t
* rdev
)
1459 unbind_rdev_from_array(rdev
);
1463 static void export_array(mddev_t
*mddev
)
1465 struct list_head
*tmp
;
1468 ITERATE_RDEV(mddev
,rdev
,tmp
) {
1473 kick_rdev_from_array(rdev
);
1475 if (!list_empty(&mddev
->disks
))
1477 mddev
->raid_disks
= 0;
1478 mddev
->major_version
= 0;
1481 static void print_desc(mdp_disk_t
*desc
)
1483 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc
->number
,
1484 desc
->major
,desc
->minor
,desc
->raid_disk
,desc
->state
);
1487 static void print_sb(mdp_super_t
*sb
)
1492 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1493 sb
->major_version
, sb
->minor_version
, sb
->patch_version
,
1494 sb
->set_uuid0
, sb
->set_uuid1
, sb
->set_uuid2
, sb
->set_uuid3
,
1496 printk(KERN_INFO
"md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1497 sb
->level
, sb
->size
, sb
->nr_disks
, sb
->raid_disks
,
1498 sb
->md_minor
, sb
->layout
, sb
->chunk_size
);
1499 printk(KERN_INFO
"md: UT:%08x ST:%d AD:%d WD:%d"
1500 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1501 sb
->utime
, sb
->state
, sb
->active_disks
, sb
->working_disks
,
1502 sb
->failed_disks
, sb
->spare_disks
,
1503 sb
->sb_csum
, (unsigned long)sb
->events_lo
);
1506 for (i
= 0; i
< MD_SB_DISKS
; i
++) {
1509 desc
= sb
->disks
+ i
;
1510 if (desc
->number
|| desc
->major
|| desc
->minor
||
1511 desc
->raid_disk
|| (desc
->state
&& (desc
->state
!= 4))) {
1512 printk(" D %2d: ", i
);
1516 printk(KERN_INFO
"md: THIS: ");
1517 print_desc(&sb
->this_disk
);
1521 static void print_rdev(mdk_rdev_t
*rdev
)
1523 char b
[BDEVNAME_SIZE
];
1524 printk(KERN_INFO
"md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1525 bdevname(rdev
->bdev
,b
), (unsigned long long)rdev
->size
,
1526 test_bit(Faulty
, &rdev
->flags
), test_bit(In_sync
, &rdev
->flags
),
1528 if (rdev
->sb_loaded
) {
1529 printk(KERN_INFO
"md: rdev superblock:\n");
1530 print_sb((mdp_super_t
*)page_address(rdev
->sb_page
));
1532 printk(KERN_INFO
"md: no rdev superblock!\n");
1535 static void md_print_devices(void)
1537 struct list_head
*tmp
, *tmp2
;
1540 char b
[BDEVNAME_SIZE
];
1543 printk("md: **********************************\n");
1544 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1545 printk("md: **********************************\n");
1546 ITERATE_MDDEV(mddev
,tmp
) {
1549 bitmap_print_sb(mddev
->bitmap
);
1551 printk("%s: ", mdname(mddev
));
1552 ITERATE_RDEV(mddev
,rdev
,tmp2
)
1553 printk("<%s>", bdevname(rdev
->bdev
,b
));
1556 ITERATE_RDEV(mddev
,rdev
,tmp2
)
1559 printk("md: **********************************\n");
1564 static void sync_sbs(mddev_t
* mddev
, int nospares
)
1566 /* Update each superblock (in-memory image), but
1567 * if we are allowed to, skip spares which already
1568 * have the right event counter, or have one earlier
1569 * (which would mean they aren't being marked as dirty
1570 * with the rest of the array)
1573 struct list_head
*tmp
;
1575 ITERATE_RDEV(mddev
,rdev
,tmp
) {
1576 if (rdev
->sb_events
== mddev
->events
||
1578 rdev
->raid_disk
< 0 &&
1579 (rdev
->sb_events
&1)==0 &&
1580 rdev
->sb_events
+1 == mddev
->events
)) {
1581 /* Don't update this superblock */
1582 rdev
->sb_loaded
= 2;
1584 super_types
[mddev
->major_version
].
1585 sync_super(mddev
, rdev
);
1586 rdev
->sb_loaded
= 1;
1591 void md_update_sb(mddev_t
* mddev
)
1594 struct list_head
*tmp
;
1600 spin_lock_irq(&mddev
->write_lock
);
1601 sync_req
= mddev
->in_sync
;
1602 mddev
->utime
= get_seconds();
1603 if (mddev
->sb_dirty
== 3)
1604 /* just a clean<-> dirty transition, possibly leave spares alone,
1605 * though if events isn't the right even/odd, we will have to do
1610 /* If this is just a dirty<->clean transition, and the array is clean
1611 * and 'events' is odd, we can roll back to the previous clean state */
1612 if (mddev
->sb_dirty
== 3
1613 && (mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
1614 && (mddev
->events
& 1))
1617 /* otherwise we have to go forward and ... */
1619 if (!mddev
->in_sync
|| mddev
->recovery_cp
!= MaxSector
) { /* not clean */
1620 /* .. if the array isn't clean, insist on an odd 'events' */
1621 if ((mddev
->events
&1)==0) {
1626 /* otherwise insist on an even 'events' (for clean states) */
1627 if ((mddev
->events
&1)) {
1634 if (!mddev
->events
) {
1636 * oops, this 64-bit counter should never wrap.
1637 * Either we are in around ~1 trillion A.C., assuming
1638 * 1 reboot per second, or we have a bug:
1645 * do not write anything to disk if using
1646 * nonpersistent superblocks
1648 if (!mddev
->persistent
) {
1649 if (!mddev
->external
)
1650 mddev
->sb_dirty
= 0;
1651 spin_unlock_irq(&mddev
->write_lock
);
1652 wake_up(&mddev
->sb_wait
);
1655 mddev
->sb_dirty
= 2;
1656 sync_sbs(mddev
, nospares
);
1657 spin_unlock_irq(&mddev
->write_lock
);
1660 "md: updating %s RAID superblock on device (in sync %d)\n",
1661 mdname(mddev
),mddev
->in_sync
);
1663 err
= bitmap_update_sb(mddev
->bitmap
);
1664 ITERATE_RDEV(mddev
,rdev
,tmp
) {
1665 char b
[BDEVNAME_SIZE
];
1666 dprintk(KERN_INFO
"md: ");
1667 if (rdev
->sb_loaded
!= 1)
1668 continue; /* no noise on spare devices */
1669 if (test_bit(Faulty
, &rdev
->flags
))
1670 dprintk("(skipping faulty ");
1672 dprintk("%s ", bdevname(rdev
->bdev
,b
));
1673 if (!test_bit(Faulty
, &rdev
->flags
)) {
1674 md_super_write(mddev
,rdev
,
1675 rdev
->sb_offset
<<1, rdev
->sb_size
,
1677 dprintk(KERN_INFO
"(write) %s's sb offset: %llu\n",
1678 bdevname(rdev
->bdev
,b
),
1679 (unsigned long long)rdev
->sb_offset
);
1680 rdev
->sb_events
= mddev
->events
;
1684 if (mddev
->level
== LEVEL_MULTIPATH
)
1685 /* only need to write one superblock... */
1688 md_super_wait(mddev
);
1689 /* if there was a failure, sb_dirty was set to 1, and we re-write super */
1691 spin_lock_irq(&mddev
->write_lock
);
1692 if (mddev
->in_sync
!= sync_req
|| mddev
->sb_dirty
== 1) {
1693 /* have to write it out again */
1694 spin_unlock_irq(&mddev
->write_lock
);
1697 mddev
->sb_dirty
= 0;
1698 spin_unlock_irq(&mddev
->write_lock
);
1699 wake_up(&mddev
->sb_wait
);
1702 EXPORT_SYMBOL_GPL(md_update_sb
);
1704 /* words written to sysfs files may, or my not, be \n terminated.
1705 * We want to accept with case. For this we use cmd_match.
1707 static int cmd_match(const char *cmd
, const char *str
)
1709 /* See if cmd, written into a sysfs file, matches
1710 * str. They must either be the same, or cmd can
1711 * have a trailing newline
1713 while (*cmd
&& *str
&& *cmd
== *str
) {
1724 struct rdev_sysfs_entry
{
1725 struct attribute attr
;
1726 ssize_t (*show
)(mdk_rdev_t
*, char *);
1727 ssize_t (*store
)(mdk_rdev_t
*, const char *, size_t);
1731 state_show(mdk_rdev_t
*rdev
, char *page
)
1736 if (test_bit(Faulty
, &rdev
->flags
)) {
1737 len
+= sprintf(page
+len
, "%sfaulty",sep
);
1740 if (test_bit(In_sync
, &rdev
->flags
)) {
1741 len
+= sprintf(page
+len
, "%sin_sync",sep
);
1744 if (test_bit(WriteMostly
, &rdev
->flags
)) {
1745 len
+= sprintf(page
+len
, "%swrite_mostly",sep
);
1748 if (!test_bit(Faulty
, &rdev
->flags
) &&
1749 !test_bit(In_sync
, &rdev
->flags
)) {
1750 len
+= sprintf(page
+len
, "%sspare", sep
);
1753 return len
+sprintf(page
+len
, "\n");
1757 state_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
1760 * faulty - simulates and error
1761 * remove - disconnects the device
1762 * writemostly - sets write_mostly
1763 * -writemostly - clears write_mostly
1766 if (cmd_match(buf
, "faulty") && rdev
->mddev
->pers
) {
1767 md_error(rdev
->mddev
, rdev
);
1769 } else if (cmd_match(buf
, "remove")) {
1770 if (rdev
->raid_disk
>= 0)
1773 mddev_t
*mddev
= rdev
->mddev
;
1774 kick_rdev_from_array(rdev
);
1775 md_update_sb(mddev
);
1776 md_new_event(mddev
);
1779 } else if (cmd_match(buf
, "writemostly")) {
1780 set_bit(WriteMostly
, &rdev
->flags
);
1782 } else if (cmd_match(buf
, "-writemostly")) {
1783 clear_bit(WriteMostly
, &rdev
->flags
);
1786 return err
? err
: len
;
1788 static struct rdev_sysfs_entry rdev_state
=
1789 __ATTR(state
, S_IRUGO
|S_IWUSR
, state_show
, state_store
);
1792 super_show(mdk_rdev_t
*rdev
, char *page
)
1794 if (rdev
->sb_loaded
&& rdev
->sb_size
) {
1795 memcpy(page
, page_address(rdev
->sb_page
), rdev
->sb_size
);
1796 return rdev
->sb_size
;
1800 static struct rdev_sysfs_entry rdev_super
= __ATTR_RO(super
);
1803 errors_show(mdk_rdev_t
*rdev
, char *page
)
1805 return sprintf(page
, "%d\n", atomic_read(&rdev
->corrected_errors
));
1809 errors_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
1812 unsigned long n
= simple_strtoul(buf
, &e
, 10);
1813 if (*buf
&& (*e
== 0 || *e
== '\n')) {
1814 atomic_set(&rdev
->corrected_errors
, n
);
1819 static struct rdev_sysfs_entry rdev_errors
=
1820 __ATTR(errors
, S_IRUGO
|S_IWUSR
, errors_show
, errors_store
);
1823 slot_show(mdk_rdev_t
*rdev
, char *page
)
1825 if (rdev
->raid_disk
< 0)
1826 return sprintf(page
, "none\n");
1828 return sprintf(page
, "%d\n", rdev
->raid_disk
);
1832 slot_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
1835 int slot
= simple_strtoul(buf
, &e
, 10);
1836 if (strncmp(buf
, "none", 4)==0)
1838 else if (e
==buf
|| (*e
&& *e
!= '\n'))
1840 if (rdev
->mddev
->pers
)
1841 /* Cannot set slot in active array (yet) */
1843 if (slot
>= rdev
->mddev
->raid_disks
)
1845 rdev
->raid_disk
= slot
;
1846 /* assume it is working */
1848 set_bit(In_sync
, &rdev
->flags
);
1853 static struct rdev_sysfs_entry rdev_slot
=
1854 __ATTR(slot
, S_IRUGO
|S_IWUSR
, slot_show
, slot_store
);
1857 offset_show(mdk_rdev_t
*rdev
, char *page
)
1859 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->data_offset
);
1863 offset_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
1866 unsigned long long offset
= simple_strtoull(buf
, &e
, 10);
1867 if (e
==buf
|| (*e
&& *e
!= '\n'))
1869 if (rdev
->mddev
->pers
)
1871 rdev
->data_offset
= offset
;
1875 static struct rdev_sysfs_entry rdev_offset
=
1876 __ATTR(offset
, S_IRUGO
|S_IWUSR
, offset_show
, offset_store
);
1879 rdev_size_show(mdk_rdev_t
*rdev
, char *page
)
1881 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->size
);
1885 rdev_size_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
1888 unsigned long long size
= simple_strtoull(buf
, &e
, 10);
1889 if (e
==buf
|| (*e
&& *e
!= '\n'))
1891 if (rdev
->mddev
->pers
)
1894 if (size
< rdev
->mddev
->size
|| rdev
->mddev
->size
== 0)
1895 rdev
->mddev
->size
= size
;
1899 static struct rdev_sysfs_entry rdev_size
=
1900 __ATTR(size
, S_IRUGO
|S_IWUSR
, rdev_size_show
, rdev_size_store
);
1902 static struct attribute
*rdev_default_attrs
[] = {
1912 rdev_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
1914 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
1915 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
1919 return entry
->show(rdev
, page
);
1923 rdev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
1924 const char *page
, size_t length
)
1926 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
1927 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
1931 if (!capable(CAP_SYS_ADMIN
))
1933 return entry
->store(rdev
, page
, length
);
1936 static void rdev_free(struct kobject
*ko
)
1938 mdk_rdev_t
*rdev
= container_of(ko
, mdk_rdev_t
, kobj
);
1941 static struct sysfs_ops rdev_sysfs_ops
= {
1942 .show
= rdev_attr_show
,
1943 .store
= rdev_attr_store
,
1945 static struct kobj_type rdev_ktype
= {
1946 .release
= rdev_free
,
1947 .sysfs_ops
= &rdev_sysfs_ops
,
1948 .default_attrs
= rdev_default_attrs
,
1952 * Import a device. If 'super_format' >= 0, then sanity check the superblock
1954 * mark the device faulty if:
1956 * - the device is nonexistent (zero size)
1957 * - the device has no valid superblock
1959 * a faulty rdev _never_ has rdev->sb set.
1961 static mdk_rdev_t
*md_import_device(dev_t newdev
, int super_format
, int super_minor
)
1963 char b
[BDEVNAME_SIZE
];
1968 rdev
= kzalloc(sizeof(*rdev
), GFP_KERNEL
);
1970 printk(KERN_ERR
"md: could not alloc mem for new device!\n");
1971 return ERR_PTR(-ENOMEM
);
1974 if ((err
= alloc_disk_sb(rdev
)))
1977 err
= lock_rdev(rdev
, newdev
);
1981 rdev
->kobj
.parent
= NULL
;
1982 rdev
->kobj
.ktype
= &rdev_ktype
;
1983 kobject_init(&rdev
->kobj
);
1987 rdev
->data_offset
= 0;
1988 rdev
->sb_events
= 0;
1989 atomic_set(&rdev
->nr_pending
, 0);
1990 atomic_set(&rdev
->read_errors
, 0);
1991 atomic_set(&rdev
->corrected_errors
, 0);
1993 size
= rdev
->bdev
->bd_inode
->i_size
>> BLOCK_SIZE_BITS
;
1996 "md: %s has zero or unknown size, marking faulty!\n",
1997 bdevname(rdev
->bdev
,b
));
2002 if (super_format
>= 0) {
2003 err
= super_types
[super_format
].
2004 load_super(rdev
, NULL
, super_minor
);
2005 if (err
== -EINVAL
) {
2007 "md: %s has invalid sb, not importing!\n",
2008 bdevname(rdev
->bdev
,b
));
2013 "md: could not read %s's sb, not importing!\n",
2014 bdevname(rdev
->bdev
,b
));
2018 INIT_LIST_HEAD(&rdev
->same_set
);
2023 if (rdev
->sb_page
) {
2029 return ERR_PTR(err
);
2033 * Check a full RAID array for plausibility
2037 static void analyze_sbs(mddev_t
* mddev
)
2040 struct list_head
*tmp
;
2041 mdk_rdev_t
*rdev
, *freshest
;
2042 char b
[BDEVNAME_SIZE
];
2045 ITERATE_RDEV(mddev
,rdev
,tmp
)
2046 switch (super_types
[mddev
->major_version
].
2047 load_super(rdev
, freshest
, mddev
->minor_version
)) {
2055 "md: fatal superblock inconsistency in %s"
2056 " -- removing from array\n",
2057 bdevname(rdev
->bdev
,b
));
2058 kick_rdev_from_array(rdev
);
2062 super_types
[mddev
->major_version
].
2063 validate_super(mddev
, freshest
);
2066 ITERATE_RDEV(mddev
,rdev
,tmp
) {
2067 if (rdev
!= freshest
)
2068 if (super_types
[mddev
->major_version
].
2069 validate_super(mddev
, rdev
)) {
2070 printk(KERN_WARNING
"md: kicking non-fresh %s"
2072 bdevname(rdev
->bdev
,b
));
2073 kick_rdev_from_array(rdev
);
2076 if (mddev
->level
== LEVEL_MULTIPATH
) {
2077 rdev
->desc_nr
= i
++;
2078 rdev
->raid_disk
= rdev
->desc_nr
;
2079 set_bit(In_sync
, &rdev
->flags
);
2085 if (mddev
->recovery_cp
!= MaxSector
&&
2087 printk(KERN_ERR
"md: %s: raid array is not clean"
2088 " -- starting background reconstruction\n",
2094 safe_delay_show(mddev_t
*mddev
, char *page
)
2096 int msec
= (mddev
->safemode_delay
*1000)/HZ
;
2097 return sprintf(page
, "%d.%03d\n", msec
/1000, msec
%1000);
2100 safe_delay_store(mddev_t
*mddev
, const char *cbuf
, size_t len
)
2108 /* remove a period, and count digits after it */
2109 if (len
>= sizeof(buf
))
2111 strlcpy(buf
, cbuf
, len
);
2113 for (i
=0; i
<len
; i
++) {
2115 if (isdigit(buf
[i
])) {
2120 } else if (buf
[i
] == '.') {
2125 msec
= simple_strtoul(buf
, &e
, 10);
2126 if (e
== buf
|| (*e
&& *e
!= '\n'))
2128 msec
= (msec
* 1000) / scale
;
2130 mddev
->safemode_delay
= 0;
2132 mddev
->safemode_delay
= (msec
*HZ
)/1000;
2133 if (mddev
->safemode_delay
== 0)
2134 mddev
->safemode_delay
= 1;
2138 static struct md_sysfs_entry md_safe_delay
=
2139 __ATTR(safe_mode_delay
, S_IRUGO
|S_IWUSR
,safe_delay_show
, safe_delay_store
);
2142 level_show(mddev_t
*mddev
, char *page
)
2144 struct mdk_personality
*p
= mddev
->pers
;
2146 return sprintf(page
, "%s\n", p
->name
);
2147 else if (mddev
->clevel
[0])
2148 return sprintf(page
, "%s\n", mddev
->clevel
);
2149 else if (mddev
->level
!= LEVEL_NONE
)
2150 return sprintf(page
, "%d\n", mddev
->level
);
2156 level_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2163 if (len
>= sizeof(mddev
->clevel
))
2165 strncpy(mddev
->clevel
, buf
, len
);
2166 if (mddev
->clevel
[len
-1] == '\n')
2168 mddev
->clevel
[len
] = 0;
2169 mddev
->level
= LEVEL_NONE
;
2173 static struct md_sysfs_entry md_level
=
2174 __ATTR(level
, S_IRUGO
|S_IWUSR
, level_show
, level_store
);
2178 layout_show(mddev_t
*mddev
, char *page
)
2180 /* just a number, not meaningful for all levels */
2181 return sprintf(page
, "%d\n", mddev
->layout
);
2185 layout_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2188 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2192 if (!*buf
|| (*e
&& *e
!= '\n'))
2198 static struct md_sysfs_entry md_layout
=
2199 __ATTR(layout
, S_IRUGO
|S_IWUSR
, layout_show
, layout_store
);
2203 raid_disks_show(mddev_t
*mddev
, char *page
)
2205 if (mddev
->raid_disks
== 0)
2207 return sprintf(page
, "%d\n", mddev
->raid_disks
);
2210 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
);
2213 raid_disks_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2215 /* can only set raid_disks if array is not yet active */
2218 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2220 if (!*buf
|| (*e
&& *e
!= '\n'))
2224 rv
= update_raid_disks(mddev
, n
);
2226 mddev
->raid_disks
= n
;
2227 return rv
? rv
: len
;
2229 static struct md_sysfs_entry md_raid_disks
=
2230 __ATTR(raid_disks
, S_IRUGO
|S_IWUSR
, raid_disks_show
, raid_disks_store
);
2233 chunk_size_show(mddev_t
*mddev
, char *page
)
2235 return sprintf(page
, "%d\n", mddev
->chunk_size
);
2239 chunk_size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2241 /* can only set chunk_size if array is not yet active */
2243 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2247 if (!*buf
|| (*e
&& *e
!= '\n'))
2250 mddev
->chunk_size
= n
;
2253 static struct md_sysfs_entry md_chunk_size
=
2254 __ATTR(chunk_size
, S_IRUGO
|S_IWUSR
, chunk_size_show
, chunk_size_store
);
2257 resync_start_show(mddev_t
*mddev
, char *page
)
2259 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->recovery_cp
);
2263 resync_start_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2265 /* can only set chunk_size if array is not yet active */
2267 unsigned long long n
= simple_strtoull(buf
, &e
, 10);
2271 if (!*buf
|| (*e
&& *e
!= '\n'))
2274 mddev
->recovery_cp
= n
;
2277 static struct md_sysfs_entry md_resync_start
=
2278 __ATTR(resync_start
, S_IRUGO
|S_IWUSR
, resync_start_show
, resync_start_store
);
2281 * The array state can be:
2284 * No devices, no size, no level
2285 * Equivalent to STOP_ARRAY ioctl
2287 * May have some settings, but array is not active
2288 * all IO results in error
2289 * When written, doesn't tear down array, but just stops it
2290 * suspended (not supported yet)
2291 * All IO requests will block. The array can be reconfigured.
2292 * Writing this, if accepted, will block until array is quiessent
2294 * no resync can happen. no superblocks get written.
2295 * write requests fail
2297 * like readonly, but behaves like 'clean' on a write request.
2299 * clean - no pending writes, but otherwise active.
2300 * When written to inactive array, starts without resync
2301 * If a write request arrives then
2302 * if metadata is known, mark 'dirty' and switch to 'active'.
2303 * if not known, block and switch to write-pending
2304 * If written to an active array that has pending writes, then fails.
2306 * fully active: IO and resync can be happening.
2307 * When written to inactive array, starts with resync
2310 * clean, but writes are blocked waiting for 'active' to be written.
2313 * like active, but no writes have been seen for a while (100msec).
2316 enum array_state
{ clear
, inactive
, suspended
, readonly
, read_auto
, clean
, active
,
2317 write_pending
, active_idle
, bad_word
};
2318 char *array_states
[] = {
2319 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2320 "write-pending", "active-idle", NULL
};
2322 static int match_word(const char *word
, char **list
)
2325 for (n
=0; list
[n
]; n
++)
2326 if (cmd_match(word
, list
[n
]))
2332 array_state_show(mddev_t
*mddev
, char *page
)
2334 enum array_state st
= inactive
;
2347 else if (mddev
->sb_dirty
)
2349 else if (mddev
->safemode
)
2355 if (list_empty(&mddev
->disks
) &&
2356 mddev
->raid_disks
== 0 &&
2362 return sprintf(page
, "%s\n", array_states
[st
]);
2365 static int do_md_stop(mddev_t
* mddev
, int ro
);
2366 static int do_md_run(mddev_t
* mddev
);
2367 static int restart_array(mddev_t
*mddev
);
2370 array_state_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2373 enum array_state st
= match_word(buf
, array_states
);
2378 /* stopping an active array */
2379 if (atomic_read(&mddev
->active
) > 1)
2381 err
= do_md_stop(mddev
, 0);
2384 /* stopping an active array */
2386 if (atomic_read(&mddev
->active
) > 1)
2388 err
= do_md_stop(mddev
, 2);
2390 err
= 0; /* already inactive */
2393 break; /* not supported yet */
2396 err
= do_md_stop(mddev
, 1);
2399 err
= do_md_run(mddev
);
2403 /* stopping an active array */
2405 err
= do_md_stop(mddev
, 1);
2407 mddev
->ro
= 2; /* FIXME mark devices writable */
2410 err
= do_md_run(mddev
);
2415 restart_array(mddev
);
2416 spin_lock_irq(&mddev
->write_lock
);
2417 if (atomic_read(&mddev
->writes_pending
) == 0) {
2418 if (mddev
->in_sync
== 0) {
2420 if (mddev
->persistent
)
2421 mddev
->sb_dirty
= 1;
2426 spin_unlock_irq(&mddev
->write_lock
);
2429 mddev
->recovery_cp
= MaxSector
;
2430 err
= do_md_run(mddev
);
2435 restart_array(mddev
);
2436 mddev
->sb_dirty
= 0;
2437 wake_up(&mddev
->sb_wait
);
2441 err
= do_md_run(mddev
);
2446 /* these cannot be set */
2454 static struct md_sysfs_entry md_array_state
=
2455 __ATTR(array_state
, S_IRUGO
|S_IWUSR
, array_state_show
, array_state_store
);
2458 null_show(mddev_t
*mddev
, char *page
)
2464 new_dev_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2466 /* buf must be %d:%d\n? giving major and minor numbers */
2467 /* The new device is added to the array.
2468 * If the array has a persistent superblock, we read the
2469 * superblock to initialise info and check validity.
2470 * Otherwise, only checking done is that in bind_rdev_to_array,
2471 * which mainly checks size.
2474 int major
= simple_strtoul(buf
, &e
, 10);
2480 if (!*buf
|| *e
!= ':' || !e
[1] || e
[1] == '\n')
2482 minor
= simple_strtoul(e
+1, &e
, 10);
2483 if (*e
&& *e
!= '\n')
2485 dev
= MKDEV(major
, minor
);
2486 if (major
!= MAJOR(dev
) ||
2487 minor
!= MINOR(dev
))
2491 if (mddev
->persistent
) {
2492 rdev
= md_import_device(dev
, mddev
->major_version
,
2493 mddev
->minor_version
);
2494 if (!IS_ERR(rdev
) && !list_empty(&mddev
->disks
)) {
2495 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
2496 mdk_rdev_t
, same_set
);
2497 err
= super_types
[mddev
->major_version
]
2498 .load_super(rdev
, rdev0
, mddev
->minor_version
);
2503 rdev
= md_import_device(dev
, -1, -1);
2506 return PTR_ERR(rdev
);
2507 err
= bind_rdev_to_array(rdev
, mddev
);
2511 return err
? err
: len
;
2514 static struct md_sysfs_entry md_new_device
=
2515 __ATTR(new_dev
, S_IWUSR
, null_show
, new_dev_store
);
2518 size_show(mddev_t
*mddev
, char *page
)
2520 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->size
);
2523 static int update_size(mddev_t
*mddev
, unsigned long size
);
2526 size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2528 /* If array is inactive, we can reduce the component size, but
2529 * not increase it (except from 0).
2530 * If array is active, we can try an on-line resize
2534 unsigned long long size
= simple_strtoull(buf
, &e
, 10);
2535 if (!*buf
|| *buf
== '\n' ||
2540 err
= update_size(mddev
, size
);
2541 md_update_sb(mddev
);
2543 if (mddev
->size
== 0 ||
2549 return err
? err
: len
;
2552 static struct md_sysfs_entry md_size
=
2553 __ATTR(component_size
, S_IRUGO
|S_IWUSR
, size_show
, size_store
);
2558 * 'none' for arrays with no metadata (good luck...)
2559 * 'external' for arrays with externally managed metadata,
2560 * or N.M for internally known formats
2563 metadata_show(mddev_t
*mddev
, char *page
)
2565 if (mddev
->persistent
)
2566 return sprintf(page
, "%d.%d\n",
2567 mddev
->major_version
, mddev
->minor_version
);
2568 else if (mddev
->external
)
2569 return sprintf(page
, "external\n");
2571 return sprintf(page
, "none\n");
2575 metadata_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2579 if (!list_empty(&mddev
->disks
))
2582 if (cmd_match(buf
, "none")) {
2583 mddev
->persistent
= 0;
2584 mddev
->external
= 0;
2585 mddev
->major_version
= 0;
2586 mddev
->minor_version
= 90;
2589 if (cmd_match(buf
, "external")) {
2590 mddev
->persistent
= 0;
2591 mddev
->external
= 1;
2592 mddev
->major_version
= 0;
2593 mddev
->minor_version
= 90;
2596 major
= simple_strtoul(buf
, &e
, 10);
2597 if (e
==buf
|| *e
!= '.')
2600 minor
= simple_strtoul(buf
, &e
, 10);
2601 if (e
==buf
|| *e
!= '\n')
2603 if (major
>= sizeof(super_types
)/sizeof(super_types
[0]) ||
2604 super_types
[major
].name
== NULL
)
2606 mddev
->major_version
= major
;
2607 mddev
->minor_version
= minor
;
2608 mddev
->persistent
= 1;
2609 mddev
->external
= 0;
2613 static struct md_sysfs_entry md_metadata
=
2614 __ATTR(metadata_version
, S_IRUGO
|S_IWUSR
, metadata_show
, metadata_store
);
2617 action_show(mddev_t
*mddev
, char *page
)
2619 char *type
= "idle";
2620 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
2621 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
)) {
2622 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
2624 else if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
2625 if (!test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
2627 else if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
2634 return sprintf(page
, "%s\n", type
);
2638 action_store(mddev_t
*mddev
, const char *page
, size_t len
)
2640 if (!mddev
->pers
|| !mddev
->pers
->sync_request
)
2643 if (cmd_match(page
, "idle")) {
2644 if (mddev
->sync_thread
) {
2645 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
2646 md_unregister_thread(mddev
->sync_thread
);
2647 mddev
->sync_thread
= NULL
;
2648 mddev
->recovery
= 0;
2650 } else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
2651 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
2653 else if (cmd_match(page
, "resync") || cmd_match(page
, "recover"))
2654 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
2655 else if (cmd_match(page
, "reshape")) {
2657 if (mddev
->pers
->start_reshape
== NULL
)
2659 err
= mddev
->pers
->start_reshape(mddev
);
2663 if (cmd_match(page
, "check"))
2664 set_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
2665 else if (!cmd_match(page
, "repair"))
2667 set_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
2668 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
2670 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
2671 md_wakeup_thread(mddev
->thread
);
2676 mismatch_cnt_show(mddev_t
*mddev
, char *page
)
2678 return sprintf(page
, "%llu\n",
2679 (unsigned long long) mddev
->resync_mismatches
);
2682 static struct md_sysfs_entry md_scan_mode
=
2683 __ATTR(sync_action
, S_IRUGO
|S_IWUSR
, action_show
, action_store
);
2686 static struct md_sysfs_entry md_mismatches
= __ATTR_RO(mismatch_cnt
);
2689 sync_min_show(mddev_t
*mddev
, char *page
)
2691 return sprintf(page
, "%d (%s)\n", speed_min(mddev
),
2692 mddev
->sync_speed_min
? "local": "system");
2696 sync_min_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2700 if (strncmp(buf
, "system", 6)==0) {
2701 mddev
->sync_speed_min
= 0;
2704 min
= simple_strtoul(buf
, &e
, 10);
2705 if (buf
== e
|| (*e
&& *e
!= '\n') || min
<= 0)
2707 mddev
->sync_speed_min
= min
;
2711 static struct md_sysfs_entry md_sync_min
=
2712 __ATTR(sync_speed_min
, S_IRUGO
|S_IWUSR
, sync_min_show
, sync_min_store
);
2715 sync_max_show(mddev_t
*mddev
, char *page
)
2717 return sprintf(page
, "%d (%s)\n", speed_max(mddev
),
2718 mddev
->sync_speed_max
? "local": "system");
2722 sync_max_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2726 if (strncmp(buf
, "system", 6)==0) {
2727 mddev
->sync_speed_max
= 0;
2730 max
= simple_strtoul(buf
, &e
, 10);
2731 if (buf
== e
|| (*e
&& *e
!= '\n') || max
<= 0)
2733 mddev
->sync_speed_max
= max
;
2737 static struct md_sysfs_entry md_sync_max
=
2738 __ATTR(sync_speed_max
, S_IRUGO
|S_IWUSR
, sync_max_show
, sync_max_store
);
2742 sync_speed_show(mddev_t
*mddev
, char *page
)
2744 unsigned long resync
, dt
, db
;
2745 resync
= (mddev
->curr_resync
- atomic_read(&mddev
->recovery_active
));
2746 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
2748 db
= resync
- (mddev
->resync_mark_cnt
);
2749 return sprintf(page
, "%ld\n", db
/dt
/2); /* K/sec */
2752 static struct md_sysfs_entry md_sync_speed
= __ATTR_RO(sync_speed
);
2755 sync_completed_show(mddev_t
*mddev
, char *page
)
2757 unsigned long max_blocks
, resync
;
2759 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
2760 max_blocks
= mddev
->resync_max_sectors
;
2762 max_blocks
= mddev
->size
<< 1;
2764 resync
= (mddev
->curr_resync
- atomic_read(&mddev
->recovery_active
));
2765 return sprintf(page
, "%lu / %lu\n", resync
, max_blocks
);
2768 static struct md_sysfs_entry md_sync_completed
= __ATTR_RO(sync_completed
);
2771 max_sync_show(mddev_t
*mddev
, char *page
)
2773 if (mddev
->resync_max
== MaxSector
)
2774 return sprintf(page
, "max\n");
2776 return sprintf(page
, "%llu\n",
2777 (unsigned long long)mddev
->resync_max
);
2780 max_sync_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2782 if (strncmp(buf
, "max", 3)==0)
2783 mddev
->resync_max
= MaxSector
;
2786 unsigned long long max
= simple_strtoull(buf
, &ep
, 10);
2787 if (ep
== buf
|| (*ep
!= 0 && *ep
!= '\n'))
2789 if (max
< mddev
->resync_max
&&
2790 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
2793 /* Must be a multiple of chunk_size */
2794 if (mddev
->chunk_size
) {
2795 if (max
& (sector_t
)((mddev
->chunk_size
>>9)-1))
2798 mddev
->resync_max
= max
;
2800 wake_up(&mddev
->recovery_wait
);
2804 static struct md_sysfs_entry md_max_sync
=
2805 __ATTR(sync_max
, S_IRUGO
|S_IWUSR
, max_sync_show
, max_sync_store
);
2808 suspend_lo_show(mddev_t
*mddev
, char *page
)
2810 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_lo
);
2814 suspend_lo_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2817 unsigned long long new = simple_strtoull(buf
, &e
, 10);
2819 if (mddev
->pers
->quiesce
== NULL
)
2821 if (buf
== e
|| (*e
&& *e
!= '\n'))
2823 if (new >= mddev
->suspend_hi
||
2824 (new > mddev
->suspend_lo
&& new < mddev
->suspend_hi
)) {
2825 mddev
->suspend_lo
= new;
2826 mddev
->pers
->quiesce(mddev
, 2);
2831 static struct md_sysfs_entry md_suspend_lo
=
2832 __ATTR(suspend_lo
, S_IRUGO
|S_IWUSR
, suspend_lo_show
, suspend_lo_store
);
2836 suspend_hi_show(mddev_t
*mddev
, char *page
)
2838 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_hi
);
2842 suspend_hi_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2845 unsigned long long new = simple_strtoull(buf
, &e
, 10);
2847 if (mddev
->pers
->quiesce
== NULL
)
2849 if (buf
== e
|| (*e
&& *e
!= '\n'))
2851 if ((new <= mddev
->suspend_lo
&& mddev
->suspend_lo
>= mddev
->suspend_hi
) ||
2852 (new > mddev
->suspend_lo
&& new > mddev
->suspend_hi
)) {
2853 mddev
->suspend_hi
= new;
2854 mddev
->pers
->quiesce(mddev
, 1);
2855 mddev
->pers
->quiesce(mddev
, 0);
2860 static struct md_sysfs_entry md_suspend_hi
=
2861 __ATTR(suspend_hi
, S_IRUGO
|S_IWUSR
, suspend_hi_show
, suspend_hi_store
);
2864 static struct attribute
*md_default_attrs
[] = {
2867 &md_raid_disks
.attr
,
2868 &md_chunk_size
.attr
,
2870 &md_resync_start
.attr
,
2872 &md_new_device
.attr
,
2873 &md_safe_delay
.attr
,
2874 &md_array_state
.attr
,
2878 static struct attribute
*md_redundancy_attrs
[] = {
2880 &md_mismatches
.attr
,
2883 &md_sync_speed
.attr
,
2884 &md_sync_completed
.attr
,
2886 &md_suspend_lo
.attr
,
2887 &md_suspend_hi
.attr
,
2890 static struct attribute_group md_redundancy_group
= {
2892 .attrs
= md_redundancy_attrs
,
2897 md_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
2899 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
2900 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
2905 rv
= mddev_lock(mddev
);
2907 rv
= entry
->show(mddev
, page
);
2908 mddev_unlock(mddev
);
2914 md_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
2915 const char *page
, size_t length
)
2917 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
2918 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
2923 if (!capable(CAP_SYS_ADMIN
))
2925 rv
= mddev_lock(mddev
);
2927 rv
= entry
->store(mddev
, page
, length
);
2928 mddev_unlock(mddev
);
2933 static void md_free(struct kobject
*ko
)
2935 mddev_t
*mddev
= container_of(ko
, mddev_t
, kobj
);
2939 static struct sysfs_ops md_sysfs_ops
= {
2940 .show
= md_attr_show
,
2941 .store
= md_attr_store
,
2943 static struct kobj_type md_ktype
= {
2945 .sysfs_ops
= &md_sysfs_ops
,
2946 .default_attrs
= md_default_attrs
,
2951 static struct kobject
*md_probe(dev_t dev
, int *part
, void *data
)
2953 static DEFINE_MUTEX(disks_mutex
);
2954 mddev_t
*mddev
= mddev_find(dev
);
2955 struct gendisk
*disk
;
2956 int partitioned
= (MAJOR(dev
) != MD_MAJOR
);
2957 int shift
= partitioned
? MdpMinorShift
: 0;
2958 int unit
= MINOR(dev
) >> shift
;
2963 mutex_lock(&disks_mutex
);
2964 if (mddev
->gendisk
) {
2965 mutex_unlock(&disks_mutex
);
2969 disk
= alloc_disk(1 << shift
);
2971 mutex_unlock(&disks_mutex
);
2975 disk
->major
= MAJOR(dev
);
2976 disk
->first_minor
= unit
<< shift
;
2978 sprintf(disk
->disk_name
, "md_d%d", unit
);
2979 sprintf(disk
->devfs_name
, "md/d%d", unit
);
2981 sprintf(disk
->disk_name
, "md%d", unit
);
2982 sprintf(disk
->devfs_name
, "md/%d", unit
);
2984 disk
->fops
= &md_fops
;
2985 disk
->private_data
= mddev
;
2986 disk
->queue
= mddev
->queue
;
2988 mddev
->gendisk
= disk
;
2989 mutex_unlock(&disks_mutex
);
2990 mddev
->kobj
.parent
= &disk
->kobj
;
2991 mddev
->kobj
.k_name
= NULL
;
2992 snprintf(mddev
->kobj
.name
, KOBJ_NAME_LEN
, "%s", "md");
2993 mddev
->kobj
.ktype
= &md_ktype
;
2994 kobject_register(&mddev
->kobj
);
2998 static void md_safemode_timeout(unsigned long data
)
3000 mddev_t
*mddev
= (mddev_t
*) data
;
3002 mddev
->safemode
= 1;
3003 md_wakeup_thread(mddev
->thread
);
3006 static int start_dirty_degraded
;
3008 static int do_md_run(mddev_t
* mddev
)
3012 struct list_head
*tmp
;
3014 struct gendisk
*disk
;
3015 struct mdk_personality
*pers
;
3016 char b
[BDEVNAME_SIZE
];
3018 if (list_empty(&mddev
->disks
))
3019 /* cannot run an array with no devices.. */
3026 * Analyze all RAID superblock(s)
3028 if (!mddev
->raid_disks
)
3031 chunk_size
= mddev
->chunk_size
;
3034 if (chunk_size
> MAX_CHUNK_SIZE
) {
3035 printk(KERN_ERR
"too big chunk_size: %d > %d\n",
3036 chunk_size
, MAX_CHUNK_SIZE
);
3040 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
3042 if ( (1 << ffz(~chunk_size
)) != chunk_size
) {
3043 printk(KERN_ERR
"chunk_size of %d not valid\n", chunk_size
);
3046 if (chunk_size
< PAGE_SIZE
) {
3047 printk(KERN_ERR
"too small chunk_size: %d < %ld\n",
3048 chunk_size
, PAGE_SIZE
);
3052 /* devices must have minimum size of one chunk */
3053 ITERATE_RDEV(mddev
,rdev
,tmp
) {
3054 if (test_bit(Faulty
, &rdev
->flags
))
3056 if (rdev
->size
< chunk_size
/ 1024) {
3058 "md: Dev %s smaller than chunk_size:"
3060 bdevname(rdev
->bdev
,b
),
3061 (unsigned long long)rdev
->size
,
3069 if (mddev
->level
!= LEVEL_NONE
)
3070 request_module("md-level-%d", mddev
->level
);
3071 else if (mddev
->clevel
[0])
3072 request_module("md-%s", mddev
->clevel
);
3076 * Drop all container device buffers, from now on
3077 * the only valid external interface is through the md
3079 * Also find largest hardsector size
3081 ITERATE_RDEV(mddev
,rdev
,tmp
) {
3082 if (test_bit(Faulty
, &rdev
->flags
))
3084 sync_blockdev(rdev
->bdev
);
3085 invalidate_bdev(rdev
->bdev
, 0);
3088 md_probe(mddev
->unit
, NULL
, NULL
);
3089 disk
= mddev
->gendisk
;
3093 spin_lock(&pers_lock
);
3094 pers
= find_pers(mddev
->level
, mddev
->clevel
);
3095 if (!pers
|| !try_module_get(pers
->owner
)) {
3096 spin_unlock(&pers_lock
);
3097 if (mddev
->level
!= LEVEL_NONE
)
3098 printk(KERN_WARNING
"md: personality for level %d is not loaded!\n",
3101 printk(KERN_WARNING
"md: personality for level %s is not loaded!\n",
3106 spin_unlock(&pers_lock
);
3107 mddev
->level
= pers
->level
;
3108 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
3110 if (mddev
->reshape_position
!= MaxSector
&&
3111 pers
->start_reshape
== NULL
) {
3112 /* This personality cannot handle reshaping... */
3114 module_put(pers
->owner
);
3118 mddev
->recovery
= 0;
3119 mddev
->resync_max_sectors
= mddev
->size
<< 1; /* may be over-ridden by personality */
3120 mddev
->barriers_work
= 1;
3121 mddev
->ok_start_degraded
= start_dirty_degraded
;
3124 mddev
->ro
= 2; /* read-only, but switch on first write */
3126 err
= mddev
->pers
->run(mddev
);
3127 if (!err
&& mddev
->pers
->sync_request
) {
3128 err
= bitmap_create(mddev
);
3130 printk(KERN_ERR
"%s: failed to create bitmap (%d)\n",
3131 mdname(mddev
), err
);
3132 mddev
->pers
->stop(mddev
);
3136 printk(KERN_ERR
"md: pers->run() failed ...\n");
3137 module_put(mddev
->pers
->owner
);
3139 bitmap_destroy(mddev
);
3142 if (mddev
->pers
->sync_request
)
3143 sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
);
3144 else if (mddev
->ro
== 2) /* auto-readonly not meaningful */
3147 atomic_set(&mddev
->writes_pending
,0);
3148 mddev
->safemode
= 0;
3149 mddev
->safemode_timer
.function
= md_safemode_timeout
;
3150 mddev
->safemode_timer
.data
= (unsigned long) mddev
;
3151 mddev
->safemode_delay
= (200 * HZ
)/1000 +1; /* 200 msec delay */
3154 ITERATE_RDEV(mddev
,rdev
,tmp
)
3155 if (rdev
->raid_disk
>= 0) {
3157 sprintf(nm
, "rd%d", rdev
->raid_disk
);
3158 sysfs_create_link(&mddev
->kobj
, &rdev
->kobj
, nm
);
3161 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3162 md_wakeup_thread(mddev
->thread
);
3164 if (mddev
->sb_dirty
)
3165 md_update_sb(mddev
);
3167 set_capacity(disk
, mddev
->array_size
<<1);
3169 /* If we call blk_queue_make_request here, it will
3170 * re-initialise max_sectors etc which may have been
3171 * refined inside -> run. So just set the bits we need to set.
3172 * Most initialisation happended when we called
3173 * blk_queue_make_request(..., md_fail_request)
3176 mddev
->queue
->queuedata
= mddev
;
3177 mddev
->queue
->make_request_fn
= mddev
->pers
->make_request
;
3179 /* If there is a partially-recovered drive we need to
3180 * start recovery here. If we leave it to md_check_recovery,
3181 * it will remove the drives and not do the right thing
3183 if (mddev
->degraded
) {
3184 struct list_head
*rtmp
;
3186 ITERATE_RDEV(mddev
,rdev
,rtmp
)
3187 if (rdev
->raid_disk
>= 0 &&
3188 !test_bit(In_sync
, &rdev
->flags
) &&
3189 !test_bit(Faulty
, &rdev
->flags
))
3190 /* complete an interrupted recovery */
3192 if (spares
&& mddev
->pers
->sync_request
) {
3193 mddev
->recovery
= 0;
3194 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
3195 mddev
->sync_thread
= md_register_thread(md_do_sync
,
3198 if (!mddev
->sync_thread
) {
3199 printk(KERN_ERR
"%s: could not start resync"
3202 /* leave the spares where they are, it shouldn't hurt */
3203 mddev
->recovery
= 0;
3205 md_wakeup_thread(mddev
->sync_thread
);
3210 md_new_event(mddev
);
3214 static int restart_array(mddev_t
*mddev
)
3216 struct gendisk
*disk
= mddev
->gendisk
;
3220 * Complain if it has no devices
3223 if (list_empty(&mddev
->disks
))
3231 mddev
->safemode
= 0;
3233 set_disk_ro(disk
, 0);
3235 printk(KERN_INFO
"md: %s switched to read-write mode.\n",
3238 * Kick recovery or resync if necessary
3240 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3241 md_wakeup_thread(mddev
->thread
);
3242 md_wakeup_thread(mddev
->sync_thread
);
3251 /* similar to deny_write_access, but accounts for our holding a reference
3252 * to the file ourselves */
3253 static int deny_bitmap_write_access(struct file
* file
)
3255 struct inode
*inode
= file
->f_mapping
->host
;
3257 spin_lock(&inode
->i_lock
);
3258 if (atomic_read(&inode
->i_writecount
) > 1) {
3259 spin_unlock(&inode
->i_lock
);
3262 atomic_set(&inode
->i_writecount
, -1);
3263 spin_unlock(&inode
->i_lock
);
3268 static void restore_bitmap_write_access(struct file
*file
)
3270 struct inode
*inode
= file
->f_mapping
->host
;
3272 spin_lock(&inode
->i_lock
);
3273 atomic_set(&inode
->i_writecount
, 1);
3274 spin_unlock(&inode
->i_lock
);
3278 * 0 - completely stop and dis-assemble array
3279 * 1 - switch to readonly
3280 * 2 - stop but do not disassemble array
3282 static int do_md_stop(mddev_t
* mddev
, int mode
)
3285 struct gendisk
*disk
= mddev
->gendisk
;
3288 if (atomic_read(&mddev
->active
)>2) {
3289 printk("md: %s still in use.\n",mdname(mddev
));
3293 if (mddev
->sync_thread
) {
3294 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
3295 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
3296 md_unregister_thread(mddev
->sync_thread
);
3297 mddev
->sync_thread
= NULL
;
3300 del_timer_sync(&mddev
->safemode_timer
);
3302 invalidate_partition(disk
, 0);
3305 case 1: /* readonly */
3311 case 0: /* disassemble */
3313 bitmap_flush(mddev
);
3314 md_super_wait(mddev
);
3316 set_disk_ro(disk
, 0);
3317 blk_queue_make_request(mddev
->queue
, md_fail_request
);
3318 mddev
->pers
->stop(mddev
);
3319 if (mddev
->pers
->sync_request
)
3320 sysfs_remove_group(&mddev
->kobj
, &md_redundancy_group
);
3322 module_put(mddev
->pers
->owner
);
3327 if (!mddev
->in_sync
|| mddev
->sb_dirty
) {
3328 /* mark array as shutdown cleanly */
3330 md_update_sb(mddev
);
3333 set_disk_ro(disk
, 1);
3334 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
3338 * Free resources if final stop
3342 struct list_head
*tmp
;
3343 struct gendisk
*disk
;
3344 printk(KERN_INFO
"md: %s stopped.\n", mdname(mddev
));
3346 bitmap_destroy(mddev
);
3347 if (mddev
->bitmap_file
) {
3348 restore_bitmap_write_access(mddev
->bitmap_file
);
3349 fput(mddev
->bitmap_file
);
3350 mddev
->bitmap_file
= NULL
;
3352 mddev
->bitmap_offset
= 0;
3354 ITERATE_RDEV(mddev
,rdev
,tmp
)
3355 if (rdev
->raid_disk
>= 0) {
3357 sprintf(nm
, "rd%d", rdev
->raid_disk
);
3358 sysfs_remove_link(&mddev
->kobj
, nm
);
3361 export_array(mddev
);
3363 mddev
->array_size
= 0;
3365 mddev
->raid_disks
= 0;
3366 mddev
->recovery_cp
= 0;
3367 mddev
->resync_max
= MaxSector
;
3369 disk
= mddev
->gendisk
;
3371 set_capacity(disk
, 0);
3373 } else if (mddev
->pers
)
3374 printk(KERN_INFO
"md: %s switched to read-only mode.\n",
3377 md_new_event(mddev
);
3382 static void autorun_array(mddev_t
*mddev
)
3385 struct list_head
*tmp
;
3388 if (list_empty(&mddev
->disks
))
3391 printk(KERN_INFO
"md: running: ");
3393 ITERATE_RDEV(mddev
,rdev
,tmp
) {
3394 char b
[BDEVNAME_SIZE
];
3395 printk("<%s>", bdevname(rdev
->bdev
,b
));
3399 err
= do_md_run (mddev
);
3401 printk(KERN_WARNING
"md: do_md_run() returned %d\n", err
);
3402 do_md_stop (mddev
, 0);
3407 * lets try to run arrays based on all disks that have arrived
3408 * until now. (those are in pending_raid_disks)
3410 * the method: pick the first pending disk, collect all disks with
3411 * the same UUID, remove all from the pending list and put them into
3412 * the 'same_array' list. Then order this list based on superblock
3413 * update time (freshest comes first), kick out 'old' disks and
3414 * compare superblocks. If everything's fine then run it.
3416 * If "unit" is allocated, then bump its reference count
3418 static void autorun_devices(int part
)
3420 struct list_head
*tmp
;
3421 mdk_rdev_t
*rdev0
, *rdev
;
3423 char b
[BDEVNAME_SIZE
];
3425 printk(KERN_INFO
"md: autorun ...\n");
3426 while (!list_empty(&pending_raid_disks
)) {
3428 LIST_HEAD(candidates
);
3429 rdev0
= list_entry(pending_raid_disks
.next
,
3430 mdk_rdev_t
, same_set
);
3432 printk(KERN_INFO
"md: considering %s ...\n",
3433 bdevname(rdev0
->bdev
,b
));
3434 INIT_LIST_HEAD(&candidates
);
3435 ITERATE_RDEV_PENDING(rdev
,tmp
)
3436 if (super_90_load(rdev
, rdev0
, 0) >= 0) {
3437 printk(KERN_INFO
"md: adding %s ...\n",
3438 bdevname(rdev
->bdev
,b
));
3439 list_move(&rdev
->same_set
, &candidates
);
3442 * now we have a set of devices, with all of them having
3443 * mostly sane superblocks. It's time to allocate the
3446 if (rdev0
->preferred_minor
< 0 || rdev0
->preferred_minor
>= MAX_MD_DEVS
) {
3447 printk(KERN_INFO
"md: unit number in %s is bad: %d\n",
3448 bdevname(rdev0
->bdev
, b
), rdev0
->preferred_minor
);
3452 dev
= MKDEV(mdp_major
,
3453 rdev0
->preferred_minor
<< MdpMinorShift
);
3455 dev
= MKDEV(MD_MAJOR
, rdev0
->preferred_minor
);
3457 md_probe(dev
, NULL
, NULL
);
3458 mddev
= mddev_find(dev
);
3461 "md: cannot allocate memory for md drive.\n");
3464 if (mddev_lock(mddev
))
3465 printk(KERN_WARNING
"md: %s locked, cannot run\n",
3467 else if (mddev
->raid_disks
|| mddev
->major_version
3468 || !list_empty(&mddev
->disks
)) {
3470 "md: %s already running, cannot run %s\n",
3471 mdname(mddev
), bdevname(rdev0
->bdev
,b
));
3472 mddev_unlock(mddev
);
3474 printk(KERN_INFO
"md: created %s\n", mdname(mddev
));
3475 ITERATE_RDEV_GENERIC(candidates
,rdev
,tmp
) {
3476 list_del_init(&rdev
->same_set
);
3477 if (bind_rdev_to_array(rdev
, mddev
))
3480 autorun_array(mddev
);
3481 mddev_unlock(mddev
);
3483 /* on success, candidates will be empty, on error
3486 ITERATE_RDEV_GENERIC(candidates
,rdev
,tmp
)
3490 printk(KERN_INFO
"md: ... autorun DONE.\n");
3494 * import RAID devices based on one partition
3495 * if possible, the array gets run as well.
3498 static int autostart_array(dev_t startdev
)
3500 char b
[BDEVNAME_SIZE
];
3501 int err
= -EINVAL
, i
;
3502 mdp_super_t
*sb
= NULL
;
3503 mdk_rdev_t
*start_rdev
= NULL
, *rdev
;
3505 start_rdev
= md_import_device(startdev
, 0, 0);
3506 if (IS_ERR(start_rdev
))
3510 /* NOTE: this can only work for 0.90.0 superblocks */
3511 sb
= (mdp_super_t
*)page_address(start_rdev
->sb_page
);
3512 if (sb
->major_version
!= 0 ||
3513 sb
->minor_version
!= 90 ) {
3514 printk(KERN_WARNING
"md: can only autostart 0.90.0 arrays\n");
3515 export_rdev(start_rdev
);
3519 if (test_bit(Faulty
, &start_rdev
->flags
)) {
3521 "md: can not autostart based on faulty %s!\n",
3522 bdevname(start_rdev
->bdev
,b
));
3523 export_rdev(start_rdev
);
3526 list_add(&start_rdev
->same_set
, &pending_raid_disks
);
3528 for (i
= 0; i
< MD_SB_DISKS
; i
++) {
3529 mdp_disk_t
*desc
= sb
->disks
+ i
;
3530 dev_t dev
= MKDEV(desc
->major
, desc
->minor
);
3534 if (dev
== startdev
)
3536 if (MAJOR(dev
) != desc
->major
|| MINOR(dev
) != desc
->minor
)
3538 rdev
= md_import_device(dev
, 0, 0);
3542 list_add(&rdev
->same_set
, &pending_raid_disks
);
3546 * possibly return codes
3554 static int get_version(void __user
* arg
)
3558 ver
.major
= MD_MAJOR_VERSION
;
3559 ver
.minor
= MD_MINOR_VERSION
;
3560 ver
.patchlevel
= MD_PATCHLEVEL_VERSION
;
3562 if (copy_to_user(arg
, &ver
, sizeof(ver
)))
3568 static int get_array_info(mddev_t
* mddev
, void __user
* arg
)
3570 mdu_array_info_t info
;
3571 int nr
,working
,active
,failed
,spare
;
3573 struct list_head
*tmp
;
3575 nr
=working
=active
=failed
=spare
=0;
3576 ITERATE_RDEV(mddev
,rdev
,tmp
) {
3578 if (test_bit(Faulty
, &rdev
->flags
))
3582 if (test_bit(In_sync
, &rdev
->flags
))
3589 info
.major_version
= mddev
->major_version
;
3590 info
.minor_version
= mddev
->minor_version
;
3591 info
.patch_version
= MD_PATCHLEVEL_VERSION
;
3592 info
.ctime
= mddev
->ctime
;
3593 info
.level
= mddev
->level
;
3594 info
.size
= mddev
->size
;
3595 if (info
.size
!= mddev
->size
) /* overflow */
3598 info
.raid_disks
= mddev
->raid_disks
;
3599 info
.md_minor
= mddev
->md_minor
;
3600 info
.not_persistent
= !mddev
->persistent
;
3602 info
.utime
= mddev
->utime
;
3605 info
.state
= (1<<MD_SB_CLEAN
);
3606 if (mddev
->bitmap
&& mddev
->bitmap_offset
)
3607 info
.state
= (1<<MD_SB_BITMAP_PRESENT
);
3608 info
.active_disks
= active
;
3609 info
.working_disks
= working
;
3610 info
.failed_disks
= failed
;
3611 info
.spare_disks
= spare
;
3613 info
.layout
= mddev
->layout
;
3614 info
.chunk_size
= mddev
->chunk_size
;
3616 if (copy_to_user(arg
, &info
, sizeof(info
)))
3622 static int get_bitmap_file(mddev_t
* mddev
, void __user
* arg
)
3624 mdu_bitmap_file_t
*file
= NULL
; /* too big for stack allocation */
3625 char *ptr
, *buf
= NULL
;
3628 file
= kmalloc(sizeof(*file
), GFP_KERNEL
);
3632 /* bitmap disabled, zero the first byte and copy out */
3633 if (!mddev
->bitmap
|| !mddev
->bitmap
->file
) {
3634 file
->pathname
[0] = '\0';
3638 buf
= kmalloc(sizeof(file
->pathname
), GFP_KERNEL
);
3642 ptr
= file_path(mddev
->bitmap
->file
, buf
, sizeof(file
->pathname
));
3646 strcpy(file
->pathname
, ptr
);
3650 if (copy_to_user(arg
, file
, sizeof(*file
)))
3658 static int get_disk_info(mddev_t
* mddev
, void __user
* arg
)
3660 mdu_disk_info_t info
;
3664 if (copy_from_user(&info
, arg
, sizeof(info
)))
3669 rdev
= find_rdev_nr(mddev
, nr
);
3671 info
.major
= MAJOR(rdev
->bdev
->bd_dev
);
3672 info
.minor
= MINOR(rdev
->bdev
->bd_dev
);
3673 info
.raid_disk
= rdev
->raid_disk
;
3675 if (test_bit(Faulty
, &rdev
->flags
))
3676 info
.state
|= (1<<MD_DISK_FAULTY
);
3677 else if (test_bit(In_sync
, &rdev
->flags
)) {
3678 info
.state
|= (1<<MD_DISK_ACTIVE
);
3679 info
.state
|= (1<<MD_DISK_SYNC
);
3681 if (test_bit(WriteMostly
, &rdev
->flags
))
3682 info
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
3684 info
.major
= info
.minor
= 0;
3685 info
.raid_disk
= -1;
3686 info
.state
= (1<<MD_DISK_REMOVED
);
3689 if (copy_to_user(arg
, &info
, sizeof(info
)))
3695 static int add_new_disk(mddev_t
* mddev
, mdu_disk_info_t
*info
)
3697 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
3699 dev_t dev
= MKDEV(info
->major
,info
->minor
);
3701 if (info
->major
!= MAJOR(dev
) || info
->minor
!= MINOR(dev
))
3704 if (!mddev
->raid_disks
) {
3706 /* expecting a device which has a superblock */
3707 rdev
= md_import_device(dev
, mddev
->major_version
, mddev
->minor_version
);
3710 "md: md_import_device returned %ld\n",
3712 return PTR_ERR(rdev
);
3714 if (!list_empty(&mddev
->disks
)) {
3715 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
3716 mdk_rdev_t
, same_set
);
3717 int err
= super_types
[mddev
->major_version
]
3718 .load_super(rdev
, rdev0
, mddev
->minor_version
);
3721 "md: %s has different UUID to %s\n",
3722 bdevname(rdev
->bdev
,b
),
3723 bdevname(rdev0
->bdev
,b2
));
3728 err
= bind_rdev_to_array(rdev
, mddev
);
3735 * add_new_disk can be used once the array is assembled
3736 * to add "hot spares". They must already have a superblock
3741 if (!mddev
->pers
->hot_add_disk
) {
3743 "%s: personality does not support diskops!\n",
3747 if (mddev
->persistent
)
3748 rdev
= md_import_device(dev
, mddev
->major_version
,
3749 mddev
->minor_version
);
3751 rdev
= md_import_device(dev
, -1, -1);
3754 "md: md_import_device returned %ld\n",
3756 return PTR_ERR(rdev
);
3758 /* set save_raid_disk if appropriate */
3759 if (!mddev
->persistent
) {
3760 if (info
->state
& (1<<MD_DISK_SYNC
) &&
3761 info
->raid_disk
< mddev
->raid_disks
)
3762 rdev
->raid_disk
= info
->raid_disk
;
3764 rdev
->raid_disk
= -1;
3766 super_types
[mddev
->major_version
].
3767 validate_super(mddev
, rdev
);
3768 rdev
->saved_raid_disk
= rdev
->raid_disk
;
3770 clear_bit(In_sync
, &rdev
->flags
); /* just to be sure */
3771 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
3772 set_bit(WriteMostly
, &rdev
->flags
);
3774 rdev
->raid_disk
= -1;
3775 err
= bind_rdev_to_array(rdev
, mddev
);
3776 if (!err
&& !mddev
->pers
->hot_remove_disk
) {
3777 /* If there is hot_add_disk but no hot_remove_disk
3778 * then added disks for geometry changes,
3779 * and should be added immediately.
3781 super_types
[mddev
->major_version
].
3782 validate_super(mddev
, rdev
);
3783 err
= mddev
->pers
->hot_add_disk(mddev
, rdev
);
3785 unbind_rdev_from_array(rdev
);
3790 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3791 md_wakeup_thread(mddev
->thread
);
3795 /* otherwise, add_new_disk is only allowed
3796 * for major_version==0 superblocks
3798 if (mddev
->major_version
!= 0) {
3799 printk(KERN_WARNING
"%s: ADD_NEW_DISK not supported\n",
3804 if (!(info
->state
& (1<<MD_DISK_FAULTY
))) {
3806 rdev
= md_import_device (dev
, -1, 0);
3809 "md: error, md_import_device() returned %ld\n",
3811 return PTR_ERR(rdev
);
3813 rdev
->desc_nr
= info
->number
;
3814 if (info
->raid_disk
< mddev
->raid_disks
)
3815 rdev
->raid_disk
= info
->raid_disk
;
3817 rdev
->raid_disk
= -1;
3821 if (rdev
->raid_disk
< mddev
->raid_disks
)
3822 if (info
->state
& (1<<MD_DISK_SYNC
))
3823 set_bit(In_sync
, &rdev
->flags
);
3825 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
3826 set_bit(WriteMostly
, &rdev
->flags
);
3828 if (!mddev
->persistent
) {
3829 printk(KERN_INFO
"md: nonpersistent superblock ...\n");
3830 rdev
->sb_offset
= rdev
->bdev
->bd_inode
->i_size
>> BLOCK_SIZE_BITS
;
3832 rdev
->sb_offset
= calc_dev_sboffset(rdev
->bdev
);
3833 rdev
->size
= calc_dev_size(rdev
, mddev
->chunk_size
);
3835 err
= bind_rdev_to_array(rdev
, mddev
);
3845 static int hot_remove_disk(mddev_t
* mddev
, dev_t dev
)
3847 char b
[BDEVNAME_SIZE
];
3853 rdev
= find_rdev(mddev
, dev
);
3857 if (rdev
->raid_disk
>= 0)
3860 kick_rdev_from_array(rdev
);
3861 md_update_sb(mddev
);
3862 md_new_event(mddev
);
3866 printk(KERN_WARNING
"md: cannot remove active disk %s from %s ... \n",
3867 bdevname(rdev
->bdev
,b
), mdname(mddev
));
3871 static int hot_add_disk(mddev_t
* mddev
, dev_t dev
)
3873 char b
[BDEVNAME_SIZE
];
3881 if (mddev
->major_version
!= 0) {
3882 printk(KERN_WARNING
"%s: HOT_ADD may only be used with"
3883 " version-0 superblocks.\n",
3887 if (!mddev
->pers
->hot_add_disk
) {
3889 "%s: personality does not support diskops!\n",
3894 rdev
= md_import_device (dev
, -1, 0);
3897 "md: error, md_import_device() returned %ld\n",
3902 if (mddev
->persistent
)
3903 rdev
->sb_offset
= calc_dev_sboffset(rdev
->bdev
);
3906 rdev
->bdev
->bd_inode
->i_size
>> BLOCK_SIZE_BITS
;
3908 size
= calc_dev_size(rdev
, mddev
->chunk_size
);
3911 if (test_bit(Faulty
, &rdev
->flags
)) {
3913 "md: can not hot-add faulty %s disk to %s!\n",
3914 bdevname(rdev
->bdev
,b
), mdname(mddev
));
3918 clear_bit(In_sync
, &rdev
->flags
);
3920 err
= bind_rdev_to_array(rdev
, mddev
);
3925 * The rest should better be atomic, we can have disk failures
3926 * noticed in interrupt contexts ...
3929 if (rdev
->desc_nr
== mddev
->max_disks
) {
3930 printk(KERN_WARNING
"%s: can not hot-add to full array!\n",
3933 goto abort_unbind_export
;
3936 rdev
->raid_disk
= -1;
3938 md_update_sb(mddev
);
3941 * Kick recovery, maybe this spare has to be added to the
3942 * array immediately.
3944 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3945 md_wakeup_thread(mddev
->thread
);
3946 md_new_event(mddev
);
3949 abort_unbind_export
:
3950 unbind_rdev_from_array(rdev
);
3957 static int set_bitmap_file(mddev_t
*mddev
, int fd
)
3962 if (!mddev
->pers
->quiesce
)
3964 if (mddev
->recovery
|| mddev
->sync_thread
)
3966 /* we should be able to change the bitmap.. */
3972 return -EEXIST
; /* cannot add when bitmap is present */
3973 mddev
->bitmap_file
= fget(fd
);
3975 if (mddev
->bitmap_file
== NULL
) {
3976 printk(KERN_ERR
"%s: error: failed to get bitmap file\n",
3981 err
= deny_bitmap_write_access(mddev
->bitmap_file
);
3983 printk(KERN_ERR
"%s: error: bitmap file is already in use\n",
3985 fput(mddev
->bitmap_file
);
3986 mddev
->bitmap_file
= NULL
;
3989 mddev
->bitmap_offset
= 0; /* file overrides offset */
3990 } else if (mddev
->bitmap
== NULL
)
3991 return -ENOENT
; /* cannot remove what isn't there */
3994 mddev
->pers
->quiesce(mddev
, 1);
3996 err
= bitmap_create(mddev
);
3997 if (fd
< 0 || err
) {
3998 bitmap_destroy(mddev
);
3999 fd
= -1; /* make sure to put the file */
4001 mddev
->pers
->quiesce(mddev
, 0);
4004 if (mddev
->bitmap_file
) {
4005 restore_bitmap_write_access(mddev
->bitmap_file
);
4006 fput(mddev
->bitmap_file
);
4008 mddev
->bitmap_file
= NULL
;
4015 * set_array_info is used two different ways
4016 * The original usage is when creating a new array.
4017 * In this usage, raid_disks is > 0 and it together with
4018 * level, size, not_persistent,layout,chunksize determine the
4019 * shape of the array.
4020 * This will always create an array with a type-0.90.0 superblock.
4021 * The newer usage is when assembling an array.
4022 * In this case raid_disks will be 0, and the major_version field is
4023 * use to determine which style super-blocks are to be found on the devices.
4024 * The minor and patch _version numbers are also kept incase the
4025 * super_block handler wishes to interpret them.
4027 static int set_array_info(mddev_t
* mddev
, mdu_array_info_t
*info
)
4030 if (info
->raid_disks
== 0) {
4031 /* just setting version number for superblock loading */
4032 if (info
->major_version
< 0 ||
4033 info
->major_version
>= sizeof(super_types
)/sizeof(super_types
[0]) ||
4034 super_types
[info
->major_version
].name
== NULL
) {
4035 /* maybe try to auto-load a module? */
4037 "md: superblock version %d not known\n",
4038 info
->major_version
);
4041 mddev
->major_version
= info
->major_version
;
4042 mddev
->minor_version
= info
->minor_version
;
4043 mddev
->patch_version
= info
->patch_version
;
4046 mddev
->major_version
= MD_MAJOR_VERSION
;
4047 mddev
->minor_version
= MD_MINOR_VERSION
;
4048 mddev
->patch_version
= MD_PATCHLEVEL_VERSION
;
4049 mddev
->ctime
= get_seconds();
4051 mddev
->level
= info
->level
;
4052 mddev
->clevel
[0] = 0;
4053 mddev
->size
= info
->size
;
4054 mddev
->raid_disks
= info
->raid_disks
;
4055 /* don't set md_minor, it is determined by which /dev/md* was
4058 if (info
->state
& (1<<MD_SB_CLEAN
))
4059 mddev
->recovery_cp
= MaxSector
;
4061 mddev
->recovery_cp
= 0;
4062 mddev
->persistent
= ! info
->not_persistent
;
4063 mddev
->external
= 0;
4065 mddev
->layout
= info
->layout
;
4066 mddev
->chunk_size
= info
->chunk_size
;
4068 mddev
->max_disks
= MD_SB_DISKS
;
4070 if (mddev
->persistent
)
4071 mddev
->sb_dirty
= 1;
4073 mddev
->default_bitmap_offset
= MD_SB_BYTES
>> 9;
4074 mddev
->bitmap_offset
= 0;
4076 mddev
->reshape_position
= MaxSector
;
4079 * Generate a 128 bit UUID
4081 get_random_bytes(mddev
->uuid
, 16);
4083 mddev
->new_level
= mddev
->level
;
4084 mddev
->new_chunk
= mddev
->chunk_size
;
4085 mddev
->new_layout
= mddev
->layout
;
4086 mddev
->delta_disks
= 0;
4091 static int update_size(mddev_t
*mddev
, unsigned long size
)
4095 struct list_head
*tmp
;
4096 int fit
= (size
== 0);
4098 if (mddev
->pers
->resize
== NULL
)
4100 /* The "size" is the amount of each device that is used.
4101 * This can only make sense for arrays with redundancy.
4102 * linear and raid0 always use whatever space is available
4103 * We can only consider changing the size if no resync
4104 * or reconstruction is happening, and if the new size
4105 * is acceptable. It must fit before the sb_offset or,
4106 * if that is <data_offset, it must fit before the
4107 * size of each device.
4108 * If size is zero, we find the largest size that fits.
4110 if (mddev
->sync_thread
)
4112 ITERATE_RDEV(mddev
,rdev
,tmp
) {
4114 if (rdev
->sb_offset
> rdev
->data_offset
)
4115 avail
= (rdev
->sb_offset
*2) - rdev
->data_offset
;
4117 avail
= get_capacity(rdev
->bdev
->bd_disk
)
4118 - rdev
->data_offset
;
4119 if (fit
&& (size
== 0 || size
> avail
/2))
4121 if (avail
< ((sector_t
)size
<< 1))
4124 rv
= mddev
->pers
->resize(mddev
, (sector_t
)size
*2);
4126 struct block_device
*bdev
;
4128 bdev
= bdget_disk(mddev
->gendisk
, 0);
4130 mutex_lock(&bdev
->bd_inode
->i_mutex
);
4131 i_size_write(bdev
->bd_inode
, (loff_t
)mddev
->array_size
<< 10);
4132 mutex_unlock(&bdev
->bd_inode
->i_mutex
);
4139 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
)
4142 /* change the number of raid disks */
4143 if (mddev
->pers
->check_reshape
== NULL
)
4145 if (raid_disks
<= 0 ||
4146 raid_disks
>= mddev
->max_disks
)
4148 if (mddev
->sync_thread
|| mddev
->reshape_position
!= MaxSector
)
4150 mddev
->delta_disks
= raid_disks
- mddev
->raid_disks
;
4152 rv
= mddev
->pers
->check_reshape(mddev
);
4158 * update_array_info is used to change the configuration of an
4160 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
4161 * fields in the info are checked against the array.
4162 * Any differences that cannot be handled will cause an error.
4163 * Normally, only one change can be managed at a time.
4165 static int update_array_info(mddev_t
*mddev
, mdu_array_info_t
*info
)
4171 /* calculate expected state,ignoring low bits */
4172 if (mddev
->bitmap
&& mddev
->bitmap_offset
)
4173 state
|= (1 << MD_SB_BITMAP_PRESENT
);
4175 if (mddev
->major_version
!= info
->major_version
||
4176 mddev
->minor_version
!= info
->minor_version
||
4177 /* mddev->patch_version != info->patch_version || */
4178 mddev
->ctime
!= info
->ctime
||
4179 mddev
->level
!= info
->level
||
4180 /* mddev->layout != info->layout || */
4181 !mddev
->persistent
!= info
->not_persistent
||
4182 mddev
->chunk_size
!= info
->chunk_size
||
4183 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
4184 ((state
^info
->state
) & 0xfffffe00)
4187 /* Check there is only one change */
4188 if (info
->size
>= 0 && mddev
->size
!= info
->size
) cnt
++;
4189 if (mddev
->raid_disks
!= info
->raid_disks
) cnt
++;
4190 if (mddev
->layout
!= info
->layout
) cnt
++;
4191 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) cnt
++;
4192 if (cnt
== 0) return 0;
4193 if (cnt
> 1) return -EINVAL
;
4195 if (mddev
->layout
!= info
->layout
) {
4197 * we don't need to do anything at the md level, the
4198 * personality will take care of it all.
4200 if (mddev
->pers
->reconfig
== NULL
)
4203 return mddev
->pers
->reconfig(mddev
, info
->layout
, -1);
4205 if (info
->size
>= 0 && mddev
->size
!= info
->size
)
4206 rv
= update_size(mddev
, info
->size
);
4208 if (mddev
->raid_disks
!= info
->raid_disks
)
4209 rv
= update_raid_disks(mddev
, info
->raid_disks
);
4211 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) {
4212 if (mddev
->pers
->quiesce
== NULL
)
4214 if (mddev
->recovery
|| mddev
->sync_thread
)
4216 if (info
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
4217 /* add the bitmap */
4220 if (mddev
->default_bitmap_offset
== 0)
4222 mddev
->bitmap_offset
= mddev
->default_bitmap_offset
;
4223 mddev
->pers
->quiesce(mddev
, 1);
4224 rv
= bitmap_create(mddev
);
4226 bitmap_destroy(mddev
);
4227 mddev
->pers
->quiesce(mddev
, 0);
4229 /* remove the bitmap */
4232 if (mddev
->bitmap
->file
)
4234 mddev
->pers
->quiesce(mddev
, 1);
4235 bitmap_destroy(mddev
);
4236 mddev
->pers
->quiesce(mddev
, 0);
4237 mddev
->bitmap_offset
= 0;
4240 md_update_sb(mddev
);
4244 static int set_disk_faulty(mddev_t
*mddev
, dev_t dev
)
4248 if (mddev
->pers
== NULL
)
4251 rdev
= find_rdev(mddev
, dev
);
4255 md_error(mddev
, rdev
);
4259 static int md_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
4261 mddev_t
*mddev
= bdev
->bd_disk
->private_data
;
4265 geo
->cylinders
= get_capacity(mddev
->gendisk
) / 8;
4269 static int md_ioctl(struct inode
*inode
, struct file
*file
,
4270 unsigned int cmd
, unsigned long arg
)
4273 void __user
*argp
= (void __user
*)arg
;
4274 mddev_t
*mddev
= NULL
;
4276 if (!capable(CAP_SYS_ADMIN
))
4280 * Commands dealing with the RAID driver but not any
4286 err
= get_version(argp
);
4289 case PRINT_RAID_DEBUG
:
4297 autostart_arrays(arg
);
4304 * Commands creating/starting a new array:
4307 mddev
= inode
->i_bdev
->bd_disk
->private_data
;
4315 if (cmd
== START_ARRAY
) {
4316 /* START_ARRAY doesn't need to lock the array as autostart_array
4317 * does the locking, and it could even be a different array
4322 "md: %s(pid %d) used deprecated START_ARRAY ioctl. "
4323 "This will not be supported beyond July 2006\n",
4324 current
->comm
, current
->pid
);
4327 err
= autostart_array(new_decode_dev(arg
));
4329 printk(KERN_WARNING
"md: autostart failed!\n");
4335 err
= mddev_lock(mddev
);
4338 "md: ioctl lock interrupted, reason %d, cmd %d\n",
4345 case SET_ARRAY_INFO
:
4347 mdu_array_info_t info
;
4349 memset(&info
, 0, sizeof(info
));
4350 else if (copy_from_user(&info
, argp
, sizeof(info
))) {
4355 err
= update_array_info(mddev
, &info
);
4357 printk(KERN_WARNING
"md: couldn't update"
4358 " array info. %d\n", err
);
4363 if (!list_empty(&mddev
->disks
)) {
4365 "md: array %s already has disks!\n",
4370 if (mddev
->raid_disks
) {
4372 "md: array %s already initialised!\n",
4377 err
= set_array_info(mddev
, &info
);
4379 printk(KERN_WARNING
"md: couldn't set"
4380 " array info. %d\n", err
);
4390 * Commands querying/configuring an existing array:
4392 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
4393 * RUN_ARRAY, and SET_BITMAP_FILE are allowed */
4394 if (!mddev
->raid_disks
&& cmd
!= ADD_NEW_DISK
&& cmd
!= STOP_ARRAY
4395 && cmd
!= RUN_ARRAY
&& cmd
!= SET_BITMAP_FILE
) {
4401 * Commands even a read-only array can execute:
4405 case GET_ARRAY_INFO
:
4406 err
= get_array_info(mddev
, argp
);
4409 case GET_BITMAP_FILE
:
4410 err
= get_bitmap_file(mddev
, argp
);
4414 err
= get_disk_info(mddev
, argp
);
4417 case RESTART_ARRAY_RW
:
4418 err
= restart_array(mddev
);
4422 err
= do_md_stop (mddev
, 0);
4426 err
= do_md_stop (mddev
, 1);
4430 * We have a problem here : there is no easy way to give a CHS
4431 * virtual geometry. We currently pretend that we have a 2 heads
4432 * 4 sectors (with a BIG number of cylinders...). This drives
4433 * dosfs just mad... ;-)
4438 * The remaining ioctls are changing the state of the
4439 * superblock, so we do not allow them on read-only arrays.
4440 * However non-MD ioctls (e.g. get-size) will still come through
4441 * here and hit the 'default' below, so only disallow
4442 * 'md' ioctls, and switch to rw mode if started auto-readonly.
4444 if (_IOC_TYPE(cmd
) == MD_MAJOR
&&
4445 mddev
->ro
&& mddev
->pers
) {
4446 if (mddev
->ro
== 2) {
4448 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4449 md_wakeup_thread(mddev
->thread
);
4461 mdu_disk_info_t info
;
4462 if (copy_from_user(&info
, argp
, sizeof(info
)))
4465 err
= add_new_disk(mddev
, &info
);
4469 case HOT_REMOVE_DISK
:
4470 err
= hot_remove_disk(mddev
, new_decode_dev(arg
));
4474 err
= hot_add_disk(mddev
, new_decode_dev(arg
));
4477 case SET_DISK_FAULTY
:
4478 err
= set_disk_faulty(mddev
, new_decode_dev(arg
));
4482 err
= do_md_run (mddev
);
4485 case SET_BITMAP_FILE
:
4486 err
= set_bitmap_file(mddev
, (int)arg
);
4496 mddev_unlock(mddev
);
4506 static int md_open(struct inode
*inode
, struct file
*file
)
4509 * Succeed if we can lock the mddev, which confirms that
4510 * it isn't being stopped right now.
4512 mddev_t
*mddev
= inode
->i_bdev
->bd_disk
->private_data
;
4515 if ((err
= mddev_lock(mddev
)))
4520 mddev_unlock(mddev
);
4522 check_disk_change(inode
->i_bdev
);
4527 static int md_release(struct inode
*inode
, struct file
* file
)
4529 mddev_t
*mddev
= inode
->i_bdev
->bd_disk
->private_data
;
4538 static int md_media_changed(struct gendisk
*disk
)
4540 mddev_t
*mddev
= disk
->private_data
;
4542 return mddev
->changed
;
4545 static int md_revalidate(struct gendisk
*disk
)
4547 mddev_t
*mddev
= disk
->private_data
;
4552 static struct block_device_operations md_fops
=
4554 .owner
= THIS_MODULE
,
4556 .release
= md_release
,
4558 .getgeo
= md_getgeo
,
4559 .media_changed
= md_media_changed
,
4560 .revalidate_disk
= md_revalidate
,
4563 static int md_thread(void * arg
)
4565 mdk_thread_t
*thread
= arg
;
4568 * md_thread is a 'system-thread', it's priority should be very
4569 * high. We avoid resource deadlocks individually in each
4570 * raid personality. (RAID5 does preallocation) We also use RR and
4571 * the very same RT priority as kswapd, thus we will never get
4572 * into a priority inversion deadlock.
4574 * we definitely have to have equal or higher priority than
4575 * bdflush, otherwise bdflush will deadlock if there are too
4576 * many dirty RAID5 blocks.
4579 allow_signal(SIGKILL
);
4580 while (!kthread_should_stop()) {
4582 /* We need to wait INTERRUPTIBLE so that
4583 * we don't add to the load-average.
4584 * That means we need to be sure no signals are
4587 if (signal_pending(current
))
4588 flush_signals(current
);
4590 wait_event_interruptible_timeout
4592 test_bit(THREAD_WAKEUP
, &thread
->flags
)
4593 || kthread_should_stop(),
4597 clear_bit(THREAD_WAKEUP
, &thread
->flags
);
4599 thread
->run(thread
->mddev
);
4605 void md_wakeup_thread(mdk_thread_t
*thread
)
4608 dprintk("md: waking up MD thread %s.\n", thread
->tsk
->comm
);
4609 set_bit(THREAD_WAKEUP
, &thread
->flags
);
4610 wake_up(&thread
->wqueue
);
4614 mdk_thread_t
*md_register_thread(void (*run
) (mddev_t
*), mddev_t
*mddev
,
4617 mdk_thread_t
*thread
;
4619 thread
= kzalloc(sizeof(mdk_thread_t
), GFP_KERNEL
);
4623 init_waitqueue_head(&thread
->wqueue
);
4626 thread
->mddev
= mddev
;
4627 thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
4628 thread
->tsk
= kthread_run(md_thread
, thread
, name
, mdname(thread
->mddev
));
4629 if (IS_ERR(thread
->tsk
)) {
4636 void md_unregister_thread(mdk_thread_t
*thread
)
4638 dprintk("interrupting MD-thread pid %d\n", thread
->tsk
->pid
);
4640 kthread_stop(thread
->tsk
);
4644 void md_error(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
4651 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
4654 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
4656 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
4657 __builtin_return_address(0),__builtin_return_address(1),
4658 __builtin_return_address(2),__builtin_return_address(3));
4660 if (!mddev
->pers
->error_handler
)
4662 mddev
->pers
->error_handler(mddev
,rdev
);
4663 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4664 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4665 md_wakeup_thread(mddev
->thread
);
4666 md_new_event_inintr(mddev
);
4669 /* seq_file implementation /proc/mdstat */
4671 static void status_unused(struct seq_file
*seq
)
4675 struct list_head
*tmp
;
4677 seq_printf(seq
, "unused devices: ");
4679 ITERATE_RDEV_PENDING(rdev
,tmp
) {
4680 char b
[BDEVNAME_SIZE
];
4682 seq_printf(seq
, "%s ",
4683 bdevname(rdev
->bdev
,b
));
4686 seq_printf(seq
, "<none>");
4688 seq_printf(seq
, "\n");
4692 static void status_resync(struct seq_file
*seq
, mddev_t
* mddev
)
4694 sector_t max_blocks
, resync
, res
;
4695 unsigned long dt
, db
, rt
;
4697 unsigned int per_milli
;
4699 resync
= (mddev
->curr_resync
- atomic_read(&mddev
->recovery_active
))/2;
4701 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
4702 max_blocks
= mddev
->resync_max_sectors
>> 1;
4704 max_blocks
= mddev
->size
;
4707 * Should not happen.
4713 /* Pick 'scale' such that (resync>>scale)*1000 will fit
4714 * in a sector_t, and (max_blocks>>scale) will fit in a
4715 * u32, as those are the requirements for sector_div.
4716 * Thus 'scale' must be at least 10
4719 if (sizeof(sector_t
) > sizeof(unsigned long)) {
4720 while ( max_blocks
/2 > (1ULL<<(scale
+32)))
4723 res
= (resync
>>scale
)*1000;
4724 sector_div(res
, (u32
)((max_blocks
>>scale
)+1));
4728 int i
, x
= per_milli
/50, y
= 20-x
;
4729 seq_printf(seq
, "[");
4730 for (i
= 0; i
< x
; i
++)
4731 seq_printf(seq
, "=");
4732 seq_printf(seq
, ">");
4733 for (i
= 0; i
< y
; i
++)
4734 seq_printf(seq
, ".");
4735 seq_printf(seq
, "] ");
4737 seq_printf(seq
, " %s =%3u.%u%% (%llu/%llu)",
4738 (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)?
4740 (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ?
4741 "resync" : "recovery")),
4742 per_milli
/10, per_milli
% 10,
4743 (unsigned long long) resync
,
4744 (unsigned long long) max_blocks
);
4747 * We do not want to overflow, so the order of operands and
4748 * the * 100 / 100 trick are important. We do a +1 to be
4749 * safe against division by zero. We only estimate anyway.
4751 * dt: time from mark until now
4752 * db: blocks written from mark until now
4753 * rt: remaining time
4755 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
4757 db
= resync
- (mddev
->resync_mark_cnt
/2);
4758 rt
= (dt
* ((unsigned long)(max_blocks
-resync
) / (db
/100+1)))/100;
4760 seq_printf(seq
, " finish=%lu.%lumin", rt
/ 60, (rt
% 60)/6);
4762 seq_printf(seq
, " speed=%ldK/sec", db
/dt
);
4765 static void *md_seq_start(struct seq_file
*seq
, loff_t
*pos
)
4767 struct list_head
*tmp
;
4777 spin_lock(&all_mddevs_lock
);
4778 list_for_each(tmp
,&all_mddevs
)
4780 mddev
= list_entry(tmp
, mddev_t
, all_mddevs
);
4782 spin_unlock(&all_mddevs_lock
);
4785 spin_unlock(&all_mddevs_lock
);
4787 return (void*)2;/* tail */
4791 static void *md_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
4793 struct list_head
*tmp
;
4794 mddev_t
*next_mddev
, *mddev
= v
;
4800 spin_lock(&all_mddevs_lock
);
4802 tmp
= all_mddevs
.next
;
4804 tmp
= mddev
->all_mddevs
.next
;
4805 if (tmp
!= &all_mddevs
)
4806 next_mddev
= mddev_get(list_entry(tmp
,mddev_t
,all_mddevs
));
4808 next_mddev
= (void*)2;
4811 spin_unlock(&all_mddevs_lock
);
4819 static void md_seq_stop(struct seq_file
*seq
, void *v
)
4823 if (mddev
&& v
!= (void*)1 && v
!= (void*)2)
4827 struct mdstat_info
{
4831 static int md_seq_show(struct seq_file
*seq
, void *v
)
4835 struct list_head
*tmp2
;
4837 struct mdstat_info
*mi
= seq
->private;
4838 struct bitmap
*bitmap
;
4840 if (v
== (void*)1) {
4841 struct mdk_personality
*pers
;
4842 seq_printf(seq
, "Personalities : ");
4843 spin_lock(&pers_lock
);
4844 list_for_each_entry(pers
, &pers_list
, list
)
4845 seq_printf(seq
, "[%s] ", pers
->name
);
4847 spin_unlock(&pers_lock
);
4848 seq_printf(seq
, "\n");
4849 mi
->event
= atomic_read(&md_event_count
);
4852 if (v
== (void*)2) {
4857 if (mddev_lock(mddev
) < 0)
4860 if (mddev
->pers
|| mddev
->raid_disks
|| !list_empty(&mddev
->disks
)) {
4861 seq_printf(seq
, "%s : %sactive", mdname(mddev
),
4862 mddev
->pers
? "" : "in");
4865 seq_printf(seq
, " (read-only)");
4867 seq_printf(seq
, "(auto-read-only)");
4868 seq_printf(seq
, " %s", mddev
->pers
->name
);
4872 ITERATE_RDEV(mddev
,rdev
,tmp2
) {
4873 char b
[BDEVNAME_SIZE
];
4874 seq_printf(seq
, " %s[%d]",
4875 bdevname(rdev
->bdev
,b
), rdev
->desc_nr
);
4876 if (test_bit(WriteMostly
, &rdev
->flags
))
4877 seq_printf(seq
, "(W)");
4878 if (test_bit(Faulty
, &rdev
->flags
)) {
4879 seq_printf(seq
, "(F)");
4881 } else if (rdev
->raid_disk
< 0)
4882 seq_printf(seq
, "(S)"); /* spare */
4886 if (!list_empty(&mddev
->disks
)) {
4888 seq_printf(seq
, "\n %llu blocks",
4889 (unsigned long long)mddev
->array_size
);
4891 seq_printf(seq
, "\n %llu blocks",
4892 (unsigned long long)size
);
4894 if (mddev
->persistent
) {
4895 if (mddev
->major_version
!= 0 ||
4896 mddev
->minor_version
!= 90) {
4897 seq_printf(seq
," super %d.%d",
4898 mddev
->major_version
,
4899 mddev
->minor_version
);
4902 seq_printf(seq
, " super non-persistent");
4905 mddev
->pers
->status (seq
, mddev
);
4906 seq_printf(seq
, "\n ");
4907 if (mddev
->pers
->sync_request
) {
4908 if (mddev
->curr_resync
> 2) {
4909 status_resync (seq
, mddev
);
4910 seq_printf(seq
, "\n ");
4911 } else if (mddev
->curr_resync
== 1 || mddev
->curr_resync
== 2)
4912 seq_printf(seq
, "\tresync=DELAYED\n ");
4913 else if (mddev
->recovery_cp
< MaxSector
)
4914 seq_printf(seq
, "\tresync=PENDING\n ");
4917 seq_printf(seq
, "\n ");
4919 if ((bitmap
= mddev
->bitmap
)) {
4920 unsigned long chunk_kb
;
4921 unsigned long flags
;
4922 spin_lock_irqsave(&bitmap
->lock
, flags
);
4923 chunk_kb
= bitmap
->chunksize
>> 10;
4924 seq_printf(seq
, "bitmap: %lu/%lu pages [%luKB], "
4926 bitmap
->pages
- bitmap
->missing_pages
,
4928 (bitmap
->pages
- bitmap
->missing_pages
)
4929 << (PAGE_SHIFT
- 10),
4930 chunk_kb
? chunk_kb
: bitmap
->chunksize
,
4931 chunk_kb
? "KB" : "B");
4933 seq_printf(seq
, ", file: ");
4934 seq_path(seq
, bitmap
->file
->f_vfsmnt
,
4935 bitmap
->file
->f_dentry
," \t\n");
4938 seq_printf(seq
, "\n");
4939 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
4942 seq_printf(seq
, "\n");
4944 mddev_unlock(mddev
);
4949 static struct seq_operations md_seq_ops
= {
4950 .start
= md_seq_start
,
4951 .next
= md_seq_next
,
4952 .stop
= md_seq_stop
,
4953 .show
= md_seq_show
,
4956 static int md_seq_open(struct inode
*inode
, struct file
*file
)
4959 struct mdstat_info
*mi
= kmalloc(sizeof(*mi
), GFP_KERNEL
);
4963 error
= seq_open(file
, &md_seq_ops
);
4967 struct seq_file
*p
= file
->private_data
;
4969 mi
->event
= atomic_read(&md_event_count
);
4974 static int md_seq_release(struct inode
*inode
, struct file
*file
)
4976 struct seq_file
*m
= file
->private_data
;
4977 struct mdstat_info
*mi
= m
->private;
4980 return seq_release(inode
, file
);
4983 static unsigned int mdstat_poll(struct file
*filp
, poll_table
*wait
)
4985 struct seq_file
*m
= filp
->private_data
;
4986 struct mdstat_info
*mi
= m
->private;
4989 poll_wait(filp
, &md_event_waiters
, wait
);
4991 /* always allow read */
4992 mask
= POLLIN
| POLLRDNORM
;
4994 if (mi
->event
!= atomic_read(&md_event_count
))
4995 mask
|= POLLERR
| POLLPRI
;
4999 static struct file_operations md_seq_fops
= {
5000 .open
= md_seq_open
,
5002 .llseek
= seq_lseek
,
5003 .release
= md_seq_release
,
5004 .poll
= mdstat_poll
,
5007 int register_md_personality(struct mdk_personality
*p
)
5009 spin_lock(&pers_lock
);
5010 list_add_tail(&p
->list
, &pers_list
);
5011 printk(KERN_INFO
"md: %s personality registered for level %d\n", p
->name
, p
->level
);
5012 spin_unlock(&pers_lock
);
5016 int unregister_md_personality(struct mdk_personality
*p
)
5018 printk(KERN_INFO
"md: %s personality unregistered\n", p
->name
);
5019 spin_lock(&pers_lock
);
5020 list_del_init(&p
->list
);
5021 spin_unlock(&pers_lock
);
5025 static int is_mddev_idle(mddev_t
*mddev
)
5028 struct list_head
*tmp
;
5030 unsigned long curr_events
;
5033 ITERATE_RDEV(mddev
,rdev
,tmp
) {
5034 struct gendisk
*disk
= rdev
->bdev
->bd_contains
->bd_disk
;
5035 curr_events
= disk_stat_read(disk
, sectors
[0]) +
5036 disk_stat_read(disk
, sectors
[1]) -
5037 atomic_read(&disk
->sync_io
);
5038 /* The difference between curr_events and last_events
5039 * will be affected by any new non-sync IO (making
5040 * curr_events bigger) and any difference in the amount of
5041 * in-flight syncio (making current_events bigger or smaller)
5042 * The amount in-flight is currently limited to
5043 * 32*64K in raid1/10 and 256*PAGE_SIZE in raid5/6
5044 * which is at most 4096 sectors.
5045 * These numbers are fairly fragile and should be made
5046 * more robust, probably by enforcing the
5047 * 'window size' that md_do_sync sort-of uses.
5049 * Note: the following is an unsigned comparison.
5051 if ((curr_events
- rdev
->last_events
+ 4096) > 8192) {
5052 rdev
->last_events
= curr_events
;
5059 void md_done_sync(mddev_t
*mddev
, int blocks
, int ok
)
5061 /* another "blocks" (512byte) blocks have been synced */
5062 atomic_sub(blocks
, &mddev
->recovery_active
);
5063 wake_up(&mddev
->recovery_wait
);
5065 set_bit(MD_RECOVERY_ERR
, &mddev
->recovery
);
5066 md_wakeup_thread(mddev
->thread
);
5067 // stop recovery, signal do_sync ....
5072 /* md_write_start(mddev, bi)
5073 * If we need to update some array metadata (e.g. 'active' flag
5074 * in superblock) before writing, schedule a superblock update
5075 * and wait for it to complete.
5077 void md_write_start(mddev_t
*mddev
, struct bio
*bi
)
5079 if (bio_data_dir(bi
) != WRITE
)
5082 BUG_ON(mddev
->ro
== 1);
5083 if (mddev
->ro
== 2) {
5084 /* need to switch to read/write */
5086 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5087 md_wakeup_thread(mddev
->thread
);
5089 atomic_inc(&mddev
->writes_pending
);
5090 if (mddev
->in_sync
) {
5091 spin_lock_irq(&mddev
->write_lock
);
5092 if (mddev
->in_sync
) {
5094 mddev
->sb_dirty
= 3;
5095 md_wakeup_thread(mddev
->thread
);
5097 spin_unlock_irq(&mddev
->write_lock
);
5099 wait_event(mddev
->sb_wait
, mddev
->sb_dirty
==0);
5102 void md_write_end(mddev_t
*mddev
)
5104 if (atomic_dec_and_test(&mddev
->writes_pending
)) {
5105 if (mddev
->safemode
== 2)
5106 md_wakeup_thread(mddev
->thread
);
5107 else if (mddev
->safemode_delay
)
5108 mod_timer(&mddev
->safemode_timer
, jiffies
+ mddev
->safemode_delay
);
5112 static DECLARE_WAIT_QUEUE_HEAD(resync_wait
);
5114 #define SYNC_MARKS 10
5115 #define SYNC_MARK_STEP (3*HZ)
5116 void md_do_sync(mddev_t
*mddev
)
5119 unsigned int currspeed
= 0,
5121 sector_t max_sectors
,j
, io_sectors
;
5122 unsigned long mark
[SYNC_MARKS
];
5123 sector_t mark_cnt
[SYNC_MARKS
];
5125 struct list_head
*tmp
;
5126 sector_t last_check
;
5128 struct list_head
*rtmp
;
5131 /* just incase thread restarts... */
5132 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
5134 if (mddev
->ro
) /* never try to sync a read-only array */
5137 /* we overload curr_resync somewhat here.
5138 * 0 == not engaged in resync at all
5139 * 2 == checking that there is no conflict with another sync
5140 * 1 == like 2, but have yielded to allow conflicting resync to
5142 * other == active in resync - this many blocks
5144 * Before starting a resync we must have set curr_resync to
5145 * 2, and then checked that every "conflicting" array has curr_resync
5146 * less than ours. When we find one that is the same or higher
5147 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
5148 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
5149 * This will mean we have to start checking from the beginning again.
5154 mddev
->curr_resync
= 2;
5157 if (kthread_should_stop()) {
5158 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5161 ITERATE_MDDEV(mddev2
,tmp
) {
5162 if (mddev2
== mddev
)
5164 if (mddev2
->curr_resync
&&
5165 match_mddev_units(mddev
,mddev2
)) {
5167 if (mddev
< mddev2
&& mddev
->curr_resync
== 2) {
5168 /* arbitrarily yield */
5169 mddev
->curr_resync
= 1;
5170 wake_up(&resync_wait
);
5172 if (mddev
> mddev2
&& mddev
->curr_resync
== 1)
5173 /* no need to wait here, we can wait the next
5174 * time 'round when curr_resync == 2
5177 prepare_to_wait(&resync_wait
, &wq
, TASK_UNINTERRUPTIBLE
);
5178 if (!kthread_should_stop() &&
5179 mddev2
->curr_resync
>= mddev
->curr_resync
) {
5180 printk(KERN_INFO
"md: delaying resync of %s"
5181 " until %s has finished resync (they"
5182 " share one or more physical units)\n",
5183 mdname(mddev
), mdname(mddev2
));
5186 finish_wait(&resync_wait
, &wq
);
5189 finish_wait(&resync_wait
, &wq
);
5192 } while (mddev
->curr_resync
< 2);
5195 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
5196 /* resync follows the size requested by the personality,
5197 * which defaults to physical size, but can be virtual size
5199 max_sectors
= mddev
->resync_max_sectors
;
5200 mddev
->resync_mismatches
= 0;
5201 /* we don't use the checkpoint if there's a bitmap */
5202 if (!mddev
->bitmap
&&
5203 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
5204 j
= mddev
->recovery_cp
;
5205 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
5206 max_sectors
= mddev
->size
<< 1;
5208 /* recovery follows the physical size of devices */
5209 max_sectors
= mddev
->size
<< 1;
5211 ITERATE_RDEV(mddev
,rdev
,rtmp
)
5212 if (rdev
->raid_disk
>= 0 &&
5213 !test_bit(Faulty
, &rdev
->flags
) &&
5214 !test_bit(In_sync
, &rdev
->flags
) &&
5215 rdev
->recovery_offset
< j
)
5216 j
= rdev
->recovery_offset
;
5219 printk(KERN_INFO
"md: syncing RAID array %s\n", mdname(mddev
));
5220 printk(KERN_INFO
"md: minimum _guaranteed_ reconstruction speed:"
5221 " %d KB/sec/disc.\n", speed_min(mddev
));
5222 printk(KERN_INFO
"md: using maximum available idle IO bandwidth "
5223 "(but not more than %d KB/sec) for reconstruction.\n",
5226 is_mddev_idle(mddev
); /* this also initializes IO event counters */
5229 for (m
= 0; m
< SYNC_MARKS
; m
++) {
5231 mark_cnt
[m
] = io_sectors
;
5234 mddev
->resync_mark
= mark
[last_mark
];
5235 mddev
->resync_mark_cnt
= mark_cnt
[last_mark
];
5238 * Tune reconstruction:
5240 window
= 32*(PAGE_SIZE
/512);
5241 printk(KERN_INFO
"md: using %dk window, over a total of %llu blocks.\n",
5242 window
/2,(unsigned long long) max_sectors
/2);
5244 atomic_set(&mddev
->recovery_active
, 0);
5245 init_waitqueue_head(&mddev
->recovery_wait
);
5250 "md: resuming recovery of %s from checkpoint.\n",
5252 mddev
->curr_resync
= j
;
5255 while (j
< max_sectors
) {
5259 if (j
>= mddev
->resync_max
) {
5260 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
5261 wait_event(mddev
->recovery_wait
,
5262 mddev
->resync_max
> j
|| kthread_should_stop());
5264 if (kthread_should_stop())
5266 sectors
= mddev
->pers
->sync_request(mddev
, j
, &skipped
,
5267 currspeed
< speed_min(mddev
));
5269 set_bit(MD_RECOVERY_ERR
, &mddev
->recovery
);
5273 if (!skipped
) { /* actual IO requested */
5274 io_sectors
+= sectors
;
5275 atomic_add(sectors
, &mddev
->recovery_active
);
5279 if (j
>1) mddev
->curr_resync
= j
;
5280 if (last_check
== 0)
5281 /* this is the earliers that rebuilt will be
5282 * visible in /proc/mdstat
5284 md_new_event(mddev
);
5286 if (last_check
+ window
> io_sectors
|| j
== max_sectors
)
5289 last_check
= io_sectors
;
5291 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) ||
5292 test_bit(MD_RECOVERY_ERR
, &mddev
->recovery
))
5296 if (time_after_eq(jiffies
, mark
[last_mark
] + SYNC_MARK_STEP
)) {
5298 int next
= (last_mark
+1) % SYNC_MARKS
;
5300 mddev
->resync_mark
= mark
[next
];
5301 mddev
->resync_mark_cnt
= mark_cnt
[next
];
5302 mark
[next
] = jiffies
;
5303 mark_cnt
[next
] = io_sectors
- atomic_read(&mddev
->recovery_active
);
5308 if (kthread_should_stop())
5313 * this loop exits only if either when we are slower than
5314 * the 'hard' speed limit, or the system was IO-idle for
5316 * the system might be non-idle CPU-wise, but we only care
5317 * about not overloading the IO subsystem. (things like an
5318 * e2fsck being done on the RAID array should execute fast)
5320 mddev
->queue
->unplug_fn(mddev
->queue
);
5323 currspeed
= ((unsigned long)(io_sectors
-mddev
->resync_mark_cnt
))/2
5324 /((jiffies
-mddev
->resync_mark
)/HZ
+1) +1;
5326 if (currspeed
> speed_min(mddev
)) {
5327 if ((currspeed
> speed_max(mddev
)) ||
5328 !is_mddev_idle(mddev
)) {
5334 printk(KERN_INFO
"md: %s: sync done.\n",mdname(mddev
));
5336 * this also signals 'finished resyncing' to md_stop
5339 mddev
->queue
->unplug_fn(mddev
->queue
);
5341 wait_event(mddev
->recovery_wait
, !atomic_read(&mddev
->recovery_active
));
5343 /* tell personality that we are finished */
5344 mddev
->pers
->sync_request(mddev
, max_sectors
, &skipped
, 1);
5346 if (!test_bit(MD_RECOVERY_ERR
, &mddev
->recovery
) &&
5347 test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) &&
5348 !test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
) &&
5349 mddev
->curr_resync
> 2) {
5350 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
5351 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
5352 if (mddev
->curr_resync
>= mddev
->recovery_cp
) {
5354 "md: checkpointing recovery of %s.\n",
5356 mddev
->recovery_cp
= mddev
->curr_resync
;
5359 mddev
->recovery_cp
= MaxSector
;
5361 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
5362 mddev
->curr_resync
= MaxSector
;
5363 ITERATE_RDEV(mddev
,rdev
,rtmp
)
5364 if (rdev
->raid_disk
>= 0 &&
5365 !test_bit(Faulty
, &rdev
->flags
) &&
5366 !test_bit(In_sync
, &rdev
->flags
) &&
5367 rdev
->recovery_offset
< mddev
->curr_resync
)
5368 rdev
->recovery_offset
= mddev
->curr_resync
;
5373 mddev
->curr_resync
= 0;
5374 mddev
->resync_max
= MaxSector
;
5375 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
5376 wake_up(&resync_wait
);
5377 set_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
5378 md_wakeup_thread(mddev
->thread
);
5383 * got a signal, exit.
5386 "md: md_do_sync() got signal ... exiting\n");
5387 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5391 EXPORT_SYMBOL_GPL(md_do_sync
);
5395 * This routine is regularly called by all per-raid-array threads to
5396 * deal with generic issues like resync and super-block update.
5397 * Raid personalities that don't have a thread (linear/raid0) do not
5398 * need this as they never do any recovery or update the superblock.
5400 * It does not do any resync itself, but rather "forks" off other threads
5401 * to do that as needed.
5402 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
5403 * "->recovery" and create a thread at ->sync_thread.
5404 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
5405 * and wakeups up this thread which will reap the thread and finish up.
5406 * This thread also removes any faulty devices (with nr_pending == 0).
5408 * The overall approach is:
5409 * 1/ if the superblock needs updating, update it.
5410 * 2/ If a recovery thread is running, don't do anything else.
5411 * 3/ If recovery has finished, clean up, possibly marking spares active.
5412 * 4/ If there are any faulty devices, remove them.
5413 * 5/ If array is degraded, try to add spares devices
5414 * 6/ If array has spares or is not in-sync, start a resync thread.
5416 void md_check_recovery(mddev_t
*mddev
)
5419 struct list_head
*rtmp
;
5423 bitmap_daemon_work(mddev
->bitmap
);
5428 if (signal_pending(current
)) {
5429 if (mddev
->pers
->sync_request
) {
5430 printk(KERN_INFO
"md: %s in immediate safe mode\n",
5432 mddev
->safemode
= 2;
5434 flush_signals(current
);
5438 (mddev
->sb_dirty
&& !mddev
->external
) ||
5439 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
5440 test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
5441 (mddev
->safemode
== 1) ||
5442 (mddev
->safemode
== 2 && ! atomic_read(&mddev
->writes_pending
)
5443 && !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
5447 if (mddev_trylock(mddev
)) {
5450 spin_lock_irq(&mddev
->write_lock
);
5451 if (mddev
->safemode
&& !atomic_read(&mddev
->writes_pending
) &&
5452 !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
) {
5454 if (mddev
->persistent
)
5455 mddev
->sb_dirty
= 3;
5457 if (mddev
->safemode
== 1)
5458 mddev
->safemode
= 0;
5459 spin_unlock_irq(&mddev
->write_lock
);
5461 if (mddev
->sb_dirty
)
5462 md_update_sb(mddev
);
5465 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
5466 !test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
)) {
5467 /* resync/recovery still happening */
5468 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5471 if (mddev
->sync_thread
) {
5472 /* resync has finished, collect result */
5473 md_unregister_thread(mddev
->sync_thread
);
5474 mddev
->sync_thread
= NULL
;
5475 if (!test_bit(MD_RECOVERY_ERR
, &mddev
->recovery
) &&
5476 !test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
5478 /* activate any spares */
5479 mddev
->pers
->spare_active(mddev
);
5481 md_update_sb(mddev
);
5483 /* if array is no-longer degraded, then any saved_raid_disk
5484 * information must be scrapped
5486 if (!mddev
->degraded
)
5487 ITERATE_RDEV(mddev
,rdev
,rtmp
)
5488 rdev
->saved_raid_disk
= -1;
5490 mddev
->recovery
= 0;
5491 /* flag recovery needed just to double check */
5492 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5493 md_new_event(mddev
);
5496 /* Clear some bits that don't mean anything, but
5499 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5500 clear_bit(MD_RECOVERY_ERR
, &mddev
->recovery
);
5501 clear_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5502 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
5504 if (test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
5506 /* no recovery is running.
5507 * remove any failed drives, then
5508 * add spares if possible.
5509 * Spare are also removed and re-added, to allow
5510 * the personality to fail the re-add.
5512 ITERATE_RDEV(mddev
,rdev
,rtmp
)
5513 if (rdev
->raid_disk
>= 0 &&
5514 (test_bit(Faulty
, &rdev
->flags
) || ! test_bit(In_sync
, &rdev
->flags
)) &&
5515 atomic_read(&rdev
->nr_pending
)==0) {
5516 if (mddev
->pers
->hot_remove_disk(mddev
, rdev
->raid_disk
)==0) {
5518 sprintf(nm
,"rd%d", rdev
->raid_disk
);
5519 sysfs_remove_link(&mddev
->kobj
, nm
);
5520 rdev
->raid_disk
= -1;
5524 if (mddev
->degraded
) {
5525 ITERATE_RDEV(mddev
,rdev
,rtmp
)
5526 if (rdev
->raid_disk
< 0
5527 && !test_bit(Faulty
, &rdev
->flags
)) {
5528 rdev
->recovery_offset
= 0;
5529 if (mddev
->pers
->hot_add_disk(mddev
,rdev
)) {
5531 sprintf(nm
, "rd%d", rdev
->raid_disk
);
5532 sysfs_create_link(&mddev
->kobj
, &rdev
->kobj
, nm
);
5534 md_new_event(mddev
);
5541 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
5542 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
5543 } else if (mddev
->recovery_cp
< MaxSector
) {
5544 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
5545 } else if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
5546 /* nothing to be done ... */
5549 if (mddev
->pers
->sync_request
) {
5550 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
5551 if (spares
&& mddev
->bitmap
&& ! mddev
->bitmap
->file
) {
5552 /* We are adding a device or devices to an array
5553 * which has the bitmap stored on all devices.
5554 * So make sure all bitmap pages get written
5556 bitmap_write_all(mddev
->bitmap
);
5558 mddev
->sync_thread
= md_register_thread(md_do_sync
,
5561 if (!mddev
->sync_thread
) {
5562 printk(KERN_ERR
"%s: could not start resync"
5565 /* leave the spares where they are, it shouldn't hurt */
5566 mddev
->recovery
= 0;
5568 md_wakeup_thread(mddev
->sync_thread
);
5569 md_new_event(mddev
);
5572 mddev_unlock(mddev
);
5576 static int md_notify_reboot(struct notifier_block
*this,
5577 unsigned long code
, void *x
)
5579 struct list_head
*tmp
;
5582 if ((code
== SYS_DOWN
) || (code
== SYS_HALT
) || (code
== SYS_POWER_OFF
)) {
5584 printk(KERN_INFO
"md: stopping all md devices.\n");
5586 ITERATE_MDDEV(mddev
,tmp
)
5587 if (mddev_trylock(mddev
)) {
5588 do_md_stop (mddev
, 1);
5589 mddev_unlock(mddev
);
5592 * certain more exotic SCSI devices are known to be
5593 * volatile wrt too early system reboots. While the
5594 * right place to handle this issue is the given
5595 * driver, we do want to have a safe RAID driver ...
5602 static struct notifier_block md_notifier
= {
5603 .notifier_call
= md_notify_reboot
,
5605 .priority
= INT_MAX
, /* before any real devices */
5608 static void md_geninit(void)
5610 struct proc_dir_entry
*p
;
5612 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t
));
5614 p
= create_proc_entry("mdstat", S_IRUGO
, NULL
);
5616 p
->proc_fops
= &md_seq_fops
;
5619 static int __init
md_init(void)
5623 printk(KERN_INFO
"md: md driver %d.%d.%d MAX_MD_DEVS=%d,"
5624 " MD_SB_DISKS=%d\n",
5625 MD_MAJOR_VERSION
, MD_MINOR_VERSION
,
5626 MD_PATCHLEVEL_VERSION
, MAX_MD_DEVS
, MD_SB_DISKS
);
5627 printk(KERN_INFO
"md: bitmap version %d.%d\n", BITMAP_MAJOR_HI
,
5630 if (register_blkdev(MAJOR_NR
, "md"))
5632 if ((mdp_major
=register_blkdev(0, "mdp"))<=0) {
5633 unregister_blkdev(MAJOR_NR
, "md");
5637 blk_register_region(MKDEV(MAJOR_NR
, 0), MAX_MD_DEVS
, THIS_MODULE
,
5638 md_probe
, NULL
, NULL
);
5639 blk_register_region(MKDEV(mdp_major
, 0), MAX_MD_DEVS
<<MdpMinorShift
, THIS_MODULE
,
5640 md_probe
, NULL
, NULL
);
5642 for (minor
=0; minor
< MAX_MD_DEVS
; ++minor
)
5643 devfs_mk_bdev(MKDEV(MAJOR_NR
, minor
),
5644 S_IFBLK
|S_IRUSR
|S_IWUSR
,
5647 for (minor
=0; minor
< MAX_MD_DEVS
; ++minor
)
5648 devfs_mk_bdev(MKDEV(mdp_major
, minor
<<MdpMinorShift
),
5649 S_IFBLK
|S_IRUSR
|S_IWUSR
,
5653 register_reboot_notifier(&md_notifier
);
5654 raid_table_header
= register_sysctl_table(raid_root_table
, 1);
5664 * Searches all registered partitions for autorun RAID arrays
5667 static dev_t detected_devices
[128];
5670 void md_autodetect_dev(dev_t dev
)
5672 if (dev_cnt
>= 0 && dev_cnt
< 127)
5673 detected_devices
[dev_cnt
++] = dev
;
5677 static void autostart_arrays(int part
)
5682 printk(KERN_INFO
"md: Autodetecting RAID arrays.\n");
5684 for (i
= 0; i
< dev_cnt
; i
++) {
5685 dev_t dev
= detected_devices
[i
];
5687 rdev
= md_import_device(dev
,0, 0);
5691 if (test_bit(Faulty
, &rdev
->flags
)) {
5695 list_add(&rdev
->same_set
, &pending_raid_disks
);
5699 autorun_devices(part
);
5704 static __exit
void md_exit(void)
5707 struct list_head
*tmp
;
5709 blk_unregister_region(MKDEV(MAJOR_NR
,0), MAX_MD_DEVS
);
5710 blk_unregister_region(MKDEV(mdp_major
,0), MAX_MD_DEVS
<< MdpMinorShift
);
5711 for (i
=0; i
< MAX_MD_DEVS
; i
++)
5712 devfs_remove("md/%d", i
);
5713 for (i
=0; i
< MAX_MD_DEVS
; i
++)
5714 devfs_remove("md/d%d", i
);
5718 unregister_blkdev(MAJOR_NR
,"md");
5719 unregister_blkdev(mdp_major
, "mdp");
5720 unregister_reboot_notifier(&md_notifier
);
5721 unregister_sysctl_table(raid_table_header
);
5722 remove_proc_entry("mdstat", NULL
);
5723 ITERATE_MDDEV(mddev
,tmp
) {
5724 struct gendisk
*disk
= mddev
->gendisk
;
5727 export_array(mddev
);
5730 mddev
->gendisk
= NULL
;
5735 module_init(md_init
)
5736 module_exit(md_exit
)
5738 static int get_ro(char *buffer
, struct kernel_param
*kp
)
5740 return sprintf(buffer
, "%d", start_readonly
);
5742 static int set_ro(const char *val
, struct kernel_param
*kp
)
5745 int num
= simple_strtoul(val
, &e
, 10);
5746 if (*val
&& (*e
== '\0' || *e
== '\n')) {
5747 start_readonly
= num
;
5753 module_param_call(start_ro
, set_ro
, get_ro
, NULL
, S_IRUSR
|S_IWUSR
);
5754 module_param(start_dirty_degraded
, int, S_IRUGO
|S_IWUSR
);
5757 EXPORT_SYMBOL(register_md_personality
);
5758 EXPORT_SYMBOL(unregister_md_personality
);
5759 EXPORT_SYMBOL(md_error
);
5760 EXPORT_SYMBOL(md_done_sync
);
5761 EXPORT_SYMBOL(md_write_start
);
5762 EXPORT_SYMBOL(md_write_end
);
5763 EXPORT_SYMBOL(md_register_thread
);
5764 EXPORT_SYMBOL(md_unregister_thread
);
5765 EXPORT_SYMBOL(md_wakeup_thread
);
5766 EXPORT_SYMBOL(md_check_recovery
);
5767 MODULE_LICENSE("GPL");
5769 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR
);