2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
5 completely rewritten, based on the MD driver code from Marc Zyngier
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
20 Neil Brown <neilb@cse.unsw.edu.au>.
22 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
25 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 #include <linux/module.h>
36 #include <linux/config.h>
37 #include <linux/kthread.h>
38 #include <linux/linkage.h>
39 #include <linux/raid/md.h>
40 #include <linux/raid/bitmap.h>
41 #include <linux/sysctl.h>
42 #include <linux/devfs_fs_kernel.h>
43 #include <linux/buffer_head.h> /* for invalidate_bdev */
44 #include <linux/suspend.h>
45 #include <linux/poll.h>
46 #include <linux/mutex.h>
48 #include <linux/init.h>
50 #include <linux/file.h>
53 #include <linux/kmod.h>
56 #include <asm/unaligned.h>
58 #define MAJOR_NR MD_MAJOR
61 /* 63 partitions with the alternate major number (mdp) */
62 #define MdpMinorShift 6
65 #define dprintk(x...) ((void)(DEBUG && printk(x)))
69 static void autostart_arrays (int part
);
72 static LIST_HEAD(pers_list
);
73 static DEFINE_SPINLOCK(pers_lock
);
76 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
77 * is 1000 KB/sec, so the extra system load does not show up that much.
78 * Increase it if you want to have more _guaranteed_ speed. Note that
79 * the RAID driver will use the maximum available bandwidth if the IO
80 * subsystem is idle. There is also an 'absolute maximum' reconstruction
81 * speed limit - in case reconstruction slows down your system despite
84 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
85 * or /sys/block/mdX/md/sync_speed_{min,max}
88 static int sysctl_speed_limit_min
= 1000;
89 static int sysctl_speed_limit_max
= 200000;
90 static inline int speed_min(mddev_t
*mddev
)
92 return mddev
->sync_speed_min
?
93 mddev
->sync_speed_min
: sysctl_speed_limit_min
;
96 static inline int speed_max(mddev_t
*mddev
)
98 return mddev
->sync_speed_max
?
99 mddev
->sync_speed_max
: sysctl_speed_limit_max
;
102 static struct ctl_table_header
*raid_table_header
;
104 static ctl_table raid_table
[] = {
106 .ctl_name
= DEV_RAID_SPEED_LIMIT_MIN
,
107 .procname
= "speed_limit_min",
108 .data
= &sysctl_speed_limit_min
,
109 .maxlen
= sizeof(int),
111 .proc_handler
= &proc_dointvec
,
114 .ctl_name
= DEV_RAID_SPEED_LIMIT_MAX
,
115 .procname
= "speed_limit_max",
116 .data
= &sysctl_speed_limit_max
,
117 .maxlen
= sizeof(int),
119 .proc_handler
= &proc_dointvec
,
124 static ctl_table raid_dir_table
[] = {
126 .ctl_name
= DEV_RAID
,
135 static ctl_table raid_root_table
[] = {
141 .child
= raid_dir_table
,
146 static struct block_device_operations md_fops
;
148 static int start_readonly
;
151 * We have a system wide 'event count' that is incremented
152 * on any 'interesting' event, and readers of /proc/mdstat
153 * can use 'poll' or 'select' to find out when the event
157 * start array, stop array, error, add device, remove device,
158 * start build, activate spare
160 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters
);
161 static atomic_t md_event_count
;
162 void md_new_event(mddev_t
*mddev
)
164 atomic_inc(&md_event_count
);
165 wake_up(&md_event_waiters
);
166 sysfs_notify(&mddev
->kobj
, NULL
, "sync_action");
168 EXPORT_SYMBOL_GPL(md_new_event
);
170 /* Alternate version that can be called from interrupts
171 * when calling sysfs_notify isn't needed.
173 void md_new_event_inintr(mddev_t
*mddev
)
175 atomic_inc(&md_event_count
);
176 wake_up(&md_event_waiters
);
180 * Enables to iterate over all existing md arrays
181 * all_mddevs_lock protects this list.
183 static LIST_HEAD(all_mddevs
);
184 static DEFINE_SPINLOCK(all_mddevs_lock
);
188 * iterates through all used mddevs in the system.
189 * We take care to grab the all_mddevs_lock whenever navigating
190 * the list, and to always hold a refcount when unlocked.
191 * Any code which breaks out of this loop while own
192 * a reference to the current mddev and must mddev_put it.
194 #define ITERATE_MDDEV(mddev,tmp) \
196 for (({ spin_lock(&all_mddevs_lock); \
197 tmp = all_mddevs.next; \
199 ({ if (tmp != &all_mddevs) \
200 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
201 spin_unlock(&all_mddevs_lock); \
202 if (mddev) mddev_put(mddev); \
203 mddev = list_entry(tmp, mddev_t, all_mddevs); \
204 tmp != &all_mddevs;}); \
205 ({ spin_lock(&all_mddevs_lock); \
210 static int md_fail_request (request_queue_t
*q
, struct bio
*bio
)
212 bio_io_error(bio
, bio
->bi_size
);
216 static inline mddev_t
*mddev_get(mddev_t
*mddev
)
218 atomic_inc(&mddev
->active
);
222 static void mddev_put(mddev_t
*mddev
)
224 if (!atomic_dec_and_lock(&mddev
->active
, &all_mddevs_lock
))
226 if (!mddev
->raid_disks
&& list_empty(&mddev
->disks
)) {
227 list_del(&mddev
->all_mddevs
);
228 spin_unlock(&all_mddevs_lock
);
229 blk_cleanup_queue(mddev
->queue
);
230 kobject_unregister(&mddev
->kobj
);
232 spin_unlock(&all_mddevs_lock
);
235 static mddev_t
* mddev_find(dev_t unit
)
237 mddev_t
*mddev
, *new = NULL
;
240 spin_lock(&all_mddevs_lock
);
241 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
242 if (mddev
->unit
== unit
) {
244 spin_unlock(&all_mddevs_lock
);
250 list_add(&new->all_mddevs
, &all_mddevs
);
251 spin_unlock(&all_mddevs_lock
);
254 spin_unlock(&all_mddevs_lock
);
256 new = kzalloc(sizeof(*new), GFP_KERNEL
);
261 if (MAJOR(unit
) == MD_MAJOR
)
262 new->md_minor
= MINOR(unit
);
264 new->md_minor
= MINOR(unit
) >> MdpMinorShift
;
266 mutex_init(&new->reconfig_mutex
);
267 INIT_LIST_HEAD(&new->disks
);
268 INIT_LIST_HEAD(&new->all_mddevs
);
269 init_timer(&new->safemode_timer
);
270 atomic_set(&new->active
, 1);
271 spin_lock_init(&new->write_lock
);
272 init_waitqueue_head(&new->sb_wait
);
274 new->queue
= blk_alloc_queue(GFP_KERNEL
);
279 set_bit(QUEUE_FLAG_CLUSTER
, &new->queue
->queue_flags
);
281 blk_queue_make_request(new->queue
, md_fail_request
);
286 static inline int mddev_lock(mddev_t
* mddev
)
288 return mutex_lock_interruptible(&mddev
->reconfig_mutex
);
291 static inline int mddev_trylock(mddev_t
* mddev
)
293 return mutex_trylock(&mddev
->reconfig_mutex
);
296 static inline void mddev_unlock(mddev_t
* mddev
)
298 mutex_unlock(&mddev
->reconfig_mutex
);
300 md_wakeup_thread(mddev
->thread
);
303 static mdk_rdev_t
* find_rdev_nr(mddev_t
*mddev
, int nr
)
306 struct list_head
*tmp
;
308 ITERATE_RDEV(mddev
,rdev
,tmp
) {
309 if (rdev
->desc_nr
== nr
)
315 static mdk_rdev_t
* find_rdev(mddev_t
* mddev
, dev_t dev
)
317 struct list_head
*tmp
;
320 ITERATE_RDEV(mddev
,rdev
,tmp
) {
321 if (rdev
->bdev
->bd_dev
== dev
)
327 static struct mdk_personality
*find_pers(int level
, char *clevel
)
329 struct mdk_personality
*pers
;
330 list_for_each_entry(pers
, &pers_list
, list
) {
331 if (level
!= LEVEL_NONE
&& pers
->level
== level
)
333 if (strcmp(pers
->name
, clevel
)==0)
339 static inline sector_t
calc_dev_sboffset(struct block_device
*bdev
)
341 sector_t size
= bdev
->bd_inode
->i_size
>> BLOCK_SIZE_BITS
;
342 return MD_NEW_SIZE_BLOCKS(size
);
345 static sector_t
calc_dev_size(mdk_rdev_t
*rdev
, unsigned chunk_size
)
349 size
= rdev
->sb_offset
;
352 size
&= ~((sector_t
)chunk_size
/1024 - 1);
356 static int alloc_disk_sb(mdk_rdev_t
* rdev
)
361 rdev
->sb_page
= alloc_page(GFP_KERNEL
);
362 if (!rdev
->sb_page
) {
363 printk(KERN_ALERT
"md: out of memory.\n");
370 static void free_disk_sb(mdk_rdev_t
* rdev
)
373 put_page(rdev
->sb_page
);
375 rdev
->sb_page
= NULL
;
382 static int super_written(struct bio
*bio
, unsigned int bytes_done
, int error
)
384 mdk_rdev_t
*rdev
= bio
->bi_private
;
385 mddev_t
*mddev
= rdev
->mddev
;
389 if (error
|| !test_bit(BIO_UPTODATE
, &bio
->bi_flags
))
390 md_error(mddev
, rdev
);
392 if (atomic_dec_and_test(&mddev
->pending_writes
))
393 wake_up(&mddev
->sb_wait
);
398 static int super_written_barrier(struct bio
*bio
, unsigned int bytes_done
, int error
)
400 struct bio
*bio2
= bio
->bi_private
;
401 mdk_rdev_t
*rdev
= bio2
->bi_private
;
402 mddev_t
*mddev
= rdev
->mddev
;
406 if (!test_bit(BIO_UPTODATE
, &bio
->bi_flags
) &&
407 error
== -EOPNOTSUPP
) {
409 /* barriers don't appear to be supported :-( */
410 set_bit(BarriersNotsupp
, &rdev
->flags
);
411 mddev
->barriers_work
= 0;
412 spin_lock_irqsave(&mddev
->write_lock
, flags
);
413 bio2
->bi_next
= mddev
->biolist
;
414 mddev
->biolist
= bio2
;
415 spin_unlock_irqrestore(&mddev
->write_lock
, flags
);
416 wake_up(&mddev
->sb_wait
);
421 bio
->bi_private
= rdev
;
422 return super_written(bio
, bytes_done
, error
);
425 void md_super_write(mddev_t
*mddev
, mdk_rdev_t
*rdev
,
426 sector_t sector
, int size
, struct page
*page
)
428 /* write first size bytes of page to sector of rdev
429 * Increment mddev->pending_writes before returning
430 * and decrement it on completion, waking up sb_wait
431 * if zero is reached.
432 * If an error occurred, call md_error
434 * As we might need to resubmit the request if BIO_RW_BARRIER
435 * causes ENOTSUPP, we allocate a spare bio...
437 struct bio
*bio
= bio_alloc(GFP_NOIO
, 1);
438 int rw
= (1<<BIO_RW
) | (1<<BIO_RW_SYNC
);
440 bio
->bi_bdev
= rdev
->bdev
;
441 bio
->bi_sector
= sector
;
442 bio_add_page(bio
, page
, size
, 0);
443 bio
->bi_private
= rdev
;
444 bio
->bi_end_io
= super_written
;
447 atomic_inc(&mddev
->pending_writes
);
448 if (!test_bit(BarriersNotsupp
, &rdev
->flags
)) {
450 rw
|= (1<<BIO_RW_BARRIER
);
451 rbio
= bio_clone(bio
, GFP_NOIO
);
452 rbio
->bi_private
= bio
;
453 rbio
->bi_end_io
= super_written_barrier
;
454 submit_bio(rw
, rbio
);
459 void md_super_wait(mddev_t
*mddev
)
461 /* wait for all superblock writes that were scheduled to complete.
462 * if any had to be retried (due to BARRIER problems), retry them
466 prepare_to_wait(&mddev
->sb_wait
, &wq
, TASK_UNINTERRUPTIBLE
);
467 if (atomic_read(&mddev
->pending_writes
)==0)
469 while (mddev
->biolist
) {
471 spin_lock_irq(&mddev
->write_lock
);
472 bio
= mddev
->biolist
;
473 mddev
->biolist
= bio
->bi_next
;
475 spin_unlock_irq(&mddev
->write_lock
);
476 submit_bio(bio
->bi_rw
, bio
);
480 finish_wait(&mddev
->sb_wait
, &wq
);
483 static int bi_complete(struct bio
*bio
, unsigned int bytes_done
, int error
)
488 complete((struct completion
*)bio
->bi_private
);
492 int sync_page_io(struct block_device
*bdev
, sector_t sector
, int size
,
493 struct page
*page
, int rw
)
495 struct bio
*bio
= bio_alloc(GFP_NOIO
, 1);
496 struct completion event
;
499 rw
|= (1 << BIO_RW_SYNC
);
502 bio
->bi_sector
= sector
;
503 bio_add_page(bio
, page
, size
, 0);
504 init_completion(&event
);
505 bio
->bi_private
= &event
;
506 bio
->bi_end_io
= bi_complete
;
508 wait_for_completion(&event
);
510 ret
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
514 EXPORT_SYMBOL_GPL(sync_page_io
);
516 static int read_disk_sb(mdk_rdev_t
* rdev
, int size
)
518 char b
[BDEVNAME_SIZE
];
519 if (!rdev
->sb_page
) {
527 if (!sync_page_io(rdev
->bdev
, rdev
->sb_offset
<<1, size
, rdev
->sb_page
, READ
))
533 printk(KERN_WARNING
"md: disabled device %s, could not read superblock.\n",
534 bdevname(rdev
->bdev
,b
));
538 static int uuid_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
540 if ( (sb1
->set_uuid0
== sb2
->set_uuid0
) &&
541 (sb1
->set_uuid1
== sb2
->set_uuid1
) &&
542 (sb1
->set_uuid2
== sb2
->set_uuid2
) &&
543 (sb1
->set_uuid3
== sb2
->set_uuid3
))
551 static int sb_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
554 mdp_super_t
*tmp1
, *tmp2
;
556 tmp1
= kmalloc(sizeof(*tmp1
),GFP_KERNEL
);
557 tmp2
= kmalloc(sizeof(*tmp2
),GFP_KERNEL
);
559 if (!tmp1
|| !tmp2
) {
561 printk(KERN_INFO
"md.c: sb1 is not equal to sb2!\n");
569 * nr_disks is not constant
574 if (memcmp(tmp1
, tmp2
, MD_SB_GENERIC_CONSTANT_WORDS
* 4))
585 static unsigned int calc_sb_csum(mdp_super_t
* sb
)
587 unsigned int disk_csum
, csum
;
589 disk_csum
= sb
->sb_csum
;
591 csum
= csum_partial((void *)sb
, MD_SB_BYTES
, 0);
592 sb
->sb_csum
= disk_csum
;
598 * Handle superblock details.
599 * We want to be able to handle multiple superblock formats
600 * so we have a common interface to them all, and an array of
601 * different handlers.
602 * We rely on user-space to write the initial superblock, and support
603 * reading and updating of superblocks.
604 * Interface methods are:
605 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
606 * loads and validates a superblock on dev.
607 * if refdev != NULL, compare superblocks on both devices
609 * 0 - dev has a superblock that is compatible with refdev
610 * 1 - dev has a superblock that is compatible and newer than refdev
611 * so dev should be used as the refdev in future
612 * -EINVAL superblock incompatible or invalid
613 * -othererror e.g. -EIO
615 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
616 * Verify that dev is acceptable into mddev.
617 * The first time, mddev->raid_disks will be 0, and data from
618 * dev should be merged in. Subsequent calls check that dev
619 * is new enough. Return 0 or -EINVAL
621 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
622 * Update the superblock for rdev with data in mddev
623 * This does not write to disc.
629 struct module
*owner
;
630 int (*load_super
)(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
);
631 int (*validate_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
632 void (*sync_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
636 * load_super for 0.90.0
638 static int super_90_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
640 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
646 * Calculate the position of the superblock,
647 * it's at the end of the disk.
649 * It also happens to be a multiple of 4Kb.
651 sb_offset
= calc_dev_sboffset(rdev
->bdev
);
652 rdev
->sb_offset
= sb_offset
;
654 ret
= read_disk_sb(rdev
, MD_SB_BYTES
);
659 bdevname(rdev
->bdev
, b
);
660 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
662 if (sb
->md_magic
!= MD_SB_MAGIC
) {
663 printk(KERN_ERR
"md: invalid raid superblock magic on %s\n",
668 if (sb
->major_version
!= 0 ||
669 sb
->minor_version
< 90 ||
670 sb
->minor_version
> 91) {
671 printk(KERN_WARNING
"Bad version number %d.%d on %s\n",
672 sb
->major_version
, sb
->minor_version
,
677 if (sb
->raid_disks
<= 0)
680 if (csum_fold(calc_sb_csum(sb
)) != csum_fold(sb
->sb_csum
)) {
681 printk(KERN_WARNING
"md: invalid superblock checksum on %s\n",
686 rdev
->preferred_minor
= sb
->md_minor
;
687 rdev
->data_offset
= 0;
688 rdev
->sb_size
= MD_SB_BYTES
;
690 if (sb
->level
== LEVEL_MULTIPATH
)
693 rdev
->desc_nr
= sb
->this_disk
.number
;
699 mdp_super_t
*refsb
= (mdp_super_t
*)page_address(refdev
->sb_page
);
700 if (!uuid_equal(refsb
, sb
)) {
701 printk(KERN_WARNING
"md: %s has different UUID to %s\n",
702 b
, bdevname(refdev
->bdev
,b2
));
705 if (!sb_equal(refsb
, sb
)) {
706 printk(KERN_WARNING
"md: %s has same UUID"
707 " but different superblock to %s\n",
708 b
, bdevname(refdev
->bdev
, b2
));
712 ev2
= md_event(refsb
);
718 rdev
->size
= calc_dev_size(rdev
, sb
->chunk_size
);
720 if (rdev
->size
< sb
->size
&& sb
->level
> 1)
721 /* "this cannot possibly happen" ... */
729 * validate_super for 0.90.0
731 static int super_90_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
734 mdp_super_t
*sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
736 rdev
->raid_disk
= -1;
738 if (mddev
->raid_disks
== 0) {
739 mddev
->major_version
= 0;
740 mddev
->minor_version
= sb
->minor_version
;
741 mddev
->patch_version
= sb
->patch_version
;
742 mddev
->persistent
= ! sb
->not_persistent
;
743 mddev
->chunk_size
= sb
->chunk_size
;
744 mddev
->ctime
= sb
->ctime
;
745 mddev
->utime
= sb
->utime
;
746 mddev
->level
= sb
->level
;
747 mddev
->clevel
[0] = 0;
748 mddev
->layout
= sb
->layout
;
749 mddev
->raid_disks
= sb
->raid_disks
;
750 mddev
->size
= sb
->size
;
751 mddev
->events
= md_event(sb
);
752 mddev
->bitmap_offset
= 0;
753 mddev
->default_bitmap_offset
= MD_SB_BYTES
>> 9;
755 if (mddev
->minor_version
>= 91) {
756 mddev
->reshape_position
= sb
->reshape_position
;
757 mddev
->delta_disks
= sb
->delta_disks
;
758 mddev
->new_level
= sb
->new_level
;
759 mddev
->new_layout
= sb
->new_layout
;
760 mddev
->new_chunk
= sb
->new_chunk
;
762 mddev
->reshape_position
= MaxSector
;
763 mddev
->delta_disks
= 0;
764 mddev
->new_level
= mddev
->level
;
765 mddev
->new_layout
= mddev
->layout
;
766 mddev
->new_chunk
= mddev
->chunk_size
;
769 if (sb
->state
& (1<<MD_SB_CLEAN
))
770 mddev
->recovery_cp
= MaxSector
;
772 if (sb
->events_hi
== sb
->cp_events_hi
&&
773 sb
->events_lo
== sb
->cp_events_lo
) {
774 mddev
->recovery_cp
= sb
->recovery_cp
;
776 mddev
->recovery_cp
= 0;
779 memcpy(mddev
->uuid
+0, &sb
->set_uuid0
, 4);
780 memcpy(mddev
->uuid
+4, &sb
->set_uuid1
, 4);
781 memcpy(mddev
->uuid
+8, &sb
->set_uuid2
, 4);
782 memcpy(mddev
->uuid
+12,&sb
->set_uuid3
, 4);
784 mddev
->max_disks
= MD_SB_DISKS
;
786 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
) &&
787 mddev
->bitmap_file
== NULL
) {
788 if (mddev
->level
!= 1 && mddev
->level
!= 4
789 && mddev
->level
!= 5 && mddev
->level
!= 6
790 && mddev
->level
!= 10) {
791 /* FIXME use a better test */
792 printk(KERN_WARNING
"md: bitmaps not supported for this level.\n");
795 mddev
->bitmap_offset
= mddev
->default_bitmap_offset
;
798 } else if (mddev
->pers
== NULL
) {
799 /* Insist on good event counter while assembling */
800 __u64 ev1
= md_event(sb
);
802 if (ev1
< mddev
->events
)
804 } else if (mddev
->bitmap
) {
805 /* if adding to array with a bitmap, then we can accept an
806 * older device ... but not too old.
808 __u64 ev1
= md_event(sb
);
809 if (ev1
< mddev
->bitmap
->events_cleared
)
811 } else /* just a hot-add of a new device, leave raid_disk at -1 */
814 if (mddev
->level
!= LEVEL_MULTIPATH
) {
815 desc
= sb
->disks
+ rdev
->desc_nr
;
817 if (desc
->state
& (1<<MD_DISK_FAULTY
))
818 set_bit(Faulty
, &rdev
->flags
);
819 else if (desc
->state
& (1<<MD_DISK_SYNC
) &&
820 desc
->raid_disk
< mddev
->raid_disks
) {
821 set_bit(In_sync
, &rdev
->flags
);
822 rdev
->raid_disk
= desc
->raid_disk
;
824 if (desc
->state
& (1<<MD_DISK_WRITEMOSTLY
))
825 set_bit(WriteMostly
, &rdev
->flags
);
826 } else /* MULTIPATH are always insync */
827 set_bit(In_sync
, &rdev
->flags
);
832 * sync_super for 0.90.0
834 static void super_90_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
837 struct list_head
*tmp
;
839 int next_spare
= mddev
->raid_disks
;
842 /* make rdev->sb match mddev data..
845 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
846 * 3/ any empty disks < next_spare become removed
848 * disks[0] gets initialised to REMOVED because
849 * we cannot be sure from other fields if it has
850 * been initialised or not.
853 int active
=0, working
=0,failed
=0,spare
=0,nr_disks
=0;
855 rdev
->sb_size
= MD_SB_BYTES
;
857 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
859 memset(sb
, 0, sizeof(*sb
));
861 sb
->md_magic
= MD_SB_MAGIC
;
862 sb
->major_version
= mddev
->major_version
;
863 sb
->patch_version
= mddev
->patch_version
;
864 sb
->gvalid_words
= 0; /* ignored */
865 memcpy(&sb
->set_uuid0
, mddev
->uuid
+0, 4);
866 memcpy(&sb
->set_uuid1
, mddev
->uuid
+4, 4);
867 memcpy(&sb
->set_uuid2
, mddev
->uuid
+8, 4);
868 memcpy(&sb
->set_uuid3
, mddev
->uuid
+12,4);
870 sb
->ctime
= mddev
->ctime
;
871 sb
->level
= mddev
->level
;
872 sb
->size
= mddev
->size
;
873 sb
->raid_disks
= mddev
->raid_disks
;
874 sb
->md_minor
= mddev
->md_minor
;
875 sb
->not_persistent
= !mddev
->persistent
;
876 sb
->utime
= mddev
->utime
;
878 sb
->events_hi
= (mddev
->events
>>32);
879 sb
->events_lo
= (u32
)mddev
->events
;
881 if (mddev
->reshape_position
== MaxSector
)
882 sb
->minor_version
= 90;
884 sb
->minor_version
= 91;
885 sb
->reshape_position
= mddev
->reshape_position
;
886 sb
->new_level
= mddev
->new_level
;
887 sb
->delta_disks
= mddev
->delta_disks
;
888 sb
->new_layout
= mddev
->new_layout
;
889 sb
->new_chunk
= mddev
->new_chunk
;
891 mddev
->minor_version
= sb
->minor_version
;
894 sb
->recovery_cp
= mddev
->recovery_cp
;
895 sb
->cp_events_hi
= (mddev
->events
>>32);
896 sb
->cp_events_lo
= (u32
)mddev
->events
;
897 if (mddev
->recovery_cp
== MaxSector
)
898 sb
->state
= (1<< MD_SB_CLEAN
);
902 sb
->layout
= mddev
->layout
;
903 sb
->chunk_size
= mddev
->chunk_size
;
905 if (mddev
->bitmap
&& mddev
->bitmap_file
== NULL
)
906 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
908 sb
->disks
[0].state
= (1<<MD_DISK_REMOVED
);
909 ITERATE_RDEV(mddev
,rdev2
,tmp
) {
912 if (rdev2
->raid_disk
>= 0 && test_bit(In_sync
, &rdev2
->flags
)
913 && !test_bit(Faulty
, &rdev2
->flags
))
914 desc_nr
= rdev2
->raid_disk
;
916 desc_nr
= next_spare
++;
917 rdev2
->desc_nr
= desc_nr
;
918 d
= &sb
->disks
[rdev2
->desc_nr
];
920 d
->number
= rdev2
->desc_nr
;
921 d
->major
= MAJOR(rdev2
->bdev
->bd_dev
);
922 d
->minor
= MINOR(rdev2
->bdev
->bd_dev
);
923 if (rdev2
->raid_disk
>= 0 && test_bit(In_sync
, &rdev2
->flags
)
924 && !test_bit(Faulty
, &rdev2
->flags
))
925 d
->raid_disk
= rdev2
->raid_disk
;
927 d
->raid_disk
= rdev2
->desc_nr
; /* compatibility */
928 if (test_bit(Faulty
, &rdev2
->flags
))
929 d
->state
= (1<<MD_DISK_FAULTY
);
930 else if (test_bit(In_sync
, &rdev2
->flags
)) {
931 d
->state
= (1<<MD_DISK_ACTIVE
);
932 d
->state
|= (1<<MD_DISK_SYNC
);
940 if (test_bit(WriteMostly
, &rdev2
->flags
))
941 d
->state
|= (1<<MD_DISK_WRITEMOSTLY
);
943 /* now set the "removed" and "faulty" bits on any missing devices */
944 for (i
=0 ; i
< mddev
->raid_disks
; i
++) {
945 mdp_disk_t
*d
= &sb
->disks
[i
];
946 if (d
->state
== 0 && d
->number
== 0) {
949 d
->state
= (1<<MD_DISK_REMOVED
);
950 d
->state
|= (1<<MD_DISK_FAULTY
);
954 sb
->nr_disks
= nr_disks
;
955 sb
->active_disks
= active
;
956 sb
->working_disks
= working
;
957 sb
->failed_disks
= failed
;
958 sb
->spare_disks
= spare
;
960 sb
->this_disk
= sb
->disks
[rdev
->desc_nr
];
961 sb
->sb_csum
= calc_sb_csum(sb
);
965 * version 1 superblock
968 static unsigned int calc_sb_1_csum(struct mdp_superblock_1
* sb
)
970 unsigned int disk_csum
, csum
;
971 unsigned long long newcsum
;
972 int size
= 256 + le32_to_cpu(sb
->max_dev
)*2;
973 unsigned int *isuper
= (unsigned int*)sb
;
976 disk_csum
= sb
->sb_csum
;
979 for (i
=0; size
>=4; size
-= 4 )
980 newcsum
+= le32_to_cpu(*isuper
++);
983 newcsum
+= le16_to_cpu(*(unsigned short*) isuper
);
985 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
986 sb
->sb_csum
= disk_csum
;
987 return cpu_to_le32(csum
);
990 static int super_1_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
992 struct mdp_superblock_1
*sb
;
995 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
999 * Calculate the position of the superblock.
1000 * It is always aligned to a 4K boundary and
1001 * depeding on minor_version, it can be:
1002 * 0: At least 8K, but less than 12K, from end of device
1003 * 1: At start of device
1004 * 2: 4K from start of device.
1006 switch(minor_version
) {
1008 sb_offset
= rdev
->bdev
->bd_inode
->i_size
>> 9;
1010 sb_offset
&= ~(sector_t
)(4*2-1);
1011 /* convert from sectors to K */
1023 rdev
->sb_offset
= sb_offset
;
1025 /* superblock is rarely larger than 1K, but it can be larger,
1026 * and it is safe to read 4k, so we do that
1028 ret
= read_disk_sb(rdev
, 4096);
1029 if (ret
) return ret
;
1032 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1034 if (sb
->magic
!= cpu_to_le32(MD_SB_MAGIC
) ||
1035 sb
->major_version
!= cpu_to_le32(1) ||
1036 le32_to_cpu(sb
->max_dev
) > (4096-256)/2 ||
1037 le64_to_cpu(sb
->super_offset
) != (rdev
->sb_offset
<<1) ||
1038 (le32_to_cpu(sb
->feature_map
) & ~MD_FEATURE_ALL
) != 0)
1041 if (calc_sb_1_csum(sb
) != sb
->sb_csum
) {
1042 printk("md: invalid superblock checksum on %s\n",
1043 bdevname(rdev
->bdev
,b
));
1046 if (le64_to_cpu(sb
->data_size
) < 10) {
1047 printk("md: data_size too small on %s\n",
1048 bdevname(rdev
->bdev
,b
));
1051 rdev
->preferred_minor
= 0xffff;
1052 rdev
->data_offset
= le64_to_cpu(sb
->data_offset
);
1053 atomic_set(&rdev
->corrected_errors
, le32_to_cpu(sb
->cnt_corrected_read
));
1055 rdev
->sb_size
= le32_to_cpu(sb
->max_dev
) * 2 + 256;
1056 bmask
= queue_hardsect_size(rdev
->bdev
->bd_disk
->queue
)-1;
1057 if (rdev
->sb_size
& bmask
)
1058 rdev
-> sb_size
= (rdev
->sb_size
| bmask
)+1;
1064 struct mdp_superblock_1
*refsb
=
1065 (struct mdp_superblock_1
*)page_address(refdev
->sb_page
);
1067 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16) != 0 ||
1068 sb
->level
!= refsb
->level
||
1069 sb
->layout
!= refsb
->layout
||
1070 sb
->chunksize
!= refsb
->chunksize
) {
1071 printk(KERN_WARNING
"md: %s has strangely different"
1072 " superblock to %s\n",
1073 bdevname(rdev
->bdev
,b
),
1074 bdevname(refdev
->bdev
,b2
));
1077 ev1
= le64_to_cpu(sb
->events
);
1078 ev2
= le64_to_cpu(refsb
->events
);
1086 rdev
->size
= ((rdev
->bdev
->bd_inode
->i_size
>>9) - le64_to_cpu(sb
->data_offset
)) / 2;
1088 rdev
->size
= rdev
->sb_offset
;
1089 if (rdev
->size
< le64_to_cpu(sb
->data_size
)/2)
1091 rdev
->size
= le64_to_cpu(sb
->data_size
)/2;
1092 if (le32_to_cpu(sb
->chunksize
))
1093 rdev
->size
&= ~((sector_t
)le32_to_cpu(sb
->chunksize
)/2 - 1);
1095 if (le32_to_cpu(sb
->size
) > rdev
->size
*2)
1100 static int super_1_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1102 struct mdp_superblock_1
*sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1104 rdev
->raid_disk
= -1;
1106 if (mddev
->raid_disks
== 0) {
1107 mddev
->major_version
= 1;
1108 mddev
->patch_version
= 0;
1109 mddev
->persistent
= 1;
1110 mddev
->chunk_size
= le32_to_cpu(sb
->chunksize
) << 9;
1111 mddev
->ctime
= le64_to_cpu(sb
->ctime
) & ((1ULL << 32)-1);
1112 mddev
->utime
= le64_to_cpu(sb
->utime
) & ((1ULL << 32)-1);
1113 mddev
->level
= le32_to_cpu(sb
->level
);
1114 mddev
->clevel
[0] = 0;
1115 mddev
->layout
= le32_to_cpu(sb
->layout
);
1116 mddev
->raid_disks
= le32_to_cpu(sb
->raid_disks
);
1117 mddev
->size
= le64_to_cpu(sb
->size
)/2;
1118 mddev
->events
= le64_to_cpu(sb
->events
);
1119 mddev
->bitmap_offset
= 0;
1120 mddev
->default_bitmap_offset
= 1024 >> 9;
1122 mddev
->recovery_cp
= le64_to_cpu(sb
->resync_offset
);
1123 memcpy(mddev
->uuid
, sb
->set_uuid
, 16);
1125 mddev
->max_disks
= (4096-256)/2;
1127 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BITMAP_OFFSET
) &&
1128 mddev
->bitmap_file
== NULL
) {
1129 if (mddev
->level
!= 1 && mddev
->level
!= 5 && mddev
->level
!= 6
1130 && mddev
->level
!= 10) {
1131 printk(KERN_WARNING
"md: bitmaps not supported for this level.\n");
1134 mddev
->bitmap_offset
= (__s32
)le32_to_cpu(sb
->bitmap_offset
);
1136 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
1137 mddev
->reshape_position
= le64_to_cpu(sb
->reshape_position
);
1138 mddev
->delta_disks
= le32_to_cpu(sb
->delta_disks
);
1139 mddev
->new_level
= le32_to_cpu(sb
->new_level
);
1140 mddev
->new_layout
= le32_to_cpu(sb
->new_layout
);
1141 mddev
->new_chunk
= le32_to_cpu(sb
->new_chunk
)<<9;
1143 mddev
->reshape_position
= MaxSector
;
1144 mddev
->delta_disks
= 0;
1145 mddev
->new_level
= mddev
->level
;
1146 mddev
->new_layout
= mddev
->layout
;
1147 mddev
->new_chunk
= mddev
->chunk_size
;
1150 } else if (mddev
->pers
== NULL
) {
1151 /* Insist of good event counter while assembling */
1152 __u64 ev1
= le64_to_cpu(sb
->events
);
1154 if (ev1
< mddev
->events
)
1156 } else if (mddev
->bitmap
) {
1157 /* If adding to array with a bitmap, then we can accept an
1158 * older device, but not too old.
1160 __u64 ev1
= le64_to_cpu(sb
->events
);
1161 if (ev1
< mddev
->bitmap
->events_cleared
)
1163 } else /* just a hot-add of a new device, leave raid_disk at -1 */
1166 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1168 rdev
->desc_nr
= le32_to_cpu(sb
->dev_number
);
1169 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
1171 case 0xffff: /* spare */
1173 case 0xfffe: /* faulty */
1174 set_bit(Faulty
, &rdev
->flags
);
1177 set_bit(In_sync
, &rdev
->flags
);
1178 rdev
->raid_disk
= role
;
1181 if (sb
->devflags
& WriteMostly1
)
1182 set_bit(WriteMostly
, &rdev
->flags
);
1183 } else /* MULTIPATH are always insync */
1184 set_bit(In_sync
, &rdev
->flags
);
1189 static void super_1_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1191 struct mdp_superblock_1
*sb
;
1192 struct list_head
*tmp
;
1195 /* make rdev->sb match mddev and rdev data. */
1197 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1199 sb
->feature_map
= 0;
1201 memset(sb
->pad1
, 0, sizeof(sb
->pad1
));
1202 memset(sb
->pad2
, 0, sizeof(sb
->pad2
));
1203 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
1205 sb
->utime
= cpu_to_le64((__u64
)mddev
->utime
);
1206 sb
->events
= cpu_to_le64(mddev
->events
);
1208 sb
->resync_offset
= cpu_to_le64(mddev
->recovery_cp
);
1210 sb
->resync_offset
= cpu_to_le64(0);
1212 sb
->cnt_corrected_read
= atomic_read(&rdev
->corrected_errors
);
1214 sb
->raid_disks
= cpu_to_le32(mddev
->raid_disks
);
1215 sb
->size
= cpu_to_le64(mddev
->size
<<1);
1217 if (mddev
->bitmap
&& mddev
->bitmap_file
== NULL
) {
1218 sb
->bitmap_offset
= cpu_to_le32((__u32
)mddev
->bitmap_offset
);
1219 sb
->feature_map
= cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
);
1221 if (mddev
->reshape_position
!= MaxSector
) {
1222 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE
);
1223 sb
->reshape_position
= cpu_to_le64(mddev
->reshape_position
);
1224 sb
->new_layout
= cpu_to_le32(mddev
->new_layout
);
1225 sb
->delta_disks
= cpu_to_le32(mddev
->delta_disks
);
1226 sb
->new_level
= cpu_to_le32(mddev
->new_level
);
1227 sb
->new_chunk
= cpu_to_le32(mddev
->new_chunk
>>9);
1231 ITERATE_RDEV(mddev
,rdev2
,tmp
)
1232 if (rdev2
->desc_nr
+1 > max_dev
)
1233 max_dev
= rdev2
->desc_nr
+1;
1235 sb
->max_dev
= cpu_to_le32(max_dev
);
1236 for (i
=0; i
<max_dev
;i
++)
1237 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1239 ITERATE_RDEV(mddev
,rdev2
,tmp
) {
1241 if (test_bit(Faulty
, &rdev2
->flags
))
1242 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1243 else if (test_bit(In_sync
, &rdev2
->flags
))
1244 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1246 sb
->dev_roles
[i
] = cpu_to_le16(0xffff);
1249 sb
->recovery_offset
= cpu_to_le64(0); /* not supported yet */
1250 sb
->sb_csum
= calc_sb_1_csum(sb
);
1254 static struct super_type super_types
[] = {
1257 .owner
= THIS_MODULE
,
1258 .load_super
= super_90_load
,
1259 .validate_super
= super_90_validate
,
1260 .sync_super
= super_90_sync
,
1264 .owner
= THIS_MODULE
,
1265 .load_super
= super_1_load
,
1266 .validate_super
= super_1_validate
,
1267 .sync_super
= super_1_sync
,
1271 static mdk_rdev_t
* match_dev_unit(mddev_t
*mddev
, mdk_rdev_t
*dev
)
1273 struct list_head
*tmp
;
1276 ITERATE_RDEV(mddev
,rdev
,tmp
)
1277 if (rdev
->bdev
->bd_contains
== dev
->bdev
->bd_contains
)
1283 static int match_mddev_units(mddev_t
*mddev1
, mddev_t
*mddev2
)
1285 struct list_head
*tmp
;
1288 ITERATE_RDEV(mddev1
,rdev
,tmp
)
1289 if (match_dev_unit(mddev2
, rdev
))
1295 static LIST_HEAD(pending_raid_disks
);
1297 static int bind_rdev_to_array(mdk_rdev_t
* rdev
, mddev_t
* mddev
)
1299 mdk_rdev_t
*same_pdev
;
1300 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1308 /* make sure rdev->size exceeds mddev->size */
1309 if (rdev
->size
&& (mddev
->size
== 0 || rdev
->size
< mddev
->size
)) {
1311 /* Cannot change size, so fail */
1314 mddev
->size
= rdev
->size
;
1316 same_pdev
= match_dev_unit(mddev
, rdev
);
1319 "%s: WARNING: %s appears to be on the same physical"
1320 " disk as %s. True\n protection against single-disk"
1321 " failure might be compromised.\n",
1322 mdname(mddev
), bdevname(rdev
->bdev
,b
),
1323 bdevname(same_pdev
->bdev
,b2
));
1325 /* Verify rdev->desc_nr is unique.
1326 * If it is -1, assign a free number, else
1327 * check number is not in use
1329 if (rdev
->desc_nr
< 0) {
1331 if (mddev
->pers
) choice
= mddev
->raid_disks
;
1332 while (find_rdev_nr(mddev
, choice
))
1334 rdev
->desc_nr
= choice
;
1336 if (find_rdev_nr(mddev
, rdev
->desc_nr
))
1339 bdevname(rdev
->bdev
,b
);
1340 if (kobject_set_name(&rdev
->kobj
, "dev-%s", b
) < 0)
1342 while ( (s
=strchr(rdev
->kobj
.k_name
, '/')) != NULL
)
1345 list_add(&rdev
->same_set
, &mddev
->disks
);
1346 rdev
->mddev
= mddev
;
1347 printk(KERN_INFO
"md: bind<%s>\n", b
);
1349 rdev
->kobj
.parent
= &mddev
->kobj
;
1350 kobject_add(&rdev
->kobj
);
1352 if (rdev
->bdev
->bd_part
)
1353 ko
= &rdev
->bdev
->bd_part
->kobj
;
1355 ko
= &rdev
->bdev
->bd_disk
->kobj
;
1356 sysfs_create_link(&rdev
->kobj
, ko
, "block");
1357 bd_claim_by_disk(rdev
->bdev
, rdev
, mddev
->gendisk
);
1361 static void unbind_rdev_from_array(mdk_rdev_t
* rdev
)
1363 char b
[BDEVNAME_SIZE
];
1368 bd_release_from_disk(rdev
->bdev
, rdev
->mddev
->gendisk
);
1369 list_del_init(&rdev
->same_set
);
1370 printk(KERN_INFO
"md: unbind<%s>\n", bdevname(rdev
->bdev
,b
));
1372 sysfs_remove_link(&rdev
->kobj
, "block");
1373 kobject_del(&rdev
->kobj
);
1377 * prevent the device from being mounted, repartitioned or
1378 * otherwise reused by a RAID array (or any other kernel
1379 * subsystem), by bd_claiming the device.
1381 static int lock_rdev(mdk_rdev_t
*rdev
, dev_t dev
)
1384 struct block_device
*bdev
;
1385 char b
[BDEVNAME_SIZE
];
1387 bdev
= open_by_devnum(dev
, FMODE_READ
|FMODE_WRITE
);
1389 printk(KERN_ERR
"md: could not open %s.\n",
1390 __bdevname(dev
, b
));
1391 return PTR_ERR(bdev
);
1393 err
= bd_claim(bdev
, rdev
);
1395 printk(KERN_ERR
"md: could not bd_claim %s.\n",
1404 static void unlock_rdev(mdk_rdev_t
*rdev
)
1406 struct block_device
*bdev
= rdev
->bdev
;
1414 void md_autodetect_dev(dev_t dev
);
1416 static void export_rdev(mdk_rdev_t
* rdev
)
1418 char b
[BDEVNAME_SIZE
];
1419 printk(KERN_INFO
"md: export_rdev(%s)\n",
1420 bdevname(rdev
->bdev
,b
));
1424 list_del_init(&rdev
->same_set
);
1426 md_autodetect_dev(rdev
->bdev
->bd_dev
);
1429 kobject_put(&rdev
->kobj
);
1432 static void kick_rdev_from_array(mdk_rdev_t
* rdev
)
1434 unbind_rdev_from_array(rdev
);
1438 static void export_array(mddev_t
*mddev
)
1440 struct list_head
*tmp
;
1443 ITERATE_RDEV(mddev
,rdev
,tmp
) {
1448 kick_rdev_from_array(rdev
);
1450 if (!list_empty(&mddev
->disks
))
1452 mddev
->raid_disks
= 0;
1453 mddev
->major_version
= 0;
1456 static void print_desc(mdp_disk_t
*desc
)
1458 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc
->number
,
1459 desc
->major
,desc
->minor
,desc
->raid_disk
,desc
->state
);
1462 static void print_sb(mdp_super_t
*sb
)
1467 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1468 sb
->major_version
, sb
->minor_version
, sb
->patch_version
,
1469 sb
->set_uuid0
, sb
->set_uuid1
, sb
->set_uuid2
, sb
->set_uuid3
,
1471 printk(KERN_INFO
"md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1472 sb
->level
, sb
->size
, sb
->nr_disks
, sb
->raid_disks
,
1473 sb
->md_minor
, sb
->layout
, sb
->chunk_size
);
1474 printk(KERN_INFO
"md: UT:%08x ST:%d AD:%d WD:%d"
1475 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1476 sb
->utime
, sb
->state
, sb
->active_disks
, sb
->working_disks
,
1477 sb
->failed_disks
, sb
->spare_disks
,
1478 sb
->sb_csum
, (unsigned long)sb
->events_lo
);
1481 for (i
= 0; i
< MD_SB_DISKS
; i
++) {
1484 desc
= sb
->disks
+ i
;
1485 if (desc
->number
|| desc
->major
|| desc
->minor
||
1486 desc
->raid_disk
|| (desc
->state
&& (desc
->state
!= 4))) {
1487 printk(" D %2d: ", i
);
1491 printk(KERN_INFO
"md: THIS: ");
1492 print_desc(&sb
->this_disk
);
1496 static void print_rdev(mdk_rdev_t
*rdev
)
1498 char b
[BDEVNAME_SIZE
];
1499 printk(KERN_INFO
"md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1500 bdevname(rdev
->bdev
,b
), (unsigned long long)rdev
->size
,
1501 test_bit(Faulty
, &rdev
->flags
), test_bit(In_sync
, &rdev
->flags
),
1503 if (rdev
->sb_loaded
) {
1504 printk(KERN_INFO
"md: rdev superblock:\n");
1505 print_sb((mdp_super_t
*)page_address(rdev
->sb_page
));
1507 printk(KERN_INFO
"md: no rdev superblock!\n");
1510 void md_print_devices(void)
1512 struct list_head
*tmp
, *tmp2
;
1515 char b
[BDEVNAME_SIZE
];
1518 printk("md: **********************************\n");
1519 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1520 printk("md: **********************************\n");
1521 ITERATE_MDDEV(mddev
,tmp
) {
1524 bitmap_print_sb(mddev
->bitmap
);
1526 printk("%s: ", mdname(mddev
));
1527 ITERATE_RDEV(mddev
,rdev
,tmp2
)
1528 printk("<%s>", bdevname(rdev
->bdev
,b
));
1531 ITERATE_RDEV(mddev
,rdev
,tmp2
)
1534 printk("md: **********************************\n");
1539 static void sync_sbs(mddev_t
* mddev
)
1542 struct list_head
*tmp
;
1544 ITERATE_RDEV(mddev
,rdev
,tmp
) {
1545 super_types
[mddev
->major_version
].
1546 sync_super(mddev
, rdev
);
1547 rdev
->sb_loaded
= 1;
1551 void md_update_sb(mddev_t
* mddev
)
1554 struct list_head
*tmp
;
1559 spin_lock_irq(&mddev
->write_lock
);
1560 sync_req
= mddev
->in_sync
;
1561 mddev
->utime
= get_seconds();
1564 if (!mddev
->events
) {
1566 * oops, this 64-bit counter should never wrap.
1567 * Either we are in around ~1 trillion A.C., assuming
1568 * 1 reboot per second, or we have a bug:
1573 mddev
->sb_dirty
= 2;
1577 * do not write anything to disk if using
1578 * nonpersistent superblocks
1580 if (!mddev
->persistent
) {
1581 mddev
->sb_dirty
= 0;
1582 spin_unlock_irq(&mddev
->write_lock
);
1583 wake_up(&mddev
->sb_wait
);
1586 spin_unlock_irq(&mddev
->write_lock
);
1589 "md: updating %s RAID superblock on device (in sync %d)\n",
1590 mdname(mddev
),mddev
->in_sync
);
1592 err
= bitmap_update_sb(mddev
->bitmap
);
1593 ITERATE_RDEV(mddev
,rdev
,tmp
) {
1594 char b
[BDEVNAME_SIZE
];
1595 dprintk(KERN_INFO
"md: ");
1596 if (test_bit(Faulty
, &rdev
->flags
))
1597 dprintk("(skipping faulty ");
1599 dprintk("%s ", bdevname(rdev
->bdev
,b
));
1600 if (!test_bit(Faulty
, &rdev
->flags
)) {
1601 md_super_write(mddev
,rdev
,
1602 rdev
->sb_offset
<<1, rdev
->sb_size
,
1604 dprintk(KERN_INFO
"(write) %s's sb offset: %llu\n",
1605 bdevname(rdev
->bdev
,b
),
1606 (unsigned long long)rdev
->sb_offset
);
1610 if (mddev
->level
== LEVEL_MULTIPATH
)
1611 /* only need to write one superblock... */
1614 md_super_wait(mddev
);
1615 /* if there was a failure, sb_dirty was set to 1, and we re-write super */
1617 spin_lock_irq(&mddev
->write_lock
);
1618 if (mddev
->in_sync
!= sync_req
|| mddev
->sb_dirty
== 1) {
1619 /* have to write it out again */
1620 spin_unlock_irq(&mddev
->write_lock
);
1623 mddev
->sb_dirty
= 0;
1624 spin_unlock_irq(&mddev
->write_lock
);
1625 wake_up(&mddev
->sb_wait
);
1628 EXPORT_SYMBOL_GPL(md_update_sb
);
1630 /* words written to sysfs files may, or my not, be \n terminated.
1631 * We want to accept with case. For this we use cmd_match.
1633 static int cmd_match(const char *cmd
, const char *str
)
1635 /* See if cmd, written into a sysfs file, matches
1636 * str. They must either be the same, or cmd can
1637 * have a trailing newline
1639 while (*cmd
&& *str
&& *cmd
== *str
) {
1650 struct rdev_sysfs_entry
{
1651 struct attribute attr
;
1652 ssize_t (*show
)(mdk_rdev_t
*, char *);
1653 ssize_t (*store
)(mdk_rdev_t
*, const char *, size_t);
1657 state_show(mdk_rdev_t
*rdev
, char *page
)
1662 if (test_bit(Faulty
, &rdev
->flags
)) {
1663 len
+= sprintf(page
+len
, "%sfaulty",sep
);
1666 if (test_bit(In_sync
, &rdev
->flags
)) {
1667 len
+= sprintf(page
+len
, "%sin_sync",sep
);
1670 if (!test_bit(Faulty
, &rdev
->flags
) &&
1671 !test_bit(In_sync
, &rdev
->flags
)) {
1672 len
+= sprintf(page
+len
, "%sspare", sep
);
1675 return len
+sprintf(page
+len
, "\n");
1678 static struct rdev_sysfs_entry
1679 rdev_state
= __ATTR_RO(state
);
1682 super_show(mdk_rdev_t
*rdev
, char *page
)
1684 if (rdev
->sb_loaded
&& rdev
->sb_size
) {
1685 memcpy(page
, page_address(rdev
->sb_page
), rdev
->sb_size
);
1686 return rdev
->sb_size
;
1690 static struct rdev_sysfs_entry rdev_super
= __ATTR_RO(super
);
1693 errors_show(mdk_rdev_t
*rdev
, char *page
)
1695 return sprintf(page
, "%d\n", atomic_read(&rdev
->corrected_errors
));
1699 errors_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
1702 unsigned long n
= simple_strtoul(buf
, &e
, 10);
1703 if (*buf
&& (*e
== 0 || *e
== '\n')) {
1704 atomic_set(&rdev
->corrected_errors
, n
);
1709 static struct rdev_sysfs_entry rdev_errors
=
1710 __ATTR(errors
, 0644, errors_show
, errors_store
);
1713 slot_show(mdk_rdev_t
*rdev
, char *page
)
1715 if (rdev
->raid_disk
< 0)
1716 return sprintf(page
, "none\n");
1718 return sprintf(page
, "%d\n", rdev
->raid_disk
);
1722 slot_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
1725 int slot
= simple_strtoul(buf
, &e
, 10);
1726 if (strncmp(buf
, "none", 4)==0)
1728 else if (e
==buf
|| (*e
&& *e
!= '\n'))
1730 if (rdev
->mddev
->pers
)
1731 /* Cannot set slot in active array (yet) */
1733 if (slot
>= rdev
->mddev
->raid_disks
)
1735 rdev
->raid_disk
= slot
;
1736 /* assume it is working */
1738 set_bit(In_sync
, &rdev
->flags
);
1743 static struct rdev_sysfs_entry rdev_slot
=
1744 __ATTR(slot
, 0644, slot_show
, slot_store
);
1747 offset_show(mdk_rdev_t
*rdev
, char *page
)
1749 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->data_offset
);
1753 offset_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
1756 unsigned long long offset
= simple_strtoull(buf
, &e
, 10);
1757 if (e
==buf
|| (*e
&& *e
!= '\n'))
1759 if (rdev
->mddev
->pers
)
1761 rdev
->data_offset
= offset
;
1765 static struct rdev_sysfs_entry rdev_offset
=
1766 __ATTR(offset
, 0644, offset_show
, offset_store
);
1769 rdev_size_show(mdk_rdev_t
*rdev
, char *page
)
1771 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->size
);
1775 rdev_size_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
1778 unsigned long long size
= simple_strtoull(buf
, &e
, 10);
1779 if (e
==buf
|| (*e
&& *e
!= '\n'))
1781 if (rdev
->mddev
->pers
)
1784 if (size
< rdev
->mddev
->size
|| rdev
->mddev
->size
== 0)
1785 rdev
->mddev
->size
= size
;
1789 static struct rdev_sysfs_entry rdev_size
=
1790 __ATTR(size
, 0644, rdev_size_show
, rdev_size_store
);
1792 static struct attribute
*rdev_default_attrs
[] = {
1802 rdev_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
1804 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
1805 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
1809 return entry
->show(rdev
, page
);
1813 rdev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
1814 const char *page
, size_t length
)
1816 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
1817 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
1821 return entry
->store(rdev
, page
, length
);
1824 static void rdev_free(struct kobject
*ko
)
1826 mdk_rdev_t
*rdev
= container_of(ko
, mdk_rdev_t
, kobj
);
1829 static struct sysfs_ops rdev_sysfs_ops
= {
1830 .show
= rdev_attr_show
,
1831 .store
= rdev_attr_store
,
1833 static struct kobj_type rdev_ktype
= {
1834 .release
= rdev_free
,
1835 .sysfs_ops
= &rdev_sysfs_ops
,
1836 .default_attrs
= rdev_default_attrs
,
1840 * Import a device. If 'super_format' >= 0, then sanity check the superblock
1842 * mark the device faulty if:
1844 * - the device is nonexistent (zero size)
1845 * - the device has no valid superblock
1847 * a faulty rdev _never_ has rdev->sb set.
1849 static mdk_rdev_t
*md_import_device(dev_t newdev
, int super_format
, int super_minor
)
1851 char b
[BDEVNAME_SIZE
];
1856 rdev
= kzalloc(sizeof(*rdev
), GFP_KERNEL
);
1858 printk(KERN_ERR
"md: could not alloc mem for new device!\n");
1859 return ERR_PTR(-ENOMEM
);
1862 if ((err
= alloc_disk_sb(rdev
)))
1865 err
= lock_rdev(rdev
, newdev
);
1869 rdev
->kobj
.parent
= NULL
;
1870 rdev
->kobj
.ktype
= &rdev_ktype
;
1871 kobject_init(&rdev
->kobj
);
1875 rdev
->data_offset
= 0;
1876 atomic_set(&rdev
->nr_pending
, 0);
1877 atomic_set(&rdev
->read_errors
, 0);
1878 atomic_set(&rdev
->corrected_errors
, 0);
1880 size
= rdev
->bdev
->bd_inode
->i_size
>> BLOCK_SIZE_BITS
;
1883 "md: %s has zero or unknown size, marking faulty!\n",
1884 bdevname(rdev
->bdev
,b
));
1889 if (super_format
>= 0) {
1890 err
= super_types
[super_format
].
1891 load_super(rdev
, NULL
, super_minor
);
1892 if (err
== -EINVAL
) {
1894 "md: %s has invalid sb, not importing!\n",
1895 bdevname(rdev
->bdev
,b
));
1900 "md: could not read %s's sb, not importing!\n",
1901 bdevname(rdev
->bdev
,b
));
1905 INIT_LIST_HEAD(&rdev
->same_set
);
1910 if (rdev
->sb_page
) {
1916 return ERR_PTR(err
);
1920 * Check a full RAID array for plausibility
1924 static void analyze_sbs(mddev_t
* mddev
)
1927 struct list_head
*tmp
;
1928 mdk_rdev_t
*rdev
, *freshest
;
1929 char b
[BDEVNAME_SIZE
];
1932 ITERATE_RDEV(mddev
,rdev
,tmp
)
1933 switch (super_types
[mddev
->major_version
].
1934 load_super(rdev
, freshest
, mddev
->minor_version
)) {
1942 "md: fatal superblock inconsistency in %s"
1943 " -- removing from array\n",
1944 bdevname(rdev
->bdev
,b
));
1945 kick_rdev_from_array(rdev
);
1949 super_types
[mddev
->major_version
].
1950 validate_super(mddev
, freshest
);
1953 ITERATE_RDEV(mddev
,rdev
,tmp
) {
1954 if (rdev
!= freshest
)
1955 if (super_types
[mddev
->major_version
].
1956 validate_super(mddev
, rdev
)) {
1957 printk(KERN_WARNING
"md: kicking non-fresh %s"
1959 bdevname(rdev
->bdev
,b
));
1960 kick_rdev_from_array(rdev
);
1963 if (mddev
->level
== LEVEL_MULTIPATH
) {
1964 rdev
->desc_nr
= i
++;
1965 rdev
->raid_disk
= rdev
->desc_nr
;
1966 set_bit(In_sync
, &rdev
->flags
);
1972 if (mddev
->recovery_cp
!= MaxSector
&&
1974 printk(KERN_ERR
"md: %s: raid array is not clean"
1975 " -- starting background reconstruction\n",
1981 level_show(mddev_t
*mddev
, char *page
)
1983 struct mdk_personality
*p
= mddev
->pers
;
1985 return sprintf(page
, "%s\n", p
->name
);
1986 else if (mddev
->clevel
[0])
1987 return sprintf(page
, "%s\n", mddev
->clevel
);
1988 else if (mddev
->level
!= LEVEL_NONE
)
1989 return sprintf(page
, "%d\n", mddev
->level
);
1995 level_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2002 if (len
>= sizeof(mddev
->clevel
))
2004 strncpy(mddev
->clevel
, buf
, len
);
2005 if (mddev
->clevel
[len
-1] == '\n')
2007 mddev
->clevel
[len
] = 0;
2008 mddev
->level
= LEVEL_NONE
;
2012 static struct md_sysfs_entry md_level
=
2013 __ATTR(level
, 0644, level_show
, level_store
);
2016 raid_disks_show(mddev_t
*mddev
, char *page
)
2018 if (mddev
->raid_disks
== 0)
2020 return sprintf(page
, "%d\n", mddev
->raid_disks
);
2023 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
);
2026 raid_disks_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2028 /* can only set raid_disks if array is not yet active */
2031 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2033 if (!*buf
|| (*e
&& *e
!= '\n'))
2037 rv
= update_raid_disks(mddev
, n
);
2039 mddev
->raid_disks
= n
;
2040 return rv
? rv
: len
;
2042 static struct md_sysfs_entry md_raid_disks
=
2043 __ATTR(raid_disks
, 0644, raid_disks_show
, raid_disks_store
);
2046 chunk_size_show(mddev_t
*mddev
, char *page
)
2048 return sprintf(page
, "%d\n", mddev
->chunk_size
);
2052 chunk_size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2054 /* can only set chunk_size if array is not yet active */
2056 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2060 if (!*buf
|| (*e
&& *e
!= '\n'))
2063 mddev
->chunk_size
= n
;
2066 static struct md_sysfs_entry md_chunk_size
=
2067 __ATTR(chunk_size
, 0644, chunk_size_show
, chunk_size_store
);
2070 null_show(mddev_t
*mddev
, char *page
)
2076 new_dev_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2078 /* buf must be %d:%d\n? giving major and minor numbers */
2079 /* The new device is added to the array.
2080 * If the array has a persistent superblock, we read the
2081 * superblock to initialise info and check validity.
2082 * Otherwise, only checking done is that in bind_rdev_to_array,
2083 * which mainly checks size.
2086 int major
= simple_strtoul(buf
, &e
, 10);
2092 if (!*buf
|| *e
!= ':' || !e
[1] || e
[1] == '\n')
2094 minor
= simple_strtoul(e
+1, &e
, 10);
2095 if (*e
&& *e
!= '\n')
2097 dev
= MKDEV(major
, minor
);
2098 if (major
!= MAJOR(dev
) ||
2099 minor
!= MINOR(dev
))
2103 if (mddev
->persistent
) {
2104 rdev
= md_import_device(dev
, mddev
->major_version
,
2105 mddev
->minor_version
);
2106 if (!IS_ERR(rdev
) && !list_empty(&mddev
->disks
)) {
2107 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
2108 mdk_rdev_t
, same_set
);
2109 err
= super_types
[mddev
->major_version
]
2110 .load_super(rdev
, rdev0
, mddev
->minor_version
);
2115 rdev
= md_import_device(dev
, -1, -1);
2118 return PTR_ERR(rdev
);
2119 err
= bind_rdev_to_array(rdev
, mddev
);
2123 return err
? err
: len
;
2126 static struct md_sysfs_entry md_new_device
=
2127 __ATTR(new_dev
, 0200, null_show
, new_dev_store
);
2130 size_show(mddev_t
*mddev
, char *page
)
2132 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->size
);
2135 static int update_size(mddev_t
*mddev
, unsigned long size
);
2138 size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2140 /* If array is inactive, we can reduce the component size, but
2141 * not increase it (except from 0).
2142 * If array is active, we can try an on-line resize
2146 unsigned long long size
= simple_strtoull(buf
, &e
, 10);
2147 if (!*buf
|| *buf
== '\n' ||
2152 err
= update_size(mddev
, size
);
2153 md_update_sb(mddev
);
2155 if (mddev
->size
== 0 ||
2161 return err
? err
: len
;
2164 static struct md_sysfs_entry md_size
=
2165 __ATTR(component_size
, 0644, size_show
, size_store
);
2169 * This is either 'none' for arrays with externally managed metadata,
2170 * or N.M for internally known formats
2173 metadata_show(mddev_t
*mddev
, char *page
)
2175 if (mddev
->persistent
)
2176 return sprintf(page
, "%d.%d\n",
2177 mddev
->major_version
, mddev
->minor_version
);
2179 return sprintf(page
, "none\n");
2183 metadata_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2187 if (!list_empty(&mddev
->disks
))
2190 if (cmd_match(buf
, "none")) {
2191 mddev
->persistent
= 0;
2192 mddev
->major_version
= 0;
2193 mddev
->minor_version
= 90;
2196 major
= simple_strtoul(buf
, &e
, 10);
2197 if (e
==buf
|| *e
!= '.')
2200 minor
= simple_strtoul(buf
, &e
, 10);
2201 if (e
==buf
|| *e
!= '\n')
2203 if (major
>= sizeof(super_types
)/sizeof(super_types
[0]) ||
2204 super_types
[major
].name
== NULL
)
2206 mddev
->major_version
= major
;
2207 mddev
->minor_version
= minor
;
2208 mddev
->persistent
= 1;
2212 static struct md_sysfs_entry md_metadata
=
2213 __ATTR(metadata_version
, 0644, metadata_show
, metadata_store
);
2216 action_show(mddev_t
*mddev
, char *page
)
2218 char *type
= "idle";
2219 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
2220 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
)) {
2221 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
2223 else if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
2224 if (!test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
2226 else if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
2233 return sprintf(page
, "%s\n", type
);
2237 action_store(mddev_t
*mddev
, const char *page
, size_t len
)
2239 if (!mddev
->pers
|| !mddev
->pers
->sync_request
)
2242 if (cmd_match(page
, "idle")) {
2243 if (mddev
->sync_thread
) {
2244 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
2245 md_unregister_thread(mddev
->sync_thread
);
2246 mddev
->sync_thread
= NULL
;
2247 mddev
->recovery
= 0;
2249 } else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
2250 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
2252 else if (cmd_match(page
, "resync") || cmd_match(page
, "recover"))
2253 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
2254 else if (cmd_match(page
, "reshape")) {
2256 if (mddev
->pers
->start_reshape
== NULL
)
2258 err
= mddev
->pers
->start_reshape(mddev
);
2262 if (cmd_match(page
, "check"))
2263 set_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
2264 else if (!cmd_match(page
, "repair"))
2266 set_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
2267 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
2269 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
2270 md_wakeup_thread(mddev
->thread
);
2275 mismatch_cnt_show(mddev_t
*mddev
, char *page
)
2277 return sprintf(page
, "%llu\n",
2278 (unsigned long long) mddev
->resync_mismatches
);
2281 static struct md_sysfs_entry
2282 md_scan_mode
= __ATTR(sync_action
, S_IRUGO
|S_IWUSR
, action_show
, action_store
);
2285 static struct md_sysfs_entry
2286 md_mismatches
= __ATTR_RO(mismatch_cnt
);
2289 sync_min_show(mddev_t
*mddev
, char *page
)
2291 return sprintf(page
, "%d (%s)\n", speed_min(mddev
),
2292 mddev
->sync_speed_min
? "local": "system");
2296 sync_min_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2300 if (strncmp(buf
, "system", 6)==0) {
2301 mddev
->sync_speed_min
= 0;
2304 min
= simple_strtoul(buf
, &e
, 10);
2305 if (buf
== e
|| (*e
&& *e
!= '\n') || min
<= 0)
2307 mddev
->sync_speed_min
= min
;
2311 static struct md_sysfs_entry md_sync_min
=
2312 __ATTR(sync_speed_min
, S_IRUGO
|S_IWUSR
, sync_min_show
, sync_min_store
);
2315 sync_max_show(mddev_t
*mddev
, char *page
)
2317 return sprintf(page
, "%d (%s)\n", speed_max(mddev
),
2318 mddev
->sync_speed_max
? "local": "system");
2322 sync_max_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2326 if (strncmp(buf
, "system", 6)==0) {
2327 mddev
->sync_speed_max
= 0;
2330 max
= simple_strtoul(buf
, &e
, 10);
2331 if (buf
== e
|| (*e
&& *e
!= '\n') || max
<= 0)
2333 mddev
->sync_speed_max
= max
;
2337 static struct md_sysfs_entry md_sync_max
=
2338 __ATTR(sync_speed_max
, S_IRUGO
|S_IWUSR
, sync_max_show
, sync_max_store
);
2342 sync_speed_show(mddev_t
*mddev
, char *page
)
2344 unsigned long resync
, dt
, db
;
2345 resync
= (mddev
->curr_resync
- atomic_read(&mddev
->recovery_active
));
2346 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
2348 db
= resync
- (mddev
->resync_mark_cnt
);
2349 return sprintf(page
, "%ld\n", db
/dt
/2); /* K/sec */
2352 static struct md_sysfs_entry
2353 md_sync_speed
= __ATTR_RO(sync_speed
);
2356 sync_completed_show(mddev_t
*mddev
, char *page
)
2358 unsigned long max_blocks
, resync
;
2360 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
2361 max_blocks
= mddev
->resync_max_sectors
;
2363 max_blocks
= mddev
->size
<< 1;
2365 resync
= (mddev
->curr_resync
- atomic_read(&mddev
->recovery_active
));
2366 return sprintf(page
, "%lu / %lu\n", resync
, max_blocks
);
2369 static struct md_sysfs_entry
2370 md_sync_completed
= __ATTR_RO(sync_completed
);
2373 suspend_lo_show(mddev_t
*mddev
, char *page
)
2375 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_lo
);
2379 suspend_lo_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2382 unsigned long long new = simple_strtoull(buf
, &e
, 10);
2384 if (mddev
->pers
->quiesce
== NULL
)
2386 if (buf
== e
|| (*e
&& *e
!= '\n'))
2388 if (new >= mddev
->suspend_hi
||
2389 (new > mddev
->suspend_lo
&& new < mddev
->suspend_hi
)) {
2390 mddev
->suspend_lo
= new;
2391 mddev
->pers
->quiesce(mddev
, 2);
2396 static struct md_sysfs_entry md_suspend_lo
=
2397 __ATTR(suspend_lo
, S_IRUGO
|S_IWUSR
, suspend_lo_show
, suspend_lo_store
);
2401 suspend_hi_show(mddev_t
*mddev
, char *page
)
2403 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_hi
);
2407 suspend_hi_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2410 unsigned long long new = simple_strtoull(buf
, &e
, 10);
2412 if (mddev
->pers
->quiesce
== NULL
)
2414 if (buf
== e
|| (*e
&& *e
!= '\n'))
2416 if ((new <= mddev
->suspend_lo
&& mddev
->suspend_lo
>= mddev
->suspend_hi
) ||
2417 (new > mddev
->suspend_lo
&& new > mddev
->suspend_hi
)) {
2418 mddev
->suspend_hi
= new;
2419 mddev
->pers
->quiesce(mddev
, 1);
2420 mddev
->pers
->quiesce(mddev
, 0);
2425 static struct md_sysfs_entry md_suspend_hi
=
2426 __ATTR(suspend_hi
, S_IRUGO
|S_IWUSR
, suspend_hi_show
, suspend_hi_store
);
2429 static struct attribute
*md_default_attrs
[] = {
2431 &md_raid_disks
.attr
,
2432 &md_chunk_size
.attr
,
2435 &md_new_device
.attr
,
2439 static struct attribute
*md_redundancy_attrs
[] = {
2441 &md_mismatches
.attr
,
2444 &md_sync_speed
.attr
,
2445 &md_sync_completed
.attr
,
2446 &md_suspend_lo
.attr
,
2447 &md_suspend_hi
.attr
,
2450 static struct attribute_group md_redundancy_group
= {
2452 .attrs
= md_redundancy_attrs
,
2457 md_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
2459 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
2460 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
2465 rv
= mddev_lock(mddev
);
2467 rv
= entry
->show(mddev
, page
);
2468 mddev_unlock(mddev
);
2474 md_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
2475 const char *page
, size_t length
)
2477 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
2478 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
2483 rv
= mddev_lock(mddev
);
2485 rv
= entry
->store(mddev
, page
, length
);
2486 mddev_unlock(mddev
);
2491 static void md_free(struct kobject
*ko
)
2493 mddev_t
*mddev
= container_of(ko
, mddev_t
, kobj
);
2497 static struct sysfs_ops md_sysfs_ops
= {
2498 .show
= md_attr_show
,
2499 .store
= md_attr_store
,
2501 static struct kobj_type md_ktype
= {
2503 .sysfs_ops
= &md_sysfs_ops
,
2504 .default_attrs
= md_default_attrs
,
2509 static struct kobject
*md_probe(dev_t dev
, int *part
, void *data
)
2511 static DEFINE_MUTEX(disks_mutex
);
2512 mddev_t
*mddev
= mddev_find(dev
);
2513 struct gendisk
*disk
;
2514 int partitioned
= (MAJOR(dev
) != MD_MAJOR
);
2515 int shift
= partitioned
? MdpMinorShift
: 0;
2516 int unit
= MINOR(dev
) >> shift
;
2521 mutex_lock(&disks_mutex
);
2522 if (mddev
->gendisk
) {
2523 mutex_unlock(&disks_mutex
);
2527 disk
= alloc_disk(1 << shift
);
2529 mutex_unlock(&disks_mutex
);
2533 disk
->major
= MAJOR(dev
);
2534 disk
->first_minor
= unit
<< shift
;
2536 sprintf(disk
->disk_name
, "md_d%d", unit
);
2537 sprintf(disk
->devfs_name
, "md/d%d", unit
);
2539 sprintf(disk
->disk_name
, "md%d", unit
);
2540 sprintf(disk
->devfs_name
, "md/%d", unit
);
2542 disk
->fops
= &md_fops
;
2543 disk
->private_data
= mddev
;
2544 disk
->queue
= mddev
->queue
;
2546 mddev
->gendisk
= disk
;
2547 mutex_unlock(&disks_mutex
);
2548 mddev
->kobj
.parent
= &disk
->kobj
;
2549 mddev
->kobj
.k_name
= NULL
;
2550 snprintf(mddev
->kobj
.name
, KOBJ_NAME_LEN
, "%s", "md");
2551 mddev
->kobj
.ktype
= &md_ktype
;
2552 kobject_register(&mddev
->kobj
);
2556 void md_wakeup_thread(mdk_thread_t
*thread
);
2558 static void md_safemode_timeout(unsigned long data
)
2560 mddev_t
*mddev
= (mddev_t
*) data
;
2562 mddev
->safemode
= 1;
2563 md_wakeup_thread(mddev
->thread
);
2566 static int start_dirty_degraded
;
2568 static int do_md_run(mddev_t
* mddev
)
2572 struct list_head
*tmp
;
2574 struct gendisk
*disk
;
2575 struct mdk_personality
*pers
;
2576 char b
[BDEVNAME_SIZE
];
2578 if (list_empty(&mddev
->disks
))
2579 /* cannot run an array with no devices.. */
2586 * Analyze all RAID superblock(s)
2588 if (!mddev
->raid_disks
)
2591 chunk_size
= mddev
->chunk_size
;
2594 if (chunk_size
> MAX_CHUNK_SIZE
) {
2595 printk(KERN_ERR
"too big chunk_size: %d > %d\n",
2596 chunk_size
, MAX_CHUNK_SIZE
);
2600 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
2602 if ( (1 << ffz(~chunk_size
)) != chunk_size
) {
2603 printk(KERN_ERR
"chunk_size of %d not valid\n", chunk_size
);
2606 if (chunk_size
< PAGE_SIZE
) {
2607 printk(KERN_ERR
"too small chunk_size: %d < %ld\n",
2608 chunk_size
, PAGE_SIZE
);
2612 /* devices must have minimum size of one chunk */
2613 ITERATE_RDEV(mddev
,rdev
,tmp
) {
2614 if (test_bit(Faulty
, &rdev
->flags
))
2616 if (rdev
->size
< chunk_size
/ 1024) {
2618 "md: Dev %s smaller than chunk_size:"
2620 bdevname(rdev
->bdev
,b
),
2621 (unsigned long long)rdev
->size
,
2629 if (mddev
->level
!= LEVEL_NONE
)
2630 request_module("md-level-%d", mddev
->level
);
2631 else if (mddev
->clevel
[0])
2632 request_module("md-%s", mddev
->clevel
);
2636 * Drop all container device buffers, from now on
2637 * the only valid external interface is through the md
2639 * Also find largest hardsector size
2641 ITERATE_RDEV(mddev
,rdev
,tmp
) {
2642 if (test_bit(Faulty
, &rdev
->flags
))
2644 sync_blockdev(rdev
->bdev
);
2645 invalidate_bdev(rdev
->bdev
, 0);
2648 md_probe(mddev
->unit
, NULL
, NULL
);
2649 disk
= mddev
->gendisk
;
2653 spin_lock(&pers_lock
);
2654 pers
= find_pers(mddev
->level
, mddev
->clevel
);
2655 if (!pers
|| !try_module_get(pers
->owner
)) {
2656 spin_unlock(&pers_lock
);
2657 if (mddev
->level
!= LEVEL_NONE
)
2658 printk(KERN_WARNING
"md: personality for level %d is not loaded!\n",
2661 printk(KERN_WARNING
"md: personality for level %s is not loaded!\n",
2666 spin_unlock(&pers_lock
);
2667 mddev
->level
= pers
->level
;
2668 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
2670 if (mddev
->reshape_position
!= MaxSector
&&
2671 pers
->start_reshape
== NULL
) {
2672 /* This personality cannot handle reshaping... */
2674 module_put(pers
->owner
);
2678 mddev
->recovery
= 0;
2679 mddev
->resync_max_sectors
= mddev
->size
<< 1; /* may be over-ridden by personality */
2680 mddev
->barriers_work
= 1;
2681 mddev
->ok_start_degraded
= start_dirty_degraded
;
2684 mddev
->ro
= 2; /* read-only, but switch on first write */
2686 err
= mddev
->pers
->run(mddev
);
2687 if (!err
&& mddev
->pers
->sync_request
) {
2688 err
= bitmap_create(mddev
);
2690 printk(KERN_ERR
"%s: failed to create bitmap (%d)\n",
2691 mdname(mddev
), err
);
2692 mddev
->pers
->stop(mddev
);
2696 printk(KERN_ERR
"md: pers->run() failed ...\n");
2697 module_put(mddev
->pers
->owner
);
2699 bitmap_destroy(mddev
);
2702 if (mddev
->pers
->sync_request
)
2703 sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
);
2704 else if (mddev
->ro
== 2) /* auto-readonly not meaningful */
2707 atomic_set(&mddev
->writes_pending
,0);
2708 mddev
->safemode
= 0;
2709 mddev
->safemode_timer
.function
= md_safemode_timeout
;
2710 mddev
->safemode_timer
.data
= (unsigned long) mddev
;
2711 mddev
->safemode_delay
= (20 * HZ
)/1000 +1; /* 20 msec delay */
2714 ITERATE_RDEV(mddev
,rdev
,tmp
)
2715 if (rdev
->raid_disk
>= 0) {
2717 sprintf(nm
, "rd%d", rdev
->raid_disk
);
2718 sysfs_create_link(&mddev
->kobj
, &rdev
->kobj
, nm
);
2721 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
2722 md_wakeup_thread(mddev
->thread
);
2724 if (mddev
->sb_dirty
)
2725 md_update_sb(mddev
);
2727 set_capacity(disk
, mddev
->array_size
<<1);
2729 /* If we call blk_queue_make_request here, it will
2730 * re-initialise max_sectors etc which may have been
2731 * refined inside -> run. So just set the bits we need to set.
2732 * Most initialisation happended when we called
2733 * blk_queue_make_request(..., md_fail_request)
2736 mddev
->queue
->queuedata
= mddev
;
2737 mddev
->queue
->make_request_fn
= mddev
->pers
->make_request
;
2740 md_new_event(mddev
);
2744 static int restart_array(mddev_t
*mddev
)
2746 struct gendisk
*disk
= mddev
->gendisk
;
2750 * Complain if it has no devices
2753 if (list_empty(&mddev
->disks
))
2761 mddev
->safemode
= 0;
2763 set_disk_ro(disk
, 0);
2765 printk(KERN_INFO
"md: %s switched to read-write mode.\n",
2768 * Kick recovery or resync if necessary
2770 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
2771 md_wakeup_thread(mddev
->thread
);
2774 printk(KERN_ERR
"md: %s has no personality assigned.\n",
2783 static int do_md_stop(mddev_t
* mddev
, int ro
)
2786 struct gendisk
*disk
= mddev
->gendisk
;
2789 if (atomic_read(&mddev
->active
)>2) {
2790 printk("md: %s still in use.\n",mdname(mddev
));
2794 if (mddev
->sync_thread
) {
2795 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
2796 md_unregister_thread(mddev
->sync_thread
);
2797 mddev
->sync_thread
= NULL
;
2800 del_timer_sync(&mddev
->safemode_timer
);
2802 invalidate_partition(disk
, 0);
2810 bitmap_flush(mddev
);
2811 md_super_wait(mddev
);
2813 set_disk_ro(disk
, 0);
2814 blk_queue_make_request(mddev
->queue
, md_fail_request
);
2815 mddev
->pers
->stop(mddev
);
2816 if (mddev
->pers
->sync_request
)
2817 sysfs_remove_group(&mddev
->kobj
, &md_redundancy_group
);
2819 module_put(mddev
->pers
->owner
);
2824 if (!mddev
->in_sync
) {
2825 /* mark array as shutdown cleanly */
2827 md_update_sb(mddev
);
2830 set_disk_ro(disk
, 1);
2834 * Free resources if final stop
2838 struct list_head
*tmp
;
2839 struct gendisk
*disk
;
2840 printk(KERN_INFO
"md: %s stopped.\n", mdname(mddev
));
2842 bitmap_destroy(mddev
);
2843 if (mddev
->bitmap_file
) {
2844 atomic_set(&mddev
->bitmap_file
->f_dentry
->d_inode
->i_writecount
, 1);
2845 fput(mddev
->bitmap_file
);
2846 mddev
->bitmap_file
= NULL
;
2848 mddev
->bitmap_offset
= 0;
2850 ITERATE_RDEV(mddev
,rdev
,tmp
)
2851 if (rdev
->raid_disk
>= 0) {
2853 sprintf(nm
, "rd%d", rdev
->raid_disk
);
2854 sysfs_remove_link(&mddev
->kobj
, nm
);
2857 export_array(mddev
);
2859 mddev
->array_size
= 0;
2860 disk
= mddev
->gendisk
;
2862 set_capacity(disk
, 0);
2865 printk(KERN_INFO
"md: %s switched to read-only mode.\n",
2868 md_new_event(mddev
);
2873 static void autorun_array(mddev_t
*mddev
)
2876 struct list_head
*tmp
;
2879 if (list_empty(&mddev
->disks
))
2882 printk(KERN_INFO
"md: running: ");
2884 ITERATE_RDEV(mddev
,rdev
,tmp
) {
2885 char b
[BDEVNAME_SIZE
];
2886 printk("<%s>", bdevname(rdev
->bdev
,b
));
2890 err
= do_md_run (mddev
);
2892 printk(KERN_WARNING
"md: do_md_run() returned %d\n", err
);
2893 do_md_stop (mddev
, 0);
2898 * lets try to run arrays based on all disks that have arrived
2899 * until now. (those are in pending_raid_disks)
2901 * the method: pick the first pending disk, collect all disks with
2902 * the same UUID, remove all from the pending list and put them into
2903 * the 'same_array' list. Then order this list based on superblock
2904 * update time (freshest comes first), kick out 'old' disks and
2905 * compare superblocks. If everything's fine then run it.
2907 * If "unit" is allocated, then bump its reference count
2909 static void autorun_devices(int part
)
2911 struct list_head
*tmp
;
2912 mdk_rdev_t
*rdev0
, *rdev
;
2914 char b
[BDEVNAME_SIZE
];
2916 printk(KERN_INFO
"md: autorun ...\n");
2917 while (!list_empty(&pending_raid_disks
)) {
2919 LIST_HEAD(candidates
);
2920 rdev0
= list_entry(pending_raid_disks
.next
,
2921 mdk_rdev_t
, same_set
);
2923 printk(KERN_INFO
"md: considering %s ...\n",
2924 bdevname(rdev0
->bdev
,b
));
2925 INIT_LIST_HEAD(&candidates
);
2926 ITERATE_RDEV_PENDING(rdev
,tmp
)
2927 if (super_90_load(rdev
, rdev0
, 0) >= 0) {
2928 printk(KERN_INFO
"md: adding %s ...\n",
2929 bdevname(rdev
->bdev
,b
));
2930 list_move(&rdev
->same_set
, &candidates
);
2933 * now we have a set of devices, with all of them having
2934 * mostly sane superblocks. It's time to allocate the
2937 if (rdev0
->preferred_minor
< 0 || rdev0
->preferred_minor
>= MAX_MD_DEVS
) {
2938 printk(KERN_INFO
"md: unit number in %s is bad: %d\n",
2939 bdevname(rdev0
->bdev
, b
), rdev0
->preferred_minor
);
2943 dev
= MKDEV(mdp_major
,
2944 rdev0
->preferred_minor
<< MdpMinorShift
);
2946 dev
= MKDEV(MD_MAJOR
, rdev0
->preferred_minor
);
2948 md_probe(dev
, NULL
, NULL
);
2949 mddev
= mddev_find(dev
);
2952 "md: cannot allocate memory for md drive.\n");
2955 if (mddev_lock(mddev
))
2956 printk(KERN_WARNING
"md: %s locked, cannot run\n",
2958 else if (mddev
->raid_disks
|| mddev
->major_version
2959 || !list_empty(&mddev
->disks
)) {
2961 "md: %s already running, cannot run %s\n",
2962 mdname(mddev
), bdevname(rdev0
->bdev
,b
));
2963 mddev_unlock(mddev
);
2965 printk(KERN_INFO
"md: created %s\n", mdname(mddev
));
2966 ITERATE_RDEV_GENERIC(candidates
,rdev
,tmp
) {
2967 list_del_init(&rdev
->same_set
);
2968 if (bind_rdev_to_array(rdev
, mddev
))
2971 autorun_array(mddev
);
2972 mddev_unlock(mddev
);
2974 /* on success, candidates will be empty, on error
2977 ITERATE_RDEV_GENERIC(candidates
,rdev
,tmp
)
2981 printk(KERN_INFO
"md: ... autorun DONE.\n");
2985 * import RAID devices based on one partition
2986 * if possible, the array gets run as well.
2989 static int autostart_array(dev_t startdev
)
2991 char b
[BDEVNAME_SIZE
];
2992 int err
= -EINVAL
, i
;
2993 mdp_super_t
*sb
= NULL
;
2994 mdk_rdev_t
*start_rdev
= NULL
, *rdev
;
2996 start_rdev
= md_import_device(startdev
, 0, 0);
2997 if (IS_ERR(start_rdev
))
3001 /* NOTE: this can only work for 0.90.0 superblocks */
3002 sb
= (mdp_super_t
*)page_address(start_rdev
->sb_page
);
3003 if (sb
->major_version
!= 0 ||
3004 sb
->minor_version
!= 90 ) {
3005 printk(KERN_WARNING
"md: can only autostart 0.90.0 arrays\n");
3006 export_rdev(start_rdev
);
3010 if (test_bit(Faulty
, &start_rdev
->flags
)) {
3012 "md: can not autostart based on faulty %s!\n",
3013 bdevname(start_rdev
->bdev
,b
));
3014 export_rdev(start_rdev
);
3017 list_add(&start_rdev
->same_set
, &pending_raid_disks
);
3019 for (i
= 0; i
< MD_SB_DISKS
; i
++) {
3020 mdp_disk_t
*desc
= sb
->disks
+ i
;
3021 dev_t dev
= MKDEV(desc
->major
, desc
->minor
);
3025 if (dev
== startdev
)
3027 if (MAJOR(dev
) != desc
->major
|| MINOR(dev
) != desc
->minor
)
3029 rdev
= md_import_device(dev
, 0, 0);
3033 list_add(&rdev
->same_set
, &pending_raid_disks
);
3037 * possibly return codes
3045 static int get_version(void __user
* arg
)
3049 ver
.major
= MD_MAJOR_VERSION
;
3050 ver
.minor
= MD_MINOR_VERSION
;
3051 ver
.patchlevel
= MD_PATCHLEVEL_VERSION
;
3053 if (copy_to_user(arg
, &ver
, sizeof(ver
)))
3059 static int get_array_info(mddev_t
* mddev
, void __user
* arg
)
3061 mdu_array_info_t info
;
3062 int nr
,working
,active
,failed
,spare
;
3064 struct list_head
*tmp
;
3066 nr
=working
=active
=failed
=spare
=0;
3067 ITERATE_RDEV(mddev
,rdev
,tmp
) {
3069 if (test_bit(Faulty
, &rdev
->flags
))
3073 if (test_bit(In_sync
, &rdev
->flags
))
3080 info
.major_version
= mddev
->major_version
;
3081 info
.minor_version
= mddev
->minor_version
;
3082 info
.patch_version
= MD_PATCHLEVEL_VERSION
;
3083 info
.ctime
= mddev
->ctime
;
3084 info
.level
= mddev
->level
;
3085 info
.size
= mddev
->size
;
3086 if (info
.size
!= mddev
->size
) /* overflow */
3089 info
.raid_disks
= mddev
->raid_disks
;
3090 info
.md_minor
= mddev
->md_minor
;
3091 info
.not_persistent
= !mddev
->persistent
;
3093 info
.utime
= mddev
->utime
;
3096 info
.state
= (1<<MD_SB_CLEAN
);
3097 if (mddev
->bitmap
&& mddev
->bitmap_offset
)
3098 info
.state
= (1<<MD_SB_BITMAP_PRESENT
);
3099 info
.active_disks
= active
;
3100 info
.working_disks
= working
;
3101 info
.failed_disks
= failed
;
3102 info
.spare_disks
= spare
;
3104 info
.layout
= mddev
->layout
;
3105 info
.chunk_size
= mddev
->chunk_size
;
3107 if (copy_to_user(arg
, &info
, sizeof(info
)))
3113 static int get_bitmap_file(mddev_t
* mddev
, void __user
* arg
)
3115 mdu_bitmap_file_t
*file
= NULL
; /* too big for stack allocation */
3116 char *ptr
, *buf
= NULL
;
3119 file
= kmalloc(sizeof(*file
), GFP_KERNEL
);
3123 /* bitmap disabled, zero the first byte and copy out */
3124 if (!mddev
->bitmap
|| !mddev
->bitmap
->file
) {
3125 file
->pathname
[0] = '\0';
3129 buf
= kmalloc(sizeof(file
->pathname
), GFP_KERNEL
);
3133 ptr
= file_path(mddev
->bitmap
->file
, buf
, sizeof(file
->pathname
));
3137 strcpy(file
->pathname
, ptr
);
3141 if (copy_to_user(arg
, file
, sizeof(*file
)))
3149 static int get_disk_info(mddev_t
* mddev
, void __user
* arg
)
3151 mdu_disk_info_t info
;
3155 if (copy_from_user(&info
, arg
, sizeof(info
)))
3160 rdev
= find_rdev_nr(mddev
, nr
);
3162 info
.major
= MAJOR(rdev
->bdev
->bd_dev
);
3163 info
.minor
= MINOR(rdev
->bdev
->bd_dev
);
3164 info
.raid_disk
= rdev
->raid_disk
;
3166 if (test_bit(Faulty
, &rdev
->flags
))
3167 info
.state
|= (1<<MD_DISK_FAULTY
);
3168 else if (test_bit(In_sync
, &rdev
->flags
)) {
3169 info
.state
|= (1<<MD_DISK_ACTIVE
);
3170 info
.state
|= (1<<MD_DISK_SYNC
);
3172 if (test_bit(WriteMostly
, &rdev
->flags
))
3173 info
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
3175 info
.major
= info
.minor
= 0;
3176 info
.raid_disk
= -1;
3177 info
.state
= (1<<MD_DISK_REMOVED
);
3180 if (copy_to_user(arg
, &info
, sizeof(info
)))
3186 static int add_new_disk(mddev_t
* mddev
, mdu_disk_info_t
*info
)
3188 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
3190 dev_t dev
= MKDEV(info
->major
,info
->minor
);
3192 if (info
->major
!= MAJOR(dev
) || info
->minor
!= MINOR(dev
))
3195 if (!mddev
->raid_disks
) {
3197 /* expecting a device which has a superblock */
3198 rdev
= md_import_device(dev
, mddev
->major_version
, mddev
->minor_version
);
3201 "md: md_import_device returned %ld\n",
3203 return PTR_ERR(rdev
);
3205 if (!list_empty(&mddev
->disks
)) {
3206 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
3207 mdk_rdev_t
, same_set
);
3208 int err
= super_types
[mddev
->major_version
]
3209 .load_super(rdev
, rdev0
, mddev
->minor_version
);
3212 "md: %s has different UUID to %s\n",
3213 bdevname(rdev
->bdev
,b
),
3214 bdevname(rdev0
->bdev
,b2
));
3219 err
= bind_rdev_to_array(rdev
, mddev
);
3226 * add_new_disk can be used once the array is assembled
3227 * to add "hot spares". They must already have a superblock
3232 if (!mddev
->pers
->hot_add_disk
) {
3234 "%s: personality does not support diskops!\n",
3238 if (mddev
->persistent
)
3239 rdev
= md_import_device(dev
, mddev
->major_version
,
3240 mddev
->minor_version
);
3242 rdev
= md_import_device(dev
, -1, -1);
3245 "md: md_import_device returned %ld\n",
3247 return PTR_ERR(rdev
);
3249 /* set save_raid_disk if appropriate */
3250 if (!mddev
->persistent
) {
3251 if (info
->state
& (1<<MD_DISK_SYNC
) &&
3252 info
->raid_disk
< mddev
->raid_disks
)
3253 rdev
->raid_disk
= info
->raid_disk
;
3255 rdev
->raid_disk
= -1;
3257 super_types
[mddev
->major_version
].
3258 validate_super(mddev
, rdev
);
3259 rdev
->saved_raid_disk
= rdev
->raid_disk
;
3261 clear_bit(In_sync
, &rdev
->flags
); /* just to be sure */
3262 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
3263 set_bit(WriteMostly
, &rdev
->flags
);
3265 rdev
->raid_disk
= -1;
3266 err
= bind_rdev_to_array(rdev
, mddev
);
3270 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3271 md_wakeup_thread(mddev
->thread
);
3275 /* otherwise, add_new_disk is only allowed
3276 * for major_version==0 superblocks
3278 if (mddev
->major_version
!= 0) {
3279 printk(KERN_WARNING
"%s: ADD_NEW_DISK not supported\n",
3284 if (!(info
->state
& (1<<MD_DISK_FAULTY
))) {
3286 rdev
= md_import_device (dev
, -1, 0);
3289 "md: error, md_import_device() returned %ld\n",
3291 return PTR_ERR(rdev
);
3293 rdev
->desc_nr
= info
->number
;
3294 if (info
->raid_disk
< mddev
->raid_disks
)
3295 rdev
->raid_disk
= info
->raid_disk
;
3297 rdev
->raid_disk
= -1;
3301 if (rdev
->raid_disk
< mddev
->raid_disks
)
3302 if (info
->state
& (1<<MD_DISK_SYNC
))
3303 set_bit(In_sync
, &rdev
->flags
);
3305 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
3306 set_bit(WriteMostly
, &rdev
->flags
);
3308 if (!mddev
->persistent
) {
3309 printk(KERN_INFO
"md: nonpersistent superblock ...\n");
3310 rdev
->sb_offset
= rdev
->bdev
->bd_inode
->i_size
>> BLOCK_SIZE_BITS
;
3312 rdev
->sb_offset
= calc_dev_sboffset(rdev
->bdev
);
3313 rdev
->size
= calc_dev_size(rdev
, mddev
->chunk_size
);
3315 err
= bind_rdev_to_array(rdev
, mddev
);
3325 static int hot_remove_disk(mddev_t
* mddev
, dev_t dev
)
3327 char b
[BDEVNAME_SIZE
];
3333 rdev
= find_rdev(mddev
, dev
);
3337 if (rdev
->raid_disk
>= 0)
3340 kick_rdev_from_array(rdev
);
3341 md_update_sb(mddev
);
3342 md_new_event(mddev
);
3346 printk(KERN_WARNING
"md: cannot remove active disk %s from %s ... \n",
3347 bdevname(rdev
->bdev
,b
), mdname(mddev
));
3351 static int hot_add_disk(mddev_t
* mddev
, dev_t dev
)
3353 char b
[BDEVNAME_SIZE
];
3361 if (mddev
->major_version
!= 0) {
3362 printk(KERN_WARNING
"%s: HOT_ADD may only be used with"
3363 " version-0 superblocks.\n",
3367 if (!mddev
->pers
->hot_add_disk
) {
3369 "%s: personality does not support diskops!\n",
3374 rdev
= md_import_device (dev
, -1, 0);
3377 "md: error, md_import_device() returned %ld\n",
3382 if (mddev
->persistent
)
3383 rdev
->sb_offset
= calc_dev_sboffset(rdev
->bdev
);
3386 rdev
->bdev
->bd_inode
->i_size
>> BLOCK_SIZE_BITS
;
3388 size
= calc_dev_size(rdev
, mddev
->chunk_size
);
3391 if (test_bit(Faulty
, &rdev
->flags
)) {
3393 "md: can not hot-add faulty %s disk to %s!\n",
3394 bdevname(rdev
->bdev
,b
), mdname(mddev
));
3398 clear_bit(In_sync
, &rdev
->flags
);
3400 err
= bind_rdev_to_array(rdev
, mddev
);
3405 * The rest should better be atomic, we can have disk failures
3406 * noticed in interrupt contexts ...
3409 if (rdev
->desc_nr
== mddev
->max_disks
) {
3410 printk(KERN_WARNING
"%s: can not hot-add to full array!\n",
3413 goto abort_unbind_export
;
3416 rdev
->raid_disk
= -1;
3418 md_update_sb(mddev
);
3421 * Kick recovery, maybe this spare has to be added to the
3422 * array immediately.
3424 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3425 md_wakeup_thread(mddev
->thread
);
3426 md_new_event(mddev
);
3429 abort_unbind_export
:
3430 unbind_rdev_from_array(rdev
);
3437 /* similar to deny_write_access, but accounts for our holding a reference
3438 * to the file ourselves */
3439 static int deny_bitmap_write_access(struct file
* file
)
3441 struct inode
*inode
= file
->f_mapping
->host
;
3443 spin_lock(&inode
->i_lock
);
3444 if (atomic_read(&inode
->i_writecount
) > 1) {
3445 spin_unlock(&inode
->i_lock
);
3448 atomic_set(&inode
->i_writecount
, -1);
3449 spin_unlock(&inode
->i_lock
);
3454 static int set_bitmap_file(mddev_t
*mddev
, int fd
)
3459 if (!mddev
->pers
->quiesce
)
3461 if (mddev
->recovery
|| mddev
->sync_thread
)
3463 /* we should be able to change the bitmap.. */
3469 return -EEXIST
; /* cannot add when bitmap is present */
3470 mddev
->bitmap_file
= fget(fd
);
3472 if (mddev
->bitmap_file
== NULL
) {
3473 printk(KERN_ERR
"%s: error: failed to get bitmap file\n",
3478 err
= deny_bitmap_write_access(mddev
->bitmap_file
);
3480 printk(KERN_ERR
"%s: error: bitmap file is already in use\n",
3482 fput(mddev
->bitmap_file
);
3483 mddev
->bitmap_file
= NULL
;
3486 mddev
->bitmap_offset
= 0; /* file overrides offset */
3487 } else if (mddev
->bitmap
== NULL
)
3488 return -ENOENT
; /* cannot remove what isn't there */
3491 mddev
->pers
->quiesce(mddev
, 1);
3493 err
= bitmap_create(mddev
);
3495 bitmap_destroy(mddev
);
3496 mddev
->pers
->quiesce(mddev
, 0);
3497 } else if (fd
< 0) {
3498 if (mddev
->bitmap_file
)
3499 fput(mddev
->bitmap_file
);
3500 mddev
->bitmap_file
= NULL
;
3507 * set_array_info is used two different ways
3508 * The original usage is when creating a new array.
3509 * In this usage, raid_disks is > 0 and it together with
3510 * level, size, not_persistent,layout,chunksize determine the
3511 * shape of the array.
3512 * This will always create an array with a type-0.90.0 superblock.
3513 * The newer usage is when assembling an array.
3514 * In this case raid_disks will be 0, and the major_version field is
3515 * use to determine which style super-blocks are to be found on the devices.
3516 * The minor and patch _version numbers are also kept incase the
3517 * super_block handler wishes to interpret them.
3519 static int set_array_info(mddev_t
* mddev
, mdu_array_info_t
*info
)
3522 if (info
->raid_disks
== 0) {
3523 /* just setting version number for superblock loading */
3524 if (info
->major_version
< 0 ||
3525 info
->major_version
>= sizeof(super_types
)/sizeof(super_types
[0]) ||
3526 super_types
[info
->major_version
].name
== NULL
) {
3527 /* maybe try to auto-load a module? */
3529 "md: superblock version %d not known\n",
3530 info
->major_version
);
3533 mddev
->major_version
= info
->major_version
;
3534 mddev
->minor_version
= info
->minor_version
;
3535 mddev
->patch_version
= info
->patch_version
;
3538 mddev
->major_version
= MD_MAJOR_VERSION
;
3539 mddev
->minor_version
= MD_MINOR_VERSION
;
3540 mddev
->patch_version
= MD_PATCHLEVEL_VERSION
;
3541 mddev
->ctime
= get_seconds();
3543 mddev
->level
= info
->level
;
3544 mddev
->clevel
[0] = 0;
3545 mddev
->size
= info
->size
;
3546 mddev
->raid_disks
= info
->raid_disks
;
3547 /* don't set md_minor, it is determined by which /dev/md* was
3550 if (info
->state
& (1<<MD_SB_CLEAN
))
3551 mddev
->recovery_cp
= MaxSector
;
3553 mddev
->recovery_cp
= 0;
3554 mddev
->persistent
= ! info
->not_persistent
;
3556 mddev
->layout
= info
->layout
;
3557 mddev
->chunk_size
= info
->chunk_size
;
3559 mddev
->max_disks
= MD_SB_DISKS
;
3561 mddev
->sb_dirty
= 1;
3563 mddev
->default_bitmap_offset
= MD_SB_BYTES
>> 9;
3564 mddev
->bitmap_offset
= 0;
3566 mddev
->reshape_position
= MaxSector
;
3569 * Generate a 128 bit UUID
3571 get_random_bytes(mddev
->uuid
, 16);
3573 mddev
->new_level
= mddev
->level
;
3574 mddev
->new_chunk
= mddev
->chunk_size
;
3575 mddev
->new_layout
= mddev
->layout
;
3576 mddev
->delta_disks
= 0;
3581 static int update_size(mddev_t
*mddev
, unsigned long size
)
3585 struct list_head
*tmp
;
3586 int fit
= (size
== 0);
3588 if (mddev
->pers
->resize
== NULL
)
3590 /* The "size" is the amount of each device that is used.
3591 * This can only make sense for arrays with redundancy.
3592 * linear and raid0 always use whatever space is available
3593 * We can only consider changing the size if no resync
3594 * or reconstruction is happening, and if the new size
3595 * is acceptable. It must fit before the sb_offset or,
3596 * if that is <data_offset, it must fit before the
3597 * size of each device.
3598 * If size is zero, we find the largest size that fits.
3600 if (mddev
->sync_thread
)
3602 ITERATE_RDEV(mddev
,rdev
,tmp
) {
3604 if (rdev
->sb_offset
> rdev
->data_offset
)
3605 avail
= (rdev
->sb_offset
*2) - rdev
->data_offset
;
3607 avail
= get_capacity(rdev
->bdev
->bd_disk
)
3608 - rdev
->data_offset
;
3609 if (fit
&& (size
== 0 || size
> avail
/2))
3611 if (avail
< ((sector_t
)size
<< 1))
3614 rv
= mddev
->pers
->resize(mddev
, (sector_t
)size
*2);
3616 struct block_device
*bdev
;
3618 bdev
= bdget_disk(mddev
->gendisk
, 0);
3620 mutex_lock(&bdev
->bd_inode
->i_mutex
);
3621 i_size_write(bdev
->bd_inode
, (loff_t
)mddev
->array_size
<< 10);
3622 mutex_unlock(&bdev
->bd_inode
->i_mutex
);
3629 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
)
3632 /* change the number of raid disks */
3633 if (mddev
->pers
->check_reshape
== NULL
)
3635 if (raid_disks
<= 0 ||
3636 raid_disks
>= mddev
->max_disks
)
3638 if (mddev
->sync_thread
|| mddev
->reshape_position
!= MaxSector
)
3640 mddev
->delta_disks
= raid_disks
- mddev
->raid_disks
;
3642 rv
= mddev
->pers
->check_reshape(mddev
);
3648 * update_array_info is used to change the configuration of an
3650 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
3651 * fields in the info are checked against the array.
3652 * Any differences that cannot be handled will cause an error.
3653 * Normally, only one change can be managed at a time.
3655 static int update_array_info(mddev_t
*mddev
, mdu_array_info_t
*info
)
3661 /* calculate expected state,ignoring low bits */
3662 if (mddev
->bitmap
&& mddev
->bitmap_offset
)
3663 state
|= (1 << MD_SB_BITMAP_PRESENT
);
3665 if (mddev
->major_version
!= info
->major_version
||
3666 mddev
->minor_version
!= info
->minor_version
||
3667 /* mddev->patch_version != info->patch_version || */
3668 mddev
->ctime
!= info
->ctime
||
3669 mddev
->level
!= info
->level
||
3670 /* mddev->layout != info->layout || */
3671 !mddev
->persistent
!= info
->not_persistent
||
3672 mddev
->chunk_size
!= info
->chunk_size
||
3673 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
3674 ((state
^info
->state
) & 0xfffffe00)
3677 /* Check there is only one change */
3678 if (info
->size
>= 0 && mddev
->size
!= info
->size
) cnt
++;
3679 if (mddev
->raid_disks
!= info
->raid_disks
) cnt
++;
3680 if (mddev
->layout
!= info
->layout
) cnt
++;
3681 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) cnt
++;
3682 if (cnt
== 0) return 0;
3683 if (cnt
> 1) return -EINVAL
;
3685 if (mddev
->layout
!= info
->layout
) {
3687 * we don't need to do anything at the md level, the
3688 * personality will take care of it all.
3690 if (mddev
->pers
->reconfig
== NULL
)
3693 return mddev
->pers
->reconfig(mddev
, info
->layout
, -1);
3695 if (info
->size
>= 0 && mddev
->size
!= info
->size
)
3696 rv
= update_size(mddev
, info
->size
);
3698 if (mddev
->raid_disks
!= info
->raid_disks
)
3699 rv
= update_raid_disks(mddev
, info
->raid_disks
);
3701 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) {
3702 if (mddev
->pers
->quiesce
== NULL
)
3704 if (mddev
->recovery
|| mddev
->sync_thread
)
3706 if (info
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
3707 /* add the bitmap */
3710 if (mddev
->default_bitmap_offset
== 0)
3712 mddev
->bitmap_offset
= mddev
->default_bitmap_offset
;
3713 mddev
->pers
->quiesce(mddev
, 1);
3714 rv
= bitmap_create(mddev
);
3716 bitmap_destroy(mddev
);
3717 mddev
->pers
->quiesce(mddev
, 0);
3719 /* remove the bitmap */
3722 if (mddev
->bitmap
->file
)
3724 mddev
->pers
->quiesce(mddev
, 1);
3725 bitmap_destroy(mddev
);
3726 mddev
->pers
->quiesce(mddev
, 0);
3727 mddev
->bitmap_offset
= 0;
3730 md_update_sb(mddev
);
3734 static int set_disk_faulty(mddev_t
*mddev
, dev_t dev
)
3738 if (mddev
->pers
== NULL
)
3741 rdev
= find_rdev(mddev
, dev
);
3745 md_error(mddev
, rdev
);
3749 static int md_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
3751 mddev_t
*mddev
= bdev
->bd_disk
->private_data
;
3755 geo
->cylinders
= get_capacity(mddev
->gendisk
) / 8;
3759 static int md_ioctl(struct inode
*inode
, struct file
*file
,
3760 unsigned int cmd
, unsigned long arg
)
3763 void __user
*argp
= (void __user
*)arg
;
3764 mddev_t
*mddev
= NULL
;
3766 if (!capable(CAP_SYS_ADMIN
))
3770 * Commands dealing with the RAID driver but not any
3776 err
= get_version(argp
);
3779 case PRINT_RAID_DEBUG
:
3787 autostart_arrays(arg
);
3794 * Commands creating/starting a new array:
3797 mddev
= inode
->i_bdev
->bd_disk
->private_data
;
3805 if (cmd
== START_ARRAY
) {
3806 /* START_ARRAY doesn't need to lock the array as autostart_array
3807 * does the locking, and it could even be a different array
3812 "md: %s(pid %d) used deprecated START_ARRAY ioctl. "
3813 "This will not be supported beyond July 2006\n",
3814 current
->comm
, current
->pid
);
3817 err
= autostart_array(new_decode_dev(arg
));
3819 printk(KERN_WARNING
"md: autostart failed!\n");
3825 err
= mddev_lock(mddev
);
3828 "md: ioctl lock interrupted, reason %d, cmd %d\n",
3835 case SET_ARRAY_INFO
:
3837 mdu_array_info_t info
;
3839 memset(&info
, 0, sizeof(info
));
3840 else if (copy_from_user(&info
, argp
, sizeof(info
))) {
3845 err
= update_array_info(mddev
, &info
);
3847 printk(KERN_WARNING
"md: couldn't update"
3848 " array info. %d\n", err
);
3853 if (!list_empty(&mddev
->disks
)) {
3855 "md: array %s already has disks!\n",
3860 if (mddev
->raid_disks
) {
3862 "md: array %s already initialised!\n",
3867 err
= set_array_info(mddev
, &info
);
3869 printk(KERN_WARNING
"md: couldn't set"
3870 " array info. %d\n", err
);
3880 * Commands querying/configuring an existing array:
3882 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
3883 * RUN_ARRAY, and SET_BITMAP_FILE are allowed */
3884 if (!mddev
->raid_disks
&& cmd
!= ADD_NEW_DISK
&& cmd
!= STOP_ARRAY
3885 && cmd
!= RUN_ARRAY
&& cmd
!= SET_BITMAP_FILE
) {
3891 * Commands even a read-only array can execute:
3895 case GET_ARRAY_INFO
:
3896 err
= get_array_info(mddev
, argp
);
3899 case GET_BITMAP_FILE
:
3900 err
= get_bitmap_file(mddev
, argp
);
3904 err
= get_disk_info(mddev
, argp
);
3907 case RESTART_ARRAY_RW
:
3908 err
= restart_array(mddev
);
3912 err
= do_md_stop (mddev
, 0);
3916 err
= do_md_stop (mddev
, 1);
3920 * We have a problem here : there is no easy way to give a CHS
3921 * virtual geometry. We currently pretend that we have a 2 heads
3922 * 4 sectors (with a BIG number of cylinders...). This drives
3923 * dosfs just mad... ;-)
3928 * The remaining ioctls are changing the state of the
3929 * superblock, so we do not allow them on read-only arrays.
3930 * However non-MD ioctls (e.g. get-size) will still come through
3931 * here and hit the 'default' below, so only disallow
3932 * 'md' ioctls, and switch to rw mode if started auto-readonly.
3934 if (_IOC_TYPE(cmd
) == MD_MAJOR
&&
3935 mddev
->ro
&& mddev
->pers
) {
3936 if (mddev
->ro
== 2) {
3938 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3939 md_wakeup_thread(mddev
->thread
);
3951 mdu_disk_info_t info
;
3952 if (copy_from_user(&info
, argp
, sizeof(info
)))
3955 err
= add_new_disk(mddev
, &info
);
3959 case HOT_REMOVE_DISK
:
3960 err
= hot_remove_disk(mddev
, new_decode_dev(arg
));
3964 err
= hot_add_disk(mddev
, new_decode_dev(arg
));
3967 case SET_DISK_FAULTY
:
3968 err
= set_disk_faulty(mddev
, new_decode_dev(arg
));
3972 err
= do_md_run (mddev
);
3975 case SET_BITMAP_FILE
:
3976 err
= set_bitmap_file(mddev
, (int)arg
);
3980 if (_IOC_TYPE(cmd
) == MD_MAJOR
)
3981 printk(KERN_WARNING
"md: %s(pid %d) used"
3982 " obsolete MD ioctl, upgrade your"
3983 " software to use new ictls.\n",
3984 current
->comm
, current
->pid
);
3991 mddev_unlock(mddev
);
4001 static int md_open(struct inode
*inode
, struct file
*file
)
4004 * Succeed if we can lock the mddev, which confirms that
4005 * it isn't being stopped right now.
4007 mddev_t
*mddev
= inode
->i_bdev
->bd_disk
->private_data
;
4010 if ((err
= mddev_lock(mddev
)))
4015 mddev_unlock(mddev
);
4017 check_disk_change(inode
->i_bdev
);
4022 static int md_release(struct inode
*inode
, struct file
* file
)
4024 mddev_t
*mddev
= inode
->i_bdev
->bd_disk
->private_data
;
4033 static int md_media_changed(struct gendisk
*disk
)
4035 mddev_t
*mddev
= disk
->private_data
;
4037 return mddev
->changed
;
4040 static int md_revalidate(struct gendisk
*disk
)
4042 mddev_t
*mddev
= disk
->private_data
;
4047 static struct block_device_operations md_fops
=
4049 .owner
= THIS_MODULE
,
4051 .release
= md_release
,
4053 .getgeo
= md_getgeo
,
4054 .media_changed
= md_media_changed
,
4055 .revalidate_disk
= md_revalidate
,
4058 static int md_thread(void * arg
)
4060 mdk_thread_t
*thread
= arg
;
4063 * md_thread is a 'system-thread', it's priority should be very
4064 * high. We avoid resource deadlocks individually in each
4065 * raid personality. (RAID5 does preallocation) We also use RR and
4066 * the very same RT priority as kswapd, thus we will never get
4067 * into a priority inversion deadlock.
4069 * we definitely have to have equal or higher priority than
4070 * bdflush, otherwise bdflush will deadlock if there are too
4071 * many dirty RAID5 blocks.
4074 allow_signal(SIGKILL
);
4075 while (!kthread_should_stop()) {
4077 /* We need to wait INTERRUPTIBLE so that
4078 * we don't add to the load-average.
4079 * That means we need to be sure no signals are
4082 if (signal_pending(current
))
4083 flush_signals(current
);
4085 wait_event_interruptible_timeout
4087 test_bit(THREAD_WAKEUP
, &thread
->flags
)
4088 || kthread_should_stop(),
4092 clear_bit(THREAD_WAKEUP
, &thread
->flags
);
4094 thread
->run(thread
->mddev
);
4100 void md_wakeup_thread(mdk_thread_t
*thread
)
4103 dprintk("md: waking up MD thread %s.\n", thread
->tsk
->comm
);
4104 set_bit(THREAD_WAKEUP
, &thread
->flags
);
4105 wake_up(&thread
->wqueue
);
4109 mdk_thread_t
*md_register_thread(void (*run
) (mddev_t
*), mddev_t
*mddev
,
4112 mdk_thread_t
*thread
;
4114 thread
= kzalloc(sizeof(mdk_thread_t
), GFP_KERNEL
);
4118 init_waitqueue_head(&thread
->wqueue
);
4121 thread
->mddev
= mddev
;
4122 thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
4123 thread
->tsk
= kthread_run(md_thread
, thread
, name
, mdname(thread
->mddev
));
4124 if (IS_ERR(thread
->tsk
)) {
4131 void md_unregister_thread(mdk_thread_t
*thread
)
4133 dprintk("interrupting MD-thread pid %d\n", thread
->tsk
->pid
);
4135 kthread_stop(thread
->tsk
);
4139 void md_error(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
4146 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
4149 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
4151 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
4152 __builtin_return_address(0),__builtin_return_address(1),
4153 __builtin_return_address(2),__builtin_return_address(3));
4155 if (!mddev
->pers
->error_handler
)
4157 mddev
->pers
->error_handler(mddev
,rdev
);
4158 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4159 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4160 md_wakeup_thread(mddev
->thread
);
4161 md_new_event_inintr(mddev
);
4164 /* seq_file implementation /proc/mdstat */
4166 static void status_unused(struct seq_file
*seq
)
4170 struct list_head
*tmp
;
4172 seq_printf(seq
, "unused devices: ");
4174 ITERATE_RDEV_PENDING(rdev
,tmp
) {
4175 char b
[BDEVNAME_SIZE
];
4177 seq_printf(seq
, "%s ",
4178 bdevname(rdev
->bdev
,b
));
4181 seq_printf(seq
, "<none>");
4183 seq_printf(seq
, "\n");
4187 static void status_resync(struct seq_file
*seq
, mddev_t
* mddev
)
4189 sector_t max_blocks
, resync
, res
;
4190 unsigned long dt
, db
, rt
;
4192 unsigned int per_milli
;
4194 resync
= (mddev
->curr_resync
- atomic_read(&mddev
->recovery_active
))/2;
4196 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
4197 max_blocks
= mddev
->resync_max_sectors
>> 1;
4199 max_blocks
= mddev
->size
;
4202 * Should not happen.
4208 /* Pick 'scale' such that (resync>>scale)*1000 will fit
4209 * in a sector_t, and (max_blocks>>scale) will fit in a
4210 * u32, as those are the requirements for sector_div.
4211 * Thus 'scale' must be at least 10
4214 if (sizeof(sector_t
) > sizeof(unsigned long)) {
4215 while ( max_blocks
/2 > (1ULL<<(scale
+32)))
4218 res
= (resync
>>scale
)*1000;
4219 sector_div(res
, (u32
)((max_blocks
>>scale
)+1));
4223 int i
, x
= per_milli
/50, y
= 20-x
;
4224 seq_printf(seq
, "[");
4225 for (i
= 0; i
< x
; i
++)
4226 seq_printf(seq
, "=");
4227 seq_printf(seq
, ">");
4228 for (i
= 0; i
< y
; i
++)
4229 seq_printf(seq
, ".");
4230 seq_printf(seq
, "] ");
4232 seq_printf(seq
, " %s =%3u.%u%% (%llu/%llu)",
4233 (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)?
4235 (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ?
4236 "resync" : "recovery")),
4237 per_milli
/10, per_milli
% 10,
4238 (unsigned long long) resync
,
4239 (unsigned long long) max_blocks
);
4242 * We do not want to overflow, so the order of operands and
4243 * the * 100 / 100 trick are important. We do a +1 to be
4244 * safe against division by zero. We only estimate anyway.
4246 * dt: time from mark until now
4247 * db: blocks written from mark until now
4248 * rt: remaining time
4250 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
4252 db
= resync
- (mddev
->resync_mark_cnt
/2);
4253 rt
= (dt
* ((unsigned long)(max_blocks
-resync
) / (db
/100+1)))/100;
4255 seq_printf(seq
, " finish=%lu.%lumin", rt
/ 60, (rt
% 60)/6);
4257 seq_printf(seq
, " speed=%ldK/sec", db
/dt
);
4260 static void *md_seq_start(struct seq_file
*seq
, loff_t
*pos
)
4262 struct list_head
*tmp
;
4272 spin_lock(&all_mddevs_lock
);
4273 list_for_each(tmp
,&all_mddevs
)
4275 mddev
= list_entry(tmp
, mddev_t
, all_mddevs
);
4277 spin_unlock(&all_mddevs_lock
);
4280 spin_unlock(&all_mddevs_lock
);
4282 return (void*)2;/* tail */
4286 static void *md_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
4288 struct list_head
*tmp
;
4289 mddev_t
*next_mddev
, *mddev
= v
;
4295 spin_lock(&all_mddevs_lock
);
4297 tmp
= all_mddevs
.next
;
4299 tmp
= mddev
->all_mddevs
.next
;
4300 if (tmp
!= &all_mddevs
)
4301 next_mddev
= mddev_get(list_entry(tmp
,mddev_t
,all_mddevs
));
4303 next_mddev
= (void*)2;
4306 spin_unlock(&all_mddevs_lock
);
4314 static void md_seq_stop(struct seq_file
*seq
, void *v
)
4318 if (mddev
&& v
!= (void*)1 && v
!= (void*)2)
4322 struct mdstat_info
{
4326 static int md_seq_show(struct seq_file
*seq
, void *v
)
4330 struct list_head
*tmp2
;
4332 struct mdstat_info
*mi
= seq
->private;
4333 struct bitmap
*bitmap
;
4335 if (v
== (void*)1) {
4336 struct mdk_personality
*pers
;
4337 seq_printf(seq
, "Personalities : ");
4338 spin_lock(&pers_lock
);
4339 list_for_each_entry(pers
, &pers_list
, list
)
4340 seq_printf(seq
, "[%s] ", pers
->name
);
4342 spin_unlock(&pers_lock
);
4343 seq_printf(seq
, "\n");
4344 mi
->event
= atomic_read(&md_event_count
);
4347 if (v
== (void*)2) {
4352 if (mddev_lock(mddev
) < 0)
4355 if (mddev
->pers
|| mddev
->raid_disks
|| !list_empty(&mddev
->disks
)) {
4356 seq_printf(seq
, "%s : %sactive", mdname(mddev
),
4357 mddev
->pers
? "" : "in");
4360 seq_printf(seq
, " (read-only)");
4362 seq_printf(seq
, "(auto-read-only)");
4363 seq_printf(seq
, " %s", mddev
->pers
->name
);
4367 ITERATE_RDEV(mddev
,rdev
,tmp2
) {
4368 char b
[BDEVNAME_SIZE
];
4369 seq_printf(seq
, " %s[%d]",
4370 bdevname(rdev
->bdev
,b
), rdev
->desc_nr
);
4371 if (test_bit(WriteMostly
, &rdev
->flags
))
4372 seq_printf(seq
, "(W)");
4373 if (test_bit(Faulty
, &rdev
->flags
)) {
4374 seq_printf(seq
, "(F)");
4376 } else if (rdev
->raid_disk
< 0)
4377 seq_printf(seq
, "(S)"); /* spare */
4381 if (!list_empty(&mddev
->disks
)) {
4383 seq_printf(seq
, "\n %llu blocks",
4384 (unsigned long long)mddev
->array_size
);
4386 seq_printf(seq
, "\n %llu blocks",
4387 (unsigned long long)size
);
4389 if (mddev
->persistent
) {
4390 if (mddev
->major_version
!= 0 ||
4391 mddev
->minor_version
!= 90) {
4392 seq_printf(seq
," super %d.%d",
4393 mddev
->major_version
,
4394 mddev
->minor_version
);
4397 seq_printf(seq
, " super non-persistent");
4400 mddev
->pers
->status (seq
, mddev
);
4401 seq_printf(seq
, "\n ");
4402 if (mddev
->pers
->sync_request
) {
4403 if (mddev
->curr_resync
> 2) {
4404 status_resync (seq
, mddev
);
4405 seq_printf(seq
, "\n ");
4406 } else if (mddev
->curr_resync
== 1 || mddev
->curr_resync
== 2)
4407 seq_printf(seq
, "\tresync=DELAYED\n ");
4408 else if (mddev
->recovery_cp
< MaxSector
)
4409 seq_printf(seq
, "\tresync=PENDING\n ");
4412 seq_printf(seq
, "\n ");
4414 if ((bitmap
= mddev
->bitmap
)) {
4415 unsigned long chunk_kb
;
4416 unsigned long flags
;
4417 spin_lock_irqsave(&bitmap
->lock
, flags
);
4418 chunk_kb
= bitmap
->chunksize
>> 10;
4419 seq_printf(seq
, "bitmap: %lu/%lu pages [%luKB], "
4421 bitmap
->pages
- bitmap
->missing_pages
,
4423 (bitmap
->pages
- bitmap
->missing_pages
)
4424 << (PAGE_SHIFT
- 10),
4425 chunk_kb
? chunk_kb
: bitmap
->chunksize
,
4426 chunk_kb
? "KB" : "B");
4428 seq_printf(seq
, ", file: ");
4429 seq_path(seq
, bitmap
->file
->f_vfsmnt
,
4430 bitmap
->file
->f_dentry
," \t\n");
4433 seq_printf(seq
, "\n");
4434 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
4437 seq_printf(seq
, "\n");
4439 mddev_unlock(mddev
);
4444 static struct seq_operations md_seq_ops
= {
4445 .start
= md_seq_start
,
4446 .next
= md_seq_next
,
4447 .stop
= md_seq_stop
,
4448 .show
= md_seq_show
,
4451 static int md_seq_open(struct inode
*inode
, struct file
*file
)
4454 struct mdstat_info
*mi
= kmalloc(sizeof(*mi
), GFP_KERNEL
);
4458 error
= seq_open(file
, &md_seq_ops
);
4462 struct seq_file
*p
= file
->private_data
;
4464 mi
->event
= atomic_read(&md_event_count
);
4469 static int md_seq_release(struct inode
*inode
, struct file
*file
)
4471 struct seq_file
*m
= file
->private_data
;
4472 struct mdstat_info
*mi
= m
->private;
4475 return seq_release(inode
, file
);
4478 static unsigned int mdstat_poll(struct file
*filp
, poll_table
*wait
)
4480 struct seq_file
*m
= filp
->private_data
;
4481 struct mdstat_info
*mi
= m
->private;
4484 poll_wait(filp
, &md_event_waiters
, wait
);
4486 /* always allow read */
4487 mask
= POLLIN
| POLLRDNORM
;
4489 if (mi
->event
!= atomic_read(&md_event_count
))
4490 mask
|= POLLERR
| POLLPRI
;
4494 static struct file_operations md_seq_fops
= {
4495 .open
= md_seq_open
,
4497 .llseek
= seq_lseek
,
4498 .release
= md_seq_release
,
4499 .poll
= mdstat_poll
,
4502 int register_md_personality(struct mdk_personality
*p
)
4504 spin_lock(&pers_lock
);
4505 list_add_tail(&p
->list
, &pers_list
);
4506 printk(KERN_INFO
"md: %s personality registered for level %d\n", p
->name
, p
->level
);
4507 spin_unlock(&pers_lock
);
4511 int unregister_md_personality(struct mdk_personality
*p
)
4513 printk(KERN_INFO
"md: %s personality unregistered\n", p
->name
);
4514 spin_lock(&pers_lock
);
4515 list_del_init(&p
->list
);
4516 spin_unlock(&pers_lock
);
4520 static int is_mddev_idle(mddev_t
*mddev
)
4523 struct list_head
*tmp
;
4525 unsigned long curr_events
;
4528 ITERATE_RDEV(mddev
,rdev
,tmp
) {
4529 struct gendisk
*disk
= rdev
->bdev
->bd_contains
->bd_disk
;
4530 curr_events
= disk_stat_read(disk
, sectors
[0]) +
4531 disk_stat_read(disk
, sectors
[1]) -
4532 atomic_read(&disk
->sync_io
);
4533 /* The difference between curr_events and last_events
4534 * will be affected by any new non-sync IO (making
4535 * curr_events bigger) and any difference in the amount of
4536 * in-flight syncio (making current_events bigger or smaller)
4537 * The amount in-flight is currently limited to
4538 * 32*64K in raid1/10 and 256*PAGE_SIZE in raid5/6
4539 * which is at most 4096 sectors.
4540 * These numbers are fairly fragile and should be made
4541 * more robust, probably by enforcing the
4542 * 'window size' that md_do_sync sort-of uses.
4544 * Note: the following is an unsigned comparison.
4546 if ((curr_events
- rdev
->last_events
+ 4096) > 8192) {
4547 rdev
->last_events
= curr_events
;
4554 void md_done_sync(mddev_t
*mddev
, int blocks
, int ok
)
4556 /* another "blocks" (512byte) blocks have been synced */
4557 atomic_sub(blocks
, &mddev
->recovery_active
);
4558 wake_up(&mddev
->recovery_wait
);
4560 set_bit(MD_RECOVERY_ERR
, &mddev
->recovery
);
4561 md_wakeup_thread(mddev
->thread
);
4562 // stop recovery, signal do_sync ....
4567 /* md_write_start(mddev, bi)
4568 * If we need to update some array metadata (e.g. 'active' flag
4569 * in superblock) before writing, schedule a superblock update
4570 * and wait for it to complete.
4572 void md_write_start(mddev_t
*mddev
, struct bio
*bi
)
4574 if (bio_data_dir(bi
) != WRITE
)
4577 BUG_ON(mddev
->ro
== 1);
4578 if (mddev
->ro
== 2) {
4579 /* need to switch to read/write */
4581 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4582 md_wakeup_thread(mddev
->thread
);
4584 atomic_inc(&mddev
->writes_pending
);
4585 if (mddev
->in_sync
) {
4586 spin_lock_irq(&mddev
->write_lock
);
4587 if (mddev
->in_sync
) {
4589 mddev
->sb_dirty
= 1;
4590 md_wakeup_thread(mddev
->thread
);
4592 spin_unlock_irq(&mddev
->write_lock
);
4594 wait_event(mddev
->sb_wait
, mddev
->sb_dirty
==0);
4597 void md_write_end(mddev_t
*mddev
)
4599 if (atomic_dec_and_test(&mddev
->writes_pending
)) {
4600 if (mddev
->safemode
== 2)
4601 md_wakeup_thread(mddev
->thread
);
4603 mod_timer(&mddev
->safemode_timer
, jiffies
+ mddev
->safemode_delay
);
4607 static DECLARE_WAIT_QUEUE_HEAD(resync_wait
);
4609 #define SYNC_MARKS 10
4610 #define SYNC_MARK_STEP (3*HZ)
4611 void md_do_sync(mddev_t
*mddev
)
4614 unsigned int currspeed
= 0,
4616 sector_t max_sectors
,j
, io_sectors
;
4617 unsigned long mark
[SYNC_MARKS
];
4618 sector_t mark_cnt
[SYNC_MARKS
];
4620 struct list_head
*tmp
;
4621 sector_t last_check
;
4624 /* just incase thread restarts... */
4625 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
4628 /* we overload curr_resync somewhat here.
4629 * 0 == not engaged in resync at all
4630 * 2 == checking that there is no conflict with another sync
4631 * 1 == like 2, but have yielded to allow conflicting resync to
4633 * other == active in resync - this many blocks
4635 * Before starting a resync we must have set curr_resync to
4636 * 2, and then checked that every "conflicting" array has curr_resync
4637 * less than ours. When we find one that is the same or higher
4638 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
4639 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
4640 * This will mean we have to start checking from the beginning again.
4645 mddev
->curr_resync
= 2;
4648 if (kthread_should_stop()) {
4649 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4652 ITERATE_MDDEV(mddev2
,tmp
) {
4653 if (mddev2
== mddev
)
4655 if (mddev2
->curr_resync
&&
4656 match_mddev_units(mddev
,mddev2
)) {
4658 if (mddev
< mddev2
&& mddev
->curr_resync
== 2) {
4659 /* arbitrarily yield */
4660 mddev
->curr_resync
= 1;
4661 wake_up(&resync_wait
);
4663 if (mddev
> mddev2
&& mddev
->curr_resync
== 1)
4664 /* no need to wait here, we can wait the next
4665 * time 'round when curr_resync == 2
4668 prepare_to_wait(&resync_wait
, &wq
, TASK_UNINTERRUPTIBLE
);
4669 if (!kthread_should_stop() &&
4670 mddev2
->curr_resync
>= mddev
->curr_resync
) {
4671 printk(KERN_INFO
"md: delaying resync of %s"
4672 " until %s has finished resync (they"
4673 " share one or more physical units)\n",
4674 mdname(mddev
), mdname(mddev2
));
4677 finish_wait(&resync_wait
, &wq
);
4680 finish_wait(&resync_wait
, &wq
);
4683 } while (mddev
->curr_resync
< 2);
4685 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
4686 /* resync follows the size requested by the personality,
4687 * which defaults to physical size, but can be virtual size
4689 max_sectors
= mddev
->resync_max_sectors
;
4690 mddev
->resync_mismatches
= 0;
4691 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
4692 max_sectors
= mddev
->size
<< 1;
4694 /* recovery follows the physical size of devices */
4695 max_sectors
= mddev
->size
<< 1;
4697 printk(KERN_INFO
"md: syncing RAID array %s\n", mdname(mddev
));
4698 printk(KERN_INFO
"md: minimum _guaranteed_ reconstruction speed:"
4699 " %d KB/sec/disc.\n", speed_min(mddev
));
4700 printk(KERN_INFO
"md: using maximum available idle IO bandwidth "
4701 "(but not more than %d KB/sec) for reconstruction.\n",
4704 is_mddev_idle(mddev
); /* this also initializes IO event counters */
4705 /* we don't use the checkpoint if there's a bitmap */
4706 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) && !mddev
->bitmap
4707 && ! test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
4708 j
= mddev
->recovery_cp
;
4712 for (m
= 0; m
< SYNC_MARKS
; m
++) {
4714 mark_cnt
[m
] = io_sectors
;
4717 mddev
->resync_mark
= mark
[last_mark
];
4718 mddev
->resync_mark_cnt
= mark_cnt
[last_mark
];
4721 * Tune reconstruction:
4723 window
= 32*(PAGE_SIZE
/512);
4724 printk(KERN_INFO
"md: using %dk window, over a total of %llu blocks.\n",
4725 window
/2,(unsigned long long) max_sectors
/2);
4727 atomic_set(&mddev
->recovery_active
, 0);
4728 init_waitqueue_head(&mddev
->recovery_wait
);
4733 "md: resuming recovery of %s from checkpoint.\n",
4735 mddev
->curr_resync
= j
;
4738 while (j
< max_sectors
) {
4742 sectors
= mddev
->pers
->sync_request(mddev
, j
, &skipped
,
4743 currspeed
< speed_min(mddev
));
4745 set_bit(MD_RECOVERY_ERR
, &mddev
->recovery
);
4749 if (!skipped
) { /* actual IO requested */
4750 io_sectors
+= sectors
;
4751 atomic_add(sectors
, &mddev
->recovery_active
);
4755 if (j
>1) mddev
->curr_resync
= j
;
4756 if (last_check
== 0)
4757 /* this is the earliers that rebuilt will be
4758 * visible in /proc/mdstat
4760 md_new_event(mddev
);
4762 if (last_check
+ window
> io_sectors
|| j
== max_sectors
)
4765 last_check
= io_sectors
;
4767 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) ||
4768 test_bit(MD_RECOVERY_ERR
, &mddev
->recovery
))
4772 if (time_after_eq(jiffies
, mark
[last_mark
] + SYNC_MARK_STEP
)) {
4774 int next
= (last_mark
+1) % SYNC_MARKS
;
4776 mddev
->resync_mark
= mark
[next
];
4777 mddev
->resync_mark_cnt
= mark_cnt
[next
];
4778 mark
[next
] = jiffies
;
4779 mark_cnt
[next
] = io_sectors
- atomic_read(&mddev
->recovery_active
);
4784 if (kthread_should_stop()) {
4786 * got a signal, exit.
4789 "md: md_do_sync() got signal ... exiting\n");
4790 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4795 * this loop exits only if either when we are slower than
4796 * the 'hard' speed limit, or the system was IO-idle for
4798 * the system might be non-idle CPU-wise, but we only care
4799 * about not overloading the IO subsystem. (things like an
4800 * e2fsck being done on the RAID array should execute fast)
4802 mddev
->queue
->unplug_fn(mddev
->queue
);
4805 currspeed
= ((unsigned long)(io_sectors
-mddev
->resync_mark_cnt
))/2
4806 /((jiffies
-mddev
->resync_mark
)/HZ
+1) +1;
4808 if (currspeed
> speed_min(mddev
)) {
4809 if ((currspeed
> speed_max(mddev
)) ||
4810 !is_mddev_idle(mddev
)) {
4816 printk(KERN_INFO
"md: %s: sync done.\n",mdname(mddev
));
4818 * this also signals 'finished resyncing' to md_stop
4821 mddev
->queue
->unplug_fn(mddev
->queue
);
4823 wait_event(mddev
->recovery_wait
, !atomic_read(&mddev
->recovery_active
));
4825 /* tell personality that we are finished */
4826 mddev
->pers
->sync_request(mddev
, max_sectors
, &skipped
, 1);
4828 if (!test_bit(MD_RECOVERY_ERR
, &mddev
->recovery
) &&
4829 test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) &&
4830 !test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
) &&
4831 mddev
->curr_resync
> 2 &&
4832 mddev
->curr_resync
>= mddev
->recovery_cp
) {
4833 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
4835 "md: checkpointing recovery of %s.\n",
4837 mddev
->recovery_cp
= mddev
->curr_resync
;
4839 mddev
->recovery_cp
= MaxSector
;
4843 mddev
->curr_resync
= 0;
4844 wake_up(&resync_wait
);
4845 set_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
4846 md_wakeup_thread(mddev
->thread
);
4848 EXPORT_SYMBOL_GPL(md_do_sync
);
4852 * This routine is regularly called by all per-raid-array threads to
4853 * deal with generic issues like resync and super-block update.
4854 * Raid personalities that don't have a thread (linear/raid0) do not
4855 * need this as they never do any recovery or update the superblock.
4857 * It does not do any resync itself, but rather "forks" off other threads
4858 * to do that as needed.
4859 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
4860 * "->recovery" and create a thread at ->sync_thread.
4861 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
4862 * and wakeups up this thread which will reap the thread and finish up.
4863 * This thread also removes any faulty devices (with nr_pending == 0).
4865 * The overall approach is:
4866 * 1/ if the superblock needs updating, update it.
4867 * 2/ If a recovery thread is running, don't do anything else.
4868 * 3/ If recovery has finished, clean up, possibly marking spares active.
4869 * 4/ If there are any faulty devices, remove them.
4870 * 5/ If array is degraded, try to add spares devices
4871 * 6/ If array has spares or is not in-sync, start a resync thread.
4873 void md_check_recovery(mddev_t
*mddev
)
4876 struct list_head
*rtmp
;
4880 bitmap_daemon_work(mddev
->bitmap
);
4885 if (signal_pending(current
)) {
4886 if (mddev
->pers
->sync_request
) {
4887 printk(KERN_INFO
"md: %s in immediate safe mode\n",
4889 mddev
->safemode
= 2;
4891 flush_signals(current
);
4896 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
4897 test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
4898 (mddev
->safemode
== 1) ||
4899 (mddev
->safemode
== 2 && ! atomic_read(&mddev
->writes_pending
)
4900 && !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
4904 if (mddev_trylock(mddev
)) {
4907 spin_lock_irq(&mddev
->write_lock
);
4908 if (mddev
->safemode
&& !atomic_read(&mddev
->writes_pending
) &&
4909 !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
) {
4911 mddev
->sb_dirty
= 1;
4913 if (mddev
->safemode
== 1)
4914 mddev
->safemode
= 0;
4915 spin_unlock_irq(&mddev
->write_lock
);
4917 if (mddev
->sb_dirty
)
4918 md_update_sb(mddev
);
4921 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
4922 !test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
)) {
4923 /* resync/recovery still happening */
4924 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4927 if (mddev
->sync_thread
) {
4928 /* resync has finished, collect result */
4929 md_unregister_thread(mddev
->sync_thread
);
4930 mddev
->sync_thread
= NULL
;
4931 if (!test_bit(MD_RECOVERY_ERR
, &mddev
->recovery
) &&
4932 !test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
4934 /* activate any spares */
4935 mddev
->pers
->spare_active(mddev
);
4937 md_update_sb(mddev
);
4939 /* if array is no-longer degraded, then any saved_raid_disk
4940 * information must be scrapped
4942 if (!mddev
->degraded
)
4943 ITERATE_RDEV(mddev
,rdev
,rtmp
)
4944 rdev
->saved_raid_disk
= -1;
4946 mddev
->recovery
= 0;
4947 /* flag recovery needed just to double check */
4948 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4949 md_new_event(mddev
);
4952 /* Clear some bits that don't mean anything, but
4955 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4956 clear_bit(MD_RECOVERY_ERR
, &mddev
->recovery
);
4957 clear_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4958 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
4960 /* no recovery is running.
4961 * remove any failed drives, then
4962 * add spares if possible.
4963 * Spare are also removed and re-added, to allow
4964 * the personality to fail the re-add.
4966 ITERATE_RDEV(mddev
,rdev
,rtmp
)
4967 if (rdev
->raid_disk
>= 0 &&
4968 (test_bit(Faulty
, &rdev
->flags
) || ! test_bit(In_sync
, &rdev
->flags
)) &&
4969 atomic_read(&rdev
->nr_pending
)==0) {
4970 if (mddev
->pers
->hot_remove_disk(mddev
, rdev
->raid_disk
)==0) {
4972 sprintf(nm
,"rd%d", rdev
->raid_disk
);
4973 sysfs_remove_link(&mddev
->kobj
, nm
);
4974 rdev
->raid_disk
= -1;
4978 if (mddev
->degraded
) {
4979 ITERATE_RDEV(mddev
,rdev
,rtmp
)
4980 if (rdev
->raid_disk
< 0
4981 && !test_bit(Faulty
, &rdev
->flags
)) {
4982 if (mddev
->pers
->hot_add_disk(mddev
,rdev
)) {
4984 sprintf(nm
, "rd%d", rdev
->raid_disk
);
4985 sysfs_create_link(&mddev
->kobj
, &rdev
->kobj
, nm
);
4987 md_new_event(mddev
);
4994 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
4995 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
4996 } else if (mddev
->recovery_cp
< MaxSector
) {
4997 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
4998 } else if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
4999 /* nothing to be done ... */
5002 if (mddev
->pers
->sync_request
) {
5003 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
5004 if (spares
&& mddev
->bitmap
&& ! mddev
->bitmap
->file
) {
5005 /* We are adding a device or devices to an array
5006 * which has the bitmap stored on all devices.
5007 * So make sure all bitmap pages get written
5009 bitmap_write_all(mddev
->bitmap
);
5011 mddev
->sync_thread
= md_register_thread(md_do_sync
,
5014 if (!mddev
->sync_thread
) {
5015 printk(KERN_ERR
"%s: could not start resync"
5018 /* leave the spares where they are, it shouldn't hurt */
5019 mddev
->recovery
= 0;
5021 md_wakeup_thread(mddev
->sync_thread
);
5022 md_new_event(mddev
);
5025 mddev_unlock(mddev
);
5029 static int md_notify_reboot(struct notifier_block
*this,
5030 unsigned long code
, void *x
)
5032 struct list_head
*tmp
;
5035 if ((code
== SYS_DOWN
) || (code
== SYS_HALT
) || (code
== SYS_POWER_OFF
)) {
5037 printk(KERN_INFO
"md: stopping all md devices.\n");
5039 ITERATE_MDDEV(mddev
,tmp
)
5040 if (mddev_trylock(mddev
)) {
5041 do_md_stop (mddev
, 1);
5042 mddev_unlock(mddev
);
5045 * certain more exotic SCSI devices are known to be
5046 * volatile wrt too early system reboots. While the
5047 * right place to handle this issue is the given
5048 * driver, we do want to have a safe RAID driver ...
5055 static struct notifier_block md_notifier
= {
5056 .notifier_call
= md_notify_reboot
,
5058 .priority
= INT_MAX
, /* before any real devices */
5061 static void md_geninit(void)
5063 struct proc_dir_entry
*p
;
5065 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t
));
5067 p
= create_proc_entry("mdstat", S_IRUGO
, NULL
);
5069 p
->proc_fops
= &md_seq_fops
;
5072 static int __init
md_init(void)
5076 printk(KERN_INFO
"md: md driver %d.%d.%d MAX_MD_DEVS=%d,"
5077 " MD_SB_DISKS=%d\n",
5078 MD_MAJOR_VERSION
, MD_MINOR_VERSION
,
5079 MD_PATCHLEVEL_VERSION
, MAX_MD_DEVS
, MD_SB_DISKS
);
5080 printk(KERN_INFO
"md: bitmap version %d.%d\n", BITMAP_MAJOR_HI
,
5083 if (register_blkdev(MAJOR_NR
, "md"))
5085 if ((mdp_major
=register_blkdev(0, "mdp"))<=0) {
5086 unregister_blkdev(MAJOR_NR
, "md");
5090 blk_register_region(MKDEV(MAJOR_NR
, 0), MAX_MD_DEVS
, THIS_MODULE
,
5091 md_probe
, NULL
, NULL
);
5092 blk_register_region(MKDEV(mdp_major
, 0), MAX_MD_DEVS
<<MdpMinorShift
, THIS_MODULE
,
5093 md_probe
, NULL
, NULL
);
5095 for (minor
=0; minor
< MAX_MD_DEVS
; ++minor
)
5096 devfs_mk_bdev(MKDEV(MAJOR_NR
, minor
),
5097 S_IFBLK
|S_IRUSR
|S_IWUSR
,
5100 for (minor
=0; minor
< MAX_MD_DEVS
; ++minor
)
5101 devfs_mk_bdev(MKDEV(mdp_major
, minor
<<MdpMinorShift
),
5102 S_IFBLK
|S_IRUSR
|S_IWUSR
,
5106 register_reboot_notifier(&md_notifier
);
5107 raid_table_header
= register_sysctl_table(raid_root_table
, 1);
5117 * Searches all registered partitions for autorun RAID arrays
5120 static dev_t detected_devices
[128];
5123 void md_autodetect_dev(dev_t dev
)
5125 if (dev_cnt
>= 0 && dev_cnt
< 127)
5126 detected_devices
[dev_cnt
++] = dev
;
5130 static void autostart_arrays(int part
)
5135 printk(KERN_INFO
"md: Autodetecting RAID arrays.\n");
5137 for (i
= 0; i
< dev_cnt
; i
++) {
5138 dev_t dev
= detected_devices
[i
];
5140 rdev
= md_import_device(dev
,0, 0);
5144 if (test_bit(Faulty
, &rdev
->flags
)) {
5148 list_add(&rdev
->same_set
, &pending_raid_disks
);
5152 autorun_devices(part
);
5157 static __exit
void md_exit(void)
5160 struct list_head
*tmp
;
5162 blk_unregister_region(MKDEV(MAJOR_NR
,0), MAX_MD_DEVS
);
5163 blk_unregister_region(MKDEV(mdp_major
,0), MAX_MD_DEVS
<< MdpMinorShift
);
5164 for (i
=0; i
< MAX_MD_DEVS
; i
++)
5165 devfs_remove("md/%d", i
);
5166 for (i
=0; i
< MAX_MD_DEVS
; i
++)
5167 devfs_remove("md/d%d", i
);
5171 unregister_blkdev(MAJOR_NR
,"md");
5172 unregister_blkdev(mdp_major
, "mdp");
5173 unregister_reboot_notifier(&md_notifier
);
5174 unregister_sysctl_table(raid_table_header
);
5175 remove_proc_entry("mdstat", NULL
);
5176 ITERATE_MDDEV(mddev
,tmp
) {
5177 struct gendisk
*disk
= mddev
->gendisk
;
5180 export_array(mddev
);
5183 mddev
->gendisk
= NULL
;
5188 module_init(md_init
)
5189 module_exit(md_exit
)
5191 static int get_ro(char *buffer
, struct kernel_param
*kp
)
5193 return sprintf(buffer
, "%d", start_readonly
);
5195 static int set_ro(const char *val
, struct kernel_param
*kp
)
5198 int num
= simple_strtoul(val
, &e
, 10);
5199 if (*val
&& (*e
== '\0' || *e
== '\n')) {
5200 start_readonly
= num
;
5206 module_param_call(start_ro
, set_ro
, get_ro
, NULL
, 0600);
5207 module_param(start_dirty_degraded
, int, 0644);
5210 EXPORT_SYMBOL(register_md_personality
);
5211 EXPORT_SYMBOL(unregister_md_personality
);
5212 EXPORT_SYMBOL(md_error
);
5213 EXPORT_SYMBOL(md_done_sync
);
5214 EXPORT_SYMBOL(md_write_start
);
5215 EXPORT_SYMBOL(md_write_end
);
5216 EXPORT_SYMBOL(md_register_thread
);
5217 EXPORT_SYMBOL(md_unregister_thread
);
5218 EXPORT_SYMBOL(md_wakeup_thread
);
5219 EXPORT_SYMBOL(md_print_devices
);
5220 EXPORT_SYMBOL(md_check_recovery
);
5221 MODULE_LICENSE("GPL");
5223 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR
);