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
;
291 if (mddev
== NULL
|| mddev
->pers
== NULL
296 smp_rmb(); /* Ensure implications of 'active' are visible */
298 if (mddev
->suspended
) {
301 prepare_to_wait(&mddev
->sb_wait
, &__wait
,
302 TASK_UNINTERRUPTIBLE
);
303 if (!mddev
->suspended
)
309 finish_wait(&mddev
->sb_wait
, &__wait
);
311 atomic_inc(&mddev
->active_io
);
314 rv
= mddev
->pers
->make_request(mddev
, bio
);
316 cpu
= part_stat_lock();
317 part_stat_inc(cpu
, &mddev
->gendisk
->part0
, ios
[rw
]);
318 part_stat_add(cpu
, &mddev
->gendisk
->part0
, sectors
[rw
],
322 if (atomic_dec_and_test(&mddev
->active_io
) && mddev
->suspended
)
323 wake_up(&mddev
->sb_wait
);
328 /* mddev_suspend makes sure no new requests are submitted
329 * to the device, and that any requests that have been submitted
330 * are completely handled.
331 * Once ->stop is called and completes, the module will be completely
334 void mddev_suspend(mddev_t
*mddev
)
336 BUG_ON(mddev
->suspended
);
337 mddev
->suspended
= 1;
339 wait_event(mddev
->sb_wait
, atomic_read(&mddev
->active_io
) == 0);
340 mddev
->pers
->quiesce(mddev
, 1);
342 EXPORT_SYMBOL_GPL(mddev_suspend
);
344 void mddev_resume(mddev_t
*mddev
)
346 mddev
->suspended
= 0;
347 wake_up(&mddev
->sb_wait
);
348 mddev
->pers
->quiesce(mddev
, 0);
350 EXPORT_SYMBOL_GPL(mddev_resume
);
352 int mddev_congested(mddev_t
*mddev
, int bits
)
354 return mddev
->suspended
;
356 EXPORT_SYMBOL(mddev_congested
);
359 * Generic flush handling for md
362 static void md_end_flush(struct bio
*bio
, int err
)
364 mdk_rdev_t
*rdev
= bio
->bi_private
;
365 mddev_t
*mddev
= rdev
->mddev
;
367 rdev_dec_pending(rdev
, mddev
);
369 if (atomic_dec_and_test(&mddev
->flush_pending
)) {
370 /* The pre-request flush has finished */
371 queue_work(md_wq
, &mddev
->flush_work
);
376 static void md_submit_flush_data(struct work_struct
*ws
);
378 static void submit_flushes(struct work_struct
*ws
)
380 mddev_t
*mddev
= container_of(ws
, mddev_t
, flush_work
);
383 INIT_WORK(&mddev
->flush_work
, md_submit_flush_data
);
384 atomic_set(&mddev
->flush_pending
, 1);
386 list_for_each_entry_rcu(rdev
, &mddev
->disks
, same_set
)
387 if (rdev
->raid_disk
>= 0 &&
388 !test_bit(Faulty
, &rdev
->flags
)) {
389 /* Take two references, one is dropped
390 * when request finishes, one after
391 * we reclaim rcu_read_lock
394 atomic_inc(&rdev
->nr_pending
);
395 atomic_inc(&rdev
->nr_pending
);
397 bi
= bio_alloc_mddev(GFP_KERNEL
, 0, mddev
);
398 bi
->bi_end_io
= md_end_flush
;
399 bi
->bi_private
= rdev
;
400 bi
->bi_bdev
= rdev
->bdev
;
401 atomic_inc(&mddev
->flush_pending
);
402 submit_bio(WRITE_FLUSH
, bi
);
404 rdev_dec_pending(rdev
, mddev
);
407 if (atomic_dec_and_test(&mddev
->flush_pending
))
408 queue_work(md_wq
, &mddev
->flush_work
);
411 static void md_submit_flush_data(struct work_struct
*ws
)
413 mddev_t
*mddev
= container_of(ws
, mddev_t
, flush_work
);
414 struct bio
*bio
= mddev
->flush_bio
;
416 if (bio
->bi_size
== 0)
417 /* an empty barrier - all done */
420 bio
->bi_rw
&= ~REQ_FLUSH
;
421 if (mddev
->pers
->make_request(mddev
, bio
))
422 generic_make_request(bio
);
425 mddev
->flush_bio
= NULL
;
426 wake_up(&mddev
->sb_wait
);
429 void md_flush_request(mddev_t
*mddev
, struct bio
*bio
)
431 spin_lock_irq(&mddev
->write_lock
);
432 wait_event_lock_irq(mddev
->sb_wait
,
434 mddev
->write_lock
, /*nothing*/);
435 mddev
->flush_bio
= bio
;
436 spin_unlock_irq(&mddev
->write_lock
);
438 INIT_WORK(&mddev
->flush_work
, submit_flushes
);
439 queue_work(md_wq
, &mddev
->flush_work
);
441 EXPORT_SYMBOL(md_flush_request
);
443 /* Support for plugging.
444 * This mirrors the plugging support in request_queue, but does not
445 * require having a whole queue
447 static void plugger_work(struct work_struct
*work
)
449 struct plug_handle
*plug
=
450 container_of(work
, struct plug_handle
, unplug_work
);
451 plug
->unplug_fn(plug
);
453 static void plugger_timeout(unsigned long data
)
455 struct plug_handle
*plug
= (void *)data
;
456 kblockd_schedule_work(NULL
, &plug
->unplug_work
);
458 void plugger_init(struct plug_handle
*plug
,
459 void (*unplug_fn
)(struct plug_handle
*))
461 plug
->unplug_flag
= 0;
462 plug
->unplug_fn
= unplug_fn
;
463 init_timer(&plug
->unplug_timer
);
464 plug
->unplug_timer
.function
= plugger_timeout
;
465 plug
->unplug_timer
.data
= (unsigned long)plug
;
466 INIT_WORK(&plug
->unplug_work
, plugger_work
);
468 EXPORT_SYMBOL_GPL(plugger_init
);
470 void plugger_set_plug(struct plug_handle
*plug
)
472 if (!test_and_set_bit(PLUGGED_FLAG
, &plug
->unplug_flag
))
473 mod_timer(&plug
->unplug_timer
, jiffies
+ msecs_to_jiffies(3)+1);
475 EXPORT_SYMBOL_GPL(plugger_set_plug
);
477 int plugger_remove_plug(struct plug_handle
*plug
)
479 if (test_and_clear_bit(PLUGGED_FLAG
, &plug
->unplug_flag
)) {
480 del_timer(&plug
->unplug_timer
);
485 EXPORT_SYMBOL_GPL(plugger_remove_plug
);
488 static inline mddev_t
*mddev_get(mddev_t
*mddev
)
490 atomic_inc(&mddev
->active
);
494 static void mddev_delayed_delete(struct work_struct
*ws
);
496 static void mddev_put(mddev_t
*mddev
)
498 struct bio_set
*bs
= NULL
;
500 if (!atomic_dec_and_lock(&mddev
->active
, &all_mddevs_lock
))
502 if (!mddev
->raid_disks
&& list_empty(&mddev
->disks
) &&
503 mddev
->ctime
== 0 && !mddev
->hold_active
) {
504 /* Array is not configured at all, and not held active,
506 list_del(&mddev
->all_mddevs
);
508 mddev
->bio_set
= NULL
;
509 if (mddev
->gendisk
) {
510 /* We did a probe so need to clean up. Call
511 * queue_work inside the spinlock so that
512 * flush_workqueue() after mddev_find will
513 * succeed in waiting for the work to be done.
515 INIT_WORK(&mddev
->del_work
, mddev_delayed_delete
);
516 queue_work(md_misc_wq
, &mddev
->del_work
);
520 spin_unlock(&all_mddevs_lock
);
525 void mddev_init(mddev_t
*mddev
)
527 mutex_init(&mddev
->open_mutex
);
528 mutex_init(&mddev
->reconfig_mutex
);
529 mutex_init(&mddev
->bitmap_info
.mutex
);
530 INIT_LIST_HEAD(&mddev
->disks
);
531 INIT_LIST_HEAD(&mddev
->all_mddevs
);
532 init_timer(&mddev
->safemode_timer
);
533 atomic_set(&mddev
->active
, 1);
534 atomic_set(&mddev
->openers
, 0);
535 atomic_set(&mddev
->active_io
, 0);
536 spin_lock_init(&mddev
->write_lock
);
537 atomic_set(&mddev
->flush_pending
, 0);
538 init_waitqueue_head(&mddev
->sb_wait
);
539 init_waitqueue_head(&mddev
->recovery_wait
);
540 mddev
->reshape_position
= MaxSector
;
541 mddev
->resync_min
= 0;
542 mddev
->resync_max
= MaxSector
;
543 mddev
->level
= LEVEL_NONE
;
545 EXPORT_SYMBOL_GPL(mddev_init
);
547 static mddev_t
* mddev_find(dev_t unit
)
549 mddev_t
*mddev
, *new = NULL
;
552 spin_lock(&all_mddevs_lock
);
555 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
556 if (mddev
->unit
== unit
) {
558 spin_unlock(&all_mddevs_lock
);
564 list_add(&new->all_mddevs
, &all_mddevs
);
565 spin_unlock(&all_mddevs_lock
);
566 new->hold_active
= UNTIL_IOCTL
;
570 /* find an unused unit number */
571 static int next_minor
= 512;
572 int start
= next_minor
;
576 dev
= MKDEV(MD_MAJOR
, next_minor
);
578 if (next_minor
> MINORMASK
)
580 if (next_minor
== start
) {
581 /* Oh dear, all in use. */
582 spin_unlock(&all_mddevs_lock
);
588 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
589 if (mddev
->unit
== dev
) {
595 new->md_minor
= MINOR(dev
);
596 new->hold_active
= UNTIL_STOP
;
597 list_add(&new->all_mddevs
, &all_mddevs
);
598 spin_unlock(&all_mddevs_lock
);
601 spin_unlock(&all_mddevs_lock
);
603 new = kzalloc(sizeof(*new), GFP_KERNEL
);
608 if (MAJOR(unit
) == MD_MAJOR
)
609 new->md_minor
= MINOR(unit
);
611 new->md_minor
= MINOR(unit
) >> MdpMinorShift
;
618 static inline int mddev_lock(mddev_t
* mddev
)
620 return mutex_lock_interruptible(&mddev
->reconfig_mutex
);
623 static inline int mddev_is_locked(mddev_t
*mddev
)
625 return mutex_is_locked(&mddev
->reconfig_mutex
);
628 static inline int mddev_trylock(mddev_t
* mddev
)
630 return mutex_trylock(&mddev
->reconfig_mutex
);
633 static struct attribute_group md_redundancy_group
;
635 static void mddev_unlock(mddev_t
* mddev
)
637 if (mddev
->to_remove
) {
638 /* These cannot be removed under reconfig_mutex as
639 * an access to the files will try to take reconfig_mutex
640 * while holding the file unremovable, which leads to
642 * So hold set sysfs_active while the remove in happeing,
643 * and anything else which might set ->to_remove or my
644 * otherwise change the sysfs namespace will fail with
645 * -EBUSY if sysfs_active is still set.
646 * We set sysfs_active under reconfig_mutex and elsewhere
647 * test it under the same mutex to ensure its correct value
650 struct attribute_group
*to_remove
= mddev
->to_remove
;
651 mddev
->to_remove
= NULL
;
652 mddev
->sysfs_active
= 1;
653 mutex_unlock(&mddev
->reconfig_mutex
);
655 if (mddev
->kobj
.sd
) {
656 if (to_remove
!= &md_redundancy_group
)
657 sysfs_remove_group(&mddev
->kobj
, to_remove
);
658 if (mddev
->pers
== NULL
||
659 mddev
->pers
->sync_request
== NULL
) {
660 sysfs_remove_group(&mddev
->kobj
, &md_redundancy_group
);
661 if (mddev
->sysfs_action
)
662 sysfs_put(mddev
->sysfs_action
);
663 mddev
->sysfs_action
= NULL
;
666 mddev
->sysfs_active
= 0;
668 mutex_unlock(&mddev
->reconfig_mutex
);
670 md_wakeup_thread(mddev
->thread
);
673 static mdk_rdev_t
* find_rdev_nr(mddev_t
*mddev
, int nr
)
677 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
678 if (rdev
->desc_nr
== nr
)
684 static mdk_rdev_t
* find_rdev(mddev_t
* mddev
, dev_t dev
)
688 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
689 if (rdev
->bdev
->bd_dev
== dev
)
695 static struct mdk_personality
*find_pers(int level
, char *clevel
)
697 struct mdk_personality
*pers
;
698 list_for_each_entry(pers
, &pers_list
, list
) {
699 if (level
!= LEVEL_NONE
&& pers
->level
== level
)
701 if (strcmp(pers
->name
, clevel
)==0)
707 /* return the offset of the super block in 512byte sectors */
708 static inline sector_t
calc_dev_sboffset(struct block_device
*bdev
)
710 sector_t num_sectors
= i_size_read(bdev
->bd_inode
) / 512;
711 return MD_NEW_SIZE_SECTORS(num_sectors
);
714 static int alloc_disk_sb(mdk_rdev_t
* rdev
)
719 rdev
->sb_page
= alloc_page(GFP_KERNEL
);
720 if (!rdev
->sb_page
) {
721 printk(KERN_ALERT
"md: out of memory.\n");
728 static void free_disk_sb(mdk_rdev_t
* rdev
)
731 put_page(rdev
->sb_page
);
733 rdev
->sb_page
= NULL
;
740 static void super_written(struct bio
*bio
, int error
)
742 mdk_rdev_t
*rdev
= bio
->bi_private
;
743 mddev_t
*mddev
= rdev
->mddev
;
745 if (error
|| !test_bit(BIO_UPTODATE
, &bio
->bi_flags
)) {
746 printk("md: super_written gets error=%d, uptodate=%d\n",
747 error
, test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
748 WARN_ON(test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
749 md_error(mddev
, rdev
);
752 if (atomic_dec_and_test(&mddev
->pending_writes
))
753 wake_up(&mddev
->sb_wait
);
757 void md_super_write(mddev_t
*mddev
, mdk_rdev_t
*rdev
,
758 sector_t sector
, int size
, struct page
*page
)
760 /* write first size bytes of page to sector of rdev
761 * Increment mddev->pending_writes before returning
762 * and decrement it on completion, waking up sb_wait
763 * if zero is reached.
764 * If an error occurred, call md_error
766 struct bio
*bio
= bio_alloc_mddev(GFP_NOIO
, 1, mddev
);
768 bio
->bi_bdev
= rdev
->bdev
;
769 bio
->bi_sector
= sector
;
770 bio_add_page(bio
, page
, size
, 0);
771 bio
->bi_private
= rdev
;
772 bio
->bi_end_io
= super_written
;
774 atomic_inc(&mddev
->pending_writes
);
775 submit_bio(REQ_WRITE
| REQ_SYNC
| REQ_UNPLUG
| REQ_FLUSH
| REQ_FUA
,
779 void md_super_wait(mddev_t
*mddev
)
781 /* wait for all superblock writes that were scheduled to complete */
784 prepare_to_wait(&mddev
->sb_wait
, &wq
, TASK_UNINTERRUPTIBLE
);
785 if (atomic_read(&mddev
->pending_writes
)==0)
789 finish_wait(&mddev
->sb_wait
, &wq
);
792 static void bi_complete(struct bio
*bio
, int error
)
794 complete((struct completion
*)bio
->bi_private
);
797 int sync_page_io(mdk_rdev_t
*rdev
, sector_t sector
, int size
,
798 struct page
*page
, int rw
)
800 struct bio
*bio
= bio_alloc_mddev(GFP_NOIO
, 1, rdev
->mddev
);
801 struct completion event
;
804 rw
|= REQ_SYNC
| REQ_UNPLUG
;
806 bio
->bi_bdev
= rdev
->bdev
;
807 bio
->bi_sector
= sector
;
808 bio_add_page(bio
, page
, size
, 0);
809 init_completion(&event
);
810 bio
->bi_private
= &event
;
811 bio
->bi_end_io
= bi_complete
;
813 wait_for_completion(&event
);
815 ret
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
819 EXPORT_SYMBOL_GPL(sync_page_io
);
821 static int read_disk_sb(mdk_rdev_t
* rdev
, int size
)
823 char b
[BDEVNAME_SIZE
];
824 if (!rdev
->sb_page
) {
832 if (!sync_page_io(rdev
, rdev
->sb_start
, size
, rdev
->sb_page
, READ
))
838 printk(KERN_WARNING
"md: disabled device %s, could not read superblock.\n",
839 bdevname(rdev
->bdev
,b
));
843 static int uuid_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
845 return sb1
->set_uuid0
== sb2
->set_uuid0
&&
846 sb1
->set_uuid1
== sb2
->set_uuid1
&&
847 sb1
->set_uuid2
== sb2
->set_uuid2
&&
848 sb1
->set_uuid3
== sb2
->set_uuid3
;
851 static int sb_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
854 mdp_super_t
*tmp1
, *tmp2
;
856 tmp1
= kmalloc(sizeof(*tmp1
),GFP_KERNEL
);
857 tmp2
= kmalloc(sizeof(*tmp2
),GFP_KERNEL
);
859 if (!tmp1
|| !tmp2
) {
861 printk(KERN_INFO
"md.c sb_equal(): failed to allocate memory!\n");
869 * nr_disks is not constant
874 ret
= (memcmp(tmp1
, tmp2
, MD_SB_GENERIC_CONSTANT_WORDS
* 4) == 0);
882 static u32
md_csum_fold(u32 csum
)
884 csum
= (csum
& 0xffff) + (csum
>> 16);
885 return (csum
& 0xffff) + (csum
>> 16);
888 static unsigned int calc_sb_csum(mdp_super_t
* sb
)
891 u32
*sb32
= (u32
*)sb
;
893 unsigned int disk_csum
, csum
;
895 disk_csum
= sb
->sb_csum
;
898 for (i
= 0; i
< MD_SB_BYTES
/4 ; i
++)
900 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
904 /* This used to use csum_partial, which was wrong for several
905 * reasons including that different results are returned on
906 * different architectures. It isn't critical that we get exactly
907 * the same return value as before (we always csum_fold before
908 * testing, and that removes any differences). However as we
909 * know that csum_partial always returned a 16bit value on
910 * alphas, do a fold to maximise conformity to previous behaviour.
912 sb
->sb_csum
= md_csum_fold(disk_csum
);
914 sb
->sb_csum
= disk_csum
;
921 * Handle superblock details.
922 * We want to be able to handle multiple superblock formats
923 * so we have a common interface to them all, and an array of
924 * different handlers.
925 * We rely on user-space to write the initial superblock, and support
926 * reading and updating of superblocks.
927 * Interface methods are:
928 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
929 * loads and validates a superblock on dev.
930 * if refdev != NULL, compare superblocks on both devices
932 * 0 - dev has a superblock that is compatible with refdev
933 * 1 - dev has a superblock that is compatible and newer than refdev
934 * so dev should be used as the refdev in future
935 * -EINVAL superblock incompatible or invalid
936 * -othererror e.g. -EIO
938 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
939 * Verify that dev is acceptable into mddev.
940 * The first time, mddev->raid_disks will be 0, and data from
941 * dev should be merged in. Subsequent calls check that dev
942 * is new enough. Return 0 or -EINVAL
944 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
945 * Update the superblock for rdev with data in mddev
946 * This does not write to disc.
952 struct module
*owner
;
953 int (*load_super
)(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
,
955 int (*validate_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
956 void (*sync_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
957 unsigned long long (*rdev_size_change
)(mdk_rdev_t
*rdev
,
958 sector_t num_sectors
);
962 * Check that the given mddev has no bitmap.
964 * This function is called from the run method of all personalities that do not
965 * support bitmaps. It prints an error message and returns non-zero if mddev
966 * has a bitmap. Otherwise, it returns 0.
969 int md_check_no_bitmap(mddev_t
*mddev
)
971 if (!mddev
->bitmap_info
.file
&& !mddev
->bitmap_info
.offset
)
973 printk(KERN_ERR
"%s: bitmaps are not supported for %s\n",
974 mdname(mddev
), mddev
->pers
->name
);
977 EXPORT_SYMBOL(md_check_no_bitmap
);
980 * load_super for 0.90.0
982 static int super_90_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
984 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
989 * Calculate the position of the superblock (512byte sectors),
990 * it's at the end of the disk.
992 * It also happens to be a multiple of 4Kb.
994 rdev
->sb_start
= calc_dev_sboffset(rdev
->bdev
);
996 ret
= read_disk_sb(rdev
, MD_SB_BYTES
);
1001 bdevname(rdev
->bdev
, b
);
1002 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
1004 if (sb
->md_magic
!= MD_SB_MAGIC
) {
1005 printk(KERN_ERR
"md: invalid raid superblock magic on %s\n",
1010 if (sb
->major_version
!= 0 ||
1011 sb
->minor_version
< 90 ||
1012 sb
->minor_version
> 91) {
1013 printk(KERN_WARNING
"Bad version number %d.%d on %s\n",
1014 sb
->major_version
, sb
->minor_version
,
1019 if (sb
->raid_disks
<= 0)
1022 if (md_csum_fold(calc_sb_csum(sb
)) != md_csum_fold(sb
->sb_csum
)) {
1023 printk(KERN_WARNING
"md: invalid superblock checksum on %s\n",
1028 rdev
->preferred_minor
= sb
->md_minor
;
1029 rdev
->data_offset
= 0;
1030 rdev
->sb_size
= MD_SB_BYTES
;
1032 if (sb
->level
== LEVEL_MULTIPATH
)
1035 rdev
->desc_nr
= sb
->this_disk
.number
;
1041 mdp_super_t
*refsb
= (mdp_super_t
*)page_address(refdev
->sb_page
);
1042 if (!uuid_equal(refsb
, sb
)) {
1043 printk(KERN_WARNING
"md: %s has different UUID to %s\n",
1044 b
, bdevname(refdev
->bdev
,b2
));
1047 if (!sb_equal(refsb
, sb
)) {
1048 printk(KERN_WARNING
"md: %s has same UUID"
1049 " but different superblock to %s\n",
1050 b
, bdevname(refdev
->bdev
, b2
));
1054 ev2
= md_event(refsb
);
1060 rdev
->sectors
= rdev
->sb_start
;
1062 if (rdev
->sectors
< sb
->size
* 2 && sb
->level
> 1)
1063 /* "this cannot possibly happen" ... */
1071 * validate_super for 0.90.0
1073 static int super_90_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1076 mdp_super_t
*sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
1077 __u64 ev1
= md_event(sb
);
1079 rdev
->raid_disk
= -1;
1080 clear_bit(Faulty
, &rdev
->flags
);
1081 clear_bit(In_sync
, &rdev
->flags
);
1082 clear_bit(WriteMostly
, &rdev
->flags
);
1084 if (mddev
->raid_disks
== 0) {
1085 mddev
->major_version
= 0;
1086 mddev
->minor_version
= sb
->minor_version
;
1087 mddev
->patch_version
= sb
->patch_version
;
1088 mddev
->external
= 0;
1089 mddev
->chunk_sectors
= sb
->chunk_size
>> 9;
1090 mddev
->ctime
= sb
->ctime
;
1091 mddev
->utime
= sb
->utime
;
1092 mddev
->level
= sb
->level
;
1093 mddev
->clevel
[0] = 0;
1094 mddev
->layout
= sb
->layout
;
1095 mddev
->raid_disks
= sb
->raid_disks
;
1096 mddev
->dev_sectors
= sb
->size
* 2;
1097 mddev
->events
= ev1
;
1098 mddev
->bitmap_info
.offset
= 0;
1099 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
1101 if (mddev
->minor_version
>= 91) {
1102 mddev
->reshape_position
= sb
->reshape_position
;
1103 mddev
->delta_disks
= sb
->delta_disks
;
1104 mddev
->new_level
= sb
->new_level
;
1105 mddev
->new_layout
= sb
->new_layout
;
1106 mddev
->new_chunk_sectors
= sb
->new_chunk
>> 9;
1108 mddev
->reshape_position
= MaxSector
;
1109 mddev
->delta_disks
= 0;
1110 mddev
->new_level
= mddev
->level
;
1111 mddev
->new_layout
= mddev
->layout
;
1112 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1115 if (sb
->state
& (1<<MD_SB_CLEAN
))
1116 mddev
->recovery_cp
= MaxSector
;
1118 if (sb
->events_hi
== sb
->cp_events_hi
&&
1119 sb
->events_lo
== sb
->cp_events_lo
) {
1120 mddev
->recovery_cp
= sb
->recovery_cp
;
1122 mddev
->recovery_cp
= 0;
1125 memcpy(mddev
->uuid
+0, &sb
->set_uuid0
, 4);
1126 memcpy(mddev
->uuid
+4, &sb
->set_uuid1
, 4);
1127 memcpy(mddev
->uuid
+8, &sb
->set_uuid2
, 4);
1128 memcpy(mddev
->uuid
+12,&sb
->set_uuid3
, 4);
1130 mddev
->max_disks
= MD_SB_DISKS
;
1132 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
) &&
1133 mddev
->bitmap_info
.file
== NULL
)
1134 mddev
->bitmap_info
.offset
=
1135 mddev
->bitmap_info
.default_offset
;
1137 } else if (mddev
->pers
== NULL
) {
1138 /* Insist on good event counter while assembling, except
1139 * for spares (which don't need an event count) */
1141 if (sb
->disks
[rdev
->desc_nr
].state
& (
1142 (1<<MD_DISK_SYNC
) | (1 << MD_DISK_ACTIVE
)))
1143 if (ev1
< mddev
->events
)
1145 } else if (mddev
->bitmap
) {
1146 /* if adding to array with a bitmap, then we can accept an
1147 * older device ... but not too old.
1149 if (ev1
< mddev
->bitmap
->events_cleared
)
1152 if (ev1
< mddev
->events
)
1153 /* just a hot-add of a new device, leave raid_disk at -1 */
1157 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1158 desc
= sb
->disks
+ rdev
->desc_nr
;
1160 if (desc
->state
& (1<<MD_DISK_FAULTY
))
1161 set_bit(Faulty
, &rdev
->flags
);
1162 else if (desc
->state
& (1<<MD_DISK_SYNC
) /* &&
1163 desc->raid_disk < mddev->raid_disks */) {
1164 set_bit(In_sync
, &rdev
->flags
);
1165 rdev
->raid_disk
= desc
->raid_disk
;
1166 } else if (desc
->state
& (1<<MD_DISK_ACTIVE
)) {
1167 /* active but not in sync implies recovery up to
1168 * reshape position. We don't know exactly where
1169 * that is, so set to zero for now */
1170 if (mddev
->minor_version
>= 91) {
1171 rdev
->recovery_offset
= 0;
1172 rdev
->raid_disk
= desc
->raid_disk
;
1175 if (desc
->state
& (1<<MD_DISK_WRITEMOSTLY
))
1176 set_bit(WriteMostly
, &rdev
->flags
);
1177 } else /* MULTIPATH are always insync */
1178 set_bit(In_sync
, &rdev
->flags
);
1183 * sync_super for 0.90.0
1185 static void super_90_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1189 int next_spare
= mddev
->raid_disks
;
1192 /* make rdev->sb match mddev data..
1195 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1196 * 3/ any empty disks < next_spare become removed
1198 * disks[0] gets initialised to REMOVED because
1199 * we cannot be sure from other fields if it has
1200 * been initialised or not.
1203 int active
=0, working
=0,failed
=0,spare
=0,nr_disks
=0;
1205 rdev
->sb_size
= MD_SB_BYTES
;
1207 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
1209 memset(sb
, 0, sizeof(*sb
));
1211 sb
->md_magic
= MD_SB_MAGIC
;
1212 sb
->major_version
= mddev
->major_version
;
1213 sb
->patch_version
= mddev
->patch_version
;
1214 sb
->gvalid_words
= 0; /* ignored */
1215 memcpy(&sb
->set_uuid0
, mddev
->uuid
+0, 4);
1216 memcpy(&sb
->set_uuid1
, mddev
->uuid
+4, 4);
1217 memcpy(&sb
->set_uuid2
, mddev
->uuid
+8, 4);
1218 memcpy(&sb
->set_uuid3
, mddev
->uuid
+12,4);
1220 sb
->ctime
= mddev
->ctime
;
1221 sb
->level
= mddev
->level
;
1222 sb
->size
= mddev
->dev_sectors
/ 2;
1223 sb
->raid_disks
= mddev
->raid_disks
;
1224 sb
->md_minor
= mddev
->md_minor
;
1225 sb
->not_persistent
= 0;
1226 sb
->utime
= mddev
->utime
;
1228 sb
->events_hi
= (mddev
->events
>>32);
1229 sb
->events_lo
= (u32
)mddev
->events
;
1231 if (mddev
->reshape_position
== MaxSector
)
1232 sb
->minor_version
= 90;
1234 sb
->minor_version
= 91;
1235 sb
->reshape_position
= mddev
->reshape_position
;
1236 sb
->new_level
= mddev
->new_level
;
1237 sb
->delta_disks
= mddev
->delta_disks
;
1238 sb
->new_layout
= mddev
->new_layout
;
1239 sb
->new_chunk
= mddev
->new_chunk_sectors
<< 9;
1241 mddev
->minor_version
= sb
->minor_version
;
1244 sb
->recovery_cp
= mddev
->recovery_cp
;
1245 sb
->cp_events_hi
= (mddev
->events
>>32);
1246 sb
->cp_events_lo
= (u32
)mddev
->events
;
1247 if (mddev
->recovery_cp
== MaxSector
)
1248 sb
->state
= (1<< MD_SB_CLEAN
);
1250 sb
->recovery_cp
= 0;
1252 sb
->layout
= mddev
->layout
;
1253 sb
->chunk_size
= mddev
->chunk_sectors
<< 9;
1255 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
)
1256 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
1258 sb
->disks
[0].state
= (1<<MD_DISK_REMOVED
);
1259 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
1262 int is_active
= test_bit(In_sync
, &rdev2
->flags
);
1264 if (rdev2
->raid_disk
>= 0 &&
1265 sb
->minor_version
>= 91)
1266 /* we have nowhere to store the recovery_offset,
1267 * but if it is not below the reshape_position,
1268 * we can piggy-back on that.
1271 if (rdev2
->raid_disk
< 0 ||
1272 test_bit(Faulty
, &rdev2
->flags
))
1275 desc_nr
= rdev2
->raid_disk
;
1277 desc_nr
= next_spare
++;
1278 rdev2
->desc_nr
= desc_nr
;
1279 d
= &sb
->disks
[rdev2
->desc_nr
];
1281 d
->number
= rdev2
->desc_nr
;
1282 d
->major
= MAJOR(rdev2
->bdev
->bd_dev
);
1283 d
->minor
= MINOR(rdev2
->bdev
->bd_dev
);
1285 d
->raid_disk
= rdev2
->raid_disk
;
1287 d
->raid_disk
= rdev2
->desc_nr
; /* compatibility */
1288 if (test_bit(Faulty
, &rdev2
->flags
))
1289 d
->state
= (1<<MD_DISK_FAULTY
);
1290 else if (is_active
) {
1291 d
->state
= (1<<MD_DISK_ACTIVE
);
1292 if (test_bit(In_sync
, &rdev2
->flags
))
1293 d
->state
|= (1<<MD_DISK_SYNC
);
1301 if (test_bit(WriteMostly
, &rdev2
->flags
))
1302 d
->state
|= (1<<MD_DISK_WRITEMOSTLY
);
1304 /* now set the "removed" and "faulty" bits on any missing devices */
1305 for (i
=0 ; i
< mddev
->raid_disks
; i
++) {
1306 mdp_disk_t
*d
= &sb
->disks
[i
];
1307 if (d
->state
== 0 && d
->number
== 0) {
1310 d
->state
= (1<<MD_DISK_REMOVED
);
1311 d
->state
|= (1<<MD_DISK_FAULTY
);
1315 sb
->nr_disks
= nr_disks
;
1316 sb
->active_disks
= active
;
1317 sb
->working_disks
= working
;
1318 sb
->failed_disks
= failed
;
1319 sb
->spare_disks
= spare
;
1321 sb
->this_disk
= sb
->disks
[rdev
->desc_nr
];
1322 sb
->sb_csum
= calc_sb_csum(sb
);
1326 * rdev_size_change for 0.90.0
1328 static unsigned long long
1329 super_90_rdev_size_change(mdk_rdev_t
*rdev
, sector_t num_sectors
)
1331 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1332 return 0; /* component must fit device */
1333 if (rdev
->mddev
->bitmap_info
.offset
)
1334 return 0; /* can't move bitmap */
1335 rdev
->sb_start
= calc_dev_sboffset(rdev
->bdev
);
1336 if (!num_sectors
|| num_sectors
> rdev
->sb_start
)
1337 num_sectors
= rdev
->sb_start
;
1338 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1340 md_super_wait(rdev
->mddev
);
1346 * version 1 superblock
1349 static __le32
calc_sb_1_csum(struct mdp_superblock_1
* sb
)
1353 unsigned long long newcsum
;
1354 int size
= 256 + le32_to_cpu(sb
->max_dev
)*2;
1355 __le32
*isuper
= (__le32
*)sb
;
1358 disk_csum
= sb
->sb_csum
;
1361 for (i
=0; size
>=4; size
-= 4 )
1362 newcsum
+= le32_to_cpu(*isuper
++);
1365 newcsum
+= le16_to_cpu(*(__le16
*) isuper
);
1367 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
1368 sb
->sb_csum
= disk_csum
;
1369 return cpu_to_le32(csum
);
1372 static int super_1_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
1374 struct mdp_superblock_1
*sb
;
1377 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1381 * Calculate the position of the superblock in 512byte sectors.
1382 * It is always aligned to a 4K boundary and
1383 * depeding on minor_version, it can be:
1384 * 0: At least 8K, but less than 12K, from end of device
1385 * 1: At start of device
1386 * 2: 4K from start of device.
1388 switch(minor_version
) {
1390 sb_start
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
1392 sb_start
&= ~(sector_t
)(4*2-1);
1403 rdev
->sb_start
= sb_start
;
1405 /* superblock is rarely larger than 1K, but it can be larger,
1406 * and it is safe to read 4k, so we do that
1408 ret
= read_disk_sb(rdev
, 4096);
1409 if (ret
) return ret
;
1412 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1414 if (sb
->magic
!= cpu_to_le32(MD_SB_MAGIC
) ||
1415 sb
->major_version
!= cpu_to_le32(1) ||
1416 le32_to_cpu(sb
->max_dev
) > (4096-256)/2 ||
1417 le64_to_cpu(sb
->super_offset
) != rdev
->sb_start
||
1418 (le32_to_cpu(sb
->feature_map
) & ~MD_FEATURE_ALL
) != 0)
1421 if (calc_sb_1_csum(sb
) != sb
->sb_csum
) {
1422 printk("md: invalid superblock checksum on %s\n",
1423 bdevname(rdev
->bdev
,b
));
1426 if (le64_to_cpu(sb
->data_size
) < 10) {
1427 printk("md: data_size too small on %s\n",
1428 bdevname(rdev
->bdev
,b
));
1432 rdev
->preferred_minor
= 0xffff;
1433 rdev
->data_offset
= le64_to_cpu(sb
->data_offset
);
1434 atomic_set(&rdev
->corrected_errors
, le32_to_cpu(sb
->cnt_corrected_read
));
1436 rdev
->sb_size
= le32_to_cpu(sb
->max_dev
) * 2 + 256;
1437 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1438 if (rdev
->sb_size
& bmask
)
1439 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1442 && rdev
->data_offset
< sb_start
+ (rdev
->sb_size
/512))
1445 if (sb
->level
== cpu_to_le32(LEVEL_MULTIPATH
))
1448 rdev
->desc_nr
= le32_to_cpu(sb
->dev_number
);
1454 struct mdp_superblock_1
*refsb
=
1455 (struct mdp_superblock_1
*)page_address(refdev
->sb_page
);
1457 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16) != 0 ||
1458 sb
->level
!= refsb
->level
||
1459 sb
->layout
!= refsb
->layout
||
1460 sb
->chunksize
!= refsb
->chunksize
) {
1461 printk(KERN_WARNING
"md: %s has strangely different"
1462 " superblock to %s\n",
1463 bdevname(rdev
->bdev
,b
),
1464 bdevname(refdev
->bdev
,b2
));
1467 ev1
= le64_to_cpu(sb
->events
);
1468 ev2
= le64_to_cpu(refsb
->events
);
1476 rdev
->sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) -
1477 le64_to_cpu(sb
->data_offset
);
1479 rdev
->sectors
= rdev
->sb_start
;
1480 if (rdev
->sectors
< le64_to_cpu(sb
->data_size
))
1482 rdev
->sectors
= le64_to_cpu(sb
->data_size
);
1483 if (le64_to_cpu(sb
->size
) > rdev
->sectors
)
1488 static int super_1_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1490 struct mdp_superblock_1
*sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1491 __u64 ev1
= le64_to_cpu(sb
->events
);
1493 rdev
->raid_disk
= -1;
1494 clear_bit(Faulty
, &rdev
->flags
);
1495 clear_bit(In_sync
, &rdev
->flags
);
1496 clear_bit(WriteMostly
, &rdev
->flags
);
1498 if (mddev
->raid_disks
== 0) {
1499 mddev
->major_version
= 1;
1500 mddev
->patch_version
= 0;
1501 mddev
->external
= 0;
1502 mddev
->chunk_sectors
= le32_to_cpu(sb
->chunksize
);
1503 mddev
->ctime
= le64_to_cpu(sb
->ctime
) & ((1ULL << 32)-1);
1504 mddev
->utime
= le64_to_cpu(sb
->utime
) & ((1ULL << 32)-1);
1505 mddev
->level
= le32_to_cpu(sb
->level
);
1506 mddev
->clevel
[0] = 0;
1507 mddev
->layout
= le32_to_cpu(sb
->layout
);
1508 mddev
->raid_disks
= le32_to_cpu(sb
->raid_disks
);
1509 mddev
->dev_sectors
= le64_to_cpu(sb
->size
);
1510 mddev
->events
= ev1
;
1511 mddev
->bitmap_info
.offset
= 0;
1512 mddev
->bitmap_info
.default_offset
= 1024 >> 9;
1514 mddev
->recovery_cp
= le64_to_cpu(sb
->resync_offset
);
1515 memcpy(mddev
->uuid
, sb
->set_uuid
, 16);
1517 mddev
->max_disks
= (4096-256)/2;
1519 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BITMAP_OFFSET
) &&
1520 mddev
->bitmap_info
.file
== NULL
)
1521 mddev
->bitmap_info
.offset
=
1522 (__s32
)le32_to_cpu(sb
->bitmap_offset
);
1524 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
1525 mddev
->reshape_position
= le64_to_cpu(sb
->reshape_position
);
1526 mddev
->delta_disks
= le32_to_cpu(sb
->delta_disks
);
1527 mddev
->new_level
= le32_to_cpu(sb
->new_level
);
1528 mddev
->new_layout
= le32_to_cpu(sb
->new_layout
);
1529 mddev
->new_chunk_sectors
= le32_to_cpu(sb
->new_chunk
);
1531 mddev
->reshape_position
= MaxSector
;
1532 mddev
->delta_disks
= 0;
1533 mddev
->new_level
= mddev
->level
;
1534 mddev
->new_layout
= mddev
->layout
;
1535 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1538 } else if (mddev
->pers
== NULL
) {
1539 /* Insist of good event counter while assembling, except for
1540 * spares (which don't need an event count) */
1542 if (rdev
->desc_nr
>= 0 &&
1543 rdev
->desc_nr
< le32_to_cpu(sb
->max_dev
) &&
1544 le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]) < 0xfffe)
1545 if (ev1
< mddev
->events
)
1547 } else if (mddev
->bitmap
) {
1548 /* If adding to array with a bitmap, then we can accept an
1549 * older device, but not too old.
1551 if (ev1
< mddev
->bitmap
->events_cleared
)
1554 if (ev1
< mddev
->events
)
1555 /* just a hot-add of a new device, leave raid_disk at -1 */
1558 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1560 if (rdev
->desc_nr
< 0 ||
1561 rdev
->desc_nr
>= le32_to_cpu(sb
->max_dev
)) {
1565 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
1567 case 0xffff: /* spare */
1569 case 0xfffe: /* faulty */
1570 set_bit(Faulty
, &rdev
->flags
);
1573 if ((le32_to_cpu(sb
->feature_map
) &
1574 MD_FEATURE_RECOVERY_OFFSET
))
1575 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
1577 set_bit(In_sync
, &rdev
->flags
);
1578 rdev
->raid_disk
= role
;
1581 if (sb
->devflags
& WriteMostly1
)
1582 set_bit(WriteMostly
, &rdev
->flags
);
1583 } else /* MULTIPATH are always insync */
1584 set_bit(In_sync
, &rdev
->flags
);
1589 static void super_1_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1591 struct mdp_superblock_1
*sb
;
1594 /* make rdev->sb match mddev and rdev data. */
1596 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1598 sb
->feature_map
= 0;
1600 sb
->recovery_offset
= cpu_to_le64(0);
1601 memset(sb
->pad1
, 0, sizeof(sb
->pad1
));
1602 memset(sb
->pad2
, 0, sizeof(sb
->pad2
));
1603 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
1605 sb
->utime
= cpu_to_le64((__u64
)mddev
->utime
);
1606 sb
->events
= cpu_to_le64(mddev
->events
);
1608 sb
->resync_offset
= cpu_to_le64(mddev
->recovery_cp
);
1610 sb
->resync_offset
= cpu_to_le64(0);
1612 sb
->cnt_corrected_read
= cpu_to_le32(atomic_read(&rdev
->corrected_errors
));
1614 sb
->raid_disks
= cpu_to_le32(mddev
->raid_disks
);
1615 sb
->size
= cpu_to_le64(mddev
->dev_sectors
);
1616 sb
->chunksize
= cpu_to_le32(mddev
->chunk_sectors
);
1617 sb
->level
= cpu_to_le32(mddev
->level
);
1618 sb
->layout
= cpu_to_le32(mddev
->layout
);
1620 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
) {
1621 sb
->bitmap_offset
= cpu_to_le32((__u32
)mddev
->bitmap_info
.offset
);
1622 sb
->feature_map
= cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
);
1625 if (rdev
->raid_disk
>= 0 &&
1626 !test_bit(In_sync
, &rdev
->flags
)) {
1628 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET
);
1629 sb
->recovery_offset
=
1630 cpu_to_le64(rdev
->recovery_offset
);
1633 if (mddev
->reshape_position
!= MaxSector
) {
1634 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE
);
1635 sb
->reshape_position
= cpu_to_le64(mddev
->reshape_position
);
1636 sb
->new_layout
= cpu_to_le32(mddev
->new_layout
);
1637 sb
->delta_disks
= cpu_to_le32(mddev
->delta_disks
);
1638 sb
->new_level
= cpu_to_le32(mddev
->new_level
);
1639 sb
->new_chunk
= cpu_to_le32(mddev
->new_chunk_sectors
);
1643 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
)
1644 if (rdev2
->desc_nr
+1 > max_dev
)
1645 max_dev
= rdev2
->desc_nr
+1;
1647 if (max_dev
> le32_to_cpu(sb
->max_dev
)) {
1649 sb
->max_dev
= cpu_to_le32(max_dev
);
1650 rdev
->sb_size
= max_dev
* 2 + 256;
1651 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1652 if (rdev
->sb_size
& bmask
)
1653 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1655 max_dev
= le32_to_cpu(sb
->max_dev
);
1657 for (i
=0; i
<max_dev
;i
++)
1658 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1660 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
1662 if (test_bit(Faulty
, &rdev2
->flags
))
1663 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1664 else if (test_bit(In_sync
, &rdev2
->flags
))
1665 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1666 else if (rdev2
->raid_disk
>= 0)
1667 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1669 sb
->dev_roles
[i
] = cpu_to_le16(0xffff);
1672 sb
->sb_csum
= calc_sb_1_csum(sb
);
1675 static unsigned long long
1676 super_1_rdev_size_change(mdk_rdev_t
*rdev
, sector_t num_sectors
)
1678 struct mdp_superblock_1
*sb
;
1679 sector_t max_sectors
;
1680 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1681 return 0; /* component must fit device */
1682 if (rdev
->sb_start
< rdev
->data_offset
) {
1683 /* minor versions 1 and 2; superblock before data */
1684 max_sectors
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
1685 max_sectors
-= rdev
->data_offset
;
1686 if (!num_sectors
|| num_sectors
> max_sectors
)
1687 num_sectors
= max_sectors
;
1688 } else if (rdev
->mddev
->bitmap_info
.offset
) {
1689 /* minor version 0 with bitmap we can't move */
1692 /* minor version 0; superblock after data */
1694 sb_start
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) - 8*2;
1695 sb_start
&= ~(sector_t
)(4*2 - 1);
1696 max_sectors
= rdev
->sectors
+ sb_start
- rdev
->sb_start
;
1697 if (!num_sectors
|| num_sectors
> max_sectors
)
1698 num_sectors
= max_sectors
;
1699 rdev
->sb_start
= sb_start
;
1701 sb
= (struct mdp_superblock_1
*) page_address(rdev
->sb_page
);
1702 sb
->data_size
= cpu_to_le64(num_sectors
);
1703 sb
->super_offset
= rdev
->sb_start
;
1704 sb
->sb_csum
= calc_sb_1_csum(sb
);
1705 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1707 md_super_wait(rdev
->mddev
);
1711 static struct super_type super_types
[] = {
1714 .owner
= THIS_MODULE
,
1715 .load_super
= super_90_load
,
1716 .validate_super
= super_90_validate
,
1717 .sync_super
= super_90_sync
,
1718 .rdev_size_change
= super_90_rdev_size_change
,
1722 .owner
= THIS_MODULE
,
1723 .load_super
= super_1_load
,
1724 .validate_super
= super_1_validate
,
1725 .sync_super
= super_1_sync
,
1726 .rdev_size_change
= super_1_rdev_size_change
,
1730 static int match_mddev_units(mddev_t
*mddev1
, mddev_t
*mddev2
)
1732 mdk_rdev_t
*rdev
, *rdev2
;
1735 rdev_for_each_rcu(rdev
, mddev1
)
1736 rdev_for_each_rcu(rdev2
, mddev2
)
1737 if (rdev
->bdev
->bd_contains
==
1738 rdev2
->bdev
->bd_contains
) {
1746 static LIST_HEAD(pending_raid_disks
);
1749 * Try to register data integrity profile for an mddev
1751 * This is called when an array is started and after a disk has been kicked
1752 * from the array. It only succeeds if all working and active component devices
1753 * are integrity capable with matching profiles.
1755 int md_integrity_register(mddev_t
*mddev
)
1757 mdk_rdev_t
*rdev
, *reference
= NULL
;
1759 if (list_empty(&mddev
->disks
))
1760 return 0; /* nothing to do */
1761 if (blk_get_integrity(mddev
->gendisk
))
1762 return 0; /* already registered */
1763 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
1764 /* skip spares and non-functional disks */
1765 if (test_bit(Faulty
, &rdev
->flags
))
1767 if (rdev
->raid_disk
< 0)
1770 * If at least one rdev is not integrity capable, we can not
1771 * enable data integrity for the md device.
1773 if (!bdev_get_integrity(rdev
->bdev
))
1776 /* Use the first rdev as the reference */
1780 /* does this rdev's profile match the reference profile? */
1781 if (blk_integrity_compare(reference
->bdev
->bd_disk
,
1782 rdev
->bdev
->bd_disk
) < 0)
1786 * All component devices are integrity capable and have matching
1787 * profiles, register the common profile for the md device.
1789 if (blk_integrity_register(mddev
->gendisk
,
1790 bdev_get_integrity(reference
->bdev
)) != 0) {
1791 printk(KERN_ERR
"md: failed to register integrity for %s\n",
1795 printk(KERN_NOTICE
"md: data integrity on %s enabled\n",
1799 EXPORT_SYMBOL(md_integrity_register
);
1801 /* Disable data integrity if non-capable/non-matching disk is being added */
1802 void md_integrity_add_rdev(mdk_rdev_t
*rdev
, mddev_t
*mddev
)
1804 struct blk_integrity
*bi_rdev
= bdev_get_integrity(rdev
->bdev
);
1805 struct blk_integrity
*bi_mddev
= blk_get_integrity(mddev
->gendisk
);
1807 if (!bi_mddev
) /* nothing to do */
1809 if (rdev
->raid_disk
< 0) /* skip spares */
1811 if (bi_rdev
&& blk_integrity_compare(mddev
->gendisk
,
1812 rdev
->bdev
->bd_disk
) >= 0)
1814 printk(KERN_NOTICE
"disabling data integrity on %s\n", mdname(mddev
));
1815 blk_integrity_unregister(mddev
->gendisk
);
1817 EXPORT_SYMBOL(md_integrity_add_rdev
);
1819 static int bind_rdev_to_array(mdk_rdev_t
* rdev
, mddev_t
* mddev
)
1821 char b
[BDEVNAME_SIZE
];
1831 /* prevent duplicates */
1832 if (find_rdev(mddev
, rdev
->bdev
->bd_dev
))
1835 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1836 if (rdev
->sectors
&& (mddev
->dev_sectors
== 0 ||
1837 rdev
->sectors
< mddev
->dev_sectors
)) {
1839 /* Cannot change size, so fail
1840 * If mddev->level <= 0, then we don't care
1841 * about aligning sizes (e.g. linear)
1843 if (mddev
->level
> 0)
1846 mddev
->dev_sectors
= rdev
->sectors
;
1849 /* Verify rdev->desc_nr is unique.
1850 * If it is -1, assign a free number, else
1851 * check number is not in use
1853 if (rdev
->desc_nr
< 0) {
1855 if (mddev
->pers
) choice
= mddev
->raid_disks
;
1856 while (find_rdev_nr(mddev
, choice
))
1858 rdev
->desc_nr
= choice
;
1860 if (find_rdev_nr(mddev
, rdev
->desc_nr
))
1863 if (mddev
->max_disks
&& rdev
->desc_nr
>= mddev
->max_disks
) {
1864 printk(KERN_WARNING
"md: %s: array is limited to %d devices\n",
1865 mdname(mddev
), mddev
->max_disks
);
1868 bdevname(rdev
->bdev
,b
);
1869 while ( (s
=strchr(b
, '/')) != NULL
)
1872 rdev
->mddev
= mddev
;
1873 printk(KERN_INFO
"md: bind<%s>\n", b
);
1875 if ((err
= kobject_add(&rdev
->kobj
, &mddev
->kobj
, "dev-%s", b
)))
1878 ko
= &part_to_dev(rdev
->bdev
->bd_part
)->kobj
;
1879 if (sysfs_create_link(&rdev
->kobj
, ko
, "block"))
1880 /* failure here is OK */;
1881 rdev
->sysfs_state
= sysfs_get_dirent_safe(rdev
->kobj
.sd
, "state");
1883 list_add_rcu(&rdev
->same_set
, &mddev
->disks
);
1884 bd_claim_by_disk(rdev
->bdev
, rdev
->bdev
->bd_holder
, mddev
->gendisk
);
1886 /* May as well allow recovery to be retried once */
1887 mddev
->recovery_disabled
= 0;
1892 printk(KERN_WARNING
"md: failed to register dev-%s for %s\n",
1897 static void md_delayed_delete(struct work_struct
*ws
)
1899 mdk_rdev_t
*rdev
= container_of(ws
, mdk_rdev_t
, del_work
);
1900 kobject_del(&rdev
->kobj
);
1901 kobject_put(&rdev
->kobj
);
1904 static void unbind_rdev_from_array(mdk_rdev_t
* rdev
)
1906 char b
[BDEVNAME_SIZE
];
1911 bd_release_from_disk(rdev
->bdev
, rdev
->mddev
->gendisk
);
1912 list_del_rcu(&rdev
->same_set
);
1913 printk(KERN_INFO
"md: unbind<%s>\n", bdevname(rdev
->bdev
,b
));
1915 sysfs_remove_link(&rdev
->kobj
, "block");
1916 sysfs_put(rdev
->sysfs_state
);
1917 rdev
->sysfs_state
= NULL
;
1918 /* We need to delay this, otherwise we can deadlock when
1919 * writing to 'remove' to "dev/state". We also need
1920 * to delay it due to rcu usage.
1923 INIT_WORK(&rdev
->del_work
, md_delayed_delete
);
1924 kobject_get(&rdev
->kobj
);
1925 queue_work(md_misc_wq
, &rdev
->del_work
);
1929 * prevent the device from being mounted, repartitioned or
1930 * otherwise reused by a RAID array (or any other kernel
1931 * subsystem), by bd_claiming the device.
1933 static int lock_rdev(mdk_rdev_t
*rdev
, dev_t dev
, int shared
)
1936 struct block_device
*bdev
;
1937 char b
[BDEVNAME_SIZE
];
1939 bdev
= open_by_devnum(dev
, FMODE_READ
|FMODE_WRITE
);
1941 printk(KERN_ERR
"md: could not open %s.\n",
1942 __bdevname(dev
, b
));
1943 return PTR_ERR(bdev
);
1945 err
= bd_claim(bdev
, shared
? (mdk_rdev_t
*)lock_rdev
: rdev
);
1947 printk(KERN_ERR
"md: could not bd_claim %s.\n",
1949 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
);
1953 set_bit(AllReserved
, &rdev
->flags
);
1958 static void unlock_rdev(mdk_rdev_t
*rdev
)
1960 struct block_device
*bdev
= rdev
->bdev
;
1965 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
);
1968 void md_autodetect_dev(dev_t dev
);
1970 static void export_rdev(mdk_rdev_t
* rdev
)
1972 char b
[BDEVNAME_SIZE
];
1973 printk(KERN_INFO
"md: export_rdev(%s)\n",
1974 bdevname(rdev
->bdev
,b
));
1979 if (test_bit(AutoDetected
, &rdev
->flags
))
1980 md_autodetect_dev(rdev
->bdev
->bd_dev
);
1983 kobject_put(&rdev
->kobj
);
1986 static void kick_rdev_from_array(mdk_rdev_t
* rdev
)
1988 unbind_rdev_from_array(rdev
);
1992 static void export_array(mddev_t
*mddev
)
1994 mdk_rdev_t
*rdev
, *tmp
;
1996 rdev_for_each(rdev
, tmp
, mddev
) {
2001 kick_rdev_from_array(rdev
);
2003 if (!list_empty(&mddev
->disks
))
2005 mddev
->raid_disks
= 0;
2006 mddev
->major_version
= 0;
2009 static void print_desc(mdp_disk_t
*desc
)
2011 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc
->number
,
2012 desc
->major
,desc
->minor
,desc
->raid_disk
,desc
->state
);
2015 static void print_sb_90(mdp_super_t
*sb
)
2020 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2021 sb
->major_version
, sb
->minor_version
, sb
->patch_version
,
2022 sb
->set_uuid0
, sb
->set_uuid1
, sb
->set_uuid2
, sb
->set_uuid3
,
2024 printk(KERN_INFO
"md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2025 sb
->level
, sb
->size
, sb
->nr_disks
, sb
->raid_disks
,
2026 sb
->md_minor
, sb
->layout
, sb
->chunk_size
);
2027 printk(KERN_INFO
"md: UT:%08x ST:%d AD:%d WD:%d"
2028 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2029 sb
->utime
, sb
->state
, sb
->active_disks
, sb
->working_disks
,
2030 sb
->failed_disks
, sb
->spare_disks
,
2031 sb
->sb_csum
, (unsigned long)sb
->events_lo
);
2034 for (i
= 0; i
< MD_SB_DISKS
; i
++) {
2037 desc
= sb
->disks
+ i
;
2038 if (desc
->number
|| desc
->major
|| desc
->minor
||
2039 desc
->raid_disk
|| (desc
->state
&& (desc
->state
!= 4))) {
2040 printk(" D %2d: ", i
);
2044 printk(KERN_INFO
"md: THIS: ");
2045 print_desc(&sb
->this_disk
);
2048 static void print_sb_1(struct mdp_superblock_1
*sb
)
2052 uuid
= sb
->set_uuid
;
2054 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
2055 "md: Name: \"%s\" CT:%llu\n",
2056 le32_to_cpu(sb
->major_version
),
2057 le32_to_cpu(sb
->feature_map
),
2060 (unsigned long long)le64_to_cpu(sb
->ctime
)
2061 & MD_SUPERBLOCK_1_TIME_SEC_MASK
);
2063 uuid
= sb
->device_uuid
;
2065 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
2067 "md: Dev:%08x UUID: %pU\n"
2068 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2069 "md: (MaxDev:%u) \n",
2070 le32_to_cpu(sb
->level
),
2071 (unsigned long long)le64_to_cpu(sb
->size
),
2072 le32_to_cpu(sb
->raid_disks
),
2073 le32_to_cpu(sb
->layout
),
2074 le32_to_cpu(sb
->chunksize
),
2075 (unsigned long long)le64_to_cpu(sb
->data_offset
),
2076 (unsigned long long)le64_to_cpu(sb
->data_size
),
2077 (unsigned long long)le64_to_cpu(sb
->super_offset
),
2078 (unsigned long long)le64_to_cpu(sb
->recovery_offset
),
2079 le32_to_cpu(sb
->dev_number
),
2082 (unsigned long long)le64_to_cpu(sb
->utime
) & MD_SUPERBLOCK_1_TIME_SEC_MASK
,
2083 (unsigned long long)le64_to_cpu(sb
->events
),
2084 (unsigned long long)le64_to_cpu(sb
->resync_offset
),
2085 le32_to_cpu(sb
->sb_csum
),
2086 le32_to_cpu(sb
->max_dev
)
2090 static void print_rdev(mdk_rdev_t
*rdev
, int major_version
)
2092 char b
[BDEVNAME_SIZE
];
2093 printk(KERN_INFO
"md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2094 bdevname(rdev
->bdev
, b
), (unsigned long long)rdev
->sectors
,
2095 test_bit(Faulty
, &rdev
->flags
), test_bit(In_sync
, &rdev
->flags
),
2097 if (rdev
->sb_loaded
) {
2098 printk(KERN_INFO
"md: rdev superblock (MJ:%d):\n", major_version
);
2099 switch (major_version
) {
2101 print_sb_90((mdp_super_t
*)page_address(rdev
->sb_page
));
2104 print_sb_1((struct mdp_superblock_1
*)page_address(rdev
->sb_page
));
2108 printk(KERN_INFO
"md: no rdev superblock!\n");
2111 static void md_print_devices(void)
2113 struct list_head
*tmp
;
2116 char b
[BDEVNAME_SIZE
];
2119 printk("md: **********************************\n");
2120 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2121 printk("md: **********************************\n");
2122 for_each_mddev(mddev
, tmp
) {
2125 bitmap_print_sb(mddev
->bitmap
);
2127 printk("%s: ", mdname(mddev
));
2128 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
2129 printk("<%s>", bdevname(rdev
->bdev
,b
));
2132 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
2133 print_rdev(rdev
, mddev
->major_version
);
2135 printk("md: **********************************\n");
2140 static void sync_sbs(mddev_t
* mddev
, int nospares
)
2142 /* Update each superblock (in-memory image), but
2143 * if we are allowed to, skip spares which already
2144 * have the right event counter, or have one earlier
2145 * (which would mean they aren't being marked as dirty
2146 * with the rest of the array)
2149 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2150 if (rdev
->sb_events
== mddev
->events
||
2152 rdev
->raid_disk
< 0 &&
2153 rdev
->sb_events
+1 == mddev
->events
)) {
2154 /* Don't update this superblock */
2155 rdev
->sb_loaded
= 2;
2157 super_types
[mddev
->major_version
].
2158 sync_super(mddev
, rdev
);
2159 rdev
->sb_loaded
= 1;
2164 static void md_update_sb(mddev_t
* mddev
, int force_change
)
2171 /* First make sure individual recovery_offsets are correct */
2172 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2173 if (rdev
->raid_disk
>= 0 &&
2174 mddev
->delta_disks
>= 0 &&
2175 !test_bit(In_sync
, &rdev
->flags
) &&
2176 mddev
->curr_resync_completed
> rdev
->recovery_offset
)
2177 rdev
->recovery_offset
= mddev
->curr_resync_completed
;
2180 if (!mddev
->persistent
) {
2181 clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
2182 clear_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
2183 if (!mddev
->external
)
2184 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
2185 wake_up(&mddev
->sb_wait
);
2189 spin_lock_irq(&mddev
->write_lock
);
2191 mddev
->utime
= get_seconds();
2193 if (test_and_clear_bit(MD_CHANGE_DEVS
, &mddev
->flags
))
2195 if (test_and_clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
))
2196 /* just a clean<-> dirty transition, possibly leave spares alone,
2197 * though if events isn't the right even/odd, we will have to do
2203 if (mddev
->degraded
)
2204 /* If the array is degraded, then skipping spares is both
2205 * dangerous and fairly pointless.
2206 * Dangerous because a device that was removed from the array
2207 * might have a event_count that still looks up-to-date,
2208 * so it can be re-added without a resync.
2209 * Pointless because if there are any spares to skip,
2210 * then a recovery will happen and soon that array won't
2211 * be degraded any more and the spare can go back to sleep then.
2215 sync_req
= mddev
->in_sync
;
2217 /* If this is just a dirty<->clean transition, and the array is clean
2218 * and 'events' is odd, we can roll back to the previous clean state */
2220 && (mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
2221 && mddev
->can_decrease_events
2222 && mddev
->events
!= 1) {
2224 mddev
->can_decrease_events
= 0;
2226 /* otherwise we have to go forward and ... */
2228 mddev
->can_decrease_events
= nospares
;
2231 if (!mddev
->events
) {
2233 * oops, this 64-bit counter should never wrap.
2234 * Either we are in around ~1 trillion A.C., assuming
2235 * 1 reboot per second, or we have a bug:
2240 sync_sbs(mddev
, nospares
);
2241 spin_unlock_irq(&mddev
->write_lock
);
2244 "md: updating %s RAID superblock on device (in sync %d)\n",
2245 mdname(mddev
),mddev
->in_sync
);
2247 bitmap_update_sb(mddev
->bitmap
);
2248 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2249 char b
[BDEVNAME_SIZE
];
2250 dprintk(KERN_INFO
"md: ");
2251 if (rdev
->sb_loaded
!= 1)
2252 continue; /* no noise on spare devices */
2253 if (test_bit(Faulty
, &rdev
->flags
))
2254 dprintk("(skipping faulty ");
2256 dprintk("%s ", bdevname(rdev
->bdev
,b
));
2257 if (!test_bit(Faulty
, &rdev
->flags
)) {
2258 md_super_write(mddev
,rdev
,
2259 rdev
->sb_start
, rdev
->sb_size
,
2261 dprintk(KERN_INFO
"(write) %s's sb offset: %llu\n",
2262 bdevname(rdev
->bdev
,b
),
2263 (unsigned long long)rdev
->sb_start
);
2264 rdev
->sb_events
= mddev
->events
;
2268 if (mddev
->level
== LEVEL_MULTIPATH
)
2269 /* only need to write one superblock... */
2272 md_super_wait(mddev
);
2273 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
2275 spin_lock_irq(&mddev
->write_lock
);
2276 if (mddev
->in_sync
!= sync_req
||
2277 test_bit(MD_CHANGE_DEVS
, &mddev
->flags
)) {
2278 /* have to write it out again */
2279 spin_unlock_irq(&mddev
->write_lock
);
2282 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
2283 spin_unlock_irq(&mddev
->write_lock
);
2284 wake_up(&mddev
->sb_wait
);
2285 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
2286 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
2290 /* words written to sysfs files may, or may not, be \n terminated.
2291 * We want to accept with case. For this we use cmd_match.
2293 static int cmd_match(const char *cmd
, const char *str
)
2295 /* See if cmd, written into a sysfs file, matches
2296 * str. They must either be the same, or cmd can
2297 * have a trailing newline
2299 while (*cmd
&& *str
&& *cmd
== *str
) {
2310 struct rdev_sysfs_entry
{
2311 struct attribute attr
;
2312 ssize_t (*show
)(mdk_rdev_t
*, char *);
2313 ssize_t (*store
)(mdk_rdev_t
*, const char *, size_t);
2317 state_show(mdk_rdev_t
*rdev
, char *page
)
2322 if (test_bit(Faulty
, &rdev
->flags
)) {
2323 len
+= sprintf(page
+len
, "%sfaulty",sep
);
2326 if (test_bit(In_sync
, &rdev
->flags
)) {
2327 len
+= sprintf(page
+len
, "%sin_sync",sep
);
2330 if (test_bit(WriteMostly
, &rdev
->flags
)) {
2331 len
+= sprintf(page
+len
, "%swrite_mostly",sep
);
2334 if (test_bit(Blocked
, &rdev
->flags
)) {
2335 len
+= sprintf(page
+len
, "%sblocked", sep
);
2338 if (!test_bit(Faulty
, &rdev
->flags
) &&
2339 !test_bit(In_sync
, &rdev
->flags
)) {
2340 len
+= sprintf(page
+len
, "%sspare", sep
);
2343 return len
+sprintf(page
+len
, "\n");
2347 state_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2350 * faulty - simulates and error
2351 * remove - disconnects the device
2352 * writemostly - sets write_mostly
2353 * -writemostly - clears write_mostly
2354 * blocked - sets the Blocked flag
2355 * -blocked - clears the Blocked flag
2356 * insync - sets Insync providing device isn't active
2359 if (cmd_match(buf
, "faulty") && rdev
->mddev
->pers
) {
2360 md_error(rdev
->mddev
, rdev
);
2362 } else if (cmd_match(buf
, "remove")) {
2363 if (rdev
->raid_disk
>= 0)
2366 mddev_t
*mddev
= rdev
->mddev
;
2367 kick_rdev_from_array(rdev
);
2369 md_update_sb(mddev
, 1);
2370 md_new_event(mddev
);
2373 } else if (cmd_match(buf
, "writemostly")) {
2374 set_bit(WriteMostly
, &rdev
->flags
);
2376 } else if (cmd_match(buf
, "-writemostly")) {
2377 clear_bit(WriteMostly
, &rdev
->flags
);
2379 } else if (cmd_match(buf
, "blocked")) {
2380 set_bit(Blocked
, &rdev
->flags
);
2382 } else if (cmd_match(buf
, "-blocked")) {
2383 clear_bit(Blocked
, &rdev
->flags
);
2384 wake_up(&rdev
->blocked_wait
);
2385 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2386 md_wakeup_thread(rdev
->mddev
->thread
);
2389 } else if (cmd_match(buf
, "insync") && rdev
->raid_disk
== -1) {
2390 set_bit(In_sync
, &rdev
->flags
);
2394 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2395 return err
? err
: len
;
2397 static struct rdev_sysfs_entry rdev_state
=
2398 __ATTR(state
, S_IRUGO
|S_IWUSR
, state_show
, state_store
);
2401 errors_show(mdk_rdev_t
*rdev
, char *page
)
2403 return sprintf(page
, "%d\n", atomic_read(&rdev
->corrected_errors
));
2407 errors_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2410 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2411 if (*buf
&& (*e
== 0 || *e
== '\n')) {
2412 atomic_set(&rdev
->corrected_errors
, n
);
2417 static struct rdev_sysfs_entry rdev_errors
=
2418 __ATTR(errors
, S_IRUGO
|S_IWUSR
, errors_show
, errors_store
);
2421 slot_show(mdk_rdev_t
*rdev
, char *page
)
2423 if (rdev
->raid_disk
< 0)
2424 return sprintf(page
, "none\n");
2426 return sprintf(page
, "%d\n", rdev
->raid_disk
);
2430 slot_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2435 int slot
= simple_strtoul(buf
, &e
, 10);
2436 if (strncmp(buf
, "none", 4)==0)
2438 else if (e
==buf
|| (*e
&& *e
!= '\n'))
2440 if (rdev
->mddev
->pers
&& slot
== -1) {
2441 /* Setting 'slot' on an active array requires also
2442 * updating the 'rd%d' link, and communicating
2443 * with the personality with ->hot_*_disk.
2444 * For now we only support removing
2445 * failed/spare devices. This normally happens automatically,
2446 * but not when the metadata is externally managed.
2448 if (rdev
->raid_disk
== -1)
2450 /* personality does all needed checks */
2451 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
2453 err
= rdev
->mddev
->pers
->
2454 hot_remove_disk(rdev
->mddev
, rdev
->raid_disk
);
2457 sprintf(nm
, "rd%d", rdev
->raid_disk
);
2458 sysfs_remove_link(&rdev
->mddev
->kobj
, nm
);
2459 rdev
->raid_disk
= -1;
2460 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2461 md_wakeup_thread(rdev
->mddev
->thread
);
2462 } else if (rdev
->mddev
->pers
) {
2464 /* Activating a spare .. or possibly reactivating
2465 * if we ever get bitmaps working here.
2468 if (rdev
->raid_disk
!= -1)
2471 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
2474 list_for_each_entry(rdev2
, &rdev
->mddev
->disks
, same_set
)
2475 if (rdev2
->raid_disk
== slot
)
2478 rdev
->raid_disk
= slot
;
2479 if (test_bit(In_sync
, &rdev
->flags
))
2480 rdev
->saved_raid_disk
= slot
;
2482 rdev
->saved_raid_disk
= -1;
2483 err
= rdev
->mddev
->pers
->
2484 hot_add_disk(rdev
->mddev
, rdev
);
2486 rdev
->raid_disk
= -1;
2489 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2490 sprintf(nm
, "rd%d", rdev
->raid_disk
);
2491 if (sysfs_create_link(&rdev
->mddev
->kobj
, &rdev
->kobj
, nm
))
2492 /* failure here is OK */;
2493 /* don't wakeup anyone, leave that to userspace. */
2495 if (slot
>= rdev
->mddev
->raid_disks
)
2497 rdev
->raid_disk
= slot
;
2498 /* assume it is working */
2499 clear_bit(Faulty
, &rdev
->flags
);
2500 clear_bit(WriteMostly
, &rdev
->flags
);
2501 set_bit(In_sync
, &rdev
->flags
);
2502 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2508 static struct rdev_sysfs_entry rdev_slot
=
2509 __ATTR(slot
, S_IRUGO
|S_IWUSR
, slot_show
, slot_store
);
2512 offset_show(mdk_rdev_t
*rdev
, char *page
)
2514 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->data_offset
);
2518 offset_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2521 unsigned long long offset
= simple_strtoull(buf
, &e
, 10);
2522 if (e
==buf
|| (*e
&& *e
!= '\n'))
2524 if (rdev
->mddev
->pers
&& rdev
->raid_disk
>= 0)
2526 if (rdev
->sectors
&& rdev
->mddev
->external
)
2527 /* Must set offset before size, so overlap checks
2530 rdev
->data_offset
= offset
;
2534 static struct rdev_sysfs_entry rdev_offset
=
2535 __ATTR(offset
, S_IRUGO
|S_IWUSR
, offset_show
, offset_store
);
2538 rdev_size_show(mdk_rdev_t
*rdev
, char *page
)
2540 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->sectors
/ 2);
2543 static int overlaps(sector_t s1
, sector_t l1
, sector_t s2
, sector_t l2
)
2545 /* check if two start/length pairs overlap */
2553 static int strict_blocks_to_sectors(const char *buf
, sector_t
*sectors
)
2555 unsigned long long blocks
;
2558 if (strict_strtoull(buf
, 10, &blocks
) < 0)
2561 if (blocks
& 1ULL << (8 * sizeof(blocks
) - 1))
2562 return -EINVAL
; /* sector conversion overflow */
2565 if (new != blocks
* 2)
2566 return -EINVAL
; /* unsigned long long to sector_t overflow */
2573 rdev_size_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2575 mddev_t
*my_mddev
= rdev
->mddev
;
2576 sector_t oldsectors
= rdev
->sectors
;
2579 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
2581 if (my_mddev
->pers
&& rdev
->raid_disk
>= 0) {
2582 if (my_mddev
->persistent
) {
2583 sectors
= super_types
[my_mddev
->major_version
].
2584 rdev_size_change(rdev
, sectors
);
2587 } else if (!sectors
)
2588 sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) -
2591 if (sectors
< my_mddev
->dev_sectors
)
2592 return -EINVAL
; /* component must fit device */
2594 rdev
->sectors
= sectors
;
2595 if (sectors
> oldsectors
&& my_mddev
->external
) {
2596 /* need to check that all other rdevs with the same ->bdev
2597 * do not overlap. We need to unlock the mddev to avoid
2598 * a deadlock. We have already changed rdev->sectors, and if
2599 * we have to change it back, we will have the lock again.
2603 struct list_head
*tmp
;
2605 mddev_unlock(my_mddev
);
2606 for_each_mddev(mddev
, tmp
) {
2610 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
)
2611 if (test_bit(AllReserved
, &rdev2
->flags
) ||
2612 (rdev
->bdev
== rdev2
->bdev
&&
2614 overlaps(rdev
->data_offset
, rdev
->sectors
,
2620 mddev_unlock(mddev
);
2626 mddev_lock(my_mddev
);
2628 /* Someone else could have slipped in a size
2629 * change here, but doing so is just silly.
2630 * We put oldsectors back because we *know* it is
2631 * safe, and trust userspace not to race with
2634 rdev
->sectors
= oldsectors
;
2641 static struct rdev_sysfs_entry rdev_size
=
2642 __ATTR(size
, S_IRUGO
|S_IWUSR
, rdev_size_show
, rdev_size_store
);
2645 static ssize_t
recovery_start_show(mdk_rdev_t
*rdev
, char *page
)
2647 unsigned long long recovery_start
= rdev
->recovery_offset
;
2649 if (test_bit(In_sync
, &rdev
->flags
) ||
2650 recovery_start
== MaxSector
)
2651 return sprintf(page
, "none\n");
2653 return sprintf(page
, "%llu\n", recovery_start
);
2656 static ssize_t
recovery_start_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2658 unsigned long long recovery_start
;
2660 if (cmd_match(buf
, "none"))
2661 recovery_start
= MaxSector
;
2662 else if (strict_strtoull(buf
, 10, &recovery_start
))
2665 if (rdev
->mddev
->pers
&&
2666 rdev
->raid_disk
>= 0)
2669 rdev
->recovery_offset
= recovery_start
;
2670 if (recovery_start
== MaxSector
)
2671 set_bit(In_sync
, &rdev
->flags
);
2673 clear_bit(In_sync
, &rdev
->flags
);
2677 static struct rdev_sysfs_entry rdev_recovery_start
=
2678 __ATTR(recovery_start
, S_IRUGO
|S_IWUSR
, recovery_start_show
, recovery_start_store
);
2680 static struct attribute
*rdev_default_attrs
[] = {
2686 &rdev_recovery_start
.attr
,
2690 rdev_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
2692 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
2693 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
2694 mddev_t
*mddev
= rdev
->mddev
;
2700 rv
= mddev
? mddev_lock(mddev
) : -EBUSY
;
2702 if (rdev
->mddev
== NULL
)
2705 rv
= entry
->show(rdev
, page
);
2706 mddev_unlock(mddev
);
2712 rdev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
2713 const char *page
, size_t length
)
2715 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
2716 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
2718 mddev_t
*mddev
= rdev
->mddev
;
2722 if (!capable(CAP_SYS_ADMIN
))
2724 rv
= mddev
? mddev_lock(mddev
): -EBUSY
;
2726 if (rdev
->mddev
== NULL
)
2729 rv
= entry
->store(rdev
, page
, length
);
2730 mddev_unlock(mddev
);
2735 static void rdev_free(struct kobject
*ko
)
2737 mdk_rdev_t
*rdev
= container_of(ko
, mdk_rdev_t
, kobj
);
2740 static const struct sysfs_ops rdev_sysfs_ops
= {
2741 .show
= rdev_attr_show
,
2742 .store
= rdev_attr_store
,
2744 static struct kobj_type rdev_ktype
= {
2745 .release
= rdev_free
,
2746 .sysfs_ops
= &rdev_sysfs_ops
,
2747 .default_attrs
= rdev_default_attrs
,
2750 void md_rdev_init(mdk_rdev_t
*rdev
)
2753 rdev
->saved_raid_disk
= -1;
2754 rdev
->raid_disk
= -1;
2756 rdev
->data_offset
= 0;
2757 rdev
->sb_events
= 0;
2758 rdev
->last_read_error
.tv_sec
= 0;
2759 rdev
->last_read_error
.tv_nsec
= 0;
2760 atomic_set(&rdev
->nr_pending
, 0);
2761 atomic_set(&rdev
->read_errors
, 0);
2762 atomic_set(&rdev
->corrected_errors
, 0);
2764 INIT_LIST_HEAD(&rdev
->same_set
);
2765 init_waitqueue_head(&rdev
->blocked_wait
);
2767 EXPORT_SYMBOL_GPL(md_rdev_init
);
2769 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2771 * mark the device faulty if:
2773 * - the device is nonexistent (zero size)
2774 * - the device has no valid superblock
2776 * a faulty rdev _never_ has rdev->sb set.
2778 static mdk_rdev_t
*md_import_device(dev_t newdev
, int super_format
, int super_minor
)
2780 char b
[BDEVNAME_SIZE
];
2785 rdev
= kzalloc(sizeof(*rdev
), GFP_KERNEL
);
2787 printk(KERN_ERR
"md: could not alloc mem for new device!\n");
2788 return ERR_PTR(-ENOMEM
);
2792 if ((err
= alloc_disk_sb(rdev
)))
2795 err
= lock_rdev(rdev
, newdev
, super_format
== -2);
2799 kobject_init(&rdev
->kobj
, &rdev_ktype
);
2801 size
= i_size_read(rdev
->bdev
->bd_inode
) >> BLOCK_SIZE_BITS
;
2804 "md: %s has zero or unknown size, marking faulty!\n",
2805 bdevname(rdev
->bdev
,b
));
2810 if (super_format
>= 0) {
2811 err
= super_types
[super_format
].
2812 load_super(rdev
, NULL
, super_minor
);
2813 if (err
== -EINVAL
) {
2815 "md: %s does not have a valid v%d.%d "
2816 "superblock, not importing!\n",
2817 bdevname(rdev
->bdev
,b
),
2818 super_format
, super_minor
);
2823 "md: could not read %s's sb, not importing!\n",
2824 bdevname(rdev
->bdev
,b
));
2832 if (rdev
->sb_page
) {
2838 return ERR_PTR(err
);
2842 * Check a full RAID array for plausibility
2846 static void analyze_sbs(mddev_t
* mddev
)
2849 mdk_rdev_t
*rdev
, *freshest
, *tmp
;
2850 char b
[BDEVNAME_SIZE
];
2853 rdev_for_each(rdev
, tmp
, mddev
)
2854 switch (super_types
[mddev
->major_version
].
2855 load_super(rdev
, freshest
, mddev
->minor_version
)) {
2863 "md: fatal superblock inconsistency in %s"
2864 " -- removing from array\n",
2865 bdevname(rdev
->bdev
,b
));
2866 kick_rdev_from_array(rdev
);
2870 super_types
[mddev
->major_version
].
2871 validate_super(mddev
, freshest
);
2874 rdev_for_each(rdev
, tmp
, mddev
) {
2875 if (mddev
->max_disks
&&
2876 (rdev
->desc_nr
>= mddev
->max_disks
||
2877 i
> mddev
->max_disks
)) {
2879 "md: %s: %s: only %d devices permitted\n",
2880 mdname(mddev
), bdevname(rdev
->bdev
, b
),
2882 kick_rdev_from_array(rdev
);
2885 if (rdev
!= freshest
)
2886 if (super_types
[mddev
->major_version
].
2887 validate_super(mddev
, rdev
)) {
2888 printk(KERN_WARNING
"md: kicking non-fresh %s"
2890 bdevname(rdev
->bdev
,b
));
2891 kick_rdev_from_array(rdev
);
2894 if (mddev
->level
== LEVEL_MULTIPATH
) {
2895 rdev
->desc_nr
= i
++;
2896 rdev
->raid_disk
= rdev
->desc_nr
;
2897 set_bit(In_sync
, &rdev
->flags
);
2898 } else if (rdev
->raid_disk
>= (mddev
->raid_disks
- min(0, mddev
->delta_disks
))) {
2899 rdev
->raid_disk
= -1;
2900 clear_bit(In_sync
, &rdev
->flags
);
2905 /* Read a fixed-point number.
2906 * Numbers in sysfs attributes should be in "standard" units where
2907 * possible, so time should be in seconds.
2908 * However we internally use a a much smaller unit such as
2909 * milliseconds or jiffies.
2910 * This function takes a decimal number with a possible fractional
2911 * component, and produces an integer which is the result of
2912 * multiplying that number by 10^'scale'.
2913 * all without any floating-point arithmetic.
2915 int strict_strtoul_scaled(const char *cp
, unsigned long *res
, int scale
)
2917 unsigned long result
= 0;
2919 while (isdigit(*cp
) || (*cp
== '.' && decimals
< 0)) {
2922 else if (decimals
< scale
) {
2925 result
= result
* 10 + value
;
2937 while (decimals
< scale
) {
2946 static void md_safemode_timeout(unsigned long data
);
2949 safe_delay_show(mddev_t
*mddev
, char *page
)
2951 int msec
= (mddev
->safemode_delay
*1000)/HZ
;
2952 return sprintf(page
, "%d.%03d\n", msec
/1000, msec
%1000);
2955 safe_delay_store(mddev_t
*mddev
, const char *cbuf
, size_t len
)
2959 if (strict_strtoul_scaled(cbuf
, &msec
, 3) < 0)
2962 mddev
->safemode_delay
= 0;
2964 unsigned long old_delay
= mddev
->safemode_delay
;
2965 mddev
->safemode_delay
= (msec
*HZ
)/1000;
2966 if (mddev
->safemode_delay
== 0)
2967 mddev
->safemode_delay
= 1;
2968 if (mddev
->safemode_delay
< old_delay
)
2969 md_safemode_timeout((unsigned long)mddev
);
2973 static struct md_sysfs_entry md_safe_delay
=
2974 __ATTR(safe_mode_delay
, S_IRUGO
|S_IWUSR
,safe_delay_show
, safe_delay_store
);
2977 level_show(mddev_t
*mddev
, char *page
)
2979 struct mdk_personality
*p
= mddev
->pers
;
2981 return sprintf(page
, "%s\n", p
->name
);
2982 else if (mddev
->clevel
[0])
2983 return sprintf(page
, "%s\n", mddev
->clevel
);
2984 else if (mddev
->level
!= LEVEL_NONE
)
2985 return sprintf(page
, "%d\n", mddev
->level
);
2991 level_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2995 struct mdk_personality
*pers
;
3000 if (mddev
->pers
== NULL
) {
3003 if (len
>= sizeof(mddev
->clevel
))
3005 strncpy(mddev
->clevel
, buf
, len
);
3006 if (mddev
->clevel
[len
-1] == '\n')
3008 mddev
->clevel
[len
] = 0;
3009 mddev
->level
= LEVEL_NONE
;
3013 /* request to change the personality. Need to ensure:
3014 * - array is not engaged in resync/recovery/reshape
3015 * - old personality can be suspended
3016 * - new personality will access other array.
3019 if (mddev
->sync_thread
||
3020 mddev
->reshape_position
!= MaxSector
||
3021 mddev
->sysfs_active
)
3024 if (!mddev
->pers
->quiesce
) {
3025 printk(KERN_WARNING
"md: %s: %s does not support online personality change\n",
3026 mdname(mddev
), mddev
->pers
->name
);
3030 /* Now find the new personality */
3031 if (len
== 0 || len
>= sizeof(clevel
))
3033 strncpy(clevel
, buf
, len
);
3034 if (clevel
[len
-1] == '\n')
3037 if (strict_strtol(clevel
, 10, &level
))
3040 if (request_module("md-%s", clevel
) != 0)
3041 request_module("md-level-%s", clevel
);
3042 spin_lock(&pers_lock
);
3043 pers
= find_pers(level
, clevel
);
3044 if (!pers
|| !try_module_get(pers
->owner
)) {
3045 spin_unlock(&pers_lock
);
3046 printk(KERN_WARNING
"md: personality %s not loaded\n", clevel
);
3049 spin_unlock(&pers_lock
);
3051 if (pers
== mddev
->pers
) {
3052 /* Nothing to do! */
3053 module_put(pers
->owner
);
3056 if (!pers
->takeover
) {
3057 module_put(pers
->owner
);
3058 printk(KERN_WARNING
"md: %s: %s does not support personality takeover\n",
3059 mdname(mddev
), clevel
);
3063 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
3064 rdev
->new_raid_disk
= rdev
->raid_disk
;
3066 /* ->takeover must set new_* and/or delta_disks
3067 * if it succeeds, and may set them when it fails.
3069 priv
= pers
->takeover(mddev
);
3071 mddev
->new_level
= mddev
->level
;
3072 mddev
->new_layout
= mddev
->layout
;
3073 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3074 mddev
->raid_disks
-= mddev
->delta_disks
;
3075 mddev
->delta_disks
= 0;
3076 module_put(pers
->owner
);
3077 printk(KERN_WARNING
"md: %s: %s would not accept array\n",
3078 mdname(mddev
), clevel
);
3079 return PTR_ERR(priv
);
3082 /* Looks like we have a winner */
3083 mddev_suspend(mddev
);
3084 mddev
->pers
->stop(mddev
);
3086 if (mddev
->pers
->sync_request
== NULL
&&
3087 pers
->sync_request
!= NULL
) {
3088 /* need to add the md_redundancy_group */
3089 if (sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
3091 "md: cannot register extra attributes for %s\n",
3093 mddev
->sysfs_action
= sysfs_get_dirent(mddev
->kobj
.sd
, NULL
, "sync_action");
3095 if (mddev
->pers
->sync_request
!= NULL
&&
3096 pers
->sync_request
== NULL
) {
3097 /* need to remove the md_redundancy_group */
3098 if (mddev
->to_remove
== NULL
)
3099 mddev
->to_remove
= &md_redundancy_group
;
3102 if (mddev
->pers
->sync_request
== NULL
&&
3104 /* We are converting from a no-redundancy array
3105 * to a redundancy array and metadata is managed
3106 * externally so we need to be sure that writes
3107 * won't block due to a need to transition
3109 * until external management is started.
3112 mddev
->safemode_delay
= 0;
3113 mddev
->safemode
= 0;
3116 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
3118 if (rdev
->raid_disk
< 0)
3120 if (rdev
->new_raid_disk
> mddev
->raid_disks
)
3121 rdev
->new_raid_disk
= -1;
3122 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3124 sprintf(nm
, "rd%d", rdev
->raid_disk
);
3125 sysfs_remove_link(&mddev
->kobj
, nm
);
3127 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
3128 if (rdev
->raid_disk
< 0)
3130 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3132 rdev
->raid_disk
= rdev
->new_raid_disk
;
3133 if (rdev
->raid_disk
< 0)
3134 clear_bit(In_sync
, &rdev
->flags
);
3137 sprintf(nm
, "rd%d", rdev
->raid_disk
);
3138 if(sysfs_create_link(&mddev
->kobj
, &rdev
->kobj
, nm
))
3139 printk("md: cannot register %s for %s after level change\n",
3144 module_put(mddev
->pers
->owner
);
3146 mddev
->private = priv
;
3147 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
3148 mddev
->level
= mddev
->new_level
;
3149 mddev
->layout
= mddev
->new_layout
;
3150 mddev
->chunk_sectors
= mddev
->new_chunk_sectors
;
3151 mddev
->delta_disks
= 0;
3152 if (mddev
->pers
->sync_request
== NULL
) {
3153 /* this is now an array without redundancy, so
3154 * it must always be in_sync
3157 del_timer_sync(&mddev
->safemode_timer
);
3160 mddev_resume(mddev
);
3161 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
3162 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3163 md_wakeup_thread(mddev
->thread
);
3164 sysfs_notify(&mddev
->kobj
, NULL
, "level");
3165 md_new_event(mddev
);
3169 static struct md_sysfs_entry md_level
=
3170 __ATTR(level
, S_IRUGO
|S_IWUSR
, level_show
, level_store
);
3174 layout_show(mddev_t
*mddev
, char *page
)
3176 /* just a number, not meaningful for all levels */
3177 if (mddev
->reshape_position
!= MaxSector
&&
3178 mddev
->layout
!= mddev
->new_layout
)
3179 return sprintf(page
, "%d (%d)\n",
3180 mddev
->new_layout
, mddev
->layout
);
3181 return sprintf(page
, "%d\n", mddev
->layout
);
3185 layout_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3188 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3190 if (!*buf
|| (*e
&& *e
!= '\n'))
3195 if (mddev
->pers
->check_reshape
== NULL
)
3197 mddev
->new_layout
= n
;
3198 err
= mddev
->pers
->check_reshape(mddev
);
3200 mddev
->new_layout
= mddev
->layout
;
3204 mddev
->new_layout
= n
;
3205 if (mddev
->reshape_position
== MaxSector
)
3210 static struct md_sysfs_entry md_layout
=
3211 __ATTR(layout
, S_IRUGO
|S_IWUSR
, layout_show
, layout_store
);
3215 raid_disks_show(mddev_t
*mddev
, char *page
)
3217 if (mddev
->raid_disks
== 0)
3219 if (mddev
->reshape_position
!= MaxSector
&&
3220 mddev
->delta_disks
!= 0)
3221 return sprintf(page
, "%d (%d)\n", mddev
->raid_disks
,
3222 mddev
->raid_disks
- mddev
->delta_disks
);
3223 return sprintf(page
, "%d\n", mddev
->raid_disks
);
3226 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
);
3229 raid_disks_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3233 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3235 if (!*buf
|| (*e
&& *e
!= '\n'))
3239 rv
= update_raid_disks(mddev
, n
);
3240 else if (mddev
->reshape_position
!= MaxSector
) {
3241 int olddisks
= mddev
->raid_disks
- mddev
->delta_disks
;
3242 mddev
->delta_disks
= n
- olddisks
;
3243 mddev
->raid_disks
= n
;
3245 mddev
->raid_disks
= n
;
3246 return rv
? rv
: len
;
3248 static struct md_sysfs_entry md_raid_disks
=
3249 __ATTR(raid_disks
, S_IRUGO
|S_IWUSR
, raid_disks_show
, raid_disks_store
);
3252 chunk_size_show(mddev_t
*mddev
, char *page
)
3254 if (mddev
->reshape_position
!= MaxSector
&&
3255 mddev
->chunk_sectors
!= mddev
->new_chunk_sectors
)
3256 return sprintf(page
, "%d (%d)\n",
3257 mddev
->new_chunk_sectors
<< 9,
3258 mddev
->chunk_sectors
<< 9);
3259 return sprintf(page
, "%d\n", mddev
->chunk_sectors
<< 9);
3263 chunk_size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3266 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3268 if (!*buf
|| (*e
&& *e
!= '\n'))
3273 if (mddev
->pers
->check_reshape
== NULL
)
3275 mddev
->new_chunk_sectors
= n
>> 9;
3276 err
= mddev
->pers
->check_reshape(mddev
);
3278 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3282 mddev
->new_chunk_sectors
= n
>> 9;
3283 if (mddev
->reshape_position
== MaxSector
)
3284 mddev
->chunk_sectors
= n
>> 9;
3288 static struct md_sysfs_entry md_chunk_size
=
3289 __ATTR(chunk_size
, S_IRUGO
|S_IWUSR
, chunk_size_show
, chunk_size_store
);
3292 resync_start_show(mddev_t
*mddev
, char *page
)
3294 if (mddev
->recovery_cp
== MaxSector
)
3295 return sprintf(page
, "none\n");
3296 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->recovery_cp
);
3300 resync_start_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3303 unsigned long long n
= simple_strtoull(buf
, &e
, 10);
3307 if (cmd_match(buf
, "none"))
3309 else if (!*buf
|| (*e
&& *e
!= '\n'))
3312 mddev
->recovery_cp
= n
;
3315 static struct md_sysfs_entry md_resync_start
=
3316 __ATTR(resync_start
, S_IRUGO
|S_IWUSR
, resync_start_show
, resync_start_store
);
3319 * The array state can be:
3322 * No devices, no size, no level
3323 * Equivalent to STOP_ARRAY ioctl
3325 * May have some settings, but array is not active
3326 * all IO results in error
3327 * When written, doesn't tear down array, but just stops it
3328 * suspended (not supported yet)
3329 * All IO requests will block. The array can be reconfigured.
3330 * Writing this, if accepted, will block until array is quiescent
3332 * no resync can happen. no superblocks get written.
3333 * write requests fail
3335 * like readonly, but behaves like 'clean' on a write request.
3337 * clean - no pending writes, but otherwise active.
3338 * When written to inactive array, starts without resync
3339 * If a write request arrives then
3340 * if metadata is known, mark 'dirty' and switch to 'active'.
3341 * if not known, block and switch to write-pending
3342 * If written to an active array that has pending writes, then fails.
3344 * fully active: IO and resync can be happening.
3345 * When written to inactive array, starts with resync
3348 * clean, but writes are blocked waiting for 'active' to be written.
3351 * like active, but no writes have been seen for a while (100msec).
3354 enum array_state
{ clear
, inactive
, suspended
, readonly
, read_auto
, clean
, active
,
3355 write_pending
, active_idle
, bad_word
};
3356 static char *array_states
[] = {
3357 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3358 "write-pending", "active-idle", NULL
};
3360 static int match_word(const char *word
, char **list
)
3363 for (n
=0; list
[n
]; n
++)
3364 if (cmd_match(word
, list
[n
]))
3370 array_state_show(mddev_t
*mddev
, char *page
)
3372 enum array_state st
= inactive
;
3385 else if (test_bit(MD_CHANGE_PENDING
, &mddev
->flags
))
3387 else if (mddev
->safemode
)
3393 if (list_empty(&mddev
->disks
) &&
3394 mddev
->raid_disks
== 0 &&
3395 mddev
->dev_sectors
== 0)
3400 return sprintf(page
, "%s\n", array_states
[st
]);
3403 static int do_md_stop(mddev_t
* mddev
, int ro
, int is_open
);
3404 static int md_set_readonly(mddev_t
* mddev
, int is_open
);
3405 static int do_md_run(mddev_t
* mddev
);
3406 static int restart_array(mddev_t
*mddev
);
3409 array_state_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3412 enum array_state st
= match_word(buf
, array_states
);
3417 /* stopping an active array */
3418 if (atomic_read(&mddev
->openers
) > 0)
3420 err
= do_md_stop(mddev
, 0, 0);
3423 /* stopping an active array */
3425 if (atomic_read(&mddev
->openers
) > 0)
3427 err
= do_md_stop(mddev
, 2, 0);
3429 err
= 0; /* already inactive */
3432 break; /* not supported yet */
3435 err
= md_set_readonly(mddev
, 0);
3438 set_disk_ro(mddev
->gendisk
, 1);
3439 err
= do_md_run(mddev
);
3445 err
= md_set_readonly(mddev
, 0);
3446 else if (mddev
->ro
== 1)
3447 err
= restart_array(mddev
);
3450 set_disk_ro(mddev
->gendisk
, 0);
3454 err
= do_md_run(mddev
);
3459 restart_array(mddev
);
3460 spin_lock_irq(&mddev
->write_lock
);
3461 if (atomic_read(&mddev
->writes_pending
) == 0) {
3462 if (mddev
->in_sync
== 0) {
3464 if (mddev
->safemode
== 1)
3465 mddev
->safemode
= 0;
3466 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
3471 spin_unlock_irq(&mddev
->write_lock
);
3477 restart_array(mddev
);
3478 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
3479 wake_up(&mddev
->sb_wait
);
3483 set_disk_ro(mddev
->gendisk
, 0);
3484 err
= do_md_run(mddev
);
3489 /* these cannot be set */
3495 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
3499 static struct md_sysfs_entry md_array_state
=
3500 __ATTR(array_state
, S_IRUGO
|S_IWUSR
, array_state_show
, array_state_store
);
3503 max_corrected_read_errors_show(mddev_t
*mddev
, char *page
) {
3504 return sprintf(page
, "%d\n",
3505 atomic_read(&mddev
->max_corr_read_errors
));
3509 max_corrected_read_errors_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3512 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3514 if (*buf
&& (*e
== 0 || *e
== '\n')) {
3515 atomic_set(&mddev
->max_corr_read_errors
, n
);
3521 static struct md_sysfs_entry max_corr_read_errors
=
3522 __ATTR(max_read_errors
, S_IRUGO
|S_IWUSR
, max_corrected_read_errors_show
,
3523 max_corrected_read_errors_store
);
3526 null_show(mddev_t
*mddev
, char *page
)
3532 new_dev_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3534 /* buf must be %d:%d\n? giving major and minor numbers */
3535 /* The new device is added to the array.
3536 * If the array has a persistent superblock, we read the
3537 * superblock to initialise info and check validity.
3538 * Otherwise, only checking done is that in bind_rdev_to_array,
3539 * which mainly checks size.
3542 int major
= simple_strtoul(buf
, &e
, 10);
3548 if (!*buf
|| *e
!= ':' || !e
[1] || e
[1] == '\n')
3550 minor
= simple_strtoul(e
+1, &e
, 10);
3551 if (*e
&& *e
!= '\n')
3553 dev
= MKDEV(major
, minor
);
3554 if (major
!= MAJOR(dev
) ||
3555 minor
!= MINOR(dev
))
3559 if (mddev
->persistent
) {
3560 rdev
= md_import_device(dev
, mddev
->major_version
,
3561 mddev
->minor_version
);
3562 if (!IS_ERR(rdev
) && !list_empty(&mddev
->disks
)) {
3563 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
3564 mdk_rdev_t
, same_set
);
3565 err
= super_types
[mddev
->major_version
]
3566 .load_super(rdev
, rdev0
, mddev
->minor_version
);
3570 } else if (mddev
->external
)
3571 rdev
= md_import_device(dev
, -2, -1);
3573 rdev
= md_import_device(dev
, -1, -1);
3576 return PTR_ERR(rdev
);
3577 err
= bind_rdev_to_array(rdev
, mddev
);
3581 return err
? err
: len
;
3584 static struct md_sysfs_entry md_new_device
=
3585 __ATTR(new_dev
, S_IWUSR
, null_show
, new_dev_store
);
3588 bitmap_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3591 unsigned long chunk
, end_chunk
;
3595 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3597 chunk
= end_chunk
= simple_strtoul(buf
, &end
, 0);
3598 if (buf
== end
) break;
3599 if (*end
== '-') { /* range */
3601 end_chunk
= simple_strtoul(buf
, &end
, 0);
3602 if (buf
== end
) break;
3604 if (*end
&& !isspace(*end
)) break;
3605 bitmap_dirty_bits(mddev
->bitmap
, chunk
, end_chunk
);
3606 buf
= skip_spaces(end
);
3608 bitmap_unplug(mddev
->bitmap
); /* flush the bits to disk */
3613 static struct md_sysfs_entry md_bitmap
=
3614 __ATTR(bitmap_set_bits
, S_IWUSR
, null_show
, bitmap_store
);
3617 size_show(mddev_t
*mddev
, char *page
)
3619 return sprintf(page
, "%llu\n",
3620 (unsigned long long)mddev
->dev_sectors
/ 2);
3623 static int update_size(mddev_t
*mddev
, sector_t num_sectors
);
3626 size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3628 /* If array is inactive, we can reduce the component size, but
3629 * not increase it (except from 0).
3630 * If array is active, we can try an on-line resize
3633 int err
= strict_blocks_to_sectors(buf
, §ors
);
3638 err
= update_size(mddev
, sectors
);
3639 md_update_sb(mddev
, 1);
3641 if (mddev
->dev_sectors
== 0 ||
3642 mddev
->dev_sectors
> sectors
)
3643 mddev
->dev_sectors
= sectors
;
3647 return err
? err
: len
;
3650 static struct md_sysfs_entry md_size
=
3651 __ATTR(component_size
, S_IRUGO
|S_IWUSR
, size_show
, size_store
);
3656 * 'none' for arrays with no metadata (good luck...)
3657 * 'external' for arrays with externally managed metadata,
3658 * or N.M for internally known formats
3661 metadata_show(mddev_t
*mddev
, char *page
)
3663 if (mddev
->persistent
)
3664 return sprintf(page
, "%d.%d\n",
3665 mddev
->major_version
, mddev
->minor_version
);
3666 else if (mddev
->external
)
3667 return sprintf(page
, "external:%s\n", mddev
->metadata_type
);
3669 return sprintf(page
, "none\n");
3673 metadata_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3677 /* Changing the details of 'external' metadata is
3678 * always permitted. Otherwise there must be
3679 * no devices attached to the array.
3681 if (mddev
->external
&& strncmp(buf
, "external:", 9) == 0)
3683 else if (!list_empty(&mddev
->disks
))
3686 if (cmd_match(buf
, "none")) {
3687 mddev
->persistent
= 0;
3688 mddev
->external
= 0;
3689 mddev
->major_version
= 0;
3690 mddev
->minor_version
= 90;
3693 if (strncmp(buf
, "external:", 9) == 0) {
3694 size_t namelen
= len
-9;
3695 if (namelen
>= sizeof(mddev
->metadata_type
))
3696 namelen
= sizeof(mddev
->metadata_type
)-1;
3697 strncpy(mddev
->metadata_type
, buf
+9, namelen
);
3698 mddev
->metadata_type
[namelen
] = 0;
3699 if (namelen
&& mddev
->metadata_type
[namelen
-1] == '\n')
3700 mddev
->metadata_type
[--namelen
] = 0;
3701 mddev
->persistent
= 0;
3702 mddev
->external
= 1;
3703 mddev
->major_version
= 0;
3704 mddev
->minor_version
= 90;
3707 major
= simple_strtoul(buf
, &e
, 10);
3708 if (e
==buf
|| *e
!= '.')
3711 minor
= simple_strtoul(buf
, &e
, 10);
3712 if (e
==buf
|| (*e
&& *e
!= '\n') )
3714 if (major
>= ARRAY_SIZE(super_types
) || super_types
[major
].name
== NULL
)
3716 mddev
->major_version
= major
;
3717 mddev
->minor_version
= minor
;
3718 mddev
->persistent
= 1;
3719 mddev
->external
= 0;
3723 static struct md_sysfs_entry md_metadata
=
3724 __ATTR(metadata_version
, S_IRUGO
|S_IWUSR
, metadata_show
, metadata_store
);
3727 action_show(mddev_t
*mddev
, char *page
)
3729 char *type
= "idle";
3730 if (test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
3732 else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
3733 (!mddev
->ro
&& test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))) {
3734 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
3736 else if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
3737 if (!test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
3739 else if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
3743 } else if (test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
))
3746 return sprintf(page
, "%s\n", type
);
3749 static void reap_sync_thread(mddev_t
*mddev
);
3752 action_store(mddev_t
*mddev
, const char *page
, size_t len
)
3754 if (!mddev
->pers
|| !mddev
->pers
->sync_request
)
3757 if (cmd_match(page
, "frozen"))
3758 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
3760 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
3762 if (cmd_match(page
, "idle") || cmd_match(page
, "frozen")) {
3763 if (mddev
->sync_thread
) {
3764 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
3765 reap_sync_thread(mddev
);
3767 } else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
3768 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
3770 else if (cmd_match(page
, "resync"))
3771 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3772 else if (cmd_match(page
, "recover")) {
3773 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
3774 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3775 } else if (cmd_match(page
, "reshape")) {
3777 if (mddev
->pers
->start_reshape
== NULL
)
3779 err
= mddev
->pers
->start_reshape(mddev
);
3782 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
3784 if (cmd_match(page
, "check"))
3785 set_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
3786 else if (!cmd_match(page
, "repair"))
3788 set_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
3789 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
3791 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3792 md_wakeup_thread(mddev
->thread
);
3793 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
3798 mismatch_cnt_show(mddev_t
*mddev
, char *page
)
3800 return sprintf(page
, "%llu\n",
3801 (unsigned long long) mddev
->resync_mismatches
);
3804 static struct md_sysfs_entry md_scan_mode
=
3805 __ATTR(sync_action
, S_IRUGO
|S_IWUSR
, action_show
, action_store
);
3808 static struct md_sysfs_entry md_mismatches
= __ATTR_RO(mismatch_cnt
);
3811 sync_min_show(mddev_t
*mddev
, char *page
)
3813 return sprintf(page
, "%d (%s)\n", speed_min(mddev
),
3814 mddev
->sync_speed_min
? "local": "system");
3818 sync_min_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3822 if (strncmp(buf
, "system", 6)==0) {
3823 mddev
->sync_speed_min
= 0;
3826 min
= simple_strtoul(buf
, &e
, 10);
3827 if (buf
== e
|| (*e
&& *e
!= '\n') || min
<= 0)
3829 mddev
->sync_speed_min
= min
;
3833 static struct md_sysfs_entry md_sync_min
=
3834 __ATTR(sync_speed_min
, S_IRUGO
|S_IWUSR
, sync_min_show
, sync_min_store
);
3837 sync_max_show(mddev_t
*mddev
, char *page
)
3839 return sprintf(page
, "%d (%s)\n", speed_max(mddev
),
3840 mddev
->sync_speed_max
? "local": "system");
3844 sync_max_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3848 if (strncmp(buf
, "system", 6)==0) {
3849 mddev
->sync_speed_max
= 0;
3852 max
= simple_strtoul(buf
, &e
, 10);
3853 if (buf
== e
|| (*e
&& *e
!= '\n') || max
<= 0)
3855 mddev
->sync_speed_max
= max
;
3859 static struct md_sysfs_entry md_sync_max
=
3860 __ATTR(sync_speed_max
, S_IRUGO
|S_IWUSR
, sync_max_show
, sync_max_store
);
3863 degraded_show(mddev_t
*mddev
, char *page
)
3865 return sprintf(page
, "%d\n", mddev
->degraded
);
3867 static struct md_sysfs_entry md_degraded
= __ATTR_RO(degraded
);
3870 sync_force_parallel_show(mddev_t
*mddev
, char *page
)
3872 return sprintf(page
, "%d\n", mddev
->parallel_resync
);
3876 sync_force_parallel_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3880 if (strict_strtol(buf
, 10, &n
))
3883 if (n
!= 0 && n
!= 1)
3886 mddev
->parallel_resync
= n
;
3888 if (mddev
->sync_thread
)
3889 wake_up(&resync_wait
);
3894 /* force parallel resync, even with shared block devices */
3895 static struct md_sysfs_entry md_sync_force_parallel
=
3896 __ATTR(sync_force_parallel
, S_IRUGO
|S_IWUSR
,
3897 sync_force_parallel_show
, sync_force_parallel_store
);
3900 sync_speed_show(mddev_t
*mddev
, char *page
)
3902 unsigned long resync
, dt
, db
;
3903 if (mddev
->curr_resync
== 0)
3904 return sprintf(page
, "none\n");
3905 resync
= mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
);
3906 dt
= (jiffies
- mddev
->resync_mark
) / HZ
;
3908 db
= resync
- mddev
->resync_mark_cnt
;
3909 return sprintf(page
, "%lu\n", db
/dt
/2); /* K/sec */
3912 static struct md_sysfs_entry md_sync_speed
= __ATTR_RO(sync_speed
);
3915 sync_completed_show(mddev_t
*mddev
, char *page
)
3917 unsigned long max_sectors
, resync
;
3919 if (!test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3920 return sprintf(page
, "none\n");
3922 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
3923 max_sectors
= mddev
->resync_max_sectors
;
3925 max_sectors
= mddev
->dev_sectors
;
3927 resync
= mddev
->curr_resync_completed
;
3928 return sprintf(page
, "%lu / %lu\n", resync
, max_sectors
);
3931 static struct md_sysfs_entry md_sync_completed
= __ATTR_RO(sync_completed
);
3934 min_sync_show(mddev_t
*mddev
, char *page
)
3936 return sprintf(page
, "%llu\n",
3937 (unsigned long long)mddev
->resync_min
);
3940 min_sync_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3942 unsigned long long min
;
3943 if (strict_strtoull(buf
, 10, &min
))
3945 if (min
> mddev
->resync_max
)
3947 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3950 /* Must be a multiple of chunk_size */
3951 if (mddev
->chunk_sectors
) {
3952 sector_t temp
= min
;
3953 if (sector_div(temp
, mddev
->chunk_sectors
))
3956 mddev
->resync_min
= min
;
3961 static struct md_sysfs_entry md_min_sync
=
3962 __ATTR(sync_min
, S_IRUGO
|S_IWUSR
, min_sync_show
, min_sync_store
);
3965 max_sync_show(mddev_t
*mddev
, char *page
)
3967 if (mddev
->resync_max
== MaxSector
)
3968 return sprintf(page
, "max\n");
3970 return sprintf(page
, "%llu\n",
3971 (unsigned long long)mddev
->resync_max
);
3974 max_sync_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3976 if (strncmp(buf
, "max", 3) == 0)
3977 mddev
->resync_max
= MaxSector
;
3979 unsigned long long max
;
3980 if (strict_strtoull(buf
, 10, &max
))
3982 if (max
< mddev
->resync_min
)
3984 if (max
< mddev
->resync_max
&&
3986 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3989 /* Must be a multiple of chunk_size */
3990 if (mddev
->chunk_sectors
) {
3991 sector_t temp
= max
;
3992 if (sector_div(temp
, mddev
->chunk_sectors
))
3995 mddev
->resync_max
= max
;
3997 wake_up(&mddev
->recovery_wait
);
4001 static struct md_sysfs_entry md_max_sync
=
4002 __ATTR(sync_max
, S_IRUGO
|S_IWUSR
, max_sync_show
, max_sync_store
);
4005 suspend_lo_show(mddev_t
*mddev
, char *page
)
4007 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_lo
);
4011 suspend_lo_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4014 unsigned long long new = simple_strtoull(buf
, &e
, 10);
4016 if (mddev
->pers
== NULL
||
4017 mddev
->pers
->quiesce
== NULL
)
4019 if (buf
== e
|| (*e
&& *e
!= '\n'))
4021 if (new >= mddev
->suspend_hi
||
4022 (new > mddev
->suspend_lo
&& new < mddev
->suspend_hi
)) {
4023 mddev
->suspend_lo
= new;
4024 mddev
->pers
->quiesce(mddev
, 2);
4029 static struct md_sysfs_entry md_suspend_lo
=
4030 __ATTR(suspend_lo
, S_IRUGO
|S_IWUSR
, suspend_lo_show
, suspend_lo_store
);
4034 suspend_hi_show(mddev_t
*mddev
, char *page
)
4036 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_hi
);
4040 suspend_hi_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4043 unsigned long long new = simple_strtoull(buf
, &e
, 10);
4045 if (mddev
->pers
== NULL
||
4046 mddev
->pers
->quiesce
== NULL
)
4048 if (buf
== e
|| (*e
&& *e
!= '\n'))
4050 if ((new <= mddev
->suspend_lo
&& mddev
->suspend_lo
>= mddev
->suspend_hi
) ||
4051 (new > mddev
->suspend_lo
&& new > mddev
->suspend_hi
)) {
4052 mddev
->suspend_hi
= new;
4053 mddev
->pers
->quiesce(mddev
, 1);
4054 mddev
->pers
->quiesce(mddev
, 0);
4059 static struct md_sysfs_entry md_suspend_hi
=
4060 __ATTR(suspend_hi
, S_IRUGO
|S_IWUSR
, suspend_hi_show
, suspend_hi_store
);
4063 reshape_position_show(mddev_t
*mddev
, char *page
)
4065 if (mddev
->reshape_position
!= MaxSector
)
4066 return sprintf(page
, "%llu\n",
4067 (unsigned long long)mddev
->reshape_position
);
4068 strcpy(page
, "none\n");
4073 reshape_position_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4076 unsigned long long new = simple_strtoull(buf
, &e
, 10);
4079 if (buf
== e
|| (*e
&& *e
!= '\n'))
4081 mddev
->reshape_position
= new;
4082 mddev
->delta_disks
= 0;
4083 mddev
->new_level
= mddev
->level
;
4084 mddev
->new_layout
= mddev
->layout
;
4085 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
4089 static struct md_sysfs_entry md_reshape_position
=
4090 __ATTR(reshape_position
, S_IRUGO
|S_IWUSR
, reshape_position_show
,
4091 reshape_position_store
);
4094 array_size_show(mddev_t
*mddev
, char *page
)
4096 if (mddev
->external_size
)
4097 return sprintf(page
, "%llu\n",
4098 (unsigned long long)mddev
->array_sectors
/2);
4100 return sprintf(page
, "default\n");
4104 array_size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4108 if (strncmp(buf
, "default", 7) == 0) {
4110 sectors
= mddev
->pers
->size(mddev
, 0, 0);
4112 sectors
= mddev
->array_sectors
;
4114 mddev
->external_size
= 0;
4116 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
4118 if (mddev
->pers
&& mddev
->pers
->size(mddev
, 0, 0) < sectors
)
4121 mddev
->external_size
= 1;
4124 mddev
->array_sectors
= sectors
;
4125 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
4127 revalidate_disk(mddev
->gendisk
);
4132 static struct md_sysfs_entry md_array_size
=
4133 __ATTR(array_size
, S_IRUGO
|S_IWUSR
, array_size_show
,
4136 static struct attribute
*md_default_attrs
[] = {
4139 &md_raid_disks
.attr
,
4140 &md_chunk_size
.attr
,
4142 &md_resync_start
.attr
,
4144 &md_new_device
.attr
,
4145 &md_safe_delay
.attr
,
4146 &md_array_state
.attr
,
4147 &md_reshape_position
.attr
,
4148 &md_array_size
.attr
,
4149 &max_corr_read_errors
.attr
,
4153 static struct attribute
*md_redundancy_attrs
[] = {
4155 &md_mismatches
.attr
,
4158 &md_sync_speed
.attr
,
4159 &md_sync_force_parallel
.attr
,
4160 &md_sync_completed
.attr
,
4163 &md_suspend_lo
.attr
,
4164 &md_suspend_hi
.attr
,
4169 static struct attribute_group md_redundancy_group
= {
4171 .attrs
= md_redundancy_attrs
,
4176 md_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
4178 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
4179 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
4184 rv
= mddev_lock(mddev
);
4186 rv
= entry
->show(mddev
, page
);
4187 mddev_unlock(mddev
);
4193 md_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
4194 const char *page
, size_t length
)
4196 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
4197 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
4202 if (!capable(CAP_SYS_ADMIN
))
4204 rv
= mddev_lock(mddev
);
4205 if (mddev
->hold_active
== UNTIL_IOCTL
)
4206 mddev
->hold_active
= 0;
4208 rv
= entry
->store(mddev
, page
, length
);
4209 mddev_unlock(mddev
);
4214 static void md_free(struct kobject
*ko
)
4216 mddev_t
*mddev
= container_of(ko
, mddev_t
, kobj
);
4218 if (mddev
->sysfs_state
)
4219 sysfs_put(mddev
->sysfs_state
);
4221 if (mddev
->gendisk
) {
4222 del_gendisk(mddev
->gendisk
);
4223 put_disk(mddev
->gendisk
);
4226 blk_cleanup_queue(mddev
->queue
);
4231 static const struct sysfs_ops md_sysfs_ops
= {
4232 .show
= md_attr_show
,
4233 .store
= md_attr_store
,
4235 static struct kobj_type md_ktype
= {
4237 .sysfs_ops
= &md_sysfs_ops
,
4238 .default_attrs
= md_default_attrs
,
4243 static void mddev_delayed_delete(struct work_struct
*ws
)
4245 mddev_t
*mddev
= container_of(ws
, mddev_t
, del_work
);
4247 sysfs_remove_group(&mddev
->kobj
, &md_bitmap_group
);
4248 kobject_del(&mddev
->kobj
);
4249 kobject_put(&mddev
->kobj
);
4252 static int md_alloc(dev_t dev
, char *name
)
4254 static DEFINE_MUTEX(disks_mutex
);
4255 mddev_t
*mddev
= mddev_find(dev
);
4256 struct gendisk
*disk
;
4265 partitioned
= (MAJOR(mddev
->unit
) != MD_MAJOR
);
4266 shift
= partitioned
? MdpMinorShift
: 0;
4267 unit
= MINOR(mddev
->unit
) >> shift
;
4269 /* wait for any previous instance of this device to be
4270 * completely removed (mddev_delayed_delete).
4272 flush_workqueue(md_misc_wq
);
4274 mutex_lock(&disks_mutex
);
4280 /* Need to ensure that 'name' is not a duplicate.
4283 spin_lock(&all_mddevs_lock
);
4285 list_for_each_entry(mddev2
, &all_mddevs
, all_mddevs
)
4286 if (mddev2
->gendisk
&&
4287 strcmp(mddev2
->gendisk
->disk_name
, name
) == 0) {
4288 spin_unlock(&all_mddevs_lock
);
4291 spin_unlock(&all_mddevs_lock
);
4295 mddev
->queue
= blk_alloc_queue(GFP_KERNEL
);
4298 mddev
->queue
->queuedata
= mddev
;
4300 blk_queue_make_request(mddev
->queue
, md_make_request
);
4302 disk
= alloc_disk(1 << shift
);
4304 blk_cleanup_queue(mddev
->queue
);
4305 mddev
->queue
= NULL
;
4308 disk
->major
= MAJOR(mddev
->unit
);
4309 disk
->first_minor
= unit
<< shift
;
4311 strcpy(disk
->disk_name
, name
);
4312 else if (partitioned
)
4313 sprintf(disk
->disk_name
, "md_d%d", unit
);
4315 sprintf(disk
->disk_name
, "md%d", unit
);
4316 disk
->fops
= &md_fops
;
4317 disk
->private_data
= mddev
;
4318 disk
->queue
= mddev
->queue
;
4319 /* Allow extended partitions. This makes the
4320 * 'mdp' device redundant, but we can't really
4323 disk
->flags
|= GENHD_FL_EXT_DEVT
;
4325 mddev
->gendisk
= disk
;
4326 error
= kobject_init_and_add(&mddev
->kobj
, &md_ktype
,
4327 &disk_to_dev(disk
)->kobj
, "%s", "md");
4329 /* This isn't possible, but as kobject_init_and_add is marked
4330 * __must_check, we must do something with the result
4332 printk(KERN_WARNING
"md: cannot register %s/md - name in use\n",
4336 if (mddev
->kobj
.sd
&&
4337 sysfs_create_group(&mddev
->kobj
, &md_bitmap_group
))
4338 printk(KERN_DEBUG
"pointless warning\n");
4340 blk_queue_flush(mddev
->queue
, REQ_FLUSH
| REQ_FUA
);
4342 mutex_unlock(&disks_mutex
);
4343 if (!error
&& mddev
->kobj
.sd
) {
4344 kobject_uevent(&mddev
->kobj
, KOBJ_ADD
);
4345 mddev
->sysfs_state
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "array_state");
4351 static struct kobject
*md_probe(dev_t dev
, int *part
, void *data
)
4353 md_alloc(dev
, NULL
);
4357 static int add_named_array(const char *val
, struct kernel_param
*kp
)
4359 /* val must be "md_*" where * is not all digits.
4360 * We allocate an array with a large free minor number, and
4361 * set the name to val. val must not already be an active name.
4363 int len
= strlen(val
);
4364 char buf
[DISK_NAME_LEN
];
4366 while (len
&& val
[len
-1] == '\n')
4368 if (len
>= DISK_NAME_LEN
)
4370 strlcpy(buf
, val
, len
+1);
4371 if (strncmp(buf
, "md_", 3) != 0)
4373 return md_alloc(0, buf
);
4376 static void md_safemode_timeout(unsigned long data
)
4378 mddev_t
*mddev
= (mddev_t
*) data
;
4380 if (!atomic_read(&mddev
->writes_pending
)) {
4381 mddev
->safemode
= 1;
4382 if (mddev
->external
)
4383 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4385 md_wakeup_thread(mddev
->thread
);
4388 static int start_dirty_degraded
;
4390 int md_run(mddev_t
*mddev
)
4394 struct mdk_personality
*pers
;
4396 if (list_empty(&mddev
->disks
))
4397 /* cannot run an array with no devices.. */
4402 /* Cannot run until previous stop completes properly */
4403 if (mddev
->sysfs_active
)
4407 * Analyze all RAID superblock(s)
4409 if (!mddev
->raid_disks
) {
4410 if (!mddev
->persistent
)
4415 if (mddev
->level
!= LEVEL_NONE
)
4416 request_module("md-level-%d", mddev
->level
);
4417 else if (mddev
->clevel
[0])
4418 request_module("md-%s", mddev
->clevel
);
4421 * Drop all container device buffers, from now on
4422 * the only valid external interface is through the md
4425 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
4426 if (test_bit(Faulty
, &rdev
->flags
))
4428 sync_blockdev(rdev
->bdev
);
4429 invalidate_bdev(rdev
->bdev
);
4431 /* perform some consistency tests on the device.
4432 * We don't want the data to overlap the metadata,
4433 * Internal Bitmap issues have been handled elsewhere.
4435 if (rdev
->data_offset
< rdev
->sb_start
) {
4436 if (mddev
->dev_sectors
&&
4437 rdev
->data_offset
+ mddev
->dev_sectors
4439 printk("md: %s: data overlaps metadata\n",
4444 if (rdev
->sb_start
+ rdev
->sb_size
/512
4445 > rdev
->data_offset
) {
4446 printk("md: %s: metadata overlaps data\n",
4451 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
4454 if (mddev
->bio_set
== NULL
)
4455 mddev
->bio_set
= bioset_create(BIO_POOL_SIZE
, sizeof(mddev
));
4457 spin_lock(&pers_lock
);
4458 pers
= find_pers(mddev
->level
, mddev
->clevel
);
4459 if (!pers
|| !try_module_get(pers
->owner
)) {
4460 spin_unlock(&pers_lock
);
4461 if (mddev
->level
!= LEVEL_NONE
)
4462 printk(KERN_WARNING
"md: personality for level %d is not loaded!\n",
4465 printk(KERN_WARNING
"md: personality for level %s is not loaded!\n",
4470 spin_unlock(&pers_lock
);
4471 if (mddev
->level
!= pers
->level
) {
4472 mddev
->level
= pers
->level
;
4473 mddev
->new_level
= pers
->level
;
4475 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
4477 if (mddev
->reshape_position
!= MaxSector
&&
4478 pers
->start_reshape
== NULL
) {
4479 /* This personality cannot handle reshaping... */
4481 module_put(pers
->owner
);
4485 if (pers
->sync_request
) {
4486 /* Warn if this is a potentially silly
4489 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
4493 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4494 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
4496 rdev
->bdev
->bd_contains
==
4497 rdev2
->bdev
->bd_contains
) {
4499 "%s: WARNING: %s appears to be"
4500 " on the same physical disk as"
4503 bdevname(rdev
->bdev
,b
),
4504 bdevname(rdev2
->bdev
,b2
));
4511 "True protection against single-disk"
4512 " failure might be compromised.\n");
4515 mddev
->recovery
= 0;
4516 /* may be over-ridden by personality */
4517 mddev
->resync_max_sectors
= mddev
->dev_sectors
;
4519 mddev
->ok_start_degraded
= start_dirty_degraded
;
4521 if (start_readonly
&& mddev
->ro
== 0)
4522 mddev
->ro
= 2; /* read-only, but switch on first write */
4524 err
= mddev
->pers
->run(mddev
);
4526 printk(KERN_ERR
"md: pers->run() failed ...\n");
4527 else if (mddev
->pers
->size(mddev
, 0, 0) < mddev
->array_sectors
) {
4528 WARN_ONCE(!mddev
->external_size
, "%s: default size too small,"
4529 " but 'external_size' not in effect?\n", __func__
);
4531 "md: invalid array_size %llu > default size %llu\n",
4532 (unsigned long long)mddev
->array_sectors
/ 2,
4533 (unsigned long long)mddev
->pers
->size(mddev
, 0, 0) / 2);
4535 mddev
->pers
->stop(mddev
);
4537 if (err
== 0 && mddev
->pers
->sync_request
) {
4538 err
= bitmap_create(mddev
);
4540 printk(KERN_ERR
"%s: failed to create bitmap (%d)\n",
4541 mdname(mddev
), err
);
4542 mddev
->pers
->stop(mddev
);
4546 module_put(mddev
->pers
->owner
);
4548 bitmap_destroy(mddev
);
4551 if (mddev
->pers
->sync_request
) {
4552 if (mddev
->kobj
.sd
&&
4553 sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
4555 "md: cannot register extra attributes for %s\n",
4557 mddev
->sysfs_action
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "sync_action");
4558 } else if (mddev
->ro
== 2) /* auto-readonly not meaningful */
4561 atomic_set(&mddev
->writes_pending
,0);
4562 atomic_set(&mddev
->max_corr_read_errors
,
4563 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS
);
4564 mddev
->safemode
= 0;
4565 mddev
->safemode_timer
.function
= md_safemode_timeout
;
4566 mddev
->safemode_timer
.data
= (unsigned long) mddev
;
4567 mddev
->safemode_delay
= (200 * HZ
)/1000 +1; /* 200 msec delay */
4571 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4572 if (rdev
->raid_disk
>= 0) {
4574 sprintf(nm
, "rd%d", rdev
->raid_disk
);
4575 if (sysfs_create_link(&mddev
->kobj
, &rdev
->kobj
, nm
))
4576 /* failure here is OK */;
4579 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4582 md_update_sb(mddev
, 0);
4584 md_wakeup_thread(mddev
->thread
);
4585 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
4587 md_new_event(mddev
);
4588 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4589 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
4590 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
4593 EXPORT_SYMBOL_GPL(md_run
);
4595 static int do_md_run(mddev_t
*mddev
)
4599 err
= md_run(mddev
);
4602 err
= bitmap_load(mddev
);
4604 bitmap_destroy(mddev
);
4607 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
4608 revalidate_disk(mddev
->gendisk
);
4609 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
4614 static int restart_array(mddev_t
*mddev
)
4616 struct gendisk
*disk
= mddev
->gendisk
;
4618 /* Complain if it has no devices */
4619 if (list_empty(&mddev
->disks
))
4625 mddev
->safemode
= 0;
4627 set_disk_ro(disk
, 0);
4628 printk(KERN_INFO
"md: %s switched to read-write mode.\n",
4630 /* Kick recovery or resync if necessary */
4631 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4632 md_wakeup_thread(mddev
->thread
);
4633 md_wakeup_thread(mddev
->sync_thread
);
4634 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4638 /* similar to deny_write_access, but accounts for our holding a reference
4639 * to the file ourselves */
4640 static int deny_bitmap_write_access(struct file
* file
)
4642 struct inode
*inode
= file
->f_mapping
->host
;
4644 spin_lock(&inode
->i_lock
);
4645 if (atomic_read(&inode
->i_writecount
) > 1) {
4646 spin_unlock(&inode
->i_lock
);
4649 atomic_set(&inode
->i_writecount
, -1);
4650 spin_unlock(&inode
->i_lock
);
4655 void restore_bitmap_write_access(struct file
*file
)
4657 struct inode
*inode
= file
->f_mapping
->host
;
4659 spin_lock(&inode
->i_lock
);
4660 atomic_set(&inode
->i_writecount
, 1);
4661 spin_unlock(&inode
->i_lock
);
4664 static void md_clean(mddev_t
*mddev
)
4666 mddev
->array_sectors
= 0;
4667 mddev
->external_size
= 0;
4668 mddev
->dev_sectors
= 0;
4669 mddev
->raid_disks
= 0;
4670 mddev
->recovery_cp
= 0;
4671 mddev
->resync_min
= 0;
4672 mddev
->resync_max
= MaxSector
;
4673 mddev
->reshape_position
= MaxSector
;
4674 mddev
->external
= 0;
4675 mddev
->persistent
= 0;
4676 mddev
->level
= LEVEL_NONE
;
4677 mddev
->clevel
[0] = 0;
4680 mddev
->metadata_type
[0] = 0;
4681 mddev
->chunk_sectors
= 0;
4682 mddev
->ctime
= mddev
->utime
= 0;
4684 mddev
->max_disks
= 0;
4686 mddev
->can_decrease_events
= 0;
4687 mddev
->delta_disks
= 0;
4688 mddev
->new_level
= LEVEL_NONE
;
4689 mddev
->new_layout
= 0;
4690 mddev
->new_chunk_sectors
= 0;
4691 mddev
->curr_resync
= 0;
4692 mddev
->resync_mismatches
= 0;
4693 mddev
->suspend_lo
= mddev
->suspend_hi
= 0;
4694 mddev
->sync_speed_min
= mddev
->sync_speed_max
= 0;
4695 mddev
->recovery
= 0;
4697 mddev
->degraded
= 0;
4698 mddev
->safemode
= 0;
4699 mddev
->bitmap_info
.offset
= 0;
4700 mddev
->bitmap_info
.default_offset
= 0;
4701 mddev
->bitmap_info
.chunksize
= 0;
4702 mddev
->bitmap_info
.daemon_sleep
= 0;
4703 mddev
->bitmap_info
.max_write_behind
= 0;
4707 static void __md_stop_writes(mddev_t
*mddev
)
4709 if (mddev
->sync_thread
) {
4710 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4711 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4712 reap_sync_thread(mddev
);
4715 del_timer_sync(&mddev
->safemode_timer
);
4717 bitmap_flush(mddev
);
4718 md_super_wait(mddev
);
4720 if (!mddev
->in_sync
|| mddev
->flags
) {
4721 /* mark array as shutdown cleanly */
4723 md_update_sb(mddev
, 1);
4727 void md_stop_writes(mddev_t
*mddev
)
4730 __md_stop_writes(mddev
);
4731 mddev_unlock(mddev
);
4733 EXPORT_SYMBOL_GPL(md_stop_writes
);
4735 void md_stop(mddev_t
*mddev
)
4738 mddev
->pers
->stop(mddev
);
4739 if (mddev
->pers
->sync_request
&& mddev
->to_remove
== NULL
)
4740 mddev
->to_remove
= &md_redundancy_group
;
4741 module_put(mddev
->pers
->owner
);
4743 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4745 EXPORT_SYMBOL_GPL(md_stop
);
4747 static int md_set_readonly(mddev_t
*mddev
, int is_open
)
4750 mutex_lock(&mddev
->open_mutex
);
4751 if (atomic_read(&mddev
->openers
) > is_open
) {
4752 printk("md: %s still in use.\n",mdname(mddev
));
4757 __md_stop_writes(mddev
);
4763 set_disk_ro(mddev
->gendisk
, 1);
4764 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4765 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4769 mutex_unlock(&mddev
->open_mutex
);
4774 * 0 - completely stop and dis-assemble array
4775 * 2 - stop but do not disassemble array
4777 static int do_md_stop(mddev_t
* mddev
, int mode
, int is_open
)
4779 struct gendisk
*disk
= mddev
->gendisk
;
4782 mutex_lock(&mddev
->open_mutex
);
4783 if (atomic_read(&mddev
->openers
) > is_open
||
4784 mddev
->sysfs_active
) {
4785 printk("md: %s still in use.\n",mdname(mddev
));
4786 mutex_unlock(&mddev
->open_mutex
);
4792 set_disk_ro(disk
, 0);
4794 __md_stop_writes(mddev
);
4796 mddev
->queue
->merge_bvec_fn
= NULL
;
4797 mddev
->queue
->unplug_fn
= NULL
;
4798 mddev
->queue
->backing_dev_info
.congested_fn
= NULL
;
4800 /* tell userspace to handle 'inactive' */
4801 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4803 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4804 if (rdev
->raid_disk
>= 0) {
4806 sprintf(nm
, "rd%d", rdev
->raid_disk
);
4807 sysfs_remove_link(&mddev
->kobj
, nm
);
4810 set_capacity(disk
, 0);
4811 mutex_unlock(&mddev
->open_mutex
);
4812 revalidate_disk(disk
);
4817 mutex_unlock(&mddev
->open_mutex
);
4819 * Free resources if final stop
4822 printk(KERN_INFO
"md: %s stopped.\n", mdname(mddev
));
4824 bitmap_destroy(mddev
);
4825 if (mddev
->bitmap_info
.file
) {
4826 restore_bitmap_write_access(mddev
->bitmap_info
.file
);
4827 fput(mddev
->bitmap_info
.file
);
4828 mddev
->bitmap_info
.file
= NULL
;
4830 mddev
->bitmap_info
.offset
= 0;
4832 export_array(mddev
);
4835 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
4836 if (mddev
->hold_active
== UNTIL_STOP
)
4837 mddev
->hold_active
= 0;
4839 blk_integrity_unregister(disk
);
4840 md_new_event(mddev
);
4841 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4846 static void autorun_array(mddev_t
*mddev
)
4851 if (list_empty(&mddev
->disks
))
4854 printk(KERN_INFO
"md: running: ");
4856 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
4857 char b
[BDEVNAME_SIZE
];
4858 printk("<%s>", bdevname(rdev
->bdev
,b
));
4862 err
= do_md_run(mddev
);
4864 printk(KERN_WARNING
"md: do_md_run() returned %d\n", err
);
4865 do_md_stop(mddev
, 0, 0);
4870 * lets try to run arrays based on all disks that have arrived
4871 * until now. (those are in pending_raid_disks)
4873 * the method: pick the first pending disk, collect all disks with
4874 * the same UUID, remove all from the pending list and put them into
4875 * the 'same_array' list. Then order this list based on superblock
4876 * update time (freshest comes first), kick out 'old' disks and
4877 * compare superblocks. If everything's fine then run it.
4879 * If "unit" is allocated, then bump its reference count
4881 static void autorun_devices(int part
)
4883 mdk_rdev_t
*rdev0
, *rdev
, *tmp
;
4885 char b
[BDEVNAME_SIZE
];
4887 printk(KERN_INFO
"md: autorun ...\n");
4888 while (!list_empty(&pending_raid_disks
)) {
4891 LIST_HEAD(candidates
);
4892 rdev0
= list_entry(pending_raid_disks
.next
,
4893 mdk_rdev_t
, same_set
);
4895 printk(KERN_INFO
"md: considering %s ...\n",
4896 bdevname(rdev0
->bdev
,b
));
4897 INIT_LIST_HEAD(&candidates
);
4898 rdev_for_each_list(rdev
, tmp
, &pending_raid_disks
)
4899 if (super_90_load(rdev
, rdev0
, 0) >= 0) {
4900 printk(KERN_INFO
"md: adding %s ...\n",
4901 bdevname(rdev
->bdev
,b
));
4902 list_move(&rdev
->same_set
, &candidates
);
4905 * now we have a set of devices, with all of them having
4906 * mostly sane superblocks. It's time to allocate the
4910 dev
= MKDEV(mdp_major
,
4911 rdev0
->preferred_minor
<< MdpMinorShift
);
4912 unit
= MINOR(dev
) >> MdpMinorShift
;
4914 dev
= MKDEV(MD_MAJOR
, rdev0
->preferred_minor
);
4917 if (rdev0
->preferred_minor
!= unit
) {
4918 printk(KERN_INFO
"md: unit number in %s is bad: %d\n",
4919 bdevname(rdev0
->bdev
, b
), rdev0
->preferred_minor
);
4923 md_probe(dev
, NULL
, NULL
);
4924 mddev
= mddev_find(dev
);
4925 if (!mddev
|| !mddev
->gendisk
) {
4929 "md: cannot allocate memory for md drive.\n");
4932 if (mddev_lock(mddev
))
4933 printk(KERN_WARNING
"md: %s locked, cannot run\n",
4935 else if (mddev
->raid_disks
|| mddev
->major_version
4936 || !list_empty(&mddev
->disks
)) {
4938 "md: %s already running, cannot run %s\n",
4939 mdname(mddev
), bdevname(rdev0
->bdev
,b
));
4940 mddev_unlock(mddev
);
4942 printk(KERN_INFO
"md: created %s\n", mdname(mddev
));
4943 mddev
->persistent
= 1;
4944 rdev_for_each_list(rdev
, tmp
, &candidates
) {
4945 list_del_init(&rdev
->same_set
);
4946 if (bind_rdev_to_array(rdev
, mddev
))
4949 autorun_array(mddev
);
4950 mddev_unlock(mddev
);
4952 /* on success, candidates will be empty, on error
4955 rdev_for_each_list(rdev
, tmp
, &candidates
) {
4956 list_del_init(&rdev
->same_set
);
4961 printk(KERN_INFO
"md: ... autorun DONE.\n");
4963 #endif /* !MODULE */
4965 static int get_version(void __user
* arg
)
4969 ver
.major
= MD_MAJOR_VERSION
;
4970 ver
.minor
= MD_MINOR_VERSION
;
4971 ver
.patchlevel
= MD_PATCHLEVEL_VERSION
;
4973 if (copy_to_user(arg
, &ver
, sizeof(ver
)))
4979 static int get_array_info(mddev_t
* mddev
, void __user
* arg
)
4981 mdu_array_info_t info
;
4982 int nr
,working
,insync
,failed
,spare
;
4985 nr
=working
=insync
=failed
=spare
=0;
4986 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
4988 if (test_bit(Faulty
, &rdev
->flags
))
4992 if (test_bit(In_sync
, &rdev
->flags
))
4999 info
.major_version
= mddev
->major_version
;
5000 info
.minor_version
= mddev
->minor_version
;
5001 info
.patch_version
= MD_PATCHLEVEL_VERSION
;
5002 info
.ctime
= mddev
->ctime
;
5003 info
.level
= mddev
->level
;
5004 info
.size
= mddev
->dev_sectors
/ 2;
5005 if (info
.size
!= mddev
->dev_sectors
/ 2) /* overflow */
5008 info
.raid_disks
= mddev
->raid_disks
;
5009 info
.md_minor
= mddev
->md_minor
;
5010 info
.not_persistent
= !mddev
->persistent
;
5012 info
.utime
= mddev
->utime
;
5015 info
.state
= (1<<MD_SB_CLEAN
);
5016 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
5017 info
.state
= (1<<MD_SB_BITMAP_PRESENT
);
5018 info
.active_disks
= insync
;
5019 info
.working_disks
= working
;
5020 info
.failed_disks
= failed
;
5021 info
.spare_disks
= spare
;
5023 info
.layout
= mddev
->layout
;
5024 info
.chunk_size
= mddev
->chunk_sectors
<< 9;
5026 if (copy_to_user(arg
, &info
, sizeof(info
)))
5032 static int get_bitmap_file(mddev_t
* mddev
, void __user
* arg
)
5034 mdu_bitmap_file_t
*file
= NULL
; /* too big for stack allocation */
5035 char *ptr
, *buf
= NULL
;
5038 if (md_allow_write(mddev
))
5039 file
= kmalloc(sizeof(*file
), GFP_NOIO
);
5041 file
= kmalloc(sizeof(*file
), GFP_KERNEL
);
5046 /* bitmap disabled, zero the first byte and copy out */
5047 if (!mddev
->bitmap
|| !mddev
->bitmap
->file
) {
5048 file
->pathname
[0] = '\0';
5052 buf
= kmalloc(sizeof(file
->pathname
), GFP_KERNEL
);
5056 ptr
= d_path(&mddev
->bitmap
->file
->f_path
, buf
, sizeof(file
->pathname
));
5060 strcpy(file
->pathname
, ptr
);
5064 if (copy_to_user(arg
, file
, sizeof(*file
)))
5072 static int get_disk_info(mddev_t
* mddev
, void __user
* arg
)
5074 mdu_disk_info_t info
;
5077 if (copy_from_user(&info
, arg
, sizeof(info
)))
5080 rdev
= find_rdev_nr(mddev
, info
.number
);
5082 info
.major
= MAJOR(rdev
->bdev
->bd_dev
);
5083 info
.minor
= MINOR(rdev
->bdev
->bd_dev
);
5084 info
.raid_disk
= rdev
->raid_disk
;
5086 if (test_bit(Faulty
, &rdev
->flags
))
5087 info
.state
|= (1<<MD_DISK_FAULTY
);
5088 else if (test_bit(In_sync
, &rdev
->flags
)) {
5089 info
.state
|= (1<<MD_DISK_ACTIVE
);
5090 info
.state
|= (1<<MD_DISK_SYNC
);
5092 if (test_bit(WriteMostly
, &rdev
->flags
))
5093 info
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
5095 info
.major
= info
.minor
= 0;
5096 info
.raid_disk
= -1;
5097 info
.state
= (1<<MD_DISK_REMOVED
);
5100 if (copy_to_user(arg
, &info
, sizeof(info
)))
5106 static int add_new_disk(mddev_t
* mddev
, mdu_disk_info_t
*info
)
5108 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
5110 dev_t dev
= MKDEV(info
->major
,info
->minor
);
5112 if (info
->major
!= MAJOR(dev
) || info
->minor
!= MINOR(dev
))
5115 if (!mddev
->raid_disks
) {
5117 /* expecting a device which has a superblock */
5118 rdev
= md_import_device(dev
, mddev
->major_version
, mddev
->minor_version
);
5121 "md: md_import_device returned %ld\n",
5123 return PTR_ERR(rdev
);
5125 if (!list_empty(&mddev
->disks
)) {
5126 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
5127 mdk_rdev_t
, same_set
);
5128 err
= super_types
[mddev
->major_version
]
5129 .load_super(rdev
, rdev0
, mddev
->minor_version
);
5132 "md: %s has different UUID to %s\n",
5133 bdevname(rdev
->bdev
,b
),
5134 bdevname(rdev0
->bdev
,b2
));
5139 err
= bind_rdev_to_array(rdev
, mddev
);
5146 * add_new_disk can be used once the array is assembled
5147 * to add "hot spares". They must already have a superblock
5152 if (!mddev
->pers
->hot_add_disk
) {
5154 "%s: personality does not support diskops!\n",
5158 if (mddev
->persistent
)
5159 rdev
= md_import_device(dev
, mddev
->major_version
,
5160 mddev
->minor_version
);
5162 rdev
= md_import_device(dev
, -1, -1);
5165 "md: md_import_device returned %ld\n",
5167 return PTR_ERR(rdev
);
5169 /* set saved_raid_disk if appropriate */
5170 if (!mddev
->persistent
) {
5171 if (info
->state
& (1<<MD_DISK_SYNC
) &&
5172 info
->raid_disk
< mddev
->raid_disks
) {
5173 rdev
->raid_disk
= info
->raid_disk
;
5174 set_bit(In_sync
, &rdev
->flags
);
5176 rdev
->raid_disk
= -1;
5178 super_types
[mddev
->major_version
].
5179 validate_super(mddev
, rdev
);
5180 if (test_bit(In_sync
, &rdev
->flags
))
5181 rdev
->saved_raid_disk
= rdev
->raid_disk
;
5183 rdev
->saved_raid_disk
= -1;
5185 clear_bit(In_sync
, &rdev
->flags
); /* just to be sure */
5186 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
5187 set_bit(WriteMostly
, &rdev
->flags
);
5189 clear_bit(WriteMostly
, &rdev
->flags
);
5191 rdev
->raid_disk
= -1;
5192 err
= bind_rdev_to_array(rdev
, mddev
);
5193 if (!err
&& !mddev
->pers
->hot_remove_disk
) {
5194 /* If there is hot_add_disk but no hot_remove_disk
5195 * then added disks for geometry changes,
5196 * and should be added immediately.
5198 super_types
[mddev
->major_version
].
5199 validate_super(mddev
, rdev
);
5200 err
= mddev
->pers
->hot_add_disk(mddev
, rdev
);
5202 unbind_rdev_from_array(rdev
);
5207 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
5209 md_update_sb(mddev
, 1);
5210 if (mddev
->degraded
)
5211 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
5212 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5213 md_wakeup_thread(mddev
->thread
);
5217 /* otherwise, add_new_disk is only allowed
5218 * for major_version==0 superblocks
5220 if (mddev
->major_version
!= 0) {
5221 printk(KERN_WARNING
"%s: ADD_NEW_DISK not supported\n",
5226 if (!(info
->state
& (1<<MD_DISK_FAULTY
))) {
5228 rdev
= md_import_device(dev
, -1, 0);
5231 "md: error, md_import_device() returned %ld\n",
5233 return PTR_ERR(rdev
);
5235 rdev
->desc_nr
= info
->number
;
5236 if (info
->raid_disk
< mddev
->raid_disks
)
5237 rdev
->raid_disk
= info
->raid_disk
;
5239 rdev
->raid_disk
= -1;
5241 if (rdev
->raid_disk
< mddev
->raid_disks
)
5242 if (info
->state
& (1<<MD_DISK_SYNC
))
5243 set_bit(In_sync
, &rdev
->flags
);
5245 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
5246 set_bit(WriteMostly
, &rdev
->flags
);
5248 if (!mddev
->persistent
) {
5249 printk(KERN_INFO
"md: nonpersistent superblock ...\n");
5250 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
5252 rdev
->sb_start
= calc_dev_sboffset(rdev
->bdev
);
5253 rdev
->sectors
= rdev
->sb_start
;
5255 err
= bind_rdev_to_array(rdev
, mddev
);
5265 static int hot_remove_disk(mddev_t
* mddev
, dev_t dev
)
5267 char b
[BDEVNAME_SIZE
];
5270 rdev
= find_rdev(mddev
, dev
);
5274 if (rdev
->raid_disk
>= 0)
5277 kick_rdev_from_array(rdev
);
5278 md_update_sb(mddev
, 1);
5279 md_new_event(mddev
);
5283 printk(KERN_WARNING
"md: cannot remove active disk %s from %s ...\n",
5284 bdevname(rdev
->bdev
,b
), mdname(mddev
));
5288 static int hot_add_disk(mddev_t
* mddev
, dev_t dev
)
5290 char b
[BDEVNAME_SIZE
];
5297 if (mddev
->major_version
!= 0) {
5298 printk(KERN_WARNING
"%s: HOT_ADD may only be used with"
5299 " version-0 superblocks.\n",
5303 if (!mddev
->pers
->hot_add_disk
) {
5305 "%s: personality does not support diskops!\n",
5310 rdev
= md_import_device(dev
, -1, 0);
5313 "md: error, md_import_device() returned %ld\n",
5318 if (mddev
->persistent
)
5319 rdev
->sb_start
= calc_dev_sboffset(rdev
->bdev
);
5321 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
5323 rdev
->sectors
= rdev
->sb_start
;
5325 if (test_bit(Faulty
, &rdev
->flags
)) {
5327 "md: can not hot-add faulty %s disk to %s!\n",
5328 bdevname(rdev
->bdev
,b
), mdname(mddev
));
5332 clear_bit(In_sync
, &rdev
->flags
);
5334 rdev
->saved_raid_disk
= -1;
5335 err
= bind_rdev_to_array(rdev
, mddev
);
5340 * The rest should better be atomic, we can have disk failures
5341 * noticed in interrupt contexts ...
5344 rdev
->raid_disk
= -1;
5346 md_update_sb(mddev
, 1);
5349 * Kick recovery, maybe this spare has to be added to the
5350 * array immediately.
5352 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5353 md_wakeup_thread(mddev
->thread
);
5354 md_new_event(mddev
);
5362 static int set_bitmap_file(mddev_t
*mddev
, int fd
)
5367 if (!mddev
->pers
->quiesce
)
5369 if (mddev
->recovery
|| mddev
->sync_thread
)
5371 /* we should be able to change the bitmap.. */
5377 return -EEXIST
; /* cannot add when bitmap is present */
5378 mddev
->bitmap_info
.file
= fget(fd
);
5380 if (mddev
->bitmap_info
.file
== NULL
) {
5381 printk(KERN_ERR
"%s: error: failed to get bitmap file\n",
5386 err
= deny_bitmap_write_access(mddev
->bitmap_info
.file
);
5388 printk(KERN_ERR
"%s: error: bitmap file is already in use\n",
5390 fput(mddev
->bitmap_info
.file
);
5391 mddev
->bitmap_info
.file
= NULL
;
5394 mddev
->bitmap_info
.offset
= 0; /* file overrides offset */
5395 } else if (mddev
->bitmap
== NULL
)
5396 return -ENOENT
; /* cannot remove what isn't there */
5399 mddev
->pers
->quiesce(mddev
, 1);
5401 err
= bitmap_create(mddev
);
5403 err
= bitmap_load(mddev
);
5405 if (fd
< 0 || err
) {
5406 bitmap_destroy(mddev
);
5407 fd
= -1; /* make sure to put the file */
5409 mddev
->pers
->quiesce(mddev
, 0);
5412 if (mddev
->bitmap_info
.file
) {
5413 restore_bitmap_write_access(mddev
->bitmap_info
.file
);
5414 fput(mddev
->bitmap_info
.file
);
5416 mddev
->bitmap_info
.file
= NULL
;
5423 * set_array_info is used two different ways
5424 * The original usage is when creating a new array.
5425 * In this usage, raid_disks is > 0 and it together with
5426 * level, size, not_persistent,layout,chunksize determine the
5427 * shape of the array.
5428 * This will always create an array with a type-0.90.0 superblock.
5429 * The newer usage is when assembling an array.
5430 * In this case raid_disks will be 0, and the major_version field is
5431 * use to determine which style super-blocks are to be found on the devices.
5432 * The minor and patch _version numbers are also kept incase the
5433 * super_block handler wishes to interpret them.
5435 static int set_array_info(mddev_t
* mddev
, mdu_array_info_t
*info
)
5438 if (info
->raid_disks
== 0) {
5439 /* just setting version number for superblock loading */
5440 if (info
->major_version
< 0 ||
5441 info
->major_version
>= ARRAY_SIZE(super_types
) ||
5442 super_types
[info
->major_version
].name
== NULL
) {
5443 /* maybe try to auto-load a module? */
5445 "md: superblock version %d not known\n",
5446 info
->major_version
);
5449 mddev
->major_version
= info
->major_version
;
5450 mddev
->minor_version
= info
->minor_version
;
5451 mddev
->patch_version
= info
->patch_version
;
5452 mddev
->persistent
= !info
->not_persistent
;
5453 /* ensure mddev_put doesn't delete this now that there
5454 * is some minimal configuration.
5456 mddev
->ctime
= get_seconds();
5459 mddev
->major_version
= MD_MAJOR_VERSION
;
5460 mddev
->minor_version
= MD_MINOR_VERSION
;
5461 mddev
->patch_version
= MD_PATCHLEVEL_VERSION
;
5462 mddev
->ctime
= get_seconds();
5464 mddev
->level
= info
->level
;
5465 mddev
->clevel
[0] = 0;
5466 mddev
->dev_sectors
= 2 * (sector_t
)info
->size
;
5467 mddev
->raid_disks
= info
->raid_disks
;
5468 /* don't set md_minor, it is determined by which /dev/md* was
5471 if (info
->state
& (1<<MD_SB_CLEAN
))
5472 mddev
->recovery_cp
= MaxSector
;
5474 mddev
->recovery_cp
= 0;
5475 mddev
->persistent
= ! info
->not_persistent
;
5476 mddev
->external
= 0;
5478 mddev
->layout
= info
->layout
;
5479 mddev
->chunk_sectors
= info
->chunk_size
>> 9;
5481 mddev
->max_disks
= MD_SB_DISKS
;
5483 if (mddev
->persistent
)
5485 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
5487 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
5488 mddev
->bitmap_info
.offset
= 0;
5490 mddev
->reshape_position
= MaxSector
;
5493 * Generate a 128 bit UUID
5495 get_random_bytes(mddev
->uuid
, 16);
5497 mddev
->new_level
= mddev
->level
;
5498 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
5499 mddev
->new_layout
= mddev
->layout
;
5500 mddev
->delta_disks
= 0;
5505 void md_set_array_sectors(mddev_t
*mddev
, sector_t array_sectors
)
5507 WARN(!mddev_is_locked(mddev
), "%s: unlocked mddev!\n", __func__
);
5509 if (mddev
->external_size
)
5512 mddev
->array_sectors
= array_sectors
;
5514 EXPORT_SYMBOL(md_set_array_sectors
);
5516 static int update_size(mddev_t
*mddev
, sector_t num_sectors
)
5520 int fit
= (num_sectors
== 0);
5522 if (mddev
->pers
->resize
== NULL
)
5524 /* The "num_sectors" is the number of sectors of each device that
5525 * is used. This can only make sense for arrays with redundancy.
5526 * linear and raid0 always use whatever space is available. We can only
5527 * consider changing this number if no resync or reconstruction is
5528 * happening, and if the new size is acceptable. It must fit before the
5529 * sb_start or, if that is <data_offset, it must fit before the size
5530 * of each device. If num_sectors is zero, we find the largest size
5534 if (mddev
->sync_thread
)
5537 /* Sorry, cannot grow a bitmap yet, just remove it,
5541 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
5542 sector_t avail
= rdev
->sectors
;
5544 if (fit
&& (num_sectors
== 0 || num_sectors
> avail
))
5545 num_sectors
= avail
;
5546 if (avail
< num_sectors
)
5549 rv
= mddev
->pers
->resize(mddev
, num_sectors
);
5551 revalidate_disk(mddev
->gendisk
);
5555 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
)
5558 /* change the number of raid disks */
5559 if (mddev
->pers
->check_reshape
== NULL
)
5561 if (raid_disks
<= 0 ||
5562 (mddev
->max_disks
&& raid_disks
>= mddev
->max_disks
))
5564 if (mddev
->sync_thread
|| mddev
->reshape_position
!= MaxSector
)
5566 mddev
->delta_disks
= raid_disks
- mddev
->raid_disks
;
5568 rv
= mddev
->pers
->check_reshape(mddev
);
5574 * update_array_info is used to change the configuration of an
5576 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5577 * fields in the info are checked against the array.
5578 * Any differences that cannot be handled will cause an error.
5579 * Normally, only one change can be managed at a time.
5581 static int update_array_info(mddev_t
*mddev
, mdu_array_info_t
*info
)
5587 /* calculate expected state,ignoring low bits */
5588 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
5589 state
|= (1 << MD_SB_BITMAP_PRESENT
);
5591 if (mddev
->major_version
!= info
->major_version
||
5592 mddev
->minor_version
!= info
->minor_version
||
5593 /* mddev->patch_version != info->patch_version || */
5594 mddev
->ctime
!= info
->ctime
||
5595 mddev
->level
!= info
->level
||
5596 /* mddev->layout != info->layout || */
5597 !mddev
->persistent
!= info
->not_persistent
||
5598 mddev
->chunk_sectors
!= info
->chunk_size
>> 9 ||
5599 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5600 ((state
^info
->state
) & 0xfffffe00)
5603 /* Check there is only one change */
5604 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
5606 if (mddev
->raid_disks
!= info
->raid_disks
)
5608 if (mddev
->layout
!= info
->layout
)
5610 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
))
5617 if (mddev
->layout
!= info
->layout
) {
5619 * we don't need to do anything at the md level, the
5620 * personality will take care of it all.
5622 if (mddev
->pers
->check_reshape
== NULL
)
5625 mddev
->new_layout
= info
->layout
;
5626 rv
= mddev
->pers
->check_reshape(mddev
);
5628 mddev
->new_layout
= mddev
->layout
;
5632 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
5633 rv
= update_size(mddev
, (sector_t
)info
->size
* 2);
5635 if (mddev
->raid_disks
!= info
->raid_disks
)
5636 rv
= update_raid_disks(mddev
, info
->raid_disks
);
5638 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) {
5639 if (mddev
->pers
->quiesce
== NULL
)
5641 if (mddev
->recovery
|| mddev
->sync_thread
)
5643 if (info
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
5644 /* add the bitmap */
5647 if (mddev
->bitmap_info
.default_offset
== 0)
5649 mddev
->bitmap_info
.offset
=
5650 mddev
->bitmap_info
.default_offset
;
5651 mddev
->pers
->quiesce(mddev
, 1);
5652 rv
= bitmap_create(mddev
);
5654 rv
= bitmap_load(mddev
);
5656 bitmap_destroy(mddev
);
5657 mddev
->pers
->quiesce(mddev
, 0);
5659 /* remove the bitmap */
5662 if (mddev
->bitmap
->file
)
5664 mddev
->pers
->quiesce(mddev
, 1);
5665 bitmap_destroy(mddev
);
5666 mddev
->pers
->quiesce(mddev
, 0);
5667 mddev
->bitmap_info
.offset
= 0;
5670 md_update_sb(mddev
, 1);
5674 static int set_disk_faulty(mddev_t
*mddev
, dev_t dev
)
5678 if (mddev
->pers
== NULL
)
5681 rdev
= find_rdev(mddev
, dev
);
5685 md_error(mddev
, rdev
);
5690 * We have a problem here : there is no easy way to give a CHS
5691 * virtual geometry. We currently pretend that we have a 2 heads
5692 * 4 sectors (with a BIG number of cylinders...). This drives
5693 * dosfs just mad... ;-)
5695 static int md_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
5697 mddev_t
*mddev
= bdev
->bd_disk
->private_data
;
5701 geo
->cylinders
= mddev
->array_sectors
/ 8;
5705 static int md_ioctl(struct block_device
*bdev
, fmode_t mode
,
5706 unsigned int cmd
, unsigned long arg
)
5709 void __user
*argp
= (void __user
*)arg
;
5710 mddev_t
*mddev
= NULL
;
5713 if (!capable(CAP_SYS_ADMIN
))
5717 * Commands dealing with the RAID driver but not any
5723 err
= get_version(argp
);
5726 case PRINT_RAID_DEBUG
:
5734 autostart_arrays(arg
);
5741 * Commands creating/starting a new array:
5744 mddev
= bdev
->bd_disk
->private_data
;
5751 err
= mddev_lock(mddev
);
5754 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5761 case SET_ARRAY_INFO
:
5763 mdu_array_info_t info
;
5765 memset(&info
, 0, sizeof(info
));
5766 else if (copy_from_user(&info
, argp
, sizeof(info
))) {
5771 err
= update_array_info(mddev
, &info
);
5773 printk(KERN_WARNING
"md: couldn't update"
5774 " array info. %d\n", err
);
5779 if (!list_empty(&mddev
->disks
)) {
5781 "md: array %s already has disks!\n",
5786 if (mddev
->raid_disks
) {
5788 "md: array %s already initialised!\n",
5793 err
= set_array_info(mddev
, &info
);
5795 printk(KERN_WARNING
"md: couldn't set"
5796 " array info. %d\n", err
);
5806 * Commands querying/configuring an existing array:
5808 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
5809 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
5810 if ((!mddev
->raid_disks
&& !mddev
->external
)
5811 && cmd
!= ADD_NEW_DISK
&& cmd
!= STOP_ARRAY
5812 && cmd
!= RUN_ARRAY
&& cmd
!= SET_BITMAP_FILE
5813 && cmd
!= GET_BITMAP_FILE
) {
5819 * Commands even a read-only array can execute:
5823 case GET_ARRAY_INFO
:
5824 err
= get_array_info(mddev
, argp
);
5827 case GET_BITMAP_FILE
:
5828 err
= get_bitmap_file(mddev
, argp
);
5832 err
= get_disk_info(mddev
, argp
);
5835 case RESTART_ARRAY_RW
:
5836 err
= restart_array(mddev
);
5840 err
= do_md_stop(mddev
, 0, 1);
5844 err
= md_set_readonly(mddev
, 1);
5848 if (get_user(ro
, (int __user
*)(arg
))) {
5854 /* if the bdev is going readonly the value of mddev->ro
5855 * does not matter, no writes are coming
5860 /* are we are already prepared for writes? */
5864 /* transitioning to readauto need only happen for
5865 * arrays that call md_write_start
5868 err
= restart_array(mddev
);
5871 set_disk_ro(mddev
->gendisk
, 0);
5878 * The remaining ioctls are changing the state of the
5879 * superblock, so we do not allow them on read-only arrays.
5880 * However non-MD ioctls (e.g. get-size) will still come through
5881 * here and hit the 'default' below, so only disallow
5882 * 'md' ioctls, and switch to rw mode if started auto-readonly.
5884 if (_IOC_TYPE(cmd
) == MD_MAJOR
&& mddev
->ro
&& mddev
->pers
) {
5885 if (mddev
->ro
== 2) {
5887 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5888 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5889 md_wakeup_thread(mddev
->thread
);
5900 mdu_disk_info_t info
;
5901 if (copy_from_user(&info
, argp
, sizeof(info
)))
5904 err
= add_new_disk(mddev
, &info
);
5908 case HOT_REMOVE_DISK
:
5909 err
= hot_remove_disk(mddev
, new_decode_dev(arg
));
5913 err
= hot_add_disk(mddev
, new_decode_dev(arg
));
5916 case SET_DISK_FAULTY
:
5917 err
= set_disk_faulty(mddev
, new_decode_dev(arg
));
5921 err
= do_md_run(mddev
);
5924 case SET_BITMAP_FILE
:
5925 err
= set_bitmap_file(mddev
, (int)arg
);
5935 if (mddev
->hold_active
== UNTIL_IOCTL
&&
5937 mddev
->hold_active
= 0;
5938 mddev_unlock(mddev
);
5947 #ifdef CONFIG_COMPAT
5948 static int md_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
5949 unsigned int cmd
, unsigned long arg
)
5952 case HOT_REMOVE_DISK
:
5954 case SET_DISK_FAULTY
:
5955 case SET_BITMAP_FILE
:
5956 /* These take in integer arg, do not convert */
5959 arg
= (unsigned long)compat_ptr(arg
);
5963 return md_ioctl(bdev
, mode
, cmd
, arg
);
5965 #endif /* CONFIG_COMPAT */
5967 static int md_open(struct block_device
*bdev
, fmode_t mode
)
5970 * Succeed if we can lock the mddev, which confirms that
5971 * it isn't being stopped right now.
5973 mddev_t
*mddev
= mddev_find(bdev
->bd_dev
);
5976 if (mddev
->gendisk
!= bdev
->bd_disk
) {
5977 /* we are racing with mddev_put which is discarding this
5981 /* Wait until bdev->bd_disk is definitely gone */
5982 flush_workqueue(md_misc_wq
);
5983 /* Then retry the open from the top */
5984 return -ERESTARTSYS
;
5986 BUG_ON(mddev
!= bdev
->bd_disk
->private_data
);
5988 if ((err
= mutex_lock_interruptible(&mddev
->open_mutex
)))
5992 atomic_inc(&mddev
->openers
);
5993 mutex_unlock(&mddev
->open_mutex
);
5995 check_disk_size_change(mddev
->gendisk
, bdev
);
6000 static int md_release(struct gendisk
*disk
, fmode_t mode
)
6002 mddev_t
*mddev
= disk
->private_data
;
6005 atomic_dec(&mddev
->openers
);
6010 static const struct block_device_operations md_fops
=
6012 .owner
= THIS_MODULE
,
6014 .release
= md_release
,
6016 #ifdef CONFIG_COMPAT
6017 .compat_ioctl
= md_compat_ioctl
,
6019 .getgeo
= md_getgeo
,
6022 static int md_thread(void * arg
)
6024 mdk_thread_t
*thread
= arg
;
6027 * md_thread is a 'system-thread', it's priority should be very
6028 * high. We avoid resource deadlocks individually in each
6029 * raid personality. (RAID5 does preallocation) We also use RR and
6030 * the very same RT priority as kswapd, thus we will never get
6031 * into a priority inversion deadlock.
6033 * we definitely have to have equal or higher priority than
6034 * bdflush, otherwise bdflush will deadlock if there are too
6035 * many dirty RAID5 blocks.
6038 allow_signal(SIGKILL
);
6039 while (!kthread_should_stop()) {
6041 /* We need to wait INTERRUPTIBLE so that
6042 * we don't add to the load-average.
6043 * That means we need to be sure no signals are
6046 if (signal_pending(current
))
6047 flush_signals(current
);
6049 wait_event_interruptible_timeout
6051 test_bit(THREAD_WAKEUP
, &thread
->flags
)
6052 || kthread_should_stop(),
6055 clear_bit(THREAD_WAKEUP
, &thread
->flags
);
6056 if (!kthread_should_stop())
6057 thread
->run(thread
->mddev
);
6063 void md_wakeup_thread(mdk_thread_t
*thread
)
6066 dprintk("md: waking up MD thread %s.\n", thread
->tsk
->comm
);
6067 set_bit(THREAD_WAKEUP
, &thread
->flags
);
6068 wake_up(&thread
->wqueue
);
6072 mdk_thread_t
*md_register_thread(void (*run
) (mddev_t
*), mddev_t
*mddev
,
6075 mdk_thread_t
*thread
;
6077 thread
= kzalloc(sizeof(mdk_thread_t
), GFP_KERNEL
);
6081 init_waitqueue_head(&thread
->wqueue
);
6084 thread
->mddev
= mddev
;
6085 thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
6086 thread
->tsk
= kthread_run(md_thread
, thread
,
6088 mdname(thread
->mddev
),
6089 name
?: mddev
->pers
->name
);
6090 if (IS_ERR(thread
->tsk
)) {
6097 void md_unregister_thread(mdk_thread_t
*thread
)
6101 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread
->tsk
));
6103 kthread_stop(thread
->tsk
);
6107 void md_error(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
6114 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
6117 if (mddev
->external
)
6118 set_bit(Blocked
, &rdev
->flags
);
6120 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
6122 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
6123 __builtin_return_address(0),__builtin_return_address(1),
6124 __builtin_return_address(2),__builtin_return_address(3));
6128 if (!mddev
->pers
->error_handler
)
6130 mddev
->pers
->error_handler(mddev
,rdev
);
6131 if (mddev
->degraded
)
6132 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
6133 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
6134 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6135 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6136 md_wakeup_thread(mddev
->thread
);
6137 if (mddev
->event_work
.func
)
6138 queue_work(md_misc_wq
, &mddev
->event_work
);
6139 md_new_event_inintr(mddev
);
6142 /* seq_file implementation /proc/mdstat */
6144 static void status_unused(struct seq_file
*seq
)
6149 seq_printf(seq
, "unused devices: ");
6151 list_for_each_entry(rdev
, &pending_raid_disks
, same_set
) {
6152 char b
[BDEVNAME_SIZE
];
6154 seq_printf(seq
, "%s ",
6155 bdevname(rdev
->bdev
,b
));
6158 seq_printf(seq
, "<none>");
6160 seq_printf(seq
, "\n");
6164 static void status_resync(struct seq_file
*seq
, mddev_t
* mddev
)
6166 sector_t max_sectors
, resync
, res
;
6167 unsigned long dt
, db
;
6170 unsigned int per_milli
;
6172 resync
= mddev
->curr_resync
- atomic_read(&mddev
->recovery_active
);
6174 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
6175 max_sectors
= mddev
->resync_max_sectors
;
6177 max_sectors
= mddev
->dev_sectors
;
6180 * Should not happen.
6186 /* Pick 'scale' such that (resync>>scale)*1000 will fit
6187 * in a sector_t, and (max_sectors>>scale) will fit in a
6188 * u32, as those are the requirements for sector_div.
6189 * Thus 'scale' must be at least 10
6192 if (sizeof(sector_t
) > sizeof(unsigned long)) {
6193 while ( max_sectors
/2 > (1ULL<<(scale
+32)))
6196 res
= (resync
>>scale
)*1000;
6197 sector_div(res
, (u32
)((max_sectors
>>scale
)+1));
6201 int i
, x
= per_milli
/50, y
= 20-x
;
6202 seq_printf(seq
, "[");
6203 for (i
= 0; i
< x
; i
++)
6204 seq_printf(seq
, "=");
6205 seq_printf(seq
, ">");
6206 for (i
= 0; i
< y
; i
++)
6207 seq_printf(seq
, ".");
6208 seq_printf(seq
, "] ");
6210 seq_printf(seq
, " %s =%3u.%u%% (%llu/%llu)",
6211 (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)?
6213 (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)?
6215 (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ?
6216 "resync" : "recovery"))),
6217 per_milli
/10, per_milli
% 10,
6218 (unsigned long long) resync
/2,
6219 (unsigned long long) max_sectors
/2);
6222 * dt: time from mark until now
6223 * db: blocks written from mark until now
6224 * rt: remaining time
6226 * rt is a sector_t, so could be 32bit or 64bit.
6227 * So we divide before multiply in case it is 32bit and close
6229 * We scale the divisor (db) by 32 to avoid loosing precision
6230 * near the end of resync when the number of remaining sectors
6232 * We then divide rt by 32 after multiplying by db to compensate.
6233 * The '+1' avoids division by zero if db is very small.
6235 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
6237 db
= (mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
))
6238 - mddev
->resync_mark_cnt
;
6240 rt
= max_sectors
- resync
; /* number of remaining sectors */
6241 sector_div(rt
, db
/32+1);
6245 seq_printf(seq
, " finish=%lu.%lumin", (unsigned long)rt
/ 60,
6246 ((unsigned long)rt
% 60)/6);
6248 seq_printf(seq
, " speed=%ldK/sec", db
/2/dt
);
6251 static void *md_seq_start(struct seq_file
*seq
, loff_t
*pos
)
6253 struct list_head
*tmp
;
6263 spin_lock(&all_mddevs_lock
);
6264 list_for_each(tmp
,&all_mddevs
)
6266 mddev
= list_entry(tmp
, mddev_t
, all_mddevs
);
6268 spin_unlock(&all_mddevs_lock
);
6271 spin_unlock(&all_mddevs_lock
);
6273 return (void*)2;/* tail */
6277 static void *md_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
6279 struct list_head
*tmp
;
6280 mddev_t
*next_mddev
, *mddev
= v
;
6286 spin_lock(&all_mddevs_lock
);
6288 tmp
= all_mddevs
.next
;
6290 tmp
= mddev
->all_mddevs
.next
;
6291 if (tmp
!= &all_mddevs
)
6292 next_mddev
= mddev_get(list_entry(tmp
,mddev_t
,all_mddevs
));
6294 next_mddev
= (void*)2;
6297 spin_unlock(&all_mddevs_lock
);
6305 static void md_seq_stop(struct seq_file
*seq
, void *v
)
6309 if (mddev
&& v
!= (void*)1 && v
!= (void*)2)
6313 struct mdstat_info
{
6317 static int md_seq_show(struct seq_file
*seq
, void *v
)
6322 struct mdstat_info
*mi
= seq
->private;
6323 struct bitmap
*bitmap
;
6325 if (v
== (void*)1) {
6326 struct mdk_personality
*pers
;
6327 seq_printf(seq
, "Personalities : ");
6328 spin_lock(&pers_lock
);
6329 list_for_each_entry(pers
, &pers_list
, list
)
6330 seq_printf(seq
, "[%s] ", pers
->name
);
6332 spin_unlock(&pers_lock
);
6333 seq_printf(seq
, "\n");
6334 mi
->event
= atomic_read(&md_event_count
);
6337 if (v
== (void*)2) {
6342 if (mddev_lock(mddev
) < 0)
6345 if (mddev
->pers
|| mddev
->raid_disks
|| !list_empty(&mddev
->disks
)) {
6346 seq_printf(seq
, "%s : %sactive", mdname(mddev
),
6347 mddev
->pers
? "" : "in");
6350 seq_printf(seq
, " (read-only)");
6352 seq_printf(seq
, " (auto-read-only)");
6353 seq_printf(seq
, " %s", mddev
->pers
->name
);
6357 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
6358 char b
[BDEVNAME_SIZE
];
6359 seq_printf(seq
, " %s[%d]",
6360 bdevname(rdev
->bdev
,b
), rdev
->desc_nr
);
6361 if (test_bit(WriteMostly
, &rdev
->flags
))
6362 seq_printf(seq
, "(W)");
6363 if (test_bit(Faulty
, &rdev
->flags
)) {
6364 seq_printf(seq
, "(F)");
6366 } else if (rdev
->raid_disk
< 0)
6367 seq_printf(seq
, "(S)"); /* spare */
6368 sectors
+= rdev
->sectors
;
6371 if (!list_empty(&mddev
->disks
)) {
6373 seq_printf(seq
, "\n %llu blocks",
6374 (unsigned long long)
6375 mddev
->array_sectors
/ 2);
6377 seq_printf(seq
, "\n %llu blocks",
6378 (unsigned long long)sectors
/ 2);
6380 if (mddev
->persistent
) {
6381 if (mddev
->major_version
!= 0 ||
6382 mddev
->minor_version
!= 90) {
6383 seq_printf(seq
," super %d.%d",
6384 mddev
->major_version
,
6385 mddev
->minor_version
);
6387 } else if (mddev
->external
)
6388 seq_printf(seq
, " super external:%s",
6389 mddev
->metadata_type
);
6391 seq_printf(seq
, " super non-persistent");
6394 mddev
->pers
->status(seq
, mddev
);
6395 seq_printf(seq
, "\n ");
6396 if (mddev
->pers
->sync_request
) {
6397 if (mddev
->curr_resync
> 2) {
6398 status_resync(seq
, mddev
);
6399 seq_printf(seq
, "\n ");
6400 } else if (mddev
->curr_resync
== 1 || mddev
->curr_resync
== 2)
6401 seq_printf(seq
, "\tresync=DELAYED\n ");
6402 else if (mddev
->recovery_cp
< MaxSector
)
6403 seq_printf(seq
, "\tresync=PENDING\n ");
6406 seq_printf(seq
, "\n ");
6408 if ((bitmap
= mddev
->bitmap
)) {
6409 unsigned long chunk_kb
;
6410 unsigned long flags
;
6411 spin_lock_irqsave(&bitmap
->lock
, flags
);
6412 chunk_kb
= mddev
->bitmap_info
.chunksize
>> 10;
6413 seq_printf(seq
, "bitmap: %lu/%lu pages [%luKB], "
6415 bitmap
->pages
- bitmap
->missing_pages
,
6417 (bitmap
->pages
- bitmap
->missing_pages
)
6418 << (PAGE_SHIFT
- 10),
6419 chunk_kb
? chunk_kb
: mddev
->bitmap_info
.chunksize
,
6420 chunk_kb
? "KB" : "B");
6422 seq_printf(seq
, ", file: ");
6423 seq_path(seq
, &bitmap
->file
->f_path
, " \t\n");
6426 seq_printf(seq
, "\n");
6427 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
6430 seq_printf(seq
, "\n");
6432 mddev_unlock(mddev
);
6437 static const struct seq_operations md_seq_ops
= {
6438 .start
= md_seq_start
,
6439 .next
= md_seq_next
,
6440 .stop
= md_seq_stop
,
6441 .show
= md_seq_show
,
6444 static int md_seq_open(struct inode
*inode
, struct file
*file
)
6447 struct mdstat_info
*mi
= kmalloc(sizeof(*mi
), GFP_KERNEL
);
6451 error
= seq_open(file
, &md_seq_ops
);
6455 struct seq_file
*p
= file
->private_data
;
6457 mi
->event
= atomic_read(&md_event_count
);
6462 static unsigned int mdstat_poll(struct file
*filp
, poll_table
*wait
)
6464 struct seq_file
*m
= filp
->private_data
;
6465 struct mdstat_info
*mi
= m
->private;
6468 poll_wait(filp
, &md_event_waiters
, wait
);
6470 /* always allow read */
6471 mask
= POLLIN
| POLLRDNORM
;
6473 if (mi
->event
!= atomic_read(&md_event_count
))
6474 mask
|= POLLERR
| POLLPRI
;
6478 static const struct file_operations md_seq_fops
= {
6479 .owner
= THIS_MODULE
,
6480 .open
= md_seq_open
,
6482 .llseek
= seq_lseek
,
6483 .release
= seq_release_private
,
6484 .poll
= mdstat_poll
,
6487 int register_md_personality(struct mdk_personality
*p
)
6489 spin_lock(&pers_lock
);
6490 list_add_tail(&p
->list
, &pers_list
);
6491 printk(KERN_INFO
"md: %s personality registered for level %d\n", p
->name
, p
->level
);
6492 spin_unlock(&pers_lock
);
6496 int unregister_md_personality(struct mdk_personality
*p
)
6498 printk(KERN_INFO
"md: %s personality unregistered\n", p
->name
);
6499 spin_lock(&pers_lock
);
6500 list_del_init(&p
->list
);
6501 spin_unlock(&pers_lock
);
6505 static int is_mddev_idle(mddev_t
*mddev
, int init
)
6513 rdev_for_each_rcu(rdev
, mddev
) {
6514 struct gendisk
*disk
= rdev
->bdev
->bd_contains
->bd_disk
;
6515 curr_events
= (int)part_stat_read(&disk
->part0
, sectors
[0]) +
6516 (int)part_stat_read(&disk
->part0
, sectors
[1]) -
6517 atomic_read(&disk
->sync_io
);
6518 /* sync IO will cause sync_io to increase before the disk_stats
6519 * as sync_io is counted when a request starts, and
6520 * disk_stats is counted when it completes.
6521 * So resync activity will cause curr_events to be smaller than
6522 * when there was no such activity.
6523 * non-sync IO will cause disk_stat to increase without
6524 * increasing sync_io so curr_events will (eventually)
6525 * be larger than it was before. Once it becomes
6526 * substantially larger, the test below will cause
6527 * the array to appear non-idle, and resync will slow
6529 * If there is a lot of outstanding resync activity when
6530 * we set last_event to curr_events, then all that activity
6531 * completing might cause the array to appear non-idle
6532 * and resync will be slowed down even though there might
6533 * not have been non-resync activity. This will only
6534 * happen once though. 'last_events' will soon reflect
6535 * the state where there is little or no outstanding
6536 * resync requests, and further resync activity will
6537 * always make curr_events less than last_events.
6540 if (init
|| curr_events
- rdev
->last_events
> 64) {
6541 rdev
->last_events
= curr_events
;
6549 void md_done_sync(mddev_t
*mddev
, int blocks
, int ok
)
6551 /* another "blocks" (512byte) blocks have been synced */
6552 atomic_sub(blocks
, &mddev
->recovery_active
);
6553 wake_up(&mddev
->recovery_wait
);
6555 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6556 md_wakeup_thread(mddev
->thread
);
6557 // stop recovery, signal do_sync ....
6562 /* md_write_start(mddev, bi)
6563 * If we need to update some array metadata (e.g. 'active' flag
6564 * in superblock) before writing, schedule a superblock update
6565 * and wait for it to complete.
6567 void md_write_start(mddev_t
*mddev
, struct bio
*bi
)
6570 if (bio_data_dir(bi
) != WRITE
)
6573 BUG_ON(mddev
->ro
== 1);
6574 if (mddev
->ro
== 2) {
6575 /* need to switch to read/write */
6577 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6578 md_wakeup_thread(mddev
->thread
);
6579 md_wakeup_thread(mddev
->sync_thread
);
6582 atomic_inc(&mddev
->writes_pending
);
6583 if (mddev
->safemode
== 1)
6584 mddev
->safemode
= 0;
6585 if (mddev
->in_sync
) {
6586 spin_lock_irq(&mddev
->write_lock
);
6587 if (mddev
->in_sync
) {
6589 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6590 set_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
6591 md_wakeup_thread(mddev
->thread
);
6594 spin_unlock_irq(&mddev
->write_lock
);
6597 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6598 wait_event(mddev
->sb_wait
,
6599 !test_bit(MD_CHANGE_PENDING
, &mddev
->flags
));
6602 void md_write_end(mddev_t
*mddev
)
6604 if (atomic_dec_and_test(&mddev
->writes_pending
)) {
6605 if (mddev
->safemode
== 2)
6606 md_wakeup_thread(mddev
->thread
);
6607 else if (mddev
->safemode_delay
)
6608 mod_timer(&mddev
->safemode_timer
, jiffies
+ mddev
->safemode_delay
);
6612 /* md_allow_write(mddev)
6613 * Calling this ensures that the array is marked 'active' so that writes
6614 * may proceed without blocking. It is important to call this before
6615 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6616 * Must be called with mddev_lock held.
6618 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6619 * is dropped, so return -EAGAIN after notifying userspace.
6621 int md_allow_write(mddev_t
*mddev
)
6627 if (!mddev
->pers
->sync_request
)
6630 spin_lock_irq(&mddev
->write_lock
);
6631 if (mddev
->in_sync
) {
6633 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6634 set_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
6635 if (mddev
->safemode_delay
&&
6636 mddev
->safemode
== 0)
6637 mddev
->safemode
= 1;
6638 spin_unlock_irq(&mddev
->write_lock
);
6639 md_update_sb(mddev
, 0);
6640 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6642 spin_unlock_irq(&mddev
->write_lock
);
6644 if (test_bit(MD_CHANGE_PENDING
, &mddev
->flags
))
6649 EXPORT_SYMBOL_GPL(md_allow_write
);
6651 void md_unplug(mddev_t
*mddev
)
6654 blk_unplug(mddev
->queue
);
6656 mddev
->plug
->unplug_fn(mddev
->plug
);
6659 #define SYNC_MARKS 10
6660 #define SYNC_MARK_STEP (3*HZ)
6661 void md_do_sync(mddev_t
*mddev
)
6664 unsigned int currspeed
= 0,
6666 sector_t max_sectors
,j
, io_sectors
;
6667 unsigned long mark
[SYNC_MARKS
];
6668 sector_t mark_cnt
[SYNC_MARKS
];
6670 struct list_head
*tmp
;
6671 sector_t last_check
;
6676 /* just incase thread restarts... */
6677 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
6679 if (mddev
->ro
) /* never try to sync a read-only array */
6682 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
6683 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
6684 desc
= "data-check";
6685 else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
6686 desc
= "requested-resync";
6689 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
6694 /* we overload curr_resync somewhat here.
6695 * 0 == not engaged in resync at all
6696 * 2 == checking that there is no conflict with another sync
6697 * 1 == like 2, but have yielded to allow conflicting resync to
6699 * other == active in resync - this many blocks
6701 * Before starting a resync we must have set curr_resync to
6702 * 2, and then checked that every "conflicting" array has curr_resync
6703 * less than ours. When we find one that is the same or higher
6704 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6705 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6706 * This will mean we have to start checking from the beginning again.
6711 mddev
->curr_resync
= 2;
6714 if (kthread_should_stop())
6715 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6717 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
6719 for_each_mddev(mddev2
, tmp
) {
6720 if (mddev2
== mddev
)
6722 if (!mddev
->parallel_resync
6723 && mddev2
->curr_resync
6724 && match_mddev_units(mddev
, mddev2
)) {
6726 if (mddev
< mddev2
&& mddev
->curr_resync
== 2) {
6727 /* arbitrarily yield */
6728 mddev
->curr_resync
= 1;
6729 wake_up(&resync_wait
);
6731 if (mddev
> mddev2
&& mddev
->curr_resync
== 1)
6732 /* no need to wait here, we can wait the next
6733 * time 'round when curr_resync == 2
6736 /* We need to wait 'interruptible' so as not to
6737 * contribute to the load average, and not to
6738 * be caught by 'softlockup'
6740 prepare_to_wait(&resync_wait
, &wq
, TASK_INTERRUPTIBLE
);
6741 if (!kthread_should_stop() &&
6742 mddev2
->curr_resync
>= mddev
->curr_resync
) {
6743 printk(KERN_INFO
"md: delaying %s of %s"
6744 " until %s has finished (they"
6745 " share one or more physical units)\n",
6746 desc
, mdname(mddev
), mdname(mddev2
));
6748 if (signal_pending(current
))
6749 flush_signals(current
);
6751 finish_wait(&resync_wait
, &wq
);
6754 finish_wait(&resync_wait
, &wq
);
6757 } while (mddev
->curr_resync
< 2);
6760 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
6761 /* resync follows the size requested by the personality,
6762 * which defaults to physical size, but can be virtual size
6764 max_sectors
= mddev
->resync_max_sectors
;
6765 mddev
->resync_mismatches
= 0;
6766 /* we don't use the checkpoint if there's a bitmap */
6767 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
6768 j
= mddev
->resync_min
;
6769 else if (!mddev
->bitmap
)
6770 j
= mddev
->recovery_cp
;
6772 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
6773 max_sectors
= mddev
->dev_sectors
;
6775 /* recovery follows the physical size of devices */
6776 max_sectors
= mddev
->dev_sectors
;
6779 list_for_each_entry_rcu(rdev
, &mddev
->disks
, same_set
)
6780 if (rdev
->raid_disk
>= 0 &&
6781 !test_bit(Faulty
, &rdev
->flags
) &&
6782 !test_bit(In_sync
, &rdev
->flags
) &&
6783 rdev
->recovery_offset
< j
)
6784 j
= rdev
->recovery_offset
;
6788 printk(KERN_INFO
"md: %s of RAID array %s\n", desc
, mdname(mddev
));
6789 printk(KERN_INFO
"md: minimum _guaranteed_ speed:"
6790 " %d KB/sec/disk.\n", speed_min(mddev
));
6791 printk(KERN_INFO
"md: using maximum available idle IO bandwidth "
6792 "(but not more than %d KB/sec) for %s.\n",
6793 speed_max(mddev
), desc
);
6795 is_mddev_idle(mddev
, 1); /* this initializes IO event counters */
6798 for (m
= 0; m
< SYNC_MARKS
; m
++) {
6800 mark_cnt
[m
] = io_sectors
;
6803 mddev
->resync_mark
= mark
[last_mark
];
6804 mddev
->resync_mark_cnt
= mark_cnt
[last_mark
];
6807 * Tune reconstruction:
6809 window
= 32*(PAGE_SIZE
/512);
6810 printk(KERN_INFO
"md: using %dk window, over a total of %llu blocks.\n",
6811 window
/2,(unsigned long long) max_sectors
/2);
6813 atomic_set(&mddev
->recovery_active
, 0);
6818 "md: resuming %s of %s from checkpoint.\n",
6819 desc
, mdname(mddev
));
6820 mddev
->curr_resync
= j
;
6822 mddev
->curr_resync_completed
= mddev
->curr_resync
;
6824 while (j
< max_sectors
) {
6829 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
6830 ((mddev
->curr_resync
> mddev
->curr_resync_completed
&&
6831 (mddev
->curr_resync
- mddev
->curr_resync_completed
)
6832 > (max_sectors
>> 4)) ||
6833 (j
- mddev
->curr_resync_completed
)*2
6834 >= mddev
->resync_max
- mddev
->curr_resync_completed
6836 /* time to update curr_resync_completed */
6838 wait_event(mddev
->recovery_wait
,
6839 atomic_read(&mddev
->recovery_active
) == 0);
6840 mddev
->curr_resync_completed
=
6842 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6843 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
6846 while (j
>= mddev
->resync_max
&& !kthread_should_stop()) {
6847 /* As this condition is controlled by user-space,
6848 * we can block indefinitely, so use '_interruptible'
6849 * to avoid triggering warnings.
6851 flush_signals(current
); /* just in case */
6852 wait_event_interruptible(mddev
->recovery_wait
,
6853 mddev
->resync_max
> j
6854 || kthread_should_stop());
6857 if (kthread_should_stop())
6860 sectors
= mddev
->pers
->sync_request(mddev
, j
, &skipped
,
6861 currspeed
< speed_min(mddev
));
6863 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6867 if (!skipped
) { /* actual IO requested */
6868 io_sectors
+= sectors
;
6869 atomic_add(sectors
, &mddev
->recovery_active
);
6873 if (j
>1) mddev
->curr_resync
= j
;
6874 mddev
->curr_mark_cnt
= io_sectors
;
6875 if (last_check
== 0)
6876 /* this is the earliers that rebuilt will be
6877 * visible in /proc/mdstat
6879 md_new_event(mddev
);
6881 if (last_check
+ window
> io_sectors
|| j
== max_sectors
)
6884 last_check
= io_sectors
;
6886 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
6890 if (time_after_eq(jiffies
, mark
[last_mark
] + SYNC_MARK_STEP
)) {
6892 int next
= (last_mark
+1) % SYNC_MARKS
;
6894 mddev
->resync_mark
= mark
[next
];
6895 mddev
->resync_mark_cnt
= mark_cnt
[next
];
6896 mark
[next
] = jiffies
;
6897 mark_cnt
[next
] = io_sectors
- atomic_read(&mddev
->recovery_active
);
6902 if (kthread_should_stop())
6907 * this loop exits only if either when we are slower than
6908 * the 'hard' speed limit, or the system was IO-idle for
6910 * the system might be non-idle CPU-wise, but we only care
6911 * about not overloading the IO subsystem. (things like an
6912 * e2fsck being done on the RAID array should execute fast)
6917 currspeed
= ((unsigned long)(io_sectors
-mddev
->resync_mark_cnt
))/2
6918 /((jiffies
-mddev
->resync_mark
)/HZ
+1) +1;
6920 if (currspeed
> speed_min(mddev
)) {
6921 if ((currspeed
> speed_max(mddev
)) ||
6922 !is_mddev_idle(mddev
, 0)) {
6928 printk(KERN_INFO
"md: %s: %s done.\n",mdname(mddev
), desc
);
6930 * this also signals 'finished resyncing' to md_stop
6935 wait_event(mddev
->recovery_wait
, !atomic_read(&mddev
->recovery_active
));
6937 /* tell personality that we are finished */
6938 mddev
->pers
->sync_request(mddev
, max_sectors
, &skipped
, 1);
6940 if (!test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
) &&
6941 mddev
->curr_resync
> 2) {
6942 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
6943 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
6944 if (mddev
->curr_resync
>= mddev
->recovery_cp
) {
6946 "md: checkpointing %s of %s.\n",
6947 desc
, mdname(mddev
));
6948 mddev
->recovery_cp
= mddev
->curr_resync
;
6951 mddev
->recovery_cp
= MaxSector
;
6953 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
6954 mddev
->curr_resync
= MaxSector
;
6956 list_for_each_entry_rcu(rdev
, &mddev
->disks
, same_set
)
6957 if (rdev
->raid_disk
>= 0 &&
6958 mddev
->delta_disks
>= 0 &&
6959 !test_bit(Faulty
, &rdev
->flags
) &&
6960 !test_bit(In_sync
, &rdev
->flags
) &&
6961 rdev
->recovery_offset
< mddev
->curr_resync
)
6962 rdev
->recovery_offset
= mddev
->curr_resync
;
6966 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
6969 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
6970 /* We completed so min/max setting can be forgotten if used. */
6971 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
6972 mddev
->resync_min
= 0;
6973 mddev
->resync_max
= MaxSector
;
6974 } else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
6975 mddev
->resync_min
= mddev
->curr_resync_completed
;
6976 mddev
->curr_resync
= 0;
6977 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
6978 mddev
->curr_resync_completed
= 0;
6979 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
6980 wake_up(&resync_wait
);
6981 set_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
6982 md_wakeup_thread(mddev
->thread
);
6987 * got a signal, exit.
6990 "md: md_do_sync() got signal ... exiting\n");
6991 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6995 EXPORT_SYMBOL_GPL(md_do_sync
);
6998 static int remove_and_add_spares(mddev_t
*mddev
)
7003 mddev
->curr_resync_completed
= 0;
7005 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
7006 if (rdev
->raid_disk
>= 0 &&
7007 !test_bit(Blocked
, &rdev
->flags
) &&
7008 (test_bit(Faulty
, &rdev
->flags
) ||
7009 ! test_bit(In_sync
, &rdev
->flags
)) &&
7010 atomic_read(&rdev
->nr_pending
)==0) {
7011 if (mddev
->pers
->hot_remove_disk(
7012 mddev
, rdev
->raid_disk
)==0) {
7014 sprintf(nm
,"rd%d", rdev
->raid_disk
);
7015 sysfs_remove_link(&mddev
->kobj
, nm
);
7016 rdev
->raid_disk
= -1;
7020 if (mddev
->degraded
&& ! mddev
->ro
&& !mddev
->recovery_disabled
) {
7021 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
7022 if (rdev
->raid_disk
>= 0 &&
7023 !test_bit(In_sync
, &rdev
->flags
) &&
7024 !test_bit(Blocked
, &rdev
->flags
))
7026 if (rdev
->raid_disk
< 0
7027 && !test_bit(Faulty
, &rdev
->flags
)) {
7028 rdev
->recovery_offset
= 0;
7030 hot_add_disk(mddev
, rdev
) == 0) {
7032 sprintf(nm
, "rd%d", rdev
->raid_disk
);
7033 if (sysfs_create_link(&mddev
->kobj
,
7035 /* failure here is OK */;
7037 md_new_event(mddev
);
7038 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
7047 static void reap_sync_thread(mddev_t
*mddev
)
7051 /* resync has finished, collect result */
7052 md_unregister_thread(mddev
->sync_thread
);
7053 mddev
->sync_thread
= NULL
;
7054 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
7055 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
7057 /* activate any spares */
7058 if (mddev
->pers
->spare_active(mddev
))
7059 sysfs_notify(&mddev
->kobj
, NULL
,
7062 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
7063 mddev
->pers
->finish_reshape
)
7064 mddev
->pers
->finish_reshape(mddev
);
7065 md_update_sb(mddev
, 1);
7067 /* if array is no-longer degraded, then any saved_raid_disk
7068 * information must be scrapped
7070 if (!mddev
->degraded
)
7071 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
7072 rdev
->saved_raid_disk
= -1;
7074 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7075 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7076 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7077 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
7078 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7079 /* flag recovery needed just to double check */
7080 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7081 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7082 md_new_event(mddev
);
7086 * This routine is regularly called by all per-raid-array threads to
7087 * deal with generic issues like resync and super-block update.
7088 * Raid personalities that don't have a thread (linear/raid0) do not
7089 * need this as they never do any recovery or update the superblock.
7091 * It does not do any resync itself, but rather "forks" off other threads
7092 * to do that as needed.
7093 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7094 * "->recovery" and create a thread at ->sync_thread.
7095 * When the thread finishes it sets MD_RECOVERY_DONE
7096 * and wakeups up this thread which will reap the thread and finish up.
7097 * This thread also removes any faulty devices (with nr_pending == 0).
7099 * The overall approach is:
7100 * 1/ if the superblock needs updating, update it.
7101 * 2/ If a recovery thread is running, don't do anything else.
7102 * 3/ If recovery has finished, clean up, possibly marking spares active.
7103 * 4/ If there are any faulty devices, remove them.
7104 * 5/ If array is degraded, try to add spares devices
7105 * 6/ If array has spares or is not in-sync, start a resync thread.
7107 void md_check_recovery(mddev_t
*mddev
)
7110 bitmap_daemon_work(mddev
);
7115 if (signal_pending(current
)) {
7116 if (mddev
->pers
->sync_request
&& !mddev
->external
) {
7117 printk(KERN_INFO
"md: %s in immediate safe mode\n",
7119 mddev
->safemode
= 2;
7121 flush_signals(current
);
7124 if (mddev
->ro
&& !test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
7127 (mddev
->flags
& ~ (1<<MD_CHANGE_PENDING
)) ||
7128 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
7129 test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
7130 (mddev
->external
== 0 && mddev
->safemode
== 1) ||
7131 (mddev
->safemode
== 2 && ! atomic_read(&mddev
->writes_pending
)
7132 && !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
7136 if (mddev_trylock(mddev
)) {
7140 /* Only thing we do on a ro array is remove
7143 remove_and_add_spares(mddev
);
7144 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7148 if (!mddev
->external
) {
7150 spin_lock_irq(&mddev
->write_lock
);
7151 if (mddev
->safemode
&&
7152 !atomic_read(&mddev
->writes_pending
) &&
7154 mddev
->recovery_cp
== MaxSector
) {
7157 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
7159 if (mddev
->safemode
== 1)
7160 mddev
->safemode
= 0;
7161 spin_unlock_irq(&mddev
->write_lock
);
7163 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
7167 md_update_sb(mddev
, 0);
7169 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
7170 !test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
)) {
7171 /* resync/recovery still happening */
7172 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7175 if (mddev
->sync_thread
) {
7176 reap_sync_thread(mddev
);
7179 /* Set RUNNING before clearing NEEDED to avoid
7180 * any transients in the value of "sync_action".
7182 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7183 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7184 /* Clear some bits that don't mean anything, but
7187 clear_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7188 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
7190 if (test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
7192 /* no recovery is running.
7193 * remove any failed drives, then
7194 * add spares if possible.
7195 * Spare are also removed and re-added, to allow
7196 * the personality to fail the re-add.
7199 if (mddev
->reshape_position
!= MaxSector
) {
7200 if (mddev
->pers
->check_reshape
== NULL
||
7201 mddev
->pers
->check_reshape(mddev
) != 0)
7202 /* Cannot proceed */
7204 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7205 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7206 } else if ((spares
= remove_and_add_spares(mddev
))) {
7207 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7208 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7209 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
7210 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7211 } else if (mddev
->recovery_cp
< MaxSector
) {
7212 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7213 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7214 } else if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
7215 /* nothing to be done ... */
7218 if (mddev
->pers
->sync_request
) {
7219 if (spares
&& mddev
->bitmap
&& ! mddev
->bitmap
->file
) {
7220 /* We are adding a device or devices to an array
7221 * which has the bitmap stored on all devices.
7222 * So make sure all bitmap pages get written
7224 bitmap_write_all(mddev
->bitmap
);
7226 mddev
->sync_thread
= md_register_thread(md_do_sync
,
7229 if (!mddev
->sync_thread
) {
7230 printk(KERN_ERR
"%s: could not start resync"
7233 /* leave the spares where they are, it shouldn't hurt */
7234 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7235 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7236 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7237 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
7238 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7240 md_wakeup_thread(mddev
->sync_thread
);
7241 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7242 md_new_event(mddev
);
7245 if (!mddev
->sync_thread
) {
7246 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7247 if (test_and_clear_bit(MD_RECOVERY_RECOVER
,
7249 if (mddev
->sysfs_action
)
7250 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7252 mddev_unlock(mddev
);
7256 void md_wait_for_blocked_rdev(mdk_rdev_t
*rdev
, mddev_t
*mddev
)
7258 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
7259 wait_event_timeout(rdev
->blocked_wait
,
7260 !test_bit(Blocked
, &rdev
->flags
),
7261 msecs_to_jiffies(5000));
7262 rdev_dec_pending(rdev
, mddev
);
7264 EXPORT_SYMBOL(md_wait_for_blocked_rdev
);
7266 static int md_notify_reboot(struct notifier_block
*this,
7267 unsigned long code
, void *x
)
7269 struct list_head
*tmp
;
7272 if ((code
== SYS_DOWN
) || (code
== SYS_HALT
) || (code
== SYS_POWER_OFF
)) {
7274 printk(KERN_INFO
"md: stopping all md devices.\n");
7276 for_each_mddev(mddev
, tmp
)
7277 if (mddev_trylock(mddev
)) {
7278 /* Force a switch to readonly even array
7279 * appears to still be in use. Hence
7282 md_set_readonly(mddev
, 100);
7283 mddev_unlock(mddev
);
7286 * certain more exotic SCSI devices are known to be
7287 * volatile wrt too early system reboots. While the
7288 * right place to handle this issue is the given
7289 * driver, we do want to have a safe RAID driver ...
7296 static struct notifier_block md_notifier
= {
7297 .notifier_call
= md_notify_reboot
,
7299 .priority
= INT_MAX
, /* before any real devices */
7302 static void md_geninit(void)
7304 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t
));
7306 proc_create("mdstat", S_IRUGO
, NULL
, &md_seq_fops
);
7309 static int __init
md_init(void)
7313 md_wq
= alloc_workqueue("md", WQ_RESCUER
, 0);
7317 md_misc_wq
= alloc_workqueue("md_misc", 0, 0);
7321 if ((ret
= register_blkdev(MD_MAJOR
, "md")) < 0)
7324 if ((ret
= register_blkdev(0, "mdp")) < 0)
7328 blk_register_region(MKDEV(MD_MAJOR
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
7329 md_probe
, NULL
, NULL
);
7330 blk_register_region(MKDEV(mdp_major
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
7331 md_probe
, NULL
, NULL
);
7333 register_reboot_notifier(&md_notifier
);
7334 raid_table_header
= register_sysctl_table(raid_root_table
);
7340 unregister_blkdev(MD_MAJOR
, "md");
7342 destroy_workqueue(md_misc_wq
);
7344 destroy_workqueue(md_wq
);
7352 * Searches all registered partitions for autorun RAID arrays
7356 static LIST_HEAD(all_detected_devices
);
7357 struct detected_devices_node
{
7358 struct list_head list
;
7362 void md_autodetect_dev(dev_t dev
)
7364 struct detected_devices_node
*node_detected_dev
;
7366 node_detected_dev
= kzalloc(sizeof(*node_detected_dev
), GFP_KERNEL
);
7367 if (node_detected_dev
) {
7368 node_detected_dev
->dev
= dev
;
7369 list_add_tail(&node_detected_dev
->list
, &all_detected_devices
);
7371 printk(KERN_CRIT
"md: md_autodetect_dev: kzalloc failed"
7372 ", skipping dev(%d,%d)\n", MAJOR(dev
), MINOR(dev
));
7377 static void autostart_arrays(int part
)
7380 struct detected_devices_node
*node_detected_dev
;
7382 int i_scanned
, i_passed
;
7387 printk(KERN_INFO
"md: Autodetecting RAID arrays.\n");
7389 while (!list_empty(&all_detected_devices
) && i_scanned
< INT_MAX
) {
7391 node_detected_dev
= list_entry(all_detected_devices
.next
,
7392 struct detected_devices_node
, list
);
7393 list_del(&node_detected_dev
->list
);
7394 dev
= node_detected_dev
->dev
;
7395 kfree(node_detected_dev
);
7396 rdev
= md_import_device(dev
,0, 90);
7400 if (test_bit(Faulty
, &rdev
->flags
)) {
7404 set_bit(AutoDetected
, &rdev
->flags
);
7405 list_add(&rdev
->same_set
, &pending_raid_disks
);
7409 printk(KERN_INFO
"md: Scanned %d and added %d devices.\n",
7410 i_scanned
, i_passed
);
7412 autorun_devices(part
);
7415 #endif /* !MODULE */
7417 static __exit
void md_exit(void)
7420 struct list_head
*tmp
;
7422 blk_unregister_region(MKDEV(MD_MAJOR
,0), 1U << MINORBITS
);
7423 blk_unregister_region(MKDEV(mdp_major
,0), 1U << MINORBITS
);
7425 unregister_blkdev(MD_MAJOR
,"md");
7426 unregister_blkdev(mdp_major
, "mdp");
7427 unregister_reboot_notifier(&md_notifier
);
7428 unregister_sysctl_table(raid_table_header
);
7429 remove_proc_entry("mdstat", NULL
);
7430 for_each_mddev(mddev
, tmp
) {
7431 export_array(mddev
);
7432 mddev
->hold_active
= 0;
7434 destroy_workqueue(md_misc_wq
);
7435 destroy_workqueue(md_wq
);
7438 subsys_initcall(md_init
);
7439 module_exit(md_exit
)
7441 static int get_ro(char *buffer
, struct kernel_param
*kp
)
7443 return sprintf(buffer
, "%d", start_readonly
);
7445 static int set_ro(const char *val
, struct kernel_param
*kp
)
7448 int num
= simple_strtoul(val
, &e
, 10);
7449 if (*val
&& (*e
== '\0' || *e
== '\n')) {
7450 start_readonly
= num
;
7456 module_param_call(start_ro
, set_ro
, get_ro
, NULL
, S_IRUSR
|S_IWUSR
);
7457 module_param(start_dirty_degraded
, int, S_IRUGO
|S_IWUSR
);
7459 module_param_call(new_array
, add_named_array
, NULL
, NULL
, S_IWUSR
);
7461 EXPORT_SYMBOL(register_md_personality
);
7462 EXPORT_SYMBOL(unregister_md_personality
);
7463 EXPORT_SYMBOL(md_error
);
7464 EXPORT_SYMBOL(md_done_sync
);
7465 EXPORT_SYMBOL(md_write_start
);
7466 EXPORT_SYMBOL(md_write_end
);
7467 EXPORT_SYMBOL(md_register_thread
);
7468 EXPORT_SYMBOL(md_unregister_thread
);
7469 EXPORT_SYMBOL(md_wakeup_thread
);
7470 EXPORT_SYMBOL(md_check_recovery
);
7471 MODULE_LICENSE("GPL");
7472 MODULE_DESCRIPTION("MD RAID framework");
7474 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR
);