2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
5 completely rewritten, based on the MD driver code from Marc Zyngier
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
20 Neil Brown <neilb@cse.unsw.edu.au>.
22 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
25 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 #include <linux/kthread.h>
36 #include <linux/blkdev.h>
37 #include <linux/sysctl.h>
38 #include <linux/seq_file.h>
39 #include <linux/mutex.h>
40 #include <linux/buffer_head.h> /* for invalidate_bdev */
41 #include <linux/poll.h>
42 #include <linux/ctype.h>
43 #include <linux/string.h>
44 #include <linux/hdreg.h>
45 #include <linux/proc_fs.h>
46 #include <linux/random.h>
47 #include <linux/reboot.h>
48 #include <linux/file.h>
49 #include <linux/compat.h>
50 #include <linux/delay.h>
51 #include <linux/raid/md_p.h>
52 #include <linux/raid/md_u.h>
53 #include <linux/slab.h>
58 #define dprintk(x...) ((void)(DEBUG && printk(x)))
61 static void autostart_arrays(int part
);
64 static LIST_HEAD(pers_list
);
65 static DEFINE_SPINLOCK(pers_lock
);
67 static void md_print_devices(void);
69 static DECLARE_WAIT_QUEUE_HEAD(resync_wait
);
70 static struct workqueue_struct
*md_wq
;
71 static struct workqueue_struct
*md_misc_wq
;
73 #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
76 * Default number of read corrections we'll attempt on an rdev
77 * before ejecting it from the array. We divide the read error
78 * count by 2 for every hour elapsed between read errors.
80 #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
82 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
83 * is 1000 KB/sec, so the extra system load does not show up that much.
84 * Increase it if you want to have more _guaranteed_ speed. Note that
85 * the RAID driver will use the maximum available bandwidth if the IO
86 * subsystem is idle. There is also an 'absolute maximum' reconstruction
87 * speed limit - in case reconstruction slows down your system despite
90 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
91 * or /sys/block/mdX/md/sync_speed_{min,max}
94 static int sysctl_speed_limit_min
= 1000;
95 static int sysctl_speed_limit_max
= 200000;
96 static inline int speed_min(mddev_t
*mddev
)
98 return mddev
->sync_speed_min
?
99 mddev
->sync_speed_min
: sysctl_speed_limit_min
;
102 static inline int speed_max(mddev_t
*mddev
)
104 return mddev
->sync_speed_max
?
105 mddev
->sync_speed_max
: sysctl_speed_limit_max
;
108 static struct ctl_table_header
*raid_table_header
;
110 static ctl_table raid_table
[] = {
112 .procname
= "speed_limit_min",
113 .data
= &sysctl_speed_limit_min
,
114 .maxlen
= sizeof(int),
115 .mode
= S_IRUGO
|S_IWUSR
,
116 .proc_handler
= proc_dointvec
,
119 .procname
= "speed_limit_max",
120 .data
= &sysctl_speed_limit_max
,
121 .maxlen
= sizeof(int),
122 .mode
= S_IRUGO
|S_IWUSR
,
123 .proc_handler
= proc_dointvec
,
128 static ctl_table raid_dir_table
[] = {
132 .mode
= S_IRUGO
|S_IXUGO
,
138 static ctl_table raid_root_table
[] = {
143 .child
= raid_dir_table
,
148 static const struct block_device_operations md_fops
;
150 static int start_readonly
;
153 * like bio_clone, but with a local bio set
156 static void mddev_bio_destructor(struct bio
*bio
)
158 mddev_t
*mddev
, **mddevp
;
163 bio_free(bio
, mddev
->bio_set
);
166 struct bio
*bio_alloc_mddev(gfp_t gfp_mask
, int nr_iovecs
,
172 if (!mddev
|| !mddev
->bio_set
)
173 return bio_alloc(gfp_mask
, nr_iovecs
);
175 b
= bio_alloc_bioset(gfp_mask
, nr_iovecs
,
181 b
->bi_destructor
= mddev_bio_destructor
;
184 EXPORT_SYMBOL_GPL(bio_alloc_mddev
);
186 struct bio
*bio_clone_mddev(struct bio
*bio
, gfp_t gfp_mask
,
192 if (!mddev
|| !mddev
->bio_set
)
193 return bio_clone(bio
, gfp_mask
);
195 b
= bio_alloc_bioset(gfp_mask
, bio
->bi_max_vecs
,
201 b
->bi_destructor
= mddev_bio_destructor
;
203 if (bio_integrity(bio
)) {
206 ret
= bio_integrity_clone(b
, bio
, gfp_mask
, mddev
->bio_set
);
216 EXPORT_SYMBOL_GPL(bio_clone_mddev
);
219 * We have a system wide 'event count' that is incremented
220 * on any 'interesting' event, and readers of /proc/mdstat
221 * can use 'poll' or 'select' to find out when the event
225 * start array, stop array, error, add device, remove device,
226 * start build, activate spare
228 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters
);
229 static atomic_t md_event_count
;
230 void md_new_event(mddev_t
*mddev
)
232 atomic_inc(&md_event_count
);
233 wake_up(&md_event_waiters
);
235 EXPORT_SYMBOL_GPL(md_new_event
);
237 /* Alternate version that can be called from interrupts
238 * when calling sysfs_notify isn't needed.
240 static void md_new_event_inintr(mddev_t
*mddev
)
242 atomic_inc(&md_event_count
);
243 wake_up(&md_event_waiters
);
247 * Enables to iterate over all existing md arrays
248 * all_mddevs_lock protects this list.
250 static LIST_HEAD(all_mddevs
);
251 static DEFINE_SPINLOCK(all_mddevs_lock
);
255 * iterates through all used mddevs in the system.
256 * We take care to grab the all_mddevs_lock whenever navigating
257 * the list, and to always hold a refcount when unlocked.
258 * Any code which breaks out of this loop while own
259 * a reference to the current mddev and must mddev_put it.
261 #define for_each_mddev(mddev,tmp) \
263 for (({ spin_lock(&all_mddevs_lock); \
264 tmp = all_mddevs.next; \
266 ({ if (tmp != &all_mddevs) \
267 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
268 spin_unlock(&all_mddevs_lock); \
269 if (mddev) mddev_put(mddev); \
270 mddev = list_entry(tmp, mddev_t, all_mddevs); \
271 tmp != &all_mddevs;}); \
272 ({ spin_lock(&all_mddevs_lock); \
277 /* Rather than calling directly into the personality make_request function,
278 * IO requests come here first so that we can check if the device is
279 * being suspended pending a reconfiguration.
280 * We hold a refcount over the call to ->make_request. By the time that
281 * call has finished, the bio has been linked into some internal structure
282 * and so is visible to ->quiesce(), so we don't need the refcount any more.
284 static int md_make_request(struct request_queue
*q
, struct bio
*bio
)
286 const int rw
= bio_data_dir(bio
);
287 mddev_t
*mddev
= q
->queuedata
;
291 if (mddev
== NULL
|| mddev
->pers
== NULL
296 smp_rmb(); /* Ensure implications of 'active' are visible */
298 if (mddev
->suspended
) {
301 prepare_to_wait(&mddev
->sb_wait
, &__wait
,
302 TASK_UNINTERRUPTIBLE
);
303 if (!mddev
->suspended
)
309 finish_wait(&mddev
->sb_wait
, &__wait
);
311 atomic_inc(&mddev
->active_io
);
314 rv
= mddev
->pers
->make_request(mddev
, bio
);
316 cpu
= part_stat_lock();
317 part_stat_inc(cpu
, &mddev
->gendisk
->part0
, ios
[rw
]);
318 part_stat_add(cpu
, &mddev
->gendisk
->part0
, sectors
[rw
],
322 if (atomic_dec_and_test(&mddev
->active_io
) && mddev
->suspended
)
323 wake_up(&mddev
->sb_wait
);
328 /* mddev_suspend makes sure no new requests are submitted
329 * to the device, and that any requests that have been submitted
330 * are completely handled.
331 * Once ->stop is called and completes, the module will be completely
334 void mddev_suspend(mddev_t
*mddev
)
336 BUG_ON(mddev
->suspended
);
337 mddev
->suspended
= 1;
339 wait_event(mddev
->sb_wait
, atomic_read(&mddev
->active_io
) == 0);
340 mddev
->pers
->quiesce(mddev
, 1);
342 EXPORT_SYMBOL_GPL(mddev_suspend
);
344 void mddev_resume(mddev_t
*mddev
)
346 mddev
->suspended
= 0;
347 wake_up(&mddev
->sb_wait
);
348 mddev
->pers
->quiesce(mddev
, 0);
350 EXPORT_SYMBOL_GPL(mddev_resume
);
352 int mddev_congested(mddev_t
*mddev
, int bits
)
354 return mddev
->suspended
;
356 EXPORT_SYMBOL(mddev_congested
);
359 * Generic flush handling for md
362 static void md_end_flush(struct bio
*bio
, int err
)
364 mdk_rdev_t
*rdev
= bio
->bi_private
;
365 mddev_t
*mddev
= rdev
->mddev
;
367 rdev_dec_pending(rdev
, mddev
);
369 if (atomic_dec_and_test(&mddev
->flush_pending
)) {
370 /* The pre-request flush has finished */
371 queue_work(md_wq
, &mddev
->flush_work
);
376 static void md_submit_flush_data(struct work_struct
*ws
);
378 static void submit_flushes(struct work_struct
*ws
)
380 mddev_t
*mddev
= container_of(ws
, mddev_t
, flush_work
);
383 INIT_WORK(&mddev
->flush_work
, md_submit_flush_data
);
384 atomic_set(&mddev
->flush_pending
, 1);
386 list_for_each_entry_rcu(rdev
, &mddev
->disks
, same_set
)
387 if (rdev
->raid_disk
>= 0 &&
388 !test_bit(Faulty
, &rdev
->flags
)) {
389 /* Take two references, one is dropped
390 * when request finishes, one after
391 * we reclaim rcu_read_lock
394 atomic_inc(&rdev
->nr_pending
);
395 atomic_inc(&rdev
->nr_pending
);
397 bi
= bio_alloc_mddev(GFP_KERNEL
, 0, mddev
);
398 bi
->bi_end_io
= md_end_flush
;
399 bi
->bi_private
= rdev
;
400 bi
->bi_bdev
= rdev
->bdev
;
401 atomic_inc(&mddev
->flush_pending
);
402 submit_bio(WRITE_FLUSH
, bi
);
404 rdev_dec_pending(rdev
, mddev
);
407 if (atomic_dec_and_test(&mddev
->flush_pending
))
408 queue_work(md_wq
, &mddev
->flush_work
);
411 static void md_submit_flush_data(struct work_struct
*ws
)
413 mddev_t
*mddev
= container_of(ws
, mddev_t
, flush_work
);
414 struct bio
*bio
= mddev
->flush_bio
;
416 if (bio
->bi_size
== 0)
417 /* an empty barrier - all done */
420 bio
->bi_rw
&= ~REQ_FLUSH
;
421 if (mddev
->pers
->make_request(mddev
, bio
))
422 generic_make_request(bio
);
425 mddev
->flush_bio
= NULL
;
426 wake_up(&mddev
->sb_wait
);
429 void md_flush_request(mddev_t
*mddev
, struct bio
*bio
)
431 spin_lock_irq(&mddev
->write_lock
);
432 wait_event_lock_irq(mddev
->sb_wait
,
434 mddev
->write_lock
, /*nothing*/);
435 mddev
->flush_bio
= bio
;
436 spin_unlock_irq(&mddev
->write_lock
);
438 INIT_WORK(&mddev
->flush_work
, submit_flushes
);
439 queue_work(md_wq
, &mddev
->flush_work
);
441 EXPORT_SYMBOL(md_flush_request
);
443 /* Support for plugging.
444 * This mirrors the plugging support in request_queue, but does not
445 * require having a whole queue
447 static void plugger_work(struct work_struct
*work
)
449 struct plug_handle
*plug
=
450 container_of(work
, struct plug_handle
, unplug_work
);
451 plug
->unplug_fn(plug
);
453 static void plugger_timeout(unsigned long data
)
455 struct plug_handle
*plug
= (void *)data
;
456 kblockd_schedule_work(NULL
, &plug
->unplug_work
);
458 void plugger_init(struct plug_handle
*plug
,
459 void (*unplug_fn
)(struct plug_handle
*))
461 plug
->unplug_flag
= 0;
462 plug
->unplug_fn
= unplug_fn
;
463 init_timer(&plug
->unplug_timer
);
464 plug
->unplug_timer
.function
= plugger_timeout
;
465 plug
->unplug_timer
.data
= (unsigned long)plug
;
466 INIT_WORK(&plug
->unplug_work
, plugger_work
);
468 EXPORT_SYMBOL_GPL(plugger_init
);
470 void plugger_set_plug(struct plug_handle
*plug
)
472 if (!test_and_set_bit(PLUGGED_FLAG
, &plug
->unplug_flag
))
473 mod_timer(&plug
->unplug_timer
, jiffies
+ msecs_to_jiffies(3)+1);
475 EXPORT_SYMBOL_GPL(plugger_set_plug
);
477 int plugger_remove_plug(struct plug_handle
*plug
)
479 if (test_and_clear_bit(PLUGGED_FLAG
, &plug
->unplug_flag
)) {
480 del_timer(&plug
->unplug_timer
);
485 EXPORT_SYMBOL_GPL(plugger_remove_plug
);
488 static inline mddev_t
*mddev_get(mddev_t
*mddev
)
490 atomic_inc(&mddev
->active
);
494 static void mddev_delayed_delete(struct work_struct
*ws
);
496 static void mddev_put(mddev_t
*mddev
)
498 struct bio_set
*bs
= NULL
;
500 if (!atomic_dec_and_lock(&mddev
->active
, &all_mddevs_lock
))
502 if (!mddev
->raid_disks
&& list_empty(&mddev
->disks
) &&
503 mddev
->ctime
== 0 && !mddev
->hold_active
) {
504 /* Array is not configured at all, and not held active,
506 list_del(&mddev
->all_mddevs
);
508 mddev
->bio_set
= NULL
;
509 if (mddev
->gendisk
) {
510 /* We did a probe so need to clean up. Call
511 * queue_work inside the spinlock so that
512 * flush_workqueue() after mddev_find will
513 * succeed in waiting for the work to be done.
515 INIT_WORK(&mddev
->del_work
, mddev_delayed_delete
);
516 queue_work(md_misc_wq
, &mddev
->del_work
);
520 spin_unlock(&all_mddevs_lock
);
525 void mddev_init(mddev_t
*mddev
)
527 mutex_init(&mddev
->open_mutex
);
528 mutex_init(&mddev
->reconfig_mutex
);
529 mutex_init(&mddev
->bitmap_info
.mutex
);
530 INIT_LIST_HEAD(&mddev
->disks
);
531 INIT_LIST_HEAD(&mddev
->all_mddevs
);
532 init_timer(&mddev
->safemode_timer
);
533 atomic_set(&mddev
->active
, 1);
534 atomic_set(&mddev
->openers
, 0);
535 atomic_set(&mddev
->active_io
, 0);
536 spin_lock_init(&mddev
->write_lock
);
537 atomic_set(&mddev
->flush_pending
, 0);
538 init_waitqueue_head(&mddev
->sb_wait
);
539 init_waitqueue_head(&mddev
->recovery_wait
);
540 mddev
->reshape_position
= MaxSector
;
541 mddev
->resync_min
= 0;
542 mddev
->resync_max
= MaxSector
;
543 mddev
->level
= LEVEL_NONE
;
545 EXPORT_SYMBOL_GPL(mddev_init
);
547 static mddev_t
* mddev_find(dev_t unit
)
549 mddev_t
*mddev
, *new = NULL
;
552 spin_lock(&all_mddevs_lock
);
555 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
556 if (mddev
->unit
== unit
) {
558 spin_unlock(&all_mddevs_lock
);
564 list_add(&new->all_mddevs
, &all_mddevs
);
565 spin_unlock(&all_mddevs_lock
);
566 new->hold_active
= UNTIL_IOCTL
;
570 /* find an unused unit number */
571 static int next_minor
= 512;
572 int start
= next_minor
;
576 dev
= MKDEV(MD_MAJOR
, next_minor
);
578 if (next_minor
> MINORMASK
)
580 if (next_minor
== start
) {
581 /* Oh dear, all in use. */
582 spin_unlock(&all_mddevs_lock
);
588 list_for_each_entry(mddev
, &all_mddevs
, all_mddevs
)
589 if (mddev
->unit
== dev
) {
595 new->md_minor
= MINOR(dev
);
596 new->hold_active
= UNTIL_STOP
;
597 list_add(&new->all_mddevs
, &all_mddevs
);
598 spin_unlock(&all_mddevs_lock
);
601 spin_unlock(&all_mddevs_lock
);
603 new = kzalloc(sizeof(*new), GFP_KERNEL
);
608 if (MAJOR(unit
) == MD_MAJOR
)
609 new->md_minor
= MINOR(unit
);
611 new->md_minor
= MINOR(unit
) >> MdpMinorShift
;
618 static inline int mddev_lock(mddev_t
* mddev
)
620 return mutex_lock_interruptible(&mddev
->reconfig_mutex
);
623 static inline int mddev_is_locked(mddev_t
*mddev
)
625 return mutex_is_locked(&mddev
->reconfig_mutex
);
628 static inline int mddev_trylock(mddev_t
* mddev
)
630 return mutex_trylock(&mddev
->reconfig_mutex
);
633 static struct attribute_group md_redundancy_group
;
635 static void mddev_unlock(mddev_t
* mddev
)
637 if (mddev
->to_remove
) {
638 /* These cannot be removed under reconfig_mutex as
639 * an access to the files will try to take reconfig_mutex
640 * while holding the file unremovable, which leads to
642 * So hold set sysfs_active while the remove in happeing,
643 * and anything else which might set ->to_remove or my
644 * otherwise change the sysfs namespace will fail with
645 * -EBUSY if sysfs_active is still set.
646 * We set sysfs_active under reconfig_mutex and elsewhere
647 * test it under the same mutex to ensure its correct value
650 struct attribute_group
*to_remove
= mddev
->to_remove
;
651 mddev
->to_remove
= NULL
;
652 mddev
->sysfs_active
= 1;
653 mutex_unlock(&mddev
->reconfig_mutex
);
655 if (mddev
->kobj
.sd
) {
656 if (to_remove
!= &md_redundancy_group
)
657 sysfs_remove_group(&mddev
->kobj
, to_remove
);
658 if (mddev
->pers
== NULL
||
659 mddev
->pers
->sync_request
== NULL
) {
660 sysfs_remove_group(&mddev
->kobj
, &md_redundancy_group
);
661 if (mddev
->sysfs_action
)
662 sysfs_put(mddev
->sysfs_action
);
663 mddev
->sysfs_action
= NULL
;
666 mddev
->sysfs_active
= 0;
668 mutex_unlock(&mddev
->reconfig_mutex
);
670 md_wakeup_thread(mddev
->thread
);
673 static mdk_rdev_t
* find_rdev_nr(mddev_t
*mddev
, int nr
)
677 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
678 if (rdev
->desc_nr
== nr
)
684 static mdk_rdev_t
* find_rdev(mddev_t
* mddev
, dev_t dev
)
688 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
689 if (rdev
->bdev
->bd_dev
== dev
)
695 static struct mdk_personality
*find_pers(int level
, char *clevel
)
697 struct mdk_personality
*pers
;
698 list_for_each_entry(pers
, &pers_list
, list
) {
699 if (level
!= LEVEL_NONE
&& pers
->level
== level
)
701 if (strcmp(pers
->name
, clevel
)==0)
707 /* return the offset of the super block in 512byte sectors */
708 static inline sector_t
calc_dev_sboffset(mdk_rdev_t
*rdev
)
710 sector_t num_sectors
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
711 return MD_NEW_SIZE_SECTORS(num_sectors
);
714 static int alloc_disk_sb(mdk_rdev_t
* rdev
)
719 rdev
->sb_page
= alloc_page(GFP_KERNEL
);
720 if (!rdev
->sb_page
) {
721 printk(KERN_ALERT
"md: out of memory.\n");
728 static void free_disk_sb(mdk_rdev_t
* rdev
)
731 put_page(rdev
->sb_page
);
733 rdev
->sb_page
= NULL
;
740 static void super_written(struct bio
*bio
, int error
)
742 mdk_rdev_t
*rdev
= bio
->bi_private
;
743 mddev_t
*mddev
= rdev
->mddev
;
745 if (error
|| !test_bit(BIO_UPTODATE
, &bio
->bi_flags
)) {
746 printk("md: super_written gets error=%d, uptodate=%d\n",
747 error
, test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
748 WARN_ON(test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
749 md_error(mddev
, rdev
);
752 if (atomic_dec_and_test(&mddev
->pending_writes
))
753 wake_up(&mddev
->sb_wait
);
757 void md_super_write(mddev_t
*mddev
, mdk_rdev_t
*rdev
,
758 sector_t sector
, int size
, struct page
*page
)
760 /* write first size bytes of page to sector of rdev
761 * Increment mddev->pending_writes before returning
762 * and decrement it on completion, waking up sb_wait
763 * if zero is reached.
764 * If an error occurred, call md_error
766 struct bio
*bio
= bio_alloc_mddev(GFP_NOIO
, 1, mddev
);
768 bio
->bi_bdev
= rdev
->meta_bdev
? rdev
->meta_bdev
: rdev
->bdev
;
769 bio
->bi_sector
= sector
;
770 bio_add_page(bio
, page
, size
, 0);
771 bio
->bi_private
= rdev
;
772 bio
->bi_end_io
= super_written
;
774 atomic_inc(&mddev
->pending_writes
);
775 submit_bio(REQ_WRITE
| REQ_SYNC
| REQ_UNPLUG
| REQ_FLUSH
| REQ_FUA
,
779 void md_super_wait(mddev_t
*mddev
)
781 /* wait for all superblock writes that were scheduled to complete */
784 prepare_to_wait(&mddev
->sb_wait
, &wq
, TASK_UNINTERRUPTIBLE
);
785 if (atomic_read(&mddev
->pending_writes
)==0)
789 finish_wait(&mddev
->sb_wait
, &wq
);
792 static void bi_complete(struct bio
*bio
, int error
)
794 complete((struct completion
*)bio
->bi_private
);
797 int sync_page_io(mdk_rdev_t
*rdev
, sector_t sector
, int size
,
798 struct page
*page
, int rw
, bool metadata_op
)
800 struct bio
*bio
= bio_alloc_mddev(GFP_NOIO
, 1, rdev
->mddev
);
801 struct completion event
;
804 rw
|= REQ_SYNC
| REQ_UNPLUG
;
806 bio
->bi_bdev
= (metadata_op
&& rdev
->meta_bdev
) ?
807 rdev
->meta_bdev
: rdev
->bdev
;
809 bio
->bi_sector
= sector
+ rdev
->sb_start
;
811 bio
->bi_sector
= sector
+ rdev
->data_offset
;
812 bio_add_page(bio
, page
, size
, 0);
813 init_completion(&event
);
814 bio
->bi_private
= &event
;
815 bio
->bi_end_io
= bi_complete
;
817 wait_for_completion(&event
);
819 ret
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
823 EXPORT_SYMBOL_GPL(sync_page_io
);
825 static int read_disk_sb(mdk_rdev_t
* rdev
, int size
)
827 char b
[BDEVNAME_SIZE
];
828 if (!rdev
->sb_page
) {
836 if (!sync_page_io(rdev
, 0, size
, rdev
->sb_page
, READ
, true))
842 printk(KERN_WARNING
"md: disabled device %s, could not read superblock.\n",
843 bdevname(rdev
->bdev
,b
));
847 static int uuid_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
849 return sb1
->set_uuid0
== sb2
->set_uuid0
&&
850 sb1
->set_uuid1
== sb2
->set_uuid1
&&
851 sb1
->set_uuid2
== sb2
->set_uuid2
&&
852 sb1
->set_uuid3
== sb2
->set_uuid3
;
855 static int sb_equal(mdp_super_t
*sb1
, mdp_super_t
*sb2
)
858 mdp_super_t
*tmp1
, *tmp2
;
860 tmp1
= kmalloc(sizeof(*tmp1
),GFP_KERNEL
);
861 tmp2
= kmalloc(sizeof(*tmp2
),GFP_KERNEL
);
863 if (!tmp1
|| !tmp2
) {
865 printk(KERN_INFO
"md.c sb_equal(): failed to allocate memory!\n");
873 * nr_disks is not constant
878 ret
= (memcmp(tmp1
, tmp2
, MD_SB_GENERIC_CONSTANT_WORDS
* 4) == 0);
886 static u32
md_csum_fold(u32 csum
)
888 csum
= (csum
& 0xffff) + (csum
>> 16);
889 return (csum
& 0xffff) + (csum
>> 16);
892 static unsigned int calc_sb_csum(mdp_super_t
* sb
)
895 u32
*sb32
= (u32
*)sb
;
897 unsigned int disk_csum
, csum
;
899 disk_csum
= sb
->sb_csum
;
902 for (i
= 0; i
< MD_SB_BYTES
/4 ; i
++)
904 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
908 /* This used to use csum_partial, which was wrong for several
909 * reasons including that different results are returned on
910 * different architectures. It isn't critical that we get exactly
911 * the same return value as before (we always csum_fold before
912 * testing, and that removes any differences). However as we
913 * know that csum_partial always returned a 16bit value on
914 * alphas, do a fold to maximise conformity to previous behaviour.
916 sb
->sb_csum
= md_csum_fold(disk_csum
);
918 sb
->sb_csum
= disk_csum
;
925 * Handle superblock details.
926 * We want to be able to handle multiple superblock formats
927 * so we have a common interface to them all, and an array of
928 * different handlers.
929 * We rely on user-space to write the initial superblock, and support
930 * reading and updating of superblocks.
931 * Interface methods are:
932 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
933 * loads and validates a superblock on dev.
934 * if refdev != NULL, compare superblocks on both devices
936 * 0 - dev has a superblock that is compatible with refdev
937 * 1 - dev has a superblock that is compatible and newer than refdev
938 * so dev should be used as the refdev in future
939 * -EINVAL superblock incompatible or invalid
940 * -othererror e.g. -EIO
942 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
943 * Verify that dev is acceptable into mddev.
944 * The first time, mddev->raid_disks will be 0, and data from
945 * dev should be merged in. Subsequent calls check that dev
946 * is new enough. Return 0 or -EINVAL
948 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
949 * Update the superblock for rdev with data in mddev
950 * This does not write to disc.
956 struct module
*owner
;
957 int (*load_super
)(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
,
959 int (*validate_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
960 void (*sync_super
)(mddev_t
*mddev
, mdk_rdev_t
*rdev
);
961 unsigned long long (*rdev_size_change
)(mdk_rdev_t
*rdev
,
962 sector_t num_sectors
);
966 * Check that the given mddev has no bitmap.
968 * This function is called from the run method of all personalities that do not
969 * support bitmaps. It prints an error message and returns non-zero if mddev
970 * has a bitmap. Otherwise, it returns 0.
973 int md_check_no_bitmap(mddev_t
*mddev
)
975 if (!mddev
->bitmap_info
.file
&& !mddev
->bitmap_info
.offset
)
977 printk(KERN_ERR
"%s: bitmaps are not supported for %s\n",
978 mdname(mddev
), mddev
->pers
->name
);
981 EXPORT_SYMBOL(md_check_no_bitmap
);
984 * load_super for 0.90.0
986 static int super_90_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
988 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
993 * Calculate the position of the superblock (512byte sectors),
994 * it's at the end of the disk.
996 * It also happens to be a multiple of 4Kb.
998 rdev
->sb_start
= calc_dev_sboffset(rdev
);
1000 ret
= read_disk_sb(rdev
, MD_SB_BYTES
);
1001 if (ret
) return ret
;
1005 bdevname(rdev
->bdev
, b
);
1006 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
1008 if (sb
->md_magic
!= MD_SB_MAGIC
) {
1009 printk(KERN_ERR
"md: invalid raid superblock magic on %s\n",
1014 if (sb
->major_version
!= 0 ||
1015 sb
->minor_version
< 90 ||
1016 sb
->minor_version
> 91) {
1017 printk(KERN_WARNING
"Bad version number %d.%d on %s\n",
1018 sb
->major_version
, sb
->minor_version
,
1023 if (sb
->raid_disks
<= 0)
1026 if (md_csum_fold(calc_sb_csum(sb
)) != md_csum_fold(sb
->sb_csum
)) {
1027 printk(KERN_WARNING
"md: invalid superblock checksum on %s\n",
1032 rdev
->preferred_minor
= sb
->md_minor
;
1033 rdev
->data_offset
= 0;
1034 rdev
->sb_size
= MD_SB_BYTES
;
1036 if (sb
->level
== LEVEL_MULTIPATH
)
1039 rdev
->desc_nr
= sb
->this_disk
.number
;
1045 mdp_super_t
*refsb
= (mdp_super_t
*)page_address(refdev
->sb_page
);
1046 if (!uuid_equal(refsb
, sb
)) {
1047 printk(KERN_WARNING
"md: %s has different UUID to %s\n",
1048 b
, bdevname(refdev
->bdev
,b2
));
1051 if (!sb_equal(refsb
, sb
)) {
1052 printk(KERN_WARNING
"md: %s has same UUID"
1053 " but different superblock to %s\n",
1054 b
, bdevname(refdev
->bdev
, b2
));
1058 ev2
= md_event(refsb
);
1064 rdev
->sectors
= rdev
->sb_start
;
1066 if (rdev
->sectors
< sb
->size
* 2 && sb
->level
> 1)
1067 /* "this cannot possibly happen" ... */
1075 * validate_super for 0.90.0
1077 static int super_90_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1080 mdp_super_t
*sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
1081 __u64 ev1
= md_event(sb
);
1083 rdev
->raid_disk
= -1;
1084 clear_bit(Faulty
, &rdev
->flags
);
1085 clear_bit(In_sync
, &rdev
->flags
);
1086 clear_bit(WriteMostly
, &rdev
->flags
);
1088 if (mddev
->raid_disks
== 0) {
1089 mddev
->major_version
= 0;
1090 mddev
->minor_version
= sb
->minor_version
;
1091 mddev
->patch_version
= sb
->patch_version
;
1092 mddev
->external
= 0;
1093 mddev
->chunk_sectors
= sb
->chunk_size
>> 9;
1094 mddev
->ctime
= sb
->ctime
;
1095 mddev
->utime
= sb
->utime
;
1096 mddev
->level
= sb
->level
;
1097 mddev
->clevel
[0] = 0;
1098 mddev
->layout
= sb
->layout
;
1099 mddev
->raid_disks
= sb
->raid_disks
;
1100 mddev
->dev_sectors
= sb
->size
* 2;
1101 mddev
->events
= ev1
;
1102 mddev
->bitmap_info
.offset
= 0;
1103 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
1105 if (mddev
->minor_version
>= 91) {
1106 mddev
->reshape_position
= sb
->reshape_position
;
1107 mddev
->delta_disks
= sb
->delta_disks
;
1108 mddev
->new_level
= sb
->new_level
;
1109 mddev
->new_layout
= sb
->new_layout
;
1110 mddev
->new_chunk_sectors
= sb
->new_chunk
>> 9;
1112 mddev
->reshape_position
= MaxSector
;
1113 mddev
->delta_disks
= 0;
1114 mddev
->new_level
= mddev
->level
;
1115 mddev
->new_layout
= mddev
->layout
;
1116 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1119 if (sb
->state
& (1<<MD_SB_CLEAN
))
1120 mddev
->recovery_cp
= MaxSector
;
1122 if (sb
->events_hi
== sb
->cp_events_hi
&&
1123 sb
->events_lo
== sb
->cp_events_lo
) {
1124 mddev
->recovery_cp
= sb
->recovery_cp
;
1126 mddev
->recovery_cp
= 0;
1129 memcpy(mddev
->uuid
+0, &sb
->set_uuid0
, 4);
1130 memcpy(mddev
->uuid
+4, &sb
->set_uuid1
, 4);
1131 memcpy(mddev
->uuid
+8, &sb
->set_uuid2
, 4);
1132 memcpy(mddev
->uuid
+12,&sb
->set_uuid3
, 4);
1134 mddev
->max_disks
= MD_SB_DISKS
;
1136 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
) &&
1137 mddev
->bitmap_info
.file
== NULL
)
1138 mddev
->bitmap_info
.offset
=
1139 mddev
->bitmap_info
.default_offset
;
1141 } else if (mddev
->pers
== NULL
) {
1142 /* Insist on good event counter while assembling, except
1143 * for spares (which don't need an event count) */
1145 if (sb
->disks
[rdev
->desc_nr
].state
& (
1146 (1<<MD_DISK_SYNC
) | (1 << MD_DISK_ACTIVE
)))
1147 if (ev1
< mddev
->events
)
1149 } else if (mddev
->bitmap
) {
1150 /* if adding to array with a bitmap, then we can accept an
1151 * older device ... but not too old.
1153 if (ev1
< mddev
->bitmap
->events_cleared
)
1156 if (ev1
< mddev
->events
)
1157 /* just a hot-add of a new device, leave raid_disk at -1 */
1161 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1162 desc
= sb
->disks
+ rdev
->desc_nr
;
1164 if (desc
->state
& (1<<MD_DISK_FAULTY
))
1165 set_bit(Faulty
, &rdev
->flags
);
1166 else if (desc
->state
& (1<<MD_DISK_SYNC
) /* &&
1167 desc->raid_disk < mddev->raid_disks */) {
1168 set_bit(In_sync
, &rdev
->flags
);
1169 rdev
->raid_disk
= desc
->raid_disk
;
1170 } else if (desc
->state
& (1<<MD_DISK_ACTIVE
)) {
1171 /* active but not in sync implies recovery up to
1172 * reshape position. We don't know exactly where
1173 * that is, so set to zero for now */
1174 if (mddev
->minor_version
>= 91) {
1175 rdev
->recovery_offset
= 0;
1176 rdev
->raid_disk
= desc
->raid_disk
;
1179 if (desc
->state
& (1<<MD_DISK_WRITEMOSTLY
))
1180 set_bit(WriteMostly
, &rdev
->flags
);
1181 } else /* MULTIPATH are always insync */
1182 set_bit(In_sync
, &rdev
->flags
);
1187 * sync_super for 0.90.0
1189 static void super_90_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1193 int next_spare
= mddev
->raid_disks
;
1196 /* make rdev->sb match mddev data..
1199 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1200 * 3/ any empty disks < next_spare become removed
1202 * disks[0] gets initialised to REMOVED because
1203 * we cannot be sure from other fields if it has
1204 * been initialised or not.
1207 int active
=0, working
=0,failed
=0,spare
=0,nr_disks
=0;
1209 rdev
->sb_size
= MD_SB_BYTES
;
1211 sb
= (mdp_super_t
*)page_address(rdev
->sb_page
);
1213 memset(sb
, 0, sizeof(*sb
));
1215 sb
->md_magic
= MD_SB_MAGIC
;
1216 sb
->major_version
= mddev
->major_version
;
1217 sb
->patch_version
= mddev
->patch_version
;
1218 sb
->gvalid_words
= 0; /* ignored */
1219 memcpy(&sb
->set_uuid0
, mddev
->uuid
+0, 4);
1220 memcpy(&sb
->set_uuid1
, mddev
->uuid
+4, 4);
1221 memcpy(&sb
->set_uuid2
, mddev
->uuid
+8, 4);
1222 memcpy(&sb
->set_uuid3
, mddev
->uuid
+12,4);
1224 sb
->ctime
= mddev
->ctime
;
1225 sb
->level
= mddev
->level
;
1226 sb
->size
= mddev
->dev_sectors
/ 2;
1227 sb
->raid_disks
= mddev
->raid_disks
;
1228 sb
->md_minor
= mddev
->md_minor
;
1229 sb
->not_persistent
= 0;
1230 sb
->utime
= mddev
->utime
;
1232 sb
->events_hi
= (mddev
->events
>>32);
1233 sb
->events_lo
= (u32
)mddev
->events
;
1235 if (mddev
->reshape_position
== MaxSector
)
1236 sb
->minor_version
= 90;
1238 sb
->minor_version
= 91;
1239 sb
->reshape_position
= mddev
->reshape_position
;
1240 sb
->new_level
= mddev
->new_level
;
1241 sb
->delta_disks
= mddev
->delta_disks
;
1242 sb
->new_layout
= mddev
->new_layout
;
1243 sb
->new_chunk
= mddev
->new_chunk_sectors
<< 9;
1245 mddev
->minor_version
= sb
->minor_version
;
1248 sb
->recovery_cp
= mddev
->recovery_cp
;
1249 sb
->cp_events_hi
= (mddev
->events
>>32);
1250 sb
->cp_events_lo
= (u32
)mddev
->events
;
1251 if (mddev
->recovery_cp
== MaxSector
)
1252 sb
->state
= (1<< MD_SB_CLEAN
);
1254 sb
->recovery_cp
= 0;
1256 sb
->layout
= mddev
->layout
;
1257 sb
->chunk_size
= mddev
->chunk_sectors
<< 9;
1259 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
)
1260 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
1262 sb
->disks
[0].state
= (1<<MD_DISK_REMOVED
);
1263 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
1266 int is_active
= test_bit(In_sync
, &rdev2
->flags
);
1268 if (rdev2
->raid_disk
>= 0 &&
1269 sb
->minor_version
>= 91)
1270 /* we have nowhere to store the recovery_offset,
1271 * but if it is not below the reshape_position,
1272 * we can piggy-back on that.
1275 if (rdev2
->raid_disk
< 0 ||
1276 test_bit(Faulty
, &rdev2
->flags
))
1279 desc_nr
= rdev2
->raid_disk
;
1281 desc_nr
= next_spare
++;
1282 rdev2
->desc_nr
= desc_nr
;
1283 d
= &sb
->disks
[rdev2
->desc_nr
];
1285 d
->number
= rdev2
->desc_nr
;
1286 d
->major
= MAJOR(rdev2
->bdev
->bd_dev
);
1287 d
->minor
= MINOR(rdev2
->bdev
->bd_dev
);
1289 d
->raid_disk
= rdev2
->raid_disk
;
1291 d
->raid_disk
= rdev2
->desc_nr
; /* compatibility */
1292 if (test_bit(Faulty
, &rdev2
->flags
))
1293 d
->state
= (1<<MD_DISK_FAULTY
);
1294 else if (is_active
) {
1295 d
->state
= (1<<MD_DISK_ACTIVE
);
1296 if (test_bit(In_sync
, &rdev2
->flags
))
1297 d
->state
|= (1<<MD_DISK_SYNC
);
1305 if (test_bit(WriteMostly
, &rdev2
->flags
))
1306 d
->state
|= (1<<MD_DISK_WRITEMOSTLY
);
1308 /* now set the "removed" and "faulty" bits on any missing devices */
1309 for (i
=0 ; i
< mddev
->raid_disks
; i
++) {
1310 mdp_disk_t
*d
= &sb
->disks
[i
];
1311 if (d
->state
== 0 && d
->number
== 0) {
1314 d
->state
= (1<<MD_DISK_REMOVED
);
1315 d
->state
|= (1<<MD_DISK_FAULTY
);
1319 sb
->nr_disks
= nr_disks
;
1320 sb
->active_disks
= active
;
1321 sb
->working_disks
= working
;
1322 sb
->failed_disks
= failed
;
1323 sb
->spare_disks
= spare
;
1325 sb
->this_disk
= sb
->disks
[rdev
->desc_nr
];
1326 sb
->sb_csum
= calc_sb_csum(sb
);
1330 * rdev_size_change for 0.90.0
1332 static unsigned long long
1333 super_90_rdev_size_change(mdk_rdev_t
*rdev
, sector_t num_sectors
)
1335 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1336 return 0; /* component must fit device */
1337 if (rdev
->mddev
->bitmap_info
.offset
)
1338 return 0; /* can't move bitmap */
1339 rdev
->sb_start
= calc_dev_sboffset(rdev
);
1340 if (!num_sectors
|| num_sectors
> rdev
->sb_start
)
1341 num_sectors
= rdev
->sb_start
;
1342 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1344 md_super_wait(rdev
->mddev
);
1350 * version 1 superblock
1353 static __le32
calc_sb_1_csum(struct mdp_superblock_1
* sb
)
1357 unsigned long long newcsum
;
1358 int size
= 256 + le32_to_cpu(sb
->max_dev
)*2;
1359 __le32
*isuper
= (__le32
*)sb
;
1362 disk_csum
= sb
->sb_csum
;
1365 for (i
=0; size
>=4; size
-= 4 )
1366 newcsum
+= le32_to_cpu(*isuper
++);
1369 newcsum
+= le16_to_cpu(*(__le16
*) isuper
);
1371 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
1372 sb
->sb_csum
= disk_csum
;
1373 return cpu_to_le32(csum
);
1376 static int super_1_load(mdk_rdev_t
*rdev
, mdk_rdev_t
*refdev
, int minor_version
)
1378 struct mdp_superblock_1
*sb
;
1381 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
1385 * Calculate the position of the superblock in 512byte sectors.
1386 * It is always aligned to a 4K boundary and
1387 * depeding on minor_version, it can be:
1388 * 0: At least 8K, but less than 12K, from end of device
1389 * 1: At start of device
1390 * 2: 4K from start of device.
1392 switch(minor_version
) {
1394 sb_start
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
1396 sb_start
&= ~(sector_t
)(4*2-1);
1407 rdev
->sb_start
= sb_start
;
1409 /* superblock is rarely larger than 1K, but it can be larger,
1410 * and it is safe to read 4k, so we do that
1412 ret
= read_disk_sb(rdev
, 4096);
1413 if (ret
) return ret
;
1416 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1418 if (sb
->magic
!= cpu_to_le32(MD_SB_MAGIC
) ||
1419 sb
->major_version
!= cpu_to_le32(1) ||
1420 le32_to_cpu(sb
->max_dev
) > (4096-256)/2 ||
1421 le64_to_cpu(sb
->super_offset
) != rdev
->sb_start
||
1422 (le32_to_cpu(sb
->feature_map
) & ~MD_FEATURE_ALL
) != 0)
1425 if (calc_sb_1_csum(sb
) != sb
->sb_csum
) {
1426 printk("md: invalid superblock checksum on %s\n",
1427 bdevname(rdev
->bdev
,b
));
1430 if (le64_to_cpu(sb
->data_size
) < 10) {
1431 printk("md: data_size too small on %s\n",
1432 bdevname(rdev
->bdev
,b
));
1436 rdev
->preferred_minor
= 0xffff;
1437 rdev
->data_offset
= le64_to_cpu(sb
->data_offset
);
1438 atomic_set(&rdev
->corrected_errors
, le32_to_cpu(sb
->cnt_corrected_read
));
1440 rdev
->sb_size
= le32_to_cpu(sb
->max_dev
) * 2 + 256;
1441 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1442 if (rdev
->sb_size
& bmask
)
1443 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1446 && rdev
->data_offset
< sb_start
+ (rdev
->sb_size
/512))
1449 if (sb
->level
== cpu_to_le32(LEVEL_MULTIPATH
))
1452 rdev
->desc_nr
= le32_to_cpu(sb
->dev_number
);
1458 struct mdp_superblock_1
*refsb
=
1459 (struct mdp_superblock_1
*)page_address(refdev
->sb_page
);
1461 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16) != 0 ||
1462 sb
->level
!= refsb
->level
||
1463 sb
->layout
!= refsb
->layout
||
1464 sb
->chunksize
!= refsb
->chunksize
) {
1465 printk(KERN_WARNING
"md: %s has strangely different"
1466 " superblock to %s\n",
1467 bdevname(rdev
->bdev
,b
),
1468 bdevname(refdev
->bdev
,b2
));
1471 ev1
= le64_to_cpu(sb
->events
);
1472 ev2
= le64_to_cpu(refsb
->events
);
1480 rdev
->sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) -
1481 le64_to_cpu(sb
->data_offset
);
1483 rdev
->sectors
= rdev
->sb_start
;
1484 if (rdev
->sectors
< le64_to_cpu(sb
->data_size
))
1486 rdev
->sectors
= le64_to_cpu(sb
->data_size
);
1487 if (le64_to_cpu(sb
->size
) > rdev
->sectors
)
1492 static int super_1_validate(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1494 struct mdp_superblock_1
*sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1495 __u64 ev1
= le64_to_cpu(sb
->events
);
1497 rdev
->raid_disk
= -1;
1498 clear_bit(Faulty
, &rdev
->flags
);
1499 clear_bit(In_sync
, &rdev
->flags
);
1500 clear_bit(WriteMostly
, &rdev
->flags
);
1502 if (mddev
->raid_disks
== 0) {
1503 mddev
->major_version
= 1;
1504 mddev
->patch_version
= 0;
1505 mddev
->external
= 0;
1506 mddev
->chunk_sectors
= le32_to_cpu(sb
->chunksize
);
1507 mddev
->ctime
= le64_to_cpu(sb
->ctime
) & ((1ULL << 32)-1);
1508 mddev
->utime
= le64_to_cpu(sb
->utime
) & ((1ULL << 32)-1);
1509 mddev
->level
= le32_to_cpu(sb
->level
);
1510 mddev
->clevel
[0] = 0;
1511 mddev
->layout
= le32_to_cpu(sb
->layout
);
1512 mddev
->raid_disks
= le32_to_cpu(sb
->raid_disks
);
1513 mddev
->dev_sectors
= le64_to_cpu(sb
->size
);
1514 mddev
->events
= ev1
;
1515 mddev
->bitmap_info
.offset
= 0;
1516 mddev
->bitmap_info
.default_offset
= 1024 >> 9;
1518 mddev
->recovery_cp
= le64_to_cpu(sb
->resync_offset
);
1519 memcpy(mddev
->uuid
, sb
->set_uuid
, 16);
1521 mddev
->max_disks
= (4096-256)/2;
1523 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_BITMAP_OFFSET
) &&
1524 mddev
->bitmap_info
.file
== NULL
)
1525 mddev
->bitmap_info
.offset
=
1526 (__s32
)le32_to_cpu(sb
->bitmap_offset
);
1528 if ((le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RESHAPE_ACTIVE
)) {
1529 mddev
->reshape_position
= le64_to_cpu(sb
->reshape_position
);
1530 mddev
->delta_disks
= le32_to_cpu(sb
->delta_disks
);
1531 mddev
->new_level
= le32_to_cpu(sb
->new_level
);
1532 mddev
->new_layout
= le32_to_cpu(sb
->new_layout
);
1533 mddev
->new_chunk_sectors
= le32_to_cpu(sb
->new_chunk
);
1535 mddev
->reshape_position
= MaxSector
;
1536 mddev
->delta_disks
= 0;
1537 mddev
->new_level
= mddev
->level
;
1538 mddev
->new_layout
= mddev
->layout
;
1539 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
1542 } else if (mddev
->pers
== NULL
) {
1543 /* Insist of good event counter while assembling, except for
1544 * spares (which don't need an event count) */
1546 if (rdev
->desc_nr
>= 0 &&
1547 rdev
->desc_nr
< le32_to_cpu(sb
->max_dev
) &&
1548 le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]) < 0xfffe)
1549 if (ev1
< mddev
->events
)
1551 } else if (mddev
->bitmap
) {
1552 /* If adding to array with a bitmap, then we can accept an
1553 * older device, but not too old.
1555 if (ev1
< mddev
->bitmap
->events_cleared
)
1558 if (ev1
< mddev
->events
)
1559 /* just a hot-add of a new device, leave raid_disk at -1 */
1562 if (mddev
->level
!= LEVEL_MULTIPATH
) {
1564 if (rdev
->desc_nr
< 0 ||
1565 rdev
->desc_nr
>= le32_to_cpu(sb
->max_dev
)) {
1569 role
= le16_to_cpu(sb
->dev_roles
[rdev
->desc_nr
]);
1571 case 0xffff: /* spare */
1573 case 0xfffe: /* faulty */
1574 set_bit(Faulty
, &rdev
->flags
);
1577 if ((le32_to_cpu(sb
->feature_map
) &
1578 MD_FEATURE_RECOVERY_OFFSET
))
1579 rdev
->recovery_offset
= le64_to_cpu(sb
->recovery_offset
);
1581 set_bit(In_sync
, &rdev
->flags
);
1582 rdev
->raid_disk
= role
;
1585 if (sb
->devflags
& WriteMostly1
)
1586 set_bit(WriteMostly
, &rdev
->flags
);
1587 } else /* MULTIPATH are always insync */
1588 set_bit(In_sync
, &rdev
->flags
);
1593 static void super_1_sync(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1595 struct mdp_superblock_1
*sb
;
1598 /* make rdev->sb match mddev and rdev data. */
1600 sb
= (struct mdp_superblock_1
*)page_address(rdev
->sb_page
);
1602 sb
->feature_map
= 0;
1604 sb
->recovery_offset
= cpu_to_le64(0);
1605 memset(sb
->pad1
, 0, sizeof(sb
->pad1
));
1606 memset(sb
->pad2
, 0, sizeof(sb
->pad2
));
1607 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
1609 sb
->utime
= cpu_to_le64((__u64
)mddev
->utime
);
1610 sb
->events
= cpu_to_le64(mddev
->events
);
1612 sb
->resync_offset
= cpu_to_le64(mddev
->recovery_cp
);
1614 sb
->resync_offset
= cpu_to_le64(0);
1616 sb
->cnt_corrected_read
= cpu_to_le32(atomic_read(&rdev
->corrected_errors
));
1618 sb
->raid_disks
= cpu_to_le32(mddev
->raid_disks
);
1619 sb
->size
= cpu_to_le64(mddev
->dev_sectors
);
1620 sb
->chunksize
= cpu_to_le32(mddev
->chunk_sectors
);
1621 sb
->level
= cpu_to_le32(mddev
->level
);
1622 sb
->layout
= cpu_to_le32(mddev
->layout
);
1624 if (mddev
->bitmap
&& mddev
->bitmap_info
.file
== NULL
) {
1625 sb
->bitmap_offset
= cpu_to_le32((__u32
)mddev
->bitmap_info
.offset
);
1626 sb
->feature_map
= cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
);
1629 if (rdev
->raid_disk
>= 0 &&
1630 !test_bit(In_sync
, &rdev
->flags
)) {
1632 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET
);
1633 sb
->recovery_offset
=
1634 cpu_to_le64(rdev
->recovery_offset
);
1637 if (mddev
->reshape_position
!= MaxSector
) {
1638 sb
->feature_map
|= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE
);
1639 sb
->reshape_position
= cpu_to_le64(mddev
->reshape_position
);
1640 sb
->new_layout
= cpu_to_le32(mddev
->new_layout
);
1641 sb
->delta_disks
= cpu_to_le32(mddev
->delta_disks
);
1642 sb
->new_level
= cpu_to_le32(mddev
->new_level
);
1643 sb
->new_chunk
= cpu_to_le32(mddev
->new_chunk_sectors
);
1647 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
)
1648 if (rdev2
->desc_nr
+1 > max_dev
)
1649 max_dev
= rdev2
->desc_nr
+1;
1651 if (max_dev
> le32_to_cpu(sb
->max_dev
)) {
1653 sb
->max_dev
= cpu_to_le32(max_dev
);
1654 rdev
->sb_size
= max_dev
* 2 + 256;
1655 bmask
= queue_logical_block_size(rdev
->bdev
->bd_disk
->queue
)-1;
1656 if (rdev
->sb_size
& bmask
)
1657 rdev
->sb_size
= (rdev
->sb_size
| bmask
) + 1;
1659 max_dev
= le32_to_cpu(sb
->max_dev
);
1661 for (i
=0; i
<max_dev
;i
++)
1662 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1664 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
1666 if (test_bit(Faulty
, &rdev2
->flags
))
1667 sb
->dev_roles
[i
] = cpu_to_le16(0xfffe);
1668 else if (test_bit(In_sync
, &rdev2
->flags
))
1669 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1670 else if (rdev2
->raid_disk
>= 0)
1671 sb
->dev_roles
[i
] = cpu_to_le16(rdev2
->raid_disk
);
1673 sb
->dev_roles
[i
] = cpu_to_le16(0xffff);
1676 sb
->sb_csum
= calc_sb_1_csum(sb
);
1679 static unsigned long long
1680 super_1_rdev_size_change(mdk_rdev_t
*rdev
, sector_t num_sectors
)
1682 struct mdp_superblock_1
*sb
;
1683 sector_t max_sectors
;
1684 if (num_sectors
&& num_sectors
< rdev
->mddev
->dev_sectors
)
1685 return 0; /* component must fit device */
1686 if (rdev
->sb_start
< rdev
->data_offset
) {
1687 /* minor versions 1 and 2; superblock before data */
1688 max_sectors
= i_size_read(rdev
->bdev
->bd_inode
) >> 9;
1689 max_sectors
-= rdev
->data_offset
;
1690 if (!num_sectors
|| num_sectors
> max_sectors
)
1691 num_sectors
= max_sectors
;
1692 } else if (rdev
->mddev
->bitmap_info
.offset
) {
1693 /* minor version 0 with bitmap we can't move */
1696 /* minor version 0; superblock after data */
1698 sb_start
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) - 8*2;
1699 sb_start
&= ~(sector_t
)(4*2 - 1);
1700 max_sectors
= rdev
->sectors
+ sb_start
- rdev
->sb_start
;
1701 if (!num_sectors
|| num_sectors
> max_sectors
)
1702 num_sectors
= max_sectors
;
1703 rdev
->sb_start
= sb_start
;
1705 sb
= (struct mdp_superblock_1
*) page_address(rdev
->sb_page
);
1706 sb
->data_size
= cpu_to_le64(num_sectors
);
1707 sb
->super_offset
= rdev
->sb_start
;
1708 sb
->sb_csum
= calc_sb_1_csum(sb
);
1709 md_super_write(rdev
->mddev
, rdev
, rdev
->sb_start
, rdev
->sb_size
,
1711 md_super_wait(rdev
->mddev
);
1715 static struct super_type super_types
[] = {
1718 .owner
= THIS_MODULE
,
1719 .load_super
= super_90_load
,
1720 .validate_super
= super_90_validate
,
1721 .sync_super
= super_90_sync
,
1722 .rdev_size_change
= super_90_rdev_size_change
,
1726 .owner
= THIS_MODULE
,
1727 .load_super
= super_1_load
,
1728 .validate_super
= super_1_validate
,
1729 .sync_super
= super_1_sync
,
1730 .rdev_size_change
= super_1_rdev_size_change
,
1734 static int match_mddev_units(mddev_t
*mddev1
, mddev_t
*mddev2
)
1736 mdk_rdev_t
*rdev
, *rdev2
;
1739 rdev_for_each_rcu(rdev
, mddev1
)
1740 rdev_for_each_rcu(rdev2
, mddev2
)
1741 if (rdev
->bdev
->bd_contains
==
1742 rdev2
->bdev
->bd_contains
) {
1750 static LIST_HEAD(pending_raid_disks
);
1753 * Try to register data integrity profile for an mddev
1755 * This is called when an array is started and after a disk has been kicked
1756 * from the array. It only succeeds if all working and active component devices
1757 * are integrity capable with matching profiles.
1759 int md_integrity_register(mddev_t
*mddev
)
1761 mdk_rdev_t
*rdev
, *reference
= NULL
;
1763 if (list_empty(&mddev
->disks
))
1764 return 0; /* nothing to do */
1765 if (blk_get_integrity(mddev
->gendisk
))
1766 return 0; /* already registered */
1767 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
1768 /* skip spares and non-functional disks */
1769 if (test_bit(Faulty
, &rdev
->flags
))
1771 if (rdev
->raid_disk
< 0)
1774 * If at least one rdev is not integrity capable, we can not
1775 * enable data integrity for the md device.
1777 if (!bdev_get_integrity(rdev
->bdev
))
1780 /* Use the first rdev as the reference */
1784 /* does this rdev's profile match the reference profile? */
1785 if (blk_integrity_compare(reference
->bdev
->bd_disk
,
1786 rdev
->bdev
->bd_disk
) < 0)
1790 * All component devices are integrity capable and have matching
1791 * profiles, register the common profile for the md device.
1793 if (blk_integrity_register(mddev
->gendisk
,
1794 bdev_get_integrity(reference
->bdev
)) != 0) {
1795 printk(KERN_ERR
"md: failed to register integrity for %s\n",
1799 printk(KERN_NOTICE
"md: data integrity on %s enabled\n",
1803 EXPORT_SYMBOL(md_integrity_register
);
1805 /* Disable data integrity if non-capable/non-matching disk is being added */
1806 void md_integrity_add_rdev(mdk_rdev_t
*rdev
, mddev_t
*mddev
)
1808 struct blk_integrity
*bi_rdev
= bdev_get_integrity(rdev
->bdev
);
1809 struct blk_integrity
*bi_mddev
= blk_get_integrity(mddev
->gendisk
);
1811 if (!bi_mddev
) /* nothing to do */
1813 if (rdev
->raid_disk
< 0) /* skip spares */
1815 if (bi_rdev
&& blk_integrity_compare(mddev
->gendisk
,
1816 rdev
->bdev
->bd_disk
) >= 0)
1818 printk(KERN_NOTICE
"disabling data integrity on %s\n", mdname(mddev
));
1819 blk_integrity_unregister(mddev
->gendisk
);
1821 EXPORT_SYMBOL(md_integrity_add_rdev
);
1823 static int bind_rdev_to_array(mdk_rdev_t
* rdev
, mddev_t
* mddev
)
1825 char b
[BDEVNAME_SIZE
];
1835 /* prevent duplicates */
1836 if (find_rdev(mddev
, rdev
->bdev
->bd_dev
))
1839 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1840 if (rdev
->sectors
&& (mddev
->dev_sectors
== 0 ||
1841 rdev
->sectors
< mddev
->dev_sectors
)) {
1843 /* Cannot change size, so fail
1844 * If mddev->level <= 0, then we don't care
1845 * about aligning sizes (e.g. linear)
1847 if (mddev
->level
> 0)
1850 mddev
->dev_sectors
= rdev
->sectors
;
1853 /* Verify rdev->desc_nr is unique.
1854 * If it is -1, assign a free number, else
1855 * check number is not in use
1857 if (rdev
->desc_nr
< 0) {
1859 if (mddev
->pers
) choice
= mddev
->raid_disks
;
1860 while (find_rdev_nr(mddev
, choice
))
1862 rdev
->desc_nr
= choice
;
1864 if (find_rdev_nr(mddev
, rdev
->desc_nr
))
1867 if (mddev
->max_disks
&& rdev
->desc_nr
>= mddev
->max_disks
) {
1868 printk(KERN_WARNING
"md: %s: array is limited to %d devices\n",
1869 mdname(mddev
), mddev
->max_disks
);
1872 bdevname(rdev
->bdev
,b
);
1873 while ( (s
=strchr(b
, '/')) != NULL
)
1876 rdev
->mddev
= mddev
;
1877 printk(KERN_INFO
"md: bind<%s>\n", b
);
1879 if ((err
= kobject_add(&rdev
->kobj
, &mddev
->kobj
, "dev-%s", b
)))
1882 ko
= &part_to_dev(rdev
->bdev
->bd_part
)->kobj
;
1883 if (sysfs_create_link(&rdev
->kobj
, ko
, "block"))
1884 /* failure here is OK */;
1885 rdev
->sysfs_state
= sysfs_get_dirent_safe(rdev
->kobj
.sd
, "state");
1887 list_add_rcu(&rdev
->same_set
, &mddev
->disks
);
1888 bd_link_disk_holder(rdev
->bdev
, mddev
->gendisk
);
1890 /* May as well allow recovery to be retried once */
1891 mddev
->recovery_disabled
= 0;
1896 printk(KERN_WARNING
"md: failed to register dev-%s for %s\n",
1901 static void md_delayed_delete(struct work_struct
*ws
)
1903 mdk_rdev_t
*rdev
= container_of(ws
, mdk_rdev_t
, del_work
);
1904 kobject_del(&rdev
->kobj
);
1905 kobject_put(&rdev
->kobj
);
1908 static void unbind_rdev_from_array(mdk_rdev_t
* rdev
)
1910 char b
[BDEVNAME_SIZE
];
1915 bd_unlink_disk_holder(rdev
->bdev
, rdev
->mddev
->gendisk
);
1916 list_del_rcu(&rdev
->same_set
);
1917 printk(KERN_INFO
"md: unbind<%s>\n", bdevname(rdev
->bdev
,b
));
1919 sysfs_remove_link(&rdev
->kobj
, "block");
1920 sysfs_put(rdev
->sysfs_state
);
1921 rdev
->sysfs_state
= NULL
;
1922 /* We need to delay this, otherwise we can deadlock when
1923 * writing to 'remove' to "dev/state". We also need
1924 * to delay it due to rcu usage.
1927 INIT_WORK(&rdev
->del_work
, md_delayed_delete
);
1928 kobject_get(&rdev
->kobj
);
1929 queue_work(md_misc_wq
, &rdev
->del_work
);
1933 * prevent the device from being mounted, repartitioned or
1934 * otherwise reused by a RAID array (or any other kernel
1935 * subsystem), by bd_claiming the device.
1937 static int lock_rdev(mdk_rdev_t
*rdev
, dev_t dev
, int shared
)
1940 struct block_device
*bdev
;
1941 char b
[BDEVNAME_SIZE
];
1943 bdev
= blkdev_get_by_dev(dev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
,
1944 shared
? (mdk_rdev_t
*)lock_rdev
: rdev
);
1946 printk(KERN_ERR
"md: could not open %s.\n",
1947 __bdevname(dev
, b
));
1948 return PTR_ERR(bdev
);
1951 set_bit(AllReserved
, &rdev
->flags
);
1956 static void unlock_rdev(mdk_rdev_t
*rdev
)
1958 struct block_device
*bdev
= rdev
->bdev
;
1962 blkdev_put(bdev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
);
1965 void md_autodetect_dev(dev_t dev
);
1967 static void export_rdev(mdk_rdev_t
* rdev
)
1969 char b
[BDEVNAME_SIZE
];
1970 printk(KERN_INFO
"md: export_rdev(%s)\n",
1971 bdevname(rdev
->bdev
,b
));
1976 if (test_bit(AutoDetected
, &rdev
->flags
))
1977 md_autodetect_dev(rdev
->bdev
->bd_dev
);
1980 kobject_put(&rdev
->kobj
);
1983 static void kick_rdev_from_array(mdk_rdev_t
* rdev
)
1985 unbind_rdev_from_array(rdev
);
1989 static void export_array(mddev_t
*mddev
)
1991 mdk_rdev_t
*rdev
, *tmp
;
1993 rdev_for_each(rdev
, tmp
, mddev
) {
1998 kick_rdev_from_array(rdev
);
2000 if (!list_empty(&mddev
->disks
))
2002 mddev
->raid_disks
= 0;
2003 mddev
->major_version
= 0;
2006 static void print_desc(mdp_disk_t
*desc
)
2008 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc
->number
,
2009 desc
->major
,desc
->minor
,desc
->raid_disk
,desc
->state
);
2012 static void print_sb_90(mdp_super_t
*sb
)
2017 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2018 sb
->major_version
, sb
->minor_version
, sb
->patch_version
,
2019 sb
->set_uuid0
, sb
->set_uuid1
, sb
->set_uuid2
, sb
->set_uuid3
,
2021 printk(KERN_INFO
"md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2022 sb
->level
, sb
->size
, sb
->nr_disks
, sb
->raid_disks
,
2023 sb
->md_minor
, sb
->layout
, sb
->chunk_size
);
2024 printk(KERN_INFO
"md: UT:%08x ST:%d AD:%d WD:%d"
2025 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2026 sb
->utime
, sb
->state
, sb
->active_disks
, sb
->working_disks
,
2027 sb
->failed_disks
, sb
->spare_disks
,
2028 sb
->sb_csum
, (unsigned long)sb
->events_lo
);
2031 for (i
= 0; i
< MD_SB_DISKS
; i
++) {
2034 desc
= sb
->disks
+ i
;
2035 if (desc
->number
|| desc
->major
|| desc
->minor
||
2036 desc
->raid_disk
|| (desc
->state
&& (desc
->state
!= 4))) {
2037 printk(" D %2d: ", i
);
2041 printk(KERN_INFO
"md: THIS: ");
2042 print_desc(&sb
->this_disk
);
2045 static void print_sb_1(struct mdp_superblock_1
*sb
)
2049 uuid
= sb
->set_uuid
;
2051 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
2052 "md: Name: \"%s\" CT:%llu\n",
2053 le32_to_cpu(sb
->major_version
),
2054 le32_to_cpu(sb
->feature_map
),
2057 (unsigned long long)le64_to_cpu(sb
->ctime
)
2058 & MD_SUPERBLOCK_1_TIME_SEC_MASK
);
2060 uuid
= sb
->device_uuid
;
2062 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
2064 "md: Dev:%08x UUID: %pU\n"
2065 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2066 "md: (MaxDev:%u) \n",
2067 le32_to_cpu(sb
->level
),
2068 (unsigned long long)le64_to_cpu(sb
->size
),
2069 le32_to_cpu(sb
->raid_disks
),
2070 le32_to_cpu(sb
->layout
),
2071 le32_to_cpu(sb
->chunksize
),
2072 (unsigned long long)le64_to_cpu(sb
->data_offset
),
2073 (unsigned long long)le64_to_cpu(sb
->data_size
),
2074 (unsigned long long)le64_to_cpu(sb
->super_offset
),
2075 (unsigned long long)le64_to_cpu(sb
->recovery_offset
),
2076 le32_to_cpu(sb
->dev_number
),
2079 (unsigned long long)le64_to_cpu(sb
->utime
) & MD_SUPERBLOCK_1_TIME_SEC_MASK
,
2080 (unsigned long long)le64_to_cpu(sb
->events
),
2081 (unsigned long long)le64_to_cpu(sb
->resync_offset
),
2082 le32_to_cpu(sb
->sb_csum
),
2083 le32_to_cpu(sb
->max_dev
)
2087 static void print_rdev(mdk_rdev_t
*rdev
, int major_version
)
2089 char b
[BDEVNAME_SIZE
];
2090 printk(KERN_INFO
"md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2091 bdevname(rdev
->bdev
, b
), (unsigned long long)rdev
->sectors
,
2092 test_bit(Faulty
, &rdev
->flags
), test_bit(In_sync
, &rdev
->flags
),
2094 if (rdev
->sb_loaded
) {
2095 printk(KERN_INFO
"md: rdev superblock (MJ:%d):\n", major_version
);
2096 switch (major_version
) {
2098 print_sb_90((mdp_super_t
*)page_address(rdev
->sb_page
));
2101 print_sb_1((struct mdp_superblock_1
*)page_address(rdev
->sb_page
));
2105 printk(KERN_INFO
"md: no rdev superblock!\n");
2108 static void md_print_devices(void)
2110 struct list_head
*tmp
;
2113 char b
[BDEVNAME_SIZE
];
2116 printk("md: **********************************\n");
2117 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2118 printk("md: **********************************\n");
2119 for_each_mddev(mddev
, tmp
) {
2122 bitmap_print_sb(mddev
->bitmap
);
2124 printk("%s: ", mdname(mddev
));
2125 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
2126 printk("<%s>", bdevname(rdev
->bdev
,b
));
2129 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
2130 print_rdev(rdev
, mddev
->major_version
);
2132 printk("md: **********************************\n");
2137 static void sync_sbs(mddev_t
* mddev
, int nospares
)
2139 /* Update each superblock (in-memory image), but
2140 * if we are allowed to, skip spares which already
2141 * have the right event counter, or have one earlier
2142 * (which would mean they aren't being marked as dirty
2143 * with the rest of the array)
2146 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2147 if (rdev
->sb_events
== mddev
->events
||
2149 rdev
->raid_disk
< 0 &&
2150 rdev
->sb_events
+1 == mddev
->events
)) {
2151 /* Don't update this superblock */
2152 rdev
->sb_loaded
= 2;
2154 super_types
[mddev
->major_version
].
2155 sync_super(mddev
, rdev
);
2156 rdev
->sb_loaded
= 1;
2161 static void md_update_sb(mddev_t
* mddev
, int force_change
)
2168 /* First make sure individual recovery_offsets are correct */
2169 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2170 if (rdev
->raid_disk
>= 0 &&
2171 mddev
->delta_disks
>= 0 &&
2172 !test_bit(In_sync
, &rdev
->flags
) &&
2173 mddev
->curr_resync_completed
> rdev
->recovery_offset
)
2174 rdev
->recovery_offset
= mddev
->curr_resync_completed
;
2177 if (!mddev
->persistent
) {
2178 clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
2179 clear_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
2180 if (!mddev
->external
)
2181 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
2182 wake_up(&mddev
->sb_wait
);
2186 spin_lock_irq(&mddev
->write_lock
);
2188 mddev
->utime
= get_seconds();
2190 if (test_and_clear_bit(MD_CHANGE_DEVS
, &mddev
->flags
))
2192 if (test_and_clear_bit(MD_CHANGE_CLEAN
, &mddev
->flags
))
2193 /* just a clean<-> dirty transition, possibly leave spares alone,
2194 * though if events isn't the right even/odd, we will have to do
2200 if (mddev
->degraded
)
2201 /* If the array is degraded, then skipping spares is both
2202 * dangerous and fairly pointless.
2203 * Dangerous because a device that was removed from the array
2204 * might have a event_count that still looks up-to-date,
2205 * so it can be re-added without a resync.
2206 * Pointless because if there are any spares to skip,
2207 * then a recovery will happen and soon that array won't
2208 * be degraded any more and the spare can go back to sleep then.
2212 sync_req
= mddev
->in_sync
;
2214 /* If this is just a dirty<->clean transition, and the array is clean
2215 * and 'events' is odd, we can roll back to the previous clean state */
2217 && (mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
2218 && mddev
->can_decrease_events
2219 && mddev
->events
!= 1) {
2221 mddev
->can_decrease_events
= 0;
2223 /* otherwise we have to go forward and ... */
2225 mddev
->can_decrease_events
= nospares
;
2228 if (!mddev
->events
) {
2230 * oops, this 64-bit counter should never wrap.
2231 * Either we are in around ~1 trillion A.C., assuming
2232 * 1 reboot per second, or we have a bug:
2237 sync_sbs(mddev
, nospares
);
2238 spin_unlock_irq(&mddev
->write_lock
);
2241 "md: updating %s RAID superblock on device (in sync %d)\n",
2242 mdname(mddev
),mddev
->in_sync
);
2244 bitmap_update_sb(mddev
->bitmap
);
2245 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2246 char b
[BDEVNAME_SIZE
];
2247 dprintk(KERN_INFO
"md: ");
2248 if (rdev
->sb_loaded
!= 1)
2249 continue; /* no noise on spare devices */
2250 if (test_bit(Faulty
, &rdev
->flags
))
2251 dprintk("(skipping faulty ");
2253 dprintk("%s ", bdevname(rdev
->bdev
,b
));
2254 if (!test_bit(Faulty
, &rdev
->flags
)) {
2255 md_super_write(mddev
,rdev
,
2256 rdev
->sb_start
, rdev
->sb_size
,
2258 dprintk(KERN_INFO
"(write) %s's sb offset: %llu\n",
2259 bdevname(rdev
->bdev
,b
),
2260 (unsigned long long)rdev
->sb_start
);
2261 rdev
->sb_events
= mddev
->events
;
2265 if (mddev
->level
== LEVEL_MULTIPATH
)
2266 /* only need to write one superblock... */
2269 md_super_wait(mddev
);
2270 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
2272 spin_lock_irq(&mddev
->write_lock
);
2273 if (mddev
->in_sync
!= sync_req
||
2274 test_bit(MD_CHANGE_DEVS
, &mddev
->flags
)) {
2275 /* have to write it out again */
2276 spin_unlock_irq(&mddev
->write_lock
);
2279 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
2280 spin_unlock_irq(&mddev
->write_lock
);
2281 wake_up(&mddev
->sb_wait
);
2282 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
2283 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
2287 /* words written to sysfs files may, or may not, be \n terminated.
2288 * We want to accept with case. For this we use cmd_match.
2290 static int cmd_match(const char *cmd
, const char *str
)
2292 /* See if cmd, written into a sysfs file, matches
2293 * str. They must either be the same, or cmd can
2294 * have a trailing newline
2296 while (*cmd
&& *str
&& *cmd
== *str
) {
2307 struct rdev_sysfs_entry
{
2308 struct attribute attr
;
2309 ssize_t (*show
)(mdk_rdev_t
*, char *);
2310 ssize_t (*store
)(mdk_rdev_t
*, const char *, size_t);
2314 state_show(mdk_rdev_t
*rdev
, char *page
)
2319 if (test_bit(Faulty
, &rdev
->flags
)) {
2320 len
+= sprintf(page
+len
, "%sfaulty",sep
);
2323 if (test_bit(In_sync
, &rdev
->flags
)) {
2324 len
+= sprintf(page
+len
, "%sin_sync",sep
);
2327 if (test_bit(WriteMostly
, &rdev
->flags
)) {
2328 len
+= sprintf(page
+len
, "%swrite_mostly",sep
);
2331 if (test_bit(Blocked
, &rdev
->flags
)) {
2332 len
+= sprintf(page
+len
, "%sblocked", sep
);
2335 if (!test_bit(Faulty
, &rdev
->flags
) &&
2336 !test_bit(In_sync
, &rdev
->flags
)) {
2337 len
+= sprintf(page
+len
, "%sspare", sep
);
2340 return len
+sprintf(page
+len
, "\n");
2344 state_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2347 * faulty - simulates and error
2348 * remove - disconnects the device
2349 * writemostly - sets write_mostly
2350 * -writemostly - clears write_mostly
2351 * blocked - sets the Blocked flag
2352 * -blocked - clears the Blocked flag
2353 * insync - sets Insync providing device isn't active
2356 if (cmd_match(buf
, "faulty") && rdev
->mddev
->pers
) {
2357 md_error(rdev
->mddev
, rdev
);
2359 } else if (cmd_match(buf
, "remove")) {
2360 if (rdev
->raid_disk
>= 0)
2363 mddev_t
*mddev
= rdev
->mddev
;
2364 kick_rdev_from_array(rdev
);
2366 md_update_sb(mddev
, 1);
2367 md_new_event(mddev
);
2370 } else if (cmd_match(buf
, "writemostly")) {
2371 set_bit(WriteMostly
, &rdev
->flags
);
2373 } else if (cmd_match(buf
, "-writemostly")) {
2374 clear_bit(WriteMostly
, &rdev
->flags
);
2376 } else if (cmd_match(buf
, "blocked")) {
2377 set_bit(Blocked
, &rdev
->flags
);
2379 } else if (cmd_match(buf
, "-blocked")) {
2380 clear_bit(Blocked
, &rdev
->flags
);
2381 wake_up(&rdev
->blocked_wait
);
2382 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2383 md_wakeup_thread(rdev
->mddev
->thread
);
2386 } else if (cmd_match(buf
, "insync") && rdev
->raid_disk
== -1) {
2387 set_bit(In_sync
, &rdev
->flags
);
2391 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2392 return err
? err
: len
;
2394 static struct rdev_sysfs_entry rdev_state
=
2395 __ATTR(state
, S_IRUGO
|S_IWUSR
, state_show
, state_store
);
2398 errors_show(mdk_rdev_t
*rdev
, char *page
)
2400 return sprintf(page
, "%d\n", atomic_read(&rdev
->corrected_errors
));
2404 errors_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2407 unsigned long n
= simple_strtoul(buf
, &e
, 10);
2408 if (*buf
&& (*e
== 0 || *e
== '\n')) {
2409 atomic_set(&rdev
->corrected_errors
, n
);
2414 static struct rdev_sysfs_entry rdev_errors
=
2415 __ATTR(errors
, S_IRUGO
|S_IWUSR
, errors_show
, errors_store
);
2418 slot_show(mdk_rdev_t
*rdev
, char *page
)
2420 if (rdev
->raid_disk
< 0)
2421 return sprintf(page
, "none\n");
2423 return sprintf(page
, "%d\n", rdev
->raid_disk
);
2427 slot_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2432 int slot
= simple_strtoul(buf
, &e
, 10);
2433 if (strncmp(buf
, "none", 4)==0)
2435 else if (e
==buf
|| (*e
&& *e
!= '\n'))
2437 if (rdev
->mddev
->pers
&& slot
== -1) {
2438 /* Setting 'slot' on an active array requires also
2439 * updating the 'rd%d' link, and communicating
2440 * with the personality with ->hot_*_disk.
2441 * For now we only support removing
2442 * failed/spare devices. This normally happens automatically,
2443 * but not when the metadata is externally managed.
2445 if (rdev
->raid_disk
== -1)
2447 /* personality does all needed checks */
2448 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
2450 err
= rdev
->mddev
->pers
->
2451 hot_remove_disk(rdev
->mddev
, rdev
->raid_disk
);
2454 sprintf(nm
, "rd%d", rdev
->raid_disk
);
2455 sysfs_remove_link(&rdev
->mddev
->kobj
, nm
);
2456 rdev
->raid_disk
= -1;
2457 set_bit(MD_RECOVERY_NEEDED
, &rdev
->mddev
->recovery
);
2458 md_wakeup_thread(rdev
->mddev
->thread
);
2459 } else if (rdev
->mddev
->pers
) {
2461 /* Activating a spare .. or possibly reactivating
2462 * if we ever get bitmaps working here.
2465 if (rdev
->raid_disk
!= -1)
2468 if (rdev
->mddev
->pers
->hot_add_disk
== NULL
)
2471 list_for_each_entry(rdev2
, &rdev
->mddev
->disks
, same_set
)
2472 if (rdev2
->raid_disk
== slot
)
2475 if (slot
>= rdev
->mddev
->raid_disks
&&
2476 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
2479 rdev
->raid_disk
= slot
;
2480 if (test_bit(In_sync
, &rdev
->flags
))
2481 rdev
->saved_raid_disk
= slot
;
2483 rdev
->saved_raid_disk
= -1;
2484 err
= rdev
->mddev
->pers
->
2485 hot_add_disk(rdev
->mddev
, rdev
);
2487 rdev
->raid_disk
= -1;
2490 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2491 sprintf(nm
, "rd%d", rdev
->raid_disk
);
2492 if (sysfs_create_link(&rdev
->mddev
->kobj
, &rdev
->kobj
, nm
))
2493 /* failure here is OK */;
2494 /* don't wakeup anyone, leave that to userspace. */
2496 if (slot
>= rdev
->mddev
->raid_disks
&&
2497 slot
>= rdev
->mddev
->raid_disks
+ rdev
->mddev
->delta_disks
)
2499 rdev
->raid_disk
= slot
;
2500 /* assume it is working */
2501 clear_bit(Faulty
, &rdev
->flags
);
2502 clear_bit(WriteMostly
, &rdev
->flags
);
2503 set_bit(In_sync
, &rdev
->flags
);
2504 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
2510 static struct rdev_sysfs_entry rdev_slot
=
2511 __ATTR(slot
, S_IRUGO
|S_IWUSR
, slot_show
, slot_store
);
2514 offset_show(mdk_rdev_t
*rdev
, char *page
)
2516 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->data_offset
);
2520 offset_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2523 unsigned long long offset
= simple_strtoull(buf
, &e
, 10);
2524 if (e
==buf
|| (*e
&& *e
!= '\n'))
2526 if (rdev
->mddev
->pers
&& rdev
->raid_disk
>= 0)
2528 if (rdev
->sectors
&& rdev
->mddev
->external
)
2529 /* Must set offset before size, so overlap checks
2532 rdev
->data_offset
= offset
;
2536 static struct rdev_sysfs_entry rdev_offset
=
2537 __ATTR(offset
, S_IRUGO
|S_IWUSR
, offset_show
, offset_store
);
2540 rdev_size_show(mdk_rdev_t
*rdev
, char *page
)
2542 return sprintf(page
, "%llu\n", (unsigned long long)rdev
->sectors
/ 2);
2545 static int overlaps(sector_t s1
, sector_t l1
, sector_t s2
, sector_t l2
)
2547 /* check if two start/length pairs overlap */
2555 static int strict_blocks_to_sectors(const char *buf
, sector_t
*sectors
)
2557 unsigned long long blocks
;
2560 if (strict_strtoull(buf
, 10, &blocks
) < 0)
2563 if (blocks
& 1ULL << (8 * sizeof(blocks
) - 1))
2564 return -EINVAL
; /* sector conversion overflow */
2567 if (new != blocks
* 2)
2568 return -EINVAL
; /* unsigned long long to sector_t overflow */
2575 rdev_size_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2577 mddev_t
*my_mddev
= rdev
->mddev
;
2578 sector_t oldsectors
= rdev
->sectors
;
2581 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
2583 if (my_mddev
->pers
&& rdev
->raid_disk
>= 0) {
2584 if (my_mddev
->persistent
) {
2585 sectors
= super_types
[my_mddev
->major_version
].
2586 rdev_size_change(rdev
, sectors
);
2589 } else if (!sectors
)
2590 sectors
= (i_size_read(rdev
->bdev
->bd_inode
) >> 9) -
2593 if (sectors
< my_mddev
->dev_sectors
)
2594 return -EINVAL
; /* component must fit device */
2596 rdev
->sectors
= sectors
;
2597 if (sectors
> oldsectors
&& my_mddev
->external
) {
2598 /* need to check that all other rdevs with the same ->bdev
2599 * do not overlap. We need to unlock the mddev to avoid
2600 * a deadlock. We have already changed rdev->sectors, and if
2601 * we have to change it back, we will have the lock again.
2605 struct list_head
*tmp
;
2607 mddev_unlock(my_mddev
);
2608 for_each_mddev(mddev
, tmp
) {
2612 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
)
2613 if (test_bit(AllReserved
, &rdev2
->flags
) ||
2614 (rdev
->bdev
== rdev2
->bdev
&&
2616 overlaps(rdev
->data_offset
, rdev
->sectors
,
2622 mddev_unlock(mddev
);
2628 mddev_lock(my_mddev
);
2630 /* Someone else could have slipped in a size
2631 * change here, but doing so is just silly.
2632 * We put oldsectors back because we *know* it is
2633 * safe, and trust userspace not to race with
2636 rdev
->sectors
= oldsectors
;
2643 static struct rdev_sysfs_entry rdev_size
=
2644 __ATTR(size
, S_IRUGO
|S_IWUSR
, rdev_size_show
, rdev_size_store
);
2647 static ssize_t
recovery_start_show(mdk_rdev_t
*rdev
, char *page
)
2649 unsigned long long recovery_start
= rdev
->recovery_offset
;
2651 if (test_bit(In_sync
, &rdev
->flags
) ||
2652 recovery_start
== MaxSector
)
2653 return sprintf(page
, "none\n");
2655 return sprintf(page
, "%llu\n", recovery_start
);
2658 static ssize_t
recovery_start_store(mdk_rdev_t
*rdev
, const char *buf
, size_t len
)
2660 unsigned long long recovery_start
;
2662 if (cmd_match(buf
, "none"))
2663 recovery_start
= MaxSector
;
2664 else if (strict_strtoull(buf
, 10, &recovery_start
))
2667 if (rdev
->mddev
->pers
&&
2668 rdev
->raid_disk
>= 0)
2671 rdev
->recovery_offset
= recovery_start
;
2672 if (recovery_start
== MaxSector
)
2673 set_bit(In_sync
, &rdev
->flags
);
2675 clear_bit(In_sync
, &rdev
->flags
);
2679 static struct rdev_sysfs_entry rdev_recovery_start
=
2680 __ATTR(recovery_start
, S_IRUGO
|S_IWUSR
, recovery_start_show
, recovery_start_store
);
2682 static struct attribute
*rdev_default_attrs
[] = {
2688 &rdev_recovery_start
.attr
,
2692 rdev_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
2694 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
2695 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
2696 mddev_t
*mddev
= rdev
->mddev
;
2702 rv
= mddev
? mddev_lock(mddev
) : -EBUSY
;
2704 if (rdev
->mddev
== NULL
)
2707 rv
= entry
->show(rdev
, page
);
2708 mddev_unlock(mddev
);
2714 rdev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
2715 const char *page
, size_t length
)
2717 struct rdev_sysfs_entry
*entry
= container_of(attr
, struct rdev_sysfs_entry
, attr
);
2718 mdk_rdev_t
*rdev
= container_of(kobj
, mdk_rdev_t
, kobj
);
2720 mddev_t
*mddev
= rdev
->mddev
;
2724 if (!capable(CAP_SYS_ADMIN
))
2726 rv
= mddev
? mddev_lock(mddev
): -EBUSY
;
2728 if (rdev
->mddev
== NULL
)
2731 rv
= entry
->store(rdev
, page
, length
);
2732 mddev_unlock(mddev
);
2737 static void rdev_free(struct kobject
*ko
)
2739 mdk_rdev_t
*rdev
= container_of(ko
, mdk_rdev_t
, kobj
);
2742 static const struct sysfs_ops rdev_sysfs_ops
= {
2743 .show
= rdev_attr_show
,
2744 .store
= rdev_attr_store
,
2746 static struct kobj_type rdev_ktype
= {
2747 .release
= rdev_free
,
2748 .sysfs_ops
= &rdev_sysfs_ops
,
2749 .default_attrs
= rdev_default_attrs
,
2752 void md_rdev_init(mdk_rdev_t
*rdev
)
2755 rdev
->saved_raid_disk
= -1;
2756 rdev
->raid_disk
= -1;
2758 rdev
->data_offset
= 0;
2759 rdev
->sb_events
= 0;
2760 rdev
->last_read_error
.tv_sec
= 0;
2761 rdev
->last_read_error
.tv_nsec
= 0;
2762 atomic_set(&rdev
->nr_pending
, 0);
2763 atomic_set(&rdev
->read_errors
, 0);
2764 atomic_set(&rdev
->corrected_errors
, 0);
2766 INIT_LIST_HEAD(&rdev
->same_set
);
2767 init_waitqueue_head(&rdev
->blocked_wait
);
2769 EXPORT_SYMBOL_GPL(md_rdev_init
);
2771 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2773 * mark the device faulty if:
2775 * - the device is nonexistent (zero size)
2776 * - the device has no valid superblock
2778 * a faulty rdev _never_ has rdev->sb set.
2780 static mdk_rdev_t
*md_import_device(dev_t newdev
, int super_format
, int super_minor
)
2782 char b
[BDEVNAME_SIZE
];
2787 rdev
= kzalloc(sizeof(*rdev
), GFP_KERNEL
);
2789 printk(KERN_ERR
"md: could not alloc mem for new device!\n");
2790 return ERR_PTR(-ENOMEM
);
2794 if ((err
= alloc_disk_sb(rdev
)))
2797 err
= lock_rdev(rdev
, newdev
, super_format
== -2);
2801 kobject_init(&rdev
->kobj
, &rdev_ktype
);
2803 size
= i_size_read(rdev
->bdev
->bd_inode
) >> BLOCK_SIZE_BITS
;
2806 "md: %s has zero or unknown size, marking faulty!\n",
2807 bdevname(rdev
->bdev
,b
));
2812 if (super_format
>= 0) {
2813 err
= super_types
[super_format
].
2814 load_super(rdev
, NULL
, super_minor
);
2815 if (err
== -EINVAL
) {
2817 "md: %s does not have a valid v%d.%d "
2818 "superblock, not importing!\n",
2819 bdevname(rdev
->bdev
,b
),
2820 super_format
, super_minor
);
2825 "md: could not read %s's sb, not importing!\n",
2826 bdevname(rdev
->bdev
,b
));
2834 if (rdev
->sb_page
) {
2840 return ERR_PTR(err
);
2844 * Check a full RAID array for plausibility
2848 static void analyze_sbs(mddev_t
* mddev
)
2851 mdk_rdev_t
*rdev
, *freshest
, *tmp
;
2852 char b
[BDEVNAME_SIZE
];
2855 rdev_for_each(rdev
, tmp
, mddev
)
2856 switch (super_types
[mddev
->major_version
].
2857 load_super(rdev
, freshest
, mddev
->minor_version
)) {
2865 "md: fatal superblock inconsistency in %s"
2866 " -- removing from array\n",
2867 bdevname(rdev
->bdev
,b
));
2868 kick_rdev_from_array(rdev
);
2872 super_types
[mddev
->major_version
].
2873 validate_super(mddev
, freshest
);
2876 rdev_for_each(rdev
, tmp
, mddev
) {
2877 if (mddev
->max_disks
&&
2878 (rdev
->desc_nr
>= mddev
->max_disks
||
2879 i
> mddev
->max_disks
)) {
2881 "md: %s: %s: only %d devices permitted\n",
2882 mdname(mddev
), bdevname(rdev
->bdev
, b
),
2884 kick_rdev_from_array(rdev
);
2887 if (rdev
!= freshest
)
2888 if (super_types
[mddev
->major_version
].
2889 validate_super(mddev
, rdev
)) {
2890 printk(KERN_WARNING
"md: kicking non-fresh %s"
2892 bdevname(rdev
->bdev
,b
));
2893 kick_rdev_from_array(rdev
);
2896 if (mddev
->level
== LEVEL_MULTIPATH
) {
2897 rdev
->desc_nr
= i
++;
2898 rdev
->raid_disk
= rdev
->desc_nr
;
2899 set_bit(In_sync
, &rdev
->flags
);
2900 } else if (rdev
->raid_disk
>= (mddev
->raid_disks
- min(0, mddev
->delta_disks
))) {
2901 rdev
->raid_disk
= -1;
2902 clear_bit(In_sync
, &rdev
->flags
);
2907 /* Read a fixed-point number.
2908 * Numbers in sysfs attributes should be in "standard" units where
2909 * possible, so time should be in seconds.
2910 * However we internally use a a much smaller unit such as
2911 * milliseconds or jiffies.
2912 * This function takes a decimal number with a possible fractional
2913 * component, and produces an integer which is the result of
2914 * multiplying that number by 10^'scale'.
2915 * all without any floating-point arithmetic.
2917 int strict_strtoul_scaled(const char *cp
, unsigned long *res
, int scale
)
2919 unsigned long result
= 0;
2921 while (isdigit(*cp
) || (*cp
== '.' && decimals
< 0)) {
2924 else if (decimals
< scale
) {
2927 result
= result
* 10 + value
;
2939 while (decimals
< scale
) {
2948 static void md_safemode_timeout(unsigned long data
);
2951 safe_delay_show(mddev_t
*mddev
, char *page
)
2953 int msec
= (mddev
->safemode_delay
*1000)/HZ
;
2954 return sprintf(page
, "%d.%03d\n", msec
/1000, msec
%1000);
2957 safe_delay_store(mddev_t
*mddev
, const char *cbuf
, size_t len
)
2961 if (strict_strtoul_scaled(cbuf
, &msec
, 3) < 0)
2964 mddev
->safemode_delay
= 0;
2966 unsigned long old_delay
= mddev
->safemode_delay
;
2967 mddev
->safemode_delay
= (msec
*HZ
)/1000;
2968 if (mddev
->safemode_delay
== 0)
2969 mddev
->safemode_delay
= 1;
2970 if (mddev
->safemode_delay
< old_delay
)
2971 md_safemode_timeout((unsigned long)mddev
);
2975 static struct md_sysfs_entry md_safe_delay
=
2976 __ATTR(safe_mode_delay
, S_IRUGO
|S_IWUSR
,safe_delay_show
, safe_delay_store
);
2979 level_show(mddev_t
*mddev
, char *page
)
2981 struct mdk_personality
*p
= mddev
->pers
;
2983 return sprintf(page
, "%s\n", p
->name
);
2984 else if (mddev
->clevel
[0])
2985 return sprintf(page
, "%s\n", mddev
->clevel
);
2986 else if (mddev
->level
!= LEVEL_NONE
)
2987 return sprintf(page
, "%d\n", mddev
->level
);
2993 level_store(mddev_t
*mddev
, const char *buf
, size_t len
)
2997 struct mdk_personality
*pers
;
3002 if (mddev
->pers
== NULL
) {
3005 if (len
>= sizeof(mddev
->clevel
))
3007 strncpy(mddev
->clevel
, buf
, len
);
3008 if (mddev
->clevel
[len
-1] == '\n')
3010 mddev
->clevel
[len
] = 0;
3011 mddev
->level
= LEVEL_NONE
;
3015 /* request to change the personality. Need to ensure:
3016 * - array is not engaged in resync/recovery/reshape
3017 * - old personality can be suspended
3018 * - new personality will access other array.
3021 if (mddev
->sync_thread
||
3022 mddev
->reshape_position
!= MaxSector
||
3023 mddev
->sysfs_active
)
3026 if (!mddev
->pers
->quiesce
) {
3027 printk(KERN_WARNING
"md: %s: %s does not support online personality change\n",
3028 mdname(mddev
), mddev
->pers
->name
);
3032 /* Now find the new personality */
3033 if (len
== 0 || len
>= sizeof(clevel
))
3035 strncpy(clevel
, buf
, len
);
3036 if (clevel
[len
-1] == '\n')
3039 if (strict_strtol(clevel
, 10, &level
))
3042 if (request_module("md-%s", clevel
) != 0)
3043 request_module("md-level-%s", clevel
);
3044 spin_lock(&pers_lock
);
3045 pers
= find_pers(level
, clevel
);
3046 if (!pers
|| !try_module_get(pers
->owner
)) {
3047 spin_unlock(&pers_lock
);
3048 printk(KERN_WARNING
"md: personality %s not loaded\n", clevel
);
3051 spin_unlock(&pers_lock
);
3053 if (pers
== mddev
->pers
) {
3054 /* Nothing to do! */
3055 module_put(pers
->owner
);
3058 if (!pers
->takeover
) {
3059 module_put(pers
->owner
);
3060 printk(KERN_WARNING
"md: %s: %s does not support personality takeover\n",
3061 mdname(mddev
), clevel
);
3065 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
3066 rdev
->new_raid_disk
= rdev
->raid_disk
;
3068 /* ->takeover must set new_* and/or delta_disks
3069 * if it succeeds, and may set them when it fails.
3071 priv
= pers
->takeover(mddev
);
3073 mddev
->new_level
= mddev
->level
;
3074 mddev
->new_layout
= mddev
->layout
;
3075 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3076 mddev
->raid_disks
-= mddev
->delta_disks
;
3077 mddev
->delta_disks
= 0;
3078 module_put(pers
->owner
);
3079 printk(KERN_WARNING
"md: %s: %s would not accept array\n",
3080 mdname(mddev
), clevel
);
3081 return PTR_ERR(priv
);
3084 /* Looks like we have a winner */
3085 mddev_suspend(mddev
);
3086 mddev
->pers
->stop(mddev
);
3088 if (mddev
->pers
->sync_request
== NULL
&&
3089 pers
->sync_request
!= NULL
) {
3090 /* need to add the md_redundancy_group */
3091 if (sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
3093 "md: cannot register extra attributes for %s\n",
3095 mddev
->sysfs_action
= sysfs_get_dirent(mddev
->kobj
.sd
, NULL
, "sync_action");
3097 if (mddev
->pers
->sync_request
!= NULL
&&
3098 pers
->sync_request
== NULL
) {
3099 /* need to remove the md_redundancy_group */
3100 if (mddev
->to_remove
== NULL
)
3101 mddev
->to_remove
= &md_redundancy_group
;
3104 if (mddev
->pers
->sync_request
== NULL
&&
3106 /* We are converting from a no-redundancy array
3107 * to a redundancy array and metadata is managed
3108 * externally so we need to be sure that writes
3109 * won't block due to a need to transition
3111 * until external management is started.
3114 mddev
->safemode_delay
= 0;
3115 mddev
->safemode
= 0;
3118 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
3120 if (rdev
->raid_disk
< 0)
3122 if (rdev
->new_raid_disk
>= mddev
->raid_disks
)
3123 rdev
->new_raid_disk
= -1;
3124 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3126 sprintf(nm
, "rd%d", rdev
->raid_disk
);
3127 sysfs_remove_link(&mddev
->kobj
, nm
);
3129 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
3130 if (rdev
->raid_disk
< 0)
3132 if (rdev
->new_raid_disk
== rdev
->raid_disk
)
3134 rdev
->raid_disk
= rdev
->new_raid_disk
;
3135 if (rdev
->raid_disk
< 0)
3136 clear_bit(In_sync
, &rdev
->flags
);
3139 sprintf(nm
, "rd%d", rdev
->raid_disk
);
3140 if(sysfs_create_link(&mddev
->kobj
, &rdev
->kobj
, nm
))
3141 printk("md: cannot register %s for %s after level change\n",
3146 module_put(mddev
->pers
->owner
);
3148 mddev
->private = priv
;
3149 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
3150 mddev
->level
= mddev
->new_level
;
3151 mddev
->layout
= mddev
->new_layout
;
3152 mddev
->chunk_sectors
= mddev
->new_chunk_sectors
;
3153 mddev
->delta_disks
= 0;
3154 if (mddev
->pers
->sync_request
== NULL
) {
3155 /* this is now an array without redundancy, so
3156 * it must always be in_sync
3159 del_timer_sync(&mddev
->safemode_timer
);
3162 mddev_resume(mddev
);
3163 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
3164 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3165 md_wakeup_thread(mddev
->thread
);
3166 sysfs_notify(&mddev
->kobj
, NULL
, "level");
3167 md_new_event(mddev
);
3171 static struct md_sysfs_entry md_level
=
3172 __ATTR(level
, S_IRUGO
|S_IWUSR
, level_show
, level_store
);
3176 layout_show(mddev_t
*mddev
, char *page
)
3178 /* just a number, not meaningful for all levels */
3179 if (mddev
->reshape_position
!= MaxSector
&&
3180 mddev
->layout
!= mddev
->new_layout
)
3181 return sprintf(page
, "%d (%d)\n",
3182 mddev
->new_layout
, mddev
->layout
);
3183 return sprintf(page
, "%d\n", mddev
->layout
);
3187 layout_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3190 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3192 if (!*buf
|| (*e
&& *e
!= '\n'))
3197 if (mddev
->pers
->check_reshape
== NULL
)
3199 mddev
->new_layout
= n
;
3200 err
= mddev
->pers
->check_reshape(mddev
);
3202 mddev
->new_layout
= mddev
->layout
;
3206 mddev
->new_layout
= n
;
3207 if (mddev
->reshape_position
== MaxSector
)
3212 static struct md_sysfs_entry md_layout
=
3213 __ATTR(layout
, S_IRUGO
|S_IWUSR
, layout_show
, layout_store
);
3217 raid_disks_show(mddev_t
*mddev
, char *page
)
3219 if (mddev
->raid_disks
== 0)
3221 if (mddev
->reshape_position
!= MaxSector
&&
3222 mddev
->delta_disks
!= 0)
3223 return sprintf(page
, "%d (%d)\n", mddev
->raid_disks
,
3224 mddev
->raid_disks
- mddev
->delta_disks
);
3225 return sprintf(page
, "%d\n", mddev
->raid_disks
);
3228 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
);
3231 raid_disks_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3235 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3237 if (!*buf
|| (*e
&& *e
!= '\n'))
3241 rv
= update_raid_disks(mddev
, n
);
3242 else if (mddev
->reshape_position
!= MaxSector
) {
3243 int olddisks
= mddev
->raid_disks
- mddev
->delta_disks
;
3244 mddev
->delta_disks
= n
- olddisks
;
3245 mddev
->raid_disks
= n
;
3247 mddev
->raid_disks
= n
;
3248 return rv
? rv
: len
;
3250 static struct md_sysfs_entry md_raid_disks
=
3251 __ATTR(raid_disks
, S_IRUGO
|S_IWUSR
, raid_disks_show
, raid_disks_store
);
3254 chunk_size_show(mddev_t
*mddev
, char *page
)
3256 if (mddev
->reshape_position
!= MaxSector
&&
3257 mddev
->chunk_sectors
!= mddev
->new_chunk_sectors
)
3258 return sprintf(page
, "%d (%d)\n",
3259 mddev
->new_chunk_sectors
<< 9,
3260 mddev
->chunk_sectors
<< 9);
3261 return sprintf(page
, "%d\n", mddev
->chunk_sectors
<< 9);
3265 chunk_size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3268 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3270 if (!*buf
|| (*e
&& *e
!= '\n'))
3275 if (mddev
->pers
->check_reshape
== NULL
)
3277 mddev
->new_chunk_sectors
= n
>> 9;
3278 err
= mddev
->pers
->check_reshape(mddev
);
3280 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3284 mddev
->new_chunk_sectors
= n
>> 9;
3285 if (mddev
->reshape_position
== MaxSector
)
3286 mddev
->chunk_sectors
= n
>> 9;
3290 static struct md_sysfs_entry md_chunk_size
=
3291 __ATTR(chunk_size
, S_IRUGO
|S_IWUSR
, chunk_size_show
, chunk_size_store
);
3294 resync_start_show(mddev_t
*mddev
, char *page
)
3296 if (mddev
->recovery_cp
== MaxSector
)
3297 return sprintf(page
, "none\n");
3298 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->recovery_cp
);
3302 resync_start_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3305 unsigned long long n
= simple_strtoull(buf
, &e
, 10);
3309 if (cmd_match(buf
, "none"))
3311 else if (!*buf
|| (*e
&& *e
!= '\n'))
3314 mddev
->recovery_cp
= n
;
3317 static struct md_sysfs_entry md_resync_start
=
3318 __ATTR(resync_start
, S_IRUGO
|S_IWUSR
, resync_start_show
, resync_start_store
);
3321 * The array state can be:
3324 * No devices, no size, no level
3325 * Equivalent to STOP_ARRAY ioctl
3327 * May have some settings, but array is not active
3328 * all IO results in error
3329 * When written, doesn't tear down array, but just stops it
3330 * suspended (not supported yet)
3331 * All IO requests will block. The array can be reconfigured.
3332 * Writing this, if accepted, will block until array is quiescent
3334 * no resync can happen. no superblocks get written.
3335 * write requests fail
3337 * like readonly, but behaves like 'clean' on a write request.
3339 * clean - no pending writes, but otherwise active.
3340 * When written to inactive array, starts without resync
3341 * If a write request arrives then
3342 * if metadata is known, mark 'dirty' and switch to 'active'.
3343 * if not known, block and switch to write-pending
3344 * If written to an active array that has pending writes, then fails.
3346 * fully active: IO and resync can be happening.
3347 * When written to inactive array, starts with resync
3350 * clean, but writes are blocked waiting for 'active' to be written.
3353 * like active, but no writes have been seen for a while (100msec).
3356 enum array_state
{ clear
, inactive
, suspended
, readonly
, read_auto
, clean
, active
,
3357 write_pending
, active_idle
, bad_word
};
3358 static char *array_states
[] = {
3359 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3360 "write-pending", "active-idle", NULL
};
3362 static int match_word(const char *word
, char **list
)
3365 for (n
=0; list
[n
]; n
++)
3366 if (cmd_match(word
, list
[n
]))
3372 array_state_show(mddev_t
*mddev
, char *page
)
3374 enum array_state st
= inactive
;
3387 else if (test_bit(MD_CHANGE_PENDING
, &mddev
->flags
))
3389 else if (mddev
->safemode
)
3395 if (list_empty(&mddev
->disks
) &&
3396 mddev
->raid_disks
== 0 &&
3397 mddev
->dev_sectors
== 0)
3402 return sprintf(page
, "%s\n", array_states
[st
]);
3405 static int do_md_stop(mddev_t
* mddev
, int ro
, int is_open
);
3406 static int md_set_readonly(mddev_t
* mddev
, int is_open
);
3407 static int do_md_run(mddev_t
* mddev
);
3408 static int restart_array(mddev_t
*mddev
);
3411 array_state_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3414 enum array_state st
= match_word(buf
, array_states
);
3419 /* stopping an active array */
3420 if (atomic_read(&mddev
->openers
) > 0)
3422 err
= do_md_stop(mddev
, 0, 0);
3425 /* stopping an active array */
3427 if (atomic_read(&mddev
->openers
) > 0)
3429 err
= do_md_stop(mddev
, 2, 0);
3431 err
= 0; /* already inactive */
3434 break; /* not supported yet */
3437 err
= md_set_readonly(mddev
, 0);
3440 set_disk_ro(mddev
->gendisk
, 1);
3441 err
= do_md_run(mddev
);
3447 err
= md_set_readonly(mddev
, 0);
3448 else if (mddev
->ro
== 1)
3449 err
= restart_array(mddev
);
3452 set_disk_ro(mddev
->gendisk
, 0);
3456 err
= do_md_run(mddev
);
3461 restart_array(mddev
);
3462 spin_lock_irq(&mddev
->write_lock
);
3463 if (atomic_read(&mddev
->writes_pending
) == 0) {
3464 if (mddev
->in_sync
== 0) {
3466 if (mddev
->safemode
== 1)
3467 mddev
->safemode
= 0;
3468 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
3473 spin_unlock_irq(&mddev
->write_lock
);
3479 restart_array(mddev
);
3480 clear_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
3481 wake_up(&mddev
->sb_wait
);
3485 set_disk_ro(mddev
->gendisk
, 0);
3486 err
= do_md_run(mddev
);
3491 /* these cannot be set */
3497 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
3501 static struct md_sysfs_entry md_array_state
=
3502 __ATTR(array_state
, S_IRUGO
|S_IWUSR
, array_state_show
, array_state_store
);
3505 max_corrected_read_errors_show(mddev_t
*mddev
, char *page
) {
3506 return sprintf(page
, "%d\n",
3507 atomic_read(&mddev
->max_corr_read_errors
));
3511 max_corrected_read_errors_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3514 unsigned long n
= simple_strtoul(buf
, &e
, 10);
3516 if (*buf
&& (*e
== 0 || *e
== '\n')) {
3517 atomic_set(&mddev
->max_corr_read_errors
, n
);
3523 static struct md_sysfs_entry max_corr_read_errors
=
3524 __ATTR(max_read_errors
, S_IRUGO
|S_IWUSR
, max_corrected_read_errors_show
,
3525 max_corrected_read_errors_store
);
3528 null_show(mddev_t
*mddev
, char *page
)
3534 new_dev_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3536 /* buf must be %d:%d\n? giving major and minor numbers */
3537 /* The new device is added to the array.
3538 * If the array has a persistent superblock, we read the
3539 * superblock to initialise info and check validity.
3540 * Otherwise, only checking done is that in bind_rdev_to_array,
3541 * which mainly checks size.
3544 int major
= simple_strtoul(buf
, &e
, 10);
3550 if (!*buf
|| *e
!= ':' || !e
[1] || e
[1] == '\n')
3552 minor
= simple_strtoul(e
+1, &e
, 10);
3553 if (*e
&& *e
!= '\n')
3555 dev
= MKDEV(major
, minor
);
3556 if (major
!= MAJOR(dev
) ||
3557 minor
!= MINOR(dev
))
3561 if (mddev
->persistent
) {
3562 rdev
= md_import_device(dev
, mddev
->major_version
,
3563 mddev
->minor_version
);
3564 if (!IS_ERR(rdev
) && !list_empty(&mddev
->disks
)) {
3565 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
3566 mdk_rdev_t
, same_set
);
3567 err
= super_types
[mddev
->major_version
]
3568 .load_super(rdev
, rdev0
, mddev
->minor_version
);
3572 } else if (mddev
->external
)
3573 rdev
= md_import_device(dev
, -2, -1);
3575 rdev
= md_import_device(dev
, -1, -1);
3578 return PTR_ERR(rdev
);
3579 err
= bind_rdev_to_array(rdev
, mddev
);
3583 return err
? err
: len
;
3586 static struct md_sysfs_entry md_new_device
=
3587 __ATTR(new_dev
, S_IWUSR
, null_show
, new_dev_store
);
3590 bitmap_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3593 unsigned long chunk
, end_chunk
;
3597 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3599 chunk
= end_chunk
= simple_strtoul(buf
, &end
, 0);
3600 if (buf
== end
) break;
3601 if (*end
== '-') { /* range */
3603 end_chunk
= simple_strtoul(buf
, &end
, 0);
3604 if (buf
== end
) break;
3606 if (*end
&& !isspace(*end
)) break;
3607 bitmap_dirty_bits(mddev
->bitmap
, chunk
, end_chunk
);
3608 buf
= skip_spaces(end
);
3610 bitmap_unplug(mddev
->bitmap
); /* flush the bits to disk */
3615 static struct md_sysfs_entry md_bitmap
=
3616 __ATTR(bitmap_set_bits
, S_IWUSR
, null_show
, bitmap_store
);
3619 size_show(mddev_t
*mddev
, char *page
)
3621 return sprintf(page
, "%llu\n",
3622 (unsigned long long)mddev
->dev_sectors
/ 2);
3625 static int update_size(mddev_t
*mddev
, sector_t num_sectors
);
3628 size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3630 /* If array is inactive, we can reduce the component size, but
3631 * not increase it (except from 0).
3632 * If array is active, we can try an on-line resize
3635 int err
= strict_blocks_to_sectors(buf
, §ors
);
3640 err
= update_size(mddev
, sectors
);
3641 md_update_sb(mddev
, 1);
3643 if (mddev
->dev_sectors
== 0 ||
3644 mddev
->dev_sectors
> sectors
)
3645 mddev
->dev_sectors
= sectors
;
3649 return err
? err
: len
;
3652 static struct md_sysfs_entry md_size
=
3653 __ATTR(component_size
, S_IRUGO
|S_IWUSR
, size_show
, size_store
);
3658 * 'none' for arrays with no metadata (good luck...)
3659 * 'external' for arrays with externally managed metadata,
3660 * or N.M for internally known formats
3663 metadata_show(mddev_t
*mddev
, char *page
)
3665 if (mddev
->persistent
)
3666 return sprintf(page
, "%d.%d\n",
3667 mddev
->major_version
, mddev
->minor_version
);
3668 else if (mddev
->external
)
3669 return sprintf(page
, "external:%s\n", mddev
->metadata_type
);
3671 return sprintf(page
, "none\n");
3675 metadata_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3679 /* Changing the details of 'external' metadata is
3680 * always permitted. Otherwise there must be
3681 * no devices attached to the array.
3683 if (mddev
->external
&& strncmp(buf
, "external:", 9) == 0)
3685 else if (!list_empty(&mddev
->disks
))
3688 if (cmd_match(buf
, "none")) {
3689 mddev
->persistent
= 0;
3690 mddev
->external
= 0;
3691 mddev
->major_version
= 0;
3692 mddev
->minor_version
= 90;
3695 if (strncmp(buf
, "external:", 9) == 0) {
3696 size_t namelen
= len
-9;
3697 if (namelen
>= sizeof(mddev
->metadata_type
))
3698 namelen
= sizeof(mddev
->metadata_type
)-1;
3699 strncpy(mddev
->metadata_type
, buf
+9, namelen
);
3700 mddev
->metadata_type
[namelen
] = 0;
3701 if (namelen
&& mddev
->metadata_type
[namelen
-1] == '\n')
3702 mddev
->metadata_type
[--namelen
] = 0;
3703 mddev
->persistent
= 0;
3704 mddev
->external
= 1;
3705 mddev
->major_version
= 0;
3706 mddev
->minor_version
= 90;
3709 major
= simple_strtoul(buf
, &e
, 10);
3710 if (e
==buf
|| *e
!= '.')
3713 minor
= simple_strtoul(buf
, &e
, 10);
3714 if (e
==buf
|| (*e
&& *e
!= '\n') )
3716 if (major
>= ARRAY_SIZE(super_types
) || super_types
[major
].name
== NULL
)
3718 mddev
->major_version
= major
;
3719 mddev
->minor_version
= minor
;
3720 mddev
->persistent
= 1;
3721 mddev
->external
= 0;
3725 static struct md_sysfs_entry md_metadata
=
3726 __ATTR(metadata_version
, S_IRUGO
|S_IWUSR
, metadata_show
, metadata_store
);
3729 action_show(mddev_t
*mddev
, char *page
)
3731 char *type
= "idle";
3732 if (test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
3734 else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
3735 (!mddev
->ro
&& test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))) {
3736 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
3738 else if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
3739 if (!test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
3741 else if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
3745 } else if (test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
))
3748 return sprintf(page
, "%s\n", type
);
3751 static void reap_sync_thread(mddev_t
*mddev
);
3754 action_store(mddev_t
*mddev
, const char *page
, size_t len
)
3756 if (!mddev
->pers
|| !mddev
->pers
->sync_request
)
3759 if (cmd_match(page
, "frozen"))
3760 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
3762 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
3764 if (cmd_match(page
, "idle") || cmd_match(page
, "frozen")) {
3765 if (mddev
->sync_thread
) {
3766 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
3767 reap_sync_thread(mddev
);
3769 } else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
3770 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
3772 else if (cmd_match(page
, "resync"))
3773 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3774 else if (cmd_match(page
, "recover")) {
3775 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
3776 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3777 } else if (cmd_match(page
, "reshape")) {
3779 if (mddev
->pers
->start_reshape
== NULL
)
3781 err
= mddev
->pers
->start_reshape(mddev
);
3784 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
3786 if (cmd_match(page
, "check"))
3787 set_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
3788 else if (!cmd_match(page
, "repair"))
3790 set_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
3791 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
3793 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3794 md_wakeup_thread(mddev
->thread
);
3795 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
3800 mismatch_cnt_show(mddev_t
*mddev
, char *page
)
3802 return sprintf(page
, "%llu\n",
3803 (unsigned long long) mddev
->resync_mismatches
);
3806 static struct md_sysfs_entry md_scan_mode
=
3807 __ATTR(sync_action
, S_IRUGO
|S_IWUSR
, action_show
, action_store
);
3810 static struct md_sysfs_entry md_mismatches
= __ATTR_RO(mismatch_cnt
);
3813 sync_min_show(mddev_t
*mddev
, char *page
)
3815 return sprintf(page
, "%d (%s)\n", speed_min(mddev
),
3816 mddev
->sync_speed_min
? "local": "system");
3820 sync_min_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3824 if (strncmp(buf
, "system", 6)==0) {
3825 mddev
->sync_speed_min
= 0;
3828 min
= simple_strtoul(buf
, &e
, 10);
3829 if (buf
== e
|| (*e
&& *e
!= '\n') || min
<= 0)
3831 mddev
->sync_speed_min
= min
;
3835 static struct md_sysfs_entry md_sync_min
=
3836 __ATTR(sync_speed_min
, S_IRUGO
|S_IWUSR
, sync_min_show
, sync_min_store
);
3839 sync_max_show(mddev_t
*mddev
, char *page
)
3841 return sprintf(page
, "%d (%s)\n", speed_max(mddev
),
3842 mddev
->sync_speed_max
? "local": "system");
3846 sync_max_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3850 if (strncmp(buf
, "system", 6)==0) {
3851 mddev
->sync_speed_max
= 0;
3854 max
= simple_strtoul(buf
, &e
, 10);
3855 if (buf
== e
|| (*e
&& *e
!= '\n') || max
<= 0)
3857 mddev
->sync_speed_max
= max
;
3861 static struct md_sysfs_entry md_sync_max
=
3862 __ATTR(sync_speed_max
, S_IRUGO
|S_IWUSR
, sync_max_show
, sync_max_store
);
3865 degraded_show(mddev_t
*mddev
, char *page
)
3867 return sprintf(page
, "%d\n", mddev
->degraded
);
3869 static struct md_sysfs_entry md_degraded
= __ATTR_RO(degraded
);
3872 sync_force_parallel_show(mddev_t
*mddev
, char *page
)
3874 return sprintf(page
, "%d\n", mddev
->parallel_resync
);
3878 sync_force_parallel_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3882 if (strict_strtol(buf
, 10, &n
))
3885 if (n
!= 0 && n
!= 1)
3888 mddev
->parallel_resync
= n
;
3890 if (mddev
->sync_thread
)
3891 wake_up(&resync_wait
);
3896 /* force parallel resync, even with shared block devices */
3897 static struct md_sysfs_entry md_sync_force_parallel
=
3898 __ATTR(sync_force_parallel
, S_IRUGO
|S_IWUSR
,
3899 sync_force_parallel_show
, sync_force_parallel_store
);
3902 sync_speed_show(mddev_t
*mddev
, char *page
)
3904 unsigned long resync
, dt
, db
;
3905 if (mddev
->curr_resync
== 0)
3906 return sprintf(page
, "none\n");
3907 resync
= mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
);
3908 dt
= (jiffies
- mddev
->resync_mark
) / HZ
;
3910 db
= resync
- mddev
->resync_mark_cnt
;
3911 return sprintf(page
, "%lu\n", db
/dt
/2); /* K/sec */
3914 static struct md_sysfs_entry md_sync_speed
= __ATTR_RO(sync_speed
);
3917 sync_completed_show(mddev_t
*mddev
, char *page
)
3919 unsigned long long max_sectors
, resync
;
3921 if (!test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3922 return sprintf(page
, "none\n");
3924 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
3925 max_sectors
= mddev
->resync_max_sectors
;
3927 max_sectors
= mddev
->dev_sectors
;
3929 resync
= mddev
->curr_resync_completed
;
3930 return sprintf(page
, "%llu / %llu\n", resync
, max_sectors
);
3933 static struct md_sysfs_entry md_sync_completed
= __ATTR_RO(sync_completed
);
3936 min_sync_show(mddev_t
*mddev
, char *page
)
3938 return sprintf(page
, "%llu\n",
3939 (unsigned long long)mddev
->resync_min
);
3942 min_sync_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3944 unsigned long long min
;
3945 if (strict_strtoull(buf
, 10, &min
))
3947 if (min
> mddev
->resync_max
)
3949 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3952 /* Must be a multiple of chunk_size */
3953 if (mddev
->chunk_sectors
) {
3954 sector_t temp
= min
;
3955 if (sector_div(temp
, mddev
->chunk_sectors
))
3958 mddev
->resync_min
= min
;
3963 static struct md_sysfs_entry md_min_sync
=
3964 __ATTR(sync_min
, S_IRUGO
|S_IWUSR
, min_sync_show
, min_sync_store
);
3967 max_sync_show(mddev_t
*mddev
, char *page
)
3969 if (mddev
->resync_max
== MaxSector
)
3970 return sprintf(page
, "max\n");
3972 return sprintf(page
, "%llu\n",
3973 (unsigned long long)mddev
->resync_max
);
3976 max_sync_store(mddev_t
*mddev
, const char *buf
, size_t len
)
3978 if (strncmp(buf
, "max", 3) == 0)
3979 mddev
->resync_max
= MaxSector
;
3981 unsigned long long max
;
3982 if (strict_strtoull(buf
, 10, &max
))
3984 if (max
< mddev
->resync_min
)
3986 if (max
< mddev
->resync_max
&&
3988 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3991 /* Must be a multiple of chunk_size */
3992 if (mddev
->chunk_sectors
) {
3993 sector_t temp
= max
;
3994 if (sector_div(temp
, mddev
->chunk_sectors
))
3997 mddev
->resync_max
= max
;
3999 wake_up(&mddev
->recovery_wait
);
4003 static struct md_sysfs_entry md_max_sync
=
4004 __ATTR(sync_max
, S_IRUGO
|S_IWUSR
, max_sync_show
, max_sync_store
);
4007 suspend_lo_show(mddev_t
*mddev
, char *page
)
4009 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_lo
);
4013 suspend_lo_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4016 unsigned long long new = simple_strtoull(buf
, &e
, 10);
4017 unsigned long long old
= mddev
->suspend_lo
;
4019 if (mddev
->pers
== NULL
||
4020 mddev
->pers
->quiesce
== NULL
)
4022 if (buf
== e
|| (*e
&& *e
!= '\n'))
4025 mddev
->suspend_lo
= new;
4027 /* Shrinking suspended region */
4028 mddev
->pers
->quiesce(mddev
, 2);
4030 /* Expanding suspended region - need to wait */
4031 mddev
->pers
->quiesce(mddev
, 1);
4032 mddev
->pers
->quiesce(mddev
, 0);
4036 static struct md_sysfs_entry md_suspend_lo
=
4037 __ATTR(suspend_lo
, S_IRUGO
|S_IWUSR
, suspend_lo_show
, suspend_lo_store
);
4041 suspend_hi_show(mddev_t
*mddev
, char *page
)
4043 return sprintf(page
, "%llu\n", (unsigned long long)mddev
->suspend_hi
);
4047 suspend_hi_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4050 unsigned long long new = simple_strtoull(buf
, &e
, 10);
4051 unsigned long long old
= mddev
->suspend_hi
;
4053 if (mddev
->pers
== NULL
||
4054 mddev
->pers
->quiesce
== NULL
)
4056 if (buf
== e
|| (*e
&& *e
!= '\n'))
4059 mddev
->suspend_hi
= new;
4061 /* Shrinking suspended region */
4062 mddev
->pers
->quiesce(mddev
, 2);
4064 /* Expanding suspended region - need to wait */
4065 mddev
->pers
->quiesce(mddev
, 1);
4066 mddev
->pers
->quiesce(mddev
, 0);
4070 static struct md_sysfs_entry md_suspend_hi
=
4071 __ATTR(suspend_hi
, S_IRUGO
|S_IWUSR
, suspend_hi_show
, suspend_hi_store
);
4074 reshape_position_show(mddev_t
*mddev
, char *page
)
4076 if (mddev
->reshape_position
!= MaxSector
)
4077 return sprintf(page
, "%llu\n",
4078 (unsigned long long)mddev
->reshape_position
);
4079 strcpy(page
, "none\n");
4084 reshape_position_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4087 unsigned long long new = simple_strtoull(buf
, &e
, 10);
4090 if (buf
== e
|| (*e
&& *e
!= '\n'))
4092 mddev
->reshape_position
= new;
4093 mddev
->delta_disks
= 0;
4094 mddev
->new_level
= mddev
->level
;
4095 mddev
->new_layout
= mddev
->layout
;
4096 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
4100 static struct md_sysfs_entry md_reshape_position
=
4101 __ATTR(reshape_position
, S_IRUGO
|S_IWUSR
, reshape_position_show
,
4102 reshape_position_store
);
4105 array_size_show(mddev_t
*mddev
, char *page
)
4107 if (mddev
->external_size
)
4108 return sprintf(page
, "%llu\n",
4109 (unsigned long long)mddev
->array_sectors
/2);
4111 return sprintf(page
, "default\n");
4115 array_size_store(mddev_t
*mddev
, const char *buf
, size_t len
)
4119 if (strncmp(buf
, "default", 7) == 0) {
4121 sectors
= mddev
->pers
->size(mddev
, 0, 0);
4123 sectors
= mddev
->array_sectors
;
4125 mddev
->external_size
= 0;
4127 if (strict_blocks_to_sectors(buf
, §ors
) < 0)
4129 if (mddev
->pers
&& mddev
->pers
->size(mddev
, 0, 0) < sectors
)
4132 mddev
->external_size
= 1;
4135 mddev
->array_sectors
= sectors
;
4136 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
4138 revalidate_disk(mddev
->gendisk
);
4143 static struct md_sysfs_entry md_array_size
=
4144 __ATTR(array_size
, S_IRUGO
|S_IWUSR
, array_size_show
,
4147 static struct attribute
*md_default_attrs
[] = {
4150 &md_raid_disks
.attr
,
4151 &md_chunk_size
.attr
,
4153 &md_resync_start
.attr
,
4155 &md_new_device
.attr
,
4156 &md_safe_delay
.attr
,
4157 &md_array_state
.attr
,
4158 &md_reshape_position
.attr
,
4159 &md_array_size
.attr
,
4160 &max_corr_read_errors
.attr
,
4164 static struct attribute
*md_redundancy_attrs
[] = {
4166 &md_mismatches
.attr
,
4169 &md_sync_speed
.attr
,
4170 &md_sync_force_parallel
.attr
,
4171 &md_sync_completed
.attr
,
4174 &md_suspend_lo
.attr
,
4175 &md_suspend_hi
.attr
,
4180 static struct attribute_group md_redundancy_group
= {
4182 .attrs
= md_redundancy_attrs
,
4187 md_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
4189 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
4190 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
4195 rv
= mddev_lock(mddev
);
4197 rv
= entry
->show(mddev
, page
);
4198 mddev_unlock(mddev
);
4204 md_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
4205 const char *page
, size_t length
)
4207 struct md_sysfs_entry
*entry
= container_of(attr
, struct md_sysfs_entry
, attr
);
4208 mddev_t
*mddev
= container_of(kobj
, struct mddev_s
, kobj
);
4213 if (!capable(CAP_SYS_ADMIN
))
4215 rv
= mddev_lock(mddev
);
4216 if (mddev
->hold_active
== UNTIL_IOCTL
)
4217 mddev
->hold_active
= 0;
4219 rv
= entry
->store(mddev
, page
, length
);
4220 mddev_unlock(mddev
);
4225 static void md_free(struct kobject
*ko
)
4227 mddev_t
*mddev
= container_of(ko
, mddev_t
, kobj
);
4229 if (mddev
->sysfs_state
)
4230 sysfs_put(mddev
->sysfs_state
);
4232 if (mddev
->gendisk
) {
4233 del_gendisk(mddev
->gendisk
);
4234 put_disk(mddev
->gendisk
);
4237 blk_cleanup_queue(mddev
->queue
);
4242 static const struct sysfs_ops md_sysfs_ops
= {
4243 .show
= md_attr_show
,
4244 .store
= md_attr_store
,
4246 static struct kobj_type md_ktype
= {
4248 .sysfs_ops
= &md_sysfs_ops
,
4249 .default_attrs
= md_default_attrs
,
4254 static void mddev_delayed_delete(struct work_struct
*ws
)
4256 mddev_t
*mddev
= container_of(ws
, mddev_t
, del_work
);
4258 sysfs_remove_group(&mddev
->kobj
, &md_bitmap_group
);
4259 kobject_del(&mddev
->kobj
);
4260 kobject_put(&mddev
->kobj
);
4263 static int md_alloc(dev_t dev
, char *name
)
4265 static DEFINE_MUTEX(disks_mutex
);
4266 mddev_t
*mddev
= mddev_find(dev
);
4267 struct gendisk
*disk
;
4276 partitioned
= (MAJOR(mddev
->unit
) != MD_MAJOR
);
4277 shift
= partitioned
? MdpMinorShift
: 0;
4278 unit
= MINOR(mddev
->unit
) >> shift
;
4280 /* wait for any previous instance of this device to be
4281 * completely removed (mddev_delayed_delete).
4283 flush_workqueue(md_misc_wq
);
4285 mutex_lock(&disks_mutex
);
4291 /* Need to ensure that 'name' is not a duplicate.
4294 spin_lock(&all_mddevs_lock
);
4296 list_for_each_entry(mddev2
, &all_mddevs
, all_mddevs
)
4297 if (mddev2
->gendisk
&&
4298 strcmp(mddev2
->gendisk
->disk_name
, name
) == 0) {
4299 spin_unlock(&all_mddevs_lock
);
4302 spin_unlock(&all_mddevs_lock
);
4306 mddev
->queue
= blk_alloc_queue(GFP_KERNEL
);
4309 mddev
->queue
->queuedata
= mddev
;
4311 blk_queue_make_request(mddev
->queue
, md_make_request
);
4313 disk
= alloc_disk(1 << shift
);
4315 blk_cleanup_queue(mddev
->queue
);
4316 mddev
->queue
= NULL
;
4319 disk
->major
= MAJOR(mddev
->unit
);
4320 disk
->first_minor
= unit
<< shift
;
4322 strcpy(disk
->disk_name
, name
);
4323 else if (partitioned
)
4324 sprintf(disk
->disk_name
, "md_d%d", unit
);
4326 sprintf(disk
->disk_name
, "md%d", unit
);
4327 disk
->fops
= &md_fops
;
4328 disk
->private_data
= mddev
;
4329 disk
->queue
= mddev
->queue
;
4330 /* Allow extended partitions. This makes the
4331 * 'mdp' device redundant, but we can't really
4334 disk
->flags
|= GENHD_FL_EXT_DEVT
;
4336 mddev
->gendisk
= disk
;
4337 error
= kobject_init_and_add(&mddev
->kobj
, &md_ktype
,
4338 &disk_to_dev(disk
)->kobj
, "%s", "md");
4340 /* This isn't possible, but as kobject_init_and_add is marked
4341 * __must_check, we must do something with the result
4343 printk(KERN_WARNING
"md: cannot register %s/md - name in use\n",
4347 if (mddev
->kobj
.sd
&&
4348 sysfs_create_group(&mddev
->kobj
, &md_bitmap_group
))
4349 printk(KERN_DEBUG
"pointless warning\n");
4351 blk_queue_flush(mddev
->queue
, REQ_FLUSH
| REQ_FUA
);
4353 mutex_unlock(&disks_mutex
);
4354 if (!error
&& mddev
->kobj
.sd
) {
4355 kobject_uevent(&mddev
->kobj
, KOBJ_ADD
);
4356 mddev
->sysfs_state
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "array_state");
4362 static struct kobject
*md_probe(dev_t dev
, int *part
, void *data
)
4364 md_alloc(dev
, NULL
);
4368 static int add_named_array(const char *val
, struct kernel_param
*kp
)
4370 /* val must be "md_*" where * is not all digits.
4371 * We allocate an array with a large free minor number, and
4372 * set the name to val. val must not already be an active name.
4374 int len
= strlen(val
);
4375 char buf
[DISK_NAME_LEN
];
4377 while (len
&& val
[len
-1] == '\n')
4379 if (len
>= DISK_NAME_LEN
)
4381 strlcpy(buf
, val
, len
+1);
4382 if (strncmp(buf
, "md_", 3) != 0)
4384 return md_alloc(0, buf
);
4387 static void md_safemode_timeout(unsigned long data
)
4389 mddev_t
*mddev
= (mddev_t
*) data
;
4391 if (!atomic_read(&mddev
->writes_pending
)) {
4392 mddev
->safemode
= 1;
4393 if (mddev
->external
)
4394 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4396 md_wakeup_thread(mddev
->thread
);
4399 static int start_dirty_degraded
;
4401 int md_run(mddev_t
*mddev
)
4405 struct mdk_personality
*pers
;
4407 if (list_empty(&mddev
->disks
))
4408 /* cannot run an array with no devices.. */
4413 /* Cannot run until previous stop completes properly */
4414 if (mddev
->sysfs_active
)
4418 * Analyze all RAID superblock(s)
4420 if (!mddev
->raid_disks
) {
4421 if (!mddev
->persistent
)
4426 if (mddev
->level
!= LEVEL_NONE
)
4427 request_module("md-level-%d", mddev
->level
);
4428 else if (mddev
->clevel
[0])
4429 request_module("md-%s", mddev
->clevel
);
4432 * Drop all container device buffers, from now on
4433 * the only valid external interface is through the md
4436 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
4437 if (test_bit(Faulty
, &rdev
->flags
))
4439 sync_blockdev(rdev
->bdev
);
4440 invalidate_bdev(rdev
->bdev
);
4442 /* perform some consistency tests on the device.
4443 * We don't want the data to overlap the metadata,
4444 * Internal Bitmap issues have been handled elsewhere.
4446 if (rdev
->meta_bdev
) {
4447 /* Nothing to check */;
4448 } else if (rdev
->data_offset
< rdev
->sb_start
) {
4449 if (mddev
->dev_sectors
&&
4450 rdev
->data_offset
+ mddev
->dev_sectors
4452 printk("md: %s: data overlaps metadata\n",
4457 if (rdev
->sb_start
+ rdev
->sb_size
/512
4458 > rdev
->data_offset
) {
4459 printk("md: %s: metadata overlaps data\n",
4464 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
4467 if (mddev
->bio_set
== NULL
)
4468 mddev
->bio_set
= bioset_create(BIO_POOL_SIZE
, sizeof(mddev
));
4470 spin_lock(&pers_lock
);
4471 pers
= find_pers(mddev
->level
, mddev
->clevel
);
4472 if (!pers
|| !try_module_get(pers
->owner
)) {
4473 spin_unlock(&pers_lock
);
4474 if (mddev
->level
!= LEVEL_NONE
)
4475 printk(KERN_WARNING
"md: personality for level %d is not loaded!\n",
4478 printk(KERN_WARNING
"md: personality for level %s is not loaded!\n",
4483 spin_unlock(&pers_lock
);
4484 if (mddev
->level
!= pers
->level
) {
4485 mddev
->level
= pers
->level
;
4486 mddev
->new_level
= pers
->level
;
4488 strlcpy(mddev
->clevel
, pers
->name
, sizeof(mddev
->clevel
));
4490 if (mddev
->reshape_position
!= MaxSector
&&
4491 pers
->start_reshape
== NULL
) {
4492 /* This personality cannot handle reshaping... */
4494 module_put(pers
->owner
);
4498 if (pers
->sync_request
) {
4499 /* Warn if this is a potentially silly
4502 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
4506 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4507 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
4509 rdev
->bdev
->bd_contains
==
4510 rdev2
->bdev
->bd_contains
) {
4512 "%s: WARNING: %s appears to be"
4513 " on the same physical disk as"
4516 bdevname(rdev
->bdev
,b
),
4517 bdevname(rdev2
->bdev
,b2
));
4524 "True protection against single-disk"
4525 " failure might be compromised.\n");
4528 mddev
->recovery
= 0;
4529 /* may be over-ridden by personality */
4530 mddev
->resync_max_sectors
= mddev
->dev_sectors
;
4532 mddev
->ok_start_degraded
= start_dirty_degraded
;
4534 if (start_readonly
&& mddev
->ro
== 0)
4535 mddev
->ro
= 2; /* read-only, but switch on first write */
4537 err
= mddev
->pers
->run(mddev
);
4539 printk(KERN_ERR
"md: pers->run() failed ...\n");
4540 else if (mddev
->pers
->size(mddev
, 0, 0) < mddev
->array_sectors
) {
4541 WARN_ONCE(!mddev
->external_size
, "%s: default size too small,"
4542 " but 'external_size' not in effect?\n", __func__
);
4544 "md: invalid array_size %llu > default size %llu\n",
4545 (unsigned long long)mddev
->array_sectors
/ 2,
4546 (unsigned long long)mddev
->pers
->size(mddev
, 0, 0) / 2);
4548 mddev
->pers
->stop(mddev
);
4550 if (err
== 0 && mddev
->pers
->sync_request
) {
4551 err
= bitmap_create(mddev
);
4553 printk(KERN_ERR
"%s: failed to create bitmap (%d)\n",
4554 mdname(mddev
), err
);
4555 mddev
->pers
->stop(mddev
);
4559 module_put(mddev
->pers
->owner
);
4561 bitmap_destroy(mddev
);
4564 if (mddev
->pers
->sync_request
) {
4565 if (mddev
->kobj
.sd
&&
4566 sysfs_create_group(&mddev
->kobj
, &md_redundancy_group
))
4568 "md: cannot register extra attributes for %s\n",
4570 mddev
->sysfs_action
= sysfs_get_dirent_safe(mddev
->kobj
.sd
, "sync_action");
4571 } else if (mddev
->ro
== 2) /* auto-readonly not meaningful */
4574 atomic_set(&mddev
->writes_pending
,0);
4575 atomic_set(&mddev
->max_corr_read_errors
,
4576 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS
);
4577 mddev
->safemode
= 0;
4578 mddev
->safemode_timer
.function
= md_safemode_timeout
;
4579 mddev
->safemode_timer
.data
= (unsigned long) mddev
;
4580 mddev
->safemode_delay
= (200 * HZ
)/1000 +1; /* 200 msec delay */
4584 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4585 if (rdev
->raid_disk
>= 0) {
4587 sprintf(nm
, "rd%d", rdev
->raid_disk
);
4588 if (sysfs_create_link(&mddev
->kobj
, &rdev
->kobj
, nm
))
4589 /* failure here is OK */;
4592 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4595 md_update_sb(mddev
, 0);
4597 md_wakeup_thread(mddev
->thread
);
4598 md_wakeup_thread(mddev
->sync_thread
); /* possibly kick off a reshape */
4600 md_new_event(mddev
);
4601 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4602 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
4603 sysfs_notify(&mddev
->kobj
, NULL
, "degraded");
4606 EXPORT_SYMBOL_GPL(md_run
);
4608 static int do_md_run(mddev_t
*mddev
)
4612 err
= md_run(mddev
);
4615 err
= bitmap_load(mddev
);
4617 bitmap_destroy(mddev
);
4620 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
4621 revalidate_disk(mddev
->gendisk
);
4622 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
4627 static int restart_array(mddev_t
*mddev
)
4629 struct gendisk
*disk
= mddev
->gendisk
;
4631 /* Complain if it has no devices */
4632 if (list_empty(&mddev
->disks
))
4638 mddev
->safemode
= 0;
4640 set_disk_ro(disk
, 0);
4641 printk(KERN_INFO
"md: %s switched to read-write mode.\n",
4643 /* Kick recovery or resync if necessary */
4644 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4645 md_wakeup_thread(mddev
->thread
);
4646 md_wakeup_thread(mddev
->sync_thread
);
4647 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4651 /* similar to deny_write_access, but accounts for our holding a reference
4652 * to the file ourselves */
4653 static int deny_bitmap_write_access(struct file
* file
)
4655 struct inode
*inode
= file
->f_mapping
->host
;
4657 spin_lock(&inode
->i_lock
);
4658 if (atomic_read(&inode
->i_writecount
) > 1) {
4659 spin_unlock(&inode
->i_lock
);
4662 atomic_set(&inode
->i_writecount
, -1);
4663 spin_unlock(&inode
->i_lock
);
4668 void restore_bitmap_write_access(struct file
*file
)
4670 struct inode
*inode
= file
->f_mapping
->host
;
4672 spin_lock(&inode
->i_lock
);
4673 atomic_set(&inode
->i_writecount
, 1);
4674 spin_unlock(&inode
->i_lock
);
4677 static void md_clean(mddev_t
*mddev
)
4679 mddev
->array_sectors
= 0;
4680 mddev
->external_size
= 0;
4681 mddev
->dev_sectors
= 0;
4682 mddev
->raid_disks
= 0;
4683 mddev
->recovery_cp
= 0;
4684 mddev
->resync_min
= 0;
4685 mddev
->resync_max
= MaxSector
;
4686 mddev
->reshape_position
= MaxSector
;
4687 mddev
->external
= 0;
4688 mddev
->persistent
= 0;
4689 mddev
->level
= LEVEL_NONE
;
4690 mddev
->clevel
[0] = 0;
4693 mddev
->metadata_type
[0] = 0;
4694 mddev
->chunk_sectors
= 0;
4695 mddev
->ctime
= mddev
->utime
= 0;
4697 mddev
->max_disks
= 0;
4699 mddev
->can_decrease_events
= 0;
4700 mddev
->delta_disks
= 0;
4701 mddev
->new_level
= LEVEL_NONE
;
4702 mddev
->new_layout
= 0;
4703 mddev
->new_chunk_sectors
= 0;
4704 mddev
->curr_resync
= 0;
4705 mddev
->resync_mismatches
= 0;
4706 mddev
->suspend_lo
= mddev
->suspend_hi
= 0;
4707 mddev
->sync_speed_min
= mddev
->sync_speed_max
= 0;
4708 mddev
->recovery
= 0;
4710 mddev
->degraded
= 0;
4711 mddev
->safemode
= 0;
4712 mddev
->bitmap_info
.offset
= 0;
4713 mddev
->bitmap_info
.default_offset
= 0;
4714 mddev
->bitmap_info
.chunksize
= 0;
4715 mddev
->bitmap_info
.daemon_sleep
= 0;
4716 mddev
->bitmap_info
.max_write_behind
= 0;
4720 static void __md_stop_writes(mddev_t
*mddev
)
4722 if (mddev
->sync_thread
) {
4723 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4724 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4725 reap_sync_thread(mddev
);
4728 del_timer_sync(&mddev
->safemode_timer
);
4730 bitmap_flush(mddev
);
4731 md_super_wait(mddev
);
4733 if (!mddev
->in_sync
|| mddev
->flags
) {
4734 /* mark array as shutdown cleanly */
4736 md_update_sb(mddev
, 1);
4740 void md_stop_writes(mddev_t
*mddev
)
4743 __md_stop_writes(mddev
);
4744 mddev_unlock(mddev
);
4746 EXPORT_SYMBOL_GPL(md_stop_writes
);
4748 void md_stop(mddev_t
*mddev
)
4751 mddev
->pers
->stop(mddev
);
4752 if (mddev
->pers
->sync_request
&& mddev
->to_remove
== NULL
)
4753 mddev
->to_remove
= &md_redundancy_group
;
4754 module_put(mddev
->pers
->owner
);
4756 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4758 EXPORT_SYMBOL_GPL(md_stop
);
4760 static int md_set_readonly(mddev_t
*mddev
, int is_open
)
4763 mutex_lock(&mddev
->open_mutex
);
4764 if (atomic_read(&mddev
->openers
) > is_open
) {
4765 printk("md: %s still in use.\n",mdname(mddev
));
4770 __md_stop_writes(mddev
);
4776 set_disk_ro(mddev
->gendisk
, 1);
4777 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
4778 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4782 mutex_unlock(&mddev
->open_mutex
);
4787 * 0 - completely stop and dis-assemble array
4788 * 2 - stop but do not disassemble array
4790 static int do_md_stop(mddev_t
* mddev
, int mode
, int is_open
)
4792 struct gendisk
*disk
= mddev
->gendisk
;
4795 mutex_lock(&mddev
->open_mutex
);
4796 if (atomic_read(&mddev
->openers
) > is_open
||
4797 mddev
->sysfs_active
) {
4798 printk("md: %s still in use.\n",mdname(mddev
));
4799 mutex_unlock(&mddev
->open_mutex
);
4805 set_disk_ro(disk
, 0);
4807 __md_stop_writes(mddev
);
4809 mddev
->queue
->merge_bvec_fn
= NULL
;
4810 mddev
->queue
->unplug_fn
= NULL
;
4811 mddev
->queue
->backing_dev_info
.congested_fn
= NULL
;
4813 /* tell userspace to handle 'inactive' */
4814 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4816 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4817 if (rdev
->raid_disk
>= 0) {
4819 sprintf(nm
, "rd%d", rdev
->raid_disk
);
4820 sysfs_remove_link(&mddev
->kobj
, nm
);
4823 set_capacity(disk
, 0);
4824 mutex_unlock(&mddev
->open_mutex
);
4825 revalidate_disk(disk
);
4830 mutex_unlock(&mddev
->open_mutex
);
4832 * Free resources if final stop
4835 printk(KERN_INFO
"md: %s stopped.\n", mdname(mddev
));
4837 bitmap_destroy(mddev
);
4838 if (mddev
->bitmap_info
.file
) {
4839 restore_bitmap_write_access(mddev
->bitmap_info
.file
);
4840 fput(mddev
->bitmap_info
.file
);
4841 mddev
->bitmap_info
.file
= NULL
;
4843 mddev
->bitmap_info
.offset
= 0;
4845 export_array(mddev
);
4848 kobject_uevent(&disk_to_dev(mddev
->gendisk
)->kobj
, KOBJ_CHANGE
);
4849 if (mddev
->hold_active
== UNTIL_STOP
)
4850 mddev
->hold_active
= 0;
4852 blk_integrity_unregister(disk
);
4853 md_new_event(mddev
);
4854 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
4859 static void autorun_array(mddev_t
*mddev
)
4864 if (list_empty(&mddev
->disks
))
4867 printk(KERN_INFO
"md: running: ");
4869 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
4870 char b
[BDEVNAME_SIZE
];
4871 printk("<%s>", bdevname(rdev
->bdev
,b
));
4875 err
= do_md_run(mddev
);
4877 printk(KERN_WARNING
"md: do_md_run() returned %d\n", err
);
4878 do_md_stop(mddev
, 0, 0);
4883 * lets try to run arrays based on all disks that have arrived
4884 * until now. (those are in pending_raid_disks)
4886 * the method: pick the first pending disk, collect all disks with
4887 * the same UUID, remove all from the pending list and put them into
4888 * the 'same_array' list. Then order this list based on superblock
4889 * update time (freshest comes first), kick out 'old' disks and
4890 * compare superblocks. If everything's fine then run it.
4892 * If "unit" is allocated, then bump its reference count
4894 static void autorun_devices(int part
)
4896 mdk_rdev_t
*rdev0
, *rdev
, *tmp
;
4898 char b
[BDEVNAME_SIZE
];
4900 printk(KERN_INFO
"md: autorun ...\n");
4901 while (!list_empty(&pending_raid_disks
)) {
4904 LIST_HEAD(candidates
);
4905 rdev0
= list_entry(pending_raid_disks
.next
,
4906 mdk_rdev_t
, same_set
);
4908 printk(KERN_INFO
"md: considering %s ...\n",
4909 bdevname(rdev0
->bdev
,b
));
4910 INIT_LIST_HEAD(&candidates
);
4911 rdev_for_each_list(rdev
, tmp
, &pending_raid_disks
)
4912 if (super_90_load(rdev
, rdev0
, 0) >= 0) {
4913 printk(KERN_INFO
"md: adding %s ...\n",
4914 bdevname(rdev
->bdev
,b
));
4915 list_move(&rdev
->same_set
, &candidates
);
4918 * now we have a set of devices, with all of them having
4919 * mostly sane superblocks. It's time to allocate the
4923 dev
= MKDEV(mdp_major
,
4924 rdev0
->preferred_minor
<< MdpMinorShift
);
4925 unit
= MINOR(dev
) >> MdpMinorShift
;
4927 dev
= MKDEV(MD_MAJOR
, rdev0
->preferred_minor
);
4930 if (rdev0
->preferred_minor
!= unit
) {
4931 printk(KERN_INFO
"md: unit number in %s is bad: %d\n",
4932 bdevname(rdev0
->bdev
, b
), rdev0
->preferred_minor
);
4936 md_probe(dev
, NULL
, NULL
);
4937 mddev
= mddev_find(dev
);
4938 if (!mddev
|| !mddev
->gendisk
) {
4942 "md: cannot allocate memory for md drive.\n");
4945 if (mddev_lock(mddev
))
4946 printk(KERN_WARNING
"md: %s locked, cannot run\n",
4948 else if (mddev
->raid_disks
|| mddev
->major_version
4949 || !list_empty(&mddev
->disks
)) {
4951 "md: %s already running, cannot run %s\n",
4952 mdname(mddev
), bdevname(rdev0
->bdev
,b
));
4953 mddev_unlock(mddev
);
4955 printk(KERN_INFO
"md: created %s\n", mdname(mddev
));
4956 mddev
->persistent
= 1;
4957 rdev_for_each_list(rdev
, tmp
, &candidates
) {
4958 list_del_init(&rdev
->same_set
);
4959 if (bind_rdev_to_array(rdev
, mddev
))
4962 autorun_array(mddev
);
4963 mddev_unlock(mddev
);
4965 /* on success, candidates will be empty, on error
4968 rdev_for_each_list(rdev
, tmp
, &candidates
) {
4969 list_del_init(&rdev
->same_set
);
4974 printk(KERN_INFO
"md: ... autorun DONE.\n");
4976 #endif /* !MODULE */
4978 static int get_version(void __user
* arg
)
4982 ver
.major
= MD_MAJOR_VERSION
;
4983 ver
.minor
= MD_MINOR_VERSION
;
4984 ver
.patchlevel
= MD_PATCHLEVEL_VERSION
;
4986 if (copy_to_user(arg
, &ver
, sizeof(ver
)))
4992 static int get_array_info(mddev_t
* mddev
, void __user
* arg
)
4994 mdu_array_info_t info
;
4995 int nr
,working
,insync
,failed
,spare
;
4998 nr
=working
=insync
=failed
=spare
=0;
4999 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
5001 if (test_bit(Faulty
, &rdev
->flags
))
5005 if (test_bit(In_sync
, &rdev
->flags
))
5012 info
.major_version
= mddev
->major_version
;
5013 info
.minor_version
= mddev
->minor_version
;
5014 info
.patch_version
= MD_PATCHLEVEL_VERSION
;
5015 info
.ctime
= mddev
->ctime
;
5016 info
.level
= mddev
->level
;
5017 info
.size
= mddev
->dev_sectors
/ 2;
5018 if (info
.size
!= mddev
->dev_sectors
/ 2) /* overflow */
5021 info
.raid_disks
= mddev
->raid_disks
;
5022 info
.md_minor
= mddev
->md_minor
;
5023 info
.not_persistent
= !mddev
->persistent
;
5025 info
.utime
= mddev
->utime
;
5028 info
.state
= (1<<MD_SB_CLEAN
);
5029 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
5030 info
.state
= (1<<MD_SB_BITMAP_PRESENT
);
5031 info
.active_disks
= insync
;
5032 info
.working_disks
= working
;
5033 info
.failed_disks
= failed
;
5034 info
.spare_disks
= spare
;
5036 info
.layout
= mddev
->layout
;
5037 info
.chunk_size
= mddev
->chunk_sectors
<< 9;
5039 if (copy_to_user(arg
, &info
, sizeof(info
)))
5045 static int get_bitmap_file(mddev_t
* mddev
, void __user
* arg
)
5047 mdu_bitmap_file_t
*file
= NULL
; /* too big for stack allocation */
5048 char *ptr
, *buf
= NULL
;
5051 if (md_allow_write(mddev
))
5052 file
= kmalloc(sizeof(*file
), GFP_NOIO
);
5054 file
= kmalloc(sizeof(*file
), GFP_KERNEL
);
5059 /* bitmap disabled, zero the first byte and copy out */
5060 if (!mddev
->bitmap
|| !mddev
->bitmap
->file
) {
5061 file
->pathname
[0] = '\0';
5065 buf
= kmalloc(sizeof(file
->pathname
), GFP_KERNEL
);
5069 ptr
= d_path(&mddev
->bitmap
->file
->f_path
, buf
, sizeof(file
->pathname
));
5073 strcpy(file
->pathname
, ptr
);
5077 if (copy_to_user(arg
, file
, sizeof(*file
)))
5085 static int get_disk_info(mddev_t
* mddev
, void __user
* arg
)
5087 mdu_disk_info_t info
;
5090 if (copy_from_user(&info
, arg
, sizeof(info
)))
5093 rdev
= find_rdev_nr(mddev
, info
.number
);
5095 info
.major
= MAJOR(rdev
->bdev
->bd_dev
);
5096 info
.minor
= MINOR(rdev
->bdev
->bd_dev
);
5097 info
.raid_disk
= rdev
->raid_disk
;
5099 if (test_bit(Faulty
, &rdev
->flags
))
5100 info
.state
|= (1<<MD_DISK_FAULTY
);
5101 else if (test_bit(In_sync
, &rdev
->flags
)) {
5102 info
.state
|= (1<<MD_DISK_ACTIVE
);
5103 info
.state
|= (1<<MD_DISK_SYNC
);
5105 if (test_bit(WriteMostly
, &rdev
->flags
))
5106 info
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
5108 info
.major
= info
.minor
= 0;
5109 info
.raid_disk
= -1;
5110 info
.state
= (1<<MD_DISK_REMOVED
);
5113 if (copy_to_user(arg
, &info
, sizeof(info
)))
5119 static int add_new_disk(mddev_t
* mddev
, mdu_disk_info_t
*info
)
5121 char b
[BDEVNAME_SIZE
], b2
[BDEVNAME_SIZE
];
5123 dev_t dev
= MKDEV(info
->major
,info
->minor
);
5125 if (info
->major
!= MAJOR(dev
) || info
->minor
!= MINOR(dev
))
5128 if (!mddev
->raid_disks
) {
5130 /* expecting a device which has a superblock */
5131 rdev
= md_import_device(dev
, mddev
->major_version
, mddev
->minor_version
);
5134 "md: md_import_device returned %ld\n",
5136 return PTR_ERR(rdev
);
5138 if (!list_empty(&mddev
->disks
)) {
5139 mdk_rdev_t
*rdev0
= list_entry(mddev
->disks
.next
,
5140 mdk_rdev_t
, same_set
);
5141 err
= super_types
[mddev
->major_version
]
5142 .load_super(rdev
, rdev0
, mddev
->minor_version
);
5145 "md: %s has different UUID to %s\n",
5146 bdevname(rdev
->bdev
,b
),
5147 bdevname(rdev0
->bdev
,b2
));
5152 err
= bind_rdev_to_array(rdev
, mddev
);
5159 * add_new_disk can be used once the array is assembled
5160 * to add "hot spares". They must already have a superblock
5165 if (!mddev
->pers
->hot_add_disk
) {
5167 "%s: personality does not support diskops!\n",
5171 if (mddev
->persistent
)
5172 rdev
= md_import_device(dev
, mddev
->major_version
,
5173 mddev
->minor_version
);
5175 rdev
= md_import_device(dev
, -1, -1);
5178 "md: md_import_device returned %ld\n",
5180 return PTR_ERR(rdev
);
5182 /* set saved_raid_disk if appropriate */
5183 if (!mddev
->persistent
) {
5184 if (info
->state
& (1<<MD_DISK_SYNC
) &&
5185 info
->raid_disk
< mddev
->raid_disks
) {
5186 rdev
->raid_disk
= info
->raid_disk
;
5187 set_bit(In_sync
, &rdev
->flags
);
5189 rdev
->raid_disk
= -1;
5191 super_types
[mddev
->major_version
].
5192 validate_super(mddev
, rdev
);
5193 if (test_bit(In_sync
, &rdev
->flags
))
5194 rdev
->saved_raid_disk
= rdev
->raid_disk
;
5196 rdev
->saved_raid_disk
= -1;
5198 clear_bit(In_sync
, &rdev
->flags
); /* just to be sure */
5199 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
5200 set_bit(WriteMostly
, &rdev
->flags
);
5202 clear_bit(WriteMostly
, &rdev
->flags
);
5204 rdev
->raid_disk
= -1;
5205 err
= bind_rdev_to_array(rdev
, mddev
);
5206 if (!err
&& !mddev
->pers
->hot_remove_disk
) {
5207 /* If there is hot_add_disk but no hot_remove_disk
5208 * then added disks for geometry changes,
5209 * and should be added immediately.
5211 super_types
[mddev
->major_version
].
5212 validate_super(mddev
, rdev
);
5213 err
= mddev
->pers
->hot_add_disk(mddev
, rdev
);
5215 unbind_rdev_from_array(rdev
);
5220 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
5222 md_update_sb(mddev
, 1);
5223 if (mddev
->degraded
)
5224 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
5225 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5226 md_wakeup_thread(mddev
->thread
);
5230 /* otherwise, add_new_disk is only allowed
5231 * for major_version==0 superblocks
5233 if (mddev
->major_version
!= 0) {
5234 printk(KERN_WARNING
"%s: ADD_NEW_DISK not supported\n",
5239 if (!(info
->state
& (1<<MD_DISK_FAULTY
))) {
5241 rdev
= md_import_device(dev
, -1, 0);
5244 "md: error, md_import_device() returned %ld\n",
5246 return PTR_ERR(rdev
);
5248 rdev
->desc_nr
= info
->number
;
5249 if (info
->raid_disk
< mddev
->raid_disks
)
5250 rdev
->raid_disk
= info
->raid_disk
;
5252 rdev
->raid_disk
= -1;
5254 if (rdev
->raid_disk
< mddev
->raid_disks
)
5255 if (info
->state
& (1<<MD_DISK_SYNC
))
5256 set_bit(In_sync
, &rdev
->flags
);
5258 if (info
->state
& (1<<MD_DISK_WRITEMOSTLY
))
5259 set_bit(WriteMostly
, &rdev
->flags
);
5261 if (!mddev
->persistent
) {
5262 printk(KERN_INFO
"md: nonpersistent superblock ...\n");
5263 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
5265 rdev
->sb_start
= calc_dev_sboffset(rdev
);
5266 rdev
->sectors
= rdev
->sb_start
;
5268 err
= bind_rdev_to_array(rdev
, mddev
);
5278 static int hot_remove_disk(mddev_t
* mddev
, dev_t dev
)
5280 char b
[BDEVNAME_SIZE
];
5283 rdev
= find_rdev(mddev
, dev
);
5287 if (rdev
->raid_disk
>= 0)
5290 kick_rdev_from_array(rdev
);
5291 md_update_sb(mddev
, 1);
5292 md_new_event(mddev
);
5296 printk(KERN_WARNING
"md: cannot remove active disk %s from %s ...\n",
5297 bdevname(rdev
->bdev
,b
), mdname(mddev
));
5301 static int hot_add_disk(mddev_t
* mddev
, dev_t dev
)
5303 char b
[BDEVNAME_SIZE
];
5310 if (mddev
->major_version
!= 0) {
5311 printk(KERN_WARNING
"%s: HOT_ADD may only be used with"
5312 " version-0 superblocks.\n",
5316 if (!mddev
->pers
->hot_add_disk
) {
5318 "%s: personality does not support diskops!\n",
5323 rdev
= md_import_device(dev
, -1, 0);
5326 "md: error, md_import_device() returned %ld\n",
5331 if (mddev
->persistent
)
5332 rdev
->sb_start
= calc_dev_sboffset(rdev
);
5334 rdev
->sb_start
= i_size_read(rdev
->bdev
->bd_inode
) / 512;
5336 rdev
->sectors
= rdev
->sb_start
;
5338 if (test_bit(Faulty
, &rdev
->flags
)) {
5340 "md: can not hot-add faulty %s disk to %s!\n",
5341 bdevname(rdev
->bdev
,b
), mdname(mddev
));
5345 clear_bit(In_sync
, &rdev
->flags
);
5347 rdev
->saved_raid_disk
= -1;
5348 err
= bind_rdev_to_array(rdev
, mddev
);
5353 * The rest should better be atomic, we can have disk failures
5354 * noticed in interrupt contexts ...
5357 rdev
->raid_disk
= -1;
5359 md_update_sb(mddev
, 1);
5362 * Kick recovery, maybe this spare has to be added to the
5363 * array immediately.
5365 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5366 md_wakeup_thread(mddev
->thread
);
5367 md_new_event(mddev
);
5375 static int set_bitmap_file(mddev_t
*mddev
, int fd
)
5380 if (!mddev
->pers
->quiesce
)
5382 if (mddev
->recovery
|| mddev
->sync_thread
)
5384 /* we should be able to change the bitmap.. */
5390 return -EEXIST
; /* cannot add when bitmap is present */
5391 mddev
->bitmap_info
.file
= fget(fd
);
5393 if (mddev
->bitmap_info
.file
== NULL
) {
5394 printk(KERN_ERR
"%s: error: failed to get bitmap file\n",
5399 err
= deny_bitmap_write_access(mddev
->bitmap_info
.file
);
5401 printk(KERN_ERR
"%s: error: bitmap file is already in use\n",
5403 fput(mddev
->bitmap_info
.file
);
5404 mddev
->bitmap_info
.file
= NULL
;
5407 mddev
->bitmap_info
.offset
= 0; /* file overrides offset */
5408 } else if (mddev
->bitmap
== NULL
)
5409 return -ENOENT
; /* cannot remove what isn't there */
5412 mddev
->pers
->quiesce(mddev
, 1);
5414 err
= bitmap_create(mddev
);
5416 err
= bitmap_load(mddev
);
5418 if (fd
< 0 || err
) {
5419 bitmap_destroy(mddev
);
5420 fd
= -1; /* make sure to put the file */
5422 mddev
->pers
->quiesce(mddev
, 0);
5425 if (mddev
->bitmap_info
.file
) {
5426 restore_bitmap_write_access(mddev
->bitmap_info
.file
);
5427 fput(mddev
->bitmap_info
.file
);
5429 mddev
->bitmap_info
.file
= NULL
;
5436 * set_array_info is used two different ways
5437 * The original usage is when creating a new array.
5438 * In this usage, raid_disks is > 0 and it together with
5439 * level, size, not_persistent,layout,chunksize determine the
5440 * shape of the array.
5441 * This will always create an array with a type-0.90.0 superblock.
5442 * The newer usage is when assembling an array.
5443 * In this case raid_disks will be 0, and the major_version field is
5444 * use to determine which style super-blocks are to be found on the devices.
5445 * The minor and patch _version numbers are also kept incase the
5446 * super_block handler wishes to interpret them.
5448 static int set_array_info(mddev_t
* mddev
, mdu_array_info_t
*info
)
5451 if (info
->raid_disks
== 0) {
5452 /* just setting version number for superblock loading */
5453 if (info
->major_version
< 0 ||
5454 info
->major_version
>= ARRAY_SIZE(super_types
) ||
5455 super_types
[info
->major_version
].name
== NULL
) {
5456 /* maybe try to auto-load a module? */
5458 "md: superblock version %d not known\n",
5459 info
->major_version
);
5462 mddev
->major_version
= info
->major_version
;
5463 mddev
->minor_version
= info
->minor_version
;
5464 mddev
->patch_version
= info
->patch_version
;
5465 mddev
->persistent
= !info
->not_persistent
;
5466 /* ensure mddev_put doesn't delete this now that there
5467 * is some minimal configuration.
5469 mddev
->ctime
= get_seconds();
5472 mddev
->major_version
= MD_MAJOR_VERSION
;
5473 mddev
->minor_version
= MD_MINOR_VERSION
;
5474 mddev
->patch_version
= MD_PATCHLEVEL_VERSION
;
5475 mddev
->ctime
= get_seconds();
5477 mddev
->level
= info
->level
;
5478 mddev
->clevel
[0] = 0;
5479 mddev
->dev_sectors
= 2 * (sector_t
)info
->size
;
5480 mddev
->raid_disks
= info
->raid_disks
;
5481 /* don't set md_minor, it is determined by which /dev/md* was
5484 if (info
->state
& (1<<MD_SB_CLEAN
))
5485 mddev
->recovery_cp
= MaxSector
;
5487 mddev
->recovery_cp
= 0;
5488 mddev
->persistent
= ! info
->not_persistent
;
5489 mddev
->external
= 0;
5491 mddev
->layout
= info
->layout
;
5492 mddev
->chunk_sectors
= info
->chunk_size
>> 9;
5494 mddev
->max_disks
= MD_SB_DISKS
;
5496 if (mddev
->persistent
)
5498 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
5500 mddev
->bitmap_info
.default_offset
= MD_SB_BYTES
>> 9;
5501 mddev
->bitmap_info
.offset
= 0;
5503 mddev
->reshape_position
= MaxSector
;
5506 * Generate a 128 bit UUID
5508 get_random_bytes(mddev
->uuid
, 16);
5510 mddev
->new_level
= mddev
->level
;
5511 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
5512 mddev
->new_layout
= mddev
->layout
;
5513 mddev
->delta_disks
= 0;
5518 void md_set_array_sectors(mddev_t
*mddev
, sector_t array_sectors
)
5520 WARN(!mddev_is_locked(mddev
), "%s: unlocked mddev!\n", __func__
);
5522 if (mddev
->external_size
)
5525 mddev
->array_sectors
= array_sectors
;
5527 EXPORT_SYMBOL(md_set_array_sectors
);
5529 static int update_size(mddev_t
*mddev
, sector_t num_sectors
)
5533 int fit
= (num_sectors
== 0);
5535 if (mddev
->pers
->resize
== NULL
)
5537 /* The "num_sectors" is the number of sectors of each device that
5538 * is used. This can only make sense for arrays with redundancy.
5539 * linear and raid0 always use whatever space is available. We can only
5540 * consider changing this number if no resync or reconstruction is
5541 * happening, and if the new size is acceptable. It must fit before the
5542 * sb_start or, if that is <data_offset, it must fit before the size
5543 * of each device. If num_sectors is zero, we find the largest size
5546 if (mddev
->sync_thread
)
5549 /* Sorry, cannot grow a bitmap yet, just remove it,
5553 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
5554 sector_t avail
= rdev
->sectors
;
5556 if (fit
&& (num_sectors
== 0 || num_sectors
> avail
))
5557 num_sectors
= avail
;
5558 if (avail
< num_sectors
)
5561 rv
= mddev
->pers
->resize(mddev
, num_sectors
);
5563 revalidate_disk(mddev
->gendisk
);
5567 static int update_raid_disks(mddev_t
*mddev
, int raid_disks
)
5570 /* change the number of raid disks */
5571 if (mddev
->pers
->check_reshape
== NULL
)
5573 if (raid_disks
<= 0 ||
5574 (mddev
->max_disks
&& raid_disks
>= mddev
->max_disks
))
5576 if (mddev
->sync_thread
|| mddev
->reshape_position
!= MaxSector
)
5578 mddev
->delta_disks
= raid_disks
- mddev
->raid_disks
;
5580 rv
= mddev
->pers
->check_reshape(mddev
);
5586 * update_array_info is used to change the configuration of an
5588 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5589 * fields in the info are checked against the array.
5590 * Any differences that cannot be handled will cause an error.
5591 * Normally, only one change can be managed at a time.
5593 static int update_array_info(mddev_t
*mddev
, mdu_array_info_t
*info
)
5599 /* calculate expected state,ignoring low bits */
5600 if (mddev
->bitmap
&& mddev
->bitmap_info
.offset
)
5601 state
|= (1 << MD_SB_BITMAP_PRESENT
);
5603 if (mddev
->major_version
!= info
->major_version
||
5604 mddev
->minor_version
!= info
->minor_version
||
5605 /* mddev->patch_version != info->patch_version || */
5606 mddev
->ctime
!= info
->ctime
||
5607 mddev
->level
!= info
->level
||
5608 /* mddev->layout != info->layout || */
5609 !mddev
->persistent
!= info
->not_persistent
||
5610 mddev
->chunk_sectors
!= info
->chunk_size
>> 9 ||
5611 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5612 ((state
^info
->state
) & 0xfffffe00)
5615 /* Check there is only one change */
5616 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
5618 if (mddev
->raid_disks
!= info
->raid_disks
)
5620 if (mddev
->layout
!= info
->layout
)
5622 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
))
5629 if (mddev
->layout
!= info
->layout
) {
5631 * we don't need to do anything at the md level, the
5632 * personality will take care of it all.
5634 if (mddev
->pers
->check_reshape
== NULL
)
5637 mddev
->new_layout
= info
->layout
;
5638 rv
= mddev
->pers
->check_reshape(mddev
);
5640 mddev
->new_layout
= mddev
->layout
;
5644 if (info
->size
>= 0 && mddev
->dev_sectors
/ 2 != info
->size
)
5645 rv
= update_size(mddev
, (sector_t
)info
->size
* 2);
5647 if (mddev
->raid_disks
!= info
->raid_disks
)
5648 rv
= update_raid_disks(mddev
, info
->raid_disks
);
5650 if ((state
^ info
->state
) & (1<<MD_SB_BITMAP_PRESENT
)) {
5651 if (mddev
->pers
->quiesce
== NULL
)
5653 if (mddev
->recovery
|| mddev
->sync_thread
)
5655 if (info
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
5656 /* add the bitmap */
5659 if (mddev
->bitmap_info
.default_offset
== 0)
5661 mddev
->bitmap_info
.offset
=
5662 mddev
->bitmap_info
.default_offset
;
5663 mddev
->pers
->quiesce(mddev
, 1);
5664 rv
= bitmap_create(mddev
);
5666 rv
= bitmap_load(mddev
);
5668 bitmap_destroy(mddev
);
5669 mddev
->pers
->quiesce(mddev
, 0);
5671 /* remove the bitmap */
5674 if (mddev
->bitmap
->file
)
5676 mddev
->pers
->quiesce(mddev
, 1);
5677 bitmap_destroy(mddev
);
5678 mddev
->pers
->quiesce(mddev
, 0);
5679 mddev
->bitmap_info
.offset
= 0;
5682 md_update_sb(mddev
, 1);
5686 static int set_disk_faulty(mddev_t
*mddev
, dev_t dev
)
5690 if (mddev
->pers
== NULL
)
5693 rdev
= find_rdev(mddev
, dev
);
5697 md_error(mddev
, rdev
);
5702 * We have a problem here : there is no easy way to give a CHS
5703 * virtual geometry. We currently pretend that we have a 2 heads
5704 * 4 sectors (with a BIG number of cylinders...). This drives
5705 * dosfs just mad... ;-)
5707 static int md_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
5709 mddev_t
*mddev
= bdev
->bd_disk
->private_data
;
5713 geo
->cylinders
= mddev
->array_sectors
/ 8;
5717 static int md_ioctl(struct block_device
*bdev
, fmode_t mode
,
5718 unsigned int cmd
, unsigned long arg
)
5721 void __user
*argp
= (void __user
*)arg
;
5722 mddev_t
*mddev
= NULL
;
5725 if (!capable(CAP_SYS_ADMIN
))
5729 * Commands dealing with the RAID driver but not any
5735 err
= get_version(argp
);
5738 case PRINT_RAID_DEBUG
:
5746 autostart_arrays(arg
);
5753 * Commands creating/starting a new array:
5756 mddev
= bdev
->bd_disk
->private_data
;
5763 err
= mddev_lock(mddev
);
5766 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5773 case SET_ARRAY_INFO
:
5775 mdu_array_info_t info
;
5777 memset(&info
, 0, sizeof(info
));
5778 else if (copy_from_user(&info
, argp
, sizeof(info
))) {
5783 err
= update_array_info(mddev
, &info
);
5785 printk(KERN_WARNING
"md: couldn't update"
5786 " array info. %d\n", err
);
5791 if (!list_empty(&mddev
->disks
)) {
5793 "md: array %s already has disks!\n",
5798 if (mddev
->raid_disks
) {
5800 "md: array %s already initialised!\n",
5805 err
= set_array_info(mddev
, &info
);
5807 printk(KERN_WARNING
"md: couldn't set"
5808 " array info. %d\n", err
);
5818 * Commands querying/configuring an existing array:
5820 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
5821 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
5822 if ((!mddev
->raid_disks
&& !mddev
->external
)
5823 && cmd
!= ADD_NEW_DISK
&& cmd
!= STOP_ARRAY
5824 && cmd
!= RUN_ARRAY
&& cmd
!= SET_BITMAP_FILE
5825 && cmd
!= GET_BITMAP_FILE
) {
5831 * Commands even a read-only array can execute:
5835 case GET_ARRAY_INFO
:
5836 err
= get_array_info(mddev
, argp
);
5839 case GET_BITMAP_FILE
:
5840 err
= get_bitmap_file(mddev
, argp
);
5844 err
= get_disk_info(mddev
, argp
);
5847 case RESTART_ARRAY_RW
:
5848 err
= restart_array(mddev
);
5852 err
= do_md_stop(mddev
, 0, 1);
5856 err
= md_set_readonly(mddev
, 1);
5860 if (get_user(ro
, (int __user
*)(arg
))) {
5866 /* if the bdev is going readonly the value of mddev->ro
5867 * does not matter, no writes are coming
5872 /* are we are already prepared for writes? */
5876 /* transitioning to readauto need only happen for
5877 * arrays that call md_write_start
5880 err
= restart_array(mddev
);
5883 set_disk_ro(mddev
->gendisk
, 0);
5890 * The remaining ioctls are changing the state of the
5891 * superblock, so we do not allow them on read-only arrays.
5892 * However non-MD ioctls (e.g. get-size) will still come through
5893 * here and hit the 'default' below, so only disallow
5894 * 'md' ioctls, and switch to rw mode if started auto-readonly.
5896 if (_IOC_TYPE(cmd
) == MD_MAJOR
&& mddev
->ro
&& mddev
->pers
) {
5897 if (mddev
->ro
== 2) {
5899 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
5900 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5901 md_wakeup_thread(mddev
->thread
);
5912 mdu_disk_info_t info
;
5913 if (copy_from_user(&info
, argp
, sizeof(info
)))
5916 err
= add_new_disk(mddev
, &info
);
5920 case HOT_REMOVE_DISK
:
5921 err
= hot_remove_disk(mddev
, new_decode_dev(arg
));
5925 err
= hot_add_disk(mddev
, new_decode_dev(arg
));
5928 case SET_DISK_FAULTY
:
5929 err
= set_disk_faulty(mddev
, new_decode_dev(arg
));
5933 err
= do_md_run(mddev
);
5936 case SET_BITMAP_FILE
:
5937 err
= set_bitmap_file(mddev
, (int)arg
);
5947 if (mddev
->hold_active
== UNTIL_IOCTL
&&
5949 mddev
->hold_active
= 0;
5950 mddev_unlock(mddev
);
5959 #ifdef CONFIG_COMPAT
5960 static int md_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
5961 unsigned int cmd
, unsigned long arg
)
5964 case HOT_REMOVE_DISK
:
5966 case SET_DISK_FAULTY
:
5967 case SET_BITMAP_FILE
:
5968 /* These take in integer arg, do not convert */
5971 arg
= (unsigned long)compat_ptr(arg
);
5975 return md_ioctl(bdev
, mode
, cmd
, arg
);
5977 #endif /* CONFIG_COMPAT */
5979 static int md_open(struct block_device
*bdev
, fmode_t mode
)
5982 * Succeed if we can lock the mddev, which confirms that
5983 * it isn't being stopped right now.
5985 mddev_t
*mddev
= mddev_find(bdev
->bd_dev
);
5988 if (mddev
->gendisk
!= bdev
->bd_disk
) {
5989 /* we are racing with mddev_put which is discarding this
5993 /* Wait until bdev->bd_disk is definitely gone */
5994 flush_workqueue(md_misc_wq
);
5995 /* Then retry the open from the top */
5996 return -ERESTARTSYS
;
5998 BUG_ON(mddev
!= bdev
->bd_disk
->private_data
);
6000 if ((err
= mutex_lock_interruptible(&mddev
->open_mutex
)))
6004 atomic_inc(&mddev
->openers
);
6005 mutex_unlock(&mddev
->open_mutex
);
6007 check_disk_size_change(mddev
->gendisk
, bdev
);
6012 static int md_release(struct gendisk
*disk
, fmode_t mode
)
6014 mddev_t
*mddev
= disk
->private_data
;
6017 atomic_dec(&mddev
->openers
);
6022 static const struct block_device_operations md_fops
=
6024 .owner
= THIS_MODULE
,
6026 .release
= md_release
,
6028 #ifdef CONFIG_COMPAT
6029 .compat_ioctl
= md_compat_ioctl
,
6031 .getgeo
= md_getgeo
,
6034 static int md_thread(void * arg
)
6036 mdk_thread_t
*thread
= arg
;
6039 * md_thread is a 'system-thread', it's priority should be very
6040 * high. We avoid resource deadlocks individually in each
6041 * raid personality. (RAID5 does preallocation) We also use RR and
6042 * the very same RT priority as kswapd, thus we will never get
6043 * into a priority inversion deadlock.
6045 * we definitely have to have equal or higher priority than
6046 * bdflush, otherwise bdflush will deadlock if there are too
6047 * many dirty RAID5 blocks.
6050 allow_signal(SIGKILL
);
6051 while (!kthread_should_stop()) {
6053 /* We need to wait INTERRUPTIBLE so that
6054 * we don't add to the load-average.
6055 * That means we need to be sure no signals are
6058 if (signal_pending(current
))
6059 flush_signals(current
);
6061 wait_event_interruptible_timeout
6063 test_bit(THREAD_WAKEUP
, &thread
->flags
)
6064 || kthread_should_stop(),
6067 clear_bit(THREAD_WAKEUP
, &thread
->flags
);
6068 if (!kthread_should_stop())
6069 thread
->run(thread
->mddev
);
6075 void md_wakeup_thread(mdk_thread_t
*thread
)
6078 dprintk("md: waking up MD thread %s.\n", thread
->tsk
->comm
);
6079 set_bit(THREAD_WAKEUP
, &thread
->flags
);
6080 wake_up(&thread
->wqueue
);
6084 mdk_thread_t
*md_register_thread(void (*run
) (mddev_t
*), mddev_t
*mddev
,
6087 mdk_thread_t
*thread
;
6089 thread
= kzalloc(sizeof(mdk_thread_t
), GFP_KERNEL
);
6093 init_waitqueue_head(&thread
->wqueue
);
6096 thread
->mddev
= mddev
;
6097 thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
6098 thread
->tsk
= kthread_run(md_thread
, thread
,
6100 mdname(thread
->mddev
),
6101 name
?: mddev
->pers
->name
);
6102 if (IS_ERR(thread
->tsk
)) {
6109 void md_unregister_thread(mdk_thread_t
*thread
)
6113 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread
->tsk
));
6115 kthread_stop(thread
->tsk
);
6119 void md_error(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
6126 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
6129 if (mddev
->external
)
6130 set_bit(Blocked
, &rdev
->flags
);
6132 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
6134 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
6135 __builtin_return_address(0),__builtin_return_address(1),
6136 __builtin_return_address(2),__builtin_return_address(3));
6140 if (!mddev
->pers
->error_handler
)
6142 mddev
->pers
->error_handler(mddev
,rdev
);
6143 if (mddev
->degraded
)
6144 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
6145 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
6146 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6147 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6148 md_wakeup_thread(mddev
->thread
);
6149 if (mddev
->event_work
.func
)
6150 queue_work(md_misc_wq
, &mddev
->event_work
);
6151 md_new_event_inintr(mddev
);
6154 /* seq_file implementation /proc/mdstat */
6156 static void status_unused(struct seq_file
*seq
)
6161 seq_printf(seq
, "unused devices: ");
6163 list_for_each_entry(rdev
, &pending_raid_disks
, same_set
) {
6164 char b
[BDEVNAME_SIZE
];
6166 seq_printf(seq
, "%s ",
6167 bdevname(rdev
->bdev
,b
));
6170 seq_printf(seq
, "<none>");
6172 seq_printf(seq
, "\n");
6176 static void status_resync(struct seq_file
*seq
, mddev_t
* mddev
)
6178 sector_t max_sectors
, resync
, res
;
6179 unsigned long dt
, db
;
6182 unsigned int per_milli
;
6184 resync
= mddev
->curr_resync
- atomic_read(&mddev
->recovery_active
);
6186 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
6187 max_sectors
= mddev
->resync_max_sectors
;
6189 max_sectors
= mddev
->dev_sectors
;
6192 * Should not happen.
6198 /* Pick 'scale' such that (resync>>scale)*1000 will fit
6199 * in a sector_t, and (max_sectors>>scale) will fit in a
6200 * u32, as those are the requirements for sector_div.
6201 * Thus 'scale' must be at least 10
6204 if (sizeof(sector_t
) > sizeof(unsigned long)) {
6205 while ( max_sectors
/2 > (1ULL<<(scale
+32)))
6208 res
= (resync
>>scale
)*1000;
6209 sector_div(res
, (u32
)((max_sectors
>>scale
)+1));
6213 int i
, x
= per_milli
/50, y
= 20-x
;
6214 seq_printf(seq
, "[");
6215 for (i
= 0; i
< x
; i
++)
6216 seq_printf(seq
, "=");
6217 seq_printf(seq
, ">");
6218 for (i
= 0; i
< y
; i
++)
6219 seq_printf(seq
, ".");
6220 seq_printf(seq
, "] ");
6222 seq_printf(seq
, " %s =%3u.%u%% (%llu/%llu)",
6223 (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)?
6225 (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)?
6227 (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ?
6228 "resync" : "recovery"))),
6229 per_milli
/10, per_milli
% 10,
6230 (unsigned long long) resync
/2,
6231 (unsigned long long) max_sectors
/2);
6234 * dt: time from mark until now
6235 * db: blocks written from mark until now
6236 * rt: remaining time
6238 * rt is a sector_t, so could be 32bit or 64bit.
6239 * So we divide before multiply in case it is 32bit and close
6241 * We scale the divisor (db) by 32 to avoid loosing precision
6242 * near the end of resync when the number of remaining sectors
6244 * We then divide rt by 32 after multiplying by db to compensate.
6245 * The '+1' avoids division by zero if db is very small.
6247 dt
= ((jiffies
- mddev
->resync_mark
) / HZ
);
6249 db
= (mddev
->curr_mark_cnt
- atomic_read(&mddev
->recovery_active
))
6250 - mddev
->resync_mark_cnt
;
6252 rt
= max_sectors
- resync
; /* number of remaining sectors */
6253 sector_div(rt
, db
/32+1);
6257 seq_printf(seq
, " finish=%lu.%lumin", (unsigned long)rt
/ 60,
6258 ((unsigned long)rt
% 60)/6);
6260 seq_printf(seq
, " speed=%ldK/sec", db
/2/dt
);
6263 static void *md_seq_start(struct seq_file
*seq
, loff_t
*pos
)
6265 struct list_head
*tmp
;
6275 spin_lock(&all_mddevs_lock
);
6276 list_for_each(tmp
,&all_mddevs
)
6278 mddev
= list_entry(tmp
, mddev_t
, all_mddevs
);
6280 spin_unlock(&all_mddevs_lock
);
6283 spin_unlock(&all_mddevs_lock
);
6285 return (void*)2;/* tail */
6289 static void *md_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
6291 struct list_head
*tmp
;
6292 mddev_t
*next_mddev
, *mddev
= v
;
6298 spin_lock(&all_mddevs_lock
);
6300 tmp
= all_mddevs
.next
;
6302 tmp
= mddev
->all_mddevs
.next
;
6303 if (tmp
!= &all_mddevs
)
6304 next_mddev
= mddev_get(list_entry(tmp
,mddev_t
,all_mddevs
));
6306 next_mddev
= (void*)2;
6309 spin_unlock(&all_mddevs_lock
);
6317 static void md_seq_stop(struct seq_file
*seq
, void *v
)
6321 if (mddev
&& v
!= (void*)1 && v
!= (void*)2)
6325 struct mdstat_info
{
6329 static int md_seq_show(struct seq_file
*seq
, void *v
)
6334 struct mdstat_info
*mi
= seq
->private;
6335 struct bitmap
*bitmap
;
6337 if (v
== (void*)1) {
6338 struct mdk_personality
*pers
;
6339 seq_printf(seq
, "Personalities : ");
6340 spin_lock(&pers_lock
);
6341 list_for_each_entry(pers
, &pers_list
, list
)
6342 seq_printf(seq
, "[%s] ", pers
->name
);
6344 spin_unlock(&pers_lock
);
6345 seq_printf(seq
, "\n");
6346 mi
->event
= atomic_read(&md_event_count
);
6349 if (v
== (void*)2) {
6354 if (mddev_lock(mddev
) < 0)
6357 if (mddev
->pers
|| mddev
->raid_disks
|| !list_empty(&mddev
->disks
)) {
6358 seq_printf(seq
, "%s : %sactive", mdname(mddev
),
6359 mddev
->pers
? "" : "in");
6362 seq_printf(seq
, " (read-only)");
6364 seq_printf(seq
, " (auto-read-only)");
6365 seq_printf(seq
, " %s", mddev
->pers
->name
);
6369 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
6370 char b
[BDEVNAME_SIZE
];
6371 seq_printf(seq
, " %s[%d]",
6372 bdevname(rdev
->bdev
,b
), rdev
->desc_nr
);
6373 if (test_bit(WriteMostly
, &rdev
->flags
))
6374 seq_printf(seq
, "(W)");
6375 if (test_bit(Faulty
, &rdev
->flags
)) {
6376 seq_printf(seq
, "(F)");
6378 } else if (rdev
->raid_disk
< 0)
6379 seq_printf(seq
, "(S)"); /* spare */
6380 sectors
+= rdev
->sectors
;
6383 if (!list_empty(&mddev
->disks
)) {
6385 seq_printf(seq
, "\n %llu blocks",
6386 (unsigned long long)
6387 mddev
->array_sectors
/ 2);
6389 seq_printf(seq
, "\n %llu blocks",
6390 (unsigned long long)sectors
/ 2);
6392 if (mddev
->persistent
) {
6393 if (mddev
->major_version
!= 0 ||
6394 mddev
->minor_version
!= 90) {
6395 seq_printf(seq
," super %d.%d",
6396 mddev
->major_version
,
6397 mddev
->minor_version
);
6399 } else if (mddev
->external
)
6400 seq_printf(seq
, " super external:%s",
6401 mddev
->metadata_type
);
6403 seq_printf(seq
, " super non-persistent");
6406 mddev
->pers
->status(seq
, mddev
);
6407 seq_printf(seq
, "\n ");
6408 if (mddev
->pers
->sync_request
) {
6409 if (mddev
->curr_resync
> 2) {
6410 status_resync(seq
, mddev
);
6411 seq_printf(seq
, "\n ");
6412 } else if (mddev
->curr_resync
== 1 || mddev
->curr_resync
== 2)
6413 seq_printf(seq
, "\tresync=DELAYED\n ");
6414 else if (mddev
->recovery_cp
< MaxSector
)
6415 seq_printf(seq
, "\tresync=PENDING\n ");
6418 seq_printf(seq
, "\n ");
6420 if ((bitmap
= mddev
->bitmap
)) {
6421 unsigned long chunk_kb
;
6422 unsigned long flags
;
6423 spin_lock_irqsave(&bitmap
->lock
, flags
);
6424 chunk_kb
= mddev
->bitmap_info
.chunksize
>> 10;
6425 seq_printf(seq
, "bitmap: %lu/%lu pages [%luKB], "
6427 bitmap
->pages
- bitmap
->missing_pages
,
6429 (bitmap
->pages
- bitmap
->missing_pages
)
6430 << (PAGE_SHIFT
- 10),
6431 chunk_kb
? chunk_kb
: mddev
->bitmap_info
.chunksize
,
6432 chunk_kb
? "KB" : "B");
6434 seq_printf(seq
, ", file: ");
6435 seq_path(seq
, &bitmap
->file
->f_path
, " \t\n");
6438 seq_printf(seq
, "\n");
6439 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
6442 seq_printf(seq
, "\n");
6444 mddev_unlock(mddev
);
6449 static const struct seq_operations md_seq_ops
= {
6450 .start
= md_seq_start
,
6451 .next
= md_seq_next
,
6452 .stop
= md_seq_stop
,
6453 .show
= md_seq_show
,
6456 static int md_seq_open(struct inode
*inode
, struct file
*file
)
6459 struct mdstat_info
*mi
= kmalloc(sizeof(*mi
), GFP_KERNEL
);
6463 error
= seq_open(file
, &md_seq_ops
);
6467 struct seq_file
*p
= file
->private_data
;
6469 mi
->event
= atomic_read(&md_event_count
);
6474 static unsigned int mdstat_poll(struct file
*filp
, poll_table
*wait
)
6476 struct seq_file
*m
= filp
->private_data
;
6477 struct mdstat_info
*mi
= m
->private;
6480 poll_wait(filp
, &md_event_waiters
, wait
);
6482 /* always allow read */
6483 mask
= POLLIN
| POLLRDNORM
;
6485 if (mi
->event
!= atomic_read(&md_event_count
))
6486 mask
|= POLLERR
| POLLPRI
;
6490 static const struct file_operations md_seq_fops
= {
6491 .owner
= THIS_MODULE
,
6492 .open
= md_seq_open
,
6494 .llseek
= seq_lseek
,
6495 .release
= seq_release_private
,
6496 .poll
= mdstat_poll
,
6499 int register_md_personality(struct mdk_personality
*p
)
6501 spin_lock(&pers_lock
);
6502 list_add_tail(&p
->list
, &pers_list
);
6503 printk(KERN_INFO
"md: %s personality registered for level %d\n", p
->name
, p
->level
);
6504 spin_unlock(&pers_lock
);
6508 int unregister_md_personality(struct mdk_personality
*p
)
6510 printk(KERN_INFO
"md: %s personality unregistered\n", p
->name
);
6511 spin_lock(&pers_lock
);
6512 list_del_init(&p
->list
);
6513 spin_unlock(&pers_lock
);
6517 static int is_mddev_idle(mddev_t
*mddev
, int init
)
6525 rdev_for_each_rcu(rdev
, mddev
) {
6526 struct gendisk
*disk
= rdev
->bdev
->bd_contains
->bd_disk
;
6527 curr_events
= (int)part_stat_read(&disk
->part0
, sectors
[0]) +
6528 (int)part_stat_read(&disk
->part0
, sectors
[1]) -
6529 atomic_read(&disk
->sync_io
);
6530 /* sync IO will cause sync_io to increase before the disk_stats
6531 * as sync_io is counted when a request starts, and
6532 * disk_stats is counted when it completes.
6533 * So resync activity will cause curr_events to be smaller than
6534 * when there was no such activity.
6535 * non-sync IO will cause disk_stat to increase without
6536 * increasing sync_io so curr_events will (eventually)
6537 * be larger than it was before. Once it becomes
6538 * substantially larger, the test below will cause
6539 * the array to appear non-idle, and resync will slow
6541 * If there is a lot of outstanding resync activity when
6542 * we set last_event to curr_events, then all that activity
6543 * completing might cause the array to appear non-idle
6544 * and resync will be slowed down even though there might
6545 * not have been non-resync activity. This will only
6546 * happen once though. 'last_events' will soon reflect
6547 * the state where there is little or no outstanding
6548 * resync requests, and further resync activity will
6549 * always make curr_events less than last_events.
6552 if (init
|| curr_events
- rdev
->last_events
> 64) {
6553 rdev
->last_events
= curr_events
;
6561 void md_done_sync(mddev_t
*mddev
, int blocks
, int ok
)
6563 /* another "blocks" (512byte) blocks have been synced */
6564 atomic_sub(blocks
, &mddev
->recovery_active
);
6565 wake_up(&mddev
->recovery_wait
);
6567 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6568 md_wakeup_thread(mddev
->thread
);
6569 // stop recovery, signal do_sync ....
6574 /* md_write_start(mddev, bi)
6575 * If we need to update some array metadata (e.g. 'active' flag
6576 * in superblock) before writing, schedule a superblock update
6577 * and wait for it to complete.
6579 void md_write_start(mddev_t
*mddev
, struct bio
*bi
)
6582 if (bio_data_dir(bi
) != WRITE
)
6585 BUG_ON(mddev
->ro
== 1);
6586 if (mddev
->ro
== 2) {
6587 /* need to switch to read/write */
6589 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6590 md_wakeup_thread(mddev
->thread
);
6591 md_wakeup_thread(mddev
->sync_thread
);
6594 atomic_inc(&mddev
->writes_pending
);
6595 if (mddev
->safemode
== 1)
6596 mddev
->safemode
= 0;
6597 if (mddev
->in_sync
) {
6598 spin_lock_irq(&mddev
->write_lock
);
6599 if (mddev
->in_sync
) {
6601 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6602 set_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
6603 md_wakeup_thread(mddev
->thread
);
6606 spin_unlock_irq(&mddev
->write_lock
);
6609 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6610 wait_event(mddev
->sb_wait
,
6611 !test_bit(MD_CHANGE_PENDING
, &mddev
->flags
));
6614 void md_write_end(mddev_t
*mddev
)
6616 if (atomic_dec_and_test(&mddev
->writes_pending
)) {
6617 if (mddev
->safemode
== 2)
6618 md_wakeup_thread(mddev
->thread
);
6619 else if (mddev
->safemode_delay
)
6620 mod_timer(&mddev
->safemode_timer
, jiffies
+ mddev
->safemode_delay
);
6624 /* md_allow_write(mddev)
6625 * Calling this ensures that the array is marked 'active' so that writes
6626 * may proceed without blocking. It is important to call this before
6627 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6628 * Must be called with mddev_lock held.
6630 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6631 * is dropped, so return -EAGAIN after notifying userspace.
6633 int md_allow_write(mddev_t
*mddev
)
6639 if (!mddev
->pers
->sync_request
)
6642 spin_lock_irq(&mddev
->write_lock
);
6643 if (mddev
->in_sync
) {
6645 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6646 set_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
6647 if (mddev
->safemode_delay
&&
6648 mddev
->safemode
== 0)
6649 mddev
->safemode
= 1;
6650 spin_unlock_irq(&mddev
->write_lock
);
6651 md_update_sb(mddev
, 0);
6652 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
6654 spin_unlock_irq(&mddev
->write_lock
);
6656 if (test_bit(MD_CHANGE_PENDING
, &mddev
->flags
))
6661 EXPORT_SYMBOL_GPL(md_allow_write
);
6663 void md_unplug(mddev_t
*mddev
)
6666 blk_unplug(mddev
->queue
);
6668 mddev
->plug
->unplug_fn(mddev
->plug
);
6671 #define SYNC_MARKS 10
6672 #define SYNC_MARK_STEP (3*HZ)
6673 void md_do_sync(mddev_t
*mddev
)
6676 unsigned int currspeed
= 0,
6678 sector_t max_sectors
,j
, io_sectors
;
6679 unsigned long mark
[SYNC_MARKS
];
6680 sector_t mark_cnt
[SYNC_MARKS
];
6682 struct list_head
*tmp
;
6683 sector_t last_check
;
6688 /* just incase thread restarts... */
6689 if (test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
))
6691 if (mddev
->ro
) /* never try to sync a read-only array */
6694 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
6695 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
6696 desc
= "data-check";
6697 else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
6698 desc
= "requested-resync";
6701 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
6706 /* we overload curr_resync somewhat here.
6707 * 0 == not engaged in resync at all
6708 * 2 == checking that there is no conflict with another sync
6709 * 1 == like 2, but have yielded to allow conflicting resync to
6711 * other == active in resync - this many blocks
6713 * Before starting a resync we must have set curr_resync to
6714 * 2, and then checked that every "conflicting" array has curr_resync
6715 * less than ours. When we find one that is the same or higher
6716 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6717 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6718 * This will mean we have to start checking from the beginning again.
6723 mddev
->curr_resync
= 2;
6726 if (kthread_should_stop())
6727 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6729 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
6731 for_each_mddev(mddev2
, tmp
) {
6732 if (mddev2
== mddev
)
6734 if (!mddev
->parallel_resync
6735 && mddev2
->curr_resync
6736 && match_mddev_units(mddev
, mddev2
)) {
6738 if (mddev
< mddev2
&& mddev
->curr_resync
== 2) {
6739 /* arbitrarily yield */
6740 mddev
->curr_resync
= 1;
6741 wake_up(&resync_wait
);
6743 if (mddev
> mddev2
&& mddev
->curr_resync
== 1)
6744 /* no need to wait here, we can wait the next
6745 * time 'round when curr_resync == 2
6748 /* We need to wait 'interruptible' so as not to
6749 * contribute to the load average, and not to
6750 * be caught by 'softlockup'
6752 prepare_to_wait(&resync_wait
, &wq
, TASK_INTERRUPTIBLE
);
6753 if (!kthread_should_stop() &&
6754 mddev2
->curr_resync
>= mddev
->curr_resync
) {
6755 printk(KERN_INFO
"md: delaying %s of %s"
6756 " until %s has finished (they"
6757 " share one or more physical units)\n",
6758 desc
, mdname(mddev
), mdname(mddev2
));
6760 if (signal_pending(current
))
6761 flush_signals(current
);
6763 finish_wait(&resync_wait
, &wq
);
6766 finish_wait(&resync_wait
, &wq
);
6769 } while (mddev
->curr_resync
< 2);
6772 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
6773 /* resync follows the size requested by the personality,
6774 * which defaults to physical size, but can be virtual size
6776 max_sectors
= mddev
->resync_max_sectors
;
6777 mddev
->resync_mismatches
= 0;
6778 /* we don't use the checkpoint if there's a bitmap */
6779 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
6780 j
= mddev
->resync_min
;
6781 else if (!mddev
->bitmap
)
6782 j
= mddev
->recovery_cp
;
6784 } else if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
6785 max_sectors
= mddev
->dev_sectors
;
6787 /* recovery follows the physical size of devices */
6788 max_sectors
= mddev
->dev_sectors
;
6791 list_for_each_entry_rcu(rdev
, &mddev
->disks
, same_set
)
6792 if (rdev
->raid_disk
>= 0 &&
6793 !test_bit(Faulty
, &rdev
->flags
) &&
6794 !test_bit(In_sync
, &rdev
->flags
) &&
6795 rdev
->recovery_offset
< j
)
6796 j
= rdev
->recovery_offset
;
6800 printk(KERN_INFO
"md: %s of RAID array %s\n", desc
, mdname(mddev
));
6801 printk(KERN_INFO
"md: minimum _guaranteed_ speed:"
6802 " %d KB/sec/disk.\n", speed_min(mddev
));
6803 printk(KERN_INFO
"md: using maximum available idle IO bandwidth "
6804 "(but not more than %d KB/sec) for %s.\n",
6805 speed_max(mddev
), desc
);
6807 is_mddev_idle(mddev
, 1); /* this initializes IO event counters */
6810 for (m
= 0; m
< SYNC_MARKS
; m
++) {
6812 mark_cnt
[m
] = io_sectors
;
6815 mddev
->resync_mark
= mark
[last_mark
];
6816 mddev
->resync_mark_cnt
= mark_cnt
[last_mark
];
6819 * Tune reconstruction:
6821 window
= 32*(PAGE_SIZE
/512);
6822 printk(KERN_INFO
"md: using %dk window, over a total of %llu blocks.\n",
6823 window
/2,(unsigned long long) max_sectors
/2);
6825 atomic_set(&mddev
->recovery_active
, 0);
6830 "md: resuming %s of %s from checkpoint.\n",
6831 desc
, mdname(mddev
));
6832 mddev
->curr_resync
= j
;
6834 mddev
->curr_resync_completed
= j
;
6836 while (j
< max_sectors
) {
6841 if (!test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
6842 ((mddev
->curr_resync
> mddev
->curr_resync_completed
&&
6843 (mddev
->curr_resync
- mddev
->curr_resync_completed
)
6844 > (max_sectors
>> 4)) ||
6845 (j
- mddev
->curr_resync_completed
)*2
6846 >= mddev
->resync_max
- mddev
->curr_resync_completed
6848 /* time to update curr_resync_completed */
6850 wait_event(mddev
->recovery_wait
,
6851 atomic_read(&mddev
->recovery_active
) == 0);
6852 mddev
->curr_resync_completed
= j
;
6853 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
6854 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
6857 while (j
>= mddev
->resync_max
&& !kthread_should_stop()) {
6858 /* As this condition is controlled by user-space,
6859 * we can block indefinitely, so use '_interruptible'
6860 * to avoid triggering warnings.
6862 flush_signals(current
); /* just in case */
6863 wait_event_interruptible(mddev
->recovery_wait
,
6864 mddev
->resync_max
> j
6865 || kthread_should_stop());
6868 if (kthread_should_stop())
6871 sectors
= mddev
->pers
->sync_request(mddev
, j
, &skipped
,
6872 currspeed
< speed_min(mddev
));
6874 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
6878 if (!skipped
) { /* actual IO requested */
6879 io_sectors
+= sectors
;
6880 atomic_add(sectors
, &mddev
->recovery_active
);
6884 if (j
>1) mddev
->curr_resync
= j
;
6885 mddev
->curr_mark_cnt
= io_sectors
;
6886 if (last_check
== 0)
6887 /* this is the earliers that rebuilt will be
6888 * visible in /proc/mdstat
6890 md_new_event(mddev
);
6892 if (last_check
+ window
> io_sectors
|| j
== max_sectors
)
6895 last_check
= io_sectors
;
6897 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
6901 if (time_after_eq(jiffies
, mark
[last_mark
] + SYNC_MARK_STEP
)) {
6903 int next
= (last_mark
+1) % SYNC_MARKS
;
6905 mddev
->resync_mark
= mark
[next
];
6906 mddev
->resync_mark_cnt
= mark_cnt
[next
];
6907 mark
[next
] = jiffies
;
6908 mark_cnt
[next
] = io_sectors
- atomic_read(&mddev
->recovery_active
);
6913 if (kthread_should_stop())
6918 * this loop exits only if either when we are slower than
6919 * the 'hard' speed limit, or the system was IO-idle for
6921 * the system might be non-idle CPU-wise, but we only care
6922 * about not overloading the IO subsystem. (things like an
6923 * e2fsck being done on the RAID array should execute fast)
6928 currspeed
= ((unsigned long)(io_sectors
-mddev
->resync_mark_cnt
))/2
6929 /((jiffies
-mddev
->resync_mark
)/HZ
+1) +1;
6931 if (currspeed
> speed_min(mddev
)) {
6932 if ((currspeed
> speed_max(mddev
)) ||
6933 !is_mddev_idle(mddev
, 0)) {
6939 printk(KERN_INFO
"md: %s: %s done.\n",mdname(mddev
), desc
);
6941 * this also signals 'finished resyncing' to md_stop
6946 wait_event(mddev
->recovery_wait
, !atomic_read(&mddev
->recovery_active
));
6948 /* tell personality that we are finished */
6949 mddev
->pers
->sync_request(mddev
, max_sectors
, &skipped
, 1);
6951 if (!test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
) &&
6952 mddev
->curr_resync
> 2) {
6953 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
6954 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
6955 if (mddev
->curr_resync
>= mddev
->recovery_cp
) {
6957 "md: checkpointing %s of %s.\n",
6958 desc
, mdname(mddev
));
6959 mddev
->recovery_cp
= mddev
->curr_resync
;
6962 mddev
->recovery_cp
= MaxSector
;
6964 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
6965 mddev
->curr_resync
= MaxSector
;
6967 list_for_each_entry_rcu(rdev
, &mddev
->disks
, same_set
)
6968 if (rdev
->raid_disk
>= 0 &&
6969 mddev
->delta_disks
>= 0 &&
6970 !test_bit(Faulty
, &rdev
->flags
) &&
6971 !test_bit(In_sync
, &rdev
->flags
) &&
6972 rdev
->recovery_offset
< mddev
->curr_resync
)
6973 rdev
->recovery_offset
= mddev
->curr_resync
;
6977 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
6980 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
6981 /* We completed so min/max setting can be forgotten if used. */
6982 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
6983 mddev
->resync_min
= 0;
6984 mddev
->resync_max
= MaxSector
;
6985 } else if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
6986 mddev
->resync_min
= mddev
->curr_resync_completed
;
6987 mddev
->curr_resync
= 0;
6988 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
6989 mddev
->curr_resync_completed
= 0;
6990 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
6991 wake_up(&resync_wait
);
6992 set_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
6993 md_wakeup_thread(mddev
->thread
);
6998 * got a signal, exit.
7001 "md: md_do_sync() got signal ... exiting\n");
7002 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7006 EXPORT_SYMBOL_GPL(md_do_sync
);
7009 static int remove_and_add_spares(mddev_t
*mddev
)
7014 mddev
->curr_resync_completed
= 0;
7016 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
7017 if (rdev
->raid_disk
>= 0 &&
7018 !test_bit(Blocked
, &rdev
->flags
) &&
7019 (test_bit(Faulty
, &rdev
->flags
) ||
7020 ! test_bit(In_sync
, &rdev
->flags
)) &&
7021 atomic_read(&rdev
->nr_pending
)==0) {
7022 if (mddev
->pers
->hot_remove_disk(
7023 mddev
, rdev
->raid_disk
)==0) {
7025 sprintf(nm
,"rd%d", rdev
->raid_disk
);
7026 sysfs_remove_link(&mddev
->kobj
, nm
);
7027 rdev
->raid_disk
= -1;
7031 if (mddev
->degraded
&& ! mddev
->ro
&& !mddev
->recovery_disabled
) {
7032 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
7033 if (rdev
->raid_disk
>= 0 &&
7034 !test_bit(In_sync
, &rdev
->flags
) &&
7035 !test_bit(Blocked
, &rdev
->flags
))
7037 if (rdev
->raid_disk
< 0
7038 && !test_bit(Faulty
, &rdev
->flags
)) {
7039 rdev
->recovery_offset
= 0;
7041 hot_add_disk(mddev
, rdev
) == 0) {
7043 sprintf(nm
, "rd%d", rdev
->raid_disk
);
7044 if (sysfs_create_link(&mddev
->kobj
,
7046 /* failure here is OK */;
7048 md_new_event(mddev
);
7049 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
7058 static void reap_sync_thread(mddev_t
*mddev
)
7062 /* resync has finished, collect result */
7063 md_unregister_thread(mddev
->sync_thread
);
7064 mddev
->sync_thread
= NULL
;
7065 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
) &&
7066 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
7068 /* activate any spares */
7069 if (mddev
->pers
->spare_active(mddev
))
7070 sysfs_notify(&mddev
->kobj
, NULL
,
7073 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
7074 mddev
->pers
->finish_reshape
)
7075 mddev
->pers
->finish_reshape(mddev
);
7076 md_update_sb(mddev
, 1);
7078 /* if array is no-longer degraded, then any saved_raid_disk
7079 * information must be scrapped
7081 if (!mddev
->degraded
)
7082 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
7083 rdev
->saved_raid_disk
= -1;
7085 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7086 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7087 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7088 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
7089 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7090 /* flag recovery needed just to double check */
7091 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7092 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7093 md_new_event(mddev
);
7097 * This routine is regularly called by all per-raid-array threads to
7098 * deal with generic issues like resync and super-block update.
7099 * Raid personalities that don't have a thread (linear/raid0) do not
7100 * need this as they never do any recovery or update the superblock.
7102 * It does not do any resync itself, but rather "forks" off other threads
7103 * to do that as needed.
7104 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7105 * "->recovery" and create a thread at ->sync_thread.
7106 * When the thread finishes it sets MD_RECOVERY_DONE
7107 * and wakeups up this thread which will reap the thread and finish up.
7108 * This thread also removes any faulty devices (with nr_pending == 0).
7110 * The overall approach is:
7111 * 1/ if the superblock needs updating, update it.
7112 * 2/ If a recovery thread is running, don't do anything else.
7113 * 3/ If recovery has finished, clean up, possibly marking spares active.
7114 * 4/ If there are any faulty devices, remove them.
7115 * 5/ If array is degraded, try to add spares devices
7116 * 6/ If array has spares or is not in-sync, start a resync thread.
7118 void md_check_recovery(mddev_t
*mddev
)
7121 bitmap_daemon_work(mddev
);
7126 if (signal_pending(current
)) {
7127 if (mddev
->pers
->sync_request
&& !mddev
->external
) {
7128 printk(KERN_INFO
"md: %s in immediate safe mode\n",
7130 mddev
->safemode
= 2;
7132 flush_signals(current
);
7135 if (mddev
->ro
&& !test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
7138 (mddev
->flags
& ~ (1<<MD_CHANGE_PENDING
)) ||
7139 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
) ||
7140 test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
) ||
7141 (mddev
->external
== 0 && mddev
->safemode
== 1) ||
7142 (mddev
->safemode
== 2 && ! atomic_read(&mddev
->writes_pending
)
7143 && !mddev
->in_sync
&& mddev
->recovery_cp
== MaxSector
)
7147 if (mddev_trylock(mddev
)) {
7151 /* Only thing we do on a ro array is remove
7154 remove_and_add_spares(mddev
);
7155 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7159 if (!mddev
->external
) {
7161 spin_lock_irq(&mddev
->write_lock
);
7162 if (mddev
->safemode
&&
7163 !atomic_read(&mddev
->writes_pending
) &&
7165 mddev
->recovery_cp
== MaxSector
) {
7168 set_bit(MD_CHANGE_CLEAN
, &mddev
->flags
);
7170 if (mddev
->safemode
== 1)
7171 mddev
->safemode
= 0;
7172 spin_unlock_irq(&mddev
->write_lock
);
7174 sysfs_notify_dirent_safe(mddev
->sysfs_state
);
7178 md_update_sb(mddev
, 0);
7180 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) &&
7181 !test_bit(MD_RECOVERY_DONE
, &mddev
->recovery
)) {
7182 /* resync/recovery still happening */
7183 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7186 if (mddev
->sync_thread
) {
7187 reap_sync_thread(mddev
);
7190 /* Set RUNNING before clearing NEEDED to avoid
7191 * any transients in the value of "sync_action".
7193 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7194 clear_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7195 /* Clear some bits that don't mean anything, but
7198 clear_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
7199 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
7201 if (test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
7203 /* no recovery is running.
7204 * remove any failed drives, then
7205 * add spares if possible.
7206 * Spare are also removed and re-added, to allow
7207 * the personality to fail the re-add.
7210 if (mddev
->reshape_position
!= MaxSector
) {
7211 if (mddev
->pers
->check_reshape
== NULL
||
7212 mddev
->pers
->check_reshape(mddev
) != 0)
7213 /* Cannot proceed */
7215 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7216 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7217 } else if ((spares
= remove_and_add_spares(mddev
))) {
7218 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7219 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7220 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
7221 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7222 } else if (mddev
->recovery_cp
< MaxSector
) {
7223 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7224 clear_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
7225 } else if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
7226 /* nothing to be done ... */
7229 if (mddev
->pers
->sync_request
) {
7230 if (spares
&& mddev
->bitmap
&& ! mddev
->bitmap
->file
) {
7231 /* We are adding a device or devices to an array
7232 * which has the bitmap stored on all devices.
7233 * So make sure all bitmap pages get written
7235 bitmap_write_all(mddev
->bitmap
);
7237 mddev
->sync_thread
= md_register_thread(md_do_sync
,
7240 if (!mddev
->sync_thread
) {
7241 printk(KERN_ERR
"%s: could not start resync"
7244 /* leave the spares where they are, it shouldn't hurt */
7245 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7246 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7247 clear_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7248 clear_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
7249 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7251 md_wakeup_thread(mddev
->sync_thread
);
7252 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7253 md_new_event(mddev
);
7256 if (!mddev
->sync_thread
) {
7257 clear_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7258 if (test_and_clear_bit(MD_RECOVERY_RECOVER
,
7260 if (mddev
->sysfs_action
)
7261 sysfs_notify_dirent_safe(mddev
->sysfs_action
);
7263 mddev_unlock(mddev
);
7267 void md_wait_for_blocked_rdev(mdk_rdev_t
*rdev
, mddev_t
*mddev
)
7269 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
7270 wait_event_timeout(rdev
->blocked_wait
,
7271 !test_bit(Blocked
, &rdev
->flags
),
7272 msecs_to_jiffies(5000));
7273 rdev_dec_pending(rdev
, mddev
);
7275 EXPORT_SYMBOL(md_wait_for_blocked_rdev
);
7277 static int md_notify_reboot(struct notifier_block
*this,
7278 unsigned long code
, void *x
)
7280 struct list_head
*tmp
;
7283 if ((code
== SYS_DOWN
) || (code
== SYS_HALT
) || (code
== SYS_POWER_OFF
)) {
7285 printk(KERN_INFO
"md: stopping all md devices.\n");
7287 for_each_mddev(mddev
, tmp
)
7288 if (mddev_trylock(mddev
)) {
7289 /* Force a switch to readonly even array
7290 * appears to still be in use. Hence
7293 md_set_readonly(mddev
, 100);
7294 mddev_unlock(mddev
);
7297 * certain more exotic SCSI devices are known to be
7298 * volatile wrt too early system reboots. While the
7299 * right place to handle this issue is the given
7300 * driver, we do want to have a safe RAID driver ...
7307 static struct notifier_block md_notifier
= {
7308 .notifier_call
= md_notify_reboot
,
7310 .priority
= INT_MAX
, /* before any real devices */
7313 static void md_geninit(void)
7315 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t
));
7317 proc_create("mdstat", S_IRUGO
, NULL
, &md_seq_fops
);
7320 static int __init
md_init(void)
7324 md_wq
= alloc_workqueue("md", WQ_RESCUER
, 0);
7328 md_misc_wq
= alloc_workqueue("md_misc", 0, 0);
7332 if ((ret
= register_blkdev(MD_MAJOR
, "md")) < 0)
7335 if ((ret
= register_blkdev(0, "mdp")) < 0)
7339 blk_register_region(MKDEV(MD_MAJOR
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
7340 md_probe
, NULL
, NULL
);
7341 blk_register_region(MKDEV(mdp_major
, 0), 1UL<<MINORBITS
, THIS_MODULE
,
7342 md_probe
, NULL
, NULL
);
7344 register_reboot_notifier(&md_notifier
);
7345 raid_table_header
= register_sysctl_table(raid_root_table
);
7351 unregister_blkdev(MD_MAJOR
, "md");
7353 destroy_workqueue(md_misc_wq
);
7355 destroy_workqueue(md_wq
);
7363 * Searches all registered partitions for autorun RAID arrays
7367 static LIST_HEAD(all_detected_devices
);
7368 struct detected_devices_node
{
7369 struct list_head list
;
7373 void md_autodetect_dev(dev_t dev
)
7375 struct detected_devices_node
*node_detected_dev
;
7377 node_detected_dev
= kzalloc(sizeof(*node_detected_dev
), GFP_KERNEL
);
7378 if (node_detected_dev
) {
7379 node_detected_dev
->dev
= dev
;
7380 list_add_tail(&node_detected_dev
->list
, &all_detected_devices
);
7382 printk(KERN_CRIT
"md: md_autodetect_dev: kzalloc failed"
7383 ", skipping dev(%d,%d)\n", MAJOR(dev
), MINOR(dev
));
7388 static void autostart_arrays(int part
)
7391 struct detected_devices_node
*node_detected_dev
;
7393 int i_scanned
, i_passed
;
7398 printk(KERN_INFO
"md: Autodetecting RAID arrays.\n");
7400 while (!list_empty(&all_detected_devices
) && i_scanned
< INT_MAX
) {
7402 node_detected_dev
= list_entry(all_detected_devices
.next
,
7403 struct detected_devices_node
, list
);
7404 list_del(&node_detected_dev
->list
);
7405 dev
= node_detected_dev
->dev
;
7406 kfree(node_detected_dev
);
7407 rdev
= md_import_device(dev
,0, 90);
7411 if (test_bit(Faulty
, &rdev
->flags
)) {
7415 set_bit(AutoDetected
, &rdev
->flags
);
7416 list_add(&rdev
->same_set
, &pending_raid_disks
);
7420 printk(KERN_INFO
"md: Scanned %d and added %d devices.\n",
7421 i_scanned
, i_passed
);
7423 autorun_devices(part
);
7426 #endif /* !MODULE */
7428 static __exit
void md_exit(void)
7431 struct list_head
*tmp
;
7433 blk_unregister_region(MKDEV(MD_MAJOR
,0), 1U << MINORBITS
);
7434 blk_unregister_region(MKDEV(mdp_major
,0), 1U << MINORBITS
);
7436 unregister_blkdev(MD_MAJOR
,"md");
7437 unregister_blkdev(mdp_major
, "mdp");
7438 unregister_reboot_notifier(&md_notifier
);
7439 unregister_sysctl_table(raid_table_header
);
7440 remove_proc_entry("mdstat", NULL
);
7441 for_each_mddev(mddev
, tmp
) {
7442 export_array(mddev
);
7443 mddev
->hold_active
= 0;
7445 destroy_workqueue(md_misc_wq
);
7446 destroy_workqueue(md_wq
);
7449 subsys_initcall(md_init
);
7450 module_exit(md_exit
)
7452 static int get_ro(char *buffer
, struct kernel_param
*kp
)
7454 return sprintf(buffer
, "%d", start_readonly
);
7456 static int set_ro(const char *val
, struct kernel_param
*kp
)
7459 int num
= simple_strtoul(val
, &e
, 10);
7460 if (*val
&& (*e
== '\0' || *e
== '\n')) {
7461 start_readonly
= num
;
7467 module_param_call(start_ro
, set_ro
, get_ro
, NULL
, S_IRUSR
|S_IWUSR
);
7468 module_param(start_dirty_degraded
, int, S_IRUGO
|S_IWUSR
);
7470 module_param_call(new_array
, add_named_array
, NULL
, NULL
, S_IWUSR
);
7472 EXPORT_SYMBOL(register_md_personality
);
7473 EXPORT_SYMBOL(unregister_md_personality
);
7474 EXPORT_SYMBOL(md_error
);
7475 EXPORT_SYMBOL(md_done_sync
);
7476 EXPORT_SYMBOL(md_write_start
);
7477 EXPORT_SYMBOL(md_write_end
);
7478 EXPORT_SYMBOL(md_register_thread
);
7479 EXPORT_SYMBOL(md_unregister_thread
);
7480 EXPORT_SYMBOL(md_wakeup_thread
);
7481 EXPORT_SYMBOL(md_check_recovery
);
7482 MODULE_LICENSE("GPL");
7483 MODULE_DESCRIPTION("MD RAID framework");
7485 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR
);