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/blkdev.h>
37 #include <linux/sysctl.h>
38 #include <linux/seq_file.h>
39 #include <linux/buffer_head.h> /* for invalidate_bdev */
40 #include <linux/poll.h>
41 #include <linux/ctype.h>
42 #include <linux/string.h>
43 #include <linux/hdreg.h>
44 #include <linux/proc_fs.h>
45 #include <linux/random.h>
46 #include <linux/reboot.h>
47 #include <linux/file.h>
48 #include <linux/compat.h>
49 #include <linux/delay.h>
50 #include <linux/raid/md_p.h>
51 #include <linux/raid/md_u.h>
52 #include <linux/slab.h>
57 #define dprintk(x...) ((void)(DEBUG && printk(x)))
61 static void autostart_arrays(int part
);
64 static LIST_HEAD(pers_list
);
65 static DEFINE_SPINLOCK(pers_lock
);
67 static void md_print_devices(void);
69 static DECLARE_WAIT_QUEUE_HEAD(resync_wait
);
71 #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
74 * Default number of read corrections we'll attempt on an rdev
75 * before ejecting it from the array. We divide the read error
76 * count by 2 for every hour elapsed between read errors.
78 #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
80 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
81 * is 1000 KB/sec, so the extra system load does not show up that much.
82 * Increase it if you want to have more _guaranteed_ speed. Note that
83 * the RAID driver will use the maximum available bandwidth if the IO
84 * subsystem is idle. There is also an 'absolute maximum' reconstruction
85 * speed limit - in case reconstruction slows down your system despite
88 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
89 * or /sys/block/mdX/md/sync_speed_{min,max}
92 static int sysctl_speed_limit_min
= 1000;
93 static int sysctl_speed_limit_max
= 200000;
94 static inline int speed_min(mddev_t
*mddev
)
96 return mddev
->sync_speed_min
?
97 mddev
->sync_speed_min
: sysctl_speed_limit_min
;
100 static inline int speed_max(mddev_t
*mddev
)
102 return mddev
->sync_speed_max
?
103 mddev
->sync_speed_max
: sysctl_speed_limit_max
;
106 static struct ctl_table_header
*raid_table_header
;
108 static ctl_table raid_table
[] = {
110 .procname
= "speed_limit_min",
111 .data
= &sysctl_speed_limit_min
,
112 .maxlen
= sizeof(int),
113 .mode
= S_IRUGO
|S_IWUSR
,
114 .proc_handler
= proc_dointvec
,
117 .procname
= "speed_limit_max",
118 .data
= &sysctl_speed_limit_max
,
119 .maxlen
= sizeof(int),
120 .mode
= S_IRUGO
|S_IWUSR
,
121 .proc_handler
= proc_dointvec
,
126 static ctl_table raid_dir_table
[] = {
130 .mode
= S_IRUGO
|S_IXUGO
,
136 static ctl_table raid_root_table
[] = {
141 .child
= raid_dir_table
,
146 static const struct block_device_operations md_fops
;
148 static int start_readonly
;
151 * We have a system wide 'event count' that is incremented
152 * on any 'interesting' event, and readers of /proc/mdstat
153 * can use 'poll' or 'select' to find out when the event
157 * start array, stop array, error, add device, remove device,
158 * start build, activate spare
160 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters
);
161 static atomic_t md_event_count
;
162 void md_new_event(mddev_t
*mddev
)
164 atomic_inc(&md_event_count
);
165 wake_up(&md_event_waiters
);
167 EXPORT_SYMBOL_GPL(md_new_event
);
169 /* Alternate version that can be called from interrupts
170 * when calling sysfs_notify isn't needed.
172 static void md_new_event_inintr(mddev_t
*mddev
)
174 atomic_inc(&md_event_count
);
175 wake_up(&md_event_waiters
);
179 * Enables to iterate over all existing md arrays
180 * all_mddevs_lock protects this list.
182 static LIST_HEAD(all_mddevs
);
183 static DEFINE_SPINLOCK(all_mddevs_lock
);
187 * iterates through all used mddevs in the system.
188 * We take care to grab the all_mddevs_lock whenever navigating
189 * the list, and to always hold a refcount when unlocked.
190 * Any code which breaks out of this loop while own
191 * a reference to the current mddev and must mddev_put it.
193 #define for_each_mddev(mddev,tmp) \
195 for (({ spin_lock(&all_mddevs_lock); \
196 tmp = all_mddevs.next; \
198 ({ if (tmp != &all_mddevs) \
199 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
200 spin_unlock(&all_mddevs_lock); \
201 if (mddev) mddev_put(mddev); \
202 mddev = list_entry(tmp, mddev_t, all_mddevs); \
203 tmp != &all_mddevs;}); \
204 ({ spin_lock(&all_mddevs_lock); \
209 /* Rather than calling directly into the personality make_request function,
210 * IO requests come here first so that we can check if the device is
211 * being suspended pending a reconfiguration.
212 * We hold a refcount over the call to ->make_request. By the time that
213 * call has finished, the bio has been linked into some internal structure
214 * and so is visible to ->quiesce(), so we don't need the refcount any more.
216 static int md_make_request(struct request_queue
*q
, struct bio
*bio
)
218 mddev_t
*mddev
= q
->queuedata
;
220 if (mddev
== NULL
|| mddev
->pers
== NULL
) {
225 if (mddev
->suspended
|| mddev
->barrier
) {
228 prepare_to_wait(&mddev
->sb_wait
, &__wait
,
229 TASK_UNINTERRUPTIBLE
);
230 if (!mddev
->suspended
&& !mddev
->barrier
)
236 finish_wait(&mddev
->sb_wait
, &__wait
);
238 atomic_inc(&mddev
->active_io
);
240 rv
= mddev
->pers
->make_request(q
, bio
);
241 if (atomic_dec_and_test(&mddev
->active_io
) && mddev
->suspended
)
242 wake_up(&mddev
->sb_wait
);
247 static void mddev_suspend(mddev_t
*mddev
)
249 BUG_ON(mddev
->suspended
);
250 mddev
->suspended
= 1;
252 wait_event(mddev
->sb_wait
, atomic_read(&mddev
->active_io
) == 0);
253 mddev
->pers
->quiesce(mddev
, 1);
254 md_unregister_thread(mddev
->thread
);
255 mddev
->thread
= NULL
;
256 /* we now know that no code is executing in the personality module,
257 * except possibly the tail end of a ->bi_end_io function, but that
258 * is certain to complete before the module has a chance to get
263 static void mddev_resume(mddev_t
*mddev
)
265 mddev
->suspended
= 0;
266 wake_up(&mddev
->sb_wait
);
267 mddev
->pers
->quiesce(mddev
, 0);
270 int mddev_congested(mddev_t
*mddev
, int bits
)
274 return mddev
->suspended
;
276 EXPORT_SYMBOL(mddev_congested
);
279 * Generic barrier handling for md
282 #define POST_REQUEST_BARRIER ((void*)1)
284 static void md_end_barrier(struct bio
*bio
, int err
)
286 mdk_rdev_t
*rdev
= bio
->bi_private
;
287 mddev_t
*mddev
= rdev
->mddev
;
288 if (err
== -EOPNOTSUPP
&& mddev
->barrier
!= POST_REQUEST_BARRIER
)
289 set_bit(BIO_EOPNOTSUPP
, &mddev
->barrier
->bi_flags
);
291 rdev_dec_pending(rdev
, mddev
);
293 if (atomic_dec_and_test(&mddev
->flush_pending
)) {
294 if (mddev
->barrier
== POST_REQUEST_BARRIER
) {
295 /* This was a post-request barrier */
296 mddev
->barrier
= NULL
;
297 wake_up(&mddev
->sb_wait
);
299 /* The pre-request barrier has finished */
300 schedule_work(&mddev
->barrier_work
);
305 static void submit_barriers(mddev_t
*mddev
)
310 list_for_each_entry_rcu(rdev
, &mddev
->disks
, same_set
)
311 if (rdev
->raid_disk
>= 0 &&
312 !test_bit(Faulty
, &rdev
->flags
)) {
313 /* Take two references, one is dropped
314 * when request finishes, one after
315 * we reclaim rcu_read_lock
318 atomic_inc(&rdev
->nr_pending
);
319 atomic_inc(&rdev
->nr_pending
);
321 bi
= bio_alloc(GFP_KERNEL
, 0);
322 bi
->bi_end_io
= md_end_barrier
;
323 bi
->bi_private
= rdev
;
324 bi
->bi_bdev
= rdev
->bdev
;
325 atomic_inc(&mddev
->flush_pending
);
326 submit_bio(WRITE_BARRIER
, bi
);
328 rdev_dec_pending(rdev
, mddev
);
333 static void md_submit_barrier(struct work_struct
*ws
)
335 mddev_t
*mddev
= container_of(ws
, mddev_t
, barrier_work
);
336 struct bio
*bio
= mddev
->barrier
;
338 atomic_set(&mddev
->flush_pending
, 1);
340 if (test_bit(BIO_EOPNOTSUPP
, &bio
->bi_flags
))
341 bio_endio(bio
, -EOPNOTSUPP
);
342 else if (bio
->bi_size
== 0)
343 /* an empty barrier - all done */
346 bio
->bi_rw
&= ~(1<<BIO_RW_BARRIER
);
347 if (mddev
->pers
->make_request(mddev
->queue
, bio
))
348 generic_make_request(bio
);
349 mddev
->barrier
= POST_REQUEST_BARRIER
;
350 submit_barriers(mddev
);
352 if (atomic_dec_and_test(&mddev
->flush_pending
)) {
353 mddev
->barrier
= NULL
;
354 wake_up(&mddev
->sb_wait
);
358 void md_barrier_request(mddev_t
*mddev
, struct bio
*bio
)
360 spin_lock_irq(&mddev
->write_lock
);
361 wait_event_lock_irq(mddev
->sb_wait
,
363 mddev
->write_lock
, /*nothing*/);
364 mddev
->barrier
= bio
;
365 spin_unlock_irq(&mddev
->write_lock
);
367 atomic_set(&mddev
->flush_pending
, 1);
368 INIT_WORK(&mddev
->barrier_work
, md_submit_barrier
);
370 submit_barriers(mddev
);
372 if (atomic_dec_and_test(&mddev
->flush_pending
))
373 schedule_work(&mddev
->barrier_work
);
375 EXPORT_SYMBOL(md_barrier_request
);
377 static inline mddev_t
*mddev_get(mddev_t
*mddev
)
379 atomic_inc(&mddev
->active
);
383 static void mddev_delayed_delete(struct work_struct
*ws
);
385 static void mddev_put(mddev_t
*mddev
)
387 if (!atomic_dec_and_lock(&mddev
->active
, &all_mddevs_lock
))
389 if (!mddev
->raid_disks
&& list_empty(&mddev
->disks
) &&
390 mddev
->ctime
== 0 && !mddev
->hold_active
) {
391 /* Array is not configured at all, and not held active,
393 list_del(&mddev
->all_mddevs
);
394 if (mddev
->gendisk
) {
395 /* we did a probe so need to clean up.
396 * Call schedule_work inside the spinlock
397 * so that flush_scheduled_work() after
398 * mddev_find will succeed in waiting for the
401 INIT_WORK(&mddev
->del_work
, mddev_delayed_delete
);
402 schedule_work(&mddev
->del_work
);
406 spin_unlock(&all_mddevs_lock
);
409 static mddev_t
* mddev_find(dev_t unit
)
411 mddev_t
*mddev
, *new = NULL
;
414 spin_lock(&all_mddevs_lock
);
417 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
418 if (mddev
->unit
== unit
) {
420 spin_unlock(&all_mddevs_lock
);
426 list_add(&new->all_mddevs
, &all_mddevs
);
427 spin_unlock(&all_mddevs_lock
);
428 new->hold_active
= UNTIL_IOCTL
;
432 /* find an unused unit number */
433 static int next_minor
= 512;
434 int start
= next_minor
;
438 dev
= MKDEV(MD_MAJOR
, next_minor
);
440 if (next_minor
> MINORMASK
)
442 if (next_minor
== start
) {
443 /* Oh dear, all in use. */
444 spin_unlock(&all_mddevs_lock
);
450 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
451 if (mddev
->unit
== dev
) {
457 new->md_minor
= MINOR(dev
);
458 new->hold_active
= UNTIL_STOP
;
459 list_add(&new->all_mddevs
, &all_mddevs
);
460 spin_unlock(&all_mddevs_lock
);
463 spin_unlock(&all_mddevs_lock
);
465 new = kzalloc(sizeof(*new), GFP_KERNEL
);
470 if (MAJOR(unit
) == MD_MAJOR
)
471 new->md_minor
= MINOR(unit
);
473 new->md_minor
= MINOR(unit
) >> MdpMinorShift
;
475 mutex_init(&new->open_mutex
);
476 mutex_init(&new->reconfig_mutex
);
477 mutex_init(&new->bitmap_info
.mutex
);
478 INIT_LIST_HEAD(&new->disks
);
479 INIT_LIST_HEAD(&new->all_mddevs
);
480 init_timer(&new->safemode_timer
);
481 atomic_set(&new->active
, 1);
482 atomic_set(&new->openers
, 0);
483 atomic_set(&new->active_io
, 0);
484 spin_lock_init(&new->write_lock
);
485 atomic_set(&new->flush_pending
, 0);
486 init_waitqueue_head(&new->sb_wait
);
487 init_waitqueue_head(&new->recovery_wait
);
488 new->reshape_position
= MaxSector
;
490 new->resync_max
= MaxSector
;
491 new->level
= LEVEL_NONE
;
496 static inline int mddev_lock(mddev_t
* mddev
)
498 return mutex_lock_interruptible(&mddev
->reconfig_mutex
);
501 static inline int mddev_is_locked(mddev_t
*mddev
)
503 return mutex_is_locked(&mddev
->reconfig_mutex
);
506 static inline int mddev_trylock(mddev_t
* mddev
)
508 return mutex_trylock(&mddev
->reconfig_mutex
);
511 static inline void mddev_unlock(mddev_t
* mddev
)
513 mutex_unlock(&mddev
->reconfig_mutex
);
515 md_wakeup_thread(mddev
->thread
);
518 static mdk_rdev_t
* find_rdev_nr(mddev_t
*mddev
, int nr
)
522 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
523 if (rdev
->desc_nr
== nr
)
529 static mdk_rdev_t
* find_rdev(mddev_t
* mddev
, dev_t dev
)
533 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
534 if (rdev
->bdev
->bd_dev
== dev
)
540 static struct mdk_personality
*find_pers(int level
, char *clevel
)
542 struct mdk_personality
*pers
;
543 list_for_each_entry(pers
, &pers_list
, list
) {
544 if (level
!= LEVEL_NONE
&& pers
->level
== level
)
546 if (strcmp(pers
->name
, clevel
)==0)
552 /* return the offset of the super block in 512byte sectors */
553 static inline sector_t
calc_dev_sboffset(struct block_device
*bdev
)
555 sector_t num_sectors
= bdev
->bd_inode
->i_size
/ 512;
556 return MD_NEW_SIZE_SECTORS(num_sectors
);
559 static int alloc_disk_sb(mdk_rdev_t
* rdev
)
564 rdev
->sb_page
= alloc_page(GFP_KERNEL
);
565 if (!rdev
->sb_page
) {
566 printk(KERN_ALERT
"md: out of memory.\n");
573 static void free_disk_sb(mdk_rdev_t
* rdev
)
576 put_page(rdev
->sb_page
);
578 rdev
->sb_page
= NULL
;
585 static void super_written(struct bio
*bio
, int error
)
587 mdk_rdev_t
*rdev
= bio
->bi_private
;
588 mddev_t
*mddev
= rdev
->mddev
;
590 if (error
|| !test_bit(BIO_UPTODATE
, &bio
->bi_flags
)) {
591 printk("md: super_written gets error=%d, uptodate=%d\n",
592 error
, test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
593 WARN_ON(test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
594 md_error(mddev
, rdev
);
597 if (atomic_dec_and_test(&mddev
->pending_writes
))
598 wake_up(&mddev
->sb_wait
);
602 static void super_written_barrier(struct bio
*bio
, int error
)
604 struct bio
*bio2
= bio
->bi_private
;
605 mdk_rdev_t
*rdev
= bio2
->bi_private
;
606 mddev_t
*mddev
= rdev
->mddev
;
608 if (!test_bit(BIO_UPTODATE
, &bio
->bi_flags
) &&
609 error
== -EOPNOTSUPP
) {
611 /* barriers don't appear to be supported :-( */
612 set_bit(BarriersNotsupp
, &rdev
->flags
);
613 mddev
->barriers_work
= 0;
614 spin_lock_irqsave(&mddev
->write_lock
, flags
);
615 bio2
->bi_next
= mddev
->biolist
;
616 mddev
->biolist
= bio2
;
617 spin_unlock_irqrestore(&mddev
->write_lock
, flags
);
618 wake_up(&mddev
->sb_wait
);
622 bio
->bi_private
= rdev
;
623 super_written(bio
, error
);
627 void md_super_write(mddev_t
*mddev
, mdk_rdev_t
*rdev
,
628 sector_t sector
, int size
, struct page
*page
)
630 /* write first size bytes of page to sector of rdev
631 * Increment mddev->pending_writes before returning
632 * and decrement it on completion, waking up sb_wait
633 * if zero is reached.
634 * If an error occurred, call md_error
636 * As we might need to resubmit the request if BIO_RW_BARRIER
637 * causes ENOTSUPP, we allocate a spare bio...
639 struct bio
*bio
= bio_alloc(GFP_NOIO
, 1);
640 int rw
= (1<<BIO_RW
) | (1<<BIO_RW_SYNCIO
) | (1<<BIO_RW_UNPLUG
);
642 bio
->bi_bdev
= rdev
->bdev
;
643 bio
->bi_sector
= sector
;
644 bio_add_page(bio
, page
, size
, 0);
645 bio
->bi_private
= rdev
;
646 bio
->bi_end_io
= super_written
;
649 atomic_inc(&mddev
->pending_writes
);
650 if (!test_bit(BarriersNotsupp
, &rdev
->flags
)) {
652 rw
|= (1<<BIO_RW_BARRIER
);
653 rbio
= bio_clone(bio
, GFP_NOIO
);
654 rbio
->bi_private
= bio
;
655 rbio
->bi_end_io
= super_written_barrier
;
656 submit_bio(rw
, rbio
);
661 void md_super_wait(mddev_t
*mddev
)
663 /* wait for all superblock writes that were scheduled to complete.
664 * if any had to be retried (due to BARRIER problems), retry them
668 prepare_to_wait(&mddev
->sb_wait
, &wq
, TASK_UNINTERRUPTIBLE
);
669 if (atomic_read(&mddev
->pending_writes
)==0)
671 while (mddev
->biolist
) {
673 spin_lock_irq(&mddev
->write_lock
);
674 bio
= mddev
->biolist
;
675 mddev
->biolist
= bio
->bi_next
;
677 spin_unlock_irq(&mddev
->write_lock
);
678 submit_bio(bio
->bi_rw
, bio
);
682 finish_wait(&mddev
->sb_wait
, &wq
);
685 static void bi_complete(struct bio
*bio
, int error
)
687 complete((struct completion
*)bio
->bi_private
);
690 int sync_page_io(struct block_device
*bdev
, sector_t sector
, int size
,
691 struct page
*page
, int rw
)
693 struct bio
*bio
= bio_alloc(GFP_NOIO
, 1);
694 struct completion event
;
697 rw
|= (1 << BIO_RW_SYNCIO
) | (1 << BIO_RW_UNPLUG
);
700 bio
->bi_sector
= sector
;
701 bio_add_page(bio
, page
, size
, 0);
702 init_completion(&event
);
703 bio
->bi_private
= &event
;
704 bio
->bi_end_io
= bi_complete
;
706 wait_for_completion(&event
);
708 ret
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
712 EXPORT_SYMBOL_GPL(sync_page_io
);
714 static int read_disk_sb(mdk_rdev_t
* rdev
, int size
)
716 char b
[BDEVNAME_SIZE
];
717 if (!rdev
->sb_page
) {
725 if (!sync_page_io(rdev
->bdev
, rdev
->sb_start
, size
, rdev
->sb_page
, READ
))
731 printk(KERN_WARNING
"md: disabled device %s, could not read superblock.\n",
732 bdevname(rdev
->bdev
,b
));
736 static int uuid_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
738 return sb1
->set_uuid0
== sb2
->set_uuid0
&&
739 sb1
->set_uuid1
== sb2
->set_uuid1
&&
740 sb1
->set_uuid2
== sb2
->set_uuid2
&&
741 sb1
->set_uuid3
== sb2
->set_uuid3
;
744 static int sb_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
747 mdp_super_t
*tmp1
, *tmp2
;
749 tmp1
= kmalloc(sizeof(*tmp1
),GFP_KERNEL
);
750 tmp2
= kmalloc(sizeof(*tmp2
),GFP_KERNEL
);
752 if (!tmp1
|| !tmp2
) {
754 printk(KERN_INFO
"md.c sb_equal(): failed to allocate memory!\n");
762 * nr_disks is not constant
767 ret
= (memcmp(tmp1
, tmp2
, MD_SB_GENERIC_CONSTANT_WORDS
* 4) == 0);
775 static u32
md_csum_fold(u32 csum
)
777 csum
= (csum
& 0xffff) + (csum
>> 16);
778 return (csum
& 0xffff) + (csum
>> 16);
781 static unsigned int calc_sb_csum(mdp_super_t
* sb
)
784 u32
*sb32
= (u32
*)sb
;
786 unsigned int disk_csum
, csum
;
788 disk_csum
= sb
->sb_csum
;
791 for (i
= 0; i
< MD_SB_BYTES
/4 ; i
++)
793 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
797 /* This used to use csum_partial, which was wrong for several
798 * reasons including that different results are returned on
799 * different architectures. It isn't critical that we get exactly
800 * the same return value as before (we always csum_fold before
801 * testing, and that removes any differences). However as we
802 * know that csum_partial always returned a 16bit value on
803 * alphas, do a fold to maximise conformity to previous behaviour.
805 sb
->sb_csum
= md_csum_fold(disk_csum
);
807 sb
->sb_csum
= disk_csum
;
814 * Handle superblock details.
815 * We want to be able to handle multiple superblock formats
816 * so we have a common interface to them all, and an array of
817 * different handlers.
818 * We rely on user-space to write the initial superblock, and support
819 * reading and updating of superblocks.
820 * Interface methods are:
821 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
822 * loads and validates a superblock on dev.
823 * if refdev != NULL, compare superblocks on both devices
825 * 0 - dev has a superblock that is compatible with refdev
826 * 1 - dev has a superblock that is compatible and newer than refdev
827 * so dev should be used as the refdev in future
828 * -EINVAL superblock incompatible or invalid
829 * -othererror e.g. -EIO
831 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
832 * Verify that dev is acceptable into mddev.
833 * The first time, mddev->raid_disks will be 0, and data from
834 * dev should be merged in. Subsequent calls check that dev
835 * is new enough. Return 0 or -EINVAL
837 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
838 * Update the superblock for rdev with data in mddev
839 * This does not write to disc.
845 struct module
*owner
;
846 int (*load_super
)(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
,
848 int (*validate_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
849 void (*sync_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
850 unsigned long long (*rdev_size_change
)(mdk_rdev_t
*rdev
,
851 sector_t num_sectors
);
855 * Check that the given mddev has no bitmap.
857 * This function is called from the run method of all personalities that do not
858 * support bitmaps. It prints an error message and returns non-zero if mddev
859 * has a bitmap. Otherwise, it returns 0.
862 int md_check_no_bitmap(mddev_t
*mddev
)
864 if (!mddev
->bitmap_info
.file
&& !mddev
->bitmap_info
.offset
)
866 printk(KERN_ERR
"%s: bitmaps are not supported for %s\n",
867 mdname(mddev
), mddev
->pers
->name
);
870 EXPORT_SYMBOL(md_check_no_bitmap
);
873 * load_super for 0.90.0
875 static int super_90_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
877 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
882 * Calculate the position of the superblock (512byte sectors),
883 * it's at the end of the disk.
885 * It also happens to be a multiple of 4Kb.
887 rdev
->sb_start
= calc_dev_sboffset(rdev
->bdev
);
889 ret
= read_disk_sb(rdev
, MD_SB_BYTES
);
894 bdevname(rdev
->bdev
, b
);
895 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
897 if (sb
->md_magic
!= MD_SB_MAGIC
) {
898 printk(KERN_ERR
"md: invalid raid superblock magic on %s\n",
903 if (sb
->major_version
!= 0 ||
904 sb
->minor_version
< 90 ||
905 sb
->minor_version
> 91) {
906 printk(KERN_WARNING
"Bad version number %d.%d on %s\n",
907 sb
->major_version
, sb
->minor_version
,
912 if (sb
->raid_disks
<= 0)
915 if (md_csum_fold(calc_sb_csum(sb
)) != md_csum_fold(sb
->sb_csum
)) {
916 printk(KERN_WARNING
"md: invalid superblock checksum on %s\n",
921 rdev
->preferred_minor
= sb
->md_minor
;
922 rdev
->data_offset
= 0;
923 rdev
->sb_size
= MD_SB_BYTES
;
925 if (sb
->level
== LEVEL_MULTIPATH
)
928 rdev
->desc_nr
= sb
->this_disk
.number
;
934 mdp_super_t
*refsb
= (mdp_super_t
*)page_address(refdev
->sb_page
);
935 if (!uuid_equal(refsb
, sb
)) {
936 printk(KERN_WARNING
"md: %s has different UUID to %s\n",
937 b
, bdevname(refdev
->bdev
,b2
));
940 if (!sb_equal(refsb
, sb
)) {
941 printk(KERN_WARNING
"md: %s has same UUID"
942 " but different superblock to %s\n",
943 b
, bdevname(refdev
->bdev
, b2
));
947 ev2
= md_event(refsb
);
953 rdev
->sectors
= rdev
->sb_start
;
955 if (rdev
->sectors
< sb
->size
* 2 && sb
->level
> 1)
956 /* "this cannot possibly happen" ... */
964 * validate_super for 0.90.0
966 static int super_90_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
969 mdp_super_t
*sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
970 __u64 ev1
= md_event(sb
);
972 rdev
->raid_disk
= -1;
973 clear_bit(Faulty
, &rdev
->flags
);
974 clear_bit(In_sync
, &rdev
->flags
);
975 clear_bit(WriteMostly
, &rdev
->flags
);
976 clear_bit(BarriersNotsupp
, &rdev
->flags
);
978 if (mddev
->raid_disks
== 0) {
979 mddev
->major_version
= 0;
980 mddev
->minor_version
= sb
->minor_version
;
981 mddev
->patch_version
= sb
->patch_version
;
983 mddev
->chunk_sectors
= sb
->chunk_size
>> 9;
984 mddev
->ctime
= sb
->ctime
;
985 mddev
->utime
= sb
->utime
;
986 mddev
->level
= sb
->level
;
987 mddev
->clevel
[0] = 0;
988 mddev
->layout
= sb
->layout
;
989 mddev
->raid_disks
= sb
->raid_disks
;
990 mddev
->dev_sectors
= sb
->size
* 2;
992 mddev
->bitmap_info
.offset
= 0;
993 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
995 if (mddev
->minor_version
>= 91) {
996 mddev
->reshape_position
= sb
->reshape_position
;
997 mddev
->delta_disks
= sb
->delta_disks
;
998 mddev
->new_level
= sb
->new_level
;
999 mddev
->new_layout
= sb
->new_layout
;
1000 mddev
->new_chunk_sectors
= sb
->new_chunk
>> 9;
1002 mddev
->reshape_position
= MaxSector
;
1003 mddev
->delta_disks
= 0;
1004 mddev
->new_level
= mddev
->level
;
1005 mddev
->new_layout
= mddev
->layout
;
1006 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1009 if (sb
->state
& (1<<MD_SB_CLEAN
))
1010 mddev
->recovery_cp
= MaxSector
;
1012 if (sb
->events_hi
== sb
->cp_events_hi
&&
1013 sb
->events_lo
== sb
->cp_events_lo
) {
1014 mddev
->recovery_cp
= sb
->recovery_cp
;
1016 mddev
->recovery_cp
= 0;
1019 memcpy(mddev
->uuid
+0, &sb
->set_uuid0
, 4);
1020 memcpy(mddev
->uuid
+4, &sb
->set_uuid1
, 4);
1021 memcpy(mddev
->uuid
+8, &sb
->set_uuid2
, 4);
1022 memcpy(mddev
->uuid
+12,&sb
->set_uuid3
, 4);
1024 mddev
->max_disks
= MD_SB_DISKS
;
1026 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
) &&
1027 mddev
->bitmap_info
.file
== NULL
)
1028 mddev
->bitmap_info
.offset
=
1029 mddev
->bitmap_info
.default_offset
;
1031 } else if (mddev
->pers
== NULL
) {
1032 /* Insist on good event counter while assembling */
1034 if (ev1
< mddev
->events
)
1036 } else if (mddev
->bitmap
) {
1037 /* if adding to array with a bitmap, then we can accept an
1038 * older device ... but not too old.
1040 if (ev1
< mddev
->bitmap
->events_cleared
)
1043 if (ev1
< mddev
->events
)
1044 /* just a hot-add of a new device, leave raid_disk at -1 */
1048 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1049 desc
= sb
->disks
+ rdev
->desc_nr
;
1051 if (desc
->state
& (1<<MD_DISK_FAULTY
))
1052 set_bit(Faulty
, &rdev
->flags
);
1053 else if (desc
->state
& (1<<MD_DISK_SYNC
) /* &&
1054 desc->raid_disk < mddev->raid_disks */) {
1055 set_bit(In_sync
, &rdev
->flags
);
1056 rdev
->raid_disk
= desc
->raid_disk
;
1057 } else if (desc
->state
& (1<<MD_DISK_ACTIVE
)) {
1058 /* active but not in sync implies recovery up to
1059 * reshape position. We don't know exactly where
1060 * that is, so set to zero for now */
1061 if (mddev
->minor_version
>= 91) {
1062 rdev
->recovery_offset
= 0;
1063 rdev
->raid_disk
= desc
->raid_disk
;
1066 if (desc
->state
& (1<<MD_DISK_WRITEMOSTLY
))
1067 set_bit(WriteMostly
, &rdev
->flags
);
1068 } else /* MULTIPATH are always insync */
1069 set_bit(In_sync
, &rdev
->flags
);
1074 * sync_super for 0.90.0
1076 static void super_90_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1080 int next_spare
= mddev
->raid_disks
;
1083 /* make rdev->sb match mddev data..
1086 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1087 * 3/ any empty disks < next_spare become removed
1089 * disks[0] gets initialised to REMOVED because
1090 * we cannot be sure from other fields if it has
1091 * been initialised or not.
1094 int active
=0, working
=0,failed
=0,spare
=0,nr_disks
=0;
1096 rdev
->sb_size
= MD_SB_BYTES
;
1098 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
1100 memset(sb
, 0, sizeof(*sb
));
1102 sb
->md_magic
= MD_SB_MAGIC
;
1103 sb
->major_version
= mddev
->major_version
;
1104 sb
->patch_version
= mddev
->patch_version
;
1105 sb
->gvalid_words
= 0; /* ignored */
1106 memcpy(&sb
->set_uuid0
, mddev
->uuid
+0, 4);
1107 memcpy(&sb
->set_uuid1
, mddev
->uuid
+4, 4);
1108 memcpy(&sb
->set_uuid2
, mddev
->uuid
+8, 4);
1109 memcpy(&sb
->set_uuid3
, mddev
->uuid
+12,4);
1111 sb
->ctime
= mddev
->ctime
;
1112 sb
->level
= mddev
->level
;
1113 sb
->size
= mddev
->dev_sectors
/ 2;
1114 sb
->raid_disks
= mddev
->raid_disks
;
1115 sb
->md_minor
= mddev
->md_minor
;
1116 sb
->not_persistent
= 0;
1117 sb
->utime
= mddev
->utime
;
1119 sb
->events_hi
= (mddev
->events
>>32);
1120 sb
->events_lo
= (u32
)mddev
->events
;
1122 if (mddev
->reshape_position
== MaxSector
)
1123 sb
->minor_version
= 90;
1125 sb
->minor_version
= 91;
1126 sb
->reshape_position
= mddev
->reshape_position
;
1127 sb
->new_level
= mddev
->new_level
;
1128 sb
->delta_disks
= mddev
->delta_disks
;
1129 sb
->new_layout
= mddev
->new_layout
;
1130 sb
->new_chunk
= mddev
->new_chunk_sectors
<< 9;
1132 mddev
->minor_version
= sb
->minor_version
;
1135 sb
->recovery_cp
= mddev
->recovery_cp
;
1136 sb
->cp_events_hi
= (mddev
->events
>>32);
1137 sb
->cp_events_lo
= (u32
)mddev
->events
;
1138 if (mddev
->recovery_cp
== MaxSector
)
1139 sb
->state
= (1<< MD_SB_CLEAN
);
1141 sb
->recovery_cp
= 0;
1143 sb
->layout
= mddev
->layout
;
1144 sb
->chunk_size
= mddev
->chunk_sectors
<< 9;
1146 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
)
1147 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
1149 sb
->disks
[0].state
= (1<<MD_DISK_REMOVED
);
1150 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
1153 int is_active
= test_bit(In_sync
, &rdev2
->flags
);
1155 if (rdev2
->raid_disk
>= 0 &&
1156 sb
->minor_version
>= 91)
1157 /* we have nowhere to store the recovery_offset,
1158 * but if it is not below the reshape_position,
1159 * we can piggy-back on that.
1162 if (rdev2
->raid_disk
< 0 ||
1163 test_bit(Faulty
, &rdev2
->flags
))
1166 desc_nr
= rdev2
->raid_disk
;
1168 desc_nr
= next_spare
++;
1169 rdev2
->desc_nr
= desc_nr
;
1170 d
= &sb
->disks
[rdev2
->desc_nr
];
1172 d
->number
= rdev2
->desc_nr
;
1173 d
->major
= MAJOR(rdev2
->bdev
->bd_dev
);
1174 d
->minor
= MINOR(rdev2
->bdev
->bd_dev
);
1176 d
->raid_disk
= rdev2
->raid_disk
;
1178 d
->raid_disk
= rdev2
->desc_nr
; /* compatibility */
1179 if (test_bit(Faulty
, &rdev2
->flags
))
1180 d
->state
= (1<<MD_DISK_FAULTY
);
1181 else if (is_active
) {
1182 d
->state
= (1<<MD_DISK_ACTIVE
);
1183 if (test_bit(In_sync
, &rdev2
->flags
))
1184 d
->state
|= (1<<MD_DISK_SYNC
);
1192 if (test_bit(WriteMostly
, &rdev2
->flags
))
1193 d
->state
|= (1<<MD_DISK_WRITEMOSTLY
);
1195 /* now set the "removed" and "faulty" bits on any missing devices */
1196 for (i
=0 ; i
< mddev
->raid_disks
; i
++) {
1197 mdp_disk_t
*d
= &sb
->disks
[i
];
1198 if (d
->state
== 0 && d
->number
== 0) {
1201 d
->state
= (1<<MD_DISK_REMOVED
);
1202 d
->state
|= (1<<MD_DISK_FAULTY
);
1206 sb
->nr_disks
= nr_disks
;
1207 sb
->active_disks
= active
;
1208 sb
->working_disks
= working
;
1209 sb
->failed_disks
= failed
;
1210 sb
->spare_disks
= spare
;
1212 sb
->this_disk
= sb
->disks
[rdev
->desc_nr
];
1213 sb
->sb_csum
= calc_sb_csum(sb
);
1217 * rdev_size_change for 0.90.0
1219 static unsigned long long
1220 super_90_rdev_size_change(mdk_rdev_t
*rdev
, sector_t num_sectors
)
1222 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1223 return 0; /* component must fit device */
1224 if (rdev
->mddev
->bitmap_info
.offset
)
1225 return 0; /* can't move bitmap */
1226 rdev
->sb_start
= calc_dev_sboffset(rdev
->bdev
);
1227 if (!num_sectors
|| num_sectors
> rdev
->sb_start
)
1228 num_sectors
= rdev
->sb_start
;
1229 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1231 md_super_wait(rdev
->mddev
);
1232 return num_sectors
/ 2; /* kB for sysfs */
1237 * version 1 superblock
1240 static __le32
calc_sb_1_csum(struct mdp_superblock_1
* sb
)
1244 unsigned long long newcsum
;
1245 int size
= 256 + le32_to_cpu(sb
->max_dev
)*2;
1246 __le32
*isuper
= (__le32
*)sb
;
1249 disk_csum
= sb
->sb_csum
;
1252 for (i
=0; size
>=4; size
-= 4 )
1253 newcsum
+= le32_to_cpu(*isuper
++);
1256 newcsum
+= le16_to_cpu(*(__le16
*) isuper
);
1258 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
1259 sb
->sb_csum
= disk_csum
;
1260 return cpu_to_le32(csum
);
1263 static int super_1_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
1265 struct mdp_superblock_1
*sb
;
1268 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1272 * Calculate the position of the superblock in 512byte sectors.
1273 * It is always aligned to a 4K boundary and
1274 * depeding on minor_version, it can be:
1275 * 0: At least 8K, but less than 12K, from end of device
1276 * 1: At start of device
1277 * 2: 4K from start of device.
1279 switch(minor_version
) {
1281 sb_start
= rdev
->bdev
->bd_inode
->i_size
>> 9;
1283 sb_start
&= ~(sector_t
)(4*2-1);
1294 rdev
->sb_start
= sb_start
;
1296 /* superblock is rarely larger than 1K, but it can be larger,
1297 * and it is safe to read 4k, so we do that
1299 ret
= read_disk_sb(rdev
, 4096);
1300 if (ret
) return ret
;
1303 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1305 if (sb
->magic
!= cpu_to_le32(MD_SB_MAGIC
) ||
1306 sb
->major_version
!= cpu_to_le32(1) ||
1307 le32_to_cpu(sb
->max_dev
) > (4096-256)/2 ||
1308 le64_to_cpu(sb
->super_offset
) != rdev
->sb_start
||
1309 (le32_to_cpu(sb
->feature_map
) & ~MD_FEATURE_ALL
) != 0)
1312 if (calc_sb_1_csum(sb
) != sb
->sb_csum
) {
1313 printk("md: invalid superblock checksum on %s\n",
1314 bdevname(rdev
->bdev
,b
));
1317 if (le64_to_cpu(sb
->data_size
) < 10) {
1318 printk("md: data_size too small on %s\n",
1319 bdevname(rdev
->bdev
,b
));
1323 rdev
->preferred_minor
= 0xffff;
1324 rdev
->data_offset
= le64_to_cpu(sb
->data_offset
);
1325 atomic_set(&rdev
->corrected_errors
, le32_to_cpu(sb
->cnt_corrected_read
));
1327 rdev
->sb_size
= le32_to_cpu(sb
->max_dev
) * 2 + 256;
1328 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1329 if (rdev
->sb_size
& bmask
)
1330 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1333 && rdev
->data_offset
< sb_start
+ (rdev
->sb_size
/512))
1336 if (sb
->level
== cpu_to_le32(LEVEL_MULTIPATH
))
1339 rdev
->desc_nr
= le32_to_cpu(sb
->dev_number
);
1345 struct mdp_superblock_1
*refsb
=
1346 (struct mdp_superblock_1
*)page_address(refdev
->sb_page
);
1348 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16) != 0 ||
1349 sb
->level
!= refsb
->level
||
1350 sb
->layout
!= refsb
->layout
||
1351 sb
->chunksize
!= refsb
->chunksize
) {
1352 printk(KERN_WARNING
"md: %s has strangely different"
1353 " superblock to %s\n",
1354 bdevname(rdev
->bdev
,b
),
1355 bdevname(refdev
->bdev
,b2
));
1358 ev1
= le64_to_cpu(sb
->events
);
1359 ev2
= le64_to_cpu(refsb
->events
);
1367 rdev
->sectors
= (rdev
->bdev
->bd_inode
->i_size
>> 9) -
1368 le64_to_cpu(sb
->data_offset
);
1370 rdev
->sectors
= rdev
->sb_start
;
1371 if (rdev
->sectors
< le64_to_cpu(sb
->data_size
))
1373 rdev
->sectors
= le64_to_cpu(sb
->data_size
);
1374 if (le64_to_cpu(sb
->size
) > rdev
->sectors
)
1379 static int super_1_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1381 struct mdp_superblock_1
*sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1382 __u64 ev1
= le64_to_cpu(sb
->events
);
1384 rdev
->raid_disk
= -1;
1385 clear_bit(Faulty
, &rdev
->flags
);
1386 clear_bit(In_sync
, &rdev
->flags
);
1387 clear_bit(WriteMostly
, &rdev
->flags
);
1388 clear_bit(BarriersNotsupp
, &rdev
->flags
);
1390 if (mddev
->raid_disks
== 0) {
1391 mddev
->major_version
= 1;
1392 mddev
->patch_version
= 0;
1393 mddev
->external
= 0;
1394 mddev
->chunk_sectors
= le32_to_cpu(sb
->chunksize
);
1395 mddev
->ctime
= le64_to_cpu(sb
->ctime
) & ((1ULL << 32)-1);
1396 mddev
->utime
= le64_to_cpu(sb
->utime
) & ((1ULL << 32)-1);
1397 mddev
->level
= le32_to_cpu(sb
->level
);
1398 mddev
->clevel
[0] = 0;
1399 mddev
->layout
= le32_to_cpu(sb
->layout
);
1400 mddev
->raid_disks
= le32_to_cpu(sb
->raid_disks
);
1401 mddev
->dev_sectors
= le64_to_cpu(sb
->size
);
1402 mddev
->events
= ev1
;
1403 mddev
->bitmap_info
.offset
= 0;
1404 mddev
->bitmap_info
.default_offset
= 1024 >> 9;
1406 mddev
->recovery_cp
= le64_to_cpu(sb
->resync_offset
);
1407 memcpy(mddev
->uuid
, sb
->set_uuid
, 16);
1409 mddev
->max_disks
= (4096-256)/2;
1411 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BITMAP_OFFSET
) &&
1412 mddev
->bitmap_info
.file
== NULL
)
1413 mddev
->bitmap_info
.offset
=
1414 (__s32
)le32_to_cpu(sb
->bitmap_offset
);
1416 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
1417 mddev
->reshape_position
= le64_to_cpu(sb
->reshape_position
);
1418 mddev
->delta_disks
= le32_to_cpu(sb
->delta_disks
);
1419 mddev
->new_level
= le32_to_cpu(sb
->new_level
);
1420 mddev
->new_layout
= le32_to_cpu(sb
->new_layout
);
1421 mddev
->new_chunk_sectors
= le32_to_cpu(sb
->new_chunk
);
1423 mddev
->reshape_position
= MaxSector
;
1424 mddev
->delta_disks
= 0;
1425 mddev
->new_level
= mddev
->level
;
1426 mddev
->new_layout
= mddev
->layout
;
1427 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1430 } else if (mddev
->pers
== NULL
) {
1431 /* Insist of good event counter while assembling */
1433 if (ev1
< mddev
->events
)
1435 } else if (mddev
->bitmap
) {
1436 /* If adding to array with a bitmap, then we can accept an
1437 * older device, but not too old.
1439 if (ev1
< mddev
->bitmap
->events_cleared
)
1442 if (ev1
< mddev
->events
)
1443 /* just a hot-add of a new device, leave raid_disk at -1 */
1446 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1448 if (rdev
->desc_nr
< 0 ||
1449 rdev
->desc_nr
>= le32_to_cpu(sb
->max_dev
)) {
1453 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
1455 case 0xffff: /* spare */
1457 case 0xfffe: /* faulty */
1458 set_bit(Faulty
, &rdev
->flags
);
1461 if ((le32_to_cpu(sb
->feature_map
) &
1462 MD_FEATURE_RECOVERY_OFFSET
))
1463 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
1465 set_bit(In_sync
, &rdev
->flags
);
1466 rdev
->raid_disk
= role
;
1469 if (sb
->devflags
& WriteMostly1
)
1470 set_bit(WriteMostly
, &rdev
->flags
);
1471 } else /* MULTIPATH are always insync */
1472 set_bit(In_sync
, &rdev
->flags
);
1477 static void super_1_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1479 struct mdp_superblock_1
*sb
;
1482 /* make rdev->sb match mddev and rdev data. */
1484 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1486 sb
->feature_map
= 0;
1488 sb
->recovery_offset
= cpu_to_le64(0);
1489 memset(sb
->pad1
, 0, sizeof(sb
->pad1
));
1490 memset(sb
->pad2
, 0, sizeof(sb
->pad2
));
1491 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
1493 sb
->utime
= cpu_to_le64((__u64
)mddev
->utime
);
1494 sb
->events
= cpu_to_le64(mddev
->events
);
1496 sb
->resync_offset
= cpu_to_le64(mddev
->recovery_cp
);
1498 sb
->resync_offset
= cpu_to_le64(0);
1500 sb
->cnt_corrected_read
= cpu_to_le32(atomic_read(&rdev
->corrected_errors
));
1502 sb
->raid_disks
= cpu_to_le32(mddev
->raid_disks
);
1503 sb
->size
= cpu_to_le64(mddev
->dev_sectors
);
1504 sb
->chunksize
= cpu_to_le32(mddev
->chunk_sectors
);
1505 sb
->level
= cpu_to_le32(mddev
->level
);
1506 sb
->layout
= cpu_to_le32(mddev
->layout
);
1508 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
) {
1509 sb
->bitmap_offset
= cpu_to_le32((__u32
)mddev
->bitmap_info
.offset
);
1510 sb
->feature_map
= cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
);
1513 if (rdev
->raid_disk
>= 0 &&
1514 !test_bit(In_sync
, &rdev
->flags
)) {
1516 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET
);
1517 sb
->recovery_offset
=
1518 cpu_to_le64(rdev
->recovery_offset
);
1521 if (mddev
->reshape_position
!= MaxSector
) {
1522 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE
);
1523 sb
->reshape_position
= cpu_to_le64(mddev
->reshape_position
);
1524 sb
->new_layout
= cpu_to_le32(mddev
->new_layout
);
1525 sb
->delta_disks
= cpu_to_le32(mddev
->delta_disks
);
1526 sb
->new_level
= cpu_to_le32(mddev
->new_level
);
1527 sb
->new_chunk
= cpu_to_le32(mddev
->new_chunk_sectors
);
1531 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
)
1532 if (rdev2
->desc_nr
+1 > max_dev
)
1533 max_dev
= rdev2
->desc_nr
+1;
1535 if (max_dev
> le32_to_cpu(sb
->max_dev
)) {
1537 sb
->max_dev
= cpu_to_le32(max_dev
);
1538 rdev
->sb_size
= max_dev
* 2 + 256;
1539 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1540 if (rdev
->sb_size
& bmask
)
1541 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1543 for (i
=0; i
<max_dev
;i
++)
1544 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1546 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
1548 if (test_bit(Faulty
, &rdev2
->flags
))
1549 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1550 else if (test_bit(In_sync
, &rdev2
->flags
))
1551 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1552 else if (rdev2
->raid_disk
>= 0)
1553 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1555 sb
->dev_roles
[i
] = cpu_to_le16(0xffff);
1558 sb
->sb_csum
= calc_sb_1_csum(sb
);
1561 static unsigned long long
1562 super_1_rdev_size_change(mdk_rdev_t
*rdev
, sector_t num_sectors
)
1564 struct mdp_superblock_1
*sb
;
1565 sector_t max_sectors
;
1566 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1567 return 0; /* component must fit device */
1568 if (rdev
->sb_start
< rdev
->data_offset
) {
1569 /* minor versions 1 and 2; superblock before data */
1570 max_sectors
= rdev
->bdev
->bd_inode
->i_size
>> 9;
1571 max_sectors
-= rdev
->data_offset
;
1572 if (!num_sectors
|| num_sectors
> max_sectors
)
1573 num_sectors
= max_sectors
;
1574 } else if (rdev
->mddev
->bitmap_info
.offset
) {
1575 /* minor version 0 with bitmap we can't move */
1578 /* minor version 0; superblock after data */
1580 sb_start
= (rdev
->bdev
->bd_inode
->i_size
>> 9) - 8*2;
1581 sb_start
&= ~(sector_t
)(4*2 - 1);
1582 max_sectors
= rdev
->sectors
+ sb_start
- rdev
->sb_start
;
1583 if (!num_sectors
|| num_sectors
> max_sectors
)
1584 num_sectors
= max_sectors
;
1585 rdev
->sb_start
= sb_start
;
1587 sb
= (struct mdp_superblock_1
*) page_address(rdev
->sb_page
);
1588 sb
->data_size
= cpu_to_le64(num_sectors
);
1589 sb
->super_offset
= rdev
->sb_start
;
1590 sb
->sb_csum
= calc_sb_1_csum(sb
);
1591 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1593 md_super_wait(rdev
->mddev
);
1594 return num_sectors
/ 2; /* kB for sysfs */
1597 static struct super_type super_types
[] = {
1600 .owner
= THIS_MODULE
,
1601 .load_super
= super_90_load
,
1602 .validate_super
= super_90_validate
,
1603 .sync_super
= super_90_sync
,
1604 .rdev_size_change
= super_90_rdev_size_change
,
1608 .owner
= THIS_MODULE
,
1609 .load_super
= super_1_load
,
1610 .validate_super
= super_1_validate
,
1611 .sync_super
= super_1_sync
,
1612 .rdev_size_change
= super_1_rdev_size_change
,
1616 static int match_mddev_units(mddev_t
*mddev1
, mddev_t
*mddev2
)
1618 mdk_rdev_t
*rdev
, *rdev2
;
1621 rdev_for_each_rcu(rdev
, mddev1
)
1622 rdev_for_each_rcu(rdev2
, mddev2
)
1623 if (rdev
->bdev
->bd_contains
==
1624 rdev2
->bdev
->bd_contains
) {
1632 static LIST_HEAD(pending_raid_disks
);
1635 * Try to register data integrity profile for an mddev
1637 * This is called when an array is started and after a disk has been kicked
1638 * from the array. It only succeeds if all working and active component devices
1639 * are integrity capable with matching profiles.
1641 int md_integrity_register(mddev_t
*mddev
)
1643 mdk_rdev_t
*rdev
, *reference
= NULL
;
1645 if (list_empty(&mddev
->disks
))
1646 return 0; /* nothing to do */
1647 if (blk_get_integrity(mddev
->gendisk
))
1648 return 0; /* already registered */
1649 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
1650 /* skip spares and non-functional disks */
1651 if (test_bit(Faulty
, &rdev
->flags
))
1653 if (rdev
->raid_disk
< 0)
1656 * If at least one rdev is not integrity capable, we can not
1657 * enable data integrity for the md device.
1659 if (!bdev_get_integrity(rdev
->bdev
))
1662 /* Use the first rdev as the reference */
1666 /* does this rdev's profile match the reference profile? */
1667 if (blk_integrity_compare(reference
->bdev
->bd_disk
,
1668 rdev
->bdev
->bd_disk
) < 0)
1672 * All component devices are integrity capable and have matching
1673 * profiles, register the common profile for the md device.
1675 if (blk_integrity_register(mddev
->gendisk
,
1676 bdev_get_integrity(reference
->bdev
)) != 0) {
1677 printk(KERN_ERR
"md: failed to register integrity for %s\n",
1681 printk(KERN_NOTICE
"md: data integrity on %s enabled\n",
1685 EXPORT_SYMBOL(md_integrity_register
);
1687 /* Disable data integrity if non-capable/non-matching disk is being added */
1688 void md_integrity_add_rdev(mdk_rdev_t
*rdev
, mddev_t
*mddev
)
1690 struct blk_integrity
*bi_rdev
= bdev_get_integrity(rdev
->bdev
);
1691 struct blk_integrity
*bi_mddev
= blk_get_integrity(mddev
->gendisk
);
1693 if (!bi_mddev
) /* nothing to do */
1695 if (rdev
->raid_disk
< 0) /* skip spares */
1697 if (bi_rdev
&& blk_integrity_compare(mddev
->gendisk
,
1698 rdev
->bdev
->bd_disk
) >= 0)
1700 printk(KERN_NOTICE
"disabling data integrity on %s\n", mdname(mddev
));
1701 blk_integrity_unregister(mddev
->gendisk
);
1703 EXPORT_SYMBOL(md_integrity_add_rdev
);
1705 static int bind_rdev_to_array(mdk_rdev_t
* rdev
, mddev_t
* mddev
)
1707 char b
[BDEVNAME_SIZE
];
1717 /* prevent duplicates */
1718 if (find_rdev(mddev
, rdev
->bdev
->bd_dev
))
1721 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1722 if (rdev
->sectors
&& (mddev
->dev_sectors
== 0 ||
1723 rdev
->sectors
< mddev
->dev_sectors
)) {
1725 /* Cannot change size, so fail
1726 * If mddev->level <= 0, then we don't care
1727 * about aligning sizes (e.g. linear)
1729 if (mddev
->level
> 0)
1732 mddev
->dev_sectors
= rdev
->sectors
;
1735 /* Verify rdev->desc_nr is unique.
1736 * If it is -1, assign a free number, else
1737 * check number is not in use
1739 if (rdev
->desc_nr
< 0) {
1741 if (mddev
->pers
) choice
= mddev
->raid_disks
;
1742 while (find_rdev_nr(mddev
, choice
))
1744 rdev
->desc_nr
= choice
;
1746 if (find_rdev_nr(mddev
, rdev
->desc_nr
))
1749 if (mddev
->max_disks
&& rdev
->desc_nr
>= mddev
->max_disks
) {
1750 printk(KERN_WARNING
"md: %s: array is limited to %d devices\n",
1751 mdname(mddev
), mddev
->max_disks
);
1754 bdevname(rdev
->bdev
,b
);
1755 while ( (s
=strchr(b
, '/')) != NULL
)
1758 rdev
->mddev
= mddev
;
1759 printk(KERN_INFO
"md: bind<%s>\n", b
);
1761 if ((err
= kobject_add(&rdev
->kobj
, &mddev
->kobj
, "dev-%s", b
)))
1764 ko
= &part_to_dev(rdev
->bdev
->bd_part
)->kobj
;
1765 if ((err
= sysfs_create_link(&rdev
->kobj
, ko
, "block"))) {
1766 kobject_del(&rdev
->kobj
);
1769 rdev
->sysfs_state
= sysfs_get_dirent(rdev
->kobj
.sd
, "state");
1771 list_add_rcu(&rdev
->same_set
, &mddev
->disks
);
1772 bd_claim_by_disk(rdev
->bdev
, rdev
->bdev
->bd_holder
, mddev
->gendisk
);
1774 /* May as well allow recovery to be retried once */
1775 mddev
->recovery_disabled
= 0;
1780 printk(KERN_WARNING
"md: failed to register dev-%s for %s\n",
1785 static void md_delayed_delete(struct work_struct
*ws
)
1787 mdk_rdev_t
*rdev
= container_of(ws
, mdk_rdev_t
, del_work
);
1788 kobject_del(&rdev
->kobj
);
1789 kobject_put(&rdev
->kobj
);
1792 static void unbind_rdev_from_array(mdk_rdev_t
* rdev
)
1794 char b
[BDEVNAME_SIZE
];
1799 bd_release_from_disk(rdev
->bdev
, rdev
->mddev
->gendisk
);
1800 list_del_rcu(&rdev
->same_set
);
1801 printk(KERN_INFO
"md: unbind<%s>\n", bdevname(rdev
->bdev
,b
));
1803 sysfs_remove_link(&rdev
->kobj
, "block");
1804 sysfs_put(rdev
->sysfs_state
);
1805 rdev
->sysfs_state
= NULL
;
1806 /* We need to delay this, otherwise we can deadlock when
1807 * writing to 'remove' to "dev/state". We also need
1808 * to delay it due to rcu usage.
1811 INIT_WORK(&rdev
->del_work
, md_delayed_delete
);
1812 kobject_get(&rdev
->kobj
);
1813 schedule_work(&rdev
->del_work
);
1817 * prevent the device from being mounted, repartitioned or
1818 * otherwise reused by a RAID array (or any other kernel
1819 * subsystem), by bd_claiming the device.
1821 static int lock_rdev(mdk_rdev_t
*rdev
, dev_t dev
, int shared
)
1824 struct block_device
*bdev
;
1825 char b
[BDEVNAME_SIZE
];
1827 bdev
= open_by_devnum(dev
, FMODE_READ
|FMODE_WRITE
);
1829 printk(KERN_ERR
"md: could not open %s.\n",
1830 __bdevname(dev
, b
));
1831 return PTR_ERR(bdev
);
1833 err
= bd_claim(bdev
, shared
? (mdk_rdev_t
*)lock_rdev
: rdev
);
1835 printk(KERN_ERR
"md: could not bd_claim %s.\n",
1837 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
);
1841 set_bit(AllReserved
, &rdev
->flags
);
1846 static void unlock_rdev(mdk_rdev_t
*rdev
)
1848 struct block_device
*bdev
= rdev
->bdev
;
1853 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
);
1856 void md_autodetect_dev(dev_t dev
);
1858 static void export_rdev(mdk_rdev_t
* rdev
)
1860 char b
[BDEVNAME_SIZE
];
1861 printk(KERN_INFO
"md: export_rdev(%s)\n",
1862 bdevname(rdev
->bdev
,b
));
1867 if (test_bit(AutoDetected
, &rdev
->flags
))
1868 md_autodetect_dev(rdev
->bdev
->bd_dev
);
1871 kobject_put(&rdev
->kobj
);
1874 static void kick_rdev_from_array(mdk_rdev_t
* rdev
)
1876 unbind_rdev_from_array(rdev
);
1880 static void export_array(mddev_t
*mddev
)
1882 mdk_rdev_t
*rdev
, *tmp
;
1884 rdev_for_each(rdev
, tmp
, mddev
) {
1889 kick_rdev_from_array(rdev
);
1891 if (!list_empty(&mddev
->disks
))
1893 mddev
->raid_disks
= 0;
1894 mddev
->major_version
= 0;
1897 static void print_desc(mdp_disk_t
*desc
)
1899 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc
->number
,
1900 desc
->major
,desc
->minor
,desc
->raid_disk
,desc
->state
);
1903 static void print_sb_90(mdp_super_t
*sb
)
1908 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1909 sb
->major_version
, sb
->minor_version
, sb
->patch_version
,
1910 sb
->set_uuid0
, sb
->set_uuid1
, sb
->set_uuid2
, sb
->set_uuid3
,
1912 printk(KERN_INFO
"md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1913 sb
->level
, sb
->size
, sb
->nr_disks
, sb
->raid_disks
,
1914 sb
->md_minor
, sb
->layout
, sb
->chunk_size
);
1915 printk(KERN_INFO
"md: UT:%08x ST:%d AD:%d WD:%d"
1916 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1917 sb
->utime
, sb
->state
, sb
->active_disks
, sb
->working_disks
,
1918 sb
->failed_disks
, sb
->spare_disks
,
1919 sb
->sb_csum
, (unsigned long)sb
->events_lo
);
1922 for (i
= 0; i
< MD_SB_DISKS
; i
++) {
1925 desc
= sb
->disks
+ i
;
1926 if (desc
->number
|| desc
->major
|| desc
->minor
||
1927 desc
->raid_disk
|| (desc
->state
&& (desc
->state
!= 4))) {
1928 printk(" D %2d: ", i
);
1932 printk(KERN_INFO
"md: THIS: ");
1933 print_desc(&sb
->this_disk
);
1936 static void print_sb_1(struct mdp_superblock_1
*sb
)
1940 uuid
= sb
->set_uuid
;
1942 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
1943 "md: Name: \"%s\" CT:%llu\n",
1944 le32_to_cpu(sb
->major_version
),
1945 le32_to_cpu(sb
->feature_map
),
1948 (unsigned long long)le64_to_cpu(sb
->ctime
)
1949 & MD_SUPERBLOCK_1_TIME_SEC_MASK
);
1951 uuid
= sb
->device_uuid
;
1953 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
1955 "md: Dev:%08x UUID: %pU\n"
1956 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
1957 "md: (MaxDev:%u) \n",
1958 le32_to_cpu(sb
->level
),
1959 (unsigned long long)le64_to_cpu(sb
->size
),
1960 le32_to_cpu(sb
->raid_disks
),
1961 le32_to_cpu(sb
->layout
),
1962 le32_to_cpu(sb
->chunksize
),
1963 (unsigned long long)le64_to_cpu(sb
->data_offset
),
1964 (unsigned long long)le64_to_cpu(sb
->data_size
),
1965 (unsigned long long)le64_to_cpu(sb
->super_offset
),
1966 (unsigned long long)le64_to_cpu(sb
->recovery_offset
),
1967 le32_to_cpu(sb
->dev_number
),
1970 (unsigned long long)le64_to_cpu(sb
->utime
) & MD_SUPERBLOCK_1_TIME_SEC_MASK
,
1971 (unsigned long long)le64_to_cpu(sb
->events
),
1972 (unsigned long long)le64_to_cpu(sb
->resync_offset
),
1973 le32_to_cpu(sb
->sb_csum
),
1974 le32_to_cpu(sb
->max_dev
)
1978 static void print_rdev(mdk_rdev_t
*rdev
, int major_version
)
1980 char b
[BDEVNAME_SIZE
];
1981 printk(KERN_INFO
"md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
1982 bdevname(rdev
->bdev
, b
), (unsigned long long)rdev
->sectors
,
1983 test_bit(Faulty
, &rdev
->flags
), test_bit(In_sync
, &rdev
->flags
),
1985 if (rdev
->sb_loaded
) {
1986 printk(KERN_INFO
"md: rdev superblock (MJ:%d):\n", major_version
);
1987 switch (major_version
) {
1989 print_sb_90((mdp_super_t
*)page_address(rdev
->sb_page
));
1992 print_sb_1((struct mdp_superblock_1
*)page_address(rdev
->sb_page
));
1996 printk(KERN_INFO
"md: no rdev superblock!\n");
1999 static void md_print_devices(void)
2001 struct list_head
*tmp
;
2004 char b
[BDEVNAME_SIZE
];
2007 printk("md: **********************************\n");
2008 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2009 printk("md: **********************************\n");
2010 for_each_mddev(mddev
, tmp
) {
2013 bitmap_print_sb(mddev
->bitmap
);
2015 printk("%s: ", mdname(mddev
));
2016 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
2017 printk("<%s>", bdevname(rdev
->bdev
,b
));
2020 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
2021 print_rdev(rdev
, mddev
->major_version
);
2023 printk("md: **********************************\n");
2028 static void sync_sbs(mddev_t
* mddev
, int nospares
)
2030 /* Update each superblock (in-memory image), but
2031 * if we are allowed to, skip spares which already
2032 * have the right event counter, or have one earlier
2033 * (which would mean they aren't being marked as dirty
2034 * with the rest of the array)
2038 /* First make sure individual recovery_offsets are correct */
2039 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2040 if (rdev
->raid_disk
>= 0 &&
2041 !test_bit(In_sync
, &rdev
->flags
) &&
2042 mddev
->curr_resync_completed
> rdev
->recovery_offset
)
2043 rdev
->recovery_offset
= mddev
->curr_resync_completed
;
2046 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2047 if (rdev
->sb_events
== mddev
->events
||
2049 rdev
->raid_disk
< 0 &&
2050 (rdev
->sb_events
&1)==0 &&
2051 rdev
->sb_events
+1 == mddev
->events
)) {
2052 /* Don't update this superblock */
2053 rdev
->sb_loaded
= 2;
2055 super_types
[mddev
->major_version
].
2056 sync_super(mddev
, rdev
);
2057 rdev
->sb_loaded
= 1;
2062 static void md_update_sb(mddev_t
* mddev
, int force_change
)
2068 mddev
->utime
= get_seconds();
2069 if (mddev
->external
)
2072 spin_lock_irq(&mddev
->write_lock
);
2074 set_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
2075 if (test_and_clear_bit(MD_CHANGE_DEVS
, &mddev
->flags
))
2077 if (test_and_clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
))
2078 /* just a clean<-> dirty transition, possibly leave spares alone,
2079 * though if events isn't the right even/odd, we will have to do
2085 if (mddev
->degraded
)
2086 /* If the array is degraded, then skipping spares is both
2087 * dangerous and fairly pointless.
2088 * Dangerous because a device that was removed from the array
2089 * might have a event_count that still looks up-to-date,
2090 * so it can be re-added without a resync.
2091 * Pointless because if there are any spares to skip,
2092 * then a recovery will happen and soon that array won't
2093 * be degraded any more and the spare can go back to sleep then.
2097 sync_req
= mddev
->in_sync
;
2099 /* If this is just a dirty<->clean transition, and the array is clean
2100 * and 'events' is odd, we can roll back to the previous clean state */
2102 && (mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
2103 && (mddev
->events
& 1)
2104 && mddev
->events
!= 1)
2107 /* otherwise we have to go forward and ... */
2109 if (!mddev
->in_sync
|| mddev
->recovery_cp
!= MaxSector
) { /* not clean */
2110 /* .. if the array isn't clean, an 'even' event must also go
2112 if ((mddev
->events
&1)==0)
2115 /* otherwise an 'odd' event must go to spares */
2116 if ((mddev
->events
&1))
2121 if (!mddev
->events
) {
2123 * oops, this 64-bit counter should never wrap.
2124 * Either we are in around ~1 trillion A.C., assuming
2125 * 1 reboot per second, or we have a bug:
2132 * do not write anything to disk if using
2133 * nonpersistent superblocks
2135 if (!mddev
->persistent
) {
2136 if (!mddev
->external
)
2137 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
2139 spin_unlock_irq(&mddev
->write_lock
);
2140 wake_up(&mddev
->sb_wait
);
2143 sync_sbs(mddev
, nospares
);
2144 spin_unlock_irq(&mddev
->write_lock
);
2147 "md: updating %s RAID superblock on device (in sync %d)\n",
2148 mdname(mddev
),mddev
->in_sync
);
2150 bitmap_update_sb(mddev
->bitmap
);
2151 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2152 char b
[BDEVNAME_SIZE
];
2153 dprintk(KERN_INFO
"md: ");
2154 if (rdev
->sb_loaded
!= 1)
2155 continue; /* no noise on spare devices */
2156 if (test_bit(Faulty
, &rdev
->flags
))
2157 dprintk("(skipping faulty ");
2159 dprintk("%s ", bdevname(rdev
->bdev
,b
));
2160 if (!test_bit(Faulty
, &rdev
->flags
)) {
2161 md_super_write(mddev
,rdev
,
2162 rdev
->sb_start
, rdev
->sb_size
,
2164 dprintk(KERN_INFO
"(write) %s's sb offset: %llu\n",
2165 bdevname(rdev
->bdev
,b
),
2166 (unsigned long long)rdev
->sb_start
);
2167 rdev
->sb_events
= mddev
->events
;
2171 if (mddev
->level
== LEVEL_MULTIPATH
)
2172 /* only need to write one superblock... */
2175 md_super_wait(mddev
);
2176 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
2178 spin_lock_irq(&mddev
->write_lock
);
2179 if (mddev
->in_sync
!= sync_req
||
2180 test_bit(MD_CHANGE_DEVS
, &mddev
->flags
)) {
2181 /* have to write it out again */
2182 spin_unlock_irq(&mddev
->write_lock
);
2185 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
2186 spin_unlock_irq(&mddev
->write_lock
);
2187 wake_up(&mddev
->sb_wait
);
2188 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
2189 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
2193 /* words written to sysfs files may, or may not, be \n terminated.
2194 * We want to accept with case. For this we use cmd_match.
2196 static int cmd_match(const char *cmd
, const char *str
)
2198 /* See if cmd, written into a sysfs file, matches
2199 * str. They must either be the same, or cmd can
2200 * have a trailing newline
2202 while (*cmd
&& *str
&& *cmd
== *str
) {
2213 struct rdev_sysfs_entry
{
2214 struct attribute attr
;
2215 ssize_t (*show
)(mdk_rdev_t
*, char *);
2216 ssize_t (*store
)(mdk_rdev_t
*, const char *, size_t);
2220 state_show(mdk_rdev_t
*rdev
, char *page
)
2225 if (test_bit(Faulty
, &rdev
->flags
)) {
2226 len
+= sprintf(page
+len
, "%sfaulty",sep
);
2229 if (test_bit(In_sync
, &rdev
->flags
)) {
2230 len
+= sprintf(page
+len
, "%sin_sync",sep
);
2233 if (test_bit(WriteMostly
, &rdev
->flags
)) {
2234 len
+= sprintf(page
+len
, "%swrite_mostly",sep
);
2237 if (test_bit(Blocked
, &rdev
->flags
)) {
2238 len
+= sprintf(page
+len
, "%sblocked", sep
);
2241 if (!test_bit(Faulty
, &rdev
->flags
) &&
2242 !test_bit(In_sync
, &rdev
->flags
)) {
2243 len
+= sprintf(page
+len
, "%sspare", sep
);
2246 return len
+sprintf(page
+len
, "\n");
2250 state_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2253 * faulty - simulates and error
2254 * remove - disconnects the device
2255 * writemostly - sets write_mostly
2256 * -writemostly - clears write_mostly
2257 * blocked - sets the Blocked flag
2258 * -blocked - clears the Blocked flag
2259 * insync - sets Insync providing device isn't active
2262 if (cmd_match(buf
, "faulty") && rdev
->mddev
->pers
) {
2263 md_error(rdev
->mddev
, rdev
);
2265 } else if (cmd_match(buf
, "remove")) {
2266 if (rdev
->raid_disk
>= 0)
2269 mddev_t
*mddev
= rdev
->mddev
;
2270 kick_rdev_from_array(rdev
);
2272 md_update_sb(mddev
, 1);
2273 md_new_event(mddev
);
2276 } else if (cmd_match(buf
, "writemostly")) {
2277 set_bit(WriteMostly
, &rdev
->flags
);
2279 } else if (cmd_match(buf
, "-writemostly")) {
2280 clear_bit(WriteMostly
, &rdev
->flags
);
2282 } else if (cmd_match(buf
, "blocked")) {
2283 set_bit(Blocked
, &rdev
->flags
);
2285 } else if (cmd_match(buf
, "-blocked")) {
2286 clear_bit(Blocked
, &rdev
->flags
);
2287 wake_up(&rdev
->blocked_wait
);
2288 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2289 md_wakeup_thread(rdev
->mddev
->thread
);
2292 } else if (cmd_match(buf
, "insync") && rdev
->raid_disk
== -1) {
2293 set_bit(In_sync
, &rdev
->flags
);
2296 if (!err
&& rdev
->sysfs_state
)
2297 sysfs_notify_dirent(rdev
->sysfs_state
);
2298 return err
? err
: len
;
2300 static struct rdev_sysfs_entry rdev_state
=
2301 __ATTR(state
, S_IRUGO
|S_IWUSR
, state_show
, state_store
);
2304 errors_show(mdk_rdev_t
*rdev
, char *page
)
2306 return sprintf(page
, "%d\n", atomic_read(&rdev
->corrected_errors
));
2310 errors_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2313 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2314 if (*buf
&& (*e
== 0 || *e
== '\n')) {
2315 atomic_set(&rdev
->corrected_errors
, n
);
2320 static struct rdev_sysfs_entry rdev_errors
=
2321 __ATTR(errors
, S_IRUGO
|S_IWUSR
, errors_show
, errors_store
);
2324 slot_show(mdk_rdev_t
*rdev
, char *page
)
2326 if (rdev
->raid_disk
< 0)
2327 return sprintf(page
, "none\n");
2329 return sprintf(page
, "%d\n", rdev
->raid_disk
);
2333 slot_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2338 int slot
= simple_strtoul(buf
, &e
, 10);
2339 if (strncmp(buf
, "none", 4)==0)
2341 else if (e
==buf
|| (*e
&& *e
!= '\n'))
2343 if (rdev
->mddev
->pers
&& slot
== -1) {
2344 /* Setting 'slot' on an active array requires also
2345 * updating the 'rd%d' link, and communicating
2346 * with the personality with ->hot_*_disk.
2347 * For now we only support removing
2348 * failed/spare devices. This normally happens automatically,
2349 * but not when the metadata is externally managed.
2351 if (rdev
->raid_disk
== -1)
2353 /* personality does all needed checks */
2354 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
2356 err
= rdev
->mddev
->pers
->
2357 hot_remove_disk(rdev
->mddev
, rdev
->raid_disk
);
2360 sprintf(nm
, "rd%d", rdev
->raid_disk
);
2361 sysfs_remove_link(&rdev
->mddev
->kobj
, nm
);
2362 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2363 md_wakeup_thread(rdev
->mddev
->thread
);
2364 } else if (rdev
->mddev
->pers
) {
2366 /* Activating a spare .. or possibly reactivating
2367 * if we ever get bitmaps working here.
2370 if (rdev
->raid_disk
!= -1)
2373 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
2376 list_for_each_entry(rdev2
, &rdev
->mddev
->disks
, same_set
)
2377 if (rdev2
->raid_disk
== slot
)
2380 rdev
->raid_disk
= slot
;
2381 if (test_bit(In_sync
, &rdev
->flags
))
2382 rdev
->saved_raid_disk
= slot
;
2384 rdev
->saved_raid_disk
= -1;
2385 err
= rdev
->mddev
->pers
->
2386 hot_add_disk(rdev
->mddev
, rdev
);
2388 rdev
->raid_disk
= -1;
2391 sysfs_notify_dirent(rdev
->sysfs_state
);
2392 sprintf(nm
, "rd%d", rdev
->raid_disk
);
2393 if (sysfs_create_link(&rdev
->mddev
->kobj
, &rdev
->kobj
, nm
))
2395 "md: cannot register "
2397 nm
, mdname(rdev
->mddev
));
2399 /* don't wakeup anyone, leave that to userspace. */
2401 if (slot
>= rdev
->mddev
->raid_disks
)
2403 rdev
->raid_disk
= slot
;
2404 /* assume it is working */
2405 clear_bit(Faulty
, &rdev
->flags
);
2406 clear_bit(WriteMostly
, &rdev
->flags
);
2407 set_bit(In_sync
, &rdev
->flags
);
2408 sysfs_notify_dirent(rdev
->sysfs_state
);
2414 static struct rdev_sysfs_entry rdev_slot
=
2415 __ATTR(slot
, S_IRUGO
|S_IWUSR
, slot_show
, slot_store
);
2418 offset_show(mdk_rdev_t
*rdev
, char *page
)
2420 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->data_offset
);
2424 offset_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2427 unsigned long long offset
= simple_strtoull(buf
, &e
, 10);
2428 if (e
==buf
|| (*e
&& *e
!= '\n'))
2430 if (rdev
->mddev
->pers
&& rdev
->raid_disk
>= 0)
2432 if (rdev
->sectors
&& rdev
->mddev
->external
)
2433 /* Must set offset before size, so overlap checks
2436 rdev
->data_offset
= offset
;
2440 static struct rdev_sysfs_entry rdev_offset
=
2441 __ATTR(offset
, S_IRUGO
|S_IWUSR
, offset_show
, offset_store
);
2444 rdev_size_show(mdk_rdev_t
*rdev
, char *page
)
2446 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->sectors
/ 2);
2449 static int overlaps(sector_t s1
, sector_t l1
, sector_t s2
, sector_t l2
)
2451 /* check if two start/length pairs overlap */
2459 static int strict_blocks_to_sectors(const char *buf
, sector_t
*sectors
)
2461 unsigned long long blocks
;
2464 if (strict_strtoull(buf
, 10, &blocks
) < 0)
2467 if (blocks
& 1ULL << (8 * sizeof(blocks
) - 1))
2468 return -EINVAL
; /* sector conversion overflow */
2471 if (new != blocks
* 2)
2472 return -EINVAL
; /* unsigned long long to sector_t overflow */
2479 rdev_size_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2481 mddev_t
*my_mddev
= rdev
->mddev
;
2482 sector_t oldsectors
= rdev
->sectors
;
2485 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
2487 if (my_mddev
->pers
&& rdev
->raid_disk
>= 0) {
2488 if (my_mddev
->persistent
) {
2489 sectors
= super_types
[my_mddev
->major_version
].
2490 rdev_size_change(rdev
, sectors
);
2493 } else if (!sectors
)
2494 sectors
= (rdev
->bdev
->bd_inode
->i_size
>> 9) -
2497 if (sectors
< my_mddev
->dev_sectors
)
2498 return -EINVAL
; /* component must fit device */
2500 rdev
->sectors
= sectors
;
2501 if (sectors
> oldsectors
&& my_mddev
->external
) {
2502 /* need to check that all other rdevs with the same ->bdev
2503 * do not overlap. We need to unlock the mddev to avoid
2504 * a deadlock. We have already changed rdev->sectors, and if
2505 * we have to change it back, we will have the lock again.
2509 struct list_head
*tmp
;
2511 mddev_unlock(my_mddev
);
2512 for_each_mddev(mddev
, tmp
) {
2516 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
)
2517 if (test_bit(AllReserved
, &rdev2
->flags
) ||
2518 (rdev
->bdev
== rdev2
->bdev
&&
2520 overlaps(rdev
->data_offset
, rdev
->sectors
,
2526 mddev_unlock(mddev
);
2532 mddev_lock(my_mddev
);
2534 /* Someone else could have slipped in a size
2535 * change here, but doing so is just silly.
2536 * We put oldsectors back because we *know* it is
2537 * safe, and trust userspace not to race with
2540 rdev
->sectors
= oldsectors
;
2547 static struct rdev_sysfs_entry rdev_size
=
2548 __ATTR(size
, S_IRUGO
|S_IWUSR
, rdev_size_show
, rdev_size_store
);
2551 static ssize_t
recovery_start_show(mdk_rdev_t
*rdev
, char *page
)
2553 unsigned long long recovery_start
= rdev
->recovery_offset
;
2555 if (test_bit(In_sync
, &rdev
->flags
) ||
2556 recovery_start
== MaxSector
)
2557 return sprintf(page
, "none\n");
2559 return sprintf(page
, "%llu\n", recovery_start
);
2562 static ssize_t
recovery_start_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2564 unsigned long long recovery_start
;
2566 if (cmd_match(buf
, "none"))
2567 recovery_start
= MaxSector
;
2568 else if (strict_strtoull(buf
, 10, &recovery_start
))
2571 if (rdev
->mddev
->pers
&&
2572 rdev
->raid_disk
>= 0)
2575 rdev
->recovery_offset
= recovery_start
;
2576 if (recovery_start
== MaxSector
)
2577 set_bit(In_sync
, &rdev
->flags
);
2579 clear_bit(In_sync
, &rdev
->flags
);
2583 static struct rdev_sysfs_entry rdev_recovery_start
=
2584 __ATTR(recovery_start
, S_IRUGO
|S_IWUSR
, recovery_start_show
, recovery_start_store
);
2586 static struct attribute
*rdev_default_attrs
[] = {
2592 &rdev_recovery_start
.attr
,
2596 rdev_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
2598 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
2599 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
2600 mddev_t
*mddev
= rdev
->mddev
;
2606 rv
= mddev
? mddev_lock(mddev
) : -EBUSY
;
2608 if (rdev
->mddev
== NULL
)
2611 rv
= entry
->show(rdev
, page
);
2612 mddev_unlock(mddev
);
2618 rdev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
2619 const char *page
, size_t length
)
2621 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
2622 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
2624 mddev_t
*mddev
= rdev
->mddev
;
2628 if (!capable(CAP_SYS_ADMIN
))
2630 rv
= mddev
? mddev_lock(mddev
): -EBUSY
;
2632 if (rdev
->mddev
== NULL
)
2635 rv
= entry
->store(rdev
, page
, length
);
2636 mddev_unlock(mddev
);
2641 static void rdev_free(struct kobject
*ko
)
2643 mdk_rdev_t
*rdev
= container_of(ko
, mdk_rdev_t
, kobj
);
2646 static const struct sysfs_ops rdev_sysfs_ops
= {
2647 .show
= rdev_attr_show
,
2648 .store
= rdev_attr_store
,
2650 static struct kobj_type rdev_ktype
= {
2651 .release
= rdev_free
,
2652 .sysfs_ops
= &rdev_sysfs_ops
,
2653 .default_attrs
= rdev_default_attrs
,
2657 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2659 * mark the device faulty if:
2661 * - the device is nonexistent (zero size)
2662 * - the device has no valid superblock
2664 * a faulty rdev _never_ has rdev->sb set.
2666 static mdk_rdev_t
*md_import_device(dev_t newdev
, int super_format
, int super_minor
)
2668 char b
[BDEVNAME_SIZE
];
2673 rdev
= kzalloc(sizeof(*rdev
), GFP_KERNEL
);
2675 printk(KERN_ERR
"md: could not alloc mem for new device!\n");
2676 return ERR_PTR(-ENOMEM
);
2679 if ((err
= alloc_disk_sb(rdev
)))
2682 err
= lock_rdev(rdev
, newdev
, super_format
== -2);
2686 kobject_init(&rdev
->kobj
, &rdev_ktype
);
2689 rdev
->saved_raid_disk
= -1;
2690 rdev
->raid_disk
= -1;
2692 rdev
->data_offset
= 0;
2693 rdev
->sb_events
= 0;
2694 rdev
->last_read_error
.tv_sec
= 0;
2695 rdev
->last_read_error
.tv_nsec
= 0;
2696 atomic_set(&rdev
->nr_pending
, 0);
2697 atomic_set(&rdev
->read_errors
, 0);
2698 atomic_set(&rdev
->corrected_errors
, 0);
2700 size
= rdev
->bdev
->bd_inode
->i_size
>> BLOCK_SIZE_BITS
;
2703 "md: %s has zero or unknown size, marking faulty!\n",
2704 bdevname(rdev
->bdev
,b
));
2709 if (super_format
>= 0) {
2710 err
= super_types
[super_format
].
2711 load_super(rdev
, NULL
, super_minor
);
2712 if (err
== -EINVAL
) {
2714 "md: %s does not have a valid v%d.%d "
2715 "superblock, not importing!\n",
2716 bdevname(rdev
->bdev
,b
),
2717 super_format
, super_minor
);
2722 "md: could not read %s's sb, not importing!\n",
2723 bdevname(rdev
->bdev
,b
));
2728 INIT_LIST_HEAD(&rdev
->same_set
);
2729 init_waitqueue_head(&rdev
->blocked_wait
);
2734 if (rdev
->sb_page
) {
2740 return ERR_PTR(err
);
2744 * Check a full RAID array for plausibility
2748 static void analyze_sbs(mddev_t
* mddev
)
2751 mdk_rdev_t
*rdev
, *freshest
, *tmp
;
2752 char b
[BDEVNAME_SIZE
];
2755 rdev_for_each(rdev
, tmp
, mddev
)
2756 switch (super_types
[mddev
->major_version
].
2757 load_super(rdev
, freshest
, mddev
->minor_version
)) {
2765 "md: fatal superblock inconsistency in %s"
2766 " -- removing from array\n",
2767 bdevname(rdev
->bdev
,b
));
2768 kick_rdev_from_array(rdev
);
2772 super_types
[mddev
->major_version
].
2773 validate_super(mddev
, freshest
);
2776 rdev_for_each(rdev
, tmp
, mddev
) {
2777 if (rdev
->desc_nr
>= mddev
->max_disks
||
2778 i
> mddev
->max_disks
) {
2780 "md: %s: %s: only %d devices permitted\n",
2781 mdname(mddev
), bdevname(rdev
->bdev
, b
),
2783 kick_rdev_from_array(rdev
);
2786 if (rdev
!= freshest
)
2787 if (super_types
[mddev
->major_version
].
2788 validate_super(mddev
, rdev
)) {
2789 printk(KERN_WARNING
"md: kicking non-fresh %s"
2791 bdevname(rdev
->bdev
,b
));
2792 kick_rdev_from_array(rdev
);
2795 if (mddev
->level
== LEVEL_MULTIPATH
) {
2796 rdev
->desc_nr
= i
++;
2797 rdev
->raid_disk
= rdev
->desc_nr
;
2798 set_bit(In_sync
, &rdev
->flags
);
2799 } else if (rdev
->raid_disk
>= (mddev
->raid_disks
- min(0, mddev
->delta_disks
))) {
2800 rdev
->raid_disk
= -1;
2801 clear_bit(In_sync
, &rdev
->flags
);
2806 /* Read a fixed-point number.
2807 * Numbers in sysfs attributes should be in "standard" units where
2808 * possible, so time should be in seconds.
2809 * However we internally use a a much smaller unit such as
2810 * milliseconds or jiffies.
2811 * This function takes a decimal number with a possible fractional
2812 * component, and produces an integer which is the result of
2813 * multiplying that number by 10^'scale'.
2814 * all without any floating-point arithmetic.
2816 int strict_strtoul_scaled(const char *cp
, unsigned long *res
, int scale
)
2818 unsigned long result
= 0;
2820 while (isdigit(*cp
) || (*cp
== '.' && decimals
< 0)) {
2823 else if (decimals
< scale
) {
2826 result
= result
* 10 + value
;
2838 while (decimals
< scale
) {
2847 static void md_safemode_timeout(unsigned long data
);
2850 safe_delay_show(mddev_t
*mddev
, char *page
)
2852 int msec
= (mddev
->safemode_delay
*1000)/HZ
;
2853 return sprintf(page
, "%d.%03d\n", msec
/1000, msec
%1000);
2856 safe_delay_store(mddev_t
*mddev
, const char *cbuf
, size_t len
)
2860 if (strict_strtoul_scaled(cbuf
, &msec
, 3) < 0)
2863 mddev
->safemode_delay
= 0;
2865 unsigned long old_delay
= mddev
->safemode_delay
;
2866 mddev
->safemode_delay
= (msec
*HZ
)/1000;
2867 if (mddev
->safemode_delay
== 0)
2868 mddev
->safemode_delay
= 1;
2869 if (mddev
->safemode_delay
< old_delay
)
2870 md_safemode_timeout((unsigned long)mddev
);
2874 static struct md_sysfs_entry md_safe_delay
=
2875 __ATTR(safe_mode_delay
, S_IRUGO
|S_IWUSR
,safe_delay_show
, safe_delay_store
);
2878 level_show(mddev_t
*mddev
, char *page
)
2880 struct mdk_personality
*p
= mddev
->pers
;
2882 return sprintf(page
, "%s\n", p
->name
);
2883 else if (mddev
->clevel
[0])
2884 return sprintf(page
, "%s\n", mddev
->clevel
);
2885 else if (mddev
->level
!= LEVEL_NONE
)
2886 return sprintf(page
, "%d\n", mddev
->level
);
2892 level_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2896 struct mdk_personality
*pers
;
2900 if (mddev
->pers
== NULL
) {
2903 if (len
>= sizeof(mddev
->clevel
))
2905 strncpy(mddev
->clevel
, buf
, len
);
2906 if (mddev
->clevel
[len
-1] == '\n')
2908 mddev
->clevel
[len
] = 0;
2909 mddev
->level
= LEVEL_NONE
;
2913 /* request to change the personality. Need to ensure:
2914 * - array is not engaged in resync/recovery/reshape
2915 * - old personality can be suspended
2916 * - new personality will access other array.
2919 if (mddev
->sync_thread
|| mddev
->reshape_position
!= MaxSector
)
2922 if (!mddev
->pers
->quiesce
) {
2923 printk(KERN_WARNING
"md: %s: %s does not support online personality change\n",
2924 mdname(mddev
), mddev
->pers
->name
);
2928 /* Now find the new personality */
2929 if (len
== 0 || len
>= sizeof(level
))
2931 strncpy(level
, buf
, len
);
2932 if (level
[len
-1] == '\n')
2936 request_module("md-%s", level
);
2937 spin_lock(&pers_lock
);
2938 pers
= find_pers(LEVEL_NONE
, level
);
2939 if (!pers
|| !try_module_get(pers
->owner
)) {
2940 spin_unlock(&pers_lock
);
2941 printk(KERN_WARNING
"md: personality %s not loaded\n", level
);
2944 spin_unlock(&pers_lock
);
2946 if (pers
== mddev
->pers
) {
2947 /* Nothing to do! */
2948 module_put(pers
->owner
);
2951 if (!pers
->takeover
) {
2952 module_put(pers
->owner
);
2953 printk(KERN_WARNING
"md: %s: %s does not support personality takeover\n",
2954 mdname(mddev
), level
);
2958 /* ->takeover must set new_* and/or delta_disks
2959 * if it succeeds, and may set them when it fails.
2961 priv
= pers
->takeover(mddev
);
2963 mddev
->new_level
= mddev
->level
;
2964 mddev
->new_layout
= mddev
->layout
;
2965 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
2966 mddev
->raid_disks
-= mddev
->delta_disks
;
2967 mddev
->delta_disks
= 0;
2968 module_put(pers
->owner
);
2969 printk(KERN_WARNING
"md: %s: %s would not accept array\n",
2970 mdname(mddev
), level
);
2971 return PTR_ERR(priv
);
2974 /* Looks like we have a winner */
2975 mddev_suspend(mddev
);
2976 mddev
->pers
->stop(mddev
);
2977 module_put(mddev
->pers
->owner
);
2978 /* Invalidate devices that are now superfluous */
2979 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
2980 if (rdev
->raid_disk
>= mddev
->raid_disks
) {
2981 rdev
->raid_disk
= -1;
2982 clear_bit(In_sync
, &rdev
->flags
);
2985 mddev
->private = priv
;
2986 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
2987 mddev
->level
= mddev
->new_level
;
2988 mddev
->layout
= mddev
->new_layout
;
2989 mddev
->chunk_sectors
= mddev
->new_chunk_sectors
;
2990 mddev
->delta_disks
= 0;
2992 mddev_resume(mddev
);
2993 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
2994 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
2995 md_wakeup_thread(mddev
->thread
);
2999 static struct md_sysfs_entry md_level
=
3000 __ATTR(level
, S_IRUGO
|S_IWUSR
, level_show
, level_store
);
3004 layout_show(mddev_t
*mddev
, char *page
)
3006 /* just a number, not meaningful for all levels */
3007 if (mddev
->reshape_position
!= MaxSector
&&
3008 mddev
->layout
!= mddev
->new_layout
)
3009 return sprintf(page
, "%d (%d)\n",
3010 mddev
->new_layout
, mddev
->layout
);
3011 return sprintf(page
, "%d\n", mddev
->layout
);
3015 layout_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3018 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3020 if (!*buf
|| (*e
&& *e
!= '\n'))
3025 if (mddev
->pers
->check_reshape
== NULL
)
3027 mddev
->new_layout
= n
;
3028 err
= mddev
->pers
->check_reshape(mddev
);
3030 mddev
->new_layout
= mddev
->layout
;
3034 mddev
->new_layout
= n
;
3035 if (mddev
->reshape_position
== MaxSector
)
3040 static struct md_sysfs_entry md_layout
=
3041 __ATTR(layout
, S_IRUGO
|S_IWUSR
, layout_show
, layout_store
);
3045 raid_disks_show(mddev_t
*mddev
, char *page
)
3047 if (mddev
->raid_disks
== 0)
3049 if (mddev
->reshape_position
!= MaxSector
&&
3050 mddev
->delta_disks
!= 0)
3051 return sprintf(page
, "%d (%d)\n", mddev
->raid_disks
,
3052 mddev
->raid_disks
- mddev
->delta_disks
);
3053 return sprintf(page
, "%d\n", mddev
->raid_disks
);
3056 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
);
3059 raid_disks_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3063 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3065 if (!*buf
|| (*e
&& *e
!= '\n'))
3069 rv
= update_raid_disks(mddev
, n
);
3070 else if (mddev
->reshape_position
!= MaxSector
) {
3071 int olddisks
= mddev
->raid_disks
- mddev
->delta_disks
;
3072 mddev
->delta_disks
= n
- olddisks
;
3073 mddev
->raid_disks
= n
;
3075 mddev
->raid_disks
= n
;
3076 return rv
? rv
: len
;
3078 static struct md_sysfs_entry md_raid_disks
=
3079 __ATTR(raid_disks
, S_IRUGO
|S_IWUSR
, raid_disks_show
, raid_disks_store
);
3082 chunk_size_show(mddev_t
*mddev
, char *page
)
3084 if (mddev
->reshape_position
!= MaxSector
&&
3085 mddev
->chunk_sectors
!= mddev
->new_chunk_sectors
)
3086 return sprintf(page
, "%d (%d)\n",
3087 mddev
->new_chunk_sectors
<< 9,
3088 mddev
->chunk_sectors
<< 9);
3089 return sprintf(page
, "%d\n", mddev
->chunk_sectors
<< 9);
3093 chunk_size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3096 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3098 if (!*buf
|| (*e
&& *e
!= '\n'))
3103 if (mddev
->pers
->check_reshape
== NULL
)
3105 mddev
->new_chunk_sectors
= n
>> 9;
3106 err
= mddev
->pers
->check_reshape(mddev
);
3108 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3112 mddev
->new_chunk_sectors
= n
>> 9;
3113 if (mddev
->reshape_position
== MaxSector
)
3114 mddev
->chunk_sectors
= n
>> 9;
3118 static struct md_sysfs_entry md_chunk_size
=
3119 __ATTR(chunk_size
, S_IRUGO
|S_IWUSR
, chunk_size_show
, chunk_size_store
);
3122 resync_start_show(mddev_t
*mddev
, char *page
)
3124 if (mddev
->recovery_cp
== MaxSector
)
3125 return sprintf(page
, "none\n");
3126 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->recovery_cp
);
3130 resync_start_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3133 unsigned long long n
= simple_strtoull(buf
, &e
, 10);
3137 if (cmd_match(buf
, "none"))
3139 else if (!*buf
|| (*e
&& *e
!= '\n'))
3142 mddev
->recovery_cp
= n
;
3145 static struct md_sysfs_entry md_resync_start
=
3146 __ATTR(resync_start
, S_IRUGO
|S_IWUSR
, resync_start_show
, resync_start_store
);
3149 * The array state can be:
3152 * No devices, no size, no level
3153 * Equivalent to STOP_ARRAY ioctl
3155 * May have some settings, but array is not active
3156 * all IO results in error
3157 * When written, doesn't tear down array, but just stops it
3158 * suspended (not supported yet)
3159 * All IO requests will block. The array can be reconfigured.
3160 * Writing this, if accepted, will block until array is quiescent
3162 * no resync can happen. no superblocks get written.
3163 * write requests fail
3165 * like readonly, but behaves like 'clean' on a write request.
3167 * clean - no pending writes, but otherwise active.
3168 * When written to inactive array, starts without resync
3169 * If a write request arrives then
3170 * if metadata is known, mark 'dirty' and switch to 'active'.
3171 * if not known, block and switch to write-pending
3172 * If written to an active array that has pending writes, then fails.
3174 * fully active: IO and resync can be happening.
3175 * When written to inactive array, starts with resync
3178 * clean, but writes are blocked waiting for 'active' to be written.
3181 * like active, but no writes have been seen for a while (100msec).
3184 enum array_state
{ clear
, inactive
, suspended
, readonly
, read_auto
, clean
, active
,
3185 write_pending
, active_idle
, bad_word
};
3186 static char *array_states
[] = {
3187 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3188 "write-pending", "active-idle", NULL
};
3190 static int match_word(const char *word
, char **list
)
3193 for (n
=0; list
[n
]; n
++)
3194 if (cmd_match(word
, list
[n
]))
3200 array_state_show(mddev_t
*mddev
, char *page
)
3202 enum array_state st
= inactive
;
3215 else if (test_bit(MD_CHANGE_CLEAN
, &mddev
->flags
))
3217 else if (mddev
->safemode
)
3223 if (list_empty(&mddev
->disks
) &&
3224 mddev
->raid_disks
== 0 &&
3225 mddev
->dev_sectors
== 0)
3230 return sprintf(page
, "%s\n", array_states
[st
]);
3233 static int do_md_stop(mddev_t
* mddev
, int ro
, int is_open
);
3234 static int do_md_run(mddev_t
* mddev
);
3235 static int restart_array(mddev_t
*mddev
);
3238 array_state_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3241 enum array_state st
= match_word(buf
, array_states
);
3246 /* stopping an active array */
3247 if (atomic_read(&mddev
->openers
) > 0)
3249 err
= do_md_stop(mddev
, 0, 0);
3252 /* stopping an active array */
3254 if (atomic_read(&mddev
->openers
) > 0)
3256 err
= do_md_stop(mddev
, 2, 0);
3258 err
= 0; /* already inactive */
3261 break; /* not supported yet */
3264 err
= do_md_stop(mddev
, 1, 0);
3267 set_disk_ro(mddev
->gendisk
, 1);
3268 err
= do_md_run(mddev
);
3274 err
= do_md_stop(mddev
, 1, 0);
3275 else if (mddev
->ro
== 1)
3276 err
= restart_array(mddev
);
3279 set_disk_ro(mddev
->gendisk
, 0);
3283 err
= do_md_run(mddev
);
3288 restart_array(mddev
);
3289 spin_lock_irq(&mddev
->write_lock
);
3290 if (atomic_read(&mddev
->writes_pending
) == 0) {
3291 if (mddev
->in_sync
== 0) {
3293 if (mddev
->safemode
== 1)
3294 mddev
->safemode
= 0;
3295 if (mddev
->persistent
)
3296 set_bit(MD_CHANGE_CLEAN
,
3302 spin_unlock_irq(&mddev
->write_lock
);
3308 restart_array(mddev
);
3309 if (mddev
->external
)
3310 clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
3311 wake_up(&mddev
->sb_wait
);
3315 set_disk_ro(mddev
->gendisk
, 0);
3316 err
= do_md_run(mddev
);
3321 /* these cannot be set */
3327 sysfs_notify_dirent(mddev
->sysfs_state
);
3331 static struct md_sysfs_entry md_array_state
=
3332 __ATTR(array_state
, S_IRUGO
|S_IWUSR
, array_state_show
, array_state_store
);
3335 max_corrected_read_errors_show(mddev_t
*mddev
, char *page
) {
3336 return sprintf(page
, "%d\n",
3337 atomic_read(&mddev
->max_corr_read_errors
));
3341 max_corrected_read_errors_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3344 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3346 if (*buf
&& (*e
== 0 || *e
== '\n')) {
3347 atomic_set(&mddev
->max_corr_read_errors
, n
);
3353 static struct md_sysfs_entry max_corr_read_errors
=
3354 __ATTR(max_read_errors
, S_IRUGO
|S_IWUSR
, max_corrected_read_errors_show
,
3355 max_corrected_read_errors_store
);
3358 null_show(mddev_t
*mddev
, char *page
)
3364 new_dev_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3366 /* buf must be %d:%d\n? giving major and minor numbers */
3367 /* The new device is added to the array.
3368 * If the array has a persistent superblock, we read the
3369 * superblock to initialise info and check validity.
3370 * Otherwise, only checking done is that in bind_rdev_to_array,
3371 * which mainly checks size.
3374 int major
= simple_strtoul(buf
, &e
, 10);
3380 if (!*buf
|| *e
!= ':' || !e
[1] || e
[1] == '\n')
3382 minor
= simple_strtoul(e
+1, &e
, 10);
3383 if (*e
&& *e
!= '\n')
3385 dev
= MKDEV(major
, minor
);
3386 if (major
!= MAJOR(dev
) ||
3387 minor
!= MINOR(dev
))
3391 if (mddev
->persistent
) {
3392 rdev
= md_import_device(dev
, mddev
->major_version
,
3393 mddev
->minor_version
);
3394 if (!IS_ERR(rdev
) && !list_empty(&mddev
->disks
)) {
3395 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
3396 mdk_rdev_t
, same_set
);
3397 err
= super_types
[mddev
->major_version
]
3398 .load_super(rdev
, rdev0
, mddev
->minor_version
);
3402 } else if (mddev
->external
)
3403 rdev
= md_import_device(dev
, -2, -1);
3405 rdev
= md_import_device(dev
, -1, -1);
3408 return PTR_ERR(rdev
);
3409 err
= bind_rdev_to_array(rdev
, mddev
);
3413 return err
? err
: len
;
3416 static struct md_sysfs_entry md_new_device
=
3417 __ATTR(new_dev
, S_IWUSR
, null_show
, new_dev_store
);
3420 bitmap_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3423 unsigned long chunk
, end_chunk
;
3427 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3429 chunk
= end_chunk
= simple_strtoul(buf
, &end
, 0);
3430 if (buf
== end
) break;
3431 if (*end
== '-') { /* range */
3433 end_chunk
= simple_strtoul(buf
, &end
, 0);
3434 if (buf
== end
) break;
3436 if (*end
&& !isspace(*end
)) break;
3437 bitmap_dirty_bits(mddev
->bitmap
, chunk
, end_chunk
);
3438 buf
= skip_spaces(end
);
3440 bitmap_unplug(mddev
->bitmap
); /* flush the bits to disk */
3445 static struct md_sysfs_entry md_bitmap
=
3446 __ATTR(bitmap_set_bits
, S_IWUSR
, null_show
, bitmap_store
);
3449 size_show(mddev_t
*mddev
, char *page
)
3451 return sprintf(page
, "%llu\n",
3452 (unsigned long long)mddev
->dev_sectors
/ 2);
3455 static int update_size(mddev_t
*mddev
, sector_t num_sectors
);
3458 size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3460 /* If array is inactive, we can reduce the component size, but
3461 * not increase it (except from 0).
3462 * If array is active, we can try an on-line resize
3465 int err
= strict_blocks_to_sectors(buf
, §ors
);
3470 err
= update_size(mddev
, sectors
);
3471 md_update_sb(mddev
, 1);
3473 if (mddev
->dev_sectors
== 0 ||
3474 mddev
->dev_sectors
> sectors
)
3475 mddev
->dev_sectors
= sectors
;
3479 return err
? err
: len
;
3482 static struct md_sysfs_entry md_size
=
3483 __ATTR(component_size
, S_IRUGO
|S_IWUSR
, size_show
, size_store
);
3488 * 'none' for arrays with no metadata (good luck...)
3489 * 'external' for arrays with externally managed metadata,
3490 * or N.M for internally known formats
3493 metadata_show(mddev_t
*mddev
, char *page
)
3495 if (mddev
->persistent
)
3496 return sprintf(page
, "%d.%d\n",
3497 mddev
->major_version
, mddev
->minor_version
);
3498 else if (mddev
->external
)
3499 return sprintf(page
, "external:%s\n", mddev
->metadata_type
);
3501 return sprintf(page
, "none\n");
3505 metadata_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3509 /* Changing the details of 'external' metadata is
3510 * always permitted. Otherwise there must be
3511 * no devices attached to the array.
3513 if (mddev
->external
&& strncmp(buf
, "external:", 9) == 0)
3515 else if (!list_empty(&mddev
->disks
))
3518 if (cmd_match(buf
, "none")) {
3519 mddev
->persistent
= 0;
3520 mddev
->external
= 0;
3521 mddev
->major_version
= 0;
3522 mddev
->minor_version
= 90;
3525 if (strncmp(buf
, "external:", 9) == 0) {
3526 size_t namelen
= len
-9;
3527 if (namelen
>= sizeof(mddev
->metadata_type
))
3528 namelen
= sizeof(mddev
->metadata_type
)-1;
3529 strncpy(mddev
->metadata_type
, buf
+9, namelen
);
3530 mddev
->metadata_type
[namelen
] = 0;
3531 if (namelen
&& mddev
->metadata_type
[namelen
-1] == '\n')
3532 mddev
->metadata_type
[--namelen
] = 0;
3533 mddev
->persistent
= 0;
3534 mddev
->external
= 1;
3535 mddev
->major_version
= 0;
3536 mddev
->minor_version
= 90;
3539 major
= simple_strtoul(buf
, &e
, 10);
3540 if (e
==buf
|| *e
!= '.')
3543 minor
= simple_strtoul(buf
, &e
, 10);
3544 if (e
==buf
|| (*e
&& *e
!= '\n') )
3546 if (major
>= ARRAY_SIZE(super_types
) || super_types
[major
].name
== NULL
)
3548 mddev
->major_version
= major
;
3549 mddev
->minor_version
= minor
;
3550 mddev
->persistent
= 1;
3551 mddev
->external
= 0;
3555 static struct md_sysfs_entry md_metadata
=
3556 __ATTR(metadata_version
, S_IRUGO
|S_IWUSR
, metadata_show
, metadata_store
);
3559 action_show(mddev_t
*mddev
, char *page
)
3561 char *type
= "idle";
3562 if (test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
3564 else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
3565 (!mddev
->ro
&& test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))) {
3566 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
3568 else if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
3569 if (!test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
3571 else if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
3575 } else if (test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
))
3578 return sprintf(page
, "%s\n", type
);
3582 action_store(mddev_t
*mddev
, const char *page
, size_t len
)
3584 if (!mddev
->pers
|| !mddev
->pers
->sync_request
)
3587 if (cmd_match(page
, "frozen"))
3588 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
3590 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
3592 if (cmd_match(page
, "idle") || cmd_match(page
, "frozen")) {
3593 if (mddev
->sync_thread
) {
3594 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
3595 md_unregister_thread(mddev
->sync_thread
);
3596 mddev
->sync_thread
= NULL
;
3597 mddev
->recovery
= 0;
3599 } else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
3600 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
3602 else if (cmd_match(page
, "resync"))
3603 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3604 else if (cmd_match(page
, "recover")) {
3605 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
3606 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3607 } else if (cmd_match(page
, "reshape")) {
3609 if (mddev
->pers
->start_reshape
== NULL
)
3611 err
= mddev
->pers
->start_reshape(mddev
);
3614 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
3616 if (cmd_match(page
, "check"))
3617 set_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
3618 else if (!cmd_match(page
, "repair"))
3620 set_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
3621 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
3623 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3624 md_wakeup_thread(mddev
->thread
);
3625 sysfs_notify_dirent(mddev
->sysfs_action
);
3630 mismatch_cnt_show(mddev_t
*mddev
, char *page
)
3632 return sprintf(page
, "%llu\n",
3633 (unsigned long long) mddev
->resync_mismatches
);
3636 static struct md_sysfs_entry md_scan_mode
=
3637 __ATTR(sync_action
, S_IRUGO
|S_IWUSR
, action_show
, action_store
);
3640 static struct md_sysfs_entry md_mismatches
= __ATTR_RO(mismatch_cnt
);
3643 sync_min_show(mddev_t
*mddev
, char *page
)
3645 return sprintf(page
, "%d (%s)\n", speed_min(mddev
),
3646 mddev
->sync_speed_min
? "local": "system");
3650 sync_min_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3654 if (strncmp(buf
, "system", 6)==0) {
3655 mddev
->sync_speed_min
= 0;
3658 min
= simple_strtoul(buf
, &e
, 10);
3659 if (buf
== e
|| (*e
&& *e
!= '\n') || min
<= 0)
3661 mddev
->sync_speed_min
= min
;
3665 static struct md_sysfs_entry md_sync_min
=
3666 __ATTR(sync_speed_min
, S_IRUGO
|S_IWUSR
, sync_min_show
, sync_min_store
);
3669 sync_max_show(mddev_t
*mddev
, char *page
)
3671 return sprintf(page
, "%d (%s)\n", speed_max(mddev
),
3672 mddev
->sync_speed_max
? "local": "system");
3676 sync_max_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3680 if (strncmp(buf
, "system", 6)==0) {
3681 mddev
->sync_speed_max
= 0;
3684 max
= simple_strtoul(buf
, &e
, 10);
3685 if (buf
== e
|| (*e
&& *e
!= '\n') || max
<= 0)
3687 mddev
->sync_speed_max
= max
;
3691 static struct md_sysfs_entry md_sync_max
=
3692 __ATTR(sync_speed_max
, S_IRUGO
|S_IWUSR
, sync_max_show
, sync_max_store
);
3695 degraded_show(mddev_t
*mddev
, char *page
)
3697 return sprintf(page
, "%d\n", mddev
->degraded
);
3699 static struct md_sysfs_entry md_degraded
= __ATTR_RO(degraded
);
3702 sync_force_parallel_show(mddev_t
*mddev
, char *page
)
3704 return sprintf(page
, "%d\n", mddev
->parallel_resync
);
3708 sync_force_parallel_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3712 if (strict_strtol(buf
, 10, &n
))
3715 if (n
!= 0 && n
!= 1)
3718 mddev
->parallel_resync
= n
;
3720 if (mddev
->sync_thread
)
3721 wake_up(&resync_wait
);
3726 /* force parallel resync, even with shared block devices */
3727 static struct md_sysfs_entry md_sync_force_parallel
=
3728 __ATTR(sync_force_parallel
, S_IRUGO
|S_IWUSR
,
3729 sync_force_parallel_show
, sync_force_parallel_store
);
3732 sync_speed_show(mddev_t
*mddev
, char *page
)
3734 unsigned long resync
, dt
, db
;
3735 if (mddev
->curr_resync
== 0)
3736 return sprintf(page
, "none\n");
3737 resync
= mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
);
3738 dt
= (jiffies
- mddev
->resync_mark
) / HZ
;
3740 db
= resync
- mddev
->resync_mark_cnt
;
3741 return sprintf(page
, "%lu\n", db
/dt
/2); /* K/sec */
3744 static struct md_sysfs_entry md_sync_speed
= __ATTR_RO(sync_speed
);
3747 sync_completed_show(mddev_t
*mddev
, char *page
)
3749 unsigned long max_sectors
, resync
;
3751 if (!test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3752 return sprintf(page
, "none\n");
3754 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
3755 max_sectors
= mddev
->resync_max_sectors
;
3757 max_sectors
= mddev
->dev_sectors
;
3759 resync
= mddev
->curr_resync_completed
;
3760 return sprintf(page
, "%lu / %lu\n", resync
, max_sectors
);
3763 static struct md_sysfs_entry md_sync_completed
= __ATTR_RO(sync_completed
);
3766 min_sync_show(mddev_t
*mddev
, char *page
)
3768 return sprintf(page
, "%llu\n",
3769 (unsigned long long)mddev
->resync_min
);
3772 min_sync_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3774 unsigned long long min
;
3775 if (strict_strtoull(buf
, 10, &min
))
3777 if (min
> mddev
->resync_max
)
3779 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3782 /* Must be a multiple of chunk_size */
3783 if (mddev
->chunk_sectors
) {
3784 sector_t temp
= min
;
3785 if (sector_div(temp
, mddev
->chunk_sectors
))
3788 mddev
->resync_min
= min
;
3793 static struct md_sysfs_entry md_min_sync
=
3794 __ATTR(sync_min
, S_IRUGO
|S_IWUSR
, min_sync_show
, min_sync_store
);
3797 max_sync_show(mddev_t
*mddev
, char *page
)
3799 if (mddev
->resync_max
== MaxSector
)
3800 return sprintf(page
, "max\n");
3802 return sprintf(page
, "%llu\n",
3803 (unsigned long long)mddev
->resync_max
);
3806 max_sync_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3808 if (strncmp(buf
, "max", 3) == 0)
3809 mddev
->resync_max
= MaxSector
;
3811 unsigned long long max
;
3812 if (strict_strtoull(buf
, 10, &max
))
3814 if (max
< mddev
->resync_min
)
3816 if (max
< mddev
->resync_max
&&
3818 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3821 /* Must be a multiple of chunk_size */
3822 if (mddev
->chunk_sectors
) {
3823 sector_t temp
= max
;
3824 if (sector_div(temp
, mddev
->chunk_sectors
))
3827 mddev
->resync_max
= max
;
3829 wake_up(&mddev
->recovery_wait
);
3833 static struct md_sysfs_entry md_max_sync
=
3834 __ATTR(sync_max
, S_IRUGO
|S_IWUSR
, max_sync_show
, max_sync_store
);
3837 suspend_lo_show(mddev_t
*mddev
, char *page
)
3839 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_lo
);
3843 suspend_lo_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3846 unsigned long long new = simple_strtoull(buf
, &e
, 10);
3848 if (mddev
->pers
== NULL
||
3849 mddev
->pers
->quiesce
== NULL
)
3851 if (buf
== e
|| (*e
&& *e
!= '\n'))
3853 if (new >= mddev
->suspend_hi
||
3854 (new > mddev
->suspend_lo
&& new < mddev
->suspend_hi
)) {
3855 mddev
->suspend_lo
= new;
3856 mddev
->pers
->quiesce(mddev
, 2);
3861 static struct md_sysfs_entry md_suspend_lo
=
3862 __ATTR(suspend_lo
, S_IRUGO
|S_IWUSR
, suspend_lo_show
, suspend_lo_store
);
3866 suspend_hi_show(mddev_t
*mddev
, char *page
)
3868 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_hi
);
3872 suspend_hi_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3875 unsigned long long new = simple_strtoull(buf
, &e
, 10);
3877 if (mddev
->pers
== NULL
||
3878 mddev
->pers
->quiesce
== NULL
)
3880 if (buf
== e
|| (*e
&& *e
!= '\n'))
3882 if ((new <= mddev
->suspend_lo
&& mddev
->suspend_lo
>= mddev
->suspend_hi
) ||
3883 (new > mddev
->suspend_lo
&& new > mddev
->suspend_hi
)) {
3884 mddev
->suspend_hi
= new;
3885 mddev
->pers
->quiesce(mddev
, 1);
3886 mddev
->pers
->quiesce(mddev
, 0);
3891 static struct md_sysfs_entry md_suspend_hi
=
3892 __ATTR(suspend_hi
, S_IRUGO
|S_IWUSR
, suspend_hi_show
, suspend_hi_store
);
3895 reshape_position_show(mddev_t
*mddev
, char *page
)
3897 if (mddev
->reshape_position
!= MaxSector
)
3898 return sprintf(page
, "%llu\n",
3899 (unsigned long long)mddev
->reshape_position
);
3900 strcpy(page
, "none\n");
3905 reshape_position_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3908 unsigned long long new = simple_strtoull(buf
, &e
, 10);
3911 if (buf
== e
|| (*e
&& *e
!= '\n'))
3913 mddev
->reshape_position
= new;
3914 mddev
->delta_disks
= 0;
3915 mddev
->new_level
= mddev
->level
;
3916 mddev
->new_layout
= mddev
->layout
;
3917 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3921 static struct md_sysfs_entry md_reshape_position
=
3922 __ATTR(reshape_position
, S_IRUGO
|S_IWUSR
, reshape_position_show
,
3923 reshape_position_store
);
3926 array_size_show(mddev_t
*mddev
, char *page
)
3928 if (mddev
->external_size
)
3929 return sprintf(page
, "%llu\n",
3930 (unsigned long long)mddev
->array_sectors
/2);
3932 return sprintf(page
, "default\n");
3936 array_size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3940 if (strncmp(buf
, "default", 7) == 0) {
3942 sectors
= mddev
->pers
->size(mddev
, 0, 0);
3944 sectors
= mddev
->array_sectors
;
3946 mddev
->external_size
= 0;
3948 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
3950 if (mddev
->pers
&& mddev
->pers
->size(mddev
, 0, 0) < sectors
)
3953 mddev
->external_size
= 1;
3956 mddev
->array_sectors
= sectors
;
3957 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
3959 revalidate_disk(mddev
->gendisk
);
3964 static struct md_sysfs_entry md_array_size
=
3965 __ATTR(array_size
, S_IRUGO
|S_IWUSR
, array_size_show
,
3968 static struct attribute
*md_default_attrs
[] = {
3971 &md_raid_disks
.attr
,
3972 &md_chunk_size
.attr
,
3974 &md_resync_start
.attr
,
3976 &md_new_device
.attr
,
3977 &md_safe_delay
.attr
,
3978 &md_array_state
.attr
,
3979 &md_reshape_position
.attr
,
3980 &md_array_size
.attr
,
3981 &max_corr_read_errors
.attr
,
3985 static struct attribute
*md_redundancy_attrs
[] = {
3987 &md_mismatches
.attr
,
3990 &md_sync_speed
.attr
,
3991 &md_sync_force_parallel
.attr
,
3992 &md_sync_completed
.attr
,
3995 &md_suspend_lo
.attr
,
3996 &md_suspend_hi
.attr
,
4001 static struct attribute_group md_redundancy_group
= {
4003 .attrs
= md_redundancy_attrs
,
4008 md_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
4010 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
4011 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
4016 rv
= mddev_lock(mddev
);
4018 rv
= entry
->show(mddev
, page
);
4019 mddev_unlock(mddev
);
4025 md_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
4026 const char *page
, size_t length
)
4028 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
4029 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
4034 if (!capable(CAP_SYS_ADMIN
))
4036 rv
= mddev_lock(mddev
);
4037 if (mddev
->hold_active
== UNTIL_IOCTL
)
4038 mddev
->hold_active
= 0;
4040 rv
= entry
->store(mddev
, page
, length
);
4041 mddev_unlock(mddev
);
4046 static void md_free(struct kobject
*ko
)
4048 mddev_t
*mddev
= container_of(ko
, mddev_t
, kobj
);
4050 if (mddev
->sysfs_state
)
4051 sysfs_put(mddev
->sysfs_state
);
4053 if (mddev
->gendisk
) {
4054 del_gendisk(mddev
->gendisk
);
4055 put_disk(mddev
->gendisk
);
4058 blk_cleanup_queue(mddev
->queue
);
4063 static const struct sysfs_ops md_sysfs_ops
= {
4064 .show
= md_attr_show
,
4065 .store
= md_attr_store
,
4067 static struct kobj_type md_ktype
= {
4069 .sysfs_ops
= &md_sysfs_ops
,
4070 .default_attrs
= md_default_attrs
,
4075 static void mddev_delayed_delete(struct work_struct
*ws
)
4077 mddev_t
*mddev
= container_of(ws
, mddev_t
, del_work
);
4079 if (mddev
->private) {
4080 sysfs_remove_group(&mddev
->kobj
, &md_redundancy_group
);
4081 if (mddev
->private != (void*)1)
4082 sysfs_remove_group(&mddev
->kobj
, mddev
->private);
4083 if (mddev
->sysfs_action
)
4084 sysfs_put(mddev
->sysfs_action
);
4085 mddev
->sysfs_action
= NULL
;
4086 mddev
->private = NULL
;
4088 sysfs_remove_group(&mddev
->kobj
, &md_bitmap_group
);
4089 kobject_del(&mddev
->kobj
);
4090 kobject_put(&mddev
->kobj
);
4093 static int md_alloc(dev_t dev
, char *name
)
4095 static DEFINE_MUTEX(disks_mutex
);
4096 mddev_t
*mddev
= mddev_find(dev
);
4097 struct gendisk
*disk
;
4106 partitioned
= (MAJOR(mddev
->unit
) != MD_MAJOR
);
4107 shift
= partitioned
? MdpMinorShift
: 0;
4108 unit
= MINOR(mddev
->unit
) >> shift
;
4110 /* wait for any previous instance if this device
4111 * to be completed removed (mddev_delayed_delete).
4113 flush_scheduled_work();
4115 mutex_lock(&disks_mutex
);
4121 /* Need to ensure that 'name' is not a duplicate.
4124 spin_lock(&all_mddevs_lock
);
4126 list_for_each_entry(mddev2
, &all_mddevs
, all_mddevs
)
4127 if (mddev2
->gendisk
&&
4128 strcmp(mddev2
->gendisk
->disk_name
, name
) == 0) {
4129 spin_unlock(&all_mddevs_lock
);
4132 spin_unlock(&all_mddevs_lock
);
4136 mddev
->queue
= blk_alloc_queue(GFP_KERNEL
);
4139 mddev
->queue
->queuedata
= mddev
;
4141 /* Can be unlocked because the queue is new: no concurrency */
4142 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER
, mddev
->queue
);
4144 blk_queue_make_request(mddev
->queue
, md_make_request
);
4146 disk
= alloc_disk(1 << shift
);
4148 blk_cleanup_queue(mddev
->queue
);
4149 mddev
->queue
= NULL
;
4152 disk
->major
= MAJOR(mddev
->unit
);
4153 disk
->first_minor
= unit
<< shift
;
4155 strcpy(disk
->disk_name
, name
);
4156 else if (partitioned
)
4157 sprintf(disk
->disk_name
, "md_d%d", unit
);
4159 sprintf(disk
->disk_name
, "md%d", unit
);
4160 disk
->fops
= &md_fops
;
4161 disk
->private_data
= mddev
;
4162 disk
->queue
= mddev
->queue
;
4163 /* Allow extended partitions. This makes the
4164 * 'mdp' device redundant, but we can't really
4167 disk
->flags
|= GENHD_FL_EXT_DEVT
;
4169 mddev
->gendisk
= disk
;
4170 error
= kobject_init_and_add(&mddev
->kobj
, &md_ktype
,
4171 &disk_to_dev(disk
)->kobj
, "%s", "md");
4173 /* This isn't possible, but as kobject_init_and_add is marked
4174 * __must_check, we must do something with the result
4176 printk(KERN_WARNING
"md: cannot register %s/md - name in use\n",
4180 if (sysfs_create_group(&mddev
->kobj
, &md_bitmap_group
))
4181 printk(KERN_DEBUG
"pointless warning\n");
4183 mutex_unlock(&disks_mutex
);
4185 kobject_uevent(&mddev
->kobj
, KOBJ_ADD
);
4186 mddev
->sysfs_state
= sysfs_get_dirent(mddev
->kobj
.sd
, "array_state");
4192 static struct kobject
*md_probe(dev_t dev
, int *part
, void *data
)
4194 md_alloc(dev
, NULL
);
4198 static int add_named_array(const char *val
, struct kernel_param
*kp
)
4200 /* val must be "md_*" where * is not all digits.
4201 * We allocate an array with a large free minor number, and
4202 * set the name to val. val must not already be an active name.
4204 int len
= strlen(val
);
4205 char buf
[DISK_NAME_LEN
];
4207 while (len
&& val
[len
-1] == '\n')
4209 if (len
>= DISK_NAME_LEN
)
4211 strlcpy(buf
, val
, len
+1);
4212 if (strncmp(buf
, "md_", 3) != 0)
4214 return md_alloc(0, buf
);
4217 static void md_safemode_timeout(unsigned long data
)
4219 mddev_t
*mddev
= (mddev_t
*) data
;
4221 if (!atomic_read(&mddev
->writes_pending
)) {
4222 mddev
->safemode
= 1;
4223 if (mddev
->external
)
4224 sysfs_notify_dirent(mddev
->sysfs_state
);
4226 md_wakeup_thread(mddev
->thread
);
4229 static int start_dirty_degraded
;
4231 static int do_md_run(mddev_t
* mddev
)
4235 struct gendisk
*disk
;
4236 struct mdk_personality
*pers
;
4238 if (list_empty(&mddev
->disks
))
4239 /* cannot run an array with no devices.. */
4246 * Analyze all RAID superblock(s)
4248 if (!mddev
->raid_disks
) {
4249 if (!mddev
->persistent
)
4254 if (mddev
->level
!= LEVEL_NONE
)
4255 request_module("md-level-%d", mddev
->level
);
4256 else if (mddev
->clevel
[0])
4257 request_module("md-%s", mddev
->clevel
);
4260 * Drop all container device buffers, from now on
4261 * the only valid external interface is through the md
4264 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
4265 if (test_bit(Faulty
, &rdev
->flags
))
4267 sync_blockdev(rdev
->bdev
);
4268 invalidate_bdev(rdev
->bdev
);
4270 /* perform some consistency tests on the device.
4271 * We don't want the data to overlap the metadata,
4272 * Internal Bitmap issues have been handled elsewhere.
4274 if (rdev
->data_offset
< rdev
->sb_start
) {
4275 if (mddev
->dev_sectors
&&
4276 rdev
->data_offset
+ mddev
->dev_sectors
4278 printk("md: %s: data overlaps metadata\n",
4283 if (rdev
->sb_start
+ rdev
->sb_size
/512
4284 > rdev
->data_offset
) {
4285 printk("md: %s: metadata overlaps data\n",
4290 sysfs_notify_dirent(rdev
->sysfs_state
);
4293 disk
= mddev
->gendisk
;
4295 spin_lock(&pers_lock
);
4296 pers
= find_pers(mddev
->level
, mddev
->clevel
);
4297 if (!pers
|| !try_module_get(pers
->owner
)) {
4298 spin_unlock(&pers_lock
);
4299 if (mddev
->level
!= LEVEL_NONE
)
4300 printk(KERN_WARNING
"md: personality for level %d is not loaded!\n",
4303 printk(KERN_WARNING
"md: personality for level %s is not loaded!\n",
4308 spin_unlock(&pers_lock
);
4309 if (mddev
->level
!= pers
->level
) {
4310 mddev
->level
= pers
->level
;
4311 mddev
->new_level
= pers
->level
;
4313 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
4315 if (mddev
->reshape_position
!= MaxSector
&&
4316 pers
->start_reshape
== NULL
) {
4317 /* This personality cannot handle reshaping... */
4319 module_put(pers
->owner
);
4323 if (pers
->sync_request
) {
4324 /* Warn if this is a potentially silly
4327 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
4331 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4332 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
4334 rdev
->bdev
->bd_contains
==
4335 rdev2
->bdev
->bd_contains
) {
4337 "%s: WARNING: %s appears to be"
4338 " on the same physical disk as"
4341 bdevname(rdev
->bdev
,b
),
4342 bdevname(rdev2
->bdev
,b2
));
4349 "True protection against single-disk"
4350 " failure might be compromised.\n");
4353 mddev
->recovery
= 0;
4354 /* may be over-ridden by personality */
4355 mddev
->resync_max_sectors
= mddev
->dev_sectors
;
4357 mddev
->barriers_work
= 1;
4358 mddev
->ok_start_degraded
= start_dirty_degraded
;
4360 if (start_readonly
&& mddev
->ro
== 0)
4361 mddev
->ro
= 2; /* read-only, but switch on first write */
4363 err
= mddev
->pers
->run(mddev
);
4365 printk(KERN_ERR
"md: pers->run() failed ...\n");
4366 else if (mddev
->pers
->size(mddev
, 0, 0) < mddev
->array_sectors
) {
4367 WARN_ONCE(!mddev
->external_size
, "%s: default size too small,"
4368 " but 'external_size' not in effect?\n", __func__
);
4370 "md: invalid array_size %llu > default size %llu\n",
4371 (unsigned long long)mddev
->array_sectors
/ 2,
4372 (unsigned long long)mddev
->pers
->size(mddev
, 0, 0) / 2);
4374 mddev
->pers
->stop(mddev
);
4376 if (err
== 0 && mddev
->pers
->sync_request
) {
4377 err
= bitmap_create(mddev
);
4379 printk(KERN_ERR
"%s: failed to create bitmap (%d)\n",
4380 mdname(mddev
), err
);
4381 mddev
->pers
->stop(mddev
);
4385 module_put(mddev
->pers
->owner
);
4387 bitmap_destroy(mddev
);
4390 if (mddev
->pers
->sync_request
) {
4391 if (sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
4393 "md: cannot register extra attributes for %s\n",
4395 mddev
->sysfs_action
= sysfs_get_dirent(mddev
->kobj
.sd
, "sync_action");
4396 } else if (mddev
->ro
== 2) /* auto-readonly not meaningful */
4399 atomic_set(&mddev
->writes_pending
,0);
4400 atomic_set(&mddev
->max_corr_read_errors
,
4401 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS
);
4402 mddev
->safemode
= 0;
4403 mddev
->safemode_timer
.function
= md_safemode_timeout
;
4404 mddev
->safemode_timer
.data
= (unsigned long) mddev
;
4405 mddev
->safemode_delay
= (200 * HZ
)/1000 +1; /* 200 msec delay */
4408 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4409 if (rdev
->raid_disk
>= 0) {
4411 sprintf(nm
, "rd%d", rdev
->raid_disk
);
4412 if (sysfs_create_link(&mddev
->kobj
, &rdev
->kobj
, nm
))
4413 printk("md: cannot register %s for %s\n",
4417 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4420 md_update_sb(mddev
, 0);
4422 set_capacity(disk
, mddev
->array_sectors
);
4424 md_wakeup_thread(mddev
->thread
);
4425 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
4427 revalidate_disk(mddev
->gendisk
);
4429 md_new_event(mddev
);
4430 sysfs_notify_dirent(mddev
->sysfs_state
);
4431 if (mddev
->sysfs_action
)
4432 sysfs_notify_dirent(mddev
->sysfs_action
);
4433 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
4434 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
4438 static int restart_array(mddev_t
*mddev
)
4440 struct gendisk
*disk
= mddev
->gendisk
;
4442 /* Complain if it has no devices */
4443 if (list_empty(&mddev
->disks
))
4449 mddev
->safemode
= 0;
4451 set_disk_ro(disk
, 0);
4452 printk(KERN_INFO
"md: %s switched to read-write mode.\n",
4454 /* Kick recovery or resync if necessary */
4455 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4456 md_wakeup_thread(mddev
->thread
);
4457 md_wakeup_thread(mddev
->sync_thread
);
4458 sysfs_notify_dirent(mddev
->sysfs_state
);
4462 /* similar to deny_write_access, but accounts for our holding a reference
4463 * to the file ourselves */
4464 static int deny_bitmap_write_access(struct file
* file
)
4466 struct inode
*inode
= file
->f_mapping
->host
;
4468 spin_lock(&inode
->i_lock
);
4469 if (atomic_read(&inode
->i_writecount
) > 1) {
4470 spin_unlock(&inode
->i_lock
);
4473 atomic_set(&inode
->i_writecount
, -1);
4474 spin_unlock(&inode
->i_lock
);
4479 void restore_bitmap_write_access(struct file
*file
)
4481 struct inode
*inode
= file
->f_mapping
->host
;
4483 spin_lock(&inode
->i_lock
);
4484 atomic_set(&inode
->i_writecount
, 1);
4485 spin_unlock(&inode
->i_lock
);
4489 * 0 - completely stop and dis-assemble array
4490 * 1 - switch to readonly
4491 * 2 - stop but do not disassemble array
4493 static int do_md_stop(mddev_t
* mddev
, int mode
, int is_open
)
4496 struct gendisk
*disk
= mddev
->gendisk
;
4499 mutex_lock(&mddev
->open_mutex
);
4500 if (atomic_read(&mddev
->openers
) > is_open
) {
4501 printk("md: %s still in use.\n",mdname(mddev
));
4503 } else if (mddev
->pers
) {
4505 if (mddev
->sync_thread
) {
4506 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4507 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4508 md_unregister_thread(mddev
->sync_thread
);
4509 mddev
->sync_thread
= NULL
;
4512 del_timer_sync(&mddev
->safemode_timer
);
4515 case 1: /* readonly */
4521 case 0: /* disassemble */
4523 bitmap_flush(mddev
);
4524 md_super_wait(mddev
);
4526 set_disk_ro(disk
, 0);
4528 mddev
->pers
->stop(mddev
);
4529 mddev
->queue
->merge_bvec_fn
= NULL
;
4530 mddev
->queue
->unplug_fn
= NULL
;
4531 mddev
->queue
->backing_dev_info
.congested_fn
= NULL
;
4532 module_put(mddev
->pers
->owner
);
4533 if (mddev
->pers
->sync_request
&& mddev
->private == NULL
)
4534 mddev
->private = (void*)1;
4536 /* tell userspace to handle 'inactive' */
4537 sysfs_notify_dirent(mddev
->sysfs_state
);
4539 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4540 if (rdev
->raid_disk
>= 0) {
4542 sprintf(nm
, "rd%d", rdev
->raid_disk
);
4543 sysfs_remove_link(&mddev
->kobj
, nm
);
4546 set_capacity(disk
, 0);
4552 if (!mddev
->in_sync
|| mddev
->flags
) {
4553 /* mark array as shutdown cleanly */
4555 md_update_sb(mddev
, 1);
4558 set_disk_ro(disk
, 1);
4559 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4563 mutex_unlock(&mddev
->open_mutex
);
4567 * Free resources if final stop
4571 printk(KERN_INFO
"md: %s stopped.\n", mdname(mddev
));
4573 bitmap_destroy(mddev
);
4574 if (mddev
->bitmap_info
.file
) {
4575 restore_bitmap_write_access(mddev
->bitmap_info
.file
);
4576 fput(mddev
->bitmap_info
.file
);
4577 mddev
->bitmap_info
.file
= NULL
;
4579 mddev
->bitmap_info
.offset
= 0;
4581 export_array(mddev
);
4583 mddev
->array_sectors
= 0;
4584 mddev
->external_size
= 0;
4585 mddev
->dev_sectors
= 0;
4586 mddev
->raid_disks
= 0;
4587 mddev
->recovery_cp
= 0;
4588 mddev
->resync_min
= 0;
4589 mddev
->resync_max
= MaxSector
;
4590 mddev
->reshape_position
= MaxSector
;
4591 mddev
->external
= 0;
4592 mddev
->persistent
= 0;
4593 mddev
->level
= LEVEL_NONE
;
4594 mddev
->clevel
[0] = 0;
4597 mddev
->metadata_type
[0] = 0;
4598 mddev
->chunk_sectors
= 0;
4599 mddev
->ctime
= mddev
->utime
= 0;
4601 mddev
->max_disks
= 0;
4603 mddev
->delta_disks
= 0;
4604 mddev
->new_level
= LEVEL_NONE
;
4605 mddev
->new_layout
= 0;
4606 mddev
->new_chunk_sectors
= 0;
4607 mddev
->curr_resync
= 0;
4608 mddev
->resync_mismatches
= 0;
4609 mddev
->suspend_lo
= mddev
->suspend_hi
= 0;
4610 mddev
->sync_speed_min
= mddev
->sync_speed_max
= 0;
4611 mddev
->recovery
= 0;
4614 mddev
->degraded
= 0;
4615 mddev
->barriers_work
= 0;
4616 mddev
->safemode
= 0;
4617 mddev
->bitmap_info
.offset
= 0;
4618 mddev
->bitmap_info
.default_offset
= 0;
4619 mddev
->bitmap_info
.chunksize
= 0;
4620 mddev
->bitmap_info
.daemon_sleep
= 0;
4621 mddev
->bitmap_info
.max_write_behind
= 0;
4622 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
4623 if (mddev
->hold_active
== UNTIL_STOP
)
4624 mddev
->hold_active
= 0;
4626 } else if (mddev
->pers
)
4627 printk(KERN_INFO
"md: %s switched to read-only mode.\n",
4630 blk_integrity_unregister(disk
);
4631 md_new_event(mddev
);
4632 sysfs_notify_dirent(mddev
->sysfs_state
);
4637 static void autorun_array(mddev_t
*mddev
)
4642 if (list_empty(&mddev
->disks
))
4645 printk(KERN_INFO
"md: running: ");
4647 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
4648 char b
[BDEVNAME_SIZE
];
4649 printk("<%s>", bdevname(rdev
->bdev
,b
));
4653 err
= do_md_run(mddev
);
4655 printk(KERN_WARNING
"md: do_md_run() returned %d\n", err
);
4656 do_md_stop(mddev
, 0, 0);
4661 * lets try to run arrays based on all disks that have arrived
4662 * until now. (those are in pending_raid_disks)
4664 * the method: pick the first pending disk, collect all disks with
4665 * the same UUID, remove all from the pending list and put them into
4666 * the 'same_array' list. Then order this list based on superblock
4667 * update time (freshest comes first), kick out 'old' disks and
4668 * compare superblocks. If everything's fine then run it.
4670 * If "unit" is allocated, then bump its reference count
4672 static void autorun_devices(int part
)
4674 mdk_rdev_t
*rdev0
, *rdev
, *tmp
;
4676 char b
[BDEVNAME_SIZE
];
4678 printk(KERN_INFO
"md: autorun ...\n");
4679 while (!list_empty(&pending_raid_disks
)) {
4682 LIST_HEAD(candidates
);
4683 rdev0
= list_entry(pending_raid_disks
.next
,
4684 mdk_rdev_t
, same_set
);
4686 printk(KERN_INFO
"md: considering %s ...\n",
4687 bdevname(rdev0
->bdev
,b
));
4688 INIT_LIST_HEAD(&candidates
);
4689 rdev_for_each_list(rdev
, tmp
, &pending_raid_disks
)
4690 if (super_90_load(rdev
, rdev0
, 0) >= 0) {
4691 printk(KERN_INFO
"md: adding %s ...\n",
4692 bdevname(rdev
->bdev
,b
));
4693 list_move(&rdev
->same_set
, &candidates
);
4696 * now we have a set of devices, with all of them having
4697 * mostly sane superblocks. It's time to allocate the
4701 dev
= MKDEV(mdp_major
,
4702 rdev0
->preferred_minor
<< MdpMinorShift
);
4703 unit
= MINOR(dev
) >> MdpMinorShift
;
4705 dev
= MKDEV(MD_MAJOR
, rdev0
->preferred_minor
);
4708 if (rdev0
->preferred_minor
!= unit
) {
4709 printk(KERN_INFO
"md: unit number in %s is bad: %d\n",
4710 bdevname(rdev0
->bdev
, b
), rdev0
->preferred_minor
);
4714 md_probe(dev
, NULL
, NULL
);
4715 mddev
= mddev_find(dev
);
4716 if (!mddev
|| !mddev
->gendisk
) {
4720 "md: cannot allocate memory for md drive.\n");
4723 if (mddev_lock(mddev
))
4724 printk(KERN_WARNING
"md: %s locked, cannot run\n",
4726 else if (mddev
->raid_disks
|| mddev
->major_version
4727 || !list_empty(&mddev
->disks
)) {
4729 "md: %s already running, cannot run %s\n",
4730 mdname(mddev
), bdevname(rdev0
->bdev
,b
));
4731 mddev_unlock(mddev
);
4733 printk(KERN_INFO
"md: created %s\n", mdname(mddev
));
4734 mddev
->persistent
= 1;
4735 rdev_for_each_list(rdev
, tmp
, &candidates
) {
4736 list_del_init(&rdev
->same_set
);
4737 if (bind_rdev_to_array(rdev
, mddev
))
4740 autorun_array(mddev
);
4741 mddev_unlock(mddev
);
4743 /* on success, candidates will be empty, on error
4746 rdev_for_each_list(rdev
, tmp
, &candidates
) {
4747 list_del_init(&rdev
->same_set
);
4752 printk(KERN_INFO
"md: ... autorun DONE.\n");
4754 #endif /* !MODULE */
4756 static int get_version(void __user
* arg
)
4760 ver
.major
= MD_MAJOR_VERSION
;
4761 ver
.minor
= MD_MINOR_VERSION
;
4762 ver
.patchlevel
= MD_PATCHLEVEL_VERSION
;
4764 if (copy_to_user(arg
, &ver
, sizeof(ver
)))
4770 static int get_array_info(mddev_t
* mddev
, void __user
* arg
)
4772 mdu_array_info_t info
;
4773 int nr
,working
,insync
,failed
,spare
;
4776 nr
=working
=insync
=failed
=spare
=0;
4777 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
4779 if (test_bit(Faulty
, &rdev
->flags
))
4783 if (test_bit(In_sync
, &rdev
->flags
))
4790 info
.major_version
= mddev
->major_version
;
4791 info
.minor_version
= mddev
->minor_version
;
4792 info
.patch_version
= MD_PATCHLEVEL_VERSION
;
4793 info
.ctime
= mddev
->ctime
;
4794 info
.level
= mddev
->level
;
4795 info
.size
= mddev
->dev_sectors
/ 2;
4796 if (info
.size
!= mddev
->dev_sectors
/ 2) /* overflow */
4799 info
.raid_disks
= mddev
->raid_disks
;
4800 info
.md_minor
= mddev
->md_minor
;
4801 info
.not_persistent
= !mddev
->persistent
;
4803 info
.utime
= mddev
->utime
;
4806 info
.state
= (1<<MD_SB_CLEAN
);
4807 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
4808 info
.state
= (1<<MD_SB_BITMAP_PRESENT
);
4809 info
.active_disks
= insync
;
4810 info
.working_disks
= working
;
4811 info
.failed_disks
= failed
;
4812 info
.spare_disks
= spare
;
4814 info
.layout
= mddev
->layout
;
4815 info
.chunk_size
= mddev
->chunk_sectors
<< 9;
4817 if (copy_to_user(arg
, &info
, sizeof(info
)))
4823 static int get_bitmap_file(mddev_t
* mddev
, void __user
* arg
)
4825 mdu_bitmap_file_t
*file
= NULL
; /* too big for stack allocation */
4826 char *ptr
, *buf
= NULL
;
4829 if (md_allow_write(mddev
))
4830 file
= kmalloc(sizeof(*file
), GFP_NOIO
);
4832 file
= kmalloc(sizeof(*file
), GFP_KERNEL
);
4837 /* bitmap disabled, zero the first byte and copy out */
4838 if (!mddev
->bitmap
|| !mddev
->bitmap
->file
) {
4839 file
->pathname
[0] = '\0';
4843 buf
= kmalloc(sizeof(file
->pathname
), GFP_KERNEL
);
4847 ptr
= d_path(&mddev
->bitmap
->file
->f_path
, buf
, sizeof(file
->pathname
));
4851 strcpy(file
->pathname
, ptr
);
4855 if (copy_to_user(arg
, file
, sizeof(*file
)))
4863 static int get_disk_info(mddev_t
* mddev
, void __user
* arg
)
4865 mdu_disk_info_t info
;
4868 if (copy_from_user(&info
, arg
, sizeof(info
)))
4871 rdev
= find_rdev_nr(mddev
, info
.number
);
4873 info
.major
= MAJOR(rdev
->bdev
->bd_dev
);
4874 info
.minor
= MINOR(rdev
->bdev
->bd_dev
);
4875 info
.raid_disk
= rdev
->raid_disk
;
4877 if (test_bit(Faulty
, &rdev
->flags
))
4878 info
.state
|= (1<<MD_DISK_FAULTY
);
4879 else if (test_bit(In_sync
, &rdev
->flags
)) {
4880 info
.state
|= (1<<MD_DISK_ACTIVE
);
4881 info
.state
|= (1<<MD_DISK_SYNC
);
4883 if (test_bit(WriteMostly
, &rdev
->flags
))
4884 info
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
4886 info
.major
= info
.minor
= 0;
4887 info
.raid_disk
= -1;
4888 info
.state
= (1<<MD_DISK_REMOVED
);
4891 if (copy_to_user(arg
, &info
, sizeof(info
)))
4897 static int add_new_disk(mddev_t
* mddev
, mdu_disk_info_t
*info
)
4899 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
4901 dev_t dev
= MKDEV(info
->major
,info
->minor
);
4903 if (info
->major
!= MAJOR(dev
) || info
->minor
!= MINOR(dev
))
4906 if (!mddev
->raid_disks
) {
4908 /* expecting a device which has a superblock */
4909 rdev
= md_import_device(dev
, mddev
->major_version
, mddev
->minor_version
);
4912 "md: md_import_device returned %ld\n",
4914 return PTR_ERR(rdev
);
4916 if (!list_empty(&mddev
->disks
)) {
4917 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
4918 mdk_rdev_t
, same_set
);
4919 err
= super_types
[mddev
->major_version
]
4920 .load_super(rdev
, rdev0
, mddev
->minor_version
);
4923 "md: %s has different UUID to %s\n",
4924 bdevname(rdev
->bdev
,b
),
4925 bdevname(rdev0
->bdev
,b2
));
4930 err
= bind_rdev_to_array(rdev
, mddev
);
4937 * add_new_disk can be used once the array is assembled
4938 * to add "hot spares". They must already have a superblock
4943 if (!mddev
->pers
->hot_add_disk
) {
4945 "%s: personality does not support diskops!\n",
4949 if (mddev
->persistent
)
4950 rdev
= md_import_device(dev
, mddev
->major_version
,
4951 mddev
->minor_version
);
4953 rdev
= md_import_device(dev
, -1, -1);
4956 "md: md_import_device returned %ld\n",
4958 return PTR_ERR(rdev
);
4960 /* set save_raid_disk if appropriate */
4961 if (!mddev
->persistent
) {
4962 if (info
->state
& (1<<MD_DISK_SYNC
) &&
4963 info
->raid_disk
< mddev
->raid_disks
)
4964 rdev
->raid_disk
= info
->raid_disk
;
4966 rdev
->raid_disk
= -1;
4968 super_types
[mddev
->major_version
].
4969 validate_super(mddev
, rdev
);
4970 rdev
->saved_raid_disk
= rdev
->raid_disk
;
4972 clear_bit(In_sync
, &rdev
->flags
); /* just to be sure */
4973 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
4974 set_bit(WriteMostly
, &rdev
->flags
);
4976 clear_bit(WriteMostly
, &rdev
->flags
);
4978 rdev
->raid_disk
= -1;
4979 err
= bind_rdev_to_array(rdev
, mddev
);
4980 if (!err
&& !mddev
->pers
->hot_remove_disk
) {
4981 /* If there is hot_add_disk but no hot_remove_disk
4982 * then added disks for geometry changes,
4983 * and should be added immediately.
4985 super_types
[mddev
->major_version
].
4986 validate_super(mddev
, rdev
);
4987 err
= mddev
->pers
->hot_add_disk(mddev
, rdev
);
4989 unbind_rdev_from_array(rdev
);
4994 sysfs_notify_dirent(rdev
->sysfs_state
);
4996 md_update_sb(mddev
, 1);
4997 if (mddev
->degraded
)
4998 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
4999 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5000 md_wakeup_thread(mddev
->thread
);
5004 /* otherwise, add_new_disk is only allowed
5005 * for major_version==0 superblocks
5007 if (mddev
->major_version
!= 0) {
5008 printk(KERN_WARNING
"%s: ADD_NEW_DISK not supported\n",
5013 if (!(info
->state
& (1<<MD_DISK_FAULTY
))) {
5015 rdev
= md_import_device(dev
, -1, 0);
5018 "md: error, md_import_device() returned %ld\n",
5020 return PTR_ERR(rdev
);
5022 rdev
->desc_nr
= info
->number
;
5023 if (info
->raid_disk
< mddev
->raid_disks
)
5024 rdev
->raid_disk
= info
->raid_disk
;
5026 rdev
->raid_disk
= -1;
5028 if (rdev
->raid_disk
< mddev
->raid_disks
)
5029 if (info
->state
& (1<<MD_DISK_SYNC
))
5030 set_bit(In_sync
, &rdev
->flags
);
5032 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
5033 set_bit(WriteMostly
, &rdev
->flags
);
5035 if (!mddev
->persistent
) {
5036 printk(KERN_INFO
"md: nonpersistent superblock ...\n");
5037 rdev
->sb_start
= rdev
->bdev
->bd_inode
->i_size
/ 512;
5039 rdev
->sb_start
= calc_dev_sboffset(rdev
->bdev
);
5040 rdev
->sectors
= rdev
->sb_start
;
5042 err
= bind_rdev_to_array(rdev
, mddev
);
5052 static int hot_remove_disk(mddev_t
* mddev
, dev_t dev
)
5054 char b
[BDEVNAME_SIZE
];
5057 rdev
= find_rdev(mddev
, dev
);
5061 if (rdev
->raid_disk
>= 0)
5064 kick_rdev_from_array(rdev
);
5065 md_update_sb(mddev
, 1);
5066 md_new_event(mddev
);
5070 printk(KERN_WARNING
"md: cannot remove active disk %s from %s ...\n",
5071 bdevname(rdev
->bdev
,b
), mdname(mddev
));
5075 static int hot_add_disk(mddev_t
* mddev
, dev_t dev
)
5077 char b
[BDEVNAME_SIZE
];
5084 if (mddev
->major_version
!= 0) {
5085 printk(KERN_WARNING
"%s: HOT_ADD may only be used with"
5086 " version-0 superblocks.\n",
5090 if (!mddev
->pers
->hot_add_disk
) {
5092 "%s: personality does not support diskops!\n",
5097 rdev
= md_import_device(dev
, -1, 0);
5100 "md: error, md_import_device() returned %ld\n",
5105 if (mddev
->persistent
)
5106 rdev
->sb_start
= calc_dev_sboffset(rdev
->bdev
);
5108 rdev
->sb_start
= rdev
->bdev
->bd_inode
->i_size
/ 512;
5110 rdev
->sectors
= rdev
->sb_start
;
5112 if (test_bit(Faulty
, &rdev
->flags
)) {
5114 "md: can not hot-add faulty %s disk to %s!\n",
5115 bdevname(rdev
->bdev
,b
), mdname(mddev
));
5119 clear_bit(In_sync
, &rdev
->flags
);
5121 rdev
->saved_raid_disk
= -1;
5122 err
= bind_rdev_to_array(rdev
, mddev
);
5127 * The rest should better be atomic, we can have disk failures
5128 * noticed in interrupt contexts ...
5131 rdev
->raid_disk
= -1;
5133 md_update_sb(mddev
, 1);
5136 * Kick recovery, maybe this spare has to be added to the
5137 * array immediately.
5139 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5140 md_wakeup_thread(mddev
->thread
);
5141 md_new_event(mddev
);
5149 static int set_bitmap_file(mddev_t
*mddev
, int fd
)
5154 if (!mddev
->pers
->quiesce
)
5156 if (mddev
->recovery
|| mddev
->sync_thread
)
5158 /* we should be able to change the bitmap.. */
5164 return -EEXIST
; /* cannot add when bitmap is present */
5165 mddev
->bitmap_info
.file
= fget(fd
);
5167 if (mddev
->bitmap_info
.file
== NULL
) {
5168 printk(KERN_ERR
"%s: error: failed to get bitmap file\n",
5173 err
= deny_bitmap_write_access(mddev
->bitmap_info
.file
);
5175 printk(KERN_ERR
"%s: error: bitmap file is already in use\n",
5177 fput(mddev
->bitmap_info
.file
);
5178 mddev
->bitmap_info
.file
= NULL
;
5181 mddev
->bitmap_info
.offset
= 0; /* file overrides offset */
5182 } else if (mddev
->bitmap
== NULL
)
5183 return -ENOENT
; /* cannot remove what isn't there */
5186 mddev
->pers
->quiesce(mddev
, 1);
5188 err
= bitmap_create(mddev
);
5189 if (fd
< 0 || err
) {
5190 bitmap_destroy(mddev
);
5191 fd
= -1; /* make sure to put the file */
5193 mddev
->pers
->quiesce(mddev
, 0);
5196 if (mddev
->bitmap_info
.file
) {
5197 restore_bitmap_write_access(mddev
->bitmap_info
.file
);
5198 fput(mddev
->bitmap_info
.file
);
5200 mddev
->bitmap_info
.file
= NULL
;
5207 * set_array_info is used two different ways
5208 * The original usage is when creating a new array.
5209 * In this usage, raid_disks is > 0 and it together with
5210 * level, size, not_persistent,layout,chunksize determine the
5211 * shape of the array.
5212 * This will always create an array with a type-0.90.0 superblock.
5213 * The newer usage is when assembling an array.
5214 * In this case raid_disks will be 0, and the major_version field is
5215 * use to determine which style super-blocks are to be found on the devices.
5216 * The minor and patch _version numbers are also kept incase the
5217 * super_block handler wishes to interpret them.
5219 static int set_array_info(mddev_t
* mddev
, mdu_array_info_t
*info
)
5222 if (info
->raid_disks
== 0) {
5223 /* just setting version number for superblock loading */
5224 if (info
->major_version
< 0 ||
5225 info
->major_version
>= ARRAY_SIZE(super_types
) ||
5226 super_types
[info
->major_version
].name
== NULL
) {
5227 /* maybe try to auto-load a module? */
5229 "md: superblock version %d not known\n",
5230 info
->major_version
);
5233 mddev
->major_version
= info
->major_version
;
5234 mddev
->minor_version
= info
->minor_version
;
5235 mddev
->patch_version
= info
->patch_version
;
5236 mddev
->persistent
= !info
->not_persistent
;
5237 /* ensure mddev_put doesn't delete this now that there
5238 * is some minimal configuration.
5240 mddev
->ctime
= get_seconds();
5243 mddev
->major_version
= MD_MAJOR_VERSION
;
5244 mddev
->minor_version
= MD_MINOR_VERSION
;
5245 mddev
->patch_version
= MD_PATCHLEVEL_VERSION
;
5246 mddev
->ctime
= get_seconds();
5248 mddev
->level
= info
->level
;
5249 mddev
->clevel
[0] = 0;
5250 mddev
->dev_sectors
= 2 * (sector_t
)info
->size
;
5251 mddev
->raid_disks
= info
->raid_disks
;
5252 /* don't set md_minor, it is determined by which /dev/md* was
5255 if (info
->state
& (1<<MD_SB_CLEAN
))
5256 mddev
->recovery_cp
= MaxSector
;
5258 mddev
->recovery_cp
= 0;
5259 mddev
->persistent
= ! info
->not_persistent
;
5260 mddev
->external
= 0;
5262 mddev
->layout
= info
->layout
;
5263 mddev
->chunk_sectors
= info
->chunk_size
>> 9;
5265 mddev
->max_disks
= MD_SB_DISKS
;
5267 if (mddev
->persistent
)
5269 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
5271 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
5272 mddev
->bitmap_info
.offset
= 0;
5274 mddev
->reshape_position
= MaxSector
;
5277 * Generate a 128 bit UUID
5279 get_random_bytes(mddev
->uuid
, 16);
5281 mddev
->new_level
= mddev
->level
;
5282 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
5283 mddev
->new_layout
= mddev
->layout
;
5284 mddev
->delta_disks
= 0;
5289 void md_set_array_sectors(mddev_t
*mddev
, sector_t array_sectors
)
5291 WARN(!mddev_is_locked(mddev
), "%s: unlocked mddev!\n", __func__
);
5293 if (mddev
->external_size
)
5296 mddev
->array_sectors
= array_sectors
;
5298 EXPORT_SYMBOL(md_set_array_sectors
);
5300 static int update_size(mddev_t
*mddev
, sector_t num_sectors
)
5304 int fit
= (num_sectors
== 0);
5306 if (mddev
->pers
->resize
== NULL
)
5308 /* The "num_sectors" is the number of sectors of each device that
5309 * is used. This can only make sense for arrays with redundancy.
5310 * linear and raid0 always use whatever space is available. We can only
5311 * consider changing this number if no resync or reconstruction is
5312 * happening, and if the new size is acceptable. It must fit before the
5313 * sb_start or, if that is <data_offset, it must fit before the size
5314 * of each device. If num_sectors is zero, we find the largest size
5318 if (mddev
->sync_thread
)
5321 /* Sorry, cannot grow a bitmap yet, just remove it,
5325 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
5326 sector_t avail
= rdev
->sectors
;
5328 if (fit
&& (num_sectors
== 0 || num_sectors
> avail
))
5329 num_sectors
= avail
;
5330 if (avail
< num_sectors
)
5333 rv
= mddev
->pers
->resize(mddev
, num_sectors
);
5335 revalidate_disk(mddev
->gendisk
);
5339 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
)
5342 /* change the number of raid disks */
5343 if (mddev
->pers
->check_reshape
== NULL
)
5345 if (raid_disks
<= 0 ||
5346 raid_disks
>= mddev
->max_disks
)
5348 if (mddev
->sync_thread
|| mddev
->reshape_position
!= MaxSector
)
5350 mddev
->delta_disks
= raid_disks
- mddev
->raid_disks
;
5352 rv
= mddev
->pers
->check_reshape(mddev
);
5358 * update_array_info is used to change the configuration of an
5360 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5361 * fields in the info are checked against the array.
5362 * Any differences that cannot be handled will cause an error.
5363 * Normally, only one change can be managed at a time.
5365 static int update_array_info(mddev_t
*mddev
, mdu_array_info_t
*info
)
5371 /* calculate expected state,ignoring low bits */
5372 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
5373 state
|= (1 << MD_SB_BITMAP_PRESENT
);
5375 if (mddev
->major_version
!= info
->major_version
||
5376 mddev
->minor_version
!= info
->minor_version
||
5377 /* mddev->patch_version != info->patch_version || */
5378 mddev
->ctime
!= info
->ctime
||
5379 mddev
->level
!= info
->level
||
5380 /* mddev->layout != info->layout || */
5381 !mddev
->persistent
!= info
->not_persistent
||
5382 mddev
->chunk_sectors
!= info
->chunk_size
>> 9 ||
5383 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5384 ((state
^info
->state
) & 0xfffffe00)
5387 /* Check there is only one change */
5388 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
5390 if (mddev
->raid_disks
!= info
->raid_disks
)
5392 if (mddev
->layout
!= info
->layout
)
5394 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
))
5401 if (mddev
->layout
!= info
->layout
) {
5403 * we don't need to do anything at the md level, the
5404 * personality will take care of it all.
5406 if (mddev
->pers
->check_reshape
== NULL
)
5409 mddev
->new_layout
= info
->layout
;
5410 rv
= mddev
->pers
->check_reshape(mddev
);
5412 mddev
->new_layout
= mddev
->layout
;
5416 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
5417 rv
= update_size(mddev
, (sector_t
)info
->size
* 2);
5419 if (mddev
->raid_disks
!= info
->raid_disks
)
5420 rv
= update_raid_disks(mddev
, info
->raid_disks
);
5422 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) {
5423 if (mddev
->pers
->quiesce
== NULL
)
5425 if (mddev
->recovery
|| mddev
->sync_thread
)
5427 if (info
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
5428 /* add the bitmap */
5431 if (mddev
->bitmap_info
.default_offset
== 0)
5433 mddev
->bitmap_info
.offset
=
5434 mddev
->bitmap_info
.default_offset
;
5435 mddev
->pers
->quiesce(mddev
, 1);
5436 rv
= bitmap_create(mddev
);
5438 bitmap_destroy(mddev
);
5439 mddev
->pers
->quiesce(mddev
, 0);
5441 /* remove the bitmap */
5444 if (mddev
->bitmap
->file
)
5446 mddev
->pers
->quiesce(mddev
, 1);
5447 bitmap_destroy(mddev
);
5448 mddev
->pers
->quiesce(mddev
, 0);
5449 mddev
->bitmap_info
.offset
= 0;
5452 md_update_sb(mddev
, 1);
5456 static int set_disk_faulty(mddev_t
*mddev
, dev_t dev
)
5460 if (mddev
->pers
== NULL
)
5463 rdev
= find_rdev(mddev
, dev
);
5467 md_error(mddev
, rdev
);
5472 * We have a problem here : there is no easy way to give a CHS
5473 * virtual geometry. We currently pretend that we have a 2 heads
5474 * 4 sectors (with a BIG number of cylinders...). This drives
5475 * dosfs just mad... ;-)
5477 static int md_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
5479 mddev_t
*mddev
= bdev
->bd_disk
->private_data
;
5483 geo
->cylinders
= get_capacity(mddev
->gendisk
) / 8;
5487 static int md_ioctl(struct block_device
*bdev
, fmode_t mode
,
5488 unsigned int cmd
, unsigned long arg
)
5491 void __user
*argp
= (void __user
*)arg
;
5492 mddev_t
*mddev
= NULL
;
5494 if (!capable(CAP_SYS_ADMIN
))
5498 * Commands dealing with the RAID driver but not any
5504 err
= get_version(argp
);
5507 case PRINT_RAID_DEBUG
:
5515 autostart_arrays(arg
);
5522 * Commands creating/starting a new array:
5525 mddev
= bdev
->bd_disk
->private_data
;
5532 err
= mddev_lock(mddev
);
5535 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5542 case SET_ARRAY_INFO
:
5544 mdu_array_info_t info
;
5546 memset(&info
, 0, sizeof(info
));
5547 else if (copy_from_user(&info
, argp
, sizeof(info
))) {
5552 err
= update_array_info(mddev
, &info
);
5554 printk(KERN_WARNING
"md: couldn't update"
5555 " array info. %d\n", err
);
5560 if (!list_empty(&mddev
->disks
)) {
5562 "md: array %s already has disks!\n",
5567 if (mddev
->raid_disks
) {
5569 "md: array %s already initialised!\n",
5574 err
= set_array_info(mddev
, &info
);
5576 printk(KERN_WARNING
"md: couldn't set"
5577 " array info. %d\n", err
);
5587 * Commands querying/configuring an existing array:
5589 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
5590 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
5591 if ((!mddev
->raid_disks
&& !mddev
->external
)
5592 && cmd
!= ADD_NEW_DISK
&& cmd
!= STOP_ARRAY
5593 && cmd
!= RUN_ARRAY
&& cmd
!= SET_BITMAP_FILE
5594 && cmd
!= GET_BITMAP_FILE
) {
5600 * Commands even a read-only array can execute:
5604 case GET_ARRAY_INFO
:
5605 err
= get_array_info(mddev
, argp
);
5608 case GET_BITMAP_FILE
:
5609 err
= get_bitmap_file(mddev
, argp
);
5613 err
= get_disk_info(mddev
, argp
);
5616 case RESTART_ARRAY_RW
:
5617 err
= restart_array(mddev
);
5621 err
= do_md_stop(mddev
, 0, 1);
5625 err
= do_md_stop(mddev
, 1, 1);
5631 * The remaining ioctls are changing the state of the
5632 * superblock, so we do not allow them on read-only arrays.
5633 * However non-MD ioctls (e.g. get-size) will still come through
5634 * here and hit the 'default' below, so only disallow
5635 * 'md' ioctls, and switch to rw mode if started auto-readonly.
5637 if (_IOC_TYPE(cmd
) == MD_MAJOR
&& mddev
->ro
&& mddev
->pers
) {
5638 if (mddev
->ro
== 2) {
5640 sysfs_notify_dirent(mddev
->sysfs_state
);
5641 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5642 md_wakeup_thread(mddev
->thread
);
5653 mdu_disk_info_t info
;
5654 if (copy_from_user(&info
, argp
, sizeof(info
)))
5657 err
= add_new_disk(mddev
, &info
);
5661 case HOT_REMOVE_DISK
:
5662 err
= hot_remove_disk(mddev
, new_decode_dev(arg
));
5666 err
= hot_add_disk(mddev
, new_decode_dev(arg
));
5669 case SET_DISK_FAULTY
:
5670 err
= set_disk_faulty(mddev
, new_decode_dev(arg
));
5674 err
= do_md_run(mddev
);
5677 case SET_BITMAP_FILE
:
5678 err
= set_bitmap_file(mddev
, (int)arg
);
5688 if (mddev
->hold_active
== UNTIL_IOCTL
&&
5690 mddev
->hold_active
= 0;
5691 mddev_unlock(mddev
);
5700 #ifdef CONFIG_COMPAT
5701 static int md_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
5702 unsigned int cmd
, unsigned long arg
)
5705 case HOT_REMOVE_DISK
:
5707 case SET_DISK_FAULTY
:
5708 case SET_BITMAP_FILE
:
5709 /* These take in integer arg, do not convert */
5712 arg
= (unsigned long)compat_ptr(arg
);
5716 return md_ioctl(bdev
, mode
, cmd
, arg
);
5718 #endif /* CONFIG_COMPAT */
5720 static int md_open(struct block_device
*bdev
, fmode_t mode
)
5723 * Succeed if we can lock the mddev, which confirms that
5724 * it isn't being stopped right now.
5726 mddev_t
*mddev
= mddev_find(bdev
->bd_dev
);
5729 if (mddev
->gendisk
!= bdev
->bd_disk
) {
5730 /* we are racing with mddev_put which is discarding this
5734 /* Wait until bdev->bd_disk is definitely gone */
5735 flush_scheduled_work();
5736 /* Then retry the open from the top */
5737 return -ERESTARTSYS
;
5739 BUG_ON(mddev
!= bdev
->bd_disk
->private_data
);
5741 if ((err
= mutex_lock_interruptible(&mddev
->open_mutex
)))
5745 atomic_inc(&mddev
->openers
);
5746 mutex_unlock(&mddev
->open_mutex
);
5748 check_disk_change(bdev
);
5753 static int md_release(struct gendisk
*disk
, fmode_t mode
)
5755 mddev_t
*mddev
= disk
->private_data
;
5758 atomic_dec(&mddev
->openers
);
5764 static int md_media_changed(struct gendisk
*disk
)
5766 mddev_t
*mddev
= disk
->private_data
;
5768 return mddev
->changed
;
5771 static int md_revalidate(struct gendisk
*disk
)
5773 mddev_t
*mddev
= disk
->private_data
;
5778 static const struct block_device_operations md_fops
=
5780 .owner
= THIS_MODULE
,
5782 .release
= md_release
,
5784 #ifdef CONFIG_COMPAT
5785 .compat_ioctl
= md_compat_ioctl
,
5787 .getgeo
= md_getgeo
,
5788 .media_changed
= md_media_changed
,
5789 .revalidate_disk
= md_revalidate
,
5792 static int md_thread(void * arg
)
5794 mdk_thread_t
*thread
= arg
;
5797 * md_thread is a 'system-thread', it's priority should be very
5798 * high. We avoid resource deadlocks individually in each
5799 * raid personality. (RAID5 does preallocation) We also use RR and
5800 * the very same RT priority as kswapd, thus we will never get
5801 * into a priority inversion deadlock.
5803 * we definitely have to have equal or higher priority than
5804 * bdflush, otherwise bdflush will deadlock if there are too
5805 * many dirty RAID5 blocks.
5808 allow_signal(SIGKILL
);
5809 while (!kthread_should_stop()) {
5811 /* We need to wait INTERRUPTIBLE so that
5812 * we don't add to the load-average.
5813 * That means we need to be sure no signals are
5816 if (signal_pending(current
))
5817 flush_signals(current
);
5819 wait_event_interruptible_timeout
5821 test_bit(THREAD_WAKEUP
, &thread
->flags
)
5822 || kthread_should_stop(),
5825 clear_bit(THREAD_WAKEUP
, &thread
->flags
);
5827 thread
->run(thread
->mddev
);
5833 void md_wakeup_thread(mdk_thread_t
*thread
)
5836 dprintk("md: waking up MD thread %s.\n", thread
->tsk
->comm
);
5837 set_bit(THREAD_WAKEUP
, &thread
->flags
);
5838 wake_up(&thread
->wqueue
);
5842 mdk_thread_t
*md_register_thread(void (*run
) (mddev_t
*), mddev_t
*mddev
,
5845 mdk_thread_t
*thread
;
5847 thread
= kzalloc(sizeof(mdk_thread_t
), GFP_KERNEL
);
5851 init_waitqueue_head(&thread
->wqueue
);
5854 thread
->mddev
= mddev
;
5855 thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
5856 thread
->tsk
= kthread_run(md_thread
, thread
,
5858 mdname(thread
->mddev
),
5859 name
?: mddev
->pers
->name
);
5860 if (IS_ERR(thread
->tsk
)) {
5867 void md_unregister_thread(mdk_thread_t
*thread
)
5871 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread
->tsk
));
5873 kthread_stop(thread
->tsk
);
5877 void md_error(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
5884 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
5887 if (mddev
->external
)
5888 set_bit(Blocked
, &rdev
->flags
);
5890 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5892 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5893 __builtin_return_address(0),__builtin_return_address(1),
5894 __builtin_return_address(2),__builtin_return_address(3));
5898 if (!mddev
->pers
->error_handler
)
5900 mddev
->pers
->error_handler(mddev
,rdev
);
5901 if (mddev
->degraded
)
5902 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
5903 set_bit(StateChanged
, &rdev
->flags
);
5904 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5905 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5906 md_wakeup_thread(mddev
->thread
);
5907 md_new_event_inintr(mddev
);
5910 /* seq_file implementation /proc/mdstat */
5912 static void status_unused(struct seq_file
*seq
)
5917 seq_printf(seq
, "unused devices: ");
5919 list_for_each_entry(rdev
, &pending_raid_disks
, same_set
) {
5920 char b
[BDEVNAME_SIZE
];
5922 seq_printf(seq
, "%s ",
5923 bdevname(rdev
->bdev
,b
));
5926 seq_printf(seq
, "<none>");
5928 seq_printf(seq
, "\n");
5932 static void status_resync(struct seq_file
*seq
, mddev_t
* mddev
)
5934 sector_t max_sectors
, resync
, res
;
5935 unsigned long dt
, db
;
5938 unsigned int per_milli
;
5940 resync
= mddev
->curr_resync
- atomic_read(&mddev
->recovery_active
);
5942 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
5943 max_sectors
= mddev
->resync_max_sectors
;
5945 max_sectors
= mddev
->dev_sectors
;
5948 * Should not happen.
5954 /* Pick 'scale' such that (resync>>scale)*1000 will fit
5955 * in a sector_t, and (max_sectors>>scale) will fit in a
5956 * u32, as those are the requirements for sector_div.
5957 * Thus 'scale' must be at least 10
5960 if (sizeof(sector_t
) > sizeof(unsigned long)) {
5961 while ( max_sectors
/2 > (1ULL<<(scale
+32)))
5964 res
= (resync
>>scale
)*1000;
5965 sector_div(res
, (u32
)((max_sectors
>>scale
)+1));
5969 int i
, x
= per_milli
/50, y
= 20-x
;
5970 seq_printf(seq
, "[");
5971 for (i
= 0; i
< x
; i
++)
5972 seq_printf(seq
, "=");
5973 seq_printf(seq
, ">");
5974 for (i
= 0; i
< y
; i
++)
5975 seq_printf(seq
, ".");
5976 seq_printf(seq
, "] ");
5978 seq_printf(seq
, " %s =%3u.%u%% (%llu/%llu)",
5979 (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)?
5981 (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)?
5983 (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ?
5984 "resync" : "recovery"))),
5985 per_milli
/10, per_milli
% 10,
5986 (unsigned long long) resync
/2,
5987 (unsigned long long) max_sectors
/2);
5990 * dt: time from mark until now
5991 * db: blocks written from mark until now
5992 * rt: remaining time
5994 * rt is a sector_t, so could be 32bit or 64bit.
5995 * So we divide before multiply in case it is 32bit and close
5997 * We scale the divisor (db) by 32 to avoid loosing precision
5998 * near the end of resync when the number of remaining sectors
6000 * We then divide rt by 32 after multiplying by db to compensate.
6001 * The '+1' avoids division by zero if db is very small.
6003 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
6005 db
= (mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
))
6006 - mddev
->resync_mark_cnt
;
6008 rt
= max_sectors
- resync
; /* number of remaining sectors */
6009 sector_div(rt
, db
/32+1);
6013 seq_printf(seq
, " finish=%lu.%lumin", (unsigned long)rt
/ 60,
6014 ((unsigned long)rt
% 60)/6);
6016 seq_printf(seq
, " speed=%ldK/sec", db
/2/dt
);
6019 static void *md_seq_start(struct seq_file
*seq
, loff_t
*pos
)
6021 struct list_head
*tmp
;
6031 spin_lock(&all_mddevs_lock
);
6032 list_for_each(tmp
,&all_mddevs
)
6034 mddev
= list_entry(tmp
, mddev_t
, all_mddevs
);
6036 spin_unlock(&all_mddevs_lock
);
6039 spin_unlock(&all_mddevs_lock
);
6041 return (void*)2;/* tail */
6045 static void *md_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
6047 struct list_head
*tmp
;
6048 mddev_t
*next_mddev
, *mddev
= v
;
6054 spin_lock(&all_mddevs_lock
);
6056 tmp
= all_mddevs
.next
;
6058 tmp
= mddev
->all_mddevs
.next
;
6059 if (tmp
!= &all_mddevs
)
6060 next_mddev
= mddev_get(list_entry(tmp
,mddev_t
,all_mddevs
));
6062 next_mddev
= (void*)2;
6065 spin_unlock(&all_mddevs_lock
);
6073 static void md_seq_stop(struct seq_file
*seq
, void *v
)
6077 if (mddev
&& v
!= (void*)1 && v
!= (void*)2)
6081 struct mdstat_info
{
6085 static int md_seq_show(struct seq_file
*seq
, void *v
)
6090 struct mdstat_info
*mi
= seq
->private;
6091 struct bitmap
*bitmap
;
6093 if (v
== (void*)1) {
6094 struct mdk_personality
*pers
;
6095 seq_printf(seq
, "Personalities : ");
6096 spin_lock(&pers_lock
);
6097 list_for_each_entry(pers
, &pers_list
, list
)
6098 seq_printf(seq
, "[%s] ", pers
->name
);
6100 spin_unlock(&pers_lock
);
6101 seq_printf(seq
, "\n");
6102 mi
->event
= atomic_read(&md_event_count
);
6105 if (v
== (void*)2) {
6110 if (mddev_lock(mddev
) < 0)
6113 if (mddev
->pers
|| mddev
->raid_disks
|| !list_empty(&mddev
->disks
)) {
6114 seq_printf(seq
, "%s : %sactive", mdname(mddev
),
6115 mddev
->pers
? "" : "in");
6118 seq_printf(seq
, " (read-only)");
6120 seq_printf(seq
, " (auto-read-only)");
6121 seq_printf(seq
, " %s", mddev
->pers
->name
);
6125 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
6126 char b
[BDEVNAME_SIZE
];
6127 seq_printf(seq
, " %s[%d]",
6128 bdevname(rdev
->bdev
,b
), rdev
->desc_nr
);
6129 if (test_bit(WriteMostly
, &rdev
->flags
))
6130 seq_printf(seq
, "(W)");
6131 if (test_bit(Faulty
, &rdev
->flags
)) {
6132 seq_printf(seq
, "(F)");
6134 } else if (rdev
->raid_disk
< 0)
6135 seq_printf(seq
, "(S)"); /* spare */
6136 sectors
+= rdev
->sectors
;
6139 if (!list_empty(&mddev
->disks
)) {
6141 seq_printf(seq
, "\n %llu blocks",
6142 (unsigned long long)
6143 mddev
->array_sectors
/ 2);
6145 seq_printf(seq
, "\n %llu blocks",
6146 (unsigned long long)sectors
/ 2);
6148 if (mddev
->persistent
) {
6149 if (mddev
->major_version
!= 0 ||
6150 mddev
->minor_version
!= 90) {
6151 seq_printf(seq
," super %d.%d",
6152 mddev
->major_version
,
6153 mddev
->minor_version
);
6155 } else if (mddev
->external
)
6156 seq_printf(seq
, " super external:%s",
6157 mddev
->metadata_type
);
6159 seq_printf(seq
, " super non-persistent");
6162 mddev
->pers
->status(seq
, mddev
);
6163 seq_printf(seq
, "\n ");
6164 if (mddev
->pers
->sync_request
) {
6165 if (mddev
->curr_resync
> 2) {
6166 status_resync(seq
, mddev
);
6167 seq_printf(seq
, "\n ");
6168 } else if (mddev
->curr_resync
== 1 || mddev
->curr_resync
== 2)
6169 seq_printf(seq
, "\tresync=DELAYED\n ");
6170 else if (mddev
->recovery_cp
< MaxSector
)
6171 seq_printf(seq
, "\tresync=PENDING\n ");
6174 seq_printf(seq
, "\n ");
6176 if ((bitmap
= mddev
->bitmap
)) {
6177 unsigned long chunk_kb
;
6178 unsigned long flags
;
6179 spin_lock_irqsave(&bitmap
->lock
, flags
);
6180 chunk_kb
= mddev
->bitmap_info
.chunksize
>> 10;
6181 seq_printf(seq
, "bitmap: %lu/%lu pages [%luKB], "
6183 bitmap
->pages
- bitmap
->missing_pages
,
6185 (bitmap
->pages
- bitmap
->missing_pages
)
6186 << (PAGE_SHIFT
- 10),
6187 chunk_kb
? chunk_kb
: mddev
->bitmap_info
.chunksize
,
6188 chunk_kb
? "KB" : "B");
6190 seq_printf(seq
, ", file: ");
6191 seq_path(seq
, &bitmap
->file
->f_path
, " \t\n");
6194 seq_printf(seq
, "\n");
6195 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
6198 seq_printf(seq
, "\n");
6200 mddev_unlock(mddev
);
6205 static const struct seq_operations md_seq_ops
= {
6206 .start
= md_seq_start
,
6207 .next
= md_seq_next
,
6208 .stop
= md_seq_stop
,
6209 .show
= md_seq_show
,
6212 static int md_seq_open(struct inode
*inode
, struct file
*file
)
6215 struct mdstat_info
*mi
= kmalloc(sizeof(*mi
), GFP_KERNEL
);
6219 error
= seq_open(file
, &md_seq_ops
);
6223 struct seq_file
*p
= file
->private_data
;
6225 mi
->event
= atomic_read(&md_event_count
);
6230 static unsigned int mdstat_poll(struct file
*filp
, poll_table
*wait
)
6232 struct seq_file
*m
= filp
->private_data
;
6233 struct mdstat_info
*mi
= m
->private;
6236 poll_wait(filp
, &md_event_waiters
, wait
);
6238 /* always allow read */
6239 mask
= POLLIN
| POLLRDNORM
;
6241 if (mi
->event
!= atomic_read(&md_event_count
))
6242 mask
|= POLLERR
| POLLPRI
;
6246 static const struct file_operations md_seq_fops
= {
6247 .owner
= THIS_MODULE
,
6248 .open
= md_seq_open
,
6250 .llseek
= seq_lseek
,
6251 .release
= seq_release_private
,
6252 .poll
= mdstat_poll
,
6255 int register_md_personality(struct mdk_personality
*p
)
6257 spin_lock(&pers_lock
);
6258 list_add_tail(&p
->list
, &pers_list
);
6259 printk(KERN_INFO
"md: %s personality registered for level %d\n", p
->name
, p
->level
);
6260 spin_unlock(&pers_lock
);
6264 int unregister_md_personality(struct mdk_personality
*p
)
6266 printk(KERN_INFO
"md: %s personality unregistered\n", p
->name
);
6267 spin_lock(&pers_lock
);
6268 list_del_init(&p
->list
);
6269 spin_unlock(&pers_lock
);
6273 static int is_mddev_idle(mddev_t
*mddev
, int init
)
6281 rdev_for_each_rcu(rdev
, mddev
) {
6282 struct gendisk
*disk
= rdev
->bdev
->bd_contains
->bd_disk
;
6283 curr_events
= (int)part_stat_read(&disk
->part0
, sectors
[0]) +
6284 (int)part_stat_read(&disk
->part0
, sectors
[1]) -
6285 atomic_read(&disk
->sync_io
);
6286 /* sync IO will cause sync_io to increase before the disk_stats
6287 * as sync_io is counted when a request starts, and
6288 * disk_stats is counted when it completes.
6289 * So resync activity will cause curr_events to be smaller than
6290 * when there was no such activity.
6291 * non-sync IO will cause disk_stat to increase without
6292 * increasing sync_io so curr_events will (eventually)
6293 * be larger than it was before. Once it becomes
6294 * substantially larger, the test below will cause
6295 * the array to appear non-idle, and resync will slow
6297 * If there is a lot of outstanding resync activity when
6298 * we set last_event to curr_events, then all that activity
6299 * completing might cause the array to appear non-idle
6300 * and resync will be slowed down even though there might
6301 * not have been non-resync activity. This will only
6302 * happen once though. 'last_events' will soon reflect
6303 * the state where there is little or no outstanding
6304 * resync requests, and further resync activity will
6305 * always make curr_events less than last_events.
6308 if (init
|| curr_events
- rdev
->last_events
> 64) {
6309 rdev
->last_events
= curr_events
;
6317 void md_done_sync(mddev_t
*mddev
, int blocks
, int ok
)
6319 /* another "blocks" (512byte) blocks have been synced */
6320 atomic_sub(blocks
, &mddev
->recovery_active
);
6321 wake_up(&mddev
->recovery_wait
);
6323 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6324 md_wakeup_thread(mddev
->thread
);
6325 // stop recovery, signal do_sync ....
6330 /* md_write_start(mddev, bi)
6331 * If we need to update some array metadata (e.g. 'active' flag
6332 * in superblock) before writing, schedule a superblock update
6333 * and wait for it to complete.
6335 void md_write_start(mddev_t
*mddev
, struct bio
*bi
)
6338 if (bio_data_dir(bi
) != WRITE
)
6341 BUG_ON(mddev
->ro
== 1);
6342 if (mddev
->ro
== 2) {
6343 /* need to switch to read/write */
6345 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6346 md_wakeup_thread(mddev
->thread
);
6347 md_wakeup_thread(mddev
->sync_thread
);
6350 atomic_inc(&mddev
->writes_pending
);
6351 if (mddev
->safemode
== 1)
6352 mddev
->safemode
= 0;
6353 if (mddev
->in_sync
) {
6354 spin_lock_irq(&mddev
->write_lock
);
6355 if (mddev
->in_sync
) {
6357 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6358 md_wakeup_thread(mddev
->thread
);
6361 spin_unlock_irq(&mddev
->write_lock
);
6364 sysfs_notify_dirent(mddev
->sysfs_state
);
6365 wait_event(mddev
->sb_wait
,
6366 !test_bit(MD_CHANGE_CLEAN
, &mddev
->flags
) &&
6367 !test_bit(MD_CHANGE_PENDING
, &mddev
->flags
));
6370 void md_write_end(mddev_t
*mddev
)
6372 if (atomic_dec_and_test(&mddev
->writes_pending
)) {
6373 if (mddev
->safemode
== 2)
6374 md_wakeup_thread(mddev
->thread
);
6375 else if (mddev
->safemode_delay
)
6376 mod_timer(&mddev
->safemode_timer
, jiffies
+ mddev
->safemode_delay
);
6380 /* md_allow_write(mddev)
6381 * Calling this ensures that the array is marked 'active' so that writes
6382 * may proceed without blocking. It is important to call this before
6383 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6384 * Must be called with mddev_lock held.
6386 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6387 * is dropped, so return -EAGAIN after notifying userspace.
6389 int md_allow_write(mddev_t
*mddev
)
6395 if (!mddev
->pers
->sync_request
)
6398 spin_lock_irq(&mddev
->write_lock
);
6399 if (mddev
->in_sync
) {
6401 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6402 if (mddev
->safemode_delay
&&
6403 mddev
->safemode
== 0)
6404 mddev
->safemode
= 1;
6405 spin_unlock_irq(&mddev
->write_lock
);
6406 md_update_sb(mddev
, 0);
6407 sysfs_notify_dirent(mddev
->sysfs_state
);
6409 spin_unlock_irq(&mddev
->write_lock
);
6411 if (test_bit(MD_CHANGE_CLEAN
, &mddev
->flags
))
6416 EXPORT_SYMBOL_GPL(md_allow_write
);
6418 #define SYNC_MARKS 10
6419 #define SYNC_MARK_STEP (3*HZ)
6420 void md_do_sync(mddev_t
*mddev
)
6423 unsigned int currspeed
= 0,
6425 sector_t max_sectors
,j
, io_sectors
;
6426 unsigned long mark
[SYNC_MARKS
];
6427 sector_t mark_cnt
[SYNC_MARKS
];
6429 struct list_head
*tmp
;
6430 sector_t last_check
;
6435 /* just incase thread restarts... */
6436 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
6438 if (mddev
->ro
) /* never try to sync a read-only array */
6441 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
6442 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
6443 desc
= "data-check";
6444 else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
6445 desc
= "requested-resync";
6448 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
6453 /* we overload curr_resync somewhat here.
6454 * 0 == not engaged in resync at all
6455 * 2 == checking that there is no conflict with another sync
6456 * 1 == like 2, but have yielded to allow conflicting resync to
6458 * other == active in resync - this many blocks
6460 * Before starting a resync we must have set curr_resync to
6461 * 2, and then checked that every "conflicting" array has curr_resync
6462 * less than ours. When we find one that is the same or higher
6463 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6464 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6465 * This will mean we have to start checking from the beginning again.
6470 mddev
->curr_resync
= 2;
6473 if (kthread_should_stop())
6474 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6476 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
6478 for_each_mddev(mddev2
, tmp
) {
6479 if (mddev2
== mddev
)
6481 if (!mddev
->parallel_resync
6482 && mddev2
->curr_resync
6483 && match_mddev_units(mddev
, mddev2
)) {
6485 if (mddev
< mddev2
&& mddev
->curr_resync
== 2) {
6486 /* arbitrarily yield */
6487 mddev
->curr_resync
= 1;
6488 wake_up(&resync_wait
);
6490 if (mddev
> mddev2
&& mddev
->curr_resync
== 1)
6491 /* no need to wait here, we can wait the next
6492 * time 'round when curr_resync == 2
6495 /* We need to wait 'interruptible' so as not to
6496 * contribute to the load average, and not to
6497 * be caught by 'softlockup'
6499 prepare_to_wait(&resync_wait
, &wq
, TASK_INTERRUPTIBLE
);
6500 if (!kthread_should_stop() &&
6501 mddev2
->curr_resync
>= mddev
->curr_resync
) {
6502 printk(KERN_INFO
"md: delaying %s of %s"
6503 " until %s has finished (they"
6504 " share one or more physical units)\n",
6505 desc
, mdname(mddev
), mdname(mddev2
));
6507 if (signal_pending(current
))
6508 flush_signals(current
);
6510 finish_wait(&resync_wait
, &wq
);
6513 finish_wait(&resync_wait
, &wq
);
6516 } while (mddev
->curr_resync
< 2);
6519 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
6520 /* resync follows the size requested by the personality,
6521 * which defaults to physical size, but can be virtual size
6523 max_sectors
= mddev
->resync_max_sectors
;
6524 mddev
->resync_mismatches
= 0;
6525 /* we don't use the checkpoint if there's a bitmap */
6526 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
6527 j
= mddev
->resync_min
;
6528 else if (!mddev
->bitmap
)
6529 j
= mddev
->recovery_cp
;
6531 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
6532 max_sectors
= mddev
->dev_sectors
;
6534 /* recovery follows the physical size of devices */
6535 max_sectors
= mddev
->dev_sectors
;
6538 list_for_each_entry_rcu(rdev
, &mddev
->disks
, same_set
)
6539 if (rdev
->raid_disk
>= 0 &&
6540 !test_bit(Faulty
, &rdev
->flags
) &&
6541 !test_bit(In_sync
, &rdev
->flags
) &&
6542 rdev
->recovery_offset
< j
)
6543 j
= rdev
->recovery_offset
;
6547 printk(KERN_INFO
"md: %s of RAID array %s\n", desc
, mdname(mddev
));
6548 printk(KERN_INFO
"md: minimum _guaranteed_ speed:"
6549 " %d KB/sec/disk.\n", speed_min(mddev
));
6550 printk(KERN_INFO
"md: using maximum available idle IO bandwidth "
6551 "(but not more than %d KB/sec) for %s.\n",
6552 speed_max(mddev
), desc
);
6554 is_mddev_idle(mddev
, 1); /* this initializes IO event counters */
6557 for (m
= 0; m
< SYNC_MARKS
; m
++) {
6559 mark_cnt
[m
] = io_sectors
;
6562 mddev
->resync_mark
= mark
[last_mark
];
6563 mddev
->resync_mark_cnt
= mark_cnt
[last_mark
];
6566 * Tune reconstruction:
6568 window
= 32*(PAGE_SIZE
/512);
6569 printk(KERN_INFO
"md: using %dk window, over a total of %llu blocks.\n",
6570 window
/2,(unsigned long long) max_sectors
/2);
6572 atomic_set(&mddev
->recovery_active
, 0);
6577 "md: resuming %s of %s from checkpoint.\n",
6578 desc
, mdname(mddev
));
6579 mddev
->curr_resync
= j
;
6581 mddev
->curr_resync_completed
= mddev
->curr_resync
;
6583 while (j
< max_sectors
) {
6588 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
6589 ((mddev
->curr_resync
> mddev
->curr_resync_completed
&&
6590 (mddev
->curr_resync
- mddev
->curr_resync_completed
)
6591 > (max_sectors
>> 4)) ||
6592 (j
- mddev
->curr_resync_completed
)*2
6593 >= mddev
->resync_max
- mddev
->curr_resync_completed
6595 /* time to update curr_resync_completed */
6596 blk_unplug(mddev
->queue
);
6597 wait_event(mddev
->recovery_wait
,
6598 atomic_read(&mddev
->recovery_active
) == 0);
6599 mddev
->curr_resync_completed
=
6601 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6602 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
6605 while (j
>= mddev
->resync_max
&& !kthread_should_stop()) {
6606 /* As this condition is controlled by user-space,
6607 * we can block indefinitely, so use '_interruptible'
6608 * to avoid triggering warnings.
6610 flush_signals(current
); /* just in case */
6611 wait_event_interruptible(mddev
->recovery_wait
,
6612 mddev
->resync_max
> j
6613 || kthread_should_stop());
6616 if (kthread_should_stop())
6619 sectors
= mddev
->pers
->sync_request(mddev
, j
, &skipped
,
6620 currspeed
< speed_min(mddev
));
6622 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6626 if (!skipped
) { /* actual IO requested */
6627 io_sectors
+= sectors
;
6628 atomic_add(sectors
, &mddev
->recovery_active
);
6632 if (j
>1) mddev
->curr_resync
= j
;
6633 mddev
->curr_mark_cnt
= io_sectors
;
6634 if (last_check
== 0)
6635 /* this is the earliers that rebuilt will be
6636 * visible in /proc/mdstat
6638 md_new_event(mddev
);
6640 if (last_check
+ window
> io_sectors
|| j
== max_sectors
)
6643 last_check
= io_sectors
;
6645 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
6649 if (time_after_eq(jiffies
, mark
[last_mark
] + SYNC_MARK_STEP
)) {
6651 int next
= (last_mark
+1) % SYNC_MARKS
;
6653 mddev
->resync_mark
= mark
[next
];
6654 mddev
->resync_mark_cnt
= mark_cnt
[next
];
6655 mark
[next
] = jiffies
;
6656 mark_cnt
[next
] = io_sectors
- atomic_read(&mddev
->recovery_active
);
6661 if (kthread_should_stop())
6666 * this loop exits only if either when we are slower than
6667 * the 'hard' speed limit, or the system was IO-idle for
6669 * the system might be non-idle CPU-wise, but we only care
6670 * about not overloading the IO subsystem. (things like an
6671 * e2fsck being done on the RAID array should execute fast)
6673 blk_unplug(mddev
->queue
);
6676 currspeed
= ((unsigned long)(io_sectors
-mddev
->resync_mark_cnt
))/2
6677 /((jiffies
-mddev
->resync_mark
)/HZ
+1) +1;
6679 if (currspeed
> speed_min(mddev
)) {
6680 if ((currspeed
> speed_max(mddev
)) ||
6681 !is_mddev_idle(mddev
, 0)) {
6687 printk(KERN_INFO
"md: %s: %s done.\n",mdname(mddev
), desc
);
6689 * this also signals 'finished resyncing' to md_stop
6692 blk_unplug(mddev
->queue
);
6694 wait_event(mddev
->recovery_wait
, !atomic_read(&mddev
->recovery_active
));
6696 /* tell personality that we are finished */
6697 mddev
->pers
->sync_request(mddev
, max_sectors
, &skipped
, 1);
6699 if (!test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
) &&
6700 mddev
->curr_resync
> 2) {
6701 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
6702 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
6703 if (mddev
->curr_resync
>= mddev
->recovery_cp
) {
6705 "md: checkpointing %s of %s.\n",
6706 desc
, mdname(mddev
));
6707 mddev
->recovery_cp
= mddev
->curr_resync
;
6710 mddev
->recovery_cp
= MaxSector
;
6712 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
6713 mddev
->curr_resync
= MaxSector
;
6715 list_for_each_entry_rcu(rdev
, &mddev
->disks
, same_set
)
6716 if (rdev
->raid_disk
>= 0 &&
6717 !test_bit(Faulty
, &rdev
->flags
) &&
6718 !test_bit(In_sync
, &rdev
->flags
) &&
6719 rdev
->recovery_offset
< mddev
->curr_resync
)
6720 rdev
->recovery_offset
= mddev
->curr_resync
;
6724 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
6727 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
6728 /* We completed so min/max setting can be forgotten if used. */
6729 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
6730 mddev
->resync_min
= 0;
6731 mddev
->resync_max
= MaxSector
;
6732 } else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
6733 mddev
->resync_min
= mddev
->curr_resync_completed
;
6734 mddev
->curr_resync
= 0;
6735 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
6736 mddev
->curr_resync_completed
= 0;
6737 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
6738 wake_up(&resync_wait
);
6739 set_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
6740 md_wakeup_thread(mddev
->thread
);
6745 * got a signal, exit.
6748 "md: md_do_sync() got signal ... exiting\n");
6749 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6753 EXPORT_SYMBOL_GPL(md_do_sync
);
6756 static int remove_and_add_spares(mddev_t
*mddev
)
6761 mddev
->curr_resync_completed
= 0;
6763 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
6764 if (rdev
->raid_disk
>= 0 &&
6765 !test_bit(Blocked
, &rdev
->flags
) &&
6766 (test_bit(Faulty
, &rdev
->flags
) ||
6767 ! test_bit(In_sync
, &rdev
->flags
)) &&
6768 atomic_read(&rdev
->nr_pending
)==0) {
6769 if (mddev
->pers
->hot_remove_disk(
6770 mddev
, rdev
->raid_disk
)==0) {
6772 sprintf(nm
,"rd%d", rdev
->raid_disk
);
6773 sysfs_remove_link(&mddev
->kobj
, nm
);
6774 rdev
->raid_disk
= -1;
6778 if (mddev
->degraded
&& ! mddev
->ro
&& !mddev
->recovery_disabled
) {
6779 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
6780 if (rdev
->raid_disk
>= 0 &&
6781 !test_bit(In_sync
, &rdev
->flags
) &&
6782 !test_bit(Blocked
, &rdev
->flags
))
6784 if (rdev
->raid_disk
< 0
6785 && !test_bit(Faulty
, &rdev
->flags
)) {
6786 rdev
->recovery_offset
= 0;
6788 hot_add_disk(mddev
, rdev
) == 0) {
6790 sprintf(nm
, "rd%d", rdev
->raid_disk
);
6791 if (sysfs_create_link(&mddev
->kobj
,
6794 "md: cannot register "
6798 md_new_event(mddev
);
6799 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
6808 * This routine is regularly called by all per-raid-array threads to
6809 * deal with generic issues like resync and super-block update.
6810 * Raid personalities that don't have a thread (linear/raid0) do not
6811 * need this as they never do any recovery or update the superblock.
6813 * It does not do any resync itself, but rather "forks" off other threads
6814 * to do that as needed.
6815 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6816 * "->recovery" and create a thread at ->sync_thread.
6817 * When the thread finishes it sets MD_RECOVERY_DONE
6818 * and wakeups up this thread which will reap the thread and finish up.
6819 * This thread also removes any faulty devices (with nr_pending == 0).
6821 * The overall approach is:
6822 * 1/ if the superblock needs updating, update it.
6823 * 2/ If a recovery thread is running, don't do anything else.
6824 * 3/ If recovery has finished, clean up, possibly marking spares active.
6825 * 4/ If there are any faulty devices, remove them.
6826 * 5/ If array is degraded, try to add spares devices
6827 * 6/ If array has spares or is not in-sync, start a resync thread.
6829 void md_check_recovery(mddev_t
*mddev
)
6835 bitmap_daemon_work(mddev
);
6840 if (signal_pending(current
)) {
6841 if (mddev
->pers
->sync_request
&& !mddev
->external
) {
6842 printk(KERN_INFO
"md: %s in immediate safe mode\n",
6844 mddev
->safemode
= 2;
6846 flush_signals(current
);
6849 if (mddev
->ro
&& !test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
6852 (mddev
->flags
&& !mddev
->external
) ||
6853 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
6854 test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
6855 (mddev
->external
== 0 && mddev
->safemode
== 1) ||
6856 (mddev
->safemode
== 2 && ! atomic_read(&mddev
->writes_pending
)
6857 && !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
6861 if (mddev_trylock(mddev
)) {
6865 /* Only thing we do on a ro array is remove
6868 remove_and_add_spares(mddev
);
6869 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6873 if (!mddev
->external
) {
6875 spin_lock_irq(&mddev
->write_lock
);
6876 if (mddev
->safemode
&&
6877 !atomic_read(&mddev
->writes_pending
) &&
6879 mddev
->recovery_cp
== MaxSector
) {
6882 if (mddev
->persistent
)
6883 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6885 if (mddev
->safemode
== 1)
6886 mddev
->safemode
= 0;
6887 spin_unlock_irq(&mddev
->write_lock
);
6889 sysfs_notify_dirent(mddev
->sysfs_state
);
6893 md_update_sb(mddev
, 0);
6895 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
6896 if (test_and_clear_bit(StateChanged
, &rdev
->flags
))
6897 sysfs_notify_dirent(rdev
->sysfs_state
);
6900 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
6901 !test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
)) {
6902 /* resync/recovery still happening */
6903 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6906 if (mddev
->sync_thread
) {
6907 /* resync has finished, collect result */
6908 md_unregister_thread(mddev
->sync_thread
);
6909 mddev
->sync_thread
= NULL
;
6910 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
6911 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
6913 /* activate any spares */
6914 if (mddev
->pers
->spare_active(mddev
))
6915 sysfs_notify(&mddev
->kobj
, NULL
,
6918 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
6919 mddev
->pers
->finish_reshape
)
6920 mddev
->pers
->finish_reshape(mddev
);
6921 md_update_sb(mddev
, 1);
6923 /* if array is no-longer degraded, then any saved_raid_disk
6924 * information must be scrapped
6926 if (!mddev
->degraded
)
6927 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
6928 rdev
->saved_raid_disk
= -1;
6930 mddev
->recovery
= 0;
6931 /* flag recovery needed just to double check */
6932 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6933 sysfs_notify_dirent(mddev
->sysfs_action
);
6934 md_new_event(mddev
);
6937 /* Set RUNNING before clearing NEEDED to avoid
6938 * any transients in the value of "sync_action".
6940 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
6941 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6942 /* Clear some bits that don't mean anything, but
6945 clear_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6946 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
6948 if (test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
6950 /* no recovery is running.
6951 * remove any failed drives, then
6952 * add spares if possible.
6953 * Spare are also removed and re-added, to allow
6954 * the personality to fail the re-add.
6957 if (mddev
->reshape_position
!= MaxSector
) {
6958 if (mddev
->pers
->check_reshape
== NULL
||
6959 mddev
->pers
->check_reshape(mddev
) != 0)
6960 /* Cannot proceed */
6962 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
6963 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
6964 } else if ((spares
= remove_and_add_spares(mddev
))) {
6965 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
6966 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
6967 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
6968 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
6969 } else if (mddev
->recovery_cp
< MaxSector
) {
6970 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
6971 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
6972 } else if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
6973 /* nothing to be done ... */
6976 if (mddev
->pers
->sync_request
) {
6977 if (spares
&& mddev
->bitmap
&& ! mddev
->bitmap
->file
) {
6978 /* We are adding a device or devices to an array
6979 * which has the bitmap stored on all devices.
6980 * So make sure all bitmap pages get written
6982 bitmap_write_all(mddev
->bitmap
);
6984 mddev
->sync_thread
= md_register_thread(md_do_sync
,
6987 if (!mddev
->sync_thread
) {
6988 printk(KERN_ERR
"%s: could not start resync"
6991 /* leave the spares where they are, it shouldn't hurt */
6992 mddev
->recovery
= 0;
6994 md_wakeup_thread(mddev
->sync_thread
);
6995 sysfs_notify_dirent(mddev
->sysfs_action
);
6996 md_new_event(mddev
);
6999 if (!mddev
->sync_thread
) {
7000 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7001 if (test_and_clear_bit(MD_RECOVERY_RECOVER
,
7003 if (mddev
->sysfs_action
)
7004 sysfs_notify_dirent(mddev
->sysfs_action
);
7006 mddev_unlock(mddev
);
7010 void md_wait_for_blocked_rdev(mdk_rdev_t
*rdev
, mddev_t
*mddev
)
7012 sysfs_notify_dirent(rdev
->sysfs_state
);
7013 wait_event_timeout(rdev
->blocked_wait
,
7014 !test_bit(Blocked
, &rdev
->flags
),
7015 msecs_to_jiffies(5000));
7016 rdev_dec_pending(rdev
, mddev
);
7018 EXPORT_SYMBOL(md_wait_for_blocked_rdev
);
7020 static int md_notify_reboot(struct notifier_block
*this,
7021 unsigned long code
, void *x
)
7023 struct list_head
*tmp
;
7026 if ((code
== SYS_DOWN
) || (code
== SYS_HALT
) || (code
== SYS_POWER_OFF
)) {
7028 printk(KERN_INFO
"md: stopping all md devices.\n");
7030 for_each_mddev(mddev
, tmp
)
7031 if (mddev_trylock(mddev
)) {
7032 /* Force a switch to readonly even array
7033 * appears to still be in use. Hence
7036 do_md_stop(mddev
, 1, 100);
7037 mddev_unlock(mddev
);
7040 * certain more exotic SCSI devices are known to be
7041 * volatile wrt too early system reboots. While the
7042 * right place to handle this issue is the given
7043 * driver, we do want to have a safe RAID driver ...
7050 static struct notifier_block md_notifier
= {
7051 .notifier_call
= md_notify_reboot
,
7053 .priority
= INT_MAX
, /* before any real devices */
7056 static void md_geninit(void)
7058 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t
));
7060 proc_create("mdstat", S_IRUGO
, NULL
, &md_seq_fops
);
7063 static int __init
md_init(void)
7065 if (register_blkdev(MD_MAJOR
, "md"))
7067 if ((mdp_major
=register_blkdev(0, "mdp"))<=0) {
7068 unregister_blkdev(MD_MAJOR
, "md");
7071 blk_register_region(MKDEV(MD_MAJOR
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
7072 md_probe
, NULL
, NULL
);
7073 blk_register_region(MKDEV(mdp_major
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
7074 md_probe
, NULL
, NULL
);
7076 register_reboot_notifier(&md_notifier
);
7077 raid_table_header
= register_sysctl_table(raid_root_table
);
7087 * Searches all registered partitions for autorun RAID arrays
7091 static LIST_HEAD(all_detected_devices
);
7092 struct detected_devices_node
{
7093 struct list_head list
;
7097 void md_autodetect_dev(dev_t dev
)
7099 struct detected_devices_node
*node_detected_dev
;
7101 node_detected_dev
= kzalloc(sizeof(*node_detected_dev
), GFP_KERNEL
);
7102 if (node_detected_dev
) {
7103 node_detected_dev
->dev
= dev
;
7104 list_add_tail(&node_detected_dev
->list
, &all_detected_devices
);
7106 printk(KERN_CRIT
"md: md_autodetect_dev: kzalloc failed"
7107 ", skipping dev(%d,%d)\n", MAJOR(dev
), MINOR(dev
));
7112 static void autostart_arrays(int part
)
7115 struct detected_devices_node
*node_detected_dev
;
7117 int i_scanned
, i_passed
;
7122 printk(KERN_INFO
"md: Autodetecting RAID arrays.\n");
7124 while (!list_empty(&all_detected_devices
) && i_scanned
< INT_MAX
) {
7126 node_detected_dev
= list_entry(all_detected_devices
.next
,
7127 struct detected_devices_node
, list
);
7128 list_del(&node_detected_dev
->list
);
7129 dev
= node_detected_dev
->dev
;
7130 kfree(node_detected_dev
);
7131 rdev
= md_import_device(dev
,0, 90);
7135 if (test_bit(Faulty
, &rdev
->flags
)) {
7139 set_bit(AutoDetected
, &rdev
->flags
);
7140 list_add(&rdev
->same_set
, &pending_raid_disks
);
7144 printk(KERN_INFO
"md: Scanned %d and added %d devices.\n",
7145 i_scanned
, i_passed
);
7147 autorun_devices(part
);
7150 #endif /* !MODULE */
7152 static __exit
void md_exit(void)
7155 struct list_head
*tmp
;
7157 blk_unregister_region(MKDEV(MD_MAJOR
,0), 1U << MINORBITS
);
7158 blk_unregister_region(MKDEV(mdp_major
,0), 1U << MINORBITS
);
7160 unregister_blkdev(MD_MAJOR
,"md");
7161 unregister_blkdev(mdp_major
, "mdp");
7162 unregister_reboot_notifier(&md_notifier
);
7163 unregister_sysctl_table(raid_table_header
);
7164 remove_proc_entry("mdstat", NULL
);
7165 for_each_mddev(mddev
, tmp
) {
7166 export_array(mddev
);
7167 mddev
->hold_active
= 0;
7171 subsys_initcall(md_init
);
7172 module_exit(md_exit
)
7174 static int get_ro(char *buffer
, struct kernel_param
*kp
)
7176 return sprintf(buffer
, "%d", start_readonly
);
7178 static int set_ro(const char *val
, struct kernel_param
*kp
)
7181 int num
= simple_strtoul(val
, &e
, 10);
7182 if (*val
&& (*e
== '\0' || *e
== '\n')) {
7183 start_readonly
= num
;
7189 module_param_call(start_ro
, set_ro
, get_ro
, NULL
, S_IRUSR
|S_IWUSR
);
7190 module_param(start_dirty_degraded
, int, S_IRUGO
|S_IWUSR
);
7192 module_param_call(new_array
, add_named_array
, NULL
, NULL
, S_IWUSR
);
7194 EXPORT_SYMBOL(register_md_personality
);
7195 EXPORT_SYMBOL(unregister_md_personality
);
7196 EXPORT_SYMBOL(md_error
);
7197 EXPORT_SYMBOL(md_done_sync
);
7198 EXPORT_SYMBOL(md_write_start
);
7199 EXPORT_SYMBOL(md_write_end
);
7200 EXPORT_SYMBOL(md_register_thread
);
7201 EXPORT_SYMBOL(md_unregister_thread
);
7202 EXPORT_SYMBOL(md_wakeup_thread
);
7203 EXPORT_SYMBOL(md_check_recovery
);
7204 MODULE_LICENSE("GPL");
7205 MODULE_DESCRIPTION("MD RAID framework");
7207 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR
);