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/mutex.h>
40 #include <linux/buffer_head.h> /* for invalidate_bdev */
41 #include <linux/poll.h>
42 #include <linux/ctype.h>
43 #include <linux/string.h>
44 #include <linux/hdreg.h>
45 #include <linux/proc_fs.h>
46 #include <linux/random.h>
47 #include <linux/reboot.h>
48 #include <linux/file.h>
49 #include <linux/compat.h>
50 #include <linux/delay.h>
51 #include <linux/raid/md_p.h>
52 #include <linux/raid/md_u.h>
53 #include <linux/slab.h>
58 #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
);
70 static struct workqueue_struct
*md_wq
;
71 static struct workqueue_struct
*md_misc_wq
;
73 #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
76 * Default number of read corrections we'll attempt on an rdev
77 * before ejecting it from the array. We divide the read error
78 * count by 2 for every hour elapsed between read errors.
80 #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
82 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
83 * is 1000 KB/sec, so the extra system load does not show up that much.
84 * Increase it if you want to have more _guaranteed_ speed. Note that
85 * the RAID driver will use the maximum available bandwidth if the IO
86 * subsystem is idle. There is also an 'absolute maximum' reconstruction
87 * speed limit - in case reconstruction slows down your system despite
90 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
91 * or /sys/block/mdX/md/sync_speed_{min,max}
94 static int sysctl_speed_limit_min
= 1000;
95 static int sysctl_speed_limit_max
= 200000;
96 static inline int speed_min(mddev_t
*mddev
)
98 return mddev
->sync_speed_min
?
99 mddev
->sync_speed_min
: sysctl_speed_limit_min
;
102 static inline int speed_max(mddev_t
*mddev
)
104 return mddev
->sync_speed_max
?
105 mddev
->sync_speed_max
: sysctl_speed_limit_max
;
108 static struct ctl_table_header
*raid_table_header
;
110 static ctl_table raid_table
[] = {
112 .procname
= "speed_limit_min",
113 .data
= &sysctl_speed_limit_min
,
114 .maxlen
= sizeof(int),
115 .mode
= S_IRUGO
|S_IWUSR
,
116 .proc_handler
= proc_dointvec
,
119 .procname
= "speed_limit_max",
120 .data
= &sysctl_speed_limit_max
,
121 .maxlen
= sizeof(int),
122 .mode
= S_IRUGO
|S_IWUSR
,
123 .proc_handler
= proc_dointvec
,
128 static ctl_table raid_dir_table
[] = {
132 .mode
= S_IRUGO
|S_IXUGO
,
138 static ctl_table raid_root_table
[] = {
143 .child
= raid_dir_table
,
148 static const struct block_device_operations md_fops
;
150 static int start_readonly
;
153 * like bio_clone, but with a local bio set
156 static void mddev_bio_destructor(struct bio
*bio
)
158 mddev_t
*mddev
, **mddevp
;
163 bio_free(bio
, mddev
->bio_set
);
166 struct bio
*bio_alloc_mddev(gfp_t gfp_mask
, int nr_iovecs
,
172 if (!mddev
|| !mddev
->bio_set
)
173 return bio_alloc(gfp_mask
, nr_iovecs
);
175 b
= bio_alloc_bioset(gfp_mask
, nr_iovecs
,
181 b
->bi_destructor
= mddev_bio_destructor
;
184 EXPORT_SYMBOL_GPL(bio_alloc_mddev
);
186 struct bio
*bio_clone_mddev(struct bio
*bio
, gfp_t gfp_mask
,
192 if (!mddev
|| !mddev
->bio_set
)
193 return bio_clone(bio
, gfp_mask
);
195 b
= bio_alloc_bioset(gfp_mask
, bio
->bi_max_vecs
,
201 b
->bi_destructor
= mddev_bio_destructor
;
203 if (bio_integrity(bio
)) {
206 ret
= bio_integrity_clone(b
, bio
, gfp_mask
, mddev
->bio_set
);
216 EXPORT_SYMBOL_GPL(bio_clone_mddev
);
219 * We have a system wide 'event count' that is incremented
220 * on any 'interesting' event, and readers of /proc/mdstat
221 * can use 'poll' or 'select' to find out when the event
225 * start array, stop array, error, add device, remove device,
226 * start build, activate spare
228 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters
);
229 static atomic_t md_event_count
;
230 void md_new_event(mddev_t
*mddev
)
232 atomic_inc(&md_event_count
);
233 wake_up(&md_event_waiters
);
235 EXPORT_SYMBOL_GPL(md_new_event
);
237 /* Alternate version that can be called from interrupts
238 * when calling sysfs_notify isn't needed.
240 static void md_new_event_inintr(mddev_t
*mddev
)
242 atomic_inc(&md_event_count
);
243 wake_up(&md_event_waiters
);
247 * Enables to iterate over all existing md arrays
248 * all_mddevs_lock protects this list.
250 static LIST_HEAD(all_mddevs
);
251 static DEFINE_SPINLOCK(all_mddevs_lock
);
255 * iterates through all used mddevs in the system.
256 * We take care to grab the all_mddevs_lock whenever navigating
257 * the list, and to always hold a refcount when unlocked.
258 * Any code which breaks out of this loop while own
259 * a reference to the current mddev and must mddev_put it.
261 #define for_each_mddev(mddev,tmp) \
263 for (({ spin_lock(&all_mddevs_lock); \
264 tmp = all_mddevs.next; \
266 ({ if (tmp != &all_mddevs) \
267 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
268 spin_unlock(&all_mddevs_lock); \
269 if (mddev) mddev_put(mddev); \
270 mddev = list_entry(tmp, mddev_t, all_mddevs); \
271 tmp != &all_mddevs;}); \
272 ({ spin_lock(&all_mddevs_lock); \
277 /* Rather than calling directly into the personality make_request function,
278 * IO requests come here first so that we can check if the device is
279 * being suspended pending a reconfiguration.
280 * We hold a refcount over the call to ->make_request. By the time that
281 * call has finished, the bio has been linked into some internal structure
282 * and so is visible to ->quiesce(), so we don't need the refcount any more.
284 static int md_make_request(struct request_queue
*q
, struct bio
*bio
)
286 const int rw
= bio_data_dir(bio
);
287 mddev_t
*mddev
= q
->queuedata
;
290 unsigned int sectors
;
292 if (mddev
== NULL
|| mddev
->pers
== NULL
297 smp_rmb(); /* Ensure implications of 'active' are visible */
299 if (mddev
->suspended
) {
302 prepare_to_wait(&mddev
->sb_wait
, &__wait
,
303 TASK_UNINTERRUPTIBLE
);
304 if (!mddev
->suspended
)
310 finish_wait(&mddev
->sb_wait
, &__wait
);
312 atomic_inc(&mddev
->active_io
);
316 * save the sectors now since our bio can
317 * go away inside make_request
319 sectors
= bio_sectors(bio
);
320 rv
= mddev
->pers
->make_request(mddev
, bio
);
322 cpu
= part_stat_lock();
323 part_stat_inc(cpu
, &mddev
->gendisk
->part0
, ios
[rw
]);
324 part_stat_add(cpu
, &mddev
->gendisk
->part0
, sectors
[rw
], sectors
);
327 if (atomic_dec_and_test(&mddev
->active_io
) && mddev
->suspended
)
328 wake_up(&mddev
->sb_wait
);
333 /* mddev_suspend makes sure no new requests are submitted
334 * to the device, and that any requests that have been submitted
335 * are completely handled.
336 * Once ->stop is called and completes, the module will be completely
339 void mddev_suspend(mddev_t
*mddev
)
341 BUG_ON(mddev
->suspended
);
342 mddev
->suspended
= 1;
344 wait_event(mddev
->sb_wait
, atomic_read(&mddev
->active_io
) == 0);
345 mddev
->pers
->quiesce(mddev
, 1);
347 EXPORT_SYMBOL_GPL(mddev_suspend
);
349 void mddev_resume(mddev_t
*mddev
)
351 mddev
->suspended
= 0;
352 wake_up(&mddev
->sb_wait
);
353 mddev
->pers
->quiesce(mddev
, 0);
355 md_wakeup_thread(mddev
->thread
);
356 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
358 EXPORT_SYMBOL_GPL(mddev_resume
);
360 int mddev_congested(mddev_t
*mddev
, int bits
)
362 return mddev
->suspended
;
364 EXPORT_SYMBOL(mddev_congested
);
367 * Generic flush handling for md
370 static void md_end_flush(struct bio
*bio
, int err
)
372 mdk_rdev_t
*rdev
= bio
->bi_private
;
373 mddev_t
*mddev
= rdev
->mddev
;
375 rdev_dec_pending(rdev
, mddev
);
377 if (atomic_dec_and_test(&mddev
->flush_pending
)) {
378 /* The pre-request flush has finished */
379 queue_work(md_wq
, &mddev
->flush_work
);
384 static void md_submit_flush_data(struct work_struct
*ws
);
386 static void submit_flushes(struct work_struct
*ws
)
388 mddev_t
*mddev
= container_of(ws
, mddev_t
, flush_work
);
391 INIT_WORK(&mddev
->flush_work
, md_submit_flush_data
);
392 atomic_set(&mddev
->flush_pending
, 1);
394 list_for_each_entry_rcu(rdev
, &mddev
->disks
, same_set
)
395 if (rdev
->raid_disk
>= 0 &&
396 !test_bit(Faulty
, &rdev
->flags
)) {
397 /* Take two references, one is dropped
398 * when request finishes, one after
399 * we reclaim rcu_read_lock
402 atomic_inc(&rdev
->nr_pending
);
403 atomic_inc(&rdev
->nr_pending
);
405 bi
= bio_alloc_mddev(GFP_KERNEL
, 0, mddev
);
406 bi
->bi_end_io
= md_end_flush
;
407 bi
->bi_private
= rdev
;
408 bi
->bi_bdev
= rdev
->bdev
;
409 atomic_inc(&mddev
->flush_pending
);
410 submit_bio(WRITE_FLUSH
, bi
);
412 rdev_dec_pending(rdev
, mddev
);
415 if (atomic_dec_and_test(&mddev
->flush_pending
))
416 queue_work(md_wq
, &mddev
->flush_work
);
419 static void md_submit_flush_data(struct work_struct
*ws
)
421 mddev_t
*mddev
= container_of(ws
, mddev_t
, flush_work
);
422 struct bio
*bio
= mddev
->flush_bio
;
424 if (bio
->bi_size
== 0)
425 /* an empty barrier - all done */
428 bio
->bi_rw
&= ~REQ_FLUSH
;
429 if (mddev
->pers
->make_request(mddev
, bio
))
430 generic_make_request(bio
);
433 mddev
->flush_bio
= NULL
;
434 wake_up(&mddev
->sb_wait
);
437 void md_flush_request(mddev_t
*mddev
, struct bio
*bio
)
439 spin_lock_irq(&mddev
->write_lock
);
440 wait_event_lock_irq(mddev
->sb_wait
,
442 mddev
->write_lock
, /*nothing*/);
443 mddev
->flush_bio
= bio
;
444 spin_unlock_irq(&mddev
->write_lock
);
446 INIT_WORK(&mddev
->flush_work
, submit_flushes
);
447 queue_work(md_wq
, &mddev
->flush_work
);
449 EXPORT_SYMBOL(md_flush_request
);
451 /* Support for plugging.
452 * This mirrors the plugging support in request_queue, but does not
453 * require having a whole queue or request structures.
454 * We allocate an md_plug_cb for each md device and each thread it gets
455 * plugged on. This links tot the private plug_handle structure in the
456 * personality data where we keep a count of the number of outstanding
457 * plugs so other code can see if a plug is active.
460 struct blk_plug_cb cb
;
464 static void plugger_unplug(struct blk_plug_cb
*cb
)
466 struct md_plug_cb
*mdcb
= container_of(cb
, struct md_plug_cb
, cb
);
467 if (atomic_dec_and_test(&mdcb
->mddev
->plug_cnt
))
468 md_wakeup_thread(mdcb
->mddev
->thread
);
472 /* Check that an unplug wakeup will come shortly.
473 * If not, wakeup the md thread immediately
475 int mddev_check_plugged(mddev_t
*mddev
)
477 struct blk_plug
*plug
= current
->plug
;
478 struct md_plug_cb
*mdcb
;
483 list_for_each_entry(mdcb
, &plug
->cb_list
, cb
.list
) {
484 if (mdcb
->cb
.callback
== plugger_unplug
&&
485 mdcb
->mddev
== mddev
) {
486 /* Already on the list, move to top */
487 if (mdcb
!= list_first_entry(&plug
->cb_list
,
490 list_move(&mdcb
->cb
.list
, &plug
->cb_list
);
494 /* Not currently on the callback list */
495 mdcb
= kmalloc(sizeof(*mdcb
), GFP_ATOMIC
);
500 mdcb
->cb
.callback
= plugger_unplug
;
501 atomic_inc(&mddev
->plug_cnt
);
502 list_add(&mdcb
->cb
.list
, &plug
->cb_list
);
505 EXPORT_SYMBOL_GPL(mddev_check_plugged
);
507 static inline mddev_t
*mddev_get(mddev_t
*mddev
)
509 atomic_inc(&mddev
->active
);
513 static void mddev_delayed_delete(struct work_struct
*ws
);
515 static void mddev_put(mddev_t
*mddev
)
517 struct bio_set
*bs
= NULL
;
519 if (!atomic_dec_and_lock(&mddev
->active
, &all_mddevs_lock
))
521 if (!mddev
->raid_disks
&& list_empty(&mddev
->disks
) &&
522 mddev
->ctime
== 0 && !mddev
->hold_active
) {
523 /* Array is not configured at all, and not held active,
525 list_del(&mddev
->all_mddevs
);
527 mddev
->bio_set
= NULL
;
528 if (mddev
->gendisk
) {
529 /* We did a probe so need to clean up. Call
530 * queue_work inside the spinlock so that
531 * flush_workqueue() after mddev_find will
532 * succeed in waiting for the work to be done.
534 INIT_WORK(&mddev
->del_work
, mddev_delayed_delete
);
535 queue_work(md_misc_wq
, &mddev
->del_work
);
539 spin_unlock(&all_mddevs_lock
);
544 void mddev_init(mddev_t
*mddev
)
546 mutex_init(&mddev
->open_mutex
);
547 mutex_init(&mddev
->reconfig_mutex
);
548 mutex_init(&mddev
->bitmap_info
.mutex
);
549 INIT_LIST_HEAD(&mddev
->disks
);
550 INIT_LIST_HEAD(&mddev
->all_mddevs
);
551 init_timer(&mddev
->safemode_timer
);
552 atomic_set(&mddev
->active
, 1);
553 atomic_set(&mddev
->openers
, 0);
554 atomic_set(&mddev
->active_io
, 0);
555 atomic_set(&mddev
->plug_cnt
, 0);
556 spin_lock_init(&mddev
->write_lock
);
557 atomic_set(&mddev
->flush_pending
, 0);
558 init_waitqueue_head(&mddev
->sb_wait
);
559 init_waitqueue_head(&mddev
->recovery_wait
);
560 mddev
->reshape_position
= MaxSector
;
561 mddev
->resync_min
= 0;
562 mddev
->resync_max
= MaxSector
;
563 mddev
->level
= LEVEL_NONE
;
565 EXPORT_SYMBOL_GPL(mddev_init
);
567 static mddev_t
* mddev_find(dev_t unit
)
569 mddev_t
*mddev
, *new = NULL
;
571 if (unit
&& MAJOR(unit
) != MD_MAJOR
)
572 unit
&= ~((1<<MdpMinorShift
)-1);
575 spin_lock(&all_mddevs_lock
);
578 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
579 if (mddev
->unit
== unit
) {
581 spin_unlock(&all_mddevs_lock
);
587 list_add(&new->all_mddevs
, &all_mddevs
);
588 spin_unlock(&all_mddevs_lock
);
589 new->hold_active
= UNTIL_IOCTL
;
593 /* find an unused unit number */
594 static int next_minor
= 512;
595 int start
= next_minor
;
599 dev
= MKDEV(MD_MAJOR
, next_minor
);
601 if (next_minor
> MINORMASK
)
603 if (next_minor
== start
) {
604 /* Oh dear, all in use. */
605 spin_unlock(&all_mddevs_lock
);
611 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
612 if (mddev
->unit
== dev
) {
618 new->md_minor
= MINOR(dev
);
619 new->hold_active
= UNTIL_STOP
;
620 list_add(&new->all_mddevs
, &all_mddevs
);
621 spin_unlock(&all_mddevs_lock
);
624 spin_unlock(&all_mddevs_lock
);
626 new = kzalloc(sizeof(*new), GFP_KERNEL
);
631 if (MAJOR(unit
) == MD_MAJOR
)
632 new->md_minor
= MINOR(unit
);
634 new->md_minor
= MINOR(unit
) >> MdpMinorShift
;
641 static inline int mddev_lock(mddev_t
* mddev
)
643 return mutex_lock_interruptible(&mddev
->reconfig_mutex
);
646 static inline int mddev_is_locked(mddev_t
*mddev
)
648 return mutex_is_locked(&mddev
->reconfig_mutex
);
651 static inline int mddev_trylock(mddev_t
* mddev
)
653 return mutex_trylock(&mddev
->reconfig_mutex
);
656 static struct attribute_group md_redundancy_group
;
658 static void mddev_unlock(mddev_t
* mddev
)
660 if (mddev
->to_remove
) {
661 /* These cannot be removed under reconfig_mutex as
662 * an access to the files will try to take reconfig_mutex
663 * while holding the file unremovable, which leads to
665 * So hold set sysfs_active while the remove in happeing,
666 * and anything else which might set ->to_remove or my
667 * otherwise change the sysfs namespace will fail with
668 * -EBUSY if sysfs_active is still set.
669 * We set sysfs_active under reconfig_mutex and elsewhere
670 * test it under the same mutex to ensure its correct value
673 struct attribute_group
*to_remove
= mddev
->to_remove
;
674 mddev
->to_remove
= NULL
;
675 mddev
->sysfs_active
= 1;
676 mutex_unlock(&mddev
->reconfig_mutex
);
678 if (mddev
->kobj
.sd
) {
679 if (to_remove
!= &md_redundancy_group
)
680 sysfs_remove_group(&mddev
->kobj
, to_remove
);
681 if (mddev
->pers
== NULL
||
682 mddev
->pers
->sync_request
== NULL
) {
683 sysfs_remove_group(&mddev
->kobj
, &md_redundancy_group
);
684 if (mddev
->sysfs_action
)
685 sysfs_put(mddev
->sysfs_action
);
686 mddev
->sysfs_action
= NULL
;
689 mddev
->sysfs_active
= 0;
691 mutex_unlock(&mddev
->reconfig_mutex
);
693 md_wakeup_thread(mddev
->thread
);
696 static mdk_rdev_t
* find_rdev_nr(mddev_t
*mddev
, int nr
)
700 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
701 if (rdev
->desc_nr
== nr
)
707 static mdk_rdev_t
* find_rdev(mddev_t
* mddev
, dev_t dev
)
711 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
712 if (rdev
->bdev
->bd_dev
== dev
)
718 static struct mdk_personality
*find_pers(int level
, char *clevel
)
720 struct mdk_personality
*pers
;
721 list_for_each_entry(pers
, &pers_list
, list
) {
722 if (level
!= LEVEL_NONE
&& pers
->level
== level
)
724 if (strcmp(pers
->name
, clevel
)==0)
730 /* return the offset of the super block in 512byte sectors */
731 static inline sector_t
calc_dev_sboffset(mdk_rdev_t
*rdev
)
733 sector_t num_sectors
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
734 return MD_NEW_SIZE_SECTORS(num_sectors
);
737 static int alloc_disk_sb(mdk_rdev_t
* rdev
)
742 rdev
->sb_page
= alloc_page(GFP_KERNEL
);
743 if (!rdev
->sb_page
) {
744 printk(KERN_ALERT
"md: out of memory.\n");
751 static void free_disk_sb(mdk_rdev_t
* rdev
)
754 put_page(rdev
->sb_page
);
756 rdev
->sb_page
= NULL
;
763 static void super_written(struct bio
*bio
, int error
)
765 mdk_rdev_t
*rdev
= bio
->bi_private
;
766 mddev_t
*mddev
= rdev
->mddev
;
768 if (error
|| !test_bit(BIO_UPTODATE
, &bio
->bi_flags
)) {
769 printk("md: super_written gets error=%d, uptodate=%d\n",
770 error
, test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
771 WARN_ON(test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
772 md_error(mddev
, rdev
);
775 if (atomic_dec_and_test(&mddev
->pending_writes
))
776 wake_up(&mddev
->sb_wait
);
780 void md_super_write(mddev_t
*mddev
, mdk_rdev_t
*rdev
,
781 sector_t sector
, int size
, struct page
*page
)
783 /* write first size bytes of page to sector of rdev
784 * Increment mddev->pending_writes before returning
785 * and decrement it on completion, waking up sb_wait
786 * if zero is reached.
787 * If an error occurred, call md_error
789 struct bio
*bio
= bio_alloc_mddev(GFP_NOIO
, 1, mddev
);
791 bio
->bi_bdev
= rdev
->meta_bdev
? rdev
->meta_bdev
: rdev
->bdev
;
792 bio
->bi_sector
= sector
;
793 bio_add_page(bio
, page
, size
, 0);
794 bio
->bi_private
= rdev
;
795 bio
->bi_end_io
= super_written
;
797 atomic_inc(&mddev
->pending_writes
);
798 submit_bio(REQ_WRITE
| REQ_SYNC
| REQ_FLUSH
| REQ_FUA
, bio
);
801 void md_super_wait(mddev_t
*mddev
)
803 /* wait for all superblock writes that were scheduled to complete */
806 prepare_to_wait(&mddev
->sb_wait
, &wq
, TASK_UNINTERRUPTIBLE
);
807 if (atomic_read(&mddev
->pending_writes
)==0)
811 finish_wait(&mddev
->sb_wait
, &wq
);
814 static void bi_complete(struct bio
*bio
, int error
)
816 complete((struct completion
*)bio
->bi_private
);
819 int sync_page_io(mdk_rdev_t
*rdev
, sector_t sector
, int size
,
820 struct page
*page
, int rw
, bool metadata_op
)
822 struct bio
*bio
= bio_alloc_mddev(GFP_NOIO
, 1, rdev
->mddev
);
823 struct completion event
;
828 bio
->bi_bdev
= (metadata_op
&& rdev
->meta_bdev
) ?
829 rdev
->meta_bdev
: rdev
->bdev
;
831 bio
->bi_sector
= sector
+ rdev
->sb_start
;
833 bio
->bi_sector
= sector
+ rdev
->data_offset
;
834 bio_add_page(bio
, page
, size
, 0);
835 init_completion(&event
);
836 bio
->bi_private
= &event
;
837 bio
->bi_end_io
= bi_complete
;
839 wait_for_completion(&event
);
841 ret
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
845 EXPORT_SYMBOL_GPL(sync_page_io
);
847 static int read_disk_sb(mdk_rdev_t
* rdev
, int size
)
849 char b
[BDEVNAME_SIZE
];
850 if (!rdev
->sb_page
) {
858 if (!sync_page_io(rdev
, 0, size
, rdev
->sb_page
, READ
, true))
864 printk(KERN_WARNING
"md: disabled device %s, could not read superblock.\n",
865 bdevname(rdev
->bdev
,b
));
869 static int uuid_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
871 return sb1
->set_uuid0
== sb2
->set_uuid0
&&
872 sb1
->set_uuid1
== sb2
->set_uuid1
&&
873 sb1
->set_uuid2
== sb2
->set_uuid2
&&
874 sb1
->set_uuid3
== sb2
->set_uuid3
;
877 static int sb_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
880 mdp_super_t
*tmp1
, *tmp2
;
882 tmp1
= kmalloc(sizeof(*tmp1
),GFP_KERNEL
);
883 tmp2
= kmalloc(sizeof(*tmp2
),GFP_KERNEL
);
885 if (!tmp1
|| !tmp2
) {
887 printk(KERN_INFO
"md.c sb_equal(): failed to allocate memory!\n");
895 * nr_disks is not constant
900 ret
= (memcmp(tmp1
, tmp2
, MD_SB_GENERIC_CONSTANT_WORDS
* 4) == 0);
908 static u32
md_csum_fold(u32 csum
)
910 csum
= (csum
& 0xffff) + (csum
>> 16);
911 return (csum
& 0xffff) + (csum
>> 16);
914 static unsigned int calc_sb_csum(mdp_super_t
* sb
)
917 u32
*sb32
= (u32
*)sb
;
919 unsigned int disk_csum
, csum
;
921 disk_csum
= sb
->sb_csum
;
924 for (i
= 0; i
< MD_SB_BYTES
/4 ; i
++)
926 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
930 /* This used to use csum_partial, which was wrong for several
931 * reasons including that different results are returned on
932 * different architectures. It isn't critical that we get exactly
933 * the same return value as before (we always csum_fold before
934 * testing, and that removes any differences). However as we
935 * know that csum_partial always returned a 16bit value on
936 * alphas, do a fold to maximise conformity to previous behaviour.
938 sb
->sb_csum
= md_csum_fold(disk_csum
);
940 sb
->sb_csum
= disk_csum
;
947 * Handle superblock details.
948 * We want to be able to handle multiple superblock formats
949 * so we have a common interface to them all, and an array of
950 * different handlers.
951 * We rely on user-space to write the initial superblock, and support
952 * reading and updating of superblocks.
953 * Interface methods are:
954 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
955 * loads and validates a superblock on dev.
956 * if refdev != NULL, compare superblocks on both devices
958 * 0 - dev has a superblock that is compatible with refdev
959 * 1 - dev has a superblock that is compatible and newer than refdev
960 * so dev should be used as the refdev in future
961 * -EINVAL superblock incompatible or invalid
962 * -othererror e.g. -EIO
964 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
965 * Verify that dev is acceptable into mddev.
966 * The first time, mddev->raid_disks will be 0, and data from
967 * dev should be merged in. Subsequent calls check that dev
968 * is new enough. Return 0 or -EINVAL
970 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
971 * Update the superblock for rdev with data in mddev
972 * This does not write to disc.
978 struct module
*owner
;
979 int (*load_super
)(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
,
981 int (*validate_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
982 void (*sync_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
983 unsigned long long (*rdev_size_change
)(mdk_rdev_t
*rdev
,
984 sector_t num_sectors
);
988 * Check that the given mddev has no bitmap.
990 * This function is called from the run method of all personalities that do not
991 * support bitmaps. It prints an error message and returns non-zero if mddev
992 * has a bitmap. Otherwise, it returns 0.
995 int md_check_no_bitmap(mddev_t
*mddev
)
997 if (!mddev
->bitmap_info
.file
&& !mddev
->bitmap_info
.offset
)
999 printk(KERN_ERR
"%s: bitmaps are not supported for %s\n",
1000 mdname(mddev
), mddev
->pers
->name
);
1003 EXPORT_SYMBOL(md_check_no_bitmap
);
1006 * load_super for 0.90.0
1008 static int super_90_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
1010 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1015 * Calculate the position of the superblock (512byte sectors),
1016 * it's at the end of the disk.
1018 * It also happens to be a multiple of 4Kb.
1020 rdev
->sb_start
= calc_dev_sboffset(rdev
);
1022 ret
= read_disk_sb(rdev
, MD_SB_BYTES
);
1023 if (ret
) return ret
;
1027 bdevname(rdev
->bdev
, b
);
1028 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
1030 if (sb
->md_magic
!= MD_SB_MAGIC
) {
1031 printk(KERN_ERR
"md: invalid raid superblock magic on %s\n",
1036 if (sb
->major_version
!= 0 ||
1037 sb
->minor_version
< 90 ||
1038 sb
->minor_version
> 91) {
1039 printk(KERN_WARNING
"Bad version number %d.%d on %s\n",
1040 sb
->major_version
, sb
->minor_version
,
1045 if (sb
->raid_disks
<= 0)
1048 if (md_csum_fold(calc_sb_csum(sb
)) != md_csum_fold(sb
->sb_csum
)) {
1049 printk(KERN_WARNING
"md: invalid superblock checksum on %s\n",
1054 rdev
->preferred_minor
= sb
->md_minor
;
1055 rdev
->data_offset
= 0;
1056 rdev
->sb_size
= MD_SB_BYTES
;
1058 if (sb
->level
== LEVEL_MULTIPATH
)
1061 rdev
->desc_nr
= sb
->this_disk
.number
;
1067 mdp_super_t
*refsb
= (mdp_super_t
*)page_address(refdev
->sb_page
);
1068 if (!uuid_equal(refsb
, sb
)) {
1069 printk(KERN_WARNING
"md: %s has different UUID to %s\n",
1070 b
, bdevname(refdev
->bdev
,b2
));
1073 if (!sb_equal(refsb
, sb
)) {
1074 printk(KERN_WARNING
"md: %s has same UUID"
1075 " but different superblock to %s\n",
1076 b
, bdevname(refdev
->bdev
, b2
));
1080 ev2
= md_event(refsb
);
1086 rdev
->sectors
= rdev
->sb_start
;
1088 if (rdev
->sectors
< sb
->size
* 2 && sb
->level
> 1)
1089 /* "this cannot possibly happen" ... */
1097 * validate_super for 0.90.0
1099 static int super_90_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1102 mdp_super_t
*sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
1103 __u64 ev1
= md_event(sb
);
1105 rdev
->raid_disk
= -1;
1106 clear_bit(Faulty
, &rdev
->flags
);
1107 clear_bit(In_sync
, &rdev
->flags
);
1108 clear_bit(WriteMostly
, &rdev
->flags
);
1110 if (mddev
->raid_disks
== 0) {
1111 mddev
->major_version
= 0;
1112 mddev
->minor_version
= sb
->minor_version
;
1113 mddev
->patch_version
= sb
->patch_version
;
1114 mddev
->external
= 0;
1115 mddev
->chunk_sectors
= sb
->chunk_size
>> 9;
1116 mddev
->ctime
= sb
->ctime
;
1117 mddev
->utime
= sb
->utime
;
1118 mddev
->level
= sb
->level
;
1119 mddev
->clevel
[0] = 0;
1120 mddev
->layout
= sb
->layout
;
1121 mddev
->raid_disks
= sb
->raid_disks
;
1122 mddev
->dev_sectors
= sb
->size
* 2;
1123 mddev
->events
= ev1
;
1124 mddev
->bitmap_info
.offset
= 0;
1125 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
1127 if (mddev
->minor_version
>= 91) {
1128 mddev
->reshape_position
= sb
->reshape_position
;
1129 mddev
->delta_disks
= sb
->delta_disks
;
1130 mddev
->new_level
= sb
->new_level
;
1131 mddev
->new_layout
= sb
->new_layout
;
1132 mddev
->new_chunk_sectors
= sb
->new_chunk
>> 9;
1134 mddev
->reshape_position
= MaxSector
;
1135 mddev
->delta_disks
= 0;
1136 mddev
->new_level
= mddev
->level
;
1137 mddev
->new_layout
= mddev
->layout
;
1138 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1141 if (sb
->state
& (1<<MD_SB_CLEAN
))
1142 mddev
->recovery_cp
= MaxSector
;
1144 if (sb
->events_hi
== sb
->cp_events_hi
&&
1145 sb
->events_lo
== sb
->cp_events_lo
) {
1146 mddev
->recovery_cp
= sb
->recovery_cp
;
1148 mddev
->recovery_cp
= 0;
1151 memcpy(mddev
->uuid
+0, &sb
->set_uuid0
, 4);
1152 memcpy(mddev
->uuid
+4, &sb
->set_uuid1
, 4);
1153 memcpy(mddev
->uuid
+8, &sb
->set_uuid2
, 4);
1154 memcpy(mddev
->uuid
+12,&sb
->set_uuid3
, 4);
1156 mddev
->max_disks
= MD_SB_DISKS
;
1158 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
) &&
1159 mddev
->bitmap_info
.file
== NULL
)
1160 mddev
->bitmap_info
.offset
=
1161 mddev
->bitmap_info
.default_offset
;
1163 } else if (mddev
->pers
== NULL
) {
1164 /* Insist on good event counter while assembling, except
1165 * for spares (which don't need an event count) */
1167 if (sb
->disks
[rdev
->desc_nr
].state
& (
1168 (1<<MD_DISK_SYNC
) | (1 << MD_DISK_ACTIVE
)))
1169 if (ev1
< mddev
->events
)
1171 } else if (mddev
->bitmap
) {
1172 /* if adding to array with a bitmap, then we can accept an
1173 * older device ... but not too old.
1175 if (ev1
< mddev
->bitmap
->events_cleared
)
1178 if (ev1
< mddev
->events
)
1179 /* just a hot-add of a new device, leave raid_disk at -1 */
1183 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1184 desc
= sb
->disks
+ rdev
->desc_nr
;
1186 if (desc
->state
& (1<<MD_DISK_FAULTY
))
1187 set_bit(Faulty
, &rdev
->flags
);
1188 else if (desc
->state
& (1<<MD_DISK_SYNC
) /* &&
1189 desc->raid_disk < mddev->raid_disks */) {
1190 set_bit(In_sync
, &rdev
->flags
);
1191 rdev
->raid_disk
= desc
->raid_disk
;
1192 } else if (desc
->state
& (1<<MD_DISK_ACTIVE
)) {
1193 /* active but not in sync implies recovery up to
1194 * reshape position. We don't know exactly where
1195 * that is, so set to zero for now */
1196 if (mddev
->minor_version
>= 91) {
1197 rdev
->recovery_offset
= 0;
1198 rdev
->raid_disk
= desc
->raid_disk
;
1201 if (desc
->state
& (1<<MD_DISK_WRITEMOSTLY
))
1202 set_bit(WriteMostly
, &rdev
->flags
);
1203 } else /* MULTIPATH are always insync */
1204 set_bit(In_sync
, &rdev
->flags
);
1209 * sync_super for 0.90.0
1211 static void super_90_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1215 int next_spare
= mddev
->raid_disks
;
1218 /* make rdev->sb match mddev data..
1221 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1222 * 3/ any empty disks < next_spare become removed
1224 * disks[0] gets initialised to REMOVED because
1225 * we cannot be sure from other fields if it has
1226 * been initialised or not.
1229 int active
=0, working
=0,failed
=0,spare
=0,nr_disks
=0;
1231 rdev
->sb_size
= MD_SB_BYTES
;
1233 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
1235 memset(sb
, 0, sizeof(*sb
));
1237 sb
->md_magic
= MD_SB_MAGIC
;
1238 sb
->major_version
= mddev
->major_version
;
1239 sb
->patch_version
= mddev
->patch_version
;
1240 sb
->gvalid_words
= 0; /* ignored */
1241 memcpy(&sb
->set_uuid0
, mddev
->uuid
+0, 4);
1242 memcpy(&sb
->set_uuid1
, mddev
->uuid
+4, 4);
1243 memcpy(&sb
->set_uuid2
, mddev
->uuid
+8, 4);
1244 memcpy(&sb
->set_uuid3
, mddev
->uuid
+12,4);
1246 sb
->ctime
= mddev
->ctime
;
1247 sb
->level
= mddev
->level
;
1248 sb
->size
= mddev
->dev_sectors
/ 2;
1249 sb
->raid_disks
= mddev
->raid_disks
;
1250 sb
->md_minor
= mddev
->md_minor
;
1251 sb
->not_persistent
= 0;
1252 sb
->utime
= mddev
->utime
;
1254 sb
->events_hi
= (mddev
->events
>>32);
1255 sb
->events_lo
= (u32
)mddev
->events
;
1257 if (mddev
->reshape_position
== MaxSector
)
1258 sb
->minor_version
= 90;
1260 sb
->minor_version
= 91;
1261 sb
->reshape_position
= mddev
->reshape_position
;
1262 sb
->new_level
= mddev
->new_level
;
1263 sb
->delta_disks
= mddev
->delta_disks
;
1264 sb
->new_layout
= mddev
->new_layout
;
1265 sb
->new_chunk
= mddev
->new_chunk_sectors
<< 9;
1267 mddev
->minor_version
= sb
->minor_version
;
1270 sb
->recovery_cp
= mddev
->recovery_cp
;
1271 sb
->cp_events_hi
= (mddev
->events
>>32);
1272 sb
->cp_events_lo
= (u32
)mddev
->events
;
1273 if (mddev
->recovery_cp
== MaxSector
)
1274 sb
->state
= (1<< MD_SB_CLEAN
);
1276 sb
->recovery_cp
= 0;
1278 sb
->layout
= mddev
->layout
;
1279 sb
->chunk_size
= mddev
->chunk_sectors
<< 9;
1281 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
)
1282 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
1284 sb
->disks
[0].state
= (1<<MD_DISK_REMOVED
);
1285 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
1288 int is_active
= test_bit(In_sync
, &rdev2
->flags
);
1290 if (rdev2
->raid_disk
>= 0 &&
1291 sb
->minor_version
>= 91)
1292 /* we have nowhere to store the recovery_offset,
1293 * but if it is not below the reshape_position,
1294 * we can piggy-back on that.
1297 if (rdev2
->raid_disk
< 0 ||
1298 test_bit(Faulty
, &rdev2
->flags
))
1301 desc_nr
= rdev2
->raid_disk
;
1303 desc_nr
= next_spare
++;
1304 rdev2
->desc_nr
= desc_nr
;
1305 d
= &sb
->disks
[rdev2
->desc_nr
];
1307 d
->number
= rdev2
->desc_nr
;
1308 d
->major
= MAJOR(rdev2
->bdev
->bd_dev
);
1309 d
->minor
= MINOR(rdev2
->bdev
->bd_dev
);
1311 d
->raid_disk
= rdev2
->raid_disk
;
1313 d
->raid_disk
= rdev2
->desc_nr
; /* compatibility */
1314 if (test_bit(Faulty
, &rdev2
->flags
))
1315 d
->state
= (1<<MD_DISK_FAULTY
);
1316 else if (is_active
) {
1317 d
->state
= (1<<MD_DISK_ACTIVE
);
1318 if (test_bit(In_sync
, &rdev2
->flags
))
1319 d
->state
|= (1<<MD_DISK_SYNC
);
1327 if (test_bit(WriteMostly
, &rdev2
->flags
))
1328 d
->state
|= (1<<MD_DISK_WRITEMOSTLY
);
1330 /* now set the "removed" and "faulty" bits on any missing devices */
1331 for (i
=0 ; i
< mddev
->raid_disks
; i
++) {
1332 mdp_disk_t
*d
= &sb
->disks
[i
];
1333 if (d
->state
== 0 && d
->number
== 0) {
1336 d
->state
= (1<<MD_DISK_REMOVED
);
1337 d
->state
|= (1<<MD_DISK_FAULTY
);
1341 sb
->nr_disks
= nr_disks
;
1342 sb
->active_disks
= active
;
1343 sb
->working_disks
= working
;
1344 sb
->failed_disks
= failed
;
1345 sb
->spare_disks
= spare
;
1347 sb
->this_disk
= sb
->disks
[rdev
->desc_nr
];
1348 sb
->sb_csum
= calc_sb_csum(sb
);
1352 * rdev_size_change for 0.90.0
1354 static unsigned long long
1355 super_90_rdev_size_change(mdk_rdev_t
*rdev
, sector_t num_sectors
)
1357 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1358 return 0; /* component must fit device */
1359 if (rdev
->mddev
->bitmap_info
.offset
)
1360 return 0; /* can't move bitmap */
1361 rdev
->sb_start
= calc_dev_sboffset(rdev
);
1362 if (!num_sectors
|| num_sectors
> rdev
->sb_start
)
1363 num_sectors
= rdev
->sb_start
;
1364 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1366 md_super_wait(rdev
->mddev
);
1372 * version 1 superblock
1375 static __le32
calc_sb_1_csum(struct mdp_superblock_1
* sb
)
1379 unsigned long long newcsum
;
1380 int size
= 256 + le32_to_cpu(sb
->max_dev
)*2;
1381 __le32
*isuper
= (__le32
*)sb
;
1384 disk_csum
= sb
->sb_csum
;
1387 for (i
=0; size
>=4; size
-= 4 )
1388 newcsum
+= le32_to_cpu(*isuper
++);
1391 newcsum
+= le16_to_cpu(*(__le16
*) isuper
);
1393 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
1394 sb
->sb_csum
= disk_csum
;
1395 return cpu_to_le32(csum
);
1398 static int super_1_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
1400 struct mdp_superblock_1
*sb
;
1403 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1407 * Calculate the position of the superblock in 512byte sectors.
1408 * It is always aligned to a 4K boundary and
1409 * depeding on minor_version, it can be:
1410 * 0: At least 8K, but less than 12K, from end of device
1411 * 1: At start of device
1412 * 2: 4K from start of device.
1414 switch(minor_version
) {
1416 sb_start
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
1418 sb_start
&= ~(sector_t
)(4*2-1);
1429 rdev
->sb_start
= sb_start
;
1431 /* superblock is rarely larger than 1K, but it can be larger,
1432 * and it is safe to read 4k, so we do that
1434 ret
= read_disk_sb(rdev
, 4096);
1435 if (ret
) return ret
;
1438 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1440 if (sb
->magic
!= cpu_to_le32(MD_SB_MAGIC
) ||
1441 sb
->major_version
!= cpu_to_le32(1) ||
1442 le32_to_cpu(sb
->max_dev
) > (4096-256)/2 ||
1443 le64_to_cpu(sb
->super_offset
) != rdev
->sb_start
||
1444 (le32_to_cpu(sb
->feature_map
) & ~MD_FEATURE_ALL
) != 0)
1447 if (calc_sb_1_csum(sb
) != sb
->sb_csum
) {
1448 printk("md: invalid superblock checksum on %s\n",
1449 bdevname(rdev
->bdev
,b
));
1452 if (le64_to_cpu(sb
->data_size
) < 10) {
1453 printk("md: data_size too small on %s\n",
1454 bdevname(rdev
->bdev
,b
));
1458 rdev
->preferred_minor
= 0xffff;
1459 rdev
->data_offset
= le64_to_cpu(sb
->data_offset
);
1460 atomic_set(&rdev
->corrected_errors
, le32_to_cpu(sb
->cnt_corrected_read
));
1462 rdev
->sb_size
= le32_to_cpu(sb
->max_dev
) * 2 + 256;
1463 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1464 if (rdev
->sb_size
& bmask
)
1465 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1468 && rdev
->data_offset
< sb_start
+ (rdev
->sb_size
/512))
1471 if (sb
->level
== cpu_to_le32(LEVEL_MULTIPATH
))
1474 rdev
->desc_nr
= le32_to_cpu(sb
->dev_number
);
1480 struct mdp_superblock_1
*refsb
=
1481 (struct mdp_superblock_1
*)page_address(refdev
->sb_page
);
1483 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16) != 0 ||
1484 sb
->level
!= refsb
->level
||
1485 sb
->layout
!= refsb
->layout
||
1486 sb
->chunksize
!= refsb
->chunksize
) {
1487 printk(KERN_WARNING
"md: %s has strangely different"
1488 " superblock to %s\n",
1489 bdevname(rdev
->bdev
,b
),
1490 bdevname(refdev
->bdev
,b2
));
1493 ev1
= le64_to_cpu(sb
->events
);
1494 ev2
= le64_to_cpu(refsb
->events
);
1502 rdev
->sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) -
1503 le64_to_cpu(sb
->data_offset
);
1505 rdev
->sectors
= rdev
->sb_start
;
1506 if (rdev
->sectors
< le64_to_cpu(sb
->data_size
))
1508 rdev
->sectors
= le64_to_cpu(sb
->data_size
);
1509 if (le64_to_cpu(sb
->size
) > rdev
->sectors
)
1514 static int super_1_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1516 struct mdp_superblock_1
*sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1517 __u64 ev1
= le64_to_cpu(sb
->events
);
1519 rdev
->raid_disk
= -1;
1520 clear_bit(Faulty
, &rdev
->flags
);
1521 clear_bit(In_sync
, &rdev
->flags
);
1522 clear_bit(WriteMostly
, &rdev
->flags
);
1524 if (mddev
->raid_disks
== 0) {
1525 mddev
->major_version
= 1;
1526 mddev
->patch_version
= 0;
1527 mddev
->external
= 0;
1528 mddev
->chunk_sectors
= le32_to_cpu(sb
->chunksize
);
1529 mddev
->ctime
= le64_to_cpu(sb
->ctime
) & ((1ULL << 32)-1);
1530 mddev
->utime
= le64_to_cpu(sb
->utime
) & ((1ULL << 32)-1);
1531 mddev
->level
= le32_to_cpu(sb
->level
);
1532 mddev
->clevel
[0] = 0;
1533 mddev
->layout
= le32_to_cpu(sb
->layout
);
1534 mddev
->raid_disks
= le32_to_cpu(sb
->raid_disks
);
1535 mddev
->dev_sectors
= le64_to_cpu(sb
->size
);
1536 mddev
->events
= ev1
;
1537 mddev
->bitmap_info
.offset
= 0;
1538 mddev
->bitmap_info
.default_offset
= 1024 >> 9;
1540 mddev
->recovery_cp
= le64_to_cpu(sb
->resync_offset
);
1541 memcpy(mddev
->uuid
, sb
->set_uuid
, 16);
1543 mddev
->max_disks
= (4096-256)/2;
1545 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BITMAP_OFFSET
) &&
1546 mddev
->bitmap_info
.file
== NULL
)
1547 mddev
->bitmap_info
.offset
=
1548 (__s32
)le32_to_cpu(sb
->bitmap_offset
);
1550 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
1551 mddev
->reshape_position
= le64_to_cpu(sb
->reshape_position
);
1552 mddev
->delta_disks
= le32_to_cpu(sb
->delta_disks
);
1553 mddev
->new_level
= le32_to_cpu(sb
->new_level
);
1554 mddev
->new_layout
= le32_to_cpu(sb
->new_layout
);
1555 mddev
->new_chunk_sectors
= le32_to_cpu(sb
->new_chunk
);
1557 mddev
->reshape_position
= MaxSector
;
1558 mddev
->delta_disks
= 0;
1559 mddev
->new_level
= mddev
->level
;
1560 mddev
->new_layout
= mddev
->layout
;
1561 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1564 } else if (mddev
->pers
== NULL
) {
1565 /* Insist of good event counter while assembling, except for
1566 * spares (which don't need an event count) */
1568 if (rdev
->desc_nr
>= 0 &&
1569 rdev
->desc_nr
< le32_to_cpu(sb
->max_dev
) &&
1570 le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]) < 0xfffe)
1571 if (ev1
< mddev
->events
)
1573 } else if (mddev
->bitmap
) {
1574 /* If adding to array with a bitmap, then we can accept an
1575 * older device, but not too old.
1577 if (ev1
< mddev
->bitmap
->events_cleared
)
1580 if (ev1
< mddev
->events
)
1581 /* just a hot-add of a new device, leave raid_disk at -1 */
1584 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1586 if (rdev
->desc_nr
< 0 ||
1587 rdev
->desc_nr
>= le32_to_cpu(sb
->max_dev
)) {
1591 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
1593 case 0xffff: /* spare */
1595 case 0xfffe: /* faulty */
1596 set_bit(Faulty
, &rdev
->flags
);
1599 if ((le32_to_cpu(sb
->feature_map
) &
1600 MD_FEATURE_RECOVERY_OFFSET
))
1601 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
1603 set_bit(In_sync
, &rdev
->flags
);
1604 rdev
->raid_disk
= role
;
1607 if (sb
->devflags
& WriteMostly1
)
1608 set_bit(WriteMostly
, &rdev
->flags
);
1609 } else /* MULTIPATH are always insync */
1610 set_bit(In_sync
, &rdev
->flags
);
1615 static void super_1_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1617 struct mdp_superblock_1
*sb
;
1620 /* make rdev->sb match mddev and rdev data. */
1622 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1624 sb
->feature_map
= 0;
1626 sb
->recovery_offset
= cpu_to_le64(0);
1627 memset(sb
->pad1
, 0, sizeof(sb
->pad1
));
1628 memset(sb
->pad2
, 0, sizeof(sb
->pad2
));
1629 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
1631 sb
->utime
= cpu_to_le64((__u64
)mddev
->utime
);
1632 sb
->events
= cpu_to_le64(mddev
->events
);
1634 sb
->resync_offset
= cpu_to_le64(mddev
->recovery_cp
);
1636 sb
->resync_offset
= cpu_to_le64(0);
1638 sb
->cnt_corrected_read
= cpu_to_le32(atomic_read(&rdev
->corrected_errors
));
1640 sb
->raid_disks
= cpu_to_le32(mddev
->raid_disks
);
1641 sb
->size
= cpu_to_le64(mddev
->dev_sectors
);
1642 sb
->chunksize
= cpu_to_le32(mddev
->chunk_sectors
);
1643 sb
->level
= cpu_to_le32(mddev
->level
);
1644 sb
->layout
= cpu_to_le32(mddev
->layout
);
1646 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
) {
1647 sb
->bitmap_offset
= cpu_to_le32((__u32
)mddev
->bitmap_info
.offset
);
1648 sb
->feature_map
= cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
);
1651 if (rdev
->raid_disk
>= 0 &&
1652 !test_bit(In_sync
, &rdev
->flags
)) {
1654 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET
);
1655 sb
->recovery_offset
=
1656 cpu_to_le64(rdev
->recovery_offset
);
1659 if (mddev
->reshape_position
!= MaxSector
) {
1660 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE
);
1661 sb
->reshape_position
= cpu_to_le64(mddev
->reshape_position
);
1662 sb
->new_layout
= cpu_to_le32(mddev
->new_layout
);
1663 sb
->delta_disks
= cpu_to_le32(mddev
->delta_disks
);
1664 sb
->new_level
= cpu_to_le32(mddev
->new_level
);
1665 sb
->new_chunk
= cpu_to_le32(mddev
->new_chunk_sectors
);
1669 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
)
1670 if (rdev2
->desc_nr
+1 > max_dev
)
1671 max_dev
= rdev2
->desc_nr
+1;
1673 if (max_dev
> le32_to_cpu(sb
->max_dev
)) {
1675 sb
->max_dev
= cpu_to_le32(max_dev
);
1676 rdev
->sb_size
= max_dev
* 2 + 256;
1677 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1678 if (rdev
->sb_size
& bmask
)
1679 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1681 max_dev
= le32_to_cpu(sb
->max_dev
);
1683 for (i
=0; i
<max_dev
;i
++)
1684 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1686 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
1688 if (test_bit(Faulty
, &rdev2
->flags
))
1689 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1690 else if (test_bit(In_sync
, &rdev2
->flags
))
1691 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1692 else if (rdev2
->raid_disk
>= 0)
1693 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1695 sb
->dev_roles
[i
] = cpu_to_le16(0xffff);
1698 sb
->sb_csum
= calc_sb_1_csum(sb
);
1701 static unsigned long long
1702 super_1_rdev_size_change(mdk_rdev_t
*rdev
, sector_t num_sectors
)
1704 struct mdp_superblock_1
*sb
;
1705 sector_t max_sectors
;
1706 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1707 return 0; /* component must fit device */
1708 if (rdev
->sb_start
< rdev
->data_offset
) {
1709 /* minor versions 1 and 2; superblock before data */
1710 max_sectors
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
1711 max_sectors
-= rdev
->data_offset
;
1712 if (!num_sectors
|| num_sectors
> max_sectors
)
1713 num_sectors
= max_sectors
;
1714 } else if (rdev
->mddev
->bitmap_info
.offset
) {
1715 /* minor version 0 with bitmap we can't move */
1718 /* minor version 0; superblock after data */
1720 sb_start
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) - 8*2;
1721 sb_start
&= ~(sector_t
)(4*2 - 1);
1722 max_sectors
= rdev
->sectors
+ sb_start
- rdev
->sb_start
;
1723 if (!num_sectors
|| num_sectors
> max_sectors
)
1724 num_sectors
= max_sectors
;
1725 rdev
->sb_start
= sb_start
;
1727 sb
= (struct mdp_superblock_1
*) page_address(rdev
->sb_page
);
1728 sb
->data_size
= cpu_to_le64(num_sectors
);
1729 sb
->super_offset
= rdev
->sb_start
;
1730 sb
->sb_csum
= calc_sb_1_csum(sb
);
1731 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1733 md_super_wait(rdev
->mddev
);
1737 static struct super_type super_types
[] = {
1740 .owner
= THIS_MODULE
,
1741 .load_super
= super_90_load
,
1742 .validate_super
= super_90_validate
,
1743 .sync_super
= super_90_sync
,
1744 .rdev_size_change
= super_90_rdev_size_change
,
1748 .owner
= THIS_MODULE
,
1749 .load_super
= super_1_load
,
1750 .validate_super
= super_1_validate
,
1751 .sync_super
= super_1_sync
,
1752 .rdev_size_change
= super_1_rdev_size_change
,
1756 static void sync_super(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1758 if (mddev
->sync_super
) {
1759 mddev
->sync_super(mddev
, rdev
);
1763 BUG_ON(mddev
->major_version
>= ARRAY_SIZE(super_types
));
1765 super_types
[mddev
->major_version
].sync_super(mddev
, rdev
);
1768 static int match_mddev_units(mddev_t
*mddev1
, mddev_t
*mddev2
)
1770 mdk_rdev_t
*rdev
, *rdev2
;
1773 rdev_for_each_rcu(rdev
, mddev1
)
1774 rdev_for_each_rcu(rdev2
, mddev2
)
1775 if (rdev
->bdev
->bd_contains
==
1776 rdev2
->bdev
->bd_contains
) {
1784 static LIST_HEAD(pending_raid_disks
);
1787 * Try to register data integrity profile for an mddev
1789 * This is called when an array is started and after a disk has been kicked
1790 * from the array. It only succeeds if all working and active component devices
1791 * are integrity capable with matching profiles.
1793 int md_integrity_register(mddev_t
*mddev
)
1795 mdk_rdev_t
*rdev
, *reference
= NULL
;
1797 if (list_empty(&mddev
->disks
))
1798 return 0; /* nothing to do */
1799 if (!mddev
->gendisk
|| blk_get_integrity(mddev
->gendisk
))
1800 return 0; /* shouldn't register, or already is */
1801 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
1802 /* skip spares and non-functional disks */
1803 if (test_bit(Faulty
, &rdev
->flags
))
1805 if (rdev
->raid_disk
< 0)
1808 /* Use the first rdev as the reference */
1812 /* does this rdev's profile match the reference profile? */
1813 if (blk_integrity_compare(reference
->bdev
->bd_disk
,
1814 rdev
->bdev
->bd_disk
) < 0)
1817 if (!reference
|| !bdev_get_integrity(reference
->bdev
))
1820 * All component devices are integrity capable and have matching
1821 * profiles, register the common profile for the md device.
1823 if (blk_integrity_register(mddev
->gendisk
,
1824 bdev_get_integrity(reference
->bdev
)) != 0) {
1825 printk(KERN_ERR
"md: failed to register integrity for %s\n",
1829 printk(KERN_NOTICE
"md: data integrity enabled on %s\n", mdname(mddev
));
1830 if (bioset_integrity_create(mddev
->bio_set
, BIO_POOL_SIZE
)) {
1831 printk(KERN_ERR
"md: failed to create integrity pool for %s\n",
1837 EXPORT_SYMBOL(md_integrity_register
);
1839 /* Disable data integrity if non-capable/non-matching disk is being added */
1840 void md_integrity_add_rdev(mdk_rdev_t
*rdev
, mddev_t
*mddev
)
1842 struct blk_integrity
*bi_rdev
= bdev_get_integrity(rdev
->bdev
);
1843 struct blk_integrity
*bi_mddev
= blk_get_integrity(mddev
->gendisk
);
1845 if (!bi_mddev
) /* nothing to do */
1847 if (rdev
->raid_disk
< 0) /* skip spares */
1849 if (bi_rdev
&& blk_integrity_compare(mddev
->gendisk
,
1850 rdev
->bdev
->bd_disk
) >= 0)
1852 printk(KERN_NOTICE
"disabling data integrity on %s\n", mdname(mddev
));
1853 blk_integrity_unregister(mddev
->gendisk
);
1855 EXPORT_SYMBOL(md_integrity_add_rdev
);
1857 static int bind_rdev_to_array(mdk_rdev_t
* rdev
, mddev_t
* mddev
)
1859 char b
[BDEVNAME_SIZE
];
1869 /* prevent duplicates */
1870 if (find_rdev(mddev
, rdev
->bdev
->bd_dev
))
1873 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1874 if (rdev
->sectors
&& (mddev
->dev_sectors
== 0 ||
1875 rdev
->sectors
< mddev
->dev_sectors
)) {
1877 /* Cannot change size, so fail
1878 * If mddev->level <= 0, then we don't care
1879 * about aligning sizes (e.g. linear)
1881 if (mddev
->level
> 0)
1884 mddev
->dev_sectors
= rdev
->sectors
;
1887 /* Verify rdev->desc_nr is unique.
1888 * If it is -1, assign a free number, else
1889 * check number is not in use
1891 if (rdev
->desc_nr
< 0) {
1893 if (mddev
->pers
) choice
= mddev
->raid_disks
;
1894 while (find_rdev_nr(mddev
, choice
))
1896 rdev
->desc_nr
= choice
;
1898 if (find_rdev_nr(mddev
, rdev
->desc_nr
))
1901 if (mddev
->max_disks
&& rdev
->desc_nr
>= mddev
->max_disks
) {
1902 printk(KERN_WARNING
"md: %s: array is limited to %d devices\n",
1903 mdname(mddev
), mddev
->max_disks
);
1906 bdevname(rdev
->bdev
,b
);
1907 while ( (s
=strchr(b
, '/')) != NULL
)
1910 rdev
->mddev
= mddev
;
1911 printk(KERN_INFO
"md: bind<%s>\n", b
);
1913 if ((err
= kobject_add(&rdev
->kobj
, &mddev
->kobj
, "dev-%s", b
)))
1916 ko
= &part_to_dev(rdev
->bdev
->bd_part
)->kobj
;
1917 if (sysfs_create_link(&rdev
->kobj
, ko
, "block"))
1918 /* failure here is OK */;
1919 rdev
->sysfs_state
= sysfs_get_dirent_safe(rdev
->kobj
.sd
, "state");
1921 list_add_rcu(&rdev
->same_set
, &mddev
->disks
);
1922 bd_link_disk_holder(rdev
->bdev
, mddev
->gendisk
);
1924 /* May as well allow recovery to be retried once */
1925 mddev
->recovery_disabled
= 0;
1930 printk(KERN_WARNING
"md: failed to register dev-%s for %s\n",
1935 static void md_delayed_delete(struct work_struct
*ws
)
1937 mdk_rdev_t
*rdev
= container_of(ws
, mdk_rdev_t
, del_work
);
1938 kobject_del(&rdev
->kobj
);
1939 kobject_put(&rdev
->kobj
);
1942 static void unbind_rdev_from_array(mdk_rdev_t
* rdev
)
1944 char b
[BDEVNAME_SIZE
];
1949 bd_unlink_disk_holder(rdev
->bdev
, rdev
->mddev
->gendisk
);
1950 list_del_rcu(&rdev
->same_set
);
1951 printk(KERN_INFO
"md: unbind<%s>\n", bdevname(rdev
->bdev
,b
));
1953 sysfs_remove_link(&rdev
->kobj
, "block");
1954 sysfs_put(rdev
->sysfs_state
);
1955 rdev
->sysfs_state
= NULL
;
1956 /* We need to delay this, otherwise we can deadlock when
1957 * writing to 'remove' to "dev/state". We also need
1958 * to delay it due to rcu usage.
1961 INIT_WORK(&rdev
->del_work
, md_delayed_delete
);
1962 kobject_get(&rdev
->kobj
);
1963 queue_work(md_misc_wq
, &rdev
->del_work
);
1967 * prevent the device from being mounted, repartitioned or
1968 * otherwise reused by a RAID array (or any other kernel
1969 * subsystem), by bd_claiming the device.
1971 static int lock_rdev(mdk_rdev_t
*rdev
, dev_t dev
, int shared
)
1974 struct block_device
*bdev
;
1975 char b
[BDEVNAME_SIZE
];
1977 bdev
= blkdev_get_by_dev(dev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
,
1978 shared
? (mdk_rdev_t
*)lock_rdev
: rdev
);
1980 printk(KERN_ERR
"md: could not open %s.\n",
1981 __bdevname(dev
, b
));
1982 return PTR_ERR(bdev
);
1988 static void unlock_rdev(mdk_rdev_t
*rdev
)
1990 struct block_device
*bdev
= rdev
->bdev
;
1994 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
);
1997 void md_autodetect_dev(dev_t dev
);
1999 static void export_rdev(mdk_rdev_t
* rdev
)
2001 char b
[BDEVNAME_SIZE
];
2002 printk(KERN_INFO
"md: export_rdev(%s)\n",
2003 bdevname(rdev
->bdev
,b
));
2008 if (test_bit(AutoDetected
, &rdev
->flags
))
2009 md_autodetect_dev(rdev
->bdev
->bd_dev
);
2012 kobject_put(&rdev
->kobj
);
2015 static void kick_rdev_from_array(mdk_rdev_t
* rdev
)
2017 unbind_rdev_from_array(rdev
);
2021 static void export_array(mddev_t
*mddev
)
2023 mdk_rdev_t
*rdev
, *tmp
;
2025 rdev_for_each(rdev
, tmp
, mddev
) {
2030 kick_rdev_from_array(rdev
);
2032 if (!list_empty(&mddev
->disks
))
2034 mddev
->raid_disks
= 0;
2035 mddev
->major_version
= 0;
2038 static void print_desc(mdp_disk_t
*desc
)
2040 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc
->number
,
2041 desc
->major
,desc
->minor
,desc
->raid_disk
,desc
->state
);
2044 static void print_sb_90(mdp_super_t
*sb
)
2049 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2050 sb
->major_version
, sb
->minor_version
, sb
->patch_version
,
2051 sb
->set_uuid0
, sb
->set_uuid1
, sb
->set_uuid2
, sb
->set_uuid3
,
2053 printk(KERN_INFO
"md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2054 sb
->level
, sb
->size
, sb
->nr_disks
, sb
->raid_disks
,
2055 sb
->md_minor
, sb
->layout
, sb
->chunk_size
);
2056 printk(KERN_INFO
"md: UT:%08x ST:%d AD:%d WD:%d"
2057 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2058 sb
->utime
, sb
->state
, sb
->active_disks
, sb
->working_disks
,
2059 sb
->failed_disks
, sb
->spare_disks
,
2060 sb
->sb_csum
, (unsigned long)sb
->events_lo
);
2063 for (i
= 0; i
< MD_SB_DISKS
; i
++) {
2066 desc
= sb
->disks
+ i
;
2067 if (desc
->number
|| desc
->major
|| desc
->minor
||
2068 desc
->raid_disk
|| (desc
->state
&& (desc
->state
!= 4))) {
2069 printk(" D %2d: ", i
);
2073 printk(KERN_INFO
"md: THIS: ");
2074 print_desc(&sb
->this_disk
);
2077 static void print_sb_1(struct mdp_superblock_1
*sb
)
2081 uuid
= sb
->set_uuid
;
2083 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
2084 "md: Name: \"%s\" CT:%llu\n",
2085 le32_to_cpu(sb
->major_version
),
2086 le32_to_cpu(sb
->feature_map
),
2089 (unsigned long long)le64_to_cpu(sb
->ctime
)
2090 & MD_SUPERBLOCK_1_TIME_SEC_MASK
);
2092 uuid
= sb
->device_uuid
;
2094 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
2096 "md: Dev:%08x UUID: %pU\n"
2097 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2098 "md: (MaxDev:%u) \n",
2099 le32_to_cpu(sb
->level
),
2100 (unsigned long long)le64_to_cpu(sb
->size
),
2101 le32_to_cpu(sb
->raid_disks
),
2102 le32_to_cpu(sb
->layout
),
2103 le32_to_cpu(sb
->chunksize
),
2104 (unsigned long long)le64_to_cpu(sb
->data_offset
),
2105 (unsigned long long)le64_to_cpu(sb
->data_size
),
2106 (unsigned long long)le64_to_cpu(sb
->super_offset
),
2107 (unsigned long long)le64_to_cpu(sb
->recovery_offset
),
2108 le32_to_cpu(sb
->dev_number
),
2111 (unsigned long long)le64_to_cpu(sb
->utime
) & MD_SUPERBLOCK_1_TIME_SEC_MASK
,
2112 (unsigned long long)le64_to_cpu(sb
->events
),
2113 (unsigned long long)le64_to_cpu(sb
->resync_offset
),
2114 le32_to_cpu(sb
->sb_csum
),
2115 le32_to_cpu(sb
->max_dev
)
2119 static void print_rdev(mdk_rdev_t
*rdev
, int major_version
)
2121 char b
[BDEVNAME_SIZE
];
2122 printk(KERN_INFO
"md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2123 bdevname(rdev
->bdev
, b
), (unsigned long long)rdev
->sectors
,
2124 test_bit(Faulty
, &rdev
->flags
), test_bit(In_sync
, &rdev
->flags
),
2126 if (rdev
->sb_loaded
) {
2127 printk(KERN_INFO
"md: rdev superblock (MJ:%d):\n", major_version
);
2128 switch (major_version
) {
2130 print_sb_90((mdp_super_t
*)page_address(rdev
->sb_page
));
2133 print_sb_1((struct mdp_superblock_1
*)page_address(rdev
->sb_page
));
2137 printk(KERN_INFO
"md: no rdev superblock!\n");
2140 static void md_print_devices(void)
2142 struct list_head
*tmp
;
2145 char b
[BDEVNAME_SIZE
];
2148 printk("md: **********************************\n");
2149 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2150 printk("md: **********************************\n");
2151 for_each_mddev(mddev
, tmp
) {
2154 bitmap_print_sb(mddev
->bitmap
);
2156 printk("%s: ", mdname(mddev
));
2157 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
2158 printk("<%s>", bdevname(rdev
->bdev
,b
));
2161 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
2162 print_rdev(rdev
, mddev
->major_version
);
2164 printk("md: **********************************\n");
2169 static void sync_sbs(mddev_t
* mddev
, int nospares
)
2171 /* Update each superblock (in-memory image), but
2172 * if we are allowed to, skip spares which already
2173 * have the right event counter, or have one earlier
2174 * (which would mean they aren't being marked as dirty
2175 * with the rest of the array)
2178 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2179 if (rdev
->sb_events
== mddev
->events
||
2181 rdev
->raid_disk
< 0 &&
2182 rdev
->sb_events
+1 == mddev
->events
)) {
2183 /* Don't update this superblock */
2184 rdev
->sb_loaded
= 2;
2186 sync_super(mddev
, rdev
);
2187 rdev
->sb_loaded
= 1;
2192 static void md_update_sb(mddev_t
* mddev
, int force_change
)
2199 /* First make sure individual recovery_offsets are correct */
2200 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2201 if (rdev
->raid_disk
>= 0 &&
2202 mddev
->delta_disks
>= 0 &&
2203 !test_bit(In_sync
, &rdev
->flags
) &&
2204 mddev
->curr_resync_completed
> rdev
->recovery_offset
)
2205 rdev
->recovery_offset
= mddev
->curr_resync_completed
;
2208 if (!mddev
->persistent
) {
2209 clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
2210 clear_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
2211 if (!mddev
->external
)
2212 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
2213 wake_up(&mddev
->sb_wait
);
2217 spin_lock_irq(&mddev
->write_lock
);
2219 mddev
->utime
= get_seconds();
2221 if (test_and_clear_bit(MD_CHANGE_DEVS
, &mddev
->flags
))
2223 if (test_and_clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
))
2224 /* just a clean<-> dirty transition, possibly leave spares alone,
2225 * though if events isn't the right even/odd, we will have to do
2231 if (mddev
->degraded
)
2232 /* If the array is degraded, then skipping spares is both
2233 * dangerous and fairly pointless.
2234 * Dangerous because a device that was removed from the array
2235 * might have a event_count that still looks up-to-date,
2236 * so it can be re-added without a resync.
2237 * Pointless because if there are any spares to skip,
2238 * then a recovery will happen and soon that array won't
2239 * be degraded any more and the spare can go back to sleep then.
2243 sync_req
= mddev
->in_sync
;
2245 /* If this is just a dirty<->clean transition, and the array is clean
2246 * and 'events' is odd, we can roll back to the previous clean state */
2248 && (mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
2249 && mddev
->can_decrease_events
2250 && mddev
->events
!= 1) {
2252 mddev
->can_decrease_events
= 0;
2254 /* otherwise we have to go forward and ... */
2256 mddev
->can_decrease_events
= nospares
;
2259 if (!mddev
->events
) {
2261 * oops, this 64-bit counter should never wrap.
2262 * Either we are in around ~1 trillion A.C., assuming
2263 * 1 reboot per second, or we have a bug:
2268 sync_sbs(mddev
, nospares
);
2269 spin_unlock_irq(&mddev
->write_lock
);
2272 "md: updating %s RAID superblock on device (in sync %d)\n",
2273 mdname(mddev
),mddev
->in_sync
);
2275 bitmap_update_sb(mddev
->bitmap
);
2276 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2277 char b
[BDEVNAME_SIZE
];
2278 dprintk(KERN_INFO
"md: ");
2279 if (rdev
->sb_loaded
!= 1)
2280 continue; /* no noise on spare devices */
2281 if (test_bit(Faulty
, &rdev
->flags
))
2282 dprintk("(skipping faulty ");
2284 dprintk("%s ", bdevname(rdev
->bdev
,b
));
2285 if (!test_bit(Faulty
, &rdev
->flags
)) {
2286 md_super_write(mddev
,rdev
,
2287 rdev
->sb_start
, rdev
->sb_size
,
2289 dprintk(KERN_INFO
"(write) %s's sb offset: %llu\n",
2290 bdevname(rdev
->bdev
,b
),
2291 (unsigned long long)rdev
->sb_start
);
2292 rdev
->sb_events
= mddev
->events
;
2296 if (mddev
->level
== LEVEL_MULTIPATH
)
2297 /* only need to write one superblock... */
2300 md_super_wait(mddev
);
2301 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
2303 spin_lock_irq(&mddev
->write_lock
);
2304 if (mddev
->in_sync
!= sync_req
||
2305 test_bit(MD_CHANGE_DEVS
, &mddev
->flags
)) {
2306 /* have to write it out again */
2307 spin_unlock_irq(&mddev
->write_lock
);
2310 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
2311 spin_unlock_irq(&mddev
->write_lock
);
2312 wake_up(&mddev
->sb_wait
);
2313 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
2314 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
2318 /* words written to sysfs files may, or may not, be \n terminated.
2319 * We want to accept with case. For this we use cmd_match.
2321 static int cmd_match(const char *cmd
, const char *str
)
2323 /* See if cmd, written into a sysfs file, matches
2324 * str. They must either be the same, or cmd can
2325 * have a trailing newline
2327 while (*cmd
&& *str
&& *cmd
== *str
) {
2338 struct rdev_sysfs_entry
{
2339 struct attribute attr
;
2340 ssize_t (*show
)(mdk_rdev_t
*, char *);
2341 ssize_t (*store
)(mdk_rdev_t
*, const char *, size_t);
2345 state_show(mdk_rdev_t
*rdev
, char *page
)
2350 if (test_bit(Faulty
, &rdev
->flags
)) {
2351 len
+= sprintf(page
+len
, "%sfaulty",sep
);
2354 if (test_bit(In_sync
, &rdev
->flags
)) {
2355 len
+= sprintf(page
+len
, "%sin_sync",sep
);
2358 if (test_bit(WriteMostly
, &rdev
->flags
)) {
2359 len
+= sprintf(page
+len
, "%swrite_mostly",sep
);
2362 if (test_bit(Blocked
, &rdev
->flags
)) {
2363 len
+= sprintf(page
+len
, "%sblocked", sep
);
2366 if (!test_bit(Faulty
, &rdev
->flags
) &&
2367 !test_bit(In_sync
, &rdev
->flags
)) {
2368 len
+= sprintf(page
+len
, "%sspare", sep
);
2371 return len
+sprintf(page
+len
, "\n");
2375 state_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2378 * faulty - simulates and error
2379 * remove - disconnects the device
2380 * writemostly - sets write_mostly
2381 * -writemostly - clears write_mostly
2382 * blocked - sets the Blocked flag
2383 * -blocked - clears the Blocked flag
2384 * insync - sets Insync providing device isn't active
2387 if (cmd_match(buf
, "faulty") && rdev
->mddev
->pers
) {
2388 md_error(rdev
->mddev
, rdev
);
2390 } else if (cmd_match(buf
, "remove")) {
2391 if (rdev
->raid_disk
>= 0)
2394 mddev_t
*mddev
= rdev
->mddev
;
2395 kick_rdev_from_array(rdev
);
2397 md_update_sb(mddev
, 1);
2398 md_new_event(mddev
);
2401 } else if (cmd_match(buf
, "writemostly")) {
2402 set_bit(WriteMostly
, &rdev
->flags
);
2404 } else if (cmd_match(buf
, "-writemostly")) {
2405 clear_bit(WriteMostly
, &rdev
->flags
);
2407 } else if (cmd_match(buf
, "blocked")) {
2408 set_bit(Blocked
, &rdev
->flags
);
2410 } else if (cmd_match(buf
, "-blocked")) {
2411 clear_bit(Blocked
, &rdev
->flags
);
2412 wake_up(&rdev
->blocked_wait
);
2413 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2414 md_wakeup_thread(rdev
->mddev
->thread
);
2417 } else if (cmd_match(buf
, "insync") && rdev
->raid_disk
== -1) {
2418 set_bit(In_sync
, &rdev
->flags
);
2422 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2423 return err
? err
: len
;
2425 static struct rdev_sysfs_entry rdev_state
=
2426 __ATTR(state
, S_IRUGO
|S_IWUSR
, state_show
, state_store
);
2429 errors_show(mdk_rdev_t
*rdev
, char *page
)
2431 return sprintf(page
, "%d\n", atomic_read(&rdev
->corrected_errors
));
2435 errors_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2438 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2439 if (*buf
&& (*e
== 0 || *e
== '\n')) {
2440 atomic_set(&rdev
->corrected_errors
, n
);
2445 static struct rdev_sysfs_entry rdev_errors
=
2446 __ATTR(errors
, S_IRUGO
|S_IWUSR
, errors_show
, errors_store
);
2449 slot_show(mdk_rdev_t
*rdev
, char *page
)
2451 if (rdev
->raid_disk
< 0)
2452 return sprintf(page
, "none\n");
2454 return sprintf(page
, "%d\n", rdev
->raid_disk
);
2458 slot_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2463 int slot
= simple_strtoul(buf
, &e
, 10);
2464 if (strncmp(buf
, "none", 4)==0)
2466 else if (e
==buf
|| (*e
&& *e
!= '\n'))
2468 if (rdev
->mddev
->pers
&& slot
== -1) {
2469 /* Setting 'slot' on an active array requires also
2470 * updating the 'rd%d' link, and communicating
2471 * with the personality with ->hot_*_disk.
2472 * For now we only support removing
2473 * failed/spare devices. This normally happens automatically,
2474 * but not when the metadata is externally managed.
2476 if (rdev
->raid_disk
== -1)
2478 /* personality does all needed checks */
2479 if (rdev
->mddev
->pers
->hot_remove_disk
== NULL
)
2481 err
= rdev
->mddev
->pers
->
2482 hot_remove_disk(rdev
->mddev
, rdev
->raid_disk
);
2485 sprintf(nm
, "rd%d", rdev
->raid_disk
);
2486 sysfs_remove_link(&rdev
->mddev
->kobj
, nm
);
2487 rdev
->raid_disk
= -1;
2488 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2489 md_wakeup_thread(rdev
->mddev
->thread
);
2490 } else if (rdev
->mddev
->pers
) {
2492 /* Activating a spare .. or possibly reactivating
2493 * if we ever get bitmaps working here.
2496 if (rdev
->raid_disk
!= -1)
2499 if (test_bit(MD_RECOVERY_RUNNING
, &rdev
->mddev
->recovery
))
2502 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
2505 list_for_each_entry(rdev2
, &rdev
->mddev
->disks
, same_set
)
2506 if (rdev2
->raid_disk
== slot
)
2509 if (slot
>= rdev
->mddev
->raid_disks
&&
2510 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
2513 rdev
->raid_disk
= slot
;
2514 if (test_bit(In_sync
, &rdev
->flags
))
2515 rdev
->saved_raid_disk
= slot
;
2517 rdev
->saved_raid_disk
= -1;
2518 err
= rdev
->mddev
->pers
->
2519 hot_add_disk(rdev
->mddev
, rdev
);
2521 rdev
->raid_disk
= -1;
2524 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2525 sprintf(nm
, "rd%d", rdev
->raid_disk
);
2526 if (sysfs_create_link(&rdev
->mddev
->kobj
, &rdev
->kobj
, nm
))
2527 /* failure here is OK */;
2528 /* don't wakeup anyone, leave that to userspace. */
2530 if (slot
>= rdev
->mddev
->raid_disks
&&
2531 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
2533 rdev
->raid_disk
= slot
;
2534 /* assume it is working */
2535 clear_bit(Faulty
, &rdev
->flags
);
2536 clear_bit(WriteMostly
, &rdev
->flags
);
2537 set_bit(In_sync
, &rdev
->flags
);
2538 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2544 static struct rdev_sysfs_entry rdev_slot
=
2545 __ATTR(slot
, S_IRUGO
|S_IWUSR
, slot_show
, slot_store
);
2548 offset_show(mdk_rdev_t
*rdev
, char *page
)
2550 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->data_offset
);
2554 offset_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2557 unsigned long long offset
= simple_strtoull(buf
, &e
, 10);
2558 if (e
==buf
|| (*e
&& *e
!= '\n'))
2560 if (rdev
->mddev
->pers
&& rdev
->raid_disk
>= 0)
2562 if (rdev
->sectors
&& rdev
->mddev
->external
)
2563 /* Must set offset before size, so overlap checks
2566 rdev
->data_offset
= offset
;
2570 static struct rdev_sysfs_entry rdev_offset
=
2571 __ATTR(offset
, S_IRUGO
|S_IWUSR
, offset_show
, offset_store
);
2574 rdev_size_show(mdk_rdev_t
*rdev
, char *page
)
2576 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->sectors
/ 2);
2579 static int overlaps(sector_t s1
, sector_t l1
, sector_t s2
, sector_t l2
)
2581 /* check if two start/length pairs overlap */
2589 static int strict_blocks_to_sectors(const char *buf
, sector_t
*sectors
)
2591 unsigned long long blocks
;
2594 if (strict_strtoull(buf
, 10, &blocks
) < 0)
2597 if (blocks
& 1ULL << (8 * sizeof(blocks
) - 1))
2598 return -EINVAL
; /* sector conversion overflow */
2601 if (new != blocks
* 2)
2602 return -EINVAL
; /* unsigned long long to sector_t overflow */
2609 rdev_size_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2611 mddev_t
*my_mddev
= rdev
->mddev
;
2612 sector_t oldsectors
= rdev
->sectors
;
2615 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
2617 if (my_mddev
->pers
&& rdev
->raid_disk
>= 0) {
2618 if (my_mddev
->persistent
) {
2619 sectors
= super_types
[my_mddev
->major_version
].
2620 rdev_size_change(rdev
, sectors
);
2623 } else if (!sectors
)
2624 sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) -
2627 if (sectors
< my_mddev
->dev_sectors
)
2628 return -EINVAL
; /* component must fit device */
2630 rdev
->sectors
= sectors
;
2631 if (sectors
> oldsectors
&& my_mddev
->external
) {
2632 /* need to check that all other rdevs with the same ->bdev
2633 * do not overlap. We need to unlock the mddev to avoid
2634 * a deadlock. We have already changed rdev->sectors, and if
2635 * we have to change it back, we will have the lock again.
2639 struct list_head
*tmp
;
2641 mddev_unlock(my_mddev
);
2642 for_each_mddev(mddev
, tmp
) {
2646 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
)
2647 if (rdev
->bdev
== rdev2
->bdev
&&
2649 overlaps(rdev
->data_offset
, rdev
->sectors
,
2655 mddev_unlock(mddev
);
2661 mddev_lock(my_mddev
);
2663 /* Someone else could have slipped in a size
2664 * change here, but doing so is just silly.
2665 * We put oldsectors back because we *know* it is
2666 * safe, and trust userspace not to race with
2669 rdev
->sectors
= oldsectors
;
2676 static struct rdev_sysfs_entry rdev_size
=
2677 __ATTR(size
, S_IRUGO
|S_IWUSR
, rdev_size_show
, rdev_size_store
);
2680 static ssize_t
recovery_start_show(mdk_rdev_t
*rdev
, char *page
)
2682 unsigned long long recovery_start
= rdev
->recovery_offset
;
2684 if (test_bit(In_sync
, &rdev
->flags
) ||
2685 recovery_start
== MaxSector
)
2686 return sprintf(page
, "none\n");
2688 return sprintf(page
, "%llu\n", recovery_start
);
2691 static ssize_t
recovery_start_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2693 unsigned long long recovery_start
;
2695 if (cmd_match(buf
, "none"))
2696 recovery_start
= MaxSector
;
2697 else if (strict_strtoull(buf
, 10, &recovery_start
))
2700 if (rdev
->mddev
->pers
&&
2701 rdev
->raid_disk
>= 0)
2704 rdev
->recovery_offset
= recovery_start
;
2705 if (recovery_start
== MaxSector
)
2706 set_bit(In_sync
, &rdev
->flags
);
2708 clear_bit(In_sync
, &rdev
->flags
);
2712 static struct rdev_sysfs_entry rdev_recovery_start
=
2713 __ATTR(recovery_start
, S_IRUGO
|S_IWUSR
, recovery_start_show
, recovery_start_store
);
2715 static struct attribute
*rdev_default_attrs
[] = {
2721 &rdev_recovery_start
.attr
,
2725 rdev_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
2727 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
2728 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
2729 mddev_t
*mddev
= rdev
->mddev
;
2735 rv
= mddev
? mddev_lock(mddev
) : -EBUSY
;
2737 if (rdev
->mddev
== NULL
)
2740 rv
= entry
->show(rdev
, page
);
2741 mddev_unlock(mddev
);
2747 rdev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
2748 const char *page
, size_t length
)
2750 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
2751 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
2753 mddev_t
*mddev
= rdev
->mddev
;
2757 if (!capable(CAP_SYS_ADMIN
))
2759 rv
= mddev
? mddev_lock(mddev
): -EBUSY
;
2761 if (rdev
->mddev
== NULL
)
2764 rv
= entry
->store(rdev
, page
, length
);
2765 mddev_unlock(mddev
);
2770 static void rdev_free(struct kobject
*ko
)
2772 mdk_rdev_t
*rdev
= container_of(ko
, mdk_rdev_t
, kobj
);
2775 static const struct sysfs_ops rdev_sysfs_ops
= {
2776 .show
= rdev_attr_show
,
2777 .store
= rdev_attr_store
,
2779 static struct kobj_type rdev_ktype
= {
2780 .release
= rdev_free
,
2781 .sysfs_ops
= &rdev_sysfs_ops
,
2782 .default_attrs
= rdev_default_attrs
,
2785 void md_rdev_init(mdk_rdev_t
*rdev
)
2788 rdev
->saved_raid_disk
= -1;
2789 rdev
->raid_disk
= -1;
2791 rdev
->data_offset
= 0;
2792 rdev
->sb_events
= 0;
2793 rdev
->last_read_error
.tv_sec
= 0;
2794 rdev
->last_read_error
.tv_nsec
= 0;
2795 atomic_set(&rdev
->nr_pending
, 0);
2796 atomic_set(&rdev
->read_errors
, 0);
2797 atomic_set(&rdev
->corrected_errors
, 0);
2799 INIT_LIST_HEAD(&rdev
->same_set
);
2800 init_waitqueue_head(&rdev
->blocked_wait
);
2802 EXPORT_SYMBOL_GPL(md_rdev_init
);
2804 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2806 * mark the device faulty if:
2808 * - the device is nonexistent (zero size)
2809 * - the device has no valid superblock
2811 * a faulty rdev _never_ has rdev->sb set.
2813 static mdk_rdev_t
*md_import_device(dev_t newdev
, int super_format
, int super_minor
)
2815 char b
[BDEVNAME_SIZE
];
2820 rdev
= kzalloc(sizeof(*rdev
), GFP_KERNEL
);
2822 printk(KERN_ERR
"md: could not alloc mem for new device!\n");
2823 return ERR_PTR(-ENOMEM
);
2827 if ((err
= alloc_disk_sb(rdev
)))
2830 err
= lock_rdev(rdev
, newdev
, super_format
== -2);
2834 kobject_init(&rdev
->kobj
, &rdev_ktype
);
2836 size
= i_size_read(rdev
->bdev
->bd_inode
) >> BLOCK_SIZE_BITS
;
2839 "md: %s has zero or unknown size, marking faulty!\n",
2840 bdevname(rdev
->bdev
,b
));
2845 if (super_format
>= 0) {
2846 err
= super_types
[super_format
].
2847 load_super(rdev
, NULL
, super_minor
);
2848 if (err
== -EINVAL
) {
2850 "md: %s does not have a valid v%d.%d "
2851 "superblock, not importing!\n",
2852 bdevname(rdev
->bdev
,b
),
2853 super_format
, super_minor
);
2858 "md: could not read %s's sb, not importing!\n",
2859 bdevname(rdev
->bdev
,b
));
2867 if (rdev
->sb_page
) {
2873 return ERR_PTR(err
);
2877 * Check a full RAID array for plausibility
2881 static void analyze_sbs(mddev_t
* mddev
)
2884 mdk_rdev_t
*rdev
, *freshest
, *tmp
;
2885 char b
[BDEVNAME_SIZE
];
2888 rdev_for_each(rdev
, tmp
, mddev
)
2889 switch (super_types
[mddev
->major_version
].
2890 load_super(rdev
, freshest
, mddev
->minor_version
)) {
2898 "md: fatal superblock inconsistency in %s"
2899 " -- removing from array\n",
2900 bdevname(rdev
->bdev
,b
));
2901 kick_rdev_from_array(rdev
);
2905 super_types
[mddev
->major_version
].
2906 validate_super(mddev
, freshest
);
2909 rdev_for_each(rdev
, tmp
, mddev
) {
2910 if (mddev
->max_disks
&&
2911 (rdev
->desc_nr
>= mddev
->max_disks
||
2912 i
> mddev
->max_disks
)) {
2914 "md: %s: %s: only %d devices permitted\n",
2915 mdname(mddev
), bdevname(rdev
->bdev
, b
),
2917 kick_rdev_from_array(rdev
);
2920 if (rdev
!= freshest
)
2921 if (super_types
[mddev
->major_version
].
2922 validate_super(mddev
, rdev
)) {
2923 printk(KERN_WARNING
"md: kicking non-fresh %s"
2925 bdevname(rdev
->bdev
,b
));
2926 kick_rdev_from_array(rdev
);
2929 if (mddev
->level
== LEVEL_MULTIPATH
) {
2930 rdev
->desc_nr
= i
++;
2931 rdev
->raid_disk
= rdev
->desc_nr
;
2932 set_bit(In_sync
, &rdev
->flags
);
2933 } else if (rdev
->raid_disk
>= (mddev
->raid_disks
- min(0, mddev
->delta_disks
))) {
2934 rdev
->raid_disk
= -1;
2935 clear_bit(In_sync
, &rdev
->flags
);
2940 /* Read a fixed-point number.
2941 * Numbers in sysfs attributes should be in "standard" units where
2942 * possible, so time should be in seconds.
2943 * However we internally use a a much smaller unit such as
2944 * milliseconds or jiffies.
2945 * This function takes a decimal number with a possible fractional
2946 * component, and produces an integer which is the result of
2947 * multiplying that number by 10^'scale'.
2948 * all without any floating-point arithmetic.
2950 int strict_strtoul_scaled(const char *cp
, unsigned long *res
, int scale
)
2952 unsigned long result
= 0;
2954 while (isdigit(*cp
) || (*cp
== '.' && decimals
< 0)) {
2957 else if (decimals
< scale
) {
2960 result
= result
* 10 + value
;
2972 while (decimals
< scale
) {
2981 static void md_safemode_timeout(unsigned long data
);
2984 safe_delay_show(mddev_t
*mddev
, char *page
)
2986 int msec
= (mddev
->safemode_delay
*1000)/HZ
;
2987 return sprintf(page
, "%d.%03d\n", msec
/1000, msec
%1000);
2990 safe_delay_store(mddev_t
*mddev
, const char *cbuf
, size_t len
)
2994 if (strict_strtoul_scaled(cbuf
, &msec
, 3) < 0)
2997 mddev
->safemode_delay
= 0;
2999 unsigned long old_delay
= mddev
->safemode_delay
;
3000 mddev
->safemode_delay
= (msec
*HZ
)/1000;
3001 if (mddev
->safemode_delay
== 0)
3002 mddev
->safemode_delay
= 1;
3003 if (mddev
->safemode_delay
< old_delay
)
3004 md_safemode_timeout((unsigned long)mddev
);
3008 static struct md_sysfs_entry md_safe_delay
=
3009 __ATTR(safe_mode_delay
, S_IRUGO
|S_IWUSR
,safe_delay_show
, safe_delay_store
);
3012 level_show(mddev_t
*mddev
, char *page
)
3014 struct mdk_personality
*p
= mddev
->pers
;
3016 return sprintf(page
, "%s\n", p
->name
);
3017 else if (mddev
->clevel
[0])
3018 return sprintf(page
, "%s\n", mddev
->clevel
);
3019 else if (mddev
->level
!= LEVEL_NONE
)
3020 return sprintf(page
, "%d\n", mddev
->level
);
3026 level_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3030 struct mdk_personality
*pers
;
3035 if (mddev
->pers
== NULL
) {
3038 if (len
>= sizeof(mddev
->clevel
))
3040 strncpy(mddev
->clevel
, buf
, len
);
3041 if (mddev
->clevel
[len
-1] == '\n')
3043 mddev
->clevel
[len
] = 0;
3044 mddev
->level
= LEVEL_NONE
;
3048 /* request to change the personality. Need to ensure:
3049 * - array is not engaged in resync/recovery/reshape
3050 * - old personality can be suspended
3051 * - new personality will access other array.
3054 if (mddev
->sync_thread
||
3055 mddev
->reshape_position
!= MaxSector
||
3056 mddev
->sysfs_active
)
3059 if (!mddev
->pers
->quiesce
) {
3060 printk(KERN_WARNING
"md: %s: %s does not support online personality change\n",
3061 mdname(mddev
), mddev
->pers
->name
);
3065 /* Now find the new personality */
3066 if (len
== 0 || len
>= sizeof(clevel
))
3068 strncpy(clevel
, buf
, len
);
3069 if (clevel
[len
-1] == '\n')
3072 if (strict_strtol(clevel
, 10, &level
))
3075 if (request_module("md-%s", clevel
) != 0)
3076 request_module("md-level-%s", clevel
);
3077 spin_lock(&pers_lock
);
3078 pers
= find_pers(level
, clevel
);
3079 if (!pers
|| !try_module_get(pers
->owner
)) {
3080 spin_unlock(&pers_lock
);
3081 printk(KERN_WARNING
"md: personality %s not loaded\n", clevel
);
3084 spin_unlock(&pers_lock
);
3086 if (pers
== mddev
->pers
) {
3087 /* Nothing to do! */
3088 module_put(pers
->owner
);
3091 if (!pers
->takeover
) {
3092 module_put(pers
->owner
);
3093 printk(KERN_WARNING
"md: %s: %s does not support personality takeover\n",
3094 mdname(mddev
), clevel
);
3098 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
3099 rdev
->new_raid_disk
= rdev
->raid_disk
;
3101 /* ->takeover must set new_* and/or delta_disks
3102 * if it succeeds, and may set them when it fails.
3104 priv
= pers
->takeover(mddev
);
3106 mddev
->new_level
= mddev
->level
;
3107 mddev
->new_layout
= mddev
->layout
;
3108 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3109 mddev
->raid_disks
-= mddev
->delta_disks
;
3110 mddev
->delta_disks
= 0;
3111 module_put(pers
->owner
);
3112 printk(KERN_WARNING
"md: %s: %s would not accept array\n",
3113 mdname(mddev
), clevel
);
3114 return PTR_ERR(priv
);
3117 /* Looks like we have a winner */
3118 mddev_suspend(mddev
);
3119 mddev
->pers
->stop(mddev
);
3121 if (mddev
->pers
->sync_request
== NULL
&&
3122 pers
->sync_request
!= NULL
) {
3123 /* need to add the md_redundancy_group */
3124 if (sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
3126 "md: cannot register extra attributes for %s\n",
3128 mddev
->sysfs_action
= sysfs_get_dirent(mddev
->kobj
.sd
, NULL
, "sync_action");
3130 if (mddev
->pers
->sync_request
!= NULL
&&
3131 pers
->sync_request
== NULL
) {
3132 /* need to remove the md_redundancy_group */
3133 if (mddev
->to_remove
== NULL
)
3134 mddev
->to_remove
= &md_redundancy_group
;
3137 if (mddev
->pers
->sync_request
== NULL
&&
3139 /* We are converting from a no-redundancy array
3140 * to a redundancy array and metadata is managed
3141 * externally so we need to be sure that writes
3142 * won't block due to a need to transition
3144 * until external management is started.
3147 mddev
->safemode_delay
= 0;
3148 mddev
->safemode
= 0;
3151 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
3153 if (rdev
->raid_disk
< 0)
3155 if (rdev
->new_raid_disk
>= mddev
->raid_disks
)
3156 rdev
->new_raid_disk
= -1;
3157 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3159 sprintf(nm
, "rd%d", rdev
->raid_disk
);
3160 sysfs_remove_link(&mddev
->kobj
, nm
);
3162 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
3163 if (rdev
->raid_disk
< 0)
3165 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3167 rdev
->raid_disk
= rdev
->new_raid_disk
;
3168 if (rdev
->raid_disk
< 0)
3169 clear_bit(In_sync
, &rdev
->flags
);
3172 sprintf(nm
, "rd%d", rdev
->raid_disk
);
3173 if(sysfs_create_link(&mddev
->kobj
, &rdev
->kobj
, nm
))
3174 printk("md: cannot register %s for %s after level change\n",
3179 module_put(mddev
->pers
->owner
);
3181 mddev
->private = priv
;
3182 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
3183 mddev
->level
= mddev
->new_level
;
3184 mddev
->layout
= mddev
->new_layout
;
3185 mddev
->chunk_sectors
= mddev
->new_chunk_sectors
;
3186 mddev
->delta_disks
= 0;
3187 mddev
->degraded
= 0;
3188 if (mddev
->pers
->sync_request
== NULL
) {
3189 /* this is now an array without redundancy, so
3190 * it must always be in_sync
3193 del_timer_sync(&mddev
->safemode_timer
);
3196 mddev_resume(mddev
);
3197 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
3198 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3199 md_wakeup_thread(mddev
->thread
);
3200 sysfs_notify(&mddev
->kobj
, NULL
, "level");
3201 md_new_event(mddev
);
3205 static struct md_sysfs_entry md_level
=
3206 __ATTR(level
, S_IRUGO
|S_IWUSR
, level_show
, level_store
);
3210 layout_show(mddev_t
*mddev
, char *page
)
3212 /* just a number, not meaningful for all levels */
3213 if (mddev
->reshape_position
!= MaxSector
&&
3214 mddev
->layout
!= mddev
->new_layout
)
3215 return sprintf(page
, "%d (%d)\n",
3216 mddev
->new_layout
, mddev
->layout
);
3217 return sprintf(page
, "%d\n", mddev
->layout
);
3221 layout_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3224 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3226 if (!*buf
|| (*e
&& *e
!= '\n'))
3231 if (mddev
->pers
->check_reshape
== NULL
)
3233 mddev
->new_layout
= n
;
3234 err
= mddev
->pers
->check_reshape(mddev
);
3236 mddev
->new_layout
= mddev
->layout
;
3240 mddev
->new_layout
= n
;
3241 if (mddev
->reshape_position
== MaxSector
)
3246 static struct md_sysfs_entry md_layout
=
3247 __ATTR(layout
, S_IRUGO
|S_IWUSR
, layout_show
, layout_store
);
3251 raid_disks_show(mddev_t
*mddev
, char *page
)
3253 if (mddev
->raid_disks
== 0)
3255 if (mddev
->reshape_position
!= MaxSector
&&
3256 mddev
->delta_disks
!= 0)
3257 return sprintf(page
, "%d (%d)\n", mddev
->raid_disks
,
3258 mddev
->raid_disks
- mddev
->delta_disks
);
3259 return sprintf(page
, "%d\n", mddev
->raid_disks
);
3262 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
);
3265 raid_disks_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3269 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3271 if (!*buf
|| (*e
&& *e
!= '\n'))
3275 rv
= update_raid_disks(mddev
, n
);
3276 else if (mddev
->reshape_position
!= MaxSector
) {
3277 int olddisks
= mddev
->raid_disks
- mddev
->delta_disks
;
3278 mddev
->delta_disks
= n
- olddisks
;
3279 mddev
->raid_disks
= n
;
3281 mddev
->raid_disks
= n
;
3282 return rv
? rv
: len
;
3284 static struct md_sysfs_entry md_raid_disks
=
3285 __ATTR(raid_disks
, S_IRUGO
|S_IWUSR
, raid_disks_show
, raid_disks_store
);
3288 chunk_size_show(mddev_t
*mddev
, char *page
)
3290 if (mddev
->reshape_position
!= MaxSector
&&
3291 mddev
->chunk_sectors
!= mddev
->new_chunk_sectors
)
3292 return sprintf(page
, "%d (%d)\n",
3293 mddev
->new_chunk_sectors
<< 9,
3294 mddev
->chunk_sectors
<< 9);
3295 return sprintf(page
, "%d\n", mddev
->chunk_sectors
<< 9);
3299 chunk_size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3302 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3304 if (!*buf
|| (*e
&& *e
!= '\n'))
3309 if (mddev
->pers
->check_reshape
== NULL
)
3311 mddev
->new_chunk_sectors
= n
>> 9;
3312 err
= mddev
->pers
->check_reshape(mddev
);
3314 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3318 mddev
->new_chunk_sectors
= n
>> 9;
3319 if (mddev
->reshape_position
== MaxSector
)
3320 mddev
->chunk_sectors
= n
>> 9;
3324 static struct md_sysfs_entry md_chunk_size
=
3325 __ATTR(chunk_size
, S_IRUGO
|S_IWUSR
, chunk_size_show
, chunk_size_store
);
3328 resync_start_show(mddev_t
*mddev
, char *page
)
3330 if (mddev
->recovery_cp
== MaxSector
)
3331 return sprintf(page
, "none\n");
3332 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->recovery_cp
);
3336 resync_start_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3339 unsigned long long n
= simple_strtoull(buf
, &e
, 10);
3341 if (mddev
->pers
&& !test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
3343 if (cmd_match(buf
, "none"))
3345 else if (!*buf
|| (*e
&& *e
!= '\n'))
3348 mddev
->recovery_cp
= n
;
3351 static struct md_sysfs_entry md_resync_start
=
3352 __ATTR(resync_start
, S_IRUGO
|S_IWUSR
, resync_start_show
, resync_start_store
);
3355 * The array state can be:
3358 * No devices, no size, no level
3359 * Equivalent to STOP_ARRAY ioctl
3361 * May have some settings, but array is not active
3362 * all IO results in error
3363 * When written, doesn't tear down array, but just stops it
3364 * suspended (not supported yet)
3365 * All IO requests will block. The array can be reconfigured.
3366 * Writing this, if accepted, will block until array is quiescent
3368 * no resync can happen. no superblocks get written.
3369 * write requests fail
3371 * like readonly, but behaves like 'clean' on a write request.
3373 * clean - no pending writes, but otherwise active.
3374 * When written to inactive array, starts without resync
3375 * If a write request arrives then
3376 * if metadata is known, mark 'dirty' and switch to 'active'.
3377 * if not known, block and switch to write-pending
3378 * If written to an active array that has pending writes, then fails.
3380 * fully active: IO and resync can be happening.
3381 * When written to inactive array, starts with resync
3384 * clean, but writes are blocked waiting for 'active' to be written.
3387 * like active, but no writes have been seen for a while (100msec).
3390 enum array_state
{ clear
, inactive
, suspended
, readonly
, read_auto
, clean
, active
,
3391 write_pending
, active_idle
, bad_word
};
3392 static char *array_states
[] = {
3393 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3394 "write-pending", "active-idle", NULL
};
3396 static int match_word(const char *word
, char **list
)
3399 for (n
=0; list
[n
]; n
++)
3400 if (cmd_match(word
, list
[n
]))
3406 array_state_show(mddev_t
*mddev
, char *page
)
3408 enum array_state st
= inactive
;
3421 else if (test_bit(MD_CHANGE_PENDING
, &mddev
->flags
))
3423 else if (mddev
->safemode
)
3429 if (list_empty(&mddev
->disks
) &&
3430 mddev
->raid_disks
== 0 &&
3431 mddev
->dev_sectors
== 0)
3436 return sprintf(page
, "%s\n", array_states
[st
]);
3439 static int do_md_stop(mddev_t
* mddev
, int ro
, int is_open
);
3440 static int md_set_readonly(mddev_t
* mddev
, int is_open
);
3441 static int do_md_run(mddev_t
* mddev
);
3442 static int restart_array(mddev_t
*mddev
);
3445 array_state_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3448 enum array_state st
= match_word(buf
, array_states
);
3453 /* stopping an active array */
3454 if (atomic_read(&mddev
->openers
) > 0)
3456 err
= do_md_stop(mddev
, 0, 0);
3459 /* stopping an active array */
3461 if (atomic_read(&mddev
->openers
) > 0)
3463 err
= do_md_stop(mddev
, 2, 0);
3465 err
= 0; /* already inactive */
3468 break; /* not supported yet */
3471 err
= md_set_readonly(mddev
, 0);
3474 set_disk_ro(mddev
->gendisk
, 1);
3475 err
= do_md_run(mddev
);
3481 err
= md_set_readonly(mddev
, 0);
3482 else if (mddev
->ro
== 1)
3483 err
= restart_array(mddev
);
3486 set_disk_ro(mddev
->gendisk
, 0);
3490 err
= do_md_run(mddev
);
3495 restart_array(mddev
);
3496 spin_lock_irq(&mddev
->write_lock
);
3497 if (atomic_read(&mddev
->writes_pending
) == 0) {
3498 if (mddev
->in_sync
== 0) {
3500 if (mddev
->safemode
== 1)
3501 mddev
->safemode
= 0;
3502 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
3507 spin_unlock_irq(&mddev
->write_lock
);
3513 restart_array(mddev
);
3514 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
3515 wake_up(&mddev
->sb_wait
);
3519 set_disk_ro(mddev
->gendisk
, 0);
3520 err
= do_md_run(mddev
);
3525 /* these cannot be set */
3531 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
3535 static struct md_sysfs_entry md_array_state
=
3536 __ATTR(array_state
, S_IRUGO
|S_IWUSR
, array_state_show
, array_state_store
);
3539 max_corrected_read_errors_show(mddev_t
*mddev
, char *page
) {
3540 return sprintf(page
, "%d\n",
3541 atomic_read(&mddev
->max_corr_read_errors
));
3545 max_corrected_read_errors_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3548 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3550 if (*buf
&& (*e
== 0 || *e
== '\n')) {
3551 atomic_set(&mddev
->max_corr_read_errors
, n
);
3557 static struct md_sysfs_entry max_corr_read_errors
=
3558 __ATTR(max_read_errors
, S_IRUGO
|S_IWUSR
, max_corrected_read_errors_show
,
3559 max_corrected_read_errors_store
);
3562 null_show(mddev_t
*mddev
, char *page
)
3568 new_dev_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3570 /* buf must be %d:%d\n? giving major and minor numbers */
3571 /* The new device is added to the array.
3572 * If the array has a persistent superblock, we read the
3573 * superblock to initialise info and check validity.
3574 * Otherwise, only checking done is that in bind_rdev_to_array,
3575 * which mainly checks size.
3578 int major
= simple_strtoul(buf
, &e
, 10);
3584 if (!*buf
|| *e
!= ':' || !e
[1] || e
[1] == '\n')
3586 minor
= simple_strtoul(e
+1, &e
, 10);
3587 if (*e
&& *e
!= '\n')
3589 dev
= MKDEV(major
, minor
);
3590 if (major
!= MAJOR(dev
) ||
3591 minor
!= MINOR(dev
))
3595 if (mddev
->persistent
) {
3596 rdev
= md_import_device(dev
, mddev
->major_version
,
3597 mddev
->minor_version
);
3598 if (!IS_ERR(rdev
) && !list_empty(&mddev
->disks
)) {
3599 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
3600 mdk_rdev_t
, same_set
);
3601 err
= super_types
[mddev
->major_version
]
3602 .load_super(rdev
, rdev0
, mddev
->minor_version
);
3606 } else if (mddev
->external
)
3607 rdev
= md_import_device(dev
, -2, -1);
3609 rdev
= md_import_device(dev
, -1, -1);
3612 return PTR_ERR(rdev
);
3613 err
= bind_rdev_to_array(rdev
, mddev
);
3617 return err
? err
: len
;
3620 static struct md_sysfs_entry md_new_device
=
3621 __ATTR(new_dev
, S_IWUSR
, null_show
, new_dev_store
);
3624 bitmap_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3627 unsigned long chunk
, end_chunk
;
3631 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3633 chunk
= end_chunk
= simple_strtoul(buf
, &end
, 0);
3634 if (buf
== end
) break;
3635 if (*end
== '-') { /* range */
3637 end_chunk
= simple_strtoul(buf
, &end
, 0);
3638 if (buf
== end
) break;
3640 if (*end
&& !isspace(*end
)) break;
3641 bitmap_dirty_bits(mddev
->bitmap
, chunk
, end_chunk
);
3642 buf
= skip_spaces(end
);
3644 bitmap_unplug(mddev
->bitmap
); /* flush the bits to disk */
3649 static struct md_sysfs_entry md_bitmap
=
3650 __ATTR(bitmap_set_bits
, S_IWUSR
, null_show
, bitmap_store
);
3653 size_show(mddev_t
*mddev
, char *page
)
3655 return sprintf(page
, "%llu\n",
3656 (unsigned long long)mddev
->dev_sectors
/ 2);
3659 static int update_size(mddev_t
*mddev
, sector_t num_sectors
);
3662 size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3664 /* If array is inactive, we can reduce the component size, but
3665 * not increase it (except from 0).
3666 * If array is active, we can try an on-line resize
3669 int err
= strict_blocks_to_sectors(buf
, §ors
);
3674 err
= update_size(mddev
, sectors
);
3675 md_update_sb(mddev
, 1);
3677 if (mddev
->dev_sectors
== 0 ||
3678 mddev
->dev_sectors
> sectors
)
3679 mddev
->dev_sectors
= sectors
;
3683 return err
? err
: len
;
3686 static struct md_sysfs_entry md_size
=
3687 __ATTR(component_size
, S_IRUGO
|S_IWUSR
, size_show
, size_store
);
3692 * 'none' for arrays with no metadata (good luck...)
3693 * 'external' for arrays with externally managed metadata,
3694 * or N.M for internally known formats
3697 metadata_show(mddev_t
*mddev
, char *page
)
3699 if (mddev
->persistent
)
3700 return sprintf(page
, "%d.%d\n",
3701 mddev
->major_version
, mddev
->minor_version
);
3702 else if (mddev
->external
)
3703 return sprintf(page
, "external:%s\n", mddev
->metadata_type
);
3705 return sprintf(page
, "none\n");
3709 metadata_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3713 /* Changing the details of 'external' metadata is
3714 * always permitted. Otherwise there must be
3715 * no devices attached to the array.
3717 if (mddev
->external
&& strncmp(buf
, "external:", 9) == 0)
3719 else if (!list_empty(&mddev
->disks
))
3722 if (cmd_match(buf
, "none")) {
3723 mddev
->persistent
= 0;
3724 mddev
->external
= 0;
3725 mddev
->major_version
= 0;
3726 mddev
->minor_version
= 90;
3729 if (strncmp(buf
, "external:", 9) == 0) {
3730 size_t namelen
= len
-9;
3731 if (namelen
>= sizeof(mddev
->metadata_type
))
3732 namelen
= sizeof(mddev
->metadata_type
)-1;
3733 strncpy(mddev
->metadata_type
, buf
+9, namelen
);
3734 mddev
->metadata_type
[namelen
] = 0;
3735 if (namelen
&& mddev
->metadata_type
[namelen
-1] == '\n')
3736 mddev
->metadata_type
[--namelen
] = 0;
3737 mddev
->persistent
= 0;
3738 mddev
->external
= 1;
3739 mddev
->major_version
= 0;
3740 mddev
->minor_version
= 90;
3743 major
= simple_strtoul(buf
, &e
, 10);
3744 if (e
==buf
|| *e
!= '.')
3747 minor
= simple_strtoul(buf
, &e
, 10);
3748 if (e
==buf
|| (*e
&& *e
!= '\n') )
3750 if (major
>= ARRAY_SIZE(super_types
) || super_types
[major
].name
== NULL
)
3752 mddev
->major_version
= major
;
3753 mddev
->minor_version
= minor
;
3754 mddev
->persistent
= 1;
3755 mddev
->external
= 0;
3759 static struct md_sysfs_entry md_metadata
=
3760 __ATTR(metadata_version
, S_IRUGO
|S_IWUSR
, metadata_show
, metadata_store
);
3763 action_show(mddev_t
*mddev
, char *page
)
3765 char *type
= "idle";
3766 if (test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
3768 else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
3769 (!mddev
->ro
&& test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))) {
3770 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
3772 else if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
3773 if (!test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
3775 else if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
3779 } else if (test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
))
3782 return sprintf(page
, "%s\n", type
);
3785 static void reap_sync_thread(mddev_t
*mddev
);
3788 action_store(mddev_t
*mddev
, const char *page
, size_t len
)
3790 if (!mddev
->pers
|| !mddev
->pers
->sync_request
)
3793 if (cmd_match(page
, "frozen"))
3794 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
3796 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
3798 if (cmd_match(page
, "idle") || cmd_match(page
, "frozen")) {
3799 if (mddev
->sync_thread
) {
3800 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
3801 reap_sync_thread(mddev
);
3803 } else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
3804 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
3806 else if (cmd_match(page
, "resync"))
3807 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3808 else if (cmd_match(page
, "recover")) {
3809 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
3810 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3811 } else if (cmd_match(page
, "reshape")) {
3813 if (mddev
->pers
->start_reshape
== NULL
)
3815 err
= mddev
->pers
->start_reshape(mddev
);
3818 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
3820 if (cmd_match(page
, "check"))
3821 set_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
3822 else if (!cmd_match(page
, "repair"))
3824 set_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
3825 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
3827 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3828 md_wakeup_thread(mddev
->thread
);
3829 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
3834 mismatch_cnt_show(mddev_t
*mddev
, char *page
)
3836 return sprintf(page
, "%llu\n",
3837 (unsigned long long) mddev
->resync_mismatches
);
3840 static struct md_sysfs_entry md_scan_mode
=
3841 __ATTR(sync_action
, S_IRUGO
|S_IWUSR
, action_show
, action_store
);
3844 static struct md_sysfs_entry md_mismatches
= __ATTR_RO(mismatch_cnt
);
3847 sync_min_show(mddev_t
*mddev
, char *page
)
3849 return sprintf(page
, "%d (%s)\n", speed_min(mddev
),
3850 mddev
->sync_speed_min
? "local": "system");
3854 sync_min_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3858 if (strncmp(buf
, "system", 6)==0) {
3859 mddev
->sync_speed_min
= 0;
3862 min
= simple_strtoul(buf
, &e
, 10);
3863 if (buf
== e
|| (*e
&& *e
!= '\n') || min
<= 0)
3865 mddev
->sync_speed_min
= min
;
3869 static struct md_sysfs_entry md_sync_min
=
3870 __ATTR(sync_speed_min
, S_IRUGO
|S_IWUSR
, sync_min_show
, sync_min_store
);
3873 sync_max_show(mddev_t
*mddev
, char *page
)
3875 return sprintf(page
, "%d (%s)\n", speed_max(mddev
),
3876 mddev
->sync_speed_max
? "local": "system");
3880 sync_max_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3884 if (strncmp(buf
, "system", 6)==0) {
3885 mddev
->sync_speed_max
= 0;
3888 max
= simple_strtoul(buf
, &e
, 10);
3889 if (buf
== e
|| (*e
&& *e
!= '\n') || max
<= 0)
3891 mddev
->sync_speed_max
= max
;
3895 static struct md_sysfs_entry md_sync_max
=
3896 __ATTR(sync_speed_max
, S_IRUGO
|S_IWUSR
, sync_max_show
, sync_max_store
);
3899 degraded_show(mddev_t
*mddev
, char *page
)
3901 return sprintf(page
, "%d\n", mddev
->degraded
);
3903 static struct md_sysfs_entry md_degraded
= __ATTR_RO(degraded
);
3906 sync_force_parallel_show(mddev_t
*mddev
, char *page
)
3908 return sprintf(page
, "%d\n", mddev
->parallel_resync
);
3912 sync_force_parallel_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3916 if (strict_strtol(buf
, 10, &n
))
3919 if (n
!= 0 && n
!= 1)
3922 mddev
->parallel_resync
= n
;
3924 if (mddev
->sync_thread
)
3925 wake_up(&resync_wait
);
3930 /* force parallel resync, even with shared block devices */
3931 static struct md_sysfs_entry md_sync_force_parallel
=
3932 __ATTR(sync_force_parallel
, S_IRUGO
|S_IWUSR
,
3933 sync_force_parallel_show
, sync_force_parallel_store
);
3936 sync_speed_show(mddev_t
*mddev
, char *page
)
3938 unsigned long resync
, dt
, db
;
3939 if (mddev
->curr_resync
== 0)
3940 return sprintf(page
, "none\n");
3941 resync
= mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
);
3942 dt
= (jiffies
- mddev
->resync_mark
) / HZ
;
3944 db
= resync
- mddev
->resync_mark_cnt
;
3945 return sprintf(page
, "%lu\n", db
/dt
/2); /* K/sec */
3948 static struct md_sysfs_entry md_sync_speed
= __ATTR_RO(sync_speed
);
3951 sync_completed_show(mddev_t
*mddev
, char *page
)
3953 unsigned long long max_sectors
, resync
;
3955 if (!test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3956 return sprintf(page
, "none\n");
3958 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
3959 max_sectors
= mddev
->resync_max_sectors
;
3961 max_sectors
= mddev
->dev_sectors
;
3963 resync
= mddev
->curr_resync_completed
;
3964 return sprintf(page
, "%llu / %llu\n", resync
, max_sectors
);
3967 static struct md_sysfs_entry md_sync_completed
= __ATTR_RO(sync_completed
);
3970 min_sync_show(mddev_t
*mddev
, char *page
)
3972 return sprintf(page
, "%llu\n",
3973 (unsigned long long)mddev
->resync_min
);
3976 min_sync_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3978 unsigned long long min
;
3979 if (strict_strtoull(buf
, 10, &min
))
3981 if (min
> mddev
->resync_max
)
3983 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3986 /* Must be a multiple of chunk_size */
3987 if (mddev
->chunk_sectors
) {
3988 sector_t temp
= min
;
3989 if (sector_div(temp
, mddev
->chunk_sectors
))
3992 mddev
->resync_min
= min
;
3997 static struct md_sysfs_entry md_min_sync
=
3998 __ATTR(sync_min
, S_IRUGO
|S_IWUSR
, min_sync_show
, min_sync_store
);
4001 max_sync_show(mddev_t
*mddev
, char *page
)
4003 if (mddev
->resync_max
== MaxSector
)
4004 return sprintf(page
, "max\n");
4006 return sprintf(page
, "%llu\n",
4007 (unsigned long long)mddev
->resync_max
);
4010 max_sync_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4012 if (strncmp(buf
, "max", 3) == 0)
4013 mddev
->resync_max
= MaxSector
;
4015 unsigned long long max
;
4016 if (strict_strtoull(buf
, 10, &max
))
4018 if (max
< mddev
->resync_min
)
4020 if (max
< mddev
->resync_max
&&
4022 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4025 /* Must be a multiple of chunk_size */
4026 if (mddev
->chunk_sectors
) {
4027 sector_t temp
= max
;
4028 if (sector_div(temp
, mddev
->chunk_sectors
))
4031 mddev
->resync_max
= max
;
4033 wake_up(&mddev
->recovery_wait
);
4037 static struct md_sysfs_entry md_max_sync
=
4038 __ATTR(sync_max
, S_IRUGO
|S_IWUSR
, max_sync_show
, max_sync_store
);
4041 suspend_lo_show(mddev_t
*mddev
, char *page
)
4043 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_lo
);
4047 suspend_lo_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4050 unsigned long long new = simple_strtoull(buf
, &e
, 10);
4051 unsigned long long old
= mddev
->suspend_lo
;
4053 if (mddev
->pers
== NULL
||
4054 mddev
->pers
->quiesce
== NULL
)
4056 if (buf
== e
|| (*e
&& *e
!= '\n'))
4059 mddev
->suspend_lo
= new;
4061 /* Shrinking suspended region */
4062 mddev
->pers
->quiesce(mddev
, 2);
4064 /* Expanding suspended region - need to wait */
4065 mddev
->pers
->quiesce(mddev
, 1);
4066 mddev
->pers
->quiesce(mddev
, 0);
4070 static struct md_sysfs_entry md_suspend_lo
=
4071 __ATTR(suspend_lo
, S_IRUGO
|S_IWUSR
, suspend_lo_show
, suspend_lo_store
);
4075 suspend_hi_show(mddev_t
*mddev
, char *page
)
4077 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_hi
);
4081 suspend_hi_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4084 unsigned long long new = simple_strtoull(buf
, &e
, 10);
4085 unsigned long long old
= mddev
->suspend_hi
;
4087 if (mddev
->pers
== NULL
||
4088 mddev
->pers
->quiesce
== NULL
)
4090 if (buf
== e
|| (*e
&& *e
!= '\n'))
4093 mddev
->suspend_hi
= new;
4095 /* Shrinking suspended region */
4096 mddev
->pers
->quiesce(mddev
, 2);
4098 /* Expanding suspended region - need to wait */
4099 mddev
->pers
->quiesce(mddev
, 1);
4100 mddev
->pers
->quiesce(mddev
, 0);
4104 static struct md_sysfs_entry md_suspend_hi
=
4105 __ATTR(suspend_hi
, S_IRUGO
|S_IWUSR
, suspend_hi_show
, suspend_hi_store
);
4108 reshape_position_show(mddev_t
*mddev
, char *page
)
4110 if (mddev
->reshape_position
!= MaxSector
)
4111 return sprintf(page
, "%llu\n",
4112 (unsigned long long)mddev
->reshape_position
);
4113 strcpy(page
, "none\n");
4118 reshape_position_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4121 unsigned long long new = simple_strtoull(buf
, &e
, 10);
4124 if (buf
== e
|| (*e
&& *e
!= '\n'))
4126 mddev
->reshape_position
= new;
4127 mddev
->delta_disks
= 0;
4128 mddev
->new_level
= mddev
->level
;
4129 mddev
->new_layout
= mddev
->layout
;
4130 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
4134 static struct md_sysfs_entry md_reshape_position
=
4135 __ATTR(reshape_position
, S_IRUGO
|S_IWUSR
, reshape_position_show
,
4136 reshape_position_store
);
4139 array_size_show(mddev_t
*mddev
, char *page
)
4141 if (mddev
->external_size
)
4142 return sprintf(page
, "%llu\n",
4143 (unsigned long long)mddev
->array_sectors
/2);
4145 return sprintf(page
, "default\n");
4149 array_size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4153 if (strncmp(buf
, "default", 7) == 0) {
4155 sectors
= mddev
->pers
->size(mddev
, 0, 0);
4157 sectors
= mddev
->array_sectors
;
4159 mddev
->external_size
= 0;
4161 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
4163 if (mddev
->pers
&& mddev
->pers
->size(mddev
, 0, 0) < sectors
)
4166 mddev
->external_size
= 1;
4169 mddev
->array_sectors
= sectors
;
4171 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
4172 revalidate_disk(mddev
->gendisk
);
4177 static struct md_sysfs_entry md_array_size
=
4178 __ATTR(array_size
, S_IRUGO
|S_IWUSR
, array_size_show
,
4181 static struct attribute
*md_default_attrs
[] = {
4184 &md_raid_disks
.attr
,
4185 &md_chunk_size
.attr
,
4187 &md_resync_start
.attr
,
4189 &md_new_device
.attr
,
4190 &md_safe_delay
.attr
,
4191 &md_array_state
.attr
,
4192 &md_reshape_position
.attr
,
4193 &md_array_size
.attr
,
4194 &max_corr_read_errors
.attr
,
4198 static struct attribute
*md_redundancy_attrs
[] = {
4200 &md_mismatches
.attr
,
4203 &md_sync_speed
.attr
,
4204 &md_sync_force_parallel
.attr
,
4205 &md_sync_completed
.attr
,
4208 &md_suspend_lo
.attr
,
4209 &md_suspend_hi
.attr
,
4214 static struct attribute_group md_redundancy_group
= {
4216 .attrs
= md_redundancy_attrs
,
4221 md_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
4223 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
4224 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
4229 rv
= mddev_lock(mddev
);
4231 rv
= entry
->show(mddev
, page
);
4232 mddev_unlock(mddev
);
4238 md_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
4239 const char *page
, size_t length
)
4241 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
4242 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
4247 if (!capable(CAP_SYS_ADMIN
))
4249 rv
= mddev_lock(mddev
);
4250 if (mddev
->hold_active
== UNTIL_IOCTL
)
4251 mddev
->hold_active
= 0;
4253 rv
= entry
->store(mddev
, page
, length
);
4254 mddev_unlock(mddev
);
4259 static void md_free(struct kobject
*ko
)
4261 mddev_t
*mddev
= container_of(ko
, mddev_t
, kobj
);
4263 if (mddev
->sysfs_state
)
4264 sysfs_put(mddev
->sysfs_state
);
4266 if (mddev
->gendisk
) {
4267 del_gendisk(mddev
->gendisk
);
4268 put_disk(mddev
->gendisk
);
4271 blk_cleanup_queue(mddev
->queue
);
4276 static const struct sysfs_ops md_sysfs_ops
= {
4277 .show
= md_attr_show
,
4278 .store
= md_attr_store
,
4280 static struct kobj_type md_ktype
= {
4282 .sysfs_ops
= &md_sysfs_ops
,
4283 .default_attrs
= md_default_attrs
,
4288 static void mddev_delayed_delete(struct work_struct
*ws
)
4290 mddev_t
*mddev
= container_of(ws
, mddev_t
, del_work
);
4292 sysfs_remove_group(&mddev
->kobj
, &md_bitmap_group
);
4293 kobject_del(&mddev
->kobj
);
4294 kobject_put(&mddev
->kobj
);
4297 static int md_alloc(dev_t dev
, char *name
)
4299 static DEFINE_MUTEX(disks_mutex
);
4300 mddev_t
*mddev
= mddev_find(dev
);
4301 struct gendisk
*disk
;
4310 partitioned
= (MAJOR(mddev
->unit
) != MD_MAJOR
);
4311 shift
= partitioned
? MdpMinorShift
: 0;
4312 unit
= MINOR(mddev
->unit
) >> shift
;
4314 /* wait for any previous instance of this device to be
4315 * completely removed (mddev_delayed_delete).
4317 flush_workqueue(md_misc_wq
);
4319 mutex_lock(&disks_mutex
);
4325 /* Need to ensure that 'name' is not a duplicate.
4328 spin_lock(&all_mddevs_lock
);
4330 list_for_each_entry(mddev2
, &all_mddevs
, all_mddevs
)
4331 if (mddev2
->gendisk
&&
4332 strcmp(mddev2
->gendisk
->disk_name
, name
) == 0) {
4333 spin_unlock(&all_mddevs_lock
);
4336 spin_unlock(&all_mddevs_lock
);
4340 mddev
->queue
= blk_alloc_queue(GFP_KERNEL
);
4343 mddev
->queue
->queuedata
= mddev
;
4345 blk_queue_make_request(mddev
->queue
, md_make_request
);
4347 disk
= alloc_disk(1 << shift
);
4349 blk_cleanup_queue(mddev
->queue
);
4350 mddev
->queue
= NULL
;
4353 disk
->major
= MAJOR(mddev
->unit
);
4354 disk
->first_minor
= unit
<< shift
;
4356 strcpy(disk
->disk_name
, name
);
4357 else if (partitioned
)
4358 sprintf(disk
->disk_name
, "md_d%d", unit
);
4360 sprintf(disk
->disk_name
, "md%d", unit
);
4361 disk
->fops
= &md_fops
;
4362 disk
->private_data
= mddev
;
4363 disk
->queue
= mddev
->queue
;
4364 blk_queue_flush(mddev
->queue
, REQ_FLUSH
| REQ_FUA
);
4365 /* Allow extended partitions. This makes the
4366 * 'mdp' device redundant, but we can't really
4369 disk
->flags
|= GENHD_FL_EXT_DEVT
;
4370 mddev
->gendisk
= disk
;
4371 /* As soon as we call add_disk(), another thread could get
4372 * through to md_open, so make sure it doesn't get too far
4374 mutex_lock(&mddev
->open_mutex
);
4377 error
= kobject_init_and_add(&mddev
->kobj
, &md_ktype
,
4378 &disk_to_dev(disk
)->kobj
, "%s", "md");
4380 /* This isn't possible, but as kobject_init_and_add is marked
4381 * __must_check, we must do something with the result
4383 printk(KERN_WARNING
"md: cannot register %s/md - name in use\n",
4387 if (mddev
->kobj
.sd
&&
4388 sysfs_create_group(&mddev
->kobj
, &md_bitmap_group
))
4389 printk(KERN_DEBUG
"pointless warning\n");
4390 mutex_unlock(&mddev
->open_mutex
);
4392 mutex_unlock(&disks_mutex
);
4393 if (!error
&& mddev
->kobj
.sd
) {
4394 kobject_uevent(&mddev
->kobj
, KOBJ_ADD
);
4395 mddev
->sysfs_state
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "array_state");
4401 static struct kobject
*md_probe(dev_t dev
, int *part
, void *data
)
4403 md_alloc(dev
, NULL
);
4407 static int add_named_array(const char *val
, struct kernel_param
*kp
)
4409 /* val must be "md_*" where * is not all digits.
4410 * We allocate an array with a large free minor number, and
4411 * set the name to val. val must not already be an active name.
4413 int len
= strlen(val
);
4414 char buf
[DISK_NAME_LEN
];
4416 while (len
&& val
[len
-1] == '\n')
4418 if (len
>= DISK_NAME_LEN
)
4420 strlcpy(buf
, val
, len
+1);
4421 if (strncmp(buf
, "md_", 3) != 0)
4423 return md_alloc(0, buf
);
4426 static void md_safemode_timeout(unsigned long data
)
4428 mddev_t
*mddev
= (mddev_t
*) data
;
4430 if (!atomic_read(&mddev
->writes_pending
)) {
4431 mddev
->safemode
= 1;
4432 if (mddev
->external
)
4433 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4435 md_wakeup_thread(mddev
->thread
);
4438 static int start_dirty_degraded
;
4440 int md_run(mddev_t
*mddev
)
4444 struct mdk_personality
*pers
;
4446 if (list_empty(&mddev
->disks
))
4447 /* cannot run an array with no devices.. */
4452 /* Cannot run until previous stop completes properly */
4453 if (mddev
->sysfs_active
)
4457 * Analyze all RAID superblock(s)
4459 if (!mddev
->raid_disks
) {
4460 if (!mddev
->persistent
)
4465 if (mddev
->level
!= LEVEL_NONE
)
4466 request_module("md-level-%d", mddev
->level
);
4467 else if (mddev
->clevel
[0])
4468 request_module("md-%s", mddev
->clevel
);
4471 * Drop all container device buffers, from now on
4472 * the only valid external interface is through the md
4475 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
4476 if (test_bit(Faulty
, &rdev
->flags
))
4478 sync_blockdev(rdev
->bdev
);
4479 invalidate_bdev(rdev
->bdev
);
4481 /* perform some consistency tests on the device.
4482 * We don't want the data to overlap the metadata,
4483 * Internal Bitmap issues have been handled elsewhere.
4485 if (rdev
->meta_bdev
) {
4486 /* Nothing to check */;
4487 } else if (rdev
->data_offset
< rdev
->sb_start
) {
4488 if (mddev
->dev_sectors
&&
4489 rdev
->data_offset
+ mddev
->dev_sectors
4491 printk("md: %s: data overlaps metadata\n",
4496 if (rdev
->sb_start
+ rdev
->sb_size
/512
4497 > rdev
->data_offset
) {
4498 printk("md: %s: metadata overlaps data\n",
4503 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
4506 if (mddev
->bio_set
== NULL
)
4507 mddev
->bio_set
= bioset_create(BIO_POOL_SIZE
, sizeof(mddev
));
4509 spin_lock(&pers_lock
);
4510 pers
= find_pers(mddev
->level
, mddev
->clevel
);
4511 if (!pers
|| !try_module_get(pers
->owner
)) {
4512 spin_unlock(&pers_lock
);
4513 if (mddev
->level
!= LEVEL_NONE
)
4514 printk(KERN_WARNING
"md: personality for level %d is not loaded!\n",
4517 printk(KERN_WARNING
"md: personality for level %s is not loaded!\n",
4522 spin_unlock(&pers_lock
);
4523 if (mddev
->level
!= pers
->level
) {
4524 mddev
->level
= pers
->level
;
4525 mddev
->new_level
= pers
->level
;
4527 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
4529 if (mddev
->reshape_position
!= MaxSector
&&
4530 pers
->start_reshape
== NULL
) {
4531 /* This personality cannot handle reshaping... */
4533 module_put(pers
->owner
);
4537 if (pers
->sync_request
) {
4538 /* Warn if this is a potentially silly
4541 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
4545 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4546 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
4548 rdev
->bdev
->bd_contains
==
4549 rdev2
->bdev
->bd_contains
) {
4551 "%s: WARNING: %s appears to be"
4552 " on the same physical disk as"
4555 bdevname(rdev
->bdev
,b
),
4556 bdevname(rdev2
->bdev
,b2
));
4563 "True protection against single-disk"
4564 " failure might be compromised.\n");
4567 mddev
->recovery
= 0;
4568 /* may be over-ridden by personality */
4569 mddev
->resync_max_sectors
= mddev
->dev_sectors
;
4571 mddev
->ok_start_degraded
= start_dirty_degraded
;
4573 if (start_readonly
&& mddev
->ro
== 0)
4574 mddev
->ro
= 2; /* read-only, but switch on first write */
4576 err
= mddev
->pers
->run(mddev
);
4578 printk(KERN_ERR
"md: pers->run() failed ...\n");
4579 else if (mddev
->pers
->size(mddev
, 0, 0) < mddev
->array_sectors
) {
4580 WARN_ONCE(!mddev
->external_size
, "%s: default size too small,"
4581 " but 'external_size' not in effect?\n", __func__
);
4583 "md: invalid array_size %llu > default size %llu\n",
4584 (unsigned long long)mddev
->array_sectors
/ 2,
4585 (unsigned long long)mddev
->pers
->size(mddev
, 0, 0) / 2);
4587 mddev
->pers
->stop(mddev
);
4589 if (err
== 0 && mddev
->pers
->sync_request
) {
4590 err
= bitmap_create(mddev
);
4592 printk(KERN_ERR
"%s: failed to create bitmap (%d)\n",
4593 mdname(mddev
), err
);
4594 mddev
->pers
->stop(mddev
);
4598 module_put(mddev
->pers
->owner
);
4600 bitmap_destroy(mddev
);
4603 if (mddev
->pers
->sync_request
) {
4604 if (mddev
->kobj
.sd
&&
4605 sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
4607 "md: cannot register extra attributes for %s\n",
4609 mddev
->sysfs_action
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "sync_action");
4610 } else if (mddev
->ro
== 2) /* auto-readonly not meaningful */
4613 atomic_set(&mddev
->writes_pending
,0);
4614 atomic_set(&mddev
->max_corr_read_errors
,
4615 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS
);
4616 mddev
->safemode
= 0;
4617 mddev
->safemode_timer
.function
= md_safemode_timeout
;
4618 mddev
->safemode_timer
.data
= (unsigned long) mddev
;
4619 mddev
->safemode_delay
= (200 * HZ
)/1000 +1; /* 200 msec delay */
4623 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4624 if (rdev
->raid_disk
>= 0) {
4626 sprintf(nm
, "rd%d", rdev
->raid_disk
);
4627 if (sysfs_create_link(&mddev
->kobj
, &rdev
->kobj
, nm
))
4628 /* failure here is OK */;
4631 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4634 md_update_sb(mddev
, 0);
4636 md_new_event(mddev
);
4637 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4638 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
4639 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
4642 EXPORT_SYMBOL_GPL(md_run
);
4644 static int do_md_run(mddev_t
*mddev
)
4648 err
= md_run(mddev
);
4651 err
= bitmap_load(mddev
);
4653 bitmap_destroy(mddev
);
4657 md_wakeup_thread(mddev
->thread
);
4658 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
4660 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
4661 revalidate_disk(mddev
->gendisk
);
4663 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
4668 static int restart_array(mddev_t
*mddev
)
4670 struct gendisk
*disk
= mddev
->gendisk
;
4672 /* Complain if it has no devices */
4673 if (list_empty(&mddev
->disks
))
4679 mddev
->safemode
= 0;
4681 set_disk_ro(disk
, 0);
4682 printk(KERN_INFO
"md: %s switched to read-write mode.\n",
4684 /* Kick recovery or resync if necessary */
4685 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4686 md_wakeup_thread(mddev
->thread
);
4687 md_wakeup_thread(mddev
->sync_thread
);
4688 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4692 /* similar to deny_write_access, but accounts for our holding a reference
4693 * to the file ourselves */
4694 static int deny_bitmap_write_access(struct file
* file
)
4696 struct inode
*inode
= file
->f_mapping
->host
;
4698 spin_lock(&inode
->i_lock
);
4699 if (atomic_read(&inode
->i_writecount
) > 1) {
4700 spin_unlock(&inode
->i_lock
);
4703 atomic_set(&inode
->i_writecount
, -1);
4704 spin_unlock(&inode
->i_lock
);
4709 void restore_bitmap_write_access(struct file
*file
)
4711 struct inode
*inode
= file
->f_mapping
->host
;
4713 spin_lock(&inode
->i_lock
);
4714 atomic_set(&inode
->i_writecount
, 1);
4715 spin_unlock(&inode
->i_lock
);
4718 static void md_clean(mddev_t
*mddev
)
4720 mddev
->array_sectors
= 0;
4721 mddev
->external_size
= 0;
4722 mddev
->dev_sectors
= 0;
4723 mddev
->raid_disks
= 0;
4724 mddev
->recovery_cp
= 0;
4725 mddev
->resync_min
= 0;
4726 mddev
->resync_max
= MaxSector
;
4727 mddev
->reshape_position
= MaxSector
;
4728 mddev
->external
= 0;
4729 mddev
->persistent
= 0;
4730 mddev
->level
= LEVEL_NONE
;
4731 mddev
->clevel
[0] = 0;
4734 mddev
->metadata_type
[0] = 0;
4735 mddev
->chunk_sectors
= 0;
4736 mddev
->ctime
= mddev
->utime
= 0;
4738 mddev
->max_disks
= 0;
4740 mddev
->can_decrease_events
= 0;
4741 mddev
->delta_disks
= 0;
4742 mddev
->new_level
= LEVEL_NONE
;
4743 mddev
->new_layout
= 0;
4744 mddev
->new_chunk_sectors
= 0;
4745 mddev
->curr_resync
= 0;
4746 mddev
->resync_mismatches
= 0;
4747 mddev
->suspend_lo
= mddev
->suspend_hi
= 0;
4748 mddev
->sync_speed_min
= mddev
->sync_speed_max
= 0;
4749 mddev
->recovery
= 0;
4752 mddev
->degraded
= 0;
4753 mddev
->safemode
= 0;
4754 mddev
->bitmap_info
.offset
= 0;
4755 mddev
->bitmap_info
.default_offset
= 0;
4756 mddev
->bitmap_info
.chunksize
= 0;
4757 mddev
->bitmap_info
.daemon_sleep
= 0;
4758 mddev
->bitmap_info
.max_write_behind
= 0;
4761 static void __md_stop_writes(mddev_t
*mddev
)
4763 if (mddev
->sync_thread
) {
4764 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4765 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4766 reap_sync_thread(mddev
);
4769 del_timer_sync(&mddev
->safemode_timer
);
4771 bitmap_flush(mddev
);
4772 md_super_wait(mddev
);
4774 if (!mddev
->in_sync
|| mddev
->flags
) {
4775 /* mark array as shutdown cleanly */
4777 md_update_sb(mddev
, 1);
4781 void md_stop_writes(mddev_t
*mddev
)
4784 __md_stop_writes(mddev
);
4785 mddev_unlock(mddev
);
4787 EXPORT_SYMBOL_GPL(md_stop_writes
);
4789 void md_stop(mddev_t
*mddev
)
4792 mddev
->pers
->stop(mddev
);
4793 if (mddev
->pers
->sync_request
&& mddev
->to_remove
== NULL
)
4794 mddev
->to_remove
= &md_redundancy_group
;
4795 module_put(mddev
->pers
->owner
);
4797 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4799 EXPORT_SYMBOL_GPL(md_stop
);
4801 static int md_set_readonly(mddev_t
*mddev
, int is_open
)
4804 mutex_lock(&mddev
->open_mutex
);
4805 if (atomic_read(&mddev
->openers
) > is_open
) {
4806 printk("md: %s still in use.\n",mdname(mddev
));
4811 __md_stop_writes(mddev
);
4817 set_disk_ro(mddev
->gendisk
, 1);
4818 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4819 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4823 mutex_unlock(&mddev
->open_mutex
);
4828 * 0 - completely stop and dis-assemble array
4829 * 2 - stop but do not disassemble array
4831 static int do_md_stop(mddev_t
* mddev
, int mode
, int is_open
)
4833 struct gendisk
*disk
= mddev
->gendisk
;
4836 mutex_lock(&mddev
->open_mutex
);
4837 if (atomic_read(&mddev
->openers
) > is_open
||
4838 mddev
->sysfs_active
) {
4839 printk("md: %s still in use.\n",mdname(mddev
));
4840 mutex_unlock(&mddev
->open_mutex
);
4846 set_disk_ro(disk
, 0);
4848 __md_stop_writes(mddev
);
4850 mddev
->queue
->merge_bvec_fn
= NULL
;
4851 mddev
->queue
->backing_dev_info
.congested_fn
= NULL
;
4853 /* tell userspace to handle 'inactive' */
4854 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4856 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4857 if (rdev
->raid_disk
>= 0) {
4859 sprintf(nm
, "rd%d", rdev
->raid_disk
);
4860 sysfs_remove_link(&mddev
->kobj
, nm
);
4863 set_capacity(disk
, 0);
4864 mutex_unlock(&mddev
->open_mutex
);
4866 revalidate_disk(disk
);
4871 mutex_unlock(&mddev
->open_mutex
);
4873 * Free resources if final stop
4876 printk(KERN_INFO
"md: %s stopped.\n", mdname(mddev
));
4878 bitmap_destroy(mddev
);
4879 if (mddev
->bitmap_info
.file
) {
4880 restore_bitmap_write_access(mddev
->bitmap_info
.file
);
4881 fput(mddev
->bitmap_info
.file
);
4882 mddev
->bitmap_info
.file
= NULL
;
4884 mddev
->bitmap_info
.offset
= 0;
4886 export_array(mddev
);
4889 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
4890 if (mddev
->hold_active
== UNTIL_STOP
)
4891 mddev
->hold_active
= 0;
4893 blk_integrity_unregister(disk
);
4894 md_new_event(mddev
);
4895 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4900 static void autorun_array(mddev_t
*mddev
)
4905 if (list_empty(&mddev
->disks
))
4908 printk(KERN_INFO
"md: running: ");
4910 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
4911 char b
[BDEVNAME_SIZE
];
4912 printk("<%s>", bdevname(rdev
->bdev
,b
));
4916 err
= do_md_run(mddev
);
4918 printk(KERN_WARNING
"md: do_md_run() returned %d\n", err
);
4919 do_md_stop(mddev
, 0, 0);
4924 * lets try to run arrays based on all disks that have arrived
4925 * until now. (those are in pending_raid_disks)
4927 * the method: pick the first pending disk, collect all disks with
4928 * the same UUID, remove all from the pending list and put them into
4929 * the 'same_array' list. Then order this list based on superblock
4930 * update time (freshest comes first), kick out 'old' disks and
4931 * compare superblocks. If everything's fine then run it.
4933 * If "unit" is allocated, then bump its reference count
4935 static void autorun_devices(int part
)
4937 mdk_rdev_t
*rdev0
, *rdev
, *tmp
;
4939 char b
[BDEVNAME_SIZE
];
4941 printk(KERN_INFO
"md: autorun ...\n");
4942 while (!list_empty(&pending_raid_disks
)) {
4945 LIST_HEAD(candidates
);
4946 rdev0
= list_entry(pending_raid_disks
.next
,
4947 mdk_rdev_t
, same_set
);
4949 printk(KERN_INFO
"md: considering %s ...\n",
4950 bdevname(rdev0
->bdev
,b
));
4951 INIT_LIST_HEAD(&candidates
);
4952 rdev_for_each_list(rdev
, tmp
, &pending_raid_disks
)
4953 if (super_90_load(rdev
, rdev0
, 0) >= 0) {
4954 printk(KERN_INFO
"md: adding %s ...\n",
4955 bdevname(rdev
->bdev
,b
));
4956 list_move(&rdev
->same_set
, &candidates
);
4959 * now we have a set of devices, with all of them having
4960 * mostly sane superblocks. It's time to allocate the
4964 dev
= MKDEV(mdp_major
,
4965 rdev0
->preferred_minor
<< MdpMinorShift
);
4966 unit
= MINOR(dev
) >> MdpMinorShift
;
4968 dev
= MKDEV(MD_MAJOR
, rdev0
->preferred_minor
);
4971 if (rdev0
->preferred_minor
!= unit
) {
4972 printk(KERN_INFO
"md: unit number in %s is bad: %d\n",
4973 bdevname(rdev0
->bdev
, b
), rdev0
->preferred_minor
);
4977 md_probe(dev
, NULL
, NULL
);
4978 mddev
= mddev_find(dev
);
4979 if (!mddev
|| !mddev
->gendisk
) {
4983 "md: cannot allocate memory for md drive.\n");
4986 if (mddev_lock(mddev
))
4987 printk(KERN_WARNING
"md: %s locked, cannot run\n",
4989 else if (mddev
->raid_disks
|| mddev
->major_version
4990 || !list_empty(&mddev
->disks
)) {
4992 "md: %s already running, cannot run %s\n",
4993 mdname(mddev
), bdevname(rdev0
->bdev
,b
));
4994 mddev_unlock(mddev
);
4996 printk(KERN_INFO
"md: created %s\n", mdname(mddev
));
4997 mddev
->persistent
= 1;
4998 rdev_for_each_list(rdev
, tmp
, &candidates
) {
4999 list_del_init(&rdev
->same_set
);
5000 if (bind_rdev_to_array(rdev
, mddev
))
5003 autorun_array(mddev
);
5004 mddev_unlock(mddev
);
5006 /* on success, candidates will be empty, on error
5009 rdev_for_each_list(rdev
, tmp
, &candidates
) {
5010 list_del_init(&rdev
->same_set
);
5015 printk(KERN_INFO
"md: ... autorun DONE.\n");
5017 #endif /* !MODULE */
5019 static int get_version(void __user
* arg
)
5023 ver
.major
= MD_MAJOR_VERSION
;
5024 ver
.minor
= MD_MINOR_VERSION
;
5025 ver
.patchlevel
= MD_PATCHLEVEL_VERSION
;
5027 if (copy_to_user(arg
, &ver
, sizeof(ver
)))
5033 static int get_array_info(mddev_t
* mddev
, void __user
* arg
)
5035 mdu_array_info_t info
;
5036 int nr
,working
,insync
,failed
,spare
;
5039 nr
=working
=insync
=failed
=spare
=0;
5040 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
5042 if (test_bit(Faulty
, &rdev
->flags
))
5046 if (test_bit(In_sync
, &rdev
->flags
))
5053 info
.major_version
= mddev
->major_version
;
5054 info
.minor_version
= mddev
->minor_version
;
5055 info
.patch_version
= MD_PATCHLEVEL_VERSION
;
5056 info
.ctime
= mddev
->ctime
;
5057 info
.level
= mddev
->level
;
5058 info
.size
= mddev
->dev_sectors
/ 2;
5059 if (info
.size
!= mddev
->dev_sectors
/ 2) /* overflow */
5062 info
.raid_disks
= mddev
->raid_disks
;
5063 info
.md_minor
= mddev
->md_minor
;
5064 info
.not_persistent
= !mddev
->persistent
;
5066 info
.utime
= mddev
->utime
;
5069 info
.state
= (1<<MD_SB_CLEAN
);
5070 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
5071 info
.state
= (1<<MD_SB_BITMAP_PRESENT
);
5072 info
.active_disks
= insync
;
5073 info
.working_disks
= working
;
5074 info
.failed_disks
= failed
;
5075 info
.spare_disks
= spare
;
5077 info
.layout
= mddev
->layout
;
5078 info
.chunk_size
= mddev
->chunk_sectors
<< 9;
5080 if (copy_to_user(arg
, &info
, sizeof(info
)))
5086 static int get_bitmap_file(mddev_t
* mddev
, void __user
* arg
)
5088 mdu_bitmap_file_t
*file
= NULL
; /* too big for stack allocation */
5089 char *ptr
, *buf
= NULL
;
5092 if (md_allow_write(mddev
))
5093 file
= kmalloc(sizeof(*file
), GFP_NOIO
);
5095 file
= kmalloc(sizeof(*file
), GFP_KERNEL
);
5100 /* bitmap disabled, zero the first byte and copy out */
5101 if (!mddev
->bitmap
|| !mddev
->bitmap
->file
) {
5102 file
->pathname
[0] = '\0';
5106 buf
= kmalloc(sizeof(file
->pathname
), GFP_KERNEL
);
5110 ptr
= d_path(&mddev
->bitmap
->file
->f_path
, buf
, sizeof(file
->pathname
));
5114 strcpy(file
->pathname
, ptr
);
5118 if (copy_to_user(arg
, file
, sizeof(*file
)))
5126 static int get_disk_info(mddev_t
* mddev
, void __user
* arg
)
5128 mdu_disk_info_t info
;
5131 if (copy_from_user(&info
, arg
, sizeof(info
)))
5134 rdev
= find_rdev_nr(mddev
, info
.number
);
5136 info
.major
= MAJOR(rdev
->bdev
->bd_dev
);
5137 info
.minor
= MINOR(rdev
->bdev
->bd_dev
);
5138 info
.raid_disk
= rdev
->raid_disk
;
5140 if (test_bit(Faulty
, &rdev
->flags
))
5141 info
.state
|= (1<<MD_DISK_FAULTY
);
5142 else if (test_bit(In_sync
, &rdev
->flags
)) {
5143 info
.state
|= (1<<MD_DISK_ACTIVE
);
5144 info
.state
|= (1<<MD_DISK_SYNC
);
5146 if (test_bit(WriteMostly
, &rdev
->flags
))
5147 info
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
5149 info
.major
= info
.minor
= 0;
5150 info
.raid_disk
= -1;
5151 info
.state
= (1<<MD_DISK_REMOVED
);
5154 if (copy_to_user(arg
, &info
, sizeof(info
)))
5160 static int add_new_disk(mddev_t
* mddev
, mdu_disk_info_t
*info
)
5162 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
5164 dev_t dev
= MKDEV(info
->major
,info
->minor
);
5166 if (info
->major
!= MAJOR(dev
) || info
->minor
!= MINOR(dev
))
5169 if (!mddev
->raid_disks
) {
5171 /* expecting a device which has a superblock */
5172 rdev
= md_import_device(dev
, mddev
->major_version
, mddev
->minor_version
);
5175 "md: md_import_device returned %ld\n",
5177 return PTR_ERR(rdev
);
5179 if (!list_empty(&mddev
->disks
)) {
5180 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
5181 mdk_rdev_t
, same_set
);
5182 err
= super_types
[mddev
->major_version
]
5183 .load_super(rdev
, rdev0
, mddev
->minor_version
);
5186 "md: %s has different UUID to %s\n",
5187 bdevname(rdev
->bdev
,b
),
5188 bdevname(rdev0
->bdev
,b2
));
5193 err
= bind_rdev_to_array(rdev
, mddev
);
5200 * add_new_disk can be used once the array is assembled
5201 * to add "hot spares". They must already have a superblock
5206 if (!mddev
->pers
->hot_add_disk
) {
5208 "%s: personality does not support diskops!\n",
5212 if (mddev
->persistent
)
5213 rdev
= md_import_device(dev
, mddev
->major_version
,
5214 mddev
->minor_version
);
5216 rdev
= md_import_device(dev
, -1, -1);
5219 "md: md_import_device returned %ld\n",
5221 return PTR_ERR(rdev
);
5223 /* set saved_raid_disk if appropriate */
5224 if (!mddev
->persistent
) {
5225 if (info
->state
& (1<<MD_DISK_SYNC
) &&
5226 info
->raid_disk
< mddev
->raid_disks
) {
5227 rdev
->raid_disk
= info
->raid_disk
;
5228 set_bit(In_sync
, &rdev
->flags
);
5230 rdev
->raid_disk
= -1;
5232 super_types
[mddev
->major_version
].
5233 validate_super(mddev
, rdev
);
5234 if ((info
->state
& (1<<MD_DISK_SYNC
)) &&
5235 (!test_bit(In_sync
, &rdev
->flags
) ||
5236 rdev
->raid_disk
!= info
->raid_disk
)) {
5237 /* This was a hot-add request, but events doesn't
5238 * match, so reject it.
5244 if (test_bit(In_sync
, &rdev
->flags
))
5245 rdev
->saved_raid_disk
= rdev
->raid_disk
;
5247 rdev
->saved_raid_disk
= -1;
5249 clear_bit(In_sync
, &rdev
->flags
); /* just to be sure */
5250 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
5251 set_bit(WriteMostly
, &rdev
->flags
);
5253 clear_bit(WriteMostly
, &rdev
->flags
);
5255 rdev
->raid_disk
= -1;
5256 err
= bind_rdev_to_array(rdev
, mddev
);
5257 if (!err
&& !mddev
->pers
->hot_remove_disk
) {
5258 /* If there is hot_add_disk but no hot_remove_disk
5259 * then added disks for geometry changes,
5260 * and should be added immediately.
5262 super_types
[mddev
->major_version
].
5263 validate_super(mddev
, rdev
);
5264 err
= mddev
->pers
->hot_add_disk(mddev
, rdev
);
5266 unbind_rdev_from_array(rdev
);
5271 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
5273 md_update_sb(mddev
, 1);
5274 if (mddev
->degraded
)
5275 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
5276 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5278 md_new_event(mddev
);
5279 md_wakeup_thread(mddev
->thread
);
5283 /* otherwise, add_new_disk is only allowed
5284 * for major_version==0 superblocks
5286 if (mddev
->major_version
!= 0) {
5287 printk(KERN_WARNING
"%s: ADD_NEW_DISK not supported\n",
5292 if (!(info
->state
& (1<<MD_DISK_FAULTY
))) {
5294 rdev
= md_import_device(dev
, -1, 0);
5297 "md: error, md_import_device() returned %ld\n",
5299 return PTR_ERR(rdev
);
5301 rdev
->desc_nr
= info
->number
;
5302 if (info
->raid_disk
< mddev
->raid_disks
)
5303 rdev
->raid_disk
= info
->raid_disk
;
5305 rdev
->raid_disk
= -1;
5307 if (rdev
->raid_disk
< mddev
->raid_disks
)
5308 if (info
->state
& (1<<MD_DISK_SYNC
))
5309 set_bit(In_sync
, &rdev
->flags
);
5311 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
5312 set_bit(WriteMostly
, &rdev
->flags
);
5314 if (!mddev
->persistent
) {
5315 printk(KERN_INFO
"md: nonpersistent superblock ...\n");
5316 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
5318 rdev
->sb_start
= calc_dev_sboffset(rdev
);
5319 rdev
->sectors
= rdev
->sb_start
;
5321 err
= bind_rdev_to_array(rdev
, mddev
);
5331 static int hot_remove_disk(mddev_t
* mddev
, dev_t dev
)
5333 char b
[BDEVNAME_SIZE
];
5336 rdev
= find_rdev(mddev
, dev
);
5340 if (rdev
->raid_disk
>= 0)
5343 kick_rdev_from_array(rdev
);
5344 md_update_sb(mddev
, 1);
5345 md_new_event(mddev
);
5349 printk(KERN_WARNING
"md: cannot remove active disk %s from %s ...\n",
5350 bdevname(rdev
->bdev
,b
), mdname(mddev
));
5354 static int hot_add_disk(mddev_t
* mddev
, dev_t dev
)
5356 char b
[BDEVNAME_SIZE
];
5363 if (mddev
->major_version
!= 0) {
5364 printk(KERN_WARNING
"%s: HOT_ADD may only be used with"
5365 " version-0 superblocks.\n",
5369 if (!mddev
->pers
->hot_add_disk
) {
5371 "%s: personality does not support diskops!\n",
5376 rdev
= md_import_device(dev
, -1, 0);
5379 "md: error, md_import_device() returned %ld\n",
5384 if (mddev
->persistent
)
5385 rdev
->sb_start
= calc_dev_sboffset(rdev
);
5387 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
5389 rdev
->sectors
= rdev
->sb_start
;
5391 if (test_bit(Faulty
, &rdev
->flags
)) {
5393 "md: can not hot-add faulty %s disk to %s!\n",
5394 bdevname(rdev
->bdev
,b
), mdname(mddev
));
5398 clear_bit(In_sync
, &rdev
->flags
);
5400 rdev
->saved_raid_disk
= -1;
5401 err
= bind_rdev_to_array(rdev
, mddev
);
5406 * The rest should better be atomic, we can have disk failures
5407 * noticed in interrupt contexts ...
5410 rdev
->raid_disk
= -1;
5412 md_update_sb(mddev
, 1);
5415 * Kick recovery, maybe this spare has to be added to the
5416 * array immediately.
5418 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5419 md_wakeup_thread(mddev
->thread
);
5420 md_new_event(mddev
);
5428 static int set_bitmap_file(mddev_t
*mddev
, int fd
)
5433 if (!mddev
->pers
->quiesce
)
5435 if (mddev
->recovery
|| mddev
->sync_thread
)
5437 /* we should be able to change the bitmap.. */
5443 return -EEXIST
; /* cannot add when bitmap is present */
5444 mddev
->bitmap_info
.file
= fget(fd
);
5446 if (mddev
->bitmap_info
.file
== NULL
) {
5447 printk(KERN_ERR
"%s: error: failed to get bitmap file\n",
5452 err
= deny_bitmap_write_access(mddev
->bitmap_info
.file
);
5454 printk(KERN_ERR
"%s: error: bitmap file is already in use\n",
5456 fput(mddev
->bitmap_info
.file
);
5457 mddev
->bitmap_info
.file
= NULL
;
5460 mddev
->bitmap_info
.offset
= 0; /* file overrides offset */
5461 } else if (mddev
->bitmap
== NULL
)
5462 return -ENOENT
; /* cannot remove what isn't there */
5465 mddev
->pers
->quiesce(mddev
, 1);
5467 err
= bitmap_create(mddev
);
5469 err
= bitmap_load(mddev
);
5471 if (fd
< 0 || err
) {
5472 bitmap_destroy(mddev
);
5473 fd
= -1; /* make sure to put the file */
5475 mddev
->pers
->quiesce(mddev
, 0);
5478 if (mddev
->bitmap_info
.file
) {
5479 restore_bitmap_write_access(mddev
->bitmap_info
.file
);
5480 fput(mddev
->bitmap_info
.file
);
5482 mddev
->bitmap_info
.file
= NULL
;
5489 * set_array_info is used two different ways
5490 * The original usage is when creating a new array.
5491 * In this usage, raid_disks is > 0 and it together with
5492 * level, size, not_persistent,layout,chunksize determine the
5493 * shape of the array.
5494 * This will always create an array with a type-0.90.0 superblock.
5495 * The newer usage is when assembling an array.
5496 * In this case raid_disks will be 0, and the major_version field is
5497 * use to determine which style super-blocks are to be found on the devices.
5498 * The minor and patch _version numbers are also kept incase the
5499 * super_block handler wishes to interpret them.
5501 static int set_array_info(mddev_t
* mddev
, mdu_array_info_t
*info
)
5504 if (info
->raid_disks
== 0) {
5505 /* just setting version number for superblock loading */
5506 if (info
->major_version
< 0 ||
5507 info
->major_version
>= ARRAY_SIZE(super_types
) ||
5508 super_types
[info
->major_version
].name
== NULL
) {
5509 /* maybe try to auto-load a module? */
5511 "md: superblock version %d not known\n",
5512 info
->major_version
);
5515 mddev
->major_version
= info
->major_version
;
5516 mddev
->minor_version
= info
->minor_version
;
5517 mddev
->patch_version
= info
->patch_version
;
5518 mddev
->persistent
= !info
->not_persistent
;
5519 /* ensure mddev_put doesn't delete this now that there
5520 * is some minimal configuration.
5522 mddev
->ctime
= get_seconds();
5525 mddev
->major_version
= MD_MAJOR_VERSION
;
5526 mddev
->minor_version
= MD_MINOR_VERSION
;
5527 mddev
->patch_version
= MD_PATCHLEVEL_VERSION
;
5528 mddev
->ctime
= get_seconds();
5530 mddev
->level
= info
->level
;
5531 mddev
->clevel
[0] = 0;
5532 mddev
->dev_sectors
= 2 * (sector_t
)info
->size
;
5533 mddev
->raid_disks
= info
->raid_disks
;
5534 /* don't set md_minor, it is determined by which /dev/md* was
5537 if (info
->state
& (1<<MD_SB_CLEAN
))
5538 mddev
->recovery_cp
= MaxSector
;
5540 mddev
->recovery_cp
= 0;
5541 mddev
->persistent
= ! info
->not_persistent
;
5542 mddev
->external
= 0;
5544 mddev
->layout
= info
->layout
;
5545 mddev
->chunk_sectors
= info
->chunk_size
>> 9;
5547 mddev
->max_disks
= MD_SB_DISKS
;
5549 if (mddev
->persistent
)
5551 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
5553 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
5554 mddev
->bitmap_info
.offset
= 0;
5556 mddev
->reshape_position
= MaxSector
;
5559 * Generate a 128 bit UUID
5561 get_random_bytes(mddev
->uuid
, 16);
5563 mddev
->new_level
= mddev
->level
;
5564 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
5565 mddev
->new_layout
= mddev
->layout
;
5566 mddev
->delta_disks
= 0;
5571 void md_set_array_sectors(mddev_t
*mddev
, sector_t array_sectors
)
5573 WARN(!mddev_is_locked(mddev
), "%s: unlocked mddev!\n", __func__
);
5575 if (mddev
->external_size
)
5578 mddev
->array_sectors
= array_sectors
;
5580 EXPORT_SYMBOL(md_set_array_sectors
);
5582 static int update_size(mddev_t
*mddev
, sector_t num_sectors
)
5586 int fit
= (num_sectors
== 0);
5588 if (mddev
->pers
->resize
== NULL
)
5590 /* The "num_sectors" is the number of sectors of each device that
5591 * is used. This can only make sense for arrays with redundancy.
5592 * linear and raid0 always use whatever space is available. We can only
5593 * consider changing this number if no resync or reconstruction is
5594 * happening, and if the new size is acceptable. It must fit before the
5595 * sb_start or, if that is <data_offset, it must fit before the size
5596 * of each device. If num_sectors is zero, we find the largest size
5599 if (mddev
->sync_thread
)
5602 /* Sorry, cannot grow a bitmap yet, just remove it,
5606 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
5607 sector_t avail
= rdev
->sectors
;
5609 if (fit
&& (num_sectors
== 0 || num_sectors
> avail
))
5610 num_sectors
= avail
;
5611 if (avail
< num_sectors
)
5614 rv
= mddev
->pers
->resize(mddev
, num_sectors
);
5616 revalidate_disk(mddev
->gendisk
);
5620 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
)
5623 /* change the number of raid disks */
5624 if (mddev
->pers
->check_reshape
== NULL
)
5626 if (raid_disks
<= 0 ||
5627 (mddev
->max_disks
&& raid_disks
>= mddev
->max_disks
))
5629 if (mddev
->sync_thread
|| mddev
->reshape_position
!= MaxSector
)
5631 mddev
->delta_disks
= raid_disks
- mddev
->raid_disks
;
5633 rv
= mddev
->pers
->check_reshape(mddev
);
5635 mddev
->delta_disks
= 0;
5641 * update_array_info is used to change the configuration of an
5643 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5644 * fields in the info are checked against the array.
5645 * Any differences that cannot be handled will cause an error.
5646 * Normally, only one change can be managed at a time.
5648 static int update_array_info(mddev_t
*mddev
, mdu_array_info_t
*info
)
5654 /* calculate expected state,ignoring low bits */
5655 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
5656 state
|= (1 << MD_SB_BITMAP_PRESENT
);
5658 if (mddev
->major_version
!= info
->major_version
||
5659 mddev
->minor_version
!= info
->minor_version
||
5660 /* mddev->patch_version != info->patch_version || */
5661 mddev
->ctime
!= info
->ctime
||
5662 mddev
->level
!= info
->level
||
5663 /* mddev->layout != info->layout || */
5664 !mddev
->persistent
!= info
->not_persistent
||
5665 mddev
->chunk_sectors
!= info
->chunk_size
>> 9 ||
5666 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5667 ((state
^info
->state
) & 0xfffffe00)
5670 /* Check there is only one change */
5671 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
5673 if (mddev
->raid_disks
!= info
->raid_disks
)
5675 if (mddev
->layout
!= info
->layout
)
5677 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
))
5684 if (mddev
->layout
!= info
->layout
) {
5686 * we don't need to do anything at the md level, the
5687 * personality will take care of it all.
5689 if (mddev
->pers
->check_reshape
== NULL
)
5692 mddev
->new_layout
= info
->layout
;
5693 rv
= mddev
->pers
->check_reshape(mddev
);
5695 mddev
->new_layout
= mddev
->layout
;
5699 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
5700 rv
= update_size(mddev
, (sector_t
)info
->size
* 2);
5702 if (mddev
->raid_disks
!= info
->raid_disks
)
5703 rv
= update_raid_disks(mddev
, info
->raid_disks
);
5705 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) {
5706 if (mddev
->pers
->quiesce
== NULL
)
5708 if (mddev
->recovery
|| mddev
->sync_thread
)
5710 if (info
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
5711 /* add the bitmap */
5714 if (mddev
->bitmap_info
.default_offset
== 0)
5716 mddev
->bitmap_info
.offset
=
5717 mddev
->bitmap_info
.default_offset
;
5718 mddev
->pers
->quiesce(mddev
, 1);
5719 rv
= bitmap_create(mddev
);
5721 rv
= bitmap_load(mddev
);
5723 bitmap_destroy(mddev
);
5724 mddev
->pers
->quiesce(mddev
, 0);
5726 /* remove the bitmap */
5729 if (mddev
->bitmap
->file
)
5731 mddev
->pers
->quiesce(mddev
, 1);
5732 bitmap_destroy(mddev
);
5733 mddev
->pers
->quiesce(mddev
, 0);
5734 mddev
->bitmap_info
.offset
= 0;
5737 md_update_sb(mddev
, 1);
5741 static int set_disk_faulty(mddev_t
*mddev
, dev_t dev
)
5745 if (mddev
->pers
== NULL
)
5748 rdev
= find_rdev(mddev
, dev
);
5752 md_error(mddev
, rdev
);
5757 * We have a problem here : there is no easy way to give a CHS
5758 * virtual geometry. We currently pretend that we have a 2 heads
5759 * 4 sectors (with a BIG number of cylinders...). This drives
5760 * dosfs just mad... ;-)
5762 static int md_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
5764 mddev_t
*mddev
= bdev
->bd_disk
->private_data
;
5768 geo
->cylinders
= mddev
->array_sectors
/ 8;
5772 static int md_ioctl(struct block_device
*bdev
, fmode_t mode
,
5773 unsigned int cmd
, unsigned long arg
)
5776 void __user
*argp
= (void __user
*)arg
;
5777 mddev_t
*mddev
= NULL
;
5780 if (!capable(CAP_SYS_ADMIN
))
5784 * Commands dealing with the RAID driver but not any
5790 err
= get_version(argp
);
5793 case PRINT_RAID_DEBUG
:
5801 autostart_arrays(arg
);
5808 * Commands creating/starting a new array:
5811 mddev
= bdev
->bd_disk
->private_data
;
5818 err
= mddev_lock(mddev
);
5821 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5828 case SET_ARRAY_INFO
:
5830 mdu_array_info_t info
;
5832 memset(&info
, 0, sizeof(info
));
5833 else if (copy_from_user(&info
, argp
, sizeof(info
))) {
5838 err
= update_array_info(mddev
, &info
);
5840 printk(KERN_WARNING
"md: couldn't update"
5841 " array info. %d\n", err
);
5846 if (!list_empty(&mddev
->disks
)) {
5848 "md: array %s already has disks!\n",
5853 if (mddev
->raid_disks
) {
5855 "md: array %s already initialised!\n",
5860 err
= set_array_info(mddev
, &info
);
5862 printk(KERN_WARNING
"md: couldn't set"
5863 " array info. %d\n", err
);
5873 * Commands querying/configuring an existing array:
5875 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
5876 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
5877 if ((!mddev
->raid_disks
&& !mddev
->external
)
5878 && cmd
!= ADD_NEW_DISK
&& cmd
!= STOP_ARRAY
5879 && cmd
!= RUN_ARRAY
&& cmd
!= SET_BITMAP_FILE
5880 && cmd
!= GET_BITMAP_FILE
) {
5886 * Commands even a read-only array can execute:
5890 case GET_ARRAY_INFO
:
5891 err
= get_array_info(mddev
, argp
);
5894 case GET_BITMAP_FILE
:
5895 err
= get_bitmap_file(mddev
, argp
);
5899 err
= get_disk_info(mddev
, argp
);
5902 case RESTART_ARRAY_RW
:
5903 err
= restart_array(mddev
);
5907 err
= do_md_stop(mddev
, 0, 1);
5911 err
= md_set_readonly(mddev
, 1);
5915 if (get_user(ro
, (int __user
*)(arg
))) {
5921 /* if the bdev is going readonly the value of mddev->ro
5922 * does not matter, no writes are coming
5927 /* are we are already prepared for writes? */
5931 /* transitioning to readauto need only happen for
5932 * arrays that call md_write_start
5935 err
= restart_array(mddev
);
5938 set_disk_ro(mddev
->gendisk
, 0);
5945 * The remaining ioctls are changing the state of the
5946 * superblock, so we do not allow them on read-only arrays.
5947 * However non-MD ioctls (e.g. get-size) will still come through
5948 * here and hit the 'default' below, so only disallow
5949 * 'md' ioctls, and switch to rw mode if started auto-readonly.
5951 if (_IOC_TYPE(cmd
) == MD_MAJOR
&& mddev
->ro
&& mddev
->pers
) {
5952 if (mddev
->ro
== 2) {
5954 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5955 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5956 md_wakeup_thread(mddev
->thread
);
5967 mdu_disk_info_t info
;
5968 if (copy_from_user(&info
, argp
, sizeof(info
)))
5971 err
= add_new_disk(mddev
, &info
);
5975 case HOT_REMOVE_DISK
:
5976 err
= hot_remove_disk(mddev
, new_decode_dev(arg
));
5980 err
= hot_add_disk(mddev
, new_decode_dev(arg
));
5983 case SET_DISK_FAULTY
:
5984 err
= set_disk_faulty(mddev
, new_decode_dev(arg
));
5988 err
= do_md_run(mddev
);
5991 case SET_BITMAP_FILE
:
5992 err
= set_bitmap_file(mddev
, (int)arg
);
6002 if (mddev
->hold_active
== UNTIL_IOCTL
&&
6004 mddev
->hold_active
= 0;
6005 mddev_unlock(mddev
);
6014 #ifdef CONFIG_COMPAT
6015 static int md_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
6016 unsigned int cmd
, unsigned long arg
)
6019 case HOT_REMOVE_DISK
:
6021 case SET_DISK_FAULTY
:
6022 case SET_BITMAP_FILE
:
6023 /* These take in integer arg, do not convert */
6026 arg
= (unsigned long)compat_ptr(arg
);
6030 return md_ioctl(bdev
, mode
, cmd
, arg
);
6032 #endif /* CONFIG_COMPAT */
6034 static int md_open(struct block_device
*bdev
, fmode_t mode
)
6037 * Succeed if we can lock the mddev, which confirms that
6038 * it isn't being stopped right now.
6040 mddev_t
*mddev
= mddev_find(bdev
->bd_dev
);
6043 if (mddev
->gendisk
!= bdev
->bd_disk
) {
6044 /* we are racing with mddev_put which is discarding this
6048 /* Wait until bdev->bd_disk is definitely gone */
6049 flush_workqueue(md_misc_wq
);
6050 /* Then retry the open from the top */
6051 return -ERESTARTSYS
;
6053 BUG_ON(mddev
!= bdev
->bd_disk
->private_data
);
6055 if ((err
= mutex_lock_interruptible(&mddev
->open_mutex
)))
6059 atomic_inc(&mddev
->openers
);
6060 mutex_unlock(&mddev
->open_mutex
);
6062 check_disk_change(bdev
);
6067 static int md_release(struct gendisk
*disk
, fmode_t mode
)
6069 mddev_t
*mddev
= disk
->private_data
;
6072 atomic_dec(&mddev
->openers
);
6078 static int md_media_changed(struct gendisk
*disk
)
6080 mddev_t
*mddev
= disk
->private_data
;
6082 return mddev
->changed
;
6085 static int md_revalidate(struct gendisk
*disk
)
6087 mddev_t
*mddev
= disk
->private_data
;
6092 static const struct block_device_operations md_fops
=
6094 .owner
= THIS_MODULE
,
6096 .release
= md_release
,
6098 #ifdef CONFIG_COMPAT
6099 .compat_ioctl
= md_compat_ioctl
,
6101 .getgeo
= md_getgeo
,
6102 .media_changed
= md_media_changed
,
6103 .revalidate_disk
= md_revalidate
,
6106 static int md_thread(void * arg
)
6108 mdk_thread_t
*thread
= arg
;
6111 * md_thread is a 'system-thread', it's priority should be very
6112 * high. We avoid resource deadlocks individually in each
6113 * raid personality. (RAID5 does preallocation) We also use RR and
6114 * the very same RT priority as kswapd, thus we will never get
6115 * into a priority inversion deadlock.
6117 * we definitely have to have equal or higher priority than
6118 * bdflush, otherwise bdflush will deadlock if there are too
6119 * many dirty RAID5 blocks.
6122 allow_signal(SIGKILL
);
6123 while (!kthread_should_stop()) {
6125 /* We need to wait INTERRUPTIBLE so that
6126 * we don't add to the load-average.
6127 * That means we need to be sure no signals are
6130 if (signal_pending(current
))
6131 flush_signals(current
);
6133 wait_event_interruptible_timeout
6135 test_bit(THREAD_WAKEUP
, &thread
->flags
)
6136 || kthread_should_stop(),
6139 clear_bit(THREAD_WAKEUP
, &thread
->flags
);
6140 if (!kthread_should_stop())
6141 thread
->run(thread
->mddev
);
6147 void md_wakeup_thread(mdk_thread_t
*thread
)
6150 dprintk("md: waking up MD thread %s.\n", thread
->tsk
->comm
);
6151 set_bit(THREAD_WAKEUP
, &thread
->flags
);
6152 wake_up(&thread
->wqueue
);
6156 mdk_thread_t
*md_register_thread(void (*run
) (mddev_t
*), mddev_t
*mddev
,
6159 mdk_thread_t
*thread
;
6161 thread
= kzalloc(sizeof(mdk_thread_t
), GFP_KERNEL
);
6165 init_waitqueue_head(&thread
->wqueue
);
6168 thread
->mddev
= mddev
;
6169 thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
6170 thread
->tsk
= kthread_run(md_thread
, thread
,
6172 mdname(thread
->mddev
),
6173 name
?: mddev
->pers
->name
);
6174 if (IS_ERR(thread
->tsk
)) {
6181 void md_unregister_thread(mdk_thread_t
*thread
)
6185 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread
->tsk
));
6187 kthread_stop(thread
->tsk
);
6191 void md_error(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
6198 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
6201 if (mddev
->external
)
6202 set_bit(Blocked
, &rdev
->flags
);
6204 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
6206 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
6207 __builtin_return_address(0),__builtin_return_address(1),
6208 __builtin_return_address(2),__builtin_return_address(3));
6212 if (!mddev
->pers
->error_handler
)
6214 mddev
->pers
->error_handler(mddev
,rdev
);
6215 if (mddev
->degraded
)
6216 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
6217 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
6218 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6219 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6220 md_wakeup_thread(mddev
->thread
);
6221 if (mddev
->event_work
.func
)
6222 queue_work(md_misc_wq
, &mddev
->event_work
);
6223 md_new_event_inintr(mddev
);
6226 /* seq_file implementation /proc/mdstat */
6228 static void status_unused(struct seq_file
*seq
)
6233 seq_printf(seq
, "unused devices: ");
6235 list_for_each_entry(rdev
, &pending_raid_disks
, same_set
) {
6236 char b
[BDEVNAME_SIZE
];
6238 seq_printf(seq
, "%s ",
6239 bdevname(rdev
->bdev
,b
));
6242 seq_printf(seq
, "<none>");
6244 seq_printf(seq
, "\n");
6248 static void status_resync(struct seq_file
*seq
, mddev_t
* mddev
)
6250 sector_t max_sectors
, resync
, res
;
6251 unsigned long dt
, db
;
6254 unsigned int per_milli
;
6256 resync
= mddev
->curr_resync
- atomic_read(&mddev
->recovery_active
);
6258 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
6259 max_sectors
= mddev
->resync_max_sectors
;
6261 max_sectors
= mddev
->dev_sectors
;
6264 * Should not happen.
6270 /* Pick 'scale' such that (resync>>scale)*1000 will fit
6271 * in a sector_t, and (max_sectors>>scale) will fit in a
6272 * u32, as those are the requirements for sector_div.
6273 * Thus 'scale' must be at least 10
6276 if (sizeof(sector_t
) > sizeof(unsigned long)) {
6277 while ( max_sectors
/2 > (1ULL<<(scale
+32)))
6280 res
= (resync
>>scale
)*1000;
6281 sector_div(res
, (u32
)((max_sectors
>>scale
)+1));
6285 int i
, x
= per_milli
/50, y
= 20-x
;
6286 seq_printf(seq
, "[");
6287 for (i
= 0; i
< x
; i
++)
6288 seq_printf(seq
, "=");
6289 seq_printf(seq
, ">");
6290 for (i
= 0; i
< y
; i
++)
6291 seq_printf(seq
, ".");
6292 seq_printf(seq
, "] ");
6294 seq_printf(seq
, " %s =%3u.%u%% (%llu/%llu)",
6295 (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)?
6297 (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)?
6299 (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ?
6300 "resync" : "recovery"))),
6301 per_milli
/10, per_milli
% 10,
6302 (unsigned long long) resync
/2,
6303 (unsigned long long) max_sectors
/2);
6306 * dt: time from mark until now
6307 * db: blocks written from mark until now
6308 * rt: remaining time
6310 * rt is a sector_t, so could be 32bit or 64bit.
6311 * So we divide before multiply in case it is 32bit and close
6313 * We scale the divisor (db) by 32 to avoid losing precision
6314 * near the end of resync when the number of remaining sectors
6316 * We then divide rt by 32 after multiplying by db to compensate.
6317 * The '+1' avoids division by zero if db is very small.
6319 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
6321 db
= (mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
))
6322 - mddev
->resync_mark_cnt
;
6324 rt
= max_sectors
- resync
; /* number of remaining sectors */
6325 sector_div(rt
, db
/32+1);
6329 seq_printf(seq
, " finish=%lu.%lumin", (unsigned long)rt
/ 60,
6330 ((unsigned long)rt
% 60)/6);
6332 seq_printf(seq
, " speed=%ldK/sec", db
/2/dt
);
6335 static void *md_seq_start(struct seq_file
*seq
, loff_t
*pos
)
6337 struct list_head
*tmp
;
6347 spin_lock(&all_mddevs_lock
);
6348 list_for_each(tmp
,&all_mddevs
)
6350 mddev
= list_entry(tmp
, mddev_t
, all_mddevs
);
6352 spin_unlock(&all_mddevs_lock
);
6355 spin_unlock(&all_mddevs_lock
);
6357 return (void*)2;/* tail */
6361 static void *md_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
6363 struct list_head
*tmp
;
6364 mddev_t
*next_mddev
, *mddev
= v
;
6370 spin_lock(&all_mddevs_lock
);
6372 tmp
= all_mddevs
.next
;
6374 tmp
= mddev
->all_mddevs
.next
;
6375 if (tmp
!= &all_mddevs
)
6376 next_mddev
= mddev_get(list_entry(tmp
,mddev_t
,all_mddevs
));
6378 next_mddev
= (void*)2;
6381 spin_unlock(&all_mddevs_lock
);
6389 static void md_seq_stop(struct seq_file
*seq
, void *v
)
6393 if (mddev
&& v
!= (void*)1 && v
!= (void*)2)
6397 struct mdstat_info
{
6401 static int md_seq_show(struct seq_file
*seq
, void *v
)
6406 struct mdstat_info
*mi
= seq
->private;
6407 struct bitmap
*bitmap
;
6409 if (v
== (void*)1) {
6410 struct mdk_personality
*pers
;
6411 seq_printf(seq
, "Personalities : ");
6412 spin_lock(&pers_lock
);
6413 list_for_each_entry(pers
, &pers_list
, list
)
6414 seq_printf(seq
, "[%s] ", pers
->name
);
6416 spin_unlock(&pers_lock
);
6417 seq_printf(seq
, "\n");
6418 mi
->event
= atomic_read(&md_event_count
);
6421 if (v
== (void*)2) {
6426 if (mddev_lock(mddev
) < 0)
6429 if (mddev
->pers
|| mddev
->raid_disks
|| !list_empty(&mddev
->disks
)) {
6430 seq_printf(seq
, "%s : %sactive", mdname(mddev
),
6431 mddev
->pers
? "" : "in");
6434 seq_printf(seq
, " (read-only)");
6436 seq_printf(seq
, " (auto-read-only)");
6437 seq_printf(seq
, " %s", mddev
->pers
->name
);
6441 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
6442 char b
[BDEVNAME_SIZE
];
6443 seq_printf(seq
, " %s[%d]",
6444 bdevname(rdev
->bdev
,b
), rdev
->desc_nr
);
6445 if (test_bit(WriteMostly
, &rdev
->flags
))
6446 seq_printf(seq
, "(W)");
6447 if (test_bit(Faulty
, &rdev
->flags
)) {
6448 seq_printf(seq
, "(F)");
6450 } else if (rdev
->raid_disk
< 0)
6451 seq_printf(seq
, "(S)"); /* spare */
6452 sectors
+= rdev
->sectors
;
6455 if (!list_empty(&mddev
->disks
)) {
6457 seq_printf(seq
, "\n %llu blocks",
6458 (unsigned long long)
6459 mddev
->array_sectors
/ 2);
6461 seq_printf(seq
, "\n %llu blocks",
6462 (unsigned long long)sectors
/ 2);
6464 if (mddev
->persistent
) {
6465 if (mddev
->major_version
!= 0 ||
6466 mddev
->minor_version
!= 90) {
6467 seq_printf(seq
," super %d.%d",
6468 mddev
->major_version
,
6469 mddev
->minor_version
);
6471 } else if (mddev
->external
)
6472 seq_printf(seq
, " super external:%s",
6473 mddev
->metadata_type
);
6475 seq_printf(seq
, " super non-persistent");
6478 mddev
->pers
->status(seq
, mddev
);
6479 seq_printf(seq
, "\n ");
6480 if (mddev
->pers
->sync_request
) {
6481 if (mddev
->curr_resync
> 2) {
6482 status_resync(seq
, mddev
);
6483 seq_printf(seq
, "\n ");
6484 } else if (mddev
->curr_resync
== 1 || mddev
->curr_resync
== 2)
6485 seq_printf(seq
, "\tresync=DELAYED\n ");
6486 else if (mddev
->recovery_cp
< MaxSector
)
6487 seq_printf(seq
, "\tresync=PENDING\n ");
6490 seq_printf(seq
, "\n ");
6492 if ((bitmap
= mddev
->bitmap
)) {
6493 unsigned long chunk_kb
;
6494 unsigned long flags
;
6495 spin_lock_irqsave(&bitmap
->lock
, flags
);
6496 chunk_kb
= mddev
->bitmap_info
.chunksize
>> 10;
6497 seq_printf(seq
, "bitmap: %lu/%lu pages [%luKB], "
6499 bitmap
->pages
- bitmap
->missing_pages
,
6501 (bitmap
->pages
- bitmap
->missing_pages
)
6502 << (PAGE_SHIFT
- 10),
6503 chunk_kb
? chunk_kb
: mddev
->bitmap_info
.chunksize
,
6504 chunk_kb
? "KB" : "B");
6506 seq_printf(seq
, ", file: ");
6507 seq_path(seq
, &bitmap
->file
->f_path
, " \t\n");
6510 seq_printf(seq
, "\n");
6511 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
6514 seq_printf(seq
, "\n");
6516 mddev_unlock(mddev
);
6521 static const struct seq_operations md_seq_ops
= {
6522 .start
= md_seq_start
,
6523 .next
= md_seq_next
,
6524 .stop
= md_seq_stop
,
6525 .show
= md_seq_show
,
6528 static int md_seq_open(struct inode
*inode
, struct file
*file
)
6531 struct mdstat_info
*mi
= kmalloc(sizeof(*mi
), GFP_KERNEL
);
6535 error
= seq_open(file
, &md_seq_ops
);
6539 struct seq_file
*p
= file
->private_data
;
6541 mi
->event
= atomic_read(&md_event_count
);
6546 static unsigned int mdstat_poll(struct file
*filp
, poll_table
*wait
)
6548 struct seq_file
*m
= filp
->private_data
;
6549 struct mdstat_info
*mi
= m
->private;
6552 poll_wait(filp
, &md_event_waiters
, wait
);
6554 /* always allow read */
6555 mask
= POLLIN
| POLLRDNORM
;
6557 if (mi
->event
!= atomic_read(&md_event_count
))
6558 mask
|= POLLERR
| POLLPRI
;
6562 static const struct file_operations md_seq_fops
= {
6563 .owner
= THIS_MODULE
,
6564 .open
= md_seq_open
,
6566 .llseek
= seq_lseek
,
6567 .release
= seq_release_private
,
6568 .poll
= mdstat_poll
,
6571 int register_md_personality(struct mdk_personality
*p
)
6573 spin_lock(&pers_lock
);
6574 list_add_tail(&p
->list
, &pers_list
);
6575 printk(KERN_INFO
"md: %s personality registered for level %d\n", p
->name
, p
->level
);
6576 spin_unlock(&pers_lock
);
6580 int unregister_md_personality(struct mdk_personality
*p
)
6582 printk(KERN_INFO
"md: %s personality unregistered\n", p
->name
);
6583 spin_lock(&pers_lock
);
6584 list_del_init(&p
->list
);
6585 spin_unlock(&pers_lock
);
6589 static int is_mddev_idle(mddev_t
*mddev
, int init
)
6597 rdev_for_each_rcu(rdev
, mddev
) {
6598 struct gendisk
*disk
= rdev
->bdev
->bd_contains
->bd_disk
;
6599 curr_events
= (int)part_stat_read(&disk
->part0
, sectors
[0]) +
6600 (int)part_stat_read(&disk
->part0
, sectors
[1]) -
6601 atomic_read(&disk
->sync_io
);
6602 /* sync IO will cause sync_io to increase before the disk_stats
6603 * as sync_io is counted when a request starts, and
6604 * disk_stats is counted when it completes.
6605 * So resync activity will cause curr_events to be smaller than
6606 * when there was no such activity.
6607 * non-sync IO will cause disk_stat to increase without
6608 * increasing sync_io so curr_events will (eventually)
6609 * be larger than it was before. Once it becomes
6610 * substantially larger, the test below will cause
6611 * the array to appear non-idle, and resync will slow
6613 * If there is a lot of outstanding resync activity when
6614 * we set last_event to curr_events, then all that activity
6615 * completing might cause the array to appear non-idle
6616 * and resync will be slowed down even though there might
6617 * not have been non-resync activity. This will only
6618 * happen once though. 'last_events' will soon reflect
6619 * the state where there is little or no outstanding
6620 * resync requests, and further resync activity will
6621 * always make curr_events less than last_events.
6624 if (init
|| curr_events
- rdev
->last_events
> 64) {
6625 rdev
->last_events
= curr_events
;
6633 void md_done_sync(mddev_t
*mddev
, int blocks
, int ok
)
6635 /* another "blocks" (512byte) blocks have been synced */
6636 atomic_sub(blocks
, &mddev
->recovery_active
);
6637 wake_up(&mddev
->recovery_wait
);
6639 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6640 md_wakeup_thread(mddev
->thread
);
6641 // stop recovery, signal do_sync ....
6646 /* md_write_start(mddev, bi)
6647 * If we need to update some array metadata (e.g. 'active' flag
6648 * in superblock) before writing, schedule a superblock update
6649 * and wait for it to complete.
6651 void md_write_start(mddev_t
*mddev
, struct bio
*bi
)
6654 if (bio_data_dir(bi
) != WRITE
)
6657 BUG_ON(mddev
->ro
== 1);
6658 if (mddev
->ro
== 2) {
6659 /* need to switch to read/write */
6661 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6662 md_wakeup_thread(mddev
->thread
);
6663 md_wakeup_thread(mddev
->sync_thread
);
6666 atomic_inc(&mddev
->writes_pending
);
6667 if (mddev
->safemode
== 1)
6668 mddev
->safemode
= 0;
6669 if (mddev
->in_sync
) {
6670 spin_lock_irq(&mddev
->write_lock
);
6671 if (mddev
->in_sync
) {
6673 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6674 set_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
6675 md_wakeup_thread(mddev
->thread
);
6678 spin_unlock_irq(&mddev
->write_lock
);
6681 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6682 wait_event(mddev
->sb_wait
,
6683 !test_bit(MD_CHANGE_PENDING
, &mddev
->flags
));
6686 void md_write_end(mddev_t
*mddev
)
6688 if (atomic_dec_and_test(&mddev
->writes_pending
)) {
6689 if (mddev
->safemode
== 2)
6690 md_wakeup_thread(mddev
->thread
);
6691 else if (mddev
->safemode_delay
)
6692 mod_timer(&mddev
->safemode_timer
, jiffies
+ mddev
->safemode_delay
);
6696 /* md_allow_write(mddev)
6697 * Calling this ensures that the array is marked 'active' so that writes
6698 * may proceed without blocking. It is important to call this before
6699 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6700 * Must be called with mddev_lock held.
6702 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6703 * is dropped, so return -EAGAIN after notifying userspace.
6705 int md_allow_write(mddev_t
*mddev
)
6711 if (!mddev
->pers
->sync_request
)
6714 spin_lock_irq(&mddev
->write_lock
);
6715 if (mddev
->in_sync
) {
6717 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6718 set_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
6719 if (mddev
->safemode_delay
&&
6720 mddev
->safemode
== 0)
6721 mddev
->safemode
= 1;
6722 spin_unlock_irq(&mddev
->write_lock
);
6723 md_update_sb(mddev
, 0);
6724 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6726 spin_unlock_irq(&mddev
->write_lock
);
6728 if (test_bit(MD_CHANGE_PENDING
, &mddev
->flags
))
6733 EXPORT_SYMBOL_GPL(md_allow_write
);
6735 #define SYNC_MARKS 10
6736 #define SYNC_MARK_STEP (3*HZ)
6737 void md_do_sync(mddev_t
*mddev
)
6740 unsigned int currspeed
= 0,
6742 sector_t max_sectors
,j
, io_sectors
;
6743 unsigned long mark
[SYNC_MARKS
];
6744 sector_t mark_cnt
[SYNC_MARKS
];
6746 struct list_head
*tmp
;
6747 sector_t last_check
;
6752 /* just incase thread restarts... */
6753 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
6755 if (mddev
->ro
) /* never try to sync a read-only array */
6758 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
6759 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
6760 desc
= "data-check";
6761 else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
6762 desc
= "requested-resync";
6765 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
6770 /* we overload curr_resync somewhat here.
6771 * 0 == not engaged in resync at all
6772 * 2 == checking that there is no conflict with another sync
6773 * 1 == like 2, but have yielded to allow conflicting resync to
6775 * other == active in resync - this many blocks
6777 * Before starting a resync we must have set curr_resync to
6778 * 2, and then checked that every "conflicting" array has curr_resync
6779 * less than ours. When we find one that is the same or higher
6780 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6781 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6782 * This will mean we have to start checking from the beginning again.
6787 mddev
->curr_resync
= 2;
6790 if (kthread_should_stop())
6791 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6793 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
6795 for_each_mddev(mddev2
, tmp
) {
6796 if (mddev2
== mddev
)
6798 if (!mddev
->parallel_resync
6799 && mddev2
->curr_resync
6800 && match_mddev_units(mddev
, mddev2
)) {
6802 if (mddev
< mddev2
&& mddev
->curr_resync
== 2) {
6803 /* arbitrarily yield */
6804 mddev
->curr_resync
= 1;
6805 wake_up(&resync_wait
);
6807 if (mddev
> mddev2
&& mddev
->curr_resync
== 1)
6808 /* no need to wait here, we can wait the next
6809 * time 'round when curr_resync == 2
6812 /* We need to wait 'interruptible' so as not to
6813 * contribute to the load average, and not to
6814 * be caught by 'softlockup'
6816 prepare_to_wait(&resync_wait
, &wq
, TASK_INTERRUPTIBLE
);
6817 if (!kthread_should_stop() &&
6818 mddev2
->curr_resync
>= mddev
->curr_resync
) {
6819 printk(KERN_INFO
"md: delaying %s of %s"
6820 " until %s has finished (they"
6821 " share one or more physical units)\n",
6822 desc
, mdname(mddev
), mdname(mddev2
));
6824 if (signal_pending(current
))
6825 flush_signals(current
);
6827 finish_wait(&resync_wait
, &wq
);
6830 finish_wait(&resync_wait
, &wq
);
6833 } while (mddev
->curr_resync
< 2);
6836 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
6837 /* resync follows the size requested by the personality,
6838 * which defaults to physical size, but can be virtual size
6840 max_sectors
= mddev
->resync_max_sectors
;
6841 mddev
->resync_mismatches
= 0;
6842 /* we don't use the checkpoint if there's a bitmap */
6843 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
6844 j
= mddev
->resync_min
;
6845 else if (!mddev
->bitmap
)
6846 j
= mddev
->recovery_cp
;
6848 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
6849 max_sectors
= mddev
->dev_sectors
;
6851 /* recovery follows the physical size of devices */
6852 max_sectors
= mddev
->dev_sectors
;
6855 list_for_each_entry_rcu(rdev
, &mddev
->disks
, same_set
)
6856 if (rdev
->raid_disk
>= 0 &&
6857 !test_bit(Faulty
, &rdev
->flags
) &&
6858 !test_bit(In_sync
, &rdev
->flags
) &&
6859 rdev
->recovery_offset
< j
)
6860 j
= rdev
->recovery_offset
;
6864 printk(KERN_INFO
"md: %s of RAID array %s\n", desc
, mdname(mddev
));
6865 printk(KERN_INFO
"md: minimum _guaranteed_ speed:"
6866 " %d KB/sec/disk.\n", speed_min(mddev
));
6867 printk(KERN_INFO
"md: using maximum available idle IO bandwidth "
6868 "(but not more than %d KB/sec) for %s.\n",
6869 speed_max(mddev
), desc
);
6871 is_mddev_idle(mddev
, 1); /* this initializes IO event counters */
6874 for (m
= 0; m
< SYNC_MARKS
; m
++) {
6876 mark_cnt
[m
] = io_sectors
;
6879 mddev
->resync_mark
= mark
[last_mark
];
6880 mddev
->resync_mark_cnt
= mark_cnt
[last_mark
];
6883 * Tune reconstruction:
6885 window
= 32*(PAGE_SIZE
/512);
6886 printk(KERN_INFO
"md: using %dk window, over a total of %lluk.\n",
6887 window
/2, (unsigned long long)max_sectors
/2);
6889 atomic_set(&mddev
->recovery_active
, 0);
6894 "md: resuming %s of %s from checkpoint.\n",
6895 desc
, mdname(mddev
));
6896 mddev
->curr_resync
= j
;
6898 mddev
->curr_resync_completed
= j
;
6900 while (j
< max_sectors
) {
6905 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
6906 ((mddev
->curr_resync
> mddev
->curr_resync_completed
&&
6907 (mddev
->curr_resync
- mddev
->curr_resync_completed
)
6908 > (max_sectors
>> 4)) ||
6909 (j
- mddev
->curr_resync_completed
)*2
6910 >= mddev
->resync_max
- mddev
->curr_resync_completed
6912 /* time to update curr_resync_completed */
6913 wait_event(mddev
->recovery_wait
,
6914 atomic_read(&mddev
->recovery_active
) == 0);
6915 mddev
->curr_resync_completed
= j
;
6916 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6917 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
6920 while (j
>= mddev
->resync_max
&& !kthread_should_stop()) {
6921 /* As this condition is controlled by user-space,
6922 * we can block indefinitely, so use '_interruptible'
6923 * to avoid triggering warnings.
6925 flush_signals(current
); /* just in case */
6926 wait_event_interruptible(mddev
->recovery_wait
,
6927 mddev
->resync_max
> j
6928 || kthread_should_stop());
6931 if (kthread_should_stop())
6934 sectors
= mddev
->pers
->sync_request(mddev
, j
, &skipped
,
6935 currspeed
< speed_min(mddev
));
6937 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6941 if (!skipped
) { /* actual IO requested */
6942 io_sectors
+= sectors
;
6943 atomic_add(sectors
, &mddev
->recovery_active
);
6947 if (j
>1) mddev
->curr_resync
= j
;
6948 mddev
->curr_mark_cnt
= io_sectors
;
6949 if (last_check
== 0)
6950 /* this is the earliers that rebuilt will be
6951 * visible in /proc/mdstat
6953 md_new_event(mddev
);
6955 if (last_check
+ window
> io_sectors
|| j
== max_sectors
)
6958 last_check
= io_sectors
;
6960 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
6964 if (time_after_eq(jiffies
, mark
[last_mark
] + SYNC_MARK_STEP
)) {
6966 int next
= (last_mark
+1) % SYNC_MARKS
;
6968 mddev
->resync_mark
= mark
[next
];
6969 mddev
->resync_mark_cnt
= mark_cnt
[next
];
6970 mark
[next
] = jiffies
;
6971 mark_cnt
[next
] = io_sectors
- atomic_read(&mddev
->recovery_active
);
6976 if (kthread_should_stop())
6981 * this loop exits only if either when we are slower than
6982 * the 'hard' speed limit, or the system was IO-idle for
6984 * the system might be non-idle CPU-wise, but we only care
6985 * about not overloading the IO subsystem. (things like an
6986 * e2fsck being done on the RAID array should execute fast)
6990 currspeed
= ((unsigned long)(io_sectors
-mddev
->resync_mark_cnt
))/2
6991 /((jiffies
-mddev
->resync_mark
)/HZ
+1) +1;
6993 if (currspeed
> speed_min(mddev
)) {
6994 if ((currspeed
> speed_max(mddev
)) ||
6995 !is_mddev_idle(mddev
, 0)) {
7001 printk(KERN_INFO
"md: %s: %s done.\n",mdname(mddev
), desc
);
7003 * this also signals 'finished resyncing' to md_stop
7006 wait_event(mddev
->recovery_wait
, !atomic_read(&mddev
->recovery_active
));
7008 /* tell personality that we are finished */
7009 mddev
->pers
->sync_request(mddev
, max_sectors
, &skipped
, 1);
7011 if (!test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
) &&
7012 mddev
->curr_resync
> 2) {
7013 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
7014 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
7015 if (mddev
->curr_resync
>= mddev
->recovery_cp
) {
7017 "md: checkpointing %s of %s.\n",
7018 desc
, mdname(mddev
));
7019 mddev
->recovery_cp
= mddev
->curr_resync
;
7022 mddev
->recovery_cp
= MaxSector
;
7024 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
7025 mddev
->curr_resync
= MaxSector
;
7027 list_for_each_entry_rcu(rdev
, &mddev
->disks
, same_set
)
7028 if (rdev
->raid_disk
>= 0 &&
7029 mddev
->delta_disks
>= 0 &&
7030 !test_bit(Faulty
, &rdev
->flags
) &&
7031 !test_bit(In_sync
, &rdev
->flags
) &&
7032 rdev
->recovery_offset
< mddev
->curr_resync
)
7033 rdev
->recovery_offset
= mddev
->curr_resync
;
7037 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
7040 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
7041 /* We completed so min/max setting can be forgotten if used. */
7042 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
7043 mddev
->resync_min
= 0;
7044 mddev
->resync_max
= MaxSector
;
7045 } else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
7046 mddev
->resync_min
= mddev
->curr_resync_completed
;
7047 mddev
->curr_resync
= 0;
7048 wake_up(&resync_wait
);
7049 set_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
7050 md_wakeup_thread(mddev
->thread
);
7055 * got a signal, exit.
7058 "md: md_do_sync() got signal ... exiting\n");
7059 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7063 EXPORT_SYMBOL_GPL(md_do_sync
);
7065 static int remove_and_add_spares(mddev_t
*mddev
)
7070 mddev
->curr_resync_completed
= 0;
7072 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
7073 if (rdev
->raid_disk
>= 0 &&
7074 !test_bit(Blocked
, &rdev
->flags
) &&
7075 (test_bit(Faulty
, &rdev
->flags
) ||
7076 ! test_bit(In_sync
, &rdev
->flags
)) &&
7077 atomic_read(&rdev
->nr_pending
)==0) {
7078 if (mddev
->pers
->hot_remove_disk(
7079 mddev
, rdev
->raid_disk
)==0) {
7081 sprintf(nm
,"rd%d", rdev
->raid_disk
);
7082 sysfs_remove_link(&mddev
->kobj
, nm
);
7083 rdev
->raid_disk
= -1;
7087 if (mddev
->degraded
&& !mddev
->recovery_disabled
) {
7088 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
7089 if (rdev
->raid_disk
>= 0 &&
7090 !test_bit(In_sync
, &rdev
->flags
) &&
7091 !test_bit(Faulty
, &rdev
->flags
) &&
7092 !test_bit(Blocked
, &rdev
->flags
))
7094 if (rdev
->raid_disk
< 0
7095 && !test_bit(Faulty
, &rdev
->flags
)) {
7096 rdev
->recovery_offset
= 0;
7098 hot_add_disk(mddev
, rdev
) == 0) {
7100 sprintf(nm
, "rd%d", rdev
->raid_disk
);
7101 if (sysfs_create_link(&mddev
->kobj
,
7103 /* failure here is OK */;
7105 md_new_event(mddev
);
7106 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
7115 static void reap_sync_thread(mddev_t
*mddev
)
7119 /* resync has finished, collect result */
7120 md_unregister_thread(mddev
->sync_thread
);
7121 mddev
->sync_thread
= NULL
;
7122 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
7123 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
7125 /* activate any spares */
7126 if (mddev
->pers
->spare_active(mddev
))
7127 sysfs_notify(&mddev
->kobj
, NULL
,
7130 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
7131 mddev
->pers
->finish_reshape
)
7132 mddev
->pers
->finish_reshape(mddev
);
7133 md_update_sb(mddev
, 1);
7135 /* if array is no-longer degraded, then any saved_raid_disk
7136 * information must be scrapped
7138 if (!mddev
->degraded
)
7139 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
7140 rdev
->saved_raid_disk
= -1;
7142 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7143 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7144 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7145 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
7146 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7147 /* flag recovery needed just to double check */
7148 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7149 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7150 md_new_event(mddev
);
7154 * This routine is regularly called by all per-raid-array threads to
7155 * deal with generic issues like resync and super-block update.
7156 * Raid personalities that don't have a thread (linear/raid0) do not
7157 * need this as they never do any recovery or update the superblock.
7159 * It does not do any resync itself, but rather "forks" off other threads
7160 * to do that as needed.
7161 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7162 * "->recovery" and create a thread at ->sync_thread.
7163 * When the thread finishes it sets MD_RECOVERY_DONE
7164 * and wakeups up this thread which will reap the thread and finish up.
7165 * This thread also removes any faulty devices (with nr_pending == 0).
7167 * The overall approach is:
7168 * 1/ if the superblock needs updating, update it.
7169 * 2/ If a recovery thread is running, don't do anything else.
7170 * 3/ If recovery has finished, clean up, possibly marking spares active.
7171 * 4/ If there are any faulty devices, remove them.
7172 * 5/ If array is degraded, try to add spares devices
7173 * 6/ If array has spares or is not in-sync, start a resync thread.
7175 void md_check_recovery(mddev_t
*mddev
)
7177 if (mddev
->suspended
)
7181 bitmap_daemon_work(mddev
);
7186 if (signal_pending(current
)) {
7187 if (mddev
->pers
->sync_request
&& !mddev
->external
) {
7188 printk(KERN_INFO
"md: %s in immediate safe mode\n",
7190 mddev
->safemode
= 2;
7192 flush_signals(current
);
7195 if (mddev
->ro
&& !test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
7198 (mddev
->flags
& ~ (1<<MD_CHANGE_PENDING
)) ||
7199 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
7200 test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
7201 (mddev
->external
== 0 && mddev
->safemode
== 1) ||
7202 (mddev
->safemode
== 2 && ! atomic_read(&mddev
->writes_pending
)
7203 && !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
7207 if (mddev_trylock(mddev
)) {
7211 /* Only thing we do on a ro array is remove
7215 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
7216 if (rdev
->raid_disk
>= 0 &&
7217 !test_bit(Blocked
, &rdev
->flags
) &&
7218 test_bit(Faulty
, &rdev
->flags
) &&
7219 atomic_read(&rdev
->nr_pending
)==0) {
7220 if (mddev
->pers
->hot_remove_disk(
7221 mddev
, rdev
->raid_disk
)==0) {
7223 sprintf(nm
,"rd%d", rdev
->raid_disk
);
7224 sysfs_remove_link(&mddev
->kobj
, nm
);
7225 rdev
->raid_disk
= -1;
7228 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7232 if (!mddev
->external
) {
7234 spin_lock_irq(&mddev
->write_lock
);
7235 if (mddev
->safemode
&&
7236 !atomic_read(&mddev
->writes_pending
) &&
7238 mddev
->recovery_cp
== MaxSector
) {
7241 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
7243 if (mddev
->safemode
== 1)
7244 mddev
->safemode
= 0;
7245 spin_unlock_irq(&mddev
->write_lock
);
7247 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
7251 md_update_sb(mddev
, 0);
7253 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
7254 !test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
)) {
7255 /* resync/recovery still happening */
7256 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7259 if (mddev
->sync_thread
) {
7260 reap_sync_thread(mddev
);
7263 /* Set RUNNING before clearing NEEDED to avoid
7264 * any transients in the value of "sync_action".
7266 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7267 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7268 /* Clear some bits that don't mean anything, but
7271 clear_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7272 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
7274 if (test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
7276 /* no recovery is running.
7277 * remove any failed drives, then
7278 * add spares if possible.
7279 * Spare are also removed and re-added, to allow
7280 * the personality to fail the re-add.
7283 if (mddev
->reshape_position
!= MaxSector
) {
7284 if (mddev
->pers
->check_reshape
== NULL
||
7285 mddev
->pers
->check_reshape(mddev
) != 0)
7286 /* Cannot proceed */
7288 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7289 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7290 } else if ((spares
= remove_and_add_spares(mddev
))) {
7291 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7292 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7293 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
7294 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7295 } else if (mddev
->recovery_cp
< MaxSector
) {
7296 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7297 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7298 } else if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
7299 /* nothing to be done ... */
7302 if (mddev
->pers
->sync_request
) {
7303 if (spares
&& mddev
->bitmap
&& ! mddev
->bitmap
->file
) {
7304 /* We are adding a device or devices to an array
7305 * which has the bitmap stored on all devices.
7306 * So make sure all bitmap pages get written
7308 bitmap_write_all(mddev
->bitmap
);
7310 mddev
->sync_thread
= md_register_thread(md_do_sync
,
7313 if (!mddev
->sync_thread
) {
7314 printk(KERN_ERR
"%s: could not start resync"
7317 /* leave the spares where they are, it shouldn't hurt */
7318 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7319 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7320 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7321 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
7322 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7324 md_wakeup_thread(mddev
->sync_thread
);
7325 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7326 md_new_event(mddev
);
7329 if (!mddev
->sync_thread
) {
7330 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7331 if (test_and_clear_bit(MD_RECOVERY_RECOVER
,
7333 if (mddev
->sysfs_action
)
7334 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7336 mddev_unlock(mddev
);
7340 void md_wait_for_blocked_rdev(mdk_rdev_t
*rdev
, mddev_t
*mddev
)
7342 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
7343 wait_event_timeout(rdev
->blocked_wait
,
7344 !test_bit(Blocked
, &rdev
->flags
),
7345 msecs_to_jiffies(5000));
7346 rdev_dec_pending(rdev
, mddev
);
7348 EXPORT_SYMBOL(md_wait_for_blocked_rdev
);
7350 static int md_notify_reboot(struct notifier_block
*this,
7351 unsigned long code
, void *x
)
7353 struct list_head
*tmp
;
7356 if ((code
== SYS_DOWN
) || (code
== SYS_HALT
) || (code
== SYS_POWER_OFF
)) {
7358 printk(KERN_INFO
"md: stopping all md devices.\n");
7360 for_each_mddev(mddev
, tmp
)
7361 if (mddev_trylock(mddev
)) {
7362 /* Force a switch to readonly even array
7363 * appears to still be in use. Hence
7366 md_set_readonly(mddev
, 100);
7367 mddev_unlock(mddev
);
7370 * certain more exotic SCSI devices are known to be
7371 * volatile wrt too early system reboots. While the
7372 * right place to handle this issue is the given
7373 * driver, we do want to have a safe RAID driver ...
7380 static struct notifier_block md_notifier
= {
7381 .notifier_call
= md_notify_reboot
,
7383 .priority
= INT_MAX
, /* before any real devices */
7386 static void md_geninit(void)
7388 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t
));
7390 proc_create("mdstat", S_IRUGO
, NULL
, &md_seq_fops
);
7393 static int __init
md_init(void)
7397 md_wq
= alloc_workqueue("md", WQ_MEM_RECLAIM
, 0);
7401 md_misc_wq
= alloc_workqueue("md_misc", 0, 0);
7405 if ((ret
= register_blkdev(MD_MAJOR
, "md")) < 0)
7408 if ((ret
= register_blkdev(0, "mdp")) < 0)
7412 blk_register_region(MKDEV(MD_MAJOR
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
7413 md_probe
, NULL
, NULL
);
7414 blk_register_region(MKDEV(mdp_major
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
7415 md_probe
, NULL
, NULL
);
7417 register_reboot_notifier(&md_notifier
);
7418 raid_table_header
= register_sysctl_table(raid_root_table
);
7424 unregister_blkdev(MD_MAJOR
, "md");
7426 destroy_workqueue(md_misc_wq
);
7428 destroy_workqueue(md_wq
);
7436 * Searches all registered partitions for autorun RAID arrays
7440 static LIST_HEAD(all_detected_devices
);
7441 struct detected_devices_node
{
7442 struct list_head list
;
7446 void md_autodetect_dev(dev_t dev
)
7448 struct detected_devices_node
*node_detected_dev
;
7450 node_detected_dev
= kzalloc(sizeof(*node_detected_dev
), GFP_KERNEL
);
7451 if (node_detected_dev
) {
7452 node_detected_dev
->dev
= dev
;
7453 list_add_tail(&node_detected_dev
->list
, &all_detected_devices
);
7455 printk(KERN_CRIT
"md: md_autodetect_dev: kzalloc failed"
7456 ", skipping dev(%d,%d)\n", MAJOR(dev
), MINOR(dev
));
7461 static void autostart_arrays(int part
)
7464 struct detected_devices_node
*node_detected_dev
;
7466 int i_scanned
, i_passed
;
7471 printk(KERN_INFO
"md: Autodetecting RAID arrays.\n");
7473 while (!list_empty(&all_detected_devices
) && i_scanned
< INT_MAX
) {
7475 node_detected_dev
= list_entry(all_detected_devices
.next
,
7476 struct detected_devices_node
, list
);
7477 list_del(&node_detected_dev
->list
);
7478 dev
= node_detected_dev
->dev
;
7479 kfree(node_detected_dev
);
7480 rdev
= md_import_device(dev
,0, 90);
7484 if (test_bit(Faulty
, &rdev
->flags
)) {
7488 set_bit(AutoDetected
, &rdev
->flags
);
7489 list_add(&rdev
->same_set
, &pending_raid_disks
);
7493 printk(KERN_INFO
"md: Scanned %d and added %d devices.\n",
7494 i_scanned
, i_passed
);
7496 autorun_devices(part
);
7499 #endif /* !MODULE */
7501 static __exit
void md_exit(void)
7504 struct list_head
*tmp
;
7506 blk_unregister_region(MKDEV(MD_MAJOR
,0), 1U << MINORBITS
);
7507 blk_unregister_region(MKDEV(mdp_major
,0), 1U << MINORBITS
);
7509 unregister_blkdev(MD_MAJOR
,"md");
7510 unregister_blkdev(mdp_major
, "mdp");
7511 unregister_reboot_notifier(&md_notifier
);
7512 unregister_sysctl_table(raid_table_header
);
7513 remove_proc_entry("mdstat", NULL
);
7514 for_each_mddev(mddev
, tmp
) {
7515 export_array(mddev
);
7516 mddev
->hold_active
= 0;
7518 destroy_workqueue(md_misc_wq
);
7519 destroy_workqueue(md_wq
);
7522 subsys_initcall(md_init
);
7523 module_exit(md_exit
)
7525 static int get_ro(char *buffer
, struct kernel_param
*kp
)
7527 return sprintf(buffer
, "%d", start_readonly
);
7529 static int set_ro(const char *val
, struct kernel_param
*kp
)
7532 int num
= simple_strtoul(val
, &e
, 10);
7533 if (*val
&& (*e
== '\0' || *e
== '\n')) {
7534 start_readonly
= num
;
7540 module_param_call(start_ro
, set_ro
, get_ro
, NULL
, S_IRUSR
|S_IWUSR
);
7541 module_param(start_dirty_degraded
, int, S_IRUGO
|S_IWUSR
);
7543 module_param_call(new_array
, add_named_array
, NULL
, NULL
, S_IWUSR
);
7545 EXPORT_SYMBOL(register_md_personality
);
7546 EXPORT_SYMBOL(unregister_md_personality
);
7547 EXPORT_SYMBOL(md_error
);
7548 EXPORT_SYMBOL(md_done_sync
);
7549 EXPORT_SYMBOL(md_write_start
);
7550 EXPORT_SYMBOL(md_write_end
);
7551 EXPORT_SYMBOL(md_register_thread
);
7552 EXPORT_SYMBOL(md_unregister_thread
);
7553 EXPORT_SYMBOL(md_wakeup_thread
);
7554 EXPORT_SYMBOL(md_check_recovery
);
7555 MODULE_LICENSE("GPL");
7556 MODULE_DESCRIPTION("MD RAID framework");
7558 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR
);