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/linkage.h>
38 #include <linux/raid/md.h>
39 #include <linux/raid/bitmap.h>
40 #include <linux/sysctl.h>
41 #include <linux/devfs_fs_kernel.h>
42 #include <linux/buffer_head.h> /* for invalidate_bdev */
43 #include <linux/suspend.h>
45 #include <linux/init.h>
47 #include <linux/file.h>
50 #include <linux/kmod.h>
53 #include <asm/unaligned.h>
55 #define MAJOR_NR MD_MAJOR
58 /* 63 partitions with the alternate major number (mdp) */
59 #define MdpMinorShift 6
62 #define dprintk(x...) ((void)(DEBUG && printk(x)))
66 static void autostart_arrays (int part
);
69 static mdk_personality_t
*pers
[MAX_PERSONALITY
];
70 static DEFINE_SPINLOCK(pers_lock
);
73 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
74 * is 1000 KB/sec, so the extra system load does not show up that much.
75 * Increase it if you want to have more _guaranteed_ speed. Note that
76 * the RAID driver will use the maximum available bandwith if the IO
77 * subsystem is idle. There is also an 'absolute maximum' reconstruction
78 * speed limit - in case reconstruction slows down your system despite
81 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
84 static int sysctl_speed_limit_min
= 1000;
85 static int sysctl_speed_limit_max
= 200000;
87 static struct ctl_table_header
*raid_table_header
;
89 static ctl_table raid_table
[] = {
91 .ctl_name
= DEV_RAID_SPEED_LIMIT_MIN
,
92 .procname
= "speed_limit_min",
93 .data
= &sysctl_speed_limit_min
,
94 .maxlen
= sizeof(int),
96 .proc_handler
= &proc_dointvec
,
99 .ctl_name
= DEV_RAID_SPEED_LIMIT_MAX
,
100 .procname
= "speed_limit_max",
101 .data
= &sysctl_speed_limit_max
,
102 .maxlen
= sizeof(int),
104 .proc_handler
= &proc_dointvec
,
109 static ctl_table raid_dir_table
[] = {
111 .ctl_name
= DEV_RAID
,
120 static ctl_table raid_root_table
[] = {
126 .child
= raid_dir_table
,
131 static struct block_device_operations md_fops
;
134 * Enables to iterate over all existing md arrays
135 * all_mddevs_lock protects this list.
137 static LIST_HEAD(all_mddevs
);
138 static DEFINE_SPINLOCK(all_mddevs_lock
);
142 * iterates through all used mddevs in the system.
143 * We take care to grab the all_mddevs_lock whenever navigating
144 * the list, and to always hold a refcount when unlocked.
145 * Any code which breaks out of this loop while own
146 * a reference to the current mddev and must mddev_put it.
148 #define ITERATE_MDDEV(mddev,tmp) \
150 for (({ spin_lock(&all_mddevs_lock); \
151 tmp = all_mddevs.next; \
153 ({ if (tmp != &all_mddevs) \
154 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
155 spin_unlock(&all_mddevs_lock); \
156 if (mddev) mddev_put(mddev); \
157 mddev = list_entry(tmp, mddev_t, all_mddevs); \
158 tmp != &all_mddevs;}); \
159 ({ spin_lock(&all_mddevs_lock); \
164 static int md_fail_request (request_queue_t
*q
, struct bio
*bio
)
166 bio_io_error(bio
, bio
->bi_size
);
170 static inline mddev_t
*mddev_get(mddev_t
*mddev
)
172 atomic_inc(&mddev
->active
);
176 static void mddev_put(mddev_t
*mddev
)
178 if (!atomic_dec_and_lock(&mddev
->active
, &all_mddevs_lock
))
180 if (!mddev
->raid_disks
&& list_empty(&mddev
->disks
)) {
181 list_del(&mddev
->all_mddevs
);
182 blk_put_queue(mddev
->queue
);
185 spin_unlock(&all_mddevs_lock
);
188 static mddev_t
* mddev_find(dev_t unit
)
190 mddev_t
*mddev
, *new = NULL
;
193 spin_lock(&all_mddevs_lock
);
194 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
195 if (mddev
->unit
== unit
) {
197 spin_unlock(&all_mddevs_lock
);
203 list_add(&new->all_mddevs
, &all_mddevs
);
204 spin_unlock(&all_mddevs_lock
);
207 spin_unlock(&all_mddevs_lock
);
209 new = (mddev_t
*) kmalloc(sizeof(*new), GFP_KERNEL
);
213 memset(new, 0, sizeof(*new));
216 if (MAJOR(unit
) == MD_MAJOR
)
217 new->md_minor
= MINOR(unit
);
219 new->md_minor
= MINOR(unit
) >> MdpMinorShift
;
221 init_MUTEX(&new->reconfig_sem
);
222 INIT_LIST_HEAD(&new->disks
);
223 INIT_LIST_HEAD(&new->all_mddevs
);
224 init_timer(&new->safemode_timer
);
225 atomic_set(&new->active
, 1);
226 spin_lock_init(&new->write_lock
);
227 init_waitqueue_head(&new->sb_wait
);
229 new->queue
= blk_alloc_queue(GFP_KERNEL
);
235 blk_queue_make_request(new->queue
, md_fail_request
);
240 static inline int mddev_lock(mddev_t
* mddev
)
242 return down_interruptible(&mddev
->reconfig_sem
);
245 static inline void mddev_lock_uninterruptible(mddev_t
* mddev
)
247 down(&mddev
->reconfig_sem
);
250 static inline int mddev_trylock(mddev_t
* mddev
)
252 return down_trylock(&mddev
->reconfig_sem
);
255 static inline void mddev_unlock(mddev_t
* mddev
)
257 up(&mddev
->reconfig_sem
);
259 md_wakeup_thread(mddev
->thread
);
262 mdk_rdev_t
* find_rdev_nr(mddev_t
*mddev
, int nr
)
265 struct list_head
*tmp
;
267 ITERATE_RDEV(mddev
,rdev
,tmp
) {
268 if (rdev
->desc_nr
== nr
)
274 static mdk_rdev_t
* find_rdev(mddev_t
* mddev
, dev_t dev
)
276 struct list_head
*tmp
;
279 ITERATE_RDEV(mddev
,rdev
,tmp
) {
280 if (rdev
->bdev
->bd_dev
== dev
)
286 static inline sector_t
calc_dev_sboffset(struct block_device
*bdev
)
288 sector_t size
= bdev
->bd_inode
->i_size
>> BLOCK_SIZE_BITS
;
289 return MD_NEW_SIZE_BLOCKS(size
);
292 static sector_t
calc_dev_size(mdk_rdev_t
*rdev
, unsigned chunk_size
)
296 size
= rdev
->sb_offset
;
299 size
&= ~((sector_t
)chunk_size
/1024 - 1);
303 static int alloc_disk_sb(mdk_rdev_t
* rdev
)
308 rdev
->sb_page
= alloc_page(GFP_KERNEL
);
309 if (!rdev
->sb_page
) {
310 printk(KERN_ALERT
"md: out of memory.\n");
317 static void free_disk_sb(mdk_rdev_t
* rdev
)
320 page_cache_release(rdev
->sb_page
);
322 rdev
->sb_page
= NULL
;
329 static int super_written(struct bio
*bio
, unsigned int bytes_done
, int error
)
331 mdk_rdev_t
*rdev
= bio
->bi_private
;
335 if (error
|| !test_bit(BIO_UPTODATE
, &bio
->bi_flags
))
336 md_error(rdev
->mddev
, rdev
);
338 if (atomic_dec_and_test(&rdev
->mddev
->pending_writes
))
339 wake_up(&rdev
->mddev
->sb_wait
);
344 void md_super_write(mddev_t
*mddev
, mdk_rdev_t
*rdev
,
345 sector_t sector
, int size
, struct page
*page
)
347 /* write first size bytes of page to sector of rdev
348 * Increment mddev->pending_writes before returning
349 * and decrement it on completion, waking up sb_wait
350 * if zero is reached.
351 * If an error occurred, call md_error
353 struct bio
*bio
= bio_alloc(GFP_NOIO
, 1);
355 bio
->bi_bdev
= rdev
->bdev
;
356 bio
->bi_sector
= sector
;
357 bio_add_page(bio
, page
, size
, 0);
358 bio
->bi_private
= rdev
;
359 bio
->bi_end_io
= super_written
;
360 atomic_inc(&mddev
->pending_writes
);
361 submit_bio((1<<BIO_RW
)|(1<<BIO_RW_SYNC
), bio
);
364 static int bi_complete(struct bio
*bio
, unsigned int bytes_done
, int error
)
369 complete((struct completion
*)bio
->bi_private
);
373 int sync_page_io(struct block_device
*bdev
, sector_t sector
, int size
,
374 struct page
*page
, int rw
)
376 struct bio
*bio
= bio_alloc(GFP_NOIO
, 1);
377 struct completion event
;
380 rw
|= (1 << BIO_RW_SYNC
);
383 bio
->bi_sector
= sector
;
384 bio_add_page(bio
, page
, size
, 0);
385 init_completion(&event
);
386 bio
->bi_private
= &event
;
387 bio
->bi_end_io
= bi_complete
;
389 wait_for_completion(&event
);
391 ret
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
396 static int read_disk_sb(mdk_rdev_t
* rdev
)
398 char b
[BDEVNAME_SIZE
];
399 if (!rdev
->sb_page
) {
407 if (!sync_page_io(rdev
->bdev
, rdev
->sb_offset
<<1, MD_SB_BYTES
, rdev
->sb_page
, READ
))
413 printk(KERN_WARNING
"md: disabled device %s, could not read superblock.\n",
414 bdevname(rdev
->bdev
,b
));
418 static int uuid_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
420 if ( (sb1
->set_uuid0
== sb2
->set_uuid0
) &&
421 (sb1
->set_uuid1
== sb2
->set_uuid1
) &&
422 (sb1
->set_uuid2
== sb2
->set_uuid2
) &&
423 (sb1
->set_uuid3
== sb2
->set_uuid3
))
431 static int sb_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
434 mdp_super_t
*tmp1
, *tmp2
;
436 tmp1
= kmalloc(sizeof(*tmp1
),GFP_KERNEL
);
437 tmp2
= kmalloc(sizeof(*tmp2
),GFP_KERNEL
);
439 if (!tmp1
|| !tmp2
) {
441 printk(KERN_INFO
"md.c: sb1 is not equal to sb2!\n");
449 * nr_disks is not constant
454 if (memcmp(tmp1
, tmp2
, MD_SB_GENERIC_CONSTANT_WORDS
* 4))
465 static unsigned int calc_sb_csum(mdp_super_t
* sb
)
467 unsigned int disk_csum
, csum
;
469 disk_csum
= sb
->sb_csum
;
471 csum
= csum_partial((void *)sb
, MD_SB_BYTES
, 0);
472 sb
->sb_csum
= disk_csum
;
478 * Handle superblock details.
479 * We want to be able to handle multiple superblock formats
480 * so we have a common interface to them all, and an array of
481 * different handlers.
482 * We rely on user-space to write the initial superblock, and support
483 * reading and updating of superblocks.
484 * Interface methods are:
485 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
486 * loads and validates a superblock on dev.
487 * if refdev != NULL, compare superblocks on both devices
489 * 0 - dev has a superblock that is compatible with refdev
490 * 1 - dev has a superblock that is compatible and newer than refdev
491 * so dev should be used as the refdev in future
492 * -EINVAL superblock incompatible or invalid
493 * -othererror e.g. -EIO
495 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
496 * Verify that dev is acceptable into mddev.
497 * The first time, mddev->raid_disks will be 0, and data from
498 * dev should be merged in. Subsequent calls check that dev
499 * is new enough. Return 0 or -EINVAL
501 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
502 * Update the superblock for rdev with data in mddev
503 * This does not write to disc.
509 struct module
*owner
;
510 int (*load_super
)(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
);
511 int (*validate_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
512 void (*sync_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
516 * load_super for 0.90.0
518 static int super_90_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
520 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
526 * Calculate the position of the superblock,
527 * it's at the end of the disk.
529 * It also happens to be a multiple of 4Kb.
531 sb_offset
= calc_dev_sboffset(rdev
->bdev
);
532 rdev
->sb_offset
= sb_offset
;
534 ret
= read_disk_sb(rdev
);
539 bdevname(rdev
->bdev
, b
);
540 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
542 if (sb
->md_magic
!= MD_SB_MAGIC
) {
543 printk(KERN_ERR
"md: invalid raid superblock magic on %s\n",
548 if (sb
->major_version
!= 0 ||
549 sb
->minor_version
!= 90) {
550 printk(KERN_WARNING
"Bad version number %d.%d on %s\n",
551 sb
->major_version
, sb
->minor_version
,
556 if (sb
->raid_disks
<= 0)
559 if (csum_fold(calc_sb_csum(sb
)) != csum_fold(sb
->sb_csum
)) {
560 printk(KERN_WARNING
"md: invalid superblock checksum on %s\n",
565 rdev
->preferred_minor
= sb
->md_minor
;
566 rdev
->data_offset
= 0;
568 if (sb
->level
== LEVEL_MULTIPATH
)
571 rdev
->desc_nr
= sb
->this_disk
.number
;
577 mdp_super_t
*refsb
= (mdp_super_t
*)page_address(refdev
->sb_page
);
578 if (!uuid_equal(refsb
, sb
)) {
579 printk(KERN_WARNING
"md: %s has different UUID to %s\n",
580 b
, bdevname(refdev
->bdev
,b2
));
583 if (!sb_equal(refsb
, sb
)) {
584 printk(KERN_WARNING
"md: %s has same UUID"
585 " but different superblock to %s\n",
586 b
, bdevname(refdev
->bdev
, b2
));
590 ev2
= md_event(refsb
);
596 rdev
->size
= calc_dev_size(rdev
, sb
->chunk_size
);
603 * validate_super for 0.90.0
605 static int super_90_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
608 mdp_super_t
*sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
610 rdev
->raid_disk
= -1;
612 if (mddev
->raid_disks
== 0) {
613 mddev
->major_version
= 0;
614 mddev
->minor_version
= sb
->minor_version
;
615 mddev
->patch_version
= sb
->patch_version
;
616 mddev
->persistent
= ! sb
->not_persistent
;
617 mddev
->chunk_size
= sb
->chunk_size
;
618 mddev
->ctime
= sb
->ctime
;
619 mddev
->utime
= sb
->utime
;
620 mddev
->level
= sb
->level
;
621 mddev
->layout
= sb
->layout
;
622 mddev
->raid_disks
= sb
->raid_disks
;
623 mddev
->size
= sb
->size
;
624 mddev
->events
= md_event(sb
);
625 mddev
->bitmap_offset
= 0;
627 if (sb
->state
& (1<<MD_SB_CLEAN
))
628 mddev
->recovery_cp
= MaxSector
;
630 if (sb
->events_hi
== sb
->cp_events_hi
&&
631 sb
->events_lo
== sb
->cp_events_lo
) {
632 mddev
->recovery_cp
= sb
->recovery_cp
;
634 mddev
->recovery_cp
= 0;
637 memcpy(mddev
->uuid
+0, &sb
->set_uuid0
, 4);
638 memcpy(mddev
->uuid
+4, &sb
->set_uuid1
, 4);
639 memcpy(mddev
->uuid
+8, &sb
->set_uuid2
, 4);
640 memcpy(mddev
->uuid
+12,&sb
->set_uuid3
, 4);
642 mddev
->max_disks
= MD_SB_DISKS
;
644 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
) &&
645 mddev
->bitmap_file
== NULL
) {
646 if (mddev
->level
!= 1) {
647 /* FIXME use a better test */
648 printk(KERN_WARNING
"md: bitmaps only support for raid1\n");
651 mddev
->bitmap_offset
= (MD_SB_BYTES
>> 9);
654 } else if (mddev
->pers
== NULL
) {
655 /* Insist on good event counter while assembling */
656 __u64 ev1
= md_event(sb
);
658 if (ev1
< mddev
->events
)
660 } else if (mddev
->bitmap
) {
661 /* if adding to array with a bitmap, then we can accept an
662 * older device ... but not too old.
664 __u64 ev1
= md_event(sb
);
665 if (ev1
< mddev
->bitmap
->events_cleared
)
667 } else /* just a hot-add of a new device, leave raid_disk at -1 */
670 if (mddev
->level
!= LEVEL_MULTIPATH
) {
672 desc
= sb
->disks
+ rdev
->desc_nr
;
674 if (desc
->state
& (1<<MD_DISK_FAULTY
))
676 else if (desc
->state
& (1<<MD_DISK_SYNC
) &&
677 desc
->raid_disk
< mddev
->raid_disks
) {
679 rdev
->raid_disk
= desc
->raid_disk
;
681 } else /* MULTIPATH are always insync */
687 * sync_super for 0.90.0
689 static void super_90_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
692 struct list_head
*tmp
;
694 int next_spare
= mddev
->raid_disks
;
696 /* make rdev->sb match mddev data..
699 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
700 * 3/ any empty disks < next_spare become removed
702 * disks[0] gets initialised to REMOVED because
703 * we cannot be sure from other fields if it has
704 * been initialised or not.
707 int active
=0, working
=0,failed
=0,spare
=0,nr_disks
=0;
709 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
711 memset(sb
, 0, sizeof(*sb
));
713 sb
->md_magic
= MD_SB_MAGIC
;
714 sb
->major_version
= mddev
->major_version
;
715 sb
->minor_version
= mddev
->minor_version
;
716 sb
->patch_version
= mddev
->patch_version
;
717 sb
->gvalid_words
= 0; /* ignored */
718 memcpy(&sb
->set_uuid0
, mddev
->uuid
+0, 4);
719 memcpy(&sb
->set_uuid1
, mddev
->uuid
+4, 4);
720 memcpy(&sb
->set_uuid2
, mddev
->uuid
+8, 4);
721 memcpy(&sb
->set_uuid3
, mddev
->uuid
+12,4);
723 sb
->ctime
= mddev
->ctime
;
724 sb
->level
= mddev
->level
;
725 sb
->size
= mddev
->size
;
726 sb
->raid_disks
= mddev
->raid_disks
;
727 sb
->md_minor
= mddev
->md_minor
;
728 sb
->not_persistent
= !mddev
->persistent
;
729 sb
->utime
= mddev
->utime
;
731 sb
->events_hi
= (mddev
->events
>>32);
732 sb
->events_lo
= (u32
)mddev
->events
;
736 sb
->recovery_cp
= mddev
->recovery_cp
;
737 sb
->cp_events_hi
= (mddev
->events
>>32);
738 sb
->cp_events_lo
= (u32
)mddev
->events
;
739 if (mddev
->recovery_cp
== MaxSector
)
740 sb
->state
= (1<< MD_SB_CLEAN
);
744 sb
->layout
= mddev
->layout
;
745 sb
->chunk_size
= mddev
->chunk_size
;
747 if (mddev
->bitmap
&& mddev
->bitmap_file
== NULL
)
748 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
750 sb
->disks
[0].state
= (1<<MD_DISK_REMOVED
);
751 ITERATE_RDEV(mddev
,rdev2
,tmp
) {
753 if (rdev2
->raid_disk
>= 0 && rdev2
->in_sync
&& !rdev2
->faulty
)
754 rdev2
->desc_nr
= rdev2
->raid_disk
;
756 rdev2
->desc_nr
= next_spare
++;
757 d
= &sb
->disks
[rdev2
->desc_nr
];
759 d
->number
= rdev2
->desc_nr
;
760 d
->major
= MAJOR(rdev2
->bdev
->bd_dev
);
761 d
->minor
= MINOR(rdev2
->bdev
->bd_dev
);
762 if (rdev2
->raid_disk
>= 0 && rdev
->in_sync
&& !rdev2
->faulty
)
763 d
->raid_disk
= rdev2
->raid_disk
;
765 d
->raid_disk
= rdev2
->desc_nr
; /* compatibility */
767 d
->state
= (1<<MD_DISK_FAULTY
);
769 } else if (rdev2
->in_sync
) {
770 d
->state
= (1<<MD_DISK_ACTIVE
);
771 d
->state
|= (1<<MD_DISK_SYNC
);
781 /* now set the "removed" and "faulty" bits on any missing devices */
782 for (i
=0 ; i
< mddev
->raid_disks
; i
++) {
783 mdp_disk_t
*d
= &sb
->disks
[i
];
784 if (d
->state
== 0 && d
->number
== 0) {
787 d
->state
= (1<<MD_DISK_REMOVED
);
788 d
->state
|= (1<<MD_DISK_FAULTY
);
792 sb
->nr_disks
= nr_disks
;
793 sb
->active_disks
= active
;
794 sb
->working_disks
= working
;
795 sb
->failed_disks
= failed
;
796 sb
->spare_disks
= spare
;
798 sb
->this_disk
= sb
->disks
[rdev
->desc_nr
];
799 sb
->sb_csum
= calc_sb_csum(sb
);
803 * version 1 superblock
806 static unsigned int calc_sb_1_csum(struct mdp_superblock_1
* sb
)
808 unsigned int disk_csum
, csum
;
809 unsigned long long newcsum
;
810 int size
= 256 + le32_to_cpu(sb
->max_dev
)*2;
811 unsigned int *isuper
= (unsigned int*)sb
;
814 disk_csum
= sb
->sb_csum
;
817 for (i
=0; size
>=4; size
-= 4 )
818 newcsum
+= le32_to_cpu(*isuper
++);
821 newcsum
+= le16_to_cpu(*(unsigned short*) isuper
);
823 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
824 sb
->sb_csum
= disk_csum
;
825 return cpu_to_le32(csum
);
828 static int super_1_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
830 struct mdp_superblock_1
*sb
;
833 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
836 * Calculate the position of the superblock.
837 * It is always aligned to a 4K boundary and
838 * depeding on minor_version, it can be:
839 * 0: At least 8K, but less than 12K, from end of device
840 * 1: At start of device
841 * 2: 4K from start of device.
843 switch(minor_version
) {
845 sb_offset
= rdev
->bdev
->bd_inode
->i_size
>> 9;
847 sb_offset
&= ~(sector_t
)(4*2-1);
848 /* convert from sectors to K */
860 rdev
->sb_offset
= sb_offset
;
862 ret
= read_disk_sb(rdev
);
866 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
868 if (sb
->magic
!= cpu_to_le32(MD_SB_MAGIC
) ||
869 sb
->major_version
!= cpu_to_le32(1) ||
870 le32_to_cpu(sb
->max_dev
) > (4096-256)/2 ||
871 le64_to_cpu(sb
->super_offset
) != (rdev
->sb_offset
<<1) ||
872 sb
->feature_map
!= 0)
875 if (calc_sb_1_csum(sb
) != sb
->sb_csum
) {
876 printk("md: invalid superblock checksum on %s\n",
877 bdevname(rdev
->bdev
,b
));
880 if (le64_to_cpu(sb
->data_size
) < 10) {
881 printk("md: data_size too small on %s\n",
882 bdevname(rdev
->bdev
,b
));
885 rdev
->preferred_minor
= 0xffff;
886 rdev
->data_offset
= le64_to_cpu(sb
->data_offset
);
892 struct mdp_superblock_1
*refsb
=
893 (struct mdp_superblock_1
*)page_address(refdev
->sb_page
);
895 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16) != 0 ||
896 sb
->level
!= refsb
->level
||
897 sb
->layout
!= refsb
->layout
||
898 sb
->chunksize
!= refsb
->chunksize
) {
899 printk(KERN_WARNING
"md: %s has strangely different"
900 " superblock to %s\n",
901 bdevname(rdev
->bdev
,b
),
902 bdevname(refdev
->bdev
,b2
));
905 ev1
= le64_to_cpu(sb
->events
);
906 ev2
= le64_to_cpu(refsb
->events
);
912 rdev
->size
= ((rdev
->bdev
->bd_inode
->i_size
>>9) - le64_to_cpu(sb
->data_offset
)) / 2;
914 rdev
->size
= rdev
->sb_offset
;
915 if (rdev
->size
< le64_to_cpu(sb
->data_size
)/2)
917 rdev
->size
= le64_to_cpu(sb
->data_size
)/2;
918 if (le32_to_cpu(sb
->chunksize
))
919 rdev
->size
&= ~((sector_t
)le32_to_cpu(sb
->chunksize
)/2 - 1);
923 static int super_1_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
925 struct mdp_superblock_1
*sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
927 rdev
->raid_disk
= -1;
929 if (mddev
->raid_disks
== 0) {
930 mddev
->major_version
= 1;
931 mddev
->patch_version
= 0;
932 mddev
->persistent
= 1;
933 mddev
->chunk_size
= le32_to_cpu(sb
->chunksize
) << 9;
934 mddev
->ctime
= le64_to_cpu(sb
->ctime
) & ((1ULL << 32)-1);
935 mddev
->utime
= le64_to_cpu(sb
->utime
) & ((1ULL << 32)-1);
936 mddev
->level
= le32_to_cpu(sb
->level
);
937 mddev
->layout
= le32_to_cpu(sb
->layout
);
938 mddev
->raid_disks
= le32_to_cpu(sb
->raid_disks
);
939 mddev
->size
= le64_to_cpu(sb
->size
)/2;
940 mddev
->events
= le64_to_cpu(sb
->events
);
941 mddev
->bitmap_offset
= 0;
943 mddev
->recovery_cp
= le64_to_cpu(sb
->resync_offset
);
944 memcpy(mddev
->uuid
, sb
->set_uuid
, 16);
946 mddev
->max_disks
= (4096-256)/2;
948 if ((le32_to_cpu(sb
->feature_map
) & 1) &&
949 mddev
->bitmap_file
== NULL
) {
950 if (mddev
->level
!= 1) {
951 printk(KERN_WARNING
"md: bitmaps only supported for raid1\n");
954 mddev
->bitmap_offset
= (__s32
)le32_to_cpu(sb
->bitmap_offset
);
956 } else if (mddev
->pers
== NULL
) {
957 /* Insist of good event counter while assembling */
958 __u64 ev1
= le64_to_cpu(sb
->events
);
960 if (ev1
< mddev
->events
)
962 } else if (mddev
->bitmap
) {
963 /* If adding to array with a bitmap, then we can accept an
964 * older device, but not too old.
966 __u64 ev1
= le64_to_cpu(sb
->events
);
967 if (ev1
< mddev
->bitmap
->events_cleared
)
969 } else /* just a hot-add of a new device, leave raid_disk at -1 */
972 if (mddev
->level
!= LEVEL_MULTIPATH
) {
974 rdev
->desc_nr
= le32_to_cpu(sb
->dev_number
);
975 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
977 case 0xffff: /* spare */
980 case 0xfffe: /* faulty */
986 rdev
->raid_disk
= role
;
989 } else /* MULTIPATH are always insync */
995 static void super_1_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
997 struct mdp_superblock_1
*sb
;
998 struct list_head
*tmp
;
1001 /* make rdev->sb match mddev and rdev data. */
1003 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1005 sb
->feature_map
= 0;
1007 memset(sb
->pad1
, 0, sizeof(sb
->pad1
));
1008 memset(sb
->pad2
, 0, sizeof(sb
->pad2
));
1009 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
1011 sb
->utime
= cpu_to_le64((__u64
)mddev
->utime
);
1012 sb
->events
= cpu_to_le64(mddev
->events
);
1014 sb
->resync_offset
= cpu_to_le64(mddev
->recovery_cp
);
1016 sb
->resync_offset
= cpu_to_le64(0);
1018 if (mddev
->bitmap
&& mddev
->bitmap_file
== NULL
) {
1019 sb
->bitmap_offset
= cpu_to_le32((__u32
)mddev
->bitmap_offset
);
1020 sb
->feature_map
= cpu_to_le32(1);
1024 ITERATE_RDEV(mddev
,rdev2
,tmp
)
1025 if (rdev2
->desc_nr
+1 > max_dev
)
1026 max_dev
= rdev2
->desc_nr
+1;
1028 sb
->max_dev
= cpu_to_le32(max_dev
);
1029 for (i
=0; i
<max_dev
;i
++)
1030 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1032 ITERATE_RDEV(mddev
,rdev2
,tmp
) {
1035 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1036 else if (rdev2
->in_sync
)
1037 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1039 sb
->dev_roles
[i
] = cpu_to_le16(0xffff);
1042 sb
->recovery_offset
= cpu_to_le64(0); /* not supported yet */
1043 sb
->sb_csum
= calc_sb_1_csum(sb
);
1047 static struct super_type super_types
[] = {
1050 .owner
= THIS_MODULE
,
1051 .load_super
= super_90_load
,
1052 .validate_super
= super_90_validate
,
1053 .sync_super
= super_90_sync
,
1057 .owner
= THIS_MODULE
,
1058 .load_super
= super_1_load
,
1059 .validate_super
= super_1_validate
,
1060 .sync_super
= super_1_sync
,
1064 static mdk_rdev_t
* match_dev_unit(mddev_t
*mddev
, mdk_rdev_t
*dev
)
1066 struct list_head
*tmp
;
1069 ITERATE_RDEV(mddev
,rdev
,tmp
)
1070 if (rdev
->bdev
->bd_contains
== dev
->bdev
->bd_contains
)
1076 static int match_mddev_units(mddev_t
*mddev1
, mddev_t
*mddev2
)
1078 struct list_head
*tmp
;
1081 ITERATE_RDEV(mddev1
,rdev
,tmp
)
1082 if (match_dev_unit(mddev2
, rdev
))
1088 static LIST_HEAD(pending_raid_disks
);
1090 static int bind_rdev_to_array(mdk_rdev_t
* rdev
, mddev_t
* mddev
)
1092 mdk_rdev_t
*same_pdev
;
1093 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1099 same_pdev
= match_dev_unit(mddev
, rdev
);
1102 "%s: WARNING: %s appears to be on the same physical"
1103 " disk as %s. True\n protection against single-disk"
1104 " failure might be compromised.\n",
1105 mdname(mddev
), bdevname(rdev
->bdev
,b
),
1106 bdevname(same_pdev
->bdev
,b2
));
1108 /* Verify rdev->desc_nr is unique.
1109 * If it is -1, assign a free number, else
1110 * check number is not in use
1112 if (rdev
->desc_nr
< 0) {
1114 if (mddev
->pers
) choice
= mddev
->raid_disks
;
1115 while (find_rdev_nr(mddev
, choice
))
1117 rdev
->desc_nr
= choice
;
1119 if (find_rdev_nr(mddev
, rdev
->desc_nr
))
1123 list_add(&rdev
->same_set
, &mddev
->disks
);
1124 rdev
->mddev
= mddev
;
1125 printk(KERN_INFO
"md: bind<%s>\n", bdevname(rdev
->bdev
,b
));
1129 static void unbind_rdev_from_array(mdk_rdev_t
* rdev
)
1131 char b
[BDEVNAME_SIZE
];
1136 list_del_init(&rdev
->same_set
);
1137 printk(KERN_INFO
"md: unbind<%s>\n", bdevname(rdev
->bdev
,b
));
1142 * prevent the device from being mounted, repartitioned or
1143 * otherwise reused by a RAID array (or any other kernel
1144 * subsystem), by bd_claiming the device.
1146 static int lock_rdev(mdk_rdev_t
*rdev
, dev_t dev
)
1149 struct block_device
*bdev
;
1150 char b
[BDEVNAME_SIZE
];
1152 bdev
= open_by_devnum(dev
, FMODE_READ
|FMODE_WRITE
);
1154 printk(KERN_ERR
"md: could not open %s.\n",
1155 __bdevname(dev
, b
));
1156 return PTR_ERR(bdev
);
1158 err
= bd_claim(bdev
, rdev
);
1160 printk(KERN_ERR
"md: could not bd_claim %s.\n",
1169 static void unlock_rdev(mdk_rdev_t
*rdev
)
1171 struct block_device
*bdev
= rdev
->bdev
;
1179 void md_autodetect_dev(dev_t dev
);
1181 static void export_rdev(mdk_rdev_t
* rdev
)
1183 char b
[BDEVNAME_SIZE
];
1184 printk(KERN_INFO
"md: export_rdev(%s)\n",
1185 bdevname(rdev
->bdev
,b
));
1189 list_del_init(&rdev
->same_set
);
1191 md_autodetect_dev(rdev
->bdev
->bd_dev
);
1197 static void kick_rdev_from_array(mdk_rdev_t
* rdev
)
1199 unbind_rdev_from_array(rdev
);
1203 static void export_array(mddev_t
*mddev
)
1205 struct list_head
*tmp
;
1208 ITERATE_RDEV(mddev
,rdev
,tmp
) {
1213 kick_rdev_from_array(rdev
);
1215 if (!list_empty(&mddev
->disks
))
1217 mddev
->raid_disks
= 0;
1218 mddev
->major_version
= 0;
1221 static void print_desc(mdp_disk_t
*desc
)
1223 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc
->number
,
1224 desc
->major
,desc
->minor
,desc
->raid_disk
,desc
->state
);
1227 static void print_sb(mdp_super_t
*sb
)
1232 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1233 sb
->major_version
, sb
->minor_version
, sb
->patch_version
,
1234 sb
->set_uuid0
, sb
->set_uuid1
, sb
->set_uuid2
, sb
->set_uuid3
,
1236 printk(KERN_INFO
"md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1237 sb
->level
, sb
->size
, sb
->nr_disks
, sb
->raid_disks
,
1238 sb
->md_minor
, sb
->layout
, sb
->chunk_size
);
1239 printk(KERN_INFO
"md: UT:%08x ST:%d AD:%d WD:%d"
1240 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1241 sb
->utime
, sb
->state
, sb
->active_disks
, sb
->working_disks
,
1242 sb
->failed_disks
, sb
->spare_disks
,
1243 sb
->sb_csum
, (unsigned long)sb
->events_lo
);
1246 for (i
= 0; i
< MD_SB_DISKS
; i
++) {
1249 desc
= sb
->disks
+ i
;
1250 if (desc
->number
|| desc
->major
|| desc
->minor
||
1251 desc
->raid_disk
|| (desc
->state
&& (desc
->state
!= 4))) {
1252 printk(" D %2d: ", i
);
1256 printk(KERN_INFO
"md: THIS: ");
1257 print_desc(&sb
->this_disk
);
1261 static void print_rdev(mdk_rdev_t
*rdev
)
1263 char b
[BDEVNAME_SIZE
];
1264 printk(KERN_INFO
"md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1265 bdevname(rdev
->bdev
,b
), (unsigned long long)rdev
->size
,
1266 rdev
->faulty
, rdev
->in_sync
, rdev
->desc_nr
);
1267 if (rdev
->sb_loaded
) {
1268 printk(KERN_INFO
"md: rdev superblock:\n");
1269 print_sb((mdp_super_t
*)page_address(rdev
->sb_page
));
1271 printk(KERN_INFO
"md: no rdev superblock!\n");
1274 void md_print_devices(void)
1276 struct list_head
*tmp
, *tmp2
;
1279 char b
[BDEVNAME_SIZE
];
1282 printk("md: **********************************\n");
1283 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1284 printk("md: **********************************\n");
1285 ITERATE_MDDEV(mddev
,tmp
) {
1288 bitmap_print_sb(mddev
->bitmap
);
1290 printk("%s: ", mdname(mddev
));
1291 ITERATE_RDEV(mddev
,rdev
,tmp2
)
1292 printk("<%s>", bdevname(rdev
->bdev
,b
));
1295 ITERATE_RDEV(mddev
,rdev
,tmp2
)
1298 printk("md: **********************************\n");
1303 static void sync_sbs(mddev_t
* mddev
)
1306 struct list_head
*tmp
;
1308 ITERATE_RDEV(mddev
,rdev
,tmp
) {
1309 super_types
[mddev
->major_version
].
1310 sync_super(mddev
, rdev
);
1311 rdev
->sb_loaded
= 1;
1315 static void md_update_sb(mddev_t
* mddev
)
1318 struct list_head
*tmp
;
1323 spin_lock(&mddev
->write_lock
);
1324 sync_req
= mddev
->in_sync
;
1325 mddev
->utime
= get_seconds();
1328 if (!mddev
->events
) {
1330 * oops, this 64-bit counter should never wrap.
1331 * Either we are in around ~1 trillion A.C., assuming
1332 * 1 reboot per second, or we have a bug:
1337 mddev
->sb_dirty
= 2;
1341 * do not write anything to disk if using
1342 * nonpersistent superblocks
1344 if (!mddev
->persistent
) {
1345 mddev
->sb_dirty
= 0;
1346 spin_unlock(&mddev
->write_lock
);
1347 wake_up(&mddev
->sb_wait
);
1350 spin_unlock(&mddev
->write_lock
);
1353 "md: updating %s RAID superblock on device (in sync %d)\n",
1354 mdname(mddev
),mddev
->in_sync
);
1356 err
= bitmap_update_sb(mddev
->bitmap
);
1357 ITERATE_RDEV(mddev
,rdev
,tmp
) {
1358 char b
[BDEVNAME_SIZE
];
1359 dprintk(KERN_INFO
"md: ");
1361 dprintk("(skipping faulty ");
1363 dprintk("%s ", bdevname(rdev
->bdev
,b
));
1364 if (!rdev
->faulty
) {
1365 md_super_write(mddev
,rdev
,
1366 rdev
->sb_offset
<<1, MD_SB_BYTES
,
1368 dprintk(KERN_INFO
"(write) %s's sb offset: %llu\n",
1369 bdevname(rdev
->bdev
,b
),
1370 (unsigned long long)rdev
->sb_offset
);
1374 if (mddev
->level
== LEVEL_MULTIPATH
)
1375 /* only need to write one superblock... */
1378 wait_event(mddev
->sb_wait
, atomic_read(&mddev
->pending_writes
)==0);
1379 /* if there was a failure, sb_dirty was set to 1, and we re-write super */
1381 spin_lock(&mddev
->write_lock
);
1382 if (mddev
->in_sync
!= sync_req
|| mddev
->sb_dirty
== 1) {
1383 /* have to write it out again */
1384 spin_unlock(&mddev
->write_lock
);
1387 mddev
->sb_dirty
= 0;
1388 spin_unlock(&mddev
->write_lock
);
1389 wake_up(&mddev
->sb_wait
);
1394 * Import a device. If 'super_format' >= 0, then sanity check the superblock
1396 * mark the device faulty if:
1398 * - the device is nonexistent (zero size)
1399 * - the device has no valid superblock
1401 * a faulty rdev _never_ has rdev->sb set.
1403 static mdk_rdev_t
*md_import_device(dev_t newdev
, int super_format
, int super_minor
)
1405 char b
[BDEVNAME_SIZE
];
1410 rdev
= (mdk_rdev_t
*) kmalloc(sizeof(*rdev
), GFP_KERNEL
);
1412 printk(KERN_ERR
"md: could not alloc mem for new device!\n");
1413 return ERR_PTR(-ENOMEM
);
1415 memset(rdev
, 0, sizeof(*rdev
));
1417 if ((err
= alloc_disk_sb(rdev
)))
1420 err
= lock_rdev(rdev
, newdev
);
1427 rdev
->data_offset
= 0;
1428 atomic_set(&rdev
->nr_pending
, 0);
1430 size
= rdev
->bdev
->bd_inode
->i_size
>> BLOCK_SIZE_BITS
;
1433 "md: %s has zero or unknown size, marking faulty!\n",
1434 bdevname(rdev
->bdev
,b
));
1439 if (super_format
>= 0) {
1440 err
= super_types
[super_format
].
1441 load_super(rdev
, NULL
, super_minor
);
1442 if (err
== -EINVAL
) {
1444 "md: %s has invalid sb, not importing!\n",
1445 bdevname(rdev
->bdev
,b
));
1450 "md: could not read %s's sb, not importing!\n",
1451 bdevname(rdev
->bdev
,b
));
1455 INIT_LIST_HEAD(&rdev
->same_set
);
1460 if (rdev
->sb_page
) {
1466 return ERR_PTR(err
);
1470 * Check a full RAID array for plausibility
1474 static void analyze_sbs(mddev_t
* mddev
)
1477 struct list_head
*tmp
;
1478 mdk_rdev_t
*rdev
, *freshest
;
1479 char b
[BDEVNAME_SIZE
];
1482 ITERATE_RDEV(mddev
,rdev
,tmp
)
1483 switch (super_types
[mddev
->major_version
].
1484 load_super(rdev
, freshest
, mddev
->minor_version
)) {
1492 "md: fatal superblock inconsistency in %s"
1493 " -- removing from array\n",
1494 bdevname(rdev
->bdev
,b
));
1495 kick_rdev_from_array(rdev
);
1499 super_types
[mddev
->major_version
].
1500 validate_super(mddev
, freshest
);
1503 ITERATE_RDEV(mddev
,rdev
,tmp
) {
1504 if (rdev
!= freshest
)
1505 if (super_types
[mddev
->major_version
].
1506 validate_super(mddev
, rdev
)) {
1507 printk(KERN_WARNING
"md: kicking non-fresh %s"
1509 bdevname(rdev
->bdev
,b
));
1510 kick_rdev_from_array(rdev
);
1513 if (mddev
->level
== LEVEL_MULTIPATH
) {
1514 rdev
->desc_nr
= i
++;
1515 rdev
->raid_disk
= rdev
->desc_nr
;
1522 if (mddev
->recovery_cp
!= MaxSector
&&
1524 printk(KERN_ERR
"md: %s: raid array is not clean"
1525 " -- starting background reconstruction\n",
1532 static struct kobject
*md_probe(dev_t dev
, int *part
, void *data
)
1534 static DECLARE_MUTEX(disks_sem
);
1535 mddev_t
*mddev
= mddev_find(dev
);
1536 struct gendisk
*disk
;
1537 int partitioned
= (MAJOR(dev
) != MD_MAJOR
);
1538 int shift
= partitioned
? MdpMinorShift
: 0;
1539 int unit
= MINOR(dev
) >> shift
;
1545 if (mddev
->gendisk
) {
1550 disk
= alloc_disk(1 << shift
);
1556 disk
->major
= MAJOR(dev
);
1557 disk
->first_minor
= unit
<< shift
;
1559 sprintf(disk
->disk_name
, "md_d%d", unit
);
1560 sprintf(disk
->devfs_name
, "md/d%d", unit
);
1562 sprintf(disk
->disk_name
, "md%d", unit
);
1563 sprintf(disk
->devfs_name
, "md/%d", unit
);
1565 disk
->fops
= &md_fops
;
1566 disk
->private_data
= mddev
;
1567 disk
->queue
= mddev
->queue
;
1569 mddev
->gendisk
= disk
;
1574 void md_wakeup_thread(mdk_thread_t
*thread
);
1576 static void md_safemode_timeout(unsigned long data
)
1578 mddev_t
*mddev
= (mddev_t
*) data
;
1580 mddev
->safemode
= 1;
1581 md_wakeup_thread(mddev
->thread
);
1585 static int do_md_run(mddev_t
* mddev
)
1589 struct list_head
*tmp
;
1591 struct gendisk
*disk
;
1592 char b
[BDEVNAME_SIZE
];
1594 if (list_empty(&mddev
->disks
))
1595 /* cannot run an array with no devices.. */
1602 * Analyze all RAID superblock(s)
1604 if (!mddev
->raid_disks
)
1607 chunk_size
= mddev
->chunk_size
;
1608 pnum
= level_to_pers(mddev
->level
);
1610 if ((pnum
!= MULTIPATH
) && (pnum
!= RAID1
)) {
1613 * 'default chunksize' in the old md code used to
1614 * be PAGE_SIZE, baaad.
1615 * we abort here to be on the safe side. We don't
1616 * want to continue the bad practice.
1619 "no chunksize specified, see 'man raidtab'\n");
1622 if (chunk_size
> MAX_CHUNK_SIZE
) {
1623 printk(KERN_ERR
"too big chunk_size: %d > %d\n",
1624 chunk_size
, MAX_CHUNK_SIZE
);
1628 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
1630 if ( (1 << ffz(~chunk_size
)) != chunk_size
) {
1631 printk(KERN_ERR
"chunk_size of %d not valid\n", chunk_size
);
1634 if (chunk_size
< PAGE_SIZE
) {
1635 printk(KERN_ERR
"too small chunk_size: %d < %ld\n",
1636 chunk_size
, PAGE_SIZE
);
1640 /* devices must have minimum size of one chunk */
1641 ITERATE_RDEV(mddev
,rdev
,tmp
) {
1644 if (rdev
->size
< chunk_size
/ 1024) {
1646 "md: Dev %s smaller than chunk_size:"
1648 bdevname(rdev
->bdev
,b
),
1649 (unsigned long long)rdev
->size
,
1659 request_module("md-personality-%d", pnum
);
1664 * Drop all container device buffers, from now on
1665 * the only valid external interface is through the md
1667 * Also find largest hardsector size
1669 ITERATE_RDEV(mddev
,rdev
,tmp
) {
1672 sync_blockdev(rdev
->bdev
);
1673 invalidate_bdev(rdev
->bdev
, 0);
1676 md_probe(mddev
->unit
, NULL
, NULL
);
1677 disk
= mddev
->gendisk
;
1681 spin_lock(&pers_lock
);
1682 if (!pers
[pnum
] || !try_module_get(pers
[pnum
]->owner
)) {
1683 spin_unlock(&pers_lock
);
1684 printk(KERN_WARNING
"md: personality %d is not loaded!\n",
1689 mddev
->pers
= pers
[pnum
];
1690 spin_unlock(&pers_lock
);
1692 mddev
->recovery
= 0;
1693 mddev
->resync_max_sectors
= mddev
->size
<< 1; /* may be over-ridden by personality */
1695 /* before we start the array running, initialise the bitmap */
1696 err
= bitmap_create(mddev
);
1698 printk(KERN_ERR
"%s: failed to create bitmap (%d)\n",
1699 mdname(mddev
), err
);
1701 err
= mddev
->pers
->run(mddev
);
1703 printk(KERN_ERR
"md: pers->run() failed ...\n");
1704 module_put(mddev
->pers
->owner
);
1706 bitmap_destroy(mddev
);
1709 atomic_set(&mddev
->writes_pending
,0);
1710 mddev
->safemode
= 0;
1711 mddev
->safemode_timer
.function
= md_safemode_timeout
;
1712 mddev
->safemode_timer
.data
= (unsigned long) mddev
;
1713 mddev
->safemode_delay
= (20 * HZ
)/1000 +1; /* 20 msec delay */
1716 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
1717 md_wakeup_thread(mddev
->thread
);
1719 if (mddev
->sb_dirty
)
1720 md_update_sb(mddev
);
1722 set_capacity(disk
, mddev
->array_size
<<1);
1724 /* If we call blk_queue_make_request here, it will
1725 * re-initialise max_sectors etc which may have been
1726 * refined inside -> run. So just set the bits we need to set.
1727 * Most initialisation happended when we called
1728 * blk_queue_make_request(..., md_fail_request)
1731 mddev
->queue
->queuedata
= mddev
;
1732 mddev
->queue
->make_request_fn
= mddev
->pers
->make_request
;
1738 static int restart_array(mddev_t
*mddev
)
1740 struct gendisk
*disk
= mddev
->gendisk
;
1744 * Complain if it has no devices
1747 if (list_empty(&mddev
->disks
))
1755 mddev
->safemode
= 0;
1757 set_disk_ro(disk
, 0);
1759 printk(KERN_INFO
"md: %s switched to read-write mode.\n",
1762 * Kick recovery or resync if necessary
1764 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
1765 md_wakeup_thread(mddev
->thread
);
1768 printk(KERN_ERR
"md: %s has no personality assigned.\n",
1777 static int do_md_stop(mddev_t
* mddev
, int ro
)
1780 struct gendisk
*disk
= mddev
->gendisk
;
1783 if (atomic_read(&mddev
->active
)>2) {
1784 printk("md: %s still in use.\n",mdname(mddev
));
1788 if (mddev
->sync_thread
) {
1789 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
1790 md_unregister_thread(mddev
->sync_thread
);
1791 mddev
->sync_thread
= NULL
;
1794 del_timer_sync(&mddev
->safemode_timer
);
1796 invalidate_partition(disk
, 0);
1804 bitmap_flush(mddev
);
1805 wait_event(mddev
->sb_wait
, atomic_read(&mddev
->pending_writes
)==0);
1807 set_disk_ro(disk
, 0);
1808 blk_queue_make_request(mddev
->queue
, md_fail_request
);
1809 mddev
->pers
->stop(mddev
);
1810 module_put(mddev
->pers
->owner
);
1815 if (!mddev
->in_sync
) {
1816 /* mark array as shutdown cleanly */
1818 md_update_sb(mddev
);
1821 set_disk_ro(disk
, 1);
1824 bitmap_destroy(mddev
);
1825 if (mddev
->bitmap_file
) {
1826 atomic_set(&mddev
->bitmap_file
->f_dentry
->d_inode
->i_writecount
, 1);
1827 fput(mddev
->bitmap_file
);
1828 mddev
->bitmap_file
= NULL
;
1830 mddev
->bitmap_offset
= 0;
1833 * Free resources if final stop
1836 struct gendisk
*disk
;
1837 printk(KERN_INFO
"md: %s stopped.\n", mdname(mddev
));
1839 export_array(mddev
);
1841 mddev
->array_size
= 0;
1842 disk
= mddev
->gendisk
;
1844 set_capacity(disk
, 0);
1847 printk(KERN_INFO
"md: %s switched to read-only mode.\n",
1854 static void autorun_array(mddev_t
*mddev
)
1857 struct list_head
*tmp
;
1860 if (list_empty(&mddev
->disks
))
1863 printk(KERN_INFO
"md: running: ");
1865 ITERATE_RDEV(mddev
,rdev
,tmp
) {
1866 char b
[BDEVNAME_SIZE
];
1867 printk("<%s>", bdevname(rdev
->bdev
,b
));
1871 err
= do_md_run (mddev
);
1873 printk(KERN_WARNING
"md: do_md_run() returned %d\n", err
);
1874 do_md_stop (mddev
, 0);
1879 * lets try to run arrays based on all disks that have arrived
1880 * until now. (those are in pending_raid_disks)
1882 * the method: pick the first pending disk, collect all disks with
1883 * the same UUID, remove all from the pending list and put them into
1884 * the 'same_array' list. Then order this list based on superblock
1885 * update time (freshest comes first), kick out 'old' disks and
1886 * compare superblocks. If everything's fine then run it.
1888 * If "unit" is allocated, then bump its reference count
1890 static void autorun_devices(int part
)
1892 struct list_head candidates
;
1893 struct list_head
*tmp
;
1894 mdk_rdev_t
*rdev0
, *rdev
;
1896 char b
[BDEVNAME_SIZE
];
1898 printk(KERN_INFO
"md: autorun ...\n");
1899 while (!list_empty(&pending_raid_disks
)) {
1901 rdev0
= list_entry(pending_raid_disks
.next
,
1902 mdk_rdev_t
, same_set
);
1904 printk(KERN_INFO
"md: considering %s ...\n",
1905 bdevname(rdev0
->bdev
,b
));
1906 INIT_LIST_HEAD(&candidates
);
1907 ITERATE_RDEV_PENDING(rdev
,tmp
)
1908 if (super_90_load(rdev
, rdev0
, 0) >= 0) {
1909 printk(KERN_INFO
"md: adding %s ...\n",
1910 bdevname(rdev
->bdev
,b
));
1911 list_move(&rdev
->same_set
, &candidates
);
1914 * now we have a set of devices, with all of them having
1915 * mostly sane superblocks. It's time to allocate the
1918 if (rdev0
->preferred_minor
< 0 || rdev0
->preferred_minor
>= MAX_MD_DEVS
) {
1919 printk(KERN_INFO
"md: unit number in %s is bad: %d\n",
1920 bdevname(rdev0
->bdev
, b
), rdev0
->preferred_minor
);
1924 dev
= MKDEV(mdp_major
,
1925 rdev0
->preferred_minor
<< MdpMinorShift
);
1927 dev
= MKDEV(MD_MAJOR
, rdev0
->preferred_minor
);
1929 md_probe(dev
, NULL
, NULL
);
1930 mddev
= mddev_find(dev
);
1933 "md: cannot allocate memory for md drive.\n");
1936 if (mddev_lock(mddev
))
1937 printk(KERN_WARNING
"md: %s locked, cannot run\n",
1939 else if (mddev
->raid_disks
|| mddev
->major_version
1940 || !list_empty(&mddev
->disks
)) {
1942 "md: %s already running, cannot run %s\n",
1943 mdname(mddev
), bdevname(rdev0
->bdev
,b
));
1944 mddev_unlock(mddev
);
1946 printk(KERN_INFO
"md: created %s\n", mdname(mddev
));
1947 ITERATE_RDEV_GENERIC(candidates
,rdev
,tmp
) {
1948 list_del_init(&rdev
->same_set
);
1949 if (bind_rdev_to_array(rdev
, mddev
))
1952 autorun_array(mddev
);
1953 mddev_unlock(mddev
);
1955 /* on success, candidates will be empty, on error
1958 ITERATE_RDEV_GENERIC(candidates
,rdev
,tmp
)
1962 printk(KERN_INFO
"md: ... autorun DONE.\n");
1966 * import RAID devices based on one partition
1967 * if possible, the array gets run as well.
1970 static int autostart_array(dev_t startdev
)
1972 char b
[BDEVNAME_SIZE
];
1973 int err
= -EINVAL
, i
;
1974 mdp_super_t
*sb
= NULL
;
1975 mdk_rdev_t
*start_rdev
= NULL
, *rdev
;
1977 start_rdev
= md_import_device(startdev
, 0, 0);
1978 if (IS_ERR(start_rdev
))
1982 /* NOTE: this can only work for 0.90.0 superblocks */
1983 sb
= (mdp_super_t
*)page_address(start_rdev
->sb_page
);
1984 if (sb
->major_version
!= 0 ||
1985 sb
->minor_version
!= 90 ) {
1986 printk(KERN_WARNING
"md: can only autostart 0.90.0 arrays\n");
1987 export_rdev(start_rdev
);
1991 if (start_rdev
->faulty
) {
1993 "md: can not autostart based on faulty %s!\n",
1994 bdevname(start_rdev
->bdev
,b
));
1995 export_rdev(start_rdev
);
1998 list_add(&start_rdev
->same_set
, &pending_raid_disks
);
2000 for (i
= 0; i
< MD_SB_DISKS
; i
++) {
2001 mdp_disk_t
*desc
= sb
->disks
+ i
;
2002 dev_t dev
= MKDEV(desc
->major
, desc
->minor
);
2006 if (dev
== startdev
)
2008 if (MAJOR(dev
) != desc
->major
|| MINOR(dev
) != desc
->minor
)
2010 rdev
= md_import_device(dev
, 0, 0);
2014 list_add(&rdev
->same_set
, &pending_raid_disks
);
2018 * possibly return codes
2026 static int get_version(void __user
* arg
)
2030 ver
.major
= MD_MAJOR_VERSION
;
2031 ver
.minor
= MD_MINOR_VERSION
;
2032 ver
.patchlevel
= MD_PATCHLEVEL_VERSION
;
2034 if (copy_to_user(arg
, &ver
, sizeof(ver
)))
2040 static int get_array_info(mddev_t
* mddev
, void __user
* arg
)
2042 mdu_array_info_t info
;
2043 int nr
,working
,active
,failed
,spare
;
2045 struct list_head
*tmp
;
2047 nr
=working
=active
=failed
=spare
=0;
2048 ITERATE_RDEV(mddev
,rdev
,tmp
) {
2061 info
.major_version
= mddev
->major_version
;
2062 info
.minor_version
= mddev
->minor_version
;
2063 info
.patch_version
= MD_PATCHLEVEL_VERSION
;
2064 info
.ctime
= mddev
->ctime
;
2065 info
.level
= mddev
->level
;
2066 info
.size
= mddev
->size
;
2068 info
.raid_disks
= mddev
->raid_disks
;
2069 info
.md_minor
= mddev
->md_minor
;
2070 info
.not_persistent
= !mddev
->persistent
;
2072 info
.utime
= mddev
->utime
;
2075 info
.state
= (1<<MD_SB_CLEAN
);
2076 info
.active_disks
= active
;
2077 info
.working_disks
= working
;
2078 info
.failed_disks
= failed
;
2079 info
.spare_disks
= spare
;
2081 info
.layout
= mddev
->layout
;
2082 info
.chunk_size
= mddev
->chunk_size
;
2084 if (copy_to_user(arg
, &info
, sizeof(info
)))
2090 static int get_bitmap_file(mddev_t
* mddev
, void * arg
)
2092 mdu_bitmap_file_t
*file
= NULL
; /* too big for stack allocation */
2093 char *ptr
, *buf
= NULL
;
2096 file
= kmalloc(sizeof(*file
), GFP_KERNEL
);
2100 /* bitmap disabled, zero the first byte and copy out */
2101 if (!mddev
->bitmap
|| !mddev
->bitmap
->file
) {
2102 file
->pathname
[0] = '\0';
2106 buf
= kmalloc(sizeof(file
->pathname
), GFP_KERNEL
);
2110 ptr
= file_path(mddev
->bitmap
->file
, buf
, sizeof(file
->pathname
));
2114 strcpy(file
->pathname
, ptr
);
2118 if (copy_to_user(arg
, file
, sizeof(*file
)))
2126 static int get_disk_info(mddev_t
* mddev
, void __user
* arg
)
2128 mdu_disk_info_t info
;
2132 if (copy_from_user(&info
, arg
, sizeof(info
)))
2137 rdev
= find_rdev_nr(mddev
, nr
);
2139 info
.major
= MAJOR(rdev
->bdev
->bd_dev
);
2140 info
.minor
= MINOR(rdev
->bdev
->bd_dev
);
2141 info
.raid_disk
= rdev
->raid_disk
;
2144 info
.state
|= (1<<MD_DISK_FAULTY
);
2145 else if (rdev
->in_sync
) {
2146 info
.state
|= (1<<MD_DISK_ACTIVE
);
2147 info
.state
|= (1<<MD_DISK_SYNC
);
2150 info
.major
= info
.minor
= 0;
2151 info
.raid_disk
= -1;
2152 info
.state
= (1<<MD_DISK_REMOVED
);
2155 if (copy_to_user(arg
, &info
, sizeof(info
)))
2161 static int add_new_disk(mddev_t
* mddev
, mdu_disk_info_t
*info
)
2163 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
2165 dev_t dev
= MKDEV(info
->major
,info
->minor
);
2167 if (info
->major
!= MAJOR(dev
) || info
->minor
!= MINOR(dev
))
2170 if (!mddev
->raid_disks
) {
2172 /* expecting a device which has a superblock */
2173 rdev
= md_import_device(dev
, mddev
->major_version
, mddev
->minor_version
);
2176 "md: md_import_device returned %ld\n",
2178 return PTR_ERR(rdev
);
2180 if (!list_empty(&mddev
->disks
)) {
2181 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
2182 mdk_rdev_t
, same_set
);
2183 int err
= super_types
[mddev
->major_version
]
2184 .load_super(rdev
, rdev0
, mddev
->minor_version
);
2187 "md: %s has different UUID to %s\n",
2188 bdevname(rdev
->bdev
,b
),
2189 bdevname(rdev0
->bdev
,b2
));
2194 err
= bind_rdev_to_array(rdev
, mddev
);
2201 * add_new_disk can be used once the array is assembled
2202 * to add "hot spares". They must already have a superblock
2207 if (!mddev
->pers
->hot_add_disk
) {
2209 "%s: personality does not support diskops!\n",
2213 rdev
= md_import_device(dev
, mddev
->major_version
,
2214 mddev
->minor_version
);
2217 "md: md_import_device returned %ld\n",
2219 return PTR_ERR(rdev
);
2221 /* set save_raid_disk if appropriate */
2222 if (!mddev
->persistent
) {
2223 if (info
->state
& (1<<MD_DISK_SYNC
) &&
2224 info
->raid_disk
< mddev
->raid_disks
)
2225 rdev
->raid_disk
= info
->raid_disk
;
2227 rdev
->raid_disk
= -1;
2229 super_types
[mddev
->major_version
].
2230 validate_super(mddev
, rdev
);
2231 rdev
->saved_raid_disk
= rdev
->raid_disk
;
2233 rdev
->in_sync
= 0; /* just to be sure */
2234 rdev
->raid_disk
= -1;
2235 err
= bind_rdev_to_array(rdev
, mddev
);
2239 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
2240 md_wakeup_thread(mddev
->thread
);
2244 /* otherwise, add_new_disk is only allowed
2245 * for major_version==0 superblocks
2247 if (mddev
->major_version
!= 0) {
2248 printk(KERN_WARNING
"%s: ADD_NEW_DISK not supported\n",
2253 if (!(info
->state
& (1<<MD_DISK_FAULTY
))) {
2255 rdev
= md_import_device (dev
, -1, 0);
2258 "md: error, md_import_device() returned %ld\n",
2260 return PTR_ERR(rdev
);
2262 rdev
->desc_nr
= info
->number
;
2263 if (info
->raid_disk
< mddev
->raid_disks
)
2264 rdev
->raid_disk
= info
->raid_disk
;
2266 rdev
->raid_disk
= -1;
2269 if (rdev
->raid_disk
< mddev
->raid_disks
)
2270 rdev
->in_sync
= (info
->state
& (1<<MD_DISK_SYNC
));
2274 err
= bind_rdev_to_array(rdev
, mddev
);
2280 if (!mddev
->persistent
) {
2281 printk(KERN_INFO
"md: nonpersistent superblock ...\n");
2282 rdev
->sb_offset
= rdev
->bdev
->bd_inode
->i_size
>> BLOCK_SIZE_BITS
;
2284 rdev
->sb_offset
= calc_dev_sboffset(rdev
->bdev
);
2285 rdev
->size
= calc_dev_size(rdev
, mddev
->chunk_size
);
2287 if (!mddev
->size
|| (mddev
->size
> rdev
->size
))
2288 mddev
->size
= rdev
->size
;
2294 static int hot_remove_disk(mddev_t
* mddev
, dev_t dev
)
2296 char b
[BDEVNAME_SIZE
];
2302 rdev
= find_rdev(mddev
, dev
);
2306 if (rdev
->raid_disk
>= 0)
2309 kick_rdev_from_array(rdev
);
2310 md_update_sb(mddev
);
2314 printk(KERN_WARNING
"md: cannot remove active disk %s from %s ... \n",
2315 bdevname(rdev
->bdev
,b
), mdname(mddev
));
2319 static int hot_add_disk(mddev_t
* mddev
, dev_t dev
)
2321 char b
[BDEVNAME_SIZE
];
2329 if (mddev
->major_version
!= 0) {
2330 printk(KERN_WARNING
"%s: HOT_ADD may only be used with"
2331 " version-0 superblocks.\n",
2335 if (!mddev
->pers
->hot_add_disk
) {
2337 "%s: personality does not support diskops!\n",
2342 rdev
= md_import_device (dev
, -1, 0);
2345 "md: error, md_import_device() returned %ld\n",
2350 if (mddev
->persistent
)
2351 rdev
->sb_offset
= calc_dev_sboffset(rdev
->bdev
);
2354 rdev
->bdev
->bd_inode
->i_size
>> BLOCK_SIZE_BITS
;
2356 size
= calc_dev_size(rdev
, mddev
->chunk_size
);
2359 if (size
< mddev
->size
) {
2361 "%s: disk size %llu blocks < array size %llu\n",
2362 mdname(mddev
), (unsigned long long)size
,
2363 (unsigned long long)mddev
->size
);
2370 "md: can not hot-add faulty %s disk to %s!\n",
2371 bdevname(rdev
->bdev
,b
), mdname(mddev
));
2377 bind_rdev_to_array(rdev
, mddev
);
2380 * The rest should better be atomic, we can have disk failures
2381 * noticed in interrupt contexts ...
2384 if (rdev
->desc_nr
== mddev
->max_disks
) {
2385 printk(KERN_WARNING
"%s: can not hot-add to full array!\n",
2388 goto abort_unbind_export
;
2391 rdev
->raid_disk
= -1;
2393 md_update_sb(mddev
);
2396 * Kick recovery, maybe this spare has to be added to the
2397 * array immediately.
2399 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
2400 md_wakeup_thread(mddev
->thread
);
2404 abort_unbind_export
:
2405 unbind_rdev_from_array(rdev
);
2412 /* similar to deny_write_access, but accounts for our holding a reference
2413 * to the file ourselves */
2414 static int deny_bitmap_write_access(struct file
* file
)
2416 struct inode
*inode
= file
->f_mapping
->host
;
2418 spin_lock(&inode
->i_lock
);
2419 if (atomic_read(&inode
->i_writecount
) > 1) {
2420 spin_unlock(&inode
->i_lock
);
2423 atomic_set(&inode
->i_writecount
, -1);
2424 spin_unlock(&inode
->i_lock
);
2429 static int set_bitmap_file(mddev_t
*mddev
, int fd
)
2436 mddev
->bitmap_file
= fget(fd
);
2438 if (mddev
->bitmap_file
== NULL
) {
2439 printk(KERN_ERR
"%s: error: failed to get bitmap file\n",
2444 err
= deny_bitmap_write_access(mddev
->bitmap_file
);
2446 printk(KERN_ERR
"%s: error: bitmap file is already in use\n",
2448 fput(mddev
->bitmap_file
);
2449 mddev
->bitmap_file
= NULL
;
2451 mddev
->bitmap_offset
= 0; /* file overrides offset */
2456 * set_array_info is used two different ways
2457 * The original usage is when creating a new array.
2458 * In this usage, raid_disks is > 0 and it together with
2459 * level, size, not_persistent,layout,chunksize determine the
2460 * shape of the array.
2461 * This will always create an array with a type-0.90.0 superblock.
2462 * The newer usage is when assembling an array.
2463 * In this case raid_disks will be 0, and the major_version field is
2464 * use to determine which style super-blocks are to be found on the devices.
2465 * The minor and patch _version numbers are also kept incase the
2466 * super_block handler wishes to interpret them.
2468 static int set_array_info(mddev_t
* mddev
, mdu_array_info_t
*info
)
2471 if (info
->raid_disks
== 0) {
2472 /* just setting version number for superblock loading */
2473 if (info
->major_version
< 0 ||
2474 info
->major_version
>= sizeof(super_types
)/sizeof(super_types
[0]) ||
2475 super_types
[info
->major_version
].name
== NULL
) {
2476 /* maybe try to auto-load a module? */
2478 "md: superblock version %d not known\n",
2479 info
->major_version
);
2482 mddev
->major_version
= info
->major_version
;
2483 mddev
->minor_version
= info
->minor_version
;
2484 mddev
->patch_version
= info
->patch_version
;
2487 mddev
->major_version
= MD_MAJOR_VERSION
;
2488 mddev
->minor_version
= MD_MINOR_VERSION
;
2489 mddev
->patch_version
= MD_PATCHLEVEL_VERSION
;
2490 mddev
->ctime
= get_seconds();
2492 mddev
->level
= info
->level
;
2493 mddev
->size
= info
->size
;
2494 mddev
->raid_disks
= info
->raid_disks
;
2495 /* don't set md_minor, it is determined by which /dev/md* was
2498 if (info
->state
& (1<<MD_SB_CLEAN
))
2499 mddev
->recovery_cp
= MaxSector
;
2501 mddev
->recovery_cp
= 0;
2502 mddev
->persistent
= ! info
->not_persistent
;
2504 mddev
->layout
= info
->layout
;
2505 mddev
->chunk_size
= info
->chunk_size
;
2507 mddev
->max_disks
= MD_SB_DISKS
;
2509 mddev
->sb_dirty
= 1;
2512 * Generate a 128 bit UUID
2514 get_random_bytes(mddev
->uuid
, 16);
2520 * update_array_info is used to change the configuration of an
2522 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
2523 * fields in the info are checked against the array.
2524 * Any differences that cannot be handled will cause an error.
2525 * Normally, only one change can be managed at a time.
2527 static int update_array_info(mddev_t
*mddev
, mdu_array_info_t
*info
)
2532 if (mddev
->major_version
!= info
->major_version
||
2533 mddev
->minor_version
!= info
->minor_version
||
2534 /* mddev->patch_version != info->patch_version || */
2535 mddev
->ctime
!= info
->ctime
||
2536 mddev
->level
!= info
->level
||
2537 /* mddev->layout != info->layout || */
2538 !mddev
->persistent
!= info
->not_persistent
||
2539 mddev
->chunk_size
!= info
->chunk_size
)
2541 /* Check there is only one change */
2542 if (mddev
->size
!= info
->size
) cnt
++;
2543 if (mddev
->raid_disks
!= info
->raid_disks
) cnt
++;
2544 if (mddev
->layout
!= info
->layout
) cnt
++;
2545 if (cnt
== 0) return 0;
2546 if (cnt
> 1) return -EINVAL
;
2548 if (mddev
->layout
!= info
->layout
) {
2550 * we don't need to do anything at the md level, the
2551 * personality will take care of it all.
2553 if (mddev
->pers
->reconfig
== NULL
)
2556 return mddev
->pers
->reconfig(mddev
, info
->layout
, -1);
2558 if (mddev
->size
!= info
->size
) {
2560 struct list_head
*tmp
;
2561 if (mddev
->pers
->resize
== NULL
)
2563 /* The "size" is the amount of each device that is used.
2564 * This can only make sense for arrays with redundancy.
2565 * linear and raid0 always use whatever space is available
2566 * We can only consider changing the size if no resync
2567 * or reconstruction is happening, and if the new size
2568 * is acceptable. It must fit before the sb_offset or,
2569 * if that is <data_offset, it must fit before the
2570 * size of each device.
2571 * If size is zero, we find the largest size that fits.
2573 if (mddev
->sync_thread
)
2575 ITERATE_RDEV(mddev
,rdev
,tmp
) {
2577 int fit
= (info
->size
== 0);
2578 if (rdev
->sb_offset
> rdev
->data_offset
)
2579 avail
= (rdev
->sb_offset
*2) - rdev
->data_offset
;
2581 avail
= get_capacity(rdev
->bdev
->bd_disk
)
2582 - rdev
->data_offset
;
2583 if (fit
&& (info
->size
== 0 || info
->size
> avail
/2))
2584 info
->size
= avail
/2;
2585 if (avail
< ((sector_t
)info
->size
<< 1))
2588 rv
= mddev
->pers
->resize(mddev
, (sector_t
)info
->size
*2);
2590 struct block_device
*bdev
;
2592 bdev
= bdget_disk(mddev
->gendisk
, 0);
2594 down(&bdev
->bd_inode
->i_sem
);
2595 i_size_write(bdev
->bd_inode
, mddev
->array_size
<< 10);
2596 up(&bdev
->bd_inode
->i_sem
);
2601 if (mddev
->raid_disks
!= info
->raid_disks
) {
2602 /* change the number of raid disks */
2603 if (mddev
->pers
->reshape
== NULL
)
2605 if (info
->raid_disks
<= 0 ||
2606 info
->raid_disks
>= mddev
->max_disks
)
2608 if (mddev
->sync_thread
)
2610 rv
= mddev
->pers
->reshape(mddev
, info
->raid_disks
);
2612 struct block_device
*bdev
;
2614 bdev
= bdget_disk(mddev
->gendisk
, 0);
2616 down(&bdev
->bd_inode
->i_sem
);
2617 i_size_write(bdev
->bd_inode
, mddev
->array_size
<< 10);
2618 up(&bdev
->bd_inode
->i_sem
);
2623 md_update_sb(mddev
);
2627 static int set_disk_faulty(mddev_t
*mddev
, dev_t dev
)
2631 if (mddev
->pers
== NULL
)
2634 rdev
= find_rdev(mddev
, dev
);
2638 md_error(mddev
, rdev
);
2642 static int md_ioctl(struct inode
*inode
, struct file
*file
,
2643 unsigned int cmd
, unsigned long arg
)
2646 void __user
*argp
= (void __user
*)arg
;
2647 struct hd_geometry __user
*loc
= argp
;
2648 mddev_t
*mddev
= NULL
;
2650 if (!capable(CAP_SYS_ADMIN
))
2654 * Commands dealing with the RAID driver but not any
2660 err
= get_version(argp
);
2663 case PRINT_RAID_DEBUG
:
2671 autostart_arrays(arg
);
2678 * Commands creating/starting a new array:
2681 mddev
= inode
->i_bdev
->bd_disk
->private_data
;
2689 if (cmd
== START_ARRAY
) {
2690 /* START_ARRAY doesn't need to lock the array as autostart_array
2691 * does the locking, and it could even be a different array
2696 "md: %s(pid %d) used deprecated START_ARRAY ioctl. "
2697 "This will not be supported beyond 2.6\n",
2698 current
->comm
, current
->pid
);
2701 err
= autostart_array(new_decode_dev(arg
));
2703 printk(KERN_WARNING
"md: autostart failed!\n");
2709 err
= mddev_lock(mddev
);
2712 "md: ioctl lock interrupted, reason %d, cmd %d\n",
2719 case SET_ARRAY_INFO
:
2721 mdu_array_info_t info
;
2723 memset(&info
, 0, sizeof(info
));
2724 else if (copy_from_user(&info
, argp
, sizeof(info
))) {
2729 err
= update_array_info(mddev
, &info
);
2731 printk(KERN_WARNING
"md: couldn't update"
2732 " array info. %d\n", err
);
2737 if (!list_empty(&mddev
->disks
)) {
2739 "md: array %s already has disks!\n",
2744 if (mddev
->raid_disks
) {
2746 "md: array %s already initialised!\n",
2751 err
= set_array_info(mddev
, &info
);
2753 printk(KERN_WARNING
"md: couldn't set"
2754 " array info. %d\n", err
);
2764 * Commands querying/configuring an existing array:
2766 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
2767 * RUN_ARRAY, and SET_BITMAP_FILE are allowed */
2768 if (!mddev
->raid_disks
&& cmd
!= ADD_NEW_DISK
&& cmd
!= STOP_ARRAY
2769 && cmd
!= RUN_ARRAY
&& cmd
!= SET_BITMAP_FILE
) {
2775 * Commands even a read-only array can execute:
2779 case GET_ARRAY_INFO
:
2780 err
= get_array_info(mddev
, argp
);
2783 case GET_BITMAP_FILE
:
2784 err
= get_bitmap_file(mddev
, (void *)arg
);
2788 err
= get_disk_info(mddev
, argp
);
2791 case RESTART_ARRAY_RW
:
2792 err
= restart_array(mddev
);
2796 err
= do_md_stop (mddev
, 0);
2800 err
= do_md_stop (mddev
, 1);
2804 * We have a problem here : there is no easy way to give a CHS
2805 * virtual geometry. We currently pretend that we have a 2 heads
2806 * 4 sectors (with a BIG number of cylinders...). This drives
2807 * dosfs just mad... ;-)
2814 err
= put_user (2, (char __user
*) &loc
->heads
);
2817 err
= put_user (4, (char __user
*) &loc
->sectors
);
2820 err
= put_user(get_capacity(mddev
->gendisk
)/8,
2821 (short __user
*) &loc
->cylinders
);
2824 err
= put_user (get_start_sect(inode
->i_bdev
),
2825 (long __user
*) &loc
->start
);
2830 * The remaining ioctls are changing the state of the
2831 * superblock, so we do not allow read-only arrays
2843 mdu_disk_info_t info
;
2844 if (copy_from_user(&info
, argp
, sizeof(info
)))
2847 err
= add_new_disk(mddev
, &info
);
2851 case HOT_REMOVE_DISK
:
2852 err
= hot_remove_disk(mddev
, new_decode_dev(arg
));
2856 err
= hot_add_disk(mddev
, new_decode_dev(arg
));
2859 case SET_DISK_FAULTY
:
2860 err
= set_disk_faulty(mddev
, new_decode_dev(arg
));
2864 err
= do_md_run (mddev
);
2867 case SET_BITMAP_FILE
:
2868 err
= set_bitmap_file(mddev
, (int)arg
);
2872 if (_IOC_TYPE(cmd
) == MD_MAJOR
)
2873 printk(KERN_WARNING
"md: %s(pid %d) used"
2874 " obsolete MD ioctl, upgrade your"
2875 " software to use new ictls.\n",
2876 current
->comm
, current
->pid
);
2883 mddev_unlock(mddev
);
2893 static int md_open(struct inode
*inode
, struct file
*file
)
2896 * Succeed if we can lock the mddev, which confirms that
2897 * it isn't being stopped right now.
2899 mddev_t
*mddev
= inode
->i_bdev
->bd_disk
->private_data
;
2902 if ((err
= mddev_lock(mddev
)))
2907 mddev_unlock(mddev
);
2909 check_disk_change(inode
->i_bdev
);
2914 static int md_release(struct inode
*inode
, struct file
* file
)
2916 mddev_t
*mddev
= inode
->i_bdev
->bd_disk
->private_data
;
2925 static int md_media_changed(struct gendisk
*disk
)
2927 mddev_t
*mddev
= disk
->private_data
;
2929 return mddev
->changed
;
2932 static int md_revalidate(struct gendisk
*disk
)
2934 mddev_t
*mddev
= disk
->private_data
;
2939 static struct block_device_operations md_fops
=
2941 .owner
= THIS_MODULE
,
2943 .release
= md_release
,
2945 .media_changed
= md_media_changed
,
2946 .revalidate_disk
= md_revalidate
,
2949 static int md_thread(void * arg
)
2951 mdk_thread_t
*thread
= arg
;
2959 daemonize(thread
->name
, mdname(thread
->mddev
));
2961 current
->exit_signal
= SIGCHLD
;
2962 allow_signal(SIGKILL
);
2963 thread
->tsk
= current
;
2966 * md_thread is a 'system-thread', it's priority should be very
2967 * high. We avoid resource deadlocks individually in each
2968 * raid personality. (RAID5 does preallocation) We also use RR and
2969 * the very same RT priority as kswapd, thus we will never get
2970 * into a priority inversion deadlock.
2972 * we definitely have to have equal or higher priority than
2973 * bdflush, otherwise bdflush will deadlock if there are too
2974 * many dirty RAID5 blocks.
2978 complete(thread
->event
);
2979 while (thread
->run
) {
2980 void (*run
)(mddev_t
*);
2982 wait_event_interruptible_timeout(thread
->wqueue
,
2983 test_bit(THREAD_WAKEUP
, &thread
->flags
),
2987 clear_bit(THREAD_WAKEUP
, &thread
->flags
);
2993 if (signal_pending(current
))
2994 flush_signals(current
);
2996 complete(thread
->event
);
3000 void md_wakeup_thread(mdk_thread_t
*thread
)
3003 dprintk("md: waking up MD thread %s.\n", thread
->tsk
->comm
);
3004 set_bit(THREAD_WAKEUP
, &thread
->flags
);
3005 wake_up(&thread
->wqueue
);
3009 mdk_thread_t
*md_register_thread(void (*run
) (mddev_t
*), mddev_t
*mddev
,
3012 mdk_thread_t
*thread
;
3014 struct completion event
;
3016 thread
= (mdk_thread_t
*) kmalloc
3017 (sizeof(mdk_thread_t
), GFP_KERNEL
);
3021 memset(thread
, 0, sizeof(mdk_thread_t
));
3022 init_waitqueue_head(&thread
->wqueue
);
3024 init_completion(&event
);
3025 thread
->event
= &event
;
3027 thread
->mddev
= mddev
;
3028 thread
->name
= name
;
3029 thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
3030 ret
= kernel_thread(md_thread
, thread
, 0);
3035 wait_for_completion(&event
);
3039 void md_unregister_thread(mdk_thread_t
*thread
)
3041 struct completion event
;
3043 init_completion(&event
);
3045 thread
->event
= &event
;
3047 /* As soon as ->run is set to NULL, the task could disappear,
3048 * so we need to hold tasklist_lock until we have sent the signal
3050 dprintk("interrupting MD-thread pid %d\n", thread
->tsk
->pid
);
3051 read_lock(&tasklist_lock
);
3053 send_sig(SIGKILL
, thread
->tsk
, 1);
3054 read_unlock(&tasklist_lock
);
3055 wait_for_completion(&event
);
3059 void md_error(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
3066 if (!rdev
|| rdev
->faulty
)
3069 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
3071 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
3072 __builtin_return_address(0),__builtin_return_address(1),
3073 __builtin_return_address(2),__builtin_return_address(3));
3075 if (!mddev
->pers
->error_handler
)
3077 mddev
->pers
->error_handler(mddev
,rdev
);
3078 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
3079 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3080 md_wakeup_thread(mddev
->thread
);
3083 /* seq_file implementation /proc/mdstat */
3085 static void status_unused(struct seq_file
*seq
)
3089 struct list_head
*tmp
;
3091 seq_printf(seq
, "unused devices: ");
3093 ITERATE_RDEV_PENDING(rdev
,tmp
) {
3094 char b
[BDEVNAME_SIZE
];
3096 seq_printf(seq
, "%s ",
3097 bdevname(rdev
->bdev
,b
));
3100 seq_printf(seq
, "<none>");
3102 seq_printf(seq
, "\n");
3106 static void status_resync(struct seq_file
*seq
, mddev_t
* mddev
)
3108 unsigned long max_blocks
, resync
, res
, dt
, db
, rt
;
3110 resync
= (mddev
->curr_resync
- atomic_read(&mddev
->recovery_active
))/2;
3112 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
3113 max_blocks
= mddev
->resync_max_sectors
>> 1;
3115 max_blocks
= mddev
->size
;
3118 * Should not happen.
3124 res
= (resync
/1024)*1000/(max_blocks
/1024 + 1);
3126 int i
, x
= res
/50, y
= 20-x
;
3127 seq_printf(seq
, "[");
3128 for (i
= 0; i
< x
; i
++)
3129 seq_printf(seq
, "=");
3130 seq_printf(seq
, ">");
3131 for (i
= 0; i
< y
; i
++)
3132 seq_printf(seq
, ".");
3133 seq_printf(seq
, "] ");
3135 seq_printf(seq
, " %s =%3lu.%lu%% (%lu/%lu)",
3136 (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ?
3137 "resync" : "recovery"),
3138 res
/10, res
% 10, resync
, max_blocks
);
3141 * We do not want to overflow, so the order of operands and
3142 * the * 100 / 100 trick are important. We do a +1 to be
3143 * safe against division by zero. We only estimate anyway.
3145 * dt: time from mark until now
3146 * db: blocks written from mark until now
3147 * rt: remaining time
3149 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
3151 db
= resync
- (mddev
->resync_mark_cnt
/2);
3152 rt
= (dt
* ((max_blocks
-resync
) / (db
/100+1)))/100;
3154 seq_printf(seq
, " finish=%lu.%lumin", rt
/ 60, (rt
% 60)/6);
3156 seq_printf(seq
, " speed=%ldK/sec", db
/dt
);
3159 static void *md_seq_start(struct seq_file
*seq
, loff_t
*pos
)
3161 struct list_head
*tmp
;
3171 spin_lock(&all_mddevs_lock
);
3172 list_for_each(tmp
,&all_mddevs
)
3174 mddev
= list_entry(tmp
, mddev_t
, all_mddevs
);
3176 spin_unlock(&all_mddevs_lock
);
3179 spin_unlock(&all_mddevs_lock
);
3181 return (void*)2;/* tail */
3185 static void *md_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
3187 struct list_head
*tmp
;
3188 mddev_t
*next_mddev
, *mddev
= v
;
3194 spin_lock(&all_mddevs_lock
);
3196 tmp
= all_mddevs
.next
;
3198 tmp
= mddev
->all_mddevs
.next
;
3199 if (tmp
!= &all_mddevs
)
3200 next_mddev
= mddev_get(list_entry(tmp
,mddev_t
,all_mddevs
));
3202 next_mddev
= (void*)2;
3205 spin_unlock(&all_mddevs_lock
);
3213 static void md_seq_stop(struct seq_file
*seq
, void *v
)
3217 if (mddev
&& v
!= (void*)1 && v
!= (void*)2)
3221 static int md_seq_show(struct seq_file
*seq
, void *v
)
3225 struct list_head
*tmp2
;
3228 struct bitmap
*bitmap
;
3230 if (v
== (void*)1) {
3231 seq_printf(seq
, "Personalities : ");
3232 spin_lock(&pers_lock
);
3233 for (i
= 0; i
< MAX_PERSONALITY
; i
++)
3235 seq_printf(seq
, "[%s] ", pers
[i
]->name
);
3237 spin_unlock(&pers_lock
);
3238 seq_printf(seq
, "\n");
3241 if (v
== (void*)2) {
3246 if (mddev_lock(mddev
)!=0)
3248 if (mddev
->pers
|| mddev
->raid_disks
|| !list_empty(&mddev
->disks
)) {
3249 seq_printf(seq
, "%s : %sactive", mdname(mddev
),
3250 mddev
->pers
? "" : "in");
3253 seq_printf(seq
, " (read-only)");
3254 seq_printf(seq
, " %s", mddev
->pers
->name
);
3258 ITERATE_RDEV(mddev
,rdev
,tmp2
) {
3259 char b
[BDEVNAME_SIZE
];
3260 seq_printf(seq
, " %s[%d]",
3261 bdevname(rdev
->bdev
,b
), rdev
->desc_nr
);
3263 seq_printf(seq
, "(F)");
3269 if (!list_empty(&mddev
->disks
)) {
3271 seq_printf(seq
, "\n %llu blocks",
3272 (unsigned long long)mddev
->array_size
);
3274 seq_printf(seq
, "\n %llu blocks",
3275 (unsigned long long)size
);
3279 mddev
->pers
->status (seq
, mddev
);
3280 seq_printf(seq
, "\n ");
3281 if (mddev
->curr_resync
> 2) {
3282 status_resync (seq
, mddev
);
3283 seq_printf(seq
, "\n ");
3284 } else if (mddev
->curr_resync
== 1 || mddev
->curr_resync
== 2)
3285 seq_printf(seq
, " resync=DELAYED\n ");
3287 seq_printf(seq
, "\n ");
3289 if ((bitmap
= mddev
->bitmap
)) {
3290 unsigned long chunk_kb
;
3291 unsigned long flags
;
3292 spin_lock_irqsave(&bitmap
->lock
, flags
);
3293 chunk_kb
= bitmap
->chunksize
>> 10;
3294 seq_printf(seq
, "bitmap: %lu/%lu pages [%luKB], "
3296 bitmap
->pages
- bitmap
->missing_pages
,
3298 (bitmap
->pages
- bitmap
->missing_pages
)
3299 << (PAGE_SHIFT
- 10),
3300 chunk_kb
? chunk_kb
: bitmap
->chunksize
,
3301 chunk_kb
? "KB" : "B");
3303 seq_printf(seq
, ", file: ");
3304 seq_path(seq
, bitmap
->file
->f_vfsmnt
,
3305 bitmap
->file
->f_dentry
," \t\n");
3308 seq_printf(seq
, "\n");
3309 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
3312 seq_printf(seq
, "\n");
3314 mddev_unlock(mddev
);
3319 static struct seq_operations md_seq_ops
= {
3320 .start
= md_seq_start
,
3321 .next
= md_seq_next
,
3322 .stop
= md_seq_stop
,
3323 .show
= md_seq_show
,
3326 static int md_seq_open(struct inode
*inode
, struct file
*file
)
3330 error
= seq_open(file
, &md_seq_ops
);
3334 static struct file_operations md_seq_fops
= {
3335 .open
= md_seq_open
,
3337 .llseek
= seq_lseek
,
3338 .release
= seq_release
,
3341 int register_md_personality(int pnum
, mdk_personality_t
*p
)
3343 if (pnum
>= MAX_PERSONALITY
) {
3345 "md: tried to install personality %s as nr %d, but max is %lu\n",
3346 p
->name
, pnum
, MAX_PERSONALITY
-1);
3350 spin_lock(&pers_lock
);
3352 spin_unlock(&pers_lock
);
3357 printk(KERN_INFO
"md: %s personality registered as nr %d\n", p
->name
, pnum
);
3358 spin_unlock(&pers_lock
);
3362 int unregister_md_personality(int pnum
)
3364 if (pnum
>= MAX_PERSONALITY
)
3367 printk(KERN_INFO
"md: %s personality unregistered\n", pers
[pnum
]->name
);
3368 spin_lock(&pers_lock
);
3370 spin_unlock(&pers_lock
);
3374 static int is_mddev_idle(mddev_t
*mddev
)
3377 struct list_head
*tmp
;
3379 unsigned long curr_events
;
3382 ITERATE_RDEV(mddev
,rdev
,tmp
) {
3383 struct gendisk
*disk
= rdev
->bdev
->bd_contains
->bd_disk
;
3384 curr_events
= disk_stat_read(disk
, read_sectors
) +
3385 disk_stat_read(disk
, write_sectors
) -
3386 atomic_read(&disk
->sync_io
);
3387 /* Allow some slack between valud of curr_events and last_events,
3388 * as there are some uninteresting races.
3389 * Note: the following is an unsigned comparison.
3391 if ((curr_events
- rdev
->last_events
+ 32) > 64) {
3392 rdev
->last_events
= curr_events
;
3399 void md_done_sync(mddev_t
*mddev
, int blocks
, int ok
)
3401 /* another "blocks" (512byte) blocks have been synced */
3402 atomic_sub(blocks
, &mddev
->recovery_active
);
3403 wake_up(&mddev
->recovery_wait
);
3405 set_bit(MD_RECOVERY_ERR
, &mddev
->recovery
);
3406 md_wakeup_thread(mddev
->thread
);
3407 // stop recovery, signal do_sync ....
3412 /* md_write_start(mddev, bi)
3413 * If we need to update some array metadata (e.g. 'active' flag
3414 * in superblock) before writing, schedule a superblock update
3415 * and wait for it to complete.
3417 void md_write_start(mddev_t
*mddev
, struct bio
*bi
)
3420 if (bio_data_dir(bi
) != WRITE
)
3423 atomic_inc(&mddev
->writes_pending
);
3424 if (mddev
->in_sync
) {
3425 spin_lock(&mddev
->write_lock
);
3426 if (mddev
->in_sync
) {
3428 mddev
->sb_dirty
= 1;
3429 md_wakeup_thread(mddev
->thread
);
3431 spin_unlock(&mddev
->write_lock
);
3433 wait_event(mddev
->sb_wait
, mddev
->sb_dirty
==0);
3436 void md_write_end(mddev_t
*mddev
)
3438 if (atomic_dec_and_test(&mddev
->writes_pending
)) {
3439 if (mddev
->safemode
== 2)
3440 md_wakeup_thread(mddev
->thread
);
3442 mod_timer(&mddev
->safemode_timer
, jiffies
+ mddev
->safemode_delay
);
3446 static DECLARE_WAIT_QUEUE_HEAD(resync_wait
);
3448 #define SYNC_MARKS 10
3449 #define SYNC_MARK_STEP (3*HZ)
3450 static void md_do_sync(mddev_t
*mddev
)
3453 unsigned int currspeed
= 0,
3455 sector_t max_sectors
,j
, io_sectors
;
3456 unsigned long mark
[SYNC_MARKS
];
3457 sector_t mark_cnt
[SYNC_MARKS
];
3459 struct list_head
*tmp
;
3460 sector_t last_check
;
3463 /* just incase thread restarts... */
3464 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
3467 /* we overload curr_resync somewhat here.
3468 * 0 == not engaged in resync at all
3469 * 2 == checking that there is no conflict with another sync
3470 * 1 == like 2, but have yielded to allow conflicting resync to
3472 * other == active in resync - this many blocks
3474 * Before starting a resync we must have set curr_resync to
3475 * 2, and then checked that every "conflicting" array has curr_resync
3476 * less than ours. When we find one that is the same or higher
3477 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
3478 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
3479 * This will mean we have to start checking from the beginning again.
3484 mddev
->curr_resync
= 2;
3487 if (signal_pending(current
)) {
3488 flush_signals(current
);
3491 ITERATE_MDDEV(mddev2
,tmp
) {
3492 if (mddev2
== mddev
)
3494 if (mddev2
->curr_resync
&&
3495 match_mddev_units(mddev
,mddev2
)) {
3497 if (mddev
< mddev2
&& mddev
->curr_resync
== 2) {
3498 /* arbitrarily yield */
3499 mddev
->curr_resync
= 1;
3500 wake_up(&resync_wait
);
3502 if (mddev
> mddev2
&& mddev
->curr_resync
== 1)
3503 /* no need to wait here, we can wait the next
3504 * time 'round when curr_resync == 2
3507 prepare_to_wait(&resync_wait
, &wq
, TASK_INTERRUPTIBLE
);
3508 if (!signal_pending(current
)
3509 && mddev2
->curr_resync
>= mddev
->curr_resync
) {
3510 printk(KERN_INFO
"md: delaying resync of %s"
3511 " until %s has finished resync (they"
3512 " share one or more physical units)\n",
3513 mdname(mddev
), mdname(mddev2
));
3516 finish_wait(&resync_wait
, &wq
);
3519 finish_wait(&resync_wait
, &wq
);
3522 } while (mddev
->curr_resync
< 2);
3524 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
3525 /* resync follows the size requested by the personality,
3526 * which defaults to physical size, but can be virtual size
3528 max_sectors
= mddev
->resync_max_sectors
;
3530 /* recovery follows the physical size of devices */
3531 max_sectors
= mddev
->size
<< 1;
3533 printk(KERN_INFO
"md: syncing RAID array %s\n", mdname(mddev
));
3534 printk(KERN_INFO
"md: minimum _guaranteed_ reconstruction speed:"
3535 " %d KB/sec/disc.\n", sysctl_speed_limit_min
);
3536 printk(KERN_INFO
"md: using maximum available idle IO bandwith "
3537 "(but not more than %d KB/sec) for reconstruction.\n",
3538 sysctl_speed_limit_max
);
3540 is_mddev_idle(mddev
); /* this also initializes IO event counters */
3541 /* we don't use the checkpoint if there's a bitmap */
3542 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) && !mddev
->bitmap
)
3543 j
= mddev
->recovery_cp
;
3547 for (m
= 0; m
< SYNC_MARKS
; m
++) {
3549 mark_cnt
[m
] = io_sectors
;
3552 mddev
->resync_mark
= mark
[last_mark
];
3553 mddev
->resync_mark_cnt
= mark_cnt
[last_mark
];
3556 * Tune reconstruction:
3558 window
= 32*(PAGE_SIZE
/512);
3559 printk(KERN_INFO
"md: using %dk window, over a total of %llu blocks.\n",
3560 window
/2,(unsigned long long) max_sectors
/2);
3562 atomic_set(&mddev
->recovery_active
, 0);
3563 init_waitqueue_head(&mddev
->recovery_wait
);
3568 "md: resuming recovery of %s from checkpoint.\n",
3570 mddev
->curr_resync
= j
;
3573 while (j
< max_sectors
) {
3577 sectors
= mddev
->pers
->sync_request(mddev
, j
, &skipped
,
3578 currspeed
< sysctl_speed_limit_min
);
3580 set_bit(MD_RECOVERY_ERR
, &mddev
->recovery
);
3584 if (!skipped
) { /* actual IO requested */
3585 io_sectors
+= sectors
;
3586 atomic_add(sectors
, &mddev
->recovery_active
);
3590 if (j
>1) mddev
->curr_resync
= j
;
3593 if (last_check
+ window
> io_sectors
|| j
== max_sectors
)
3596 last_check
= io_sectors
;
3598 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) ||
3599 test_bit(MD_RECOVERY_ERR
, &mddev
->recovery
))
3603 if (time_after_eq(jiffies
, mark
[last_mark
] + SYNC_MARK_STEP
)) {
3605 int next
= (last_mark
+1) % SYNC_MARKS
;
3607 mddev
->resync_mark
= mark
[next
];
3608 mddev
->resync_mark_cnt
= mark_cnt
[next
];
3609 mark
[next
] = jiffies
;
3610 mark_cnt
[next
] = io_sectors
- atomic_read(&mddev
->recovery_active
);
3615 if (signal_pending(current
)) {
3617 * got a signal, exit.
3620 "md: md_do_sync() got signal ... exiting\n");
3621 flush_signals(current
);
3622 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
3627 * this loop exits only if either when we are slower than
3628 * the 'hard' speed limit, or the system was IO-idle for
3630 * the system might be non-idle CPU-wise, but we only care
3631 * about not overloading the IO subsystem. (things like an
3632 * e2fsck being done on the RAID array should execute fast)
3634 mddev
->queue
->unplug_fn(mddev
->queue
);
3637 currspeed
= ((unsigned long)(io_sectors
-mddev
->resync_mark_cnt
))/2
3638 /((jiffies
-mddev
->resync_mark
)/HZ
+1) +1;
3640 if (currspeed
> sysctl_speed_limit_min
) {
3641 if ((currspeed
> sysctl_speed_limit_max
) ||
3642 !is_mddev_idle(mddev
)) {
3643 msleep_interruptible(250);
3648 printk(KERN_INFO
"md: %s: sync done.\n",mdname(mddev
));
3650 * this also signals 'finished resyncing' to md_stop
3653 mddev
->queue
->unplug_fn(mddev
->queue
);
3655 wait_event(mddev
->recovery_wait
, !atomic_read(&mddev
->recovery_active
));
3657 /* tell personality that we are finished */
3658 mddev
->pers
->sync_request(mddev
, max_sectors
, &skipped
, 1);
3660 if (!test_bit(MD_RECOVERY_ERR
, &mddev
->recovery
) &&
3661 mddev
->curr_resync
> 2 &&
3662 mddev
->curr_resync
>= mddev
->recovery_cp
) {
3663 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
3665 "md: checkpointing recovery of %s.\n",
3667 mddev
->recovery_cp
= mddev
->curr_resync
;
3669 mddev
->recovery_cp
= MaxSector
;
3673 mddev
->curr_resync
= 0;
3674 wake_up(&resync_wait
);
3675 set_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
3676 md_wakeup_thread(mddev
->thread
);
3681 * This routine is regularly called by all per-raid-array threads to
3682 * deal with generic issues like resync and super-block update.
3683 * Raid personalities that don't have a thread (linear/raid0) do not
3684 * need this as they never do any recovery or update the superblock.
3686 * It does not do any resync itself, but rather "forks" off other threads
3687 * to do that as needed.
3688 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
3689 * "->recovery" and create a thread at ->sync_thread.
3690 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
3691 * and wakeups up this thread which will reap the thread and finish up.
3692 * This thread also removes any faulty devices (with nr_pending == 0).
3694 * The overall approach is:
3695 * 1/ if the superblock needs updating, update it.
3696 * 2/ If a recovery thread is running, don't do anything else.
3697 * 3/ If recovery has finished, clean up, possibly marking spares active.
3698 * 4/ If there are any faulty devices, remove them.
3699 * 5/ If array is degraded, try to add spares devices
3700 * 6/ If array has spares or is not in-sync, start a resync thread.
3702 void md_check_recovery(mddev_t
*mddev
)
3705 struct list_head
*rtmp
;
3709 bitmap_daemon_work(mddev
->bitmap
);
3714 if (signal_pending(current
)) {
3715 if (mddev
->pers
->sync_request
) {
3716 printk(KERN_INFO
"md: %s in immediate safe mode\n",
3718 mddev
->safemode
= 2;
3720 flush_signals(current
);
3725 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
3726 test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
3727 (mddev
->safemode
== 1) ||
3728 (mddev
->safemode
== 2 && ! atomic_read(&mddev
->writes_pending
)
3729 && !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
3733 if (mddev_trylock(mddev
)==0) {
3736 spin_lock(&mddev
->write_lock
);
3737 if (mddev
->safemode
&& !atomic_read(&mddev
->writes_pending
) &&
3738 !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
) {
3740 mddev
->sb_dirty
= 1;
3742 if (mddev
->safemode
== 1)
3743 mddev
->safemode
= 0;
3744 spin_unlock(&mddev
->write_lock
);
3746 if (mddev
->sb_dirty
)
3747 md_update_sb(mddev
);
3750 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
3751 !test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
)) {
3752 /* resync/recovery still happening */
3753 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3756 if (mddev
->sync_thread
) {
3757 /* resync has finished, collect result */
3758 md_unregister_thread(mddev
->sync_thread
);
3759 mddev
->sync_thread
= NULL
;
3760 if (!test_bit(MD_RECOVERY_ERR
, &mddev
->recovery
) &&
3761 !test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
3763 /* activate any spares */
3764 mddev
->pers
->spare_active(mddev
);
3766 md_update_sb(mddev
);
3768 /* if array is no-longer degraded, then any saved_raid_disk
3769 * information must be scrapped
3771 if (!mddev
->degraded
)
3772 ITERATE_RDEV(mddev
,rdev
,rtmp
)
3773 rdev
->saved_raid_disk
= -1;
3775 mddev
->recovery
= 0;
3776 /* flag recovery needed just to double check */
3777 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3780 if (mddev
->recovery
)
3781 /* probably just the RECOVERY_NEEDED flag */
3782 mddev
->recovery
= 0;
3784 /* no recovery is running.
3785 * remove any failed drives, then
3786 * add spares if possible.
3787 * Spare are also removed and re-added, to allow
3788 * the personality to fail the re-add.
3790 ITERATE_RDEV(mddev
,rdev
,rtmp
)
3791 if (rdev
->raid_disk
>= 0 &&
3792 (rdev
->faulty
|| ! rdev
->in_sync
) &&
3793 atomic_read(&rdev
->nr_pending
)==0) {
3794 if (mddev
->pers
->hot_remove_disk(mddev
, rdev
->raid_disk
)==0)
3795 rdev
->raid_disk
= -1;
3798 if (mddev
->degraded
) {
3799 ITERATE_RDEV(mddev
,rdev
,rtmp
)
3800 if (rdev
->raid_disk
< 0
3802 if (mddev
->pers
->hot_add_disk(mddev
,rdev
))
3809 if (!spares
&& (mddev
->recovery_cp
== MaxSector
)) {
3810 /* nothing we can do ... */
3813 if (mddev
->pers
->sync_request
) {
3814 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
3816 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
3817 if (spares
&& mddev
->bitmap
&& ! mddev
->bitmap
->file
) {
3818 /* We are adding a device or devices to an array
3819 * which has the bitmap stored on all devices.
3820 * So make sure all bitmap pages get written
3822 bitmap_write_all(mddev
->bitmap
);
3824 mddev
->sync_thread
= md_register_thread(md_do_sync
,
3827 if (!mddev
->sync_thread
) {
3828 printk(KERN_ERR
"%s: could not start resync"
3831 /* leave the spares where they are, it shouldn't hurt */
3832 mddev
->recovery
= 0;
3834 md_wakeup_thread(mddev
->sync_thread
);
3838 mddev_unlock(mddev
);
3842 static int md_notify_reboot(struct notifier_block
*this,
3843 unsigned long code
, void *x
)
3845 struct list_head
*tmp
;
3848 if ((code
== SYS_DOWN
) || (code
== SYS_HALT
) || (code
== SYS_POWER_OFF
)) {
3850 printk(KERN_INFO
"md: stopping all md devices.\n");
3852 ITERATE_MDDEV(mddev
,tmp
)
3853 if (mddev_trylock(mddev
)==0)
3854 do_md_stop (mddev
, 1);
3856 * certain more exotic SCSI devices are known to be
3857 * volatile wrt too early system reboots. While the
3858 * right place to handle this issue is the given
3859 * driver, we do want to have a safe RAID driver ...
3866 static struct notifier_block md_notifier
= {
3867 .notifier_call
= md_notify_reboot
,
3869 .priority
= INT_MAX
, /* before any real devices */
3872 static void md_geninit(void)
3874 struct proc_dir_entry
*p
;
3876 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t
));
3878 p
= create_proc_entry("mdstat", S_IRUGO
, NULL
);
3880 p
->proc_fops
= &md_seq_fops
;
3883 static int __init
md_init(void)
3887 printk(KERN_INFO
"md: md driver %d.%d.%d MAX_MD_DEVS=%d,"
3888 " MD_SB_DISKS=%d\n",
3889 MD_MAJOR_VERSION
, MD_MINOR_VERSION
,
3890 MD_PATCHLEVEL_VERSION
, MAX_MD_DEVS
, MD_SB_DISKS
);
3891 printk(KERN_INFO
"md: bitmap version %d.%d\n", BITMAP_MAJOR
,
3894 if (register_blkdev(MAJOR_NR
, "md"))
3896 if ((mdp_major
=register_blkdev(0, "mdp"))<=0) {
3897 unregister_blkdev(MAJOR_NR
, "md");
3901 blk_register_region(MKDEV(MAJOR_NR
, 0), MAX_MD_DEVS
, THIS_MODULE
,
3902 md_probe
, NULL
, NULL
);
3903 blk_register_region(MKDEV(mdp_major
, 0), MAX_MD_DEVS
<<MdpMinorShift
, THIS_MODULE
,
3904 md_probe
, NULL
, NULL
);
3906 for (minor
=0; minor
< MAX_MD_DEVS
; ++minor
)
3907 devfs_mk_bdev(MKDEV(MAJOR_NR
, minor
),
3908 S_IFBLK
|S_IRUSR
|S_IWUSR
,
3911 for (minor
=0; minor
< MAX_MD_DEVS
; ++minor
)
3912 devfs_mk_bdev(MKDEV(mdp_major
, minor
<<MdpMinorShift
),
3913 S_IFBLK
|S_IRUSR
|S_IWUSR
,
3917 register_reboot_notifier(&md_notifier
);
3918 raid_table_header
= register_sysctl_table(raid_root_table
, 1);
3928 * Searches all registered partitions for autorun RAID arrays
3931 static dev_t detected_devices
[128];
3934 void md_autodetect_dev(dev_t dev
)
3936 if (dev_cnt
>= 0 && dev_cnt
< 127)
3937 detected_devices
[dev_cnt
++] = dev
;
3941 static void autostart_arrays(int part
)
3946 printk(KERN_INFO
"md: Autodetecting RAID arrays.\n");
3948 for (i
= 0; i
< dev_cnt
; i
++) {
3949 dev_t dev
= detected_devices
[i
];
3951 rdev
= md_import_device(dev
,0, 0);
3959 list_add(&rdev
->same_set
, &pending_raid_disks
);
3963 autorun_devices(part
);
3968 static __exit
void md_exit(void)
3971 struct list_head
*tmp
;
3973 blk_unregister_region(MKDEV(MAJOR_NR
,0), MAX_MD_DEVS
);
3974 blk_unregister_region(MKDEV(mdp_major
,0), MAX_MD_DEVS
<< MdpMinorShift
);
3975 for (i
=0; i
< MAX_MD_DEVS
; i
++)
3976 devfs_remove("md/%d", i
);
3977 for (i
=0; i
< MAX_MD_DEVS
; i
++)
3978 devfs_remove("md/d%d", i
);
3982 unregister_blkdev(MAJOR_NR
,"md");
3983 unregister_blkdev(mdp_major
, "mdp");
3984 unregister_reboot_notifier(&md_notifier
);
3985 unregister_sysctl_table(raid_table_header
);
3986 remove_proc_entry("mdstat", NULL
);
3987 ITERATE_MDDEV(mddev
,tmp
) {
3988 struct gendisk
*disk
= mddev
->gendisk
;
3991 export_array(mddev
);
3994 mddev
->gendisk
= NULL
;
3999 module_init(md_init
)
4000 module_exit(md_exit
)
4002 EXPORT_SYMBOL(register_md_personality
);
4003 EXPORT_SYMBOL(unregister_md_personality
);
4004 EXPORT_SYMBOL(md_error
);
4005 EXPORT_SYMBOL(md_done_sync
);
4006 EXPORT_SYMBOL(md_write_start
);
4007 EXPORT_SYMBOL(md_write_end
);
4008 EXPORT_SYMBOL(md_register_thread
);
4009 EXPORT_SYMBOL(md_unregister_thread
);
4010 EXPORT_SYMBOL(md_wakeup_thread
);
4011 EXPORT_SYMBOL(md_print_devices
);
4012 EXPORT_SYMBOL(md_check_recovery
);
4013 MODULE_LICENSE("GPL");
4015 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR
);