2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
5 completely rewritten, based on the MD driver code from Marc Zyngier
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
20 Neil Brown <neilb@cse.unsw.edu.au>.
22 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
25 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 #include <linux/kthread.h>
36 #include <linux/blkdev.h>
37 #include <linux/sysctl.h>
38 #include <linux/seq_file.h>
39 #include <linux/mutex.h>
40 #include <linux/buffer_head.h> /* for invalidate_bdev */
41 #include <linux/poll.h>
42 #include <linux/ctype.h>
43 #include <linux/string.h>
44 #include <linux/hdreg.h>
45 #include <linux/proc_fs.h>
46 #include <linux/random.h>
47 #include <linux/reboot.h>
48 #include <linux/file.h>
49 #include <linux/compat.h>
50 #include <linux/delay.h>
51 #include <linux/raid/md_p.h>
52 #include <linux/raid/md_u.h>
53 #include <linux/slab.h>
58 #define dprintk(x...) ((void)(DEBUG && printk(x)))
61 static void autostart_arrays(int part
);
64 static LIST_HEAD(pers_list
);
65 static DEFINE_SPINLOCK(pers_lock
);
67 static void md_print_devices(void);
69 static DECLARE_WAIT_QUEUE_HEAD(resync_wait
);
70 static struct workqueue_struct
*md_wq
;
71 static struct workqueue_struct
*md_misc_wq
;
73 #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
76 * Default number of read corrections we'll attempt on an rdev
77 * before ejecting it from the array. We divide the read error
78 * count by 2 for every hour elapsed between read errors.
80 #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
82 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
83 * is 1000 KB/sec, so the extra system load does not show up that much.
84 * Increase it if you want to have more _guaranteed_ speed. Note that
85 * the RAID driver will use the maximum available bandwidth if the IO
86 * subsystem is idle. There is also an 'absolute maximum' reconstruction
87 * speed limit - in case reconstruction slows down your system despite
90 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
91 * or /sys/block/mdX/md/sync_speed_{min,max}
94 static int sysctl_speed_limit_min
= 1000;
95 static int sysctl_speed_limit_max
= 200000;
96 static inline int speed_min(mddev_t
*mddev
)
98 return mddev
->sync_speed_min
?
99 mddev
->sync_speed_min
: sysctl_speed_limit_min
;
102 static inline int speed_max(mddev_t
*mddev
)
104 return mddev
->sync_speed_max
?
105 mddev
->sync_speed_max
: sysctl_speed_limit_max
;
108 static struct ctl_table_header
*raid_table_header
;
110 static ctl_table raid_table
[] = {
112 .procname
= "speed_limit_min",
113 .data
= &sysctl_speed_limit_min
,
114 .maxlen
= sizeof(int),
115 .mode
= S_IRUGO
|S_IWUSR
,
116 .proc_handler
= proc_dointvec
,
119 .procname
= "speed_limit_max",
120 .data
= &sysctl_speed_limit_max
,
121 .maxlen
= sizeof(int),
122 .mode
= S_IRUGO
|S_IWUSR
,
123 .proc_handler
= proc_dointvec
,
128 static ctl_table raid_dir_table
[] = {
132 .mode
= S_IRUGO
|S_IXUGO
,
138 static ctl_table raid_root_table
[] = {
143 .child
= raid_dir_table
,
148 static const struct block_device_operations md_fops
;
150 static int start_readonly
;
153 * like bio_clone, but with a local bio set
156 static void mddev_bio_destructor(struct bio
*bio
)
158 mddev_t
*mddev
, **mddevp
;
163 bio_free(bio
, mddev
->bio_set
);
166 struct bio
*bio_alloc_mddev(gfp_t gfp_mask
, int nr_iovecs
,
172 if (!mddev
|| !mddev
->bio_set
)
173 return bio_alloc(gfp_mask
, nr_iovecs
);
175 b
= bio_alloc_bioset(gfp_mask
, nr_iovecs
,
181 b
->bi_destructor
= mddev_bio_destructor
;
184 EXPORT_SYMBOL_GPL(bio_alloc_mddev
);
186 struct bio
*bio_clone_mddev(struct bio
*bio
, gfp_t gfp_mask
,
192 if (!mddev
|| !mddev
->bio_set
)
193 return bio_clone(bio
, gfp_mask
);
195 b
= bio_alloc_bioset(gfp_mask
, bio
->bi_max_vecs
,
201 b
->bi_destructor
= mddev_bio_destructor
;
203 if (bio_integrity(bio
)) {
206 ret
= bio_integrity_clone(b
, bio
, gfp_mask
, mddev
->bio_set
);
216 EXPORT_SYMBOL_GPL(bio_clone_mddev
);
219 * We have a system wide 'event count' that is incremented
220 * on any 'interesting' event, and readers of /proc/mdstat
221 * can use 'poll' or 'select' to find out when the event
225 * start array, stop array, error, add device, remove device,
226 * start build, activate spare
228 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters
);
229 static atomic_t md_event_count
;
230 void md_new_event(mddev_t
*mddev
)
232 atomic_inc(&md_event_count
);
233 wake_up(&md_event_waiters
);
235 EXPORT_SYMBOL_GPL(md_new_event
);
237 /* Alternate version that can be called from interrupts
238 * when calling sysfs_notify isn't needed.
240 static void md_new_event_inintr(mddev_t
*mddev
)
242 atomic_inc(&md_event_count
);
243 wake_up(&md_event_waiters
);
247 * Enables to iterate over all existing md arrays
248 * all_mddevs_lock protects this list.
250 static LIST_HEAD(all_mddevs
);
251 static DEFINE_SPINLOCK(all_mddevs_lock
);
255 * iterates through all used mddevs in the system.
256 * We take care to grab the all_mddevs_lock whenever navigating
257 * the list, and to always hold a refcount when unlocked.
258 * Any code which breaks out of this loop while own
259 * a reference to the current mddev and must mddev_put it.
261 #define for_each_mddev(mddev,tmp) \
263 for (({ spin_lock(&all_mddevs_lock); \
264 tmp = all_mddevs.next; \
266 ({ if (tmp != &all_mddevs) \
267 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
268 spin_unlock(&all_mddevs_lock); \
269 if (mddev) mddev_put(mddev); \
270 mddev = list_entry(tmp, mddev_t, all_mddevs); \
271 tmp != &all_mddevs;}); \
272 ({ spin_lock(&all_mddevs_lock); \
277 /* Rather than calling directly into the personality make_request function,
278 * IO requests come here first so that we can check if the device is
279 * being suspended pending a reconfiguration.
280 * We hold a refcount over the call to ->make_request. By the time that
281 * call has finished, the bio has been linked into some internal structure
282 * and so is visible to ->quiesce(), so we don't need the refcount any more.
284 static int md_make_request(struct request_queue
*q
, struct bio
*bio
)
286 const int rw
= bio_data_dir(bio
);
287 mddev_t
*mddev
= q
->queuedata
;
290 unsigned int sectors
;
292 if (mddev
== NULL
|| mddev
->pers
== NULL
297 smp_rmb(); /* Ensure implications of 'active' are visible */
299 if (mddev
->suspended
) {
302 prepare_to_wait(&mddev
->sb_wait
, &__wait
,
303 TASK_UNINTERRUPTIBLE
);
304 if (!mddev
->suspended
)
310 finish_wait(&mddev
->sb_wait
, &__wait
);
312 atomic_inc(&mddev
->active_io
);
316 * save the sectors now since our bio can
317 * go away inside make_request
319 sectors
= bio_sectors(bio
);
320 rv
= mddev
->pers
->make_request(mddev
, bio
);
322 cpu
= part_stat_lock();
323 part_stat_inc(cpu
, &mddev
->gendisk
->part0
, ios
[rw
]);
324 part_stat_add(cpu
, &mddev
->gendisk
->part0
, sectors
[rw
], sectors
);
327 if (atomic_dec_and_test(&mddev
->active_io
) && mddev
->suspended
)
328 wake_up(&mddev
->sb_wait
);
333 /* mddev_suspend makes sure no new requests are submitted
334 * to the device, and that any requests that have been submitted
335 * are completely handled.
336 * Once ->stop is called and completes, the module will be completely
339 void mddev_suspend(mddev_t
*mddev
)
341 BUG_ON(mddev
->suspended
);
342 mddev
->suspended
= 1;
344 wait_event(mddev
->sb_wait
, atomic_read(&mddev
->active_io
) == 0);
345 mddev
->pers
->quiesce(mddev
, 1);
347 EXPORT_SYMBOL_GPL(mddev_suspend
);
349 void mddev_resume(mddev_t
*mddev
)
351 mddev
->suspended
= 0;
352 wake_up(&mddev
->sb_wait
);
353 mddev
->pers
->quiesce(mddev
, 0);
355 EXPORT_SYMBOL_GPL(mddev_resume
);
357 int mddev_congested(mddev_t
*mddev
, int bits
)
359 return mddev
->suspended
;
361 EXPORT_SYMBOL(mddev_congested
);
364 * Generic flush handling for md
367 static void md_end_flush(struct bio
*bio
, int err
)
369 mdk_rdev_t
*rdev
= bio
->bi_private
;
370 mddev_t
*mddev
= rdev
->mddev
;
372 rdev_dec_pending(rdev
, mddev
);
374 if (atomic_dec_and_test(&mddev
->flush_pending
)) {
375 /* The pre-request flush has finished */
376 queue_work(md_wq
, &mddev
->flush_work
);
381 static void md_submit_flush_data(struct work_struct
*ws
);
383 static void submit_flushes(struct work_struct
*ws
)
385 mddev_t
*mddev
= container_of(ws
, mddev_t
, flush_work
);
388 INIT_WORK(&mddev
->flush_work
, md_submit_flush_data
);
389 atomic_set(&mddev
->flush_pending
, 1);
391 list_for_each_entry_rcu(rdev
, &mddev
->disks
, same_set
)
392 if (rdev
->raid_disk
>= 0 &&
393 !test_bit(Faulty
, &rdev
->flags
)) {
394 /* Take two references, one is dropped
395 * when request finishes, one after
396 * we reclaim rcu_read_lock
399 atomic_inc(&rdev
->nr_pending
);
400 atomic_inc(&rdev
->nr_pending
);
402 bi
= bio_alloc_mddev(GFP_KERNEL
, 0, mddev
);
403 bi
->bi_end_io
= md_end_flush
;
404 bi
->bi_private
= rdev
;
405 bi
->bi_bdev
= rdev
->bdev
;
406 atomic_inc(&mddev
->flush_pending
);
407 submit_bio(WRITE_FLUSH
, bi
);
409 rdev_dec_pending(rdev
, mddev
);
412 if (atomic_dec_and_test(&mddev
->flush_pending
))
413 queue_work(md_wq
, &mddev
->flush_work
);
416 static void md_submit_flush_data(struct work_struct
*ws
)
418 mddev_t
*mddev
= container_of(ws
, mddev_t
, flush_work
);
419 struct bio
*bio
= mddev
->flush_bio
;
421 if (bio
->bi_size
== 0)
422 /* an empty barrier - all done */
425 bio
->bi_rw
&= ~REQ_FLUSH
;
426 if (mddev
->pers
->make_request(mddev
, bio
))
427 generic_make_request(bio
);
430 mddev
->flush_bio
= NULL
;
431 wake_up(&mddev
->sb_wait
);
434 void md_flush_request(mddev_t
*mddev
, struct bio
*bio
)
436 spin_lock_irq(&mddev
->write_lock
);
437 wait_event_lock_irq(mddev
->sb_wait
,
439 mddev
->write_lock
, /*nothing*/);
440 mddev
->flush_bio
= bio
;
441 spin_unlock_irq(&mddev
->write_lock
);
443 INIT_WORK(&mddev
->flush_work
, submit_flushes
);
444 queue_work(md_wq
, &mddev
->flush_work
);
446 EXPORT_SYMBOL(md_flush_request
);
448 /* Support for plugging.
449 * This mirrors the plugging support in request_queue, but does not
450 * require having a whole queue
452 static void plugger_work(struct work_struct
*work
)
454 struct plug_handle
*plug
=
455 container_of(work
, struct plug_handle
, unplug_work
);
456 plug
->unplug_fn(plug
);
458 static void plugger_timeout(unsigned long data
)
460 struct plug_handle
*plug
= (void *)data
;
461 kblockd_schedule_work(NULL
, &plug
->unplug_work
);
463 void plugger_init(struct plug_handle
*plug
,
464 void (*unplug_fn
)(struct plug_handle
*))
466 plug
->unplug_flag
= 0;
467 plug
->unplug_fn
= unplug_fn
;
468 init_timer(&plug
->unplug_timer
);
469 plug
->unplug_timer
.function
= plugger_timeout
;
470 plug
->unplug_timer
.data
= (unsigned long)plug
;
471 INIT_WORK(&plug
->unplug_work
, plugger_work
);
473 EXPORT_SYMBOL_GPL(plugger_init
);
475 void plugger_set_plug(struct plug_handle
*plug
)
477 if (!test_and_set_bit(PLUGGED_FLAG
, &plug
->unplug_flag
))
478 mod_timer(&plug
->unplug_timer
, jiffies
+ msecs_to_jiffies(3)+1);
480 EXPORT_SYMBOL_GPL(plugger_set_plug
);
482 int plugger_remove_plug(struct plug_handle
*plug
)
484 if (test_and_clear_bit(PLUGGED_FLAG
, &plug
->unplug_flag
)) {
485 del_timer(&plug
->unplug_timer
);
490 EXPORT_SYMBOL_GPL(plugger_remove_plug
);
493 static inline mddev_t
*mddev_get(mddev_t
*mddev
)
495 atomic_inc(&mddev
->active
);
499 static void mddev_delayed_delete(struct work_struct
*ws
);
501 static void mddev_put(mddev_t
*mddev
)
503 struct bio_set
*bs
= NULL
;
505 if (!atomic_dec_and_lock(&mddev
->active
, &all_mddevs_lock
))
507 if (!mddev
->raid_disks
&& list_empty(&mddev
->disks
) &&
508 mddev
->ctime
== 0 && !mddev
->hold_active
) {
509 /* Array is not configured at all, and not held active,
511 list_del(&mddev
->all_mddevs
);
513 mddev
->bio_set
= NULL
;
514 if (mddev
->gendisk
) {
515 /* We did a probe so need to clean up. Call
516 * queue_work inside the spinlock so that
517 * flush_workqueue() after mddev_find will
518 * succeed in waiting for the work to be done.
520 INIT_WORK(&mddev
->del_work
, mddev_delayed_delete
);
521 queue_work(md_misc_wq
, &mddev
->del_work
);
525 spin_unlock(&all_mddevs_lock
);
530 void mddev_init(mddev_t
*mddev
)
532 mutex_init(&mddev
->open_mutex
);
533 mutex_init(&mddev
->reconfig_mutex
);
534 mutex_init(&mddev
->bitmap_info
.mutex
);
535 INIT_LIST_HEAD(&mddev
->disks
);
536 INIT_LIST_HEAD(&mddev
->all_mddevs
);
537 init_timer(&mddev
->safemode_timer
);
538 atomic_set(&mddev
->active
, 1);
539 atomic_set(&mddev
->openers
, 0);
540 atomic_set(&mddev
->active_io
, 0);
541 spin_lock_init(&mddev
->write_lock
);
542 atomic_set(&mddev
->flush_pending
, 0);
543 init_waitqueue_head(&mddev
->sb_wait
);
544 init_waitqueue_head(&mddev
->recovery_wait
);
545 mddev
->reshape_position
= MaxSector
;
546 mddev
->resync_min
= 0;
547 mddev
->resync_max
= MaxSector
;
548 mddev
->level
= LEVEL_NONE
;
550 EXPORT_SYMBOL_GPL(mddev_init
);
552 static mddev_t
* mddev_find(dev_t unit
)
554 mddev_t
*mddev
, *new = NULL
;
557 spin_lock(&all_mddevs_lock
);
560 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
561 if (mddev
->unit
== unit
) {
563 spin_unlock(&all_mddevs_lock
);
569 list_add(&new->all_mddevs
, &all_mddevs
);
570 spin_unlock(&all_mddevs_lock
);
571 new->hold_active
= UNTIL_IOCTL
;
575 /* find an unused unit number */
576 static int next_minor
= 512;
577 int start
= next_minor
;
581 dev
= MKDEV(MD_MAJOR
, next_minor
);
583 if (next_minor
> MINORMASK
)
585 if (next_minor
== start
) {
586 /* Oh dear, all in use. */
587 spin_unlock(&all_mddevs_lock
);
593 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
594 if (mddev
->unit
== dev
) {
600 new->md_minor
= MINOR(dev
);
601 new->hold_active
= UNTIL_STOP
;
602 list_add(&new->all_mddevs
, &all_mddevs
);
603 spin_unlock(&all_mddevs_lock
);
606 spin_unlock(&all_mddevs_lock
);
608 new = kzalloc(sizeof(*new), GFP_KERNEL
);
613 if (MAJOR(unit
) == MD_MAJOR
)
614 new->md_minor
= MINOR(unit
);
616 new->md_minor
= MINOR(unit
) >> MdpMinorShift
;
623 static inline int mddev_lock(mddev_t
* mddev
)
625 return mutex_lock_interruptible(&mddev
->reconfig_mutex
);
628 static inline int mddev_is_locked(mddev_t
*mddev
)
630 return mutex_is_locked(&mddev
->reconfig_mutex
);
633 static inline int mddev_trylock(mddev_t
* mddev
)
635 return mutex_trylock(&mddev
->reconfig_mutex
);
638 static struct attribute_group md_redundancy_group
;
640 static void mddev_unlock(mddev_t
* mddev
)
642 if (mddev
->to_remove
) {
643 /* These cannot be removed under reconfig_mutex as
644 * an access to the files will try to take reconfig_mutex
645 * while holding the file unremovable, which leads to
647 * So hold set sysfs_active while the remove in happeing,
648 * and anything else which might set ->to_remove or my
649 * otherwise change the sysfs namespace will fail with
650 * -EBUSY if sysfs_active is still set.
651 * We set sysfs_active under reconfig_mutex and elsewhere
652 * test it under the same mutex to ensure its correct value
655 struct attribute_group
*to_remove
= mddev
->to_remove
;
656 mddev
->to_remove
= NULL
;
657 mddev
->sysfs_active
= 1;
658 mutex_unlock(&mddev
->reconfig_mutex
);
660 if (mddev
->kobj
.sd
) {
661 if (to_remove
!= &md_redundancy_group
)
662 sysfs_remove_group(&mddev
->kobj
, to_remove
);
663 if (mddev
->pers
== NULL
||
664 mddev
->pers
->sync_request
== NULL
) {
665 sysfs_remove_group(&mddev
->kobj
, &md_redundancy_group
);
666 if (mddev
->sysfs_action
)
667 sysfs_put(mddev
->sysfs_action
);
668 mddev
->sysfs_action
= NULL
;
671 mddev
->sysfs_active
= 0;
673 mutex_unlock(&mddev
->reconfig_mutex
);
675 md_wakeup_thread(mddev
->thread
);
678 static mdk_rdev_t
* find_rdev_nr(mddev_t
*mddev
, int nr
)
682 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
683 if (rdev
->desc_nr
== nr
)
689 static mdk_rdev_t
* find_rdev(mddev_t
* mddev
, dev_t dev
)
693 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
694 if (rdev
->bdev
->bd_dev
== dev
)
700 static struct mdk_personality
*find_pers(int level
, char *clevel
)
702 struct mdk_personality
*pers
;
703 list_for_each_entry(pers
, &pers_list
, list
) {
704 if (level
!= LEVEL_NONE
&& pers
->level
== level
)
706 if (strcmp(pers
->name
, clevel
)==0)
712 /* return the offset of the super block in 512byte sectors */
713 static inline sector_t
calc_dev_sboffset(mdk_rdev_t
*rdev
)
715 sector_t num_sectors
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
716 return MD_NEW_SIZE_SECTORS(num_sectors
);
719 static int alloc_disk_sb(mdk_rdev_t
* rdev
)
724 rdev
->sb_page
= alloc_page(GFP_KERNEL
);
725 if (!rdev
->sb_page
) {
726 printk(KERN_ALERT
"md: out of memory.\n");
733 static void free_disk_sb(mdk_rdev_t
* rdev
)
736 put_page(rdev
->sb_page
);
738 rdev
->sb_page
= NULL
;
745 static void super_written(struct bio
*bio
, int error
)
747 mdk_rdev_t
*rdev
= bio
->bi_private
;
748 mddev_t
*mddev
= rdev
->mddev
;
750 if (error
|| !test_bit(BIO_UPTODATE
, &bio
->bi_flags
)) {
751 printk("md: super_written gets error=%d, uptodate=%d\n",
752 error
, test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
753 WARN_ON(test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
754 md_error(mddev
, rdev
);
757 if (atomic_dec_and_test(&mddev
->pending_writes
))
758 wake_up(&mddev
->sb_wait
);
762 void md_super_write(mddev_t
*mddev
, mdk_rdev_t
*rdev
,
763 sector_t sector
, int size
, struct page
*page
)
765 /* write first size bytes of page to sector of rdev
766 * Increment mddev->pending_writes before returning
767 * and decrement it on completion, waking up sb_wait
768 * if zero is reached.
769 * If an error occurred, call md_error
771 struct bio
*bio
= bio_alloc_mddev(GFP_NOIO
, 1, mddev
);
773 bio
->bi_bdev
= rdev
->meta_bdev
? rdev
->meta_bdev
: rdev
->bdev
;
774 bio
->bi_sector
= sector
;
775 bio_add_page(bio
, page
, size
, 0);
776 bio
->bi_private
= rdev
;
777 bio
->bi_end_io
= super_written
;
779 atomic_inc(&mddev
->pending_writes
);
780 submit_bio(REQ_WRITE
| REQ_SYNC
| REQ_UNPLUG
| REQ_FLUSH
| REQ_FUA
,
784 void md_super_wait(mddev_t
*mddev
)
786 /* wait for all superblock writes that were scheduled to complete */
789 prepare_to_wait(&mddev
->sb_wait
, &wq
, TASK_UNINTERRUPTIBLE
);
790 if (atomic_read(&mddev
->pending_writes
)==0)
794 finish_wait(&mddev
->sb_wait
, &wq
);
797 static void bi_complete(struct bio
*bio
, int error
)
799 complete((struct completion
*)bio
->bi_private
);
802 int sync_page_io(mdk_rdev_t
*rdev
, sector_t sector
, int size
,
803 struct page
*page
, int rw
, bool metadata_op
)
805 struct bio
*bio
= bio_alloc_mddev(GFP_NOIO
, 1, rdev
->mddev
);
806 struct completion event
;
809 rw
|= REQ_SYNC
| REQ_UNPLUG
;
811 bio
->bi_bdev
= (metadata_op
&& rdev
->meta_bdev
) ?
812 rdev
->meta_bdev
: rdev
->bdev
;
814 bio
->bi_sector
= sector
+ rdev
->sb_start
;
816 bio
->bi_sector
= sector
+ rdev
->data_offset
;
817 bio_add_page(bio
, page
, size
, 0);
818 init_completion(&event
);
819 bio
->bi_private
= &event
;
820 bio
->bi_end_io
= bi_complete
;
822 wait_for_completion(&event
);
824 ret
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
828 EXPORT_SYMBOL_GPL(sync_page_io
);
830 static int read_disk_sb(mdk_rdev_t
* rdev
, int size
)
832 char b
[BDEVNAME_SIZE
];
833 if (!rdev
->sb_page
) {
841 if (!sync_page_io(rdev
, 0, size
, rdev
->sb_page
, READ
, true))
847 printk(KERN_WARNING
"md: disabled device %s, could not read superblock.\n",
848 bdevname(rdev
->bdev
,b
));
852 static int uuid_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
854 return sb1
->set_uuid0
== sb2
->set_uuid0
&&
855 sb1
->set_uuid1
== sb2
->set_uuid1
&&
856 sb1
->set_uuid2
== sb2
->set_uuid2
&&
857 sb1
->set_uuid3
== sb2
->set_uuid3
;
860 static int sb_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
863 mdp_super_t
*tmp1
, *tmp2
;
865 tmp1
= kmalloc(sizeof(*tmp1
),GFP_KERNEL
);
866 tmp2
= kmalloc(sizeof(*tmp2
),GFP_KERNEL
);
868 if (!tmp1
|| !tmp2
) {
870 printk(KERN_INFO
"md.c sb_equal(): failed to allocate memory!\n");
878 * nr_disks is not constant
883 ret
= (memcmp(tmp1
, tmp2
, MD_SB_GENERIC_CONSTANT_WORDS
* 4) == 0);
891 static u32
md_csum_fold(u32 csum
)
893 csum
= (csum
& 0xffff) + (csum
>> 16);
894 return (csum
& 0xffff) + (csum
>> 16);
897 static unsigned int calc_sb_csum(mdp_super_t
* sb
)
900 u32
*sb32
= (u32
*)sb
;
902 unsigned int disk_csum
, csum
;
904 disk_csum
= sb
->sb_csum
;
907 for (i
= 0; i
< MD_SB_BYTES
/4 ; i
++)
909 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
913 /* This used to use csum_partial, which was wrong for several
914 * reasons including that different results are returned on
915 * different architectures. It isn't critical that we get exactly
916 * the same return value as before (we always csum_fold before
917 * testing, and that removes any differences). However as we
918 * know that csum_partial always returned a 16bit value on
919 * alphas, do a fold to maximise conformity to previous behaviour.
921 sb
->sb_csum
= md_csum_fold(disk_csum
);
923 sb
->sb_csum
= disk_csum
;
930 * Handle superblock details.
931 * We want to be able to handle multiple superblock formats
932 * so we have a common interface to them all, and an array of
933 * different handlers.
934 * We rely on user-space to write the initial superblock, and support
935 * reading and updating of superblocks.
936 * Interface methods are:
937 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
938 * loads and validates a superblock on dev.
939 * if refdev != NULL, compare superblocks on both devices
941 * 0 - dev has a superblock that is compatible with refdev
942 * 1 - dev has a superblock that is compatible and newer than refdev
943 * so dev should be used as the refdev in future
944 * -EINVAL superblock incompatible or invalid
945 * -othererror e.g. -EIO
947 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
948 * Verify that dev is acceptable into mddev.
949 * The first time, mddev->raid_disks will be 0, and data from
950 * dev should be merged in. Subsequent calls check that dev
951 * is new enough. Return 0 or -EINVAL
953 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
954 * Update the superblock for rdev with data in mddev
955 * This does not write to disc.
961 struct module
*owner
;
962 int (*load_super
)(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
,
964 int (*validate_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
965 void (*sync_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
966 unsigned long long (*rdev_size_change
)(mdk_rdev_t
*rdev
,
967 sector_t num_sectors
);
971 * Check that the given mddev has no bitmap.
973 * This function is called from the run method of all personalities that do not
974 * support bitmaps. It prints an error message and returns non-zero if mddev
975 * has a bitmap. Otherwise, it returns 0.
978 int md_check_no_bitmap(mddev_t
*mddev
)
980 if (!mddev
->bitmap_info
.file
&& !mddev
->bitmap_info
.offset
)
982 printk(KERN_ERR
"%s: bitmaps are not supported for %s\n",
983 mdname(mddev
), mddev
->pers
->name
);
986 EXPORT_SYMBOL(md_check_no_bitmap
);
989 * load_super for 0.90.0
991 static int super_90_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
993 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
998 * Calculate the position of the superblock (512byte sectors),
999 * it's at the end of the disk.
1001 * It also happens to be a multiple of 4Kb.
1003 rdev
->sb_start
= calc_dev_sboffset(rdev
);
1005 ret
= read_disk_sb(rdev
, MD_SB_BYTES
);
1006 if (ret
) return ret
;
1010 bdevname(rdev
->bdev
, b
);
1011 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
1013 if (sb
->md_magic
!= MD_SB_MAGIC
) {
1014 printk(KERN_ERR
"md: invalid raid superblock magic on %s\n",
1019 if (sb
->major_version
!= 0 ||
1020 sb
->minor_version
< 90 ||
1021 sb
->minor_version
> 91) {
1022 printk(KERN_WARNING
"Bad version number %d.%d on %s\n",
1023 sb
->major_version
, sb
->minor_version
,
1028 if (sb
->raid_disks
<= 0)
1031 if (md_csum_fold(calc_sb_csum(sb
)) != md_csum_fold(sb
->sb_csum
)) {
1032 printk(KERN_WARNING
"md: invalid superblock checksum on %s\n",
1037 rdev
->preferred_minor
= sb
->md_minor
;
1038 rdev
->data_offset
= 0;
1039 rdev
->sb_size
= MD_SB_BYTES
;
1041 if (sb
->level
== LEVEL_MULTIPATH
)
1044 rdev
->desc_nr
= sb
->this_disk
.number
;
1050 mdp_super_t
*refsb
= (mdp_super_t
*)page_address(refdev
->sb_page
);
1051 if (!uuid_equal(refsb
, sb
)) {
1052 printk(KERN_WARNING
"md: %s has different UUID to %s\n",
1053 b
, bdevname(refdev
->bdev
,b2
));
1056 if (!sb_equal(refsb
, sb
)) {
1057 printk(KERN_WARNING
"md: %s has same UUID"
1058 " but different superblock to %s\n",
1059 b
, bdevname(refdev
->bdev
, b2
));
1063 ev2
= md_event(refsb
);
1069 rdev
->sectors
= rdev
->sb_start
;
1071 if (rdev
->sectors
< sb
->size
* 2 && sb
->level
> 1)
1072 /* "this cannot possibly happen" ... */
1080 * validate_super for 0.90.0
1082 static int super_90_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1085 mdp_super_t
*sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
1086 __u64 ev1
= md_event(sb
);
1088 rdev
->raid_disk
= -1;
1089 clear_bit(Faulty
, &rdev
->flags
);
1090 clear_bit(In_sync
, &rdev
->flags
);
1091 clear_bit(WriteMostly
, &rdev
->flags
);
1093 if (mddev
->raid_disks
== 0) {
1094 mddev
->major_version
= 0;
1095 mddev
->minor_version
= sb
->minor_version
;
1096 mddev
->patch_version
= sb
->patch_version
;
1097 mddev
->external
= 0;
1098 mddev
->chunk_sectors
= sb
->chunk_size
>> 9;
1099 mddev
->ctime
= sb
->ctime
;
1100 mddev
->utime
= sb
->utime
;
1101 mddev
->level
= sb
->level
;
1102 mddev
->clevel
[0] = 0;
1103 mddev
->layout
= sb
->layout
;
1104 mddev
->raid_disks
= sb
->raid_disks
;
1105 mddev
->dev_sectors
= sb
->size
* 2;
1106 mddev
->events
= ev1
;
1107 mddev
->bitmap_info
.offset
= 0;
1108 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
1110 if (mddev
->minor_version
>= 91) {
1111 mddev
->reshape_position
= sb
->reshape_position
;
1112 mddev
->delta_disks
= sb
->delta_disks
;
1113 mddev
->new_level
= sb
->new_level
;
1114 mddev
->new_layout
= sb
->new_layout
;
1115 mddev
->new_chunk_sectors
= sb
->new_chunk
>> 9;
1117 mddev
->reshape_position
= MaxSector
;
1118 mddev
->delta_disks
= 0;
1119 mddev
->new_level
= mddev
->level
;
1120 mddev
->new_layout
= mddev
->layout
;
1121 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1124 if (sb
->state
& (1<<MD_SB_CLEAN
))
1125 mddev
->recovery_cp
= MaxSector
;
1127 if (sb
->events_hi
== sb
->cp_events_hi
&&
1128 sb
->events_lo
== sb
->cp_events_lo
) {
1129 mddev
->recovery_cp
= sb
->recovery_cp
;
1131 mddev
->recovery_cp
= 0;
1134 memcpy(mddev
->uuid
+0, &sb
->set_uuid0
, 4);
1135 memcpy(mddev
->uuid
+4, &sb
->set_uuid1
, 4);
1136 memcpy(mddev
->uuid
+8, &sb
->set_uuid2
, 4);
1137 memcpy(mddev
->uuid
+12,&sb
->set_uuid3
, 4);
1139 mddev
->max_disks
= MD_SB_DISKS
;
1141 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
) &&
1142 mddev
->bitmap_info
.file
== NULL
)
1143 mddev
->bitmap_info
.offset
=
1144 mddev
->bitmap_info
.default_offset
;
1146 } else if (mddev
->pers
== NULL
) {
1147 /* Insist on good event counter while assembling, except
1148 * for spares (which don't need an event count) */
1150 if (sb
->disks
[rdev
->desc_nr
].state
& (
1151 (1<<MD_DISK_SYNC
) | (1 << MD_DISK_ACTIVE
)))
1152 if (ev1
< mddev
->events
)
1154 } else if (mddev
->bitmap
) {
1155 /* if adding to array with a bitmap, then we can accept an
1156 * older device ... but not too old.
1158 if (ev1
< mddev
->bitmap
->events_cleared
)
1161 if (ev1
< mddev
->events
)
1162 /* just a hot-add of a new device, leave raid_disk at -1 */
1166 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1167 desc
= sb
->disks
+ rdev
->desc_nr
;
1169 if (desc
->state
& (1<<MD_DISK_FAULTY
))
1170 set_bit(Faulty
, &rdev
->flags
);
1171 else if (desc
->state
& (1<<MD_DISK_SYNC
) /* &&
1172 desc->raid_disk < mddev->raid_disks */) {
1173 set_bit(In_sync
, &rdev
->flags
);
1174 rdev
->raid_disk
= desc
->raid_disk
;
1175 } else if (desc
->state
& (1<<MD_DISK_ACTIVE
)) {
1176 /* active but not in sync implies recovery up to
1177 * reshape position. We don't know exactly where
1178 * that is, so set to zero for now */
1179 if (mddev
->minor_version
>= 91) {
1180 rdev
->recovery_offset
= 0;
1181 rdev
->raid_disk
= desc
->raid_disk
;
1184 if (desc
->state
& (1<<MD_DISK_WRITEMOSTLY
))
1185 set_bit(WriteMostly
, &rdev
->flags
);
1186 } else /* MULTIPATH are always insync */
1187 set_bit(In_sync
, &rdev
->flags
);
1192 * sync_super for 0.90.0
1194 static void super_90_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1198 int next_spare
= mddev
->raid_disks
;
1201 /* make rdev->sb match mddev data..
1204 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1205 * 3/ any empty disks < next_spare become removed
1207 * disks[0] gets initialised to REMOVED because
1208 * we cannot be sure from other fields if it has
1209 * been initialised or not.
1212 int active
=0, working
=0,failed
=0,spare
=0,nr_disks
=0;
1214 rdev
->sb_size
= MD_SB_BYTES
;
1216 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
1218 memset(sb
, 0, sizeof(*sb
));
1220 sb
->md_magic
= MD_SB_MAGIC
;
1221 sb
->major_version
= mddev
->major_version
;
1222 sb
->patch_version
= mddev
->patch_version
;
1223 sb
->gvalid_words
= 0; /* ignored */
1224 memcpy(&sb
->set_uuid0
, mddev
->uuid
+0, 4);
1225 memcpy(&sb
->set_uuid1
, mddev
->uuid
+4, 4);
1226 memcpy(&sb
->set_uuid2
, mddev
->uuid
+8, 4);
1227 memcpy(&sb
->set_uuid3
, mddev
->uuid
+12,4);
1229 sb
->ctime
= mddev
->ctime
;
1230 sb
->level
= mddev
->level
;
1231 sb
->size
= mddev
->dev_sectors
/ 2;
1232 sb
->raid_disks
= mddev
->raid_disks
;
1233 sb
->md_minor
= mddev
->md_minor
;
1234 sb
->not_persistent
= 0;
1235 sb
->utime
= mddev
->utime
;
1237 sb
->events_hi
= (mddev
->events
>>32);
1238 sb
->events_lo
= (u32
)mddev
->events
;
1240 if (mddev
->reshape_position
== MaxSector
)
1241 sb
->minor_version
= 90;
1243 sb
->minor_version
= 91;
1244 sb
->reshape_position
= mddev
->reshape_position
;
1245 sb
->new_level
= mddev
->new_level
;
1246 sb
->delta_disks
= mddev
->delta_disks
;
1247 sb
->new_layout
= mddev
->new_layout
;
1248 sb
->new_chunk
= mddev
->new_chunk_sectors
<< 9;
1250 mddev
->minor_version
= sb
->minor_version
;
1253 sb
->recovery_cp
= mddev
->recovery_cp
;
1254 sb
->cp_events_hi
= (mddev
->events
>>32);
1255 sb
->cp_events_lo
= (u32
)mddev
->events
;
1256 if (mddev
->recovery_cp
== MaxSector
)
1257 sb
->state
= (1<< MD_SB_CLEAN
);
1259 sb
->recovery_cp
= 0;
1261 sb
->layout
= mddev
->layout
;
1262 sb
->chunk_size
= mddev
->chunk_sectors
<< 9;
1264 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
)
1265 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
1267 sb
->disks
[0].state
= (1<<MD_DISK_REMOVED
);
1268 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
1271 int is_active
= test_bit(In_sync
, &rdev2
->flags
);
1273 if (rdev2
->raid_disk
>= 0 &&
1274 sb
->minor_version
>= 91)
1275 /* we have nowhere to store the recovery_offset,
1276 * but if it is not below the reshape_position,
1277 * we can piggy-back on that.
1280 if (rdev2
->raid_disk
< 0 ||
1281 test_bit(Faulty
, &rdev2
->flags
))
1284 desc_nr
= rdev2
->raid_disk
;
1286 desc_nr
= next_spare
++;
1287 rdev2
->desc_nr
= desc_nr
;
1288 d
= &sb
->disks
[rdev2
->desc_nr
];
1290 d
->number
= rdev2
->desc_nr
;
1291 d
->major
= MAJOR(rdev2
->bdev
->bd_dev
);
1292 d
->minor
= MINOR(rdev2
->bdev
->bd_dev
);
1294 d
->raid_disk
= rdev2
->raid_disk
;
1296 d
->raid_disk
= rdev2
->desc_nr
; /* compatibility */
1297 if (test_bit(Faulty
, &rdev2
->flags
))
1298 d
->state
= (1<<MD_DISK_FAULTY
);
1299 else if (is_active
) {
1300 d
->state
= (1<<MD_DISK_ACTIVE
);
1301 if (test_bit(In_sync
, &rdev2
->flags
))
1302 d
->state
|= (1<<MD_DISK_SYNC
);
1310 if (test_bit(WriteMostly
, &rdev2
->flags
))
1311 d
->state
|= (1<<MD_DISK_WRITEMOSTLY
);
1313 /* now set the "removed" and "faulty" bits on any missing devices */
1314 for (i
=0 ; i
< mddev
->raid_disks
; i
++) {
1315 mdp_disk_t
*d
= &sb
->disks
[i
];
1316 if (d
->state
== 0 && d
->number
== 0) {
1319 d
->state
= (1<<MD_DISK_REMOVED
);
1320 d
->state
|= (1<<MD_DISK_FAULTY
);
1324 sb
->nr_disks
= nr_disks
;
1325 sb
->active_disks
= active
;
1326 sb
->working_disks
= working
;
1327 sb
->failed_disks
= failed
;
1328 sb
->spare_disks
= spare
;
1330 sb
->this_disk
= sb
->disks
[rdev
->desc_nr
];
1331 sb
->sb_csum
= calc_sb_csum(sb
);
1335 * rdev_size_change for 0.90.0
1337 static unsigned long long
1338 super_90_rdev_size_change(mdk_rdev_t
*rdev
, sector_t num_sectors
)
1340 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1341 return 0; /* component must fit device */
1342 if (rdev
->mddev
->bitmap_info
.offset
)
1343 return 0; /* can't move bitmap */
1344 rdev
->sb_start
= calc_dev_sboffset(rdev
);
1345 if (!num_sectors
|| num_sectors
> rdev
->sb_start
)
1346 num_sectors
= rdev
->sb_start
;
1347 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1349 md_super_wait(rdev
->mddev
);
1355 * version 1 superblock
1358 static __le32
calc_sb_1_csum(struct mdp_superblock_1
* sb
)
1362 unsigned long long newcsum
;
1363 int size
= 256 + le32_to_cpu(sb
->max_dev
)*2;
1364 __le32
*isuper
= (__le32
*)sb
;
1367 disk_csum
= sb
->sb_csum
;
1370 for (i
=0; size
>=4; size
-= 4 )
1371 newcsum
+= le32_to_cpu(*isuper
++);
1374 newcsum
+= le16_to_cpu(*(__le16
*) isuper
);
1376 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
1377 sb
->sb_csum
= disk_csum
;
1378 return cpu_to_le32(csum
);
1381 static int super_1_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
1383 struct mdp_superblock_1
*sb
;
1386 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1390 * Calculate the position of the superblock in 512byte sectors.
1391 * It is always aligned to a 4K boundary and
1392 * depeding on minor_version, it can be:
1393 * 0: At least 8K, but less than 12K, from end of device
1394 * 1: At start of device
1395 * 2: 4K from start of device.
1397 switch(minor_version
) {
1399 sb_start
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
1401 sb_start
&= ~(sector_t
)(4*2-1);
1412 rdev
->sb_start
= sb_start
;
1414 /* superblock is rarely larger than 1K, but it can be larger,
1415 * and it is safe to read 4k, so we do that
1417 ret
= read_disk_sb(rdev
, 4096);
1418 if (ret
) return ret
;
1421 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1423 if (sb
->magic
!= cpu_to_le32(MD_SB_MAGIC
) ||
1424 sb
->major_version
!= cpu_to_le32(1) ||
1425 le32_to_cpu(sb
->max_dev
) > (4096-256)/2 ||
1426 le64_to_cpu(sb
->super_offset
) != rdev
->sb_start
||
1427 (le32_to_cpu(sb
->feature_map
) & ~MD_FEATURE_ALL
) != 0)
1430 if (calc_sb_1_csum(sb
) != sb
->sb_csum
) {
1431 printk("md: invalid superblock checksum on %s\n",
1432 bdevname(rdev
->bdev
,b
));
1435 if (le64_to_cpu(sb
->data_size
) < 10) {
1436 printk("md: data_size too small on %s\n",
1437 bdevname(rdev
->bdev
,b
));
1441 rdev
->preferred_minor
= 0xffff;
1442 rdev
->data_offset
= le64_to_cpu(sb
->data_offset
);
1443 atomic_set(&rdev
->corrected_errors
, le32_to_cpu(sb
->cnt_corrected_read
));
1445 rdev
->sb_size
= le32_to_cpu(sb
->max_dev
) * 2 + 256;
1446 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1447 if (rdev
->sb_size
& bmask
)
1448 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1451 && rdev
->data_offset
< sb_start
+ (rdev
->sb_size
/512))
1454 if (sb
->level
== cpu_to_le32(LEVEL_MULTIPATH
))
1457 rdev
->desc_nr
= le32_to_cpu(sb
->dev_number
);
1463 struct mdp_superblock_1
*refsb
=
1464 (struct mdp_superblock_1
*)page_address(refdev
->sb_page
);
1466 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16) != 0 ||
1467 sb
->level
!= refsb
->level
||
1468 sb
->layout
!= refsb
->layout
||
1469 sb
->chunksize
!= refsb
->chunksize
) {
1470 printk(KERN_WARNING
"md: %s has strangely different"
1471 " superblock to %s\n",
1472 bdevname(rdev
->bdev
,b
),
1473 bdevname(refdev
->bdev
,b2
));
1476 ev1
= le64_to_cpu(sb
->events
);
1477 ev2
= le64_to_cpu(refsb
->events
);
1485 rdev
->sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) -
1486 le64_to_cpu(sb
->data_offset
);
1488 rdev
->sectors
= rdev
->sb_start
;
1489 if (rdev
->sectors
< le64_to_cpu(sb
->data_size
))
1491 rdev
->sectors
= le64_to_cpu(sb
->data_size
);
1492 if (le64_to_cpu(sb
->size
) > rdev
->sectors
)
1497 static int super_1_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1499 struct mdp_superblock_1
*sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1500 __u64 ev1
= le64_to_cpu(sb
->events
);
1502 rdev
->raid_disk
= -1;
1503 clear_bit(Faulty
, &rdev
->flags
);
1504 clear_bit(In_sync
, &rdev
->flags
);
1505 clear_bit(WriteMostly
, &rdev
->flags
);
1507 if (mddev
->raid_disks
== 0) {
1508 mddev
->major_version
= 1;
1509 mddev
->patch_version
= 0;
1510 mddev
->external
= 0;
1511 mddev
->chunk_sectors
= le32_to_cpu(sb
->chunksize
);
1512 mddev
->ctime
= le64_to_cpu(sb
->ctime
) & ((1ULL << 32)-1);
1513 mddev
->utime
= le64_to_cpu(sb
->utime
) & ((1ULL << 32)-1);
1514 mddev
->level
= le32_to_cpu(sb
->level
);
1515 mddev
->clevel
[0] = 0;
1516 mddev
->layout
= le32_to_cpu(sb
->layout
);
1517 mddev
->raid_disks
= le32_to_cpu(sb
->raid_disks
);
1518 mddev
->dev_sectors
= le64_to_cpu(sb
->size
);
1519 mddev
->events
= ev1
;
1520 mddev
->bitmap_info
.offset
= 0;
1521 mddev
->bitmap_info
.default_offset
= 1024 >> 9;
1523 mddev
->recovery_cp
= le64_to_cpu(sb
->resync_offset
);
1524 memcpy(mddev
->uuid
, sb
->set_uuid
, 16);
1526 mddev
->max_disks
= (4096-256)/2;
1528 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BITMAP_OFFSET
) &&
1529 mddev
->bitmap_info
.file
== NULL
)
1530 mddev
->bitmap_info
.offset
=
1531 (__s32
)le32_to_cpu(sb
->bitmap_offset
);
1533 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
1534 mddev
->reshape_position
= le64_to_cpu(sb
->reshape_position
);
1535 mddev
->delta_disks
= le32_to_cpu(sb
->delta_disks
);
1536 mddev
->new_level
= le32_to_cpu(sb
->new_level
);
1537 mddev
->new_layout
= le32_to_cpu(sb
->new_layout
);
1538 mddev
->new_chunk_sectors
= le32_to_cpu(sb
->new_chunk
);
1540 mddev
->reshape_position
= MaxSector
;
1541 mddev
->delta_disks
= 0;
1542 mddev
->new_level
= mddev
->level
;
1543 mddev
->new_layout
= mddev
->layout
;
1544 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1547 } else if (mddev
->pers
== NULL
) {
1548 /* Insist of good event counter while assembling, except for
1549 * spares (which don't need an event count) */
1551 if (rdev
->desc_nr
>= 0 &&
1552 rdev
->desc_nr
< le32_to_cpu(sb
->max_dev
) &&
1553 le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]) < 0xfffe)
1554 if (ev1
< mddev
->events
)
1556 } else if (mddev
->bitmap
) {
1557 /* If adding to array with a bitmap, then we can accept an
1558 * older device, but not too old.
1560 if (ev1
< mddev
->bitmap
->events_cleared
)
1563 if (ev1
< mddev
->events
)
1564 /* just a hot-add of a new device, leave raid_disk at -1 */
1567 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1569 if (rdev
->desc_nr
< 0 ||
1570 rdev
->desc_nr
>= le32_to_cpu(sb
->max_dev
)) {
1574 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
1576 case 0xffff: /* spare */
1578 case 0xfffe: /* faulty */
1579 set_bit(Faulty
, &rdev
->flags
);
1582 if ((le32_to_cpu(sb
->feature_map
) &
1583 MD_FEATURE_RECOVERY_OFFSET
))
1584 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
1586 set_bit(In_sync
, &rdev
->flags
);
1587 rdev
->raid_disk
= role
;
1590 if (sb
->devflags
& WriteMostly1
)
1591 set_bit(WriteMostly
, &rdev
->flags
);
1592 } else /* MULTIPATH are always insync */
1593 set_bit(In_sync
, &rdev
->flags
);
1598 static void super_1_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1600 struct mdp_superblock_1
*sb
;
1603 /* make rdev->sb match mddev and rdev data. */
1605 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1607 sb
->feature_map
= 0;
1609 sb
->recovery_offset
= cpu_to_le64(0);
1610 memset(sb
->pad1
, 0, sizeof(sb
->pad1
));
1611 memset(sb
->pad2
, 0, sizeof(sb
->pad2
));
1612 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
1614 sb
->utime
= cpu_to_le64((__u64
)mddev
->utime
);
1615 sb
->events
= cpu_to_le64(mddev
->events
);
1617 sb
->resync_offset
= cpu_to_le64(mddev
->recovery_cp
);
1619 sb
->resync_offset
= cpu_to_le64(0);
1621 sb
->cnt_corrected_read
= cpu_to_le32(atomic_read(&rdev
->corrected_errors
));
1623 sb
->raid_disks
= cpu_to_le32(mddev
->raid_disks
);
1624 sb
->size
= cpu_to_le64(mddev
->dev_sectors
);
1625 sb
->chunksize
= cpu_to_le32(mddev
->chunk_sectors
);
1626 sb
->level
= cpu_to_le32(mddev
->level
);
1627 sb
->layout
= cpu_to_le32(mddev
->layout
);
1629 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
) {
1630 sb
->bitmap_offset
= cpu_to_le32((__u32
)mddev
->bitmap_info
.offset
);
1631 sb
->feature_map
= cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
);
1634 if (rdev
->raid_disk
>= 0 &&
1635 !test_bit(In_sync
, &rdev
->flags
)) {
1637 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET
);
1638 sb
->recovery_offset
=
1639 cpu_to_le64(rdev
->recovery_offset
);
1642 if (mddev
->reshape_position
!= MaxSector
) {
1643 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE
);
1644 sb
->reshape_position
= cpu_to_le64(mddev
->reshape_position
);
1645 sb
->new_layout
= cpu_to_le32(mddev
->new_layout
);
1646 sb
->delta_disks
= cpu_to_le32(mddev
->delta_disks
);
1647 sb
->new_level
= cpu_to_le32(mddev
->new_level
);
1648 sb
->new_chunk
= cpu_to_le32(mddev
->new_chunk_sectors
);
1652 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
)
1653 if (rdev2
->desc_nr
+1 > max_dev
)
1654 max_dev
= rdev2
->desc_nr
+1;
1656 if (max_dev
> le32_to_cpu(sb
->max_dev
)) {
1658 sb
->max_dev
= cpu_to_le32(max_dev
);
1659 rdev
->sb_size
= max_dev
* 2 + 256;
1660 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1661 if (rdev
->sb_size
& bmask
)
1662 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1664 max_dev
= le32_to_cpu(sb
->max_dev
);
1666 for (i
=0; i
<max_dev
;i
++)
1667 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1669 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
1671 if (test_bit(Faulty
, &rdev2
->flags
))
1672 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1673 else if (test_bit(In_sync
, &rdev2
->flags
))
1674 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1675 else if (rdev2
->raid_disk
>= 0)
1676 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1678 sb
->dev_roles
[i
] = cpu_to_le16(0xffff);
1681 sb
->sb_csum
= calc_sb_1_csum(sb
);
1684 static unsigned long long
1685 super_1_rdev_size_change(mdk_rdev_t
*rdev
, sector_t num_sectors
)
1687 struct mdp_superblock_1
*sb
;
1688 sector_t max_sectors
;
1689 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1690 return 0; /* component must fit device */
1691 if (rdev
->sb_start
< rdev
->data_offset
) {
1692 /* minor versions 1 and 2; superblock before data */
1693 max_sectors
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
1694 max_sectors
-= rdev
->data_offset
;
1695 if (!num_sectors
|| num_sectors
> max_sectors
)
1696 num_sectors
= max_sectors
;
1697 } else if (rdev
->mddev
->bitmap_info
.offset
) {
1698 /* minor version 0 with bitmap we can't move */
1701 /* minor version 0; superblock after data */
1703 sb_start
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) - 8*2;
1704 sb_start
&= ~(sector_t
)(4*2 - 1);
1705 max_sectors
= rdev
->sectors
+ sb_start
- rdev
->sb_start
;
1706 if (!num_sectors
|| num_sectors
> max_sectors
)
1707 num_sectors
= max_sectors
;
1708 rdev
->sb_start
= sb_start
;
1710 sb
= (struct mdp_superblock_1
*) page_address(rdev
->sb_page
);
1711 sb
->data_size
= cpu_to_le64(num_sectors
);
1712 sb
->super_offset
= rdev
->sb_start
;
1713 sb
->sb_csum
= calc_sb_1_csum(sb
);
1714 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1716 md_super_wait(rdev
->mddev
);
1720 static struct super_type super_types
[] = {
1723 .owner
= THIS_MODULE
,
1724 .load_super
= super_90_load
,
1725 .validate_super
= super_90_validate
,
1726 .sync_super
= super_90_sync
,
1727 .rdev_size_change
= super_90_rdev_size_change
,
1731 .owner
= THIS_MODULE
,
1732 .load_super
= super_1_load
,
1733 .validate_super
= super_1_validate
,
1734 .sync_super
= super_1_sync
,
1735 .rdev_size_change
= super_1_rdev_size_change
,
1739 static int match_mddev_units(mddev_t
*mddev1
, mddev_t
*mddev2
)
1741 mdk_rdev_t
*rdev
, *rdev2
;
1744 rdev_for_each_rcu(rdev
, mddev1
)
1745 rdev_for_each_rcu(rdev2
, mddev2
)
1746 if (rdev
->bdev
->bd_contains
==
1747 rdev2
->bdev
->bd_contains
) {
1755 static LIST_HEAD(pending_raid_disks
);
1758 * Try to register data integrity profile for an mddev
1760 * This is called when an array is started and after a disk has been kicked
1761 * from the array. It only succeeds if all working and active component devices
1762 * are integrity capable with matching profiles.
1764 int md_integrity_register(mddev_t
*mddev
)
1766 mdk_rdev_t
*rdev
, *reference
= NULL
;
1768 if (list_empty(&mddev
->disks
))
1769 return 0; /* nothing to do */
1770 if (blk_get_integrity(mddev
->gendisk
))
1771 return 0; /* already registered */
1772 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
1773 /* skip spares and non-functional disks */
1774 if (test_bit(Faulty
, &rdev
->flags
))
1776 if (rdev
->raid_disk
< 0)
1779 * If at least one rdev is not integrity capable, we can not
1780 * enable data integrity for the md device.
1782 if (!bdev_get_integrity(rdev
->bdev
))
1785 /* Use the first rdev as the reference */
1789 /* does this rdev's profile match the reference profile? */
1790 if (blk_integrity_compare(reference
->bdev
->bd_disk
,
1791 rdev
->bdev
->bd_disk
) < 0)
1795 * All component devices are integrity capable and have matching
1796 * profiles, register the common profile for the md device.
1798 if (blk_integrity_register(mddev
->gendisk
,
1799 bdev_get_integrity(reference
->bdev
)) != 0) {
1800 printk(KERN_ERR
"md: failed to register integrity for %s\n",
1804 printk(KERN_NOTICE
"md: data integrity on %s enabled\n",
1808 EXPORT_SYMBOL(md_integrity_register
);
1810 /* Disable data integrity if non-capable/non-matching disk is being added */
1811 void md_integrity_add_rdev(mdk_rdev_t
*rdev
, mddev_t
*mddev
)
1813 struct blk_integrity
*bi_rdev
= bdev_get_integrity(rdev
->bdev
);
1814 struct blk_integrity
*bi_mddev
= blk_get_integrity(mddev
->gendisk
);
1816 if (!bi_mddev
) /* nothing to do */
1818 if (rdev
->raid_disk
< 0) /* skip spares */
1820 if (bi_rdev
&& blk_integrity_compare(mddev
->gendisk
,
1821 rdev
->bdev
->bd_disk
) >= 0)
1823 printk(KERN_NOTICE
"disabling data integrity on %s\n", mdname(mddev
));
1824 blk_integrity_unregister(mddev
->gendisk
);
1826 EXPORT_SYMBOL(md_integrity_add_rdev
);
1828 static int bind_rdev_to_array(mdk_rdev_t
* rdev
, mddev_t
* mddev
)
1830 char b
[BDEVNAME_SIZE
];
1840 /* prevent duplicates */
1841 if (find_rdev(mddev
, rdev
->bdev
->bd_dev
))
1844 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1845 if (rdev
->sectors
&& (mddev
->dev_sectors
== 0 ||
1846 rdev
->sectors
< mddev
->dev_sectors
)) {
1848 /* Cannot change size, so fail
1849 * If mddev->level <= 0, then we don't care
1850 * about aligning sizes (e.g. linear)
1852 if (mddev
->level
> 0)
1855 mddev
->dev_sectors
= rdev
->sectors
;
1858 /* Verify rdev->desc_nr is unique.
1859 * If it is -1, assign a free number, else
1860 * check number is not in use
1862 if (rdev
->desc_nr
< 0) {
1864 if (mddev
->pers
) choice
= mddev
->raid_disks
;
1865 while (find_rdev_nr(mddev
, choice
))
1867 rdev
->desc_nr
= choice
;
1869 if (find_rdev_nr(mddev
, rdev
->desc_nr
))
1872 if (mddev
->max_disks
&& rdev
->desc_nr
>= mddev
->max_disks
) {
1873 printk(KERN_WARNING
"md: %s: array is limited to %d devices\n",
1874 mdname(mddev
), mddev
->max_disks
);
1877 bdevname(rdev
->bdev
,b
);
1878 while ( (s
=strchr(b
, '/')) != NULL
)
1881 rdev
->mddev
= mddev
;
1882 printk(KERN_INFO
"md: bind<%s>\n", b
);
1884 if ((err
= kobject_add(&rdev
->kobj
, &mddev
->kobj
, "dev-%s", b
)))
1887 ko
= &part_to_dev(rdev
->bdev
->bd_part
)->kobj
;
1888 if (sysfs_create_link(&rdev
->kobj
, ko
, "block"))
1889 /* failure here is OK */;
1890 rdev
->sysfs_state
= sysfs_get_dirent_safe(rdev
->kobj
.sd
, "state");
1892 list_add_rcu(&rdev
->same_set
, &mddev
->disks
);
1893 bd_link_disk_holder(rdev
->bdev
, mddev
->gendisk
);
1895 /* May as well allow recovery to be retried once */
1896 mddev
->recovery_disabled
= 0;
1901 printk(KERN_WARNING
"md: failed to register dev-%s for %s\n",
1906 static void md_delayed_delete(struct work_struct
*ws
)
1908 mdk_rdev_t
*rdev
= container_of(ws
, mdk_rdev_t
, del_work
);
1909 kobject_del(&rdev
->kobj
);
1910 kobject_put(&rdev
->kobj
);
1913 static void unbind_rdev_from_array(mdk_rdev_t
* rdev
)
1915 char b
[BDEVNAME_SIZE
];
1920 bd_unlink_disk_holder(rdev
->bdev
, rdev
->mddev
->gendisk
);
1921 list_del_rcu(&rdev
->same_set
);
1922 printk(KERN_INFO
"md: unbind<%s>\n", bdevname(rdev
->bdev
,b
));
1924 sysfs_remove_link(&rdev
->kobj
, "block");
1925 sysfs_put(rdev
->sysfs_state
);
1926 rdev
->sysfs_state
= NULL
;
1927 /* We need to delay this, otherwise we can deadlock when
1928 * writing to 'remove' to "dev/state". We also need
1929 * to delay it due to rcu usage.
1932 INIT_WORK(&rdev
->del_work
, md_delayed_delete
);
1933 kobject_get(&rdev
->kobj
);
1934 queue_work(md_misc_wq
, &rdev
->del_work
);
1938 * prevent the device from being mounted, repartitioned or
1939 * otherwise reused by a RAID array (or any other kernel
1940 * subsystem), by bd_claiming the device.
1942 static int lock_rdev(mdk_rdev_t
*rdev
, dev_t dev
, int shared
)
1945 struct block_device
*bdev
;
1946 char b
[BDEVNAME_SIZE
];
1948 bdev
= blkdev_get_by_dev(dev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
,
1949 shared
? (mdk_rdev_t
*)lock_rdev
: rdev
);
1951 printk(KERN_ERR
"md: could not open %s.\n",
1952 __bdevname(dev
, b
));
1953 return PTR_ERR(bdev
);
1959 static void unlock_rdev(mdk_rdev_t
*rdev
)
1961 struct block_device
*bdev
= rdev
->bdev
;
1965 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
);
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 (test_bit(MD_RECOVERY_RUNNING
, &rdev
->mddev
->recovery
))
2474 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
2477 list_for_each_entry(rdev2
, &rdev
->mddev
->disks
, same_set
)
2478 if (rdev2
->raid_disk
== slot
)
2481 if (slot
>= rdev
->mddev
->raid_disks
&&
2482 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
2485 rdev
->raid_disk
= slot
;
2486 if (test_bit(In_sync
, &rdev
->flags
))
2487 rdev
->saved_raid_disk
= slot
;
2489 rdev
->saved_raid_disk
= -1;
2490 err
= rdev
->mddev
->pers
->
2491 hot_add_disk(rdev
->mddev
, rdev
);
2493 rdev
->raid_disk
= -1;
2496 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2497 sprintf(nm
, "rd%d", rdev
->raid_disk
);
2498 if (sysfs_create_link(&rdev
->mddev
->kobj
, &rdev
->kobj
, nm
))
2499 /* failure here is OK */;
2500 /* don't wakeup anyone, leave that to userspace. */
2502 if (slot
>= rdev
->mddev
->raid_disks
&&
2503 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
2505 rdev
->raid_disk
= slot
;
2506 /* assume it is working */
2507 clear_bit(Faulty
, &rdev
->flags
);
2508 clear_bit(WriteMostly
, &rdev
->flags
);
2509 set_bit(In_sync
, &rdev
->flags
);
2510 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2516 static struct rdev_sysfs_entry rdev_slot
=
2517 __ATTR(slot
, S_IRUGO
|S_IWUSR
, slot_show
, slot_store
);
2520 offset_show(mdk_rdev_t
*rdev
, char *page
)
2522 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->data_offset
);
2526 offset_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2529 unsigned long long offset
= simple_strtoull(buf
, &e
, 10);
2530 if (e
==buf
|| (*e
&& *e
!= '\n'))
2532 if (rdev
->mddev
->pers
&& rdev
->raid_disk
>= 0)
2534 if (rdev
->sectors
&& rdev
->mddev
->external
)
2535 /* Must set offset before size, so overlap checks
2538 rdev
->data_offset
= offset
;
2542 static struct rdev_sysfs_entry rdev_offset
=
2543 __ATTR(offset
, S_IRUGO
|S_IWUSR
, offset_show
, offset_store
);
2546 rdev_size_show(mdk_rdev_t
*rdev
, char *page
)
2548 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->sectors
/ 2);
2551 static int overlaps(sector_t s1
, sector_t l1
, sector_t s2
, sector_t l2
)
2553 /* check if two start/length pairs overlap */
2561 static int strict_blocks_to_sectors(const char *buf
, sector_t
*sectors
)
2563 unsigned long long blocks
;
2566 if (strict_strtoull(buf
, 10, &blocks
) < 0)
2569 if (blocks
& 1ULL << (8 * sizeof(blocks
) - 1))
2570 return -EINVAL
; /* sector conversion overflow */
2573 if (new != blocks
* 2)
2574 return -EINVAL
; /* unsigned long long to sector_t overflow */
2581 rdev_size_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2583 mddev_t
*my_mddev
= rdev
->mddev
;
2584 sector_t oldsectors
= rdev
->sectors
;
2587 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
2589 if (my_mddev
->pers
&& rdev
->raid_disk
>= 0) {
2590 if (my_mddev
->persistent
) {
2591 sectors
= super_types
[my_mddev
->major_version
].
2592 rdev_size_change(rdev
, sectors
);
2595 } else if (!sectors
)
2596 sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) -
2599 if (sectors
< my_mddev
->dev_sectors
)
2600 return -EINVAL
; /* component must fit device */
2602 rdev
->sectors
= sectors
;
2603 if (sectors
> oldsectors
&& my_mddev
->external
) {
2604 /* need to check that all other rdevs with the same ->bdev
2605 * do not overlap. We need to unlock the mddev to avoid
2606 * a deadlock. We have already changed rdev->sectors, and if
2607 * we have to change it back, we will have the lock again.
2611 struct list_head
*tmp
;
2613 mddev_unlock(my_mddev
);
2614 for_each_mddev(mddev
, tmp
) {
2618 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
)
2619 if (rdev
->bdev
== rdev2
->bdev
&&
2621 overlaps(rdev
->data_offset
, rdev
->sectors
,
2627 mddev_unlock(mddev
);
2633 mddev_lock(my_mddev
);
2635 /* Someone else could have slipped in a size
2636 * change here, but doing so is just silly.
2637 * We put oldsectors back because we *know* it is
2638 * safe, and trust userspace not to race with
2641 rdev
->sectors
= oldsectors
;
2648 static struct rdev_sysfs_entry rdev_size
=
2649 __ATTR(size
, S_IRUGO
|S_IWUSR
, rdev_size_show
, rdev_size_store
);
2652 static ssize_t
recovery_start_show(mdk_rdev_t
*rdev
, char *page
)
2654 unsigned long long recovery_start
= rdev
->recovery_offset
;
2656 if (test_bit(In_sync
, &rdev
->flags
) ||
2657 recovery_start
== MaxSector
)
2658 return sprintf(page
, "none\n");
2660 return sprintf(page
, "%llu\n", recovery_start
);
2663 static ssize_t
recovery_start_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2665 unsigned long long recovery_start
;
2667 if (cmd_match(buf
, "none"))
2668 recovery_start
= MaxSector
;
2669 else if (strict_strtoull(buf
, 10, &recovery_start
))
2672 if (rdev
->mddev
->pers
&&
2673 rdev
->raid_disk
>= 0)
2676 rdev
->recovery_offset
= recovery_start
;
2677 if (recovery_start
== MaxSector
)
2678 set_bit(In_sync
, &rdev
->flags
);
2680 clear_bit(In_sync
, &rdev
->flags
);
2684 static struct rdev_sysfs_entry rdev_recovery_start
=
2685 __ATTR(recovery_start
, S_IRUGO
|S_IWUSR
, recovery_start_show
, recovery_start_store
);
2687 static struct attribute
*rdev_default_attrs
[] = {
2693 &rdev_recovery_start
.attr
,
2697 rdev_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
2699 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
2700 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
2701 mddev_t
*mddev
= rdev
->mddev
;
2707 rv
= mddev
? mddev_lock(mddev
) : -EBUSY
;
2709 if (rdev
->mddev
== NULL
)
2712 rv
= entry
->show(rdev
, page
);
2713 mddev_unlock(mddev
);
2719 rdev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
2720 const char *page
, size_t length
)
2722 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
2723 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
2725 mddev_t
*mddev
= rdev
->mddev
;
2729 if (!capable(CAP_SYS_ADMIN
))
2731 rv
= mddev
? mddev_lock(mddev
): -EBUSY
;
2733 if (rdev
->mddev
== NULL
)
2736 rv
= entry
->store(rdev
, page
, length
);
2737 mddev_unlock(mddev
);
2742 static void rdev_free(struct kobject
*ko
)
2744 mdk_rdev_t
*rdev
= container_of(ko
, mdk_rdev_t
, kobj
);
2747 static const struct sysfs_ops rdev_sysfs_ops
= {
2748 .show
= rdev_attr_show
,
2749 .store
= rdev_attr_store
,
2751 static struct kobj_type rdev_ktype
= {
2752 .release
= rdev_free
,
2753 .sysfs_ops
= &rdev_sysfs_ops
,
2754 .default_attrs
= rdev_default_attrs
,
2757 void md_rdev_init(mdk_rdev_t
*rdev
)
2760 rdev
->saved_raid_disk
= -1;
2761 rdev
->raid_disk
= -1;
2763 rdev
->data_offset
= 0;
2764 rdev
->sb_events
= 0;
2765 rdev
->last_read_error
.tv_sec
= 0;
2766 rdev
->last_read_error
.tv_nsec
= 0;
2767 atomic_set(&rdev
->nr_pending
, 0);
2768 atomic_set(&rdev
->read_errors
, 0);
2769 atomic_set(&rdev
->corrected_errors
, 0);
2771 INIT_LIST_HEAD(&rdev
->same_set
);
2772 init_waitqueue_head(&rdev
->blocked_wait
);
2774 EXPORT_SYMBOL_GPL(md_rdev_init
);
2776 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2778 * mark the device faulty if:
2780 * - the device is nonexistent (zero size)
2781 * - the device has no valid superblock
2783 * a faulty rdev _never_ has rdev->sb set.
2785 static mdk_rdev_t
*md_import_device(dev_t newdev
, int super_format
, int super_minor
)
2787 char b
[BDEVNAME_SIZE
];
2792 rdev
= kzalloc(sizeof(*rdev
), GFP_KERNEL
);
2794 printk(KERN_ERR
"md: could not alloc mem for new device!\n");
2795 return ERR_PTR(-ENOMEM
);
2799 if ((err
= alloc_disk_sb(rdev
)))
2802 err
= lock_rdev(rdev
, newdev
, super_format
== -2);
2806 kobject_init(&rdev
->kobj
, &rdev_ktype
);
2808 size
= i_size_read(rdev
->bdev
->bd_inode
) >> BLOCK_SIZE_BITS
;
2811 "md: %s has zero or unknown size, marking faulty!\n",
2812 bdevname(rdev
->bdev
,b
));
2817 if (super_format
>= 0) {
2818 err
= super_types
[super_format
].
2819 load_super(rdev
, NULL
, super_minor
);
2820 if (err
== -EINVAL
) {
2822 "md: %s does not have a valid v%d.%d "
2823 "superblock, not importing!\n",
2824 bdevname(rdev
->bdev
,b
),
2825 super_format
, super_minor
);
2830 "md: could not read %s's sb, not importing!\n",
2831 bdevname(rdev
->bdev
,b
));
2839 if (rdev
->sb_page
) {
2845 return ERR_PTR(err
);
2849 * Check a full RAID array for plausibility
2853 static void analyze_sbs(mddev_t
* mddev
)
2856 mdk_rdev_t
*rdev
, *freshest
, *tmp
;
2857 char b
[BDEVNAME_SIZE
];
2860 rdev_for_each(rdev
, tmp
, mddev
)
2861 switch (super_types
[mddev
->major_version
].
2862 load_super(rdev
, freshest
, mddev
->minor_version
)) {
2870 "md: fatal superblock inconsistency in %s"
2871 " -- removing from array\n",
2872 bdevname(rdev
->bdev
,b
));
2873 kick_rdev_from_array(rdev
);
2877 super_types
[mddev
->major_version
].
2878 validate_super(mddev
, freshest
);
2881 rdev_for_each(rdev
, tmp
, mddev
) {
2882 if (mddev
->max_disks
&&
2883 (rdev
->desc_nr
>= mddev
->max_disks
||
2884 i
> mddev
->max_disks
)) {
2886 "md: %s: %s: only %d devices permitted\n",
2887 mdname(mddev
), bdevname(rdev
->bdev
, b
),
2889 kick_rdev_from_array(rdev
);
2892 if (rdev
!= freshest
)
2893 if (super_types
[mddev
->major_version
].
2894 validate_super(mddev
, rdev
)) {
2895 printk(KERN_WARNING
"md: kicking non-fresh %s"
2897 bdevname(rdev
->bdev
,b
));
2898 kick_rdev_from_array(rdev
);
2901 if (mddev
->level
== LEVEL_MULTIPATH
) {
2902 rdev
->desc_nr
= i
++;
2903 rdev
->raid_disk
= rdev
->desc_nr
;
2904 set_bit(In_sync
, &rdev
->flags
);
2905 } else if (rdev
->raid_disk
>= (mddev
->raid_disks
- min(0, mddev
->delta_disks
))) {
2906 rdev
->raid_disk
= -1;
2907 clear_bit(In_sync
, &rdev
->flags
);
2912 /* Read a fixed-point number.
2913 * Numbers in sysfs attributes should be in "standard" units where
2914 * possible, so time should be in seconds.
2915 * However we internally use a a much smaller unit such as
2916 * milliseconds or jiffies.
2917 * This function takes a decimal number with a possible fractional
2918 * component, and produces an integer which is the result of
2919 * multiplying that number by 10^'scale'.
2920 * all without any floating-point arithmetic.
2922 int strict_strtoul_scaled(const char *cp
, unsigned long *res
, int scale
)
2924 unsigned long result
= 0;
2926 while (isdigit(*cp
) || (*cp
== '.' && decimals
< 0)) {
2929 else if (decimals
< scale
) {
2932 result
= result
* 10 + value
;
2944 while (decimals
< scale
) {
2953 static void md_safemode_timeout(unsigned long data
);
2956 safe_delay_show(mddev_t
*mddev
, char *page
)
2958 int msec
= (mddev
->safemode_delay
*1000)/HZ
;
2959 return sprintf(page
, "%d.%03d\n", msec
/1000, msec
%1000);
2962 safe_delay_store(mddev_t
*mddev
, const char *cbuf
, size_t len
)
2966 if (strict_strtoul_scaled(cbuf
, &msec
, 3) < 0)
2969 mddev
->safemode_delay
= 0;
2971 unsigned long old_delay
= mddev
->safemode_delay
;
2972 mddev
->safemode_delay
= (msec
*HZ
)/1000;
2973 if (mddev
->safemode_delay
== 0)
2974 mddev
->safemode_delay
= 1;
2975 if (mddev
->safemode_delay
< old_delay
)
2976 md_safemode_timeout((unsigned long)mddev
);
2980 static struct md_sysfs_entry md_safe_delay
=
2981 __ATTR(safe_mode_delay
, S_IRUGO
|S_IWUSR
,safe_delay_show
, safe_delay_store
);
2984 level_show(mddev_t
*mddev
, char *page
)
2986 struct mdk_personality
*p
= mddev
->pers
;
2988 return sprintf(page
, "%s\n", p
->name
);
2989 else if (mddev
->clevel
[0])
2990 return sprintf(page
, "%s\n", mddev
->clevel
);
2991 else if (mddev
->level
!= LEVEL_NONE
)
2992 return sprintf(page
, "%d\n", mddev
->level
);
2998 level_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3002 struct mdk_personality
*pers
;
3007 if (mddev
->pers
== NULL
) {
3010 if (len
>= sizeof(mddev
->clevel
))
3012 strncpy(mddev
->clevel
, buf
, len
);
3013 if (mddev
->clevel
[len
-1] == '\n')
3015 mddev
->clevel
[len
] = 0;
3016 mddev
->level
= LEVEL_NONE
;
3020 /* request to change the personality. Need to ensure:
3021 * - array is not engaged in resync/recovery/reshape
3022 * - old personality can be suspended
3023 * - new personality will access other array.
3026 if (mddev
->sync_thread
||
3027 mddev
->reshape_position
!= MaxSector
||
3028 mddev
->sysfs_active
)
3031 if (!mddev
->pers
->quiesce
) {
3032 printk(KERN_WARNING
"md: %s: %s does not support online personality change\n",
3033 mdname(mddev
), mddev
->pers
->name
);
3037 /* Now find the new personality */
3038 if (len
== 0 || len
>= sizeof(clevel
))
3040 strncpy(clevel
, buf
, len
);
3041 if (clevel
[len
-1] == '\n')
3044 if (strict_strtol(clevel
, 10, &level
))
3047 if (request_module("md-%s", clevel
) != 0)
3048 request_module("md-level-%s", clevel
);
3049 spin_lock(&pers_lock
);
3050 pers
= find_pers(level
, clevel
);
3051 if (!pers
|| !try_module_get(pers
->owner
)) {
3052 spin_unlock(&pers_lock
);
3053 printk(KERN_WARNING
"md: personality %s not loaded\n", clevel
);
3056 spin_unlock(&pers_lock
);
3058 if (pers
== mddev
->pers
) {
3059 /* Nothing to do! */
3060 module_put(pers
->owner
);
3063 if (!pers
->takeover
) {
3064 module_put(pers
->owner
);
3065 printk(KERN_WARNING
"md: %s: %s does not support personality takeover\n",
3066 mdname(mddev
), clevel
);
3070 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
3071 rdev
->new_raid_disk
= rdev
->raid_disk
;
3073 /* ->takeover must set new_* and/or delta_disks
3074 * if it succeeds, and may set them when it fails.
3076 priv
= pers
->takeover(mddev
);
3078 mddev
->new_level
= mddev
->level
;
3079 mddev
->new_layout
= mddev
->layout
;
3080 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3081 mddev
->raid_disks
-= mddev
->delta_disks
;
3082 mddev
->delta_disks
= 0;
3083 module_put(pers
->owner
);
3084 printk(KERN_WARNING
"md: %s: %s would not accept array\n",
3085 mdname(mddev
), clevel
);
3086 return PTR_ERR(priv
);
3089 /* Looks like we have a winner */
3090 mddev_suspend(mddev
);
3091 mddev
->pers
->stop(mddev
);
3093 if (mddev
->pers
->sync_request
== NULL
&&
3094 pers
->sync_request
!= NULL
) {
3095 /* need to add the md_redundancy_group */
3096 if (sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
3098 "md: cannot register extra attributes for %s\n",
3100 mddev
->sysfs_action
= sysfs_get_dirent(mddev
->kobj
.sd
, NULL
, "sync_action");
3102 if (mddev
->pers
->sync_request
!= NULL
&&
3103 pers
->sync_request
== NULL
) {
3104 /* need to remove the md_redundancy_group */
3105 if (mddev
->to_remove
== NULL
)
3106 mddev
->to_remove
= &md_redundancy_group
;
3109 if (mddev
->pers
->sync_request
== NULL
&&
3111 /* We are converting from a no-redundancy array
3112 * to a redundancy array and metadata is managed
3113 * externally so we need to be sure that writes
3114 * won't block due to a need to transition
3116 * until external management is started.
3119 mddev
->safemode_delay
= 0;
3120 mddev
->safemode
= 0;
3123 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
3125 if (rdev
->raid_disk
< 0)
3127 if (rdev
->new_raid_disk
>= mddev
->raid_disks
)
3128 rdev
->new_raid_disk
= -1;
3129 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3131 sprintf(nm
, "rd%d", rdev
->raid_disk
);
3132 sysfs_remove_link(&mddev
->kobj
, nm
);
3134 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
3135 if (rdev
->raid_disk
< 0)
3137 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3139 rdev
->raid_disk
= rdev
->new_raid_disk
;
3140 if (rdev
->raid_disk
< 0)
3141 clear_bit(In_sync
, &rdev
->flags
);
3144 sprintf(nm
, "rd%d", rdev
->raid_disk
);
3145 if(sysfs_create_link(&mddev
->kobj
, &rdev
->kobj
, nm
))
3146 printk("md: cannot register %s for %s after level change\n",
3151 module_put(mddev
->pers
->owner
);
3153 mddev
->private = priv
;
3154 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
3155 mddev
->level
= mddev
->new_level
;
3156 mddev
->layout
= mddev
->new_layout
;
3157 mddev
->chunk_sectors
= mddev
->new_chunk_sectors
;
3158 mddev
->delta_disks
= 0;
3159 if (mddev
->pers
->sync_request
== NULL
) {
3160 /* this is now an array without redundancy, so
3161 * it must always be in_sync
3164 del_timer_sync(&mddev
->safemode_timer
);
3167 mddev_resume(mddev
);
3168 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
3169 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3170 md_wakeup_thread(mddev
->thread
);
3171 sysfs_notify(&mddev
->kobj
, NULL
, "level");
3172 md_new_event(mddev
);
3176 static struct md_sysfs_entry md_level
=
3177 __ATTR(level
, S_IRUGO
|S_IWUSR
, level_show
, level_store
);
3181 layout_show(mddev_t
*mddev
, char *page
)
3183 /* just a number, not meaningful for all levels */
3184 if (mddev
->reshape_position
!= MaxSector
&&
3185 mddev
->layout
!= mddev
->new_layout
)
3186 return sprintf(page
, "%d (%d)\n",
3187 mddev
->new_layout
, mddev
->layout
);
3188 return sprintf(page
, "%d\n", mddev
->layout
);
3192 layout_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3195 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3197 if (!*buf
|| (*e
&& *e
!= '\n'))
3202 if (mddev
->pers
->check_reshape
== NULL
)
3204 mddev
->new_layout
= n
;
3205 err
= mddev
->pers
->check_reshape(mddev
);
3207 mddev
->new_layout
= mddev
->layout
;
3211 mddev
->new_layout
= n
;
3212 if (mddev
->reshape_position
== MaxSector
)
3217 static struct md_sysfs_entry md_layout
=
3218 __ATTR(layout
, S_IRUGO
|S_IWUSR
, layout_show
, layout_store
);
3222 raid_disks_show(mddev_t
*mddev
, char *page
)
3224 if (mddev
->raid_disks
== 0)
3226 if (mddev
->reshape_position
!= MaxSector
&&
3227 mddev
->delta_disks
!= 0)
3228 return sprintf(page
, "%d (%d)\n", mddev
->raid_disks
,
3229 mddev
->raid_disks
- mddev
->delta_disks
);
3230 return sprintf(page
, "%d\n", mddev
->raid_disks
);
3233 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
);
3236 raid_disks_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3240 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3242 if (!*buf
|| (*e
&& *e
!= '\n'))
3246 rv
= update_raid_disks(mddev
, n
);
3247 else if (mddev
->reshape_position
!= MaxSector
) {
3248 int olddisks
= mddev
->raid_disks
- mddev
->delta_disks
;
3249 mddev
->delta_disks
= n
- olddisks
;
3250 mddev
->raid_disks
= n
;
3252 mddev
->raid_disks
= n
;
3253 return rv
? rv
: len
;
3255 static struct md_sysfs_entry md_raid_disks
=
3256 __ATTR(raid_disks
, S_IRUGO
|S_IWUSR
, raid_disks_show
, raid_disks_store
);
3259 chunk_size_show(mddev_t
*mddev
, char *page
)
3261 if (mddev
->reshape_position
!= MaxSector
&&
3262 mddev
->chunk_sectors
!= mddev
->new_chunk_sectors
)
3263 return sprintf(page
, "%d (%d)\n",
3264 mddev
->new_chunk_sectors
<< 9,
3265 mddev
->chunk_sectors
<< 9);
3266 return sprintf(page
, "%d\n", mddev
->chunk_sectors
<< 9);
3270 chunk_size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3273 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3275 if (!*buf
|| (*e
&& *e
!= '\n'))
3280 if (mddev
->pers
->check_reshape
== NULL
)
3282 mddev
->new_chunk_sectors
= n
>> 9;
3283 err
= mddev
->pers
->check_reshape(mddev
);
3285 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3289 mddev
->new_chunk_sectors
= n
>> 9;
3290 if (mddev
->reshape_position
== MaxSector
)
3291 mddev
->chunk_sectors
= n
>> 9;
3295 static struct md_sysfs_entry md_chunk_size
=
3296 __ATTR(chunk_size
, S_IRUGO
|S_IWUSR
, chunk_size_show
, chunk_size_store
);
3299 resync_start_show(mddev_t
*mddev
, char *page
)
3301 if (mddev
->recovery_cp
== MaxSector
)
3302 return sprintf(page
, "none\n");
3303 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->recovery_cp
);
3307 resync_start_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3310 unsigned long long n
= simple_strtoull(buf
, &e
, 10);
3314 if (cmd_match(buf
, "none"))
3316 else if (!*buf
|| (*e
&& *e
!= '\n'))
3319 mddev
->recovery_cp
= n
;
3322 static struct md_sysfs_entry md_resync_start
=
3323 __ATTR(resync_start
, S_IRUGO
|S_IWUSR
, resync_start_show
, resync_start_store
);
3326 * The array state can be:
3329 * No devices, no size, no level
3330 * Equivalent to STOP_ARRAY ioctl
3332 * May have some settings, but array is not active
3333 * all IO results in error
3334 * When written, doesn't tear down array, but just stops it
3335 * suspended (not supported yet)
3336 * All IO requests will block. The array can be reconfigured.
3337 * Writing this, if accepted, will block until array is quiescent
3339 * no resync can happen. no superblocks get written.
3340 * write requests fail
3342 * like readonly, but behaves like 'clean' on a write request.
3344 * clean - no pending writes, but otherwise active.
3345 * When written to inactive array, starts without resync
3346 * If a write request arrives then
3347 * if metadata is known, mark 'dirty' and switch to 'active'.
3348 * if not known, block and switch to write-pending
3349 * If written to an active array that has pending writes, then fails.
3351 * fully active: IO and resync can be happening.
3352 * When written to inactive array, starts with resync
3355 * clean, but writes are blocked waiting for 'active' to be written.
3358 * like active, but no writes have been seen for a while (100msec).
3361 enum array_state
{ clear
, inactive
, suspended
, readonly
, read_auto
, clean
, active
,
3362 write_pending
, active_idle
, bad_word
};
3363 static char *array_states
[] = {
3364 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3365 "write-pending", "active-idle", NULL
};
3367 static int match_word(const char *word
, char **list
)
3370 for (n
=0; list
[n
]; n
++)
3371 if (cmd_match(word
, list
[n
]))
3377 array_state_show(mddev_t
*mddev
, char *page
)
3379 enum array_state st
= inactive
;
3392 else if (test_bit(MD_CHANGE_PENDING
, &mddev
->flags
))
3394 else if (mddev
->safemode
)
3400 if (list_empty(&mddev
->disks
) &&
3401 mddev
->raid_disks
== 0 &&
3402 mddev
->dev_sectors
== 0)
3407 return sprintf(page
, "%s\n", array_states
[st
]);
3410 static int do_md_stop(mddev_t
* mddev
, int ro
, int is_open
);
3411 static int md_set_readonly(mddev_t
* mddev
, int is_open
);
3412 static int do_md_run(mddev_t
* mddev
);
3413 static int restart_array(mddev_t
*mddev
);
3416 array_state_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3419 enum array_state st
= match_word(buf
, array_states
);
3424 /* stopping an active array */
3425 if (atomic_read(&mddev
->openers
) > 0)
3427 err
= do_md_stop(mddev
, 0, 0);
3430 /* stopping an active array */
3432 if (atomic_read(&mddev
->openers
) > 0)
3434 err
= do_md_stop(mddev
, 2, 0);
3436 err
= 0; /* already inactive */
3439 break; /* not supported yet */
3442 err
= md_set_readonly(mddev
, 0);
3445 set_disk_ro(mddev
->gendisk
, 1);
3446 err
= do_md_run(mddev
);
3452 err
= md_set_readonly(mddev
, 0);
3453 else if (mddev
->ro
== 1)
3454 err
= restart_array(mddev
);
3457 set_disk_ro(mddev
->gendisk
, 0);
3461 err
= do_md_run(mddev
);
3466 restart_array(mddev
);
3467 spin_lock_irq(&mddev
->write_lock
);
3468 if (atomic_read(&mddev
->writes_pending
) == 0) {
3469 if (mddev
->in_sync
== 0) {
3471 if (mddev
->safemode
== 1)
3472 mddev
->safemode
= 0;
3473 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
3478 spin_unlock_irq(&mddev
->write_lock
);
3484 restart_array(mddev
);
3485 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
3486 wake_up(&mddev
->sb_wait
);
3490 set_disk_ro(mddev
->gendisk
, 0);
3491 err
= do_md_run(mddev
);
3496 /* these cannot be set */
3502 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
3506 static struct md_sysfs_entry md_array_state
=
3507 __ATTR(array_state
, S_IRUGO
|S_IWUSR
, array_state_show
, array_state_store
);
3510 max_corrected_read_errors_show(mddev_t
*mddev
, char *page
) {
3511 return sprintf(page
, "%d\n",
3512 atomic_read(&mddev
->max_corr_read_errors
));
3516 max_corrected_read_errors_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3519 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3521 if (*buf
&& (*e
== 0 || *e
== '\n')) {
3522 atomic_set(&mddev
->max_corr_read_errors
, n
);
3528 static struct md_sysfs_entry max_corr_read_errors
=
3529 __ATTR(max_read_errors
, S_IRUGO
|S_IWUSR
, max_corrected_read_errors_show
,
3530 max_corrected_read_errors_store
);
3533 null_show(mddev_t
*mddev
, char *page
)
3539 new_dev_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3541 /* buf must be %d:%d\n? giving major and minor numbers */
3542 /* The new device is added to the array.
3543 * If the array has a persistent superblock, we read the
3544 * superblock to initialise info and check validity.
3545 * Otherwise, only checking done is that in bind_rdev_to_array,
3546 * which mainly checks size.
3549 int major
= simple_strtoul(buf
, &e
, 10);
3555 if (!*buf
|| *e
!= ':' || !e
[1] || e
[1] == '\n')
3557 minor
= simple_strtoul(e
+1, &e
, 10);
3558 if (*e
&& *e
!= '\n')
3560 dev
= MKDEV(major
, minor
);
3561 if (major
!= MAJOR(dev
) ||
3562 minor
!= MINOR(dev
))
3566 if (mddev
->persistent
) {
3567 rdev
= md_import_device(dev
, mddev
->major_version
,
3568 mddev
->minor_version
);
3569 if (!IS_ERR(rdev
) && !list_empty(&mddev
->disks
)) {
3570 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
3571 mdk_rdev_t
, same_set
);
3572 err
= super_types
[mddev
->major_version
]
3573 .load_super(rdev
, rdev0
, mddev
->minor_version
);
3577 } else if (mddev
->external
)
3578 rdev
= md_import_device(dev
, -2, -1);
3580 rdev
= md_import_device(dev
, -1, -1);
3583 return PTR_ERR(rdev
);
3584 err
= bind_rdev_to_array(rdev
, mddev
);
3588 return err
? err
: len
;
3591 static struct md_sysfs_entry md_new_device
=
3592 __ATTR(new_dev
, S_IWUSR
, null_show
, new_dev_store
);
3595 bitmap_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3598 unsigned long chunk
, end_chunk
;
3602 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3604 chunk
= end_chunk
= simple_strtoul(buf
, &end
, 0);
3605 if (buf
== end
) break;
3606 if (*end
== '-') { /* range */
3608 end_chunk
= simple_strtoul(buf
, &end
, 0);
3609 if (buf
== end
) break;
3611 if (*end
&& !isspace(*end
)) break;
3612 bitmap_dirty_bits(mddev
->bitmap
, chunk
, end_chunk
);
3613 buf
= skip_spaces(end
);
3615 bitmap_unplug(mddev
->bitmap
); /* flush the bits to disk */
3620 static struct md_sysfs_entry md_bitmap
=
3621 __ATTR(bitmap_set_bits
, S_IWUSR
, null_show
, bitmap_store
);
3624 size_show(mddev_t
*mddev
, char *page
)
3626 return sprintf(page
, "%llu\n",
3627 (unsigned long long)mddev
->dev_sectors
/ 2);
3630 static int update_size(mddev_t
*mddev
, sector_t num_sectors
);
3633 size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3635 /* If array is inactive, we can reduce the component size, but
3636 * not increase it (except from 0).
3637 * If array is active, we can try an on-line resize
3640 int err
= strict_blocks_to_sectors(buf
, §ors
);
3645 err
= update_size(mddev
, sectors
);
3646 md_update_sb(mddev
, 1);
3648 if (mddev
->dev_sectors
== 0 ||
3649 mddev
->dev_sectors
> sectors
)
3650 mddev
->dev_sectors
= sectors
;
3654 return err
? err
: len
;
3657 static struct md_sysfs_entry md_size
=
3658 __ATTR(component_size
, S_IRUGO
|S_IWUSR
, size_show
, size_store
);
3663 * 'none' for arrays with no metadata (good luck...)
3664 * 'external' for arrays with externally managed metadata,
3665 * or N.M for internally known formats
3668 metadata_show(mddev_t
*mddev
, char *page
)
3670 if (mddev
->persistent
)
3671 return sprintf(page
, "%d.%d\n",
3672 mddev
->major_version
, mddev
->minor_version
);
3673 else if (mddev
->external
)
3674 return sprintf(page
, "external:%s\n", mddev
->metadata_type
);
3676 return sprintf(page
, "none\n");
3680 metadata_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3684 /* Changing the details of 'external' metadata is
3685 * always permitted. Otherwise there must be
3686 * no devices attached to the array.
3688 if (mddev
->external
&& strncmp(buf
, "external:", 9) == 0)
3690 else if (!list_empty(&mddev
->disks
))
3693 if (cmd_match(buf
, "none")) {
3694 mddev
->persistent
= 0;
3695 mddev
->external
= 0;
3696 mddev
->major_version
= 0;
3697 mddev
->minor_version
= 90;
3700 if (strncmp(buf
, "external:", 9) == 0) {
3701 size_t namelen
= len
-9;
3702 if (namelen
>= sizeof(mddev
->metadata_type
))
3703 namelen
= sizeof(mddev
->metadata_type
)-1;
3704 strncpy(mddev
->metadata_type
, buf
+9, namelen
);
3705 mddev
->metadata_type
[namelen
] = 0;
3706 if (namelen
&& mddev
->metadata_type
[namelen
-1] == '\n')
3707 mddev
->metadata_type
[--namelen
] = 0;
3708 mddev
->persistent
= 0;
3709 mddev
->external
= 1;
3710 mddev
->major_version
= 0;
3711 mddev
->minor_version
= 90;
3714 major
= simple_strtoul(buf
, &e
, 10);
3715 if (e
==buf
|| *e
!= '.')
3718 minor
= simple_strtoul(buf
, &e
, 10);
3719 if (e
==buf
|| (*e
&& *e
!= '\n') )
3721 if (major
>= ARRAY_SIZE(super_types
) || super_types
[major
].name
== NULL
)
3723 mddev
->major_version
= major
;
3724 mddev
->minor_version
= minor
;
3725 mddev
->persistent
= 1;
3726 mddev
->external
= 0;
3730 static struct md_sysfs_entry md_metadata
=
3731 __ATTR(metadata_version
, S_IRUGO
|S_IWUSR
, metadata_show
, metadata_store
);
3734 action_show(mddev_t
*mddev
, char *page
)
3736 char *type
= "idle";
3737 if (test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
3739 else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
3740 (!mddev
->ro
&& test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))) {
3741 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
3743 else if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
3744 if (!test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
3746 else if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
3750 } else if (test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
))
3753 return sprintf(page
, "%s\n", type
);
3756 static void reap_sync_thread(mddev_t
*mddev
);
3759 action_store(mddev_t
*mddev
, const char *page
, size_t len
)
3761 if (!mddev
->pers
|| !mddev
->pers
->sync_request
)
3764 if (cmd_match(page
, "frozen"))
3765 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
3767 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
3769 if (cmd_match(page
, "idle") || cmd_match(page
, "frozen")) {
3770 if (mddev
->sync_thread
) {
3771 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
3772 reap_sync_thread(mddev
);
3774 } else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
3775 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
3777 else if (cmd_match(page
, "resync"))
3778 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3779 else if (cmd_match(page
, "recover")) {
3780 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
3781 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3782 } else if (cmd_match(page
, "reshape")) {
3784 if (mddev
->pers
->start_reshape
== NULL
)
3786 err
= mddev
->pers
->start_reshape(mddev
);
3789 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
3791 if (cmd_match(page
, "check"))
3792 set_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
3793 else if (!cmd_match(page
, "repair"))
3795 set_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
3796 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
3798 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3799 md_wakeup_thread(mddev
->thread
);
3800 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
3805 mismatch_cnt_show(mddev_t
*mddev
, char *page
)
3807 return sprintf(page
, "%llu\n",
3808 (unsigned long long) mddev
->resync_mismatches
);
3811 static struct md_sysfs_entry md_scan_mode
=
3812 __ATTR(sync_action
, S_IRUGO
|S_IWUSR
, action_show
, action_store
);
3815 static struct md_sysfs_entry md_mismatches
= __ATTR_RO(mismatch_cnt
);
3818 sync_min_show(mddev_t
*mddev
, char *page
)
3820 return sprintf(page
, "%d (%s)\n", speed_min(mddev
),
3821 mddev
->sync_speed_min
? "local": "system");
3825 sync_min_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3829 if (strncmp(buf
, "system", 6)==0) {
3830 mddev
->sync_speed_min
= 0;
3833 min
= simple_strtoul(buf
, &e
, 10);
3834 if (buf
== e
|| (*e
&& *e
!= '\n') || min
<= 0)
3836 mddev
->sync_speed_min
= min
;
3840 static struct md_sysfs_entry md_sync_min
=
3841 __ATTR(sync_speed_min
, S_IRUGO
|S_IWUSR
, sync_min_show
, sync_min_store
);
3844 sync_max_show(mddev_t
*mddev
, char *page
)
3846 return sprintf(page
, "%d (%s)\n", speed_max(mddev
),
3847 mddev
->sync_speed_max
? "local": "system");
3851 sync_max_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3855 if (strncmp(buf
, "system", 6)==0) {
3856 mddev
->sync_speed_max
= 0;
3859 max
= simple_strtoul(buf
, &e
, 10);
3860 if (buf
== e
|| (*e
&& *e
!= '\n') || max
<= 0)
3862 mddev
->sync_speed_max
= max
;
3866 static struct md_sysfs_entry md_sync_max
=
3867 __ATTR(sync_speed_max
, S_IRUGO
|S_IWUSR
, sync_max_show
, sync_max_store
);
3870 degraded_show(mddev_t
*mddev
, char *page
)
3872 return sprintf(page
, "%d\n", mddev
->degraded
);
3874 static struct md_sysfs_entry md_degraded
= __ATTR_RO(degraded
);
3877 sync_force_parallel_show(mddev_t
*mddev
, char *page
)
3879 return sprintf(page
, "%d\n", mddev
->parallel_resync
);
3883 sync_force_parallel_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3887 if (strict_strtol(buf
, 10, &n
))
3890 if (n
!= 0 && n
!= 1)
3893 mddev
->parallel_resync
= n
;
3895 if (mddev
->sync_thread
)
3896 wake_up(&resync_wait
);
3901 /* force parallel resync, even with shared block devices */
3902 static struct md_sysfs_entry md_sync_force_parallel
=
3903 __ATTR(sync_force_parallel
, S_IRUGO
|S_IWUSR
,
3904 sync_force_parallel_show
, sync_force_parallel_store
);
3907 sync_speed_show(mddev_t
*mddev
, char *page
)
3909 unsigned long resync
, dt
, db
;
3910 if (mddev
->curr_resync
== 0)
3911 return sprintf(page
, "none\n");
3912 resync
= mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
);
3913 dt
= (jiffies
- mddev
->resync_mark
) / HZ
;
3915 db
= resync
- mddev
->resync_mark_cnt
;
3916 return sprintf(page
, "%lu\n", db
/dt
/2); /* K/sec */
3919 static struct md_sysfs_entry md_sync_speed
= __ATTR_RO(sync_speed
);
3922 sync_completed_show(mddev_t
*mddev
, char *page
)
3924 unsigned long long max_sectors
, resync
;
3926 if (!test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3927 return sprintf(page
, "none\n");
3929 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
3930 max_sectors
= mddev
->resync_max_sectors
;
3932 max_sectors
= mddev
->dev_sectors
;
3934 resync
= mddev
->curr_resync_completed
;
3935 return sprintf(page
, "%llu / %llu\n", resync
, max_sectors
);
3938 static struct md_sysfs_entry md_sync_completed
= __ATTR_RO(sync_completed
);
3941 min_sync_show(mddev_t
*mddev
, char *page
)
3943 return sprintf(page
, "%llu\n",
3944 (unsigned long long)mddev
->resync_min
);
3947 min_sync_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3949 unsigned long long min
;
3950 if (strict_strtoull(buf
, 10, &min
))
3952 if (min
> mddev
->resync_max
)
3954 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3957 /* Must be a multiple of chunk_size */
3958 if (mddev
->chunk_sectors
) {
3959 sector_t temp
= min
;
3960 if (sector_div(temp
, mddev
->chunk_sectors
))
3963 mddev
->resync_min
= min
;
3968 static struct md_sysfs_entry md_min_sync
=
3969 __ATTR(sync_min
, S_IRUGO
|S_IWUSR
, min_sync_show
, min_sync_store
);
3972 max_sync_show(mddev_t
*mddev
, char *page
)
3974 if (mddev
->resync_max
== MaxSector
)
3975 return sprintf(page
, "max\n");
3977 return sprintf(page
, "%llu\n",
3978 (unsigned long long)mddev
->resync_max
);
3981 max_sync_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3983 if (strncmp(buf
, "max", 3) == 0)
3984 mddev
->resync_max
= MaxSector
;
3986 unsigned long long max
;
3987 if (strict_strtoull(buf
, 10, &max
))
3989 if (max
< mddev
->resync_min
)
3991 if (max
< mddev
->resync_max
&&
3993 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3996 /* Must be a multiple of chunk_size */
3997 if (mddev
->chunk_sectors
) {
3998 sector_t temp
= max
;
3999 if (sector_div(temp
, mddev
->chunk_sectors
))
4002 mddev
->resync_max
= max
;
4004 wake_up(&mddev
->recovery_wait
);
4008 static struct md_sysfs_entry md_max_sync
=
4009 __ATTR(sync_max
, S_IRUGO
|S_IWUSR
, max_sync_show
, max_sync_store
);
4012 suspend_lo_show(mddev_t
*mddev
, char *page
)
4014 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_lo
);
4018 suspend_lo_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4021 unsigned long long new = simple_strtoull(buf
, &e
, 10);
4022 unsigned long long old
= mddev
->suspend_lo
;
4024 if (mddev
->pers
== NULL
||
4025 mddev
->pers
->quiesce
== NULL
)
4027 if (buf
== e
|| (*e
&& *e
!= '\n'))
4030 mddev
->suspend_lo
= new;
4032 /* Shrinking suspended region */
4033 mddev
->pers
->quiesce(mddev
, 2);
4035 /* Expanding suspended region - need to wait */
4036 mddev
->pers
->quiesce(mddev
, 1);
4037 mddev
->pers
->quiesce(mddev
, 0);
4041 static struct md_sysfs_entry md_suspend_lo
=
4042 __ATTR(suspend_lo
, S_IRUGO
|S_IWUSR
, suspend_lo_show
, suspend_lo_store
);
4046 suspend_hi_show(mddev_t
*mddev
, char *page
)
4048 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_hi
);
4052 suspend_hi_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4055 unsigned long long new = simple_strtoull(buf
, &e
, 10);
4056 unsigned long long old
= mddev
->suspend_hi
;
4058 if (mddev
->pers
== NULL
||
4059 mddev
->pers
->quiesce
== NULL
)
4061 if (buf
== e
|| (*e
&& *e
!= '\n'))
4064 mddev
->suspend_hi
= new;
4066 /* Shrinking suspended region */
4067 mddev
->pers
->quiesce(mddev
, 2);
4069 /* Expanding suspended region - need to wait */
4070 mddev
->pers
->quiesce(mddev
, 1);
4071 mddev
->pers
->quiesce(mddev
, 0);
4075 static struct md_sysfs_entry md_suspend_hi
=
4076 __ATTR(suspend_hi
, S_IRUGO
|S_IWUSR
, suspend_hi_show
, suspend_hi_store
);
4079 reshape_position_show(mddev_t
*mddev
, char *page
)
4081 if (mddev
->reshape_position
!= MaxSector
)
4082 return sprintf(page
, "%llu\n",
4083 (unsigned long long)mddev
->reshape_position
);
4084 strcpy(page
, "none\n");
4089 reshape_position_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4092 unsigned long long new = simple_strtoull(buf
, &e
, 10);
4095 if (buf
== e
|| (*e
&& *e
!= '\n'))
4097 mddev
->reshape_position
= new;
4098 mddev
->delta_disks
= 0;
4099 mddev
->new_level
= mddev
->level
;
4100 mddev
->new_layout
= mddev
->layout
;
4101 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
4105 static struct md_sysfs_entry md_reshape_position
=
4106 __ATTR(reshape_position
, S_IRUGO
|S_IWUSR
, reshape_position_show
,
4107 reshape_position_store
);
4110 array_size_show(mddev_t
*mddev
, char *page
)
4112 if (mddev
->external_size
)
4113 return sprintf(page
, "%llu\n",
4114 (unsigned long long)mddev
->array_sectors
/2);
4116 return sprintf(page
, "default\n");
4120 array_size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4124 if (strncmp(buf
, "default", 7) == 0) {
4126 sectors
= mddev
->pers
->size(mddev
, 0, 0);
4128 sectors
= mddev
->array_sectors
;
4130 mddev
->external_size
= 0;
4132 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
4134 if (mddev
->pers
&& mddev
->pers
->size(mddev
, 0, 0) < sectors
)
4137 mddev
->external_size
= 1;
4140 mddev
->array_sectors
= sectors
;
4141 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
4143 revalidate_disk(mddev
->gendisk
);
4148 static struct md_sysfs_entry md_array_size
=
4149 __ATTR(array_size
, S_IRUGO
|S_IWUSR
, array_size_show
,
4152 static struct attribute
*md_default_attrs
[] = {
4155 &md_raid_disks
.attr
,
4156 &md_chunk_size
.attr
,
4158 &md_resync_start
.attr
,
4160 &md_new_device
.attr
,
4161 &md_safe_delay
.attr
,
4162 &md_array_state
.attr
,
4163 &md_reshape_position
.attr
,
4164 &md_array_size
.attr
,
4165 &max_corr_read_errors
.attr
,
4169 static struct attribute
*md_redundancy_attrs
[] = {
4171 &md_mismatches
.attr
,
4174 &md_sync_speed
.attr
,
4175 &md_sync_force_parallel
.attr
,
4176 &md_sync_completed
.attr
,
4179 &md_suspend_lo
.attr
,
4180 &md_suspend_hi
.attr
,
4185 static struct attribute_group md_redundancy_group
= {
4187 .attrs
= md_redundancy_attrs
,
4192 md_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
4194 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
4195 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
4200 rv
= mddev_lock(mddev
);
4202 rv
= entry
->show(mddev
, page
);
4203 mddev_unlock(mddev
);
4209 md_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
4210 const char *page
, size_t length
)
4212 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
4213 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
4218 if (!capable(CAP_SYS_ADMIN
))
4220 rv
= mddev_lock(mddev
);
4221 if (mddev
->hold_active
== UNTIL_IOCTL
)
4222 mddev
->hold_active
= 0;
4224 rv
= entry
->store(mddev
, page
, length
);
4225 mddev_unlock(mddev
);
4230 static void md_free(struct kobject
*ko
)
4232 mddev_t
*mddev
= container_of(ko
, mddev_t
, kobj
);
4234 if (mddev
->sysfs_state
)
4235 sysfs_put(mddev
->sysfs_state
);
4237 if (mddev
->gendisk
) {
4238 del_gendisk(mddev
->gendisk
);
4239 put_disk(mddev
->gendisk
);
4242 blk_cleanup_queue(mddev
->queue
);
4247 static const struct sysfs_ops md_sysfs_ops
= {
4248 .show
= md_attr_show
,
4249 .store
= md_attr_store
,
4251 static struct kobj_type md_ktype
= {
4253 .sysfs_ops
= &md_sysfs_ops
,
4254 .default_attrs
= md_default_attrs
,
4259 static void mddev_delayed_delete(struct work_struct
*ws
)
4261 mddev_t
*mddev
= container_of(ws
, mddev_t
, del_work
);
4263 sysfs_remove_group(&mddev
->kobj
, &md_bitmap_group
);
4264 kobject_del(&mddev
->kobj
);
4265 kobject_put(&mddev
->kobj
);
4268 static int md_alloc(dev_t dev
, char *name
)
4270 static DEFINE_MUTEX(disks_mutex
);
4271 mddev_t
*mddev
= mddev_find(dev
);
4272 struct gendisk
*disk
;
4281 partitioned
= (MAJOR(mddev
->unit
) != MD_MAJOR
);
4282 shift
= partitioned
? MdpMinorShift
: 0;
4283 unit
= MINOR(mddev
->unit
) >> shift
;
4285 /* wait for any previous instance of this device to be
4286 * completely removed (mddev_delayed_delete).
4288 flush_workqueue(md_misc_wq
);
4290 mutex_lock(&disks_mutex
);
4296 /* Need to ensure that 'name' is not a duplicate.
4299 spin_lock(&all_mddevs_lock
);
4301 list_for_each_entry(mddev2
, &all_mddevs
, all_mddevs
)
4302 if (mddev2
->gendisk
&&
4303 strcmp(mddev2
->gendisk
->disk_name
, name
) == 0) {
4304 spin_unlock(&all_mddevs_lock
);
4307 spin_unlock(&all_mddevs_lock
);
4311 mddev
->queue
= blk_alloc_queue(GFP_KERNEL
);
4314 mddev
->queue
->queuedata
= mddev
;
4316 blk_queue_make_request(mddev
->queue
, md_make_request
);
4318 disk
= alloc_disk(1 << shift
);
4320 blk_cleanup_queue(mddev
->queue
);
4321 mddev
->queue
= NULL
;
4324 disk
->major
= MAJOR(mddev
->unit
);
4325 disk
->first_minor
= unit
<< shift
;
4327 strcpy(disk
->disk_name
, name
);
4328 else if (partitioned
)
4329 sprintf(disk
->disk_name
, "md_d%d", unit
);
4331 sprintf(disk
->disk_name
, "md%d", unit
);
4332 disk
->fops
= &md_fops
;
4333 disk
->private_data
= mddev
;
4334 disk
->queue
= mddev
->queue
;
4335 /* Allow extended partitions. This makes the
4336 * 'mdp' device redundant, but we can't really
4339 disk
->flags
|= GENHD_FL_EXT_DEVT
;
4341 mddev
->gendisk
= disk
;
4342 error
= kobject_init_and_add(&mddev
->kobj
, &md_ktype
,
4343 &disk_to_dev(disk
)->kobj
, "%s", "md");
4345 /* This isn't possible, but as kobject_init_and_add is marked
4346 * __must_check, we must do something with the result
4348 printk(KERN_WARNING
"md: cannot register %s/md - name in use\n",
4352 if (mddev
->kobj
.sd
&&
4353 sysfs_create_group(&mddev
->kobj
, &md_bitmap_group
))
4354 printk(KERN_DEBUG
"pointless warning\n");
4356 blk_queue_flush(mddev
->queue
, REQ_FLUSH
| REQ_FUA
);
4358 mutex_unlock(&disks_mutex
);
4359 if (!error
&& mddev
->kobj
.sd
) {
4360 kobject_uevent(&mddev
->kobj
, KOBJ_ADD
);
4361 mddev
->sysfs_state
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "array_state");
4367 static struct kobject
*md_probe(dev_t dev
, int *part
, void *data
)
4369 md_alloc(dev
, NULL
);
4373 static int add_named_array(const char *val
, struct kernel_param
*kp
)
4375 /* val must be "md_*" where * is not all digits.
4376 * We allocate an array with a large free minor number, and
4377 * set the name to val. val must not already be an active name.
4379 int len
= strlen(val
);
4380 char buf
[DISK_NAME_LEN
];
4382 while (len
&& val
[len
-1] == '\n')
4384 if (len
>= DISK_NAME_LEN
)
4386 strlcpy(buf
, val
, len
+1);
4387 if (strncmp(buf
, "md_", 3) != 0)
4389 return md_alloc(0, buf
);
4392 static void md_safemode_timeout(unsigned long data
)
4394 mddev_t
*mddev
= (mddev_t
*) data
;
4396 if (!atomic_read(&mddev
->writes_pending
)) {
4397 mddev
->safemode
= 1;
4398 if (mddev
->external
)
4399 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4401 md_wakeup_thread(mddev
->thread
);
4404 static int start_dirty_degraded
;
4406 int md_run(mddev_t
*mddev
)
4410 struct mdk_personality
*pers
;
4412 if (list_empty(&mddev
->disks
))
4413 /* cannot run an array with no devices.. */
4418 /* Cannot run until previous stop completes properly */
4419 if (mddev
->sysfs_active
)
4423 * Analyze all RAID superblock(s)
4425 if (!mddev
->raid_disks
) {
4426 if (!mddev
->persistent
)
4431 if (mddev
->level
!= LEVEL_NONE
)
4432 request_module("md-level-%d", mddev
->level
);
4433 else if (mddev
->clevel
[0])
4434 request_module("md-%s", mddev
->clevel
);
4437 * Drop all container device buffers, from now on
4438 * the only valid external interface is through the md
4441 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
4442 if (test_bit(Faulty
, &rdev
->flags
))
4444 sync_blockdev(rdev
->bdev
);
4445 invalidate_bdev(rdev
->bdev
);
4447 /* perform some consistency tests on the device.
4448 * We don't want the data to overlap the metadata,
4449 * Internal Bitmap issues have been handled elsewhere.
4451 if (rdev
->meta_bdev
) {
4452 /* Nothing to check */;
4453 } else if (rdev
->data_offset
< rdev
->sb_start
) {
4454 if (mddev
->dev_sectors
&&
4455 rdev
->data_offset
+ mddev
->dev_sectors
4457 printk("md: %s: data overlaps metadata\n",
4462 if (rdev
->sb_start
+ rdev
->sb_size
/512
4463 > rdev
->data_offset
) {
4464 printk("md: %s: metadata overlaps data\n",
4469 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
4472 if (mddev
->bio_set
== NULL
)
4473 mddev
->bio_set
= bioset_create(BIO_POOL_SIZE
, sizeof(mddev
));
4475 spin_lock(&pers_lock
);
4476 pers
= find_pers(mddev
->level
, mddev
->clevel
);
4477 if (!pers
|| !try_module_get(pers
->owner
)) {
4478 spin_unlock(&pers_lock
);
4479 if (mddev
->level
!= LEVEL_NONE
)
4480 printk(KERN_WARNING
"md: personality for level %d is not loaded!\n",
4483 printk(KERN_WARNING
"md: personality for level %s is not loaded!\n",
4488 spin_unlock(&pers_lock
);
4489 if (mddev
->level
!= pers
->level
) {
4490 mddev
->level
= pers
->level
;
4491 mddev
->new_level
= pers
->level
;
4493 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
4495 if (mddev
->reshape_position
!= MaxSector
&&
4496 pers
->start_reshape
== NULL
) {
4497 /* This personality cannot handle reshaping... */
4499 module_put(pers
->owner
);
4503 if (pers
->sync_request
) {
4504 /* Warn if this is a potentially silly
4507 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
4511 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4512 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
4514 rdev
->bdev
->bd_contains
==
4515 rdev2
->bdev
->bd_contains
) {
4517 "%s: WARNING: %s appears to be"
4518 " on the same physical disk as"
4521 bdevname(rdev
->bdev
,b
),
4522 bdevname(rdev2
->bdev
,b2
));
4529 "True protection against single-disk"
4530 " failure might be compromised.\n");
4533 mddev
->recovery
= 0;
4534 /* may be over-ridden by personality */
4535 mddev
->resync_max_sectors
= mddev
->dev_sectors
;
4537 mddev
->ok_start_degraded
= start_dirty_degraded
;
4539 if (start_readonly
&& mddev
->ro
== 0)
4540 mddev
->ro
= 2; /* read-only, but switch on first write */
4542 err
= mddev
->pers
->run(mddev
);
4544 printk(KERN_ERR
"md: pers->run() failed ...\n");
4545 else if (mddev
->pers
->size(mddev
, 0, 0) < mddev
->array_sectors
) {
4546 WARN_ONCE(!mddev
->external_size
, "%s: default size too small,"
4547 " but 'external_size' not in effect?\n", __func__
);
4549 "md: invalid array_size %llu > default size %llu\n",
4550 (unsigned long long)mddev
->array_sectors
/ 2,
4551 (unsigned long long)mddev
->pers
->size(mddev
, 0, 0) / 2);
4553 mddev
->pers
->stop(mddev
);
4555 if (err
== 0 && mddev
->pers
->sync_request
) {
4556 err
= bitmap_create(mddev
);
4558 printk(KERN_ERR
"%s: failed to create bitmap (%d)\n",
4559 mdname(mddev
), err
);
4560 mddev
->pers
->stop(mddev
);
4564 module_put(mddev
->pers
->owner
);
4566 bitmap_destroy(mddev
);
4569 if (mddev
->pers
->sync_request
) {
4570 if (mddev
->kobj
.sd
&&
4571 sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
4573 "md: cannot register extra attributes for %s\n",
4575 mddev
->sysfs_action
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "sync_action");
4576 } else if (mddev
->ro
== 2) /* auto-readonly not meaningful */
4579 atomic_set(&mddev
->writes_pending
,0);
4580 atomic_set(&mddev
->max_corr_read_errors
,
4581 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS
);
4582 mddev
->safemode
= 0;
4583 mddev
->safemode_timer
.function
= md_safemode_timeout
;
4584 mddev
->safemode_timer
.data
= (unsigned long) mddev
;
4585 mddev
->safemode_delay
= (200 * HZ
)/1000 +1; /* 200 msec delay */
4589 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4590 if (rdev
->raid_disk
>= 0) {
4592 sprintf(nm
, "rd%d", rdev
->raid_disk
);
4593 if (sysfs_create_link(&mddev
->kobj
, &rdev
->kobj
, nm
))
4594 /* failure here is OK */;
4597 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4600 md_update_sb(mddev
, 0);
4602 md_wakeup_thread(mddev
->thread
);
4603 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
4605 md_new_event(mddev
);
4606 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4607 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
4608 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
4611 EXPORT_SYMBOL_GPL(md_run
);
4613 static int do_md_run(mddev_t
*mddev
)
4617 err
= md_run(mddev
);
4620 err
= bitmap_load(mddev
);
4622 bitmap_destroy(mddev
);
4625 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
4626 revalidate_disk(mddev
->gendisk
);
4627 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
4632 static int restart_array(mddev_t
*mddev
)
4634 struct gendisk
*disk
= mddev
->gendisk
;
4636 /* Complain if it has no devices */
4637 if (list_empty(&mddev
->disks
))
4643 mddev
->safemode
= 0;
4645 set_disk_ro(disk
, 0);
4646 printk(KERN_INFO
"md: %s switched to read-write mode.\n",
4648 /* Kick recovery or resync if necessary */
4649 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4650 md_wakeup_thread(mddev
->thread
);
4651 md_wakeup_thread(mddev
->sync_thread
);
4652 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4656 /* similar to deny_write_access, but accounts for our holding a reference
4657 * to the file ourselves */
4658 static int deny_bitmap_write_access(struct file
* file
)
4660 struct inode
*inode
= file
->f_mapping
->host
;
4662 spin_lock(&inode
->i_lock
);
4663 if (atomic_read(&inode
->i_writecount
) > 1) {
4664 spin_unlock(&inode
->i_lock
);
4667 atomic_set(&inode
->i_writecount
, -1);
4668 spin_unlock(&inode
->i_lock
);
4673 void restore_bitmap_write_access(struct file
*file
)
4675 struct inode
*inode
= file
->f_mapping
->host
;
4677 spin_lock(&inode
->i_lock
);
4678 atomic_set(&inode
->i_writecount
, 1);
4679 spin_unlock(&inode
->i_lock
);
4682 static void md_clean(mddev_t
*mddev
)
4684 mddev
->array_sectors
= 0;
4685 mddev
->external_size
= 0;
4686 mddev
->dev_sectors
= 0;
4687 mddev
->raid_disks
= 0;
4688 mddev
->recovery_cp
= 0;
4689 mddev
->resync_min
= 0;
4690 mddev
->resync_max
= MaxSector
;
4691 mddev
->reshape_position
= MaxSector
;
4692 mddev
->external
= 0;
4693 mddev
->persistent
= 0;
4694 mddev
->level
= LEVEL_NONE
;
4695 mddev
->clevel
[0] = 0;
4698 mddev
->metadata_type
[0] = 0;
4699 mddev
->chunk_sectors
= 0;
4700 mddev
->ctime
= mddev
->utime
= 0;
4702 mddev
->max_disks
= 0;
4704 mddev
->can_decrease_events
= 0;
4705 mddev
->delta_disks
= 0;
4706 mddev
->new_level
= LEVEL_NONE
;
4707 mddev
->new_layout
= 0;
4708 mddev
->new_chunk_sectors
= 0;
4709 mddev
->curr_resync
= 0;
4710 mddev
->resync_mismatches
= 0;
4711 mddev
->suspend_lo
= mddev
->suspend_hi
= 0;
4712 mddev
->sync_speed_min
= mddev
->sync_speed_max
= 0;
4713 mddev
->recovery
= 0;
4715 mddev
->degraded
= 0;
4716 mddev
->safemode
= 0;
4717 mddev
->bitmap_info
.offset
= 0;
4718 mddev
->bitmap_info
.default_offset
= 0;
4719 mddev
->bitmap_info
.chunksize
= 0;
4720 mddev
->bitmap_info
.daemon_sleep
= 0;
4721 mddev
->bitmap_info
.max_write_behind
= 0;
4725 static void __md_stop_writes(mddev_t
*mddev
)
4727 if (mddev
->sync_thread
) {
4728 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4729 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4730 reap_sync_thread(mddev
);
4733 del_timer_sync(&mddev
->safemode_timer
);
4735 bitmap_flush(mddev
);
4736 md_super_wait(mddev
);
4738 if (!mddev
->in_sync
|| mddev
->flags
) {
4739 /* mark array as shutdown cleanly */
4741 md_update_sb(mddev
, 1);
4745 void md_stop_writes(mddev_t
*mddev
)
4748 __md_stop_writes(mddev
);
4749 mddev_unlock(mddev
);
4751 EXPORT_SYMBOL_GPL(md_stop_writes
);
4753 void md_stop(mddev_t
*mddev
)
4756 mddev
->pers
->stop(mddev
);
4757 if (mddev
->pers
->sync_request
&& mddev
->to_remove
== NULL
)
4758 mddev
->to_remove
= &md_redundancy_group
;
4759 module_put(mddev
->pers
->owner
);
4761 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4763 EXPORT_SYMBOL_GPL(md_stop
);
4765 static int md_set_readonly(mddev_t
*mddev
, int is_open
)
4768 mutex_lock(&mddev
->open_mutex
);
4769 if (atomic_read(&mddev
->openers
) > is_open
) {
4770 printk("md: %s still in use.\n",mdname(mddev
));
4775 __md_stop_writes(mddev
);
4781 set_disk_ro(mddev
->gendisk
, 1);
4782 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4783 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4787 mutex_unlock(&mddev
->open_mutex
);
4792 * 0 - completely stop and dis-assemble array
4793 * 2 - stop but do not disassemble array
4795 static int do_md_stop(mddev_t
* mddev
, int mode
, int is_open
)
4797 struct gendisk
*disk
= mddev
->gendisk
;
4800 mutex_lock(&mddev
->open_mutex
);
4801 if (atomic_read(&mddev
->openers
) > is_open
||
4802 mddev
->sysfs_active
) {
4803 printk("md: %s still in use.\n",mdname(mddev
));
4804 mutex_unlock(&mddev
->open_mutex
);
4810 set_disk_ro(disk
, 0);
4812 __md_stop_writes(mddev
);
4814 mddev
->queue
->merge_bvec_fn
= NULL
;
4815 mddev
->queue
->unplug_fn
= NULL
;
4816 mddev
->queue
->backing_dev_info
.congested_fn
= NULL
;
4818 /* tell userspace to handle 'inactive' */
4819 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4821 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4822 if (rdev
->raid_disk
>= 0) {
4824 sprintf(nm
, "rd%d", rdev
->raid_disk
);
4825 sysfs_remove_link(&mddev
->kobj
, nm
);
4828 set_capacity(disk
, 0);
4829 mutex_unlock(&mddev
->open_mutex
);
4830 revalidate_disk(disk
);
4835 mutex_unlock(&mddev
->open_mutex
);
4837 * Free resources if final stop
4840 printk(KERN_INFO
"md: %s stopped.\n", mdname(mddev
));
4842 bitmap_destroy(mddev
);
4843 if (mddev
->bitmap_info
.file
) {
4844 restore_bitmap_write_access(mddev
->bitmap_info
.file
);
4845 fput(mddev
->bitmap_info
.file
);
4846 mddev
->bitmap_info
.file
= NULL
;
4848 mddev
->bitmap_info
.offset
= 0;
4850 export_array(mddev
);
4853 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
4854 if (mddev
->hold_active
== UNTIL_STOP
)
4855 mddev
->hold_active
= 0;
4857 blk_integrity_unregister(disk
);
4858 md_new_event(mddev
);
4859 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4864 static void autorun_array(mddev_t
*mddev
)
4869 if (list_empty(&mddev
->disks
))
4872 printk(KERN_INFO
"md: running: ");
4874 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
4875 char b
[BDEVNAME_SIZE
];
4876 printk("<%s>", bdevname(rdev
->bdev
,b
));
4880 err
= do_md_run(mddev
);
4882 printk(KERN_WARNING
"md: do_md_run() returned %d\n", err
);
4883 do_md_stop(mddev
, 0, 0);
4888 * lets try to run arrays based on all disks that have arrived
4889 * until now. (those are in pending_raid_disks)
4891 * the method: pick the first pending disk, collect all disks with
4892 * the same UUID, remove all from the pending list and put them into
4893 * the 'same_array' list. Then order this list based on superblock
4894 * update time (freshest comes first), kick out 'old' disks and
4895 * compare superblocks. If everything's fine then run it.
4897 * If "unit" is allocated, then bump its reference count
4899 static void autorun_devices(int part
)
4901 mdk_rdev_t
*rdev0
, *rdev
, *tmp
;
4903 char b
[BDEVNAME_SIZE
];
4905 printk(KERN_INFO
"md: autorun ...\n");
4906 while (!list_empty(&pending_raid_disks
)) {
4909 LIST_HEAD(candidates
);
4910 rdev0
= list_entry(pending_raid_disks
.next
,
4911 mdk_rdev_t
, same_set
);
4913 printk(KERN_INFO
"md: considering %s ...\n",
4914 bdevname(rdev0
->bdev
,b
));
4915 INIT_LIST_HEAD(&candidates
);
4916 rdev_for_each_list(rdev
, tmp
, &pending_raid_disks
)
4917 if (super_90_load(rdev
, rdev0
, 0) >= 0) {
4918 printk(KERN_INFO
"md: adding %s ...\n",
4919 bdevname(rdev
->bdev
,b
));
4920 list_move(&rdev
->same_set
, &candidates
);
4923 * now we have a set of devices, with all of them having
4924 * mostly sane superblocks. It's time to allocate the
4928 dev
= MKDEV(mdp_major
,
4929 rdev0
->preferred_minor
<< MdpMinorShift
);
4930 unit
= MINOR(dev
) >> MdpMinorShift
;
4932 dev
= MKDEV(MD_MAJOR
, rdev0
->preferred_minor
);
4935 if (rdev0
->preferred_minor
!= unit
) {
4936 printk(KERN_INFO
"md: unit number in %s is bad: %d\n",
4937 bdevname(rdev0
->bdev
, b
), rdev0
->preferred_minor
);
4941 md_probe(dev
, NULL
, NULL
);
4942 mddev
= mddev_find(dev
);
4943 if (!mddev
|| !mddev
->gendisk
) {
4947 "md: cannot allocate memory for md drive.\n");
4950 if (mddev_lock(mddev
))
4951 printk(KERN_WARNING
"md: %s locked, cannot run\n",
4953 else if (mddev
->raid_disks
|| mddev
->major_version
4954 || !list_empty(&mddev
->disks
)) {
4956 "md: %s already running, cannot run %s\n",
4957 mdname(mddev
), bdevname(rdev0
->bdev
,b
));
4958 mddev_unlock(mddev
);
4960 printk(KERN_INFO
"md: created %s\n", mdname(mddev
));
4961 mddev
->persistent
= 1;
4962 rdev_for_each_list(rdev
, tmp
, &candidates
) {
4963 list_del_init(&rdev
->same_set
);
4964 if (bind_rdev_to_array(rdev
, mddev
))
4967 autorun_array(mddev
);
4968 mddev_unlock(mddev
);
4970 /* on success, candidates will be empty, on error
4973 rdev_for_each_list(rdev
, tmp
, &candidates
) {
4974 list_del_init(&rdev
->same_set
);
4979 printk(KERN_INFO
"md: ... autorun DONE.\n");
4981 #endif /* !MODULE */
4983 static int get_version(void __user
* arg
)
4987 ver
.major
= MD_MAJOR_VERSION
;
4988 ver
.minor
= MD_MINOR_VERSION
;
4989 ver
.patchlevel
= MD_PATCHLEVEL_VERSION
;
4991 if (copy_to_user(arg
, &ver
, sizeof(ver
)))
4997 static int get_array_info(mddev_t
* mddev
, void __user
* arg
)
4999 mdu_array_info_t info
;
5000 int nr
,working
,insync
,failed
,spare
;
5003 nr
=working
=insync
=failed
=spare
=0;
5004 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
5006 if (test_bit(Faulty
, &rdev
->flags
))
5010 if (test_bit(In_sync
, &rdev
->flags
))
5017 info
.major_version
= mddev
->major_version
;
5018 info
.minor_version
= mddev
->minor_version
;
5019 info
.patch_version
= MD_PATCHLEVEL_VERSION
;
5020 info
.ctime
= mddev
->ctime
;
5021 info
.level
= mddev
->level
;
5022 info
.size
= mddev
->dev_sectors
/ 2;
5023 if (info
.size
!= mddev
->dev_sectors
/ 2) /* overflow */
5026 info
.raid_disks
= mddev
->raid_disks
;
5027 info
.md_minor
= mddev
->md_minor
;
5028 info
.not_persistent
= !mddev
->persistent
;
5030 info
.utime
= mddev
->utime
;
5033 info
.state
= (1<<MD_SB_CLEAN
);
5034 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
5035 info
.state
= (1<<MD_SB_BITMAP_PRESENT
);
5036 info
.active_disks
= insync
;
5037 info
.working_disks
= working
;
5038 info
.failed_disks
= failed
;
5039 info
.spare_disks
= spare
;
5041 info
.layout
= mddev
->layout
;
5042 info
.chunk_size
= mddev
->chunk_sectors
<< 9;
5044 if (copy_to_user(arg
, &info
, sizeof(info
)))
5050 static int get_bitmap_file(mddev_t
* mddev
, void __user
* arg
)
5052 mdu_bitmap_file_t
*file
= NULL
; /* too big for stack allocation */
5053 char *ptr
, *buf
= NULL
;
5056 if (md_allow_write(mddev
))
5057 file
= kmalloc(sizeof(*file
), GFP_NOIO
);
5059 file
= kmalloc(sizeof(*file
), GFP_KERNEL
);
5064 /* bitmap disabled, zero the first byte and copy out */
5065 if (!mddev
->bitmap
|| !mddev
->bitmap
->file
) {
5066 file
->pathname
[0] = '\0';
5070 buf
= kmalloc(sizeof(file
->pathname
), GFP_KERNEL
);
5074 ptr
= d_path(&mddev
->bitmap
->file
->f_path
, buf
, sizeof(file
->pathname
));
5078 strcpy(file
->pathname
, ptr
);
5082 if (copy_to_user(arg
, file
, sizeof(*file
)))
5090 static int get_disk_info(mddev_t
* mddev
, void __user
* arg
)
5092 mdu_disk_info_t info
;
5095 if (copy_from_user(&info
, arg
, sizeof(info
)))
5098 rdev
= find_rdev_nr(mddev
, info
.number
);
5100 info
.major
= MAJOR(rdev
->bdev
->bd_dev
);
5101 info
.minor
= MINOR(rdev
->bdev
->bd_dev
);
5102 info
.raid_disk
= rdev
->raid_disk
;
5104 if (test_bit(Faulty
, &rdev
->flags
))
5105 info
.state
|= (1<<MD_DISK_FAULTY
);
5106 else if (test_bit(In_sync
, &rdev
->flags
)) {
5107 info
.state
|= (1<<MD_DISK_ACTIVE
);
5108 info
.state
|= (1<<MD_DISK_SYNC
);
5110 if (test_bit(WriteMostly
, &rdev
->flags
))
5111 info
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
5113 info
.major
= info
.minor
= 0;
5114 info
.raid_disk
= -1;
5115 info
.state
= (1<<MD_DISK_REMOVED
);
5118 if (copy_to_user(arg
, &info
, sizeof(info
)))
5124 static int add_new_disk(mddev_t
* mddev
, mdu_disk_info_t
*info
)
5126 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
5128 dev_t dev
= MKDEV(info
->major
,info
->minor
);
5130 if (info
->major
!= MAJOR(dev
) || info
->minor
!= MINOR(dev
))
5133 if (!mddev
->raid_disks
) {
5135 /* expecting a device which has a superblock */
5136 rdev
= md_import_device(dev
, mddev
->major_version
, mddev
->minor_version
);
5139 "md: md_import_device returned %ld\n",
5141 return PTR_ERR(rdev
);
5143 if (!list_empty(&mddev
->disks
)) {
5144 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
5145 mdk_rdev_t
, same_set
);
5146 err
= super_types
[mddev
->major_version
]
5147 .load_super(rdev
, rdev0
, mddev
->minor_version
);
5150 "md: %s has different UUID to %s\n",
5151 bdevname(rdev
->bdev
,b
),
5152 bdevname(rdev0
->bdev
,b2
));
5157 err
= bind_rdev_to_array(rdev
, mddev
);
5164 * add_new_disk can be used once the array is assembled
5165 * to add "hot spares". They must already have a superblock
5170 if (!mddev
->pers
->hot_add_disk
) {
5172 "%s: personality does not support diskops!\n",
5176 if (mddev
->persistent
)
5177 rdev
= md_import_device(dev
, mddev
->major_version
,
5178 mddev
->minor_version
);
5180 rdev
= md_import_device(dev
, -1, -1);
5183 "md: md_import_device returned %ld\n",
5185 return PTR_ERR(rdev
);
5187 /* set saved_raid_disk if appropriate */
5188 if (!mddev
->persistent
) {
5189 if (info
->state
& (1<<MD_DISK_SYNC
) &&
5190 info
->raid_disk
< mddev
->raid_disks
) {
5191 rdev
->raid_disk
= info
->raid_disk
;
5192 set_bit(In_sync
, &rdev
->flags
);
5194 rdev
->raid_disk
= -1;
5196 super_types
[mddev
->major_version
].
5197 validate_super(mddev
, rdev
);
5198 if (test_bit(In_sync
, &rdev
->flags
))
5199 rdev
->saved_raid_disk
= rdev
->raid_disk
;
5201 rdev
->saved_raid_disk
= -1;
5203 clear_bit(In_sync
, &rdev
->flags
); /* just to be sure */
5204 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
5205 set_bit(WriteMostly
, &rdev
->flags
);
5207 clear_bit(WriteMostly
, &rdev
->flags
);
5209 rdev
->raid_disk
= -1;
5210 err
= bind_rdev_to_array(rdev
, mddev
);
5211 if (!err
&& !mddev
->pers
->hot_remove_disk
) {
5212 /* If there is hot_add_disk but no hot_remove_disk
5213 * then added disks for geometry changes,
5214 * and should be added immediately.
5216 super_types
[mddev
->major_version
].
5217 validate_super(mddev
, rdev
);
5218 err
= mddev
->pers
->hot_add_disk(mddev
, rdev
);
5220 unbind_rdev_from_array(rdev
);
5225 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
5227 md_update_sb(mddev
, 1);
5228 if (mddev
->degraded
)
5229 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
5230 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5231 md_wakeup_thread(mddev
->thread
);
5235 /* otherwise, add_new_disk is only allowed
5236 * for major_version==0 superblocks
5238 if (mddev
->major_version
!= 0) {
5239 printk(KERN_WARNING
"%s: ADD_NEW_DISK not supported\n",
5244 if (!(info
->state
& (1<<MD_DISK_FAULTY
))) {
5246 rdev
= md_import_device(dev
, -1, 0);
5249 "md: error, md_import_device() returned %ld\n",
5251 return PTR_ERR(rdev
);
5253 rdev
->desc_nr
= info
->number
;
5254 if (info
->raid_disk
< mddev
->raid_disks
)
5255 rdev
->raid_disk
= info
->raid_disk
;
5257 rdev
->raid_disk
= -1;
5259 if (rdev
->raid_disk
< mddev
->raid_disks
)
5260 if (info
->state
& (1<<MD_DISK_SYNC
))
5261 set_bit(In_sync
, &rdev
->flags
);
5263 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
5264 set_bit(WriteMostly
, &rdev
->flags
);
5266 if (!mddev
->persistent
) {
5267 printk(KERN_INFO
"md: nonpersistent superblock ...\n");
5268 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
5270 rdev
->sb_start
= calc_dev_sboffset(rdev
);
5271 rdev
->sectors
= rdev
->sb_start
;
5273 err
= bind_rdev_to_array(rdev
, mddev
);
5283 static int hot_remove_disk(mddev_t
* mddev
, dev_t dev
)
5285 char b
[BDEVNAME_SIZE
];
5288 rdev
= find_rdev(mddev
, dev
);
5292 if (rdev
->raid_disk
>= 0)
5295 kick_rdev_from_array(rdev
);
5296 md_update_sb(mddev
, 1);
5297 md_new_event(mddev
);
5301 printk(KERN_WARNING
"md: cannot remove active disk %s from %s ...\n",
5302 bdevname(rdev
->bdev
,b
), mdname(mddev
));
5306 static int hot_add_disk(mddev_t
* mddev
, dev_t dev
)
5308 char b
[BDEVNAME_SIZE
];
5315 if (mddev
->major_version
!= 0) {
5316 printk(KERN_WARNING
"%s: HOT_ADD may only be used with"
5317 " version-0 superblocks.\n",
5321 if (!mddev
->pers
->hot_add_disk
) {
5323 "%s: personality does not support diskops!\n",
5328 rdev
= md_import_device(dev
, -1, 0);
5331 "md: error, md_import_device() returned %ld\n",
5336 if (mddev
->persistent
)
5337 rdev
->sb_start
= calc_dev_sboffset(rdev
);
5339 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
5341 rdev
->sectors
= rdev
->sb_start
;
5343 if (test_bit(Faulty
, &rdev
->flags
)) {
5345 "md: can not hot-add faulty %s disk to %s!\n",
5346 bdevname(rdev
->bdev
,b
), mdname(mddev
));
5350 clear_bit(In_sync
, &rdev
->flags
);
5352 rdev
->saved_raid_disk
= -1;
5353 err
= bind_rdev_to_array(rdev
, mddev
);
5358 * The rest should better be atomic, we can have disk failures
5359 * noticed in interrupt contexts ...
5362 rdev
->raid_disk
= -1;
5364 md_update_sb(mddev
, 1);
5367 * Kick recovery, maybe this spare has to be added to the
5368 * array immediately.
5370 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5371 md_wakeup_thread(mddev
->thread
);
5372 md_new_event(mddev
);
5380 static int set_bitmap_file(mddev_t
*mddev
, int fd
)
5385 if (!mddev
->pers
->quiesce
)
5387 if (mddev
->recovery
|| mddev
->sync_thread
)
5389 /* we should be able to change the bitmap.. */
5395 return -EEXIST
; /* cannot add when bitmap is present */
5396 mddev
->bitmap_info
.file
= fget(fd
);
5398 if (mddev
->bitmap_info
.file
== NULL
) {
5399 printk(KERN_ERR
"%s: error: failed to get bitmap file\n",
5404 err
= deny_bitmap_write_access(mddev
->bitmap_info
.file
);
5406 printk(KERN_ERR
"%s: error: bitmap file is already in use\n",
5408 fput(mddev
->bitmap_info
.file
);
5409 mddev
->bitmap_info
.file
= NULL
;
5412 mddev
->bitmap_info
.offset
= 0; /* file overrides offset */
5413 } else if (mddev
->bitmap
== NULL
)
5414 return -ENOENT
; /* cannot remove what isn't there */
5417 mddev
->pers
->quiesce(mddev
, 1);
5419 err
= bitmap_create(mddev
);
5421 err
= bitmap_load(mddev
);
5423 if (fd
< 0 || err
) {
5424 bitmap_destroy(mddev
);
5425 fd
= -1; /* make sure to put the file */
5427 mddev
->pers
->quiesce(mddev
, 0);
5430 if (mddev
->bitmap_info
.file
) {
5431 restore_bitmap_write_access(mddev
->bitmap_info
.file
);
5432 fput(mddev
->bitmap_info
.file
);
5434 mddev
->bitmap_info
.file
= NULL
;
5441 * set_array_info is used two different ways
5442 * The original usage is when creating a new array.
5443 * In this usage, raid_disks is > 0 and it together with
5444 * level, size, not_persistent,layout,chunksize determine the
5445 * shape of the array.
5446 * This will always create an array with a type-0.90.0 superblock.
5447 * The newer usage is when assembling an array.
5448 * In this case raid_disks will be 0, and the major_version field is
5449 * use to determine which style super-blocks are to be found on the devices.
5450 * The minor and patch _version numbers are also kept incase the
5451 * super_block handler wishes to interpret them.
5453 static int set_array_info(mddev_t
* mddev
, mdu_array_info_t
*info
)
5456 if (info
->raid_disks
== 0) {
5457 /* just setting version number for superblock loading */
5458 if (info
->major_version
< 0 ||
5459 info
->major_version
>= ARRAY_SIZE(super_types
) ||
5460 super_types
[info
->major_version
].name
== NULL
) {
5461 /* maybe try to auto-load a module? */
5463 "md: superblock version %d not known\n",
5464 info
->major_version
);
5467 mddev
->major_version
= info
->major_version
;
5468 mddev
->minor_version
= info
->minor_version
;
5469 mddev
->patch_version
= info
->patch_version
;
5470 mddev
->persistent
= !info
->not_persistent
;
5471 /* ensure mddev_put doesn't delete this now that there
5472 * is some minimal configuration.
5474 mddev
->ctime
= get_seconds();
5477 mddev
->major_version
= MD_MAJOR_VERSION
;
5478 mddev
->minor_version
= MD_MINOR_VERSION
;
5479 mddev
->patch_version
= MD_PATCHLEVEL_VERSION
;
5480 mddev
->ctime
= get_seconds();
5482 mddev
->level
= info
->level
;
5483 mddev
->clevel
[0] = 0;
5484 mddev
->dev_sectors
= 2 * (sector_t
)info
->size
;
5485 mddev
->raid_disks
= info
->raid_disks
;
5486 /* don't set md_minor, it is determined by which /dev/md* was
5489 if (info
->state
& (1<<MD_SB_CLEAN
))
5490 mddev
->recovery_cp
= MaxSector
;
5492 mddev
->recovery_cp
= 0;
5493 mddev
->persistent
= ! info
->not_persistent
;
5494 mddev
->external
= 0;
5496 mddev
->layout
= info
->layout
;
5497 mddev
->chunk_sectors
= info
->chunk_size
>> 9;
5499 mddev
->max_disks
= MD_SB_DISKS
;
5501 if (mddev
->persistent
)
5503 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
5505 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
5506 mddev
->bitmap_info
.offset
= 0;
5508 mddev
->reshape_position
= MaxSector
;
5511 * Generate a 128 bit UUID
5513 get_random_bytes(mddev
->uuid
, 16);
5515 mddev
->new_level
= mddev
->level
;
5516 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
5517 mddev
->new_layout
= mddev
->layout
;
5518 mddev
->delta_disks
= 0;
5523 void md_set_array_sectors(mddev_t
*mddev
, sector_t array_sectors
)
5525 WARN(!mddev_is_locked(mddev
), "%s: unlocked mddev!\n", __func__
);
5527 if (mddev
->external_size
)
5530 mddev
->array_sectors
= array_sectors
;
5532 EXPORT_SYMBOL(md_set_array_sectors
);
5534 static int update_size(mddev_t
*mddev
, sector_t num_sectors
)
5538 int fit
= (num_sectors
== 0);
5540 if (mddev
->pers
->resize
== NULL
)
5542 /* The "num_sectors" is the number of sectors of each device that
5543 * is used. This can only make sense for arrays with redundancy.
5544 * linear and raid0 always use whatever space is available. We can only
5545 * consider changing this number if no resync or reconstruction is
5546 * happening, and if the new size is acceptable. It must fit before the
5547 * sb_start or, if that is <data_offset, it must fit before the size
5548 * of each device. If num_sectors is zero, we find the largest size
5551 if (mddev
->sync_thread
)
5554 /* Sorry, cannot grow a bitmap yet, just remove it,
5558 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
5559 sector_t avail
= rdev
->sectors
;
5561 if (fit
&& (num_sectors
== 0 || num_sectors
> avail
))
5562 num_sectors
= avail
;
5563 if (avail
< num_sectors
)
5566 rv
= mddev
->pers
->resize(mddev
, num_sectors
);
5568 revalidate_disk(mddev
->gendisk
);
5572 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
)
5575 /* change the number of raid disks */
5576 if (mddev
->pers
->check_reshape
== NULL
)
5578 if (raid_disks
<= 0 ||
5579 (mddev
->max_disks
&& raid_disks
>= mddev
->max_disks
))
5581 if (mddev
->sync_thread
|| mddev
->reshape_position
!= MaxSector
)
5583 mddev
->delta_disks
= raid_disks
- mddev
->raid_disks
;
5585 rv
= mddev
->pers
->check_reshape(mddev
);
5587 mddev
->delta_disks
= 0;
5593 * update_array_info is used to change the configuration of an
5595 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5596 * fields in the info are checked against the array.
5597 * Any differences that cannot be handled will cause an error.
5598 * Normally, only one change can be managed at a time.
5600 static int update_array_info(mddev_t
*mddev
, mdu_array_info_t
*info
)
5606 /* calculate expected state,ignoring low bits */
5607 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
5608 state
|= (1 << MD_SB_BITMAP_PRESENT
);
5610 if (mddev
->major_version
!= info
->major_version
||
5611 mddev
->minor_version
!= info
->minor_version
||
5612 /* mddev->patch_version != info->patch_version || */
5613 mddev
->ctime
!= info
->ctime
||
5614 mddev
->level
!= info
->level
||
5615 /* mddev->layout != info->layout || */
5616 !mddev
->persistent
!= info
->not_persistent
||
5617 mddev
->chunk_sectors
!= info
->chunk_size
>> 9 ||
5618 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5619 ((state
^info
->state
) & 0xfffffe00)
5622 /* Check there is only one change */
5623 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
5625 if (mddev
->raid_disks
!= info
->raid_disks
)
5627 if (mddev
->layout
!= info
->layout
)
5629 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
))
5636 if (mddev
->layout
!= info
->layout
) {
5638 * we don't need to do anything at the md level, the
5639 * personality will take care of it all.
5641 if (mddev
->pers
->check_reshape
== NULL
)
5644 mddev
->new_layout
= info
->layout
;
5645 rv
= mddev
->pers
->check_reshape(mddev
);
5647 mddev
->new_layout
= mddev
->layout
;
5651 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
5652 rv
= update_size(mddev
, (sector_t
)info
->size
* 2);
5654 if (mddev
->raid_disks
!= info
->raid_disks
)
5655 rv
= update_raid_disks(mddev
, info
->raid_disks
);
5657 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) {
5658 if (mddev
->pers
->quiesce
== NULL
)
5660 if (mddev
->recovery
|| mddev
->sync_thread
)
5662 if (info
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
5663 /* add the bitmap */
5666 if (mddev
->bitmap_info
.default_offset
== 0)
5668 mddev
->bitmap_info
.offset
=
5669 mddev
->bitmap_info
.default_offset
;
5670 mddev
->pers
->quiesce(mddev
, 1);
5671 rv
= bitmap_create(mddev
);
5673 rv
= bitmap_load(mddev
);
5675 bitmap_destroy(mddev
);
5676 mddev
->pers
->quiesce(mddev
, 0);
5678 /* remove the bitmap */
5681 if (mddev
->bitmap
->file
)
5683 mddev
->pers
->quiesce(mddev
, 1);
5684 bitmap_destroy(mddev
);
5685 mddev
->pers
->quiesce(mddev
, 0);
5686 mddev
->bitmap_info
.offset
= 0;
5689 md_update_sb(mddev
, 1);
5693 static int set_disk_faulty(mddev_t
*mddev
, dev_t dev
)
5697 if (mddev
->pers
== NULL
)
5700 rdev
= find_rdev(mddev
, dev
);
5704 md_error(mddev
, rdev
);
5709 * We have a problem here : there is no easy way to give a CHS
5710 * virtual geometry. We currently pretend that we have a 2 heads
5711 * 4 sectors (with a BIG number of cylinders...). This drives
5712 * dosfs just mad... ;-)
5714 static int md_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
5716 mddev_t
*mddev
= bdev
->bd_disk
->private_data
;
5720 geo
->cylinders
= mddev
->array_sectors
/ 8;
5724 static int md_ioctl(struct block_device
*bdev
, fmode_t mode
,
5725 unsigned int cmd
, unsigned long arg
)
5728 void __user
*argp
= (void __user
*)arg
;
5729 mddev_t
*mddev
= NULL
;
5732 if (!capable(CAP_SYS_ADMIN
))
5736 * Commands dealing with the RAID driver but not any
5742 err
= get_version(argp
);
5745 case PRINT_RAID_DEBUG
:
5753 autostart_arrays(arg
);
5760 * Commands creating/starting a new array:
5763 mddev
= bdev
->bd_disk
->private_data
;
5770 err
= mddev_lock(mddev
);
5773 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5780 case SET_ARRAY_INFO
:
5782 mdu_array_info_t info
;
5784 memset(&info
, 0, sizeof(info
));
5785 else if (copy_from_user(&info
, argp
, sizeof(info
))) {
5790 err
= update_array_info(mddev
, &info
);
5792 printk(KERN_WARNING
"md: couldn't update"
5793 " array info. %d\n", err
);
5798 if (!list_empty(&mddev
->disks
)) {
5800 "md: array %s already has disks!\n",
5805 if (mddev
->raid_disks
) {
5807 "md: array %s already initialised!\n",
5812 err
= set_array_info(mddev
, &info
);
5814 printk(KERN_WARNING
"md: couldn't set"
5815 " array info. %d\n", err
);
5825 * Commands querying/configuring an existing array:
5827 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
5828 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
5829 if ((!mddev
->raid_disks
&& !mddev
->external
)
5830 && cmd
!= ADD_NEW_DISK
&& cmd
!= STOP_ARRAY
5831 && cmd
!= RUN_ARRAY
&& cmd
!= SET_BITMAP_FILE
5832 && cmd
!= GET_BITMAP_FILE
) {
5838 * Commands even a read-only array can execute:
5842 case GET_ARRAY_INFO
:
5843 err
= get_array_info(mddev
, argp
);
5846 case GET_BITMAP_FILE
:
5847 err
= get_bitmap_file(mddev
, argp
);
5851 err
= get_disk_info(mddev
, argp
);
5854 case RESTART_ARRAY_RW
:
5855 err
= restart_array(mddev
);
5859 err
= do_md_stop(mddev
, 0, 1);
5863 err
= md_set_readonly(mddev
, 1);
5867 if (get_user(ro
, (int __user
*)(arg
))) {
5873 /* if the bdev is going readonly the value of mddev->ro
5874 * does not matter, no writes are coming
5879 /* are we are already prepared for writes? */
5883 /* transitioning to readauto need only happen for
5884 * arrays that call md_write_start
5887 err
= restart_array(mddev
);
5890 set_disk_ro(mddev
->gendisk
, 0);
5897 * The remaining ioctls are changing the state of the
5898 * superblock, so we do not allow them on read-only arrays.
5899 * However non-MD ioctls (e.g. get-size) will still come through
5900 * here and hit the 'default' below, so only disallow
5901 * 'md' ioctls, and switch to rw mode if started auto-readonly.
5903 if (_IOC_TYPE(cmd
) == MD_MAJOR
&& mddev
->ro
&& mddev
->pers
) {
5904 if (mddev
->ro
== 2) {
5906 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5907 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5908 md_wakeup_thread(mddev
->thread
);
5919 mdu_disk_info_t info
;
5920 if (copy_from_user(&info
, argp
, sizeof(info
)))
5923 err
= add_new_disk(mddev
, &info
);
5927 case HOT_REMOVE_DISK
:
5928 err
= hot_remove_disk(mddev
, new_decode_dev(arg
));
5932 err
= hot_add_disk(mddev
, new_decode_dev(arg
));
5935 case SET_DISK_FAULTY
:
5936 err
= set_disk_faulty(mddev
, new_decode_dev(arg
));
5940 err
= do_md_run(mddev
);
5943 case SET_BITMAP_FILE
:
5944 err
= set_bitmap_file(mddev
, (int)arg
);
5954 if (mddev
->hold_active
== UNTIL_IOCTL
&&
5956 mddev
->hold_active
= 0;
5957 mddev_unlock(mddev
);
5966 #ifdef CONFIG_COMPAT
5967 static int md_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
5968 unsigned int cmd
, unsigned long arg
)
5971 case HOT_REMOVE_DISK
:
5973 case SET_DISK_FAULTY
:
5974 case SET_BITMAP_FILE
:
5975 /* These take in integer arg, do not convert */
5978 arg
= (unsigned long)compat_ptr(arg
);
5982 return md_ioctl(bdev
, mode
, cmd
, arg
);
5984 #endif /* CONFIG_COMPAT */
5986 static int md_open(struct block_device
*bdev
, fmode_t mode
)
5989 * Succeed if we can lock the mddev, which confirms that
5990 * it isn't being stopped right now.
5992 mddev_t
*mddev
= mddev_find(bdev
->bd_dev
);
5995 if (mddev
->gendisk
!= bdev
->bd_disk
) {
5996 /* we are racing with mddev_put which is discarding this
6000 /* Wait until bdev->bd_disk is definitely gone */
6001 flush_workqueue(md_misc_wq
);
6002 /* Then retry the open from the top */
6003 return -ERESTARTSYS
;
6005 BUG_ON(mddev
!= bdev
->bd_disk
->private_data
);
6007 if ((err
= mutex_lock_interruptible(&mddev
->open_mutex
)))
6011 atomic_inc(&mddev
->openers
);
6012 mutex_unlock(&mddev
->open_mutex
);
6014 check_disk_size_change(mddev
->gendisk
, bdev
);
6019 static int md_release(struct gendisk
*disk
, fmode_t mode
)
6021 mddev_t
*mddev
= disk
->private_data
;
6024 atomic_dec(&mddev
->openers
);
6029 static const struct block_device_operations md_fops
=
6031 .owner
= THIS_MODULE
,
6033 .release
= md_release
,
6035 #ifdef CONFIG_COMPAT
6036 .compat_ioctl
= md_compat_ioctl
,
6038 .getgeo
= md_getgeo
,
6041 static int md_thread(void * arg
)
6043 mdk_thread_t
*thread
= arg
;
6046 * md_thread is a 'system-thread', it's priority should be very
6047 * high. We avoid resource deadlocks individually in each
6048 * raid personality. (RAID5 does preallocation) We also use RR and
6049 * the very same RT priority as kswapd, thus we will never get
6050 * into a priority inversion deadlock.
6052 * we definitely have to have equal or higher priority than
6053 * bdflush, otherwise bdflush will deadlock if there are too
6054 * many dirty RAID5 blocks.
6057 allow_signal(SIGKILL
);
6058 while (!kthread_should_stop()) {
6060 /* We need to wait INTERRUPTIBLE so that
6061 * we don't add to the load-average.
6062 * That means we need to be sure no signals are
6065 if (signal_pending(current
))
6066 flush_signals(current
);
6068 wait_event_interruptible_timeout
6070 test_bit(THREAD_WAKEUP
, &thread
->flags
)
6071 || kthread_should_stop(),
6074 clear_bit(THREAD_WAKEUP
, &thread
->flags
);
6075 if (!kthread_should_stop())
6076 thread
->run(thread
->mddev
);
6082 void md_wakeup_thread(mdk_thread_t
*thread
)
6085 dprintk("md: waking up MD thread %s.\n", thread
->tsk
->comm
);
6086 set_bit(THREAD_WAKEUP
, &thread
->flags
);
6087 wake_up(&thread
->wqueue
);
6091 mdk_thread_t
*md_register_thread(void (*run
) (mddev_t
*), mddev_t
*mddev
,
6094 mdk_thread_t
*thread
;
6096 thread
= kzalloc(sizeof(mdk_thread_t
), GFP_KERNEL
);
6100 init_waitqueue_head(&thread
->wqueue
);
6103 thread
->mddev
= mddev
;
6104 thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
6105 thread
->tsk
= kthread_run(md_thread
, thread
,
6107 mdname(thread
->mddev
),
6108 name
?: mddev
->pers
->name
);
6109 if (IS_ERR(thread
->tsk
)) {
6116 void md_unregister_thread(mdk_thread_t
*thread
)
6120 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread
->tsk
));
6122 kthread_stop(thread
->tsk
);
6126 void md_error(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
6133 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
6136 if (mddev
->external
)
6137 set_bit(Blocked
, &rdev
->flags
);
6139 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
6141 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
6142 __builtin_return_address(0),__builtin_return_address(1),
6143 __builtin_return_address(2),__builtin_return_address(3));
6147 if (!mddev
->pers
->error_handler
)
6149 mddev
->pers
->error_handler(mddev
,rdev
);
6150 if (mddev
->degraded
)
6151 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
6152 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
6153 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6154 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6155 md_wakeup_thread(mddev
->thread
);
6156 if (mddev
->event_work
.func
)
6157 queue_work(md_misc_wq
, &mddev
->event_work
);
6158 md_new_event_inintr(mddev
);
6161 /* seq_file implementation /proc/mdstat */
6163 static void status_unused(struct seq_file
*seq
)
6168 seq_printf(seq
, "unused devices: ");
6170 list_for_each_entry(rdev
, &pending_raid_disks
, same_set
) {
6171 char b
[BDEVNAME_SIZE
];
6173 seq_printf(seq
, "%s ",
6174 bdevname(rdev
->bdev
,b
));
6177 seq_printf(seq
, "<none>");
6179 seq_printf(seq
, "\n");
6183 static void status_resync(struct seq_file
*seq
, mddev_t
* mddev
)
6185 sector_t max_sectors
, resync
, res
;
6186 unsigned long dt
, db
;
6189 unsigned int per_milli
;
6191 resync
= mddev
->curr_resync
- atomic_read(&mddev
->recovery_active
);
6193 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
6194 max_sectors
= mddev
->resync_max_sectors
;
6196 max_sectors
= mddev
->dev_sectors
;
6199 * Should not happen.
6205 /* Pick 'scale' such that (resync>>scale)*1000 will fit
6206 * in a sector_t, and (max_sectors>>scale) will fit in a
6207 * u32, as those are the requirements for sector_div.
6208 * Thus 'scale' must be at least 10
6211 if (sizeof(sector_t
) > sizeof(unsigned long)) {
6212 while ( max_sectors
/2 > (1ULL<<(scale
+32)))
6215 res
= (resync
>>scale
)*1000;
6216 sector_div(res
, (u32
)((max_sectors
>>scale
)+1));
6220 int i
, x
= per_milli
/50, y
= 20-x
;
6221 seq_printf(seq
, "[");
6222 for (i
= 0; i
< x
; i
++)
6223 seq_printf(seq
, "=");
6224 seq_printf(seq
, ">");
6225 for (i
= 0; i
< y
; i
++)
6226 seq_printf(seq
, ".");
6227 seq_printf(seq
, "] ");
6229 seq_printf(seq
, " %s =%3u.%u%% (%llu/%llu)",
6230 (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)?
6232 (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)?
6234 (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ?
6235 "resync" : "recovery"))),
6236 per_milli
/10, per_milli
% 10,
6237 (unsigned long long) resync
/2,
6238 (unsigned long long) max_sectors
/2);
6241 * dt: time from mark until now
6242 * db: blocks written from mark until now
6243 * rt: remaining time
6245 * rt is a sector_t, so could be 32bit or 64bit.
6246 * So we divide before multiply in case it is 32bit and close
6248 * We scale the divisor (db) by 32 to avoid loosing precision
6249 * near the end of resync when the number of remaining sectors
6251 * We then divide rt by 32 after multiplying by db to compensate.
6252 * The '+1' avoids division by zero if db is very small.
6254 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
6256 db
= (mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
))
6257 - mddev
->resync_mark_cnt
;
6259 rt
= max_sectors
- resync
; /* number of remaining sectors */
6260 sector_div(rt
, db
/32+1);
6264 seq_printf(seq
, " finish=%lu.%lumin", (unsigned long)rt
/ 60,
6265 ((unsigned long)rt
% 60)/6);
6267 seq_printf(seq
, " speed=%ldK/sec", db
/2/dt
);
6270 static void *md_seq_start(struct seq_file
*seq
, loff_t
*pos
)
6272 struct list_head
*tmp
;
6282 spin_lock(&all_mddevs_lock
);
6283 list_for_each(tmp
,&all_mddevs
)
6285 mddev
= list_entry(tmp
, mddev_t
, all_mddevs
);
6287 spin_unlock(&all_mddevs_lock
);
6290 spin_unlock(&all_mddevs_lock
);
6292 return (void*)2;/* tail */
6296 static void *md_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
6298 struct list_head
*tmp
;
6299 mddev_t
*next_mddev
, *mddev
= v
;
6305 spin_lock(&all_mddevs_lock
);
6307 tmp
= all_mddevs
.next
;
6309 tmp
= mddev
->all_mddevs
.next
;
6310 if (tmp
!= &all_mddevs
)
6311 next_mddev
= mddev_get(list_entry(tmp
,mddev_t
,all_mddevs
));
6313 next_mddev
= (void*)2;
6316 spin_unlock(&all_mddevs_lock
);
6324 static void md_seq_stop(struct seq_file
*seq
, void *v
)
6328 if (mddev
&& v
!= (void*)1 && v
!= (void*)2)
6332 struct mdstat_info
{
6336 static int md_seq_show(struct seq_file
*seq
, void *v
)
6341 struct mdstat_info
*mi
= seq
->private;
6342 struct bitmap
*bitmap
;
6344 if (v
== (void*)1) {
6345 struct mdk_personality
*pers
;
6346 seq_printf(seq
, "Personalities : ");
6347 spin_lock(&pers_lock
);
6348 list_for_each_entry(pers
, &pers_list
, list
)
6349 seq_printf(seq
, "[%s] ", pers
->name
);
6351 spin_unlock(&pers_lock
);
6352 seq_printf(seq
, "\n");
6353 mi
->event
= atomic_read(&md_event_count
);
6356 if (v
== (void*)2) {
6361 if (mddev_lock(mddev
) < 0)
6364 if (mddev
->pers
|| mddev
->raid_disks
|| !list_empty(&mddev
->disks
)) {
6365 seq_printf(seq
, "%s : %sactive", mdname(mddev
),
6366 mddev
->pers
? "" : "in");
6369 seq_printf(seq
, " (read-only)");
6371 seq_printf(seq
, " (auto-read-only)");
6372 seq_printf(seq
, " %s", mddev
->pers
->name
);
6376 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
6377 char b
[BDEVNAME_SIZE
];
6378 seq_printf(seq
, " %s[%d]",
6379 bdevname(rdev
->bdev
,b
), rdev
->desc_nr
);
6380 if (test_bit(WriteMostly
, &rdev
->flags
))
6381 seq_printf(seq
, "(W)");
6382 if (test_bit(Faulty
, &rdev
->flags
)) {
6383 seq_printf(seq
, "(F)");
6385 } else if (rdev
->raid_disk
< 0)
6386 seq_printf(seq
, "(S)"); /* spare */
6387 sectors
+= rdev
->sectors
;
6390 if (!list_empty(&mddev
->disks
)) {
6392 seq_printf(seq
, "\n %llu blocks",
6393 (unsigned long long)
6394 mddev
->array_sectors
/ 2);
6396 seq_printf(seq
, "\n %llu blocks",
6397 (unsigned long long)sectors
/ 2);
6399 if (mddev
->persistent
) {
6400 if (mddev
->major_version
!= 0 ||
6401 mddev
->minor_version
!= 90) {
6402 seq_printf(seq
," super %d.%d",
6403 mddev
->major_version
,
6404 mddev
->minor_version
);
6406 } else if (mddev
->external
)
6407 seq_printf(seq
, " super external:%s",
6408 mddev
->metadata_type
);
6410 seq_printf(seq
, " super non-persistent");
6413 mddev
->pers
->status(seq
, mddev
);
6414 seq_printf(seq
, "\n ");
6415 if (mddev
->pers
->sync_request
) {
6416 if (mddev
->curr_resync
> 2) {
6417 status_resync(seq
, mddev
);
6418 seq_printf(seq
, "\n ");
6419 } else if (mddev
->curr_resync
== 1 || mddev
->curr_resync
== 2)
6420 seq_printf(seq
, "\tresync=DELAYED\n ");
6421 else if (mddev
->recovery_cp
< MaxSector
)
6422 seq_printf(seq
, "\tresync=PENDING\n ");
6425 seq_printf(seq
, "\n ");
6427 if ((bitmap
= mddev
->bitmap
)) {
6428 unsigned long chunk_kb
;
6429 unsigned long flags
;
6430 spin_lock_irqsave(&bitmap
->lock
, flags
);
6431 chunk_kb
= mddev
->bitmap_info
.chunksize
>> 10;
6432 seq_printf(seq
, "bitmap: %lu/%lu pages [%luKB], "
6434 bitmap
->pages
- bitmap
->missing_pages
,
6436 (bitmap
->pages
- bitmap
->missing_pages
)
6437 << (PAGE_SHIFT
- 10),
6438 chunk_kb
? chunk_kb
: mddev
->bitmap_info
.chunksize
,
6439 chunk_kb
? "KB" : "B");
6441 seq_printf(seq
, ", file: ");
6442 seq_path(seq
, &bitmap
->file
->f_path
, " \t\n");
6445 seq_printf(seq
, "\n");
6446 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
6449 seq_printf(seq
, "\n");
6451 mddev_unlock(mddev
);
6456 static const struct seq_operations md_seq_ops
= {
6457 .start
= md_seq_start
,
6458 .next
= md_seq_next
,
6459 .stop
= md_seq_stop
,
6460 .show
= md_seq_show
,
6463 static int md_seq_open(struct inode
*inode
, struct file
*file
)
6466 struct mdstat_info
*mi
= kmalloc(sizeof(*mi
), GFP_KERNEL
);
6470 error
= seq_open(file
, &md_seq_ops
);
6474 struct seq_file
*p
= file
->private_data
;
6476 mi
->event
= atomic_read(&md_event_count
);
6481 static unsigned int mdstat_poll(struct file
*filp
, poll_table
*wait
)
6483 struct seq_file
*m
= filp
->private_data
;
6484 struct mdstat_info
*mi
= m
->private;
6487 poll_wait(filp
, &md_event_waiters
, wait
);
6489 /* always allow read */
6490 mask
= POLLIN
| POLLRDNORM
;
6492 if (mi
->event
!= atomic_read(&md_event_count
))
6493 mask
|= POLLERR
| POLLPRI
;
6497 static const struct file_operations md_seq_fops
= {
6498 .owner
= THIS_MODULE
,
6499 .open
= md_seq_open
,
6501 .llseek
= seq_lseek
,
6502 .release
= seq_release_private
,
6503 .poll
= mdstat_poll
,
6506 int register_md_personality(struct mdk_personality
*p
)
6508 spin_lock(&pers_lock
);
6509 list_add_tail(&p
->list
, &pers_list
);
6510 printk(KERN_INFO
"md: %s personality registered for level %d\n", p
->name
, p
->level
);
6511 spin_unlock(&pers_lock
);
6515 int unregister_md_personality(struct mdk_personality
*p
)
6517 printk(KERN_INFO
"md: %s personality unregistered\n", p
->name
);
6518 spin_lock(&pers_lock
);
6519 list_del_init(&p
->list
);
6520 spin_unlock(&pers_lock
);
6524 static int is_mddev_idle(mddev_t
*mddev
, int init
)
6532 rdev_for_each_rcu(rdev
, mddev
) {
6533 struct gendisk
*disk
= rdev
->bdev
->bd_contains
->bd_disk
;
6534 curr_events
= (int)part_stat_read(&disk
->part0
, sectors
[0]) +
6535 (int)part_stat_read(&disk
->part0
, sectors
[1]) -
6536 atomic_read(&disk
->sync_io
);
6537 /* sync IO will cause sync_io to increase before the disk_stats
6538 * as sync_io is counted when a request starts, and
6539 * disk_stats is counted when it completes.
6540 * So resync activity will cause curr_events to be smaller than
6541 * when there was no such activity.
6542 * non-sync IO will cause disk_stat to increase without
6543 * increasing sync_io so curr_events will (eventually)
6544 * be larger than it was before. Once it becomes
6545 * substantially larger, the test below will cause
6546 * the array to appear non-idle, and resync will slow
6548 * If there is a lot of outstanding resync activity when
6549 * we set last_event to curr_events, then all that activity
6550 * completing might cause the array to appear non-idle
6551 * and resync will be slowed down even though there might
6552 * not have been non-resync activity. This will only
6553 * happen once though. 'last_events' will soon reflect
6554 * the state where there is little or no outstanding
6555 * resync requests, and further resync activity will
6556 * always make curr_events less than last_events.
6559 if (init
|| curr_events
- rdev
->last_events
> 64) {
6560 rdev
->last_events
= curr_events
;
6568 void md_done_sync(mddev_t
*mddev
, int blocks
, int ok
)
6570 /* another "blocks" (512byte) blocks have been synced */
6571 atomic_sub(blocks
, &mddev
->recovery_active
);
6572 wake_up(&mddev
->recovery_wait
);
6574 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6575 md_wakeup_thread(mddev
->thread
);
6576 // stop recovery, signal do_sync ....
6581 /* md_write_start(mddev, bi)
6582 * If we need to update some array metadata (e.g. 'active' flag
6583 * in superblock) before writing, schedule a superblock update
6584 * and wait for it to complete.
6586 void md_write_start(mddev_t
*mddev
, struct bio
*bi
)
6589 if (bio_data_dir(bi
) != WRITE
)
6592 BUG_ON(mddev
->ro
== 1);
6593 if (mddev
->ro
== 2) {
6594 /* need to switch to read/write */
6596 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6597 md_wakeup_thread(mddev
->thread
);
6598 md_wakeup_thread(mddev
->sync_thread
);
6601 atomic_inc(&mddev
->writes_pending
);
6602 if (mddev
->safemode
== 1)
6603 mddev
->safemode
= 0;
6604 if (mddev
->in_sync
) {
6605 spin_lock_irq(&mddev
->write_lock
);
6606 if (mddev
->in_sync
) {
6608 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6609 set_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
6610 md_wakeup_thread(mddev
->thread
);
6613 spin_unlock_irq(&mddev
->write_lock
);
6616 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6617 wait_event(mddev
->sb_wait
,
6618 !test_bit(MD_CHANGE_PENDING
, &mddev
->flags
));
6621 void md_write_end(mddev_t
*mddev
)
6623 if (atomic_dec_and_test(&mddev
->writes_pending
)) {
6624 if (mddev
->safemode
== 2)
6625 md_wakeup_thread(mddev
->thread
);
6626 else if (mddev
->safemode_delay
)
6627 mod_timer(&mddev
->safemode_timer
, jiffies
+ mddev
->safemode_delay
);
6631 /* md_allow_write(mddev)
6632 * Calling this ensures that the array is marked 'active' so that writes
6633 * may proceed without blocking. It is important to call this before
6634 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6635 * Must be called with mddev_lock held.
6637 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6638 * is dropped, so return -EAGAIN after notifying userspace.
6640 int md_allow_write(mddev_t
*mddev
)
6646 if (!mddev
->pers
->sync_request
)
6649 spin_lock_irq(&mddev
->write_lock
);
6650 if (mddev
->in_sync
) {
6652 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6653 set_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
6654 if (mddev
->safemode_delay
&&
6655 mddev
->safemode
== 0)
6656 mddev
->safemode
= 1;
6657 spin_unlock_irq(&mddev
->write_lock
);
6658 md_update_sb(mddev
, 0);
6659 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6661 spin_unlock_irq(&mddev
->write_lock
);
6663 if (test_bit(MD_CHANGE_PENDING
, &mddev
->flags
))
6668 EXPORT_SYMBOL_GPL(md_allow_write
);
6670 void md_unplug(mddev_t
*mddev
)
6673 blk_unplug(mddev
->queue
);
6675 mddev
->plug
->unplug_fn(mddev
->plug
);
6678 #define SYNC_MARKS 10
6679 #define SYNC_MARK_STEP (3*HZ)
6680 void md_do_sync(mddev_t
*mddev
)
6683 unsigned int currspeed
= 0,
6685 sector_t max_sectors
,j
, io_sectors
;
6686 unsigned long mark
[SYNC_MARKS
];
6687 sector_t mark_cnt
[SYNC_MARKS
];
6689 struct list_head
*tmp
;
6690 sector_t last_check
;
6695 /* just incase thread restarts... */
6696 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
6698 if (mddev
->ro
) /* never try to sync a read-only array */
6701 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
6702 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
6703 desc
= "data-check";
6704 else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
6705 desc
= "requested-resync";
6708 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
6713 /* we overload curr_resync somewhat here.
6714 * 0 == not engaged in resync at all
6715 * 2 == checking that there is no conflict with another sync
6716 * 1 == like 2, but have yielded to allow conflicting resync to
6718 * other == active in resync - this many blocks
6720 * Before starting a resync we must have set curr_resync to
6721 * 2, and then checked that every "conflicting" array has curr_resync
6722 * less than ours. When we find one that is the same or higher
6723 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6724 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6725 * This will mean we have to start checking from the beginning again.
6730 mddev
->curr_resync
= 2;
6733 if (kthread_should_stop())
6734 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6736 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
6738 for_each_mddev(mddev2
, tmp
) {
6739 if (mddev2
== mddev
)
6741 if (!mddev
->parallel_resync
6742 && mddev2
->curr_resync
6743 && match_mddev_units(mddev
, mddev2
)) {
6745 if (mddev
< mddev2
&& mddev
->curr_resync
== 2) {
6746 /* arbitrarily yield */
6747 mddev
->curr_resync
= 1;
6748 wake_up(&resync_wait
);
6750 if (mddev
> mddev2
&& mddev
->curr_resync
== 1)
6751 /* no need to wait here, we can wait the next
6752 * time 'round when curr_resync == 2
6755 /* We need to wait 'interruptible' so as not to
6756 * contribute to the load average, and not to
6757 * be caught by 'softlockup'
6759 prepare_to_wait(&resync_wait
, &wq
, TASK_INTERRUPTIBLE
);
6760 if (!kthread_should_stop() &&
6761 mddev2
->curr_resync
>= mddev
->curr_resync
) {
6762 printk(KERN_INFO
"md: delaying %s of %s"
6763 " until %s has finished (they"
6764 " share one or more physical units)\n",
6765 desc
, mdname(mddev
), mdname(mddev2
));
6767 if (signal_pending(current
))
6768 flush_signals(current
);
6770 finish_wait(&resync_wait
, &wq
);
6773 finish_wait(&resync_wait
, &wq
);
6776 } while (mddev
->curr_resync
< 2);
6779 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
6780 /* resync follows the size requested by the personality,
6781 * which defaults to physical size, but can be virtual size
6783 max_sectors
= mddev
->resync_max_sectors
;
6784 mddev
->resync_mismatches
= 0;
6785 /* we don't use the checkpoint if there's a bitmap */
6786 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
6787 j
= mddev
->resync_min
;
6788 else if (!mddev
->bitmap
)
6789 j
= mddev
->recovery_cp
;
6791 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
6792 max_sectors
= mddev
->dev_sectors
;
6794 /* recovery follows the physical size of devices */
6795 max_sectors
= mddev
->dev_sectors
;
6798 list_for_each_entry_rcu(rdev
, &mddev
->disks
, same_set
)
6799 if (rdev
->raid_disk
>= 0 &&
6800 !test_bit(Faulty
, &rdev
->flags
) &&
6801 !test_bit(In_sync
, &rdev
->flags
) &&
6802 rdev
->recovery_offset
< j
)
6803 j
= rdev
->recovery_offset
;
6807 printk(KERN_INFO
"md: %s of RAID array %s\n", desc
, mdname(mddev
));
6808 printk(KERN_INFO
"md: minimum _guaranteed_ speed:"
6809 " %d KB/sec/disk.\n", speed_min(mddev
));
6810 printk(KERN_INFO
"md: using maximum available idle IO bandwidth "
6811 "(but not more than %d KB/sec) for %s.\n",
6812 speed_max(mddev
), desc
);
6814 is_mddev_idle(mddev
, 1); /* this initializes IO event counters */
6817 for (m
= 0; m
< SYNC_MARKS
; m
++) {
6819 mark_cnt
[m
] = io_sectors
;
6822 mddev
->resync_mark
= mark
[last_mark
];
6823 mddev
->resync_mark_cnt
= mark_cnt
[last_mark
];
6826 * Tune reconstruction:
6828 window
= 32*(PAGE_SIZE
/512);
6829 printk(KERN_INFO
"md: using %dk window, over a total of %llu blocks.\n",
6830 window
/2,(unsigned long long) max_sectors
/2);
6832 atomic_set(&mddev
->recovery_active
, 0);
6837 "md: resuming %s of %s from checkpoint.\n",
6838 desc
, mdname(mddev
));
6839 mddev
->curr_resync
= j
;
6841 mddev
->curr_resync_completed
= j
;
6843 while (j
< max_sectors
) {
6848 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
6849 ((mddev
->curr_resync
> mddev
->curr_resync_completed
&&
6850 (mddev
->curr_resync
- mddev
->curr_resync_completed
)
6851 > (max_sectors
>> 4)) ||
6852 (j
- mddev
->curr_resync_completed
)*2
6853 >= mddev
->resync_max
- mddev
->curr_resync_completed
6855 /* time to update curr_resync_completed */
6857 wait_event(mddev
->recovery_wait
,
6858 atomic_read(&mddev
->recovery_active
) == 0);
6859 mddev
->curr_resync_completed
= j
;
6860 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6861 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
6864 while (j
>= mddev
->resync_max
&& !kthread_should_stop()) {
6865 /* As this condition is controlled by user-space,
6866 * we can block indefinitely, so use '_interruptible'
6867 * to avoid triggering warnings.
6869 flush_signals(current
); /* just in case */
6870 wait_event_interruptible(mddev
->recovery_wait
,
6871 mddev
->resync_max
> j
6872 || kthread_should_stop());
6875 if (kthread_should_stop())
6878 sectors
= mddev
->pers
->sync_request(mddev
, j
, &skipped
,
6879 currspeed
< speed_min(mddev
));
6881 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6885 if (!skipped
) { /* actual IO requested */
6886 io_sectors
+= sectors
;
6887 atomic_add(sectors
, &mddev
->recovery_active
);
6891 if (j
>1) mddev
->curr_resync
= j
;
6892 mddev
->curr_mark_cnt
= io_sectors
;
6893 if (last_check
== 0)
6894 /* this is the earliers that rebuilt will be
6895 * visible in /proc/mdstat
6897 md_new_event(mddev
);
6899 if (last_check
+ window
> io_sectors
|| j
== max_sectors
)
6902 last_check
= io_sectors
;
6904 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
6908 if (time_after_eq(jiffies
, mark
[last_mark
] + SYNC_MARK_STEP
)) {
6910 int next
= (last_mark
+1) % SYNC_MARKS
;
6912 mddev
->resync_mark
= mark
[next
];
6913 mddev
->resync_mark_cnt
= mark_cnt
[next
];
6914 mark
[next
] = jiffies
;
6915 mark_cnt
[next
] = io_sectors
- atomic_read(&mddev
->recovery_active
);
6920 if (kthread_should_stop())
6925 * this loop exits only if either when we are slower than
6926 * the 'hard' speed limit, or the system was IO-idle for
6928 * the system might be non-idle CPU-wise, but we only care
6929 * about not overloading the IO subsystem. (things like an
6930 * e2fsck being done on the RAID array should execute fast)
6935 currspeed
= ((unsigned long)(io_sectors
-mddev
->resync_mark_cnt
))/2
6936 /((jiffies
-mddev
->resync_mark
)/HZ
+1) +1;
6938 if (currspeed
> speed_min(mddev
)) {
6939 if ((currspeed
> speed_max(mddev
)) ||
6940 !is_mddev_idle(mddev
, 0)) {
6946 printk(KERN_INFO
"md: %s: %s done.\n",mdname(mddev
), desc
);
6948 * this also signals 'finished resyncing' to md_stop
6953 wait_event(mddev
->recovery_wait
, !atomic_read(&mddev
->recovery_active
));
6955 /* tell personality that we are finished */
6956 mddev
->pers
->sync_request(mddev
, max_sectors
, &skipped
, 1);
6958 if (!test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
) &&
6959 mddev
->curr_resync
> 2) {
6960 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
6961 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
6962 if (mddev
->curr_resync
>= mddev
->recovery_cp
) {
6964 "md: checkpointing %s of %s.\n",
6965 desc
, mdname(mddev
));
6966 mddev
->recovery_cp
= mddev
->curr_resync
;
6969 mddev
->recovery_cp
= MaxSector
;
6971 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
6972 mddev
->curr_resync
= MaxSector
;
6974 list_for_each_entry_rcu(rdev
, &mddev
->disks
, same_set
)
6975 if (rdev
->raid_disk
>= 0 &&
6976 mddev
->delta_disks
>= 0 &&
6977 !test_bit(Faulty
, &rdev
->flags
) &&
6978 !test_bit(In_sync
, &rdev
->flags
) &&
6979 rdev
->recovery_offset
< mddev
->curr_resync
)
6980 rdev
->recovery_offset
= mddev
->curr_resync
;
6984 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
6987 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
6988 /* We completed so min/max setting can be forgotten if used. */
6989 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
6990 mddev
->resync_min
= 0;
6991 mddev
->resync_max
= MaxSector
;
6992 } else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
6993 mddev
->resync_min
= mddev
->curr_resync_completed
;
6994 mddev
->curr_resync
= 0;
6995 wake_up(&resync_wait
);
6996 set_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
6997 md_wakeup_thread(mddev
->thread
);
7002 * got a signal, exit.
7005 "md: md_do_sync() got signal ... exiting\n");
7006 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7010 EXPORT_SYMBOL_GPL(md_do_sync
);
7013 static int remove_and_add_spares(mddev_t
*mddev
)
7018 mddev
->curr_resync_completed
= 0;
7020 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
7021 if (rdev
->raid_disk
>= 0 &&
7022 !test_bit(Blocked
, &rdev
->flags
) &&
7023 (test_bit(Faulty
, &rdev
->flags
) ||
7024 ! test_bit(In_sync
, &rdev
->flags
)) &&
7025 atomic_read(&rdev
->nr_pending
)==0) {
7026 if (mddev
->pers
->hot_remove_disk(
7027 mddev
, rdev
->raid_disk
)==0) {
7029 sprintf(nm
,"rd%d", rdev
->raid_disk
);
7030 sysfs_remove_link(&mddev
->kobj
, nm
);
7031 rdev
->raid_disk
= -1;
7035 if (mddev
->degraded
&& !mddev
->recovery_disabled
) {
7036 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
7037 if (rdev
->raid_disk
>= 0 &&
7038 !test_bit(In_sync
, &rdev
->flags
) &&
7039 !test_bit(Blocked
, &rdev
->flags
))
7041 if (rdev
->raid_disk
< 0
7042 && !test_bit(Faulty
, &rdev
->flags
)) {
7043 rdev
->recovery_offset
= 0;
7045 hot_add_disk(mddev
, rdev
) == 0) {
7047 sprintf(nm
, "rd%d", rdev
->raid_disk
);
7048 if (sysfs_create_link(&mddev
->kobj
,
7050 /* failure here is OK */;
7052 md_new_event(mddev
);
7053 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
7062 static void reap_sync_thread(mddev_t
*mddev
)
7066 /* resync has finished, collect result */
7067 md_unregister_thread(mddev
->sync_thread
);
7068 mddev
->sync_thread
= NULL
;
7069 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
7070 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
7072 /* activate any spares */
7073 if (mddev
->pers
->spare_active(mddev
))
7074 sysfs_notify(&mddev
->kobj
, NULL
,
7077 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
7078 mddev
->pers
->finish_reshape
)
7079 mddev
->pers
->finish_reshape(mddev
);
7080 md_update_sb(mddev
, 1);
7082 /* if array is no-longer degraded, then any saved_raid_disk
7083 * information must be scrapped
7085 if (!mddev
->degraded
)
7086 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
7087 rdev
->saved_raid_disk
= -1;
7089 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7090 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7091 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7092 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
7093 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7094 /* flag recovery needed just to double check */
7095 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7096 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7097 md_new_event(mddev
);
7101 * This routine is regularly called by all per-raid-array threads to
7102 * deal with generic issues like resync and super-block update.
7103 * Raid personalities that don't have a thread (linear/raid0) do not
7104 * need this as they never do any recovery or update the superblock.
7106 * It does not do any resync itself, but rather "forks" off other threads
7107 * to do that as needed.
7108 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7109 * "->recovery" and create a thread at ->sync_thread.
7110 * When the thread finishes it sets MD_RECOVERY_DONE
7111 * and wakeups up this thread which will reap the thread and finish up.
7112 * This thread also removes any faulty devices (with nr_pending == 0).
7114 * The overall approach is:
7115 * 1/ if the superblock needs updating, update it.
7116 * 2/ If a recovery thread is running, don't do anything else.
7117 * 3/ If recovery has finished, clean up, possibly marking spares active.
7118 * 4/ If there are any faulty devices, remove them.
7119 * 5/ If array is degraded, try to add spares devices
7120 * 6/ If array has spares or is not in-sync, start a resync thread.
7122 void md_check_recovery(mddev_t
*mddev
)
7125 bitmap_daemon_work(mddev
);
7130 if (signal_pending(current
)) {
7131 if (mddev
->pers
->sync_request
&& !mddev
->external
) {
7132 printk(KERN_INFO
"md: %s in immediate safe mode\n",
7134 mddev
->safemode
= 2;
7136 flush_signals(current
);
7139 if (mddev
->ro
&& !test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
7142 (mddev
->flags
& ~ (1<<MD_CHANGE_PENDING
)) ||
7143 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
7144 test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
7145 (mddev
->external
== 0 && mddev
->safemode
== 1) ||
7146 (mddev
->safemode
== 2 && ! atomic_read(&mddev
->writes_pending
)
7147 && !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
7151 if (mddev_trylock(mddev
)) {
7155 /* Only thing we do on a ro array is remove
7159 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
7160 if (rdev
->raid_disk
>= 0 &&
7161 !test_bit(Blocked
, &rdev
->flags
) &&
7162 test_bit(Faulty
, &rdev
->flags
) &&
7163 atomic_read(&rdev
->nr_pending
)==0) {
7164 if (mddev
->pers
->hot_remove_disk(
7165 mddev
, rdev
->raid_disk
)==0) {
7167 sprintf(nm
,"rd%d", rdev
->raid_disk
);
7168 sysfs_remove_link(&mddev
->kobj
, nm
);
7169 rdev
->raid_disk
= -1;
7172 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7176 if (!mddev
->external
) {
7178 spin_lock_irq(&mddev
->write_lock
);
7179 if (mddev
->safemode
&&
7180 !atomic_read(&mddev
->writes_pending
) &&
7182 mddev
->recovery_cp
== MaxSector
) {
7185 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
7187 if (mddev
->safemode
== 1)
7188 mddev
->safemode
= 0;
7189 spin_unlock_irq(&mddev
->write_lock
);
7191 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
7195 md_update_sb(mddev
, 0);
7197 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
7198 !test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
)) {
7199 /* resync/recovery still happening */
7200 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7203 if (mddev
->sync_thread
) {
7204 reap_sync_thread(mddev
);
7207 /* Set RUNNING before clearing NEEDED to avoid
7208 * any transients in the value of "sync_action".
7210 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7211 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7212 /* Clear some bits that don't mean anything, but
7215 clear_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7216 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
7218 if (test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
7220 /* no recovery is running.
7221 * remove any failed drives, then
7222 * add spares if possible.
7223 * Spare are also removed and re-added, to allow
7224 * the personality to fail the re-add.
7227 if (mddev
->reshape_position
!= MaxSector
) {
7228 if (mddev
->pers
->check_reshape
== NULL
||
7229 mddev
->pers
->check_reshape(mddev
) != 0)
7230 /* Cannot proceed */
7232 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7233 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7234 } else if ((spares
= remove_and_add_spares(mddev
))) {
7235 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7236 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7237 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
7238 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7239 } else if (mddev
->recovery_cp
< MaxSector
) {
7240 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7241 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7242 } else if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
7243 /* nothing to be done ... */
7246 if (mddev
->pers
->sync_request
) {
7247 if (spares
&& mddev
->bitmap
&& ! mddev
->bitmap
->file
) {
7248 /* We are adding a device or devices to an array
7249 * which has the bitmap stored on all devices.
7250 * So make sure all bitmap pages get written
7252 bitmap_write_all(mddev
->bitmap
);
7254 mddev
->sync_thread
= md_register_thread(md_do_sync
,
7257 if (!mddev
->sync_thread
) {
7258 printk(KERN_ERR
"%s: could not start resync"
7261 /* leave the spares where they are, it shouldn't hurt */
7262 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7263 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7264 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7265 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
7266 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7268 md_wakeup_thread(mddev
->sync_thread
);
7269 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7270 md_new_event(mddev
);
7273 if (!mddev
->sync_thread
) {
7274 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7275 if (test_and_clear_bit(MD_RECOVERY_RECOVER
,
7277 if (mddev
->sysfs_action
)
7278 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7280 mddev_unlock(mddev
);
7284 void md_wait_for_blocked_rdev(mdk_rdev_t
*rdev
, mddev_t
*mddev
)
7286 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
7287 wait_event_timeout(rdev
->blocked_wait
,
7288 !test_bit(Blocked
, &rdev
->flags
),
7289 msecs_to_jiffies(5000));
7290 rdev_dec_pending(rdev
, mddev
);
7292 EXPORT_SYMBOL(md_wait_for_blocked_rdev
);
7294 static int md_notify_reboot(struct notifier_block
*this,
7295 unsigned long code
, void *x
)
7297 struct list_head
*tmp
;
7300 if ((code
== SYS_DOWN
) || (code
== SYS_HALT
) || (code
== SYS_POWER_OFF
)) {
7302 printk(KERN_INFO
"md: stopping all md devices.\n");
7304 for_each_mddev(mddev
, tmp
)
7305 if (mddev_trylock(mddev
)) {
7306 /* Force a switch to readonly even array
7307 * appears to still be in use. Hence
7310 md_set_readonly(mddev
, 100);
7311 mddev_unlock(mddev
);
7314 * certain more exotic SCSI devices are known to be
7315 * volatile wrt too early system reboots. While the
7316 * right place to handle this issue is the given
7317 * driver, we do want to have a safe RAID driver ...
7324 static struct notifier_block md_notifier
= {
7325 .notifier_call
= md_notify_reboot
,
7327 .priority
= INT_MAX
, /* before any real devices */
7330 static void md_geninit(void)
7332 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t
));
7334 proc_create("mdstat", S_IRUGO
, NULL
, &md_seq_fops
);
7337 static int __init
md_init(void)
7341 md_wq
= alloc_workqueue("md", WQ_RESCUER
, 0);
7345 md_misc_wq
= alloc_workqueue("md_misc", 0, 0);
7349 if ((ret
= register_blkdev(MD_MAJOR
, "md")) < 0)
7352 if ((ret
= register_blkdev(0, "mdp")) < 0)
7356 blk_register_region(MKDEV(MD_MAJOR
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
7357 md_probe
, NULL
, NULL
);
7358 blk_register_region(MKDEV(mdp_major
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
7359 md_probe
, NULL
, NULL
);
7361 register_reboot_notifier(&md_notifier
);
7362 raid_table_header
= register_sysctl_table(raid_root_table
);
7368 unregister_blkdev(MD_MAJOR
, "md");
7370 destroy_workqueue(md_misc_wq
);
7372 destroy_workqueue(md_wq
);
7380 * Searches all registered partitions for autorun RAID arrays
7384 static LIST_HEAD(all_detected_devices
);
7385 struct detected_devices_node
{
7386 struct list_head list
;
7390 void md_autodetect_dev(dev_t dev
)
7392 struct detected_devices_node
*node_detected_dev
;
7394 node_detected_dev
= kzalloc(sizeof(*node_detected_dev
), GFP_KERNEL
);
7395 if (node_detected_dev
) {
7396 node_detected_dev
->dev
= dev
;
7397 list_add_tail(&node_detected_dev
->list
, &all_detected_devices
);
7399 printk(KERN_CRIT
"md: md_autodetect_dev: kzalloc failed"
7400 ", skipping dev(%d,%d)\n", MAJOR(dev
), MINOR(dev
));
7405 static void autostart_arrays(int part
)
7408 struct detected_devices_node
*node_detected_dev
;
7410 int i_scanned
, i_passed
;
7415 printk(KERN_INFO
"md: Autodetecting RAID arrays.\n");
7417 while (!list_empty(&all_detected_devices
) && i_scanned
< INT_MAX
) {
7419 node_detected_dev
= list_entry(all_detected_devices
.next
,
7420 struct detected_devices_node
, list
);
7421 list_del(&node_detected_dev
->list
);
7422 dev
= node_detected_dev
->dev
;
7423 kfree(node_detected_dev
);
7424 rdev
= md_import_device(dev
,0, 90);
7428 if (test_bit(Faulty
, &rdev
->flags
)) {
7432 set_bit(AutoDetected
, &rdev
->flags
);
7433 list_add(&rdev
->same_set
, &pending_raid_disks
);
7437 printk(KERN_INFO
"md: Scanned %d and added %d devices.\n",
7438 i_scanned
, i_passed
);
7440 autorun_devices(part
);
7443 #endif /* !MODULE */
7445 static __exit
void md_exit(void)
7448 struct list_head
*tmp
;
7450 blk_unregister_region(MKDEV(MD_MAJOR
,0), 1U << MINORBITS
);
7451 blk_unregister_region(MKDEV(mdp_major
,0), 1U << MINORBITS
);
7453 unregister_blkdev(MD_MAJOR
,"md");
7454 unregister_blkdev(mdp_major
, "mdp");
7455 unregister_reboot_notifier(&md_notifier
);
7456 unregister_sysctl_table(raid_table_header
);
7457 remove_proc_entry("mdstat", NULL
);
7458 for_each_mddev(mddev
, tmp
) {
7459 export_array(mddev
);
7460 mddev
->hold_active
= 0;
7462 destroy_workqueue(md_misc_wq
);
7463 destroy_workqueue(md_wq
);
7466 subsys_initcall(md_init
);
7467 module_exit(md_exit
)
7469 static int get_ro(char *buffer
, struct kernel_param
*kp
)
7471 return sprintf(buffer
, "%d", start_readonly
);
7473 static int set_ro(const char *val
, struct kernel_param
*kp
)
7476 int num
= simple_strtoul(val
, &e
, 10);
7477 if (*val
&& (*e
== '\0' || *e
== '\n')) {
7478 start_readonly
= num
;
7484 module_param_call(start_ro
, set_ro
, get_ro
, NULL
, S_IRUSR
|S_IWUSR
);
7485 module_param(start_dirty_degraded
, int, S_IRUGO
|S_IWUSR
);
7487 module_param_call(new_array
, add_named_array
, NULL
, NULL
, S_IWUSR
);
7489 EXPORT_SYMBOL(register_md_personality
);
7490 EXPORT_SYMBOL(unregister_md_personality
);
7491 EXPORT_SYMBOL(md_error
);
7492 EXPORT_SYMBOL(md_done_sync
);
7493 EXPORT_SYMBOL(md_write_start
);
7494 EXPORT_SYMBOL(md_write_end
);
7495 EXPORT_SYMBOL(md_register_thread
);
7496 EXPORT_SYMBOL(md_unregister_thread
);
7497 EXPORT_SYMBOL(md_wakeup_thread
);
7498 EXPORT_SYMBOL(md_check_recovery
);
7499 MODULE_LICENSE("GPL");
7500 MODULE_DESCRIPTION("MD RAID framework");
7502 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR
);