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
;
556 if (unit
&& MAJOR(unit
) != MD_MAJOR
)
557 unit
&= ~((1<<MdpMinorShift
)-1);
560 spin_lock(&all_mddevs_lock
);
563 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
564 if (mddev
->unit
== unit
) {
566 spin_unlock(&all_mddevs_lock
);
572 list_add(&new->all_mddevs
, &all_mddevs
);
573 spin_unlock(&all_mddevs_lock
);
574 new->hold_active
= UNTIL_IOCTL
;
578 /* find an unused unit number */
579 static int next_minor
= 512;
580 int start
= next_minor
;
584 dev
= MKDEV(MD_MAJOR
, next_minor
);
586 if (next_minor
> MINORMASK
)
588 if (next_minor
== start
) {
589 /* Oh dear, all in use. */
590 spin_unlock(&all_mddevs_lock
);
596 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
597 if (mddev
->unit
== dev
) {
603 new->md_minor
= MINOR(dev
);
604 new->hold_active
= UNTIL_STOP
;
605 list_add(&new->all_mddevs
, &all_mddevs
);
606 spin_unlock(&all_mddevs_lock
);
609 spin_unlock(&all_mddevs_lock
);
611 new = kzalloc(sizeof(*new), GFP_KERNEL
);
616 if (MAJOR(unit
) == MD_MAJOR
)
617 new->md_minor
= MINOR(unit
);
619 new->md_minor
= MINOR(unit
) >> MdpMinorShift
;
626 static inline int mddev_lock(mddev_t
* mddev
)
628 return mutex_lock_interruptible(&mddev
->reconfig_mutex
);
631 static inline int mddev_is_locked(mddev_t
*mddev
)
633 return mutex_is_locked(&mddev
->reconfig_mutex
);
636 static inline int mddev_trylock(mddev_t
* mddev
)
638 return mutex_trylock(&mddev
->reconfig_mutex
);
641 static struct attribute_group md_redundancy_group
;
643 static void mddev_unlock(mddev_t
* mddev
)
645 if (mddev
->to_remove
) {
646 /* These cannot be removed under reconfig_mutex as
647 * an access to the files will try to take reconfig_mutex
648 * while holding the file unremovable, which leads to
650 * So hold set sysfs_active while the remove in happeing,
651 * and anything else which might set ->to_remove or my
652 * otherwise change the sysfs namespace will fail with
653 * -EBUSY if sysfs_active is still set.
654 * We set sysfs_active under reconfig_mutex and elsewhere
655 * test it under the same mutex to ensure its correct value
658 struct attribute_group
*to_remove
= mddev
->to_remove
;
659 mddev
->to_remove
= NULL
;
660 mddev
->sysfs_active
= 1;
661 mutex_unlock(&mddev
->reconfig_mutex
);
663 if (mddev
->kobj
.sd
) {
664 if (to_remove
!= &md_redundancy_group
)
665 sysfs_remove_group(&mddev
->kobj
, to_remove
);
666 if (mddev
->pers
== NULL
||
667 mddev
->pers
->sync_request
== NULL
) {
668 sysfs_remove_group(&mddev
->kobj
, &md_redundancy_group
);
669 if (mddev
->sysfs_action
)
670 sysfs_put(mddev
->sysfs_action
);
671 mddev
->sysfs_action
= NULL
;
674 mddev
->sysfs_active
= 0;
676 mutex_unlock(&mddev
->reconfig_mutex
);
678 md_wakeup_thread(mddev
->thread
);
681 static mdk_rdev_t
* find_rdev_nr(mddev_t
*mddev
, int nr
)
685 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
686 if (rdev
->desc_nr
== nr
)
692 static mdk_rdev_t
* find_rdev(mddev_t
* mddev
, dev_t dev
)
696 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
697 if (rdev
->bdev
->bd_dev
== dev
)
703 static struct mdk_personality
*find_pers(int level
, char *clevel
)
705 struct mdk_personality
*pers
;
706 list_for_each_entry(pers
, &pers_list
, list
) {
707 if (level
!= LEVEL_NONE
&& pers
->level
== level
)
709 if (strcmp(pers
->name
, clevel
)==0)
715 /* return the offset of the super block in 512byte sectors */
716 static inline sector_t
calc_dev_sboffset(mdk_rdev_t
*rdev
)
718 sector_t num_sectors
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
719 return MD_NEW_SIZE_SECTORS(num_sectors
);
722 static int alloc_disk_sb(mdk_rdev_t
* rdev
)
727 rdev
->sb_page
= alloc_page(GFP_KERNEL
);
728 if (!rdev
->sb_page
) {
729 printk(KERN_ALERT
"md: out of memory.\n");
736 static void free_disk_sb(mdk_rdev_t
* rdev
)
739 put_page(rdev
->sb_page
);
741 rdev
->sb_page
= NULL
;
748 static void super_written(struct bio
*bio
, int error
)
750 mdk_rdev_t
*rdev
= bio
->bi_private
;
751 mddev_t
*mddev
= rdev
->mddev
;
753 if (error
|| !test_bit(BIO_UPTODATE
, &bio
->bi_flags
)) {
754 printk("md: super_written gets error=%d, uptodate=%d\n",
755 error
, test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
756 WARN_ON(test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
757 md_error(mddev
, rdev
);
760 if (atomic_dec_and_test(&mddev
->pending_writes
))
761 wake_up(&mddev
->sb_wait
);
765 void md_super_write(mddev_t
*mddev
, mdk_rdev_t
*rdev
,
766 sector_t sector
, int size
, struct page
*page
)
768 /* write first size bytes of page to sector of rdev
769 * Increment mddev->pending_writes before returning
770 * and decrement it on completion, waking up sb_wait
771 * if zero is reached.
772 * If an error occurred, call md_error
774 struct bio
*bio
= bio_alloc_mddev(GFP_NOIO
, 1, mddev
);
776 bio
->bi_bdev
= rdev
->meta_bdev
? rdev
->meta_bdev
: rdev
->bdev
;
777 bio
->bi_sector
= sector
;
778 bio_add_page(bio
, page
, size
, 0);
779 bio
->bi_private
= rdev
;
780 bio
->bi_end_io
= super_written
;
782 atomic_inc(&mddev
->pending_writes
);
783 submit_bio(REQ_WRITE
| REQ_SYNC
| REQ_UNPLUG
| REQ_FLUSH
| REQ_FUA
,
787 void md_super_wait(mddev_t
*mddev
)
789 /* wait for all superblock writes that were scheduled to complete */
792 prepare_to_wait(&mddev
->sb_wait
, &wq
, TASK_UNINTERRUPTIBLE
);
793 if (atomic_read(&mddev
->pending_writes
)==0)
797 finish_wait(&mddev
->sb_wait
, &wq
);
800 static void bi_complete(struct bio
*bio
, int error
)
802 complete((struct completion
*)bio
->bi_private
);
805 int sync_page_io(mdk_rdev_t
*rdev
, sector_t sector
, int size
,
806 struct page
*page
, int rw
, bool metadata_op
)
808 struct bio
*bio
= bio_alloc_mddev(GFP_NOIO
, 1, rdev
->mddev
);
809 struct completion event
;
812 rw
|= REQ_SYNC
| REQ_UNPLUG
;
814 bio
->bi_bdev
= (metadata_op
&& rdev
->meta_bdev
) ?
815 rdev
->meta_bdev
: rdev
->bdev
;
817 bio
->bi_sector
= sector
+ rdev
->sb_start
;
819 bio
->bi_sector
= sector
+ rdev
->data_offset
;
820 bio_add_page(bio
, page
, size
, 0);
821 init_completion(&event
);
822 bio
->bi_private
= &event
;
823 bio
->bi_end_io
= bi_complete
;
825 wait_for_completion(&event
);
827 ret
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
831 EXPORT_SYMBOL_GPL(sync_page_io
);
833 static int read_disk_sb(mdk_rdev_t
* rdev
, int size
)
835 char b
[BDEVNAME_SIZE
];
836 if (!rdev
->sb_page
) {
844 if (!sync_page_io(rdev
, 0, size
, rdev
->sb_page
, READ
, true))
850 printk(KERN_WARNING
"md: disabled device %s, could not read superblock.\n",
851 bdevname(rdev
->bdev
,b
));
855 static int uuid_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
857 return sb1
->set_uuid0
== sb2
->set_uuid0
&&
858 sb1
->set_uuid1
== sb2
->set_uuid1
&&
859 sb1
->set_uuid2
== sb2
->set_uuid2
&&
860 sb1
->set_uuid3
== sb2
->set_uuid3
;
863 static int sb_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
866 mdp_super_t
*tmp1
, *tmp2
;
868 tmp1
= kmalloc(sizeof(*tmp1
),GFP_KERNEL
);
869 tmp2
= kmalloc(sizeof(*tmp2
),GFP_KERNEL
);
871 if (!tmp1
|| !tmp2
) {
873 printk(KERN_INFO
"md.c sb_equal(): failed to allocate memory!\n");
881 * nr_disks is not constant
886 ret
= (memcmp(tmp1
, tmp2
, MD_SB_GENERIC_CONSTANT_WORDS
* 4) == 0);
894 static u32
md_csum_fold(u32 csum
)
896 csum
= (csum
& 0xffff) + (csum
>> 16);
897 return (csum
& 0xffff) + (csum
>> 16);
900 static unsigned int calc_sb_csum(mdp_super_t
* sb
)
903 u32
*sb32
= (u32
*)sb
;
905 unsigned int disk_csum
, csum
;
907 disk_csum
= sb
->sb_csum
;
910 for (i
= 0; i
< MD_SB_BYTES
/4 ; i
++)
912 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
916 /* This used to use csum_partial, which was wrong for several
917 * reasons including that different results are returned on
918 * different architectures. It isn't critical that we get exactly
919 * the same return value as before (we always csum_fold before
920 * testing, and that removes any differences). However as we
921 * know that csum_partial always returned a 16bit value on
922 * alphas, do a fold to maximise conformity to previous behaviour.
924 sb
->sb_csum
= md_csum_fold(disk_csum
);
926 sb
->sb_csum
= disk_csum
;
933 * Handle superblock details.
934 * We want to be able to handle multiple superblock formats
935 * so we have a common interface to them all, and an array of
936 * different handlers.
937 * We rely on user-space to write the initial superblock, and support
938 * reading and updating of superblocks.
939 * Interface methods are:
940 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
941 * loads and validates a superblock on dev.
942 * if refdev != NULL, compare superblocks on both devices
944 * 0 - dev has a superblock that is compatible with refdev
945 * 1 - dev has a superblock that is compatible and newer than refdev
946 * so dev should be used as the refdev in future
947 * -EINVAL superblock incompatible or invalid
948 * -othererror e.g. -EIO
950 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
951 * Verify that dev is acceptable into mddev.
952 * The first time, mddev->raid_disks will be 0, and data from
953 * dev should be merged in. Subsequent calls check that dev
954 * is new enough. Return 0 or -EINVAL
956 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
957 * Update the superblock for rdev with data in mddev
958 * This does not write to disc.
964 struct module
*owner
;
965 int (*load_super
)(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
,
967 int (*validate_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
968 void (*sync_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
969 unsigned long long (*rdev_size_change
)(mdk_rdev_t
*rdev
,
970 sector_t num_sectors
);
974 * Check that the given mddev has no bitmap.
976 * This function is called from the run method of all personalities that do not
977 * support bitmaps. It prints an error message and returns non-zero if mddev
978 * has a bitmap. Otherwise, it returns 0.
981 int md_check_no_bitmap(mddev_t
*mddev
)
983 if (!mddev
->bitmap_info
.file
&& !mddev
->bitmap_info
.offset
)
985 printk(KERN_ERR
"%s: bitmaps are not supported for %s\n",
986 mdname(mddev
), mddev
->pers
->name
);
989 EXPORT_SYMBOL(md_check_no_bitmap
);
992 * load_super for 0.90.0
994 static int super_90_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
996 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1001 * Calculate the position of the superblock (512byte sectors),
1002 * it's at the end of the disk.
1004 * It also happens to be a multiple of 4Kb.
1006 rdev
->sb_start
= calc_dev_sboffset(rdev
);
1008 ret
= read_disk_sb(rdev
, MD_SB_BYTES
);
1009 if (ret
) return ret
;
1013 bdevname(rdev
->bdev
, b
);
1014 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
1016 if (sb
->md_magic
!= MD_SB_MAGIC
) {
1017 printk(KERN_ERR
"md: invalid raid superblock magic on %s\n",
1022 if (sb
->major_version
!= 0 ||
1023 sb
->minor_version
< 90 ||
1024 sb
->minor_version
> 91) {
1025 printk(KERN_WARNING
"Bad version number %d.%d on %s\n",
1026 sb
->major_version
, sb
->minor_version
,
1031 if (sb
->raid_disks
<= 0)
1034 if (md_csum_fold(calc_sb_csum(sb
)) != md_csum_fold(sb
->sb_csum
)) {
1035 printk(KERN_WARNING
"md: invalid superblock checksum on %s\n",
1040 rdev
->preferred_minor
= sb
->md_minor
;
1041 rdev
->data_offset
= 0;
1042 rdev
->sb_size
= MD_SB_BYTES
;
1044 if (sb
->level
== LEVEL_MULTIPATH
)
1047 rdev
->desc_nr
= sb
->this_disk
.number
;
1053 mdp_super_t
*refsb
= (mdp_super_t
*)page_address(refdev
->sb_page
);
1054 if (!uuid_equal(refsb
, sb
)) {
1055 printk(KERN_WARNING
"md: %s has different UUID to %s\n",
1056 b
, bdevname(refdev
->bdev
,b2
));
1059 if (!sb_equal(refsb
, sb
)) {
1060 printk(KERN_WARNING
"md: %s has same UUID"
1061 " but different superblock to %s\n",
1062 b
, bdevname(refdev
->bdev
, b2
));
1066 ev2
= md_event(refsb
);
1072 rdev
->sectors
= rdev
->sb_start
;
1074 if (rdev
->sectors
< sb
->size
* 2 && sb
->level
> 1)
1075 /* "this cannot possibly happen" ... */
1083 * validate_super for 0.90.0
1085 static int super_90_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1088 mdp_super_t
*sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
1089 __u64 ev1
= md_event(sb
);
1091 rdev
->raid_disk
= -1;
1092 clear_bit(Faulty
, &rdev
->flags
);
1093 clear_bit(In_sync
, &rdev
->flags
);
1094 clear_bit(WriteMostly
, &rdev
->flags
);
1096 if (mddev
->raid_disks
== 0) {
1097 mddev
->major_version
= 0;
1098 mddev
->minor_version
= sb
->minor_version
;
1099 mddev
->patch_version
= sb
->patch_version
;
1100 mddev
->external
= 0;
1101 mddev
->chunk_sectors
= sb
->chunk_size
>> 9;
1102 mddev
->ctime
= sb
->ctime
;
1103 mddev
->utime
= sb
->utime
;
1104 mddev
->level
= sb
->level
;
1105 mddev
->clevel
[0] = 0;
1106 mddev
->layout
= sb
->layout
;
1107 mddev
->raid_disks
= sb
->raid_disks
;
1108 mddev
->dev_sectors
= sb
->size
* 2;
1109 mddev
->events
= ev1
;
1110 mddev
->bitmap_info
.offset
= 0;
1111 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
1113 if (mddev
->minor_version
>= 91) {
1114 mddev
->reshape_position
= sb
->reshape_position
;
1115 mddev
->delta_disks
= sb
->delta_disks
;
1116 mddev
->new_level
= sb
->new_level
;
1117 mddev
->new_layout
= sb
->new_layout
;
1118 mddev
->new_chunk_sectors
= sb
->new_chunk
>> 9;
1120 mddev
->reshape_position
= MaxSector
;
1121 mddev
->delta_disks
= 0;
1122 mddev
->new_level
= mddev
->level
;
1123 mddev
->new_layout
= mddev
->layout
;
1124 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1127 if (sb
->state
& (1<<MD_SB_CLEAN
))
1128 mddev
->recovery_cp
= MaxSector
;
1130 if (sb
->events_hi
== sb
->cp_events_hi
&&
1131 sb
->events_lo
== sb
->cp_events_lo
) {
1132 mddev
->recovery_cp
= sb
->recovery_cp
;
1134 mddev
->recovery_cp
= 0;
1137 memcpy(mddev
->uuid
+0, &sb
->set_uuid0
, 4);
1138 memcpy(mddev
->uuid
+4, &sb
->set_uuid1
, 4);
1139 memcpy(mddev
->uuid
+8, &sb
->set_uuid2
, 4);
1140 memcpy(mddev
->uuid
+12,&sb
->set_uuid3
, 4);
1142 mddev
->max_disks
= MD_SB_DISKS
;
1144 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
) &&
1145 mddev
->bitmap_info
.file
== NULL
)
1146 mddev
->bitmap_info
.offset
=
1147 mddev
->bitmap_info
.default_offset
;
1149 } else if (mddev
->pers
== NULL
) {
1150 /* Insist on good event counter while assembling, except
1151 * for spares (which don't need an event count) */
1153 if (sb
->disks
[rdev
->desc_nr
].state
& (
1154 (1<<MD_DISK_SYNC
) | (1 << MD_DISK_ACTIVE
)))
1155 if (ev1
< mddev
->events
)
1157 } else if (mddev
->bitmap
) {
1158 /* if adding to array with a bitmap, then we can accept an
1159 * older device ... but not too old.
1161 if (ev1
< mddev
->bitmap
->events_cleared
)
1164 if (ev1
< mddev
->events
)
1165 /* just a hot-add of a new device, leave raid_disk at -1 */
1169 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1170 desc
= sb
->disks
+ rdev
->desc_nr
;
1172 if (desc
->state
& (1<<MD_DISK_FAULTY
))
1173 set_bit(Faulty
, &rdev
->flags
);
1174 else if (desc
->state
& (1<<MD_DISK_SYNC
) /* &&
1175 desc->raid_disk < mddev->raid_disks */) {
1176 set_bit(In_sync
, &rdev
->flags
);
1177 rdev
->raid_disk
= desc
->raid_disk
;
1178 } else if (desc
->state
& (1<<MD_DISK_ACTIVE
)) {
1179 /* active but not in sync implies recovery up to
1180 * reshape position. We don't know exactly where
1181 * that is, so set to zero for now */
1182 if (mddev
->minor_version
>= 91) {
1183 rdev
->recovery_offset
= 0;
1184 rdev
->raid_disk
= desc
->raid_disk
;
1187 if (desc
->state
& (1<<MD_DISK_WRITEMOSTLY
))
1188 set_bit(WriteMostly
, &rdev
->flags
);
1189 } else /* MULTIPATH are always insync */
1190 set_bit(In_sync
, &rdev
->flags
);
1195 * sync_super for 0.90.0
1197 static void super_90_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1201 int next_spare
= mddev
->raid_disks
;
1204 /* make rdev->sb match mddev data..
1207 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1208 * 3/ any empty disks < next_spare become removed
1210 * disks[0] gets initialised to REMOVED because
1211 * we cannot be sure from other fields if it has
1212 * been initialised or not.
1215 int active
=0, working
=0,failed
=0,spare
=0,nr_disks
=0;
1217 rdev
->sb_size
= MD_SB_BYTES
;
1219 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
1221 memset(sb
, 0, sizeof(*sb
));
1223 sb
->md_magic
= MD_SB_MAGIC
;
1224 sb
->major_version
= mddev
->major_version
;
1225 sb
->patch_version
= mddev
->patch_version
;
1226 sb
->gvalid_words
= 0; /* ignored */
1227 memcpy(&sb
->set_uuid0
, mddev
->uuid
+0, 4);
1228 memcpy(&sb
->set_uuid1
, mddev
->uuid
+4, 4);
1229 memcpy(&sb
->set_uuid2
, mddev
->uuid
+8, 4);
1230 memcpy(&sb
->set_uuid3
, mddev
->uuid
+12,4);
1232 sb
->ctime
= mddev
->ctime
;
1233 sb
->level
= mddev
->level
;
1234 sb
->size
= mddev
->dev_sectors
/ 2;
1235 sb
->raid_disks
= mddev
->raid_disks
;
1236 sb
->md_minor
= mddev
->md_minor
;
1237 sb
->not_persistent
= 0;
1238 sb
->utime
= mddev
->utime
;
1240 sb
->events_hi
= (mddev
->events
>>32);
1241 sb
->events_lo
= (u32
)mddev
->events
;
1243 if (mddev
->reshape_position
== MaxSector
)
1244 sb
->minor_version
= 90;
1246 sb
->minor_version
= 91;
1247 sb
->reshape_position
= mddev
->reshape_position
;
1248 sb
->new_level
= mddev
->new_level
;
1249 sb
->delta_disks
= mddev
->delta_disks
;
1250 sb
->new_layout
= mddev
->new_layout
;
1251 sb
->new_chunk
= mddev
->new_chunk_sectors
<< 9;
1253 mddev
->minor_version
= sb
->minor_version
;
1256 sb
->recovery_cp
= mddev
->recovery_cp
;
1257 sb
->cp_events_hi
= (mddev
->events
>>32);
1258 sb
->cp_events_lo
= (u32
)mddev
->events
;
1259 if (mddev
->recovery_cp
== MaxSector
)
1260 sb
->state
= (1<< MD_SB_CLEAN
);
1262 sb
->recovery_cp
= 0;
1264 sb
->layout
= mddev
->layout
;
1265 sb
->chunk_size
= mddev
->chunk_sectors
<< 9;
1267 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
)
1268 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
1270 sb
->disks
[0].state
= (1<<MD_DISK_REMOVED
);
1271 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
1274 int is_active
= test_bit(In_sync
, &rdev2
->flags
);
1276 if (rdev2
->raid_disk
>= 0 &&
1277 sb
->minor_version
>= 91)
1278 /* we have nowhere to store the recovery_offset,
1279 * but if it is not below the reshape_position,
1280 * we can piggy-back on that.
1283 if (rdev2
->raid_disk
< 0 ||
1284 test_bit(Faulty
, &rdev2
->flags
))
1287 desc_nr
= rdev2
->raid_disk
;
1289 desc_nr
= next_spare
++;
1290 rdev2
->desc_nr
= desc_nr
;
1291 d
= &sb
->disks
[rdev2
->desc_nr
];
1293 d
->number
= rdev2
->desc_nr
;
1294 d
->major
= MAJOR(rdev2
->bdev
->bd_dev
);
1295 d
->minor
= MINOR(rdev2
->bdev
->bd_dev
);
1297 d
->raid_disk
= rdev2
->raid_disk
;
1299 d
->raid_disk
= rdev2
->desc_nr
; /* compatibility */
1300 if (test_bit(Faulty
, &rdev2
->flags
))
1301 d
->state
= (1<<MD_DISK_FAULTY
);
1302 else if (is_active
) {
1303 d
->state
= (1<<MD_DISK_ACTIVE
);
1304 if (test_bit(In_sync
, &rdev2
->flags
))
1305 d
->state
|= (1<<MD_DISK_SYNC
);
1313 if (test_bit(WriteMostly
, &rdev2
->flags
))
1314 d
->state
|= (1<<MD_DISK_WRITEMOSTLY
);
1316 /* now set the "removed" and "faulty" bits on any missing devices */
1317 for (i
=0 ; i
< mddev
->raid_disks
; i
++) {
1318 mdp_disk_t
*d
= &sb
->disks
[i
];
1319 if (d
->state
== 0 && d
->number
== 0) {
1322 d
->state
= (1<<MD_DISK_REMOVED
);
1323 d
->state
|= (1<<MD_DISK_FAULTY
);
1327 sb
->nr_disks
= nr_disks
;
1328 sb
->active_disks
= active
;
1329 sb
->working_disks
= working
;
1330 sb
->failed_disks
= failed
;
1331 sb
->spare_disks
= spare
;
1333 sb
->this_disk
= sb
->disks
[rdev
->desc_nr
];
1334 sb
->sb_csum
= calc_sb_csum(sb
);
1338 * rdev_size_change for 0.90.0
1340 static unsigned long long
1341 super_90_rdev_size_change(mdk_rdev_t
*rdev
, sector_t num_sectors
)
1343 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1344 return 0; /* component must fit device */
1345 if (rdev
->mddev
->bitmap_info
.offset
)
1346 return 0; /* can't move bitmap */
1347 rdev
->sb_start
= calc_dev_sboffset(rdev
);
1348 if (!num_sectors
|| num_sectors
> rdev
->sb_start
)
1349 num_sectors
= rdev
->sb_start
;
1350 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1352 md_super_wait(rdev
->mddev
);
1358 * version 1 superblock
1361 static __le32
calc_sb_1_csum(struct mdp_superblock_1
* sb
)
1365 unsigned long long newcsum
;
1366 int size
= 256 + le32_to_cpu(sb
->max_dev
)*2;
1367 __le32
*isuper
= (__le32
*)sb
;
1370 disk_csum
= sb
->sb_csum
;
1373 for (i
=0; size
>=4; size
-= 4 )
1374 newcsum
+= le32_to_cpu(*isuper
++);
1377 newcsum
+= le16_to_cpu(*(__le16
*) isuper
);
1379 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
1380 sb
->sb_csum
= disk_csum
;
1381 return cpu_to_le32(csum
);
1384 static int super_1_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
1386 struct mdp_superblock_1
*sb
;
1389 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1393 * Calculate the position of the superblock in 512byte sectors.
1394 * It is always aligned to a 4K boundary and
1395 * depeding on minor_version, it can be:
1396 * 0: At least 8K, but less than 12K, from end of device
1397 * 1: At start of device
1398 * 2: 4K from start of device.
1400 switch(minor_version
) {
1402 sb_start
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
1404 sb_start
&= ~(sector_t
)(4*2-1);
1415 rdev
->sb_start
= sb_start
;
1417 /* superblock is rarely larger than 1K, but it can be larger,
1418 * and it is safe to read 4k, so we do that
1420 ret
= read_disk_sb(rdev
, 4096);
1421 if (ret
) return ret
;
1424 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1426 if (sb
->magic
!= cpu_to_le32(MD_SB_MAGIC
) ||
1427 sb
->major_version
!= cpu_to_le32(1) ||
1428 le32_to_cpu(sb
->max_dev
) > (4096-256)/2 ||
1429 le64_to_cpu(sb
->super_offset
) != rdev
->sb_start
||
1430 (le32_to_cpu(sb
->feature_map
) & ~MD_FEATURE_ALL
) != 0)
1433 if (calc_sb_1_csum(sb
) != sb
->sb_csum
) {
1434 printk("md: invalid superblock checksum on %s\n",
1435 bdevname(rdev
->bdev
,b
));
1438 if (le64_to_cpu(sb
->data_size
) < 10) {
1439 printk("md: data_size too small on %s\n",
1440 bdevname(rdev
->bdev
,b
));
1444 rdev
->preferred_minor
= 0xffff;
1445 rdev
->data_offset
= le64_to_cpu(sb
->data_offset
);
1446 atomic_set(&rdev
->corrected_errors
, le32_to_cpu(sb
->cnt_corrected_read
));
1448 rdev
->sb_size
= le32_to_cpu(sb
->max_dev
) * 2 + 256;
1449 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1450 if (rdev
->sb_size
& bmask
)
1451 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1454 && rdev
->data_offset
< sb_start
+ (rdev
->sb_size
/512))
1457 if (sb
->level
== cpu_to_le32(LEVEL_MULTIPATH
))
1460 rdev
->desc_nr
= le32_to_cpu(sb
->dev_number
);
1466 struct mdp_superblock_1
*refsb
=
1467 (struct mdp_superblock_1
*)page_address(refdev
->sb_page
);
1469 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16) != 0 ||
1470 sb
->level
!= refsb
->level
||
1471 sb
->layout
!= refsb
->layout
||
1472 sb
->chunksize
!= refsb
->chunksize
) {
1473 printk(KERN_WARNING
"md: %s has strangely different"
1474 " superblock to %s\n",
1475 bdevname(rdev
->bdev
,b
),
1476 bdevname(refdev
->bdev
,b2
));
1479 ev1
= le64_to_cpu(sb
->events
);
1480 ev2
= le64_to_cpu(refsb
->events
);
1488 rdev
->sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) -
1489 le64_to_cpu(sb
->data_offset
);
1491 rdev
->sectors
= rdev
->sb_start
;
1492 if (rdev
->sectors
< le64_to_cpu(sb
->data_size
))
1494 rdev
->sectors
= le64_to_cpu(sb
->data_size
);
1495 if (le64_to_cpu(sb
->size
) > rdev
->sectors
)
1500 static int super_1_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1502 struct mdp_superblock_1
*sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1503 __u64 ev1
= le64_to_cpu(sb
->events
);
1505 rdev
->raid_disk
= -1;
1506 clear_bit(Faulty
, &rdev
->flags
);
1507 clear_bit(In_sync
, &rdev
->flags
);
1508 clear_bit(WriteMostly
, &rdev
->flags
);
1510 if (mddev
->raid_disks
== 0) {
1511 mddev
->major_version
= 1;
1512 mddev
->patch_version
= 0;
1513 mddev
->external
= 0;
1514 mddev
->chunk_sectors
= le32_to_cpu(sb
->chunksize
);
1515 mddev
->ctime
= le64_to_cpu(sb
->ctime
) & ((1ULL << 32)-1);
1516 mddev
->utime
= le64_to_cpu(sb
->utime
) & ((1ULL << 32)-1);
1517 mddev
->level
= le32_to_cpu(sb
->level
);
1518 mddev
->clevel
[0] = 0;
1519 mddev
->layout
= le32_to_cpu(sb
->layout
);
1520 mddev
->raid_disks
= le32_to_cpu(sb
->raid_disks
);
1521 mddev
->dev_sectors
= le64_to_cpu(sb
->size
);
1522 mddev
->events
= ev1
;
1523 mddev
->bitmap_info
.offset
= 0;
1524 mddev
->bitmap_info
.default_offset
= 1024 >> 9;
1526 mddev
->recovery_cp
= le64_to_cpu(sb
->resync_offset
);
1527 memcpy(mddev
->uuid
, sb
->set_uuid
, 16);
1529 mddev
->max_disks
= (4096-256)/2;
1531 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BITMAP_OFFSET
) &&
1532 mddev
->bitmap_info
.file
== NULL
)
1533 mddev
->bitmap_info
.offset
=
1534 (__s32
)le32_to_cpu(sb
->bitmap_offset
);
1536 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
1537 mddev
->reshape_position
= le64_to_cpu(sb
->reshape_position
);
1538 mddev
->delta_disks
= le32_to_cpu(sb
->delta_disks
);
1539 mddev
->new_level
= le32_to_cpu(sb
->new_level
);
1540 mddev
->new_layout
= le32_to_cpu(sb
->new_layout
);
1541 mddev
->new_chunk_sectors
= le32_to_cpu(sb
->new_chunk
);
1543 mddev
->reshape_position
= MaxSector
;
1544 mddev
->delta_disks
= 0;
1545 mddev
->new_level
= mddev
->level
;
1546 mddev
->new_layout
= mddev
->layout
;
1547 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1550 } else if (mddev
->pers
== NULL
) {
1551 /* Insist of good event counter while assembling, except for
1552 * spares (which don't need an event count) */
1554 if (rdev
->desc_nr
>= 0 &&
1555 rdev
->desc_nr
< le32_to_cpu(sb
->max_dev
) &&
1556 le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]) < 0xfffe)
1557 if (ev1
< mddev
->events
)
1559 } else if (mddev
->bitmap
) {
1560 /* If adding to array with a bitmap, then we can accept an
1561 * older device, but not too old.
1563 if (ev1
< mddev
->bitmap
->events_cleared
)
1566 if (ev1
< mddev
->events
)
1567 /* just a hot-add of a new device, leave raid_disk at -1 */
1570 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1572 if (rdev
->desc_nr
< 0 ||
1573 rdev
->desc_nr
>= le32_to_cpu(sb
->max_dev
)) {
1577 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
1579 case 0xffff: /* spare */
1581 case 0xfffe: /* faulty */
1582 set_bit(Faulty
, &rdev
->flags
);
1585 if ((le32_to_cpu(sb
->feature_map
) &
1586 MD_FEATURE_RECOVERY_OFFSET
))
1587 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
1589 set_bit(In_sync
, &rdev
->flags
);
1590 rdev
->raid_disk
= role
;
1593 if (sb
->devflags
& WriteMostly1
)
1594 set_bit(WriteMostly
, &rdev
->flags
);
1595 } else /* MULTIPATH are always insync */
1596 set_bit(In_sync
, &rdev
->flags
);
1601 static void super_1_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1603 struct mdp_superblock_1
*sb
;
1606 /* make rdev->sb match mddev and rdev data. */
1608 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1610 sb
->feature_map
= 0;
1612 sb
->recovery_offset
= cpu_to_le64(0);
1613 memset(sb
->pad1
, 0, sizeof(sb
->pad1
));
1614 memset(sb
->pad2
, 0, sizeof(sb
->pad2
));
1615 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
1617 sb
->utime
= cpu_to_le64((__u64
)mddev
->utime
);
1618 sb
->events
= cpu_to_le64(mddev
->events
);
1620 sb
->resync_offset
= cpu_to_le64(mddev
->recovery_cp
);
1622 sb
->resync_offset
= cpu_to_le64(0);
1624 sb
->cnt_corrected_read
= cpu_to_le32(atomic_read(&rdev
->corrected_errors
));
1626 sb
->raid_disks
= cpu_to_le32(mddev
->raid_disks
);
1627 sb
->size
= cpu_to_le64(mddev
->dev_sectors
);
1628 sb
->chunksize
= cpu_to_le32(mddev
->chunk_sectors
);
1629 sb
->level
= cpu_to_le32(mddev
->level
);
1630 sb
->layout
= cpu_to_le32(mddev
->layout
);
1632 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
) {
1633 sb
->bitmap_offset
= cpu_to_le32((__u32
)mddev
->bitmap_info
.offset
);
1634 sb
->feature_map
= cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
);
1637 if (rdev
->raid_disk
>= 0 &&
1638 !test_bit(In_sync
, &rdev
->flags
)) {
1640 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET
);
1641 sb
->recovery_offset
=
1642 cpu_to_le64(rdev
->recovery_offset
);
1645 if (mddev
->reshape_position
!= MaxSector
) {
1646 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE
);
1647 sb
->reshape_position
= cpu_to_le64(mddev
->reshape_position
);
1648 sb
->new_layout
= cpu_to_le32(mddev
->new_layout
);
1649 sb
->delta_disks
= cpu_to_le32(mddev
->delta_disks
);
1650 sb
->new_level
= cpu_to_le32(mddev
->new_level
);
1651 sb
->new_chunk
= cpu_to_le32(mddev
->new_chunk_sectors
);
1655 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
)
1656 if (rdev2
->desc_nr
+1 > max_dev
)
1657 max_dev
= rdev2
->desc_nr
+1;
1659 if (max_dev
> le32_to_cpu(sb
->max_dev
)) {
1661 sb
->max_dev
= cpu_to_le32(max_dev
);
1662 rdev
->sb_size
= max_dev
* 2 + 256;
1663 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1664 if (rdev
->sb_size
& bmask
)
1665 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1667 max_dev
= le32_to_cpu(sb
->max_dev
);
1669 for (i
=0; i
<max_dev
;i
++)
1670 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1672 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
1674 if (test_bit(Faulty
, &rdev2
->flags
))
1675 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1676 else if (test_bit(In_sync
, &rdev2
->flags
))
1677 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1678 else if (rdev2
->raid_disk
>= 0)
1679 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1681 sb
->dev_roles
[i
] = cpu_to_le16(0xffff);
1684 sb
->sb_csum
= calc_sb_1_csum(sb
);
1687 static unsigned long long
1688 super_1_rdev_size_change(mdk_rdev_t
*rdev
, sector_t num_sectors
)
1690 struct mdp_superblock_1
*sb
;
1691 sector_t max_sectors
;
1692 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1693 return 0; /* component must fit device */
1694 if (rdev
->sb_start
< rdev
->data_offset
) {
1695 /* minor versions 1 and 2; superblock before data */
1696 max_sectors
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
1697 max_sectors
-= rdev
->data_offset
;
1698 if (!num_sectors
|| num_sectors
> max_sectors
)
1699 num_sectors
= max_sectors
;
1700 } else if (rdev
->mddev
->bitmap_info
.offset
) {
1701 /* minor version 0 with bitmap we can't move */
1704 /* minor version 0; superblock after data */
1706 sb_start
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) - 8*2;
1707 sb_start
&= ~(sector_t
)(4*2 - 1);
1708 max_sectors
= rdev
->sectors
+ sb_start
- rdev
->sb_start
;
1709 if (!num_sectors
|| num_sectors
> max_sectors
)
1710 num_sectors
= max_sectors
;
1711 rdev
->sb_start
= sb_start
;
1713 sb
= (struct mdp_superblock_1
*) page_address(rdev
->sb_page
);
1714 sb
->data_size
= cpu_to_le64(num_sectors
);
1715 sb
->super_offset
= rdev
->sb_start
;
1716 sb
->sb_csum
= calc_sb_1_csum(sb
);
1717 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1719 md_super_wait(rdev
->mddev
);
1723 static struct super_type super_types
[] = {
1726 .owner
= THIS_MODULE
,
1727 .load_super
= super_90_load
,
1728 .validate_super
= super_90_validate
,
1729 .sync_super
= super_90_sync
,
1730 .rdev_size_change
= super_90_rdev_size_change
,
1734 .owner
= THIS_MODULE
,
1735 .load_super
= super_1_load
,
1736 .validate_super
= super_1_validate
,
1737 .sync_super
= super_1_sync
,
1738 .rdev_size_change
= super_1_rdev_size_change
,
1742 static int match_mddev_units(mddev_t
*mddev1
, mddev_t
*mddev2
)
1744 mdk_rdev_t
*rdev
, *rdev2
;
1747 rdev_for_each_rcu(rdev
, mddev1
)
1748 rdev_for_each_rcu(rdev2
, mddev2
)
1749 if (rdev
->bdev
->bd_contains
==
1750 rdev2
->bdev
->bd_contains
) {
1758 static LIST_HEAD(pending_raid_disks
);
1761 * Try to register data integrity profile for an mddev
1763 * This is called when an array is started and after a disk has been kicked
1764 * from the array. It only succeeds if all working and active component devices
1765 * are integrity capable with matching profiles.
1767 int md_integrity_register(mddev_t
*mddev
)
1769 mdk_rdev_t
*rdev
, *reference
= NULL
;
1771 if (list_empty(&mddev
->disks
))
1772 return 0; /* nothing to do */
1773 if (blk_get_integrity(mddev
->gendisk
))
1774 return 0; /* already registered */
1775 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
1776 /* skip spares and non-functional disks */
1777 if (test_bit(Faulty
, &rdev
->flags
))
1779 if (rdev
->raid_disk
< 0)
1782 * If at least one rdev is not integrity capable, we can not
1783 * enable data integrity for the md device.
1785 if (!bdev_get_integrity(rdev
->bdev
))
1788 /* Use the first rdev as the reference */
1792 /* does this rdev's profile match the reference profile? */
1793 if (blk_integrity_compare(reference
->bdev
->bd_disk
,
1794 rdev
->bdev
->bd_disk
) < 0)
1798 * All component devices are integrity capable and have matching
1799 * profiles, register the common profile for the md device.
1801 if (blk_integrity_register(mddev
->gendisk
,
1802 bdev_get_integrity(reference
->bdev
)) != 0) {
1803 printk(KERN_ERR
"md: failed to register integrity for %s\n",
1807 printk(KERN_NOTICE
"md: data integrity on %s enabled\n",
1811 EXPORT_SYMBOL(md_integrity_register
);
1813 /* Disable data integrity if non-capable/non-matching disk is being added */
1814 void md_integrity_add_rdev(mdk_rdev_t
*rdev
, mddev_t
*mddev
)
1816 struct blk_integrity
*bi_rdev
= bdev_get_integrity(rdev
->bdev
);
1817 struct blk_integrity
*bi_mddev
= blk_get_integrity(mddev
->gendisk
);
1819 if (!bi_mddev
) /* nothing to do */
1821 if (rdev
->raid_disk
< 0) /* skip spares */
1823 if (bi_rdev
&& blk_integrity_compare(mddev
->gendisk
,
1824 rdev
->bdev
->bd_disk
) >= 0)
1826 printk(KERN_NOTICE
"disabling data integrity on %s\n", mdname(mddev
));
1827 blk_integrity_unregister(mddev
->gendisk
);
1829 EXPORT_SYMBOL(md_integrity_add_rdev
);
1831 static int bind_rdev_to_array(mdk_rdev_t
* rdev
, mddev_t
* mddev
)
1833 char b
[BDEVNAME_SIZE
];
1843 /* prevent duplicates */
1844 if (find_rdev(mddev
, rdev
->bdev
->bd_dev
))
1847 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1848 if (rdev
->sectors
&& (mddev
->dev_sectors
== 0 ||
1849 rdev
->sectors
< mddev
->dev_sectors
)) {
1851 /* Cannot change size, so fail
1852 * If mddev->level <= 0, then we don't care
1853 * about aligning sizes (e.g. linear)
1855 if (mddev
->level
> 0)
1858 mddev
->dev_sectors
= rdev
->sectors
;
1861 /* Verify rdev->desc_nr is unique.
1862 * If it is -1, assign a free number, else
1863 * check number is not in use
1865 if (rdev
->desc_nr
< 0) {
1867 if (mddev
->pers
) choice
= mddev
->raid_disks
;
1868 while (find_rdev_nr(mddev
, choice
))
1870 rdev
->desc_nr
= choice
;
1872 if (find_rdev_nr(mddev
, rdev
->desc_nr
))
1875 if (mddev
->max_disks
&& rdev
->desc_nr
>= mddev
->max_disks
) {
1876 printk(KERN_WARNING
"md: %s: array is limited to %d devices\n",
1877 mdname(mddev
), mddev
->max_disks
);
1880 bdevname(rdev
->bdev
,b
);
1881 while ( (s
=strchr(b
, '/')) != NULL
)
1884 rdev
->mddev
= mddev
;
1885 printk(KERN_INFO
"md: bind<%s>\n", b
);
1887 if ((err
= kobject_add(&rdev
->kobj
, &mddev
->kobj
, "dev-%s", b
)))
1890 ko
= &part_to_dev(rdev
->bdev
->bd_part
)->kobj
;
1891 if (sysfs_create_link(&rdev
->kobj
, ko
, "block"))
1892 /* failure here is OK */;
1893 rdev
->sysfs_state
= sysfs_get_dirent_safe(rdev
->kobj
.sd
, "state");
1895 list_add_rcu(&rdev
->same_set
, &mddev
->disks
);
1896 bd_link_disk_holder(rdev
->bdev
, mddev
->gendisk
);
1898 /* May as well allow recovery to be retried once */
1899 mddev
->recovery_disabled
= 0;
1904 printk(KERN_WARNING
"md: failed to register dev-%s for %s\n",
1909 static void md_delayed_delete(struct work_struct
*ws
)
1911 mdk_rdev_t
*rdev
= container_of(ws
, mdk_rdev_t
, del_work
);
1912 kobject_del(&rdev
->kobj
);
1913 kobject_put(&rdev
->kobj
);
1916 static void unbind_rdev_from_array(mdk_rdev_t
* rdev
)
1918 char b
[BDEVNAME_SIZE
];
1923 bd_unlink_disk_holder(rdev
->bdev
, rdev
->mddev
->gendisk
);
1924 list_del_rcu(&rdev
->same_set
);
1925 printk(KERN_INFO
"md: unbind<%s>\n", bdevname(rdev
->bdev
,b
));
1927 sysfs_remove_link(&rdev
->kobj
, "block");
1928 sysfs_put(rdev
->sysfs_state
);
1929 rdev
->sysfs_state
= NULL
;
1930 /* We need to delay this, otherwise we can deadlock when
1931 * writing to 'remove' to "dev/state". We also need
1932 * to delay it due to rcu usage.
1935 INIT_WORK(&rdev
->del_work
, md_delayed_delete
);
1936 kobject_get(&rdev
->kobj
);
1937 queue_work(md_misc_wq
, &rdev
->del_work
);
1941 * prevent the device from being mounted, repartitioned or
1942 * otherwise reused by a RAID array (or any other kernel
1943 * subsystem), by bd_claiming the device.
1945 static int lock_rdev(mdk_rdev_t
*rdev
, dev_t dev
, int shared
)
1948 struct block_device
*bdev
;
1949 char b
[BDEVNAME_SIZE
];
1951 bdev
= blkdev_get_by_dev(dev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
,
1952 shared
? (mdk_rdev_t
*)lock_rdev
: rdev
);
1954 printk(KERN_ERR
"md: could not open %s.\n",
1955 __bdevname(dev
, b
));
1956 return PTR_ERR(bdev
);
1962 static void unlock_rdev(mdk_rdev_t
*rdev
)
1964 struct block_device
*bdev
= rdev
->bdev
;
1968 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
);
1971 void md_autodetect_dev(dev_t dev
);
1973 static void export_rdev(mdk_rdev_t
* rdev
)
1975 char b
[BDEVNAME_SIZE
];
1976 printk(KERN_INFO
"md: export_rdev(%s)\n",
1977 bdevname(rdev
->bdev
,b
));
1982 if (test_bit(AutoDetected
, &rdev
->flags
))
1983 md_autodetect_dev(rdev
->bdev
->bd_dev
);
1986 kobject_put(&rdev
->kobj
);
1989 static void kick_rdev_from_array(mdk_rdev_t
* rdev
)
1991 unbind_rdev_from_array(rdev
);
1995 static void export_array(mddev_t
*mddev
)
1997 mdk_rdev_t
*rdev
, *tmp
;
1999 rdev_for_each(rdev
, tmp
, mddev
) {
2004 kick_rdev_from_array(rdev
);
2006 if (!list_empty(&mddev
->disks
))
2008 mddev
->raid_disks
= 0;
2009 mddev
->major_version
= 0;
2012 static void print_desc(mdp_disk_t
*desc
)
2014 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc
->number
,
2015 desc
->major
,desc
->minor
,desc
->raid_disk
,desc
->state
);
2018 static void print_sb_90(mdp_super_t
*sb
)
2023 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2024 sb
->major_version
, sb
->minor_version
, sb
->patch_version
,
2025 sb
->set_uuid0
, sb
->set_uuid1
, sb
->set_uuid2
, sb
->set_uuid3
,
2027 printk(KERN_INFO
"md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2028 sb
->level
, sb
->size
, sb
->nr_disks
, sb
->raid_disks
,
2029 sb
->md_minor
, sb
->layout
, sb
->chunk_size
);
2030 printk(KERN_INFO
"md: UT:%08x ST:%d AD:%d WD:%d"
2031 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2032 sb
->utime
, sb
->state
, sb
->active_disks
, sb
->working_disks
,
2033 sb
->failed_disks
, sb
->spare_disks
,
2034 sb
->sb_csum
, (unsigned long)sb
->events_lo
);
2037 for (i
= 0; i
< MD_SB_DISKS
; i
++) {
2040 desc
= sb
->disks
+ i
;
2041 if (desc
->number
|| desc
->major
|| desc
->minor
||
2042 desc
->raid_disk
|| (desc
->state
&& (desc
->state
!= 4))) {
2043 printk(" D %2d: ", i
);
2047 printk(KERN_INFO
"md: THIS: ");
2048 print_desc(&sb
->this_disk
);
2051 static void print_sb_1(struct mdp_superblock_1
*sb
)
2055 uuid
= sb
->set_uuid
;
2057 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
2058 "md: Name: \"%s\" CT:%llu\n",
2059 le32_to_cpu(sb
->major_version
),
2060 le32_to_cpu(sb
->feature_map
),
2063 (unsigned long long)le64_to_cpu(sb
->ctime
)
2064 & MD_SUPERBLOCK_1_TIME_SEC_MASK
);
2066 uuid
= sb
->device_uuid
;
2068 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
2070 "md: Dev:%08x UUID: %pU\n"
2071 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2072 "md: (MaxDev:%u) \n",
2073 le32_to_cpu(sb
->level
),
2074 (unsigned long long)le64_to_cpu(sb
->size
),
2075 le32_to_cpu(sb
->raid_disks
),
2076 le32_to_cpu(sb
->layout
),
2077 le32_to_cpu(sb
->chunksize
),
2078 (unsigned long long)le64_to_cpu(sb
->data_offset
),
2079 (unsigned long long)le64_to_cpu(sb
->data_size
),
2080 (unsigned long long)le64_to_cpu(sb
->super_offset
),
2081 (unsigned long long)le64_to_cpu(sb
->recovery_offset
),
2082 le32_to_cpu(sb
->dev_number
),
2085 (unsigned long long)le64_to_cpu(sb
->utime
) & MD_SUPERBLOCK_1_TIME_SEC_MASK
,
2086 (unsigned long long)le64_to_cpu(sb
->events
),
2087 (unsigned long long)le64_to_cpu(sb
->resync_offset
),
2088 le32_to_cpu(sb
->sb_csum
),
2089 le32_to_cpu(sb
->max_dev
)
2093 static void print_rdev(mdk_rdev_t
*rdev
, int major_version
)
2095 char b
[BDEVNAME_SIZE
];
2096 printk(KERN_INFO
"md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2097 bdevname(rdev
->bdev
, b
), (unsigned long long)rdev
->sectors
,
2098 test_bit(Faulty
, &rdev
->flags
), test_bit(In_sync
, &rdev
->flags
),
2100 if (rdev
->sb_loaded
) {
2101 printk(KERN_INFO
"md: rdev superblock (MJ:%d):\n", major_version
);
2102 switch (major_version
) {
2104 print_sb_90((mdp_super_t
*)page_address(rdev
->sb_page
));
2107 print_sb_1((struct mdp_superblock_1
*)page_address(rdev
->sb_page
));
2111 printk(KERN_INFO
"md: no rdev superblock!\n");
2114 static void md_print_devices(void)
2116 struct list_head
*tmp
;
2119 char b
[BDEVNAME_SIZE
];
2122 printk("md: **********************************\n");
2123 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2124 printk("md: **********************************\n");
2125 for_each_mddev(mddev
, tmp
) {
2128 bitmap_print_sb(mddev
->bitmap
);
2130 printk("%s: ", mdname(mddev
));
2131 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
2132 printk("<%s>", bdevname(rdev
->bdev
,b
));
2135 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
2136 print_rdev(rdev
, mddev
->major_version
);
2138 printk("md: **********************************\n");
2143 static void sync_sbs(mddev_t
* mddev
, int nospares
)
2145 /* Update each superblock (in-memory image), but
2146 * if we are allowed to, skip spares which already
2147 * have the right event counter, or have one earlier
2148 * (which would mean they aren't being marked as dirty
2149 * with the rest of the array)
2152 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2153 if (rdev
->sb_events
== mddev
->events
||
2155 rdev
->raid_disk
< 0 &&
2156 rdev
->sb_events
+1 == mddev
->events
)) {
2157 /* Don't update this superblock */
2158 rdev
->sb_loaded
= 2;
2160 super_types
[mddev
->major_version
].
2161 sync_super(mddev
, rdev
);
2162 rdev
->sb_loaded
= 1;
2167 static void md_update_sb(mddev_t
* mddev
, int force_change
)
2174 /* First make sure individual recovery_offsets are correct */
2175 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2176 if (rdev
->raid_disk
>= 0 &&
2177 mddev
->delta_disks
>= 0 &&
2178 !test_bit(In_sync
, &rdev
->flags
) &&
2179 mddev
->curr_resync_completed
> rdev
->recovery_offset
)
2180 rdev
->recovery_offset
= mddev
->curr_resync_completed
;
2183 if (!mddev
->persistent
) {
2184 clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
2185 clear_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
2186 if (!mddev
->external
)
2187 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
2188 wake_up(&mddev
->sb_wait
);
2192 spin_lock_irq(&mddev
->write_lock
);
2194 mddev
->utime
= get_seconds();
2196 if (test_and_clear_bit(MD_CHANGE_DEVS
, &mddev
->flags
))
2198 if (test_and_clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
))
2199 /* just a clean<-> dirty transition, possibly leave spares alone,
2200 * though if events isn't the right even/odd, we will have to do
2206 if (mddev
->degraded
)
2207 /* If the array is degraded, then skipping spares is both
2208 * dangerous and fairly pointless.
2209 * Dangerous because a device that was removed from the array
2210 * might have a event_count that still looks up-to-date,
2211 * so it can be re-added without a resync.
2212 * Pointless because if there are any spares to skip,
2213 * then a recovery will happen and soon that array won't
2214 * be degraded any more and the spare can go back to sleep then.
2218 sync_req
= mddev
->in_sync
;
2220 /* If this is just a dirty<->clean transition, and the array is clean
2221 * and 'events' is odd, we can roll back to the previous clean state */
2223 && (mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
2224 && mddev
->can_decrease_events
2225 && mddev
->events
!= 1) {
2227 mddev
->can_decrease_events
= 0;
2229 /* otherwise we have to go forward and ... */
2231 mddev
->can_decrease_events
= nospares
;
2234 if (!mddev
->events
) {
2236 * oops, this 64-bit counter should never wrap.
2237 * Either we are in around ~1 trillion A.C., assuming
2238 * 1 reboot per second, or we have a bug:
2243 sync_sbs(mddev
, nospares
);
2244 spin_unlock_irq(&mddev
->write_lock
);
2247 "md: updating %s RAID superblock on device (in sync %d)\n",
2248 mdname(mddev
),mddev
->in_sync
);
2250 bitmap_update_sb(mddev
->bitmap
);
2251 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2252 char b
[BDEVNAME_SIZE
];
2253 dprintk(KERN_INFO
"md: ");
2254 if (rdev
->sb_loaded
!= 1)
2255 continue; /* no noise on spare devices */
2256 if (test_bit(Faulty
, &rdev
->flags
))
2257 dprintk("(skipping faulty ");
2259 dprintk("%s ", bdevname(rdev
->bdev
,b
));
2260 if (!test_bit(Faulty
, &rdev
->flags
)) {
2261 md_super_write(mddev
,rdev
,
2262 rdev
->sb_start
, rdev
->sb_size
,
2264 dprintk(KERN_INFO
"(write) %s's sb offset: %llu\n",
2265 bdevname(rdev
->bdev
,b
),
2266 (unsigned long long)rdev
->sb_start
);
2267 rdev
->sb_events
= mddev
->events
;
2271 if (mddev
->level
== LEVEL_MULTIPATH
)
2272 /* only need to write one superblock... */
2275 md_super_wait(mddev
);
2276 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
2278 spin_lock_irq(&mddev
->write_lock
);
2279 if (mddev
->in_sync
!= sync_req
||
2280 test_bit(MD_CHANGE_DEVS
, &mddev
->flags
)) {
2281 /* have to write it out again */
2282 spin_unlock_irq(&mddev
->write_lock
);
2285 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
2286 spin_unlock_irq(&mddev
->write_lock
);
2287 wake_up(&mddev
->sb_wait
);
2288 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
2289 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
2293 /* words written to sysfs files may, or may not, be \n terminated.
2294 * We want to accept with case. For this we use cmd_match.
2296 static int cmd_match(const char *cmd
, const char *str
)
2298 /* See if cmd, written into a sysfs file, matches
2299 * str. They must either be the same, or cmd can
2300 * have a trailing newline
2302 while (*cmd
&& *str
&& *cmd
== *str
) {
2313 struct rdev_sysfs_entry
{
2314 struct attribute attr
;
2315 ssize_t (*show
)(mdk_rdev_t
*, char *);
2316 ssize_t (*store
)(mdk_rdev_t
*, const char *, size_t);
2320 state_show(mdk_rdev_t
*rdev
, char *page
)
2325 if (test_bit(Faulty
, &rdev
->flags
)) {
2326 len
+= sprintf(page
+len
, "%sfaulty",sep
);
2329 if (test_bit(In_sync
, &rdev
->flags
)) {
2330 len
+= sprintf(page
+len
, "%sin_sync",sep
);
2333 if (test_bit(WriteMostly
, &rdev
->flags
)) {
2334 len
+= sprintf(page
+len
, "%swrite_mostly",sep
);
2337 if (test_bit(Blocked
, &rdev
->flags
)) {
2338 len
+= sprintf(page
+len
, "%sblocked", sep
);
2341 if (!test_bit(Faulty
, &rdev
->flags
) &&
2342 !test_bit(In_sync
, &rdev
->flags
)) {
2343 len
+= sprintf(page
+len
, "%sspare", sep
);
2346 return len
+sprintf(page
+len
, "\n");
2350 state_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2353 * faulty - simulates and error
2354 * remove - disconnects the device
2355 * writemostly - sets write_mostly
2356 * -writemostly - clears write_mostly
2357 * blocked - sets the Blocked flag
2358 * -blocked - clears the Blocked flag
2359 * insync - sets Insync providing device isn't active
2362 if (cmd_match(buf
, "faulty") && rdev
->mddev
->pers
) {
2363 md_error(rdev
->mddev
, rdev
);
2365 } else if (cmd_match(buf
, "remove")) {
2366 if (rdev
->raid_disk
>= 0)
2369 mddev_t
*mddev
= rdev
->mddev
;
2370 kick_rdev_from_array(rdev
);
2372 md_update_sb(mddev
, 1);
2373 md_new_event(mddev
);
2376 } else if (cmd_match(buf
, "writemostly")) {
2377 set_bit(WriteMostly
, &rdev
->flags
);
2379 } else if (cmd_match(buf
, "-writemostly")) {
2380 clear_bit(WriteMostly
, &rdev
->flags
);
2382 } else if (cmd_match(buf
, "blocked")) {
2383 set_bit(Blocked
, &rdev
->flags
);
2385 } else if (cmd_match(buf
, "-blocked")) {
2386 clear_bit(Blocked
, &rdev
->flags
);
2387 wake_up(&rdev
->blocked_wait
);
2388 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2389 md_wakeup_thread(rdev
->mddev
->thread
);
2392 } else if (cmd_match(buf
, "insync") && rdev
->raid_disk
== -1) {
2393 set_bit(In_sync
, &rdev
->flags
);
2397 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2398 return err
? err
: len
;
2400 static struct rdev_sysfs_entry rdev_state
=
2401 __ATTR(state
, S_IRUGO
|S_IWUSR
, state_show
, state_store
);
2404 errors_show(mdk_rdev_t
*rdev
, char *page
)
2406 return sprintf(page
, "%d\n", atomic_read(&rdev
->corrected_errors
));
2410 errors_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2413 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2414 if (*buf
&& (*e
== 0 || *e
== '\n')) {
2415 atomic_set(&rdev
->corrected_errors
, n
);
2420 static struct rdev_sysfs_entry rdev_errors
=
2421 __ATTR(errors
, S_IRUGO
|S_IWUSR
, errors_show
, errors_store
);
2424 slot_show(mdk_rdev_t
*rdev
, char *page
)
2426 if (rdev
->raid_disk
< 0)
2427 return sprintf(page
, "none\n");
2429 return sprintf(page
, "%d\n", rdev
->raid_disk
);
2433 slot_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2438 int slot
= simple_strtoul(buf
, &e
, 10);
2439 if (strncmp(buf
, "none", 4)==0)
2441 else if (e
==buf
|| (*e
&& *e
!= '\n'))
2443 if (rdev
->mddev
->pers
&& slot
== -1) {
2444 /* Setting 'slot' on an active array requires also
2445 * updating the 'rd%d' link, and communicating
2446 * with the personality with ->hot_*_disk.
2447 * For now we only support removing
2448 * failed/spare devices. This normally happens automatically,
2449 * but not when the metadata is externally managed.
2451 if (rdev
->raid_disk
== -1)
2453 /* personality does all needed checks */
2454 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
2456 err
= rdev
->mddev
->pers
->
2457 hot_remove_disk(rdev
->mddev
, rdev
->raid_disk
);
2460 sprintf(nm
, "rd%d", rdev
->raid_disk
);
2461 sysfs_remove_link(&rdev
->mddev
->kobj
, nm
);
2462 rdev
->raid_disk
= -1;
2463 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2464 md_wakeup_thread(rdev
->mddev
->thread
);
2465 } else if (rdev
->mddev
->pers
) {
2467 /* Activating a spare .. or possibly reactivating
2468 * if we ever get bitmaps working here.
2471 if (rdev
->raid_disk
!= -1)
2474 if (test_bit(MD_RECOVERY_RUNNING
, &rdev
->mddev
->recovery
))
2477 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
2480 list_for_each_entry(rdev2
, &rdev
->mddev
->disks
, same_set
)
2481 if (rdev2
->raid_disk
== slot
)
2484 if (slot
>= rdev
->mddev
->raid_disks
&&
2485 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
2488 rdev
->raid_disk
= slot
;
2489 if (test_bit(In_sync
, &rdev
->flags
))
2490 rdev
->saved_raid_disk
= slot
;
2492 rdev
->saved_raid_disk
= -1;
2493 err
= rdev
->mddev
->pers
->
2494 hot_add_disk(rdev
->mddev
, rdev
);
2496 rdev
->raid_disk
= -1;
2499 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2500 sprintf(nm
, "rd%d", rdev
->raid_disk
);
2501 if (sysfs_create_link(&rdev
->mddev
->kobj
, &rdev
->kobj
, nm
))
2502 /* failure here is OK */;
2503 /* don't wakeup anyone, leave that to userspace. */
2505 if (slot
>= rdev
->mddev
->raid_disks
&&
2506 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
2508 rdev
->raid_disk
= slot
;
2509 /* assume it is working */
2510 clear_bit(Faulty
, &rdev
->flags
);
2511 clear_bit(WriteMostly
, &rdev
->flags
);
2512 set_bit(In_sync
, &rdev
->flags
);
2513 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2519 static struct rdev_sysfs_entry rdev_slot
=
2520 __ATTR(slot
, S_IRUGO
|S_IWUSR
, slot_show
, slot_store
);
2523 offset_show(mdk_rdev_t
*rdev
, char *page
)
2525 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->data_offset
);
2529 offset_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2532 unsigned long long offset
= simple_strtoull(buf
, &e
, 10);
2533 if (e
==buf
|| (*e
&& *e
!= '\n'))
2535 if (rdev
->mddev
->pers
&& rdev
->raid_disk
>= 0)
2537 if (rdev
->sectors
&& rdev
->mddev
->external
)
2538 /* Must set offset before size, so overlap checks
2541 rdev
->data_offset
= offset
;
2545 static struct rdev_sysfs_entry rdev_offset
=
2546 __ATTR(offset
, S_IRUGO
|S_IWUSR
, offset_show
, offset_store
);
2549 rdev_size_show(mdk_rdev_t
*rdev
, char *page
)
2551 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->sectors
/ 2);
2554 static int overlaps(sector_t s1
, sector_t l1
, sector_t s2
, sector_t l2
)
2556 /* check if two start/length pairs overlap */
2564 static int strict_blocks_to_sectors(const char *buf
, sector_t
*sectors
)
2566 unsigned long long blocks
;
2569 if (strict_strtoull(buf
, 10, &blocks
) < 0)
2572 if (blocks
& 1ULL << (8 * sizeof(blocks
) - 1))
2573 return -EINVAL
; /* sector conversion overflow */
2576 if (new != blocks
* 2)
2577 return -EINVAL
; /* unsigned long long to sector_t overflow */
2584 rdev_size_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2586 mddev_t
*my_mddev
= rdev
->mddev
;
2587 sector_t oldsectors
= rdev
->sectors
;
2590 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
2592 if (my_mddev
->pers
&& rdev
->raid_disk
>= 0) {
2593 if (my_mddev
->persistent
) {
2594 sectors
= super_types
[my_mddev
->major_version
].
2595 rdev_size_change(rdev
, sectors
);
2598 } else if (!sectors
)
2599 sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) -
2602 if (sectors
< my_mddev
->dev_sectors
)
2603 return -EINVAL
; /* component must fit device */
2605 rdev
->sectors
= sectors
;
2606 if (sectors
> oldsectors
&& my_mddev
->external
) {
2607 /* need to check that all other rdevs with the same ->bdev
2608 * do not overlap. We need to unlock the mddev to avoid
2609 * a deadlock. We have already changed rdev->sectors, and if
2610 * we have to change it back, we will have the lock again.
2614 struct list_head
*tmp
;
2616 mddev_unlock(my_mddev
);
2617 for_each_mddev(mddev
, tmp
) {
2621 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
)
2622 if (rdev
->bdev
== rdev2
->bdev
&&
2624 overlaps(rdev
->data_offset
, rdev
->sectors
,
2630 mddev_unlock(mddev
);
2636 mddev_lock(my_mddev
);
2638 /* Someone else could have slipped in a size
2639 * change here, but doing so is just silly.
2640 * We put oldsectors back because we *know* it is
2641 * safe, and trust userspace not to race with
2644 rdev
->sectors
= oldsectors
;
2651 static struct rdev_sysfs_entry rdev_size
=
2652 __ATTR(size
, S_IRUGO
|S_IWUSR
, rdev_size_show
, rdev_size_store
);
2655 static ssize_t
recovery_start_show(mdk_rdev_t
*rdev
, char *page
)
2657 unsigned long long recovery_start
= rdev
->recovery_offset
;
2659 if (test_bit(In_sync
, &rdev
->flags
) ||
2660 recovery_start
== MaxSector
)
2661 return sprintf(page
, "none\n");
2663 return sprintf(page
, "%llu\n", recovery_start
);
2666 static ssize_t
recovery_start_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2668 unsigned long long recovery_start
;
2670 if (cmd_match(buf
, "none"))
2671 recovery_start
= MaxSector
;
2672 else if (strict_strtoull(buf
, 10, &recovery_start
))
2675 if (rdev
->mddev
->pers
&&
2676 rdev
->raid_disk
>= 0)
2679 rdev
->recovery_offset
= recovery_start
;
2680 if (recovery_start
== MaxSector
)
2681 set_bit(In_sync
, &rdev
->flags
);
2683 clear_bit(In_sync
, &rdev
->flags
);
2687 static struct rdev_sysfs_entry rdev_recovery_start
=
2688 __ATTR(recovery_start
, S_IRUGO
|S_IWUSR
, recovery_start_show
, recovery_start_store
);
2690 static struct attribute
*rdev_default_attrs
[] = {
2696 &rdev_recovery_start
.attr
,
2700 rdev_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
2702 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
2703 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
2704 mddev_t
*mddev
= rdev
->mddev
;
2710 rv
= mddev
? mddev_lock(mddev
) : -EBUSY
;
2712 if (rdev
->mddev
== NULL
)
2715 rv
= entry
->show(rdev
, page
);
2716 mddev_unlock(mddev
);
2722 rdev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
2723 const char *page
, size_t length
)
2725 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
2726 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
2728 mddev_t
*mddev
= rdev
->mddev
;
2732 if (!capable(CAP_SYS_ADMIN
))
2734 rv
= mddev
? mddev_lock(mddev
): -EBUSY
;
2736 if (rdev
->mddev
== NULL
)
2739 rv
= entry
->store(rdev
, page
, length
);
2740 mddev_unlock(mddev
);
2745 static void rdev_free(struct kobject
*ko
)
2747 mdk_rdev_t
*rdev
= container_of(ko
, mdk_rdev_t
, kobj
);
2750 static const struct sysfs_ops rdev_sysfs_ops
= {
2751 .show
= rdev_attr_show
,
2752 .store
= rdev_attr_store
,
2754 static struct kobj_type rdev_ktype
= {
2755 .release
= rdev_free
,
2756 .sysfs_ops
= &rdev_sysfs_ops
,
2757 .default_attrs
= rdev_default_attrs
,
2760 void md_rdev_init(mdk_rdev_t
*rdev
)
2763 rdev
->saved_raid_disk
= -1;
2764 rdev
->raid_disk
= -1;
2766 rdev
->data_offset
= 0;
2767 rdev
->sb_events
= 0;
2768 rdev
->last_read_error
.tv_sec
= 0;
2769 rdev
->last_read_error
.tv_nsec
= 0;
2770 atomic_set(&rdev
->nr_pending
, 0);
2771 atomic_set(&rdev
->read_errors
, 0);
2772 atomic_set(&rdev
->corrected_errors
, 0);
2774 INIT_LIST_HEAD(&rdev
->same_set
);
2775 init_waitqueue_head(&rdev
->blocked_wait
);
2777 EXPORT_SYMBOL_GPL(md_rdev_init
);
2779 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2781 * mark the device faulty if:
2783 * - the device is nonexistent (zero size)
2784 * - the device has no valid superblock
2786 * a faulty rdev _never_ has rdev->sb set.
2788 static mdk_rdev_t
*md_import_device(dev_t newdev
, int super_format
, int super_minor
)
2790 char b
[BDEVNAME_SIZE
];
2795 rdev
= kzalloc(sizeof(*rdev
), GFP_KERNEL
);
2797 printk(KERN_ERR
"md: could not alloc mem for new device!\n");
2798 return ERR_PTR(-ENOMEM
);
2802 if ((err
= alloc_disk_sb(rdev
)))
2805 err
= lock_rdev(rdev
, newdev
, super_format
== -2);
2809 kobject_init(&rdev
->kobj
, &rdev_ktype
);
2811 size
= i_size_read(rdev
->bdev
->bd_inode
) >> BLOCK_SIZE_BITS
;
2814 "md: %s has zero or unknown size, marking faulty!\n",
2815 bdevname(rdev
->bdev
,b
));
2820 if (super_format
>= 0) {
2821 err
= super_types
[super_format
].
2822 load_super(rdev
, NULL
, super_minor
);
2823 if (err
== -EINVAL
) {
2825 "md: %s does not have a valid v%d.%d "
2826 "superblock, not importing!\n",
2827 bdevname(rdev
->bdev
,b
),
2828 super_format
, super_minor
);
2833 "md: could not read %s's sb, not importing!\n",
2834 bdevname(rdev
->bdev
,b
));
2842 if (rdev
->sb_page
) {
2848 return ERR_PTR(err
);
2852 * Check a full RAID array for plausibility
2856 static void analyze_sbs(mddev_t
* mddev
)
2859 mdk_rdev_t
*rdev
, *freshest
, *tmp
;
2860 char b
[BDEVNAME_SIZE
];
2863 rdev_for_each(rdev
, tmp
, mddev
)
2864 switch (super_types
[mddev
->major_version
].
2865 load_super(rdev
, freshest
, mddev
->minor_version
)) {
2873 "md: fatal superblock inconsistency in %s"
2874 " -- removing from array\n",
2875 bdevname(rdev
->bdev
,b
));
2876 kick_rdev_from_array(rdev
);
2880 super_types
[mddev
->major_version
].
2881 validate_super(mddev
, freshest
);
2884 rdev_for_each(rdev
, tmp
, mddev
) {
2885 if (mddev
->max_disks
&&
2886 (rdev
->desc_nr
>= mddev
->max_disks
||
2887 i
> mddev
->max_disks
)) {
2889 "md: %s: %s: only %d devices permitted\n",
2890 mdname(mddev
), bdevname(rdev
->bdev
, b
),
2892 kick_rdev_from_array(rdev
);
2895 if (rdev
!= freshest
)
2896 if (super_types
[mddev
->major_version
].
2897 validate_super(mddev
, rdev
)) {
2898 printk(KERN_WARNING
"md: kicking non-fresh %s"
2900 bdevname(rdev
->bdev
,b
));
2901 kick_rdev_from_array(rdev
);
2904 if (mddev
->level
== LEVEL_MULTIPATH
) {
2905 rdev
->desc_nr
= i
++;
2906 rdev
->raid_disk
= rdev
->desc_nr
;
2907 set_bit(In_sync
, &rdev
->flags
);
2908 } else if (rdev
->raid_disk
>= (mddev
->raid_disks
- min(0, mddev
->delta_disks
))) {
2909 rdev
->raid_disk
= -1;
2910 clear_bit(In_sync
, &rdev
->flags
);
2915 /* Read a fixed-point number.
2916 * Numbers in sysfs attributes should be in "standard" units where
2917 * possible, so time should be in seconds.
2918 * However we internally use a a much smaller unit such as
2919 * milliseconds or jiffies.
2920 * This function takes a decimal number with a possible fractional
2921 * component, and produces an integer which is the result of
2922 * multiplying that number by 10^'scale'.
2923 * all without any floating-point arithmetic.
2925 int strict_strtoul_scaled(const char *cp
, unsigned long *res
, int scale
)
2927 unsigned long result
= 0;
2929 while (isdigit(*cp
) || (*cp
== '.' && decimals
< 0)) {
2932 else if (decimals
< scale
) {
2935 result
= result
* 10 + value
;
2947 while (decimals
< scale
) {
2956 static void md_safemode_timeout(unsigned long data
);
2959 safe_delay_show(mddev_t
*mddev
, char *page
)
2961 int msec
= (mddev
->safemode_delay
*1000)/HZ
;
2962 return sprintf(page
, "%d.%03d\n", msec
/1000, msec
%1000);
2965 safe_delay_store(mddev_t
*mddev
, const char *cbuf
, size_t len
)
2969 if (strict_strtoul_scaled(cbuf
, &msec
, 3) < 0)
2972 mddev
->safemode_delay
= 0;
2974 unsigned long old_delay
= mddev
->safemode_delay
;
2975 mddev
->safemode_delay
= (msec
*HZ
)/1000;
2976 if (mddev
->safemode_delay
== 0)
2977 mddev
->safemode_delay
= 1;
2978 if (mddev
->safemode_delay
< old_delay
)
2979 md_safemode_timeout((unsigned long)mddev
);
2983 static struct md_sysfs_entry md_safe_delay
=
2984 __ATTR(safe_mode_delay
, S_IRUGO
|S_IWUSR
,safe_delay_show
, safe_delay_store
);
2987 level_show(mddev_t
*mddev
, char *page
)
2989 struct mdk_personality
*p
= mddev
->pers
;
2991 return sprintf(page
, "%s\n", p
->name
);
2992 else if (mddev
->clevel
[0])
2993 return sprintf(page
, "%s\n", mddev
->clevel
);
2994 else if (mddev
->level
!= LEVEL_NONE
)
2995 return sprintf(page
, "%d\n", mddev
->level
);
3001 level_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3005 struct mdk_personality
*pers
;
3010 if (mddev
->pers
== NULL
) {
3013 if (len
>= sizeof(mddev
->clevel
))
3015 strncpy(mddev
->clevel
, buf
, len
);
3016 if (mddev
->clevel
[len
-1] == '\n')
3018 mddev
->clevel
[len
] = 0;
3019 mddev
->level
= LEVEL_NONE
;
3023 /* request to change the personality. Need to ensure:
3024 * - array is not engaged in resync/recovery/reshape
3025 * - old personality can be suspended
3026 * - new personality will access other array.
3029 if (mddev
->sync_thread
||
3030 mddev
->reshape_position
!= MaxSector
||
3031 mddev
->sysfs_active
)
3034 if (!mddev
->pers
->quiesce
) {
3035 printk(KERN_WARNING
"md: %s: %s does not support online personality change\n",
3036 mdname(mddev
), mddev
->pers
->name
);
3040 /* Now find the new personality */
3041 if (len
== 0 || len
>= sizeof(clevel
))
3043 strncpy(clevel
, buf
, len
);
3044 if (clevel
[len
-1] == '\n')
3047 if (strict_strtol(clevel
, 10, &level
))
3050 if (request_module("md-%s", clevel
) != 0)
3051 request_module("md-level-%s", clevel
);
3052 spin_lock(&pers_lock
);
3053 pers
= find_pers(level
, clevel
);
3054 if (!pers
|| !try_module_get(pers
->owner
)) {
3055 spin_unlock(&pers_lock
);
3056 printk(KERN_WARNING
"md: personality %s not loaded\n", clevel
);
3059 spin_unlock(&pers_lock
);
3061 if (pers
== mddev
->pers
) {
3062 /* Nothing to do! */
3063 module_put(pers
->owner
);
3066 if (!pers
->takeover
) {
3067 module_put(pers
->owner
);
3068 printk(KERN_WARNING
"md: %s: %s does not support personality takeover\n",
3069 mdname(mddev
), clevel
);
3073 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
3074 rdev
->new_raid_disk
= rdev
->raid_disk
;
3076 /* ->takeover must set new_* and/or delta_disks
3077 * if it succeeds, and may set them when it fails.
3079 priv
= pers
->takeover(mddev
);
3081 mddev
->new_level
= mddev
->level
;
3082 mddev
->new_layout
= mddev
->layout
;
3083 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3084 mddev
->raid_disks
-= mddev
->delta_disks
;
3085 mddev
->delta_disks
= 0;
3086 module_put(pers
->owner
);
3087 printk(KERN_WARNING
"md: %s: %s would not accept array\n",
3088 mdname(mddev
), clevel
);
3089 return PTR_ERR(priv
);
3092 /* Looks like we have a winner */
3093 mddev_suspend(mddev
);
3094 mddev
->pers
->stop(mddev
);
3096 if (mddev
->pers
->sync_request
== NULL
&&
3097 pers
->sync_request
!= NULL
) {
3098 /* need to add the md_redundancy_group */
3099 if (sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
3101 "md: cannot register extra attributes for %s\n",
3103 mddev
->sysfs_action
= sysfs_get_dirent(mddev
->kobj
.sd
, NULL
, "sync_action");
3105 if (mddev
->pers
->sync_request
!= NULL
&&
3106 pers
->sync_request
== NULL
) {
3107 /* need to remove the md_redundancy_group */
3108 if (mddev
->to_remove
== NULL
)
3109 mddev
->to_remove
= &md_redundancy_group
;
3112 if (mddev
->pers
->sync_request
== NULL
&&
3114 /* We are converting from a no-redundancy array
3115 * to a redundancy array and metadata is managed
3116 * externally so we need to be sure that writes
3117 * won't block due to a need to transition
3119 * until external management is started.
3122 mddev
->safemode_delay
= 0;
3123 mddev
->safemode
= 0;
3126 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
3128 if (rdev
->raid_disk
< 0)
3130 if (rdev
->new_raid_disk
>= mddev
->raid_disks
)
3131 rdev
->new_raid_disk
= -1;
3132 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3134 sprintf(nm
, "rd%d", rdev
->raid_disk
);
3135 sysfs_remove_link(&mddev
->kobj
, nm
);
3137 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
3138 if (rdev
->raid_disk
< 0)
3140 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3142 rdev
->raid_disk
= rdev
->new_raid_disk
;
3143 if (rdev
->raid_disk
< 0)
3144 clear_bit(In_sync
, &rdev
->flags
);
3147 sprintf(nm
, "rd%d", rdev
->raid_disk
);
3148 if(sysfs_create_link(&mddev
->kobj
, &rdev
->kobj
, nm
))
3149 printk("md: cannot register %s for %s after level change\n",
3154 module_put(mddev
->pers
->owner
);
3156 mddev
->private = priv
;
3157 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
3158 mddev
->level
= mddev
->new_level
;
3159 mddev
->layout
= mddev
->new_layout
;
3160 mddev
->chunk_sectors
= mddev
->new_chunk_sectors
;
3161 mddev
->delta_disks
= 0;
3162 if (mddev
->pers
->sync_request
== NULL
) {
3163 /* this is now an array without redundancy, so
3164 * it must always be in_sync
3167 del_timer_sync(&mddev
->safemode_timer
);
3170 mddev_resume(mddev
);
3171 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
3172 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3173 md_wakeup_thread(mddev
->thread
);
3174 sysfs_notify(&mddev
->kobj
, NULL
, "level");
3175 md_new_event(mddev
);
3179 static struct md_sysfs_entry md_level
=
3180 __ATTR(level
, S_IRUGO
|S_IWUSR
, level_show
, level_store
);
3184 layout_show(mddev_t
*mddev
, char *page
)
3186 /* just a number, not meaningful for all levels */
3187 if (mddev
->reshape_position
!= MaxSector
&&
3188 mddev
->layout
!= mddev
->new_layout
)
3189 return sprintf(page
, "%d (%d)\n",
3190 mddev
->new_layout
, mddev
->layout
);
3191 return sprintf(page
, "%d\n", mddev
->layout
);
3195 layout_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3198 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3200 if (!*buf
|| (*e
&& *e
!= '\n'))
3205 if (mddev
->pers
->check_reshape
== NULL
)
3207 mddev
->new_layout
= n
;
3208 err
= mddev
->pers
->check_reshape(mddev
);
3210 mddev
->new_layout
= mddev
->layout
;
3214 mddev
->new_layout
= n
;
3215 if (mddev
->reshape_position
== MaxSector
)
3220 static struct md_sysfs_entry md_layout
=
3221 __ATTR(layout
, S_IRUGO
|S_IWUSR
, layout_show
, layout_store
);
3225 raid_disks_show(mddev_t
*mddev
, char *page
)
3227 if (mddev
->raid_disks
== 0)
3229 if (mddev
->reshape_position
!= MaxSector
&&
3230 mddev
->delta_disks
!= 0)
3231 return sprintf(page
, "%d (%d)\n", mddev
->raid_disks
,
3232 mddev
->raid_disks
- mddev
->delta_disks
);
3233 return sprintf(page
, "%d\n", mddev
->raid_disks
);
3236 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
);
3239 raid_disks_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3243 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3245 if (!*buf
|| (*e
&& *e
!= '\n'))
3249 rv
= update_raid_disks(mddev
, n
);
3250 else if (mddev
->reshape_position
!= MaxSector
) {
3251 int olddisks
= mddev
->raid_disks
- mddev
->delta_disks
;
3252 mddev
->delta_disks
= n
- olddisks
;
3253 mddev
->raid_disks
= n
;
3255 mddev
->raid_disks
= n
;
3256 return rv
? rv
: len
;
3258 static struct md_sysfs_entry md_raid_disks
=
3259 __ATTR(raid_disks
, S_IRUGO
|S_IWUSR
, raid_disks_show
, raid_disks_store
);
3262 chunk_size_show(mddev_t
*mddev
, char *page
)
3264 if (mddev
->reshape_position
!= MaxSector
&&
3265 mddev
->chunk_sectors
!= mddev
->new_chunk_sectors
)
3266 return sprintf(page
, "%d (%d)\n",
3267 mddev
->new_chunk_sectors
<< 9,
3268 mddev
->chunk_sectors
<< 9);
3269 return sprintf(page
, "%d\n", mddev
->chunk_sectors
<< 9);
3273 chunk_size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3276 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3278 if (!*buf
|| (*e
&& *e
!= '\n'))
3283 if (mddev
->pers
->check_reshape
== NULL
)
3285 mddev
->new_chunk_sectors
= n
>> 9;
3286 err
= mddev
->pers
->check_reshape(mddev
);
3288 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3292 mddev
->new_chunk_sectors
= n
>> 9;
3293 if (mddev
->reshape_position
== MaxSector
)
3294 mddev
->chunk_sectors
= n
>> 9;
3298 static struct md_sysfs_entry md_chunk_size
=
3299 __ATTR(chunk_size
, S_IRUGO
|S_IWUSR
, chunk_size_show
, chunk_size_store
);
3302 resync_start_show(mddev_t
*mddev
, char *page
)
3304 if (mddev
->recovery_cp
== MaxSector
)
3305 return sprintf(page
, "none\n");
3306 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->recovery_cp
);
3310 resync_start_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3313 unsigned long long n
= simple_strtoull(buf
, &e
, 10);
3317 if (cmd_match(buf
, "none"))
3319 else if (!*buf
|| (*e
&& *e
!= '\n'))
3322 mddev
->recovery_cp
= n
;
3325 static struct md_sysfs_entry md_resync_start
=
3326 __ATTR(resync_start
, S_IRUGO
|S_IWUSR
, resync_start_show
, resync_start_store
);
3329 * The array state can be:
3332 * No devices, no size, no level
3333 * Equivalent to STOP_ARRAY ioctl
3335 * May have some settings, but array is not active
3336 * all IO results in error
3337 * When written, doesn't tear down array, but just stops it
3338 * suspended (not supported yet)
3339 * All IO requests will block. The array can be reconfigured.
3340 * Writing this, if accepted, will block until array is quiescent
3342 * no resync can happen. no superblocks get written.
3343 * write requests fail
3345 * like readonly, but behaves like 'clean' on a write request.
3347 * clean - no pending writes, but otherwise active.
3348 * When written to inactive array, starts without resync
3349 * If a write request arrives then
3350 * if metadata is known, mark 'dirty' and switch to 'active'.
3351 * if not known, block and switch to write-pending
3352 * If written to an active array that has pending writes, then fails.
3354 * fully active: IO and resync can be happening.
3355 * When written to inactive array, starts with resync
3358 * clean, but writes are blocked waiting for 'active' to be written.
3361 * like active, but no writes have been seen for a while (100msec).
3364 enum array_state
{ clear
, inactive
, suspended
, readonly
, read_auto
, clean
, active
,
3365 write_pending
, active_idle
, bad_word
};
3366 static char *array_states
[] = {
3367 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3368 "write-pending", "active-idle", NULL
};
3370 static int match_word(const char *word
, char **list
)
3373 for (n
=0; list
[n
]; n
++)
3374 if (cmd_match(word
, list
[n
]))
3380 array_state_show(mddev_t
*mddev
, char *page
)
3382 enum array_state st
= inactive
;
3395 else if (test_bit(MD_CHANGE_PENDING
, &mddev
->flags
))
3397 else if (mddev
->safemode
)
3403 if (list_empty(&mddev
->disks
) &&
3404 mddev
->raid_disks
== 0 &&
3405 mddev
->dev_sectors
== 0)
3410 return sprintf(page
, "%s\n", array_states
[st
]);
3413 static int do_md_stop(mddev_t
* mddev
, int ro
, int is_open
);
3414 static int md_set_readonly(mddev_t
* mddev
, int is_open
);
3415 static int do_md_run(mddev_t
* mddev
);
3416 static int restart_array(mddev_t
*mddev
);
3419 array_state_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3422 enum array_state st
= match_word(buf
, array_states
);
3427 /* stopping an active array */
3428 if (atomic_read(&mddev
->openers
) > 0)
3430 err
= do_md_stop(mddev
, 0, 0);
3433 /* stopping an active array */
3435 if (atomic_read(&mddev
->openers
) > 0)
3437 err
= do_md_stop(mddev
, 2, 0);
3439 err
= 0; /* already inactive */
3442 break; /* not supported yet */
3445 err
= md_set_readonly(mddev
, 0);
3448 set_disk_ro(mddev
->gendisk
, 1);
3449 err
= do_md_run(mddev
);
3455 err
= md_set_readonly(mddev
, 0);
3456 else if (mddev
->ro
== 1)
3457 err
= restart_array(mddev
);
3460 set_disk_ro(mddev
->gendisk
, 0);
3464 err
= do_md_run(mddev
);
3469 restart_array(mddev
);
3470 spin_lock_irq(&mddev
->write_lock
);
3471 if (atomic_read(&mddev
->writes_pending
) == 0) {
3472 if (mddev
->in_sync
== 0) {
3474 if (mddev
->safemode
== 1)
3475 mddev
->safemode
= 0;
3476 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
3481 spin_unlock_irq(&mddev
->write_lock
);
3487 restart_array(mddev
);
3488 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
3489 wake_up(&mddev
->sb_wait
);
3493 set_disk_ro(mddev
->gendisk
, 0);
3494 err
= do_md_run(mddev
);
3499 /* these cannot be set */
3505 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
3509 static struct md_sysfs_entry md_array_state
=
3510 __ATTR(array_state
, S_IRUGO
|S_IWUSR
, array_state_show
, array_state_store
);
3513 max_corrected_read_errors_show(mddev_t
*mddev
, char *page
) {
3514 return sprintf(page
, "%d\n",
3515 atomic_read(&mddev
->max_corr_read_errors
));
3519 max_corrected_read_errors_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3522 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3524 if (*buf
&& (*e
== 0 || *e
== '\n')) {
3525 atomic_set(&mddev
->max_corr_read_errors
, n
);
3531 static struct md_sysfs_entry max_corr_read_errors
=
3532 __ATTR(max_read_errors
, S_IRUGO
|S_IWUSR
, max_corrected_read_errors_show
,
3533 max_corrected_read_errors_store
);
3536 null_show(mddev_t
*mddev
, char *page
)
3542 new_dev_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3544 /* buf must be %d:%d\n? giving major and minor numbers */
3545 /* The new device is added to the array.
3546 * If the array has a persistent superblock, we read the
3547 * superblock to initialise info and check validity.
3548 * Otherwise, only checking done is that in bind_rdev_to_array,
3549 * which mainly checks size.
3552 int major
= simple_strtoul(buf
, &e
, 10);
3558 if (!*buf
|| *e
!= ':' || !e
[1] || e
[1] == '\n')
3560 minor
= simple_strtoul(e
+1, &e
, 10);
3561 if (*e
&& *e
!= '\n')
3563 dev
= MKDEV(major
, minor
);
3564 if (major
!= MAJOR(dev
) ||
3565 minor
!= MINOR(dev
))
3569 if (mddev
->persistent
) {
3570 rdev
= md_import_device(dev
, mddev
->major_version
,
3571 mddev
->minor_version
);
3572 if (!IS_ERR(rdev
) && !list_empty(&mddev
->disks
)) {
3573 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
3574 mdk_rdev_t
, same_set
);
3575 err
= super_types
[mddev
->major_version
]
3576 .load_super(rdev
, rdev0
, mddev
->minor_version
);
3580 } else if (mddev
->external
)
3581 rdev
= md_import_device(dev
, -2, -1);
3583 rdev
= md_import_device(dev
, -1, -1);
3586 return PTR_ERR(rdev
);
3587 err
= bind_rdev_to_array(rdev
, mddev
);
3591 return err
? err
: len
;
3594 static struct md_sysfs_entry md_new_device
=
3595 __ATTR(new_dev
, S_IWUSR
, null_show
, new_dev_store
);
3598 bitmap_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3601 unsigned long chunk
, end_chunk
;
3605 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3607 chunk
= end_chunk
= simple_strtoul(buf
, &end
, 0);
3608 if (buf
== end
) break;
3609 if (*end
== '-') { /* range */
3611 end_chunk
= simple_strtoul(buf
, &end
, 0);
3612 if (buf
== end
) break;
3614 if (*end
&& !isspace(*end
)) break;
3615 bitmap_dirty_bits(mddev
->bitmap
, chunk
, end_chunk
);
3616 buf
= skip_spaces(end
);
3618 bitmap_unplug(mddev
->bitmap
); /* flush the bits to disk */
3623 static struct md_sysfs_entry md_bitmap
=
3624 __ATTR(bitmap_set_bits
, S_IWUSR
, null_show
, bitmap_store
);
3627 size_show(mddev_t
*mddev
, char *page
)
3629 return sprintf(page
, "%llu\n",
3630 (unsigned long long)mddev
->dev_sectors
/ 2);
3633 static int update_size(mddev_t
*mddev
, sector_t num_sectors
);
3636 size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3638 /* If array is inactive, we can reduce the component size, but
3639 * not increase it (except from 0).
3640 * If array is active, we can try an on-line resize
3643 int err
= strict_blocks_to_sectors(buf
, §ors
);
3648 err
= update_size(mddev
, sectors
);
3649 md_update_sb(mddev
, 1);
3651 if (mddev
->dev_sectors
== 0 ||
3652 mddev
->dev_sectors
> sectors
)
3653 mddev
->dev_sectors
= sectors
;
3657 return err
? err
: len
;
3660 static struct md_sysfs_entry md_size
=
3661 __ATTR(component_size
, S_IRUGO
|S_IWUSR
, size_show
, size_store
);
3666 * 'none' for arrays with no metadata (good luck...)
3667 * 'external' for arrays with externally managed metadata,
3668 * or N.M for internally known formats
3671 metadata_show(mddev_t
*mddev
, char *page
)
3673 if (mddev
->persistent
)
3674 return sprintf(page
, "%d.%d\n",
3675 mddev
->major_version
, mddev
->minor_version
);
3676 else if (mddev
->external
)
3677 return sprintf(page
, "external:%s\n", mddev
->metadata_type
);
3679 return sprintf(page
, "none\n");
3683 metadata_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3687 /* Changing the details of 'external' metadata is
3688 * always permitted. Otherwise there must be
3689 * no devices attached to the array.
3691 if (mddev
->external
&& strncmp(buf
, "external:", 9) == 0)
3693 else if (!list_empty(&mddev
->disks
))
3696 if (cmd_match(buf
, "none")) {
3697 mddev
->persistent
= 0;
3698 mddev
->external
= 0;
3699 mddev
->major_version
= 0;
3700 mddev
->minor_version
= 90;
3703 if (strncmp(buf
, "external:", 9) == 0) {
3704 size_t namelen
= len
-9;
3705 if (namelen
>= sizeof(mddev
->metadata_type
))
3706 namelen
= sizeof(mddev
->metadata_type
)-1;
3707 strncpy(mddev
->metadata_type
, buf
+9, namelen
);
3708 mddev
->metadata_type
[namelen
] = 0;
3709 if (namelen
&& mddev
->metadata_type
[namelen
-1] == '\n')
3710 mddev
->metadata_type
[--namelen
] = 0;
3711 mddev
->persistent
= 0;
3712 mddev
->external
= 1;
3713 mddev
->major_version
= 0;
3714 mddev
->minor_version
= 90;
3717 major
= simple_strtoul(buf
, &e
, 10);
3718 if (e
==buf
|| *e
!= '.')
3721 minor
= simple_strtoul(buf
, &e
, 10);
3722 if (e
==buf
|| (*e
&& *e
!= '\n') )
3724 if (major
>= ARRAY_SIZE(super_types
) || super_types
[major
].name
== NULL
)
3726 mddev
->major_version
= major
;
3727 mddev
->minor_version
= minor
;
3728 mddev
->persistent
= 1;
3729 mddev
->external
= 0;
3733 static struct md_sysfs_entry md_metadata
=
3734 __ATTR(metadata_version
, S_IRUGO
|S_IWUSR
, metadata_show
, metadata_store
);
3737 action_show(mddev_t
*mddev
, char *page
)
3739 char *type
= "idle";
3740 if (test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
3742 else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
3743 (!mddev
->ro
&& test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))) {
3744 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
3746 else if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
3747 if (!test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
3749 else if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
3753 } else if (test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
))
3756 return sprintf(page
, "%s\n", type
);
3759 static void reap_sync_thread(mddev_t
*mddev
);
3762 action_store(mddev_t
*mddev
, const char *page
, size_t len
)
3764 if (!mddev
->pers
|| !mddev
->pers
->sync_request
)
3767 if (cmd_match(page
, "frozen"))
3768 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
3770 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
3772 if (cmd_match(page
, "idle") || cmd_match(page
, "frozen")) {
3773 if (mddev
->sync_thread
) {
3774 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
3775 reap_sync_thread(mddev
);
3777 } else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
3778 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
3780 else if (cmd_match(page
, "resync"))
3781 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3782 else if (cmd_match(page
, "recover")) {
3783 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
3784 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3785 } else if (cmd_match(page
, "reshape")) {
3787 if (mddev
->pers
->start_reshape
== NULL
)
3789 err
= mddev
->pers
->start_reshape(mddev
);
3792 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
3794 if (cmd_match(page
, "check"))
3795 set_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
3796 else if (!cmd_match(page
, "repair"))
3798 set_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
3799 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
3801 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3802 md_wakeup_thread(mddev
->thread
);
3803 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
3808 mismatch_cnt_show(mddev_t
*mddev
, char *page
)
3810 return sprintf(page
, "%llu\n",
3811 (unsigned long long) mddev
->resync_mismatches
);
3814 static struct md_sysfs_entry md_scan_mode
=
3815 __ATTR(sync_action
, S_IRUGO
|S_IWUSR
, action_show
, action_store
);
3818 static struct md_sysfs_entry md_mismatches
= __ATTR_RO(mismatch_cnt
);
3821 sync_min_show(mddev_t
*mddev
, char *page
)
3823 return sprintf(page
, "%d (%s)\n", speed_min(mddev
),
3824 mddev
->sync_speed_min
? "local": "system");
3828 sync_min_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3832 if (strncmp(buf
, "system", 6)==0) {
3833 mddev
->sync_speed_min
= 0;
3836 min
= simple_strtoul(buf
, &e
, 10);
3837 if (buf
== e
|| (*e
&& *e
!= '\n') || min
<= 0)
3839 mddev
->sync_speed_min
= min
;
3843 static struct md_sysfs_entry md_sync_min
=
3844 __ATTR(sync_speed_min
, S_IRUGO
|S_IWUSR
, sync_min_show
, sync_min_store
);
3847 sync_max_show(mddev_t
*mddev
, char *page
)
3849 return sprintf(page
, "%d (%s)\n", speed_max(mddev
),
3850 mddev
->sync_speed_max
? "local": "system");
3854 sync_max_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3858 if (strncmp(buf
, "system", 6)==0) {
3859 mddev
->sync_speed_max
= 0;
3862 max
= simple_strtoul(buf
, &e
, 10);
3863 if (buf
== e
|| (*e
&& *e
!= '\n') || max
<= 0)
3865 mddev
->sync_speed_max
= max
;
3869 static struct md_sysfs_entry md_sync_max
=
3870 __ATTR(sync_speed_max
, S_IRUGO
|S_IWUSR
, sync_max_show
, sync_max_store
);
3873 degraded_show(mddev_t
*mddev
, char *page
)
3875 return sprintf(page
, "%d\n", mddev
->degraded
);
3877 static struct md_sysfs_entry md_degraded
= __ATTR_RO(degraded
);
3880 sync_force_parallel_show(mddev_t
*mddev
, char *page
)
3882 return sprintf(page
, "%d\n", mddev
->parallel_resync
);
3886 sync_force_parallel_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3890 if (strict_strtol(buf
, 10, &n
))
3893 if (n
!= 0 && n
!= 1)
3896 mddev
->parallel_resync
= n
;
3898 if (mddev
->sync_thread
)
3899 wake_up(&resync_wait
);
3904 /* force parallel resync, even with shared block devices */
3905 static struct md_sysfs_entry md_sync_force_parallel
=
3906 __ATTR(sync_force_parallel
, S_IRUGO
|S_IWUSR
,
3907 sync_force_parallel_show
, sync_force_parallel_store
);
3910 sync_speed_show(mddev_t
*mddev
, char *page
)
3912 unsigned long resync
, dt
, db
;
3913 if (mddev
->curr_resync
== 0)
3914 return sprintf(page
, "none\n");
3915 resync
= mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
);
3916 dt
= (jiffies
- mddev
->resync_mark
) / HZ
;
3918 db
= resync
- mddev
->resync_mark_cnt
;
3919 return sprintf(page
, "%lu\n", db
/dt
/2); /* K/sec */
3922 static struct md_sysfs_entry md_sync_speed
= __ATTR_RO(sync_speed
);
3925 sync_completed_show(mddev_t
*mddev
, char *page
)
3927 unsigned long long max_sectors
, resync
;
3929 if (!test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3930 return sprintf(page
, "none\n");
3932 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
3933 max_sectors
= mddev
->resync_max_sectors
;
3935 max_sectors
= mddev
->dev_sectors
;
3937 resync
= mddev
->curr_resync_completed
;
3938 return sprintf(page
, "%llu / %llu\n", resync
, max_sectors
);
3941 static struct md_sysfs_entry md_sync_completed
= __ATTR_RO(sync_completed
);
3944 min_sync_show(mddev_t
*mddev
, char *page
)
3946 return sprintf(page
, "%llu\n",
3947 (unsigned long long)mddev
->resync_min
);
3950 min_sync_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3952 unsigned long long min
;
3953 if (strict_strtoull(buf
, 10, &min
))
3955 if (min
> mddev
->resync_max
)
3957 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3960 /* Must be a multiple of chunk_size */
3961 if (mddev
->chunk_sectors
) {
3962 sector_t temp
= min
;
3963 if (sector_div(temp
, mddev
->chunk_sectors
))
3966 mddev
->resync_min
= min
;
3971 static struct md_sysfs_entry md_min_sync
=
3972 __ATTR(sync_min
, S_IRUGO
|S_IWUSR
, min_sync_show
, min_sync_store
);
3975 max_sync_show(mddev_t
*mddev
, char *page
)
3977 if (mddev
->resync_max
== MaxSector
)
3978 return sprintf(page
, "max\n");
3980 return sprintf(page
, "%llu\n",
3981 (unsigned long long)mddev
->resync_max
);
3984 max_sync_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3986 if (strncmp(buf
, "max", 3) == 0)
3987 mddev
->resync_max
= MaxSector
;
3989 unsigned long long max
;
3990 if (strict_strtoull(buf
, 10, &max
))
3992 if (max
< mddev
->resync_min
)
3994 if (max
< mddev
->resync_max
&&
3996 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3999 /* Must be a multiple of chunk_size */
4000 if (mddev
->chunk_sectors
) {
4001 sector_t temp
= max
;
4002 if (sector_div(temp
, mddev
->chunk_sectors
))
4005 mddev
->resync_max
= max
;
4007 wake_up(&mddev
->recovery_wait
);
4011 static struct md_sysfs_entry md_max_sync
=
4012 __ATTR(sync_max
, S_IRUGO
|S_IWUSR
, max_sync_show
, max_sync_store
);
4015 suspend_lo_show(mddev_t
*mddev
, char *page
)
4017 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_lo
);
4021 suspend_lo_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4024 unsigned long long new = simple_strtoull(buf
, &e
, 10);
4025 unsigned long long old
= mddev
->suspend_lo
;
4027 if (mddev
->pers
== NULL
||
4028 mddev
->pers
->quiesce
== NULL
)
4030 if (buf
== e
|| (*e
&& *e
!= '\n'))
4033 mddev
->suspend_lo
= new;
4035 /* Shrinking suspended region */
4036 mddev
->pers
->quiesce(mddev
, 2);
4038 /* Expanding suspended region - need to wait */
4039 mddev
->pers
->quiesce(mddev
, 1);
4040 mddev
->pers
->quiesce(mddev
, 0);
4044 static struct md_sysfs_entry md_suspend_lo
=
4045 __ATTR(suspend_lo
, S_IRUGO
|S_IWUSR
, suspend_lo_show
, suspend_lo_store
);
4049 suspend_hi_show(mddev_t
*mddev
, char *page
)
4051 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_hi
);
4055 suspend_hi_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4058 unsigned long long new = simple_strtoull(buf
, &e
, 10);
4059 unsigned long long old
= mddev
->suspend_hi
;
4061 if (mddev
->pers
== NULL
||
4062 mddev
->pers
->quiesce
== NULL
)
4064 if (buf
== e
|| (*e
&& *e
!= '\n'))
4067 mddev
->suspend_hi
= new;
4069 /* Shrinking suspended region */
4070 mddev
->pers
->quiesce(mddev
, 2);
4072 /* Expanding suspended region - need to wait */
4073 mddev
->pers
->quiesce(mddev
, 1);
4074 mddev
->pers
->quiesce(mddev
, 0);
4078 static struct md_sysfs_entry md_suspend_hi
=
4079 __ATTR(suspend_hi
, S_IRUGO
|S_IWUSR
, suspend_hi_show
, suspend_hi_store
);
4082 reshape_position_show(mddev_t
*mddev
, char *page
)
4084 if (mddev
->reshape_position
!= MaxSector
)
4085 return sprintf(page
, "%llu\n",
4086 (unsigned long long)mddev
->reshape_position
);
4087 strcpy(page
, "none\n");
4092 reshape_position_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4095 unsigned long long new = simple_strtoull(buf
, &e
, 10);
4098 if (buf
== e
|| (*e
&& *e
!= '\n'))
4100 mddev
->reshape_position
= new;
4101 mddev
->delta_disks
= 0;
4102 mddev
->new_level
= mddev
->level
;
4103 mddev
->new_layout
= mddev
->layout
;
4104 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
4108 static struct md_sysfs_entry md_reshape_position
=
4109 __ATTR(reshape_position
, S_IRUGO
|S_IWUSR
, reshape_position_show
,
4110 reshape_position_store
);
4113 array_size_show(mddev_t
*mddev
, char *page
)
4115 if (mddev
->external_size
)
4116 return sprintf(page
, "%llu\n",
4117 (unsigned long long)mddev
->array_sectors
/2);
4119 return sprintf(page
, "default\n");
4123 array_size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4127 if (strncmp(buf
, "default", 7) == 0) {
4129 sectors
= mddev
->pers
->size(mddev
, 0, 0);
4131 sectors
= mddev
->array_sectors
;
4133 mddev
->external_size
= 0;
4135 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
4137 if (mddev
->pers
&& mddev
->pers
->size(mddev
, 0, 0) < sectors
)
4140 mddev
->external_size
= 1;
4143 mddev
->array_sectors
= sectors
;
4145 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
4146 revalidate_disk(mddev
->gendisk
);
4151 static struct md_sysfs_entry md_array_size
=
4152 __ATTR(array_size
, S_IRUGO
|S_IWUSR
, array_size_show
,
4155 static struct attribute
*md_default_attrs
[] = {
4158 &md_raid_disks
.attr
,
4159 &md_chunk_size
.attr
,
4161 &md_resync_start
.attr
,
4163 &md_new_device
.attr
,
4164 &md_safe_delay
.attr
,
4165 &md_array_state
.attr
,
4166 &md_reshape_position
.attr
,
4167 &md_array_size
.attr
,
4168 &max_corr_read_errors
.attr
,
4172 static struct attribute
*md_redundancy_attrs
[] = {
4174 &md_mismatches
.attr
,
4177 &md_sync_speed
.attr
,
4178 &md_sync_force_parallel
.attr
,
4179 &md_sync_completed
.attr
,
4182 &md_suspend_lo
.attr
,
4183 &md_suspend_hi
.attr
,
4188 static struct attribute_group md_redundancy_group
= {
4190 .attrs
= md_redundancy_attrs
,
4195 md_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
4197 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
4198 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
4203 rv
= mddev_lock(mddev
);
4205 rv
= entry
->show(mddev
, page
);
4206 mddev_unlock(mddev
);
4212 md_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
4213 const char *page
, size_t length
)
4215 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
4216 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
4221 if (!capable(CAP_SYS_ADMIN
))
4223 rv
= mddev_lock(mddev
);
4224 if (mddev
->hold_active
== UNTIL_IOCTL
)
4225 mddev
->hold_active
= 0;
4227 rv
= entry
->store(mddev
, page
, length
);
4228 mddev_unlock(mddev
);
4233 static void md_free(struct kobject
*ko
)
4235 mddev_t
*mddev
= container_of(ko
, mddev_t
, kobj
);
4237 if (mddev
->sysfs_state
)
4238 sysfs_put(mddev
->sysfs_state
);
4240 if (mddev
->gendisk
) {
4241 del_gendisk(mddev
->gendisk
);
4242 put_disk(mddev
->gendisk
);
4245 blk_cleanup_queue(mddev
->queue
);
4250 static const struct sysfs_ops md_sysfs_ops
= {
4251 .show
= md_attr_show
,
4252 .store
= md_attr_store
,
4254 static struct kobj_type md_ktype
= {
4256 .sysfs_ops
= &md_sysfs_ops
,
4257 .default_attrs
= md_default_attrs
,
4262 static void mddev_delayed_delete(struct work_struct
*ws
)
4264 mddev_t
*mddev
= container_of(ws
, mddev_t
, del_work
);
4266 sysfs_remove_group(&mddev
->kobj
, &md_bitmap_group
);
4267 kobject_del(&mddev
->kobj
);
4268 kobject_put(&mddev
->kobj
);
4271 static int md_alloc(dev_t dev
, char *name
)
4273 static DEFINE_MUTEX(disks_mutex
);
4274 mddev_t
*mddev
= mddev_find(dev
);
4275 struct gendisk
*disk
;
4284 partitioned
= (MAJOR(mddev
->unit
) != MD_MAJOR
);
4285 shift
= partitioned
? MdpMinorShift
: 0;
4286 unit
= MINOR(mddev
->unit
) >> shift
;
4288 /* wait for any previous instance of this device to be
4289 * completely removed (mddev_delayed_delete).
4291 flush_workqueue(md_misc_wq
);
4293 mutex_lock(&disks_mutex
);
4299 /* Need to ensure that 'name' is not a duplicate.
4302 spin_lock(&all_mddevs_lock
);
4304 list_for_each_entry(mddev2
, &all_mddevs
, all_mddevs
)
4305 if (mddev2
->gendisk
&&
4306 strcmp(mddev2
->gendisk
->disk_name
, name
) == 0) {
4307 spin_unlock(&all_mddevs_lock
);
4310 spin_unlock(&all_mddevs_lock
);
4314 mddev
->queue
= blk_alloc_queue(GFP_KERNEL
);
4317 mddev
->queue
->queuedata
= mddev
;
4319 blk_queue_make_request(mddev
->queue
, md_make_request
);
4321 disk
= alloc_disk(1 << shift
);
4323 blk_cleanup_queue(mddev
->queue
);
4324 mddev
->queue
= NULL
;
4327 disk
->major
= MAJOR(mddev
->unit
);
4328 disk
->first_minor
= unit
<< shift
;
4330 strcpy(disk
->disk_name
, name
);
4331 else if (partitioned
)
4332 sprintf(disk
->disk_name
, "md_d%d", unit
);
4334 sprintf(disk
->disk_name
, "md%d", unit
);
4335 disk
->fops
= &md_fops
;
4336 disk
->private_data
= mddev
;
4337 disk
->queue
= mddev
->queue
;
4338 /* Allow extended partitions. This makes the
4339 * 'mdp' device redundant, but we can't really
4342 disk
->flags
|= GENHD_FL_EXT_DEVT
;
4344 mddev
->gendisk
= disk
;
4345 error
= kobject_init_and_add(&mddev
->kobj
, &md_ktype
,
4346 &disk_to_dev(disk
)->kobj
, "%s", "md");
4348 /* This isn't possible, but as kobject_init_and_add is marked
4349 * __must_check, we must do something with the result
4351 printk(KERN_WARNING
"md: cannot register %s/md - name in use\n",
4355 if (mddev
->kobj
.sd
&&
4356 sysfs_create_group(&mddev
->kobj
, &md_bitmap_group
))
4357 printk(KERN_DEBUG
"pointless warning\n");
4359 blk_queue_flush(mddev
->queue
, REQ_FLUSH
| REQ_FUA
);
4361 mutex_unlock(&disks_mutex
);
4362 if (!error
&& mddev
->kobj
.sd
) {
4363 kobject_uevent(&mddev
->kobj
, KOBJ_ADD
);
4364 mddev
->sysfs_state
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "array_state");
4370 static struct kobject
*md_probe(dev_t dev
, int *part
, void *data
)
4372 md_alloc(dev
, NULL
);
4376 static int add_named_array(const char *val
, struct kernel_param
*kp
)
4378 /* val must be "md_*" where * is not all digits.
4379 * We allocate an array with a large free minor number, and
4380 * set the name to val. val must not already be an active name.
4382 int len
= strlen(val
);
4383 char buf
[DISK_NAME_LEN
];
4385 while (len
&& val
[len
-1] == '\n')
4387 if (len
>= DISK_NAME_LEN
)
4389 strlcpy(buf
, val
, len
+1);
4390 if (strncmp(buf
, "md_", 3) != 0)
4392 return md_alloc(0, buf
);
4395 static void md_safemode_timeout(unsigned long data
)
4397 mddev_t
*mddev
= (mddev_t
*) data
;
4399 if (!atomic_read(&mddev
->writes_pending
)) {
4400 mddev
->safemode
= 1;
4401 if (mddev
->external
)
4402 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4404 md_wakeup_thread(mddev
->thread
);
4407 static int start_dirty_degraded
;
4409 int md_run(mddev_t
*mddev
)
4413 struct mdk_personality
*pers
;
4415 if (list_empty(&mddev
->disks
))
4416 /* cannot run an array with no devices.. */
4421 /* Cannot run until previous stop completes properly */
4422 if (mddev
->sysfs_active
)
4426 * Analyze all RAID superblock(s)
4428 if (!mddev
->raid_disks
) {
4429 if (!mddev
->persistent
)
4434 if (mddev
->level
!= LEVEL_NONE
)
4435 request_module("md-level-%d", mddev
->level
);
4436 else if (mddev
->clevel
[0])
4437 request_module("md-%s", mddev
->clevel
);
4440 * Drop all container device buffers, from now on
4441 * the only valid external interface is through the md
4444 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
4445 if (test_bit(Faulty
, &rdev
->flags
))
4447 sync_blockdev(rdev
->bdev
);
4448 invalidate_bdev(rdev
->bdev
);
4450 /* perform some consistency tests on the device.
4451 * We don't want the data to overlap the metadata,
4452 * Internal Bitmap issues have been handled elsewhere.
4454 if (rdev
->meta_bdev
) {
4455 /* Nothing to check */;
4456 } else if (rdev
->data_offset
< rdev
->sb_start
) {
4457 if (mddev
->dev_sectors
&&
4458 rdev
->data_offset
+ mddev
->dev_sectors
4460 printk("md: %s: data overlaps metadata\n",
4465 if (rdev
->sb_start
+ rdev
->sb_size
/512
4466 > rdev
->data_offset
) {
4467 printk("md: %s: metadata overlaps data\n",
4472 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
4475 if (mddev
->bio_set
== NULL
)
4476 mddev
->bio_set
= bioset_create(BIO_POOL_SIZE
, sizeof(mddev
));
4478 spin_lock(&pers_lock
);
4479 pers
= find_pers(mddev
->level
, mddev
->clevel
);
4480 if (!pers
|| !try_module_get(pers
->owner
)) {
4481 spin_unlock(&pers_lock
);
4482 if (mddev
->level
!= LEVEL_NONE
)
4483 printk(KERN_WARNING
"md: personality for level %d is not loaded!\n",
4486 printk(KERN_WARNING
"md: personality for level %s is not loaded!\n",
4491 spin_unlock(&pers_lock
);
4492 if (mddev
->level
!= pers
->level
) {
4493 mddev
->level
= pers
->level
;
4494 mddev
->new_level
= pers
->level
;
4496 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
4498 if (mddev
->reshape_position
!= MaxSector
&&
4499 pers
->start_reshape
== NULL
) {
4500 /* This personality cannot handle reshaping... */
4502 module_put(pers
->owner
);
4506 if (pers
->sync_request
) {
4507 /* Warn if this is a potentially silly
4510 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
4514 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4515 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
4517 rdev
->bdev
->bd_contains
==
4518 rdev2
->bdev
->bd_contains
) {
4520 "%s: WARNING: %s appears to be"
4521 " on the same physical disk as"
4524 bdevname(rdev
->bdev
,b
),
4525 bdevname(rdev2
->bdev
,b2
));
4532 "True protection against single-disk"
4533 " failure might be compromised.\n");
4536 mddev
->recovery
= 0;
4537 /* may be over-ridden by personality */
4538 mddev
->resync_max_sectors
= mddev
->dev_sectors
;
4540 mddev
->ok_start_degraded
= start_dirty_degraded
;
4542 if (start_readonly
&& mddev
->ro
== 0)
4543 mddev
->ro
= 2; /* read-only, but switch on first write */
4545 err
= mddev
->pers
->run(mddev
);
4547 printk(KERN_ERR
"md: pers->run() failed ...\n");
4548 else if (mddev
->pers
->size(mddev
, 0, 0) < mddev
->array_sectors
) {
4549 WARN_ONCE(!mddev
->external_size
, "%s: default size too small,"
4550 " but 'external_size' not in effect?\n", __func__
);
4552 "md: invalid array_size %llu > default size %llu\n",
4553 (unsigned long long)mddev
->array_sectors
/ 2,
4554 (unsigned long long)mddev
->pers
->size(mddev
, 0, 0) / 2);
4556 mddev
->pers
->stop(mddev
);
4558 if (err
== 0 && mddev
->pers
->sync_request
) {
4559 err
= bitmap_create(mddev
);
4561 printk(KERN_ERR
"%s: failed to create bitmap (%d)\n",
4562 mdname(mddev
), err
);
4563 mddev
->pers
->stop(mddev
);
4567 module_put(mddev
->pers
->owner
);
4569 bitmap_destroy(mddev
);
4572 if (mddev
->pers
->sync_request
) {
4573 if (mddev
->kobj
.sd
&&
4574 sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
4576 "md: cannot register extra attributes for %s\n",
4578 mddev
->sysfs_action
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "sync_action");
4579 } else if (mddev
->ro
== 2) /* auto-readonly not meaningful */
4582 atomic_set(&mddev
->writes_pending
,0);
4583 atomic_set(&mddev
->max_corr_read_errors
,
4584 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS
);
4585 mddev
->safemode
= 0;
4586 mddev
->safemode_timer
.function
= md_safemode_timeout
;
4587 mddev
->safemode_timer
.data
= (unsigned long) mddev
;
4588 mddev
->safemode_delay
= (200 * HZ
)/1000 +1; /* 200 msec delay */
4592 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4593 if (rdev
->raid_disk
>= 0) {
4595 sprintf(nm
, "rd%d", rdev
->raid_disk
);
4596 if (sysfs_create_link(&mddev
->kobj
, &rdev
->kobj
, nm
))
4597 /* failure here is OK */;
4600 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4603 md_update_sb(mddev
, 0);
4605 md_wakeup_thread(mddev
->thread
);
4606 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
4608 md_new_event(mddev
);
4609 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4610 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
4611 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
4614 EXPORT_SYMBOL_GPL(md_run
);
4616 static int do_md_run(mddev_t
*mddev
)
4620 err
= md_run(mddev
);
4623 err
= bitmap_load(mddev
);
4625 bitmap_destroy(mddev
);
4628 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
4629 revalidate_disk(mddev
->gendisk
);
4631 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
4636 static int restart_array(mddev_t
*mddev
)
4638 struct gendisk
*disk
= mddev
->gendisk
;
4640 /* Complain if it has no devices */
4641 if (list_empty(&mddev
->disks
))
4647 mddev
->safemode
= 0;
4649 set_disk_ro(disk
, 0);
4650 printk(KERN_INFO
"md: %s switched to read-write mode.\n",
4652 /* Kick recovery or resync if necessary */
4653 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4654 md_wakeup_thread(mddev
->thread
);
4655 md_wakeup_thread(mddev
->sync_thread
);
4656 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4660 /* similar to deny_write_access, but accounts for our holding a reference
4661 * to the file ourselves */
4662 static int deny_bitmap_write_access(struct file
* file
)
4664 struct inode
*inode
= file
->f_mapping
->host
;
4666 spin_lock(&inode
->i_lock
);
4667 if (atomic_read(&inode
->i_writecount
) > 1) {
4668 spin_unlock(&inode
->i_lock
);
4671 atomic_set(&inode
->i_writecount
, -1);
4672 spin_unlock(&inode
->i_lock
);
4677 void restore_bitmap_write_access(struct file
*file
)
4679 struct inode
*inode
= file
->f_mapping
->host
;
4681 spin_lock(&inode
->i_lock
);
4682 atomic_set(&inode
->i_writecount
, 1);
4683 spin_unlock(&inode
->i_lock
);
4686 static void md_clean(mddev_t
*mddev
)
4688 mddev
->array_sectors
= 0;
4689 mddev
->external_size
= 0;
4690 mddev
->dev_sectors
= 0;
4691 mddev
->raid_disks
= 0;
4692 mddev
->recovery_cp
= 0;
4693 mddev
->resync_min
= 0;
4694 mddev
->resync_max
= MaxSector
;
4695 mddev
->reshape_position
= MaxSector
;
4696 mddev
->external
= 0;
4697 mddev
->persistent
= 0;
4698 mddev
->level
= LEVEL_NONE
;
4699 mddev
->clevel
[0] = 0;
4702 mddev
->metadata_type
[0] = 0;
4703 mddev
->chunk_sectors
= 0;
4704 mddev
->ctime
= mddev
->utime
= 0;
4706 mddev
->max_disks
= 0;
4708 mddev
->can_decrease_events
= 0;
4709 mddev
->delta_disks
= 0;
4710 mddev
->new_level
= LEVEL_NONE
;
4711 mddev
->new_layout
= 0;
4712 mddev
->new_chunk_sectors
= 0;
4713 mddev
->curr_resync
= 0;
4714 mddev
->resync_mismatches
= 0;
4715 mddev
->suspend_lo
= mddev
->suspend_hi
= 0;
4716 mddev
->sync_speed_min
= mddev
->sync_speed_max
= 0;
4717 mddev
->recovery
= 0;
4720 mddev
->degraded
= 0;
4721 mddev
->safemode
= 0;
4722 mddev
->bitmap_info
.offset
= 0;
4723 mddev
->bitmap_info
.default_offset
= 0;
4724 mddev
->bitmap_info
.chunksize
= 0;
4725 mddev
->bitmap_info
.daemon_sleep
= 0;
4726 mddev
->bitmap_info
.max_write_behind
= 0;
4730 static void __md_stop_writes(mddev_t
*mddev
)
4732 if (mddev
->sync_thread
) {
4733 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4734 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4735 reap_sync_thread(mddev
);
4738 del_timer_sync(&mddev
->safemode_timer
);
4740 bitmap_flush(mddev
);
4741 md_super_wait(mddev
);
4743 if (!mddev
->in_sync
|| mddev
->flags
) {
4744 /* mark array as shutdown cleanly */
4746 md_update_sb(mddev
, 1);
4750 void md_stop_writes(mddev_t
*mddev
)
4753 __md_stop_writes(mddev
);
4754 mddev_unlock(mddev
);
4756 EXPORT_SYMBOL_GPL(md_stop_writes
);
4758 void md_stop(mddev_t
*mddev
)
4761 mddev
->pers
->stop(mddev
);
4762 if (mddev
->pers
->sync_request
&& mddev
->to_remove
== NULL
)
4763 mddev
->to_remove
= &md_redundancy_group
;
4764 module_put(mddev
->pers
->owner
);
4766 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4768 EXPORT_SYMBOL_GPL(md_stop
);
4770 static int md_set_readonly(mddev_t
*mddev
, int is_open
)
4773 mutex_lock(&mddev
->open_mutex
);
4774 if (atomic_read(&mddev
->openers
) > is_open
) {
4775 printk("md: %s still in use.\n",mdname(mddev
));
4780 __md_stop_writes(mddev
);
4786 set_disk_ro(mddev
->gendisk
, 1);
4787 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4788 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4792 mutex_unlock(&mddev
->open_mutex
);
4797 * 0 - completely stop and dis-assemble array
4798 * 2 - stop but do not disassemble array
4800 static int do_md_stop(mddev_t
* mddev
, int mode
, int is_open
)
4802 struct gendisk
*disk
= mddev
->gendisk
;
4805 mutex_lock(&mddev
->open_mutex
);
4806 if (atomic_read(&mddev
->openers
) > is_open
||
4807 mddev
->sysfs_active
) {
4808 printk("md: %s still in use.\n",mdname(mddev
));
4809 mutex_unlock(&mddev
->open_mutex
);
4815 set_disk_ro(disk
, 0);
4817 __md_stop_writes(mddev
);
4819 mddev
->queue
->merge_bvec_fn
= NULL
;
4820 mddev
->queue
->unplug_fn
= NULL
;
4821 mddev
->queue
->backing_dev_info
.congested_fn
= NULL
;
4823 /* tell userspace to handle 'inactive' */
4824 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4826 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4827 if (rdev
->raid_disk
>= 0) {
4829 sprintf(nm
, "rd%d", rdev
->raid_disk
);
4830 sysfs_remove_link(&mddev
->kobj
, nm
);
4833 set_capacity(disk
, 0);
4834 mutex_unlock(&mddev
->open_mutex
);
4836 revalidate_disk(disk
);
4841 mutex_unlock(&mddev
->open_mutex
);
4843 * Free resources if final stop
4846 printk(KERN_INFO
"md: %s stopped.\n", mdname(mddev
));
4848 bitmap_destroy(mddev
);
4849 if (mddev
->bitmap_info
.file
) {
4850 restore_bitmap_write_access(mddev
->bitmap_info
.file
);
4851 fput(mddev
->bitmap_info
.file
);
4852 mddev
->bitmap_info
.file
= NULL
;
4854 mddev
->bitmap_info
.offset
= 0;
4856 export_array(mddev
);
4859 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
4860 if (mddev
->hold_active
== UNTIL_STOP
)
4861 mddev
->hold_active
= 0;
4863 blk_integrity_unregister(disk
);
4864 md_new_event(mddev
);
4865 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4870 static void autorun_array(mddev_t
*mddev
)
4875 if (list_empty(&mddev
->disks
))
4878 printk(KERN_INFO
"md: running: ");
4880 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
4881 char b
[BDEVNAME_SIZE
];
4882 printk("<%s>", bdevname(rdev
->bdev
,b
));
4886 err
= do_md_run(mddev
);
4888 printk(KERN_WARNING
"md: do_md_run() returned %d\n", err
);
4889 do_md_stop(mddev
, 0, 0);
4894 * lets try to run arrays based on all disks that have arrived
4895 * until now. (those are in pending_raid_disks)
4897 * the method: pick the first pending disk, collect all disks with
4898 * the same UUID, remove all from the pending list and put them into
4899 * the 'same_array' list. Then order this list based on superblock
4900 * update time (freshest comes first), kick out 'old' disks and
4901 * compare superblocks. If everything's fine then run it.
4903 * If "unit" is allocated, then bump its reference count
4905 static void autorun_devices(int part
)
4907 mdk_rdev_t
*rdev0
, *rdev
, *tmp
;
4909 char b
[BDEVNAME_SIZE
];
4911 printk(KERN_INFO
"md: autorun ...\n");
4912 while (!list_empty(&pending_raid_disks
)) {
4915 LIST_HEAD(candidates
);
4916 rdev0
= list_entry(pending_raid_disks
.next
,
4917 mdk_rdev_t
, same_set
);
4919 printk(KERN_INFO
"md: considering %s ...\n",
4920 bdevname(rdev0
->bdev
,b
));
4921 INIT_LIST_HEAD(&candidates
);
4922 rdev_for_each_list(rdev
, tmp
, &pending_raid_disks
)
4923 if (super_90_load(rdev
, rdev0
, 0) >= 0) {
4924 printk(KERN_INFO
"md: adding %s ...\n",
4925 bdevname(rdev
->bdev
,b
));
4926 list_move(&rdev
->same_set
, &candidates
);
4929 * now we have a set of devices, with all of them having
4930 * mostly sane superblocks. It's time to allocate the
4934 dev
= MKDEV(mdp_major
,
4935 rdev0
->preferred_minor
<< MdpMinorShift
);
4936 unit
= MINOR(dev
) >> MdpMinorShift
;
4938 dev
= MKDEV(MD_MAJOR
, rdev0
->preferred_minor
);
4941 if (rdev0
->preferred_minor
!= unit
) {
4942 printk(KERN_INFO
"md: unit number in %s is bad: %d\n",
4943 bdevname(rdev0
->bdev
, b
), rdev0
->preferred_minor
);
4947 md_probe(dev
, NULL
, NULL
);
4948 mddev
= mddev_find(dev
);
4949 if (!mddev
|| !mddev
->gendisk
) {
4953 "md: cannot allocate memory for md drive.\n");
4956 if (mddev_lock(mddev
))
4957 printk(KERN_WARNING
"md: %s locked, cannot run\n",
4959 else if (mddev
->raid_disks
|| mddev
->major_version
4960 || !list_empty(&mddev
->disks
)) {
4962 "md: %s already running, cannot run %s\n",
4963 mdname(mddev
), bdevname(rdev0
->bdev
,b
));
4964 mddev_unlock(mddev
);
4966 printk(KERN_INFO
"md: created %s\n", mdname(mddev
));
4967 mddev
->persistent
= 1;
4968 rdev_for_each_list(rdev
, tmp
, &candidates
) {
4969 list_del_init(&rdev
->same_set
);
4970 if (bind_rdev_to_array(rdev
, mddev
))
4973 autorun_array(mddev
);
4974 mddev_unlock(mddev
);
4976 /* on success, candidates will be empty, on error
4979 rdev_for_each_list(rdev
, tmp
, &candidates
) {
4980 list_del_init(&rdev
->same_set
);
4985 printk(KERN_INFO
"md: ... autorun DONE.\n");
4987 #endif /* !MODULE */
4989 static int get_version(void __user
* arg
)
4993 ver
.major
= MD_MAJOR_VERSION
;
4994 ver
.minor
= MD_MINOR_VERSION
;
4995 ver
.patchlevel
= MD_PATCHLEVEL_VERSION
;
4997 if (copy_to_user(arg
, &ver
, sizeof(ver
)))
5003 static int get_array_info(mddev_t
* mddev
, void __user
* arg
)
5005 mdu_array_info_t info
;
5006 int nr
,working
,insync
,failed
,spare
;
5009 nr
=working
=insync
=failed
=spare
=0;
5010 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
5012 if (test_bit(Faulty
, &rdev
->flags
))
5016 if (test_bit(In_sync
, &rdev
->flags
))
5023 info
.major_version
= mddev
->major_version
;
5024 info
.minor_version
= mddev
->minor_version
;
5025 info
.patch_version
= MD_PATCHLEVEL_VERSION
;
5026 info
.ctime
= mddev
->ctime
;
5027 info
.level
= mddev
->level
;
5028 info
.size
= mddev
->dev_sectors
/ 2;
5029 if (info
.size
!= mddev
->dev_sectors
/ 2) /* overflow */
5032 info
.raid_disks
= mddev
->raid_disks
;
5033 info
.md_minor
= mddev
->md_minor
;
5034 info
.not_persistent
= !mddev
->persistent
;
5036 info
.utime
= mddev
->utime
;
5039 info
.state
= (1<<MD_SB_CLEAN
);
5040 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
5041 info
.state
= (1<<MD_SB_BITMAP_PRESENT
);
5042 info
.active_disks
= insync
;
5043 info
.working_disks
= working
;
5044 info
.failed_disks
= failed
;
5045 info
.spare_disks
= spare
;
5047 info
.layout
= mddev
->layout
;
5048 info
.chunk_size
= mddev
->chunk_sectors
<< 9;
5050 if (copy_to_user(arg
, &info
, sizeof(info
)))
5056 static int get_bitmap_file(mddev_t
* mddev
, void __user
* arg
)
5058 mdu_bitmap_file_t
*file
= NULL
; /* too big for stack allocation */
5059 char *ptr
, *buf
= NULL
;
5062 if (md_allow_write(mddev
))
5063 file
= kmalloc(sizeof(*file
), GFP_NOIO
);
5065 file
= kmalloc(sizeof(*file
), GFP_KERNEL
);
5070 /* bitmap disabled, zero the first byte and copy out */
5071 if (!mddev
->bitmap
|| !mddev
->bitmap
->file
) {
5072 file
->pathname
[0] = '\0';
5076 buf
= kmalloc(sizeof(file
->pathname
), GFP_KERNEL
);
5080 ptr
= d_path(&mddev
->bitmap
->file
->f_path
, buf
, sizeof(file
->pathname
));
5084 strcpy(file
->pathname
, ptr
);
5088 if (copy_to_user(arg
, file
, sizeof(*file
)))
5096 static int get_disk_info(mddev_t
* mddev
, void __user
* arg
)
5098 mdu_disk_info_t info
;
5101 if (copy_from_user(&info
, arg
, sizeof(info
)))
5104 rdev
= find_rdev_nr(mddev
, info
.number
);
5106 info
.major
= MAJOR(rdev
->bdev
->bd_dev
);
5107 info
.minor
= MINOR(rdev
->bdev
->bd_dev
);
5108 info
.raid_disk
= rdev
->raid_disk
;
5110 if (test_bit(Faulty
, &rdev
->flags
))
5111 info
.state
|= (1<<MD_DISK_FAULTY
);
5112 else if (test_bit(In_sync
, &rdev
->flags
)) {
5113 info
.state
|= (1<<MD_DISK_ACTIVE
);
5114 info
.state
|= (1<<MD_DISK_SYNC
);
5116 if (test_bit(WriteMostly
, &rdev
->flags
))
5117 info
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
5119 info
.major
= info
.minor
= 0;
5120 info
.raid_disk
= -1;
5121 info
.state
= (1<<MD_DISK_REMOVED
);
5124 if (copy_to_user(arg
, &info
, sizeof(info
)))
5130 static int add_new_disk(mddev_t
* mddev
, mdu_disk_info_t
*info
)
5132 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
5134 dev_t dev
= MKDEV(info
->major
,info
->minor
);
5136 if (info
->major
!= MAJOR(dev
) || info
->minor
!= MINOR(dev
))
5139 if (!mddev
->raid_disks
) {
5141 /* expecting a device which has a superblock */
5142 rdev
= md_import_device(dev
, mddev
->major_version
, mddev
->minor_version
);
5145 "md: md_import_device returned %ld\n",
5147 return PTR_ERR(rdev
);
5149 if (!list_empty(&mddev
->disks
)) {
5150 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
5151 mdk_rdev_t
, same_set
);
5152 err
= super_types
[mddev
->major_version
]
5153 .load_super(rdev
, rdev0
, mddev
->minor_version
);
5156 "md: %s has different UUID to %s\n",
5157 bdevname(rdev
->bdev
,b
),
5158 bdevname(rdev0
->bdev
,b2
));
5163 err
= bind_rdev_to_array(rdev
, mddev
);
5170 * add_new_disk can be used once the array is assembled
5171 * to add "hot spares". They must already have a superblock
5176 if (!mddev
->pers
->hot_add_disk
) {
5178 "%s: personality does not support diskops!\n",
5182 if (mddev
->persistent
)
5183 rdev
= md_import_device(dev
, mddev
->major_version
,
5184 mddev
->minor_version
);
5186 rdev
= md_import_device(dev
, -1, -1);
5189 "md: md_import_device returned %ld\n",
5191 return PTR_ERR(rdev
);
5193 /* set saved_raid_disk if appropriate */
5194 if (!mddev
->persistent
) {
5195 if (info
->state
& (1<<MD_DISK_SYNC
) &&
5196 info
->raid_disk
< mddev
->raid_disks
) {
5197 rdev
->raid_disk
= info
->raid_disk
;
5198 set_bit(In_sync
, &rdev
->flags
);
5200 rdev
->raid_disk
= -1;
5202 super_types
[mddev
->major_version
].
5203 validate_super(mddev
, rdev
);
5204 if (test_bit(In_sync
, &rdev
->flags
))
5205 rdev
->saved_raid_disk
= rdev
->raid_disk
;
5207 rdev
->saved_raid_disk
= -1;
5209 clear_bit(In_sync
, &rdev
->flags
); /* just to be sure */
5210 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
5211 set_bit(WriteMostly
, &rdev
->flags
);
5213 clear_bit(WriteMostly
, &rdev
->flags
);
5215 rdev
->raid_disk
= -1;
5216 err
= bind_rdev_to_array(rdev
, mddev
);
5217 if (!err
&& !mddev
->pers
->hot_remove_disk
) {
5218 /* If there is hot_add_disk but no hot_remove_disk
5219 * then added disks for geometry changes,
5220 * and should be added immediately.
5222 super_types
[mddev
->major_version
].
5223 validate_super(mddev
, rdev
);
5224 err
= mddev
->pers
->hot_add_disk(mddev
, rdev
);
5226 unbind_rdev_from_array(rdev
);
5231 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
5233 md_update_sb(mddev
, 1);
5234 if (mddev
->degraded
)
5235 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
5236 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5237 md_wakeup_thread(mddev
->thread
);
5241 /* otherwise, add_new_disk is only allowed
5242 * for major_version==0 superblocks
5244 if (mddev
->major_version
!= 0) {
5245 printk(KERN_WARNING
"%s: ADD_NEW_DISK not supported\n",
5250 if (!(info
->state
& (1<<MD_DISK_FAULTY
))) {
5252 rdev
= md_import_device(dev
, -1, 0);
5255 "md: error, md_import_device() returned %ld\n",
5257 return PTR_ERR(rdev
);
5259 rdev
->desc_nr
= info
->number
;
5260 if (info
->raid_disk
< mddev
->raid_disks
)
5261 rdev
->raid_disk
= info
->raid_disk
;
5263 rdev
->raid_disk
= -1;
5265 if (rdev
->raid_disk
< mddev
->raid_disks
)
5266 if (info
->state
& (1<<MD_DISK_SYNC
))
5267 set_bit(In_sync
, &rdev
->flags
);
5269 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
5270 set_bit(WriteMostly
, &rdev
->flags
);
5272 if (!mddev
->persistent
) {
5273 printk(KERN_INFO
"md: nonpersistent superblock ...\n");
5274 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
5276 rdev
->sb_start
= calc_dev_sboffset(rdev
);
5277 rdev
->sectors
= rdev
->sb_start
;
5279 err
= bind_rdev_to_array(rdev
, mddev
);
5289 static int hot_remove_disk(mddev_t
* mddev
, dev_t dev
)
5291 char b
[BDEVNAME_SIZE
];
5294 rdev
= find_rdev(mddev
, dev
);
5298 if (rdev
->raid_disk
>= 0)
5301 kick_rdev_from_array(rdev
);
5302 md_update_sb(mddev
, 1);
5303 md_new_event(mddev
);
5307 printk(KERN_WARNING
"md: cannot remove active disk %s from %s ...\n",
5308 bdevname(rdev
->bdev
,b
), mdname(mddev
));
5312 static int hot_add_disk(mddev_t
* mddev
, dev_t dev
)
5314 char b
[BDEVNAME_SIZE
];
5321 if (mddev
->major_version
!= 0) {
5322 printk(KERN_WARNING
"%s: HOT_ADD may only be used with"
5323 " version-0 superblocks.\n",
5327 if (!mddev
->pers
->hot_add_disk
) {
5329 "%s: personality does not support diskops!\n",
5334 rdev
= md_import_device(dev
, -1, 0);
5337 "md: error, md_import_device() returned %ld\n",
5342 if (mddev
->persistent
)
5343 rdev
->sb_start
= calc_dev_sboffset(rdev
);
5345 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
5347 rdev
->sectors
= rdev
->sb_start
;
5349 if (test_bit(Faulty
, &rdev
->flags
)) {
5351 "md: can not hot-add faulty %s disk to %s!\n",
5352 bdevname(rdev
->bdev
,b
), mdname(mddev
));
5356 clear_bit(In_sync
, &rdev
->flags
);
5358 rdev
->saved_raid_disk
= -1;
5359 err
= bind_rdev_to_array(rdev
, mddev
);
5364 * The rest should better be atomic, we can have disk failures
5365 * noticed in interrupt contexts ...
5368 rdev
->raid_disk
= -1;
5370 md_update_sb(mddev
, 1);
5373 * Kick recovery, maybe this spare has to be added to the
5374 * array immediately.
5376 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5377 md_wakeup_thread(mddev
->thread
);
5378 md_new_event(mddev
);
5386 static int set_bitmap_file(mddev_t
*mddev
, int fd
)
5391 if (!mddev
->pers
->quiesce
)
5393 if (mddev
->recovery
|| mddev
->sync_thread
)
5395 /* we should be able to change the bitmap.. */
5401 return -EEXIST
; /* cannot add when bitmap is present */
5402 mddev
->bitmap_info
.file
= fget(fd
);
5404 if (mddev
->bitmap_info
.file
== NULL
) {
5405 printk(KERN_ERR
"%s: error: failed to get bitmap file\n",
5410 err
= deny_bitmap_write_access(mddev
->bitmap_info
.file
);
5412 printk(KERN_ERR
"%s: error: bitmap file is already in use\n",
5414 fput(mddev
->bitmap_info
.file
);
5415 mddev
->bitmap_info
.file
= NULL
;
5418 mddev
->bitmap_info
.offset
= 0; /* file overrides offset */
5419 } else if (mddev
->bitmap
== NULL
)
5420 return -ENOENT
; /* cannot remove what isn't there */
5423 mddev
->pers
->quiesce(mddev
, 1);
5425 err
= bitmap_create(mddev
);
5427 err
= bitmap_load(mddev
);
5429 if (fd
< 0 || err
) {
5430 bitmap_destroy(mddev
);
5431 fd
= -1; /* make sure to put the file */
5433 mddev
->pers
->quiesce(mddev
, 0);
5436 if (mddev
->bitmap_info
.file
) {
5437 restore_bitmap_write_access(mddev
->bitmap_info
.file
);
5438 fput(mddev
->bitmap_info
.file
);
5440 mddev
->bitmap_info
.file
= NULL
;
5447 * set_array_info is used two different ways
5448 * The original usage is when creating a new array.
5449 * In this usage, raid_disks is > 0 and it together with
5450 * level, size, not_persistent,layout,chunksize determine the
5451 * shape of the array.
5452 * This will always create an array with a type-0.90.0 superblock.
5453 * The newer usage is when assembling an array.
5454 * In this case raid_disks will be 0, and the major_version field is
5455 * use to determine which style super-blocks are to be found on the devices.
5456 * The minor and patch _version numbers are also kept incase the
5457 * super_block handler wishes to interpret them.
5459 static int set_array_info(mddev_t
* mddev
, mdu_array_info_t
*info
)
5462 if (info
->raid_disks
== 0) {
5463 /* just setting version number for superblock loading */
5464 if (info
->major_version
< 0 ||
5465 info
->major_version
>= ARRAY_SIZE(super_types
) ||
5466 super_types
[info
->major_version
].name
== NULL
) {
5467 /* maybe try to auto-load a module? */
5469 "md: superblock version %d not known\n",
5470 info
->major_version
);
5473 mddev
->major_version
= info
->major_version
;
5474 mddev
->minor_version
= info
->minor_version
;
5475 mddev
->patch_version
= info
->patch_version
;
5476 mddev
->persistent
= !info
->not_persistent
;
5477 /* ensure mddev_put doesn't delete this now that there
5478 * is some minimal configuration.
5480 mddev
->ctime
= get_seconds();
5483 mddev
->major_version
= MD_MAJOR_VERSION
;
5484 mddev
->minor_version
= MD_MINOR_VERSION
;
5485 mddev
->patch_version
= MD_PATCHLEVEL_VERSION
;
5486 mddev
->ctime
= get_seconds();
5488 mddev
->level
= info
->level
;
5489 mddev
->clevel
[0] = 0;
5490 mddev
->dev_sectors
= 2 * (sector_t
)info
->size
;
5491 mddev
->raid_disks
= info
->raid_disks
;
5492 /* don't set md_minor, it is determined by which /dev/md* was
5495 if (info
->state
& (1<<MD_SB_CLEAN
))
5496 mddev
->recovery_cp
= MaxSector
;
5498 mddev
->recovery_cp
= 0;
5499 mddev
->persistent
= ! info
->not_persistent
;
5500 mddev
->external
= 0;
5502 mddev
->layout
= info
->layout
;
5503 mddev
->chunk_sectors
= info
->chunk_size
>> 9;
5505 mddev
->max_disks
= MD_SB_DISKS
;
5507 if (mddev
->persistent
)
5509 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
5511 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
5512 mddev
->bitmap_info
.offset
= 0;
5514 mddev
->reshape_position
= MaxSector
;
5517 * Generate a 128 bit UUID
5519 get_random_bytes(mddev
->uuid
, 16);
5521 mddev
->new_level
= mddev
->level
;
5522 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
5523 mddev
->new_layout
= mddev
->layout
;
5524 mddev
->delta_disks
= 0;
5529 void md_set_array_sectors(mddev_t
*mddev
, sector_t array_sectors
)
5531 WARN(!mddev_is_locked(mddev
), "%s: unlocked mddev!\n", __func__
);
5533 if (mddev
->external_size
)
5536 mddev
->array_sectors
= array_sectors
;
5538 EXPORT_SYMBOL(md_set_array_sectors
);
5540 static int update_size(mddev_t
*mddev
, sector_t num_sectors
)
5544 int fit
= (num_sectors
== 0);
5546 if (mddev
->pers
->resize
== NULL
)
5548 /* The "num_sectors" is the number of sectors of each device that
5549 * is used. This can only make sense for arrays with redundancy.
5550 * linear and raid0 always use whatever space is available. We can only
5551 * consider changing this number if no resync or reconstruction is
5552 * happening, and if the new size is acceptable. It must fit before the
5553 * sb_start or, if that is <data_offset, it must fit before the size
5554 * of each device. If num_sectors is zero, we find the largest size
5557 if (mddev
->sync_thread
)
5560 /* Sorry, cannot grow a bitmap yet, just remove it,
5564 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
5565 sector_t avail
= rdev
->sectors
;
5567 if (fit
&& (num_sectors
== 0 || num_sectors
> avail
))
5568 num_sectors
= avail
;
5569 if (avail
< num_sectors
)
5572 rv
= mddev
->pers
->resize(mddev
, num_sectors
);
5574 revalidate_disk(mddev
->gendisk
);
5578 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
)
5581 /* change the number of raid disks */
5582 if (mddev
->pers
->check_reshape
== NULL
)
5584 if (raid_disks
<= 0 ||
5585 (mddev
->max_disks
&& raid_disks
>= mddev
->max_disks
))
5587 if (mddev
->sync_thread
|| mddev
->reshape_position
!= MaxSector
)
5589 mddev
->delta_disks
= raid_disks
- mddev
->raid_disks
;
5591 rv
= mddev
->pers
->check_reshape(mddev
);
5593 mddev
->delta_disks
= 0;
5599 * update_array_info is used to change the configuration of an
5601 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5602 * fields in the info are checked against the array.
5603 * Any differences that cannot be handled will cause an error.
5604 * Normally, only one change can be managed at a time.
5606 static int update_array_info(mddev_t
*mddev
, mdu_array_info_t
*info
)
5612 /* calculate expected state,ignoring low bits */
5613 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
5614 state
|= (1 << MD_SB_BITMAP_PRESENT
);
5616 if (mddev
->major_version
!= info
->major_version
||
5617 mddev
->minor_version
!= info
->minor_version
||
5618 /* mddev->patch_version != info->patch_version || */
5619 mddev
->ctime
!= info
->ctime
||
5620 mddev
->level
!= info
->level
||
5621 /* mddev->layout != info->layout || */
5622 !mddev
->persistent
!= info
->not_persistent
||
5623 mddev
->chunk_sectors
!= info
->chunk_size
>> 9 ||
5624 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5625 ((state
^info
->state
) & 0xfffffe00)
5628 /* Check there is only one change */
5629 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
5631 if (mddev
->raid_disks
!= info
->raid_disks
)
5633 if (mddev
->layout
!= info
->layout
)
5635 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
))
5642 if (mddev
->layout
!= info
->layout
) {
5644 * we don't need to do anything at the md level, the
5645 * personality will take care of it all.
5647 if (mddev
->pers
->check_reshape
== NULL
)
5650 mddev
->new_layout
= info
->layout
;
5651 rv
= mddev
->pers
->check_reshape(mddev
);
5653 mddev
->new_layout
= mddev
->layout
;
5657 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
5658 rv
= update_size(mddev
, (sector_t
)info
->size
* 2);
5660 if (mddev
->raid_disks
!= info
->raid_disks
)
5661 rv
= update_raid_disks(mddev
, info
->raid_disks
);
5663 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) {
5664 if (mddev
->pers
->quiesce
== NULL
)
5666 if (mddev
->recovery
|| mddev
->sync_thread
)
5668 if (info
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
5669 /* add the bitmap */
5672 if (mddev
->bitmap_info
.default_offset
== 0)
5674 mddev
->bitmap_info
.offset
=
5675 mddev
->bitmap_info
.default_offset
;
5676 mddev
->pers
->quiesce(mddev
, 1);
5677 rv
= bitmap_create(mddev
);
5679 rv
= bitmap_load(mddev
);
5681 bitmap_destroy(mddev
);
5682 mddev
->pers
->quiesce(mddev
, 0);
5684 /* remove the bitmap */
5687 if (mddev
->bitmap
->file
)
5689 mddev
->pers
->quiesce(mddev
, 1);
5690 bitmap_destroy(mddev
);
5691 mddev
->pers
->quiesce(mddev
, 0);
5692 mddev
->bitmap_info
.offset
= 0;
5695 md_update_sb(mddev
, 1);
5699 static int set_disk_faulty(mddev_t
*mddev
, dev_t dev
)
5703 if (mddev
->pers
== NULL
)
5706 rdev
= find_rdev(mddev
, dev
);
5710 md_error(mddev
, rdev
);
5715 * We have a problem here : there is no easy way to give a CHS
5716 * virtual geometry. We currently pretend that we have a 2 heads
5717 * 4 sectors (with a BIG number of cylinders...). This drives
5718 * dosfs just mad... ;-)
5720 static int md_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
5722 mddev_t
*mddev
= bdev
->bd_disk
->private_data
;
5726 geo
->cylinders
= mddev
->array_sectors
/ 8;
5730 static int md_ioctl(struct block_device
*bdev
, fmode_t mode
,
5731 unsigned int cmd
, unsigned long arg
)
5734 void __user
*argp
= (void __user
*)arg
;
5735 mddev_t
*mddev
= NULL
;
5738 if (!capable(CAP_SYS_ADMIN
))
5742 * Commands dealing with the RAID driver but not any
5748 err
= get_version(argp
);
5751 case PRINT_RAID_DEBUG
:
5759 autostart_arrays(arg
);
5766 * Commands creating/starting a new array:
5769 mddev
= bdev
->bd_disk
->private_data
;
5776 err
= mddev_lock(mddev
);
5779 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5786 case SET_ARRAY_INFO
:
5788 mdu_array_info_t info
;
5790 memset(&info
, 0, sizeof(info
));
5791 else if (copy_from_user(&info
, argp
, sizeof(info
))) {
5796 err
= update_array_info(mddev
, &info
);
5798 printk(KERN_WARNING
"md: couldn't update"
5799 " array info. %d\n", err
);
5804 if (!list_empty(&mddev
->disks
)) {
5806 "md: array %s already has disks!\n",
5811 if (mddev
->raid_disks
) {
5813 "md: array %s already initialised!\n",
5818 err
= set_array_info(mddev
, &info
);
5820 printk(KERN_WARNING
"md: couldn't set"
5821 " array info. %d\n", err
);
5831 * Commands querying/configuring an existing array:
5833 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
5834 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
5835 if ((!mddev
->raid_disks
&& !mddev
->external
)
5836 && cmd
!= ADD_NEW_DISK
&& cmd
!= STOP_ARRAY
5837 && cmd
!= RUN_ARRAY
&& cmd
!= SET_BITMAP_FILE
5838 && cmd
!= GET_BITMAP_FILE
) {
5844 * Commands even a read-only array can execute:
5848 case GET_ARRAY_INFO
:
5849 err
= get_array_info(mddev
, argp
);
5852 case GET_BITMAP_FILE
:
5853 err
= get_bitmap_file(mddev
, argp
);
5857 err
= get_disk_info(mddev
, argp
);
5860 case RESTART_ARRAY_RW
:
5861 err
= restart_array(mddev
);
5865 err
= do_md_stop(mddev
, 0, 1);
5869 err
= md_set_readonly(mddev
, 1);
5873 if (get_user(ro
, (int __user
*)(arg
))) {
5879 /* if the bdev is going readonly the value of mddev->ro
5880 * does not matter, no writes are coming
5885 /* are we are already prepared for writes? */
5889 /* transitioning to readauto need only happen for
5890 * arrays that call md_write_start
5893 err
= restart_array(mddev
);
5896 set_disk_ro(mddev
->gendisk
, 0);
5903 * The remaining ioctls are changing the state of the
5904 * superblock, so we do not allow them on read-only arrays.
5905 * However non-MD ioctls (e.g. get-size) will still come through
5906 * here and hit the 'default' below, so only disallow
5907 * 'md' ioctls, and switch to rw mode if started auto-readonly.
5909 if (_IOC_TYPE(cmd
) == MD_MAJOR
&& mddev
->ro
&& mddev
->pers
) {
5910 if (mddev
->ro
== 2) {
5912 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5913 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5914 md_wakeup_thread(mddev
->thread
);
5925 mdu_disk_info_t info
;
5926 if (copy_from_user(&info
, argp
, sizeof(info
)))
5929 err
= add_new_disk(mddev
, &info
);
5933 case HOT_REMOVE_DISK
:
5934 err
= hot_remove_disk(mddev
, new_decode_dev(arg
));
5938 err
= hot_add_disk(mddev
, new_decode_dev(arg
));
5941 case SET_DISK_FAULTY
:
5942 err
= set_disk_faulty(mddev
, new_decode_dev(arg
));
5946 err
= do_md_run(mddev
);
5949 case SET_BITMAP_FILE
:
5950 err
= set_bitmap_file(mddev
, (int)arg
);
5960 if (mddev
->hold_active
== UNTIL_IOCTL
&&
5962 mddev
->hold_active
= 0;
5963 mddev_unlock(mddev
);
5972 #ifdef CONFIG_COMPAT
5973 static int md_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
5974 unsigned int cmd
, unsigned long arg
)
5977 case HOT_REMOVE_DISK
:
5979 case SET_DISK_FAULTY
:
5980 case SET_BITMAP_FILE
:
5981 /* These take in integer arg, do not convert */
5984 arg
= (unsigned long)compat_ptr(arg
);
5988 return md_ioctl(bdev
, mode
, cmd
, arg
);
5990 #endif /* CONFIG_COMPAT */
5992 static int md_open(struct block_device
*bdev
, fmode_t mode
)
5995 * Succeed if we can lock the mddev, which confirms that
5996 * it isn't being stopped right now.
5998 mddev_t
*mddev
= mddev_find(bdev
->bd_dev
);
6001 if (mddev
->gendisk
!= bdev
->bd_disk
) {
6002 /* we are racing with mddev_put which is discarding this
6006 /* Wait until bdev->bd_disk is definitely gone */
6007 flush_workqueue(md_misc_wq
);
6008 /* Then retry the open from the top */
6009 return -ERESTARTSYS
;
6011 BUG_ON(mddev
!= bdev
->bd_disk
->private_data
);
6013 if ((err
= mutex_lock_interruptible(&mddev
->open_mutex
)))
6017 atomic_inc(&mddev
->openers
);
6018 mutex_unlock(&mddev
->open_mutex
);
6020 check_disk_change(bdev
);
6025 static int md_release(struct gendisk
*disk
, fmode_t mode
)
6027 mddev_t
*mddev
= disk
->private_data
;
6030 atomic_dec(&mddev
->openers
);
6036 static int md_media_changed(struct gendisk
*disk
)
6038 mddev_t
*mddev
= disk
->private_data
;
6040 return mddev
->changed
;
6043 static int md_revalidate(struct gendisk
*disk
)
6045 mddev_t
*mddev
= disk
->private_data
;
6050 static const struct block_device_operations md_fops
=
6052 .owner
= THIS_MODULE
,
6054 .release
= md_release
,
6056 #ifdef CONFIG_COMPAT
6057 .compat_ioctl
= md_compat_ioctl
,
6059 .getgeo
= md_getgeo
,
6060 .media_changed
= md_media_changed
,
6061 .revalidate_disk
= md_revalidate
,
6064 static int md_thread(void * arg
)
6066 mdk_thread_t
*thread
= arg
;
6069 * md_thread is a 'system-thread', it's priority should be very
6070 * high. We avoid resource deadlocks individually in each
6071 * raid personality. (RAID5 does preallocation) We also use RR and
6072 * the very same RT priority as kswapd, thus we will never get
6073 * into a priority inversion deadlock.
6075 * we definitely have to have equal or higher priority than
6076 * bdflush, otherwise bdflush will deadlock if there are too
6077 * many dirty RAID5 blocks.
6080 allow_signal(SIGKILL
);
6081 while (!kthread_should_stop()) {
6083 /* We need to wait INTERRUPTIBLE so that
6084 * we don't add to the load-average.
6085 * That means we need to be sure no signals are
6088 if (signal_pending(current
))
6089 flush_signals(current
);
6091 wait_event_interruptible_timeout
6093 test_bit(THREAD_WAKEUP
, &thread
->flags
)
6094 || kthread_should_stop(),
6097 clear_bit(THREAD_WAKEUP
, &thread
->flags
);
6098 if (!kthread_should_stop())
6099 thread
->run(thread
->mddev
);
6105 void md_wakeup_thread(mdk_thread_t
*thread
)
6108 dprintk("md: waking up MD thread %s.\n", thread
->tsk
->comm
);
6109 set_bit(THREAD_WAKEUP
, &thread
->flags
);
6110 wake_up(&thread
->wqueue
);
6114 mdk_thread_t
*md_register_thread(void (*run
) (mddev_t
*), mddev_t
*mddev
,
6117 mdk_thread_t
*thread
;
6119 thread
= kzalloc(sizeof(mdk_thread_t
), GFP_KERNEL
);
6123 init_waitqueue_head(&thread
->wqueue
);
6126 thread
->mddev
= mddev
;
6127 thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
6128 thread
->tsk
= kthread_run(md_thread
, thread
,
6130 mdname(thread
->mddev
),
6131 name
?: mddev
->pers
->name
);
6132 if (IS_ERR(thread
->tsk
)) {
6139 void md_unregister_thread(mdk_thread_t
*thread
)
6143 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread
->tsk
));
6145 kthread_stop(thread
->tsk
);
6149 void md_error(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
6156 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
6159 if (mddev
->external
)
6160 set_bit(Blocked
, &rdev
->flags
);
6162 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
6164 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
6165 __builtin_return_address(0),__builtin_return_address(1),
6166 __builtin_return_address(2),__builtin_return_address(3));
6170 if (!mddev
->pers
->error_handler
)
6172 mddev
->pers
->error_handler(mddev
,rdev
);
6173 if (mddev
->degraded
)
6174 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
6175 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
6176 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6177 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6178 md_wakeup_thread(mddev
->thread
);
6179 if (mddev
->event_work
.func
)
6180 queue_work(md_misc_wq
, &mddev
->event_work
);
6181 md_new_event_inintr(mddev
);
6184 /* seq_file implementation /proc/mdstat */
6186 static void status_unused(struct seq_file
*seq
)
6191 seq_printf(seq
, "unused devices: ");
6193 list_for_each_entry(rdev
, &pending_raid_disks
, same_set
) {
6194 char b
[BDEVNAME_SIZE
];
6196 seq_printf(seq
, "%s ",
6197 bdevname(rdev
->bdev
,b
));
6200 seq_printf(seq
, "<none>");
6202 seq_printf(seq
, "\n");
6206 static void status_resync(struct seq_file
*seq
, mddev_t
* mddev
)
6208 sector_t max_sectors
, resync
, res
;
6209 unsigned long dt
, db
;
6212 unsigned int per_milli
;
6214 resync
= mddev
->curr_resync
- atomic_read(&mddev
->recovery_active
);
6216 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
6217 max_sectors
= mddev
->resync_max_sectors
;
6219 max_sectors
= mddev
->dev_sectors
;
6222 * Should not happen.
6228 /* Pick 'scale' such that (resync>>scale)*1000 will fit
6229 * in a sector_t, and (max_sectors>>scale) will fit in a
6230 * u32, as those are the requirements for sector_div.
6231 * Thus 'scale' must be at least 10
6234 if (sizeof(sector_t
) > sizeof(unsigned long)) {
6235 while ( max_sectors
/2 > (1ULL<<(scale
+32)))
6238 res
= (resync
>>scale
)*1000;
6239 sector_div(res
, (u32
)((max_sectors
>>scale
)+1));
6243 int i
, x
= per_milli
/50, y
= 20-x
;
6244 seq_printf(seq
, "[");
6245 for (i
= 0; i
< x
; i
++)
6246 seq_printf(seq
, "=");
6247 seq_printf(seq
, ">");
6248 for (i
= 0; i
< y
; i
++)
6249 seq_printf(seq
, ".");
6250 seq_printf(seq
, "] ");
6252 seq_printf(seq
, " %s =%3u.%u%% (%llu/%llu)",
6253 (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)?
6255 (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)?
6257 (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ?
6258 "resync" : "recovery"))),
6259 per_milli
/10, per_milli
% 10,
6260 (unsigned long long) resync
/2,
6261 (unsigned long long) max_sectors
/2);
6264 * dt: time from mark until now
6265 * db: blocks written from mark until now
6266 * rt: remaining time
6268 * rt is a sector_t, so could be 32bit or 64bit.
6269 * So we divide before multiply in case it is 32bit and close
6271 * We scale the divisor (db) by 32 to avoid loosing precision
6272 * near the end of resync when the number of remaining sectors
6274 * We then divide rt by 32 after multiplying by db to compensate.
6275 * The '+1' avoids division by zero if db is very small.
6277 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
6279 db
= (mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
))
6280 - mddev
->resync_mark_cnt
;
6282 rt
= max_sectors
- resync
; /* number of remaining sectors */
6283 sector_div(rt
, db
/32+1);
6287 seq_printf(seq
, " finish=%lu.%lumin", (unsigned long)rt
/ 60,
6288 ((unsigned long)rt
% 60)/6);
6290 seq_printf(seq
, " speed=%ldK/sec", db
/2/dt
);
6293 static void *md_seq_start(struct seq_file
*seq
, loff_t
*pos
)
6295 struct list_head
*tmp
;
6305 spin_lock(&all_mddevs_lock
);
6306 list_for_each(tmp
,&all_mddevs
)
6308 mddev
= list_entry(tmp
, mddev_t
, all_mddevs
);
6310 spin_unlock(&all_mddevs_lock
);
6313 spin_unlock(&all_mddevs_lock
);
6315 return (void*)2;/* tail */
6319 static void *md_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
6321 struct list_head
*tmp
;
6322 mddev_t
*next_mddev
, *mddev
= v
;
6328 spin_lock(&all_mddevs_lock
);
6330 tmp
= all_mddevs
.next
;
6332 tmp
= mddev
->all_mddevs
.next
;
6333 if (tmp
!= &all_mddevs
)
6334 next_mddev
= mddev_get(list_entry(tmp
,mddev_t
,all_mddevs
));
6336 next_mddev
= (void*)2;
6339 spin_unlock(&all_mddevs_lock
);
6347 static void md_seq_stop(struct seq_file
*seq
, void *v
)
6351 if (mddev
&& v
!= (void*)1 && v
!= (void*)2)
6355 struct mdstat_info
{
6359 static int md_seq_show(struct seq_file
*seq
, void *v
)
6364 struct mdstat_info
*mi
= seq
->private;
6365 struct bitmap
*bitmap
;
6367 if (v
== (void*)1) {
6368 struct mdk_personality
*pers
;
6369 seq_printf(seq
, "Personalities : ");
6370 spin_lock(&pers_lock
);
6371 list_for_each_entry(pers
, &pers_list
, list
)
6372 seq_printf(seq
, "[%s] ", pers
->name
);
6374 spin_unlock(&pers_lock
);
6375 seq_printf(seq
, "\n");
6376 mi
->event
= atomic_read(&md_event_count
);
6379 if (v
== (void*)2) {
6384 if (mddev_lock(mddev
) < 0)
6387 if (mddev
->pers
|| mddev
->raid_disks
|| !list_empty(&mddev
->disks
)) {
6388 seq_printf(seq
, "%s : %sactive", mdname(mddev
),
6389 mddev
->pers
? "" : "in");
6392 seq_printf(seq
, " (read-only)");
6394 seq_printf(seq
, " (auto-read-only)");
6395 seq_printf(seq
, " %s", mddev
->pers
->name
);
6399 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
6400 char b
[BDEVNAME_SIZE
];
6401 seq_printf(seq
, " %s[%d]",
6402 bdevname(rdev
->bdev
,b
), rdev
->desc_nr
);
6403 if (test_bit(WriteMostly
, &rdev
->flags
))
6404 seq_printf(seq
, "(W)");
6405 if (test_bit(Faulty
, &rdev
->flags
)) {
6406 seq_printf(seq
, "(F)");
6408 } else if (rdev
->raid_disk
< 0)
6409 seq_printf(seq
, "(S)"); /* spare */
6410 sectors
+= rdev
->sectors
;
6413 if (!list_empty(&mddev
->disks
)) {
6415 seq_printf(seq
, "\n %llu blocks",
6416 (unsigned long long)
6417 mddev
->array_sectors
/ 2);
6419 seq_printf(seq
, "\n %llu blocks",
6420 (unsigned long long)sectors
/ 2);
6422 if (mddev
->persistent
) {
6423 if (mddev
->major_version
!= 0 ||
6424 mddev
->minor_version
!= 90) {
6425 seq_printf(seq
," super %d.%d",
6426 mddev
->major_version
,
6427 mddev
->minor_version
);
6429 } else if (mddev
->external
)
6430 seq_printf(seq
, " super external:%s",
6431 mddev
->metadata_type
);
6433 seq_printf(seq
, " super non-persistent");
6436 mddev
->pers
->status(seq
, mddev
);
6437 seq_printf(seq
, "\n ");
6438 if (mddev
->pers
->sync_request
) {
6439 if (mddev
->curr_resync
> 2) {
6440 status_resync(seq
, mddev
);
6441 seq_printf(seq
, "\n ");
6442 } else if (mddev
->curr_resync
== 1 || mddev
->curr_resync
== 2)
6443 seq_printf(seq
, "\tresync=DELAYED\n ");
6444 else if (mddev
->recovery_cp
< MaxSector
)
6445 seq_printf(seq
, "\tresync=PENDING\n ");
6448 seq_printf(seq
, "\n ");
6450 if ((bitmap
= mddev
->bitmap
)) {
6451 unsigned long chunk_kb
;
6452 unsigned long flags
;
6453 spin_lock_irqsave(&bitmap
->lock
, flags
);
6454 chunk_kb
= mddev
->bitmap_info
.chunksize
>> 10;
6455 seq_printf(seq
, "bitmap: %lu/%lu pages [%luKB], "
6457 bitmap
->pages
- bitmap
->missing_pages
,
6459 (bitmap
->pages
- bitmap
->missing_pages
)
6460 << (PAGE_SHIFT
- 10),
6461 chunk_kb
? chunk_kb
: mddev
->bitmap_info
.chunksize
,
6462 chunk_kb
? "KB" : "B");
6464 seq_printf(seq
, ", file: ");
6465 seq_path(seq
, &bitmap
->file
->f_path
, " \t\n");
6468 seq_printf(seq
, "\n");
6469 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
6472 seq_printf(seq
, "\n");
6474 mddev_unlock(mddev
);
6479 static const struct seq_operations md_seq_ops
= {
6480 .start
= md_seq_start
,
6481 .next
= md_seq_next
,
6482 .stop
= md_seq_stop
,
6483 .show
= md_seq_show
,
6486 static int md_seq_open(struct inode
*inode
, struct file
*file
)
6489 struct mdstat_info
*mi
= kmalloc(sizeof(*mi
), GFP_KERNEL
);
6493 error
= seq_open(file
, &md_seq_ops
);
6497 struct seq_file
*p
= file
->private_data
;
6499 mi
->event
= atomic_read(&md_event_count
);
6504 static unsigned int mdstat_poll(struct file
*filp
, poll_table
*wait
)
6506 struct seq_file
*m
= filp
->private_data
;
6507 struct mdstat_info
*mi
= m
->private;
6510 poll_wait(filp
, &md_event_waiters
, wait
);
6512 /* always allow read */
6513 mask
= POLLIN
| POLLRDNORM
;
6515 if (mi
->event
!= atomic_read(&md_event_count
))
6516 mask
|= POLLERR
| POLLPRI
;
6520 static const struct file_operations md_seq_fops
= {
6521 .owner
= THIS_MODULE
,
6522 .open
= md_seq_open
,
6524 .llseek
= seq_lseek
,
6525 .release
= seq_release_private
,
6526 .poll
= mdstat_poll
,
6529 int register_md_personality(struct mdk_personality
*p
)
6531 spin_lock(&pers_lock
);
6532 list_add_tail(&p
->list
, &pers_list
);
6533 printk(KERN_INFO
"md: %s personality registered for level %d\n", p
->name
, p
->level
);
6534 spin_unlock(&pers_lock
);
6538 int unregister_md_personality(struct mdk_personality
*p
)
6540 printk(KERN_INFO
"md: %s personality unregistered\n", p
->name
);
6541 spin_lock(&pers_lock
);
6542 list_del_init(&p
->list
);
6543 spin_unlock(&pers_lock
);
6547 static int is_mddev_idle(mddev_t
*mddev
, int init
)
6555 rdev_for_each_rcu(rdev
, mddev
) {
6556 struct gendisk
*disk
= rdev
->bdev
->bd_contains
->bd_disk
;
6557 curr_events
= (int)part_stat_read(&disk
->part0
, sectors
[0]) +
6558 (int)part_stat_read(&disk
->part0
, sectors
[1]) -
6559 atomic_read(&disk
->sync_io
);
6560 /* sync IO will cause sync_io to increase before the disk_stats
6561 * as sync_io is counted when a request starts, and
6562 * disk_stats is counted when it completes.
6563 * So resync activity will cause curr_events to be smaller than
6564 * when there was no such activity.
6565 * non-sync IO will cause disk_stat to increase without
6566 * increasing sync_io so curr_events will (eventually)
6567 * be larger than it was before. Once it becomes
6568 * substantially larger, the test below will cause
6569 * the array to appear non-idle, and resync will slow
6571 * If there is a lot of outstanding resync activity when
6572 * we set last_event to curr_events, then all that activity
6573 * completing might cause the array to appear non-idle
6574 * and resync will be slowed down even though there might
6575 * not have been non-resync activity. This will only
6576 * happen once though. 'last_events' will soon reflect
6577 * the state where there is little or no outstanding
6578 * resync requests, and further resync activity will
6579 * always make curr_events less than last_events.
6582 if (init
|| curr_events
- rdev
->last_events
> 64) {
6583 rdev
->last_events
= curr_events
;
6591 void md_done_sync(mddev_t
*mddev
, int blocks
, int ok
)
6593 /* another "blocks" (512byte) blocks have been synced */
6594 atomic_sub(blocks
, &mddev
->recovery_active
);
6595 wake_up(&mddev
->recovery_wait
);
6597 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6598 md_wakeup_thread(mddev
->thread
);
6599 // stop recovery, signal do_sync ....
6604 /* md_write_start(mddev, bi)
6605 * If we need to update some array metadata (e.g. 'active' flag
6606 * in superblock) before writing, schedule a superblock update
6607 * and wait for it to complete.
6609 void md_write_start(mddev_t
*mddev
, struct bio
*bi
)
6612 if (bio_data_dir(bi
) != WRITE
)
6615 BUG_ON(mddev
->ro
== 1);
6616 if (mddev
->ro
== 2) {
6617 /* need to switch to read/write */
6619 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6620 md_wakeup_thread(mddev
->thread
);
6621 md_wakeup_thread(mddev
->sync_thread
);
6624 atomic_inc(&mddev
->writes_pending
);
6625 if (mddev
->safemode
== 1)
6626 mddev
->safemode
= 0;
6627 if (mddev
->in_sync
) {
6628 spin_lock_irq(&mddev
->write_lock
);
6629 if (mddev
->in_sync
) {
6631 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6632 set_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
6633 md_wakeup_thread(mddev
->thread
);
6636 spin_unlock_irq(&mddev
->write_lock
);
6639 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6640 wait_event(mddev
->sb_wait
,
6641 !test_bit(MD_CHANGE_PENDING
, &mddev
->flags
));
6644 void md_write_end(mddev_t
*mddev
)
6646 if (atomic_dec_and_test(&mddev
->writes_pending
)) {
6647 if (mddev
->safemode
== 2)
6648 md_wakeup_thread(mddev
->thread
);
6649 else if (mddev
->safemode_delay
)
6650 mod_timer(&mddev
->safemode_timer
, jiffies
+ mddev
->safemode_delay
);
6654 /* md_allow_write(mddev)
6655 * Calling this ensures that the array is marked 'active' so that writes
6656 * may proceed without blocking. It is important to call this before
6657 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6658 * Must be called with mddev_lock held.
6660 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6661 * is dropped, so return -EAGAIN after notifying userspace.
6663 int md_allow_write(mddev_t
*mddev
)
6669 if (!mddev
->pers
->sync_request
)
6672 spin_lock_irq(&mddev
->write_lock
);
6673 if (mddev
->in_sync
) {
6675 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6676 set_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
6677 if (mddev
->safemode_delay
&&
6678 mddev
->safemode
== 0)
6679 mddev
->safemode
= 1;
6680 spin_unlock_irq(&mddev
->write_lock
);
6681 md_update_sb(mddev
, 0);
6682 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6684 spin_unlock_irq(&mddev
->write_lock
);
6686 if (test_bit(MD_CHANGE_PENDING
, &mddev
->flags
))
6691 EXPORT_SYMBOL_GPL(md_allow_write
);
6693 void md_unplug(mddev_t
*mddev
)
6696 blk_unplug(mddev
->queue
);
6698 mddev
->plug
->unplug_fn(mddev
->plug
);
6701 #define SYNC_MARKS 10
6702 #define SYNC_MARK_STEP (3*HZ)
6703 void md_do_sync(mddev_t
*mddev
)
6706 unsigned int currspeed
= 0,
6708 sector_t max_sectors
,j
, io_sectors
;
6709 unsigned long mark
[SYNC_MARKS
];
6710 sector_t mark_cnt
[SYNC_MARKS
];
6712 struct list_head
*tmp
;
6713 sector_t last_check
;
6718 /* just incase thread restarts... */
6719 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
6721 if (mddev
->ro
) /* never try to sync a read-only array */
6724 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
6725 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
6726 desc
= "data-check";
6727 else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
6728 desc
= "requested-resync";
6731 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
6736 /* we overload curr_resync somewhat here.
6737 * 0 == not engaged in resync at all
6738 * 2 == checking that there is no conflict with another sync
6739 * 1 == like 2, but have yielded to allow conflicting resync to
6741 * other == active in resync - this many blocks
6743 * Before starting a resync we must have set curr_resync to
6744 * 2, and then checked that every "conflicting" array has curr_resync
6745 * less than ours. When we find one that is the same or higher
6746 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6747 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6748 * This will mean we have to start checking from the beginning again.
6753 mddev
->curr_resync
= 2;
6756 if (kthread_should_stop())
6757 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6759 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
6761 for_each_mddev(mddev2
, tmp
) {
6762 if (mddev2
== mddev
)
6764 if (!mddev
->parallel_resync
6765 && mddev2
->curr_resync
6766 && match_mddev_units(mddev
, mddev2
)) {
6768 if (mddev
< mddev2
&& mddev
->curr_resync
== 2) {
6769 /* arbitrarily yield */
6770 mddev
->curr_resync
= 1;
6771 wake_up(&resync_wait
);
6773 if (mddev
> mddev2
&& mddev
->curr_resync
== 1)
6774 /* no need to wait here, we can wait the next
6775 * time 'round when curr_resync == 2
6778 /* We need to wait 'interruptible' so as not to
6779 * contribute to the load average, and not to
6780 * be caught by 'softlockup'
6782 prepare_to_wait(&resync_wait
, &wq
, TASK_INTERRUPTIBLE
);
6783 if (!kthread_should_stop() &&
6784 mddev2
->curr_resync
>= mddev
->curr_resync
) {
6785 printk(KERN_INFO
"md: delaying %s of %s"
6786 " until %s has finished (they"
6787 " share one or more physical units)\n",
6788 desc
, mdname(mddev
), mdname(mddev2
));
6790 if (signal_pending(current
))
6791 flush_signals(current
);
6793 finish_wait(&resync_wait
, &wq
);
6796 finish_wait(&resync_wait
, &wq
);
6799 } while (mddev
->curr_resync
< 2);
6802 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
6803 /* resync follows the size requested by the personality,
6804 * which defaults to physical size, but can be virtual size
6806 max_sectors
= mddev
->resync_max_sectors
;
6807 mddev
->resync_mismatches
= 0;
6808 /* we don't use the checkpoint if there's a bitmap */
6809 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
6810 j
= mddev
->resync_min
;
6811 else if (!mddev
->bitmap
)
6812 j
= mddev
->recovery_cp
;
6814 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
6815 max_sectors
= mddev
->dev_sectors
;
6817 /* recovery follows the physical size of devices */
6818 max_sectors
= mddev
->dev_sectors
;
6821 list_for_each_entry_rcu(rdev
, &mddev
->disks
, same_set
)
6822 if (rdev
->raid_disk
>= 0 &&
6823 !test_bit(Faulty
, &rdev
->flags
) &&
6824 !test_bit(In_sync
, &rdev
->flags
) &&
6825 rdev
->recovery_offset
< j
)
6826 j
= rdev
->recovery_offset
;
6830 printk(KERN_INFO
"md: %s of RAID array %s\n", desc
, mdname(mddev
));
6831 printk(KERN_INFO
"md: minimum _guaranteed_ speed:"
6832 " %d KB/sec/disk.\n", speed_min(mddev
));
6833 printk(KERN_INFO
"md: using maximum available idle IO bandwidth "
6834 "(but not more than %d KB/sec) for %s.\n",
6835 speed_max(mddev
), desc
);
6837 is_mddev_idle(mddev
, 1); /* this initializes IO event counters */
6840 for (m
= 0; m
< SYNC_MARKS
; m
++) {
6842 mark_cnt
[m
] = io_sectors
;
6845 mddev
->resync_mark
= mark
[last_mark
];
6846 mddev
->resync_mark_cnt
= mark_cnt
[last_mark
];
6849 * Tune reconstruction:
6851 window
= 32*(PAGE_SIZE
/512);
6852 printk(KERN_INFO
"md: using %dk window, over a total of %llu blocks.\n",
6853 window
/2,(unsigned long long) max_sectors
/2);
6855 atomic_set(&mddev
->recovery_active
, 0);
6860 "md: resuming %s of %s from checkpoint.\n",
6861 desc
, mdname(mddev
));
6862 mddev
->curr_resync
= j
;
6864 mddev
->curr_resync_completed
= j
;
6866 while (j
< max_sectors
) {
6871 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
6872 ((mddev
->curr_resync
> mddev
->curr_resync_completed
&&
6873 (mddev
->curr_resync
- mddev
->curr_resync_completed
)
6874 > (max_sectors
>> 4)) ||
6875 (j
- mddev
->curr_resync_completed
)*2
6876 >= mddev
->resync_max
- mddev
->curr_resync_completed
6878 /* time to update curr_resync_completed */
6880 wait_event(mddev
->recovery_wait
,
6881 atomic_read(&mddev
->recovery_active
) == 0);
6882 mddev
->curr_resync_completed
= j
;
6883 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6884 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
6887 while (j
>= mddev
->resync_max
&& !kthread_should_stop()) {
6888 /* As this condition is controlled by user-space,
6889 * we can block indefinitely, so use '_interruptible'
6890 * to avoid triggering warnings.
6892 flush_signals(current
); /* just in case */
6893 wait_event_interruptible(mddev
->recovery_wait
,
6894 mddev
->resync_max
> j
6895 || kthread_should_stop());
6898 if (kthread_should_stop())
6901 sectors
= mddev
->pers
->sync_request(mddev
, j
, &skipped
,
6902 currspeed
< speed_min(mddev
));
6904 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6908 if (!skipped
) { /* actual IO requested */
6909 io_sectors
+= sectors
;
6910 atomic_add(sectors
, &mddev
->recovery_active
);
6914 if (j
>1) mddev
->curr_resync
= j
;
6915 mddev
->curr_mark_cnt
= io_sectors
;
6916 if (last_check
== 0)
6917 /* this is the earliers that rebuilt will be
6918 * visible in /proc/mdstat
6920 md_new_event(mddev
);
6922 if (last_check
+ window
> io_sectors
|| j
== max_sectors
)
6925 last_check
= io_sectors
;
6927 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
6931 if (time_after_eq(jiffies
, mark
[last_mark
] + SYNC_MARK_STEP
)) {
6933 int next
= (last_mark
+1) % SYNC_MARKS
;
6935 mddev
->resync_mark
= mark
[next
];
6936 mddev
->resync_mark_cnt
= mark_cnt
[next
];
6937 mark
[next
] = jiffies
;
6938 mark_cnt
[next
] = io_sectors
- atomic_read(&mddev
->recovery_active
);
6943 if (kthread_should_stop())
6948 * this loop exits only if either when we are slower than
6949 * the 'hard' speed limit, or the system was IO-idle for
6951 * the system might be non-idle CPU-wise, but we only care
6952 * about not overloading the IO subsystem. (things like an
6953 * e2fsck being done on the RAID array should execute fast)
6958 currspeed
= ((unsigned long)(io_sectors
-mddev
->resync_mark_cnt
))/2
6959 /((jiffies
-mddev
->resync_mark
)/HZ
+1) +1;
6961 if (currspeed
> speed_min(mddev
)) {
6962 if ((currspeed
> speed_max(mddev
)) ||
6963 !is_mddev_idle(mddev
, 0)) {
6969 printk(KERN_INFO
"md: %s: %s done.\n",mdname(mddev
), desc
);
6971 * this also signals 'finished resyncing' to md_stop
6976 wait_event(mddev
->recovery_wait
, !atomic_read(&mddev
->recovery_active
));
6978 /* tell personality that we are finished */
6979 mddev
->pers
->sync_request(mddev
, max_sectors
, &skipped
, 1);
6981 if (!test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
) &&
6982 mddev
->curr_resync
> 2) {
6983 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
6984 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
6985 if (mddev
->curr_resync
>= mddev
->recovery_cp
) {
6987 "md: checkpointing %s of %s.\n",
6988 desc
, mdname(mddev
));
6989 mddev
->recovery_cp
= mddev
->curr_resync
;
6992 mddev
->recovery_cp
= MaxSector
;
6994 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
6995 mddev
->curr_resync
= MaxSector
;
6997 list_for_each_entry_rcu(rdev
, &mddev
->disks
, same_set
)
6998 if (rdev
->raid_disk
>= 0 &&
6999 mddev
->delta_disks
>= 0 &&
7000 !test_bit(Faulty
, &rdev
->flags
) &&
7001 !test_bit(In_sync
, &rdev
->flags
) &&
7002 rdev
->recovery_offset
< mddev
->curr_resync
)
7003 rdev
->recovery_offset
= mddev
->curr_resync
;
7007 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
7010 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
7011 /* We completed so min/max setting can be forgotten if used. */
7012 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
7013 mddev
->resync_min
= 0;
7014 mddev
->resync_max
= MaxSector
;
7015 } else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
7016 mddev
->resync_min
= mddev
->curr_resync_completed
;
7017 mddev
->curr_resync
= 0;
7018 wake_up(&resync_wait
);
7019 set_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
7020 md_wakeup_thread(mddev
->thread
);
7025 * got a signal, exit.
7028 "md: md_do_sync() got signal ... exiting\n");
7029 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7033 EXPORT_SYMBOL_GPL(md_do_sync
);
7036 static int remove_and_add_spares(mddev_t
*mddev
)
7041 mddev
->curr_resync_completed
= 0;
7043 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
7044 if (rdev
->raid_disk
>= 0 &&
7045 !test_bit(Blocked
, &rdev
->flags
) &&
7046 (test_bit(Faulty
, &rdev
->flags
) ||
7047 ! test_bit(In_sync
, &rdev
->flags
)) &&
7048 atomic_read(&rdev
->nr_pending
)==0) {
7049 if (mddev
->pers
->hot_remove_disk(
7050 mddev
, rdev
->raid_disk
)==0) {
7052 sprintf(nm
,"rd%d", rdev
->raid_disk
);
7053 sysfs_remove_link(&mddev
->kobj
, nm
);
7054 rdev
->raid_disk
= -1;
7058 if (mddev
->degraded
&& !mddev
->recovery_disabled
) {
7059 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
7060 if (rdev
->raid_disk
>= 0 &&
7061 !test_bit(In_sync
, &rdev
->flags
) &&
7062 !test_bit(Blocked
, &rdev
->flags
))
7064 if (rdev
->raid_disk
< 0
7065 && !test_bit(Faulty
, &rdev
->flags
)) {
7066 rdev
->recovery_offset
= 0;
7068 hot_add_disk(mddev
, rdev
) == 0) {
7070 sprintf(nm
, "rd%d", rdev
->raid_disk
);
7071 if (sysfs_create_link(&mddev
->kobj
,
7073 /* failure here is OK */;
7075 md_new_event(mddev
);
7076 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
7085 static void reap_sync_thread(mddev_t
*mddev
)
7089 /* resync has finished, collect result */
7090 md_unregister_thread(mddev
->sync_thread
);
7091 mddev
->sync_thread
= NULL
;
7092 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
7093 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
7095 /* activate any spares */
7096 if (mddev
->pers
->spare_active(mddev
))
7097 sysfs_notify(&mddev
->kobj
, NULL
,
7100 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
7101 mddev
->pers
->finish_reshape
)
7102 mddev
->pers
->finish_reshape(mddev
);
7103 md_update_sb(mddev
, 1);
7105 /* if array is no-longer degraded, then any saved_raid_disk
7106 * information must be scrapped
7108 if (!mddev
->degraded
)
7109 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
7110 rdev
->saved_raid_disk
= -1;
7112 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7113 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7114 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7115 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
7116 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7117 /* flag recovery needed just to double check */
7118 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7119 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7120 md_new_event(mddev
);
7124 * This routine is regularly called by all per-raid-array threads to
7125 * deal with generic issues like resync and super-block update.
7126 * Raid personalities that don't have a thread (linear/raid0) do not
7127 * need this as they never do any recovery or update the superblock.
7129 * It does not do any resync itself, but rather "forks" off other threads
7130 * to do that as needed.
7131 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7132 * "->recovery" and create a thread at ->sync_thread.
7133 * When the thread finishes it sets MD_RECOVERY_DONE
7134 * and wakeups up this thread which will reap the thread and finish up.
7135 * This thread also removes any faulty devices (with nr_pending == 0).
7137 * The overall approach is:
7138 * 1/ if the superblock needs updating, update it.
7139 * 2/ If a recovery thread is running, don't do anything else.
7140 * 3/ If recovery has finished, clean up, possibly marking spares active.
7141 * 4/ If there are any faulty devices, remove them.
7142 * 5/ If array is degraded, try to add spares devices
7143 * 6/ If array has spares or is not in-sync, start a resync thread.
7145 void md_check_recovery(mddev_t
*mddev
)
7148 bitmap_daemon_work(mddev
);
7153 if (signal_pending(current
)) {
7154 if (mddev
->pers
->sync_request
&& !mddev
->external
) {
7155 printk(KERN_INFO
"md: %s in immediate safe mode\n",
7157 mddev
->safemode
= 2;
7159 flush_signals(current
);
7162 if (mddev
->ro
&& !test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
7165 (mddev
->flags
& ~ (1<<MD_CHANGE_PENDING
)) ||
7166 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
7167 test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
7168 (mddev
->external
== 0 && mddev
->safemode
== 1) ||
7169 (mddev
->safemode
== 2 && ! atomic_read(&mddev
->writes_pending
)
7170 && !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
7174 if (mddev_trylock(mddev
)) {
7178 /* Only thing we do on a ro array is remove
7182 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
7183 if (rdev
->raid_disk
>= 0 &&
7184 !test_bit(Blocked
, &rdev
->flags
) &&
7185 test_bit(Faulty
, &rdev
->flags
) &&
7186 atomic_read(&rdev
->nr_pending
)==0) {
7187 if (mddev
->pers
->hot_remove_disk(
7188 mddev
, rdev
->raid_disk
)==0) {
7190 sprintf(nm
,"rd%d", rdev
->raid_disk
);
7191 sysfs_remove_link(&mddev
->kobj
, nm
);
7192 rdev
->raid_disk
= -1;
7195 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7199 if (!mddev
->external
) {
7201 spin_lock_irq(&mddev
->write_lock
);
7202 if (mddev
->safemode
&&
7203 !atomic_read(&mddev
->writes_pending
) &&
7205 mddev
->recovery_cp
== MaxSector
) {
7208 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
7210 if (mddev
->safemode
== 1)
7211 mddev
->safemode
= 0;
7212 spin_unlock_irq(&mddev
->write_lock
);
7214 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
7218 md_update_sb(mddev
, 0);
7220 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
7221 !test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
)) {
7222 /* resync/recovery still happening */
7223 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7226 if (mddev
->sync_thread
) {
7227 reap_sync_thread(mddev
);
7230 /* Set RUNNING before clearing NEEDED to avoid
7231 * any transients in the value of "sync_action".
7233 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7234 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7235 /* Clear some bits that don't mean anything, but
7238 clear_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7239 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
7241 if (test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
7243 /* no recovery is running.
7244 * remove any failed drives, then
7245 * add spares if possible.
7246 * Spare are also removed and re-added, to allow
7247 * the personality to fail the re-add.
7250 if (mddev
->reshape_position
!= MaxSector
) {
7251 if (mddev
->pers
->check_reshape
== NULL
||
7252 mddev
->pers
->check_reshape(mddev
) != 0)
7253 /* Cannot proceed */
7255 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7256 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7257 } else if ((spares
= remove_and_add_spares(mddev
))) {
7258 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7259 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7260 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
7261 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7262 } else if (mddev
->recovery_cp
< MaxSector
) {
7263 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7264 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7265 } else if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
7266 /* nothing to be done ... */
7269 if (mddev
->pers
->sync_request
) {
7270 if (spares
&& mddev
->bitmap
&& ! mddev
->bitmap
->file
) {
7271 /* We are adding a device or devices to an array
7272 * which has the bitmap stored on all devices.
7273 * So make sure all bitmap pages get written
7275 bitmap_write_all(mddev
->bitmap
);
7277 mddev
->sync_thread
= md_register_thread(md_do_sync
,
7280 if (!mddev
->sync_thread
) {
7281 printk(KERN_ERR
"%s: could not start resync"
7284 /* leave the spares where they are, it shouldn't hurt */
7285 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7286 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7287 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7288 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
7289 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7291 md_wakeup_thread(mddev
->sync_thread
);
7292 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7293 md_new_event(mddev
);
7296 if (!mddev
->sync_thread
) {
7297 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7298 if (test_and_clear_bit(MD_RECOVERY_RECOVER
,
7300 if (mddev
->sysfs_action
)
7301 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7303 mddev_unlock(mddev
);
7307 void md_wait_for_blocked_rdev(mdk_rdev_t
*rdev
, mddev_t
*mddev
)
7309 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
7310 wait_event_timeout(rdev
->blocked_wait
,
7311 !test_bit(Blocked
, &rdev
->flags
),
7312 msecs_to_jiffies(5000));
7313 rdev_dec_pending(rdev
, mddev
);
7315 EXPORT_SYMBOL(md_wait_for_blocked_rdev
);
7317 static int md_notify_reboot(struct notifier_block
*this,
7318 unsigned long code
, void *x
)
7320 struct list_head
*tmp
;
7323 if ((code
== SYS_DOWN
) || (code
== SYS_HALT
) || (code
== SYS_POWER_OFF
)) {
7325 printk(KERN_INFO
"md: stopping all md devices.\n");
7327 for_each_mddev(mddev
, tmp
)
7328 if (mddev_trylock(mddev
)) {
7329 /* Force a switch to readonly even array
7330 * appears to still be in use. Hence
7333 md_set_readonly(mddev
, 100);
7334 mddev_unlock(mddev
);
7337 * certain more exotic SCSI devices are known to be
7338 * volatile wrt too early system reboots. While the
7339 * right place to handle this issue is the given
7340 * driver, we do want to have a safe RAID driver ...
7347 static struct notifier_block md_notifier
= {
7348 .notifier_call
= md_notify_reboot
,
7350 .priority
= INT_MAX
, /* before any real devices */
7353 static void md_geninit(void)
7355 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t
));
7357 proc_create("mdstat", S_IRUGO
, NULL
, &md_seq_fops
);
7360 static int __init
md_init(void)
7364 md_wq
= alloc_workqueue("md", WQ_MEM_RECLAIM
, 0);
7368 md_misc_wq
= alloc_workqueue("md_misc", 0, 0);
7372 if ((ret
= register_blkdev(MD_MAJOR
, "md")) < 0)
7375 if ((ret
= register_blkdev(0, "mdp")) < 0)
7379 blk_register_region(MKDEV(MD_MAJOR
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
7380 md_probe
, NULL
, NULL
);
7381 blk_register_region(MKDEV(mdp_major
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
7382 md_probe
, NULL
, NULL
);
7384 register_reboot_notifier(&md_notifier
);
7385 raid_table_header
= register_sysctl_table(raid_root_table
);
7391 unregister_blkdev(MD_MAJOR
, "md");
7393 destroy_workqueue(md_misc_wq
);
7395 destroy_workqueue(md_wq
);
7403 * Searches all registered partitions for autorun RAID arrays
7407 static LIST_HEAD(all_detected_devices
);
7408 struct detected_devices_node
{
7409 struct list_head list
;
7413 void md_autodetect_dev(dev_t dev
)
7415 struct detected_devices_node
*node_detected_dev
;
7417 node_detected_dev
= kzalloc(sizeof(*node_detected_dev
), GFP_KERNEL
);
7418 if (node_detected_dev
) {
7419 node_detected_dev
->dev
= dev
;
7420 list_add_tail(&node_detected_dev
->list
, &all_detected_devices
);
7422 printk(KERN_CRIT
"md: md_autodetect_dev: kzalloc failed"
7423 ", skipping dev(%d,%d)\n", MAJOR(dev
), MINOR(dev
));
7428 static void autostart_arrays(int part
)
7431 struct detected_devices_node
*node_detected_dev
;
7433 int i_scanned
, i_passed
;
7438 printk(KERN_INFO
"md: Autodetecting RAID arrays.\n");
7440 while (!list_empty(&all_detected_devices
) && i_scanned
< INT_MAX
) {
7442 node_detected_dev
= list_entry(all_detected_devices
.next
,
7443 struct detected_devices_node
, list
);
7444 list_del(&node_detected_dev
->list
);
7445 dev
= node_detected_dev
->dev
;
7446 kfree(node_detected_dev
);
7447 rdev
= md_import_device(dev
,0, 90);
7451 if (test_bit(Faulty
, &rdev
->flags
)) {
7455 set_bit(AutoDetected
, &rdev
->flags
);
7456 list_add(&rdev
->same_set
, &pending_raid_disks
);
7460 printk(KERN_INFO
"md: Scanned %d and added %d devices.\n",
7461 i_scanned
, i_passed
);
7463 autorun_devices(part
);
7466 #endif /* !MODULE */
7468 static __exit
void md_exit(void)
7471 struct list_head
*tmp
;
7473 blk_unregister_region(MKDEV(MD_MAJOR
,0), 1U << MINORBITS
);
7474 blk_unregister_region(MKDEV(mdp_major
,0), 1U << MINORBITS
);
7476 unregister_blkdev(MD_MAJOR
,"md");
7477 unregister_blkdev(mdp_major
, "mdp");
7478 unregister_reboot_notifier(&md_notifier
);
7479 unregister_sysctl_table(raid_table_header
);
7480 remove_proc_entry("mdstat", NULL
);
7481 for_each_mddev(mddev
, tmp
) {
7482 export_array(mddev
);
7483 mddev
->hold_active
= 0;
7485 destroy_workqueue(md_misc_wq
);
7486 destroy_workqueue(md_wq
);
7489 subsys_initcall(md_init
);
7490 module_exit(md_exit
)
7492 static int get_ro(char *buffer
, struct kernel_param
*kp
)
7494 return sprintf(buffer
, "%d", start_readonly
);
7496 static int set_ro(const char *val
, struct kernel_param
*kp
)
7499 int num
= simple_strtoul(val
, &e
, 10);
7500 if (*val
&& (*e
== '\0' || *e
== '\n')) {
7501 start_readonly
= num
;
7507 module_param_call(start_ro
, set_ro
, get_ro
, NULL
, S_IRUSR
|S_IWUSR
);
7508 module_param(start_dirty_degraded
, int, S_IRUGO
|S_IWUSR
);
7510 module_param_call(new_array
, add_named_array
, NULL
, NULL
, S_IWUSR
);
7512 EXPORT_SYMBOL(register_md_personality
);
7513 EXPORT_SYMBOL(unregister_md_personality
);
7514 EXPORT_SYMBOL(md_error
);
7515 EXPORT_SYMBOL(md_done_sync
);
7516 EXPORT_SYMBOL(md_write_start
);
7517 EXPORT_SYMBOL(md_write_end
);
7518 EXPORT_SYMBOL(md_register_thread
);
7519 EXPORT_SYMBOL(md_unregister_thread
);
7520 EXPORT_SYMBOL(md_wakeup_thread
);
7521 EXPORT_SYMBOL(md_check_recovery
);
7522 MODULE_LICENSE("GPL");
7523 MODULE_DESCRIPTION("MD RAID framework");
7525 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR
);