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/kthread.h>
36 #include <linux/raid/md.h>
37 #include <linux/raid/bitmap.h>
38 #include <linux/sysctl.h>
39 #include <linux/buffer_head.h> /* for invalidate_bdev */
40 #include <linux/poll.h>
41 #include <linux/ctype.h>
42 #include <linux/hdreg.h>
43 #include <linux/proc_fs.h>
44 #include <linux/random.h>
45 #include <linux/reboot.h>
46 #include <linux/file.h>
47 #include <linux/delay.h>
49 #define MAJOR_NR MD_MAJOR
51 /* 63 partitions with the alternate major number (mdp) */
52 #define MdpMinorShift 6
55 #define dprintk(x...) ((void)(DEBUG && printk(x)))
59 static void autostart_arrays(int part
);
62 static LIST_HEAD(pers_list
);
63 static DEFINE_SPINLOCK(pers_lock
);
65 static void md_print_devices(void);
67 static DECLARE_WAIT_QUEUE_HEAD(resync_wait
);
69 #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
72 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
73 * is 1000 KB/sec, so the extra system load does not show up that much.
74 * Increase it if you want to have more _guaranteed_ speed. Note that
75 * the RAID driver will use the maximum available bandwidth if the IO
76 * subsystem is idle. There is also an 'absolute maximum' reconstruction
77 * speed limit - in case reconstruction slows down your system despite
80 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
81 * or /sys/block/mdX/md/sync_speed_{min,max}
84 static int sysctl_speed_limit_min
= 1000;
85 static int sysctl_speed_limit_max
= 200000;
86 static inline int speed_min(mddev_t
*mddev
)
88 return mddev
->sync_speed_min
?
89 mddev
->sync_speed_min
: sysctl_speed_limit_min
;
92 static inline int speed_max(mddev_t
*mddev
)
94 return mddev
->sync_speed_max
?
95 mddev
->sync_speed_max
: sysctl_speed_limit_max
;
98 static struct ctl_table_header
*raid_table_header
;
100 static ctl_table raid_table
[] = {
102 .ctl_name
= DEV_RAID_SPEED_LIMIT_MIN
,
103 .procname
= "speed_limit_min",
104 .data
= &sysctl_speed_limit_min
,
105 .maxlen
= sizeof(int),
106 .mode
= S_IRUGO
|S_IWUSR
,
107 .proc_handler
= &proc_dointvec
,
110 .ctl_name
= DEV_RAID_SPEED_LIMIT_MAX
,
111 .procname
= "speed_limit_max",
112 .data
= &sysctl_speed_limit_max
,
113 .maxlen
= sizeof(int),
114 .mode
= S_IRUGO
|S_IWUSR
,
115 .proc_handler
= &proc_dointvec
,
120 static ctl_table raid_dir_table
[] = {
122 .ctl_name
= DEV_RAID
,
125 .mode
= S_IRUGO
|S_IXUGO
,
131 static ctl_table raid_root_table
[] = {
137 .child
= raid_dir_table
,
142 static struct block_device_operations md_fops
;
144 static int start_readonly
;
147 * We have a system wide 'event count' that is incremented
148 * on any 'interesting' event, and readers of /proc/mdstat
149 * can use 'poll' or 'select' to find out when the event
153 * start array, stop array, error, add device, remove device,
154 * start build, activate spare
156 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters
);
157 static atomic_t md_event_count
;
158 void md_new_event(mddev_t
*mddev
)
160 atomic_inc(&md_event_count
);
161 wake_up(&md_event_waiters
);
163 EXPORT_SYMBOL_GPL(md_new_event
);
165 /* Alternate version that can be called from interrupts
166 * when calling sysfs_notify isn't needed.
168 static void md_new_event_inintr(mddev_t
*mddev
)
170 atomic_inc(&md_event_count
);
171 wake_up(&md_event_waiters
);
175 * Enables to iterate over all existing md arrays
176 * all_mddevs_lock protects this list.
178 static LIST_HEAD(all_mddevs
);
179 static DEFINE_SPINLOCK(all_mddevs_lock
);
183 * iterates through all used mddevs in the system.
184 * We take care to grab the all_mddevs_lock whenever navigating
185 * the list, and to always hold a refcount when unlocked.
186 * Any code which breaks out of this loop while own
187 * a reference to the current mddev and must mddev_put it.
189 #define for_each_mddev(mddev,tmp) \
191 for (({ spin_lock(&all_mddevs_lock); \
192 tmp = all_mddevs.next; \
194 ({ if (tmp != &all_mddevs) \
195 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
196 spin_unlock(&all_mddevs_lock); \
197 if (mddev) mddev_put(mddev); \
198 mddev = list_entry(tmp, mddev_t, all_mddevs); \
199 tmp != &all_mddevs;}); \
200 ({ spin_lock(&all_mddevs_lock); \
205 static int md_fail_request(struct request_queue
*q
, struct bio
*bio
)
211 static inline mddev_t
*mddev_get(mddev_t
*mddev
)
213 atomic_inc(&mddev
->active
);
217 static void mddev_delayed_delete(struct work_struct
*ws
);
219 static void mddev_put(mddev_t
*mddev
)
221 if (!atomic_dec_and_lock(&mddev
->active
, &all_mddevs_lock
))
223 if (!mddev
->raid_disks
&& list_empty(&mddev
->disks
) &&
224 !mddev
->hold_active
) {
225 list_del(&mddev
->all_mddevs
);
226 if (mddev
->gendisk
) {
227 /* we did a probe so need to clean up.
228 * Call schedule_work inside the spinlock
229 * so that flush_scheduled_work() after
230 * mddev_find will succeed in waiting for the
233 INIT_WORK(&mddev
->del_work
, mddev_delayed_delete
);
234 schedule_work(&mddev
->del_work
);
238 spin_unlock(&all_mddevs_lock
);
241 static mddev_t
* mddev_find(dev_t unit
)
243 mddev_t
*mddev
, *new = NULL
;
246 spin_lock(&all_mddevs_lock
);
249 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
250 if (mddev
->unit
== unit
) {
252 spin_unlock(&all_mddevs_lock
);
258 list_add(&new->all_mddevs
, &all_mddevs
);
259 spin_unlock(&all_mddevs_lock
);
260 new->hold_active
= UNTIL_IOCTL
;
264 /* find an unused unit number */
265 static int next_minor
= 512;
266 int start
= next_minor
;
270 dev
= MKDEV(MD_MAJOR
, next_minor
);
272 if (next_minor
> MINORMASK
)
274 if (next_minor
== start
) {
275 /* Oh dear, all in use. */
276 spin_unlock(&all_mddevs_lock
);
282 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
283 if (mddev
->unit
== dev
) {
289 new->md_minor
= MINOR(dev
);
290 new->hold_active
= UNTIL_STOP
;
291 list_add(&new->all_mddevs
, &all_mddevs
);
292 spin_unlock(&all_mddevs_lock
);
295 spin_unlock(&all_mddevs_lock
);
297 new = kzalloc(sizeof(*new), GFP_KERNEL
);
302 if (MAJOR(unit
) == MD_MAJOR
)
303 new->md_minor
= MINOR(unit
);
305 new->md_minor
= MINOR(unit
) >> MdpMinorShift
;
307 mutex_init(&new->reconfig_mutex
);
308 INIT_LIST_HEAD(&new->disks
);
309 INIT_LIST_HEAD(&new->all_mddevs
);
310 init_timer(&new->safemode_timer
);
311 atomic_set(&new->active
, 1);
312 atomic_set(&new->openers
, 0);
313 spin_lock_init(&new->write_lock
);
314 init_waitqueue_head(&new->sb_wait
);
315 init_waitqueue_head(&new->recovery_wait
);
316 new->reshape_position
= MaxSector
;
318 new->resync_max
= MaxSector
;
319 new->level
= LEVEL_NONE
;
324 static inline int mddev_lock(mddev_t
* mddev
)
326 return mutex_lock_interruptible(&mddev
->reconfig_mutex
);
329 static inline int mddev_trylock(mddev_t
* mddev
)
331 return mutex_trylock(&mddev
->reconfig_mutex
);
334 static inline void mddev_unlock(mddev_t
* mddev
)
336 mutex_unlock(&mddev
->reconfig_mutex
);
338 md_wakeup_thread(mddev
->thread
);
341 static mdk_rdev_t
* find_rdev_nr(mddev_t
*mddev
, int nr
)
345 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
346 if (rdev
->desc_nr
== nr
)
352 static mdk_rdev_t
* find_rdev(mddev_t
* mddev
, dev_t dev
)
356 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
357 if (rdev
->bdev
->bd_dev
== dev
)
363 static struct mdk_personality
*find_pers(int level
, char *clevel
)
365 struct mdk_personality
*pers
;
366 list_for_each_entry(pers
, &pers_list
, list
) {
367 if (level
!= LEVEL_NONE
&& pers
->level
== level
)
369 if (strcmp(pers
->name
, clevel
)==0)
375 /* return the offset of the super block in 512byte sectors */
376 static inline sector_t
calc_dev_sboffset(struct block_device
*bdev
)
378 sector_t num_sectors
= bdev
->bd_inode
->i_size
/ 512;
379 return MD_NEW_SIZE_SECTORS(num_sectors
);
382 static sector_t
calc_num_sectors(mdk_rdev_t
*rdev
, unsigned chunk_size
)
384 sector_t num_sectors
= rdev
->sb_start
;
387 num_sectors
&= ~((sector_t
)chunk_size
/512 - 1);
391 static int alloc_disk_sb(mdk_rdev_t
* rdev
)
396 rdev
->sb_page
= alloc_page(GFP_KERNEL
);
397 if (!rdev
->sb_page
) {
398 printk(KERN_ALERT
"md: out of memory.\n");
405 static void free_disk_sb(mdk_rdev_t
* rdev
)
408 put_page(rdev
->sb_page
);
410 rdev
->sb_page
= NULL
;
417 static void super_written(struct bio
*bio
, int error
)
419 mdk_rdev_t
*rdev
= bio
->bi_private
;
420 mddev_t
*mddev
= rdev
->mddev
;
422 if (error
|| !test_bit(BIO_UPTODATE
, &bio
->bi_flags
)) {
423 printk("md: super_written gets error=%d, uptodate=%d\n",
424 error
, test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
425 WARN_ON(test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
426 md_error(mddev
, rdev
);
429 if (atomic_dec_and_test(&mddev
->pending_writes
))
430 wake_up(&mddev
->sb_wait
);
434 static void super_written_barrier(struct bio
*bio
, int error
)
436 struct bio
*bio2
= bio
->bi_private
;
437 mdk_rdev_t
*rdev
= bio2
->bi_private
;
438 mddev_t
*mddev
= rdev
->mddev
;
440 if (!test_bit(BIO_UPTODATE
, &bio
->bi_flags
) &&
441 error
== -EOPNOTSUPP
) {
443 /* barriers don't appear to be supported :-( */
444 set_bit(BarriersNotsupp
, &rdev
->flags
);
445 mddev
->barriers_work
= 0;
446 spin_lock_irqsave(&mddev
->write_lock
, flags
);
447 bio2
->bi_next
= mddev
->biolist
;
448 mddev
->biolist
= bio2
;
449 spin_unlock_irqrestore(&mddev
->write_lock
, flags
);
450 wake_up(&mddev
->sb_wait
);
454 bio
->bi_private
= rdev
;
455 super_written(bio
, error
);
459 void md_super_write(mddev_t
*mddev
, mdk_rdev_t
*rdev
,
460 sector_t sector
, int size
, struct page
*page
)
462 /* write first size bytes of page to sector of rdev
463 * Increment mddev->pending_writes before returning
464 * and decrement it on completion, waking up sb_wait
465 * if zero is reached.
466 * If an error occurred, call md_error
468 * As we might need to resubmit the request if BIO_RW_BARRIER
469 * causes ENOTSUPP, we allocate a spare bio...
471 struct bio
*bio
= bio_alloc(GFP_NOIO
, 1);
472 int rw
= (1<<BIO_RW
) | (1<<BIO_RW_SYNCIO
) | (1<<BIO_RW_UNPLUG
);
474 bio
->bi_bdev
= rdev
->bdev
;
475 bio
->bi_sector
= sector
;
476 bio_add_page(bio
, page
, size
, 0);
477 bio
->bi_private
= rdev
;
478 bio
->bi_end_io
= super_written
;
481 atomic_inc(&mddev
->pending_writes
);
482 if (!test_bit(BarriersNotsupp
, &rdev
->flags
)) {
484 rw
|= (1<<BIO_RW_BARRIER
);
485 rbio
= bio_clone(bio
, GFP_NOIO
);
486 rbio
->bi_private
= bio
;
487 rbio
->bi_end_io
= super_written_barrier
;
488 submit_bio(rw
, rbio
);
493 void md_super_wait(mddev_t
*mddev
)
495 /* wait for all superblock writes that were scheduled to complete.
496 * if any had to be retried (due to BARRIER problems), retry them
500 prepare_to_wait(&mddev
->sb_wait
, &wq
, TASK_UNINTERRUPTIBLE
);
501 if (atomic_read(&mddev
->pending_writes
)==0)
503 while (mddev
->biolist
) {
505 spin_lock_irq(&mddev
->write_lock
);
506 bio
= mddev
->biolist
;
507 mddev
->biolist
= bio
->bi_next
;
509 spin_unlock_irq(&mddev
->write_lock
);
510 submit_bio(bio
->bi_rw
, bio
);
514 finish_wait(&mddev
->sb_wait
, &wq
);
517 static void bi_complete(struct bio
*bio
, int error
)
519 complete((struct completion
*)bio
->bi_private
);
522 int sync_page_io(struct block_device
*bdev
, sector_t sector
, int size
,
523 struct page
*page
, int rw
)
525 struct bio
*bio
= bio_alloc(GFP_NOIO
, 1);
526 struct completion event
;
529 rw
|= (1 << BIO_RW_SYNCIO
) | (1 << BIO_RW_UNPLUG
);
532 bio
->bi_sector
= sector
;
533 bio_add_page(bio
, page
, size
, 0);
534 init_completion(&event
);
535 bio
->bi_private
= &event
;
536 bio
->bi_end_io
= bi_complete
;
538 wait_for_completion(&event
);
540 ret
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
544 EXPORT_SYMBOL_GPL(sync_page_io
);
546 static int read_disk_sb(mdk_rdev_t
* rdev
, int size
)
548 char b
[BDEVNAME_SIZE
];
549 if (!rdev
->sb_page
) {
557 if (!sync_page_io(rdev
->bdev
, rdev
->sb_start
, size
, rdev
->sb_page
, READ
))
563 printk(KERN_WARNING
"md: disabled device %s, could not read superblock.\n",
564 bdevname(rdev
->bdev
,b
));
568 static int uuid_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
570 return sb1
->set_uuid0
== sb2
->set_uuid0
&&
571 sb1
->set_uuid1
== sb2
->set_uuid1
&&
572 sb1
->set_uuid2
== sb2
->set_uuid2
&&
573 sb1
->set_uuid3
== sb2
->set_uuid3
;
576 static int sb_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
579 mdp_super_t
*tmp1
, *tmp2
;
581 tmp1
= kmalloc(sizeof(*tmp1
),GFP_KERNEL
);
582 tmp2
= kmalloc(sizeof(*tmp2
),GFP_KERNEL
);
584 if (!tmp1
|| !tmp2
) {
586 printk(KERN_INFO
"md.c sb_equal(): failed to allocate memory!\n");
594 * nr_disks is not constant
599 ret
= (memcmp(tmp1
, tmp2
, MD_SB_GENERIC_CONSTANT_WORDS
* 4) == 0);
607 static u32
md_csum_fold(u32 csum
)
609 csum
= (csum
& 0xffff) + (csum
>> 16);
610 return (csum
& 0xffff) + (csum
>> 16);
613 static unsigned int calc_sb_csum(mdp_super_t
* sb
)
616 u32
*sb32
= (u32
*)sb
;
618 unsigned int disk_csum
, csum
;
620 disk_csum
= sb
->sb_csum
;
623 for (i
= 0; i
< MD_SB_BYTES
/4 ; i
++)
625 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
629 /* This used to use csum_partial, which was wrong for several
630 * reasons including that different results are returned on
631 * different architectures. It isn't critical that we get exactly
632 * the same return value as before (we always csum_fold before
633 * testing, and that removes any differences). However as we
634 * know that csum_partial always returned a 16bit value on
635 * alphas, do a fold to maximise conformity to previous behaviour.
637 sb
->sb_csum
= md_csum_fold(disk_csum
);
639 sb
->sb_csum
= disk_csum
;
646 * Handle superblock details.
647 * We want to be able to handle multiple superblock formats
648 * so we have a common interface to them all, and an array of
649 * different handlers.
650 * We rely on user-space to write the initial superblock, and support
651 * reading and updating of superblocks.
652 * Interface methods are:
653 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
654 * loads and validates a superblock on dev.
655 * if refdev != NULL, compare superblocks on both devices
657 * 0 - dev has a superblock that is compatible with refdev
658 * 1 - dev has a superblock that is compatible and newer than refdev
659 * so dev should be used as the refdev in future
660 * -EINVAL superblock incompatible or invalid
661 * -othererror e.g. -EIO
663 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
664 * Verify that dev is acceptable into mddev.
665 * The first time, mddev->raid_disks will be 0, and data from
666 * dev should be merged in. Subsequent calls check that dev
667 * is new enough. Return 0 or -EINVAL
669 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
670 * Update the superblock for rdev with data in mddev
671 * This does not write to disc.
677 struct module
*owner
;
678 int (*load_super
)(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
,
680 int (*validate_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
681 void (*sync_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
682 unsigned long long (*rdev_size_change
)(mdk_rdev_t
*rdev
,
683 sector_t num_sectors
);
687 * load_super for 0.90.0
689 static int super_90_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
691 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
696 * Calculate the position of the superblock (512byte sectors),
697 * it's at the end of the disk.
699 * It also happens to be a multiple of 4Kb.
701 rdev
->sb_start
= calc_dev_sboffset(rdev
->bdev
);
703 ret
= read_disk_sb(rdev
, MD_SB_BYTES
);
708 bdevname(rdev
->bdev
, b
);
709 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
711 if (sb
->md_magic
!= MD_SB_MAGIC
) {
712 printk(KERN_ERR
"md: invalid raid superblock magic on %s\n",
717 if (sb
->major_version
!= 0 ||
718 sb
->minor_version
< 90 ||
719 sb
->minor_version
> 91) {
720 printk(KERN_WARNING
"Bad version number %d.%d on %s\n",
721 sb
->major_version
, sb
->minor_version
,
726 if (sb
->raid_disks
<= 0)
729 if (md_csum_fold(calc_sb_csum(sb
)) != md_csum_fold(sb
->sb_csum
)) {
730 printk(KERN_WARNING
"md: invalid superblock checksum on %s\n",
735 rdev
->preferred_minor
= sb
->md_minor
;
736 rdev
->data_offset
= 0;
737 rdev
->sb_size
= MD_SB_BYTES
;
739 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
740 if (sb
->level
!= 1 && sb
->level
!= 4
741 && sb
->level
!= 5 && sb
->level
!= 6
742 && sb
->level
!= 10) {
743 /* FIXME use a better test */
745 "md: bitmaps not supported for this level.\n");
750 if (sb
->level
== LEVEL_MULTIPATH
)
753 rdev
->desc_nr
= sb
->this_disk
.number
;
759 mdp_super_t
*refsb
= (mdp_super_t
*)page_address(refdev
->sb_page
);
760 if (!uuid_equal(refsb
, sb
)) {
761 printk(KERN_WARNING
"md: %s has different UUID to %s\n",
762 b
, bdevname(refdev
->bdev
,b2
));
765 if (!sb_equal(refsb
, sb
)) {
766 printk(KERN_WARNING
"md: %s has same UUID"
767 " but different superblock to %s\n",
768 b
, bdevname(refdev
->bdev
, b2
));
772 ev2
= md_event(refsb
);
778 rdev
->size
= calc_num_sectors(rdev
, sb
->chunk_size
) / 2;
780 if (rdev
->size
< sb
->size
&& sb
->level
> 1)
781 /* "this cannot possibly happen" ... */
789 * validate_super for 0.90.0
791 static int super_90_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
794 mdp_super_t
*sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
795 __u64 ev1
= md_event(sb
);
797 rdev
->raid_disk
= -1;
798 clear_bit(Faulty
, &rdev
->flags
);
799 clear_bit(In_sync
, &rdev
->flags
);
800 clear_bit(WriteMostly
, &rdev
->flags
);
801 clear_bit(BarriersNotsupp
, &rdev
->flags
);
803 if (mddev
->raid_disks
== 0) {
804 mddev
->major_version
= 0;
805 mddev
->minor_version
= sb
->minor_version
;
806 mddev
->patch_version
= sb
->patch_version
;
808 mddev
->chunk_size
= sb
->chunk_size
;
809 mddev
->ctime
= sb
->ctime
;
810 mddev
->utime
= sb
->utime
;
811 mddev
->level
= sb
->level
;
812 mddev
->clevel
[0] = 0;
813 mddev
->layout
= sb
->layout
;
814 mddev
->raid_disks
= sb
->raid_disks
;
815 mddev
->size
= sb
->size
;
817 mddev
->bitmap_offset
= 0;
818 mddev
->default_bitmap_offset
= MD_SB_BYTES
>> 9;
820 if (mddev
->minor_version
>= 91) {
821 mddev
->reshape_position
= sb
->reshape_position
;
822 mddev
->delta_disks
= sb
->delta_disks
;
823 mddev
->new_level
= sb
->new_level
;
824 mddev
->new_layout
= sb
->new_layout
;
825 mddev
->new_chunk
= sb
->new_chunk
;
827 mddev
->reshape_position
= MaxSector
;
828 mddev
->delta_disks
= 0;
829 mddev
->new_level
= mddev
->level
;
830 mddev
->new_layout
= mddev
->layout
;
831 mddev
->new_chunk
= mddev
->chunk_size
;
834 if (sb
->state
& (1<<MD_SB_CLEAN
))
835 mddev
->recovery_cp
= MaxSector
;
837 if (sb
->events_hi
== sb
->cp_events_hi
&&
838 sb
->events_lo
== sb
->cp_events_lo
) {
839 mddev
->recovery_cp
= sb
->recovery_cp
;
841 mddev
->recovery_cp
= 0;
844 memcpy(mddev
->uuid
+0, &sb
->set_uuid0
, 4);
845 memcpy(mddev
->uuid
+4, &sb
->set_uuid1
, 4);
846 memcpy(mddev
->uuid
+8, &sb
->set_uuid2
, 4);
847 memcpy(mddev
->uuid
+12,&sb
->set_uuid3
, 4);
849 mddev
->max_disks
= MD_SB_DISKS
;
851 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
) &&
852 mddev
->bitmap_file
== NULL
)
853 mddev
->bitmap_offset
= mddev
->default_bitmap_offset
;
855 } else if (mddev
->pers
== NULL
) {
856 /* Insist on good event counter while assembling */
858 if (ev1
< mddev
->events
)
860 } else if (mddev
->bitmap
) {
861 /* if adding to array with a bitmap, then we can accept an
862 * older device ... but not too old.
864 if (ev1
< mddev
->bitmap
->events_cleared
)
867 if (ev1
< mddev
->events
)
868 /* just a hot-add of a new device, leave raid_disk at -1 */
872 if (mddev
->level
!= LEVEL_MULTIPATH
) {
873 desc
= sb
->disks
+ rdev
->desc_nr
;
875 if (desc
->state
& (1<<MD_DISK_FAULTY
))
876 set_bit(Faulty
, &rdev
->flags
);
877 else if (desc
->state
& (1<<MD_DISK_SYNC
) /* &&
878 desc->raid_disk < mddev->raid_disks */) {
879 set_bit(In_sync
, &rdev
->flags
);
880 rdev
->raid_disk
= desc
->raid_disk
;
882 if (desc
->state
& (1<<MD_DISK_WRITEMOSTLY
))
883 set_bit(WriteMostly
, &rdev
->flags
);
884 } else /* MULTIPATH are always insync */
885 set_bit(In_sync
, &rdev
->flags
);
890 * sync_super for 0.90.0
892 static void super_90_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
896 int next_spare
= mddev
->raid_disks
;
899 /* make rdev->sb match mddev data..
902 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
903 * 3/ any empty disks < next_spare become removed
905 * disks[0] gets initialised to REMOVED because
906 * we cannot be sure from other fields if it has
907 * been initialised or not.
910 int active
=0, working
=0,failed
=0,spare
=0,nr_disks
=0;
912 rdev
->sb_size
= MD_SB_BYTES
;
914 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
916 memset(sb
, 0, sizeof(*sb
));
918 sb
->md_magic
= MD_SB_MAGIC
;
919 sb
->major_version
= mddev
->major_version
;
920 sb
->patch_version
= mddev
->patch_version
;
921 sb
->gvalid_words
= 0; /* ignored */
922 memcpy(&sb
->set_uuid0
, mddev
->uuid
+0, 4);
923 memcpy(&sb
->set_uuid1
, mddev
->uuid
+4, 4);
924 memcpy(&sb
->set_uuid2
, mddev
->uuid
+8, 4);
925 memcpy(&sb
->set_uuid3
, mddev
->uuid
+12,4);
927 sb
->ctime
= mddev
->ctime
;
928 sb
->level
= mddev
->level
;
929 sb
->size
= mddev
->size
;
930 sb
->raid_disks
= mddev
->raid_disks
;
931 sb
->md_minor
= mddev
->md_minor
;
932 sb
->not_persistent
= 0;
933 sb
->utime
= mddev
->utime
;
935 sb
->events_hi
= (mddev
->events
>>32);
936 sb
->events_lo
= (u32
)mddev
->events
;
938 if (mddev
->reshape_position
== MaxSector
)
939 sb
->minor_version
= 90;
941 sb
->minor_version
= 91;
942 sb
->reshape_position
= mddev
->reshape_position
;
943 sb
->new_level
= mddev
->new_level
;
944 sb
->delta_disks
= mddev
->delta_disks
;
945 sb
->new_layout
= mddev
->new_layout
;
946 sb
->new_chunk
= mddev
->new_chunk
;
948 mddev
->minor_version
= sb
->minor_version
;
951 sb
->recovery_cp
= mddev
->recovery_cp
;
952 sb
->cp_events_hi
= (mddev
->events
>>32);
953 sb
->cp_events_lo
= (u32
)mddev
->events
;
954 if (mddev
->recovery_cp
== MaxSector
)
955 sb
->state
= (1<< MD_SB_CLEAN
);
959 sb
->layout
= mddev
->layout
;
960 sb
->chunk_size
= mddev
->chunk_size
;
962 if (mddev
->bitmap
&& mddev
->bitmap_file
== NULL
)
963 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
965 sb
->disks
[0].state
= (1<<MD_DISK_REMOVED
);
966 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
969 if (rdev2
->raid_disk
>= 0 && test_bit(In_sync
, &rdev2
->flags
)
970 && !test_bit(Faulty
, &rdev2
->flags
))
971 desc_nr
= rdev2
->raid_disk
;
973 desc_nr
= next_spare
++;
974 rdev2
->desc_nr
= desc_nr
;
975 d
= &sb
->disks
[rdev2
->desc_nr
];
977 d
->number
= rdev2
->desc_nr
;
978 d
->major
= MAJOR(rdev2
->bdev
->bd_dev
);
979 d
->minor
= MINOR(rdev2
->bdev
->bd_dev
);
980 if (rdev2
->raid_disk
>= 0 && test_bit(In_sync
, &rdev2
->flags
)
981 && !test_bit(Faulty
, &rdev2
->flags
))
982 d
->raid_disk
= rdev2
->raid_disk
;
984 d
->raid_disk
= rdev2
->desc_nr
; /* compatibility */
985 if (test_bit(Faulty
, &rdev2
->flags
))
986 d
->state
= (1<<MD_DISK_FAULTY
);
987 else if (test_bit(In_sync
, &rdev2
->flags
)) {
988 d
->state
= (1<<MD_DISK_ACTIVE
);
989 d
->state
|= (1<<MD_DISK_SYNC
);
997 if (test_bit(WriteMostly
, &rdev2
->flags
))
998 d
->state
|= (1<<MD_DISK_WRITEMOSTLY
);
1000 /* now set the "removed" and "faulty" bits on any missing devices */
1001 for (i
=0 ; i
< mddev
->raid_disks
; i
++) {
1002 mdp_disk_t
*d
= &sb
->disks
[i
];
1003 if (d
->state
== 0 && d
->number
== 0) {
1006 d
->state
= (1<<MD_DISK_REMOVED
);
1007 d
->state
|= (1<<MD_DISK_FAULTY
);
1011 sb
->nr_disks
= nr_disks
;
1012 sb
->active_disks
= active
;
1013 sb
->working_disks
= working
;
1014 sb
->failed_disks
= failed
;
1015 sb
->spare_disks
= spare
;
1017 sb
->this_disk
= sb
->disks
[rdev
->desc_nr
];
1018 sb
->sb_csum
= calc_sb_csum(sb
);
1022 * rdev_size_change for 0.90.0
1024 static unsigned long long
1025 super_90_rdev_size_change(mdk_rdev_t
*rdev
, sector_t num_sectors
)
1027 if (num_sectors
&& num_sectors
< rdev
->mddev
->size
* 2)
1028 return 0; /* component must fit device */
1029 if (rdev
->mddev
->bitmap_offset
)
1030 return 0; /* can't move bitmap */
1031 rdev
->sb_start
= calc_dev_sboffset(rdev
->bdev
);
1032 if (!num_sectors
|| num_sectors
> rdev
->sb_start
)
1033 num_sectors
= rdev
->sb_start
;
1034 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1036 md_super_wait(rdev
->mddev
);
1037 return num_sectors
/ 2; /* kB for sysfs */
1042 * version 1 superblock
1045 static __le32
calc_sb_1_csum(struct mdp_superblock_1
* sb
)
1049 unsigned long long newcsum
;
1050 int size
= 256 + le32_to_cpu(sb
->max_dev
)*2;
1051 __le32
*isuper
= (__le32
*)sb
;
1054 disk_csum
= sb
->sb_csum
;
1057 for (i
=0; size
>=4; size
-= 4 )
1058 newcsum
+= le32_to_cpu(*isuper
++);
1061 newcsum
+= le16_to_cpu(*(__le16
*) isuper
);
1063 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
1064 sb
->sb_csum
= disk_csum
;
1065 return cpu_to_le32(csum
);
1068 static int super_1_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
1070 struct mdp_superblock_1
*sb
;
1073 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1077 * Calculate the position of the superblock in 512byte sectors.
1078 * It is always aligned to a 4K boundary and
1079 * depeding on minor_version, it can be:
1080 * 0: At least 8K, but less than 12K, from end of device
1081 * 1: At start of device
1082 * 2: 4K from start of device.
1084 switch(minor_version
) {
1086 sb_start
= rdev
->bdev
->bd_inode
->i_size
>> 9;
1088 sb_start
&= ~(sector_t
)(4*2-1);
1099 rdev
->sb_start
= sb_start
;
1101 /* superblock is rarely larger than 1K, but it can be larger,
1102 * and it is safe to read 4k, so we do that
1104 ret
= read_disk_sb(rdev
, 4096);
1105 if (ret
) return ret
;
1108 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1110 if (sb
->magic
!= cpu_to_le32(MD_SB_MAGIC
) ||
1111 sb
->major_version
!= cpu_to_le32(1) ||
1112 le32_to_cpu(sb
->max_dev
) > (4096-256)/2 ||
1113 le64_to_cpu(sb
->super_offset
) != rdev
->sb_start
||
1114 (le32_to_cpu(sb
->feature_map
) & ~MD_FEATURE_ALL
) != 0)
1117 if (calc_sb_1_csum(sb
) != sb
->sb_csum
) {
1118 printk("md: invalid superblock checksum on %s\n",
1119 bdevname(rdev
->bdev
,b
));
1122 if (le64_to_cpu(sb
->data_size
) < 10) {
1123 printk("md: data_size too small on %s\n",
1124 bdevname(rdev
->bdev
,b
));
1127 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BITMAP_OFFSET
)) {
1128 if (sb
->level
!= cpu_to_le32(1) &&
1129 sb
->level
!= cpu_to_le32(4) &&
1130 sb
->level
!= cpu_to_le32(5) &&
1131 sb
->level
!= cpu_to_le32(6) &&
1132 sb
->level
!= cpu_to_le32(10)) {
1134 "md: bitmaps not supported for this level.\n");
1139 rdev
->preferred_minor
= 0xffff;
1140 rdev
->data_offset
= le64_to_cpu(sb
->data_offset
);
1141 atomic_set(&rdev
->corrected_errors
, le32_to_cpu(sb
->cnt_corrected_read
));
1143 rdev
->sb_size
= le32_to_cpu(sb
->max_dev
) * 2 + 256;
1144 bmask
= queue_hardsect_size(rdev
->bdev
->bd_disk
->queue
)-1;
1145 if (rdev
->sb_size
& bmask
)
1146 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1149 && rdev
->data_offset
< sb_start
+ (rdev
->sb_size
/512))
1152 if (sb
->level
== cpu_to_le32(LEVEL_MULTIPATH
))
1155 rdev
->desc_nr
= le32_to_cpu(sb
->dev_number
);
1161 struct mdp_superblock_1
*refsb
=
1162 (struct mdp_superblock_1
*)page_address(refdev
->sb_page
);
1164 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16) != 0 ||
1165 sb
->level
!= refsb
->level
||
1166 sb
->layout
!= refsb
->layout
||
1167 sb
->chunksize
!= refsb
->chunksize
) {
1168 printk(KERN_WARNING
"md: %s has strangely different"
1169 " superblock to %s\n",
1170 bdevname(rdev
->bdev
,b
),
1171 bdevname(refdev
->bdev
,b2
));
1174 ev1
= le64_to_cpu(sb
->events
);
1175 ev2
= le64_to_cpu(refsb
->events
);
1183 rdev
->size
= ((rdev
->bdev
->bd_inode
->i_size
>>9) - le64_to_cpu(sb
->data_offset
)) / 2;
1185 rdev
->size
= rdev
->sb_start
/ 2;
1186 if (rdev
->size
< le64_to_cpu(sb
->data_size
)/2)
1188 rdev
->size
= le64_to_cpu(sb
->data_size
)/2;
1189 if (le32_to_cpu(sb
->chunksize
))
1190 rdev
->size
&= ~((sector_t
)le32_to_cpu(sb
->chunksize
)/2 - 1);
1192 if (le64_to_cpu(sb
->size
) > rdev
->size
*2)
1197 static int super_1_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1199 struct mdp_superblock_1
*sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1200 __u64 ev1
= le64_to_cpu(sb
->events
);
1202 rdev
->raid_disk
= -1;
1203 clear_bit(Faulty
, &rdev
->flags
);
1204 clear_bit(In_sync
, &rdev
->flags
);
1205 clear_bit(WriteMostly
, &rdev
->flags
);
1206 clear_bit(BarriersNotsupp
, &rdev
->flags
);
1208 if (mddev
->raid_disks
== 0) {
1209 mddev
->major_version
= 1;
1210 mddev
->patch_version
= 0;
1211 mddev
->external
= 0;
1212 mddev
->chunk_size
= le32_to_cpu(sb
->chunksize
) << 9;
1213 mddev
->ctime
= le64_to_cpu(sb
->ctime
) & ((1ULL << 32)-1);
1214 mddev
->utime
= le64_to_cpu(sb
->utime
) & ((1ULL << 32)-1);
1215 mddev
->level
= le32_to_cpu(sb
->level
);
1216 mddev
->clevel
[0] = 0;
1217 mddev
->layout
= le32_to_cpu(sb
->layout
);
1218 mddev
->raid_disks
= le32_to_cpu(sb
->raid_disks
);
1219 mddev
->size
= le64_to_cpu(sb
->size
)/2;
1220 mddev
->events
= ev1
;
1221 mddev
->bitmap_offset
= 0;
1222 mddev
->default_bitmap_offset
= 1024 >> 9;
1224 mddev
->recovery_cp
= le64_to_cpu(sb
->resync_offset
);
1225 memcpy(mddev
->uuid
, sb
->set_uuid
, 16);
1227 mddev
->max_disks
= (4096-256)/2;
1229 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BITMAP_OFFSET
) &&
1230 mddev
->bitmap_file
== NULL
)
1231 mddev
->bitmap_offset
= (__s32
)le32_to_cpu(sb
->bitmap_offset
);
1233 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
1234 mddev
->reshape_position
= le64_to_cpu(sb
->reshape_position
);
1235 mddev
->delta_disks
= le32_to_cpu(sb
->delta_disks
);
1236 mddev
->new_level
= le32_to_cpu(sb
->new_level
);
1237 mddev
->new_layout
= le32_to_cpu(sb
->new_layout
);
1238 mddev
->new_chunk
= le32_to_cpu(sb
->new_chunk
)<<9;
1240 mddev
->reshape_position
= MaxSector
;
1241 mddev
->delta_disks
= 0;
1242 mddev
->new_level
= mddev
->level
;
1243 mddev
->new_layout
= mddev
->layout
;
1244 mddev
->new_chunk
= mddev
->chunk_size
;
1247 } else if (mddev
->pers
== NULL
) {
1248 /* Insist of good event counter while assembling */
1250 if (ev1
< mddev
->events
)
1252 } else if (mddev
->bitmap
) {
1253 /* If adding to array with a bitmap, then we can accept an
1254 * older device, but not too old.
1256 if (ev1
< mddev
->bitmap
->events_cleared
)
1259 if (ev1
< mddev
->events
)
1260 /* just a hot-add of a new device, leave raid_disk at -1 */
1263 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1265 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
1267 case 0xffff: /* spare */
1269 case 0xfffe: /* faulty */
1270 set_bit(Faulty
, &rdev
->flags
);
1273 if ((le32_to_cpu(sb
->feature_map
) &
1274 MD_FEATURE_RECOVERY_OFFSET
))
1275 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
1277 set_bit(In_sync
, &rdev
->flags
);
1278 rdev
->raid_disk
= role
;
1281 if (sb
->devflags
& WriteMostly1
)
1282 set_bit(WriteMostly
, &rdev
->flags
);
1283 } else /* MULTIPATH are always insync */
1284 set_bit(In_sync
, &rdev
->flags
);
1289 static void super_1_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1291 struct mdp_superblock_1
*sb
;
1294 /* make rdev->sb match mddev and rdev data. */
1296 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1298 sb
->feature_map
= 0;
1300 sb
->recovery_offset
= cpu_to_le64(0);
1301 memset(sb
->pad1
, 0, sizeof(sb
->pad1
));
1302 memset(sb
->pad2
, 0, sizeof(sb
->pad2
));
1303 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
1305 sb
->utime
= cpu_to_le64((__u64
)mddev
->utime
);
1306 sb
->events
= cpu_to_le64(mddev
->events
);
1308 sb
->resync_offset
= cpu_to_le64(mddev
->recovery_cp
);
1310 sb
->resync_offset
= cpu_to_le64(0);
1312 sb
->cnt_corrected_read
= cpu_to_le32(atomic_read(&rdev
->corrected_errors
));
1314 sb
->raid_disks
= cpu_to_le32(mddev
->raid_disks
);
1315 sb
->size
= cpu_to_le64(mddev
->size
<<1);
1317 if (mddev
->bitmap
&& mddev
->bitmap_file
== NULL
) {
1318 sb
->bitmap_offset
= cpu_to_le32((__u32
)mddev
->bitmap_offset
);
1319 sb
->feature_map
= cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
);
1322 if (rdev
->raid_disk
>= 0 &&
1323 !test_bit(In_sync
, &rdev
->flags
) &&
1324 rdev
->recovery_offset
> 0) {
1325 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET
);
1326 sb
->recovery_offset
= cpu_to_le64(rdev
->recovery_offset
);
1329 if (mddev
->reshape_position
!= MaxSector
) {
1330 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE
);
1331 sb
->reshape_position
= cpu_to_le64(mddev
->reshape_position
);
1332 sb
->new_layout
= cpu_to_le32(mddev
->new_layout
);
1333 sb
->delta_disks
= cpu_to_le32(mddev
->delta_disks
);
1334 sb
->new_level
= cpu_to_le32(mddev
->new_level
);
1335 sb
->new_chunk
= cpu_to_le32(mddev
->new_chunk
>>9);
1339 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
)
1340 if (rdev2
->desc_nr
+1 > max_dev
)
1341 max_dev
= rdev2
->desc_nr
+1;
1343 if (max_dev
> le32_to_cpu(sb
->max_dev
))
1344 sb
->max_dev
= cpu_to_le32(max_dev
);
1345 for (i
=0; i
<max_dev
;i
++)
1346 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1348 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
1350 if (test_bit(Faulty
, &rdev2
->flags
))
1351 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1352 else if (test_bit(In_sync
, &rdev2
->flags
))
1353 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1354 else if (rdev2
->raid_disk
>= 0 && rdev2
->recovery_offset
> 0)
1355 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1357 sb
->dev_roles
[i
] = cpu_to_le16(0xffff);
1360 sb
->sb_csum
= calc_sb_1_csum(sb
);
1363 static unsigned long long
1364 super_1_rdev_size_change(mdk_rdev_t
*rdev
, sector_t num_sectors
)
1366 struct mdp_superblock_1
*sb
;
1367 sector_t max_sectors
;
1368 if (num_sectors
&& num_sectors
< rdev
->mddev
->size
* 2)
1369 return 0; /* component must fit device */
1370 if (rdev
->sb_start
< rdev
->data_offset
) {
1371 /* minor versions 1 and 2; superblock before data */
1372 max_sectors
= rdev
->bdev
->bd_inode
->i_size
>> 9;
1373 max_sectors
-= rdev
->data_offset
;
1374 if (!num_sectors
|| num_sectors
> max_sectors
)
1375 num_sectors
= max_sectors
;
1376 } else if (rdev
->mddev
->bitmap_offset
) {
1377 /* minor version 0 with bitmap we can't move */
1380 /* minor version 0; superblock after data */
1382 sb_start
= (rdev
->bdev
->bd_inode
->i_size
>> 9) - 8*2;
1383 sb_start
&= ~(sector_t
)(4*2 - 1);
1384 max_sectors
= rdev
->size
* 2 + sb_start
- rdev
->sb_start
;
1385 if (!num_sectors
|| num_sectors
> max_sectors
)
1386 num_sectors
= max_sectors
;
1387 rdev
->sb_start
= sb_start
;
1389 sb
= (struct mdp_superblock_1
*) page_address(rdev
->sb_page
);
1390 sb
->data_size
= cpu_to_le64(num_sectors
);
1391 sb
->super_offset
= rdev
->sb_start
;
1392 sb
->sb_csum
= calc_sb_1_csum(sb
);
1393 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1395 md_super_wait(rdev
->mddev
);
1396 return num_sectors
/ 2; /* kB for sysfs */
1399 static struct super_type super_types
[] = {
1402 .owner
= THIS_MODULE
,
1403 .load_super
= super_90_load
,
1404 .validate_super
= super_90_validate
,
1405 .sync_super
= super_90_sync
,
1406 .rdev_size_change
= super_90_rdev_size_change
,
1410 .owner
= THIS_MODULE
,
1411 .load_super
= super_1_load
,
1412 .validate_super
= super_1_validate
,
1413 .sync_super
= super_1_sync
,
1414 .rdev_size_change
= super_1_rdev_size_change
,
1418 static int match_mddev_units(mddev_t
*mddev1
, mddev_t
*mddev2
)
1420 mdk_rdev_t
*rdev
, *rdev2
;
1423 rdev_for_each_rcu(rdev
, mddev1
)
1424 rdev_for_each_rcu(rdev2
, mddev2
)
1425 if (rdev
->bdev
->bd_contains
==
1426 rdev2
->bdev
->bd_contains
) {
1434 static LIST_HEAD(pending_raid_disks
);
1436 static int bind_rdev_to_array(mdk_rdev_t
* rdev
, mddev_t
* mddev
)
1438 char b
[BDEVNAME_SIZE
];
1448 /* prevent duplicates */
1449 if (find_rdev(mddev
, rdev
->bdev
->bd_dev
))
1452 /* make sure rdev->size exceeds mddev->size */
1453 if (rdev
->size
&& (mddev
->size
== 0 || rdev
->size
< mddev
->size
)) {
1455 /* Cannot change size, so fail
1456 * If mddev->level <= 0, then we don't care
1457 * about aligning sizes (e.g. linear)
1459 if (mddev
->level
> 0)
1462 mddev
->size
= rdev
->size
;
1465 /* Verify rdev->desc_nr is unique.
1466 * If it is -1, assign a free number, else
1467 * check number is not in use
1469 if (rdev
->desc_nr
< 0) {
1471 if (mddev
->pers
) choice
= mddev
->raid_disks
;
1472 while (find_rdev_nr(mddev
, choice
))
1474 rdev
->desc_nr
= choice
;
1476 if (find_rdev_nr(mddev
, rdev
->desc_nr
))
1479 if (mddev
->max_disks
&& rdev
->desc_nr
>= mddev
->max_disks
) {
1480 printk(KERN_WARNING
"md: %s: array is limited to %d devices\n",
1481 mdname(mddev
), mddev
->max_disks
);
1484 bdevname(rdev
->bdev
,b
);
1485 while ( (s
=strchr(b
, '/')) != NULL
)
1488 rdev
->mddev
= mddev
;
1489 printk(KERN_INFO
"md: bind<%s>\n", b
);
1491 if ((err
= kobject_add(&rdev
->kobj
, &mddev
->kobj
, "dev-%s", b
)))
1494 ko
= &part_to_dev(rdev
->bdev
->bd_part
)->kobj
;
1495 if ((err
= sysfs_create_link(&rdev
->kobj
, ko
, "block"))) {
1496 kobject_del(&rdev
->kobj
);
1499 rdev
->sysfs_state
= sysfs_get_dirent(rdev
->kobj
.sd
, "state");
1501 list_add_rcu(&rdev
->same_set
, &mddev
->disks
);
1502 bd_claim_by_disk(rdev
->bdev
, rdev
->bdev
->bd_holder
, mddev
->gendisk
);
1504 /* May as well allow recovery to be retried once */
1505 mddev
->recovery_disabled
= 0;
1509 printk(KERN_WARNING
"md: failed to register dev-%s for %s\n",
1514 static void md_delayed_delete(struct work_struct
*ws
)
1516 mdk_rdev_t
*rdev
= container_of(ws
, mdk_rdev_t
, del_work
);
1517 kobject_del(&rdev
->kobj
);
1518 kobject_put(&rdev
->kobj
);
1521 static void unbind_rdev_from_array(mdk_rdev_t
* rdev
)
1523 char b
[BDEVNAME_SIZE
];
1528 bd_release_from_disk(rdev
->bdev
, rdev
->mddev
->gendisk
);
1529 list_del_rcu(&rdev
->same_set
);
1530 printk(KERN_INFO
"md: unbind<%s>\n", bdevname(rdev
->bdev
,b
));
1532 sysfs_remove_link(&rdev
->kobj
, "block");
1533 sysfs_put(rdev
->sysfs_state
);
1534 rdev
->sysfs_state
= NULL
;
1535 /* We need to delay this, otherwise we can deadlock when
1536 * writing to 'remove' to "dev/state". We also need
1537 * to delay it due to rcu usage.
1540 INIT_WORK(&rdev
->del_work
, md_delayed_delete
);
1541 kobject_get(&rdev
->kobj
);
1542 schedule_work(&rdev
->del_work
);
1546 * prevent the device from being mounted, repartitioned or
1547 * otherwise reused by a RAID array (or any other kernel
1548 * subsystem), by bd_claiming the device.
1550 static int lock_rdev(mdk_rdev_t
*rdev
, dev_t dev
, int shared
)
1553 struct block_device
*bdev
;
1554 char b
[BDEVNAME_SIZE
];
1556 bdev
= open_by_devnum(dev
, FMODE_READ
|FMODE_WRITE
);
1558 printk(KERN_ERR
"md: could not open %s.\n",
1559 __bdevname(dev
, b
));
1560 return PTR_ERR(bdev
);
1562 err
= bd_claim(bdev
, shared
? (mdk_rdev_t
*)lock_rdev
: rdev
);
1564 printk(KERN_ERR
"md: could not bd_claim %s.\n",
1566 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
);
1570 set_bit(AllReserved
, &rdev
->flags
);
1575 static void unlock_rdev(mdk_rdev_t
*rdev
)
1577 struct block_device
*bdev
= rdev
->bdev
;
1582 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
);
1585 void md_autodetect_dev(dev_t dev
);
1587 static void export_rdev(mdk_rdev_t
* rdev
)
1589 char b
[BDEVNAME_SIZE
];
1590 printk(KERN_INFO
"md: export_rdev(%s)\n",
1591 bdevname(rdev
->bdev
,b
));
1596 if (test_bit(AutoDetected
, &rdev
->flags
))
1597 md_autodetect_dev(rdev
->bdev
->bd_dev
);
1600 kobject_put(&rdev
->kobj
);
1603 static void kick_rdev_from_array(mdk_rdev_t
* rdev
)
1605 unbind_rdev_from_array(rdev
);
1609 static void export_array(mddev_t
*mddev
)
1611 mdk_rdev_t
*rdev
, *tmp
;
1613 rdev_for_each(rdev
, tmp
, mddev
) {
1618 kick_rdev_from_array(rdev
);
1620 if (!list_empty(&mddev
->disks
))
1622 mddev
->raid_disks
= 0;
1623 mddev
->major_version
= 0;
1626 static void print_desc(mdp_disk_t
*desc
)
1628 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc
->number
,
1629 desc
->major
,desc
->minor
,desc
->raid_disk
,desc
->state
);
1632 static void print_sb_90(mdp_super_t
*sb
)
1637 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1638 sb
->major_version
, sb
->minor_version
, sb
->patch_version
,
1639 sb
->set_uuid0
, sb
->set_uuid1
, sb
->set_uuid2
, sb
->set_uuid3
,
1641 printk(KERN_INFO
"md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1642 sb
->level
, sb
->size
, sb
->nr_disks
, sb
->raid_disks
,
1643 sb
->md_minor
, sb
->layout
, sb
->chunk_size
);
1644 printk(KERN_INFO
"md: UT:%08x ST:%d AD:%d WD:%d"
1645 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1646 sb
->utime
, sb
->state
, sb
->active_disks
, sb
->working_disks
,
1647 sb
->failed_disks
, sb
->spare_disks
,
1648 sb
->sb_csum
, (unsigned long)sb
->events_lo
);
1651 for (i
= 0; i
< MD_SB_DISKS
; i
++) {
1654 desc
= sb
->disks
+ i
;
1655 if (desc
->number
|| desc
->major
|| desc
->minor
||
1656 desc
->raid_disk
|| (desc
->state
&& (desc
->state
!= 4))) {
1657 printk(" D %2d: ", i
);
1661 printk(KERN_INFO
"md: THIS: ");
1662 print_desc(&sb
->this_disk
);
1665 static void print_sb_1(struct mdp_superblock_1
*sb
)
1669 uuid
= sb
->set_uuid
;
1670 printk(KERN_INFO
"md: SB: (V:%u) (F:0x%08x) Array-ID:<%02x%02x%02x%02x"
1671 ":%02x%02x:%02x%02x:%02x%02x:%02x%02x%02x%02x%02x%02x>\n"
1672 KERN_INFO
"md: Name: \"%s\" CT:%llu\n",
1673 le32_to_cpu(sb
->major_version
),
1674 le32_to_cpu(sb
->feature_map
),
1675 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
1676 uuid
[4], uuid
[5], uuid
[6], uuid
[7],
1677 uuid
[8], uuid
[9], uuid
[10], uuid
[11],
1678 uuid
[12], uuid
[13], uuid
[14], uuid
[15],
1680 (unsigned long long)le64_to_cpu(sb
->ctime
)
1681 & MD_SUPERBLOCK_1_TIME_SEC_MASK
);
1683 uuid
= sb
->device_uuid
;
1684 printk(KERN_INFO
"md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
1686 KERN_INFO
"md: Dev:%08x UUID: %02x%02x%02x%02x:%02x%02x:%02x%02x:%02x%02x"
1687 ":%02x%02x%02x%02x%02x%02x\n"
1688 KERN_INFO
"md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
1689 KERN_INFO
"md: (MaxDev:%u) \n",
1690 le32_to_cpu(sb
->level
),
1691 (unsigned long long)le64_to_cpu(sb
->size
),
1692 le32_to_cpu(sb
->raid_disks
),
1693 le32_to_cpu(sb
->layout
),
1694 le32_to_cpu(sb
->chunksize
),
1695 (unsigned long long)le64_to_cpu(sb
->data_offset
),
1696 (unsigned long long)le64_to_cpu(sb
->data_size
),
1697 (unsigned long long)le64_to_cpu(sb
->super_offset
),
1698 (unsigned long long)le64_to_cpu(sb
->recovery_offset
),
1699 le32_to_cpu(sb
->dev_number
),
1700 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
1701 uuid
[4], uuid
[5], uuid
[6], uuid
[7],
1702 uuid
[8], uuid
[9], uuid
[10], uuid
[11],
1703 uuid
[12], uuid
[13], uuid
[14], uuid
[15],
1705 (unsigned long long)le64_to_cpu(sb
->utime
) & MD_SUPERBLOCK_1_TIME_SEC_MASK
,
1706 (unsigned long long)le64_to_cpu(sb
->events
),
1707 (unsigned long long)le64_to_cpu(sb
->resync_offset
),
1708 le32_to_cpu(sb
->sb_csum
),
1709 le32_to_cpu(sb
->max_dev
)
1713 static void print_rdev(mdk_rdev_t
*rdev
, int major_version
)
1715 char b
[BDEVNAME_SIZE
];
1716 printk(KERN_INFO
"md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1717 bdevname(rdev
->bdev
,b
), (unsigned long long)rdev
->size
,
1718 test_bit(Faulty
, &rdev
->flags
), test_bit(In_sync
, &rdev
->flags
),
1720 if (rdev
->sb_loaded
) {
1721 printk(KERN_INFO
"md: rdev superblock (MJ:%d):\n", major_version
);
1722 switch (major_version
) {
1724 print_sb_90((mdp_super_t
*)page_address(rdev
->sb_page
));
1727 print_sb_1((struct mdp_superblock_1
*)page_address(rdev
->sb_page
));
1731 printk(KERN_INFO
"md: no rdev superblock!\n");
1734 static void md_print_devices(void)
1736 struct list_head
*tmp
;
1739 char b
[BDEVNAME_SIZE
];
1742 printk("md: **********************************\n");
1743 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1744 printk("md: **********************************\n");
1745 for_each_mddev(mddev
, tmp
) {
1748 bitmap_print_sb(mddev
->bitmap
);
1750 printk("%s: ", mdname(mddev
));
1751 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
1752 printk("<%s>", bdevname(rdev
->bdev
,b
));
1755 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
1756 print_rdev(rdev
, mddev
->major_version
);
1758 printk("md: **********************************\n");
1763 static void sync_sbs(mddev_t
* mddev
, int nospares
)
1765 /* Update each superblock (in-memory image), but
1766 * if we are allowed to, skip spares which already
1767 * have the right event counter, or have one earlier
1768 * (which would mean they aren't being marked as dirty
1769 * with the rest of the array)
1773 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
1774 if (rdev
->sb_events
== mddev
->events
||
1776 rdev
->raid_disk
< 0 &&
1777 (rdev
->sb_events
&1)==0 &&
1778 rdev
->sb_events
+1 == mddev
->events
)) {
1779 /* Don't update this superblock */
1780 rdev
->sb_loaded
= 2;
1782 super_types
[mddev
->major_version
].
1783 sync_super(mddev
, rdev
);
1784 rdev
->sb_loaded
= 1;
1789 static void md_update_sb(mddev_t
* mddev
, int force_change
)
1795 if (mddev
->external
)
1798 spin_lock_irq(&mddev
->write_lock
);
1800 set_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
1801 if (test_and_clear_bit(MD_CHANGE_DEVS
, &mddev
->flags
))
1803 if (test_and_clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
))
1804 /* just a clean<-> dirty transition, possibly leave spares alone,
1805 * though if events isn't the right even/odd, we will have to do
1811 if (mddev
->degraded
)
1812 /* If the array is degraded, then skipping spares is both
1813 * dangerous and fairly pointless.
1814 * Dangerous because a device that was removed from the array
1815 * might have a event_count that still looks up-to-date,
1816 * so it can be re-added without a resync.
1817 * Pointless because if there are any spares to skip,
1818 * then a recovery will happen and soon that array won't
1819 * be degraded any more and the spare can go back to sleep then.
1823 sync_req
= mddev
->in_sync
;
1824 mddev
->utime
= get_seconds();
1826 /* If this is just a dirty<->clean transition, and the array is clean
1827 * and 'events' is odd, we can roll back to the previous clean state */
1829 && (mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
1830 && (mddev
->events
& 1)
1831 && mddev
->events
!= 1)
1834 /* otherwise we have to go forward and ... */
1836 if (!mddev
->in_sync
|| mddev
->recovery_cp
!= MaxSector
) { /* not clean */
1837 /* .. if the array isn't clean, insist on an odd 'events' */
1838 if ((mddev
->events
&1)==0) {
1843 /* otherwise insist on an even 'events' (for clean states) */
1844 if ((mddev
->events
&1)) {
1851 if (!mddev
->events
) {
1853 * oops, this 64-bit counter should never wrap.
1854 * Either we are in around ~1 trillion A.C., assuming
1855 * 1 reboot per second, or we have a bug:
1862 * do not write anything to disk if using
1863 * nonpersistent superblocks
1865 if (!mddev
->persistent
) {
1866 if (!mddev
->external
)
1867 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
1869 spin_unlock_irq(&mddev
->write_lock
);
1870 wake_up(&mddev
->sb_wait
);
1873 sync_sbs(mddev
, nospares
);
1874 spin_unlock_irq(&mddev
->write_lock
);
1877 "md: updating %s RAID superblock on device (in sync %d)\n",
1878 mdname(mddev
),mddev
->in_sync
);
1880 bitmap_update_sb(mddev
->bitmap
);
1881 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
1882 char b
[BDEVNAME_SIZE
];
1883 dprintk(KERN_INFO
"md: ");
1884 if (rdev
->sb_loaded
!= 1)
1885 continue; /* no noise on spare devices */
1886 if (test_bit(Faulty
, &rdev
->flags
))
1887 dprintk("(skipping faulty ");
1889 dprintk("%s ", bdevname(rdev
->bdev
,b
));
1890 if (!test_bit(Faulty
, &rdev
->flags
)) {
1891 md_super_write(mddev
,rdev
,
1892 rdev
->sb_start
, rdev
->sb_size
,
1894 dprintk(KERN_INFO
"(write) %s's sb offset: %llu\n",
1895 bdevname(rdev
->bdev
,b
),
1896 (unsigned long long)rdev
->sb_start
);
1897 rdev
->sb_events
= mddev
->events
;
1901 if (mddev
->level
== LEVEL_MULTIPATH
)
1902 /* only need to write one superblock... */
1905 md_super_wait(mddev
);
1906 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
1908 spin_lock_irq(&mddev
->write_lock
);
1909 if (mddev
->in_sync
!= sync_req
||
1910 test_bit(MD_CHANGE_DEVS
, &mddev
->flags
)) {
1911 /* have to write it out again */
1912 spin_unlock_irq(&mddev
->write_lock
);
1915 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
1916 spin_unlock_irq(&mddev
->write_lock
);
1917 wake_up(&mddev
->sb_wait
);
1921 /* words written to sysfs files may, or may not, be \n terminated.
1922 * We want to accept with case. For this we use cmd_match.
1924 static int cmd_match(const char *cmd
, const char *str
)
1926 /* See if cmd, written into a sysfs file, matches
1927 * str. They must either be the same, or cmd can
1928 * have a trailing newline
1930 while (*cmd
&& *str
&& *cmd
== *str
) {
1941 struct rdev_sysfs_entry
{
1942 struct attribute attr
;
1943 ssize_t (*show
)(mdk_rdev_t
*, char *);
1944 ssize_t (*store
)(mdk_rdev_t
*, const char *, size_t);
1948 state_show(mdk_rdev_t
*rdev
, char *page
)
1953 if (test_bit(Faulty
, &rdev
->flags
)) {
1954 len
+= sprintf(page
+len
, "%sfaulty",sep
);
1957 if (test_bit(In_sync
, &rdev
->flags
)) {
1958 len
+= sprintf(page
+len
, "%sin_sync",sep
);
1961 if (test_bit(WriteMostly
, &rdev
->flags
)) {
1962 len
+= sprintf(page
+len
, "%swrite_mostly",sep
);
1965 if (test_bit(Blocked
, &rdev
->flags
)) {
1966 len
+= sprintf(page
+len
, "%sblocked", sep
);
1969 if (!test_bit(Faulty
, &rdev
->flags
) &&
1970 !test_bit(In_sync
, &rdev
->flags
)) {
1971 len
+= sprintf(page
+len
, "%sspare", sep
);
1974 return len
+sprintf(page
+len
, "\n");
1978 state_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
1981 * faulty - simulates and error
1982 * remove - disconnects the device
1983 * writemostly - sets write_mostly
1984 * -writemostly - clears write_mostly
1985 * blocked - sets the Blocked flag
1986 * -blocked - clears the Blocked flag
1989 if (cmd_match(buf
, "faulty") && rdev
->mddev
->pers
) {
1990 md_error(rdev
->mddev
, rdev
);
1992 } else if (cmd_match(buf
, "remove")) {
1993 if (rdev
->raid_disk
>= 0)
1996 mddev_t
*mddev
= rdev
->mddev
;
1997 kick_rdev_from_array(rdev
);
1999 md_update_sb(mddev
, 1);
2000 md_new_event(mddev
);
2003 } else if (cmd_match(buf
, "writemostly")) {
2004 set_bit(WriteMostly
, &rdev
->flags
);
2006 } else if (cmd_match(buf
, "-writemostly")) {
2007 clear_bit(WriteMostly
, &rdev
->flags
);
2009 } else if (cmd_match(buf
, "blocked")) {
2010 set_bit(Blocked
, &rdev
->flags
);
2012 } else if (cmd_match(buf
, "-blocked")) {
2013 clear_bit(Blocked
, &rdev
->flags
);
2014 wake_up(&rdev
->blocked_wait
);
2015 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2016 md_wakeup_thread(rdev
->mddev
->thread
);
2020 if (!err
&& rdev
->sysfs_state
)
2021 sysfs_notify_dirent(rdev
->sysfs_state
);
2022 return err
? err
: len
;
2024 static struct rdev_sysfs_entry rdev_state
=
2025 __ATTR(state
, S_IRUGO
|S_IWUSR
, state_show
, state_store
);
2028 errors_show(mdk_rdev_t
*rdev
, char *page
)
2030 return sprintf(page
, "%d\n", atomic_read(&rdev
->corrected_errors
));
2034 errors_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2037 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2038 if (*buf
&& (*e
== 0 || *e
== '\n')) {
2039 atomic_set(&rdev
->corrected_errors
, n
);
2044 static struct rdev_sysfs_entry rdev_errors
=
2045 __ATTR(errors
, S_IRUGO
|S_IWUSR
, errors_show
, errors_store
);
2048 slot_show(mdk_rdev_t
*rdev
, char *page
)
2050 if (rdev
->raid_disk
< 0)
2051 return sprintf(page
, "none\n");
2053 return sprintf(page
, "%d\n", rdev
->raid_disk
);
2057 slot_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2062 int slot
= simple_strtoul(buf
, &e
, 10);
2063 if (strncmp(buf
, "none", 4)==0)
2065 else if (e
==buf
|| (*e
&& *e
!= '\n'))
2067 if (rdev
->mddev
->pers
&& slot
== -1) {
2068 /* Setting 'slot' on an active array requires also
2069 * updating the 'rd%d' link, and communicating
2070 * with the personality with ->hot_*_disk.
2071 * For now we only support removing
2072 * failed/spare devices. This normally happens automatically,
2073 * but not when the metadata is externally managed.
2075 if (rdev
->raid_disk
== -1)
2077 /* personality does all needed checks */
2078 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
2080 err
= rdev
->mddev
->pers
->
2081 hot_remove_disk(rdev
->mddev
, rdev
->raid_disk
);
2084 sprintf(nm
, "rd%d", rdev
->raid_disk
);
2085 sysfs_remove_link(&rdev
->mddev
->kobj
, nm
);
2086 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2087 md_wakeup_thread(rdev
->mddev
->thread
);
2088 } else if (rdev
->mddev
->pers
) {
2090 /* Activating a spare .. or possibly reactivating
2091 * if we every get bitmaps working here.
2094 if (rdev
->raid_disk
!= -1)
2097 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
2100 list_for_each_entry(rdev2
, &rdev
->mddev
->disks
, same_set
)
2101 if (rdev2
->raid_disk
== slot
)
2104 rdev
->raid_disk
= slot
;
2105 if (test_bit(In_sync
, &rdev
->flags
))
2106 rdev
->saved_raid_disk
= slot
;
2108 rdev
->saved_raid_disk
= -1;
2109 err
= rdev
->mddev
->pers
->
2110 hot_add_disk(rdev
->mddev
, rdev
);
2112 rdev
->raid_disk
= -1;
2115 sysfs_notify_dirent(rdev
->sysfs_state
);
2116 sprintf(nm
, "rd%d", rdev
->raid_disk
);
2117 if (sysfs_create_link(&rdev
->mddev
->kobj
, &rdev
->kobj
, nm
))
2119 "md: cannot register "
2121 nm
, mdname(rdev
->mddev
));
2123 /* don't wakeup anyone, leave that to userspace. */
2125 if (slot
>= rdev
->mddev
->raid_disks
)
2127 rdev
->raid_disk
= slot
;
2128 /* assume it is working */
2129 clear_bit(Faulty
, &rdev
->flags
);
2130 clear_bit(WriteMostly
, &rdev
->flags
);
2131 set_bit(In_sync
, &rdev
->flags
);
2132 sysfs_notify_dirent(rdev
->sysfs_state
);
2138 static struct rdev_sysfs_entry rdev_slot
=
2139 __ATTR(slot
, S_IRUGO
|S_IWUSR
, slot_show
, slot_store
);
2142 offset_show(mdk_rdev_t
*rdev
, char *page
)
2144 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->data_offset
);
2148 offset_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2151 unsigned long long offset
= simple_strtoull(buf
, &e
, 10);
2152 if (e
==buf
|| (*e
&& *e
!= '\n'))
2154 if (rdev
->mddev
->pers
&& rdev
->raid_disk
>= 0)
2156 if (rdev
->size
&& rdev
->mddev
->external
)
2157 /* Must set offset before size, so overlap checks
2160 rdev
->data_offset
= offset
;
2164 static struct rdev_sysfs_entry rdev_offset
=
2165 __ATTR(offset
, S_IRUGO
|S_IWUSR
, offset_show
, offset_store
);
2168 rdev_size_show(mdk_rdev_t
*rdev
, char *page
)
2170 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->size
);
2173 static int overlaps(sector_t s1
, sector_t l1
, sector_t s2
, sector_t l2
)
2175 /* check if two start/length pairs overlap */
2184 rdev_size_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2186 unsigned long long size
;
2187 unsigned long long oldsize
= rdev
->size
;
2188 mddev_t
*my_mddev
= rdev
->mddev
;
2190 if (strict_strtoull(buf
, 10, &size
) < 0)
2192 if (my_mddev
->pers
&& rdev
->raid_disk
>= 0) {
2193 if (my_mddev
->persistent
) {
2194 size
= super_types
[my_mddev
->major_version
].
2195 rdev_size_change(rdev
, size
* 2);
2199 size
= (rdev
->bdev
->bd_inode
->i_size
>> 10);
2200 size
-= rdev
->data_offset
/2;
2203 if (size
< my_mddev
->size
)
2204 return -EINVAL
; /* component must fit device */
2207 if (size
> oldsize
&& my_mddev
->external
) {
2208 /* need to check that all other rdevs with the same ->bdev
2209 * do not overlap. We need to unlock the mddev to avoid
2210 * a deadlock. We have already changed rdev->size, and if
2211 * we have to change it back, we will have the lock again.
2215 struct list_head
*tmp
;
2217 mddev_unlock(my_mddev
);
2218 for_each_mddev(mddev
, tmp
) {
2222 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
)
2223 if (test_bit(AllReserved
, &rdev2
->flags
) ||
2224 (rdev
->bdev
== rdev2
->bdev
&&
2226 overlaps(rdev
->data_offset
, rdev
->size
* 2,
2228 rdev2
->size
* 2))) {
2232 mddev_unlock(mddev
);
2238 mddev_lock(my_mddev
);
2240 /* Someone else could have slipped in a size
2241 * change here, but doing so is just silly.
2242 * We put oldsize back because we *know* it is
2243 * safe, and trust userspace not to race with
2246 rdev
->size
= oldsize
;
2253 static struct rdev_sysfs_entry rdev_size
=
2254 __ATTR(size
, S_IRUGO
|S_IWUSR
, rdev_size_show
, rdev_size_store
);
2256 static struct attribute
*rdev_default_attrs
[] = {
2265 rdev_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
2267 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
2268 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
2269 mddev_t
*mddev
= rdev
->mddev
;
2275 rv
= mddev
? mddev_lock(mddev
) : -EBUSY
;
2277 if (rdev
->mddev
== NULL
)
2280 rv
= entry
->show(rdev
, page
);
2281 mddev_unlock(mddev
);
2287 rdev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
2288 const char *page
, size_t length
)
2290 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
2291 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
2293 mddev_t
*mddev
= rdev
->mddev
;
2297 if (!capable(CAP_SYS_ADMIN
))
2299 rv
= mddev
? mddev_lock(mddev
): -EBUSY
;
2301 if (rdev
->mddev
== NULL
)
2304 rv
= entry
->store(rdev
, page
, length
);
2305 mddev_unlock(mddev
);
2310 static void rdev_free(struct kobject
*ko
)
2312 mdk_rdev_t
*rdev
= container_of(ko
, mdk_rdev_t
, kobj
);
2315 static struct sysfs_ops rdev_sysfs_ops
= {
2316 .show
= rdev_attr_show
,
2317 .store
= rdev_attr_store
,
2319 static struct kobj_type rdev_ktype
= {
2320 .release
= rdev_free
,
2321 .sysfs_ops
= &rdev_sysfs_ops
,
2322 .default_attrs
= rdev_default_attrs
,
2326 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2328 * mark the device faulty if:
2330 * - the device is nonexistent (zero size)
2331 * - the device has no valid superblock
2333 * a faulty rdev _never_ has rdev->sb set.
2335 static mdk_rdev_t
*md_import_device(dev_t newdev
, int super_format
, int super_minor
)
2337 char b
[BDEVNAME_SIZE
];
2342 rdev
= kzalloc(sizeof(*rdev
), GFP_KERNEL
);
2344 printk(KERN_ERR
"md: could not alloc mem for new device!\n");
2345 return ERR_PTR(-ENOMEM
);
2348 if ((err
= alloc_disk_sb(rdev
)))
2351 err
= lock_rdev(rdev
, newdev
, super_format
== -2);
2355 kobject_init(&rdev
->kobj
, &rdev_ktype
);
2358 rdev
->saved_raid_disk
= -1;
2359 rdev
->raid_disk
= -1;
2361 rdev
->data_offset
= 0;
2362 rdev
->sb_events
= 0;
2363 atomic_set(&rdev
->nr_pending
, 0);
2364 atomic_set(&rdev
->read_errors
, 0);
2365 atomic_set(&rdev
->corrected_errors
, 0);
2367 size
= rdev
->bdev
->bd_inode
->i_size
>> BLOCK_SIZE_BITS
;
2370 "md: %s has zero or unknown size, marking faulty!\n",
2371 bdevname(rdev
->bdev
,b
));
2376 if (super_format
>= 0) {
2377 err
= super_types
[super_format
].
2378 load_super(rdev
, NULL
, super_minor
);
2379 if (err
== -EINVAL
) {
2381 "md: %s does not have a valid v%d.%d "
2382 "superblock, not importing!\n",
2383 bdevname(rdev
->bdev
,b
),
2384 super_format
, super_minor
);
2389 "md: could not read %s's sb, not importing!\n",
2390 bdevname(rdev
->bdev
,b
));
2395 INIT_LIST_HEAD(&rdev
->same_set
);
2396 init_waitqueue_head(&rdev
->blocked_wait
);
2401 if (rdev
->sb_page
) {
2407 return ERR_PTR(err
);
2411 * Check a full RAID array for plausibility
2415 static void analyze_sbs(mddev_t
* mddev
)
2418 mdk_rdev_t
*rdev
, *freshest
, *tmp
;
2419 char b
[BDEVNAME_SIZE
];
2422 rdev_for_each(rdev
, tmp
, mddev
)
2423 switch (super_types
[mddev
->major_version
].
2424 load_super(rdev
, freshest
, mddev
->minor_version
)) {
2432 "md: fatal superblock inconsistency in %s"
2433 " -- removing from array\n",
2434 bdevname(rdev
->bdev
,b
));
2435 kick_rdev_from_array(rdev
);
2439 super_types
[mddev
->major_version
].
2440 validate_super(mddev
, freshest
);
2443 rdev_for_each(rdev
, tmp
, mddev
) {
2444 if (rdev
->desc_nr
>= mddev
->max_disks
||
2445 i
> mddev
->max_disks
) {
2447 "md: %s: %s: only %d devices permitted\n",
2448 mdname(mddev
), bdevname(rdev
->bdev
, b
),
2450 kick_rdev_from_array(rdev
);
2453 if (rdev
!= freshest
)
2454 if (super_types
[mddev
->major_version
].
2455 validate_super(mddev
, rdev
)) {
2456 printk(KERN_WARNING
"md: kicking non-fresh %s"
2458 bdevname(rdev
->bdev
,b
));
2459 kick_rdev_from_array(rdev
);
2462 if (mddev
->level
== LEVEL_MULTIPATH
) {
2463 rdev
->desc_nr
= i
++;
2464 rdev
->raid_disk
= rdev
->desc_nr
;
2465 set_bit(In_sync
, &rdev
->flags
);
2466 } else if (rdev
->raid_disk
>= mddev
->raid_disks
) {
2467 rdev
->raid_disk
= -1;
2468 clear_bit(In_sync
, &rdev
->flags
);
2474 if (mddev
->recovery_cp
!= MaxSector
&&
2476 printk(KERN_ERR
"md: %s: raid array is not clean"
2477 " -- starting background reconstruction\n",
2482 static void md_safemode_timeout(unsigned long data
);
2485 safe_delay_show(mddev_t
*mddev
, char *page
)
2487 int msec
= (mddev
->safemode_delay
*1000)/HZ
;
2488 return sprintf(page
, "%d.%03d\n", msec
/1000, msec
%1000);
2491 safe_delay_store(mddev_t
*mddev
, const char *cbuf
, size_t len
)
2499 /* remove a period, and count digits after it */
2500 if (len
>= sizeof(buf
))
2502 strlcpy(buf
, cbuf
, sizeof(buf
));
2503 for (i
=0; i
<len
; i
++) {
2505 if (isdigit(buf
[i
])) {
2510 } else if (buf
[i
] == '.') {
2515 if (strict_strtoul(buf
, 10, &msec
) < 0)
2517 msec
= (msec
* 1000) / scale
;
2519 mddev
->safemode_delay
= 0;
2521 unsigned long old_delay
= mddev
->safemode_delay
;
2522 mddev
->safemode_delay
= (msec
*HZ
)/1000;
2523 if (mddev
->safemode_delay
== 0)
2524 mddev
->safemode_delay
= 1;
2525 if (mddev
->safemode_delay
< old_delay
)
2526 md_safemode_timeout((unsigned long)mddev
);
2530 static struct md_sysfs_entry md_safe_delay
=
2531 __ATTR(safe_mode_delay
, S_IRUGO
|S_IWUSR
,safe_delay_show
, safe_delay_store
);
2534 level_show(mddev_t
*mddev
, char *page
)
2536 struct mdk_personality
*p
= mddev
->pers
;
2538 return sprintf(page
, "%s\n", p
->name
);
2539 else if (mddev
->clevel
[0])
2540 return sprintf(page
, "%s\n", mddev
->clevel
);
2541 else if (mddev
->level
!= LEVEL_NONE
)
2542 return sprintf(page
, "%d\n", mddev
->level
);
2548 level_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2555 if (len
>= sizeof(mddev
->clevel
))
2557 strncpy(mddev
->clevel
, buf
, len
);
2558 if (mddev
->clevel
[len
-1] == '\n')
2560 mddev
->clevel
[len
] = 0;
2561 mddev
->level
= LEVEL_NONE
;
2565 static struct md_sysfs_entry md_level
=
2566 __ATTR(level
, S_IRUGO
|S_IWUSR
, level_show
, level_store
);
2570 layout_show(mddev_t
*mddev
, char *page
)
2572 /* just a number, not meaningful for all levels */
2573 if (mddev
->reshape_position
!= MaxSector
&&
2574 mddev
->layout
!= mddev
->new_layout
)
2575 return sprintf(page
, "%d (%d)\n",
2576 mddev
->new_layout
, mddev
->layout
);
2577 return sprintf(page
, "%d\n", mddev
->layout
);
2581 layout_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2584 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2586 if (!*buf
|| (*e
&& *e
!= '\n'))
2591 if (mddev
->reshape_position
!= MaxSector
)
2592 mddev
->new_layout
= n
;
2597 static struct md_sysfs_entry md_layout
=
2598 __ATTR(layout
, S_IRUGO
|S_IWUSR
, layout_show
, layout_store
);
2602 raid_disks_show(mddev_t
*mddev
, char *page
)
2604 if (mddev
->raid_disks
== 0)
2606 if (mddev
->reshape_position
!= MaxSector
&&
2607 mddev
->delta_disks
!= 0)
2608 return sprintf(page
, "%d (%d)\n", mddev
->raid_disks
,
2609 mddev
->raid_disks
- mddev
->delta_disks
);
2610 return sprintf(page
, "%d\n", mddev
->raid_disks
);
2613 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
);
2616 raid_disks_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2620 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2622 if (!*buf
|| (*e
&& *e
!= '\n'))
2626 rv
= update_raid_disks(mddev
, n
);
2627 else if (mddev
->reshape_position
!= MaxSector
) {
2628 int olddisks
= mddev
->raid_disks
- mddev
->delta_disks
;
2629 mddev
->delta_disks
= n
- olddisks
;
2630 mddev
->raid_disks
= n
;
2632 mddev
->raid_disks
= n
;
2633 return rv
? rv
: len
;
2635 static struct md_sysfs_entry md_raid_disks
=
2636 __ATTR(raid_disks
, S_IRUGO
|S_IWUSR
, raid_disks_show
, raid_disks_store
);
2639 chunk_size_show(mddev_t
*mddev
, char *page
)
2641 if (mddev
->reshape_position
!= MaxSector
&&
2642 mddev
->chunk_size
!= mddev
->new_chunk
)
2643 return sprintf(page
, "%d (%d)\n", mddev
->new_chunk
,
2645 return sprintf(page
, "%d\n", mddev
->chunk_size
);
2649 chunk_size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2651 /* can only set chunk_size if array is not yet active */
2653 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2655 if (!*buf
|| (*e
&& *e
!= '\n'))
2660 else if (mddev
->reshape_position
!= MaxSector
)
2661 mddev
->new_chunk
= n
;
2663 mddev
->chunk_size
= n
;
2666 static struct md_sysfs_entry md_chunk_size
=
2667 __ATTR(chunk_size
, S_IRUGO
|S_IWUSR
, chunk_size_show
, chunk_size_store
);
2670 resync_start_show(mddev_t
*mddev
, char *page
)
2672 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->recovery_cp
);
2676 resync_start_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2679 unsigned long long n
= simple_strtoull(buf
, &e
, 10);
2683 if (!*buf
|| (*e
&& *e
!= '\n'))
2686 mddev
->recovery_cp
= n
;
2689 static struct md_sysfs_entry md_resync_start
=
2690 __ATTR(resync_start
, S_IRUGO
|S_IWUSR
, resync_start_show
, resync_start_store
);
2693 * The array state can be:
2696 * No devices, no size, no level
2697 * Equivalent to STOP_ARRAY ioctl
2699 * May have some settings, but array is not active
2700 * all IO results in error
2701 * When written, doesn't tear down array, but just stops it
2702 * suspended (not supported yet)
2703 * All IO requests will block. The array can be reconfigured.
2704 * Writing this, if accepted, will block until array is quiescent
2706 * no resync can happen. no superblocks get written.
2707 * write requests fail
2709 * like readonly, but behaves like 'clean' on a write request.
2711 * clean - no pending writes, but otherwise active.
2712 * When written to inactive array, starts without resync
2713 * If a write request arrives then
2714 * if metadata is known, mark 'dirty' and switch to 'active'.
2715 * if not known, block and switch to write-pending
2716 * If written to an active array that has pending writes, then fails.
2718 * fully active: IO and resync can be happening.
2719 * When written to inactive array, starts with resync
2722 * clean, but writes are blocked waiting for 'active' to be written.
2725 * like active, but no writes have been seen for a while (100msec).
2728 enum array_state
{ clear
, inactive
, suspended
, readonly
, read_auto
, clean
, active
,
2729 write_pending
, active_idle
, bad_word
};
2730 static char *array_states
[] = {
2731 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2732 "write-pending", "active-idle", NULL
};
2734 static int match_word(const char *word
, char **list
)
2737 for (n
=0; list
[n
]; n
++)
2738 if (cmd_match(word
, list
[n
]))
2744 array_state_show(mddev_t
*mddev
, char *page
)
2746 enum array_state st
= inactive
;
2759 else if (test_bit(MD_CHANGE_CLEAN
, &mddev
->flags
))
2761 else if (mddev
->safemode
)
2767 if (list_empty(&mddev
->disks
) &&
2768 mddev
->raid_disks
== 0 &&
2774 return sprintf(page
, "%s\n", array_states
[st
]);
2777 static int do_md_stop(mddev_t
* mddev
, int ro
, int is_open
);
2778 static int do_md_run(mddev_t
* mddev
);
2779 static int restart_array(mddev_t
*mddev
);
2782 array_state_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2785 enum array_state st
= match_word(buf
, array_states
);
2790 /* stopping an active array */
2791 if (atomic_read(&mddev
->openers
) > 0)
2793 err
= do_md_stop(mddev
, 0, 0);
2796 /* stopping an active array */
2798 if (atomic_read(&mddev
->openers
) > 0)
2800 err
= do_md_stop(mddev
, 2, 0);
2802 err
= 0; /* already inactive */
2805 break; /* not supported yet */
2808 err
= do_md_stop(mddev
, 1, 0);
2811 set_disk_ro(mddev
->gendisk
, 1);
2812 err
= do_md_run(mddev
);
2818 err
= do_md_stop(mddev
, 1, 0);
2819 else if (mddev
->ro
== 1)
2820 err
= restart_array(mddev
);
2823 set_disk_ro(mddev
->gendisk
, 0);
2827 err
= do_md_run(mddev
);
2832 restart_array(mddev
);
2833 spin_lock_irq(&mddev
->write_lock
);
2834 if (atomic_read(&mddev
->writes_pending
) == 0) {
2835 if (mddev
->in_sync
== 0) {
2837 if (mddev
->safemode
== 1)
2838 mddev
->safemode
= 0;
2839 if (mddev
->persistent
)
2840 set_bit(MD_CHANGE_CLEAN
,
2846 spin_unlock_irq(&mddev
->write_lock
);
2849 mddev
->recovery_cp
= MaxSector
;
2850 err
= do_md_run(mddev
);
2855 restart_array(mddev
);
2856 if (mddev
->external
)
2857 clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
2858 wake_up(&mddev
->sb_wait
);
2862 set_disk_ro(mddev
->gendisk
, 0);
2863 err
= do_md_run(mddev
);
2868 /* these cannot be set */
2874 sysfs_notify_dirent(mddev
->sysfs_state
);
2878 static struct md_sysfs_entry md_array_state
=
2879 __ATTR(array_state
, S_IRUGO
|S_IWUSR
, array_state_show
, array_state_store
);
2882 null_show(mddev_t
*mddev
, char *page
)
2888 new_dev_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2890 /* buf must be %d:%d\n? giving major and minor numbers */
2891 /* The new device is added to the array.
2892 * If the array has a persistent superblock, we read the
2893 * superblock to initialise info and check validity.
2894 * Otherwise, only checking done is that in bind_rdev_to_array,
2895 * which mainly checks size.
2898 int major
= simple_strtoul(buf
, &e
, 10);
2904 if (!*buf
|| *e
!= ':' || !e
[1] || e
[1] == '\n')
2906 minor
= simple_strtoul(e
+1, &e
, 10);
2907 if (*e
&& *e
!= '\n')
2909 dev
= MKDEV(major
, minor
);
2910 if (major
!= MAJOR(dev
) ||
2911 minor
!= MINOR(dev
))
2915 if (mddev
->persistent
) {
2916 rdev
= md_import_device(dev
, mddev
->major_version
,
2917 mddev
->minor_version
);
2918 if (!IS_ERR(rdev
) && !list_empty(&mddev
->disks
)) {
2919 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
2920 mdk_rdev_t
, same_set
);
2921 err
= super_types
[mddev
->major_version
]
2922 .load_super(rdev
, rdev0
, mddev
->minor_version
);
2926 } else if (mddev
->external
)
2927 rdev
= md_import_device(dev
, -2, -1);
2929 rdev
= md_import_device(dev
, -1, -1);
2932 return PTR_ERR(rdev
);
2933 err
= bind_rdev_to_array(rdev
, mddev
);
2937 return err
? err
: len
;
2940 static struct md_sysfs_entry md_new_device
=
2941 __ATTR(new_dev
, S_IWUSR
, null_show
, new_dev_store
);
2944 bitmap_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2947 unsigned long chunk
, end_chunk
;
2951 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
2953 chunk
= end_chunk
= simple_strtoul(buf
, &end
, 0);
2954 if (buf
== end
) break;
2955 if (*end
== '-') { /* range */
2957 end_chunk
= simple_strtoul(buf
, &end
, 0);
2958 if (buf
== end
) break;
2960 if (*end
&& !isspace(*end
)) break;
2961 bitmap_dirty_bits(mddev
->bitmap
, chunk
, end_chunk
);
2963 while (isspace(*buf
)) buf
++;
2965 bitmap_unplug(mddev
->bitmap
); /* flush the bits to disk */
2970 static struct md_sysfs_entry md_bitmap
=
2971 __ATTR(bitmap_set_bits
, S_IWUSR
, null_show
, bitmap_store
);
2974 size_show(mddev_t
*mddev
, char *page
)
2976 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->size
);
2979 static int update_size(mddev_t
*mddev
, sector_t num_sectors
);
2982 size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2984 /* If array is inactive, we can reduce the component size, but
2985 * not increase it (except from 0).
2986 * If array is active, we can try an on-line resize
2990 unsigned long long size
= simple_strtoull(buf
, &e
, 10);
2991 if (!*buf
|| *buf
== '\n' ||
2996 err
= update_size(mddev
, size
* 2);
2997 md_update_sb(mddev
, 1);
2999 if (mddev
->size
== 0 ||
3005 return err
? err
: len
;
3008 static struct md_sysfs_entry md_size
=
3009 __ATTR(component_size
, S_IRUGO
|S_IWUSR
, size_show
, size_store
);
3014 * 'none' for arrays with no metadata (good luck...)
3015 * 'external' for arrays with externally managed metadata,
3016 * or N.M for internally known formats
3019 metadata_show(mddev_t
*mddev
, char *page
)
3021 if (mddev
->persistent
)
3022 return sprintf(page
, "%d.%d\n",
3023 mddev
->major_version
, mddev
->minor_version
);
3024 else if (mddev
->external
)
3025 return sprintf(page
, "external:%s\n", mddev
->metadata_type
);
3027 return sprintf(page
, "none\n");
3031 metadata_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3035 /* Changing the details of 'external' metadata is
3036 * always permitted. Otherwise there must be
3037 * no devices attached to the array.
3039 if (mddev
->external
&& strncmp(buf
, "external:", 9) == 0)
3041 else if (!list_empty(&mddev
->disks
))
3044 if (cmd_match(buf
, "none")) {
3045 mddev
->persistent
= 0;
3046 mddev
->external
= 0;
3047 mddev
->major_version
= 0;
3048 mddev
->minor_version
= 90;
3051 if (strncmp(buf
, "external:", 9) == 0) {
3052 size_t namelen
= len
-9;
3053 if (namelen
>= sizeof(mddev
->metadata_type
))
3054 namelen
= sizeof(mddev
->metadata_type
)-1;
3055 strncpy(mddev
->metadata_type
, buf
+9, namelen
);
3056 mddev
->metadata_type
[namelen
] = 0;
3057 if (namelen
&& mddev
->metadata_type
[namelen
-1] == '\n')
3058 mddev
->metadata_type
[--namelen
] = 0;
3059 mddev
->persistent
= 0;
3060 mddev
->external
= 1;
3061 mddev
->major_version
= 0;
3062 mddev
->minor_version
= 90;
3065 major
= simple_strtoul(buf
, &e
, 10);
3066 if (e
==buf
|| *e
!= '.')
3069 minor
= simple_strtoul(buf
, &e
, 10);
3070 if (e
==buf
|| (*e
&& *e
!= '\n') )
3072 if (major
>= ARRAY_SIZE(super_types
) || super_types
[major
].name
== NULL
)
3074 mddev
->major_version
= major
;
3075 mddev
->minor_version
= minor
;
3076 mddev
->persistent
= 1;
3077 mddev
->external
= 0;
3081 static struct md_sysfs_entry md_metadata
=
3082 __ATTR(metadata_version
, S_IRUGO
|S_IWUSR
, metadata_show
, metadata_store
);
3085 action_show(mddev_t
*mddev
, char *page
)
3087 char *type
= "idle";
3088 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
3089 (!mddev
->ro
&& test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))) {
3090 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
3092 else if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
3093 if (!test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
3095 else if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
3099 } else if (test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
))
3102 return sprintf(page
, "%s\n", type
);
3106 action_store(mddev_t
*mddev
, const char *page
, size_t len
)
3108 if (!mddev
->pers
|| !mddev
->pers
->sync_request
)
3111 if (cmd_match(page
, "idle")) {
3112 if (mddev
->sync_thread
) {
3113 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
3114 md_unregister_thread(mddev
->sync_thread
);
3115 mddev
->sync_thread
= NULL
;
3116 mddev
->recovery
= 0;
3118 } else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
3119 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
3121 else if (cmd_match(page
, "resync"))
3122 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3123 else if (cmd_match(page
, "recover")) {
3124 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
3125 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3126 } else if (cmd_match(page
, "reshape")) {
3128 if (mddev
->pers
->start_reshape
== NULL
)
3130 err
= mddev
->pers
->start_reshape(mddev
);
3133 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
3135 if (cmd_match(page
, "check"))
3136 set_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
3137 else if (!cmd_match(page
, "repair"))
3139 set_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
3140 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
3142 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3143 md_wakeup_thread(mddev
->thread
);
3144 sysfs_notify_dirent(mddev
->sysfs_action
);
3149 mismatch_cnt_show(mddev_t
*mddev
, char *page
)
3151 return sprintf(page
, "%llu\n",
3152 (unsigned long long) mddev
->resync_mismatches
);
3155 static struct md_sysfs_entry md_scan_mode
=
3156 __ATTR(sync_action
, S_IRUGO
|S_IWUSR
, action_show
, action_store
);
3159 static struct md_sysfs_entry md_mismatches
= __ATTR_RO(mismatch_cnt
);
3162 sync_min_show(mddev_t
*mddev
, char *page
)
3164 return sprintf(page
, "%d (%s)\n", speed_min(mddev
),
3165 mddev
->sync_speed_min
? "local": "system");
3169 sync_min_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3173 if (strncmp(buf
, "system", 6)==0) {
3174 mddev
->sync_speed_min
= 0;
3177 min
= simple_strtoul(buf
, &e
, 10);
3178 if (buf
== e
|| (*e
&& *e
!= '\n') || min
<= 0)
3180 mddev
->sync_speed_min
= min
;
3184 static struct md_sysfs_entry md_sync_min
=
3185 __ATTR(sync_speed_min
, S_IRUGO
|S_IWUSR
, sync_min_show
, sync_min_store
);
3188 sync_max_show(mddev_t
*mddev
, char *page
)
3190 return sprintf(page
, "%d (%s)\n", speed_max(mddev
),
3191 mddev
->sync_speed_max
? "local": "system");
3195 sync_max_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3199 if (strncmp(buf
, "system", 6)==0) {
3200 mddev
->sync_speed_max
= 0;
3203 max
= simple_strtoul(buf
, &e
, 10);
3204 if (buf
== e
|| (*e
&& *e
!= '\n') || max
<= 0)
3206 mddev
->sync_speed_max
= max
;
3210 static struct md_sysfs_entry md_sync_max
=
3211 __ATTR(sync_speed_max
, S_IRUGO
|S_IWUSR
, sync_max_show
, sync_max_store
);
3214 degraded_show(mddev_t
*mddev
, char *page
)
3216 return sprintf(page
, "%d\n", mddev
->degraded
);
3218 static struct md_sysfs_entry md_degraded
= __ATTR_RO(degraded
);
3221 sync_force_parallel_show(mddev_t
*mddev
, char *page
)
3223 return sprintf(page
, "%d\n", mddev
->parallel_resync
);
3227 sync_force_parallel_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3231 if (strict_strtol(buf
, 10, &n
))
3234 if (n
!= 0 && n
!= 1)
3237 mddev
->parallel_resync
= n
;
3239 if (mddev
->sync_thread
)
3240 wake_up(&resync_wait
);
3245 /* force parallel resync, even with shared block devices */
3246 static struct md_sysfs_entry md_sync_force_parallel
=
3247 __ATTR(sync_force_parallel
, S_IRUGO
|S_IWUSR
,
3248 sync_force_parallel_show
, sync_force_parallel_store
);
3251 sync_speed_show(mddev_t
*mddev
, char *page
)
3253 unsigned long resync
, dt
, db
;
3254 resync
= mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
);
3255 dt
= (jiffies
- mddev
->resync_mark
) / HZ
;
3257 db
= resync
- mddev
->resync_mark_cnt
;
3258 return sprintf(page
, "%lu\n", db
/dt
/2); /* K/sec */
3261 static struct md_sysfs_entry md_sync_speed
= __ATTR_RO(sync_speed
);
3264 sync_completed_show(mddev_t
*mddev
, char *page
)
3266 unsigned long max_blocks
, resync
;
3268 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
3269 max_blocks
= mddev
->resync_max_sectors
;
3271 max_blocks
= mddev
->size
<< 1;
3273 resync
= (mddev
->curr_resync
- atomic_read(&mddev
->recovery_active
));
3274 return sprintf(page
, "%lu / %lu\n", resync
, max_blocks
);
3277 static struct md_sysfs_entry md_sync_completed
= __ATTR_RO(sync_completed
);
3280 min_sync_show(mddev_t
*mddev
, char *page
)
3282 return sprintf(page
, "%llu\n",
3283 (unsigned long long)mddev
->resync_min
);
3286 min_sync_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3288 unsigned long long min
;
3289 if (strict_strtoull(buf
, 10, &min
))
3291 if (min
> mddev
->resync_max
)
3293 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3296 /* Must be a multiple of chunk_size */
3297 if (mddev
->chunk_size
) {
3298 if (min
& (sector_t
)((mddev
->chunk_size
>>9)-1))
3301 mddev
->resync_min
= min
;
3306 static struct md_sysfs_entry md_min_sync
=
3307 __ATTR(sync_min
, S_IRUGO
|S_IWUSR
, min_sync_show
, min_sync_store
);
3310 max_sync_show(mddev_t
*mddev
, char *page
)
3312 if (mddev
->resync_max
== MaxSector
)
3313 return sprintf(page
, "max\n");
3315 return sprintf(page
, "%llu\n",
3316 (unsigned long long)mddev
->resync_max
);
3319 max_sync_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3321 if (strncmp(buf
, "max", 3) == 0)
3322 mddev
->resync_max
= MaxSector
;
3324 unsigned long long max
;
3325 if (strict_strtoull(buf
, 10, &max
))
3327 if (max
< mddev
->resync_min
)
3329 if (max
< mddev
->resync_max
&&
3330 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3333 /* Must be a multiple of chunk_size */
3334 if (mddev
->chunk_size
) {
3335 if (max
& (sector_t
)((mddev
->chunk_size
>>9)-1))
3338 mddev
->resync_max
= max
;
3340 wake_up(&mddev
->recovery_wait
);
3344 static struct md_sysfs_entry md_max_sync
=
3345 __ATTR(sync_max
, S_IRUGO
|S_IWUSR
, max_sync_show
, max_sync_store
);
3348 suspend_lo_show(mddev_t
*mddev
, char *page
)
3350 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_lo
);
3354 suspend_lo_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3357 unsigned long long new = simple_strtoull(buf
, &e
, 10);
3359 if (mddev
->pers
->quiesce
== NULL
)
3361 if (buf
== e
|| (*e
&& *e
!= '\n'))
3363 if (new >= mddev
->suspend_hi
||
3364 (new > mddev
->suspend_lo
&& new < mddev
->suspend_hi
)) {
3365 mddev
->suspend_lo
= new;
3366 mddev
->pers
->quiesce(mddev
, 2);
3371 static struct md_sysfs_entry md_suspend_lo
=
3372 __ATTR(suspend_lo
, S_IRUGO
|S_IWUSR
, suspend_lo_show
, suspend_lo_store
);
3376 suspend_hi_show(mddev_t
*mddev
, char *page
)
3378 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_hi
);
3382 suspend_hi_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3385 unsigned long long new = simple_strtoull(buf
, &e
, 10);
3387 if (mddev
->pers
->quiesce
== NULL
)
3389 if (buf
== e
|| (*e
&& *e
!= '\n'))
3391 if ((new <= mddev
->suspend_lo
&& mddev
->suspend_lo
>= mddev
->suspend_hi
) ||
3392 (new > mddev
->suspend_lo
&& new > mddev
->suspend_hi
)) {
3393 mddev
->suspend_hi
= new;
3394 mddev
->pers
->quiesce(mddev
, 1);
3395 mddev
->pers
->quiesce(mddev
, 0);
3400 static struct md_sysfs_entry md_suspend_hi
=
3401 __ATTR(suspend_hi
, S_IRUGO
|S_IWUSR
, suspend_hi_show
, suspend_hi_store
);
3404 reshape_position_show(mddev_t
*mddev
, char *page
)
3406 if (mddev
->reshape_position
!= MaxSector
)
3407 return sprintf(page
, "%llu\n",
3408 (unsigned long long)mddev
->reshape_position
);
3409 strcpy(page
, "none\n");
3414 reshape_position_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3417 unsigned long long new = simple_strtoull(buf
, &e
, 10);
3420 if (buf
== e
|| (*e
&& *e
!= '\n'))
3422 mddev
->reshape_position
= new;
3423 mddev
->delta_disks
= 0;
3424 mddev
->new_level
= mddev
->level
;
3425 mddev
->new_layout
= mddev
->layout
;
3426 mddev
->new_chunk
= mddev
->chunk_size
;
3430 static struct md_sysfs_entry md_reshape_position
=
3431 __ATTR(reshape_position
, S_IRUGO
|S_IWUSR
, reshape_position_show
,
3432 reshape_position_store
);
3435 static struct attribute
*md_default_attrs
[] = {
3438 &md_raid_disks
.attr
,
3439 &md_chunk_size
.attr
,
3441 &md_resync_start
.attr
,
3443 &md_new_device
.attr
,
3444 &md_safe_delay
.attr
,
3445 &md_array_state
.attr
,
3446 &md_reshape_position
.attr
,
3450 static struct attribute
*md_redundancy_attrs
[] = {
3452 &md_mismatches
.attr
,
3455 &md_sync_speed
.attr
,
3456 &md_sync_force_parallel
.attr
,
3457 &md_sync_completed
.attr
,
3460 &md_suspend_lo
.attr
,
3461 &md_suspend_hi
.attr
,
3466 static struct attribute_group md_redundancy_group
= {
3468 .attrs
= md_redundancy_attrs
,
3473 md_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
3475 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
3476 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
3481 rv
= mddev_lock(mddev
);
3483 rv
= entry
->show(mddev
, page
);
3484 mddev_unlock(mddev
);
3490 md_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
3491 const char *page
, size_t length
)
3493 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
3494 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
3499 if (!capable(CAP_SYS_ADMIN
))
3501 rv
= mddev_lock(mddev
);
3502 if (mddev
->hold_active
== UNTIL_IOCTL
)
3503 mddev
->hold_active
= 0;
3505 rv
= entry
->store(mddev
, page
, length
);
3506 mddev_unlock(mddev
);
3511 static void md_free(struct kobject
*ko
)
3513 mddev_t
*mddev
= container_of(ko
, mddev_t
, kobj
);
3515 if (mddev
->sysfs_state
)
3516 sysfs_put(mddev
->sysfs_state
);
3518 if (mddev
->gendisk
) {
3519 del_gendisk(mddev
->gendisk
);
3520 put_disk(mddev
->gendisk
);
3523 blk_cleanup_queue(mddev
->queue
);
3528 static struct sysfs_ops md_sysfs_ops
= {
3529 .show
= md_attr_show
,
3530 .store
= md_attr_store
,
3532 static struct kobj_type md_ktype
= {
3534 .sysfs_ops
= &md_sysfs_ops
,
3535 .default_attrs
= md_default_attrs
,
3540 static void mddev_delayed_delete(struct work_struct
*ws
)
3542 mddev_t
*mddev
= container_of(ws
, mddev_t
, del_work
);
3544 if (mddev
->private == &md_redundancy_group
) {
3545 sysfs_remove_group(&mddev
->kobj
, &md_redundancy_group
);
3546 if (mddev
->sysfs_action
)
3547 sysfs_put(mddev
->sysfs_action
);
3548 mddev
->sysfs_action
= NULL
;
3549 mddev
->private = NULL
;
3551 kobject_del(&mddev
->kobj
);
3552 kobject_put(&mddev
->kobj
);
3555 static int md_alloc(dev_t dev
, char *name
)
3557 static DEFINE_MUTEX(disks_mutex
);
3558 mddev_t
*mddev
= mddev_find(dev
);
3559 struct gendisk
*disk
;
3568 partitioned
= (MAJOR(mddev
->unit
) != MD_MAJOR
);
3569 shift
= partitioned
? MdpMinorShift
: 0;
3570 unit
= MINOR(mddev
->unit
) >> shift
;
3572 /* wait for any previous instance if this device
3573 * to be completed removed (mddev_delayed_delete).
3575 flush_scheduled_work();
3577 mutex_lock(&disks_mutex
);
3578 if (mddev
->gendisk
) {
3579 mutex_unlock(&disks_mutex
);
3585 /* Need to ensure that 'name' is not a duplicate.
3588 spin_lock(&all_mddevs_lock
);
3590 list_for_each_entry(mddev2
, &all_mddevs
, all_mddevs
)
3591 if (mddev2
->gendisk
&&
3592 strcmp(mddev2
->gendisk
->disk_name
, name
) == 0) {
3593 spin_unlock(&all_mddevs_lock
);
3596 spin_unlock(&all_mddevs_lock
);
3599 mddev
->queue
= blk_alloc_queue(GFP_KERNEL
);
3600 if (!mddev
->queue
) {
3601 mutex_unlock(&disks_mutex
);
3605 /* Can be unlocked because the queue is new: no concurrency */
3606 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER
, mddev
->queue
);
3608 blk_queue_make_request(mddev
->queue
, md_fail_request
);
3610 disk
= alloc_disk(1 << shift
);
3612 mutex_unlock(&disks_mutex
);
3613 blk_cleanup_queue(mddev
->queue
);
3614 mddev
->queue
= NULL
;
3618 disk
->major
= MAJOR(mddev
->unit
);
3619 disk
->first_minor
= unit
<< shift
;
3621 strcpy(disk
->disk_name
, name
);
3622 else if (partitioned
)
3623 sprintf(disk
->disk_name
, "md_d%d", unit
);
3625 sprintf(disk
->disk_name
, "md%d", unit
);
3626 disk
->fops
= &md_fops
;
3627 disk
->private_data
= mddev
;
3628 disk
->queue
= mddev
->queue
;
3629 /* Allow extended partitions. This makes the
3630 * 'mdp' device redundant, but we can't really
3633 disk
->flags
|= GENHD_FL_EXT_DEVT
;
3635 mddev
->gendisk
= disk
;
3636 error
= kobject_init_and_add(&mddev
->kobj
, &md_ktype
,
3637 &disk_to_dev(disk
)->kobj
, "%s", "md");
3638 mutex_unlock(&disks_mutex
);
3640 printk(KERN_WARNING
"md: cannot register %s/md - name in use\n",
3643 kobject_uevent(&mddev
->kobj
, KOBJ_ADD
);
3644 mddev
->sysfs_state
= sysfs_get_dirent(mddev
->kobj
.sd
, "array_state");
3650 static struct kobject
*md_probe(dev_t dev
, int *part
, void *data
)
3652 md_alloc(dev
, NULL
);
3656 static int add_named_array(const char *val
, struct kernel_param
*kp
)
3658 /* val must be "md_*" where * is not all digits.
3659 * We allocate an array with a large free minor number, and
3660 * set the name to val. val must not already be an active name.
3662 int len
= strlen(val
);
3663 char buf
[DISK_NAME_LEN
];
3665 while (len
&& val
[len
-1] == '\n')
3667 if (len
>= DISK_NAME_LEN
)
3669 strlcpy(buf
, val
, len
+1);
3670 if (strncmp(buf
, "md_", 3) != 0)
3672 return md_alloc(0, buf
);
3675 static void md_safemode_timeout(unsigned long data
)
3677 mddev_t
*mddev
= (mddev_t
*) data
;
3679 if (!atomic_read(&mddev
->writes_pending
)) {
3680 mddev
->safemode
= 1;
3681 if (mddev
->external
)
3682 sysfs_notify_dirent(mddev
->sysfs_state
);
3684 md_wakeup_thread(mddev
->thread
);
3687 static int start_dirty_degraded
;
3689 static int do_md_run(mddev_t
* mddev
)
3694 struct gendisk
*disk
;
3695 struct mdk_personality
*pers
;
3696 char b
[BDEVNAME_SIZE
];
3698 if (list_empty(&mddev
->disks
))
3699 /* cannot run an array with no devices.. */
3706 * Analyze all RAID superblock(s)
3708 if (!mddev
->raid_disks
) {
3709 if (!mddev
->persistent
)
3714 chunk_size
= mddev
->chunk_size
;
3717 if (chunk_size
> MAX_CHUNK_SIZE
) {
3718 printk(KERN_ERR
"too big chunk_size: %d > %d\n",
3719 chunk_size
, MAX_CHUNK_SIZE
);
3723 * chunk-size has to be a power of 2
3725 if ( (1 << ffz(~chunk_size
)) != chunk_size
) {
3726 printk(KERN_ERR
"chunk_size of %d not valid\n", chunk_size
);
3730 /* devices must have minimum size of one chunk */
3731 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
3732 if (test_bit(Faulty
, &rdev
->flags
))
3734 if (rdev
->size
< chunk_size
/ 1024) {
3736 "md: Dev %s smaller than chunk_size:"
3738 bdevname(rdev
->bdev
,b
),
3739 (unsigned long long)rdev
->size
,
3746 if (mddev
->level
!= LEVEL_NONE
)
3747 request_module("md-level-%d", mddev
->level
);
3748 else if (mddev
->clevel
[0])
3749 request_module("md-%s", mddev
->clevel
);
3752 * Drop all container device buffers, from now on
3753 * the only valid external interface is through the md
3756 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
3757 if (test_bit(Faulty
, &rdev
->flags
))
3759 sync_blockdev(rdev
->bdev
);
3760 invalidate_bdev(rdev
->bdev
);
3762 /* perform some consistency tests on the device.
3763 * We don't want the data to overlap the metadata,
3764 * Internal Bitmap issues has handled elsewhere.
3766 if (rdev
->data_offset
< rdev
->sb_start
) {
3768 rdev
->data_offset
+ mddev
->size
*2
3770 printk("md: %s: data overlaps metadata\n",
3775 if (rdev
->sb_start
+ rdev
->sb_size
/512
3776 > rdev
->data_offset
) {
3777 printk("md: %s: metadata overlaps data\n",
3782 sysfs_notify_dirent(rdev
->sysfs_state
);
3785 md_probe(mddev
->unit
, NULL
, NULL
);
3786 disk
= mddev
->gendisk
;
3790 spin_lock(&pers_lock
);
3791 pers
= find_pers(mddev
->level
, mddev
->clevel
);
3792 if (!pers
|| !try_module_get(pers
->owner
)) {
3793 spin_unlock(&pers_lock
);
3794 if (mddev
->level
!= LEVEL_NONE
)
3795 printk(KERN_WARNING
"md: personality for level %d is not loaded!\n",
3798 printk(KERN_WARNING
"md: personality for level %s is not loaded!\n",
3803 spin_unlock(&pers_lock
);
3804 mddev
->level
= pers
->level
;
3805 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
3807 if (mddev
->reshape_position
!= MaxSector
&&
3808 pers
->start_reshape
== NULL
) {
3809 /* This personality cannot handle reshaping... */
3811 module_put(pers
->owner
);
3815 if (pers
->sync_request
) {
3816 /* Warn if this is a potentially silly
3819 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
3823 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
3824 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
3826 rdev
->bdev
->bd_contains
==
3827 rdev2
->bdev
->bd_contains
) {
3829 "%s: WARNING: %s appears to be"
3830 " on the same physical disk as"
3833 bdevname(rdev
->bdev
,b
),
3834 bdevname(rdev2
->bdev
,b2
));
3841 "True protection against single-disk"
3842 " failure might be compromised.\n");
3845 mddev
->recovery
= 0;
3846 mddev
->resync_max_sectors
= mddev
->size
<< 1; /* may be over-ridden by personality */
3847 mddev
->barriers_work
= 1;
3848 mddev
->ok_start_degraded
= start_dirty_degraded
;
3851 mddev
->ro
= 2; /* read-only, but switch on first write */
3853 err
= mddev
->pers
->run(mddev
);
3855 printk(KERN_ERR
"md: pers->run() failed ...\n");
3856 else if (mddev
->pers
->sync_request
) {
3857 err
= bitmap_create(mddev
);
3859 printk(KERN_ERR
"%s: failed to create bitmap (%d)\n",
3860 mdname(mddev
), err
);
3861 mddev
->pers
->stop(mddev
);
3865 module_put(mddev
->pers
->owner
);
3867 bitmap_destroy(mddev
);
3870 if (mddev
->pers
->sync_request
) {
3871 if (sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
3873 "md: cannot register extra attributes for %s\n",
3875 mddev
->sysfs_action
= sysfs_get_dirent(mddev
->kobj
.sd
, "sync_action");
3876 } else if (mddev
->ro
== 2) /* auto-readonly not meaningful */
3879 atomic_set(&mddev
->writes_pending
,0);
3880 mddev
->safemode
= 0;
3881 mddev
->safemode_timer
.function
= md_safemode_timeout
;
3882 mddev
->safemode_timer
.data
= (unsigned long) mddev
;
3883 mddev
->safemode_delay
= (200 * HZ
)/1000 +1; /* 200 msec delay */
3886 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
3887 if (rdev
->raid_disk
>= 0) {
3889 sprintf(nm
, "rd%d", rdev
->raid_disk
);
3890 if (sysfs_create_link(&mddev
->kobj
, &rdev
->kobj
, nm
))
3891 printk("md: cannot register %s for %s\n",
3895 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3898 md_update_sb(mddev
, 0);
3900 set_capacity(disk
, mddev
->array_sectors
);
3902 /* If we call blk_queue_make_request here, it will
3903 * re-initialise max_sectors etc which may have been
3904 * refined inside -> run. So just set the bits we need to set.
3905 * Most initialisation happended when we called
3906 * blk_queue_make_request(..., md_fail_request)
3909 mddev
->queue
->queuedata
= mddev
;
3910 mddev
->queue
->make_request_fn
= mddev
->pers
->make_request
;
3912 /* If there is a partially-recovered drive we need to
3913 * start recovery here. If we leave it to md_check_recovery,
3914 * it will remove the drives and not do the right thing
3916 if (mddev
->degraded
&& !mddev
->sync_thread
) {
3918 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
3919 if (rdev
->raid_disk
>= 0 &&
3920 !test_bit(In_sync
, &rdev
->flags
) &&
3921 !test_bit(Faulty
, &rdev
->flags
))
3922 /* complete an interrupted recovery */
3924 if (spares
&& mddev
->pers
->sync_request
) {
3925 mddev
->recovery
= 0;
3926 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
3927 mddev
->sync_thread
= md_register_thread(md_do_sync
,
3930 if (!mddev
->sync_thread
) {
3931 printk(KERN_ERR
"%s: could not start resync"
3934 /* leave the spares where they are, it shouldn't hurt */
3935 mddev
->recovery
= 0;
3939 md_wakeup_thread(mddev
->thread
);
3940 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
3943 md_new_event(mddev
);
3944 sysfs_notify_dirent(mddev
->sysfs_state
);
3945 if (mddev
->sysfs_action
)
3946 sysfs_notify_dirent(mddev
->sysfs_action
);
3947 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
3948 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
3952 static int restart_array(mddev_t
*mddev
)
3954 struct gendisk
*disk
= mddev
->gendisk
;
3956 /* Complain if it has no devices */
3957 if (list_empty(&mddev
->disks
))
3963 mddev
->safemode
= 0;
3965 set_disk_ro(disk
, 0);
3966 printk(KERN_INFO
"md: %s switched to read-write mode.\n",
3968 /* Kick recovery or resync if necessary */
3969 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3970 md_wakeup_thread(mddev
->thread
);
3971 md_wakeup_thread(mddev
->sync_thread
);
3972 sysfs_notify_dirent(mddev
->sysfs_state
);
3976 /* similar to deny_write_access, but accounts for our holding a reference
3977 * to the file ourselves */
3978 static int deny_bitmap_write_access(struct file
* file
)
3980 struct inode
*inode
= file
->f_mapping
->host
;
3982 spin_lock(&inode
->i_lock
);
3983 if (atomic_read(&inode
->i_writecount
) > 1) {
3984 spin_unlock(&inode
->i_lock
);
3987 atomic_set(&inode
->i_writecount
, -1);
3988 spin_unlock(&inode
->i_lock
);
3993 static void restore_bitmap_write_access(struct file
*file
)
3995 struct inode
*inode
= file
->f_mapping
->host
;
3997 spin_lock(&inode
->i_lock
);
3998 atomic_set(&inode
->i_writecount
, 1);
3999 spin_unlock(&inode
->i_lock
);
4003 * 0 - completely stop and dis-assemble array
4004 * 1 - switch to readonly
4005 * 2 - stop but do not disassemble array
4007 static int do_md_stop(mddev_t
* mddev
, int mode
, int is_open
)
4010 struct gendisk
*disk
= mddev
->gendisk
;
4012 if (atomic_read(&mddev
->openers
) > is_open
) {
4013 printk("md: %s still in use.\n",mdname(mddev
));
4019 if (mddev
->sync_thread
) {
4020 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4021 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4022 md_unregister_thread(mddev
->sync_thread
);
4023 mddev
->sync_thread
= NULL
;
4026 del_timer_sync(&mddev
->safemode_timer
);
4029 case 1: /* readonly */
4035 case 0: /* disassemble */
4037 bitmap_flush(mddev
);
4038 md_super_wait(mddev
);
4040 set_disk_ro(disk
, 0);
4041 blk_queue_make_request(mddev
->queue
, md_fail_request
);
4042 mddev
->pers
->stop(mddev
);
4043 mddev
->queue
->merge_bvec_fn
= NULL
;
4044 mddev
->queue
->unplug_fn
= NULL
;
4045 mddev
->queue
->backing_dev_info
.congested_fn
= NULL
;
4046 module_put(mddev
->pers
->owner
);
4047 if (mddev
->pers
->sync_request
)
4048 mddev
->private = &md_redundancy_group
;
4050 /* tell userspace to handle 'inactive' */
4051 sysfs_notify_dirent(mddev
->sysfs_state
);
4053 set_capacity(disk
, 0);
4059 if (!mddev
->in_sync
|| mddev
->flags
) {
4060 /* mark array as shutdown cleanly */
4062 md_update_sb(mddev
, 1);
4065 set_disk_ro(disk
, 1);
4066 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4070 * Free resources if final stop
4075 printk(KERN_INFO
"md: %s stopped.\n", mdname(mddev
));
4077 bitmap_destroy(mddev
);
4078 if (mddev
->bitmap_file
) {
4079 restore_bitmap_write_access(mddev
->bitmap_file
);
4080 fput(mddev
->bitmap_file
);
4081 mddev
->bitmap_file
= NULL
;
4083 mddev
->bitmap_offset
= 0;
4085 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4086 if (rdev
->raid_disk
>= 0) {
4088 sprintf(nm
, "rd%d", rdev
->raid_disk
);
4089 sysfs_remove_link(&mddev
->kobj
, nm
);
4092 /* make sure all md_delayed_delete calls have finished */
4093 flush_scheduled_work();
4095 export_array(mddev
);
4097 mddev
->array_sectors
= 0;
4099 mddev
->raid_disks
= 0;
4100 mddev
->recovery_cp
= 0;
4101 mddev
->resync_min
= 0;
4102 mddev
->resync_max
= MaxSector
;
4103 mddev
->reshape_position
= MaxSector
;
4104 mddev
->external
= 0;
4105 mddev
->persistent
= 0;
4106 mddev
->level
= LEVEL_NONE
;
4107 mddev
->clevel
[0] = 0;
4110 mddev
->metadata_type
[0] = 0;
4111 mddev
->chunk_size
= 0;
4112 mddev
->ctime
= mddev
->utime
= 0;
4114 mddev
->max_disks
= 0;
4116 mddev
->delta_disks
= 0;
4117 mddev
->new_level
= LEVEL_NONE
;
4118 mddev
->new_layout
= 0;
4119 mddev
->new_chunk
= 0;
4120 mddev
->curr_resync
= 0;
4121 mddev
->resync_mismatches
= 0;
4122 mddev
->suspend_lo
= mddev
->suspend_hi
= 0;
4123 mddev
->sync_speed_min
= mddev
->sync_speed_max
= 0;
4124 mddev
->recovery
= 0;
4127 mddev
->degraded
= 0;
4128 mddev
->barriers_work
= 0;
4129 mddev
->safemode
= 0;
4130 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
4131 if (mddev
->hold_active
== UNTIL_STOP
)
4132 mddev
->hold_active
= 0;
4134 } else if (mddev
->pers
)
4135 printk(KERN_INFO
"md: %s switched to read-only mode.\n",
4138 md_new_event(mddev
);
4139 sysfs_notify_dirent(mddev
->sysfs_state
);
4145 static void autorun_array(mddev_t
*mddev
)
4150 if (list_empty(&mddev
->disks
))
4153 printk(KERN_INFO
"md: running: ");
4155 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
4156 char b
[BDEVNAME_SIZE
];
4157 printk("<%s>", bdevname(rdev
->bdev
,b
));
4161 err
= do_md_run(mddev
);
4163 printk(KERN_WARNING
"md: do_md_run() returned %d\n", err
);
4164 do_md_stop(mddev
, 0, 0);
4169 * lets try to run arrays based on all disks that have arrived
4170 * until now. (those are in pending_raid_disks)
4172 * the method: pick the first pending disk, collect all disks with
4173 * the same UUID, remove all from the pending list and put them into
4174 * the 'same_array' list. Then order this list based on superblock
4175 * update time (freshest comes first), kick out 'old' disks and
4176 * compare superblocks. If everything's fine then run it.
4178 * If "unit" is allocated, then bump its reference count
4180 static void autorun_devices(int part
)
4182 mdk_rdev_t
*rdev0
, *rdev
, *tmp
;
4184 char b
[BDEVNAME_SIZE
];
4186 printk(KERN_INFO
"md: autorun ...\n");
4187 while (!list_empty(&pending_raid_disks
)) {
4190 LIST_HEAD(candidates
);
4191 rdev0
= list_entry(pending_raid_disks
.next
,
4192 mdk_rdev_t
, same_set
);
4194 printk(KERN_INFO
"md: considering %s ...\n",
4195 bdevname(rdev0
->bdev
,b
));
4196 INIT_LIST_HEAD(&candidates
);
4197 rdev_for_each_list(rdev
, tmp
, &pending_raid_disks
)
4198 if (super_90_load(rdev
, rdev0
, 0) >= 0) {
4199 printk(KERN_INFO
"md: adding %s ...\n",
4200 bdevname(rdev
->bdev
,b
));
4201 list_move(&rdev
->same_set
, &candidates
);
4204 * now we have a set of devices, with all of them having
4205 * mostly sane superblocks. It's time to allocate the
4209 dev
= MKDEV(mdp_major
,
4210 rdev0
->preferred_minor
<< MdpMinorShift
);
4211 unit
= MINOR(dev
) >> MdpMinorShift
;
4213 dev
= MKDEV(MD_MAJOR
, rdev0
->preferred_minor
);
4216 if (rdev0
->preferred_minor
!= unit
) {
4217 printk(KERN_INFO
"md: unit number in %s is bad: %d\n",
4218 bdevname(rdev0
->bdev
, b
), rdev0
->preferred_minor
);
4222 md_probe(dev
, NULL
, NULL
);
4223 mddev
= mddev_find(dev
);
4224 if (!mddev
|| !mddev
->gendisk
) {
4228 "md: cannot allocate memory for md drive.\n");
4231 if (mddev_lock(mddev
))
4232 printk(KERN_WARNING
"md: %s locked, cannot run\n",
4234 else if (mddev
->raid_disks
|| mddev
->major_version
4235 || !list_empty(&mddev
->disks
)) {
4237 "md: %s already running, cannot run %s\n",
4238 mdname(mddev
), bdevname(rdev0
->bdev
,b
));
4239 mddev_unlock(mddev
);
4241 printk(KERN_INFO
"md: created %s\n", mdname(mddev
));
4242 mddev
->persistent
= 1;
4243 rdev_for_each_list(rdev
, tmp
, &candidates
) {
4244 list_del_init(&rdev
->same_set
);
4245 if (bind_rdev_to_array(rdev
, mddev
))
4248 autorun_array(mddev
);
4249 mddev_unlock(mddev
);
4251 /* on success, candidates will be empty, on error
4254 rdev_for_each_list(rdev
, tmp
, &candidates
) {
4255 list_del_init(&rdev
->same_set
);
4260 printk(KERN_INFO
"md: ... autorun DONE.\n");
4262 #endif /* !MODULE */
4264 static int get_version(void __user
* arg
)
4268 ver
.major
= MD_MAJOR_VERSION
;
4269 ver
.minor
= MD_MINOR_VERSION
;
4270 ver
.patchlevel
= MD_PATCHLEVEL_VERSION
;
4272 if (copy_to_user(arg
, &ver
, sizeof(ver
)))
4278 static int get_array_info(mddev_t
* mddev
, void __user
* arg
)
4280 mdu_array_info_t info
;
4281 int nr
,working
,active
,failed
,spare
;
4284 nr
=working
=active
=failed
=spare
=0;
4285 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
4287 if (test_bit(Faulty
, &rdev
->flags
))
4291 if (test_bit(In_sync
, &rdev
->flags
))
4298 info
.major_version
= mddev
->major_version
;
4299 info
.minor_version
= mddev
->minor_version
;
4300 info
.patch_version
= MD_PATCHLEVEL_VERSION
;
4301 info
.ctime
= mddev
->ctime
;
4302 info
.level
= mddev
->level
;
4303 info
.size
= mddev
->size
;
4304 if (info
.size
!= mddev
->size
) /* overflow */
4307 info
.raid_disks
= mddev
->raid_disks
;
4308 info
.md_minor
= mddev
->md_minor
;
4309 info
.not_persistent
= !mddev
->persistent
;
4311 info
.utime
= mddev
->utime
;
4314 info
.state
= (1<<MD_SB_CLEAN
);
4315 if (mddev
->bitmap
&& mddev
->bitmap_offset
)
4316 info
.state
= (1<<MD_SB_BITMAP_PRESENT
);
4317 info
.active_disks
= active
;
4318 info
.working_disks
= working
;
4319 info
.failed_disks
= failed
;
4320 info
.spare_disks
= spare
;
4322 info
.layout
= mddev
->layout
;
4323 info
.chunk_size
= mddev
->chunk_size
;
4325 if (copy_to_user(arg
, &info
, sizeof(info
)))
4331 static int get_bitmap_file(mddev_t
* mddev
, void __user
* arg
)
4333 mdu_bitmap_file_t
*file
= NULL
; /* too big for stack allocation */
4334 char *ptr
, *buf
= NULL
;
4337 if (md_allow_write(mddev
))
4338 file
= kmalloc(sizeof(*file
), GFP_NOIO
);
4340 file
= kmalloc(sizeof(*file
), GFP_KERNEL
);
4345 /* bitmap disabled, zero the first byte and copy out */
4346 if (!mddev
->bitmap
|| !mddev
->bitmap
->file
) {
4347 file
->pathname
[0] = '\0';
4351 buf
= kmalloc(sizeof(file
->pathname
), GFP_KERNEL
);
4355 ptr
= d_path(&mddev
->bitmap
->file
->f_path
, buf
, sizeof(file
->pathname
));
4359 strcpy(file
->pathname
, ptr
);
4363 if (copy_to_user(arg
, file
, sizeof(*file
)))
4371 static int get_disk_info(mddev_t
* mddev
, void __user
* arg
)
4373 mdu_disk_info_t info
;
4376 if (copy_from_user(&info
, arg
, sizeof(info
)))
4379 rdev
= find_rdev_nr(mddev
, info
.number
);
4381 info
.major
= MAJOR(rdev
->bdev
->bd_dev
);
4382 info
.minor
= MINOR(rdev
->bdev
->bd_dev
);
4383 info
.raid_disk
= rdev
->raid_disk
;
4385 if (test_bit(Faulty
, &rdev
->flags
))
4386 info
.state
|= (1<<MD_DISK_FAULTY
);
4387 else if (test_bit(In_sync
, &rdev
->flags
)) {
4388 info
.state
|= (1<<MD_DISK_ACTIVE
);
4389 info
.state
|= (1<<MD_DISK_SYNC
);
4391 if (test_bit(WriteMostly
, &rdev
->flags
))
4392 info
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
4394 info
.major
= info
.minor
= 0;
4395 info
.raid_disk
= -1;
4396 info
.state
= (1<<MD_DISK_REMOVED
);
4399 if (copy_to_user(arg
, &info
, sizeof(info
)))
4405 static int add_new_disk(mddev_t
* mddev
, mdu_disk_info_t
*info
)
4407 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
4409 dev_t dev
= MKDEV(info
->major
,info
->minor
);
4411 if (info
->major
!= MAJOR(dev
) || info
->minor
!= MINOR(dev
))
4414 if (!mddev
->raid_disks
) {
4416 /* expecting a device which has a superblock */
4417 rdev
= md_import_device(dev
, mddev
->major_version
, mddev
->minor_version
);
4420 "md: md_import_device returned %ld\n",
4422 return PTR_ERR(rdev
);
4424 if (!list_empty(&mddev
->disks
)) {
4425 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
4426 mdk_rdev_t
, same_set
);
4427 int err
= super_types
[mddev
->major_version
]
4428 .load_super(rdev
, rdev0
, mddev
->minor_version
);
4431 "md: %s has different UUID to %s\n",
4432 bdevname(rdev
->bdev
,b
),
4433 bdevname(rdev0
->bdev
,b2
));
4438 err
= bind_rdev_to_array(rdev
, mddev
);
4445 * add_new_disk can be used once the array is assembled
4446 * to add "hot spares". They must already have a superblock
4451 if (!mddev
->pers
->hot_add_disk
) {
4453 "%s: personality does not support diskops!\n",
4457 if (mddev
->persistent
)
4458 rdev
= md_import_device(dev
, mddev
->major_version
,
4459 mddev
->minor_version
);
4461 rdev
= md_import_device(dev
, -1, -1);
4464 "md: md_import_device returned %ld\n",
4466 return PTR_ERR(rdev
);
4468 /* set save_raid_disk if appropriate */
4469 if (!mddev
->persistent
) {
4470 if (info
->state
& (1<<MD_DISK_SYNC
) &&
4471 info
->raid_disk
< mddev
->raid_disks
)
4472 rdev
->raid_disk
= info
->raid_disk
;
4474 rdev
->raid_disk
= -1;
4476 super_types
[mddev
->major_version
].
4477 validate_super(mddev
, rdev
);
4478 rdev
->saved_raid_disk
= rdev
->raid_disk
;
4480 clear_bit(In_sync
, &rdev
->flags
); /* just to be sure */
4481 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
4482 set_bit(WriteMostly
, &rdev
->flags
);
4484 rdev
->raid_disk
= -1;
4485 err
= bind_rdev_to_array(rdev
, mddev
);
4486 if (!err
&& !mddev
->pers
->hot_remove_disk
) {
4487 /* If there is hot_add_disk but no hot_remove_disk
4488 * then added disks for geometry changes,
4489 * and should be added immediately.
4491 super_types
[mddev
->major_version
].
4492 validate_super(mddev
, rdev
);
4493 err
= mddev
->pers
->hot_add_disk(mddev
, rdev
);
4495 unbind_rdev_from_array(rdev
);
4500 sysfs_notify_dirent(rdev
->sysfs_state
);
4502 md_update_sb(mddev
, 1);
4503 if (mddev
->degraded
)
4504 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
4505 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4506 md_wakeup_thread(mddev
->thread
);
4510 /* otherwise, add_new_disk is only allowed
4511 * for major_version==0 superblocks
4513 if (mddev
->major_version
!= 0) {
4514 printk(KERN_WARNING
"%s: ADD_NEW_DISK not supported\n",
4519 if (!(info
->state
& (1<<MD_DISK_FAULTY
))) {
4521 rdev
= md_import_device(dev
, -1, 0);
4524 "md: error, md_import_device() returned %ld\n",
4526 return PTR_ERR(rdev
);
4528 rdev
->desc_nr
= info
->number
;
4529 if (info
->raid_disk
< mddev
->raid_disks
)
4530 rdev
->raid_disk
= info
->raid_disk
;
4532 rdev
->raid_disk
= -1;
4534 if (rdev
->raid_disk
< mddev
->raid_disks
)
4535 if (info
->state
& (1<<MD_DISK_SYNC
))
4536 set_bit(In_sync
, &rdev
->flags
);
4538 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
4539 set_bit(WriteMostly
, &rdev
->flags
);
4541 if (!mddev
->persistent
) {
4542 printk(KERN_INFO
"md: nonpersistent superblock ...\n");
4543 rdev
->sb_start
= rdev
->bdev
->bd_inode
->i_size
/ 512;
4545 rdev
->sb_start
= calc_dev_sboffset(rdev
->bdev
);
4546 rdev
->size
= calc_num_sectors(rdev
, mddev
->chunk_size
) / 2;
4548 err
= bind_rdev_to_array(rdev
, mddev
);
4558 static int hot_remove_disk(mddev_t
* mddev
, dev_t dev
)
4560 char b
[BDEVNAME_SIZE
];
4563 rdev
= find_rdev(mddev
, dev
);
4567 if (rdev
->raid_disk
>= 0)
4570 kick_rdev_from_array(rdev
);
4571 md_update_sb(mddev
, 1);
4572 md_new_event(mddev
);
4576 printk(KERN_WARNING
"md: cannot remove active disk %s from %s ...\n",
4577 bdevname(rdev
->bdev
,b
), mdname(mddev
));
4581 static int hot_add_disk(mddev_t
* mddev
, dev_t dev
)
4583 char b
[BDEVNAME_SIZE
];
4590 if (mddev
->major_version
!= 0) {
4591 printk(KERN_WARNING
"%s: HOT_ADD may only be used with"
4592 " version-0 superblocks.\n",
4596 if (!mddev
->pers
->hot_add_disk
) {
4598 "%s: personality does not support diskops!\n",
4603 rdev
= md_import_device(dev
, -1, 0);
4606 "md: error, md_import_device() returned %ld\n",
4611 if (mddev
->persistent
)
4612 rdev
->sb_start
= calc_dev_sboffset(rdev
->bdev
);
4614 rdev
->sb_start
= rdev
->bdev
->bd_inode
->i_size
/ 512;
4616 rdev
->size
= calc_num_sectors(rdev
, mddev
->chunk_size
) / 2;
4618 if (test_bit(Faulty
, &rdev
->flags
)) {
4620 "md: can not hot-add faulty %s disk to %s!\n",
4621 bdevname(rdev
->bdev
,b
), mdname(mddev
));
4625 clear_bit(In_sync
, &rdev
->flags
);
4627 rdev
->saved_raid_disk
= -1;
4628 err
= bind_rdev_to_array(rdev
, mddev
);
4633 * The rest should better be atomic, we can have disk failures
4634 * noticed in interrupt contexts ...
4637 rdev
->raid_disk
= -1;
4639 md_update_sb(mddev
, 1);
4642 * Kick recovery, maybe this spare has to be added to the
4643 * array immediately.
4645 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4646 md_wakeup_thread(mddev
->thread
);
4647 md_new_event(mddev
);
4655 static int set_bitmap_file(mddev_t
*mddev
, int fd
)
4660 if (!mddev
->pers
->quiesce
)
4662 if (mddev
->recovery
|| mddev
->sync_thread
)
4664 /* we should be able to change the bitmap.. */
4670 return -EEXIST
; /* cannot add when bitmap is present */
4671 mddev
->bitmap_file
= fget(fd
);
4673 if (mddev
->bitmap_file
== NULL
) {
4674 printk(KERN_ERR
"%s: error: failed to get bitmap file\n",
4679 err
= deny_bitmap_write_access(mddev
->bitmap_file
);
4681 printk(KERN_ERR
"%s: error: bitmap file is already in use\n",
4683 fput(mddev
->bitmap_file
);
4684 mddev
->bitmap_file
= NULL
;
4687 mddev
->bitmap_offset
= 0; /* file overrides offset */
4688 } else if (mddev
->bitmap
== NULL
)
4689 return -ENOENT
; /* cannot remove what isn't there */
4692 mddev
->pers
->quiesce(mddev
, 1);
4694 err
= bitmap_create(mddev
);
4695 if (fd
< 0 || err
) {
4696 bitmap_destroy(mddev
);
4697 fd
= -1; /* make sure to put the file */
4699 mddev
->pers
->quiesce(mddev
, 0);
4702 if (mddev
->bitmap_file
) {
4703 restore_bitmap_write_access(mddev
->bitmap_file
);
4704 fput(mddev
->bitmap_file
);
4706 mddev
->bitmap_file
= NULL
;
4713 * set_array_info is used two different ways
4714 * The original usage is when creating a new array.
4715 * In this usage, raid_disks is > 0 and it together with
4716 * level, size, not_persistent,layout,chunksize determine the
4717 * shape of the array.
4718 * This will always create an array with a type-0.90.0 superblock.
4719 * The newer usage is when assembling an array.
4720 * In this case raid_disks will be 0, and the major_version field is
4721 * use to determine which style super-blocks are to be found on the devices.
4722 * The minor and patch _version numbers are also kept incase the
4723 * super_block handler wishes to interpret them.
4725 static int set_array_info(mddev_t
* mddev
, mdu_array_info_t
*info
)
4728 if (info
->raid_disks
== 0) {
4729 /* just setting version number for superblock loading */
4730 if (info
->major_version
< 0 ||
4731 info
->major_version
>= ARRAY_SIZE(super_types
) ||
4732 super_types
[info
->major_version
].name
== NULL
) {
4733 /* maybe try to auto-load a module? */
4735 "md: superblock version %d not known\n",
4736 info
->major_version
);
4739 mddev
->major_version
= info
->major_version
;
4740 mddev
->minor_version
= info
->minor_version
;
4741 mddev
->patch_version
= info
->patch_version
;
4742 mddev
->persistent
= !info
->not_persistent
;
4745 mddev
->major_version
= MD_MAJOR_VERSION
;
4746 mddev
->minor_version
= MD_MINOR_VERSION
;
4747 mddev
->patch_version
= MD_PATCHLEVEL_VERSION
;
4748 mddev
->ctime
= get_seconds();
4750 mddev
->level
= info
->level
;
4751 mddev
->clevel
[0] = 0;
4752 mddev
->size
= info
->size
;
4753 mddev
->raid_disks
= info
->raid_disks
;
4754 /* don't set md_minor, it is determined by which /dev/md* was
4757 if (info
->state
& (1<<MD_SB_CLEAN
))
4758 mddev
->recovery_cp
= MaxSector
;
4760 mddev
->recovery_cp
= 0;
4761 mddev
->persistent
= ! info
->not_persistent
;
4762 mddev
->external
= 0;
4764 mddev
->layout
= info
->layout
;
4765 mddev
->chunk_size
= info
->chunk_size
;
4767 mddev
->max_disks
= MD_SB_DISKS
;
4769 if (mddev
->persistent
)
4771 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
4773 mddev
->default_bitmap_offset
= MD_SB_BYTES
>> 9;
4774 mddev
->bitmap_offset
= 0;
4776 mddev
->reshape_position
= MaxSector
;
4779 * Generate a 128 bit UUID
4781 get_random_bytes(mddev
->uuid
, 16);
4783 mddev
->new_level
= mddev
->level
;
4784 mddev
->new_chunk
= mddev
->chunk_size
;
4785 mddev
->new_layout
= mddev
->layout
;
4786 mddev
->delta_disks
= 0;
4791 static int update_size(mddev_t
*mddev
, sector_t num_sectors
)
4795 int fit
= (num_sectors
== 0);
4797 if (mddev
->pers
->resize
== NULL
)
4799 /* The "num_sectors" is the number of sectors of each device that
4800 * is used. This can only make sense for arrays with redundancy.
4801 * linear and raid0 always use whatever space is available. We can only
4802 * consider changing this number if no resync or reconstruction is
4803 * happening, and if the new size is acceptable. It must fit before the
4804 * sb_start or, if that is <data_offset, it must fit before the size
4805 * of each device. If num_sectors is zero, we find the largest size
4809 if (mddev
->sync_thread
)
4812 /* Sorry, cannot grow a bitmap yet, just remove it,
4816 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
4818 avail
= rdev
->size
* 2;
4820 if (fit
&& (num_sectors
== 0 || num_sectors
> avail
))
4821 num_sectors
= avail
;
4822 if (avail
< num_sectors
)
4825 rv
= mddev
->pers
->resize(mddev
, num_sectors
);
4827 struct block_device
*bdev
;
4829 bdev
= bdget_disk(mddev
->gendisk
, 0);
4831 mutex_lock(&bdev
->bd_inode
->i_mutex
);
4832 i_size_write(bdev
->bd_inode
,
4833 (loff_t
)mddev
->array_sectors
<< 9);
4834 mutex_unlock(&bdev
->bd_inode
->i_mutex
);
4841 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
)
4844 /* change the number of raid disks */
4845 if (mddev
->pers
->check_reshape
== NULL
)
4847 if (raid_disks
<= 0 ||
4848 raid_disks
>= mddev
->max_disks
)
4850 if (mddev
->sync_thread
|| mddev
->reshape_position
!= MaxSector
)
4852 mddev
->delta_disks
= raid_disks
- mddev
->raid_disks
;
4854 rv
= mddev
->pers
->check_reshape(mddev
);
4860 * update_array_info is used to change the configuration of an
4862 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
4863 * fields in the info are checked against the array.
4864 * Any differences that cannot be handled will cause an error.
4865 * Normally, only one change can be managed at a time.
4867 static int update_array_info(mddev_t
*mddev
, mdu_array_info_t
*info
)
4873 /* calculate expected state,ignoring low bits */
4874 if (mddev
->bitmap
&& mddev
->bitmap_offset
)
4875 state
|= (1 << MD_SB_BITMAP_PRESENT
);
4877 if (mddev
->major_version
!= info
->major_version
||
4878 mddev
->minor_version
!= info
->minor_version
||
4879 /* mddev->patch_version != info->patch_version || */
4880 mddev
->ctime
!= info
->ctime
||
4881 mddev
->level
!= info
->level
||
4882 /* mddev->layout != info->layout || */
4883 !mddev
->persistent
!= info
->not_persistent
||
4884 mddev
->chunk_size
!= info
->chunk_size
||
4885 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
4886 ((state
^info
->state
) & 0xfffffe00)
4889 /* Check there is only one change */
4890 if (info
->size
>= 0 && mddev
->size
!= info
->size
) cnt
++;
4891 if (mddev
->raid_disks
!= info
->raid_disks
) cnt
++;
4892 if (mddev
->layout
!= info
->layout
) cnt
++;
4893 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) cnt
++;
4894 if (cnt
== 0) return 0;
4895 if (cnt
> 1) return -EINVAL
;
4897 if (mddev
->layout
!= info
->layout
) {
4899 * we don't need to do anything at the md level, the
4900 * personality will take care of it all.
4902 if (mddev
->pers
->reconfig
== NULL
)
4905 return mddev
->pers
->reconfig(mddev
, info
->layout
, -1);
4907 if (info
->size
>= 0 && mddev
->size
!= info
->size
)
4908 rv
= update_size(mddev
, (sector_t
)info
->size
* 2);
4910 if (mddev
->raid_disks
!= info
->raid_disks
)
4911 rv
= update_raid_disks(mddev
, info
->raid_disks
);
4913 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) {
4914 if (mddev
->pers
->quiesce
== NULL
)
4916 if (mddev
->recovery
|| mddev
->sync_thread
)
4918 if (info
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
4919 /* add the bitmap */
4922 if (mddev
->default_bitmap_offset
== 0)
4924 mddev
->bitmap_offset
= mddev
->default_bitmap_offset
;
4925 mddev
->pers
->quiesce(mddev
, 1);
4926 rv
= bitmap_create(mddev
);
4928 bitmap_destroy(mddev
);
4929 mddev
->pers
->quiesce(mddev
, 0);
4931 /* remove the bitmap */
4934 if (mddev
->bitmap
->file
)
4936 mddev
->pers
->quiesce(mddev
, 1);
4937 bitmap_destroy(mddev
);
4938 mddev
->pers
->quiesce(mddev
, 0);
4939 mddev
->bitmap_offset
= 0;
4942 md_update_sb(mddev
, 1);
4946 static int set_disk_faulty(mddev_t
*mddev
, dev_t dev
)
4950 if (mddev
->pers
== NULL
)
4953 rdev
= find_rdev(mddev
, dev
);
4957 md_error(mddev
, rdev
);
4962 * We have a problem here : there is no easy way to give a CHS
4963 * virtual geometry. We currently pretend that we have a 2 heads
4964 * 4 sectors (with a BIG number of cylinders...). This drives
4965 * dosfs just mad... ;-)
4967 static int md_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
4969 mddev_t
*mddev
= bdev
->bd_disk
->private_data
;
4973 geo
->cylinders
= get_capacity(mddev
->gendisk
) / 8;
4977 static int md_ioctl(struct block_device
*bdev
, fmode_t mode
,
4978 unsigned int cmd
, unsigned long arg
)
4981 void __user
*argp
= (void __user
*)arg
;
4982 mddev_t
*mddev
= NULL
;
4984 if (!capable(CAP_SYS_ADMIN
))
4988 * Commands dealing with the RAID driver but not any
4994 err
= get_version(argp
);
4997 case PRINT_RAID_DEBUG
:
5005 autostart_arrays(arg
);
5012 * Commands creating/starting a new array:
5015 mddev
= bdev
->bd_disk
->private_data
;
5022 err
= mddev_lock(mddev
);
5025 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5032 case SET_ARRAY_INFO
:
5034 mdu_array_info_t info
;
5036 memset(&info
, 0, sizeof(info
));
5037 else if (copy_from_user(&info
, argp
, sizeof(info
))) {
5042 err
= update_array_info(mddev
, &info
);
5044 printk(KERN_WARNING
"md: couldn't update"
5045 " array info. %d\n", err
);
5050 if (!list_empty(&mddev
->disks
)) {
5052 "md: array %s already has disks!\n",
5057 if (mddev
->raid_disks
) {
5059 "md: array %s already initialised!\n",
5064 err
= set_array_info(mddev
, &info
);
5066 printk(KERN_WARNING
"md: couldn't set"
5067 " array info. %d\n", err
);
5077 * Commands querying/configuring an existing array:
5079 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
5080 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
5081 if ((!mddev
->raid_disks
&& !mddev
->external
)
5082 && cmd
!= ADD_NEW_DISK
&& cmd
!= STOP_ARRAY
5083 && cmd
!= RUN_ARRAY
&& cmd
!= SET_BITMAP_FILE
5084 && cmd
!= GET_BITMAP_FILE
) {
5090 * Commands even a read-only array can execute:
5094 case GET_ARRAY_INFO
:
5095 err
= get_array_info(mddev
, argp
);
5098 case GET_BITMAP_FILE
:
5099 err
= get_bitmap_file(mddev
, argp
);
5103 err
= get_disk_info(mddev
, argp
);
5106 case RESTART_ARRAY_RW
:
5107 err
= restart_array(mddev
);
5111 err
= do_md_stop(mddev
, 0, 1);
5115 err
= do_md_stop(mddev
, 1, 1);
5121 * The remaining ioctls are changing the state of the
5122 * superblock, so we do not allow them on read-only arrays.
5123 * However non-MD ioctls (e.g. get-size) will still come through
5124 * here and hit the 'default' below, so only disallow
5125 * 'md' ioctls, and switch to rw mode if started auto-readonly.
5127 if (_IOC_TYPE(cmd
) == MD_MAJOR
&& mddev
->ro
&& mddev
->pers
) {
5128 if (mddev
->ro
== 2) {
5130 sysfs_notify_dirent(mddev
->sysfs_state
);
5131 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5132 md_wakeup_thread(mddev
->thread
);
5143 mdu_disk_info_t info
;
5144 if (copy_from_user(&info
, argp
, sizeof(info
)))
5147 err
= add_new_disk(mddev
, &info
);
5151 case HOT_REMOVE_DISK
:
5152 err
= hot_remove_disk(mddev
, new_decode_dev(arg
));
5156 err
= hot_add_disk(mddev
, new_decode_dev(arg
));
5159 case SET_DISK_FAULTY
:
5160 err
= set_disk_faulty(mddev
, new_decode_dev(arg
));
5164 err
= do_md_run(mddev
);
5167 case SET_BITMAP_FILE
:
5168 err
= set_bitmap_file(mddev
, (int)arg
);
5178 if (mddev
->hold_active
== UNTIL_IOCTL
&&
5180 mddev
->hold_active
= 0;
5181 mddev_unlock(mddev
);
5191 static int md_open(struct block_device
*bdev
, fmode_t mode
)
5194 * Succeed if we can lock the mddev, which confirms that
5195 * it isn't being stopped right now.
5197 mddev_t
*mddev
= mddev_find(bdev
->bd_dev
);
5200 if (mddev
->gendisk
!= bdev
->bd_disk
) {
5201 /* we are racing with mddev_put which is discarding this
5205 /* Wait until bdev->bd_disk is definitely gone */
5206 flush_scheduled_work();
5207 /* Then retry the open from the top */
5208 return -ERESTARTSYS
;
5210 BUG_ON(mddev
!= bdev
->bd_disk
->private_data
);
5212 if ((err
= mutex_lock_interruptible_nested(&mddev
->reconfig_mutex
, 1)))
5216 atomic_inc(&mddev
->openers
);
5217 mddev_unlock(mddev
);
5219 check_disk_change(bdev
);
5224 static int md_release(struct gendisk
*disk
, fmode_t mode
)
5226 mddev_t
*mddev
= disk
->private_data
;
5229 atomic_dec(&mddev
->openers
);
5235 static int md_media_changed(struct gendisk
*disk
)
5237 mddev_t
*mddev
= disk
->private_data
;
5239 return mddev
->changed
;
5242 static int md_revalidate(struct gendisk
*disk
)
5244 mddev_t
*mddev
= disk
->private_data
;
5249 static struct block_device_operations md_fops
=
5251 .owner
= THIS_MODULE
,
5253 .release
= md_release
,
5254 .locked_ioctl
= md_ioctl
,
5255 .getgeo
= md_getgeo
,
5256 .media_changed
= md_media_changed
,
5257 .revalidate_disk
= md_revalidate
,
5260 static int md_thread(void * arg
)
5262 mdk_thread_t
*thread
= arg
;
5265 * md_thread is a 'system-thread', it's priority should be very
5266 * high. We avoid resource deadlocks individually in each
5267 * raid personality. (RAID5 does preallocation) We also use RR and
5268 * the very same RT priority as kswapd, thus we will never get
5269 * into a priority inversion deadlock.
5271 * we definitely have to have equal or higher priority than
5272 * bdflush, otherwise bdflush will deadlock if there are too
5273 * many dirty RAID5 blocks.
5276 allow_signal(SIGKILL
);
5277 while (!kthread_should_stop()) {
5279 /* We need to wait INTERRUPTIBLE so that
5280 * we don't add to the load-average.
5281 * That means we need to be sure no signals are
5284 if (signal_pending(current
))
5285 flush_signals(current
);
5287 wait_event_interruptible_timeout
5289 test_bit(THREAD_WAKEUP
, &thread
->flags
)
5290 || kthread_should_stop(),
5293 clear_bit(THREAD_WAKEUP
, &thread
->flags
);
5295 thread
->run(thread
->mddev
);
5301 void md_wakeup_thread(mdk_thread_t
*thread
)
5304 dprintk("md: waking up MD thread %s.\n", thread
->tsk
->comm
);
5305 set_bit(THREAD_WAKEUP
, &thread
->flags
);
5306 wake_up(&thread
->wqueue
);
5310 mdk_thread_t
*md_register_thread(void (*run
) (mddev_t
*), mddev_t
*mddev
,
5313 mdk_thread_t
*thread
;
5315 thread
= kzalloc(sizeof(mdk_thread_t
), GFP_KERNEL
);
5319 init_waitqueue_head(&thread
->wqueue
);
5322 thread
->mddev
= mddev
;
5323 thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
5324 thread
->tsk
= kthread_run(md_thread
, thread
, name
, mdname(thread
->mddev
));
5325 if (IS_ERR(thread
->tsk
)) {
5332 void md_unregister_thread(mdk_thread_t
*thread
)
5334 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread
->tsk
));
5336 kthread_stop(thread
->tsk
);
5340 void md_error(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
5347 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
5350 if (mddev
->external
)
5351 set_bit(Blocked
, &rdev
->flags
);
5353 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5355 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5356 __builtin_return_address(0),__builtin_return_address(1),
5357 __builtin_return_address(2),__builtin_return_address(3));
5361 if (!mddev
->pers
->error_handler
)
5363 mddev
->pers
->error_handler(mddev
,rdev
);
5364 if (mddev
->degraded
)
5365 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
5366 set_bit(StateChanged
, &rdev
->flags
);
5367 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5368 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5369 md_wakeup_thread(mddev
->thread
);
5370 md_new_event_inintr(mddev
);
5373 /* seq_file implementation /proc/mdstat */
5375 static void status_unused(struct seq_file
*seq
)
5380 seq_printf(seq
, "unused devices: ");
5382 list_for_each_entry(rdev
, &pending_raid_disks
, same_set
) {
5383 char b
[BDEVNAME_SIZE
];
5385 seq_printf(seq
, "%s ",
5386 bdevname(rdev
->bdev
,b
));
5389 seq_printf(seq
, "<none>");
5391 seq_printf(seq
, "\n");
5395 static void status_resync(struct seq_file
*seq
, mddev_t
* mddev
)
5397 sector_t max_blocks
, resync
, res
;
5398 unsigned long dt
, db
, rt
;
5400 unsigned int per_milli
;
5402 resync
= (mddev
->curr_resync
- atomic_read(&mddev
->recovery_active
))/2;
5404 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
5405 max_blocks
= mddev
->resync_max_sectors
>> 1;
5407 max_blocks
= mddev
->size
;
5410 * Should not happen.
5416 /* Pick 'scale' such that (resync>>scale)*1000 will fit
5417 * in a sector_t, and (max_blocks>>scale) will fit in a
5418 * u32, as those are the requirements for sector_div.
5419 * Thus 'scale' must be at least 10
5422 if (sizeof(sector_t
) > sizeof(unsigned long)) {
5423 while ( max_blocks
/2 > (1ULL<<(scale
+32)))
5426 res
= (resync
>>scale
)*1000;
5427 sector_div(res
, (u32
)((max_blocks
>>scale
)+1));
5431 int i
, x
= per_milli
/50, y
= 20-x
;
5432 seq_printf(seq
, "[");
5433 for (i
= 0; i
< x
; i
++)
5434 seq_printf(seq
, "=");
5435 seq_printf(seq
, ">");
5436 for (i
= 0; i
< y
; i
++)
5437 seq_printf(seq
, ".");
5438 seq_printf(seq
, "] ");
5440 seq_printf(seq
, " %s =%3u.%u%% (%llu/%llu)",
5441 (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)?
5443 (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)?
5445 (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ?
5446 "resync" : "recovery"))),
5447 per_milli
/10, per_milli
% 10,
5448 (unsigned long long) resync
,
5449 (unsigned long long) max_blocks
);
5452 * We do not want to overflow, so the order of operands and
5453 * the * 100 / 100 trick are important. We do a +1 to be
5454 * safe against division by zero. We only estimate anyway.
5456 * dt: time from mark until now
5457 * db: blocks written from mark until now
5458 * rt: remaining time
5460 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
5462 db
= (mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
))
5463 - mddev
->resync_mark_cnt
;
5464 rt
= (dt
* ((unsigned long)(max_blocks
-resync
) / (db
/2/100+1)))/100;
5466 seq_printf(seq
, " finish=%lu.%lumin", rt
/ 60, (rt
% 60)/6);
5468 seq_printf(seq
, " speed=%ldK/sec", db
/2/dt
);
5471 static void *md_seq_start(struct seq_file
*seq
, loff_t
*pos
)
5473 struct list_head
*tmp
;
5483 spin_lock(&all_mddevs_lock
);
5484 list_for_each(tmp
,&all_mddevs
)
5486 mddev
= list_entry(tmp
, mddev_t
, all_mddevs
);
5488 spin_unlock(&all_mddevs_lock
);
5491 spin_unlock(&all_mddevs_lock
);
5493 return (void*)2;/* tail */
5497 static void *md_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
5499 struct list_head
*tmp
;
5500 mddev_t
*next_mddev
, *mddev
= v
;
5506 spin_lock(&all_mddevs_lock
);
5508 tmp
= all_mddevs
.next
;
5510 tmp
= mddev
->all_mddevs
.next
;
5511 if (tmp
!= &all_mddevs
)
5512 next_mddev
= mddev_get(list_entry(tmp
,mddev_t
,all_mddevs
));
5514 next_mddev
= (void*)2;
5517 spin_unlock(&all_mddevs_lock
);
5525 static void md_seq_stop(struct seq_file
*seq
, void *v
)
5529 if (mddev
&& v
!= (void*)1 && v
!= (void*)2)
5533 struct mdstat_info
{
5537 static int md_seq_show(struct seq_file
*seq
, void *v
)
5542 struct mdstat_info
*mi
= seq
->private;
5543 struct bitmap
*bitmap
;
5545 if (v
== (void*)1) {
5546 struct mdk_personality
*pers
;
5547 seq_printf(seq
, "Personalities : ");
5548 spin_lock(&pers_lock
);
5549 list_for_each_entry(pers
, &pers_list
, list
)
5550 seq_printf(seq
, "[%s] ", pers
->name
);
5552 spin_unlock(&pers_lock
);
5553 seq_printf(seq
, "\n");
5554 mi
->event
= atomic_read(&md_event_count
);
5557 if (v
== (void*)2) {
5562 if (mddev_lock(mddev
) < 0)
5565 if (mddev
->pers
|| mddev
->raid_disks
|| !list_empty(&mddev
->disks
)) {
5566 seq_printf(seq
, "%s : %sactive", mdname(mddev
),
5567 mddev
->pers
? "" : "in");
5570 seq_printf(seq
, " (read-only)");
5572 seq_printf(seq
, " (auto-read-only)");
5573 seq_printf(seq
, " %s", mddev
->pers
->name
);
5577 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
5578 char b
[BDEVNAME_SIZE
];
5579 seq_printf(seq
, " %s[%d]",
5580 bdevname(rdev
->bdev
,b
), rdev
->desc_nr
);
5581 if (test_bit(WriteMostly
, &rdev
->flags
))
5582 seq_printf(seq
, "(W)");
5583 if (test_bit(Faulty
, &rdev
->flags
)) {
5584 seq_printf(seq
, "(F)");
5586 } else if (rdev
->raid_disk
< 0)
5587 seq_printf(seq
, "(S)"); /* spare */
5591 if (!list_empty(&mddev
->disks
)) {
5593 seq_printf(seq
, "\n %llu blocks",
5594 (unsigned long long)
5595 mddev
->array_sectors
/ 2);
5597 seq_printf(seq
, "\n %llu blocks",
5598 (unsigned long long)size
);
5600 if (mddev
->persistent
) {
5601 if (mddev
->major_version
!= 0 ||
5602 mddev
->minor_version
!= 90) {
5603 seq_printf(seq
," super %d.%d",
5604 mddev
->major_version
,
5605 mddev
->minor_version
);
5607 } else if (mddev
->external
)
5608 seq_printf(seq
, " super external:%s",
5609 mddev
->metadata_type
);
5611 seq_printf(seq
, " super non-persistent");
5614 mddev
->pers
->status(seq
, mddev
);
5615 seq_printf(seq
, "\n ");
5616 if (mddev
->pers
->sync_request
) {
5617 if (mddev
->curr_resync
> 2) {
5618 status_resync(seq
, mddev
);
5619 seq_printf(seq
, "\n ");
5620 } else if (mddev
->curr_resync
== 1 || mddev
->curr_resync
== 2)
5621 seq_printf(seq
, "\tresync=DELAYED\n ");
5622 else if (mddev
->recovery_cp
< MaxSector
)
5623 seq_printf(seq
, "\tresync=PENDING\n ");
5626 seq_printf(seq
, "\n ");
5628 if ((bitmap
= mddev
->bitmap
)) {
5629 unsigned long chunk_kb
;
5630 unsigned long flags
;
5631 spin_lock_irqsave(&bitmap
->lock
, flags
);
5632 chunk_kb
= bitmap
->chunksize
>> 10;
5633 seq_printf(seq
, "bitmap: %lu/%lu pages [%luKB], "
5635 bitmap
->pages
- bitmap
->missing_pages
,
5637 (bitmap
->pages
- bitmap
->missing_pages
)
5638 << (PAGE_SHIFT
- 10),
5639 chunk_kb
? chunk_kb
: bitmap
->chunksize
,
5640 chunk_kb
? "KB" : "B");
5642 seq_printf(seq
, ", file: ");
5643 seq_path(seq
, &bitmap
->file
->f_path
, " \t\n");
5646 seq_printf(seq
, "\n");
5647 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
5650 seq_printf(seq
, "\n");
5652 mddev_unlock(mddev
);
5657 static struct seq_operations md_seq_ops
= {
5658 .start
= md_seq_start
,
5659 .next
= md_seq_next
,
5660 .stop
= md_seq_stop
,
5661 .show
= md_seq_show
,
5664 static int md_seq_open(struct inode
*inode
, struct file
*file
)
5667 struct mdstat_info
*mi
= kmalloc(sizeof(*mi
), GFP_KERNEL
);
5671 error
= seq_open(file
, &md_seq_ops
);
5675 struct seq_file
*p
= file
->private_data
;
5677 mi
->event
= atomic_read(&md_event_count
);
5682 static unsigned int mdstat_poll(struct file
*filp
, poll_table
*wait
)
5684 struct seq_file
*m
= filp
->private_data
;
5685 struct mdstat_info
*mi
= m
->private;
5688 poll_wait(filp
, &md_event_waiters
, wait
);
5690 /* always allow read */
5691 mask
= POLLIN
| POLLRDNORM
;
5693 if (mi
->event
!= atomic_read(&md_event_count
))
5694 mask
|= POLLERR
| POLLPRI
;
5698 static const struct file_operations md_seq_fops
= {
5699 .owner
= THIS_MODULE
,
5700 .open
= md_seq_open
,
5702 .llseek
= seq_lseek
,
5703 .release
= seq_release_private
,
5704 .poll
= mdstat_poll
,
5707 int register_md_personality(struct mdk_personality
*p
)
5709 spin_lock(&pers_lock
);
5710 list_add_tail(&p
->list
, &pers_list
);
5711 printk(KERN_INFO
"md: %s personality registered for level %d\n", p
->name
, p
->level
);
5712 spin_unlock(&pers_lock
);
5716 int unregister_md_personality(struct mdk_personality
*p
)
5718 printk(KERN_INFO
"md: %s personality unregistered\n", p
->name
);
5719 spin_lock(&pers_lock
);
5720 list_del_init(&p
->list
);
5721 spin_unlock(&pers_lock
);
5725 static int is_mddev_idle(mddev_t
*mddev
)
5733 rdev_for_each_rcu(rdev
, mddev
) {
5734 struct gendisk
*disk
= rdev
->bdev
->bd_contains
->bd_disk
;
5735 curr_events
= part_stat_read(&disk
->part0
, sectors
[0]) +
5736 part_stat_read(&disk
->part0
, sectors
[1]) -
5737 atomic_read(&disk
->sync_io
);
5738 /* sync IO will cause sync_io to increase before the disk_stats
5739 * as sync_io is counted when a request starts, and
5740 * disk_stats is counted when it completes.
5741 * So resync activity will cause curr_events to be smaller than
5742 * when there was no such activity.
5743 * non-sync IO will cause disk_stat to increase without
5744 * increasing sync_io so curr_events will (eventually)
5745 * be larger than it was before. Once it becomes
5746 * substantially larger, the test below will cause
5747 * the array to appear non-idle, and resync will slow
5749 * If there is a lot of outstanding resync activity when
5750 * we set last_event to curr_events, then all that activity
5751 * completing might cause the array to appear non-idle
5752 * and resync will be slowed down even though there might
5753 * not have been non-resync activity. This will only
5754 * happen once though. 'last_events' will soon reflect
5755 * the state where there is little or no outstanding
5756 * resync requests, and further resync activity will
5757 * always make curr_events less than last_events.
5760 if (curr_events
- rdev
->last_events
> 4096) {
5761 rdev
->last_events
= curr_events
;
5769 void md_done_sync(mddev_t
*mddev
, int blocks
, int ok
)
5771 /* another "blocks" (512byte) blocks have been synced */
5772 atomic_sub(blocks
, &mddev
->recovery_active
);
5773 wake_up(&mddev
->recovery_wait
);
5775 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5776 md_wakeup_thread(mddev
->thread
);
5777 // stop recovery, signal do_sync ....
5782 /* md_write_start(mddev, bi)
5783 * If we need to update some array metadata (e.g. 'active' flag
5784 * in superblock) before writing, schedule a superblock update
5785 * and wait for it to complete.
5787 void md_write_start(mddev_t
*mddev
, struct bio
*bi
)
5790 if (bio_data_dir(bi
) != WRITE
)
5793 BUG_ON(mddev
->ro
== 1);
5794 if (mddev
->ro
== 2) {
5795 /* need to switch to read/write */
5797 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5798 md_wakeup_thread(mddev
->thread
);
5799 md_wakeup_thread(mddev
->sync_thread
);
5802 atomic_inc(&mddev
->writes_pending
);
5803 if (mddev
->safemode
== 1)
5804 mddev
->safemode
= 0;
5805 if (mddev
->in_sync
) {
5806 spin_lock_irq(&mddev
->write_lock
);
5807 if (mddev
->in_sync
) {
5809 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
5810 md_wakeup_thread(mddev
->thread
);
5813 spin_unlock_irq(&mddev
->write_lock
);
5816 sysfs_notify_dirent(mddev
->sysfs_state
);
5817 wait_event(mddev
->sb_wait
,
5818 !test_bit(MD_CHANGE_CLEAN
, &mddev
->flags
) &&
5819 !test_bit(MD_CHANGE_PENDING
, &mddev
->flags
));
5822 void md_write_end(mddev_t
*mddev
)
5824 if (atomic_dec_and_test(&mddev
->writes_pending
)) {
5825 if (mddev
->safemode
== 2)
5826 md_wakeup_thread(mddev
->thread
);
5827 else if (mddev
->safemode_delay
)
5828 mod_timer(&mddev
->safemode_timer
, jiffies
+ mddev
->safemode_delay
);
5832 /* md_allow_write(mddev)
5833 * Calling this ensures that the array is marked 'active' so that writes
5834 * may proceed without blocking. It is important to call this before
5835 * attempting a GFP_KERNEL allocation while holding the mddev lock.
5836 * Must be called with mddev_lock held.
5838 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
5839 * is dropped, so return -EAGAIN after notifying userspace.
5841 int md_allow_write(mddev_t
*mddev
)
5847 if (!mddev
->pers
->sync_request
)
5850 spin_lock_irq(&mddev
->write_lock
);
5851 if (mddev
->in_sync
) {
5853 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
5854 if (mddev
->safemode_delay
&&
5855 mddev
->safemode
== 0)
5856 mddev
->safemode
= 1;
5857 spin_unlock_irq(&mddev
->write_lock
);
5858 md_update_sb(mddev
, 0);
5859 sysfs_notify_dirent(mddev
->sysfs_state
);
5861 spin_unlock_irq(&mddev
->write_lock
);
5863 if (test_bit(MD_CHANGE_CLEAN
, &mddev
->flags
))
5868 EXPORT_SYMBOL_GPL(md_allow_write
);
5870 #define SYNC_MARKS 10
5871 #define SYNC_MARK_STEP (3*HZ)
5872 void md_do_sync(mddev_t
*mddev
)
5875 unsigned int currspeed
= 0,
5877 sector_t max_sectors
,j
, io_sectors
;
5878 unsigned long mark
[SYNC_MARKS
];
5879 sector_t mark_cnt
[SYNC_MARKS
];
5881 struct list_head
*tmp
;
5882 sector_t last_check
;
5887 /* just incase thread restarts... */
5888 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
5890 if (mddev
->ro
) /* never try to sync a read-only array */
5893 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
5894 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
5895 desc
= "data-check";
5896 else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
5897 desc
= "requested-resync";
5900 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
5905 /* we overload curr_resync somewhat here.
5906 * 0 == not engaged in resync at all
5907 * 2 == checking that there is no conflict with another sync
5908 * 1 == like 2, but have yielded to allow conflicting resync to
5910 * other == active in resync - this many blocks
5912 * Before starting a resync we must have set curr_resync to
5913 * 2, and then checked that every "conflicting" array has curr_resync
5914 * less than ours. When we find one that is the same or higher
5915 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
5916 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
5917 * This will mean we have to start checking from the beginning again.
5922 mddev
->curr_resync
= 2;
5925 if (kthread_should_stop()) {
5926 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5929 for_each_mddev(mddev2
, tmp
) {
5930 if (mddev2
== mddev
)
5932 if (!mddev
->parallel_resync
5933 && mddev2
->curr_resync
5934 && match_mddev_units(mddev
, mddev2
)) {
5936 if (mddev
< mddev2
&& mddev
->curr_resync
== 2) {
5937 /* arbitrarily yield */
5938 mddev
->curr_resync
= 1;
5939 wake_up(&resync_wait
);
5941 if (mddev
> mddev2
&& mddev
->curr_resync
== 1)
5942 /* no need to wait here, we can wait the next
5943 * time 'round when curr_resync == 2
5946 /* We need to wait 'interruptible' so as not to
5947 * contribute to the load average, and not to
5948 * be caught by 'softlockup'
5950 prepare_to_wait(&resync_wait
, &wq
, TASK_INTERRUPTIBLE
);
5951 if (!kthread_should_stop() &&
5952 mddev2
->curr_resync
>= mddev
->curr_resync
) {
5953 printk(KERN_INFO
"md: delaying %s of %s"
5954 " until %s has finished (they"
5955 " share one or more physical units)\n",
5956 desc
, mdname(mddev
), mdname(mddev2
));
5958 if (signal_pending(current
))
5959 flush_signals(current
);
5961 finish_wait(&resync_wait
, &wq
);
5964 finish_wait(&resync_wait
, &wq
);
5967 } while (mddev
->curr_resync
< 2);
5970 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
5971 /* resync follows the size requested by the personality,
5972 * which defaults to physical size, but can be virtual size
5974 max_sectors
= mddev
->resync_max_sectors
;
5975 mddev
->resync_mismatches
= 0;
5976 /* we don't use the checkpoint if there's a bitmap */
5977 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
5978 j
= mddev
->resync_min
;
5979 else if (!mddev
->bitmap
)
5980 j
= mddev
->recovery_cp
;
5982 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
5983 max_sectors
= mddev
->size
<< 1;
5985 /* recovery follows the physical size of devices */
5986 max_sectors
= mddev
->size
<< 1;
5988 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
5989 if (rdev
->raid_disk
>= 0 &&
5990 !test_bit(Faulty
, &rdev
->flags
) &&
5991 !test_bit(In_sync
, &rdev
->flags
) &&
5992 rdev
->recovery_offset
< j
)
5993 j
= rdev
->recovery_offset
;
5996 printk(KERN_INFO
"md: %s of RAID array %s\n", desc
, mdname(mddev
));
5997 printk(KERN_INFO
"md: minimum _guaranteed_ speed:"
5998 " %d KB/sec/disk.\n", speed_min(mddev
));
5999 printk(KERN_INFO
"md: using maximum available idle IO bandwidth "
6000 "(but not more than %d KB/sec) for %s.\n",
6001 speed_max(mddev
), desc
);
6003 is_mddev_idle(mddev
); /* this also initializes IO event counters */
6006 for (m
= 0; m
< SYNC_MARKS
; m
++) {
6008 mark_cnt
[m
] = io_sectors
;
6011 mddev
->resync_mark
= mark
[last_mark
];
6012 mddev
->resync_mark_cnt
= mark_cnt
[last_mark
];
6015 * Tune reconstruction:
6017 window
= 32*(PAGE_SIZE
/512);
6018 printk(KERN_INFO
"md: using %dk window, over a total of %llu blocks.\n",
6019 window
/2,(unsigned long long) max_sectors
/2);
6021 atomic_set(&mddev
->recovery_active
, 0);
6026 "md: resuming %s of %s from checkpoint.\n",
6027 desc
, mdname(mddev
));
6028 mddev
->curr_resync
= j
;
6031 while (j
< max_sectors
) {
6035 if (j
>= mddev
->resync_max
) {
6036 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
6037 wait_event(mddev
->recovery_wait
,
6038 mddev
->resync_max
> j
6039 || kthread_should_stop());
6041 if (kthread_should_stop())
6043 sectors
= mddev
->pers
->sync_request(mddev
, j
, &skipped
,
6044 currspeed
< speed_min(mddev
));
6046 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6050 if (!skipped
) { /* actual IO requested */
6051 io_sectors
+= sectors
;
6052 atomic_add(sectors
, &mddev
->recovery_active
);
6056 if (j
>1) mddev
->curr_resync
= j
;
6057 mddev
->curr_mark_cnt
= io_sectors
;
6058 if (last_check
== 0)
6059 /* this is the earliers that rebuilt will be
6060 * visible in /proc/mdstat
6062 md_new_event(mddev
);
6064 if (last_check
+ window
> io_sectors
|| j
== max_sectors
)
6067 last_check
= io_sectors
;
6069 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
6073 if (time_after_eq(jiffies
, mark
[last_mark
] + SYNC_MARK_STEP
)) {
6075 int next
= (last_mark
+1) % SYNC_MARKS
;
6077 mddev
->resync_mark
= mark
[next
];
6078 mddev
->resync_mark_cnt
= mark_cnt
[next
];
6079 mark
[next
] = jiffies
;
6080 mark_cnt
[next
] = io_sectors
- atomic_read(&mddev
->recovery_active
);
6085 if (kthread_should_stop())
6090 * this loop exits only if either when we are slower than
6091 * the 'hard' speed limit, or the system was IO-idle for
6093 * the system might be non-idle CPU-wise, but we only care
6094 * about not overloading the IO subsystem. (things like an
6095 * e2fsck being done on the RAID array should execute fast)
6097 blk_unplug(mddev
->queue
);
6100 currspeed
= ((unsigned long)(io_sectors
-mddev
->resync_mark_cnt
))/2
6101 /((jiffies
-mddev
->resync_mark
)/HZ
+1) +1;
6103 if (currspeed
> speed_min(mddev
)) {
6104 if ((currspeed
> speed_max(mddev
)) ||
6105 !is_mddev_idle(mddev
)) {
6111 printk(KERN_INFO
"md: %s: %s done.\n",mdname(mddev
), desc
);
6113 * this also signals 'finished resyncing' to md_stop
6116 blk_unplug(mddev
->queue
);
6118 wait_event(mddev
->recovery_wait
, !atomic_read(&mddev
->recovery_active
));
6120 /* tell personality that we are finished */
6121 mddev
->pers
->sync_request(mddev
, max_sectors
, &skipped
, 1);
6123 if (!test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
) &&
6124 mddev
->curr_resync
> 2) {
6125 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
6126 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
6127 if (mddev
->curr_resync
>= mddev
->recovery_cp
) {
6129 "md: checkpointing %s of %s.\n",
6130 desc
, mdname(mddev
));
6131 mddev
->recovery_cp
= mddev
->curr_resync
;
6134 mddev
->recovery_cp
= MaxSector
;
6136 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
6137 mddev
->curr_resync
= MaxSector
;
6138 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
6139 if (rdev
->raid_disk
>= 0 &&
6140 !test_bit(Faulty
, &rdev
->flags
) &&
6141 !test_bit(In_sync
, &rdev
->flags
) &&
6142 rdev
->recovery_offset
< mddev
->curr_resync
)
6143 rdev
->recovery_offset
= mddev
->curr_resync
;
6146 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
6149 mddev
->curr_resync
= 0;
6150 mddev
->resync_min
= 0;
6151 mddev
->resync_max
= MaxSector
;
6152 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
6153 wake_up(&resync_wait
);
6154 set_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
6155 md_wakeup_thread(mddev
->thread
);
6160 * got a signal, exit.
6163 "md: md_do_sync() got signal ... exiting\n");
6164 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6168 EXPORT_SYMBOL_GPL(md_do_sync
);
6171 static int remove_and_add_spares(mddev_t
*mddev
)
6176 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
6177 if (rdev
->raid_disk
>= 0 &&
6178 !test_bit(Blocked
, &rdev
->flags
) &&
6179 (test_bit(Faulty
, &rdev
->flags
) ||
6180 ! test_bit(In_sync
, &rdev
->flags
)) &&
6181 atomic_read(&rdev
->nr_pending
)==0) {
6182 if (mddev
->pers
->hot_remove_disk(
6183 mddev
, rdev
->raid_disk
)==0) {
6185 sprintf(nm
,"rd%d", rdev
->raid_disk
);
6186 sysfs_remove_link(&mddev
->kobj
, nm
);
6187 rdev
->raid_disk
= -1;
6191 if (mddev
->degraded
&& ! mddev
->ro
&& !mddev
->recovery_disabled
) {
6192 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
6193 if (rdev
->raid_disk
>= 0 &&
6194 !test_bit(In_sync
, &rdev
->flags
) &&
6195 !test_bit(Blocked
, &rdev
->flags
))
6197 if (rdev
->raid_disk
< 0
6198 && !test_bit(Faulty
, &rdev
->flags
)) {
6199 rdev
->recovery_offset
= 0;
6201 hot_add_disk(mddev
, rdev
) == 0) {
6203 sprintf(nm
, "rd%d", rdev
->raid_disk
);
6204 if (sysfs_create_link(&mddev
->kobj
,
6207 "md: cannot register "
6211 md_new_event(mddev
);
6220 * This routine is regularly called by all per-raid-array threads to
6221 * deal with generic issues like resync and super-block update.
6222 * Raid personalities that don't have a thread (linear/raid0) do not
6223 * need this as they never do any recovery or update the superblock.
6225 * It does not do any resync itself, but rather "forks" off other threads
6226 * to do that as needed.
6227 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6228 * "->recovery" and create a thread at ->sync_thread.
6229 * When the thread finishes it sets MD_RECOVERY_DONE
6230 * and wakeups up this thread which will reap the thread and finish up.
6231 * This thread also removes any faulty devices (with nr_pending == 0).
6233 * The overall approach is:
6234 * 1/ if the superblock needs updating, update it.
6235 * 2/ If a recovery thread is running, don't do anything else.
6236 * 3/ If recovery has finished, clean up, possibly marking spares active.
6237 * 4/ If there are any faulty devices, remove them.
6238 * 5/ If array is degraded, try to add spares devices
6239 * 6/ If array has spares or is not in-sync, start a resync thread.
6241 void md_check_recovery(mddev_t
*mddev
)
6247 bitmap_daemon_work(mddev
->bitmap
);
6252 if (signal_pending(current
)) {
6253 if (mddev
->pers
->sync_request
&& !mddev
->external
) {
6254 printk(KERN_INFO
"md: %s in immediate safe mode\n",
6256 mddev
->safemode
= 2;
6258 flush_signals(current
);
6261 if (mddev
->ro
&& !test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
6264 (mddev
->flags
&& !mddev
->external
) ||
6265 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
6266 test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
6267 (mddev
->external
== 0 && mddev
->safemode
== 1) ||
6268 (mddev
->safemode
== 2 && ! atomic_read(&mddev
->writes_pending
)
6269 && !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
6273 if (mddev_trylock(mddev
)) {
6277 /* Only thing we do on a ro array is remove
6280 remove_and_add_spares(mddev
);
6281 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6285 if (!mddev
->external
) {
6287 spin_lock_irq(&mddev
->write_lock
);
6288 if (mddev
->safemode
&&
6289 !atomic_read(&mddev
->writes_pending
) &&
6291 mddev
->recovery_cp
== MaxSector
) {
6294 if (mddev
->persistent
)
6295 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6297 if (mddev
->safemode
== 1)
6298 mddev
->safemode
= 0;
6299 spin_unlock_irq(&mddev
->write_lock
);
6301 sysfs_notify_dirent(mddev
->sysfs_state
);
6305 md_update_sb(mddev
, 0);
6307 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
6308 if (test_and_clear_bit(StateChanged
, &rdev
->flags
))
6309 sysfs_notify_dirent(rdev
->sysfs_state
);
6312 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
6313 !test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
)) {
6314 /* resync/recovery still happening */
6315 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6318 if (mddev
->sync_thread
) {
6319 /* resync has finished, collect result */
6320 md_unregister_thread(mddev
->sync_thread
);
6321 mddev
->sync_thread
= NULL
;
6322 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
6323 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
6325 /* activate any spares */
6326 if (mddev
->pers
->spare_active(mddev
))
6327 sysfs_notify(&mddev
->kobj
, NULL
,
6330 md_update_sb(mddev
, 1);
6332 /* if array is no-longer degraded, then any saved_raid_disk
6333 * information must be scrapped
6335 if (!mddev
->degraded
)
6336 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
6337 rdev
->saved_raid_disk
= -1;
6339 mddev
->recovery
= 0;
6340 /* flag recovery needed just to double check */
6341 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6342 sysfs_notify_dirent(mddev
->sysfs_action
);
6343 md_new_event(mddev
);
6346 /* Set RUNNING before clearing NEEDED to avoid
6347 * any transients in the value of "sync_action".
6349 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
6350 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6351 /* Clear some bits that don't mean anything, but
6354 clear_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6355 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
6357 if (test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
6359 /* no recovery is running.
6360 * remove any failed drives, then
6361 * add spares if possible.
6362 * Spare are also removed and re-added, to allow
6363 * the personality to fail the re-add.
6366 if (mddev
->reshape_position
!= MaxSector
) {
6367 if (mddev
->pers
->check_reshape(mddev
) != 0)
6368 /* Cannot proceed */
6370 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
6371 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
6372 } else if ((spares
= remove_and_add_spares(mddev
))) {
6373 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
6374 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
6375 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
6376 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
6377 } else if (mddev
->recovery_cp
< MaxSector
) {
6378 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
6379 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
6380 } else if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
6381 /* nothing to be done ... */
6384 if (mddev
->pers
->sync_request
) {
6385 if (spares
&& mddev
->bitmap
&& ! mddev
->bitmap
->file
) {
6386 /* We are adding a device or devices to an array
6387 * which has the bitmap stored on all devices.
6388 * So make sure all bitmap pages get written
6390 bitmap_write_all(mddev
->bitmap
);
6392 mddev
->sync_thread
= md_register_thread(md_do_sync
,
6395 if (!mddev
->sync_thread
) {
6396 printk(KERN_ERR
"%s: could not start resync"
6399 /* leave the spares where they are, it shouldn't hurt */
6400 mddev
->recovery
= 0;
6402 md_wakeup_thread(mddev
->sync_thread
);
6403 sysfs_notify_dirent(mddev
->sysfs_action
);
6404 md_new_event(mddev
);
6407 if (!mddev
->sync_thread
) {
6408 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
6409 if (test_and_clear_bit(MD_RECOVERY_RECOVER
,
6411 if (mddev
->sysfs_action
)
6412 sysfs_notify_dirent(mddev
->sysfs_action
);
6414 mddev_unlock(mddev
);
6418 void md_wait_for_blocked_rdev(mdk_rdev_t
*rdev
, mddev_t
*mddev
)
6420 sysfs_notify_dirent(rdev
->sysfs_state
);
6421 wait_event_timeout(rdev
->blocked_wait
,
6422 !test_bit(Blocked
, &rdev
->flags
),
6423 msecs_to_jiffies(5000));
6424 rdev_dec_pending(rdev
, mddev
);
6426 EXPORT_SYMBOL(md_wait_for_blocked_rdev
);
6428 static int md_notify_reboot(struct notifier_block
*this,
6429 unsigned long code
, void *x
)
6431 struct list_head
*tmp
;
6434 if ((code
== SYS_DOWN
) || (code
== SYS_HALT
) || (code
== SYS_POWER_OFF
)) {
6436 printk(KERN_INFO
"md: stopping all md devices.\n");
6438 for_each_mddev(mddev
, tmp
)
6439 if (mddev_trylock(mddev
)) {
6440 /* Force a switch to readonly even array
6441 * appears to still be in use. Hence
6444 do_md_stop(mddev
, 1, 100);
6445 mddev_unlock(mddev
);
6448 * certain more exotic SCSI devices are known to be
6449 * volatile wrt too early system reboots. While the
6450 * right place to handle this issue is the given
6451 * driver, we do want to have a safe RAID driver ...
6458 static struct notifier_block md_notifier
= {
6459 .notifier_call
= md_notify_reboot
,
6461 .priority
= INT_MAX
, /* before any real devices */
6464 static void md_geninit(void)
6466 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t
));
6468 proc_create("mdstat", S_IRUGO
, NULL
, &md_seq_fops
);
6471 static int __init
md_init(void)
6473 if (register_blkdev(MAJOR_NR
, "md"))
6475 if ((mdp_major
=register_blkdev(0, "mdp"))<=0) {
6476 unregister_blkdev(MAJOR_NR
, "md");
6479 blk_register_region(MKDEV(MAJOR_NR
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
6480 md_probe
, NULL
, NULL
);
6481 blk_register_region(MKDEV(mdp_major
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
6482 md_probe
, NULL
, NULL
);
6484 register_reboot_notifier(&md_notifier
);
6485 raid_table_header
= register_sysctl_table(raid_root_table
);
6495 * Searches all registered partitions for autorun RAID arrays
6499 static LIST_HEAD(all_detected_devices
);
6500 struct detected_devices_node
{
6501 struct list_head list
;
6505 void md_autodetect_dev(dev_t dev
)
6507 struct detected_devices_node
*node_detected_dev
;
6509 node_detected_dev
= kzalloc(sizeof(*node_detected_dev
), GFP_KERNEL
);
6510 if (node_detected_dev
) {
6511 node_detected_dev
->dev
= dev
;
6512 list_add_tail(&node_detected_dev
->list
, &all_detected_devices
);
6514 printk(KERN_CRIT
"md: md_autodetect_dev: kzalloc failed"
6515 ", skipping dev(%d,%d)\n", MAJOR(dev
), MINOR(dev
));
6520 static void autostart_arrays(int part
)
6523 struct detected_devices_node
*node_detected_dev
;
6525 int i_scanned
, i_passed
;
6530 printk(KERN_INFO
"md: Autodetecting RAID arrays.\n");
6532 while (!list_empty(&all_detected_devices
) && i_scanned
< INT_MAX
) {
6534 node_detected_dev
= list_entry(all_detected_devices
.next
,
6535 struct detected_devices_node
, list
);
6536 list_del(&node_detected_dev
->list
);
6537 dev
= node_detected_dev
->dev
;
6538 kfree(node_detected_dev
);
6539 rdev
= md_import_device(dev
,0, 90);
6543 if (test_bit(Faulty
, &rdev
->flags
)) {
6547 set_bit(AutoDetected
, &rdev
->flags
);
6548 list_add(&rdev
->same_set
, &pending_raid_disks
);
6552 printk(KERN_INFO
"md: Scanned %d and added %d devices.\n",
6553 i_scanned
, i_passed
);
6555 autorun_devices(part
);
6558 #endif /* !MODULE */
6560 static __exit
void md_exit(void)
6563 struct list_head
*tmp
;
6565 blk_unregister_region(MKDEV(MAJOR_NR
,0), 1U << MINORBITS
);
6566 blk_unregister_region(MKDEV(mdp_major
,0), 1U << MINORBITS
);
6568 unregister_blkdev(MAJOR_NR
,"md");
6569 unregister_blkdev(mdp_major
, "mdp");
6570 unregister_reboot_notifier(&md_notifier
);
6571 unregister_sysctl_table(raid_table_header
);
6572 remove_proc_entry("mdstat", NULL
);
6573 for_each_mddev(mddev
, tmp
) {
6574 export_array(mddev
);
6575 mddev
->hold_active
= 0;
6579 subsys_initcall(md_init
);
6580 module_exit(md_exit
)
6582 static int get_ro(char *buffer
, struct kernel_param
*kp
)
6584 return sprintf(buffer
, "%d", start_readonly
);
6586 static int set_ro(const char *val
, struct kernel_param
*kp
)
6589 int num
= simple_strtoul(val
, &e
, 10);
6590 if (*val
&& (*e
== '\0' || *e
== '\n')) {
6591 start_readonly
= num
;
6597 module_param_call(start_ro
, set_ro
, get_ro
, NULL
, S_IRUSR
|S_IWUSR
);
6598 module_param(start_dirty_degraded
, int, S_IRUGO
|S_IWUSR
);
6600 module_param_call(new_array
, add_named_array
, NULL
, NULL
, S_IWUSR
);
6602 EXPORT_SYMBOL(register_md_personality
);
6603 EXPORT_SYMBOL(unregister_md_personality
);
6604 EXPORT_SYMBOL(md_error
);
6605 EXPORT_SYMBOL(md_done_sync
);
6606 EXPORT_SYMBOL(md_write_start
);
6607 EXPORT_SYMBOL(md_write_end
);
6608 EXPORT_SYMBOL(md_register_thread
);
6609 EXPORT_SYMBOL(md_unregister_thread
);
6610 EXPORT_SYMBOL(md_wakeup_thread
);
6611 EXPORT_SYMBOL(md_check_recovery
);
6612 MODULE_LICENSE("GPL");
6614 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR
);