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>
46 #include <linux/init.h>
48 #include <linux/file.h>
51 #include <linux/kmod.h>
54 #include <asm/unaligned.h>
56 #define MAJOR_NR MD_MAJOR
59 /* 63 partitions with the alternate major number (mdp) */
60 #define MdpMinorShift 6
63 #define dprintk(x...) ((void)(DEBUG && printk(x)))
67 static void autostart_arrays (int part
);
70 static mdk_personality_t
*pers
[MAX_PERSONALITY
];
71 static DEFINE_SPINLOCK(pers_lock
);
74 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
75 * is 1000 KB/sec, so the extra system load does not show up that much.
76 * Increase it if you want to have more _guaranteed_ speed. Note that
77 * the RAID driver will use the maximum available bandwidth if the IO
78 * subsystem is idle. There is also an 'absolute maximum' reconstruction
79 * speed limit - in case reconstruction slows down your system despite
82 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
85 static int sysctl_speed_limit_min
= 1000;
86 static int sysctl_speed_limit_max
= 200000;
88 static struct ctl_table_header
*raid_table_header
;
90 static ctl_table raid_table
[] = {
92 .ctl_name
= DEV_RAID_SPEED_LIMIT_MIN
,
93 .procname
= "speed_limit_min",
94 .data
= &sysctl_speed_limit_min
,
95 .maxlen
= sizeof(int),
97 .proc_handler
= &proc_dointvec
,
100 .ctl_name
= DEV_RAID_SPEED_LIMIT_MAX
,
101 .procname
= "speed_limit_max",
102 .data
= &sysctl_speed_limit_max
,
103 .maxlen
= sizeof(int),
105 .proc_handler
= &proc_dointvec
,
110 static ctl_table raid_dir_table
[] = {
112 .ctl_name
= DEV_RAID
,
121 static ctl_table raid_root_table
[] = {
127 .child
= raid_dir_table
,
132 static struct block_device_operations md_fops
;
135 * Enables to iterate over all existing md arrays
136 * all_mddevs_lock protects this list.
138 static LIST_HEAD(all_mddevs
);
139 static DEFINE_SPINLOCK(all_mddevs_lock
);
143 * iterates through all used mddevs in the system.
144 * We take care to grab the all_mddevs_lock whenever navigating
145 * the list, and to always hold a refcount when unlocked.
146 * Any code which breaks out of this loop while own
147 * a reference to the current mddev and must mddev_put it.
149 #define ITERATE_MDDEV(mddev,tmp) \
151 for (({ spin_lock(&all_mddevs_lock); \
152 tmp = all_mddevs.next; \
154 ({ if (tmp != &all_mddevs) \
155 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
156 spin_unlock(&all_mddevs_lock); \
157 if (mddev) mddev_put(mddev); \
158 mddev = list_entry(tmp, mddev_t, all_mddevs); \
159 tmp != &all_mddevs;}); \
160 ({ spin_lock(&all_mddevs_lock); \
165 static int md_fail_request (request_queue_t
*q
, struct bio
*bio
)
167 bio_io_error(bio
, bio
->bi_size
);
171 static inline mddev_t
*mddev_get(mddev_t
*mddev
)
173 atomic_inc(&mddev
->active
);
177 static void mddev_put(mddev_t
*mddev
)
179 if (!atomic_dec_and_lock(&mddev
->active
, &all_mddevs_lock
))
181 if (!mddev
->raid_disks
&& list_empty(&mddev
->disks
)) {
182 list_del(&mddev
->all_mddevs
);
183 blk_put_queue(mddev
->queue
);
184 kobject_unregister(&mddev
->kobj
);
186 spin_unlock(&all_mddevs_lock
);
189 static mddev_t
* mddev_find(dev_t unit
)
191 mddev_t
*mddev
, *new = NULL
;
194 spin_lock(&all_mddevs_lock
);
195 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
196 if (mddev
->unit
== unit
) {
198 spin_unlock(&all_mddevs_lock
);
204 list_add(&new->all_mddevs
, &all_mddevs
);
205 spin_unlock(&all_mddevs_lock
);
208 spin_unlock(&all_mddevs_lock
);
210 new = (mddev_t
*) kmalloc(sizeof(*new), GFP_KERNEL
);
214 memset(new, 0, sizeof(*new));
217 if (MAJOR(unit
) == MD_MAJOR
)
218 new->md_minor
= MINOR(unit
);
220 new->md_minor
= MINOR(unit
) >> MdpMinorShift
;
222 init_MUTEX(&new->reconfig_sem
);
223 INIT_LIST_HEAD(&new->disks
);
224 INIT_LIST_HEAD(&new->all_mddevs
);
225 init_timer(&new->safemode_timer
);
226 atomic_set(&new->active
, 1);
227 spin_lock_init(&new->write_lock
);
228 init_waitqueue_head(&new->sb_wait
);
230 new->queue
= blk_alloc_queue(GFP_KERNEL
);
236 blk_queue_make_request(new->queue
, md_fail_request
);
241 static inline int mddev_lock(mddev_t
* mddev
)
243 return down_interruptible(&mddev
->reconfig_sem
);
246 static inline void mddev_lock_uninterruptible(mddev_t
* mddev
)
248 down(&mddev
->reconfig_sem
);
251 static inline int mddev_trylock(mddev_t
* mddev
)
253 return down_trylock(&mddev
->reconfig_sem
);
256 static inline void mddev_unlock(mddev_t
* mddev
)
258 up(&mddev
->reconfig_sem
);
260 md_wakeup_thread(mddev
->thread
);
263 mdk_rdev_t
* find_rdev_nr(mddev_t
*mddev
, int nr
)
266 struct list_head
*tmp
;
268 ITERATE_RDEV(mddev
,rdev
,tmp
) {
269 if (rdev
->desc_nr
== nr
)
275 static mdk_rdev_t
* find_rdev(mddev_t
* mddev
, dev_t dev
)
277 struct list_head
*tmp
;
280 ITERATE_RDEV(mddev
,rdev
,tmp
) {
281 if (rdev
->bdev
->bd_dev
== dev
)
287 static inline sector_t
calc_dev_sboffset(struct block_device
*bdev
)
289 sector_t size
= bdev
->bd_inode
->i_size
>> BLOCK_SIZE_BITS
;
290 return MD_NEW_SIZE_BLOCKS(size
);
293 static sector_t
calc_dev_size(mdk_rdev_t
*rdev
, unsigned chunk_size
)
297 size
= rdev
->sb_offset
;
300 size
&= ~((sector_t
)chunk_size
/1024 - 1);
304 static int alloc_disk_sb(mdk_rdev_t
* rdev
)
309 rdev
->sb_page
= alloc_page(GFP_KERNEL
);
310 if (!rdev
->sb_page
) {
311 printk(KERN_ALERT
"md: out of memory.\n");
318 static void free_disk_sb(mdk_rdev_t
* rdev
)
321 page_cache_release(rdev
->sb_page
);
323 rdev
->sb_page
= NULL
;
330 static int super_written(struct bio
*bio
, unsigned int bytes_done
, int error
)
332 mdk_rdev_t
*rdev
= bio
->bi_private
;
336 if (error
|| !test_bit(BIO_UPTODATE
, &bio
->bi_flags
))
337 md_error(rdev
->mddev
, rdev
);
339 if (atomic_dec_and_test(&rdev
->mddev
->pending_writes
))
340 wake_up(&rdev
->mddev
->sb_wait
);
345 void md_super_write(mddev_t
*mddev
, mdk_rdev_t
*rdev
,
346 sector_t sector
, int size
, struct page
*page
)
348 /* write first size bytes of page to sector of rdev
349 * Increment mddev->pending_writes before returning
350 * and decrement it on completion, waking up sb_wait
351 * if zero is reached.
352 * If an error occurred, call md_error
354 struct bio
*bio
= bio_alloc(GFP_NOIO
, 1);
356 bio
->bi_bdev
= rdev
->bdev
;
357 bio
->bi_sector
= sector
;
358 bio_add_page(bio
, page
, size
, 0);
359 bio
->bi_private
= rdev
;
360 bio
->bi_end_io
= super_written
;
361 atomic_inc(&mddev
->pending_writes
);
362 submit_bio((1<<BIO_RW
)|(1<<BIO_RW_SYNC
), bio
);
365 static int bi_complete(struct bio
*bio
, unsigned int bytes_done
, int error
)
370 complete((struct completion
*)bio
->bi_private
);
374 int sync_page_io(struct block_device
*bdev
, sector_t sector
, int size
,
375 struct page
*page
, int rw
)
377 struct bio
*bio
= bio_alloc(GFP_NOIO
, 1);
378 struct completion event
;
381 rw
|= (1 << BIO_RW_SYNC
);
384 bio
->bi_sector
= sector
;
385 bio_add_page(bio
, page
, size
, 0);
386 init_completion(&event
);
387 bio
->bi_private
= &event
;
388 bio
->bi_end_io
= bi_complete
;
390 wait_for_completion(&event
);
392 ret
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
397 static int read_disk_sb(mdk_rdev_t
* rdev
, int size
)
399 char b
[BDEVNAME_SIZE
];
400 if (!rdev
->sb_page
) {
408 if (!sync_page_io(rdev
->bdev
, rdev
->sb_offset
<<1, size
, rdev
->sb_page
, READ
))
414 printk(KERN_WARNING
"md: disabled device %s, could not read superblock.\n",
415 bdevname(rdev
->bdev
,b
));
419 static int uuid_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
421 if ( (sb1
->set_uuid0
== sb2
->set_uuid0
) &&
422 (sb1
->set_uuid1
== sb2
->set_uuid1
) &&
423 (sb1
->set_uuid2
== sb2
->set_uuid2
) &&
424 (sb1
->set_uuid3
== sb2
->set_uuid3
))
432 static int sb_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
435 mdp_super_t
*tmp1
, *tmp2
;
437 tmp1
= kmalloc(sizeof(*tmp1
),GFP_KERNEL
);
438 tmp2
= kmalloc(sizeof(*tmp2
),GFP_KERNEL
);
440 if (!tmp1
|| !tmp2
) {
442 printk(KERN_INFO
"md.c: sb1 is not equal to sb2!\n");
450 * nr_disks is not constant
455 if (memcmp(tmp1
, tmp2
, MD_SB_GENERIC_CONSTANT_WORDS
* 4))
466 static unsigned int calc_sb_csum(mdp_super_t
* sb
)
468 unsigned int disk_csum
, csum
;
470 disk_csum
= sb
->sb_csum
;
472 csum
= csum_partial((void *)sb
, MD_SB_BYTES
, 0);
473 sb
->sb_csum
= disk_csum
;
479 * Handle superblock details.
480 * We want to be able to handle multiple superblock formats
481 * so we have a common interface to them all, and an array of
482 * different handlers.
483 * We rely on user-space to write the initial superblock, and support
484 * reading and updating of superblocks.
485 * Interface methods are:
486 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
487 * loads and validates a superblock on dev.
488 * if refdev != NULL, compare superblocks on both devices
490 * 0 - dev has a superblock that is compatible with refdev
491 * 1 - dev has a superblock that is compatible and newer than refdev
492 * so dev should be used as the refdev in future
493 * -EINVAL superblock incompatible or invalid
494 * -othererror e.g. -EIO
496 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
497 * Verify that dev is acceptable into mddev.
498 * The first time, mddev->raid_disks will be 0, and data from
499 * dev should be merged in. Subsequent calls check that dev
500 * is new enough. Return 0 or -EINVAL
502 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
503 * Update the superblock for rdev with data in mddev
504 * This does not write to disc.
510 struct module
*owner
;
511 int (*load_super
)(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
);
512 int (*validate_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
513 void (*sync_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
517 * load_super for 0.90.0
519 static int super_90_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
521 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
527 * Calculate the position of the superblock,
528 * it's at the end of the disk.
530 * It also happens to be a multiple of 4Kb.
532 sb_offset
= calc_dev_sboffset(rdev
->bdev
);
533 rdev
->sb_offset
= sb_offset
;
535 ret
= read_disk_sb(rdev
, MD_SB_BYTES
);
540 bdevname(rdev
->bdev
, b
);
541 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
543 if (sb
->md_magic
!= MD_SB_MAGIC
) {
544 printk(KERN_ERR
"md: invalid raid superblock magic on %s\n",
549 if (sb
->major_version
!= 0 ||
550 sb
->minor_version
!= 90) {
551 printk(KERN_WARNING
"Bad version number %d.%d on %s\n",
552 sb
->major_version
, sb
->minor_version
,
557 if (sb
->raid_disks
<= 0)
560 if (csum_fold(calc_sb_csum(sb
)) != csum_fold(sb
->sb_csum
)) {
561 printk(KERN_WARNING
"md: invalid superblock checksum on %s\n",
566 rdev
->preferred_minor
= sb
->md_minor
;
567 rdev
->data_offset
= 0;
568 rdev
->sb_size
= MD_SB_BYTES
;
570 if (sb
->level
== LEVEL_MULTIPATH
)
573 rdev
->desc_nr
= sb
->this_disk
.number
;
579 mdp_super_t
*refsb
= (mdp_super_t
*)page_address(refdev
->sb_page
);
580 if (!uuid_equal(refsb
, sb
)) {
581 printk(KERN_WARNING
"md: %s has different UUID to %s\n",
582 b
, bdevname(refdev
->bdev
,b2
));
585 if (!sb_equal(refsb
, sb
)) {
586 printk(KERN_WARNING
"md: %s has same UUID"
587 " but different superblock to %s\n",
588 b
, bdevname(refdev
->bdev
, b2
));
592 ev2
= md_event(refsb
);
598 rdev
->size
= calc_dev_size(rdev
, sb
->chunk_size
);
605 * validate_super for 0.90.0
607 static int super_90_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
610 mdp_super_t
*sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
612 rdev
->raid_disk
= -1;
614 if (mddev
->raid_disks
== 0) {
615 mddev
->major_version
= 0;
616 mddev
->minor_version
= sb
->minor_version
;
617 mddev
->patch_version
= sb
->patch_version
;
618 mddev
->persistent
= ! sb
->not_persistent
;
619 mddev
->chunk_size
= sb
->chunk_size
;
620 mddev
->ctime
= sb
->ctime
;
621 mddev
->utime
= sb
->utime
;
622 mddev
->level
= sb
->level
;
623 mddev
->layout
= sb
->layout
;
624 mddev
->raid_disks
= sb
->raid_disks
;
625 mddev
->size
= sb
->size
;
626 mddev
->events
= md_event(sb
);
627 mddev
->bitmap_offset
= 0;
628 mddev
->default_bitmap_offset
= MD_SB_BYTES
>> 9;
630 if (sb
->state
& (1<<MD_SB_CLEAN
))
631 mddev
->recovery_cp
= MaxSector
;
633 if (sb
->events_hi
== sb
->cp_events_hi
&&
634 sb
->events_lo
== sb
->cp_events_lo
) {
635 mddev
->recovery_cp
= sb
->recovery_cp
;
637 mddev
->recovery_cp
= 0;
640 memcpy(mddev
->uuid
+0, &sb
->set_uuid0
, 4);
641 memcpy(mddev
->uuid
+4, &sb
->set_uuid1
, 4);
642 memcpy(mddev
->uuid
+8, &sb
->set_uuid2
, 4);
643 memcpy(mddev
->uuid
+12,&sb
->set_uuid3
, 4);
645 mddev
->max_disks
= MD_SB_DISKS
;
647 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
) &&
648 mddev
->bitmap_file
== NULL
) {
649 if (mddev
->level
!= 1 && mddev
->level
!= 5 && mddev
->level
!= 6) {
650 /* FIXME use a better test */
651 printk(KERN_WARNING
"md: bitmaps only support for raid1\n");
654 mddev
->bitmap_offset
= mddev
->default_bitmap_offset
;
657 } else if (mddev
->pers
== NULL
) {
658 /* Insist on good event counter while assembling */
659 __u64 ev1
= md_event(sb
);
661 if (ev1
< mddev
->events
)
663 } else if (mddev
->bitmap
) {
664 /* if adding to array with a bitmap, then we can accept an
665 * older device ... but not too old.
667 __u64 ev1
= md_event(sb
);
668 if (ev1
< mddev
->bitmap
->events_cleared
)
670 } else /* just a hot-add of a new device, leave raid_disk at -1 */
673 if (mddev
->level
!= LEVEL_MULTIPATH
) {
676 desc
= sb
->disks
+ rdev
->desc_nr
;
678 if (desc
->state
& (1<<MD_DISK_FAULTY
))
680 else if (desc
->state
& (1<<MD_DISK_SYNC
) &&
681 desc
->raid_disk
< mddev
->raid_disks
) {
683 rdev
->raid_disk
= desc
->raid_disk
;
685 if (desc
->state
& (1<<MD_DISK_WRITEMOSTLY
))
686 set_bit(WriteMostly
, &rdev
->flags
);
687 } else /* MULTIPATH are always insync */
693 * sync_super for 0.90.0
695 static void super_90_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
698 struct list_head
*tmp
;
700 int next_spare
= mddev
->raid_disks
;
703 /* make rdev->sb match mddev data..
706 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
707 * 3/ any empty disks < next_spare become removed
709 * disks[0] gets initialised to REMOVED because
710 * we cannot be sure from other fields if it has
711 * been initialised or not.
714 int active
=0, working
=0,failed
=0,spare
=0,nr_disks
=0;
715 unsigned int fixdesc
=0;
717 rdev
->sb_size
= MD_SB_BYTES
;
719 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
721 memset(sb
, 0, sizeof(*sb
));
723 sb
->md_magic
= MD_SB_MAGIC
;
724 sb
->major_version
= mddev
->major_version
;
725 sb
->minor_version
= mddev
->minor_version
;
726 sb
->patch_version
= mddev
->patch_version
;
727 sb
->gvalid_words
= 0; /* ignored */
728 memcpy(&sb
->set_uuid0
, mddev
->uuid
+0, 4);
729 memcpy(&sb
->set_uuid1
, mddev
->uuid
+4, 4);
730 memcpy(&sb
->set_uuid2
, mddev
->uuid
+8, 4);
731 memcpy(&sb
->set_uuid3
, mddev
->uuid
+12,4);
733 sb
->ctime
= mddev
->ctime
;
734 sb
->level
= mddev
->level
;
735 sb
->size
= mddev
->size
;
736 sb
->raid_disks
= mddev
->raid_disks
;
737 sb
->md_minor
= mddev
->md_minor
;
738 sb
->not_persistent
= !mddev
->persistent
;
739 sb
->utime
= mddev
->utime
;
741 sb
->events_hi
= (mddev
->events
>>32);
742 sb
->events_lo
= (u32
)mddev
->events
;
746 sb
->recovery_cp
= mddev
->recovery_cp
;
747 sb
->cp_events_hi
= (mddev
->events
>>32);
748 sb
->cp_events_lo
= (u32
)mddev
->events
;
749 if (mddev
->recovery_cp
== MaxSector
)
750 sb
->state
= (1<< MD_SB_CLEAN
);
754 sb
->layout
= mddev
->layout
;
755 sb
->chunk_size
= mddev
->chunk_size
;
757 if (mddev
->bitmap
&& mddev
->bitmap_file
== NULL
)
758 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
760 sb
->disks
[0].state
= (1<<MD_DISK_REMOVED
);
761 ITERATE_RDEV(mddev
,rdev2
,tmp
) {
764 if (rdev2
->raid_disk
>= 0 && rdev2
->in_sync
&& !rdev2
->faulty
)
765 desc_nr
= rdev2
->raid_disk
;
767 desc_nr
= next_spare
++;
768 if (desc_nr
!= rdev2
->desc_nr
) {
769 fixdesc
|= (1 << desc_nr
);
770 rdev2
->desc_nr
= desc_nr
;
771 if (rdev2
->raid_disk
>= 0) {
772 sprintf(nm
, "rd%d", rdev2
->raid_disk
);
773 sysfs_remove_link(&mddev
->kobj
, nm
);
775 sysfs_remove_link(&rdev2
->kobj
, "block");
776 kobject_del(&rdev2
->kobj
);
778 d
= &sb
->disks
[rdev2
->desc_nr
];
780 d
->number
= rdev2
->desc_nr
;
781 d
->major
= MAJOR(rdev2
->bdev
->bd_dev
);
782 d
->minor
= MINOR(rdev2
->bdev
->bd_dev
);
783 if (rdev2
->raid_disk
>= 0 && rdev2
->in_sync
&& !rdev2
->faulty
)
784 d
->raid_disk
= rdev2
->raid_disk
;
786 d
->raid_disk
= rdev2
->desc_nr
; /* compatibility */
788 d
->state
= (1<<MD_DISK_FAULTY
);
790 } else if (rdev2
->in_sync
) {
791 d
->state
= (1<<MD_DISK_ACTIVE
);
792 d
->state
|= (1<<MD_DISK_SYNC
);
800 if (test_bit(WriteMostly
, &rdev2
->flags
))
801 d
->state
|= (1<<MD_DISK_WRITEMOSTLY
);
804 ITERATE_RDEV(mddev
,rdev2
,tmp
)
805 if (fixdesc
& (1<<rdev2
->desc_nr
)) {
806 snprintf(rdev2
->kobj
.name
, KOBJ_NAME_LEN
, "dev%d",
808 /* kobject_add gets a ref on the parent, so
809 * we have to drop the one we already have
811 kobject_add(&rdev2
->kobj
);
812 kobject_put(rdev
->kobj
.parent
);
813 sysfs_create_link(&rdev2
->kobj
,
814 &rdev2
->bdev
->bd_disk
->kobj
,
816 if (rdev2
->raid_disk
>= 0) {
817 sprintf(nm
, "rd%d", rdev2
->raid_disk
);
818 sysfs_create_link(&mddev
->kobj
,
822 /* now set the "removed" and "faulty" bits on any missing devices */
823 for (i
=0 ; i
< mddev
->raid_disks
; i
++) {
824 mdp_disk_t
*d
= &sb
->disks
[i
];
825 if (d
->state
== 0 && d
->number
== 0) {
828 d
->state
= (1<<MD_DISK_REMOVED
);
829 d
->state
|= (1<<MD_DISK_FAULTY
);
833 sb
->nr_disks
= nr_disks
;
834 sb
->active_disks
= active
;
835 sb
->working_disks
= working
;
836 sb
->failed_disks
= failed
;
837 sb
->spare_disks
= spare
;
839 sb
->this_disk
= sb
->disks
[rdev
->desc_nr
];
840 sb
->sb_csum
= calc_sb_csum(sb
);
844 * version 1 superblock
847 static unsigned int calc_sb_1_csum(struct mdp_superblock_1
* sb
)
849 unsigned int disk_csum
, csum
;
850 unsigned long long newcsum
;
851 int size
= 256 + le32_to_cpu(sb
->max_dev
)*2;
852 unsigned int *isuper
= (unsigned int*)sb
;
855 disk_csum
= sb
->sb_csum
;
858 for (i
=0; size
>=4; size
-= 4 )
859 newcsum
+= le32_to_cpu(*isuper
++);
862 newcsum
+= le16_to_cpu(*(unsigned short*) isuper
);
864 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
865 sb
->sb_csum
= disk_csum
;
866 return cpu_to_le32(csum
);
869 static int super_1_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
871 struct mdp_superblock_1
*sb
;
874 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
878 * Calculate the position of the superblock.
879 * It is always aligned to a 4K boundary and
880 * depeding on minor_version, it can be:
881 * 0: At least 8K, but less than 12K, from end of device
882 * 1: At start of device
883 * 2: 4K from start of device.
885 switch(minor_version
) {
887 sb_offset
= rdev
->bdev
->bd_inode
->i_size
>> 9;
889 sb_offset
&= ~(sector_t
)(4*2-1);
890 /* convert from sectors to K */
902 rdev
->sb_offset
= sb_offset
;
904 /* superblock is rarely larger than 1K, but it can be larger,
905 * and it is safe to read 4k, so we do that
907 ret
= read_disk_sb(rdev
, 4096);
911 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
913 if (sb
->magic
!= cpu_to_le32(MD_SB_MAGIC
) ||
914 sb
->major_version
!= cpu_to_le32(1) ||
915 le32_to_cpu(sb
->max_dev
) > (4096-256)/2 ||
916 le64_to_cpu(sb
->super_offset
) != (rdev
->sb_offset
<<1) ||
917 (le32_to_cpu(sb
->feature_map
) & ~MD_FEATURE_ALL
) != 0)
920 if (calc_sb_1_csum(sb
) != sb
->sb_csum
) {
921 printk("md: invalid superblock checksum on %s\n",
922 bdevname(rdev
->bdev
,b
));
925 if (le64_to_cpu(sb
->data_size
) < 10) {
926 printk("md: data_size too small on %s\n",
927 bdevname(rdev
->bdev
,b
));
930 rdev
->preferred_minor
= 0xffff;
931 rdev
->data_offset
= le64_to_cpu(sb
->data_offset
);
933 rdev
->sb_size
= le32_to_cpu(sb
->max_dev
) * 2 + 256;
934 bmask
= queue_hardsect_size(rdev
->bdev
->bd_disk
->queue
)-1;
935 if (rdev
->sb_size
& bmask
)
936 rdev
-> sb_size
= (rdev
->sb_size
| bmask
)+1;
942 struct mdp_superblock_1
*refsb
=
943 (struct mdp_superblock_1
*)page_address(refdev
->sb_page
);
945 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16) != 0 ||
946 sb
->level
!= refsb
->level
||
947 sb
->layout
!= refsb
->layout
||
948 sb
->chunksize
!= refsb
->chunksize
) {
949 printk(KERN_WARNING
"md: %s has strangely different"
950 " superblock to %s\n",
951 bdevname(rdev
->bdev
,b
),
952 bdevname(refdev
->bdev
,b2
));
955 ev1
= le64_to_cpu(sb
->events
);
956 ev2
= le64_to_cpu(refsb
->events
);
962 rdev
->size
= ((rdev
->bdev
->bd_inode
->i_size
>>9) - le64_to_cpu(sb
->data_offset
)) / 2;
964 rdev
->size
= rdev
->sb_offset
;
965 if (rdev
->size
< le64_to_cpu(sb
->data_size
)/2)
967 rdev
->size
= le64_to_cpu(sb
->data_size
)/2;
968 if (le32_to_cpu(sb
->chunksize
))
969 rdev
->size
&= ~((sector_t
)le32_to_cpu(sb
->chunksize
)/2 - 1);
973 static int super_1_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
975 struct mdp_superblock_1
*sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
977 rdev
->raid_disk
= -1;
979 if (mddev
->raid_disks
== 0) {
980 mddev
->major_version
= 1;
981 mddev
->patch_version
= 0;
982 mddev
->persistent
= 1;
983 mddev
->chunk_size
= le32_to_cpu(sb
->chunksize
) << 9;
984 mddev
->ctime
= le64_to_cpu(sb
->ctime
) & ((1ULL << 32)-1);
985 mddev
->utime
= le64_to_cpu(sb
->utime
) & ((1ULL << 32)-1);
986 mddev
->level
= le32_to_cpu(sb
->level
);
987 mddev
->layout
= le32_to_cpu(sb
->layout
);
988 mddev
->raid_disks
= le32_to_cpu(sb
->raid_disks
);
989 mddev
->size
= le64_to_cpu(sb
->size
)/2;
990 mddev
->events
= le64_to_cpu(sb
->events
);
991 mddev
->bitmap_offset
= 0;
992 mddev
->default_bitmap_offset
= 0;
993 mddev
->default_bitmap_offset
= 1024;
995 mddev
->recovery_cp
= le64_to_cpu(sb
->resync_offset
);
996 memcpy(mddev
->uuid
, sb
->set_uuid
, 16);
998 mddev
->max_disks
= (4096-256)/2;
1000 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BITMAP_OFFSET
) &&
1001 mddev
->bitmap_file
== NULL
) {
1002 if (mddev
->level
!= 1) {
1003 printk(KERN_WARNING
"md: bitmaps only supported for raid1\n");
1006 mddev
->bitmap_offset
= (__s32
)le32_to_cpu(sb
->bitmap_offset
);
1008 } else if (mddev
->pers
== NULL
) {
1009 /* Insist of good event counter while assembling */
1010 __u64 ev1
= le64_to_cpu(sb
->events
);
1012 if (ev1
< mddev
->events
)
1014 } else if (mddev
->bitmap
) {
1015 /* If adding to array with a bitmap, then we can accept an
1016 * older device, but not too old.
1018 __u64 ev1
= le64_to_cpu(sb
->events
);
1019 if (ev1
< mddev
->bitmap
->events_cleared
)
1021 } else /* just a hot-add of a new device, leave raid_disk at -1 */
1024 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1026 rdev
->desc_nr
= le32_to_cpu(sb
->dev_number
);
1027 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
1029 case 0xffff: /* spare */
1032 case 0xfffe: /* faulty */
1038 rdev
->raid_disk
= role
;
1042 if (sb
->devflags
& WriteMostly1
)
1043 set_bit(WriteMostly
, &rdev
->flags
);
1044 } else /* MULTIPATH are always insync */
1050 static void super_1_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1052 struct mdp_superblock_1
*sb
;
1053 struct list_head
*tmp
;
1056 /* make rdev->sb match mddev and rdev data. */
1058 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1060 sb
->feature_map
= 0;
1062 memset(sb
->pad1
, 0, sizeof(sb
->pad1
));
1063 memset(sb
->pad2
, 0, sizeof(sb
->pad2
));
1064 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
1066 sb
->utime
= cpu_to_le64((__u64
)mddev
->utime
);
1067 sb
->events
= cpu_to_le64(mddev
->events
);
1069 sb
->resync_offset
= cpu_to_le64(mddev
->recovery_cp
);
1071 sb
->resync_offset
= cpu_to_le64(0);
1073 if (mddev
->bitmap
&& mddev
->bitmap_file
== NULL
) {
1074 sb
->bitmap_offset
= cpu_to_le32((__u32
)mddev
->bitmap_offset
);
1075 sb
->feature_map
= cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
);
1079 ITERATE_RDEV(mddev
,rdev2
,tmp
)
1080 if (rdev2
->desc_nr
+1 > max_dev
)
1081 max_dev
= rdev2
->desc_nr
+1;
1083 sb
->max_dev
= cpu_to_le32(max_dev
);
1084 for (i
=0; i
<max_dev
;i
++)
1085 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1087 ITERATE_RDEV(mddev
,rdev2
,tmp
) {
1090 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1091 else if (rdev2
->in_sync
)
1092 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1094 sb
->dev_roles
[i
] = cpu_to_le16(0xffff);
1097 sb
->recovery_offset
= cpu_to_le64(0); /* not supported yet */
1098 sb
->sb_csum
= calc_sb_1_csum(sb
);
1102 static struct super_type super_types
[] = {
1105 .owner
= THIS_MODULE
,
1106 .load_super
= super_90_load
,
1107 .validate_super
= super_90_validate
,
1108 .sync_super
= super_90_sync
,
1112 .owner
= THIS_MODULE
,
1113 .load_super
= super_1_load
,
1114 .validate_super
= super_1_validate
,
1115 .sync_super
= super_1_sync
,
1119 static mdk_rdev_t
* match_dev_unit(mddev_t
*mddev
, mdk_rdev_t
*dev
)
1121 struct list_head
*tmp
;
1124 ITERATE_RDEV(mddev
,rdev
,tmp
)
1125 if (rdev
->bdev
->bd_contains
== dev
->bdev
->bd_contains
)
1131 static int match_mddev_units(mddev_t
*mddev1
, mddev_t
*mddev2
)
1133 struct list_head
*tmp
;
1136 ITERATE_RDEV(mddev1
,rdev
,tmp
)
1137 if (match_dev_unit(mddev2
, rdev
))
1143 static LIST_HEAD(pending_raid_disks
);
1145 static int bind_rdev_to_array(mdk_rdev_t
* rdev
, mddev_t
* mddev
)
1147 mdk_rdev_t
*same_pdev
;
1148 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1154 same_pdev
= match_dev_unit(mddev
, rdev
);
1157 "%s: WARNING: %s appears to be on the same physical"
1158 " disk as %s. True\n protection against single-disk"
1159 " failure might be compromised.\n",
1160 mdname(mddev
), bdevname(rdev
->bdev
,b
),
1161 bdevname(same_pdev
->bdev
,b2
));
1163 /* Verify rdev->desc_nr is unique.
1164 * If it is -1, assign a free number, else
1165 * check number is not in use
1167 if (rdev
->desc_nr
< 0) {
1169 if (mddev
->pers
) choice
= mddev
->raid_disks
;
1170 while (find_rdev_nr(mddev
, choice
))
1172 rdev
->desc_nr
= choice
;
1174 if (find_rdev_nr(mddev
, rdev
->desc_nr
))
1178 list_add(&rdev
->same_set
, &mddev
->disks
);
1179 rdev
->mddev
= mddev
;
1180 printk(KERN_INFO
"md: bind<%s>\n", bdevname(rdev
->bdev
,b
));
1182 rdev
->kobj
.k_name
= NULL
;
1183 snprintf(rdev
->kobj
.name
, KOBJ_NAME_LEN
, "dev%d", rdev
->desc_nr
);
1184 rdev
->kobj
.parent
= &mddev
->kobj
;
1185 kobject_add(&rdev
->kobj
);
1187 sysfs_create_link(&rdev
->kobj
, &rdev
->bdev
->bd_disk
->kobj
, "block");
1191 static void unbind_rdev_from_array(mdk_rdev_t
* rdev
)
1193 char b
[BDEVNAME_SIZE
];
1198 list_del_init(&rdev
->same_set
);
1199 printk(KERN_INFO
"md: unbind<%s>\n", bdevname(rdev
->bdev
,b
));
1201 sysfs_remove_link(&rdev
->kobj
, "block");
1202 kobject_del(&rdev
->kobj
);
1206 * prevent the device from being mounted, repartitioned or
1207 * otherwise reused by a RAID array (or any other kernel
1208 * subsystem), by bd_claiming the device.
1210 static int lock_rdev(mdk_rdev_t
*rdev
, dev_t dev
)
1213 struct block_device
*bdev
;
1214 char b
[BDEVNAME_SIZE
];
1216 bdev
= open_by_devnum(dev
, FMODE_READ
|FMODE_WRITE
);
1218 printk(KERN_ERR
"md: could not open %s.\n",
1219 __bdevname(dev
, b
));
1220 return PTR_ERR(bdev
);
1222 err
= bd_claim(bdev
, rdev
);
1224 printk(KERN_ERR
"md: could not bd_claim %s.\n",
1233 static void unlock_rdev(mdk_rdev_t
*rdev
)
1235 struct block_device
*bdev
= rdev
->bdev
;
1243 void md_autodetect_dev(dev_t dev
);
1245 static void export_rdev(mdk_rdev_t
* rdev
)
1247 char b
[BDEVNAME_SIZE
];
1248 printk(KERN_INFO
"md: export_rdev(%s)\n",
1249 bdevname(rdev
->bdev
,b
));
1253 list_del_init(&rdev
->same_set
);
1255 md_autodetect_dev(rdev
->bdev
->bd_dev
);
1258 kobject_put(&rdev
->kobj
);
1261 static void kick_rdev_from_array(mdk_rdev_t
* rdev
)
1263 unbind_rdev_from_array(rdev
);
1267 static void export_array(mddev_t
*mddev
)
1269 struct list_head
*tmp
;
1272 ITERATE_RDEV(mddev
,rdev
,tmp
) {
1277 kick_rdev_from_array(rdev
);
1279 if (!list_empty(&mddev
->disks
))
1281 mddev
->raid_disks
= 0;
1282 mddev
->major_version
= 0;
1285 static void print_desc(mdp_disk_t
*desc
)
1287 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc
->number
,
1288 desc
->major
,desc
->minor
,desc
->raid_disk
,desc
->state
);
1291 static void print_sb(mdp_super_t
*sb
)
1296 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1297 sb
->major_version
, sb
->minor_version
, sb
->patch_version
,
1298 sb
->set_uuid0
, sb
->set_uuid1
, sb
->set_uuid2
, sb
->set_uuid3
,
1300 printk(KERN_INFO
"md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1301 sb
->level
, sb
->size
, sb
->nr_disks
, sb
->raid_disks
,
1302 sb
->md_minor
, sb
->layout
, sb
->chunk_size
);
1303 printk(KERN_INFO
"md: UT:%08x ST:%d AD:%d WD:%d"
1304 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1305 sb
->utime
, sb
->state
, sb
->active_disks
, sb
->working_disks
,
1306 sb
->failed_disks
, sb
->spare_disks
,
1307 sb
->sb_csum
, (unsigned long)sb
->events_lo
);
1310 for (i
= 0; i
< MD_SB_DISKS
; i
++) {
1313 desc
= sb
->disks
+ i
;
1314 if (desc
->number
|| desc
->major
|| desc
->minor
||
1315 desc
->raid_disk
|| (desc
->state
&& (desc
->state
!= 4))) {
1316 printk(" D %2d: ", i
);
1320 printk(KERN_INFO
"md: THIS: ");
1321 print_desc(&sb
->this_disk
);
1325 static void print_rdev(mdk_rdev_t
*rdev
)
1327 char b
[BDEVNAME_SIZE
];
1328 printk(KERN_INFO
"md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1329 bdevname(rdev
->bdev
,b
), (unsigned long long)rdev
->size
,
1330 rdev
->faulty
, rdev
->in_sync
, rdev
->desc_nr
);
1331 if (rdev
->sb_loaded
) {
1332 printk(KERN_INFO
"md: rdev superblock:\n");
1333 print_sb((mdp_super_t
*)page_address(rdev
->sb_page
));
1335 printk(KERN_INFO
"md: no rdev superblock!\n");
1338 void md_print_devices(void)
1340 struct list_head
*tmp
, *tmp2
;
1343 char b
[BDEVNAME_SIZE
];
1346 printk("md: **********************************\n");
1347 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1348 printk("md: **********************************\n");
1349 ITERATE_MDDEV(mddev
,tmp
) {
1352 bitmap_print_sb(mddev
->bitmap
);
1354 printk("%s: ", mdname(mddev
));
1355 ITERATE_RDEV(mddev
,rdev
,tmp2
)
1356 printk("<%s>", bdevname(rdev
->bdev
,b
));
1359 ITERATE_RDEV(mddev
,rdev
,tmp2
)
1362 printk("md: **********************************\n");
1367 static void sync_sbs(mddev_t
* mddev
)
1370 struct list_head
*tmp
;
1372 ITERATE_RDEV(mddev
,rdev
,tmp
) {
1373 super_types
[mddev
->major_version
].
1374 sync_super(mddev
, rdev
);
1375 rdev
->sb_loaded
= 1;
1379 static void md_update_sb(mddev_t
* mddev
)
1382 struct list_head
*tmp
;
1387 spin_lock(&mddev
->write_lock
);
1388 sync_req
= mddev
->in_sync
;
1389 mddev
->utime
= get_seconds();
1392 if (!mddev
->events
) {
1394 * oops, this 64-bit counter should never wrap.
1395 * Either we are in around ~1 trillion A.C., assuming
1396 * 1 reboot per second, or we have a bug:
1401 mddev
->sb_dirty
= 2;
1405 * do not write anything to disk if using
1406 * nonpersistent superblocks
1408 if (!mddev
->persistent
) {
1409 mddev
->sb_dirty
= 0;
1410 spin_unlock(&mddev
->write_lock
);
1411 wake_up(&mddev
->sb_wait
);
1414 spin_unlock(&mddev
->write_lock
);
1417 "md: updating %s RAID superblock on device (in sync %d)\n",
1418 mdname(mddev
),mddev
->in_sync
);
1420 err
= bitmap_update_sb(mddev
->bitmap
);
1421 ITERATE_RDEV(mddev
,rdev
,tmp
) {
1422 char b
[BDEVNAME_SIZE
];
1423 dprintk(KERN_INFO
"md: ");
1425 dprintk("(skipping faulty ");
1427 dprintk("%s ", bdevname(rdev
->bdev
,b
));
1428 if (!rdev
->faulty
) {
1429 md_super_write(mddev
,rdev
,
1430 rdev
->sb_offset
<<1, rdev
->sb_size
,
1432 dprintk(KERN_INFO
"(write) %s's sb offset: %llu\n",
1433 bdevname(rdev
->bdev
,b
),
1434 (unsigned long long)rdev
->sb_offset
);
1438 if (mddev
->level
== LEVEL_MULTIPATH
)
1439 /* only need to write one superblock... */
1442 wait_event(mddev
->sb_wait
, atomic_read(&mddev
->pending_writes
)==0);
1443 /* if there was a failure, sb_dirty was set to 1, and we re-write super */
1445 spin_lock(&mddev
->write_lock
);
1446 if (mddev
->in_sync
!= sync_req
|| mddev
->sb_dirty
== 1) {
1447 /* have to write it out again */
1448 spin_unlock(&mddev
->write_lock
);
1451 mddev
->sb_dirty
= 0;
1452 spin_unlock(&mddev
->write_lock
);
1453 wake_up(&mddev
->sb_wait
);
1457 struct rdev_sysfs_entry
{
1458 struct attribute attr
;
1459 ssize_t (*show
)(mdk_rdev_t
*, char *);
1460 ssize_t (*store
)(mdk_rdev_t
*, const char *, size_t);
1464 rdev_show_state(mdk_rdev_t
*rdev
, char *page
)
1470 len
+= sprintf(page
+len
, "%sfaulty",sep
);
1473 if (rdev
->in_sync
) {
1474 len
+= sprintf(page
+len
, "%sin_sync",sep
);
1477 if (!rdev
->faulty
&& !rdev
->in_sync
) {
1478 len
+= sprintf(page
+len
, "%sspare", sep
);
1481 return len
+sprintf(page
+len
, "\n");
1484 static struct rdev_sysfs_entry rdev_state
= {
1485 .attr
= {.name
= "state", .mode
= S_IRUGO
},
1486 .show
= rdev_show_state
,
1490 rdev_show_super(mdk_rdev_t
*rdev
, char *page
)
1492 if (rdev
->sb_loaded
&& rdev
->sb_size
) {
1493 memcpy(page
, page_address(rdev
->sb_page
), rdev
->sb_size
);
1494 return rdev
->sb_size
;
1498 static struct rdev_sysfs_entry rdev_super
= {
1499 .attr
= {.name
= "super", .mode
= S_IRUGO
},
1500 .show
= rdev_show_super
,
1502 static struct attribute
*rdev_default_attrs
[] = {
1508 rdev_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
1510 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
1511 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
1515 return entry
->show(rdev
, page
);
1519 rdev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
1520 const char *page
, size_t length
)
1522 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
1523 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
1527 return entry
->store(rdev
, page
, length
);
1530 static void rdev_free(struct kobject
*ko
)
1532 mdk_rdev_t
*rdev
= container_of(ko
, mdk_rdev_t
, kobj
);
1535 static struct sysfs_ops rdev_sysfs_ops
= {
1536 .show
= rdev_attr_show
,
1537 .store
= rdev_attr_store
,
1539 static struct kobj_type rdev_ktype
= {
1540 .release
= rdev_free
,
1541 .sysfs_ops
= &rdev_sysfs_ops
,
1542 .default_attrs
= rdev_default_attrs
,
1546 * Import a device. If 'super_format' >= 0, then sanity check the superblock
1548 * mark the device faulty if:
1550 * - the device is nonexistent (zero size)
1551 * - the device has no valid superblock
1553 * a faulty rdev _never_ has rdev->sb set.
1555 static mdk_rdev_t
*md_import_device(dev_t newdev
, int super_format
, int super_minor
)
1557 char b
[BDEVNAME_SIZE
];
1562 rdev
= (mdk_rdev_t
*) kmalloc(sizeof(*rdev
), GFP_KERNEL
);
1564 printk(KERN_ERR
"md: could not alloc mem for new device!\n");
1565 return ERR_PTR(-ENOMEM
);
1567 memset(rdev
, 0, sizeof(*rdev
));
1569 if ((err
= alloc_disk_sb(rdev
)))
1572 err
= lock_rdev(rdev
, newdev
);
1576 rdev
->kobj
.parent
= NULL
;
1577 rdev
->kobj
.ktype
= &rdev_ktype
;
1578 kobject_init(&rdev
->kobj
);
1583 rdev
->data_offset
= 0;
1584 atomic_set(&rdev
->nr_pending
, 0);
1585 atomic_set(&rdev
->read_errors
, 0);
1587 size
= rdev
->bdev
->bd_inode
->i_size
>> BLOCK_SIZE_BITS
;
1590 "md: %s has zero or unknown size, marking faulty!\n",
1591 bdevname(rdev
->bdev
,b
));
1596 if (super_format
>= 0) {
1597 err
= super_types
[super_format
].
1598 load_super(rdev
, NULL
, super_minor
);
1599 if (err
== -EINVAL
) {
1601 "md: %s has invalid sb, not importing!\n",
1602 bdevname(rdev
->bdev
,b
));
1607 "md: could not read %s's sb, not importing!\n",
1608 bdevname(rdev
->bdev
,b
));
1612 INIT_LIST_HEAD(&rdev
->same_set
);
1617 if (rdev
->sb_page
) {
1623 return ERR_PTR(err
);
1627 * Check a full RAID array for plausibility
1631 static void analyze_sbs(mddev_t
* mddev
)
1634 struct list_head
*tmp
;
1635 mdk_rdev_t
*rdev
, *freshest
;
1636 char b
[BDEVNAME_SIZE
];
1639 ITERATE_RDEV(mddev
,rdev
,tmp
)
1640 switch (super_types
[mddev
->major_version
].
1641 load_super(rdev
, freshest
, mddev
->minor_version
)) {
1649 "md: fatal superblock inconsistency in %s"
1650 " -- removing from array\n",
1651 bdevname(rdev
->bdev
,b
));
1652 kick_rdev_from_array(rdev
);
1656 super_types
[mddev
->major_version
].
1657 validate_super(mddev
, freshest
);
1660 ITERATE_RDEV(mddev
,rdev
,tmp
) {
1661 if (rdev
!= freshest
)
1662 if (super_types
[mddev
->major_version
].
1663 validate_super(mddev
, rdev
)) {
1664 printk(KERN_WARNING
"md: kicking non-fresh %s"
1666 bdevname(rdev
->bdev
,b
));
1667 kick_rdev_from_array(rdev
);
1670 if (mddev
->level
== LEVEL_MULTIPATH
) {
1671 rdev
->desc_nr
= i
++;
1672 rdev
->raid_disk
= rdev
->desc_nr
;
1679 if (mddev
->recovery_cp
!= MaxSector
&&
1681 printk(KERN_ERR
"md: %s: raid array is not clean"
1682 " -- starting background reconstruction\n",
1688 md_show_level(mddev_t
*mddev
, char *page
)
1690 mdk_personality_t
*p
= mddev
->pers
;
1693 if (mddev
->level
>= 0)
1694 return sprintf(page
, "RAID-%d\n", mddev
->level
);
1696 return sprintf(page
, "%s\n", p
->name
);
1699 static struct md_sysfs_entry md_level
= {
1700 .attr
= {.name
= "level", .mode
= S_IRUGO
},
1701 .show
= md_show_level
,
1705 md_show_rdisks(mddev_t
*mddev
, char *page
)
1707 return sprintf(page
, "%d\n", mddev
->raid_disks
);
1710 static struct md_sysfs_entry md_raid_disks
= {
1711 .attr
= {.name
= "raid_disks", .mode
= S_IRUGO
},
1712 .show
= md_show_rdisks
,
1716 md_show_scan(mddev_t
*mddev
, char *page
)
1718 char *type
= "none";
1719 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
1720 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
)) {
1721 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
1722 if (!test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
1724 else if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
1731 return sprintf(page
, "%s\n", type
);
1735 md_store_scan(mddev_t
*mddev
, const char *page
, size_t len
)
1739 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
1740 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
1742 down(&mddev
->reconfig_sem
);
1743 if (mddev
->pers
&& mddev
->pers
->sync_request
)
1745 up(&mddev
->reconfig_sem
);
1749 if (strcmp(page
, "check")==0 || strcmp(page
, "check\n")==0)
1750 set_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
1751 else if (strcmp(page
, "repair")!=0 && strcmp(page
, "repair\n")!=0)
1753 set_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
1754 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
1755 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
1756 md_wakeup_thread(mddev
->thread
);
1761 md_show_mismatch(mddev_t
*mddev
, char *page
)
1763 return sprintf(page
, "%llu\n",
1764 (unsigned long long) mddev
->resync_mismatches
);
1767 static struct md_sysfs_entry md_scan_mode
= {
1768 .attr
= {.name
= "scan_mode", .mode
= S_IRUGO
|S_IWUSR
},
1769 .show
= md_show_scan
,
1770 .store
= md_store_scan
,
1773 static struct md_sysfs_entry md_mismatches
= {
1774 .attr
= {.name
= "mismatch_cnt", .mode
= S_IRUGO
},
1775 .show
= md_show_mismatch
,
1778 static struct attribute
*md_default_attrs
[] = {
1780 &md_raid_disks
.attr
,
1782 &md_mismatches
.attr
,
1787 md_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
1789 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
1790 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
1794 return entry
->show(mddev
, page
);
1798 md_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
1799 const char *page
, size_t length
)
1801 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
1802 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
1806 return entry
->store(mddev
, page
, length
);
1809 static void md_free(struct kobject
*ko
)
1811 mddev_t
*mddev
= container_of(ko
, mddev_t
, kobj
);
1815 static struct sysfs_ops md_sysfs_ops
= {
1816 .show
= md_attr_show
,
1817 .store
= md_attr_store
,
1819 static struct kobj_type md_ktype
= {
1821 .sysfs_ops
= &md_sysfs_ops
,
1822 .default_attrs
= md_default_attrs
,
1827 static struct kobject
*md_probe(dev_t dev
, int *part
, void *data
)
1829 static DECLARE_MUTEX(disks_sem
);
1830 mddev_t
*mddev
= mddev_find(dev
);
1831 struct gendisk
*disk
;
1832 int partitioned
= (MAJOR(dev
) != MD_MAJOR
);
1833 int shift
= partitioned
? MdpMinorShift
: 0;
1834 int unit
= MINOR(dev
) >> shift
;
1840 if (mddev
->gendisk
) {
1845 disk
= alloc_disk(1 << shift
);
1851 disk
->major
= MAJOR(dev
);
1852 disk
->first_minor
= unit
<< shift
;
1854 sprintf(disk
->disk_name
, "md_d%d", unit
);
1855 sprintf(disk
->devfs_name
, "md/d%d", unit
);
1857 sprintf(disk
->disk_name
, "md%d", unit
);
1858 sprintf(disk
->devfs_name
, "md/%d", unit
);
1860 disk
->fops
= &md_fops
;
1861 disk
->private_data
= mddev
;
1862 disk
->queue
= mddev
->queue
;
1864 mddev
->gendisk
= disk
;
1866 mddev
->kobj
.parent
= &disk
->kobj
;
1867 mddev
->kobj
.k_name
= NULL
;
1868 snprintf(mddev
->kobj
.name
, KOBJ_NAME_LEN
, "%s", "md");
1869 mddev
->kobj
.ktype
= &md_ktype
;
1870 kobject_register(&mddev
->kobj
);
1874 void md_wakeup_thread(mdk_thread_t
*thread
);
1876 static void md_safemode_timeout(unsigned long data
)
1878 mddev_t
*mddev
= (mddev_t
*) data
;
1880 mddev
->safemode
= 1;
1881 md_wakeup_thread(mddev
->thread
);
1885 static int do_md_run(mddev_t
* mddev
)
1889 struct list_head
*tmp
;
1891 struct gendisk
*disk
;
1892 char b
[BDEVNAME_SIZE
];
1894 if (list_empty(&mddev
->disks
))
1895 /* cannot run an array with no devices.. */
1902 * Analyze all RAID superblock(s)
1904 if (!mddev
->raid_disks
)
1907 chunk_size
= mddev
->chunk_size
;
1908 pnum
= level_to_pers(mddev
->level
);
1910 if ((pnum
!= MULTIPATH
) && (pnum
!= RAID1
)) {
1913 * 'default chunksize' in the old md code used to
1914 * be PAGE_SIZE, baaad.
1915 * we abort here to be on the safe side. We don't
1916 * want to continue the bad practice.
1919 "no chunksize specified, see 'man raidtab'\n");
1922 if (chunk_size
> MAX_CHUNK_SIZE
) {
1923 printk(KERN_ERR
"too big chunk_size: %d > %d\n",
1924 chunk_size
, MAX_CHUNK_SIZE
);
1928 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
1930 if ( (1 << ffz(~chunk_size
)) != chunk_size
) {
1931 printk(KERN_ERR
"chunk_size of %d not valid\n", chunk_size
);
1934 if (chunk_size
< PAGE_SIZE
) {
1935 printk(KERN_ERR
"too small chunk_size: %d < %ld\n",
1936 chunk_size
, PAGE_SIZE
);
1940 /* devices must have minimum size of one chunk */
1941 ITERATE_RDEV(mddev
,rdev
,tmp
) {
1944 if (rdev
->size
< chunk_size
/ 1024) {
1946 "md: Dev %s smaller than chunk_size:"
1948 bdevname(rdev
->bdev
,b
),
1949 (unsigned long long)rdev
->size
,
1959 request_module("md-personality-%d", pnum
);
1964 * Drop all container device buffers, from now on
1965 * the only valid external interface is through the md
1967 * Also find largest hardsector size
1969 ITERATE_RDEV(mddev
,rdev
,tmp
) {
1972 sync_blockdev(rdev
->bdev
);
1973 invalidate_bdev(rdev
->bdev
, 0);
1976 md_probe(mddev
->unit
, NULL
, NULL
);
1977 disk
= mddev
->gendisk
;
1981 spin_lock(&pers_lock
);
1982 if (!pers
[pnum
] || !try_module_get(pers
[pnum
]->owner
)) {
1983 spin_unlock(&pers_lock
);
1984 printk(KERN_WARNING
"md: personality %d is not loaded!\n",
1989 mddev
->pers
= pers
[pnum
];
1990 spin_unlock(&pers_lock
);
1992 mddev
->recovery
= 0;
1993 mddev
->resync_max_sectors
= mddev
->size
<< 1; /* may be over-ridden by personality */
1995 /* before we start the array running, initialise the bitmap */
1996 err
= bitmap_create(mddev
);
1998 printk(KERN_ERR
"%s: failed to create bitmap (%d)\n",
1999 mdname(mddev
), err
);
2001 err
= mddev
->pers
->run(mddev
);
2003 printk(KERN_ERR
"md: pers->run() failed ...\n");
2004 module_put(mddev
->pers
->owner
);
2006 bitmap_destroy(mddev
);
2009 atomic_set(&mddev
->writes_pending
,0);
2010 mddev
->safemode
= 0;
2011 mddev
->safemode_timer
.function
= md_safemode_timeout
;
2012 mddev
->safemode_timer
.data
= (unsigned long) mddev
;
2013 mddev
->safemode_delay
= (20 * HZ
)/1000 +1; /* 20 msec delay */
2016 ITERATE_RDEV(mddev
,rdev
,tmp
)
2017 if (rdev
->raid_disk
>= 0) {
2019 sprintf(nm
, "rd%d", rdev
->raid_disk
);
2020 sysfs_create_link(&mddev
->kobj
, &rdev
->kobj
, nm
);
2023 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
2024 md_wakeup_thread(mddev
->thread
);
2026 if (mddev
->sb_dirty
)
2027 md_update_sb(mddev
);
2029 set_capacity(disk
, mddev
->array_size
<<1);
2031 /* If we call blk_queue_make_request here, it will
2032 * re-initialise max_sectors etc which may have been
2033 * refined inside -> run. So just set the bits we need to set.
2034 * Most initialisation happended when we called
2035 * blk_queue_make_request(..., md_fail_request)
2038 mddev
->queue
->queuedata
= mddev
;
2039 mddev
->queue
->make_request_fn
= mddev
->pers
->make_request
;
2045 static int restart_array(mddev_t
*mddev
)
2047 struct gendisk
*disk
= mddev
->gendisk
;
2051 * Complain if it has no devices
2054 if (list_empty(&mddev
->disks
))
2062 mddev
->safemode
= 0;
2064 set_disk_ro(disk
, 0);
2066 printk(KERN_INFO
"md: %s switched to read-write mode.\n",
2069 * Kick recovery or resync if necessary
2071 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
2072 md_wakeup_thread(mddev
->thread
);
2075 printk(KERN_ERR
"md: %s has no personality assigned.\n",
2084 static int do_md_stop(mddev_t
* mddev
, int ro
)
2087 struct gendisk
*disk
= mddev
->gendisk
;
2090 if (atomic_read(&mddev
->active
)>2) {
2091 printk("md: %s still in use.\n",mdname(mddev
));
2095 if (mddev
->sync_thread
) {
2096 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
2097 md_unregister_thread(mddev
->sync_thread
);
2098 mddev
->sync_thread
= NULL
;
2101 del_timer_sync(&mddev
->safemode_timer
);
2103 invalidate_partition(disk
, 0);
2111 bitmap_flush(mddev
);
2112 wait_event(mddev
->sb_wait
, atomic_read(&mddev
->pending_writes
)==0);
2114 set_disk_ro(disk
, 0);
2115 blk_queue_make_request(mddev
->queue
, md_fail_request
);
2116 mddev
->pers
->stop(mddev
);
2117 module_put(mddev
->pers
->owner
);
2122 if (!mddev
->in_sync
) {
2123 /* mark array as shutdown cleanly */
2125 md_update_sb(mddev
);
2128 set_disk_ro(disk
, 1);
2131 bitmap_destroy(mddev
);
2132 if (mddev
->bitmap_file
) {
2133 atomic_set(&mddev
->bitmap_file
->f_dentry
->d_inode
->i_writecount
, 1);
2134 fput(mddev
->bitmap_file
);
2135 mddev
->bitmap_file
= NULL
;
2137 mddev
->bitmap_offset
= 0;
2140 * Free resources if final stop
2144 struct list_head
*tmp
;
2145 struct gendisk
*disk
;
2146 printk(KERN_INFO
"md: %s stopped.\n", mdname(mddev
));
2148 ITERATE_RDEV(mddev
,rdev
,tmp
)
2149 if (rdev
->raid_disk
>= 0) {
2151 sprintf(nm
, "rd%d", rdev
->raid_disk
);
2152 sysfs_remove_link(&mddev
->kobj
, nm
);
2155 export_array(mddev
);
2157 mddev
->array_size
= 0;
2158 disk
= mddev
->gendisk
;
2160 set_capacity(disk
, 0);
2163 printk(KERN_INFO
"md: %s switched to read-only mode.\n",
2170 static void autorun_array(mddev_t
*mddev
)
2173 struct list_head
*tmp
;
2176 if (list_empty(&mddev
->disks
))
2179 printk(KERN_INFO
"md: running: ");
2181 ITERATE_RDEV(mddev
,rdev
,tmp
) {
2182 char b
[BDEVNAME_SIZE
];
2183 printk("<%s>", bdevname(rdev
->bdev
,b
));
2187 err
= do_md_run (mddev
);
2189 printk(KERN_WARNING
"md: do_md_run() returned %d\n", err
);
2190 do_md_stop (mddev
, 0);
2195 * lets try to run arrays based on all disks that have arrived
2196 * until now. (those are in pending_raid_disks)
2198 * the method: pick the first pending disk, collect all disks with
2199 * the same UUID, remove all from the pending list and put them into
2200 * the 'same_array' list. Then order this list based on superblock
2201 * update time (freshest comes first), kick out 'old' disks and
2202 * compare superblocks. If everything's fine then run it.
2204 * If "unit" is allocated, then bump its reference count
2206 static void autorun_devices(int part
)
2208 struct list_head candidates
;
2209 struct list_head
*tmp
;
2210 mdk_rdev_t
*rdev0
, *rdev
;
2212 char b
[BDEVNAME_SIZE
];
2214 printk(KERN_INFO
"md: autorun ...\n");
2215 while (!list_empty(&pending_raid_disks
)) {
2217 rdev0
= list_entry(pending_raid_disks
.next
,
2218 mdk_rdev_t
, same_set
);
2220 printk(KERN_INFO
"md: considering %s ...\n",
2221 bdevname(rdev0
->bdev
,b
));
2222 INIT_LIST_HEAD(&candidates
);
2223 ITERATE_RDEV_PENDING(rdev
,tmp
)
2224 if (super_90_load(rdev
, rdev0
, 0) >= 0) {
2225 printk(KERN_INFO
"md: adding %s ...\n",
2226 bdevname(rdev
->bdev
,b
));
2227 list_move(&rdev
->same_set
, &candidates
);
2230 * now we have a set of devices, with all of them having
2231 * mostly sane superblocks. It's time to allocate the
2234 if (rdev0
->preferred_minor
< 0 || rdev0
->preferred_minor
>= MAX_MD_DEVS
) {
2235 printk(KERN_INFO
"md: unit number in %s is bad: %d\n",
2236 bdevname(rdev0
->bdev
, b
), rdev0
->preferred_minor
);
2240 dev
= MKDEV(mdp_major
,
2241 rdev0
->preferred_minor
<< MdpMinorShift
);
2243 dev
= MKDEV(MD_MAJOR
, rdev0
->preferred_minor
);
2245 md_probe(dev
, NULL
, NULL
);
2246 mddev
= mddev_find(dev
);
2249 "md: cannot allocate memory for md drive.\n");
2252 if (mddev_lock(mddev
))
2253 printk(KERN_WARNING
"md: %s locked, cannot run\n",
2255 else if (mddev
->raid_disks
|| mddev
->major_version
2256 || !list_empty(&mddev
->disks
)) {
2258 "md: %s already running, cannot run %s\n",
2259 mdname(mddev
), bdevname(rdev0
->bdev
,b
));
2260 mddev_unlock(mddev
);
2262 printk(KERN_INFO
"md: created %s\n", mdname(mddev
));
2263 ITERATE_RDEV_GENERIC(candidates
,rdev
,tmp
) {
2264 list_del_init(&rdev
->same_set
);
2265 if (bind_rdev_to_array(rdev
, mddev
))
2268 autorun_array(mddev
);
2269 mddev_unlock(mddev
);
2271 /* on success, candidates will be empty, on error
2274 ITERATE_RDEV_GENERIC(candidates
,rdev
,tmp
)
2278 printk(KERN_INFO
"md: ... autorun DONE.\n");
2282 * import RAID devices based on one partition
2283 * if possible, the array gets run as well.
2286 static int autostart_array(dev_t startdev
)
2288 char b
[BDEVNAME_SIZE
];
2289 int err
= -EINVAL
, i
;
2290 mdp_super_t
*sb
= NULL
;
2291 mdk_rdev_t
*start_rdev
= NULL
, *rdev
;
2293 start_rdev
= md_import_device(startdev
, 0, 0);
2294 if (IS_ERR(start_rdev
))
2298 /* NOTE: this can only work for 0.90.0 superblocks */
2299 sb
= (mdp_super_t
*)page_address(start_rdev
->sb_page
);
2300 if (sb
->major_version
!= 0 ||
2301 sb
->minor_version
!= 90 ) {
2302 printk(KERN_WARNING
"md: can only autostart 0.90.0 arrays\n");
2303 export_rdev(start_rdev
);
2307 if (start_rdev
->faulty
) {
2309 "md: can not autostart based on faulty %s!\n",
2310 bdevname(start_rdev
->bdev
,b
));
2311 export_rdev(start_rdev
);
2314 list_add(&start_rdev
->same_set
, &pending_raid_disks
);
2316 for (i
= 0; i
< MD_SB_DISKS
; i
++) {
2317 mdp_disk_t
*desc
= sb
->disks
+ i
;
2318 dev_t dev
= MKDEV(desc
->major
, desc
->minor
);
2322 if (dev
== startdev
)
2324 if (MAJOR(dev
) != desc
->major
|| MINOR(dev
) != desc
->minor
)
2326 rdev
= md_import_device(dev
, 0, 0);
2330 list_add(&rdev
->same_set
, &pending_raid_disks
);
2334 * possibly return codes
2342 static int get_version(void __user
* arg
)
2346 ver
.major
= MD_MAJOR_VERSION
;
2347 ver
.minor
= MD_MINOR_VERSION
;
2348 ver
.patchlevel
= MD_PATCHLEVEL_VERSION
;
2350 if (copy_to_user(arg
, &ver
, sizeof(ver
)))
2356 static int get_array_info(mddev_t
* mddev
, void __user
* arg
)
2358 mdu_array_info_t info
;
2359 int nr
,working
,active
,failed
,spare
;
2361 struct list_head
*tmp
;
2363 nr
=working
=active
=failed
=spare
=0;
2364 ITERATE_RDEV(mddev
,rdev
,tmp
) {
2377 info
.major_version
= mddev
->major_version
;
2378 info
.minor_version
= mddev
->minor_version
;
2379 info
.patch_version
= MD_PATCHLEVEL_VERSION
;
2380 info
.ctime
= mddev
->ctime
;
2381 info
.level
= mddev
->level
;
2382 info
.size
= mddev
->size
;
2384 info
.raid_disks
= mddev
->raid_disks
;
2385 info
.md_minor
= mddev
->md_minor
;
2386 info
.not_persistent
= !mddev
->persistent
;
2388 info
.utime
= mddev
->utime
;
2391 info
.state
= (1<<MD_SB_CLEAN
);
2392 if (mddev
->bitmap
&& mddev
->bitmap_offset
)
2393 info
.state
= (1<<MD_SB_BITMAP_PRESENT
);
2394 info
.active_disks
= active
;
2395 info
.working_disks
= working
;
2396 info
.failed_disks
= failed
;
2397 info
.spare_disks
= spare
;
2399 info
.layout
= mddev
->layout
;
2400 info
.chunk_size
= mddev
->chunk_size
;
2402 if (copy_to_user(arg
, &info
, sizeof(info
)))
2408 static int get_bitmap_file(mddev_t
* mddev
, void __user
* arg
)
2410 mdu_bitmap_file_t
*file
= NULL
; /* too big for stack allocation */
2411 char *ptr
, *buf
= NULL
;
2414 file
= kmalloc(sizeof(*file
), GFP_KERNEL
);
2418 /* bitmap disabled, zero the first byte and copy out */
2419 if (!mddev
->bitmap
|| !mddev
->bitmap
->file
) {
2420 file
->pathname
[0] = '\0';
2424 buf
= kmalloc(sizeof(file
->pathname
), GFP_KERNEL
);
2428 ptr
= file_path(mddev
->bitmap
->file
, buf
, sizeof(file
->pathname
));
2432 strcpy(file
->pathname
, ptr
);
2436 if (copy_to_user(arg
, file
, sizeof(*file
)))
2444 static int get_disk_info(mddev_t
* mddev
, void __user
* arg
)
2446 mdu_disk_info_t info
;
2450 if (copy_from_user(&info
, arg
, sizeof(info
)))
2455 rdev
= find_rdev_nr(mddev
, nr
);
2457 info
.major
= MAJOR(rdev
->bdev
->bd_dev
);
2458 info
.minor
= MINOR(rdev
->bdev
->bd_dev
);
2459 info
.raid_disk
= rdev
->raid_disk
;
2462 info
.state
|= (1<<MD_DISK_FAULTY
);
2463 else if (rdev
->in_sync
) {
2464 info
.state
|= (1<<MD_DISK_ACTIVE
);
2465 info
.state
|= (1<<MD_DISK_SYNC
);
2467 if (test_bit(WriteMostly
, &rdev
->flags
))
2468 info
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
2470 info
.major
= info
.minor
= 0;
2471 info
.raid_disk
= -1;
2472 info
.state
= (1<<MD_DISK_REMOVED
);
2475 if (copy_to_user(arg
, &info
, sizeof(info
)))
2481 static int add_new_disk(mddev_t
* mddev
, mdu_disk_info_t
*info
)
2483 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
2485 dev_t dev
= MKDEV(info
->major
,info
->minor
);
2487 if (info
->major
!= MAJOR(dev
) || info
->minor
!= MINOR(dev
))
2490 if (!mddev
->raid_disks
) {
2492 /* expecting a device which has a superblock */
2493 rdev
= md_import_device(dev
, mddev
->major_version
, mddev
->minor_version
);
2496 "md: md_import_device returned %ld\n",
2498 return PTR_ERR(rdev
);
2500 if (!list_empty(&mddev
->disks
)) {
2501 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
2502 mdk_rdev_t
, same_set
);
2503 int err
= super_types
[mddev
->major_version
]
2504 .load_super(rdev
, rdev0
, mddev
->minor_version
);
2507 "md: %s has different UUID to %s\n",
2508 bdevname(rdev
->bdev
,b
),
2509 bdevname(rdev0
->bdev
,b2
));
2514 err
= bind_rdev_to_array(rdev
, mddev
);
2521 * add_new_disk can be used once the array is assembled
2522 * to add "hot spares". They must already have a superblock
2527 if (!mddev
->pers
->hot_add_disk
) {
2529 "%s: personality does not support diskops!\n",
2533 if (mddev
->persistent
)
2534 rdev
= md_import_device(dev
, mddev
->major_version
,
2535 mddev
->minor_version
);
2537 rdev
= md_import_device(dev
, -1, -1);
2540 "md: md_import_device returned %ld\n",
2542 return PTR_ERR(rdev
);
2544 /* set save_raid_disk if appropriate */
2545 if (!mddev
->persistent
) {
2546 if (info
->state
& (1<<MD_DISK_SYNC
) &&
2547 info
->raid_disk
< mddev
->raid_disks
)
2548 rdev
->raid_disk
= info
->raid_disk
;
2550 rdev
->raid_disk
= -1;
2552 super_types
[mddev
->major_version
].
2553 validate_super(mddev
, rdev
);
2554 rdev
->saved_raid_disk
= rdev
->raid_disk
;
2556 rdev
->in_sync
= 0; /* just to be sure */
2557 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
2558 set_bit(WriteMostly
, &rdev
->flags
);
2560 rdev
->raid_disk
= -1;
2561 err
= bind_rdev_to_array(rdev
, mddev
);
2565 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
2566 md_wakeup_thread(mddev
->thread
);
2570 /* otherwise, add_new_disk is only allowed
2571 * for major_version==0 superblocks
2573 if (mddev
->major_version
!= 0) {
2574 printk(KERN_WARNING
"%s: ADD_NEW_DISK not supported\n",
2579 if (!(info
->state
& (1<<MD_DISK_FAULTY
))) {
2581 rdev
= md_import_device (dev
, -1, 0);
2584 "md: error, md_import_device() returned %ld\n",
2586 return PTR_ERR(rdev
);
2588 rdev
->desc_nr
= info
->number
;
2589 if (info
->raid_disk
< mddev
->raid_disks
)
2590 rdev
->raid_disk
= info
->raid_disk
;
2592 rdev
->raid_disk
= -1;
2595 if (rdev
->raid_disk
< mddev
->raid_disks
)
2596 rdev
->in_sync
= (info
->state
& (1<<MD_DISK_SYNC
));
2600 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
2601 set_bit(WriteMostly
, &rdev
->flags
);
2603 err
= bind_rdev_to_array(rdev
, mddev
);
2609 if (!mddev
->persistent
) {
2610 printk(KERN_INFO
"md: nonpersistent superblock ...\n");
2611 rdev
->sb_offset
= rdev
->bdev
->bd_inode
->i_size
>> BLOCK_SIZE_BITS
;
2613 rdev
->sb_offset
= calc_dev_sboffset(rdev
->bdev
);
2614 rdev
->size
= calc_dev_size(rdev
, mddev
->chunk_size
);
2616 if (!mddev
->size
|| (mddev
->size
> rdev
->size
))
2617 mddev
->size
= rdev
->size
;
2623 static int hot_remove_disk(mddev_t
* mddev
, dev_t dev
)
2625 char b
[BDEVNAME_SIZE
];
2631 rdev
= find_rdev(mddev
, dev
);
2635 if (rdev
->raid_disk
>= 0)
2638 kick_rdev_from_array(rdev
);
2639 md_update_sb(mddev
);
2643 printk(KERN_WARNING
"md: cannot remove active disk %s from %s ... \n",
2644 bdevname(rdev
->bdev
,b
), mdname(mddev
));
2648 static int hot_add_disk(mddev_t
* mddev
, dev_t dev
)
2650 char b
[BDEVNAME_SIZE
];
2658 if (mddev
->major_version
!= 0) {
2659 printk(KERN_WARNING
"%s: HOT_ADD may only be used with"
2660 " version-0 superblocks.\n",
2664 if (!mddev
->pers
->hot_add_disk
) {
2666 "%s: personality does not support diskops!\n",
2671 rdev
= md_import_device (dev
, -1, 0);
2674 "md: error, md_import_device() returned %ld\n",
2679 if (mddev
->persistent
)
2680 rdev
->sb_offset
= calc_dev_sboffset(rdev
->bdev
);
2683 rdev
->bdev
->bd_inode
->i_size
>> BLOCK_SIZE_BITS
;
2685 size
= calc_dev_size(rdev
, mddev
->chunk_size
);
2688 if (size
< mddev
->size
) {
2690 "%s: disk size %llu blocks < array size %llu\n",
2691 mdname(mddev
), (unsigned long long)size
,
2692 (unsigned long long)mddev
->size
);
2699 "md: can not hot-add faulty %s disk to %s!\n",
2700 bdevname(rdev
->bdev
,b
), mdname(mddev
));
2706 bind_rdev_to_array(rdev
, mddev
);
2709 * The rest should better be atomic, we can have disk failures
2710 * noticed in interrupt contexts ...
2713 if (rdev
->desc_nr
== mddev
->max_disks
) {
2714 printk(KERN_WARNING
"%s: can not hot-add to full array!\n",
2717 goto abort_unbind_export
;
2720 rdev
->raid_disk
= -1;
2722 md_update_sb(mddev
);
2725 * Kick recovery, maybe this spare has to be added to the
2726 * array immediately.
2728 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
2729 md_wakeup_thread(mddev
->thread
);
2733 abort_unbind_export
:
2734 unbind_rdev_from_array(rdev
);
2741 /* similar to deny_write_access, but accounts for our holding a reference
2742 * to the file ourselves */
2743 static int deny_bitmap_write_access(struct file
* file
)
2745 struct inode
*inode
= file
->f_mapping
->host
;
2747 spin_lock(&inode
->i_lock
);
2748 if (atomic_read(&inode
->i_writecount
) > 1) {
2749 spin_unlock(&inode
->i_lock
);
2752 atomic_set(&inode
->i_writecount
, -1);
2753 spin_unlock(&inode
->i_lock
);
2758 static int set_bitmap_file(mddev_t
*mddev
, int fd
)
2763 if (!mddev
->pers
->quiesce
)
2765 if (mddev
->recovery
|| mddev
->sync_thread
)
2767 /* we should be able to change the bitmap.. */
2773 return -EEXIST
; /* cannot add when bitmap is present */
2774 mddev
->bitmap_file
= fget(fd
);
2776 if (mddev
->bitmap_file
== NULL
) {
2777 printk(KERN_ERR
"%s: error: failed to get bitmap file\n",
2782 err
= deny_bitmap_write_access(mddev
->bitmap_file
);
2784 printk(KERN_ERR
"%s: error: bitmap file is already in use\n",
2786 fput(mddev
->bitmap_file
);
2787 mddev
->bitmap_file
= NULL
;
2790 mddev
->bitmap_offset
= 0; /* file overrides offset */
2791 } else if (mddev
->bitmap
== NULL
)
2792 return -ENOENT
; /* cannot remove what isn't there */
2795 mddev
->pers
->quiesce(mddev
, 1);
2797 err
= bitmap_create(mddev
);
2799 bitmap_destroy(mddev
);
2800 mddev
->pers
->quiesce(mddev
, 0);
2801 } else if (fd
< 0) {
2802 if (mddev
->bitmap_file
)
2803 fput(mddev
->bitmap_file
);
2804 mddev
->bitmap_file
= NULL
;
2811 * set_array_info is used two different ways
2812 * The original usage is when creating a new array.
2813 * In this usage, raid_disks is > 0 and it together with
2814 * level, size, not_persistent,layout,chunksize determine the
2815 * shape of the array.
2816 * This will always create an array with a type-0.90.0 superblock.
2817 * The newer usage is when assembling an array.
2818 * In this case raid_disks will be 0, and the major_version field is
2819 * use to determine which style super-blocks are to be found on the devices.
2820 * The minor and patch _version numbers are also kept incase the
2821 * super_block handler wishes to interpret them.
2823 static int set_array_info(mddev_t
* mddev
, mdu_array_info_t
*info
)
2826 if (info
->raid_disks
== 0) {
2827 /* just setting version number for superblock loading */
2828 if (info
->major_version
< 0 ||
2829 info
->major_version
>= sizeof(super_types
)/sizeof(super_types
[0]) ||
2830 super_types
[info
->major_version
].name
== NULL
) {
2831 /* maybe try to auto-load a module? */
2833 "md: superblock version %d not known\n",
2834 info
->major_version
);
2837 mddev
->major_version
= info
->major_version
;
2838 mddev
->minor_version
= info
->minor_version
;
2839 mddev
->patch_version
= info
->patch_version
;
2842 mddev
->major_version
= MD_MAJOR_VERSION
;
2843 mddev
->minor_version
= MD_MINOR_VERSION
;
2844 mddev
->patch_version
= MD_PATCHLEVEL_VERSION
;
2845 mddev
->ctime
= get_seconds();
2847 mddev
->level
= info
->level
;
2848 mddev
->size
= info
->size
;
2849 mddev
->raid_disks
= info
->raid_disks
;
2850 /* don't set md_minor, it is determined by which /dev/md* was
2853 if (info
->state
& (1<<MD_SB_CLEAN
))
2854 mddev
->recovery_cp
= MaxSector
;
2856 mddev
->recovery_cp
= 0;
2857 mddev
->persistent
= ! info
->not_persistent
;
2859 mddev
->layout
= info
->layout
;
2860 mddev
->chunk_size
= info
->chunk_size
;
2862 mddev
->max_disks
= MD_SB_DISKS
;
2864 mddev
->sb_dirty
= 1;
2867 * Generate a 128 bit UUID
2869 get_random_bytes(mddev
->uuid
, 16);
2875 * update_array_info is used to change the configuration of an
2877 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
2878 * fields in the info are checked against the array.
2879 * Any differences that cannot be handled will cause an error.
2880 * Normally, only one change can be managed at a time.
2882 static int update_array_info(mddev_t
*mddev
, mdu_array_info_t
*info
)
2888 /* calculate expected state,ignoring low bits */
2889 if (mddev
->bitmap
&& mddev
->bitmap_offset
)
2890 state
|= (1 << MD_SB_BITMAP_PRESENT
);
2892 if (mddev
->major_version
!= info
->major_version
||
2893 mddev
->minor_version
!= info
->minor_version
||
2894 /* mddev->patch_version != info->patch_version || */
2895 mddev
->ctime
!= info
->ctime
||
2896 mddev
->level
!= info
->level
||
2897 /* mddev->layout != info->layout || */
2898 !mddev
->persistent
!= info
->not_persistent
||
2899 mddev
->chunk_size
!= info
->chunk_size
||
2900 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
2901 ((state
^info
->state
) & 0xfffffe00)
2904 /* Check there is only one change */
2905 if (mddev
->size
!= info
->size
) cnt
++;
2906 if (mddev
->raid_disks
!= info
->raid_disks
) cnt
++;
2907 if (mddev
->layout
!= info
->layout
) cnt
++;
2908 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) cnt
++;
2909 if (cnt
== 0) return 0;
2910 if (cnt
> 1) return -EINVAL
;
2912 if (mddev
->layout
!= info
->layout
) {
2914 * we don't need to do anything at the md level, the
2915 * personality will take care of it all.
2917 if (mddev
->pers
->reconfig
== NULL
)
2920 return mddev
->pers
->reconfig(mddev
, info
->layout
, -1);
2922 if (mddev
->size
!= info
->size
) {
2924 struct list_head
*tmp
;
2925 if (mddev
->pers
->resize
== NULL
)
2927 /* The "size" is the amount of each device that is used.
2928 * This can only make sense for arrays with redundancy.
2929 * linear and raid0 always use whatever space is available
2930 * We can only consider changing the size if no resync
2931 * or reconstruction is happening, and if the new size
2932 * is acceptable. It must fit before the sb_offset or,
2933 * if that is <data_offset, it must fit before the
2934 * size of each device.
2935 * If size is zero, we find the largest size that fits.
2937 if (mddev
->sync_thread
)
2939 ITERATE_RDEV(mddev
,rdev
,tmp
) {
2941 int fit
= (info
->size
== 0);
2942 if (rdev
->sb_offset
> rdev
->data_offset
)
2943 avail
= (rdev
->sb_offset
*2) - rdev
->data_offset
;
2945 avail
= get_capacity(rdev
->bdev
->bd_disk
)
2946 - rdev
->data_offset
;
2947 if (fit
&& (info
->size
== 0 || info
->size
> avail
/2))
2948 info
->size
= avail
/2;
2949 if (avail
< ((sector_t
)info
->size
<< 1))
2952 rv
= mddev
->pers
->resize(mddev
, (sector_t
)info
->size
*2);
2954 struct block_device
*bdev
;
2956 bdev
= bdget_disk(mddev
->gendisk
, 0);
2958 down(&bdev
->bd_inode
->i_sem
);
2959 i_size_write(bdev
->bd_inode
, mddev
->array_size
<< 10);
2960 up(&bdev
->bd_inode
->i_sem
);
2965 if (mddev
->raid_disks
!= info
->raid_disks
) {
2966 /* change the number of raid disks */
2967 if (mddev
->pers
->reshape
== NULL
)
2969 if (info
->raid_disks
<= 0 ||
2970 info
->raid_disks
>= mddev
->max_disks
)
2972 if (mddev
->sync_thread
)
2974 rv
= mddev
->pers
->reshape(mddev
, info
->raid_disks
);
2976 struct block_device
*bdev
;
2978 bdev
= bdget_disk(mddev
->gendisk
, 0);
2980 down(&bdev
->bd_inode
->i_sem
);
2981 i_size_write(bdev
->bd_inode
, mddev
->array_size
<< 10);
2982 up(&bdev
->bd_inode
->i_sem
);
2987 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) {
2988 if (mddev
->pers
->quiesce
== NULL
)
2990 if (mddev
->recovery
|| mddev
->sync_thread
)
2992 if (info
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
2993 /* add the bitmap */
2996 if (mddev
->default_bitmap_offset
== 0)
2998 mddev
->bitmap_offset
= mddev
->default_bitmap_offset
;
2999 mddev
->pers
->quiesce(mddev
, 1);
3000 rv
= bitmap_create(mddev
);
3002 bitmap_destroy(mddev
);
3003 mddev
->pers
->quiesce(mddev
, 0);
3005 /* remove the bitmap */
3008 if (mddev
->bitmap
->file
)
3010 mddev
->pers
->quiesce(mddev
, 1);
3011 bitmap_destroy(mddev
);
3012 mddev
->pers
->quiesce(mddev
, 0);
3013 mddev
->bitmap_offset
= 0;
3016 md_update_sb(mddev
);
3020 static int set_disk_faulty(mddev_t
*mddev
, dev_t dev
)
3024 if (mddev
->pers
== NULL
)
3027 rdev
= find_rdev(mddev
, dev
);
3031 md_error(mddev
, rdev
);
3035 static int md_ioctl(struct inode
*inode
, struct file
*file
,
3036 unsigned int cmd
, unsigned long arg
)
3039 void __user
*argp
= (void __user
*)arg
;
3040 struct hd_geometry __user
*loc
= argp
;
3041 mddev_t
*mddev
= NULL
;
3043 if (!capable(CAP_SYS_ADMIN
))
3047 * Commands dealing with the RAID driver but not any
3053 err
= get_version(argp
);
3056 case PRINT_RAID_DEBUG
:
3064 autostart_arrays(arg
);
3071 * Commands creating/starting a new array:
3074 mddev
= inode
->i_bdev
->bd_disk
->private_data
;
3082 if (cmd
== START_ARRAY
) {
3083 /* START_ARRAY doesn't need to lock the array as autostart_array
3084 * does the locking, and it could even be a different array
3089 "md: %s(pid %d) used deprecated START_ARRAY ioctl. "
3090 "This will not be supported beyond 2.6\n",
3091 current
->comm
, current
->pid
);
3094 err
= autostart_array(new_decode_dev(arg
));
3096 printk(KERN_WARNING
"md: autostart failed!\n");
3102 err
= mddev_lock(mddev
);
3105 "md: ioctl lock interrupted, reason %d, cmd %d\n",
3112 case SET_ARRAY_INFO
:
3114 mdu_array_info_t info
;
3116 memset(&info
, 0, sizeof(info
));
3117 else if (copy_from_user(&info
, argp
, sizeof(info
))) {
3122 err
= update_array_info(mddev
, &info
);
3124 printk(KERN_WARNING
"md: couldn't update"
3125 " array info. %d\n", err
);
3130 if (!list_empty(&mddev
->disks
)) {
3132 "md: array %s already has disks!\n",
3137 if (mddev
->raid_disks
) {
3139 "md: array %s already initialised!\n",
3144 err
= set_array_info(mddev
, &info
);
3146 printk(KERN_WARNING
"md: couldn't set"
3147 " array info. %d\n", err
);
3157 * Commands querying/configuring an existing array:
3159 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
3160 * RUN_ARRAY, and SET_BITMAP_FILE are allowed */
3161 if (!mddev
->raid_disks
&& cmd
!= ADD_NEW_DISK
&& cmd
!= STOP_ARRAY
3162 && cmd
!= RUN_ARRAY
&& cmd
!= SET_BITMAP_FILE
) {
3168 * Commands even a read-only array can execute:
3172 case GET_ARRAY_INFO
:
3173 err
= get_array_info(mddev
, argp
);
3176 case GET_BITMAP_FILE
:
3177 err
= get_bitmap_file(mddev
, argp
);
3181 err
= get_disk_info(mddev
, argp
);
3184 case RESTART_ARRAY_RW
:
3185 err
= restart_array(mddev
);
3189 err
= do_md_stop (mddev
, 0);
3193 err
= do_md_stop (mddev
, 1);
3197 * We have a problem here : there is no easy way to give a CHS
3198 * virtual geometry. We currently pretend that we have a 2 heads
3199 * 4 sectors (with a BIG number of cylinders...). This drives
3200 * dosfs just mad... ;-)
3207 err
= put_user (2, (char __user
*) &loc
->heads
);
3210 err
= put_user (4, (char __user
*) &loc
->sectors
);
3213 err
= put_user(get_capacity(mddev
->gendisk
)/8,
3214 (short __user
*) &loc
->cylinders
);
3217 err
= put_user (get_start_sect(inode
->i_bdev
),
3218 (long __user
*) &loc
->start
);
3223 * The remaining ioctls are changing the state of the
3224 * superblock, so we do not allow read-only arrays
3236 mdu_disk_info_t info
;
3237 if (copy_from_user(&info
, argp
, sizeof(info
)))
3240 err
= add_new_disk(mddev
, &info
);
3244 case HOT_REMOVE_DISK
:
3245 err
= hot_remove_disk(mddev
, new_decode_dev(arg
));
3249 err
= hot_add_disk(mddev
, new_decode_dev(arg
));
3252 case SET_DISK_FAULTY
:
3253 err
= set_disk_faulty(mddev
, new_decode_dev(arg
));
3257 err
= do_md_run (mddev
);
3260 case SET_BITMAP_FILE
:
3261 err
= set_bitmap_file(mddev
, (int)arg
);
3265 if (_IOC_TYPE(cmd
) == MD_MAJOR
)
3266 printk(KERN_WARNING
"md: %s(pid %d) used"
3267 " obsolete MD ioctl, upgrade your"
3268 " software to use new ictls.\n",
3269 current
->comm
, current
->pid
);
3276 mddev_unlock(mddev
);
3286 static int md_open(struct inode
*inode
, struct file
*file
)
3289 * Succeed if we can lock the mddev, which confirms that
3290 * it isn't being stopped right now.
3292 mddev_t
*mddev
= inode
->i_bdev
->bd_disk
->private_data
;
3295 if ((err
= mddev_lock(mddev
)))
3300 mddev_unlock(mddev
);
3302 check_disk_change(inode
->i_bdev
);
3307 static int md_release(struct inode
*inode
, struct file
* file
)
3309 mddev_t
*mddev
= inode
->i_bdev
->bd_disk
->private_data
;
3318 static int md_media_changed(struct gendisk
*disk
)
3320 mddev_t
*mddev
= disk
->private_data
;
3322 return mddev
->changed
;
3325 static int md_revalidate(struct gendisk
*disk
)
3327 mddev_t
*mddev
= disk
->private_data
;
3332 static struct block_device_operations md_fops
=
3334 .owner
= THIS_MODULE
,
3336 .release
= md_release
,
3338 .media_changed
= md_media_changed
,
3339 .revalidate_disk
= md_revalidate
,
3342 static int md_thread(void * arg
)
3344 mdk_thread_t
*thread
= arg
;
3347 * md_thread is a 'system-thread', it's priority should be very
3348 * high. We avoid resource deadlocks individually in each
3349 * raid personality. (RAID5 does preallocation) We also use RR and
3350 * the very same RT priority as kswapd, thus we will never get
3351 * into a priority inversion deadlock.
3353 * we definitely have to have equal or higher priority than
3354 * bdflush, otherwise bdflush will deadlock if there are too
3355 * many dirty RAID5 blocks.
3358 allow_signal(SIGKILL
);
3359 complete(thread
->event
);
3360 while (!kthread_should_stop()) {
3361 void (*run
)(mddev_t
*);
3363 wait_event_interruptible_timeout(thread
->wqueue
,
3364 test_bit(THREAD_WAKEUP
, &thread
->flags
)
3365 || kthread_should_stop(),
3369 clear_bit(THREAD_WAKEUP
, &thread
->flags
);
3379 void md_wakeup_thread(mdk_thread_t
*thread
)
3382 dprintk("md: waking up MD thread %s.\n", thread
->tsk
->comm
);
3383 set_bit(THREAD_WAKEUP
, &thread
->flags
);
3384 wake_up(&thread
->wqueue
);
3388 mdk_thread_t
*md_register_thread(void (*run
) (mddev_t
*), mddev_t
*mddev
,
3391 mdk_thread_t
*thread
;
3392 struct completion event
;
3394 thread
= kmalloc(sizeof(mdk_thread_t
), GFP_KERNEL
);
3398 memset(thread
, 0, sizeof(mdk_thread_t
));
3399 init_waitqueue_head(&thread
->wqueue
);
3401 init_completion(&event
);
3402 thread
->event
= &event
;
3404 thread
->mddev
= mddev
;
3405 thread
->name
= name
;
3406 thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
3407 thread
->tsk
= kthread_run(md_thread
, thread
, name
, mdname(thread
->mddev
));
3408 if (IS_ERR(thread
->tsk
)) {
3412 wait_for_completion(&event
);
3416 void md_unregister_thread(mdk_thread_t
*thread
)
3418 dprintk("interrupting MD-thread pid %d\n", thread
->tsk
->pid
);
3420 kthread_stop(thread
->tsk
);
3424 void md_error(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
3431 if (!rdev
|| rdev
->faulty
)
3434 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
3436 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
3437 __builtin_return_address(0),__builtin_return_address(1),
3438 __builtin_return_address(2),__builtin_return_address(3));
3440 if (!mddev
->pers
->error_handler
)
3442 mddev
->pers
->error_handler(mddev
,rdev
);
3443 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
3444 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3445 md_wakeup_thread(mddev
->thread
);
3448 /* seq_file implementation /proc/mdstat */
3450 static void status_unused(struct seq_file
*seq
)
3454 struct list_head
*tmp
;
3456 seq_printf(seq
, "unused devices: ");
3458 ITERATE_RDEV_PENDING(rdev
,tmp
) {
3459 char b
[BDEVNAME_SIZE
];
3461 seq_printf(seq
, "%s ",
3462 bdevname(rdev
->bdev
,b
));
3465 seq_printf(seq
, "<none>");
3467 seq_printf(seq
, "\n");
3471 static void status_resync(struct seq_file
*seq
, mddev_t
* mddev
)
3473 unsigned long max_blocks
, resync
, res
, dt
, db
, rt
;
3475 resync
= (mddev
->curr_resync
- atomic_read(&mddev
->recovery_active
))/2;
3477 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
3478 max_blocks
= mddev
->resync_max_sectors
>> 1;
3480 max_blocks
= mddev
->size
;
3483 * Should not happen.
3489 res
= (resync
/1024)*1000/(max_blocks
/1024 + 1);
3491 int i
, x
= res
/50, y
= 20-x
;
3492 seq_printf(seq
, "[");
3493 for (i
= 0; i
< x
; i
++)
3494 seq_printf(seq
, "=");
3495 seq_printf(seq
, ">");
3496 for (i
= 0; i
< y
; i
++)
3497 seq_printf(seq
, ".");
3498 seq_printf(seq
, "] ");
3500 seq_printf(seq
, " %s =%3lu.%lu%% (%lu/%lu)",
3501 (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ?
3502 "resync" : "recovery"),
3503 res
/10, res
% 10, resync
, max_blocks
);
3506 * We do not want to overflow, so the order of operands and
3507 * the * 100 / 100 trick are important. We do a +1 to be
3508 * safe against division by zero. We only estimate anyway.
3510 * dt: time from mark until now
3511 * db: blocks written from mark until now
3512 * rt: remaining time
3514 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
3516 db
= resync
- (mddev
->resync_mark_cnt
/2);
3517 rt
= (dt
* ((max_blocks
-resync
) / (db
/100+1)))/100;
3519 seq_printf(seq
, " finish=%lu.%lumin", rt
/ 60, (rt
% 60)/6);
3521 seq_printf(seq
, " speed=%ldK/sec", db
/dt
);
3524 static void *md_seq_start(struct seq_file
*seq
, loff_t
*pos
)
3526 struct list_head
*tmp
;
3536 spin_lock(&all_mddevs_lock
);
3537 list_for_each(tmp
,&all_mddevs
)
3539 mddev
= list_entry(tmp
, mddev_t
, all_mddevs
);
3541 spin_unlock(&all_mddevs_lock
);
3544 spin_unlock(&all_mddevs_lock
);
3546 return (void*)2;/* tail */
3550 static void *md_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
3552 struct list_head
*tmp
;
3553 mddev_t
*next_mddev
, *mddev
= v
;
3559 spin_lock(&all_mddevs_lock
);
3561 tmp
= all_mddevs
.next
;
3563 tmp
= mddev
->all_mddevs
.next
;
3564 if (tmp
!= &all_mddevs
)
3565 next_mddev
= mddev_get(list_entry(tmp
,mddev_t
,all_mddevs
));
3567 next_mddev
= (void*)2;
3570 spin_unlock(&all_mddevs_lock
);
3578 static void md_seq_stop(struct seq_file
*seq
, void *v
)
3582 if (mddev
&& v
!= (void*)1 && v
!= (void*)2)
3586 static int md_seq_show(struct seq_file
*seq
, void *v
)
3590 struct list_head
*tmp2
;
3593 struct bitmap
*bitmap
;
3595 if (v
== (void*)1) {
3596 seq_printf(seq
, "Personalities : ");
3597 spin_lock(&pers_lock
);
3598 for (i
= 0; i
< MAX_PERSONALITY
; i
++)
3600 seq_printf(seq
, "[%s] ", pers
[i
]->name
);
3602 spin_unlock(&pers_lock
);
3603 seq_printf(seq
, "\n");
3606 if (v
== (void*)2) {
3611 if (mddev_lock(mddev
)!=0)
3613 if (mddev
->pers
|| mddev
->raid_disks
|| !list_empty(&mddev
->disks
)) {
3614 seq_printf(seq
, "%s : %sactive", mdname(mddev
),
3615 mddev
->pers
? "" : "in");
3618 seq_printf(seq
, " (read-only)");
3619 seq_printf(seq
, " %s", mddev
->pers
->name
);
3623 ITERATE_RDEV(mddev
,rdev
,tmp2
) {
3624 char b
[BDEVNAME_SIZE
];
3625 seq_printf(seq
, " %s[%d]",
3626 bdevname(rdev
->bdev
,b
), rdev
->desc_nr
);
3627 if (test_bit(WriteMostly
, &rdev
->flags
))
3628 seq_printf(seq
, "(W)");
3630 seq_printf(seq
, "(F)");
3632 } else if (rdev
->raid_disk
< 0)
3633 seq_printf(seq
, "(S)"); /* spare */
3637 if (!list_empty(&mddev
->disks
)) {
3639 seq_printf(seq
, "\n %llu blocks",
3640 (unsigned long long)mddev
->array_size
);
3642 seq_printf(seq
, "\n %llu blocks",
3643 (unsigned long long)size
);
3645 if (mddev
->persistent
) {
3646 if (mddev
->major_version
!= 0 ||
3647 mddev
->minor_version
!= 90) {
3648 seq_printf(seq
," super %d.%d",
3649 mddev
->major_version
,
3650 mddev
->minor_version
);
3653 seq_printf(seq
, " super non-persistent");
3656 mddev
->pers
->status (seq
, mddev
);
3657 seq_printf(seq
, "\n ");
3658 if (mddev
->curr_resync
> 2) {
3659 status_resync (seq
, mddev
);
3660 seq_printf(seq
, "\n ");
3661 } else if (mddev
->curr_resync
== 1 || mddev
->curr_resync
== 2)
3662 seq_printf(seq
, " resync=DELAYED\n ");
3664 seq_printf(seq
, "\n ");
3666 if ((bitmap
= mddev
->bitmap
)) {
3667 unsigned long chunk_kb
;
3668 unsigned long flags
;
3669 spin_lock_irqsave(&bitmap
->lock
, flags
);
3670 chunk_kb
= bitmap
->chunksize
>> 10;
3671 seq_printf(seq
, "bitmap: %lu/%lu pages [%luKB], "
3673 bitmap
->pages
- bitmap
->missing_pages
,
3675 (bitmap
->pages
- bitmap
->missing_pages
)
3676 << (PAGE_SHIFT
- 10),
3677 chunk_kb
? chunk_kb
: bitmap
->chunksize
,
3678 chunk_kb
? "KB" : "B");
3680 seq_printf(seq
, ", file: ");
3681 seq_path(seq
, bitmap
->file
->f_vfsmnt
,
3682 bitmap
->file
->f_dentry
," \t\n");
3685 seq_printf(seq
, "\n");
3686 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
3689 seq_printf(seq
, "\n");
3691 mddev_unlock(mddev
);
3696 static struct seq_operations md_seq_ops
= {
3697 .start
= md_seq_start
,
3698 .next
= md_seq_next
,
3699 .stop
= md_seq_stop
,
3700 .show
= md_seq_show
,
3703 static int md_seq_open(struct inode
*inode
, struct file
*file
)
3707 error
= seq_open(file
, &md_seq_ops
);
3711 static struct file_operations md_seq_fops
= {
3712 .open
= md_seq_open
,
3714 .llseek
= seq_lseek
,
3715 .release
= seq_release
,
3718 int register_md_personality(int pnum
, mdk_personality_t
*p
)
3720 if (pnum
>= MAX_PERSONALITY
) {
3722 "md: tried to install personality %s as nr %d, but max is %lu\n",
3723 p
->name
, pnum
, MAX_PERSONALITY
-1);
3727 spin_lock(&pers_lock
);
3729 spin_unlock(&pers_lock
);
3734 printk(KERN_INFO
"md: %s personality registered as nr %d\n", p
->name
, pnum
);
3735 spin_unlock(&pers_lock
);
3739 int unregister_md_personality(int pnum
)
3741 if (pnum
>= MAX_PERSONALITY
)
3744 printk(KERN_INFO
"md: %s personality unregistered\n", pers
[pnum
]->name
);
3745 spin_lock(&pers_lock
);
3747 spin_unlock(&pers_lock
);
3751 static int is_mddev_idle(mddev_t
*mddev
)
3754 struct list_head
*tmp
;
3756 unsigned long curr_events
;
3759 ITERATE_RDEV(mddev
,rdev
,tmp
) {
3760 struct gendisk
*disk
= rdev
->bdev
->bd_contains
->bd_disk
;
3761 curr_events
= disk_stat_read(disk
, sectors
[0]) +
3762 disk_stat_read(disk
, sectors
[1]) -
3763 atomic_read(&disk
->sync_io
);
3764 /* Allow some slack between valud of curr_events and last_events,
3765 * as there are some uninteresting races.
3766 * Note: the following is an unsigned comparison.
3768 if ((curr_events
- rdev
->last_events
+ 32) > 64) {
3769 rdev
->last_events
= curr_events
;
3776 void md_done_sync(mddev_t
*mddev
, int blocks
, int ok
)
3778 /* another "blocks" (512byte) blocks have been synced */
3779 atomic_sub(blocks
, &mddev
->recovery_active
);
3780 wake_up(&mddev
->recovery_wait
);
3782 set_bit(MD_RECOVERY_ERR
, &mddev
->recovery
);
3783 md_wakeup_thread(mddev
->thread
);
3784 // stop recovery, signal do_sync ....
3789 /* md_write_start(mddev, bi)
3790 * If we need to update some array metadata (e.g. 'active' flag
3791 * in superblock) before writing, schedule a superblock update
3792 * and wait for it to complete.
3794 void md_write_start(mddev_t
*mddev
, struct bio
*bi
)
3796 if (bio_data_dir(bi
) != WRITE
)
3799 atomic_inc(&mddev
->writes_pending
);
3800 if (mddev
->in_sync
) {
3801 spin_lock(&mddev
->write_lock
);
3802 if (mddev
->in_sync
) {
3804 mddev
->sb_dirty
= 1;
3805 md_wakeup_thread(mddev
->thread
);
3807 spin_unlock(&mddev
->write_lock
);
3809 wait_event(mddev
->sb_wait
, mddev
->sb_dirty
==0);
3812 void md_write_end(mddev_t
*mddev
)
3814 if (atomic_dec_and_test(&mddev
->writes_pending
)) {
3815 if (mddev
->safemode
== 2)
3816 md_wakeup_thread(mddev
->thread
);
3818 mod_timer(&mddev
->safemode_timer
, jiffies
+ mddev
->safemode_delay
);
3822 static DECLARE_WAIT_QUEUE_HEAD(resync_wait
);
3824 #define SYNC_MARKS 10
3825 #define SYNC_MARK_STEP (3*HZ)
3826 static void md_do_sync(mddev_t
*mddev
)
3829 unsigned int currspeed
= 0,
3831 sector_t max_sectors
,j
, io_sectors
;
3832 unsigned long mark
[SYNC_MARKS
];
3833 sector_t mark_cnt
[SYNC_MARKS
];
3835 struct list_head
*tmp
;
3836 sector_t last_check
;
3839 /* just incase thread restarts... */
3840 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
3843 /* we overload curr_resync somewhat here.
3844 * 0 == not engaged in resync at all
3845 * 2 == checking that there is no conflict with another sync
3846 * 1 == like 2, but have yielded to allow conflicting resync to
3848 * other == active in resync - this many blocks
3850 * Before starting a resync we must have set curr_resync to
3851 * 2, and then checked that every "conflicting" array has curr_resync
3852 * less than ours. When we find one that is the same or higher
3853 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
3854 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
3855 * This will mean we have to start checking from the beginning again.
3860 mddev
->curr_resync
= 2;
3863 if (signal_pending(current
) ||
3864 kthread_should_stop()) {
3865 flush_signals(current
);
3866 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
3869 ITERATE_MDDEV(mddev2
,tmp
) {
3870 if (mddev2
== mddev
)
3872 if (mddev2
->curr_resync
&&
3873 match_mddev_units(mddev
,mddev2
)) {
3875 if (mddev
< mddev2
&& mddev
->curr_resync
== 2) {
3876 /* arbitrarily yield */
3877 mddev
->curr_resync
= 1;
3878 wake_up(&resync_wait
);
3880 if (mddev
> mddev2
&& mddev
->curr_resync
== 1)
3881 /* no need to wait here, we can wait the next
3882 * time 'round when curr_resync == 2
3885 prepare_to_wait(&resync_wait
, &wq
, TASK_INTERRUPTIBLE
);
3886 if (!signal_pending(current
) &&
3887 !kthread_should_stop() &&
3888 mddev2
->curr_resync
>= mddev
->curr_resync
) {
3889 printk(KERN_INFO
"md: delaying resync of %s"
3890 " until %s has finished resync (they"
3891 " share one or more physical units)\n",
3892 mdname(mddev
), mdname(mddev2
));
3895 finish_wait(&resync_wait
, &wq
);
3898 finish_wait(&resync_wait
, &wq
);
3901 } while (mddev
->curr_resync
< 2);
3903 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
3904 /* resync follows the size requested by the personality,
3905 * which defaults to physical size, but can be virtual size
3907 max_sectors
= mddev
->resync_max_sectors
;
3908 mddev
->resync_mismatches
= 0;
3910 /* recovery follows the physical size of devices */
3911 max_sectors
= mddev
->size
<< 1;
3913 printk(KERN_INFO
"md: syncing RAID array %s\n", mdname(mddev
));
3914 printk(KERN_INFO
"md: minimum _guaranteed_ reconstruction speed:"
3915 " %d KB/sec/disc.\n", sysctl_speed_limit_min
);
3916 printk(KERN_INFO
"md: using maximum available idle IO bandwidth "
3917 "(but not more than %d KB/sec) for reconstruction.\n",
3918 sysctl_speed_limit_max
);
3920 is_mddev_idle(mddev
); /* this also initializes IO event counters */
3921 /* we don't use the checkpoint if there's a bitmap */
3922 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) && !mddev
->bitmap
3923 && ! test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
3924 j
= mddev
->recovery_cp
;
3928 for (m
= 0; m
< SYNC_MARKS
; m
++) {
3930 mark_cnt
[m
] = io_sectors
;
3933 mddev
->resync_mark
= mark
[last_mark
];
3934 mddev
->resync_mark_cnt
= mark_cnt
[last_mark
];
3937 * Tune reconstruction:
3939 window
= 32*(PAGE_SIZE
/512);
3940 printk(KERN_INFO
"md: using %dk window, over a total of %llu blocks.\n",
3941 window
/2,(unsigned long long) max_sectors
/2);
3943 atomic_set(&mddev
->recovery_active
, 0);
3944 init_waitqueue_head(&mddev
->recovery_wait
);
3949 "md: resuming recovery of %s from checkpoint.\n",
3951 mddev
->curr_resync
= j
;
3954 while (j
< max_sectors
) {
3958 sectors
= mddev
->pers
->sync_request(mddev
, j
, &skipped
,
3959 currspeed
< sysctl_speed_limit_min
);
3961 set_bit(MD_RECOVERY_ERR
, &mddev
->recovery
);
3965 if (!skipped
) { /* actual IO requested */
3966 io_sectors
+= sectors
;
3967 atomic_add(sectors
, &mddev
->recovery_active
);
3971 if (j
>1) mddev
->curr_resync
= j
;
3974 if (last_check
+ window
> io_sectors
|| j
== max_sectors
)
3977 last_check
= io_sectors
;
3979 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) ||
3980 test_bit(MD_RECOVERY_ERR
, &mddev
->recovery
))
3984 if (time_after_eq(jiffies
, mark
[last_mark
] + SYNC_MARK_STEP
)) {
3986 int next
= (last_mark
+1) % SYNC_MARKS
;
3988 mddev
->resync_mark
= mark
[next
];
3989 mddev
->resync_mark_cnt
= mark_cnt
[next
];
3990 mark
[next
] = jiffies
;
3991 mark_cnt
[next
] = io_sectors
- atomic_read(&mddev
->recovery_active
);
3996 if (signal_pending(current
) || kthread_should_stop()) {
3998 * got a signal, exit.
4001 "md: md_do_sync() got signal ... exiting\n");
4002 flush_signals(current
);
4003 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4008 * this loop exits only if either when we are slower than
4009 * the 'hard' speed limit, or the system was IO-idle for
4011 * the system might be non-idle CPU-wise, but we only care
4012 * about not overloading the IO subsystem. (things like an
4013 * e2fsck being done on the RAID array should execute fast)
4015 mddev
->queue
->unplug_fn(mddev
->queue
);
4018 currspeed
= ((unsigned long)(io_sectors
-mddev
->resync_mark_cnt
))/2
4019 /((jiffies
-mddev
->resync_mark
)/HZ
+1) +1;
4021 if (currspeed
> sysctl_speed_limit_min
) {
4022 if ((currspeed
> sysctl_speed_limit_max
) ||
4023 !is_mddev_idle(mddev
)) {
4024 msleep_interruptible(250);
4029 printk(KERN_INFO
"md: %s: sync done.\n",mdname(mddev
));
4031 * this also signals 'finished resyncing' to md_stop
4034 mddev
->queue
->unplug_fn(mddev
->queue
);
4036 wait_event(mddev
->recovery_wait
, !atomic_read(&mddev
->recovery_active
));
4038 /* tell personality that we are finished */
4039 mddev
->pers
->sync_request(mddev
, max_sectors
, &skipped
, 1);
4041 if (!test_bit(MD_RECOVERY_ERR
, &mddev
->recovery
) &&
4042 mddev
->curr_resync
> 2 &&
4043 mddev
->curr_resync
>= mddev
->recovery_cp
) {
4044 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
4046 "md: checkpointing recovery of %s.\n",
4048 mddev
->recovery_cp
= mddev
->curr_resync
;
4050 mddev
->recovery_cp
= MaxSector
;
4054 mddev
->curr_resync
= 0;
4055 wake_up(&resync_wait
);
4056 set_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
4057 md_wakeup_thread(mddev
->thread
);
4062 * This routine is regularly called by all per-raid-array threads to
4063 * deal with generic issues like resync and super-block update.
4064 * Raid personalities that don't have a thread (linear/raid0) do not
4065 * need this as they never do any recovery or update the superblock.
4067 * It does not do any resync itself, but rather "forks" off other threads
4068 * to do that as needed.
4069 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
4070 * "->recovery" and create a thread at ->sync_thread.
4071 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
4072 * and wakeups up this thread which will reap the thread and finish up.
4073 * This thread also removes any faulty devices (with nr_pending == 0).
4075 * The overall approach is:
4076 * 1/ if the superblock needs updating, update it.
4077 * 2/ If a recovery thread is running, don't do anything else.
4078 * 3/ If recovery has finished, clean up, possibly marking spares active.
4079 * 4/ If there are any faulty devices, remove them.
4080 * 5/ If array is degraded, try to add spares devices
4081 * 6/ If array has spares or is not in-sync, start a resync thread.
4083 void md_check_recovery(mddev_t
*mddev
)
4086 struct list_head
*rtmp
;
4090 bitmap_daemon_work(mddev
->bitmap
);
4095 if (signal_pending(current
)) {
4096 if (mddev
->pers
->sync_request
) {
4097 printk(KERN_INFO
"md: %s in immediate safe mode\n",
4099 mddev
->safemode
= 2;
4101 flush_signals(current
);
4106 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
4107 test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
4108 (mddev
->safemode
== 1) ||
4109 (mddev
->safemode
== 2 && ! atomic_read(&mddev
->writes_pending
)
4110 && !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
4114 if (mddev_trylock(mddev
)==0) {
4117 spin_lock(&mddev
->write_lock
);
4118 if (mddev
->safemode
&& !atomic_read(&mddev
->writes_pending
) &&
4119 !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
) {
4121 mddev
->sb_dirty
= 1;
4123 if (mddev
->safemode
== 1)
4124 mddev
->safemode
= 0;
4125 spin_unlock(&mddev
->write_lock
);
4127 if (mddev
->sb_dirty
)
4128 md_update_sb(mddev
);
4131 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
4132 !test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
)) {
4133 /* resync/recovery still happening */
4134 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4137 if (mddev
->sync_thread
) {
4138 /* resync has finished, collect result */
4139 md_unregister_thread(mddev
->sync_thread
);
4140 mddev
->sync_thread
= NULL
;
4141 if (!test_bit(MD_RECOVERY_ERR
, &mddev
->recovery
) &&
4142 !test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
4144 /* activate any spares */
4145 mddev
->pers
->spare_active(mddev
);
4147 md_update_sb(mddev
);
4149 /* if array is no-longer degraded, then any saved_raid_disk
4150 * information must be scrapped
4152 if (!mddev
->degraded
)
4153 ITERATE_RDEV(mddev
,rdev
,rtmp
)
4154 rdev
->saved_raid_disk
= -1;
4156 mddev
->recovery
= 0;
4157 /* flag recovery needed just to double check */
4158 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4161 /* Clear some bits that don't mean anything, but
4164 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4165 clear_bit(MD_RECOVERY_ERR
, &mddev
->recovery
);
4166 clear_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4167 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
4169 /* no recovery is running.
4170 * remove any failed drives, then
4171 * add spares if possible.
4172 * Spare are also removed and re-added, to allow
4173 * the personality to fail the re-add.
4175 ITERATE_RDEV(mddev
,rdev
,rtmp
)
4176 if (rdev
->raid_disk
>= 0 &&
4177 (rdev
->faulty
|| ! rdev
->in_sync
) &&
4178 atomic_read(&rdev
->nr_pending
)==0) {
4179 if (mddev
->pers
->hot_remove_disk(mddev
, rdev
->raid_disk
)==0) {
4181 sprintf(nm
,"rd%d", rdev
->raid_disk
);
4182 sysfs_remove_link(&mddev
->kobj
, nm
);
4183 rdev
->raid_disk
= -1;
4187 if (mddev
->degraded
) {
4188 ITERATE_RDEV(mddev
,rdev
,rtmp
)
4189 if (rdev
->raid_disk
< 0
4191 if (mddev
->pers
->hot_add_disk(mddev
,rdev
)) {
4193 sprintf(nm
, "rd%d", rdev
->raid_disk
);
4194 sysfs_create_link(&mddev
->kobj
, &rdev
->kobj
, nm
);
4202 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
4203 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
4204 } else if (mddev
->recovery_cp
< MaxSector
) {
4205 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
4206 } else if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
4207 /* nothing to be done ... */
4210 if (mddev
->pers
->sync_request
) {
4211 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
4212 if (spares
&& mddev
->bitmap
&& ! mddev
->bitmap
->file
) {
4213 /* We are adding a device or devices to an array
4214 * which has the bitmap stored on all devices.
4215 * So make sure all bitmap pages get written
4217 bitmap_write_all(mddev
->bitmap
);
4219 mddev
->sync_thread
= md_register_thread(md_do_sync
,
4222 if (!mddev
->sync_thread
) {
4223 printk(KERN_ERR
"%s: could not start resync"
4226 /* leave the spares where they are, it shouldn't hurt */
4227 mddev
->recovery
= 0;
4229 md_wakeup_thread(mddev
->sync_thread
);
4233 mddev_unlock(mddev
);
4237 static int md_notify_reboot(struct notifier_block
*this,
4238 unsigned long code
, void *x
)
4240 struct list_head
*tmp
;
4243 if ((code
== SYS_DOWN
) || (code
== SYS_HALT
) || (code
== SYS_POWER_OFF
)) {
4245 printk(KERN_INFO
"md: stopping all md devices.\n");
4247 ITERATE_MDDEV(mddev
,tmp
)
4248 if (mddev_trylock(mddev
)==0)
4249 do_md_stop (mddev
, 1);
4251 * certain more exotic SCSI devices are known to be
4252 * volatile wrt too early system reboots. While the
4253 * right place to handle this issue is the given
4254 * driver, we do want to have a safe RAID driver ...
4261 static struct notifier_block md_notifier
= {
4262 .notifier_call
= md_notify_reboot
,
4264 .priority
= INT_MAX
, /* before any real devices */
4267 static void md_geninit(void)
4269 struct proc_dir_entry
*p
;
4271 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t
));
4273 p
= create_proc_entry("mdstat", S_IRUGO
, NULL
);
4275 p
->proc_fops
= &md_seq_fops
;
4278 static int __init
md_init(void)
4282 printk(KERN_INFO
"md: md driver %d.%d.%d MAX_MD_DEVS=%d,"
4283 " MD_SB_DISKS=%d\n",
4284 MD_MAJOR_VERSION
, MD_MINOR_VERSION
,
4285 MD_PATCHLEVEL_VERSION
, MAX_MD_DEVS
, MD_SB_DISKS
);
4286 printk(KERN_INFO
"md: bitmap version %d.%d\n", BITMAP_MAJOR
,
4289 if (register_blkdev(MAJOR_NR
, "md"))
4291 if ((mdp_major
=register_blkdev(0, "mdp"))<=0) {
4292 unregister_blkdev(MAJOR_NR
, "md");
4296 blk_register_region(MKDEV(MAJOR_NR
, 0), MAX_MD_DEVS
, THIS_MODULE
,
4297 md_probe
, NULL
, NULL
);
4298 blk_register_region(MKDEV(mdp_major
, 0), MAX_MD_DEVS
<<MdpMinorShift
, THIS_MODULE
,
4299 md_probe
, NULL
, NULL
);
4301 for (minor
=0; minor
< MAX_MD_DEVS
; ++minor
)
4302 devfs_mk_bdev(MKDEV(MAJOR_NR
, minor
),
4303 S_IFBLK
|S_IRUSR
|S_IWUSR
,
4306 for (minor
=0; minor
< MAX_MD_DEVS
; ++minor
)
4307 devfs_mk_bdev(MKDEV(mdp_major
, minor
<<MdpMinorShift
),
4308 S_IFBLK
|S_IRUSR
|S_IWUSR
,
4312 register_reboot_notifier(&md_notifier
);
4313 raid_table_header
= register_sysctl_table(raid_root_table
, 1);
4323 * Searches all registered partitions for autorun RAID arrays
4326 static dev_t detected_devices
[128];
4329 void md_autodetect_dev(dev_t dev
)
4331 if (dev_cnt
>= 0 && dev_cnt
< 127)
4332 detected_devices
[dev_cnt
++] = dev
;
4336 static void autostart_arrays(int part
)
4341 printk(KERN_INFO
"md: Autodetecting RAID arrays.\n");
4343 for (i
= 0; i
< dev_cnt
; i
++) {
4344 dev_t dev
= detected_devices
[i
];
4346 rdev
= md_import_device(dev
,0, 0);
4354 list_add(&rdev
->same_set
, &pending_raid_disks
);
4358 autorun_devices(part
);
4363 static __exit
void md_exit(void)
4366 struct list_head
*tmp
;
4368 blk_unregister_region(MKDEV(MAJOR_NR
,0), MAX_MD_DEVS
);
4369 blk_unregister_region(MKDEV(mdp_major
,0), MAX_MD_DEVS
<< MdpMinorShift
);
4370 for (i
=0; i
< MAX_MD_DEVS
; i
++)
4371 devfs_remove("md/%d", i
);
4372 for (i
=0; i
< MAX_MD_DEVS
; i
++)
4373 devfs_remove("md/d%d", i
);
4377 unregister_blkdev(MAJOR_NR
,"md");
4378 unregister_blkdev(mdp_major
, "mdp");
4379 unregister_reboot_notifier(&md_notifier
);
4380 unregister_sysctl_table(raid_table_header
);
4381 remove_proc_entry("mdstat", NULL
);
4382 ITERATE_MDDEV(mddev
,tmp
) {
4383 struct gendisk
*disk
= mddev
->gendisk
;
4386 export_array(mddev
);
4389 mddev
->gendisk
= NULL
;
4394 module_init(md_init
)
4395 module_exit(md_exit
)
4397 EXPORT_SYMBOL(register_md_personality
);
4398 EXPORT_SYMBOL(unregister_md_personality
);
4399 EXPORT_SYMBOL(md_error
);
4400 EXPORT_SYMBOL(md_done_sync
);
4401 EXPORT_SYMBOL(md_write_start
);
4402 EXPORT_SYMBOL(md_write_end
);
4403 EXPORT_SYMBOL(md_register_thread
);
4404 EXPORT_SYMBOL(md_unregister_thread
);
4405 EXPORT_SYMBOL(md_wakeup_thread
);
4406 EXPORT_SYMBOL(md_print_devices
);
4407 EXPORT_SYMBOL(md_check_recovery
);
4408 MODULE_LICENSE("GPL");
4410 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR
);