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_FLUSH
| REQ_FUA
, bio
);
786 void md_super_wait(mddev_t
*mddev
)
788 /* wait for all superblock writes that were scheduled to complete */
791 prepare_to_wait(&mddev
->sb_wait
, &wq
, TASK_UNINTERRUPTIBLE
);
792 if (atomic_read(&mddev
->pending_writes
)==0)
796 finish_wait(&mddev
->sb_wait
, &wq
);
799 static void bi_complete(struct bio
*bio
, int error
)
801 complete((struct completion
*)bio
->bi_private
);
804 int sync_page_io(mdk_rdev_t
*rdev
, sector_t sector
, int size
,
805 struct page
*page
, int rw
, bool metadata_op
)
807 struct bio
*bio
= bio_alloc_mddev(GFP_NOIO
, 1, rdev
->mddev
);
808 struct completion event
;
813 bio
->bi_bdev
= (metadata_op
&& rdev
->meta_bdev
) ?
814 rdev
->meta_bdev
: rdev
->bdev
;
816 bio
->bi_sector
= sector
+ rdev
->sb_start
;
818 bio
->bi_sector
= sector
+ rdev
->data_offset
;
819 bio_add_page(bio
, page
, size
, 0);
820 init_completion(&event
);
821 bio
->bi_private
= &event
;
822 bio
->bi_end_io
= bi_complete
;
824 wait_for_completion(&event
);
826 ret
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
830 EXPORT_SYMBOL_GPL(sync_page_io
);
832 static int read_disk_sb(mdk_rdev_t
* rdev
, int size
)
834 char b
[BDEVNAME_SIZE
];
835 if (!rdev
->sb_page
) {
843 if (!sync_page_io(rdev
, 0, size
, rdev
->sb_page
, READ
, true))
849 printk(KERN_WARNING
"md: disabled device %s, could not read superblock.\n",
850 bdevname(rdev
->bdev
,b
));
854 static int uuid_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
856 return sb1
->set_uuid0
== sb2
->set_uuid0
&&
857 sb1
->set_uuid1
== sb2
->set_uuid1
&&
858 sb1
->set_uuid2
== sb2
->set_uuid2
&&
859 sb1
->set_uuid3
== sb2
->set_uuid3
;
862 static int sb_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
865 mdp_super_t
*tmp1
, *tmp2
;
867 tmp1
= kmalloc(sizeof(*tmp1
),GFP_KERNEL
);
868 tmp2
= kmalloc(sizeof(*tmp2
),GFP_KERNEL
);
870 if (!tmp1
|| !tmp2
) {
872 printk(KERN_INFO
"md.c sb_equal(): failed to allocate memory!\n");
880 * nr_disks is not constant
885 ret
= (memcmp(tmp1
, tmp2
, MD_SB_GENERIC_CONSTANT_WORDS
* 4) == 0);
893 static u32
md_csum_fold(u32 csum
)
895 csum
= (csum
& 0xffff) + (csum
>> 16);
896 return (csum
& 0xffff) + (csum
>> 16);
899 static unsigned int calc_sb_csum(mdp_super_t
* sb
)
902 u32
*sb32
= (u32
*)sb
;
904 unsigned int disk_csum
, csum
;
906 disk_csum
= sb
->sb_csum
;
909 for (i
= 0; i
< MD_SB_BYTES
/4 ; i
++)
911 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
915 /* This used to use csum_partial, which was wrong for several
916 * reasons including that different results are returned on
917 * different architectures. It isn't critical that we get exactly
918 * the same return value as before (we always csum_fold before
919 * testing, and that removes any differences). However as we
920 * know that csum_partial always returned a 16bit value on
921 * alphas, do a fold to maximise conformity to previous behaviour.
923 sb
->sb_csum
= md_csum_fold(disk_csum
);
925 sb
->sb_csum
= disk_csum
;
932 * Handle superblock details.
933 * We want to be able to handle multiple superblock formats
934 * so we have a common interface to them all, and an array of
935 * different handlers.
936 * We rely on user-space to write the initial superblock, and support
937 * reading and updating of superblocks.
938 * Interface methods are:
939 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
940 * loads and validates a superblock on dev.
941 * if refdev != NULL, compare superblocks on both devices
943 * 0 - dev has a superblock that is compatible with refdev
944 * 1 - dev has a superblock that is compatible and newer than refdev
945 * so dev should be used as the refdev in future
946 * -EINVAL superblock incompatible or invalid
947 * -othererror e.g. -EIO
949 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
950 * Verify that dev is acceptable into mddev.
951 * The first time, mddev->raid_disks will be 0, and data from
952 * dev should be merged in. Subsequent calls check that dev
953 * is new enough. Return 0 or -EINVAL
955 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
956 * Update the superblock for rdev with data in mddev
957 * This does not write to disc.
963 struct module
*owner
;
964 int (*load_super
)(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
,
966 int (*validate_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
967 void (*sync_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
968 unsigned long long (*rdev_size_change
)(mdk_rdev_t
*rdev
,
969 sector_t num_sectors
);
973 * Check that the given mddev has no bitmap.
975 * This function is called from the run method of all personalities that do not
976 * support bitmaps. It prints an error message and returns non-zero if mddev
977 * has a bitmap. Otherwise, it returns 0.
980 int md_check_no_bitmap(mddev_t
*mddev
)
982 if (!mddev
->bitmap_info
.file
&& !mddev
->bitmap_info
.offset
)
984 printk(KERN_ERR
"%s: bitmaps are not supported for %s\n",
985 mdname(mddev
), mddev
->pers
->name
);
988 EXPORT_SYMBOL(md_check_no_bitmap
);
991 * load_super for 0.90.0
993 static int super_90_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
995 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1000 * Calculate the position of the superblock (512byte sectors),
1001 * it's at the end of the disk.
1003 * It also happens to be a multiple of 4Kb.
1005 rdev
->sb_start
= calc_dev_sboffset(rdev
);
1007 ret
= read_disk_sb(rdev
, MD_SB_BYTES
);
1008 if (ret
) return ret
;
1012 bdevname(rdev
->bdev
, b
);
1013 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
1015 if (sb
->md_magic
!= MD_SB_MAGIC
) {
1016 printk(KERN_ERR
"md: invalid raid superblock magic on %s\n",
1021 if (sb
->major_version
!= 0 ||
1022 sb
->minor_version
< 90 ||
1023 sb
->minor_version
> 91) {
1024 printk(KERN_WARNING
"Bad version number %d.%d on %s\n",
1025 sb
->major_version
, sb
->minor_version
,
1030 if (sb
->raid_disks
<= 0)
1033 if (md_csum_fold(calc_sb_csum(sb
)) != md_csum_fold(sb
->sb_csum
)) {
1034 printk(KERN_WARNING
"md: invalid superblock checksum on %s\n",
1039 rdev
->preferred_minor
= sb
->md_minor
;
1040 rdev
->data_offset
= 0;
1041 rdev
->sb_size
= MD_SB_BYTES
;
1043 if (sb
->level
== LEVEL_MULTIPATH
)
1046 rdev
->desc_nr
= sb
->this_disk
.number
;
1052 mdp_super_t
*refsb
= (mdp_super_t
*)page_address(refdev
->sb_page
);
1053 if (!uuid_equal(refsb
, sb
)) {
1054 printk(KERN_WARNING
"md: %s has different UUID to %s\n",
1055 b
, bdevname(refdev
->bdev
,b2
));
1058 if (!sb_equal(refsb
, sb
)) {
1059 printk(KERN_WARNING
"md: %s has same UUID"
1060 " but different superblock to %s\n",
1061 b
, bdevname(refdev
->bdev
, b2
));
1065 ev2
= md_event(refsb
);
1071 rdev
->sectors
= rdev
->sb_start
;
1073 if (rdev
->sectors
< sb
->size
* 2 && sb
->level
> 1)
1074 /* "this cannot possibly happen" ... */
1082 * validate_super for 0.90.0
1084 static int super_90_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1087 mdp_super_t
*sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
1088 __u64 ev1
= md_event(sb
);
1090 rdev
->raid_disk
= -1;
1091 clear_bit(Faulty
, &rdev
->flags
);
1092 clear_bit(In_sync
, &rdev
->flags
);
1093 clear_bit(WriteMostly
, &rdev
->flags
);
1095 if (mddev
->raid_disks
== 0) {
1096 mddev
->major_version
= 0;
1097 mddev
->minor_version
= sb
->minor_version
;
1098 mddev
->patch_version
= sb
->patch_version
;
1099 mddev
->external
= 0;
1100 mddev
->chunk_sectors
= sb
->chunk_size
>> 9;
1101 mddev
->ctime
= sb
->ctime
;
1102 mddev
->utime
= sb
->utime
;
1103 mddev
->level
= sb
->level
;
1104 mddev
->clevel
[0] = 0;
1105 mddev
->layout
= sb
->layout
;
1106 mddev
->raid_disks
= sb
->raid_disks
;
1107 mddev
->dev_sectors
= sb
->size
* 2;
1108 mddev
->events
= ev1
;
1109 mddev
->bitmap_info
.offset
= 0;
1110 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
1112 if (mddev
->minor_version
>= 91) {
1113 mddev
->reshape_position
= sb
->reshape_position
;
1114 mddev
->delta_disks
= sb
->delta_disks
;
1115 mddev
->new_level
= sb
->new_level
;
1116 mddev
->new_layout
= sb
->new_layout
;
1117 mddev
->new_chunk_sectors
= sb
->new_chunk
>> 9;
1119 mddev
->reshape_position
= MaxSector
;
1120 mddev
->delta_disks
= 0;
1121 mddev
->new_level
= mddev
->level
;
1122 mddev
->new_layout
= mddev
->layout
;
1123 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1126 if (sb
->state
& (1<<MD_SB_CLEAN
))
1127 mddev
->recovery_cp
= MaxSector
;
1129 if (sb
->events_hi
== sb
->cp_events_hi
&&
1130 sb
->events_lo
== sb
->cp_events_lo
) {
1131 mddev
->recovery_cp
= sb
->recovery_cp
;
1133 mddev
->recovery_cp
= 0;
1136 memcpy(mddev
->uuid
+0, &sb
->set_uuid0
, 4);
1137 memcpy(mddev
->uuid
+4, &sb
->set_uuid1
, 4);
1138 memcpy(mddev
->uuid
+8, &sb
->set_uuid2
, 4);
1139 memcpy(mddev
->uuid
+12,&sb
->set_uuid3
, 4);
1141 mddev
->max_disks
= MD_SB_DISKS
;
1143 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
) &&
1144 mddev
->bitmap_info
.file
== NULL
)
1145 mddev
->bitmap_info
.offset
=
1146 mddev
->bitmap_info
.default_offset
;
1148 } else if (mddev
->pers
== NULL
) {
1149 /* Insist on good event counter while assembling, except
1150 * for spares (which don't need an event count) */
1152 if (sb
->disks
[rdev
->desc_nr
].state
& (
1153 (1<<MD_DISK_SYNC
) | (1 << MD_DISK_ACTIVE
)))
1154 if (ev1
< mddev
->events
)
1156 } else if (mddev
->bitmap
) {
1157 /* if adding to array with a bitmap, then we can accept an
1158 * older device ... but not too old.
1160 if (ev1
< mddev
->bitmap
->events_cleared
)
1163 if (ev1
< mddev
->events
)
1164 /* just a hot-add of a new device, leave raid_disk at -1 */
1168 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1169 desc
= sb
->disks
+ rdev
->desc_nr
;
1171 if (desc
->state
& (1<<MD_DISK_FAULTY
))
1172 set_bit(Faulty
, &rdev
->flags
);
1173 else if (desc
->state
& (1<<MD_DISK_SYNC
) /* &&
1174 desc->raid_disk < mddev->raid_disks */) {
1175 set_bit(In_sync
, &rdev
->flags
);
1176 rdev
->raid_disk
= desc
->raid_disk
;
1177 } else if (desc
->state
& (1<<MD_DISK_ACTIVE
)) {
1178 /* active but not in sync implies recovery up to
1179 * reshape position. We don't know exactly where
1180 * that is, so set to zero for now */
1181 if (mddev
->minor_version
>= 91) {
1182 rdev
->recovery_offset
= 0;
1183 rdev
->raid_disk
= desc
->raid_disk
;
1186 if (desc
->state
& (1<<MD_DISK_WRITEMOSTLY
))
1187 set_bit(WriteMostly
, &rdev
->flags
);
1188 } else /* MULTIPATH are always insync */
1189 set_bit(In_sync
, &rdev
->flags
);
1194 * sync_super for 0.90.0
1196 static void super_90_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1200 int next_spare
= mddev
->raid_disks
;
1203 /* make rdev->sb match mddev data..
1206 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1207 * 3/ any empty disks < next_spare become removed
1209 * disks[0] gets initialised to REMOVED because
1210 * we cannot be sure from other fields if it has
1211 * been initialised or not.
1214 int active
=0, working
=0,failed
=0,spare
=0,nr_disks
=0;
1216 rdev
->sb_size
= MD_SB_BYTES
;
1218 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
1220 memset(sb
, 0, sizeof(*sb
));
1222 sb
->md_magic
= MD_SB_MAGIC
;
1223 sb
->major_version
= mddev
->major_version
;
1224 sb
->patch_version
= mddev
->patch_version
;
1225 sb
->gvalid_words
= 0; /* ignored */
1226 memcpy(&sb
->set_uuid0
, mddev
->uuid
+0, 4);
1227 memcpy(&sb
->set_uuid1
, mddev
->uuid
+4, 4);
1228 memcpy(&sb
->set_uuid2
, mddev
->uuid
+8, 4);
1229 memcpy(&sb
->set_uuid3
, mddev
->uuid
+12,4);
1231 sb
->ctime
= mddev
->ctime
;
1232 sb
->level
= mddev
->level
;
1233 sb
->size
= mddev
->dev_sectors
/ 2;
1234 sb
->raid_disks
= mddev
->raid_disks
;
1235 sb
->md_minor
= mddev
->md_minor
;
1236 sb
->not_persistent
= 0;
1237 sb
->utime
= mddev
->utime
;
1239 sb
->events_hi
= (mddev
->events
>>32);
1240 sb
->events_lo
= (u32
)mddev
->events
;
1242 if (mddev
->reshape_position
== MaxSector
)
1243 sb
->minor_version
= 90;
1245 sb
->minor_version
= 91;
1246 sb
->reshape_position
= mddev
->reshape_position
;
1247 sb
->new_level
= mddev
->new_level
;
1248 sb
->delta_disks
= mddev
->delta_disks
;
1249 sb
->new_layout
= mddev
->new_layout
;
1250 sb
->new_chunk
= mddev
->new_chunk_sectors
<< 9;
1252 mddev
->minor_version
= sb
->minor_version
;
1255 sb
->recovery_cp
= mddev
->recovery_cp
;
1256 sb
->cp_events_hi
= (mddev
->events
>>32);
1257 sb
->cp_events_lo
= (u32
)mddev
->events
;
1258 if (mddev
->recovery_cp
== MaxSector
)
1259 sb
->state
= (1<< MD_SB_CLEAN
);
1261 sb
->recovery_cp
= 0;
1263 sb
->layout
= mddev
->layout
;
1264 sb
->chunk_size
= mddev
->chunk_sectors
<< 9;
1266 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
)
1267 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
1269 sb
->disks
[0].state
= (1<<MD_DISK_REMOVED
);
1270 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
1273 int is_active
= test_bit(In_sync
, &rdev2
->flags
);
1275 if (rdev2
->raid_disk
>= 0 &&
1276 sb
->minor_version
>= 91)
1277 /* we have nowhere to store the recovery_offset,
1278 * but if it is not below the reshape_position,
1279 * we can piggy-back on that.
1282 if (rdev2
->raid_disk
< 0 ||
1283 test_bit(Faulty
, &rdev2
->flags
))
1286 desc_nr
= rdev2
->raid_disk
;
1288 desc_nr
= next_spare
++;
1289 rdev2
->desc_nr
= desc_nr
;
1290 d
= &sb
->disks
[rdev2
->desc_nr
];
1292 d
->number
= rdev2
->desc_nr
;
1293 d
->major
= MAJOR(rdev2
->bdev
->bd_dev
);
1294 d
->minor
= MINOR(rdev2
->bdev
->bd_dev
);
1296 d
->raid_disk
= rdev2
->raid_disk
;
1298 d
->raid_disk
= rdev2
->desc_nr
; /* compatibility */
1299 if (test_bit(Faulty
, &rdev2
->flags
))
1300 d
->state
= (1<<MD_DISK_FAULTY
);
1301 else if (is_active
) {
1302 d
->state
= (1<<MD_DISK_ACTIVE
);
1303 if (test_bit(In_sync
, &rdev2
->flags
))
1304 d
->state
|= (1<<MD_DISK_SYNC
);
1312 if (test_bit(WriteMostly
, &rdev2
->flags
))
1313 d
->state
|= (1<<MD_DISK_WRITEMOSTLY
);
1315 /* now set the "removed" and "faulty" bits on any missing devices */
1316 for (i
=0 ; i
< mddev
->raid_disks
; i
++) {
1317 mdp_disk_t
*d
= &sb
->disks
[i
];
1318 if (d
->state
== 0 && d
->number
== 0) {
1321 d
->state
= (1<<MD_DISK_REMOVED
);
1322 d
->state
|= (1<<MD_DISK_FAULTY
);
1326 sb
->nr_disks
= nr_disks
;
1327 sb
->active_disks
= active
;
1328 sb
->working_disks
= working
;
1329 sb
->failed_disks
= failed
;
1330 sb
->spare_disks
= spare
;
1332 sb
->this_disk
= sb
->disks
[rdev
->desc_nr
];
1333 sb
->sb_csum
= calc_sb_csum(sb
);
1337 * rdev_size_change for 0.90.0
1339 static unsigned long long
1340 super_90_rdev_size_change(mdk_rdev_t
*rdev
, sector_t num_sectors
)
1342 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1343 return 0; /* component must fit device */
1344 if (rdev
->mddev
->bitmap_info
.offset
)
1345 return 0; /* can't move bitmap */
1346 rdev
->sb_start
= calc_dev_sboffset(rdev
);
1347 if (!num_sectors
|| num_sectors
> rdev
->sb_start
)
1348 num_sectors
= rdev
->sb_start
;
1349 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1351 md_super_wait(rdev
->mddev
);
1357 * version 1 superblock
1360 static __le32
calc_sb_1_csum(struct mdp_superblock_1
* sb
)
1364 unsigned long long newcsum
;
1365 int size
= 256 + le32_to_cpu(sb
->max_dev
)*2;
1366 __le32
*isuper
= (__le32
*)sb
;
1369 disk_csum
= sb
->sb_csum
;
1372 for (i
=0; size
>=4; size
-= 4 )
1373 newcsum
+= le32_to_cpu(*isuper
++);
1376 newcsum
+= le16_to_cpu(*(__le16
*) isuper
);
1378 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
1379 sb
->sb_csum
= disk_csum
;
1380 return cpu_to_le32(csum
);
1383 static int super_1_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
1385 struct mdp_superblock_1
*sb
;
1388 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1392 * Calculate the position of the superblock in 512byte sectors.
1393 * It is always aligned to a 4K boundary and
1394 * depeding on minor_version, it can be:
1395 * 0: At least 8K, but less than 12K, from end of device
1396 * 1: At start of device
1397 * 2: 4K from start of device.
1399 switch(minor_version
) {
1401 sb_start
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
1403 sb_start
&= ~(sector_t
)(4*2-1);
1414 rdev
->sb_start
= sb_start
;
1416 /* superblock is rarely larger than 1K, but it can be larger,
1417 * and it is safe to read 4k, so we do that
1419 ret
= read_disk_sb(rdev
, 4096);
1420 if (ret
) return ret
;
1423 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1425 if (sb
->magic
!= cpu_to_le32(MD_SB_MAGIC
) ||
1426 sb
->major_version
!= cpu_to_le32(1) ||
1427 le32_to_cpu(sb
->max_dev
) > (4096-256)/2 ||
1428 le64_to_cpu(sb
->super_offset
) != rdev
->sb_start
||
1429 (le32_to_cpu(sb
->feature_map
) & ~MD_FEATURE_ALL
) != 0)
1432 if (calc_sb_1_csum(sb
) != sb
->sb_csum
) {
1433 printk("md: invalid superblock checksum on %s\n",
1434 bdevname(rdev
->bdev
,b
));
1437 if (le64_to_cpu(sb
->data_size
) < 10) {
1438 printk("md: data_size too small on %s\n",
1439 bdevname(rdev
->bdev
,b
));
1443 rdev
->preferred_minor
= 0xffff;
1444 rdev
->data_offset
= le64_to_cpu(sb
->data_offset
);
1445 atomic_set(&rdev
->corrected_errors
, le32_to_cpu(sb
->cnt_corrected_read
));
1447 rdev
->sb_size
= le32_to_cpu(sb
->max_dev
) * 2 + 256;
1448 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1449 if (rdev
->sb_size
& bmask
)
1450 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1453 && rdev
->data_offset
< sb_start
+ (rdev
->sb_size
/512))
1456 if (sb
->level
== cpu_to_le32(LEVEL_MULTIPATH
))
1459 rdev
->desc_nr
= le32_to_cpu(sb
->dev_number
);
1465 struct mdp_superblock_1
*refsb
=
1466 (struct mdp_superblock_1
*)page_address(refdev
->sb_page
);
1468 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16) != 0 ||
1469 sb
->level
!= refsb
->level
||
1470 sb
->layout
!= refsb
->layout
||
1471 sb
->chunksize
!= refsb
->chunksize
) {
1472 printk(KERN_WARNING
"md: %s has strangely different"
1473 " superblock to %s\n",
1474 bdevname(rdev
->bdev
,b
),
1475 bdevname(refdev
->bdev
,b2
));
1478 ev1
= le64_to_cpu(sb
->events
);
1479 ev2
= le64_to_cpu(refsb
->events
);
1487 rdev
->sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) -
1488 le64_to_cpu(sb
->data_offset
);
1490 rdev
->sectors
= rdev
->sb_start
;
1491 if (rdev
->sectors
< le64_to_cpu(sb
->data_size
))
1493 rdev
->sectors
= le64_to_cpu(sb
->data_size
);
1494 if (le64_to_cpu(sb
->size
) > rdev
->sectors
)
1499 static int super_1_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1501 struct mdp_superblock_1
*sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1502 __u64 ev1
= le64_to_cpu(sb
->events
);
1504 rdev
->raid_disk
= -1;
1505 clear_bit(Faulty
, &rdev
->flags
);
1506 clear_bit(In_sync
, &rdev
->flags
);
1507 clear_bit(WriteMostly
, &rdev
->flags
);
1509 if (mddev
->raid_disks
== 0) {
1510 mddev
->major_version
= 1;
1511 mddev
->patch_version
= 0;
1512 mddev
->external
= 0;
1513 mddev
->chunk_sectors
= le32_to_cpu(sb
->chunksize
);
1514 mddev
->ctime
= le64_to_cpu(sb
->ctime
) & ((1ULL << 32)-1);
1515 mddev
->utime
= le64_to_cpu(sb
->utime
) & ((1ULL << 32)-1);
1516 mddev
->level
= le32_to_cpu(sb
->level
);
1517 mddev
->clevel
[0] = 0;
1518 mddev
->layout
= le32_to_cpu(sb
->layout
);
1519 mddev
->raid_disks
= le32_to_cpu(sb
->raid_disks
);
1520 mddev
->dev_sectors
= le64_to_cpu(sb
->size
);
1521 mddev
->events
= ev1
;
1522 mddev
->bitmap_info
.offset
= 0;
1523 mddev
->bitmap_info
.default_offset
= 1024 >> 9;
1525 mddev
->recovery_cp
= le64_to_cpu(sb
->resync_offset
);
1526 memcpy(mddev
->uuid
, sb
->set_uuid
, 16);
1528 mddev
->max_disks
= (4096-256)/2;
1530 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BITMAP_OFFSET
) &&
1531 mddev
->bitmap_info
.file
== NULL
)
1532 mddev
->bitmap_info
.offset
=
1533 (__s32
)le32_to_cpu(sb
->bitmap_offset
);
1535 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
1536 mddev
->reshape_position
= le64_to_cpu(sb
->reshape_position
);
1537 mddev
->delta_disks
= le32_to_cpu(sb
->delta_disks
);
1538 mddev
->new_level
= le32_to_cpu(sb
->new_level
);
1539 mddev
->new_layout
= le32_to_cpu(sb
->new_layout
);
1540 mddev
->new_chunk_sectors
= le32_to_cpu(sb
->new_chunk
);
1542 mddev
->reshape_position
= MaxSector
;
1543 mddev
->delta_disks
= 0;
1544 mddev
->new_level
= mddev
->level
;
1545 mddev
->new_layout
= mddev
->layout
;
1546 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1549 } else if (mddev
->pers
== NULL
) {
1550 /* Insist of good event counter while assembling, except for
1551 * spares (which don't need an event count) */
1553 if (rdev
->desc_nr
>= 0 &&
1554 rdev
->desc_nr
< le32_to_cpu(sb
->max_dev
) &&
1555 le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]) < 0xfffe)
1556 if (ev1
< mddev
->events
)
1558 } else if (mddev
->bitmap
) {
1559 /* If adding to array with a bitmap, then we can accept an
1560 * older device, but not too old.
1562 if (ev1
< mddev
->bitmap
->events_cleared
)
1565 if (ev1
< mddev
->events
)
1566 /* just a hot-add of a new device, leave raid_disk at -1 */
1569 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1571 if (rdev
->desc_nr
< 0 ||
1572 rdev
->desc_nr
>= le32_to_cpu(sb
->max_dev
)) {
1576 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
1578 case 0xffff: /* spare */
1580 case 0xfffe: /* faulty */
1581 set_bit(Faulty
, &rdev
->flags
);
1584 if ((le32_to_cpu(sb
->feature_map
) &
1585 MD_FEATURE_RECOVERY_OFFSET
))
1586 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
1588 set_bit(In_sync
, &rdev
->flags
);
1589 rdev
->raid_disk
= role
;
1592 if (sb
->devflags
& WriteMostly1
)
1593 set_bit(WriteMostly
, &rdev
->flags
);
1594 } else /* MULTIPATH are always insync */
1595 set_bit(In_sync
, &rdev
->flags
);
1600 static void super_1_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1602 struct mdp_superblock_1
*sb
;
1605 /* make rdev->sb match mddev and rdev data. */
1607 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1609 sb
->feature_map
= 0;
1611 sb
->recovery_offset
= cpu_to_le64(0);
1612 memset(sb
->pad1
, 0, sizeof(sb
->pad1
));
1613 memset(sb
->pad2
, 0, sizeof(sb
->pad2
));
1614 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
1616 sb
->utime
= cpu_to_le64((__u64
)mddev
->utime
);
1617 sb
->events
= cpu_to_le64(mddev
->events
);
1619 sb
->resync_offset
= cpu_to_le64(mddev
->recovery_cp
);
1621 sb
->resync_offset
= cpu_to_le64(0);
1623 sb
->cnt_corrected_read
= cpu_to_le32(atomic_read(&rdev
->corrected_errors
));
1625 sb
->raid_disks
= cpu_to_le32(mddev
->raid_disks
);
1626 sb
->size
= cpu_to_le64(mddev
->dev_sectors
);
1627 sb
->chunksize
= cpu_to_le32(mddev
->chunk_sectors
);
1628 sb
->level
= cpu_to_le32(mddev
->level
);
1629 sb
->layout
= cpu_to_le32(mddev
->layout
);
1631 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
) {
1632 sb
->bitmap_offset
= cpu_to_le32((__u32
)mddev
->bitmap_info
.offset
);
1633 sb
->feature_map
= cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
);
1636 if (rdev
->raid_disk
>= 0 &&
1637 !test_bit(In_sync
, &rdev
->flags
)) {
1639 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET
);
1640 sb
->recovery_offset
=
1641 cpu_to_le64(rdev
->recovery_offset
);
1644 if (mddev
->reshape_position
!= MaxSector
) {
1645 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE
);
1646 sb
->reshape_position
= cpu_to_le64(mddev
->reshape_position
);
1647 sb
->new_layout
= cpu_to_le32(mddev
->new_layout
);
1648 sb
->delta_disks
= cpu_to_le32(mddev
->delta_disks
);
1649 sb
->new_level
= cpu_to_le32(mddev
->new_level
);
1650 sb
->new_chunk
= cpu_to_le32(mddev
->new_chunk_sectors
);
1654 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
)
1655 if (rdev2
->desc_nr
+1 > max_dev
)
1656 max_dev
= rdev2
->desc_nr
+1;
1658 if (max_dev
> le32_to_cpu(sb
->max_dev
)) {
1660 sb
->max_dev
= cpu_to_le32(max_dev
);
1661 rdev
->sb_size
= max_dev
* 2 + 256;
1662 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1663 if (rdev
->sb_size
& bmask
)
1664 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1666 max_dev
= le32_to_cpu(sb
->max_dev
);
1668 for (i
=0; i
<max_dev
;i
++)
1669 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1671 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
1673 if (test_bit(Faulty
, &rdev2
->flags
))
1674 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1675 else if (test_bit(In_sync
, &rdev2
->flags
))
1676 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1677 else if (rdev2
->raid_disk
>= 0)
1678 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1680 sb
->dev_roles
[i
] = cpu_to_le16(0xffff);
1683 sb
->sb_csum
= calc_sb_1_csum(sb
);
1686 static unsigned long long
1687 super_1_rdev_size_change(mdk_rdev_t
*rdev
, sector_t num_sectors
)
1689 struct mdp_superblock_1
*sb
;
1690 sector_t max_sectors
;
1691 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1692 return 0; /* component must fit device */
1693 if (rdev
->sb_start
< rdev
->data_offset
) {
1694 /* minor versions 1 and 2; superblock before data */
1695 max_sectors
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
1696 max_sectors
-= rdev
->data_offset
;
1697 if (!num_sectors
|| num_sectors
> max_sectors
)
1698 num_sectors
= max_sectors
;
1699 } else if (rdev
->mddev
->bitmap_info
.offset
) {
1700 /* minor version 0 with bitmap we can't move */
1703 /* minor version 0; superblock after data */
1705 sb_start
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) - 8*2;
1706 sb_start
&= ~(sector_t
)(4*2 - 1);
1707 max_sectors
= rdev
->sectors
+ sb_start
- rdev
->sb_start
;
1708 if (!num_sectors
|| num_sectors
> max_sectors
)
1709 num_sectors
= max_sectors
;
1710 rdev
->sb_start
= sb_start
;
1712 sb
= (struct mdp_superblock_1
*) page_address(rdev
->sb_page
);
1713 sb
->data_size
= cpu_to_le64(num_sectors
);
1714 sb
->super_offset
= rdev
->sb_start
;
1715 sb
->sb_csum
= calc_sb_1_csum(sb
);
1716 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1718 md_super_wait(rdev
->mddev
);
1722 static struct super_type super_types
[] = {
1725 .owner
= THIS_MODULE
,
1726 .load_super
= super_90_load
,
1727 .validate_super
= super_90_validate
,
1728 .sync_super
= super_90_sync
,
1729 .rdev_size_change
= super_90_rdev_size_change
,
1733 .owner
= THIS_MODULE
,
1734 .load_super
= super_1_load
,
1735 .validate_super
= super_1_validate
,
1736 .sync_super
= super_1_sync
,
1737 .rdev_size_change
= super_1_rdev_size_change
,
1741 static int match_mddev_units(mddev_t
*mddev1
, mddev_t
*mddev2
)
1743 mdk_rdev_t
*rdev
, *rdev2
;
1746 rdev_for_each_rcu(rdev
, mddev1
)
1747 rdev_for_each_rcu(rdev2
, mddev2
)
1748 if (rdev
->bdev
->bd_contains
==
1749 rdev2
->bdev
->bd_contains
) {
1757 static LIST_HEAD(pending_raid_disks
);
1760 * Try to register data integrity profile for an mddev
1762 * This is called when an array is started and after a disk has been kicked
1763 * from the array. It only succeeds if all working and active component devices
1764 * are integrity capable with matching profiles.
1766 int md_integrity_register(mddev_t
*mddev
)
1768 mdk_rdev_t
*rdev
, *reference
= NULL
;
1770 if (list_empty(&mddev
->disks
))
1771 return 0; /* nothing to do */
1772 if (blk_get_integrity(mddev
->gendisk
))
1773 return 0; /* already registered */
1774 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
1775 /* skip spares and non-functional disks */
1776 if (test_bit(Faulty
, &rdev
->flags
))
1778 if (rdev
->raid_disk
< 0)
1781 * If at least one rdev is not integrity capable, we can not
1782 * enable data integrity for the md device.
1784 if (!bdev_get_integrity(rdev
->bdev
))
1787 /* Use the first rdev as the reference */
1791 /* does this rdev's profile match the reference profile? */
1792 if (blk_integrity_compare(reference
->bdev
->bd_disk
,
1793 rdev
->bdev
->bd_disk
) < 0)
1797 * All component devices are integrity capable and have matching
1798 * profiles, register the common profile for the md device.
1800 if (blk_integrity_register(mddev
->gendisk
,
1801 bdev_get_integrity(reference
->bdev
)) != 0) {
1802 printk(KERN_ERR
"md: failed to register integrity for %s\n",
1806 printk(KERN_NOTICE
"md: data integrity enabled on %s\n", mdname(mddev
));
1807 if (bioset_integrity_create(mddev
->bio_set
, BIO_POOL_SIZE
)) {
1808 printk(KERN_ERR
"md: failed to create integrity pool for %s\n",
1814 EXPORT_SYMBOL(md_integrity_register
);
1816 /* Disable data integrity if non-capable/non-matching disk is being added */
1817 void md_integrity_add_rdev(mdk_rdev_t
*rdev
, mddev_t
*mddev
)
1819 struct blk_integrity
*bi_rdev
= bdev_get_integrity(rdev
->bdev
);
1820 struct blk_integrity
*bi_mddev
= blk_get_integrity(mddev
->gendisk
);
1822 if (!bi_mddev
) /* nothing to do */
1824 if (rdev
->raid_disk
< 0) /* skip spares */
1826 if (bi_rdev
&& blk_integrity_compare(mddev
->gendisk
,
1827 rdev
->bdev
->bd_disk
) >= 0)
1829 printk(KERN_NOTICE
"disabling data integrity on %s\n", mdname(mddev
));
1830 blk_integrity_unregister(mddev
->gendisk
);
1832 EXPORT_SYMBOL(md_integrity_add_rdev
);
1834 static int bind_rdev_to_array(mdk_rdev_t
* rdev
, mddev_t
* mddev
)
1836 char b
[BDEVNAME_SIZE
];
1846 /* prevent duplicates */
1847 if (find_rdev(mddev
, rdev
->bdev
->bd_dev
))
1850 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1851 if (rdev
->sectors
&& (mddev
->dev_sectors
== 0 ||
1852 rdev
->sectors
< mddev
->dev_sectors
)) {
1854 /* Cannot change size, so fail
1855 * If mddev->level <= 0, then we don't care
1856 * about aligning sizes (e.g. linear)
1858 if (mddev
->level
> 0)
1861 mddev
->dev_sectors
= rdev
->sectors
;
1864 /* Verify rdev->desc_nr is unique.
1865 * If it is -1, assign a free number, else
1866 * check number is not in use
1868 if (rdev
->desc_nr
< 0) {
1870 if (mddev
->pers
) choice
= mddev
->raid_disks
;
1871 while (find_rdev_nr(mddev
, choice
))
1873 rdev
->desc_nr
= choice
;
1875 if (find_rdev_nr(mddev
, rdev
->desc_nr
))
1878 if (mddev
->max_disks
&& rdev
->desc_nr
>= mddev
->max_disks
) {
1879 printk(KERN_WARNING
"md: %s: array is limited to %d devices\n",
1880 mdname(mddev
), mddev
->max_disks
);
1883 bdevname(rdev
->bdev
,b
);
1884 while ( (s
=strchr(b
, '/')) != NULL
)
1887 rdev
->mddev
= mddev
;
1888 printk(KERN_INFO
"md: bind<%s>\n", b
);
1890 if ((err
= kobject_add(&rdev
->kobj
, &mddev
->kobj
, "dev-%s", b
)))
1893 ko
= &part_to_dev(rdev
->bdev
->bd_part
)->kobj
;
1894 if (sysfs_create_link(&rdev
->kobj
, ko
, "block"))
1895 /* failure here is OK */;
1896 rdev
->sysfs_state
= sysfs_get_dirent_safe(rdev
->kobj
.sd
, "state");
1898 list_add_rcu(&rdev
->same_set
, &mddev
->disks
);
1899 bd_link_disk_holder(rdev
->bdev
, mddev
->gendisk
);
1901 /* May as well allow recovery to be retried once */
1902 mddev
->recovery_disabled
= 0;
1907 printk(KERN_WARNING
"md: failed to register dev-%s for %s\n",
1912 static void md_delayed_delete(struct work_struct
*ws
)
1914 mdk_rdev_t
*rdev
= container_of(ws
, mdk_rdev_t
, del_work
);
1915 kobject_del(&rdev
->kobj
);
1916 kobject_put(&rdev
->kobj
);
1919 static void unbind_rdev_from_array(mdk_rdev_t
* rdev
)
1921 char b
[BDEVNAME_SIZE
];
1926 bd_unlink_disk_holder(rdev
->bdev
, rdev
->mddev
->gendisk
);
1927 list_del_rcu(&rdev
->same_set
);
1928 printk(KERN_INFO
"md: unbind<%s>\n", bdevname(rdev
->bdev
,b
));
1930 sysfs_remove_link(&rdev
->kobj
, "block");
1931 sysfs_put(rdev
->sysfs_state
);
1932 rdev
->sysfs_state
= NULL
;
1933 /* We need to delay this, otherwise we can deadlock when
1934 * writing to 'remove' to "dev/state". We also need
1935 * to delay it due to rcu usage.
1938 INIT_WORK(&rdev
->del_work
, md_delayed_delete
);
1939 kobject_get(&rdev
->kobj
);
1940 queue_work(md_misc_wq
, &rdev
->del_work
);
1944 * prevent the device from being mounted, repartitioned or
1945 * otherwise reused by a RAID array (or any other kernel
1946 * subsystem), by bd_claiming the device.
1948 static int lock_rdev(mdk_rdev_t
*rdev
, dev_t dev
, int shared
)
1951 struct block_device
*bdev
;
1952 char b
[BDEVNAME_SIZE
];
1954 bdev
= blkdev_get_by_dev(dev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
,
1955 shared
? (mdk_rdev_t
*)lock_rdev
: rdev
);
1957 printk(KERN_ERR
"md: could not open %s.\n",
1958 __bdevname(dev
, b
));
1959 return PTR_ERR(bdev
);
1965 static void unlock_rdev(mdk_rdev_t
*rdev
)
1967 struct block_device
*bdev
= rdev
->bdev
;
1971 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
);
1974 void md_autodetect_dev(dev_t dev
);
1976 static void export_rdev(mdk_rdev_t
* rdev
)
1978 char b
[BDEVNAME_SIZE
];
1979 printk(KERN_INFO
"md: export_rdev(%s)\n",
1980 bdevname(rdev
->bdev
,b
));
1985 if (test_bit(AutoDetected
, &rdev
->flags
))
1986 md_autodetect_dev(rdev
->bdev
->bd_dev
);
1989 kobject_put(&rdev
->kobj
);
1992 static void kick_rdev_from_array(mdk_rdev_t
* rdev
)
1994 unbind_rdev_from_array(rdev
);
1998 static void export_array(mddev_t
*mddev
)
2000 mdk_rdev_t
*rdev
, *tmp
;
2002 rdev_for_each(rdev
, tmp
, mddev
) {
2007 kick_rdev_from_array(rdev
);
2009 if (!list_empty(&mddev
->disks
))
2011 mddev
->raid_disks
= 0;
2012 mddev
->major_version
= 0;
2015 static void print_desc(mdp_disk_t
*desc
)
2017 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc
->number
,
2018 desc
->major
,desc
->minor
,desc
->raid_disk
,desc
->state
);
2021 static void print_sb_90(mdp_super_t
*sb
)
2026 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2027 sb
->major_version
, sb
->minor_version
, sb
->patch_version
,
2028 sb
->set_uuid0
, sb
->set_uuid1
, sb
->set_uuid2
, sb
->set_uuid3
,
2030 printk(KERN_INFO
"md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2031 sb
->level
, sb
->size
, sb
->nr_disks
, sb
->raid_disks
,
2032 sb
->md_minor
, sb
->layout
, sb
->chunk_size
);
2033 printk(KERN_INFO
"md: UT:%08x ST:%d AD:%d WD:%d"
2034 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2035 sb
->utime
, sb
->state
, sb
->active_disks
, sb
->working_disks
,
2036 sb
->failed_disks
, sb
->spare_disks
,
2037 sb
->sb_csum
, (unsigned long)sb
->events_lo
);
2040 for (i
= 0; i
< MD_SB_DISKS
; i
++) {
2043 desc
= sb
->disks
+ i
;
2044 if (desc
->number
|| desc
->major
|| desc
->minor
||
2045 desc
->raid_disk
|| (desc
->state
&& (desc
->state
!= 4))) {
2046 printk(" D %2d: ", i
);
2050 printk(KERN_INFO
"md: THIS: ");
2051 print_desc(&sb
->this_disk
);
2054 static void print_sb_1(struct mdp_superblock_1
*sb
)
2058 uuid
= sb
->set_uuid
;
2060 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
2061 "md: Name: \"%s\" CT:%llu\n",
2062 le32_to_cpu(sb
->major_version
),
2063 le32_to_cpu(sb
->feature_map
),
2066 (unsigned long long)le64_to_cpu(sb
->ctime
)
2067 & MD_SUPERBLOCK_1_TIME_SEC_MASK
);
2069 uuid
= sb
->device_uuid
;
2071 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
2073 "md: Dev:%08x UUID: %pU\n"
2074 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2075 "md: (MaxDev:%u) \n",
2076 le32_to_cpu(sb
->level
),
2077 (unsigned long long)le64_to_cpu(sb
->size
),
2078 le32_to_cpu(sb
->raid_disks
),
2079 le32_to_cpu(sb
->layout
),
2080 le32_to_cpu(sb
->chunksize
),
2081 (unsigned long long)le64_to_cpu(sb
->data_offset
),
2082 (unsigned long long)le64_to_cpu(sb
->data_size
),
2083 (unsigned long long)le64_to_cpu(sb
->super_offset
),
2084 (unsigned long long)le64_to_cpu(sb
->recovery_offset
),
2085 le32_to_cpu(sb
->dev_number
),
2088 (unsigned long long)le64_to_cpu(sb
->utime
) & MD_SUPERBLOCK_1_TIME_SEC_MASK
,
2089 (unsigned long long)le64_to_cpu(sb
->events
),
2090 (unsigned long long)le64_to_cpu(sb
->resync_offset
),
2091 le32_to_cpu(sb
->sb_csum
),
2092 le32_to_cpu(sb
->max_dev
)
2096 static void print_rdev(mdk_rdev_t
*rdev
, int major_version
)
2098 char b
[BDEVNAME_SIZE
];
2099 printk(KERN_INFO
"md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2100 bdevname(rdev
->bdev
, b
), (unsigned long long)rdev
->sectors
,
2101 test_bit(Faulty
, &rdev
->flags
), test_bit(In_sync
, &rdev
->flags
),
2103 if (rdev
->sb_loaded
) {
2104 printk(KERN_INFO
"md: rdev superblock (MJ:%d):\n", major_version
);
2105 switch (major_version
) {
2107 print_sb_90((mdp_super_t
*)page_address(rdev
->sb_page
));
2110 print_sb_1((struct mdp_superblock_1
*)page_address(rdev
->sb_page
));
2114 printk(KERN_INFO
"md: no rdev superblock!\n");
2117 static void md_print_devices(void)
2119 struct list_head
*tmp
;
2122 char b
[BDEVNAME_SIZE
];
2125 printk("md: **********************************\n");
2126 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2127 printk("md: **********************************\n");
2128 for_each_mddev(mddev
, tmp
) {
2131 bitmap_print_sb(mddev
->bitmap
);
2133 printk("%s: ", mdname(mddev
));
2134 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
2135 printk("<%s>", bdevname(rdev
->bdev
,b
));
2138 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
2139 print_rdev(rdev
, mddev
->major_version
);
2141 printk("md: **********************************\n");
2146 static void sync_sbs(mddev_t
* mddev
, int nospares
)
2148 /* Update each superblock (in-memory image), but
2149 * if we are allowed to, skip spares which already
2150 * have the right event counter, or have one earlier
2151 * (which would mean they aren't being marked as dirty
2152 * with the rest of the array)
2155 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2156 if (rdev
->sb_events
== mddev
->events
||
2158 rdev
->raid_disk
< 0 &&
2159 rdev
->sb_events
+1 == mddev
->events
)) {
2160 /* Don't update this superblock */
2161 rdev
->sb_loaded
= 2;
2163 super_types
[mddev
->major_version
].
2164 sync_super(mddev
, rdev
);
2165 rdev
->sb_loaded
= 1;
2170 static void md_update_sb(mddev_t
* mddev
, int force_change
)
2177 /* First make sure individual recovery_offsets are correct */
2178 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2179 if (rdev
->raid_disk
>= 0 &&
2180 mddev
->delta_disks
>= 0 &&
2181 !test_bit(In_sync
, &rdev
->flags
) &&
2182 mddev
->curr_resync_completed
> rdev
->recovery_offset
)
2183 rdev
->recovery_offset
= mddev
->curr_resync_completed
;
2186 if (!mddev
->persistent
) {
2187 clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
2188 clear_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
2189 if (!mddev
->external
)
2190 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
2191 wake_up(&mddev
->sb_wait
);
2195 spin_lock_irq(&mddev
->write_lock
);
2197 mddev
->utime
= get_seconds();
2199 if (test_and_clear_bit(MD_CHANGE_DEVS
, &mddev
->flags
))
2201 if (test_and_clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
))
2202 /* just a clean<-> dirty transition, possibly leave spares alone,
2203 * though if events isn't the right even/odd, we will have to do
2209 if (mddev
->degraded
)
2210 /* If the array is degraded, then skipping spares is both
2211 * dangerous and fairly pointless.
2212 * Dangerous because a device that was removed from the array
2213 * might have a event_count that still looks up-to-date,
2214 * so it can be re-added without a resync.
2215 * Pointless because if there are any spares to skip,
2216 * then a recovery will happen and soon that array won't
2217 * be degraded any more and the spare can go back to sleep then.
2221 sync_req
= mddev
->in_sync
;
2223 /* If this is just a dirty<->clean transition, and the array is clean
2224 * and 'events' is odd, we can roll back to the previous clean state */
2226 && (mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
2227 && mddev
->can_decrease_events
2228 && mddev
->events
!= 1) {
2230 mddev
->can_decrease_events
= 0;
2232 /* otherwise we have to go forward and ... */
2234 mddev
->can_decrease_events
= nospares
;
2237 if (!mddev
->events
) {
2239 * oops, this 64-bit counter should never wrap.
2240 * Either we are in around ~1 trillion A.C., assuming
2241 * 1 reboot per second, or we have a bug:
2246 sync_sbs(mddev
, nospares
);
2247 spin_unlock_irq(&mddev
->write_lock
);
2250 "md: updating %s RAID superblock on device (in sync %d)\n",
2251 mdname(mddev
),mddev
->in_sync
);
2253 bitmap_update_sb(mddev
->bitmap
);
2254 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2255 char b
[BDEVNAME_SIZE
];
2256 dprintk(KERN_INFO
"md: ");
2257 if (rdev
->sb_loaded
!= 1)
2258 continue; /* no noise on spare devices */
2259 if (test_bit(Faulty
, &rdev
->flags
))
2260 dprintk("(skipping faulty ");
2262 dprintk("%s ", bdevname(rdev
->bdev
,b
));
2263 if (!test_bit(Faulty
, &rdev
->flags
)) {
2264 md_super_write(mddev
,rdev
,
2265 rdev
->sb_start
, rdev
->sb_size
,
2267 dprintk(KERN_INFO
"(write) %s's sb offset: %llu\n",
2268 bdevname(rdev
->bdev
,b
),
2269 (unsigned long long)rdev
->sb_start
);
2270 rdev
->sb_events
= mddev
->events
;
2274 if (mddev
->level
== LEVEL_MULTIPATH
)
2275 /* only need to write one superblock... */
2278 md_super_wait(mddev
);
2279 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
2281 spin_lock_irq(&mddev
->write_lock
);
2282 if (mddev
->in_sync
!= sync_req
||
2283 test_bit(MD_CHANGE_DEVS
, &mddev
->flags
)) {
2284 /* have to write it out again */
2285 spin_unlock_irq(&mddev
->write_lock
);
2288 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
2289 spin_unlock_irq(&mddev
->write_lock
);
2290 wake_up(&mddev
->sb_wait
);
2291 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
2292 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
2296 /* words written to sysfs files may, or may not, be \n terminated.
2297 * We want to accept with case. For this we use cmd_match.
2299 static int cmd_match(const char *cmd
, const char *str
)
2301 /* See if cmd, written into a sysfs file, matches
2302 * str. They must either be the same, or cmd can
2303 * have a trailing newline
2305 while (*cmd
&& *str
&& *cmd
== *str
) {
2316 struct rdev_sysfs_entry
{
2317 struct attribute attr
;
2318 ssize_t (*show
)(mdk_rdev_t
*, char *);
2319 ssize_t (*store
)(mdk_rdev_t
*, const char *, size_t);
2323 state_show(mdk_rdev_t
*rdev
, char *page
)
2328 if (test_bit(Faulty
, &rdev
->flags
)) {
2329 len
+= sprintf(page
+len
, "%sfaulty",sep
);
2332 if (test_bit(In_sync
, &rdev
->flags
)) {
2333 len
+= sprintf(page
+len
, "%sin_sync",sep
);
2336 if (test_bit(WriteMostly
, &rdev
->flags
)) {
2337 len
+= sprintf(page
+len
, "%swrite_mostly",sep
);
2340 if (test_bit(Blocked
, &rdev
->flags
)) {
2341 len
+= sprintf(page
+len
, "%sblocked", sep
);
2344 if (!test_bit(Faulty
, &rdev
->flags
) &&
2345 !test_bit(In_sync
, &rdev
->flags
)) {
2346 len
+= sprintf(page
+len
, "%sspare", sep
);
2349 return len
+sprintf(page
+len
, "\n");
2353 state_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2356 * faulty - simulates and error
2357 * remove - disconnects the device
2358 * writemostly - sets write_mostly
2359 * -writemostly - clears write_mostly
2360 * blocked - sets the Blocked flag
2361 * -blocked - clears the Blocked flag
2362 * insync - sets Insync providing device isn't active
2365 if (cmd_match(buf
, "faulty") && rdev
->mddev
->pers
) {
2366 md_error(rdev
->mddev
, rdev
);
2368 } else if (cmd_match(buf
, "remove")) {
2369 if (rdev
->raid_disk
>= 0)
2372 mddev_t
*mddev
= rdev
->mddev
;
2373 kick_rdev_from_array(rdev
);
2375 md_update_sb(mddev
, 1);
2376 md_new_event(mddev
);
2379 } else if (cmd_match(buf
, "writemostly")) {
2380 set_bit(WriteMostly
, &rdev
->flags
);
2382 } else if (cmd_match(buf
, "-writemostly")) {
2383 clear_bit(WriteMostly
, &rdev
->flags
);
2385 } else if (cmd_match(buf
, "blocked")) {
2386 set_bit(Blocked
, &rdev
->flags
);
2388 } else if (cmd_match(buf
, "-blocked")) {
2389 clear_bit(Blocked
, &rdev
->flags
);
2390 wake_up(&rdev
->blocked_wait
);
2391 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2392 md_wakeup_thread(rdev
->mddev
->thread
);
2395 } else if (cmd_match(buf
, "insync") && rdev
->raid_disk
== -1) {
2396 set_bit(In_sync
, &rdev
->flags
);
2400 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2401 return err
? err
: len
;
2403 static struct rdev_sysfs_entry rdev_state
=
2404 __ATTR(state
, S_IRUGO
|S_IWUSR
, state_show
, state_store
);
2407 errors_show(mdk_rdev_t
*rdev
, char *page
)
2409 return sprintf(page
, "%d\n", atomic_read(&rdev
->corrected_errors
));
2413 errors_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2416 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2417 if (*buf
&& (*e
== 0 || *e
== '\n')) {
2418 atomic_set(&rdev
->corrected_errors
, n
);
2423 static struct rdev_sysfs_entry rdev_errors
=
2424 __ATTR(errors
, S_IRUGO
|S_IWUSR
, errors_show
, errors_store
);
2427 slot_show(mdk_rdev_t
*rdev
, char *page
)
2429 if (rdev
->raid_disk
< 0)
2430 return sprintf(page
, "none\n");
2432 return sprintf(page
, "%d\n", rdev
->raid_disk
);
2436 slot_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2441 int slot
= simple_strtoul(buf
, &e
, 10);
2442 if (strncmp(buf
, "none", 4)==0)
2444 else if (e
==buf
|| (*e
&& *e
!= '\n'))
2446 if (rdev
->mddev
->pers
&& slot
== -1) {
2447 /* Setting 'slot' on an active array requires also
2448 * updating the 'rd%d' link, and communicating
2449 * with the personality with ->hot_*_disk.
2450 * For now we only support removing
2451 * failed/spare devices. This normally happens automatically,
2452 * but not when the metadata is externally managed.
2454 if (rdev
->raid_disk
== -1)
2456 /* personality does all needed checks */
2457 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
2459 err
= rdev
->mddev
->pers
->
2460 hot_remove_disk(rdev
->mddev
, rdev
->raid_disk
);
2463 sprintf(nm
, "rd%d", rdev
->raid_disk
);
2464 sysfs_remove_link(&rdev
->mddev
->kobj
, nm
);
2465 rdev
->raid_disk
= -1;
2466 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2467 md_wakeup_thread(rdev
->mddev
->thread
);
2468 } else if (rdev
->mddev
->pers
) {
2470 /* Activating a spare .. or possibly reactivating
2471 * if we ever get bitmaps working here.
2474 if (rdev
->raid_disk
!= -1)
2477 if (test_bit(MD_RECOVERY_RUNNING
, &rdev
->mddev
->recovery
))
2480 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
2483 list_for_each_entry(rdev2
, &rdev
->mddev
->disks
, same_set
)
2484 if (rdev2
->raid_disk
== slot
)
2487 if (slot
>= rdev
->mddev
->raid_disks
&&
2488 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
2491 rdev
->raid_disk
= slot
;
2492 if (test_bit(In_sync
, &rdev
->flags
))
2493 rdev
->saved_raid_disk
= slot
;
2495 rdev
->saved_raid_disk
= -1;
2496 err
= rdev
->mddev
->pers
->
2497 hot_add_disk(rdev
->mddev
, rdev
);
2499 rdev
->raid_disk
= -1;
2502 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2503 sprintf(nm
, "rd%d", rdev
->raid_disk
);
2504 if (sysfs_create_link(&rdev
->mddev
->kobj
, &rdev
->kobj
, nm
))
2505 /* failure here is OK */;
2506 /* don't wakeup anyone, leave that to userspace. */
2508 if (slot
>= rdev
->mddev
->raid_disks
&&
2509 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
2511 rdev
->raid_disk
= slot
;
2512 /* assume it is working */
2513 clear_bit(Faulty
, &rdev
->flags
);
2514 clear_bit(WriteMostly
, &rdev
->flags
);
2515 set_bit(In_sync
, &rdev
->flags
);
2516 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2522 static struct rdev_sysfs_entry rdev_slot
=
2523 __ATTR(slot
, S_IRUGO
|S_IWUSR
, slot_show
, slot_store
);
2526 offset_show(mdk_rdev_t
*rdev
, char *page
)
2528 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->data_offset
);
2532 offset_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2535 unsigned long long offset
= simple_strtoull(buf
, &e
, 10);
2536 if (e
==buf
|| (*e
&& *e
!= '\n'))
2538 if (rdev
->mddev
->pers
&& rdev
->raid_disk
>= 0)
2540 if (rdev
->sectors
&& rdev
->mddev
->external
)
2541 /* Must set offset before size, so overlap checks
2544 rdev
->data_offset
= offset
;
2548 static struct rdev_sysfs_entry rdev_offset
=
2549 __ATTR(offset
, S_IRUGO
|S_IWUSR
, offset_show
, offset_store
);
2552 rdev_size_show(mdk_rdev_t
*rdev
, char *page
)
2554 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->sectors
/ 2);
2557 static int overlaps(sector_t s1
, sector_t l1
, sector_t s2
, sector_t l2
)
2559 /* check if two start/length pairs overlap */
2567 static int strict_blocks_to_sectors(const char *buf
, sector_t
*sectors
)
2569 unsigned long long blocks
;
2572 if (strict_strtoull(buf
, 10, &blocks
) < 0)
2575 if (blocks
& 1ULL << (8 * sizeof(blocks
) - 1))
2576 return -EINVAL
; /* sector conversion overflow */
2579 if (new != blocks
* 2)
2580 return -EINVAL
; /* unsigned long long to sector_t overflow */
2587 rdev_size_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2589 mddev_t
*my_mddev
= rdev
->mddev
;
2590 sector_t oldsectors
= rdev
->sectors
;
2593 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
2595 if (my_mddev
->pers
&& rdev
->raid_disk
>= 0) {
2596 if (my_mddev
->persistent
) {
2597 sectors
= super_types
[my_mddev
->major_version
].
2598 rdev_size_change(rdev
, sectors
);
2601 } else if (!sectors
)
2602 sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) -
2605 if (sectors
< my_mddev
->dev_sectors
)
2606 return -EINVAL
; /* component must fit device */
2608 rdev
->sectors
= sectors
;
2609 if (sectors
> oldsectors
&& my_mddev
->external
) {
2610 /* need to check that all other rdevs with the same ->bdev
2611 * do not overlap. We need to unlock the mddev to avoid
2612 * a deadlock. We have already changed rdev->sectors, and if
2613 * we have to change it back, we will have the lock again.
2617 struct list_head
*tmp
;
2619 mddev_unlock(my_mddev
);
2620 for_each_mddev(mddev
, tmp
) {
2624 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
)
2625 if (rdev
->bdev
== rdev2
->bdev
&&
2627 overlaps(rdev
->data_offset
, rdev
->sectors
,
2633 mddev_unlock(mddev
);
2639 mddev_lock(my_mddev
);
2641 /* Someone else could have slipped in a size
2642 * change here, but doing so is just silly.
2643 * We put oldsectors back because we *know* it is
2644 * safe, and trust userspace not to race with
2647 rdev
->sectors
= oldsectors
;
2654 static struct rdev_sysfs_entry rdev_size
=
2655 __ATTR(size
, S_IRUGO
|S_IWUSR
, rdev_size_show
, rdev_size_store
);
2658 static ssize_t
recovery_start_show(mdk_rdev_t
*rdev
, char *page
)
2660 unsigned long long recovery_start
= rdev
->recovery_offset
;
2662 if (test_bit(In_sync
, &rdev
->flags
) ||
2663 recovery_start
== MaxSector
)
2664 return sprintf(page
, "none\n");
2666 return sprintf(page
, "%llu\n", recovery_start
);
2669 static ssize_t
recovery_start_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2671 unsigned long long recovery_start
;
2673 if (cmd_match(buf
, "none"))
2674 recovery_start
= MaxSector
;
2675 else if (strict_strtoull(buf
, 10, &recovery_start
))
2678 if (rdev
->mddev
->pers
&&
2679 rdev
->raid_disk
>= 0)
2682 rdev
->recovery_offset
= recovery_start
;
2683 if (recovery_start
== MaxSector
)
2684 set_bit(In_sync
, &rdev
->flags
);
2686 clear_bit(In_sync
, &rdev
->flags
);
2690 static struct rdev_sysfs_entry rdev_recovery_start
=
2691 __ATTR(recovery_start
, S_IRUGO
|S_IWUSR
, recovery_start_show
, recovery_start_store
);
2693 static struct attribute
*rdev_default_attrs
[] = {
2699 &rdev_recovery_start
.attr
,
2703 rdev_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
2705 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
2706 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
2707 mddev_t
*mddev
= rdev
->mddev
;
2713 rv
= mddev
? mddev_lock(mddev
) : -EBUSY
;
2715 if (rdev
->mddev
== NULL
)
2718 rv
= entry
->show(rdev
, page
);
2719 mddev_unlock(mddev
);
2725 rdev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
2726 const char *page
, size_t length
)
2728 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
2729 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
2731 mddev_t
*mddev
= rdev
->mddev
;
2735 if (!capable(CAP_SYS_ADMIN
))
2737 rv
= mddev
? mddev_lock(mddev
): -EBUSY
;
2739 if (rdev
->mddev
== NULL
)
2742 rv
= entry
->store(rdev
, page
, length
);
2743 mddev_unlock(mddev
);
2748 static void rdev_free(struct kobject
*ko
)
2750 mdk_rdev_t
*rdev
= container_of(ko
, mdk_rdev_t
, kobj
);
2753 static const struct sysfs_ops rdev_sysfs_ops
= {
2754 .show
= rdev_attr_show
,
2755 .store
= rdev_attr_store
,
2757 static struct kobj_type rdev_ktype
= {
2758 .release
= rdev_free
,
2759 .sysfs_ops
= &rdev_sysfs_ops
,
2760 .default_attrs
= rdev_default_attrs
,
2763 void md_rdev_init(mdk_rdev_t
*rdev
)
2766 rdev
->saved_raid_disk
= -1;
2767 rdev
->raid_disk
= -1;
2769 rdev
->data_offset
= 0;
2770 rdev
->sb_events
= 0;
2771 rdev
->last_read_error
.tv_sec
= 0;
2772 rdev
->last_read_error
.tv_nsec
= 0;
2773 atomic_set(&rdev
->nr_pending
, 0);
2774 atomic_set(&rdev
->read_errors
, 0);
2775 atomic_set(&rdev
->corrected_errors
, 0);
2777 INIT_LIST_HEAD(&rdev
->same_set
);
2778 init_waitqueue_head(&rdev
->blocked_wait
);
2780 EXPORT_SYMBOL_GPL(md_rdev_init
);
2782 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2784 * mark the device faulty if:
2786 * - the device is nonexistent (zero size)
2787 * - the device has no valid superblock
2789 * a faulty rdev _never_ has rdev->sb set.
2791 static mdk_rdev_t
*md_import_device(dev_t newdev
, int super_format
, int super_minor
)
2793 char b
[BDEVNAME_SIZE
];
2798 rdev
= kzalloc(sizeof(*rdev
), GFP_KERNEL
);
2800 printk(KERN_ERR
"md: could not alloc mem for new device!\n");
2801 return ERR_PTR(-ENOMEM
);
2805 if ((err
= alloc_disk_sb(rdev
)))
2808 err
= lock_rdev(rdev
, newdev
, super_format
== -2);
2812 kobject_init(&rdev
->kobj
, &rdev_ktype
);
2814 size
= i_size_read(rdev
->bdev
->bd_inode
) >> BLOCK_SIZE_BITS
;
2817 "md: %s has zero or unknown size, marking faulty!\n",
2818 bdevname(rdev
->bdev
,b
));
2823 if (super_format
>= 0) {
2824 err
= super_types
[super_format
].
2825 load_super(rdev
, NULL
, super_minor
);
2826 if (err
== -EINVAL
) {
2828 "md: %s does not have a valid v%d.%d "
2829 "superblock, not importing!\n",
2830 bdevname(rdev
->bdev
,b
),
2831 super_format
, super_minor
);
2836 "md: could not read %s's sb, not importing!\n",
2837 bdevname(rdev
->bdev
,b
));
2845 if (rdev
->sb_page
) {
2851 return ERR_PTR(err
);
2855 * Check a full RAID array for plausibility
2859 static void analyze_sbs(mddev_t
* mddev
)
2862 mdk_rdev_t
*rdev
, *freshest
, *tmp
;
2863 char b
[BDEVNAME_SIZE
];
2866 rdev_for_each(rdev
, tmp
, mddev
)
2867 switch (super_types
[mddev
->major_version
].
2868 load_super(rdev
, freshest
, mddev
->minor_version
)) {
2876 "md: fatal superblock inconsistency in %s"
2877 " -- removing from array\n",
2878 bdevname(rdev
->bdev
,b
));
2879 kick_rdev_from_array(rdev
);
2883 super_types
[mddev
->major_version
].
2884 validate_super(mddev
, freshest
);
2887 rdev_for_each(rdev
, tmp
, mddev
) {
2888 if (mddev
->max_disks
&&
2889 (rdev
->desc_nr
>= mddev
->max_disks
||
2890 i
> mddev
->max_disks
)) {
2892 "md: %s: %s: only %d devices permitted\n",
2893 mdname(mddev
), bdevname(rdev
->bdev
, b
),
2895 kick_rdev_from_array(rdev
);
2898 if (rdev
!= freshest
)
2899 if (super_types
[mddev
->major_version
].
2900 validate_super(mddev
, rdev
)) {
2901 printk(KERN_WARNING
"md: kicking non-fresh %s"
2903 bdevname(rdev
->bdev
,b
));
2904 kick_rdev_from_array(rdev
);
2907 if (mddev
->level
== LEVEL_MULTIPATH
) {
2908 rdev
->desc_nr
= i
++;
2909 rdev
->raid_disk
= rdev
->desc_nr
;
2910 set_bit(In_sync
, &rdev
->flags
);
2911 } else if (rdev
->raid_disk
>= (mddev
->raid_disks
- min(0, mddev
->delta_disks
))) {
2912 rdev
->raid_disk
= -1;
2913 clear_bit(In_sync
, &rdev
->flags
);
2918 /* Read a fixed-point number.
2919 * Numbers in sysfs attributes should be in "standard" units where
2920 * possible, so time should be in seconds.
2921 * However we internally use a a much smaller unit such as
2922 * milliseconds or jiffies.
2923 * This function takes a decimal number with a possible fractional
2924 * component, and produces an integer which is the result of
2925 * multiplying that number by 10^'scale'.
2926 * all without any floating-point arithmetic.
2928 int strict_strtoul_scaled(const char *cp
, unsigned long *res
, int scale
)
2930 unsigned long result
= 0;
2932 while (isdigit(*cp
) || (*cp
== '.' && decimals
< 0)) {
2935 else if (decimals
< scale
) {
2938 result
= result
* 10 + value
;
2950 while (decimals
< scale
) {
2959 static void md_safemode_timeout(unsigned long data
);
2962 safe_delay_show(mddev_t
*mddev
, char *page
)
2964 int msec
= (mddev
->safemode_delay
*1000)/HZ
;
2965 return sprintf(page
, "%d.%03d\n", msec
/1000, msec
%1000);
2968 safe_delay_store(mddev_t
*mddev
, const char *cbuf
, size_t len
)
2972 if (strict_strtoul_scaled(cbuf
, &msec
, 3) < 0)
2975 mddev
->safemode_delay
= 0;
2977 unsigned long old_delay
= mddev
->safemode_delay
;
2978 mddev
->safemode_delay
= (msec
*HZ
)/1000;
2979 if (mddev
->safemode_delay
== 0)
2980 mddev
->safemode_delay
= 1;
2981 if (mddev
->safemode_delay
< old_delay
)
2982 md_safemode_timeout((unsigned long)mddev
);
2986 static struct md_sysfs_entry md_safe_delay
=
2987 __ATTR(safe_mode_delay
, S_IRUGO
|S_IWUSR
,safe_delay_show
, safe_delay_store
);
2990 level_show(mddev_t
*mddev
, char *page
)
2992 struct mdk_personality
*p
= mddev
->pers
;
2994 return sprintf(page
, "%s\n", p
->name
);
2995 else if (mddev
->clevel
[0])
2996 return sprintf(page
, "%s\n", mddev
->clevel
);
2997 else if (mddev
->level
!= LEVEL_NONE
)
2998 return sprintf(page
, "%d\n", mddev
->level
);
3004 level_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3008 struct mdk_personality
*pers
;
3013 if (mddev
->pers
== NULL
) {
3016 if (len
>= sizeof(mddev
->clevel
))
3018 strncpy(mddev
->clevel
, buf
, len
);
3019 if (mddev
->clevel
[len
-1] == '\n')
3021 mddev
->clevel
[len
] = 0;
3022 mddev
->level
= LEVEL_NONE
;
3026 /* request to change the personality. Need to ensure:
3027 * - array is not engaged in resync/recovery/reshape
3028 * - old personality can be suspended
3029 * - new personality will access other array.
3032 if (mddev
->sync_thread
||
3033 mddev
->reshape_position
!= MaxSector
||
3034 mddev
->sysfs_active
)
3037 if (!mddev
->pers
->quiesce
) {
3038 printk(KERN_WARNING
"md: %s: %s does not support online personality change\n",
3039 mdname(mddev
), mddev
->pers
->name
);
3043 /* Now find the new personality */
3044 if (len
== 0 || len
>= sizeof(clevel
))
3046 strncpy(clevel
, buf
, len
);
3047 if (clevel
[len
-1] == '\n')
3050 if (strict_strtol(clevel
, 10, &level
))
3053 if (request_module("md-%s", clevel
) != 0)
3054 request_module("md-level-%s", clevel
);
3055 spin_lock(&pers_lock
);
3056 pers
= find_pers(level
, clevel
);
3057 if (!pers
|| !try_module_get(pers
->owner
)) {
3058 spin_unlock(&pers_lock
);
3059 printk(KERN_WARNING
"md: personality %s not loaded\n", clevel
);
3062 spin_unlock(&pers_lock
);
3064 if (pers
== mddev
->pers
) {
3065 /* Nothing to do! */
3066 module_put(pers
->owner
);
3069 if (!pers
->takeover
) {
3070 module_put(pers
->owner
);
3071 printk(KERN_WARNING
"md: %s: %s does not support personality takeover\n",
3072 mdname(mddev
), clevel
);
3076 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
3077 rdev
->new_raid_disk
= rdev
->raid_disk
;
3079 /* ->takeover must set new_* and/or delta_disks
3080 * if it succeeds, and may set them when it fails.
3082 priv
= pers
->takeover(mddev
);
3084 mddev
->new_level
= mddev
->level
;
3085 mddev
->new_layout
= mddev
->layout
;
3086 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3087 mddev
->raid_disks
-= mddev
->delta_disks
;
3088 mddev
->delta_disks
= 0;
3089 module_put(pers
->owner
);
3090 printk(KERN_WARNING
"md: %s: %s would not accept array\n",
3091 mdname(mddev
), clevel
);
3092 return PTR_ERR(priv
);
3095 /* Looks like we have a winner */
3096 mddev_suspend(mddev
);
3097 mddev
->pers
->stop(mddev
);
3099 if (mddev
->pers
->sync_request
== NULL
&&
3100 pers
->sync_request
!= NULL
) {
3101 /* need to add the md_redundancy_group */
3102 if (sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
3104 "md: cannot register extra attributes for %s\n",
3106 mddev
->sysfs_action
= sysfs_get_dirent(mddev
->kobj
.sd
, NULL
, "sync_action");
3108 if (mddev
->pers
->sync_request
!= NULL
&&
3109 pers
->sync_request
== NULL
) {
3110 /* need to remove the md_redundancy_group */
3111 if (mddev
->to_remove
== NULL
)
3112 mddev
->to_remove
= &md_redundancy_group
;
3115 if (mddev
->pers
->sync_request
== NULL
&&
3117 /* We are converting from a no-redundancy array
3118 * to a redundancy array and metadata is managed
3119 * externally so we need to be sure that writes
3120 * won't block due to a need to transition
3122 * until external management is started.
3125 mddev
->safemode_delay
= 0;
3126 mddev
->safemode
= 0;
3129 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
3131 if (rdev
->raid_disk
< 0)
3133 if (rdev
->new_raid_disk
>= mddev
->raid_disks
)
3134 rdev
->new_raid_disk
= -1;
3135 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3137 sprintf(nm
, "rd%d", rdev
->raid_disk
);
3138 sysfs_remove_link(&mddev
->kobj
, nm
);
3140 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
3141 if (rdev
->raid_disk
< 0)
3143 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3145 rdev
->raid_disk
= rdev
->new_raid_disk
;
3146 if (rdev
->raid_disk
< 0)
3147 clear_bit(In_sync
, &rdev
->flags
);
3150 sprintf(nm
, "rd%d", rdev
->raid_disk
);
3151 if(sysfs_create_link(&mddev
->kobj
, &rdev
->kobj
, nm
))
3152 printk("md: cannot register %s for %s after level change\n",
3157 module_put(mddev
->pers
->owner
);
3159 mddev
->private = priv
;
3160 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
3161 mddev
->level
= mddev
->new_level
;
3162 mddev
->layout
= mddev
->new_layout
;
3163 mddev
->chunk_sectors
= mddev
->new_chunk_sectors
;
3164 mddev
->delta_disks
= 0;
3165 if (mddev
->pers
->sync_request
== NULL
) {
3166 /* this is now an array without redundancy, so
3167 * it must always be in_sync
3170 del_timer_sync(&mddev
->safemode_timer
);
3173 mddev_resume(mddev
);
3174 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
3175 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3176 md_wakeup_thread(mddev
->thread
);
3177 sysfs_notify(&mddev
->kobj
, NULL
, "level");
3178 md_new_event(mddev
);
3182 static struct md_sysfs_entry md_level
=
3183 __ATTR(level
, S_IRUGO
|S_IWUSR
, level_show
, level_store
);
3187 layout_show(mddev_t
*mddev
, char *page
)
3189 /* just a number, not meaningful for all levels */
3190 if (mddev
->reshape_position
!= MaxSector
&&
3191 mddev
->layout
!= mddev
->new_layout
)
3192 return sprintf(page
, "%d (%d)\n",
3193 mddev
->new_layout
, mddev
->layout
);
3194 return sprintf(page
, "%d\n", mddev
->layout
);
3198 layout_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3201 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3203 if (!*buf
|| (*e
&& *e
!= '\n'))
3208 if (mddev
->pers
->check_reshape
== NULL
)
3210 mddev
->new_layout
= n
;
3211 err
= mddev
->pers
->check_reshape(mddev
);
3213 mddev
->new_layout
= mddev
->layout
;
3217 mddev
->new_layout
= n
;
3218 if (mddev
->reshape_position
== MaxSector
)
3223 static struct md_sysfs_entry md_layout
=
3224 __ATTR(layout
, S_IRUGO
|S_IWUSR
, layout_show
, layout_store
);
3228 raid_disks_show(mddev_t
*mddev
, char *page
)
3230 if (mddev
->raid_disks
== 0)
3232 if (mddev
->reshape_position
!= MaxSector
&&
3233 mddev
->delta_disks
!= 0)
3234 return sprintf(page
, "%d (%d)\n", mddev
->raid_disks
,
3235 mddev
->raid_disks
- mddev
->delta_disks
);
3236 return sprintf(page
, "%d\n", mddev
->raid_disks
);
3239 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
);
3242 raid_disks_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3246 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3248 if (!*buf
|| (*e
&& *e
!= '\n'))
3252 rv
= update_raid_disks(mddev
, n
);
3253 else if (mddev
->reshape_position
!= MaxSector
) {
3254 int olddisks
= mddev
->raid_disks
- mddev
->delta_disks
;
3255 mddev
->delta_disks
= n
- olddisks
;
3256 mddev
->raid_disks
= n
;
3258 mddev
->raid_disks
= n
;
3259 return rv
? rv
: len
;
3261 static struct md_sysfs_entry md_raid_disks
=
3262 __ATTR(raid_disks
, S_IRUGO
|S_IWUSR
, raid_disks_show
, raid_disks_store
);
3265 chunk_size_show(mddev_t
*mddev
, char *page
)
3267 if (mddev
->reshape_position
!= MaxSector
&&
3268 mddev
->chunk_sectors
!= mddev
->new_chunk_sectors
)
3269 return sprintf(page
, "%d (%d)\n",
3270 mddev
->new_chunk_sectors
<< 9,
3271 mddev
->chunk_sectors
<< 9);
3272 return sprintf(page
, "%d\n", mddev
->chunk_sectors
<< 9);
3276 chunk_size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3279 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3281 if (!*buf
|| (*e
&& *e
!= '\n'))
3286 if (mddev
->pers
->check_reshape
== NULL
)
3288 mddev
->new_chunk_sectors
= n
>> 9;
3289 err
= mddev
->pers
->check_reshape(mddev
);
3291 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3295 mddev
->new_chunk_sectors
= n
>> 9;
3296 if (mddev
->reshape_position
== MaxSector
)
3297 mddev
->chunk_sectors
= n
>> 9;
3301 static struct md_sysfs_entry md_chunk_size
=
3302 __ATTR(chunk_size
, S_IRUGO
|S_IWUSR
, chunk_size_show
, chunk_size_store
);
3305 resync_start_show(mddev_t
*mddev
, char *page
)
3307 if (mddev
->recovery_cp
== MaxSector
)
3308 return sprintf(page
, "none\n");
3309 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->recovery_cp
);
3313 resync_start_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3316 unsigned long long n
= simple_strtoull(buf
, &e
, 10);
3320 if (cmd_match(buf
, "none"))
3322 else if (!*buf
|| (*e
&& *e
!= '\n'))
3325 mddev
->recovery_cp
= n
;
3328 static struct md_sysfs_entry md_resync_start
=
3329 __ATTR(resync_start
, S_IRUGO
|S_IWUSR
, resync_start_show
, resync_start_store
);
3332 * The array state can be:
3335 * No devices, no size, no level
3336 * Equivalent to STOP_ARRAY ioctl
3338 * May have some settings, but array is not active
3339 * all IO results in error
3340 * When written, doesn't tear down array, but just stops it
3341 * suspended (not supported yet)
3342 * All IO requests will block. The array can be reconfigured.
3343 * Writing this, if accepted, will block until array is quiescent
3345 * no resync can happen. no superblocks get written.
3346 * write requests fail
3348 * like readonly, but behaves like 'clean' on a write request.
3350 * clean - no pending writes, but otherwise active.
3351 * When written to inactive array, starts without resync
3352 * If a write request arrives then
3353 * if metadata is known, mark 'dirty' and switch to 'active'.
3354 * if not known, block and switch to write-pending
3355 * If written to an active array that has pending writes, then fails.
3357 * fully active: IO and resync can be happening.
3358 * When written to inactive array, starts with resync
3361 * clean, but writes are blocked waiting for 'active' to be written.
3364 * like active, but no writes have been seen for a while (100msec).
3367 enum array_state
{ clear
, inactive
, suspended
, readonly
, read_auto
, clean
, active
,
3368 write_pending
, active_idle
, bad_word
};
3369 static char *array_states
[] = {
3370 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3371 "write-pending", "active-idle", NULL
};
3373 static int match_word(const char *word
, char **list
)
3376 for (n
=0; list
[n
]; n
++)
3377 if (cmd_match(word
, list
[n
]))
3383 array_state_show(mddev_t
*mddev
, char *page
)
3385 enum array_state st
= inactive
;
3398 else if (test_bit(MD_CHANGE_PENDING
, &mddev
->flags
))
3400 else if (mddev
->safemode
)
3406 if (list_empty(&mddev
->disks
) &&
3407 mddev
->raid_disks
== 0 &&
3408 mddev
->dev_sectors
== 0)
3413 return sprintf(page
, "%s\n", array_states
[st
]);
3416 static int do_md_stop(mddev_t
* mddev
, int ro
, int is_open
);
3417 static int md_set_readonly(mddev_t
* mddev
, int is_open
);
3418 static int do_md_run(mddev_t
* mddev
);
3419 static int restart_array(mddev_t
*mddev
);
3422 array_state_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3425 enum array_state st
= match_word(buf
, array_states
);
3430 /* stopping an active array */
3431 if (atomic_read(&mddev
->openers
) > 0)
3433 err
= do_md_stop(mddev
, 0, 0);
3436 /* stopping an active array */
3438 if (atomic_read(&mddev
->openers
) > 0)
3440 err
= do_md_stop(mddev
, 2, 0);
3442 err
= 0; /* already inactive */
3445 break; /* not supported yet */
3448 err
= md_set_readonly(mddev
, 0);
3451 set_disk_ro(mddev
->gendisk
, 1);
3452 err
= do_md_run(mddev
);
3458 err
= md_set_readonly(mddev
, 0);
3459 else if (mddev
->ro
== 1)
3460 err
= restart_array(mddev
);
3463 set_disk_ro(mddev
->gendisk
, 0);
3467 err
= do_md_run(mddev
);
3472 restart_array(mddev
);
3473 spin_lock_irq(&mddev
->write_lock
);
3474 if (atomic_read(&mddev
->writes_pending
) == 0) {
3475 if (mddev
->in_sync
== 0) {
3477 if (mddev
->safemode
== 1)
3478 mddev
->safemode
= 0;
3479 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
3484 spin_unlock_irq(&mddev
->write_lock
);
3490 restart_array(mddev
);
3491 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
3492 wake_up(&mddev
->sb_wait
);
3496 set_disk_ro(mddev
->gendisk
, 0);
3497 err
= do_md_run(mddev
);
3502 /* these cannot be set */
3508 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
3512 static struct md_sysfs_entry md_array_state
=
3513 __ATTR(array_state
, S_IRUGO
|S_IWUSR
, array_state_show
, array_state_store
);
3516 max_corrected_read_errors_show(mddev_t
*mddev
, char *page
) {
3517 return sprintf(page
, "%d\n",
3518 atomic_read(&mddev
->max_corr_read_errors
));
3522 max_corrected_read_errors_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3525 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3527 if (*buf
&& (*e
== 0 || *e
== '\n')) {
3528 atomic_set(&mddev
->max_corr_read_errors
, n
);
3534 static struct md_sysfs_entry max_corr_read_errors
=
3535 __ATTR(max_read_errors
, S_IRUGO
|S_IWUSR
, max_corrected_read_errors_show
,
3536 max_corrected_read_errors_store
);
3539 null_show(mddev_t
*mddev
, char *page
)
3545 new_dev_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3547 /* buf must be %d:%d\n? giving major and minor numbers */
3548 /* The new device is added to the array.
3549 * If the array has a persistent superblock, we read the
3550 * superblock to initialise info and check validity.
3551 * Otherwise, only checking done is that in bind_rdev_to_array,
3552 * which mainly checks size.
3555 int major
= simple_strtoul(buf
, &e
, 10);
3561 if (!*buf
|| *e
!= ':' || !e
[1] || e
[1] == '\n')
3563 minor
= simple_strtoul(e
+1, &e
, 10);
3564 if (*e
&& *e
!= '\n')
3566 dev
= MKDEV(major
, minor
);
3567 if (major
!= MAJOR(dev
) ||
3568 minor
!= MINOR(dev
))
3572 if (mddev
->persistent
) {
3573 rdev
= md_import_device(dev
, mddev
->major_version
,
3574 mddev
->minor_version
);
3575 if (!IS_ERR(rdev
) && !list_empty(&mddev
->disks
)) {
3576 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
3577 mdk_rdev_t
, same_set
);
3578 err
= super_types
[mddev
->major_version
]
3579 .load_super(rdev
, rdev0
, mddev
->minor_version
);
3583 } else if (mddev
->external
)
3584 rdev
= md_import_device(dev
, -2, -1);
3586 rdev
= md_import_device(dev
, -1, -1);
3589 return PTR_ERR(rdev
);
3590 err
= bind_rdev_to_array(rdev
, mddev
);
3594 return err
? err
: len
;
3597 static struct md_sysfs_entry md_new_device
=
3598 __ATTR(new_dev
, S_IWUSR
, null_show
, new_dev_store
);
3601 bitmap_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3604 unsigned long chunk
, end_chunk
;
3608 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3610 chunk
= end_chunk
= simple_strtoul(buf
, &end
, 0);
3611 if (buf
== end
) break;
3612 if (*end
== '-') { /* range */
3614 end_chunk
= simple_strtoul(buf
, &end
, 0);
3615 if (buf
== end
) break;
3617 if (*end
&& !isspace(*end
)) break;
3618 bitmap_dirty_bits(mddev
->bitmap
, chunk
, end_chunk
);
3619 buf
= skip_spaces(end
);
3621 bitmap_unplug(mddev
->bitmap
); /* flush the bits to disk */
3626 static struct md_sysfs_entry md_bitmap
=
3627 __ATTR(bitmap_set_bits
, S_IWUSR
, null_show
, bitmap_store
);
3630 size_show(mddev_t
*mddev
, char *page
)
3632 return sprintf(page
, "%llu\n",
3633 (unsigned long long)mddev
->dev_sectors
/ 2);
3636 static int update_size(mddev_t
*mddev
, sector_t num_sectors
);
3639 size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3641 /* If array is inactive, we can reduce the component size, but
3642 * not increase it (except from 0).
3643 * If array is active, we can try an on-line resize
3646 int err
= strict_blocks_to_sectors(buf
, §ors
);
3651 err
= update_size(mddev
, sectors
);
3652 md_update_sb(mddev
, 1);
3654 if (mddev
->dev_sectors
== 0 ||
3655 mddev
->dev_sectors
> sectors
)
3656 mddev
->dev_sectors
= sectors
;
3660 return err
? err
: len
;
3663 static struct md_sysfs_entry md_size
=
3664 __ATTR(component_size
, S_IRUGO
|S_IWUSR
, size_show
, size_store
);
3669 * 'none' for arrays with no metadata (good luck...)
3670 * 'external' for arrays with externally managed metadata,
3671 * or N.M for internally known formats
3674 metadata_show(mddev_t
*mddev
, char *page
)
3676 if (mddev
->persistent
)
3677 return sprintf(page
, "%d.%d\n",
3678 mddev
->major_version
, mddev
->minor_version
);
3679 else if (mddev
->external
)
3680 return sprintf(page
, "external:%s\n", mddev
->metadata_type
);
3682 return sprintf(page
, "none\n");
3686 metadata_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3690 /* Changing the details of 'external' metadata is
3691 * always permitted. Otherwise there must be
3692 * no devices attached to the array.
3694 if (mddev
->external
&& strncmp(buf
, "external:", 9) == 0)
3696 else if (!list_empty(&mddev
->disks
))
3699 if (cmd_match(buf
, "none")) {
3700 mddev
->persistent
= 0;
3701 mddev
->external
= 0;
3702 mddev
->major_version
= 0;
3703 mddev
->minor_version
= 90;
3706 if (strncmp(buf
, "external:", 9) == 0) {
3707 size_t namelen
= len
-9;
3708 if (namelen
>= sizeof(mddev
->metadata_type
))
3709 namelen
= sizeof(mddev
->metadata_type
)-1;
3710 strncpy(mddev
->metadata_type
, buf
+9, namelen
);
3711 mddev
->metadata_type
[namelen
] = 0;
3712 if (namelen
&& mddev
->metadata_type
[namelen
-1] == '\n')
3713 mddev
->metadata_type
[--namelen
] = 0;
3714 mddev
->persistent
= 0;
3715 mddev
->external
= 1;
3716 mddev
->major_version
= 0;
3717 mddev
->minor_version
= 90;
3720 major
= simple_strtoul(buf
, &e
, 10);
3721 if (e
==buf
|| *e
!= '.')
3724 minor
= simple_strtoul(buf
, &e
, 10);
3725 if (e
==buf
|| (*e
&& *e
!= '\n') )
3727 if (major
>= ARRAY_SIZE(super_types
) || super_types
[major
].name
== NULL
)
3729 mddev
->major_version
= major
;
3730 mddev
->minor_version
= minor
;
3731 mddev
->persistent
= 1;
3732 mddev
->external
= 0;
3736 static struct md_sysfs_entry md_metadata
=
3737 __ATTR(metadata_version
, S_IRUGO
|S_IWUSR
, metadata_show
, metadata_store
);
3740 action_show(mddev_t
*mddev
, char *page
)
3742 char *type
= "idle";
3743 if (test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
3745 else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
3746 (!mddev
->ro
&& test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))) {
3747 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
3749 else if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
3750 if (!test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
3752 else if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
3756 } else if (test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
))
3759 return sprintf(page
, "%s\n", type
);
3762 static void reap_sync_thread(mddev_t
*mddev
);
3765 action_store(mddev_t
*mddev
, const char *page
, size_t len
)
3767 if (!mddev
->pers
|| !mddev
->pers
->sync_request
)
3770 if (cmd_match(page
, "frozen"))
3771 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
3773 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
3775 if (cmd_match(page
, "idle") || cmd_match(page
, "frozen")) {
3776 if (mddev
->sync_thread
) {
3777 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
3778 reap_sync_thread(mddev
);
3780 } else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
3781 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
3783 else if (cmd_match(page
, "resync"))
3784 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3785 else if (cmd_match(page
, "recover")) {
3786 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
3787 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3788 } else if (cmd_match(page
, "reshape")) {
3790 if (mddev
->pers
->start_reshape
== NULL
)
3792 err
= mddev
->pers
->start_reshape(mddev
);
3795 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
3797 if (cmd_match(page
, "check"))
3798 set_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
3799 else if (!cmd_match(page
, "repair"))
3801 set_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
3802 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
3804 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3805 md_wakeup_thread(mddev
->thread
);
3806 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
3811 mismatch_cnt_show(mddev_t
*mddev
, char *page
)
3813 return sprintf(page
, "%llu\n",
3814 (unsigned long long) mddev
->resync_mismatches
);
3817 static struct md_sysfs_entry md_scan_mode
=
3818 __ATTR(sync_action
, S_IRUGO
|S_IWUSR
, action_show
, action_store
);
3821 static struct md_sysfs_entry md_mismatches
= __ATTR_RO(mismatch_cnt
);
3824 sync_min_show(mddev_t
*mddev
, char *page
)
3826 return sprintf(page
, "%d (%s)\n", speed_min(mddev
),
3827 mddev
->sync_speed_min
? "local": "system");
3831 sync_min_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3835 if (strncmp(buf
, "system", 6)==0) {
3836 mddev
->sync_speed_min
= 0;
3839 min
= simple_strtoul(buf
, &e
, 10);
3840 if (buf
== e
|| (*e
&& *e
!= '\n') || min
<= 0)
3842 mddev
->sync_speed_min
= min
;
3846 static struct md_sysfs_entry md_sync_min
=
3847 __ATTR(sync_speed_min
, S_IRUGO
|S_IWUSR
, sync_min_show
, sync_min_store
);
3850 sync_max_show(mddev_t
*mddev
, char *page
)
3852 return sprintf(page
, "%d (%s)\n", speed_max(mddev
),
3853 mddev
->sync_speed_max
? "local": "system");
3857 sync_max_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3861 if (strncmp(buf
, "system", 6)==0) {
3862 mddev
->sync_speed_max
= 0;
3865 max
= simple_strtoul(buf
, &e
, 10);
3866 if (buf
== e
|| (*e
&& *e
!= '\n') || max
<= 0)
3868 mddev
->sync_speed_max
= max
;
3872 static struct md_sysfs_entry md_sync_max
=
3873 __ATTR(sync_speed_max
, S_IRUGO
|S_IWUSR
, sync_max_show
, sync_max_store
);
3876 degraded_show(mddev_t
*mddev
, char *page
)
3878 return sprintf(page
, "%d\n", mddev
->degraded
);
3880 static struct md_sysfs_entry md_degraded
= __ATTR_RO(degraded
);
3883 sync_force_parallel_show(mddev_t
*mddev
, char *page
)
3885 return sprintf(page
, "%d\n", mddev
->parallel_resync
);
3889 sync_force_parallel_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3893 if (strict_strtol(buf
, 10, &n
))
3896 if (n
!= 0 && n
!= 1)
3899 mddev
->parallel_resync
= n
;
3901 if (mddev
->sync_thread
)
3902 wake_up(&resync_wait
);
3907 /* force parallel resync, even with shared block devices */
3908 static struct md_sysfs_entry md_sync_force_parallel
=
3909 __ATTR(sync_force_parallel
, S_IRUGO
|S_IWUSR
,
3910 sync_force_parallel_show
, sync_force_parallel_store
);
3913 sync_speed_show(mddev_t
*mddev
, char *page
)
3915 unsigned long resync
, dt
, db
;
3916 if (mddev
->curr_resync
== 0)
3917 return sprintf(page
, "none\n");
3918 resync
= mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
);
3919 dt
= (jiffies
- mddev
->resync_mark
) / HZ
;
3921 db
= resync
- mddev
->resync_mark_cnt
;
3922 return sprintf(page
, "%lu\n", db
/dt
/2); /* K/sec */
3925 static struct md_sysfs_entry md_sync_speed
= __ATTR_RO(sync_speed
);
3928 sync_completed_show(mddev_t
*mddev
, char *page
)
3930 unsigned long long max_sectors
, resync
;
3932 if (!test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3933 return sprintf(page
, "none\n");
3935 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
3936 max_sectors
= mddev
->resync_max_sectors
;
3938 max_sectors
= mddev
->dev_sectors
;
3940 resync
= mddev
->curr_resync_completed
;
3941 return sprintf(page
, "%llu / %llu\n", resync
, max_sectors
);
3944 static struct md_sysfs_entry md_sync_completed
= __ATTR_RO(sync_completed
);
3947 min_sync_show(mddev_t
*mddev
, char *page
)
3949 return sprintf(page
, "%llu\n",
3950 (unsigned long long)mddev
->resync_min
);
3953 min_sync_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3955 unsigned long long min
;
3956 if (strict_strtoull(buf
, 10, &min
))
3958 if (min
> mddev
->resync_max
)
3960 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3963 /* Must be a multiple of chunk_size */
3964 if (mddev
->chunk_sectors
) {
3965 sector_t temp
= min
;
3966 if (sector_div(temp
, mddev
->chunk_sectors
))
3969 mddev
->resync_min
= min
;
3974 static struct md_sysfs_entry md_min_sync
=
3975 __ATTR(sync_min
, S_IRUGO
|S_IWUSR
, min_sync_show
, min_sync_store
);
3978 max_sync_show(mddev_t
*mddev
, char *page
)
3980 if (mddev
->resync_max
== MaxSector
)
3981 return sprintf(page
, "max\n");
3983 return sprintf(page
, "%llu\n",
3984 (unsigned long long)mddev
->resync_max
);
3987 max_sync_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3989 if (strncmp(buf
, "max", 3) == 0)
3990 mddev
->resync_max
= MaxSector
;
3992 unsigned long long max
;
3993 if (strict_strtoull(buf
, 10, &max
))
3995 if (max
< mddev
->resync_min
)
3997 if (max
< mddev
->resync_max
&&
3999 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4002 /* Must be a multiple of chunk_size */
4003 if (mddev
->chunk_sectors
) {
4004 sector_t temp
= max
;
4005 if (sector_div(temp
, mddev
->chunk_sectors
))
4008 mddev
->resync_max
= max
;
4010 wake_up(&mddev
->recovery_wait
);
4014 static struct md_sysfs_entry md_max_sync
=
4015 __ATTR(sync_max
, S_IRUGO
|S_IWUSR
, max_sync_show
, max_sync_store
);
4018 suspend_lo_show(mddev_t
*mddev
, char *page
)
4020 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_lo
);
4024 suspend_lo_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4027 unsigned long long new = simple_strtoull(buf
, &e
, 10);
4028 unsigned long long old
= mddev
->suspend_lo
;
4030 if (mddev
->pers
== NULL
||
4031 mddev
->pers
->quiesce
== NULL
)
4033 if (buf
== e
|| (*e
&& *e
!= '\n'))
4036 mddev
->suspend_lo
= new;
4038 /* Shrinking suspended region */
4039 mddev
->pers
->quiesce(mddev
, 2);
4041 /* Expanding suspended region - need to wait */
4042 mddev
->pers
->quiesce(mddev
, 1);
4043 mddev
->pers
->quiesce(mddev
, 0);
4047 static struct md_sysfs_entry md_suspend_lo
=
4048 __ATTR(suspend_lo
, S_IRUGO
|S_IWUSR
, suspend_lo_show
, suspend_lo_store
);
4052 suspend_hi_show(mddev_t
*mddev
, char *page
)
4054 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_hi
);
4058 suspend_hi_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4061 unsigned long long new = simple_strtoull(buf
, &e
, 10);
4062 unsigned long long old
= mddev
->suspend_hi
;
4064 if (mddev
->pers
== NULL
||
4065 mddev
->pers
->quiesce
== NULL
)
4067 if (buf
== e
|| (*e
&& *e
!= '\n'))
4070 mddev
->suspend_hi
= new;
4072 /* Shrinking suspended region */
4073 mddev
->pers
->quiesce(mddev
, 2);
4075 /* Expanding suspended region - need to wait */
4076 mddev
->pers
->quiesce(mddev
, 1);
4077 mddev
->pers
->quiesce(mddev
, 0);
4081 static struct md_sysfs_entry md_suspend_hi
=
4082 __ATTR(suspend_hi
, S_IRUGO
|S_IWUSR
, suspend_hi_show
, suspend_hi_store
);
4085 reshape_position_show(mddev_t
*mddev
, char *page
)
4087 if (mddev
->reshape_position
!= MaxSector
)
4088 return sprintf(page
, "%llu\n",
4089 (unsigned long long)mddev
->reshape_position
);
4090 strcpy(page
, "none\n");
4095 reshape_position_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4098 unsigned long long new = simple_strtoull(buf
, &e
, 10);
4101 if (buf
== e
|| (*e
&& *e
!= '\n'))
4103 mddev
->reshape_position
= new;
4104 mddev
->delta_disks
= 0;
4105 mddev
->new_level
= mddev
->level
;
4106 mddev
->new_layout
= mddev
->layout
;
4107 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
4111 static struct md_sysfs_entry md_reshape_position
=
4112 __ATTR(reshape_position
, S_IRUGO
|S_IWUSR
, reshape_position_show
,
4113 reshape_position_store
);
4116 array_size_show(mddev_t
*mddev
, char *page
)
4118 if (mddev
->external_size
)
4119 return sprintf(page
, "%llu\n",
4120 (unsigned long long)mddev
->array_sectors
/2);
4122 return sprintf(page
, "default\n");
4126 array_size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4130 if (strncmp(buf
, "default", 7) == 0) {
4132 sectors
= mddev
->pers
->size(mddev
, 0, 0);
4134 sectors
= mddev
->array_sectors
;
4136 mddev
->external_size
= 0;
4138 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
4140 if (mddev
->pers
&& mddev
->pers
->size(mddev
, 0, 0) < sectors
)
4143 mddev
->external_size
= 1;
4146 mddev
->array_sectors
= sectors
;
4148 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
4149 revalidate_disk(mddev
->gendisk
);
4154 static struct md_sysfs_entry md_array_size
=
4155 __ATTR(array_size
, S_IRUGO
|S_IWUSR
, array_size_show
,
4158 static struct attribute
*md_default_attrs
[] = {
4161 &md_raid_disks
.attr
,
4162 &md_chunk_size
.attr
,
4164 &md_resync_start
.attr
,
4166 &md_new_device
.attr
,
4167 &md_safe_delay
.attr
,
4168 &md_array_state
.attr
,
4169 &md_reshape_position
.attr
,
4170 &md_array_size
.attr
,
4171 &max_corr_read_errors
.attr
,
4175 static struct attribute
*md_redundancy_attrs
[] = {
4177 &md_mismatches
.attr
,
4180 &md_sync_speed
.attr
,
4181 &md_sync_force_parallel
.attr
,
4182 &md_sync_completed
.attr
,
4185 &md_suspend_lo
.attr
,
4186 &md_suspend_hi
.attr
,
4191 static struct attribute_group md_redundancy_group
= {
4193 .attrs
= md_redundancy_attrs
,
4198 md_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
4200 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
4201 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
4206 rv
= mddev_lock(mddev
);
4208 rv
= entry
->show(mddev
, page
);
4209 mddev_unlock(mddev
);
4215 md_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
4216 const char *page
, size_t length
)
4218 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
4219 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
4224 if (!capable(CAP_SYS_ADMIN
))
4226 rv
= mddev_lock(mddev
);
4227 if (mddev
->hold_active
== UNTIL_IOCTL
)
4228 mddev
->hold_active
= 0;
4230 rv
= entry
->store(mddev
, page
, length
);
4231 mddev_unlock(mddev
);
4236 static void md_free(struct kobject
*ko
)
4238 mddev_t
*mddev
= container_of(ko
, mddev_t
, kobj
);
4240 if (mddev
->sysfs_state
)
4241 sysfs_put(mddev
->sysfs_state
);
4243 if (mddev
->gendisk
) {
4244 del_gendisk(mddev
->gendisk
);
4245 put_disk(mddev
->gendisk
);
4248 blk_cleanup_queue(mddev
->queue
);
4253 static const struct sysfs_ops md_sysfs_ops
= {
4254 .show
= md_attr_show
,
4255 .store
= md_attr_store
,
4257 static struct kobj_type md_ktype
= {
4259 .sysfs_ops
= &md_sysfs_ops
,
4260 .default_attrs
= md_default_attrs
,
4265 static void mddev_delayed_delete(struct work_struct
*ws
)
4267 mddev_t
*mddev
= container_of(ws
, mddev_t
, del_work
);
4269 sysfs_remove_group(&mddev
->kobj
, &md_bitmap_group
);
4270 kobject_del(&mddev
->kobj
);
4271 kobject_put(&mddev
->kobj
);
4274 static int md_alloc(dev_t dev
, char *name
)
4276 static DEFINE_MUTEX(disks_mutex
);
4277 mddev_t
*mddev
= mddev_find(dev
);
4278 struct gendisk
*disk
;
4287 partitioned
= (MAJOR(mddev
->unit
) != MD_MAJOR
);
4288 shift
= partitioned
? MdpMinorShift
: 0;
4289 unit
= MINOR(mddev
->unit
) >> shift
;
4291 /* wait for any previous instance of this device to be
4292 * completely removed (mddev_delayed_delete).
4294 flush_workqueue(md_misc_wq
);
4296 mutex_lock(&disks_mutex
);
4302 /* Need to ensure that 'name' is not a duplicate.
4305 spin_lock(&all_mddevs_lock
);
4307 list_for_each_entry(mddev2
, &all_mddevs
, all_mddevs
)
4308 if (mddev2
->gendisk
&&
4309 strcmp(mddev2
->gendisk
->disk_name
, name
) == 0) {
4310 spin_unlock(&all_mddevs_lock
);
4313 spin_unlock(&all_mddevs_lock
);
4317 mddev
->queue
= blk_alloc_queue(GFP_KERNEL
);
4320 mddev
->queue
->queuedata
= mddev
;
4322 blk_queue_make_request(mddev
->queue
, md_make_request
);
4324 disk
= alloc_disk(1 << shift
);
4326 blk_cleanup_queue(mddev
->queue
);
4327 mddev
->queue
= NULL
;
4330 disk
->major
= MAJOR(mddev
->unit
);
4331 disk
->first_minor
= unit
<< shift
;
4333 strcpy(disk
->disk_name
, name
);
4334 else if (partitioned
)
4335 sprintf(disk
->disk_name
, "md_d%d", unit
);
4337 sprintf(disk
->disk_name
, "md%d", unit
);
4338 disk
->fops
= &md_fops
;
4339 disk
->private_data
= mddev
;
4340 disk
->queue
= mddev
->queue
;
4341 /* Allow extended partitions. This makes the
4342 * 'mdp' device redundant, but we can't really
4345 disk
->flags
|= GENHD_FL_EXT_DEVT
;
4347 mddev
->gendisk
= disk
;
4348 error
= kobject_init_and_add(&mddev
->kobj
, &md_ktype
,
4349 &disk_to_dev(disk
)->kobj
, "%s", "md");
4351 /* This isn't possible, but as kobject_init_and_add is marked
4352 * __must_check, we must do something with the result
4354 printk(KERN_WARNING
"md: cannot register %s/md - name in use\n",
4358 if (mddev
->kobj
.sd
&&
4359 sysfs_create_group(&mddev
->kobj
, &md_bitmap_group
))
4360 printk(KERN_DEBUG
"pointless warning\n");
4362 blk_queue_flush(mddev
->queue
, REQ_FLUSH
| REQ_FUA
);
4364 mutex_unlock(&disks_mutex
);
4365 if (!error
&& mddev
->kobj
.sd
) {
4366 kobject_uevent(&mddev
->kobj
, KOBJ_ADD
);
4367 mddev
->sysfs_state
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "array_state");
4373 static struct kobject
*md_probe(dev_t dev
, int *part
, void *data
)
4375 md_alloc(dev
, NULL
);
4379 static int add_named_array(const char *val
, struct kernel_param
*kp
)
4381 /* val must be "md_*" where * is not all digits.
4382 * We allocate an array with a large free minor number, and
4383 * set the name to val. val must not already be an active name.
4385 int len
= strlen(val
);
4386 char buf
[DISK_NAME_LEN
];
4388 while (len
&& val
[len
-1] == '\n')
4390 if (len
>= DISK_NAME_LEN
)
4392 strlcpy(buf
, val
, len
+1);
4393 if (strncmp(buf
, "md_", 3) != 0)
4395 return md_alloc(0, buf
);
4398 static void md_safemode_timeout(unsigned long data
)
4400 mddev_t
*mddev
= (mddev_t
*) data
;
4402 if (!atomic_read(&mddev
->writes_pending
)) {
4403 mddev
->safemode
= 1;
4404 if (mddev
->external
)
4405 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4407 md_wakeup_thread(mddev
->thread
);
4410 static int start_dirty_degraded
;
4412 int md_run(mddev_t
*mddev
)
4416 struct mdk_personality
*pers
;
4418 if (list_empty(&mddev
->disks
))
4419 /* cannot run an array with no devices.. */
4424 /* Cannot run until previous stop completes properly */
4425 if (mddev
->sysfs_active
)
4429 * Analyze all RAID superblock(s)
4431 if (!mddev
->raid_disks
) {
4432 if (!mddev
->persistent
)
4437 if (mddev
->level
!= LEVEL_NONE
)
4438 request_module("md-level-%d", mddev
->level
);
4439 else if (mddev
->clevel
[0])
4440 request_module("md-%s", mddev
->clevel
);
4443 * Drop all container device buffers, from now on
4444 * the only valid external interface is through the md
4447 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
4448 if (test_bit(Faulty
, &rdev
->flags
))
4450 sync_blockdev(rdev
->bdev
);
4451 invalidate_bdev(rdev
->bdev
);
4453 /* perform some consistency tests on the device.
4454 * We don't want the data to overlap the metadata,
4455 * Internal Bitmap issues have been handled elsewhere.
4457 if (rdev
->meta_bdev
) {
4458 /* Nothing to check */;
4459 } else if (rdev
->data_offset
< rdev
->sb_start
) {
4460 if (mddev
->dev_sectors
&&
4461 rdev
->data_offset
+ mddev
->dev_sectors
4463 printk("md: %s: data overlaps metadata\n",
4468 if (rdev
->sb_start
+ rdev
->sb_size
/512
4469 > rdev
->data_offset
) {
4470 printk("md: %s: metadata overlaps data\n",
4475 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
4478 if (mddev
->bio_set
== NULL
)
4479 mddev
->bio_set
= bioset_create(BIO_POOL_SIZE
, sizeof(mddev
));
4481 spin_lock(&pers_lock
);
4482 pers
= find_pers(mddev
->level
, mddev
->clevel
);
4483 if (!pers
|| !try_module_get(pers
->owner
)) {
4484 spin_unlock(&pers_lock
);
4485 if (mddev
->level
!= LEVEL_NONE
)
4486 printk(KERN_WARNING
"md: personality for level %d is not loaded!\n",
4489 printk(KERN_WARNING
"md: personality for level %s is not loaded!\n",
4494 spin_unlock(&pers_lock
);
4495 if (mddev
->level
!= pers
->level
) {
4496 mddev
->level
= pers
->level
;
4497 mddev
->new_level
= pers
->level
;
4499 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
4501 if (mddev
->reshape_position
!= MaxSector
&&
4502 pers
->start_reshape
== NULL
) {
4503 /* This personality cannot handle reshaping... */
4505 module_put(pers
->owner
);
4509 if (pers
->sync_request
) {
4510 /* Warn if this is a potentially silly
4513 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
4517 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4518 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
4520 rdev
->bdev
->bd_contains
==
4521 rdev2
->bdev
->bd_contains
) {
4523 "%s: WARNING: %s appears to be"
4524 " on the same physical disk as"
4527 bdevname(rdev
->bdev
,b
),
4528 bdevname(rdev2
->bdev
,b2
));
4535 "True protection against single-disk"
4536 " failure might be compromised.\n");
4539 mddev
->recovery
= 0;
4540 /* may be over-ridden by personality */
4541 mddev
->resync_max_sectors
= mddev
->dev_sectors
;
4543 mddev
->ok_start_degraded
= start_dirty_degraded
;
4545 if (start_readonly
&& mddev
->ro
== 0)
4546 mddev
->ro
= 2; /* read-only, but switch on first write */
4548 err
= mddev
->pers
->run(mddev
);
4550 printk(KERN_ERR
"md: pers->run() failed ...\n");
4551 else if (mddev
->pers
->size(mddev
, 0, 0) < mddev
->array_sectors
) {
4552 WARN_ONCE(!mddev
->external_size
, "%s: default size too small,"
4553 " but 'external_size' not in effect?\n", __func__
);
4555 "md: invalid array_size %llu > default size %llu\n",
4556 (unsigned long long)mddev
->array_sectors
/ 2,
4557 (unsigned long long)mddev
->pers
->size(mddev
, 0, 0) / 2);
4559 mddev
->pers
->stop(mddev
);
4561 if (err
== 0 && mddev
->pers
->sync_request
) {
4562 err
= bitmap_create(mddev
);
4564 printk(KERN_ERR
"%s: failed to create bitmap (%d)\n",
4565 mdname(mddev
), err
);
4566 mddev
->pers
->stop(mddev
);
4570 module_put(mddev
->pers
->owner
);
4572 bitmap_destroy(mddev
);
4575 if (mddev
->pers
->sync_request
) {
4576 if (mddev
->kobj
.sd
&&
4577 sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
4579 "md: cannot register extra attributes for %s\n",
4581 mddev
->sysfs_action
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "sync_action");
4582 } else if (mddev
->ro
== 2) /* auto-readonly not meaningful */
4585 atomic_set(&mddev
->writes_pending
,0);
4586 atomic_set(&mddev
->max_corr_read_errors
,
4587 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS
);
4588 mddev
->safemode
= 0;
4589 mddev
->safemode_timer
.function
= md_safemode_timeout
;
4590 mddev
->safemode_timer
.data
= (unsigned long) mddev
;
4591 mddev
->safemode_delay
= (200 * HZ
)/1000 +1; /* 200 msec delay */
4595 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4596 if (rdev
->raid_disk
>= 0) {
4598 sprintf(nm
, "rd%d", rdev
->raid_disk
);
4599 if (sysfs_create_link(&mddev
->kobj
, &rdev
->kobj
, nm
))
4600 /* failure here is OK */;
4603 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4606 md_update_sb(mddev
, 0);
4608 md_wakeup_thread(mddev
->thread
);
4609 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
4611 md_new_event(mddev
);
4612 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4613 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
4614 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
4617 EXPORT_SYMBOL_GPL(md_run
);
4619 static int do_md_run(mddev_t
*mddev
)
4623 err
= md_run(mddev
);
4626 err
= bitmap_load(mddev
);
4628 bitmap_destroy(mddev
);
4631 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
4632 revalidate_disk(mddev
->gendisk
);
4634 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
4639 static int restart_array(mddev_t
*mddev
)
4641 struct gendisk
*disk
= mddev
->gendisk
;
4643 /* Complain if it has no devices */
4644 if (list_empty(&mddev
->disks
))
4650 mddev
->safemode
= 0;
4652 set_disk_ro(disk
, 0);
4653 printk(KERN_INFO
"md: %s switched to read-write mode.\n",
4655 /* Kick recovery or resync if necessary */
4656 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4657 md_wakeup_thread(mddev
->thread
);
4658 md_wakeup_thread(mddev
->sync_thread
);
4659 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4663 /* similar to deny_write_access, but accounts for our holding a reference
4664 * to the file ourselves */
4665 static int deny_bitmap_write_access(struct file
* file
)
4667 struct inode
*inode
= file
->f_mapping
->host
;
4669 spin_lock(&inode
->i_lock
);
4670 if (atomic_read(&inode
->i_writecount
) > 1) {
4671 spin_unlock(&inode
->i_lock
);
4674 atomic_set(&inode
->i_writecount
, -1);
4675 spin_unlock(&inode
->i_lock
);
4680 void restore_bitmap_write_access(struct file
*file
)
4682 struct inode
*inode
= file
->f_mapping
->host
;
4684 spin_lock(&inode
->i_lock
);
4685 atomic_set(&inode
->i_writecount
, 1);
4686 spin_unlock(&inode
->i_lock
);
4689 static void md_clean(mddev_t
*mddev
)
4691 mddev
->array_sectors
= 0;
4692 mddev
->external_size
= 0;
4693 mddev
->dev_sectors
= 0;
4694 mddev
->raid_disks
= 0;
4695 mddev
->recovery_cp
= 0;
4696 mddev
->resync_min
= 0;
4697 mddev
->resync_max
= MaxSector
;
4698 mddev
->reshape_position
= MaxSector
;
4699 mddev
->external
= 0;
4700 mddev
->persistent
= 0;
4701 mddev
->level
= LEVEL_NONE
;
4702 mddev
->clevel
[0] = 0;
4705 mddev
->metadata_type
[0] = 0;
4706 mddev
->chunk_sectors
= 0;
4707 mddev
->ctime
= mddev
->utime
= 0;
4709 mddev
->max_disks
= 0;
4711 mddev
->can_decrease_events
= 0;
4712 mddev
->delta_disks
= 0;
4713 mddev
->new_level
= LEVEL_NONE
;
4714 mddev
->new_layout
= 0;
4715 mddev
->new_chunk_sectors
= 0;
4716 mddev
->curr_resync
= 0;
4717 mddev
->resync_mismatches
= 0;
4718 mddev
->suspend_lo
= mddev
->suspend_hi
= 0;
4719 mddev
->sync_speed_min
= mddev
->sync_speed_max
= 0;
4720 mddev
->recovery
= 0;
4723 mddev
->degraded
= 0;
4724 mddev
->safemode
= 0;
4725 mddev
->bitmap_info
.offset
= 0;
4726 mddev
->bitmap_info
.default_offset
= 0;
4727 mddev
->bitmap_info
.chunksize
= 0;
4728 mddev
->bitmap_info
.daemon_sleep
= 0;
4729 mddev
->bitmap_info
.max_write_behind
= 0;
4733 static void __md_stop_writes(mddev_t
*mddev
)
4735 if (mddev
->sync_thread
) {
4736 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4737 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4738 reap_sync_thread(mddev
);
4741 del_timer_sync(&mddev
->safemode_timer
);
4743 bitmap_flush(mddev
);
4744 md_super_wait(mddev
);
4746 if (!mddev
->in_sync
|| mddev
->flags
) {
4747 /* mark array as shutdown cleanly */
4749 md_update_sb(mddev
, 1);
4753 void md_stop_writes(mddev_t
*mddev
)
4756 __md_stop_writes(mddev
);
4757 mddev_unlock(mddev
);
4759 EXPORT_SYMBOL_GPL(md_stop_writes
);
4761 void md_stop(mddev_t
*mddev
)
4764 mddev
->pers
->stop(mddev
);
4765 if (mddev
->pers
->sync_request
&& mddev
->to_remove
== NULL
)
4766 mddev
->to_remove
= &md_redundancy_group
;
4767 module_put(mddev
->pers
->owner
);
4769 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4771 EXPORT_SYMBOL_GPL(md_stop
);
4773 static int md_set_readonly(mddev_t
*mddev
, int is_open
)
4776 mutex_lock(&mddev
->open_mutex
);
4777 if (atomic_read(&mddev
->openers
) > is_open
) {
4778 printk("md: %s still in use.\n",mdname(mddev
));
4783 __md_stop_writes(mddev
);
4789 set_disk_ro(mddev
->gendisk
, 1);
4790 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4791 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4795 mutex_unlock(&mddev
->open_mutex
);
4800 * 0 - completely stop and dis-assemble array
4801 * 2 - stop but do not disassemble array
4803 static int do_md_stop(mddev_t
* mddev
, int mode
, int is_open
)
4805 struct gendisk
*disk
= mddev
->gendisk
;
4808 mutex_lock(&mddev
->open_mutex
);
4809 if (atomic_read(&mddev
->openers
) > is_open
||
4810 mddev
->sysfs_active
) {
4811 printk("md: %s still in use.\n",mdname(mddev
));
4812 mutex_unlock(&mddev
->open_mutex
);
4818 set_disk_ro(disk
, 0);
4820 __md_stop_writes(mddev
);
4822 mddev
->queue
->merge_bvec_fn
= NULL
;
4823 mddev
->queue
->backing_dev_info
.congested_fn
= NULL
;
4825 /* tell userspace to handle 'inactive' */
4826 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4828 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4829 if (rdev
->raid_disk
>= 0) {
4831 sprintf(nm
, "rd%d", rdev
->raid_disk
);
4832 sysfs_remove_link(&mddev
->kobj
, nm
);
4835 set_capacity(disk
, 0);
4836 mutex_unlock(&mddev
->open_mutex
);
4838 revalidate_disk(disk
);
4843 mutex_unlock(&mddev
->open_mutex
);
4845 * Free resources if final stop
4848 printk(KERN_INFO
"md: %s stopped.\n", mdname(mddev
));
4850 bitmap_destroy(mddev
);
4851 if (mddev
->bitmap_info
.file
) {
4852 restore_bitmap_write_access(mddev
->bitmap_info
.file
);
4853 fput(mddev
->bitmap_info
.file
);
4854 mddev
->bitmap_info
.file
= NULL
;
4856 mddev
->bitmap_info
.offset
= 0;
4858 export_array(mddev
);
4861 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
4862 if (mddev
->hold_active
== UNTIL_STOP
)
4863 mddev
->hold_active
= 0;
4865 blk_integrity_unregister(disk
);
4866 md_new_event(mddev
);
4867 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4872 static void autorun_array(mddev_t
*mddev
)
4877 if (list_empty(&mddev
->disks
))
4880 printk(KERN_INFO
"md: running: ");
4882 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
4883 char b
[BDEVNAME_SIZE
];
4884 printk("<%s>", bdevname(rdev
->bdev
,b
));
4888 err
= do_md_run(mddev
);
4890 printk(KERN_WARNING
"md: do_md_run() returned %d\n", err
);
4891 do_md_stop(mddev
, 0, 0);
4896 * lets try to run arrays based on all disks that have arrived
4897 * until now. (those are in pending_raid_disks)
4899 * the method: pick the first pending disk, collect all disks with
4900 * the same UUID, remove all from the pending list and put them into
4901 * the 'same_array' list. Then order this list based on superblock
4902 * update time (freshest comes first), kick out 'old' disks and
4903 * compare superblocks. If everything's fine then run it.
4905 * If "unit" is allocated, then bump its reference count
4907 static void autorun_devices(int part
)
4909 mdk_rdev_t
*rdev0
, *rdev
, *tmp
;
4911 char b
[BDEVNAME_SIZE
];
4913 printk(KERN_INFO
"md: autorun ...\n");
4914 while (!list_empty(&pending_raid_disks
)) {
4917 LIST_HEAD(candidates
);
4918 rdev0
= list_entry(pending_raid_disks
.next
,
4919 mdk_rdev_t
, same_set
);
4921 printk(KERN_INFO
"md: considering %s ...\n",
4922 bdevname(rdev0
->bdev
,b
));
4923 INIT_LIST_HEAD(&candidates
);
4924 rdev_for_each_list(rdev
, tmp
, &pending_raid_disks
)
4925 if (super_90_load(rdev
, rdev0
, 0) >= 0) {
4926 printk(KERN_INFO
"md: adding %s ...\n",
4927 bdevname(rdev
->bdev
,b
));
4928 list_move(&rdev
->same_set
, &candidates
);
4931 * now we have a set of devices, with all of them having
4932 * mostly sane superblocks. It's time to allocate the
4936 dev
= MKDEV(mdp_major
,
4937 rdev0
->preferred_minor
<< MdpMinorShift
);
4938 unit
= MINOR(dev
) >> MdpMinorShift
;
4940 dev
= MKDEV(MD_MAJOR
, rdev0
->preferred_minor
);
4943 if (rdev0
->preferred_minor
!= unit
) {
4944 printk(KERN_INFO
"md: unit number in %s is bad: %d\n",
4945 bdevname(rdev0
->bdev
, b
), rdev0
->preferred_minor
);
4949 md_probe(dev
, NULL
, NULL
);
4950 mddev
= mddev_find(dev
);
4951 if (!mddev
|| !mddev
->gendisk
) {
4955 "md: cannot allocate memory for md drive.\n");
4958 if (mddev_lock(mddev
))
4959 printk(KERN_WARNING
"md: %s locked, cannot run\n",
4961 else if (mddev
->raid_disks
|| mddev
->major_version
4962 || !list_empty(&mddev
->disks
)) {
4964 "md: %s already running, cannot run %s\n",
4965 mdname(mddev
), bdevname(rdev0
->bdev
,b
));
4966 mddev_unlock(mddev
);
4968 printk(KERN_INFO
"md: created %s\n", mdname(mddev
));
4969 mddev
->persistent
= 1;
4970 rdev_for_each_list(rdev
, tmp
, &candidates
) {
4971 list_del_init(&rdev
->same_set
);
4972 if (bind_rdev_to_array(rdev
, mddev
))
4975 autorun_array(mddev
);
4976 mddev_unlock(mddev
);
4978 /* on success, candidates will be empty, on error
4981 rdev_for_each_list(rdev
, tmp
, &candidates
) {
4982 list_del_init(&rdev
->same_set
);
4987 printk(KERN_INFO
"md: ... autorun DONE.\n");
4989 #endif /* !MODULE */
4991 static int get_version(void __user
* arg
)
4995 ver
.major
= MD_MAJOR_VERSION
;
4996 ver
.minor
= MD_MINOR_VERSION
;
4997 ver
.patchlevel
= MD_PATCHLEVEL_VERSION
;
4999 if (copy_to_user(arg
, &ver
, sizeof(ver
)))
5005 static int get_array_info(mddev_t
* mddev
, void __user
* arg
)
5007 mdu_array_info_t info
;
5008 int nr
,working
,insync
,failed
,spare
;
5011 nr
=working
=insync
=failed
=spare
=0;
5012 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
5014 if (test_bit(Faulty
, &rdev
->flags
))
5018 if (test_bit(In_sync
, &rdev
->flags
))
5025 info
.major_version
= mddev
->major_version
;
5026 info
.minor_version
= mddev
->minor_version
;
5027 info
.patch_version
= MD_PATCHLEVEL_VERSION
;
5028 info
.ctime
= mddev
->ctime
;
5029 info
.level
= mddev
->level
;
5030 info
.size
= mddev
->dev_sectors
/ 2;
5031 if (info
.size
!= mddev
->dev_sectors
/ 2) /* overflow */
5034 info
.raid_disks
= mddev
->raid_disks
;
5035 info
.md_minor
= mddev
->md_minor
;
5036 info
.not_persistent
= !mddev
->persistent
;
5038 info
.utime
= mddev
->utime
;
5041 info
.state
= (1<<MD_SB_CLEAN
);
5042 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
5043 info
.state
= (1<<MD_SB_BITMAP_PRESENT
);
5044 info
.active_disks
= insync
;
5045 info
.working_disks
= working
;
5046 info
.failed_disks
= failed
;
5047 info
.spare_disks
= spare
;
5049 info
.layout
= mddev
->layout
;
5050 info
.chunk_size
= mddev
->chunk_sectors
<< 9;
5052 if (copy_to_user(arg
, &info
, sizeof(info
)))
5058 static int get_bitmap_file(mddev_t
* mddev
, void __user
* arg
)
5060 mdu_bitmap_file_t
*file
= NULL
; /* too big for stack allocation */
5061 char *ptr
, *buf
= NULL
;
5064 if (md_allow_write(mddev
))
5065 file
= kmalloc(sizeof(*file
), GFP_NOIO
);
5067 file
= kmalloc(sizeof(*file
), GFP_KERNEL
);
5072 /* bitmap disabled, zero the first byte and copy out */
5073 if (!mddev
->bitmap
|| !mddev
->bitmap
->file
) {
5074 file
->pathname
[0] = '\0';
5078 buf
= kmalloc(sizeof(file
->pathname
), GFP_KERNEL
);
5082 ptr
= d_path(&mddev
->bitmap
->file
->f_path
, buf
, sizeof(file
->pathname
));
5086 strcpy(file
->pathname
, ptr
);
5090 if (copy_to_user(arg
, file
, sizeof(*file
)))
5098 static int get_disk_info(mddev_t
* mddev
, void __user
* arg
)
5100 mdu_disk_info_t info
;
5103 if (copy_from_user(&info
, arg
, sizeof(info
)))
5106 rdev
= find_rdev_nr(mddev
, info
.number
);
5108 info
.major
= MAJOR(rdev
->bdev
->bd_dev
);
5109 info
.minor
= MINOR(rdev
->bdev
->bd_dev
);
5110 info
.raid_disk
= rdev
->raid_disk
;
5112 if (test_bit(Faulty
, &rdev
->flags
))
5113 info
.state
|= (1<<MD_DISK_FAULTY
);
5114 else if (test_bit(In_sync
, &rdev
->flags
)) {
5115 info
.state
|= (1<<MD_DISK_ACTIVE
);
5116 info
.state
|= (1<<MD_DISK_SYNC
);
5118 if (test_bit(WriteMostly
, &rdev
->flags
))
5119 info
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
5121 info
.major
= info
.minor
= 0;
5122 info
.raid_disk
= -1;
5123 info
.state
= (1<<MD_DISK_REMOVED
);
5126 if (copy_to_user(arg
, &info
, sizeof(info
)))
5132 static int add_new_disk(mddev_t
* mddev
, mdu_disk_info_t
*info
)
5134 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
5136 dev_t dev
= MKDEV(info
->major
,info
->minor
);
5138 if (info
->major
!= MAJOR(dev
) || info
->minor
!= MINOR(dev
))
5141 if (!mddev
->raid_disks
) {
5143 /* expecting a device which has a superblock */
5144 rdev
= md_import_device(dev
, mddev
->major_version
, mddev
->minor_version
);
5147 "md: md_import_device returned %ld\n",
5149 return PTR_ERR(rdev
);
5151 if (!list_empty(&mddev
->disks
)) {
5152 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
5153 mdk_rdev_t
, same_set
);
5154 err
= super_types
[mddev
->major_version
]
5155 .load_super(rdev
, rdev0
, mddev
->minor_version
);
5158 "md: %s has different UUID to %s\n",
5159 bdevname(rdev
->bdev
,b
),
5160 bdevname(rdev0
->bdev
,b2
));
5165 err
= bind_rdev_to_array(rdev
, mddev
);
5172 * add_new_disk can be used once the array is assembled
5173 * to add "hot spares". They must already have a superblock
5178 if (!mddev
->pers
->hot_add_disk
) {
5180 "%s: personality does not support diskops!\n",
5184 if (mddev
->persistent
)
5185 rdev
= md_import_device(dev
, mddev
->major_version
,
5186 mddev
->minor_version
);
5188 rdev
= md_import_device(dev
, -1, -1);
5191 "md: md_import_device returned %ld\n",
5193 return PTR_ERR(rdev
);
5195 /* set saved_raid_disk if appropriate */
5196 if (!mddev
->persistent
) {
5197 if (info
->state
& (1<<MD_DISK_SYNC
) &&
5198 info
->raid_disk
< mddev
->raid_disks
) {
5199 rdev
->raid_disk
= info
->raid_disk
;
5200 set_bit(In_sync
, &rdev
->flags
);
5202 rdev
->raid_disk
= -1;
5204 super_types
[mddev
->major_version
].
5205 validate_super(mddev
, rdev
);
5206 if (test_bit(In_sync
, &rdev
->flags
))
5207 rdev
->saved_raid_disk
= rdev
->raid_disk
;
5209 rdev
->saved_raid_disk
= -1;
5211 clear_bit(In_sync
, &rdev
->flags
); /* just to be sure */
5212 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
5213 set_bit(WriteMostly
, &rdev
->flags
);
5215 clear_bit(WriteMostly
, &rdev
->flags
);
5217 rdev
->raid_disk
= -1;
5218 err
= bind_rdev_to_array(rdev
, mddev
);
5219 if (!err
&& !mddev
->pers
->hot_remove_disk
) {
5220 /* If there is hot_add_disk but no hot_remove_disk
5221 * then added disks for geometry changes,
5222 * and should be added immediately.
5224 super_types
[mddev
->major_version
].
5225 validate_super(mddev
, rdev
);
5226 err
= mddev
->pers
->hot_add_disk(mddev
, rdev
);
5228 unbind_rdev_from_array(rdev
);
5233 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
5235 md_update_sb(mddev
, 1);
5236 if (mddev
->degraded
)
5237 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
5238 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5239 md_wakeup_thread(mddev
->thread
);
5243 /* otherwise, add_new_disk is only allowed
5244 * for major_version==0 superblocks
5246 if (mddev
->major_version
!= 0) {
5247 printk(KERN_WARNING
"%s: ADD_NEW_DISK not supported\n",
5252 if (!(info
->state
& (1<<MD_DISK_FAULTY
))) {
5254 rdev
= md_import_device(dev
, -1, 0);
5257 "md: error, md_import_device() returned %ld\n",
5259 return PTR_ERR(rdev
);
5261 rdev
->desc_nr
= info
->number
;
5262 if (info
->raid_disk
< mddev
->raid_disks
)
5263 rdev
->raid_disk
= info
->raid_disk
;
5265 rdev
->raid_disk
= -1;
5267 if (rdev
->raid_disk
< mddev
->raid_disks
)
5268 if (info
->state
& (1<<MD_DISK_SYNC
))
5269 set_bit(In_sync
, &rdev
->flags
);
5271 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
5272 set_bit(WriteMostly
, &rdev
->flags
);
5274 if (!mddev
->persistent
) {
5275 printk(KERN_INFO
"md: nonpersistent superblock ...\n");
5276 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
5278 rdev
->sb_start
= calc_dev_sboffset(rdev
);
5279 rdev
->sectors
= rdev
->sb_start
;
5281 err
= bind_rdev_to_array(rdev
, mddev
);
5291 static int hot_remove_disk(mddev_t
* mddev
, dev_t dev
)
5293 char b
[BDEVNAME_SIZE
];
5296 rdev
= find_rdev(mddev
, dev
);
5300 if (rdev
->raid_disk
>= 0)
5303 kick_rdev_from_array(rdev
);
5304 md_update_sb(mddev
, 1);
5305 md_new_event(mddev
);
5309 printk(KERN_WARNING
"md: cannot remove active disk %s from %s ...\n",
5310 bdevname(rdev
->bdev
,b
), mdname(mddev
));
5314 static int hot_add_disk(mddev_t
* mddev
, dev_t dev
)
5316 char b
[BDEVNAME_SIZE
];
5323 if (mddev
->major_version
!= 0) {
5324 printk(KERN_WARNING
"%s: HOT_ADD may only be used with"
5325 " version-0 superblocks.\n",
5329 if (!mddev
->pers
->hot_add_disk
) {
5331 "%s: personality does not support diskops!\n",
5336 rdev
= md_import_device(dev
, -1, 0);
5339 "md: error, md_import_device() returned %ld\n",
5344 if (mddev
->persistent
)
5345 rdev
->sb_start
= calc_dev_sboffset(rdev
);
5347 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
5349 rdev
->sectors
= rdev
->sb_start
;
5351 if (test_bit(Faulty
, &rdev
->flags
)) {
5353 "md: can not hot-add faulty %s disk to %s!\n",
5354 bdevname(rdev
->bdev
,b
), mdname(mddev
));
5358 clear_bit(In_sync
, &rdev
->flags
);
5360 rdev
->saved_raid_disk
= -1;
5361 err
= bind_rdev_to_array(rdev
, mddev
);
5366 * The rest should better be atomic, we can have disk failures
5367 * noticed in interrupt contexts ...
5370 rdev
->raid_disk
= -1;
5372 md_update_sb(mddev
, 1);
5375 * Kick recovery, maybe this spare has to be added to the
5376 * array immediately.
5378 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5379 md_wakeup_thread(mddev
->thread
);
5380 md_new_event(mddev
);
5388 static int set_bitmap_file(mddev_t
*mddev
, int fd
)
5393 if (!mddev
->pers
->quiesce
)
5395 if (mddev
->recovery
|| mddev
->sync_thread
)
5397 /* we should be able to change the bitmap.. */
5403 return -EEXIST
; /* cannot add when bitmap is present */
5404 mddev
->bitmap_info
.file
= fget(fd
);
5406 if (mddev
->bitmap_info
.file
== NULL
) {
5407 printk(KERN_ERR
"%s: error: failed to get bitmap file\n",
5412 err
= deny_bitmap_write_access(mddev
->bitmap_info
.file
);
5414 printk(KERN_ERR
"%s: error: bitmap file is already in use\n",
5416 fput(mddev
->bitmap_info
.file
);
5417 mddev
->bitmap_info
.file
= NULL
;
5420 mddev
->bitmap_info
.offset
= 0; /* file overrides offset */
5421 } else if (mddev
->bitmap
== NULL
)
5422 return -ENOENT
; /* cannot remove what isn't there */
5425 mddev
->pers
->quiesce(mddev
, 1);
5427 err
= bitmap_create(mddev
);
5429 err
= bitmap_load(mddev
);
5431 if (fd
< 0 || err
) {
5432 bitmap_destroy(mddev
);
5433 fd
= -1; /* make sure to put the file */
5435 mddev
->pers
->quiesce(mddev
, 0);
5438 if (mddev
->bitmap_info
.file
) {
5439 restore_bitmap_write_access(mddev
->bitmap_info
.file
);
5440 fput(mddev
->bitmap_info
.file
);
5442 mddev
->bitmap_info
.file
= NULL
;
5449 * set_array_info is used two different ways
5450 * The original usage is when creating a new array.
5451 * In this usage, raid_disks is > 0 and it together with
5452 * level, size, not_persistent,layout,chunksize determine the
5453 * shape of the array.
5454 * This will always create an array with a type-0.90.0 superblock.
5455 * The newer usage is when assembling an array.
5456 * In this case raid_disks will be 0, and the major_version field is
5457 * use to determine which style super-blocks are to be found on the devices.
5458 * The minor and patch _version numbers are also kept incase the
5459 * super_block handler wishes to interpret them.
5461 static int set_array_info(mddev_t
* mddev
, mdu_array_info_t
*info
)
5464 if (info
->raid_disks
== 0) {
5465 /* just setting version number for superblock loading */
5466 if (info
->major_version
< 0 ||
5467 info
->major_version
>= ARRAY_SIZE(super_types
) ||
5468 super_types
[info
->major_version
].name
== NULL
) {
5469 /* maybe try to auto-load a module? */
5471 "md: superblock version %d not known\n",
5472 info
->major_version
);
5475 mddev
->major_version
= info
->major_version
;
5476 mddev
->minor_version
= info
->minor_version
;
5477 mddev
->patch_version
= info
->patch_version
;
5478 mddev
->persistent
= !info
->not_persistent
;
5479 /* ensure mddev_put doesn't delete this now that there
5480 * is some minimal configuration.
5482 mddev
->ctime
= get_seconds();
5485 mddev
->major_version
= MD_MAJOR_VERSION
;
5486 mddev
->minor_version
= MD_MINOR_VERSION
;
5487 mddev
->patch_version
= MD_PATCHLEVEL_VERSION
;
5488 mddev
->ctime
= get_seconds();
5490 mddev
->level
= info
->level
;
5491 mddev
->clevel
[0] = 0;
5492 mddev
->dev_sectors
= 2 * (sector_t
)info
->size
;
5493 mddev
->raid_disks
= info
->raid_disks
;
5494 /* don't set md_minor, it is determined by which /dev/md* was
5497 if (info
->state
& (1<<MD_SB_CLEAN
))
5498 mddev
->recovery_cp
= MaxSector
;
5500 mddev
->recovery_cp
= 0;
5501 mddev
->persistent
= ! info
->not_persistent
;
5502 mddev
->external
= 0;
5504 mddev
->layout
= info
->layout
;
5505 mddev
->chunk_sectors
= info
->chunk_size
>> 9;
5507 mddev
->max_disks
= MD_SB_DISKS
;
5509 if (mddev
->persistent
)
5511 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
5513 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
5514 mddev
->bitmap_info
.offset
= 0;
5516 mddev
->reshape_position
= MaxSector
;
5519 * Generate a 128 bit UUID
5521 get_random_bytes(mddev
->uuid
, 16);
5523 mddev
->new_level
= mddev
->level
;
5524 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
5525 mddev
->new_layout
= mddev
->layout
;
5526 mddev
->delta_disks
= 0;
5531 void md_set_array_sectors(mddev_t
*mddev
, sector_t array_sectors
)
5533 WARN(!mddev_is_locked(mddev
), "%s: unlocked mddev!\n", __func__
);
5535 if (mddev
->external_size
)
5538 mddev
->array_sectors
= array_sectors
;
5540 EXPORT_SYMBOL(md_set_array_sectors
);
5542 static int update_size(mddev_t
*mddev
, sector_t num_sectors
)
5546 int fit
= (num_sectors
== 0);
5548 if (mddev
->pers
->resize
== NULL
)
5550 /* The "num_sectors" is the number of sectors of each device that
5551 * is used. This can only make sense for arrays with redundancy.
5552 * linear and raid0 always use whatever space is available. We can only
5553 * consider changing this number if no resync or reconstruction is
5554 * happening, and if the new size is acceptable. It must fit before the
5555 * sb_start or, if that is <data_offset, it must fit before the size
5556 * of each device. If num_sectors is zero, we find the largest size
5559 if (mddev
->sync_thread
)
5562 /* Sorry, cannot grow a bitmap yet, just remove it,
5566 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
5567 sector_t avail
= rdev
->sectors
;
5569 if (fit
&& (num_sectors
== 0 || num_sectors
> avail
))
5570 num_sectors
= avail
;
5571 if (avail
< num_sectors
)
5574 rv
= mddev
->pers
->resize(mddev
, num_sectors
);
5576 revalidate_disk(mddev
->gendisk
);
5580 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
)
5583 /* change the number of raid disks */
5584 if (mddev
->pers
->check_reshape
== NULL
)
5586 if (raid_disks
<= 0 ||
5587 (mddev
->max_disks
&& raid_disks
>= mddev
->max_disks
))
5589 if (mddev
->sync_thread
|| mddev
->reshape_position
!= MaxSector
)
5591 mddev
->delta_disks
= raid_disks
- mddev
->raid_disks
;
5593 rv
= mddev
->pers
->check_reshape(mddev
);
5595 mddev
->delta_disks
= 0;
5601 * update_array_info is used to change the configuration of an
5603 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5604 * fields in the info are checked against the array.
5605 * Any differences that cannot be handled will cause an error.
5606 * Normally, only one change can be managed at a time.
5608 static int update_array_info(mddev_t
*mddev
, mdu_array_info_t
*info
)
5614 /* calculate expected state,ignoring low bits */
5615 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
5616 state
|= (1 << MD_SB_BITMAP_PRESENT
);
5618 if (mddev
->major_version
!= info
->major_version
||
5619 mddev
->minor_version
!= info
->minor_version
||
5620 /* mddev->patch_version != info->patch_version || */
5621 mddev
->ctime
!= info
->ctime
||
5622 mddev
->level
!= info
->level
||
5623 /* mddev->layout != info->layout || */
5624 !mddev
->persistent
!= info
->not_persistent
||
5625 mddev
->chunk_sectors
!= info
->chunk_size
>> 9 ||
5626 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5627 ((state
^info
->state
) & 0xfffffe00)
5630 /* Check there is only one change */
5631 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
5633 if (mddev
->raid_disks
!= info
->raid_disks
)
5635 if (mddev
->layout
!= info
->layout
)
5637 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
))
5644 if (mddev
->layout
!= info
->layout
) {
5646 * we don't need to do anything at the md level, the
5647 * personality will take care of it all.
5649 if (mddev
->pers
->check_reshape
== NULL
)
5652 mddev
->new_layout
= info
->layout
;
5653 rv
= mddev
->pers
->check_reshape(mddev
);
5655 mddev
->new_layout
= mddev
->layout
;
5659 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
5660 rv
= update_size(mddev
, (sector_t
)info
->size
* 2);
5662 if (mddev
->raid_disks
!= info
->raid_disks
)
5663 rv
= update_raid_disks(mddev
, info
->raid_disks
);
5665 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) {
5666 if (mddev
->pers
->quiesce
== NULL
)
5668 if (mddev
->recovery
|| mddev
->sync_thread
)
5670 if (info
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
5671 /* add the bitmap */
5674 if (mddev
->bitmap_info
.default_offset
== 0)
5676 mddev
->bitmap_info
.offset
=
5677 mddev
->bitmap_info
.default_offset
;
5678 mddev
->pers
->quiesce(mddev
, 1);
5679 rv
= bitmap_create(mddev
);
5681 rv
= bitmap_load(mddev
);
5683 bitmap_destroy(mddev
);
5684 mddev
->pers
->quiesce(mddev
, 0);
5686 /* remove the bitmap */
5689 if (mddev
->bitmap
->file
)
5691 mddev
->pers
->quiesce(mddev
, 1);
5692 bitmap_destroy(mddev
);
5693 mddev
->pers
->quiesce(mddev
, 0);
5694 mddev
->bitmap_info
.offset
= 0;
5697 md_update_sb(mddev
, 1);
5701 static int set_disk_faulty(mddev_t
*mddev
, dev_t dev
)
5705 if (mddev
->pers
== NULL
)
5708 rdev
= find_rdev(mddev
, dev
);
5712 md_error(mddev
, rdev
);
5717 * We have a problem here : there is no easy way to give a CHS
5718 * virtual geometry. We currently pretend that we have a 2 heads
5719 * 4 sectors (with a BIG number of cylinders...). This drives
5720 * dosfs just mad... ;-)
5722 static int md_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
5724 mddev_t
*mddev
= bdev
->bd_disk
->private_data
;
5728 geo
->cylinders
= mddev
->array_sectors
/ 8;
5732 static int md_ioctl(struct block_device
*bdev
, fmode_t mode
,
5733 unsigned int cmd
, unsigned long arg
)
5736 void __user
*argp
= (void __user
*)arg
;
5737 mddev_t
*mddev
= NULL
;
5740 if (!capable(CAP_SYS_ADMIN
))
5744 * Commands dealing with the RAID driver but not any
5750 err
= get_version(argp
);
5753 case PRINT_RAID_DEBUG
:
5761 autostart_arrays(arg
);
5768 * Commands creating/starting a new array:
5771 mddev
= bdev
->bd_disk
->private_data
;
5778 err
= mddev_lock(mddev
);
5781 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5788 case SET_ARRAY_INFO
:
5790 mdu_array_info_t info
;
5792 memset(&info
, 0, sizeof(info
));
5793 else if (copy_from_user(&info
, argp
, sizeof(info
))) {
5798 err
= update_array_info(mddev
, &info
);
5800 printk(KERN_WARNING
"md: couldn't update"
5801 " array info. %d\n", err
);
5806 if (!list_empty(&mddev
->disks
)) {
5808 "md: array %s already has disks!\n",
5813 if (mddev
->raid_disks
) {
5815 "md: array %s already initialised!\n",
5820 err
= set_array_info(mddev
, &info
);
5822 printk(KERN_WARNING
"md: couldn't set"
5823 " array info. %d\n", err
);
5833 * Commands querying/configuring an existing array:
5835 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
5836 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
5837 if ((!mddev
->raid_disks
&& !mddev
->external
)
5838 && cmd
!= ADD_NEW_DISK
&& cmd
!= STOP_ARRAY
5839 && cmd
!= RUN_ARRAY
&& cmd
!= SET_BITMAP_FILE
5840 && cmd
!= GET_BITMAP_FILE
) {
5846 * Commands even a read-only array can execute:
5850 case GET_ARRAY_INFO
:
5851 err
= get_array_info(mddev
, argp
);
5854 case GET_BITMAP_FILE
:
5855 err
= get_bitmap_file(mddev
, argp
);
5859 err
= get_disk_info(mddev
, argp
);
5862 case RESTART_ARRAY_RW
:
5863 err
= restart_array(mddev
);
5867 err
= do_md_stop(mddev
, 0, 1);
5871 err
= md_set_readonly(mddev
, 1);
5875 if (get_user(ro
, (int __user
*)(arg
))) {
5881 /* if the bdev is going readonly the value of mddev->ro
5882 * does not matter, no writes are coming
5887 /* are we are already prepared for writes? */
5891 /* transitioning to readauto need only happen for
5892 * arrays that call md_write_start
5895 err
= restart_array(mddev
);
5898 set_disk_ro(mddev
->gendisk
, 0);
5905 * The remaining ioctls are changing the state of the
5906 * superblock, so we do not allow them on read-only arrays.
5907 * However non-MD ioctls (e.g. get-size) will still come through
5908 * here and hit the 'default' below, so only disallow
5909 * 'md' ioctls, and switch to rw mode if started auto-readonly.
5911 if (_IOC_TYPE(cmd
) == MD_MAJOR
&& mddev
->ro
&& mddev
->pers
) {
5912 if (mddev
->ro
== 2) {
5914 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5915 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5916 md_wakeup_thread(mddev
->thread
);
5927 mdu_disk_info_t info
;
5928 if (copy_from_user(&info
, argp
, sizeof(info
)))
5931 err
= add_new_disk(mddev
, &info
);
5935 case HOT_REMOVE_DISK
:
5936 err
= hot_remove_disk(mddev
, new_decode_dev(arg
));
5940 err
= hot_add_disk(mddev
, new_decode_dev(arg
));
5943 case SET_DISK_FAULTY
:
5944 err
= set_disk_faulty(mddev
, new_decode_dev(arg
));
5948 err
= do_md_run(mddev
);
5951 case SET_BITMAP_FILE
:
5952 err
= set_bitmap_file(mddev
, (int)arg
);
5962 if (mddev
->hold_active
== UNTIL_IOCTL
&&
5964 mddev
->hold_active
= 0;
5965 mddev_unlock(mddev
);
5974 #ifdef CONFIG_COMPAT
5975 static int md_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
5976 unsigned int cmd
, unsigned long arg
)
5979 case HOT_REMOVE_DISK
:
5981 case SET_DISK_FAULTY
:
5982 case SET_BITMAP_FILE
:
5983 /* These take in integer arg, do not convert */
5986 arg
= (unsigned long)compat_ptr(arg
);
5990 return md_ioctl(bdev
, mode
, cmd
, arg
);
5992 #endif /* CONFIG_COMPAT */
5994 static int md_open(struct block_device
*bdev
, fmode_t mode
)
5997 * Succeed if we can lock the mddev, which confirms that
5998 * it isn't being stopped right now.
6000 mddev_t
*mddev
= mddev_find(bdev
->bd_dev
);
6003 if (mddev
->gendisk
!= bdev
->bd_disk
) {
6004 /* we are racing with mddev_put which is discarding this
6008 /* Wait until bdev->bd_disk is definitely gone */
6009 flush_workqueue(md_misc_wq
);
6010 /* Then retry the open from the top */
6011 return -ERESTARTSYS
;
6013 BUG_ON(mddev
!= bdev
->bd_disk
->private_data
);
6015 if ((err
= mutex_lock_interruptible(&mddev
->open_mutex
)))
6019 atomic_inc(&mddev
->openers
);
6020 mutex_unlock(&mddev
->open_mutex
);
6022 check_disk_change(bdev
);
6027 static int md_release(struct gendisk
*disk
, fmode_t mode
)
6029 mddev_t
*mddev
= disk
->private_data
;
6032 atomic_dec(&mddev
->openers
);
6038 static int md_media_changed(struct gendisk
*disk
)
6040 mddev_t
*mddev
= disk
->private_data
;
6042 return mddev
->changed
;
6045 static int md_revalidate(struct gendisk
*disk
)
6047 mddev_t
*mddev
= disk
->private_data
;
6052 static const struct block_device_operations md_fops
=
6054 .owner
= THIS_MODULE
,
6056 .release
= md_release
,
6058 #ifdef CONFIG_COMPAT
6059 .compat_ioctl
= md_compat_ioctl
,
6061 .getgeo
= md_getgeo
,
6062 .media_changed
= md_media_changed
,
6063 .revalidate_disk
= md_revalidate
,
6066 static int md_thread(void * arg
)
6068 mdk_thread_t
*thread
= arg
;
6071 * md_thread is a 'system-thread', it's priority should be very
6072 * high. We avoid resource deadlocks individually in each
6073 * raid personality. (RAID5 does preallocation) We also use RR and
6074 * the very same RT priority as kswapd, thus we will never get
6075 * into a priority inversion deadlock.
6077 * we definitely have to have equal or higher priority than
6078 * bdflush, otherwise bdflush will deadlock if there are too
6079 * many dirty RAID5 blocks.
6082 allow_signal(SIGKILL
);
6083 while (!kthread_should_stop()) {
6085 /* We need to wait INTERRUPTIBLE so that
6086 * we don't add to the load-average.
6087 * That means we need to be sure no signals are
6090 if (signal_pending(current
))
6091 flush_signals(current
);
6093 wait_event_interruptible_timeout
6095 test_bit(THREAD_WAKEUP
, &thread
->flags
)
6096 || kthread_should_stop(),
6099 clear_bit(THREAD_WAKEUP
, &thread
->flags
);
6100 if (!kthread_should_stop())
6101 thread
->run(thread
->mddev
);
6107 void md_wakeup_thread(mdk_thread_t
*thread
)
6110 dprintk("md: waking up MD thread %s.\n", thread
->tsk
->comm
);
6111 set_bit(THREAD_WAKEUP
, &thread
->flags
);
6112 wake_up(&thread
->wqueue
);
6116 mdk_thread_t
*md_register_thread(void (*run
) (mddev_t
*), mddev_t
*mddev
,
6119 mdk_thread_t
*thread
;
6121 thread
= kzalloc(sizeof(mdk_thread_t
), GFP_KERNEL
);
6125 init_waitqueue_head(&thread
->wqueue
);
6128 thread
->mddev
= mddev
;
6129 thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
6130 thread
->tsk
= kthread_run(md_thread
, thread
,
6132 mdname(thread
->mddev
),
6133 name
?: mddev
->pers
->name
);
6134 if (IS_ERR(thread
->tsk
)) {
6141 void md_unregister_thread(mdk_thread_t
*thread
)
6145 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread
->tsk
));
6147 kthread_stop(thread
->tsk
);
6151 void md_error(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
6158 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
6161 if (mddev
->external
)
6162 set_bit(Blocked
, &rdev
->flags
);
6164 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
6166 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
6167 __builtin_return_address(0),__builtin_return_address(1),
6168 __builtin_return_address(2),__builtin_return_address(3));
6172 if (!mddev
->pers
->error_handler
)
6174 mddev
->pers
->error_handler(mddev
,rdev
);
6175 if (mddev
->degraded
)
6176 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
6177 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
6178 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6179 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6180 md_wakeup_thread(mddev
->thread
);
6181 if (mddev
->event_work
.func
)
6182 queue_work(md_misc_wq
, &mddev
->event_work
);
6183 md_new_event_inintr(mddev
);
6186 /* seq_file implementation /proc/mdstat */
6188 static void status_unused(struct seq_file
*seq
)
6193 seq_printf(seq
, "unused devices: ");
6195 list_for_each_entry(rdev
, &pending_raid_disks
, same_set
) {
6196 char b
[BDEVNAME_SIZE
];
6198 seq_printf(seq
, "%s ",
6199 bdevname(rdev
->bdev
,b
));
6202 seq_printf(seq
, "<none>");
6204 seq_printf(seq
, "\n");
6208 static void status_resync(struct seq_file
*seq
, mddev_t
* mddev
)
6210 sector_t max_sectors
, resync
, res
;
6211 unsigned long dt
, db
;
6214 unsigned int per_milli
;
6216 resync
= mddev
->curr_resync
- atomic_read(&mddev
->recovery_active
);
6218 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
6219 max_sectors
= mddev
->resync_max_sectors
;
6221 max_sectors
= mddev
->dev_sectors
;
6224 * Should not happen.
6230 /* Pick 'scale' such that (resync>>scale)*1000 will fit
6231 * in a sector_t, and (max_sectors>>scale) will fit in a
6232 * u32, as those are the requirements for sector_div.
6233 * Thus 'scale' must be at least 10
6236 if (sizeof(sector_t
) > sizeof(unsigned long)) {
6237 while ( max_sectors
/2 > (1ULL<<(scale
+32)))
6240 res
= (resync
>>scale
)*1000;
6241 sector_div(res
, (u32
)((max_sectors
>>scale
)+1));
6245 int i
, x
= per_milli
/50, y
= 20-x
;
6246 seq_printf(seq
, "[");
6247 for (i
= 0; i
< x
; i
++)
6248 seq_printf(seq
, "=");
6249 seq_printf(seq
, ">");
6250 for (i
= 0; i
< y
; i
++)
6251 seq_printf(seq
, ".");
6252 seq_printf(seq
, "] ");
6254 seq_printf(seq
, " %s =%3u.%u%% (%llu/%llu)",
6255 (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)?
6257 (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)?
6259 (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ?
6260 "resync" : "recovery"))),
6261 per_milli
/10, per_milli
% 10,
6262 (unsigned long long) resync
/2,
6263 (unsigned long long) max_sectors
/2);
6266 * dt: time from mark until now
6267 * db: blocks written from mark until now
6268 * rt: remaining time
6270 * rt is a sector_t, so could be 32bit or 64bit.
6271 * So we divide before multiply in case it is 32bit and close
6273 * We scale the divisor (db) by 32 to avoid loosing precision
6274 * near the end of resync when the number of remaining sectors
6276 * We then divide rt by 32 after multiplying by db to compensate.
6277 * The '+1' avoids division by zero if db is very small.
6279 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
6281 db
= (mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
))
6282 - mddev
->resync_mark_cnt
;
6284 rt
= max_sectors
- resync
; /* number of remaining sectors */
6285 sector_div(rt
, db
/32+1);
6289 seq_printf(seq
, " finish=%lu.%lumin", (unsigned long)rt
/ 60,
6290 ((unsigned long)rt
% 60)/6);
6292 seq_printf(seq
, " speed=%ldK/sec", db
/2/dt
);
6295 static void *md_seq_start(struct seq_file
*seq
, loff_t
*pos
)
6297 struct list_head
*tmp
;
6307 spin_lock(&all_mddevs_lock
);
6308 list_for_each(tmp
,&all_mddevs
)
6310 mddev
= list_entry(tmp
, mddev_t
, all_mddevs
);
6312 spin_unlock(&all_mddevs_lock
);
6315 spin_unlock(&all_mddevs_lock
);
6317 return (void*)2;/* tail */
6321 static void *md_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
6323 struct list_head
*tmp
;
6324 mddev_t
*next_mddev
, *mddev
= v
;
6330 spin_lock(&all_mddevs_lock
);
6332 tmp
= all_mddevs
.next
;
6334 tmp
= mddev
->all_mddevs
.next
;
6335 if (tmp
!= &all_mddevs
)
6336 next_mddev
= mddev_get(list_entry(tmp
,mddev_t
,all_mddevs
));
6338 next_mddev
= (void*)2;
6341 spin_unlock(&all_mddevs_lock
);
6349 static void md_seq_stop(struct seq_file
*seq
, void *v
)
6353 if (mddev
&& v
!= (void*)1 && v
!= (void*)2)
6357 struct mdstat_info
{
6361 static int md_seq_show(struct seq_file
*seq
, void *v
)
6366 struct mdstat_info
*mi
= seq
->private;
6367 struct bitmap
*bitmap
;
6369 if (v
== (void*)1) {
6370 struct mdk_personality
*pers
;
6371 seq_printf(seq
, "Personalities : ");
6372 spin_lock(&pers_lock
);
6373 list_for_each_entry(pers
, &pers_list
, list
)
6374 seq_printf(seq
, "[%s] ", pers
->name
);
6376 spin_unlock(&pers_lock
);
6377 seq_printf(seq
, "\n");
6378 mi
->event
= atomic_read(&md_event_count
);
6381 if (v
== (void*)2) {
6386 if (mddev_lock(mddev
) < 0)
6389 if (mddev
->pers
|| mddev
->raid_disks
|| !list_empty(&mddev
->disks
)) {
6390 seq_printf(seq
, "%s : %sactive", mdname(mddev
),
6391 mddev
->pers
? "" : "in");
6394 seq_printf(seq
, " (read-only)");
6396 seq_printf(seq
, " (auto-read-only)");
6397 seq_printf(seq
, " %s", mddev
->pers
->name
);
6401 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
6402 char b
[BDEVNAME_SIZE
];
6403 seq_printf(seq
, " %s[%d]",
6404 bdevname(rdev
->bdev
,b
), rdev
->desc_nr
);
6405 if (test_bit(WriteMostly
, &rdev
->flags
))
6406 seq_printf(seq
, "(W)");
6407 if (test_bit(Faulty
, &rdev
->flags
)) {
6408 seq_printf(seq
, "(F)");
6410 } else if (rdev
->raid_disk
< 0)
6411 seq_printf(seq
, "(S)"); /* spare */
6412 sectors
+= rdev
->sectors
;
6415 if (!list_empty(&mddev
->disks
)) {
6417 seq_printf(seq
, "\n %llu blocks",
6418 (unsigned long long)
6419 mddev
->array_sectors
/ 2);
6421 seq_printf(seq
, "\n %llu blocks",
6422 (unsigned long long)sectors
/ 2);
6424 if (mddev
->persistent
) {
6425 if (mddev
->major_version
!= 0 ||
6426 mddev
->minor_version
!= 90) {
6427 seq_printf(seq
," super %d.%d",
6428 mddev
->major_version
,
6429 mddev
->minor_version
);
6431 } else if (mddev
->external
)
6432 seq_printf(seq
, " super external:%s",
6433 mddev
->metadata_type
);
6435 seq_printf(seq
, " super non-persistent");
6438 mddev
->pers
->status(seq
, mddev
);
6439 seq_printf(seq
, "\n ");
6440 if (mddev
->pers
->sync_request
) {
6441 if (mddev
->curr_resync
> 2) {
6442 status_resync(seq
, mddev
);
6443 seq_printf(seq
, "\n ");
6444 } else if (mddev
->curr_resync
== 1 || mddev
->curr_resync
== 2)
6445 seq_printf(seq
, "\tresync=DELAYED\n ");
6446 else if (mddev
->recovery_cp
< MaxSector
)
6447 seq_printf(seq
, "\tresync=PENDING\n ");
6450 seq_printf(seq
, "\n ");
6452 if ((bitmap
= mddev
->bitmap
)) {
6453 unsigned long chunk_kb
;
6454 unsigned long flags
;
6455 spin_lock_irqsave(&bitmap
->lock
, flags
);
6456 chunk_kb
= mddev
->bitmap_info
.chunksize
>> 10;
6457 seq_printf(seq
, "bitmap: %lu/%lu pages [%luKB], "
6459 bitmap
->pages
- bitmap
->missing_pages
,
6461 (bitmap
->pages
- bitmap
->missing_pages
)
6462 << (PAGE_SHIFT
- 10),
6463 chunk_kb
? chunk_kb
: mddev
->bitmap_info
.chunksize
,
6464 chunk_kb
? "KB" : "B");
6466 seq_printf(seq
, ", file: ");
6467 seq_path(seq
, &bitmap
->file
->f_path
, " \t\n");
6470 seq_printf(seq
, "\n");
6471 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
6474 seq_printf(seq
, "\n");
6476 mddev_unlock(mddev
);
6481 static const struct seq_operations md_seq_ops
= {
6482 .start
= md_seq_start
,
6483 .next
= md_seq_next
,
6484 .stop
= md_seq_stop
,
6485 .show
= md_seq_show
,
6488 static int md_seq_open(struct inode
*inode
, struct file
*file
)
6491 struct mdstat_info
*mi
= kmalloc(sizeof(*mi
), GFP_KERNEL
);
6495 error
= seq_open(file
, &md_seq_ops
);
6499 struct seq_file
*p
= file
->private_data
;
6501 mi
->event
= atomic_read(&md_event_count
);
6506 static unsigned int mdstat_poll(struct file
*filp
, poll_table
*wait
)
6508 struct seq_file
*m
= filp
->private_data
;
6509 struct mdstat_info
*mi
= m
->private;
6512 poll_wait(filp
, &md_event_waiters
, wait
);
6514 /* always allow read */
6515 mask
= POLLIN
| POLLRDNORM
;
6517 if (mi
->event
!= atomic_read(&md_event_count
))
6518 mask
|= POLLERR
| POLLPRI
;
6522 static const struct file_operations md_seq_fops
= {
6523 .owner
= THIS_MODULE
,
6524 .open
= md_seq_open
,
6526 .llseek
= seq_lseek
,
6527 .release
= seq_release_private
,
6528 .poll
= mdstat_poll
,
6531 int register_md_personality(struct mdk_personality
*p
)
6533 spin_lock(&pers_lock
);
6534 list_add_tail(&p
->list
, &pers_list
);
6535 printk(KERN_INFO
"md: %s personality registered for level %d\n", p
->name
, p
->level
);
6536 spin_unlock(&pers_lock
);
6540 int unregister_md_personality(struct mdk_personality
*p
)
6542 printk(KERN_INFO
"md: %s personality unregistered\n", p
->name
);
6543 spin_lock(&pers_lock
);
6544 list_del_init(&p
->list
);
6545 spin_unlock(&pers_lock
);
6549 static int is_mddev_idle(mddev_t
*mddev
, int init
)
6557 rdev_for_each_rcu(rdev
, mddev
) {
6558 struct gendisk
*disk
= rdev
->bdev
->bd_contains
->bd_disk
;
6559 curr_events
= (int)part_stat_read(&disk
->part0
, sectors
[0]) +
6560 (int)part_stat_read(&disk
->part0
, sectors
[1]) -
6561 atomic_read(&disk
->sync_io
);
6562 /* sync IO will cause sync_io to increase before the disk_stats
6563 * as sync_io is counted when a request starts, and
6564 * disk_stats is counted when it completes.
6565 * So resync activity will cause curr_events to be smaller than
6566 * when there was no such activity.
6567 * non-sync IO will cause disk_stat to increase without
6568 * increasing sync_io so curr_events will (eventually)
6569 * be larger than it was before. Once it becomes
6570 * substantially larger, the test below will cause
6571 * the array to appear non-idle, and resync will slow
6573 * If there is a lot of outstanding resync activity when
6574 * we set last_event to curr_events, then all that activity
6575 * completing might cause the array to appear non-idle
6576 * and resync will be slowed down even though there might
6577 * not have been non-resync activity. This will only
6578 * happen once though. 'last_events' will soon reflect
6579 * the state where there is little or no outstanding
6580 * resync requests, and further resync activity will
6581 * always make curr_events less than last_events.
6584 if (init
|| curr_events
- rdev
->last_events
> 64) {
6585 rdev
->last_events
= curr_events
;
6593 void md_done_sync(mddev_t
*mddev
, int blocks
, int ok
)
6595 /* another "blocks" (512byte) blocks have been synced */
6596 atomic_sub(blocks
, &mddev
->recovery_active
);
6597 wake_up(&mddev
->recovery_wait
);
6599 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6600 md_wakeup_thread(mddev
->thread
);
6601 // stop recovery, signal do_sync ....
6606 /* md_write_start(mddev, bi)
6607 * If we need to update some array metadata (e.g. 'active' flag
6608 * in superblock) before writing, schedule a superblock update
6609 * and wait for it to complete.
6611 void md_write_start(mddev_t
*mddev
, struct bio
*bi
)
6614 if (bio_data_dir(bi
) != WRITE
)
6617 BUG_ON(mddev
->ro
== 1);
6618 if (mddev
->ro
== 2) {
6619 /* need to switch to read/write */
6621 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6622 md_wakeup_thread(mddev
->thread
);
6623 md_wakeup_thread(mddev
->sync_thread
);
6626 atomic_inc(&mddev
->writes_pending
);
6627 if (mddev
->safemode
== 1)
6628 mddev
->safemode
= 0;
6629 if (mddev
->in_sync
) {
6630 spin_lock_irq(&mddev
->write_lock
);
6631 if (mddev
->in_sync
) {
6633 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6634 set_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
6635 md_wakeup_thread(mddev
->thread
);
6638 spin_unlock_irq(&mddev
->write_lock
);
6641 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6642 wait_event(mddev
->sb_wait
,
6643 !test_bit(MD_CHANGE_PENDING
, &mddev
->flags
));
6646 void md_write_end(mddev_t
*mddev
)
6648 if (atomic_dec_and_test(&mddev
->writes_pending
)) {
6649 if (mddev
->safemode
== 2)
6650 md_wakeup_thread(mddev
->thread
);
6651 else if (mddev
->safemode_delay
)
6652 mod_timer(&mddev
->safemode_timer
, jiffies
+ mddev
->safemode_delay
);
6656 /* md_allow_write(mddev)
6657 * Calling this ensures that the array is marked 'active' so that writes
6658 * may proceed without blocking. It is important to call this before
6659 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6660 * Must be called with mddev_lock held.
6662 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6663 * is dropped, so return -EAGAIN after notifying userspace.
6665 int md_allow_write(mddev_t
*mddev
)
6671 if (!mddev
->pers
->sync_request
)
6674 spin_lock_irq(&mddev
->write_lock
);
6675 if (mddev
->in_sync
) {
6677 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6678 set_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
6679 if (mddev
->safemode_delay
&&
6680 mddev
->safemode
== 0)
6681 mddev
->safemode
= 1;
6682 spin_unlock_irq(&mddev
->write_lock
);
6683 md_update_sb(mddev
, 0);
6684 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6686 spin_unlock_irq(&mddev
->write_lock
);
6688 if (test_bit(MD_CHANGE_PENDING
, &mddev
->flags
))
6693 EXPORT_SYMBOL_GPL(md_allow_write
);
6695 void md_unplug(mddev_t
*mddev
)
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 */
6879 wait_event(mddev
->recovery_wait
,
6880 atomic_read(&mddev
->recovery_active
) == 0);
6881 mddev
->curr_resync_completed
= j
;
6882 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6883 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
6886 while (j
>= mddev
->resync_max
&& !kthread_should_stop()) {
6887 /* As this condition is controlled by user-space,
6888 * we can block indefinitely, so use '_interruptible'
6889 * to avoid triggering warnings.
6891 flush_signals(current
); /* just in case */
6892 wait_event_interruptible(mddev
->recovery_wait
,
6893 mddev
->resync_max
> j
6894 || kthread_should_stop());
6897 if (kthread_should_stop())
6900 sectors
= mddev
->pers
->sync_request(mddev
, j
, &skipped
,
6901 currspeed
< speed_min(mddev
));
6903 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6907 if (!skipped
) { /* actual IO requested */
6908 io_sectors
+= sectors
;
6909 atomic_add(sectors
, &mddev
->recovery_active
);
6913 if (j
>1) mddev
->curr_resync
= j
;
6914 mddev
->curr_mark_cnt
= io_sectors
;
6915 if (last_check
== 0)
6916 /* this is the earliers that rebuilt will be
6917 * visible in /proc/mdstat
6919 md_new_event(mddev
);
6921 if (last_check
+ window
> io_sectors
|| j
== max_sectors
)
6924 last_check
= io_sectors
;
6926 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
6930 if (time_after_eq(jiffies
, mark
[last_mark
] + SYNC_MARK_STEP
)) {
6932 int next
= (last_mark
+1) % SYNC_MARKS
;
6934 mddev
->resync_mark
= mark
[next
];
6935 mddev
->resync_mark_cnt
= mark_cnt
[next
];
6936 mark
[next
] = jiffies
;
6937 mark_cnt
[next
] = io_sectors
- atomic_read(&mddev
->recovery_active
);
6942 if (kthread_should_stop())
6947 * this loop exits only if either when we are slower than
6948 * the 'hard' speed limit, or the system was IO-idle for
6950 * the system might be non-idle CPU-wise, but we only care
6951 * about not overloading the IO subsystem. (things like an
6952 * e2fsck being done on the RAID array should execute fast)
6956 currspeed
= ((unsigned long)(io_sectors
-mddev
->resync_mark_cnt
))/2
6957 /((jiffies
-mddev
->resync_mark
)/HZ
+1) +1;
6959 if (currspeed
> speed_min(mddev
)) {
6960 if ((currspeed
> speed_max(mddev
)) ||
6961 !is_mddev_idle(mddev
, 0)) {
6967 printk(KERN_INFO
"md: %s: %s done.\n",mdname(mddev
), desc
);
6969 * this also signals 'finished resyncing' to md_stop
6972 wait_event(mddev
->recovery_wait
, !atomic_read(&mddev
->recovery_active
));
6974 /* tell personality that we are finished */
6975 mddev
->pers
->sync_request(mddev
, max_sectors
, &skipped
, 1);
6977 if (!test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
) &&
6978 mddev
->curr_resync
> 2) {
6979 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
6980 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
6981 if (mddev
->curr_resync
>= mddev
->recovery_cp
) {
6983 "md: checkpointing %s of %s.\n",
6984 desc
, mdname(mddev
));
6985 mddev
->recovery_cp
= mddev
->curr_resync
;
6988 mddev
->recovery_cp
= MaxSector
;
6990 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
6991 mddev
->curr_resync
= MaxSector
;
6993 list_for_each_entry_rcu(rdev
, &mddev
->disks
, same_set
)
6994 if (rdev
->raid_disk
>= 0 &&
6995 mddev
->delta_disks
>= 0 &&
6996 !test_bit(Faulty
, &rdev
->flags
) &&
6997 !test_bit(In_sync
, &rdev
->flags
) &&
6998 rdev
->recovery_offset
< mddev
->curr_resync
)
6999 rdev
->recovery_offset
= mddev
->curr_resync
;
7003 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
7006 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
7007 /* We completed so min/max setting can be forgotten if used. */
7008 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
7009 mddev
->resync_min
= 0;
7010 mddev
->resync_max
= MaxSector
;
7011 } else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
7012 mddev
->resync_min
= mddev
->curr_resync_completed
;
7013 mddev
->curr_resync
= 0;
7014 wake_up(&resync_wait
);
7015 set_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
7016 md_wakeup_thread(mddev
->thread
);
7021 * got a signal, exit.
7024 "md: md_do_sync() got signal ... exiting\n");
7025 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7029 EXPORT_SYMBOL_GPL(md_do_sync
);
7032 static int remove_and_add_spares(mddev_t
*mddev
)
7037 mddev
->curr_resync_completed
= 0;
7039 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
7040 if (rdev
->raid_disk
>= 0 &&
7041 !test_bit(Blocked
, &rdev
->flags
) &&
7042 (test_bit(Faulty
, &rdev
->flags
) ||
7043 ! test_bit(In_sync
, &rdev
->flags
)) &&
7044 atomic_read(&rdev
->nr_pending
)==0) {
7045 if (mddev
->pers
->hot_remove_disk(
7046 mddev
, rdev
->raid_disk
)==0) {
7048 sprintf(nm
,"rd%d", rdev
->raid_disk
);
7049 sysfs_remove_link(&mddev
->kobj
, nm
);
7050 rdev
->raid_disk
= -1;
7054 if (mddev
->degraded
&& !mddev
->recovery_disabled
) {
7055 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
7056 if (rdev
->raid_disk
>= 0 &&
7057 !test_bit(In_sync
, &rdev
->flags
) &&
7058 !test_bit(Blocked
, &rdev
->flags
))
7060 if (rdev
->raid_disk
< 0
7061 && !test_bit(Faulty
, &rdev
->flags
)) {
7062 rdev
->recovery_offset
= 0;
7064 hot_add_disk(mddev
, rdev
) == 0) {
7066 sprintf(nm
, "rd%d", rdev
->raid_disk
);
7067 if (sysfs_create_link(&mddev
->kobj
,
7069 /* failure here is OK */;
7071 md_new_event(mddev
);
7072 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
7081 static void reap_sync_thread(mddev_t
*mddev
)
7085 /* resync has finished, collect result */
7086 md_unregister_thread(mddev
->sync_thread
);
7087 mddev
->sync_thread
= NULL
;
7088 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
7089 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
7091 /* activate any spares */
7092 if (mddev
->pers
->spare_active(mddev
))
7093 sysfs_notify(&mddev
->kobj
, NULL
,
7096 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
7097 mddev
->pers
->finish_reshape
)
7098 mddev
->pers
->finish_reshape(mddev
);
7099 md_update_sb(mddev
, 1);
7101 /* if array is no-longer degraded, then any saved_raid_disk
7102 * information must be scrapped
7104 if (!mddev
->degraded
)
7105 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
7106 rdev
->saved_raid_disk
= -1;
7108 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7109 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7110 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7111 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
7112 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7113 /* flag recovery needed just to double check */
7114 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7115 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7116 md_new_event(mddev
);
7120 * This routine is regularly called by all per-raid-array threads to
7121 * deal with generic issues like resync and super-block update.
7122 * Raid personalities that don't have a thread (linear/raid0) do not
7123 * need this as they never do any recovery or update the superblock.
7125 * It does not do any resync itself, but rather "forks" off other threads
7126 * to do that as needed.
7127 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7128 * "->recovery" and create a thread at ->sync_thread.
7129 * When the thread finishes it sets MD_RECOVERY_DONE
7130 * and wakeups up this thread which will reap the thread and finish up.
7131 * This thread also removes any faulty devices (with nr_pending == 0).
7133 * The overall approach is:
7134 * 1/ if the superblock needs updating, update it.
7135 * 2/ If a recovery thread is running, don't do anything else.
7136 * 3/ If recovery has finished, clean up, possibly marking spares active.
7137 * 4/ If there are any faulty devices, remove them.
7138 * 5/ If array is degraded, try to add spares devices
7139 * 6/ If array has spares or is not in-sync, start a resync thread.
7141 void md_check_recovery(mddev_t
*mddev
)
7144 bitmap_daemon_work(mddev
);
7149 if (signal_pending(current
)) {
7150 if (mddev
->pers
->sync_request
&& !mddev
->external
) {
7151 printk(KERN_INFO
"md: %s in immediate safe mode\n",
7153 mddev
->safemode
= 2;
7155 flush_signals(current
);
7158 if (mddev
->ro
&& !test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
7161 (mddev
->flags
& ~ (1<<MD_CHANGE_PENDING
)) ||
7162 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
7163 test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
7164 (mddev
->external
== 0 && mddev
->safemode
== 1) ||
7165 (mddev
->safemode
== 2 && ! atomic_read(&mddev
->writes_pending
)
7166 && !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
7170 if (mddev_trylock(mddev
)) {
7174 /* Only thing we do on a ro array is remove
7178 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
7179 if (rdev
->raid_disk
>= 0 &&
7180 !test_bit(Blocked
, &rdev
->flags
) &&
7181 test_bit(Faulty
, &rdev
->flags
) &&
7182 atomic_read(&rdev
->nr_pending
)==0) {
7183 if (mddev
->pers
->hot_remove_disk(
7184 mddev
, rdev
->raid_disk
)==0) {
7186 sprintf(nm
,"rd%d", rdev
->raid_disk
);
7187 sysfs_remove_link(&mddev
->kobj
, nm
);
7188 rdev
->raid_disk
= -1;
7191 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7195 if (!mddev
->external
) {
7197 spin_lock_irq(&mddev
->write_lock
);
7198 if (mddev
->safemode
&&
7199 !atomic_read(&mddev
->writes_pending
) &&
7201 mddev
->recovery_cp
== MaxSector
) {
7204 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
7206 if (mddev
->safemode
== 1)
7207 mddev
->safemode
= 0;
7208 spin_unlock_irq(&mddev
->write_lock
);
7210 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
7214 md_update_sb(mddev
, 0);
7216 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
7217 !test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
)) {
7218 /* resync/recovery still happening */
7219 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7222 if (mddev
->sync_thread
) {
7223 reap_sync_thread(mddev
);
7226 /* Set RUNNING before clearing NEEDED to avoid
7227 * any transients in the value of "sync_action".
7229 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7230 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7231 /* Clear some bits that don't mean anything, but
7234 clear_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7235 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
7237 if (test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
7239 /* no recovery is running.
7240 * remove any failed drives, then
7241 * add spares if possible.
7242 * Spare are also removed and re-added, to allow
7243 * the personality to fail the re-add.
7246 if (mddev
->reshape_position
!= MaxSector
) {
7247 if (mddev
->pers
->check_reshape
== NULL
||
7248 mddev
->pers
->check_reshape(mddev
) != 0)
7249 /* Cannot proceed */
7251 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7252 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7253 } else if ((spares
= remove_and_add_spares(mddev
))) {
7254 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7255 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7256 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
7257 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7258 } else if (mddev
->recovery_cp
< MaxSector
) {
7259 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7260 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7261 } else if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
7262 /* nothing to be done ... */
7265 if (mddev
->pers
->sync_request
) {
7266 if (spares
&& mddev
->bitmap
&& ! mddev
->bitmap
->file
) {
7267 /* We are adding a device or devices to an array
7268 * which has the bitmap stored on all devices.
7269 * So make sure all bitmap pages get written
7271 bitmap_write_all(mddev
->bitmap
);
7273 mddev
->sync_thread
= md_register_thread(md_do_sync
,
7276 if (!mddev
->sync_thread
) {
7277 printk(KERN_ERR
"%s: could not start resync"
7280 /* leave the spares where they are, it shouldn't hurt */
7281 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7282 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7283 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7284 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
7285 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7287 md_wakeup_thread(mddev
->sync_thread
);
7288 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7289 md_new_event(mddev
);
7292 if (!mddev
->sync_thread
) {
7293 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7294 if (test_and_clear_bit(MD_RECOVERY_RECOVER
,
7296 if (mddev
->sysfs_action
)
7297 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7299 mddev_unlock(mddev
);
7303 void md_wait_for_blocked_rdev(mdk_rdev_t
*rdev
, mddev_t
*mddev
)
7305 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
7306 wait_event_timeout(rdev
->blocked_wait
,
7307 !test_bit(Blocked
, &rdev
->flags
),
7308 msecs_to_jiffies(5000));
7309 rdev_dec_pending(rdev
, mddev
);
7311 EXPORT_SYMBOL(md_wait_for_blocked_rdev
);
7313 static int md_notify_reboot(struct notifier_block
*this,
7314 unsigned long code
, void *x
)
7316 struct list_head
*tmp
;
7319 if ((code
== SYS_DOWN
) || (code
== SYS_HALT
) || (code
== SYS_POWER_OFF
)) {
7321 printk(KERN_INFO
"md: stopping all md devices.\n");
7323 for_each_mddev(mddev
, tmp
)
7324 if (mddev_trylock(mddev
)) {
7325 /* Force a switch to readonly even array
7326 * appears to still be in use. Hence
7329 md_set_readonly(mddev
, 100);
7330 mddev_unlock(mddev
);
7333 * certain more exotic SCSI devices are known to be
7334 * volatile wrt too early system reboots. While the
7335 * right place to handle this issue is the given
7336 * driver, we do want to have a safe RAID driver ...
7343 static struct notifier_block md_notifier
= {
7344 .notifier_call
= md_notify_reboot
,
7346 .priority
= INT_MAX
, /* before any real devices */
7349 static void md_geninit(void)
7351 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t
));
7353 proc_create("mdstat", S_IRUGO
, NULL
, &md_seq_fops
);
7356 static int __init
md_init(void)
7360 md_wq
= alloc_workqueue("md", WQ_MEM_RECLAIM
, 0);
7364 md_misc_wq
= alloc_workqueue("md_misc", 0, 0);
7368 if ((ret
= register_blkdev(MD_MAJOR
, "md")) < 0)
7371 if ((ret
= register_blkdev(0, "mdp")) < 0)
7375 blk_register_region(MKDEV(MD_MAJOR
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
7376 md_probe
, NULL
, NULL
);
7377 blk_register_region(MKDEV(mdp_major
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
7378 md_probe
, NULL
, NULL
);
7380 register_reboot_notifier(&md_notifier
);
7381 raid_table_header
= register_sysctl_table(raid_root_table
);
7387 unregister_blkdev(MD_MAJOR
, "md");
7389 destroy_workqueue(md_misc_wq
);
7391 destroy_workqueue(md_wq
);
7399 * Searches all registered partitions for autorun RAID arrays
7403 static LIST_HEAD(all_detected_devices
);
7404 struct detected_devices_node
{
7405 struct list_head list
;
7409 void md_autodetect_dev(dev_t dev
)
7411 struct detected_devices_node
*node_detected_dev
;
7413 node_detected_dev
= kzalloc(sizeof(*node_detected_dev
), GFP_KERNEL
);
7414 if (node_detected_dev
) {
7415 node_detected_dev
->dev
= dev
;
7416 list_add_tail(&node_detected_dev
->list
, &all_detected_devices
);
7418 printk(KERN_CRIT
"md: md_autodetect_dev: kzalloc failed"
7419 ", skipping dev(%d,%d)\n", MAJOR(dev
), MINOR(dev
));
7424 static void autostart_arrays(int part
)
7427 struct detected_devices_node
*node_detected_dev
;
7429 int i_scanned
, i_passed
;
7434 printk(KERN_INFO
"md: Autodetecting RAID arrays.\n");
7436 while (!list_empty(&all_detected_devices
) && i_scanned
< INT_MAX
) {
7438 node_detected_dev
= list_entry(all_detected_devices
.next
,
7439 struct detected_devices_node
, list
);
7440 list_del(&node_detected_dev
->list
);
7441 dev
= node_detected_dev
->dev
;
7442 kfree(node_detected_dev
);
7443 rdev
= md_import_device(dev
,0, 90);
7447 if (test_bit(Faulty
, &rdev
->flags
)) {
7451 set_bit(AutoDetected
, &rdev
->flags
);
7452 list_add(&rdev
->same_set
, &pending_raid_disks
);
7456 printk(KERN_INFO
"md: Scanned %d and added %d devices.\n",
7457 i_scanned
, i_passed
);
7459 autorun_devices(part
);
7462 #endif /* !MODULE */
7464 static __exit
void md_exit(void)
7467 struct list_head
*tmp
;
7469 blk_unregister_region(MKDEV(MD_MAJOR
,0), 1U << MINORBITS
);
7470 blk_unregister_region(MKDEV(mdp_major
,0), 1U << MINORBITS
);
7472 unregister_blkdev(MD_MAJOR
,"md");
7473 unregister_blkdev(mdp_major
, "mdp");
7474 unregister_reboot_notifier(&md_notifier
);
7475 unregister_sysctl_table(raid_table_header
);
7476 remove_proc_entry("mdstat", NULL
);
7477 for_each_mddev(mddev
, tmp
) {
7478 export_array(mddev
);
7479 mddev
->hold_active
= 0;
7481 destroy_workqueue(md_misc_wq
);
7482 destroy_workqueue(md_wq
);
7485 subsys_initcall(md_init
);
7486 module_exit(md_exit
)
7488 static int get_ro(char *buffer
, struct kernel_param
*kp
)
7490 return sprintf(buffer
, "%d", start_readonly
);
7492 static int set_ro(const char *val
, struct kernel_param
*kp
)
7495 int num
= simple_strtoul(val
, &e
, 10);
7496 if (*val
&& (*e
== '\0' || *e
== '\n')) {
7497 start_readonly
= num
;
7503 module_param_call(start_ro
, set_ro
, get_ro
, NULL
, S_IRUSR
|S_IWUSR
);
7504 module_param(start_dirty_degraded
, int, S_IRUGO
|S_IWUSR
);
7506 module_param_call(new_array
, add_named_array
, NULL
, NULL
, S_IWUSR
);
7508 EXPORT_SYMBOL(register_md_personality
);
7509 EXPORT_SYMBOL(unregister_md_personality
);
7510 EXPORT_SYMBOL(md_error
);
7511 EXPORT_SYMBOL(md_done_sync
);
7512 EXPORT_SYMBOL(md_write_start
);
7513 EXPORT_SYMBOL(md_write_end
);
7514 EXPORT_SYMBOL(md_register_thread
);
7515 EXPORT_SYMBOL(md_unregister_thread
);
7516 EXPORT_SYMBOL(md_wakeup_thread
);
7517 EXPORT_SYMBOL(md_check_recovery
);
7518 MODULE_LICENSE("GPL");
7519 MODULE_DESCRIPTION("MD RAID framework");
7521 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR
);